ドキュメントの修正
[YACASL2.git] / src / comet2.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #define _GNU_SOURCE
4 #include <getopt.h>
5
6 #include "exec.h"
7 #include "cmem.h"
8 #include "cerr.h"
9 #include "package.h"
10
11 /**
12  * comet2コマンドのオプション
13  */
14 static struct option longopts[] = {
15     {"trace", no_argument, NULL, 't'},
16     {"tracearithmetic", no_argument, NULL, 't'},
17     {"tracelogical", no_argument, NULL, 'T'},
18     {"dump", no_argument, NULL, 'd'},
19     {"memorysize", required_argument, NULL, 'M'},
20     {"clocks", required_argument, NULL, 'C'},
21     { "version", no_argument, NULL, 'v' },
22     {"help", no_argument, NULL, 'h'},
23     {0, 0, 0, 0},
24 };
25
26 /**
27  * @brief comet2コマンドのメイン
28  *
29  * @return 正常終了時は0、異常終了時は1
30  *
31  * @param argc コマンドライン引数の数
32  * @param *argv[] コマンドライン引数の配列
33  */
34 int main(int argc, char *argv[])
35 {
36     int memsize = DEFAULT_MEMSIZE, clocks = DEFAULT_CLOCKS;
37     int opt, stat = 0;
38     const char *version = PACKAGE_VERSION,  *cmdversion = "comet2 of YACASL2 version %s\n";
39     const char *usage = "Usage: %s [-tTdvh] [-M <MEMORYSIZE>] [-C <CLOCKS>] FILE\n";
40
41     cerr_init();
42     addcerrlist_load();
43     addcerrlist_exec();
44
45     /* オプションの処理 */
46     while((opt = getopt_long(argc, argv, "tTdM:C:vh", longopts, NULL)) != -1) {
47         switch(opt) {
48         case 't':
49             execmode.trace = true;
50             break;
51         case 'T':
52             execmode.trace = true;
53             execmode.logical = true;
54             break;
55         case 'd':
56             execmode.dump = true;
57             break;
58         case 'M':
59             memsize = atoi(optarg);
60             break;
61         case 'C':
62             clocks = atoi(optarg);
63             break;
64         case 'v':
65             fprintf(stdout, cmdversion, version);
66             return 0;
67         case 'h':
68             fprintf(stdout, usage, argv[0]);
69             return 0;
70         case '?':
71             fprintf(stderr, usage, argv[0]);
72             exit(1);
73         }
74     }
75     if(argv[optind] == NULL) {
76         setcerr(211, "");    /* object file not specified */
77         fprintf(stderr, "comet2 error - %d: %s\n", cerr->num, cerr->msg);
78         exit(1);
79     }
80     /* COMET II仮想マシンのリセット */
81     reset(memsize, clocks);
82     execptr->start = 0;
83     if(loadassemble(argv[optind]) == true) {
84         exec();                /* プログラム実行 */
85     }
86     /* COMET II仮想マシンのシャットダウン */
87     shutdown();
88     stat = (cerr->num == 0) ? 0 : 1;
89     /* エラーの解放 */
90     freecerr();
91     return stat;
92 }