home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 200-299 / ff225.lzh / MyMenu / Parse.c < prev    next >
C/C++ Source or Header  |  1989-06-24  |  3KB  |  166 lines

  1. /* Copyright ) Darin Johnson, 1989 */
  2.  
  3. #include <exec/types.h>
  4. #include <intuition/intuition.h>
  5. #include <stdio.h>
  6. #include <ctype.h>
  7. #include "mymenu.h"
  8.  
  9. #define STR 1
  10.  
  11. #define SYNTAX(msg) { fprintf(stderr, "Error parsing MyMenu.conf: %s\n"); \
  12.             return FALSE; }
  13.  
  14. #ifdef AZTEC_C
  15. #define strcmp _BUILTIN_strcmp
  16. #define strcpy _BUILTIN_strcpy
  17. #define strlen _BUILTIN_strlen
  18. #endif
  19.  
  20. FILE *conf;
  21. char tok[256], menustr[256], itemstr[256], substr[256];
  22.  
  23. extern UBYTE menu_pen;
  24.  
  25. make_action(mi, typ, action)
  26.   struct ext_MenuItem *mi;
  27.   char typ, *action;
  28. {
  29.   register char *p, *index(), *rindex(), *copystr();
  30.  
  31.   mi->args = mi->cmd = NULL;
  32.   if (typ=='c') {    /* CLI */
  33.     if (*action == '"') {
  34.       action++;
  35.       p = index(action, '"');
  36.     } else {
  37.       p = index(action, ' ');
  38.     }
  39.     if (p) {    /* if arguments */
  40.       *p++ = NULL;
  41.       mi->args = copystr(p);
  42.     }
  43.     mi->cmd = copystr(action);
  44.     mi->type = 'C';
  45.   } else if (typ=='w') {
  46.     if (*action=='"') {
  47.       action++;
  48.       p = rindex(action, '"');
  49.       *p = NULL;
  50.     }
  51.     mi->cmd = copystr(action);
  52.     mi->type = 'W';
  53.   }
  54. }
  55.  
  56. char get_token()
  57. {
  58.   char quote;
  59.   register char c, *p;
  60. retry:
  61.   c = fgetc(conf);
  62.   while(isspace(c)) c=fgetc(conf);    /* skip extra spaces */
  63.   if (c=='#') {                /* comment */
  64.     while((c=fgetc(conf))!='\n' && c!=EOF);
  65.     goto retry;
  66.   }
  67.   if (!isalnum(c) && c!='"')
  68.     return c;
  69.     /* scan string */
  70.   if (c=='"') {
  71.     c = fgetc(conf);
  72.     quote = TRUE;
  73.   } else
  74.     quote = FALSE;
  75.   p = tok;
  76.   do {
  77.     if ((quote && c=='"') || (!quote && isspace(c)))
  78.       break;
  79.     if (c=='\\')
  80.       c = fgetc(conf);
  81.     if (isspace(c))
  82.       c = ' ';
  83.     *p++ = c;
  84.   } while ((c=fgetc(conf))!=EOF);
  85.   *p = NULL;
  86.   return STR;
  87. }
  88.  
  89. int parse_conf() {
  90.   register char t;
  91.   char *p, c, flag, cmd;
  92.   struct ext_MenuItem *mi;
  93.     
  94.   while ((t=get_token()) != EOF) {
  95.     if (t==STR) {
  96.       if (stricmp(tok, "MENU")==0) {
  97.         cmd = NULL;
  98.         if ((t=get_token())=='<') {    /* command char */
  99.       cmd = fgetc(conf);
  100.       if (get_token() != '>')
  101.         SYNTAX("Missing closing'>'");
  102.       t=get_token();
  103.     }
  104.         if (t==STR)
  105.       strcpy(menustr, tok);
  106.     else
  107.       SYNTAX("Missing menu name");
  108.         if (get_token()==STR)
  109.       strcpy(itemstr, tok);
  110.     else
  111.           SYNTAX("Missing menu item name");
  112.     if ((t=get_token())==STR) {
  113.       strcpy(substr, tok);
  114.           t=get_token();
  115.     } else {
  116.       substr[0] = NULL;
  117.     }
  118.     if (t != '|')
  119.       SYNTAX("Missing '|' separator");
  120.     mi = add_menu(menustr, itemstr, substr, cmd);
  121.     if (get_token() != STR)
  122.       SYNTAX("Syntax error after '|'");
  123.         /* find out type */
  124.     if (stricmp(tok, "CLI")==0)
  125.       flag = 'c';
  126.     else if (stricmp(tok, "WB")==0)
  127.       flag = 'w';
  128.     else
  129.       flag = NULL;
  130.         /* read in command (rest of line */
  131.     p = tok;
  132.     while ((c=fgetc(conf)) != '\n' && c!=EOF)
  133.       *p++ = c;
  134.     *p = NULL;
  135.     make_action(mi, flag, tok);
  136.       } else if (stricmp(tok, "COLOR")==0) {
  137.         if ((t=get_token())!=STR)
  138.           SYNTAX("Expected number after COLOR keyword");
  139.         menu_pen = (UBYTE)atoi(tok);
  140.       } else
  141.         SYNTAX("Didn't find keyword");
  142.     } else
  143.       SYNTAX("Didn't find keyword");
  144.   }
  145.   return TRUE;
  146. }
  147.  
  148. int parse_menus() {
  149.   int stat;
  150.   conf = fopen("S:MyMenu.conf", "r");
  151.   if (conf==NULL) {
  152.     conf = fopen("MyMenu.conf", "r");
  153.     if (conf==NULL) {
  154.       fprintf(stderr, "Can't open MyMenu.conf!\n");
  155.       return FALSE;
  156.     }
  157.   }
  158.   start_menu();
  159.   menu_pen = 2;
  160.   stat = parse_conf();
  161.   end_menu();
  162.   if (conf)
  163.     fclose(conf);
  164.   return stat;
  165. }
  166.