home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Misc / a2 / Source / parse.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-21  |  3.0 KB  |  206 lines

  1. /*
  2.  *  a2, an Apple II emulator in C
  3.  *  (c) Copyright 1990 by Rich Skrenta
  4.  *
  5.  *  Command line interface written by Tom Markson
  6.  *
  7.  *  Distribution agreement:
  8.  *
  9.  *    You may freely copy or redistribute this software, so long
  10.  *    as there is no profit made from its use, sale, trade or
  11.  *    reproduction.  You may not change this copyright notice,
  12.  *    and it must be included prominently in any copy made.
  13.  *
  14.  *  Send emulator related mail to:  skrenta@blekko.commodore.com
  15.  *                    skrenta@blekko.uucp
  16.  */
  17.  
  18.  
  19.  
  20. #import    <stdio.h>
  21. #import    <libc.h>
  22. #import    <strings.h>
  23. #import    <signal.h>
  24. #import    <ctype.h>
  25. #import    "a2.h"
  26. #import    "cli.h"
  27.  
  28.  
  29. char    *
  30. split(s)
  31. char    *s;
  32. {
  33.     char    *t;
  34.     extern char    *strchr();
  35.  
  36.     assert(s != NULL);
  37.  
  38.     t = strchr(s, ' ');
  39.     if (t == NULL)
  40.         return("");
  41.  
  42.     assert(*t == ' ');
  43.  
  44.     *t++ = '\0';
  45.  
  46.     while (*t && *t == ' ')
  47.         t++;
  48.  
  49.     return(t);
  50. }
  51.  
  52.  
  53. int match(char    *a, char *b)
  54. {
  55.  
  56.     assert(a != NULL);
  57.     assert(b != NULL);
  58.  
  59.     if (*b == '.' && strlen(b) > 1)
  60.         b++;
  61.     while (*a && *b) {
  62.         if (toupper(*a) != toupper(*b))
  63.             return(NO_MATCH);
  64.         a++;
  65.         b++;
  66.     }
  67.  
  68.     if (!*a) {
  69.         if (!*b)
  70.             return(IDENTICAL);
  71.         else
  72.             return(PARTIAL);
  73.     } else
  74.         return(NO_MATCH);
  75. }
  76.  
  77.  
  78. int is_hex_number(char *s)
  79. {
  80.     char    *temp;
  81.     temp = s;
  82.     assert(s != NULL);
  83.     while (*s)
  84.         if (!isxdigit(*s++))
  85.             return(FALSE);
  86.     if (temp != s)
  87.         return TRUE;
  88.     else
  89.         return FALSE;
  90. }
  91.  
  92.  
  93. long    get_hex_number(s)
  94. char    *s;
  95. {
  96.     unsigned int    x;
  97.     if (is_hex_number(s))
  98.         sscanf(s, "%x", &x);
  99.     else
  100.         return(-1);
  101.     return( (long) x );
  102. }
  103.  
  104.  
  105. int parse_str(struct cmdtbl tbl[], char    *s, char *t)
  106. {
  107. int i;
  108. int ret;
  109. int partial = -1;
  110. int count = 0;
  111.  
  112.     i = 0;
  113.     while (tbl[i].name != NULL) {
  114.         ret = match(s, tbl[i].name);
  115.         if (ret == IDENTICAL) {
  116.             partial = i;
  117.             count = 0;
  118.             break;
  119.         } else if (ret == PARTIAL) {
  120.             partial = i;
  121.             count++;
  122.         }
  123.  
  124.         i++;
  125.     }
  126.  
  127.     if (partial == -1)
  128.         return(NO_MATCH);
  129.  
  130.     if (count > 1)
  131.         return(AMBIGUOUS);
  132.  
  133.     if (tbl[partial].func == NULL)
  134.         return(NOT_IMPLEMENTED);
  135.  
  136.     assert(t != NULL);
  137.     return  (*tbl[partial].func)(t);
  138. }
  139.  
  140. void dump_list(struct cmdtbl tbl[], char *header)
  141. {
  142.     int    i, count;
  143.  
  144.     printf(header);
  145.     i = 0;
  146.     count = 0;
  147.     while (tbl[i].name != NULL) {
  148.         if (*tbl[i].name != '.' || strlen(tbl[i].name) == 1) {
  149.             if (count % 4 == 3)
  150.                 printf("    %-15s\n", tbl[i].name);
  151.             else
  152.                 printf("    %-15s", tbl[i].name);
  153.  
  154.             count++;
  155.         }
  156.         i++;
  157.     }
  158.  
  159.     if (count % 4 != 0)
  160.         printf("\n");
  161. }
  162.  
  163. int parse(struct cmdtbl tbl[], char    *s)
  164. {
  165.     char    *t;
  166.  
  167.     if (*s == '!') {            /* shell escape-ish thing */
  168.         shell_escape(++s);
  169.         return(FALSE);
  170.     }
  171.  
  172.     t = split(s);
  173.  
  174.     if (*s == '\0') {
  175.         return(TRUE);                /* single step */
  176.  
  177.     } else if (strcmp(s, "?") == 0) {
  178.         dump_list(tbl, "Command, one of the following:\n\n");
  179.  
  180.     } else switch (parse_str(tbl, s, t)) {
  181.         case AMBIGUOUS:
  182.             printf("Ambiguous command '%s'.\n", s);
  183.             break;
  184.  
  185.         case NO_MATCH:
  186.             printf("Unknown command.  Type ? for a list.\n");
  187.             break;
  188.  
  189.         case NOT_IMPLEMENTED:
  190.             printf("Sorry, command not implemented yet.\n");
  191.             break;
  192.  
  193.         case OK:
  194.             break;
  195.  
  196.         case DISPLAY:
  197.             status(stdout);
  198.             break;
  199.  
  200.         default:
  201.             assert(FALSE);
  202.     }
  203.  
  204.     return(FALSE);
  205. }
  206.