|
|
1.1 root 1: #
2: # nono
3: # Copyright (C) 2021 nono project
4: # Licensed under nono-license.txt
5: #
6:
7: # monitor_id.txt から monitor_id.h, monitor_id.cpp を作る。
8: #
9: # usage: perl monitor_id.pl monitor_id.txt
10:
11: {
12: $defs = "";
13: $body = "";
14: $first_mon_id = "";
15: $first_sub_id = "";
1.1.1.3 root 16: @maxes = ();
1.1 root 17:
18: # $#ARGV は引数の数-1。ここでいう引数は monitor_id.pl の後ろから。
19: if ($#ARGV != 0) {
20: die "usage: $0 <id.txt>\n";
21: }
22:
23: # ファイル名
24: $infile = $ARGV[0];
25: $hdrfile = $infile;
26: $srcfile = $infile;
27: $hdrfile =~ s/txt$/h/;
28: $srcfile =~ s/txt$/cpp/;
29:
30: open(IN, $infile) || die "Cannot open infile: ${infile}\n";
31: while (<IN>) {
32: # Remove comments.
33: chomp;
34: s/#.*$//;
35: next if /^$/;
36:
37: @names = split(/\s+/);
38:
1.1.1.3 root 39: if ($names[0] =~ /([^%]+)%(\d+)/) {
40: # %n なら 0..(n-1) に展開
41: $basename = $1;
42: $atnum = $2 + 0;
1.1 root 43:
1.1.1.3 root 44: # モニタの最大数はここが一次情報なので、これから定数を作る
45: push(@maxes, "MAX_${basename}_MONITOR = ${atnum}");
1.1 root 46: for ($i = 0; $i < $atnum; $i++) {
47: @rep_names = ();
48: foreach $n (@names) {
49: $r = $n;
50: $r =~ s/%${atnum}/${i}/g;
1.1.1.4 root 51: $r =~ s/%/${i}/g;
1.1 root 52: push(@rep_names, $r);
53: }
54: parse_line(@rep_names);
55: }
56: } else {
57: # default
58: parse_line(@names);
59: }
60: }
61: close(IN);
62:
63: # 文字列として出力
64: $hdrnew = "";
65: $srcnew = "";
66:
67: $hdrnew .= "// Generated from ${0}\n";
68: $hdrnew .= "\n";
69: $hdrnew .= $mon_defs;
70: $hdrnew .= "\n";
71: $hdrnew .= $sub_defs;
72: $hdrnew .= "\n";
73: $hdrnew .= "\tID_MONITOR_START = ${first_mon_id},\n";
74: $hdrnew .= "\tID_MONITOR_END = ${last_mon_id},\n";
75: $hdrnew .= "\tID_MONITOR_MAX = ID_MONITOR_END + 1,\n";
76: $hdrnew .= "\tID_SUBWIN_START = ${first_sub_id},\n";
77: $hdrnew .= "\tID_SUBWIN_END = ${last_sub_id},\n";
78: $hdrnew .= "\tID_SUBWIN_MAX = ID_SUBWIN_END + 1,\n";
79: $hdrnew .= "\n";
80: $hdrnew .= "\t// これは enum じゃないけど自動生成の都合でここに置く\n";
1.1.1.3 root 81: foreach $str (@maxes) {
82: $hdrnew .= "\t${str},\n";
83: }
1.1 root 84:
85: $srcnew .= "// Generated from ${0}\n";
86: $srcnew .= "\n";
87: $srcnew .= "#include \"monitor.h\"\n";
88: $srcnew .= "\n";
89: $srcnew .= "/*static*/ const std::vector<MonitorManager::MonitorInfo>\n";
90: $srcnew .= "MonitorManager::info {\n";
91: $srcnew .= $mon_body;
92: $srcnew .= "\n";
93: $srcnew .= $sub_body;
94: $srcnew .= "};\n";
1.1.1.3 root 95: $srcnew .= "\n";
1.1.1.5 ! root 96: $srcnew .= "/*static*/ const std::vector<std::pair<uint, const char *>>\n";
1.1.1.3 root 97: $srcnew .= "Monitor::idtext {\n";
98: $srcnew .= $label_body;
99: $srcnew .= "};\n";
1.1 root 100:
101: # 更新があれば書き出す
102: do_update($hdrfile, $hdrnew);
103: do_update($srcfile, $srcnew);
104:
105: exit 0;
106: }
107:
108: sub parse_line()
109: {
110: local @names = @_;
111: local $id = shift(@names);
112: local $type = shift(@names);
113: local $flag = shift(@names);
114: local $first;
115:
116: if ($#names == -1) {
117: # If <names...> are ommited, use ID instead.
118: $first = lc $id;
119: } else {
120: # If <names...> are specified, use it.
121: $first = shift(@names);
122: }
123:
124: if ($type eq "m") {
125: $id = "ID_MONITOR_${id}";
126: if ($first_mon_id eq "") {
127: $first_mon_id = $id;
128: }
129: $last_mon_id = $id;
130: } else {
131: $id = "ID_SUBWIN_${id}";
132: if ($first_sub_id eq "") {
133: $first_sub_id = $id;
134: }
135: $last_sub_id = $id;
136: }
137:
1.1.1.4 root 138: if ($flag eq ".") {
139: # フラグ省略時(モニタの場合)は vmcap を参照することはないはず
140: $flag = "NONE";
141: }
142: # フラグは '|' で並べることが出来る。
1.1.1.3 root 143: @flags = split(/\|/, $flag);
144: @caps = map { "VMCap::".$_ } @flags;
145: $cap = join('|', @caps);
146:
1.1 root 147: $defs = "\t${id},\n";
1.1.1.3 root 148: $body = "\t{ ${id},\t${cap},\t{ \"${first}\"";
1.1 root 149: foreach $name (@names) {
150: $body .= ", \"${name}\"";
151: }
152: $body .= " } },\n";
153:
154: if ($type eq "m") {
155: $mon_defs .= $defs;
156: $mon_body .= $body;
157: } else {
158: $sub_defs .= $defs;
159: $sub_body .= $body;
160: }
1.1.1.3 root 161:
162: # デバッグ用の ID と文字列の対応表
163: $label_body .= "\t{ ${id}, \"${id}\" },\n";
1.1 root 164: }
165:
166: sub do_update()
167: {
168: local $filename = $_[0];
169: local $filebody = $_[1];
170: local $old;
171: local @contents;
172:
173: # まず出力先ファイルを文字列として読み込む
174: $old = "";
175: if (open(IN, $filename)) {
176: @contents = <IN>;
177: $old = join("", @contents);
178: close(IN);
179: }
180:
181: # 全文を比較して変更があれば出力
182: if ($filebody ne $old) {
183: open(OUT, ">${filename}") || die "Cannot open ${filename}\n";
184: print OUT $filebody;
185: close(OUT);
186: print "Updated ${filename}\n";
187: }
188: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.