バージョンアップ
[YACASL2.git] / src / comet2.c
index 3d429d1..c821096 100644 (file)
@@ -1,11 +1,5 @@
-#include <stdio.h>
-#include <stdlib.h>
-#define _GNU_SOURCE
-#include <getopt.h>
-
+#include "package.h"
 #include "exec.h"
-#include "cmem.h"
-#include "cerr.h"
 
 /**
  * comet2コマンドのオプション
@@ -15,34 +9,53 @@ static struct option longopts[] = {
     {"tracearithmetic", no_argument, NULL, 't'},
     {"tracelogical", no_argument, NULL, 'T'},
     {"dump", no_argument, NULL, 'd'},
+    {"monitor", no_argument, NULL, 'm'},
     {"memorysize", required_argument, NULL, 'M'},
     {"clocks", required_argument, NULL, 'C'},
+    {"version", no_argument, NULL, 'v' },
     {"help", no_argument, NULL, 'h'},
     {0, 0, 0, 0},
 };
 
+
 /**
- * comet2コマンドのエラー
+ * @brief comet2のエラー定義
  */
-static CERR cerr_comet2[] = {
-    { 208, "object file is not specified" },
+CERR cerr_comet2[] = {
+    { 127, "invalid option" },
 };
 
+void addcerrlist_comet2()
+{
+    addcerrlist(ARRAYSIZE(cerr_comet2), cerr_comet2);
+}
+
 /**
- * comet2コマンドのメイン
+ * @brief comet2コマンドのメイン
+ *
+ * @return 正常終了時は0、異常終了時は1
+ *
+ * @param argc コマンドライン引数の数
+ * @param *argv[] コマンドライン引数の配列
  */
 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 memsize = DEFAULT_MEMSIZE;
+    int clocks = DEFAULT_CLOCKS;
+    int opt = 0;
+    int stat = 0;
+    const char *version = PACKAGE_VERSION;
+    const char *cmdversion = "comet2 of YACASL2 version %s\n";
+    const char *usage = "Usage: %s [-tTdmvh] [-M <MEMORYSIZE>] [-C <CLOCKS>] FILE\n";
 
+    /* エラーの定義 */
     cerr_init();
-    addcerrlist(ARRAYSIZE(cerr_comet2), cerr_comet2);
+    addcerrlist_comet2();
+    addcerrlist_load();
     addcerrlist_exec();
 
     /* オプションの処理 */
-    while((opt = getopt_long(argc, argv, "tTdM:C:h", longopts, NULL)) != -1) {
+    while((opt = getopt_long(argc, argv, "tTdmM:C:vh", longopts, NULL)) != -1) {
         switch(opt) {
         case 't':
             execmode.trace = true;
@@ -54,39 +67,43 @@ int main(int argc, char *argv[])
         case 'd':
             execmode.dump = true;
             break;
+        case 'm':
+            execmode.monitor = true;
+            break;
         case 'M':
             memsize = atoi(optarg);
             break;
         case 'C':
             clocks = atoi(optarg);
             break;
+        case 'v':
+            fprintf(stdout, cmdversion, version);
+            goto comet2fin;
         case 'h':
             fprintf(stdout, usage, argv[0]);
-            return 0;
+            goto comet2fin;
         case '?':
             fprintf(stderr, usage, argv[0]);
-            exit(-1);
+            setcerr(212, "");    /* invalid option */
+            goto comet2fin;
         }
     }
     if(argv[optind] == NULL) {
-        setcerr(208, NULL);    /* object file is not specified */
+        setcerr(211, "");    /* object file not specified */
         fprintf(stderr, "comet2 error - %d: %s\n", cerr->num, cerr->msg);
-        exit(-1);
+        goto comet2fin;
     }
-    /* COMET II仮想マシンのリセット */
-    reset(memsize, clocks);
+    reset(memsize, clocks);     /* COMET II仮想マシンのリセット */
     execptr->start = 0;
-    if(loadassemble(argv[optind]) == true) {
-        create_code_type();    /* タイプがキーの命令ハッシュ表を作成 */
-        exec();                /* プログラム実行 */
-        free_code_type();      /* タイプがキーの命令ハッシュ表を解放 */
+    execptr->end = loadassemble(argv[optind], execptr->start);
+    if(execptr->end > 0 && cerr->num == 0) {
+        exec();                 /* プログラム実行 */
     }
-    /* COMET II仮想マシンのシャットダウン */
-    shutdown();
+    shutdown();                 /* COMET II仮想マシンのシャットダウン */
+comet2fin:
     if(cerr->num > 0) {
-        status = -1;
+        stat = 1;
     }
-    /* エラーの解放 */
-    freecerr();
-    return status;
+    freecerr();                 /* エラーの解放 */
+    return stat;
 }