|
|
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;
51: push(@rep_names, $r);
52: }
53: parse_line(@rep_names);
54: }
55: } else {
56: # default
57: parse_line(@names);
58: }
59: }
60: close(IN);
61:
62: # 文字列として出力
63: $hdrnew = "";
64: $srcnew = "";
65:
66: $hdrnew .= "// Generated from ${0}\n";
67: $hdrnew .= "\n";
68: $hdrnew .= $mon_defs;
69: $hdrnew .= "\n";
70: $hdrnew .= $sub_defs;
71: $hdrnew .= "\n";
72: $hdrnew .= "\tID_MONITOR_START = ${first_mon_id},\n";
73: $hdrnew .= "\tID_MONITOR_END = ${last_mon_id},\n";
74: $hdrnew .= "\tID_MONITOR_MAX = ID_MONITOR_END + 1,\n";
75: $hdrnew .= "\tID_SUBWIN_START = ${first_sub_id},\n";
76: $hdrnew .= "\tID_SUBWIN_END = ${last_sub_id},\n";
77: $hdrnew .= "\tID_SUBWIN_MAX = ID_SUBWIN_END + 1,\n";
78: $hdrnew .= "\n";
79: $hdrnew .= "\t// これは enum じゃないけど自動生成の都合でここに置く\n";
1.1.1.3 ! root 80: foreach $str (@maxes) {
! 81: $hdrnew .= "\t${str},\n";
! 82: }
1.1 root 83:
84: $srcnew .= "// Generated from ${0}\n";
85: $srcnew .= "\n";
86: $srcnew .= "#include \"monitor.h\"\n";
87: $srcnew .= "\n";
88: $srcnew .= "/*static*/ const std::vector<MonitorManager::MonitorInfo>\n";
89: $srcnew .= "MonitorManager::info {\n";
90: $srcnew .= $mon_body;
91: $srcnew .= "\n";
92: $srcnew .= $sub_body;
93: $srcnew .= "};\n";
1.1.1.3 ! root 94: $srcnew .= "\n";
! 95: $srcnew .= "/*static*/ const std::vector<std::pair<int, const char *>>\n";
! 96: $srcnew .= "Monitor::idtext {\n";
! 97: $srcnew .= $label_body;
! 98: $srcnew .= "};\n";
1.1 root 99:
100: # 更新があれば書き出す
101: do_update($hdrfile, $hdrnew);
102: do_update($srcfile, $srcnew);
103:
104: exit 0;
105: }
106:
107: sub parse_line()
108: {
109: local @names = @_;
110: local $id = shift(@names);
111: local $type = shift(@names);
112: local $flag = shift(@names);
113: local $first;
114:
115: if ($#names == -1) {
116: # If <names...> are ommited, use ID instead.
117: $first = lc $id;
118: } else {
119: # If <names...> are specified, use it.
120: $first = shift(@names);
121: }
122:
123: if ($type eq "m") {
124: $id = "ID_MONITOR_${id}";
125: if ($first_mon_id eq "") {
126: $first_mon_id = $id;
127: }
128: $last_mon_id = $id;
129: } else {
130: $id = "ID_SUBWIN_${id}";
131: if ($first_sub_id eq "") {
132: $first_sub_id = $id;
133: }
134: $last_sub_id = $id;
135: }
136:
1.1.1.3 ! root 137: # フラグは '|' で並べることが出来る
! 138: @flags = split(/\|/, $flag);
! 139: @caps = map { "VMCap::".$_ } @flags;
! 140: $cap = join('|', @caps);
! 141:
1.1 root 142: $defs = "\t${id},\n";
1.1.1.3 ! root 143: $body = "\t{ ${id},\t${cap},\t{ \"${first}\"";
1.1 root 144: foreach $name (@names) {
145: $body .= ", \"${name}\"";
146: }
147: $body .= " } },\n";
148:
149: if ($type eq "m") {
150: $mon_defs .= $defs;
151: $mon_body .= $body;
152: } else {
153: $sub_defs .= $defs;
154: $sub_body .= $body;
155: }
1.1.1.3 ! root 156:
! 157: # デバッグ用の ID と文字列の対応表
! 158: $label_body .= "\t{ ${id}, \"${id}\" },\n";
1.1 root 159: }
160:
161: sub do_update()
162: {
163: local $filename = $_[0];
164: local $filebody = $_[1];
165: local $old;
166: local @contents;
167:
168: # まず出力先ファイルを文字列として読み込む
169: $old = "";
170: if (open(IN, $filename)) {
171: @contents = <IN>;
172: $old = join("", @contents);
173: close(IN);
174: }
175:
176: # 全文を比較して変更があれば出力
177: if ($filebody ne $old) {
178: open(OUT, ">${filename}") || die "Cannot open ${filename}\n";
179: print OUT $filebody;
180: close(OUT);
181: print "Updated ${filename}\n";
182: }
183: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.