88915a81183d12e9ab9f78cac5956ba3186669b3
[YACASL2.git] / src / dumpword.c
1 #include "package.h"
2 #include "word.h"
3
4 /**
5  * @brief dumpwordコマンドのオプション
6  */
7 static struct option longopts[] = {
8     { "arithmetic", no_argument, NULL, 'a' },
9     { "logical", no_argument, NULL, 'l' },
10     { "version", no_argument, NULL, 'v' },
11     { "help", no_argument, NULL, 'h' },
12     { 0, 0, 0, 0 },
13 };
14
15 /**
16  * @brief dumpwordコマンドのメイン
17  *
18  * @return 正常終了時は0、エラー発生時は1
19  *
20  * @param argc コマンドライン引数の数
21  * @param *argv[] コマンドライン引数の配列
22  */
23 int main(int argc, char *argv[])
24 {
25     bool logicalmode = false;    /* レジストリの内容を論理値(0から65535)で表示する場合はtrue */
26     int opt = 0;
27     int stat = 0;
28     WORD word = 0;
29     const char *version = PACKAGE_VERSION;
30     const char *cmdversion = "dumpword of YACASL2 version %s\n";
31     const char *usage = "Usage: %s [-alh] WORD\n";
32
33     /* エラーの定義 */
34     cerr_init();
35     addcerrlist_load();
36     addcerrlist_word();
37
38     /* オプションの処理 */
39     while((opt = getopt_long(argc, argv, "alvh", longopts, NULL)) != -1) {
40         switch(opt) {
41         case 'l':
42             logicalmode = true;
43             break;
44         case 'v':
45             fprintf(stdout, cmdversion, version);
46             goto dumpwordfin;
47         case 'h':
48             fprintf(stdout, usage, argv[0]);
49             goto dumpwordfin;
50         case '?':
51             fprintf(stderr, usage, argv[0]);
52             setcerr(999, "");
53             goto dumpwordfin;
54         }
55     }
56
57     if(argv[optind] == NULL) {
58         fprintf(stderr, usage, argv[0]);
59         setcerr(999, "");
60         goto dumpwordfin;
61     }
62     /* WORD値に変換 */
63     word = nh2word(argv[optind]);
64     if(cerr->num > 0) {
65         fprintf(stderr, "Dumpword Error - %d: %s\n", cerr->num, cerr->msg);
66         goto dumpwordfin;
67     }
68     fprintf(stdout, "%6s: ", argv[optind]);
69     print_dumpword(word, logicalmode);
70     fprintf(stdout, "\n");
71 dumpwordfin:
72     if(cerr->num > 0) {
73         stat = 1;
74     }
75     freecerr();                 /* エラーの解放 */
76     return stat;
77 }