バージョン表示機能を追加
[YACASL2.git] / src / dumpword.c
1 #define _GNU_SOURCE
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <getopt.h>
5 #include "word.h"
6 #include "cerr.h"
7 #include "package.h"
8
9 /**
10  * dumpwordコマンドのオプション
11  */
12 static struct option longopts[] = {
13     { "arithmetic", no_argument, NULL, 'a' },
14     { "logical", no_argument, NULL, 'l' },
15     { "version", no_argument, NULL, 'v' },
16     { "help", no_argument, NULL, 'h' },
17     { 0, 0, 0, 0 },
18 };
19
20 /**
21  * dumpwordコマンドのメイン
22  */
23 int main(int argc, char *argv[])
24 {
25     bool logicalmode = false;    /* レジストリの内容を論理値(0から65535)で表示する場合はtrue */
26     int opt;
27     WORD word;
28     const char *version = PACKAGE_VERSION,  *cmdversion = "dumpword of YACASL2 version %s\n";
29     const char *usage = "Usage: %s [-alh] WORD\n";
30
31     cerr_init();
32     addcerrlist_word();
33     while((opt = getopt_long(argc, argv, "alvh", longopts, NULL)) != -1) {
34         switch(opt) {
35         case 'l':
36             logicalmode = true;
37             break;
38         case 'v':
39             fprintf(stdout, cmdversion, version);
40             return 0;
41         case 'h':
42             fprintf(stdout, usage, argv[0]);
43             return 0;
44         case '?':
45             fprintf(stderr, usage, argv[0]);
46             exit(1);
47         }
48     }
49
50     if(argv[optind] == NULL) {
51         fprintf(stderr, usage, argv[0]);
52         exit(1);
53     }
54     /* WORD値に変換 */
55     word = nh2word(argv[optind]);
56     if(cerr->num > 0) {
57         fprintf(stderr, "Dumpword Error - %d: %s\n", cerr->num, cerr->msg);
58         exit(1);
59     }
60     fprintf(stdout, "%6s: ", argv[optind]);
61     print_dumpword(word, logicalmode);
62     return 0;
63 }