バージョン表示機能を追加
[YACASL2.git] / src / dumpword.c
index e78b108..d39dfaf 100644 (file)
@@ -1,49 +1,63 @@
-#include "casl2.h"
 #define _GNU_SOURCE
+#include <stdio.h>
+#include <stdlib.h>
 #include <getopt.h>
+#include "word.h"
+#include "cerr.h"
+#include "package.h"
 
+/**
+ * dumpwordコマンドのオプション
+ */
 static struct option longopts[] = {
-    {"arithmetic", no_argument, NULL, 'a'},
-    {"logical", no_argument, NULL, 'l'},
-    {"help", no_argument, NULL, 'h'},
-    {0, 0, 0, 0},
+    { "arithmetic", no_argument, NULL, 'a' },
+    { "logical", no_argument, NULL, 'l' },
+    { "version", no_argument, NULL, 'v' },
+    { "help", no_argument, NULL, 'h' },
+    { 0, 0, 0, 0 },
 };
 
+/**
+ * dumpwordコマンドのメイン
+ */
 int main(int argc, char *argv[])
 {
+    bool logicalmode = false;    /* レジストリの内容を論理値(0から65535)で表示する場合はtrue */
     int opt;
     WORD word;
+    const char *version = PACKAGE_VERSION,  *cmdversion = "dumpword of YACASL2 version %s\n";
     const char *usage = "Usage: %s [-alh] WORD\n";
 
-    logicalmode = false;
-    while((opt = getopt_long(argc, argv, "alh", longopts, NULL)) != -1) {
+    cerr_init();
+    addcerrlist_word();
+    while((opt = getopt_long(argc, argv, "alvh", longopts, NULL)) != -1) {
         switch(opt) {
         case 'l':
             logicalmode = true;
             break;
+        case 'v':
+            fprintf(stdout, cmdversion, version);
+            return 0;
         case 'h':
             fprintf(stdout, usage, argv[0]);
             return 0;
         case '?':
             fprintf(stderr, usage, argv[0]);
-            exit(-1);
+            exit(1);
         }
     }
 
     if(argv[optind] == NULL) {
         fprintf(stderr, usage, argv[0]);
-        exit(-1);
+        exit(1);
     }
     /* WORD値に変換 */
     word = nh2word(argv[optind]);
-    if(cerrno > 0) {
-        fprintf(stderr, "Dumpword Error - %d: %s\n", cerrno, cerrmsg);
-        exit(-1);
-    }
-    if(logicalmode == true) {
-        fprintf(stdout, "%6s: %6d = #%04X = %s\n", argv[optind], word, word, word2bit(word));
-    } else {
-        fprintf(stdout, "%6s: %6d = #%04X = %s\n", argv[optind], (short)word, word, word2bit(word));
+    if(cerr->num > 0) {
+        fprintf(stderr, "Dumpword Error - %d: %s\n", cerr->num, cerr->msg);
+        exit(1);
     }
+    fprintf(stdout, "%6s: ", argv[optind]);
+    print_dumpword(word, logicalmode);
     return 0;
 }