バージョン表示機能を追加
[YACASL2.git] / src / comet2.c
index af4bfb8..36060d3 100644 (file)
@@ -6,6 +6,7 @@
 #include "exec.h"
 #include "cmem.h"
 #include "cerr.h"
+#include "package.h"
 
 /**
  * comet2コマンドのオプション
@@ -17,6 +18,7 @@ static struct option longopts[] = {
     {"dump", no_argument, NULL, 'd'},
     {"memorysize", required_argument, NULL, 'M'},
     {"clocks", required_argument, NULL, 'C'},
+    { "version", no_argument, NULL, 'v' },
     {"help", no_argument, NULL, 'h'},
     {0, 0, 0, 0},
 };
@@ -27,15 +29,16 @@ static struct option longopts[] = {
 int main(int argc, char *argv[])
 {
     int memsize = DEFAULT_MEMSIZE, clocks = DEFAULT_CLOCKS;
-    int opt, status = 0;
-    const char *usage = "Usage: %s [-tTdh] [-M <MEMORYSIZE>] [-C <CLOCKS>] FILE\n";
+    int opt, stat = 0;
+    const char *version = PACKAGE_VERSION,  *cmdversion = "comet2 of YACASL2 version %s\n";
+    const char *usage = "Usage: %s [-tTdvh] [-M <MEMORYSIZE>] [-C <CLOCKS>] FILE\n";
 
     cerr_init();
     addcerrlist_load();
     addcerrlist_exec();
 
     /* オプションの処理 */
-    while((opt = getopt_long(argc, argv, "tTdM:C:h", longopts, NULL)) != -1) {
+    while((opt = getopt_long(argc, argv, "tTdM:C:vh", longopts, NULL)) != -1) {
         switch(opt) {
         case 't':
             execmode.trace = true;
@@ -53,33 +56,32 @@ int main(int argc, char *argv[])
         case 'C':
             clocks = atoi(optarg);
             break;
+        case 'v':
+            fprintf(stdout, cmdversion, version);
+            return 0;
         case 'h':
             fprintf(stdout, usage, argv[0]);
             return 0;
         case '?':
             fprintf(stderr, usage, argv[0]);
-            exit(-1);
+            exit(1);
         }
     }
     if(argv[optind] == NULL) {
-        setcerr(211, NULL);    /* object file not specified */
+        setcerr(211, "");    /* object file not specified */
         fprintf(stderr, "comet2 error - %d: %s\n", cerr->num, cerr->msg);
-        exit(-1);
+        exit(1);
     }
     /* COMET II仮想マシンのリセット */
     reset(memsize, clocks);
     execptr->start = 0;
     if(loadassemble(argv[optind]) == true) {
-        create_code_type();    /* タイプがキーの命令ハッシュ表を作成 */
         exec();                /* プログラム実行 */
-        free_code_type();      /* タイプがキーの命令ハッシュ表を解放 */
     }
     /* COMET II仮想マシンのシャットダウン */
     shutdown();
-    if(cerr->num > 0) {
-        status = -1;
-    }
+    stat = (cerr->num == 0) ? 0 : -1;
     /* エラーの解放 */
     freecerr();
-    return status;
+    return stat;
 }