081ee441773cf8c667b73a2f26d5bae4b20b85e1
[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 int main(int argc, char *argv[])
13 {
14     int opt;
15     WORD w;
16     char *check;
17     const char *usage = "Usage: %s [-alh] WORD\n";
18
19     logicalmode = false;
20     while((opt = getopt_long(argc, argv, "alh", longopts, NULL)) != -1) {
21         switch(opt) {
22         case 'l':
23             logicalmode = true;
24             break;
25         case 'h':
26             fprintf(stdout, usage, argv[0]);
27             exit(-1);
28         case '?':
29             fprintf(stderr, usage, argv[0]);
30             exit(-1);
31         }
32     }
33
34     if(argv[optind] == NULL) {
35         fprintf(stderr, usage, argv[0]);
36         exit(-1);
37     }
38     if(*argv[optind] == '-' || strlen(argv[optind]) > 4) {
39         setcerr(116, argv[optind]);    /* out of hex range */
40     }
41     /* WORD値に変換 */
42     w = (WORD)strtol(argv[optind], &check, 16);
43     if(*check != '\0') {
44         setcerr(115, argv[optind]);    /* not hex */
45     }
46     if(cerrno > 0) {
47         fprintf(stderr, "Dumpword Error - %d: %s\n", cerrno, cerrmsg);
48         exit(-1);
49     }
50     if(logicalmode == true) {
51         fprintf(stdout, "%4s: %6d = #%04X = %s\n", argv[optind], w, w, word2bit(w));
52     } else {
53         fprintf(stdout, "%4s: %6d = #%04X = %s\n", argv[optind], (short)w, w, word2bit(w));
54     }
55     return 0;
56 }