Annotation of nono/lib/monitor_id.pl, revision 1.1

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 = "";
        !            16: 
        !            17:        # $#ARGV は引数の数-1。ここでいう引数は monitor_id.pl の後ろから。
        !            18:        if ($#ARGV != 0) {
        !            19:                die "usage: $0 <id.txt>\n";
        !            20:        }
        !            21: 
        !            22:        # ファイル名
        !            23:        $infile = $ARGV[0];
        !            24:        $hdrfile = $infile;
        !            25:        $srcfile = $infile;
        !            26:        $hdrfile =~ s/txt$/h/;
        !            27:        $srcfile =~ s/txt$/cpp/;
        !            28: 
        !            29:        open(IN, $infile) || die "Cannot open infile: ${infile}\n";
        !            30:        while (<IN>) {
        !            31:                # Remove comments.
        !            32:                chomp;
        !            33:                s/#.*$//;
        !            34:                next if /^$/;
        !            35: 
        !            36:                @names = split(/\s+/);
        !            37: 
        !            38:                # MEMDUMP の最大数はここが一次情報なので、これから定数を作る
        !            39:                if ($names[0] =~ /^MEMDUMP%(\d+)/) {
        !            40:                        $max_memdump = $1 + 0;
        !            41:                }
        !            42: 
        !            43:                if ($names[0] =~ /%(\d+)/) {
        !            44:                        # %n なら 0..n に展開
        !            45:                        $atnum = $1 + 0;
        !            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";
        !            80:        $hdrnew .= "\tMAX_MEMDUMP_MONITOR = ${max_memdump},\n";
        !            81: 
        !            82:        $srcnew .= "// Generated from ${0}\n";
        !            83:        $srcnew .= "\n";
        !            84:        $srcnew .= "#include \"monitor.h\"\n";
        !            85:        $srcnew .= "\n";
        !            86:        $srcnew .= "/*static*/ const std::vector<MonitorManager::MonitorInfo>\n";
        !            87:        $srcnew .= "MonitorManager::info {\n";
        !            88:        $srcnew .= $mon_body;
        !            89:        $srcnew .= "\n";
        !            90:        $srcnew .= $sub_body;
        !            91:        $srcnew .= "};\n";
        !            92: 
        !            93:        # 更新があれば書き出す
        !            94:        do_update($hdrfile, $hdrnew);
        !            95:        do_update($srcfile, $srcnew);
        !            96: 
        !            97:        exit 0;
        !            98: }
        !            99: 
        !           100: sub parse_line()
        !           101: {
        !           102:        local @names = @_;
        !           103:        local $id = shift(@names);
        !           104:        local $type = shift(@names);
        !           105:        local $flag = shift(@names);
        !           106:        local $first;
        !           107: 
        !           108:        if ($#names == -1) {
        !           109:                # If <names...> are ommited, use ID instead.
        !           110:                $first = lc $id;
        !           111:        } else {
        !           112:                # If <names...> are specified, use it.
        !           113:                $first = shift(@names);
        !           114:        }
        !           115: 
        !           116:        if ($type eq "m") {
        !           117:                $id = "ID_MONITOR_${id}";
        !           118:                if ($first_mon_id eq "") {
        !           119:                        $first_mon_id = $id;
        !           120:                }
        !           121:                $last_mon_id = $id;
        !           122:        } else {
        !           123:                $id = "ID_SUBWIN_${id}";
        !           124:                if ($first_sub_id eq "") {
        !           125:                        $first_sub_id = $id;
        !           126:                }
        !           127:                $last_sub_id = $id;
        !           128:        }
        !           129: 
        !           130:        $defs = "\t${id},\n";
        !           131:        $body = "\t{ ${id},\tVMF_${flag},\t{ \"${first}\"";
        !           132:        foreach $name (@names) {
        !           133:                $body .= ", \"${name}\"";
        !           134:        }
        !           135:        $body .= " } },\n";
        !           136: 
        !           137:        if ($type eq "m") {
        !           138:                $mon_defs .= $defs;
        !           139:                $mon_body .= $body;
        !           140:        } else {
        !           141:                $sub_defs .= $defs;
        !           142:                $sub_body .= $body;
        !           143:        }
        !           144: }
        !           145: 
        !           146: sub do_update()
        !           147: {
        !           148:        local $filename = $_[0];
        !           149:        local $filebody = $_[1];
        !           150:        local $old;
        !           151:        local @contents;
        !           152: 
        !           153:        # まず出力先ファイルを文字列として読み込む
        !           154:        $old = "";
        !           155:        if (open(IN, $filename)) {
        !           156:                @contents = <IN>;
        !           157:                $old = join("", @contents);
        !           158:                close(IN);
        !           159:        }
        !           160: 
        !           161:        # 全文を比較して変更があれば出力
        !           162:        if ($filebody ne $old) {
        !           163:                open(OUT, ">${filename}") || die "Cannot open ${filename}\n";
        !           164:                print OUT $filebody;
        !           165:                close(OUT);
        !           166:                print "Updated ${filename}\n";
        !           167:        }
        !           168: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.