エラー処理にリストを使うよう仕様変更
[YACASL2.git] / src / dumpword.c
1 #include "casl2.h"
2 #define _GNU_SOURCE
3 #include <getopt.h>
4
5 static struct option longopts[] = {
6     {"arithmetic", no_argument, NULL, 'a'},
7     {"logical", no_argument, NULL, 'l'},
8     {"help", no_argument, NULL, 'h'},
9     {0, 0, 0, 0},
10 };
11
12 CERRARRAY cerr_dumpword[] = {
13     { 114, "not integer" },
14     { 115, "not hex" },
15     { 116, "out of hex range" },
16 };
17
18 int main(int argc, char *argv[])
19 {
20     bool logicalmode = false;    /* レジストリの内容を論理値(0〜65535)で表示する場合はtrue */
21     int opt;
22     WORD word;
23     const char *usage = "Usage: %s [-alh] WORD\n";
24
25     while((opt = getopt_long(argc, argv, "alh", longopts, NULL)) != -1) {
26         switch(opt) {
27         case 'l':
28             logicalmode = true;
29             break;
30         case 'h':
31             fprintf(stdout, usage, argv[0]);
32             return 0;
33         case '?':
34             fprintf(stderr, usage, argv[0]);
35             exit(-1);
36         }
37     }
38
39     if(argv[optind] == NULL) {
40         fprintf(stderr, usage, argv[0]);
41         exit(-1);
42     }
43     /* エラーリストにerr_casl2を追加 */
44     addcerrlist(ARRAYSIZE(cerr_dumpword), cerr_dumpword);
45     /* WORD値に変換 */
46     word = nh2word(argv[optind]);
47     if(cerrno > 0) {
48         fprintf(stderr, "Dumpword Error - %d: %s\n", cerrno, cerrmsg);
49         exit(-1);
50     }
51     fprintf(stdout, "%6s: ", argv[optind]);
52     print_dumpword(word, logicalmode);
53     return 0;
54 }