home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 408.lha / MyMenu_v1.1 / sources / Parse.c < prev   
Encoding:
C/C++ Source or Header  |  1990-09-04  |  3.4 KB  |  164 lines

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