fixing sloppy errors introduced with new strqtok
[dcpu16] / vm-dcpu16.c
index df86455070e7c91b008456579eeb2360b0425959..d60c340d2eeaa7a6f4eb4e9630127ae1152bd439 100644 (file)
@@ -9,6 +9,7 @@
 #include <readline/readline.h>
 
 #include "dcpu16.h"
+#include "common.h"
 
 /*
  *  shell-like driver for dcpu16 core
@@ -61,47 +62,22 @@ void usage_(char *prog, unsigned int full) {
     if (full) {
         fprintf(f, "\nOptions:\n"
                 "\t [file] -- ram image to load initially\n"
-                "\t -v -- verbose execution tracing\n"
+                "\t -v -- displays slightly more information\n"
                 "\t -h -- this screen\n");
 
         fprintf(f, "\n%78s\n", src_id_);
     }
 }
 
-/* simplified strtoul with range checking */
-static
-int str_to_word_(char *s) {
-    unsigned long l;
-    char *ep;
-
-    assert(s);
-
-    errno = 0;
-    l = strtoul(s, &ep, 0);
-
-    if (errno
-    ||  !(*s && *ep == '\0') ) {
-        /* out of range of conversion, or invalid character encountered */
-        return -1;
-    }
-
-    if (l >= DCPU16_RAM) {
-        /* out of range for our needs */
-        errno = ERANGE;
-        return -1;
-    }
-
-    return l;
-}
 
 /* flense a buffer into a newly-allocated argument list */
-/* FIXME: handle quotes */
 static
 int buf_tok_vect_(char ***v, int *c, char *buf) {
     const char *sep = " \t";
+    const char *quot = "\"'`";
     const size_t v_grow = 32;
     size_t v_sz = 32;
-    char *st;
+    char *st, *qt;
 
     *c = 0;
     *v = malloc(v_sz * sizeof **v);
@@ -110,9 +86,9 @@ int buf_tok_vect_(char ***v, int *c, char *buf) {
         return -1;
     }
 
-    for ( (*v)[*c] = strtok_r(buf, sep, &st);
+    for ( (*v)[*c] = strqtok_r(buf, sep, '\\', quot, &qt, &st);
           (*v)[*c];
-          (*v)[*c] = strtok_r(NULL, sep, &st)
+          (*v)[*c] = strqtok_r(NULL, sep, '\\', quot, &qt, &st)
         ) {
             (*c)++;
 
@@ -190,7 +166,7 @@ struct command_ {
 
 COMMAND_IMPL(quit) {
     (void)vm, (void)arg_count, (void)arg_vector;
-    VERBOSE_PRINTF("done\n");
+
     return -1;
 }
 COMMAND_HELP(quit) {
@@ -220,7 +196,7 @@ COMMAND_IMPL(load) {
     int addr = 0;
 
     if (arg_count > 2) {
-        addr = str_to_word_(arg_vector[2]);
+        addr = str_to_word(arg_vector[2]);
         if (addr < 0) {
             fprintf(stderr, "address '%s' is not a valid word: %s\n", arg_vector[2], strerror(errno));
             return 0;
@@ -250,7 +226,7 @@ COMMAND_IMPL(dump) {
     int i;
 
     for (i = 1; i < arg_count; i++) {
-        addr[i-1] = str_to_word_(arg_vector[i]);
+        addr[i-1] = str_to_word(arg_vector[i]);
         if (addr[i-1] < 0) {
             fprintf(stderr, "address '%s' is not a valid word: %s\n", arg_vector[i], strerror(errno));
             return 0;
@@ -281,7 +257,7 @@ COMMAND_IMPL(disassemble) {
     int i;
 
     for (i = 1; i < arg_count; i++) {
-        addr[i-1] = str_to_word_(arg_vector[i]);
+        addr[i-1] = str_to_word(arg_vector[i]);
         if (addr[i-1] < 0) {
             fprintf(stderr, "address '%s' is not a valid word: %s\n", arg_vector[i], strerror(errno));
             return 0;
@@ -331,12 +307,11 @@ COMMAND_IMPL(step) {
     }
 
     while (count--) {
-        VERBOSE_PRINTF("executing next cycle, instruction: ");
-        dcpu16_disassemble_print(vm, vm->pc), printf("\n");
-
+        dcpu16_disassemble_print(vm, vm->pc);
+        printf("\n");
         dcpu16_step(vm);
 
-        if (opt_.verbose)
+        if (count > 1 && opt_.verbose)
             dcpu16_state_print(vm);
     }
 
@@ -364,8 +339,12 @@ COMMAND_IMPL(run) {
 
     while(running_) {
         dcpu16_step(vm);
-        if (opt_.verbose)
+        if (opt_.verbose > 1)
             dcpu16_state_print(vm);
+        else if (opt_.verbose) {
+            dcpu16_disassemble_print(vm, vm->pc);
+            printf("\n");
+        }
     }
 
     /* restore the old interrupt signal handler */
@@ -374,7 +353,7 @@ COMMAND_IMPL(run) {
         return -1;
     }
 
-    VERBOSE_PRINTF("interrupted...\n");
+    printf("interrupted...\n");
 
     return 0;
 }
@@ -382,7 +361,8 @@ COMMAND_HELP(run) {
     fprintf(f, "\trun\n");
     if (summary) return;
 
-    fprintf(f, "Begins executing continuously.\n");
+    fprintf(f, "Begins executing continuously.\n"
+               "May be interrupted with SIGINT.\n");
 }
 
 /* gather all these together into a searchable table */
@@ -428,7 +408,7 @@ COMMAND_HELP(help) {
         fprintf(f, "\thelp [command]\n");
         if (summary) return;
 
-    fprintf(f, "Displays a list of available commands, or help on a specific command.\n");
+    fprintf(f, "Displays a list of available commands, or detailed help on a specific command.\n");
 }
 
 
@@ -466,7 +446,7 @@ int main(int argc, char **argv) {
     argv += optind;
 
     if ((vm = dcpu16_new()) == NULL) {
-        fprintf(stderr, "could not allocate new dcpu instance\n");
+        fprintf(stderr, "could not allocate new dcpu16 instance\n");
         exit(EX_UNAVAILABLE);
     }