home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume25 / ils / parse.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-02-28  |  3.6 KB  |  205 lines

  1. #include    <stdio.h>
  2. #include    <curses.h>
  3. #include    "ils.h"
  4.  
  5. #define NUETRAL        0    /* states used in parse */
  6. #define IN_OP        1
  7.  
  8. extern    char    *pnam;            /* program name */
  9. extern    struct    symbol    *symtab[];    /* symbol table */
  10. extern    int    syms;            /* number of symbols on table */
  11.  
  12. parse_line(line,lnum)
  13. char    line[];
  14. int    lnum;
  15. {
  16.     static    int    state=NUETRAL,o,no;
  17.     char    ops[2][160];
  18.  
  19.     o=get_opts(ops,line);    /* Get the 2 possible parts */
  20.  
  21.     if(!o)
  22.         return(0);        /* line was just white space */
  23.     switch(state) {
  24.         case NUETRAL:
  25.             if(o==1) {
  26.                 printw("%s: error on line %d of '%s' file\n",pnam,lnum,ILS_FILE);
  27.                 refresh();
  28.                 return(1);
  29.             }
  30.             no=new_op(ops,ILS_KEY_SEQUENCE);
  31.             state=IN_OP;
  32.             break;
  33.         case IN_OP:
  34.             if(o==2)
  35.                 no=new_op(ops,ILS_KEY_SEQUENCE);
  36.             else
  37.                 add_op(no,ops);
  38.             break;
  39.         default:
  40.             printw("%s: bad state in parse_line()\n",pnam);
  41.             refresh();
  42.             return(1);
  43.             break;
  44.     }
  45.     return(0);
  46. }
  47.  
  48. #define ESC        1
  49. #define COLON        2
  50. #define NEWLINE        3
  51. #define SKIP_WHITE    4
  52.  
  53. get_opts(s,line)
  54. char    s[2][160], line[];
  55. {
  56.     int    i,j,k,l,nonwhite=0,state=0,colons=0;
  57.     char    c;
  58.  
  59.     l=strlen(line);
  60.     s[0][0]=s[1][0]='\0';
  61.  
  62.     for(i=j=k=0;i<l;i++) {
  63.         switch(c=line[i]){
  64.             case '\\':
  65.                 state=ESC;
  66.                 continue;
  67.             case '\n':
  68.                 state=NEWLINE;
  69.                 break;
  70.             case ':':
  71.                 state=COLON;
  72.                 colons++;
  73.                 if(colons==1)
  74.                     break;
  75.                 else
  76.                     state=0;
  77.             default:
  78.                 if(c==' '||c=='\t')
  79.                     break;
  80.                 nonwhite=1;
  81.                 break;
  82.         }
  83.         switch(state) {
  84.             case ESC:
  85.                 s[j][k]=c;
  86.                 break;
  87.             case COLON:
  88.                 s[j][k]='\0';
  89.                 j=1;
  90.                 k=0;
  91.                 state=SKIP_WHITE;
  92.                 break;
  93.             case NEWLINE:
  94.                 s[j][k]='\0';
  95.                 k=0;
  96.                 break;
  97.             case SKIP_WHITE:
  98.                 if(c==' '||c=='\t')
  99.                     break;
  100.                 else
  101.                     state=0;
  102.             default:
  103.                 if(!nonwhite)    /* skip leading white space */
  104.                     continue;
  105.                 s[j][k++]=c;
  106.                 break;
  107.         }
  108.     }
  109.     if(nonwhite)
  110.         return(j+1);    /* j=0 if one field, j=1 if two fields */
  111.     else
  112.         return(0);    /* line was all white space */
  113. }
  114.  
  115. new_op(s,type)
  116. char    s[2][160];
  117. int    type;
  118. {
  119.     struct    symbol    *t;
  120.     struct    value    *v;
  121.  
  122.     if(syms==MAX_SYMBOLS) {
  123.         fprintf(stderr,"%s: symbol table size exceeded\n",pnam);
  124.         return;
  125.     }
  126.     t = symtab[syms] = (struct symbol *) malloc(sizeof(struct d_entry));
  127.     if(t == NULL) {
  128.         fprintf(stderr,"%s: out of memory\n",pnam);
  129.         return;
  130.     }
  131.     t->name = (char *)malloc(strlen(s[0])+1);
  132.     strcpy(t->name,s[0]);
  133.     t->type = type;
  134.  
  135.     if(s[1][0]=='\0')
  136.         t->val = (struct value *)NULL;
  137.     else {
  138.         v = t->val = (struct value *) malloc(sizeof(struct value));
  139.         v->v = (char *)malloc(strlen(s[1])+1);
  140.         strcpy(v->v,s[1]);
  141.         v->next =(struct value *) NULL;
  142.     }
  143.  
  144.     return(syms++);
  145. }
  146.  
  147. add_op(op,s)
  148. int    op;
  149. char    s[2][160];
  150. {
  151.     struct    value    *v;
  152.  
  153.     v = symtab[op]->val;
  154.     
  155.     if(v==(struct value *)NULL) {
  156.         v = symtab[op]->val = (struct value *) malloc(sizeof(struct value));
  157.         v->next =(struct value *) NULL;
  158.     }
  159.     else {
  160.         while(v->next != (struct value *)NULL)
  161.             v = v->next;
  162.         v->next = (struct value *) malloc(sizeof(struct value));
  163.         v->next->next = (struct value *) NULL;
  164.         v = v->next;
  165.     }
  166.     v->v = (char *)malloc(strlen(s[0])+1);
  167.     strcpy(v->v,s[0]);
  168. }
  169.  
  170. display_symtab()
  171. {
  172.     int    i, over=0;
  173.     struct    value    *v;
  174.  
  175.     for(i=0;i<syms;i++) {
  176.         clear();
  177.         standout();
  178.         printw("Symbol %d: %s\n",i+1,symtab[i]->name);
  179.         standend();
  180.         v = symtab[i]->val;
  181.         while(v!=NULL && over++ != MAX_SYMBOLS) {
  182.             printw("    %s\n",v->v);
  183.             v = v->next;
  184.         }
  185.         standout();
  186.         printw("Press RETURN next symbol, %c for previous, ESC to exit",
  187.             ILS_LEFT);
  188.         standend();
  189.         refresh();
  190.         switch(getch()&0x7f) {
  191.             case ILS_ENTER:
  192.             case ILS_RETURN:
  193.                 break;
  194.             case ILS_LEFT:
  195.                 if(i!=0)
  196.                     i -= 2;
  197.                 else
  198.                     i--;
  199.                 break;
  200.             case ILS_ESCAPE:
  201.                 return;
  202.         }
  203.     }
  204. }
  205.