home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / x / volume3 / xdbx / part03 / parser.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-03-14  |  6.6 KB  |  248 lines

  1. /****************************************************************************** 
  2.  *
  3.  *  xdbx - X Window System interface to dbx
  4.  *
  5.  *  Copyright 1989 The University of Texas at Austin
  6.  *
  7.  *  Author:    Po Cheung
  8.  *  Date:    March 10, 1989
  9.  *
  10.  *  Permission to use, copy, modify, and distribute this software and
  11.  *  its documentation for any purpose and without fee is hereby granted,
  12.  *  provided that the above copyright notice appear in all copies and that
  13.  *  both that copyright notice and this permission notice appear in
  14.  *  supporting documentation.  The University of Texas at Austin makes no 
  15.  *  representations about the suitability of this software for any purpose.  
  16.  *  It is provided "as is" without express or implied warranty.
  17.  *
  18.  ******************************************************************************/
  19.  
  20.  
  21. #include    "global.h"
  22. #include    "regex.h"
  23.  
  24. #ifdef BSD
  25. #include    "bsd_regex.h"
  26. #else
  27. #include    "sun_regex.h"
  28. #endif
  29.  
  30. #define        BYTEWIDTH    8
  31. #define        RE_BUFFER    100
  32.  
  33. Tokens             token;        /* token structure */
  34.  
  35. /*
  36.  *  Compile all the regular expression patterns in a pattern table.
  37.  *  A pattern table is an array of pattern records.
  38.  *  Each pattern record consists of a regular
  39.  *  expression, a buffer for the to-be-compiled regular expression,
  40.  *  and an array to associate a given token with a matched register number.
  41.  */
  42. static void compile(patternTable)
  43. PatternRec *patternTable;
  44. {
  45.     PatternRec    *p;
  46.     char     fastmap[(1 << BYTEWIDTH)];
  47.     int        i;
  48.  
  49.     for (i=0; patternTable[i].pat; i++) {
  50.     p = &patternTable[i];
  51.     p->buf = (struct re_pattern_buffer *) 
  52.       XtMalloc (sizeof (struct re_pattern_buffer));
  53.     p->buf->allocated = RE_BUFFER;
  54.     p->buf->buffer = (char *) XtMalloc (p->buf->allocated);
  55.     p->buf->fastmap = fastmap;
  56.     p->buf->translate = NULL;
  57.     re_compile_pattern(p->pat, strlen(p->pat), p->buf);
  58.     re_compile_fastmap(p->buf);
  59.     }
  60.  
  61. /*
  62.  *  This routine tries to match a given string with each regular 
  63.  *  expression in a given pattern table.  The best match is found, and
  64.  *  the function returns an index to the pattern table.
  65.  */
  66. static int match(patternTable, string)
  67.     PatternRec     *patternTable;
  68.     char     *string;
  69. {
  70.     struct re_registers    regs;
  71.     int            m, bestmatch = -1, index = -1, i, j, r, start, n;
  72.     char         *s;
  73.  
  74.     if (strcmp(string, "") == NULL) return -1;
  75.     for (i=0; patternTable[i].pat; i++) {
  76.     m = re_match(patternTable[i].buf, string, strlen(string), 0, ®s);
  77.     if (m > bestmatch) {
  78.         bestmatch = m;
  79.         index = i;
  80.         token.mesg = token.file = token.func = NULL;
  81.         token.line = token.stop = 0;
  82.         for (j=0; j<NTOKENS; j++) {
  83.         if ((r = patternTable[i].reg_token[j]) >= 0) {
  84.             start = regs.start[r];
  85.             if ((n = regs.end[r] - start) > 0) {
  86.             s = (char *) XtMalloc ((n+1) * sizeof(char));
  87.             strncpy(s, string+start, n);
  88.             s[n] = '\0';
  89.                 switch (j) {
  90.                 case 0: token.mesg = s; break;
  91.                 case 1: token.stop = atoi(s); XtFree(s); break;
  92.                 case 2: token.func = s; break;
  93.                 case 3: token.line = atoi(s); XtFree(s); break;
  94.                 case 4: token.file = s; break;
  95.                 }
  96.             }
  97.         }
  98.         }
  99.     }
  100.     }
  101.     return index;
  102. }
  103.  
  104. /*  PUBLIC ROUTINES */
  105.  
  106. /*  Compile the regular expressions in the output and command pattern tables. */
  107.  
  108. void parser_init()
  109. {
  110.     compile(output_pattern);
  111.     compile(command_pattern);
  112. }
  113.  
  114.  
  115. /*  This routine first parses the command string.  
  116.  *  If the command is one of run, cont, next, step, stop at, stop in, 
  117.  *  where, up, or down, it parses the dbx output to decide what action 
  118.  *  to take and dispatch it to one of the handlers.
  119.  *  For other commands, the appropriate handler is called.
  120.  */
  121. void parse(output)
  122. char *output;
  123. {
  124.     int  handler, command_flag;
  125.  
  126.     command_flag = match(command_pattern, Command);
  127.     switch (command_flag) {
  128.           case C_EXEC: 
  129.     case C_STOPAT: 
  130.     case C_STOPIN: 
  131.     case C_UPDOWN:
  132.         handler = match(output_pattern, output);
  133.         switch (handler) {
  134.           case O_EXEC  : exec_handler(&token, FALSE); break;
  135.           case O_DONE  : exec_handler(&token, TRUE); break;
  136.           case O_STOPAT: stop_at_handler(&token); break;
  137.           case O_STOPIN: stop_in_handler(&token); break;
  138.           case O_UPDOWN: updown_handler(&token); break;
  139.           case O_BELL  : Bell(0); break;
  140.         }
  141.         break;
  142.           case C_DELETE:
  143.         delete_handler(); 
  144.         break;
  145.     case C_FUNC:
  146.         if (strcmp(output, "") == NULL)
  147.             func_handler(&token); 
  148.         break;
  149.     case C_FILE:
  150.         file_handler(&token); 
  151.         break;
  152.     case C_DEBUG:
  153.         debug_handler(); 
  154.         break;
  155.     case C_CD:
  156.         if (strcmp(output, "") == NULL)
  157.             cd_handler(); 
  158.         break;
  159.     }
  160. }
  161.  
  162.  
  163. /*  This function edits the dbx output so that unnecessary information is 
  164.  *  not displayed on the dialog window.
  165.  *  It filters away the information about the halted execution point,
  166.  *  returned by the execution commands.
  167.  *  On Sun dbx, it also filters away the output returned by the up and 
  168.  *  down commands.
  169.  */
  170. void filter(s)
  171. char *s;
  172. {
  173.     struct re_registers regs;
  174.     char         *p, *q;
  175.     int            r;
  176.     static Boolean    DeleteNextLine = FALSE;
  177.  
  178.     if ((s && strcmp(s, "") == NULL) || strcmp(Command, "") == NULL)
  179.     return;
  180.  
  181. #ifndef BSD  /* for Sun dbx only */
  182.     if (re_match(command_pattern[C_UPDOWN].buf, Command, strlen(Command), 
  183.          0, NULL) > 0) {
  184.     if (re_match(output_pattern[O_BELL].buf, s, strlen(s), 0, ®s) < 0) {
  185.         if (strcmp(s+strlen(s)-strlen(PROMPT), PROMPT) == NULL)
  186.             strcpy(s, PROMPT);
  187.         else
  188.         strcpy(s, "");
  189.     }
  190.     return;
  191.     }
  192. #endif
  193.  
  194.     if (re_match(command_pattern[C_EXEC].buf, Command, strlen(Command), 
  195.          0, NULL) > 0
  196.     && re_match(output_pattern[O_EXEC].buf, s, strlen(s), 0, ®s) > 0) {
  197.     r = output_pattern[O_EXEC].reg_token[TK_MESG];
  198.     for (p=s+regs.start[r]; p!=s && *(p-1) != '\n'; p--);
  199.     if (q = (char *) strchr(s+regs.end[r]+1, '\n'))
  200.         strcpy(p, q+1);
  201.     else {
  202.         DeleteNextLine = TRUE;
  203.         *p = '\0';
  204.     }
  205.     return;
  206.     }
  207.  
  208.     if (DeleteNextLine) {
  209.     p = (char *) strchr(s, '\n');
  210.     strcpy(s, p+1);
  211.     DeleteNextLine = FALSE;
  212.     }
  213. }
  214.  
  215.  
  216. /*  Sends a command to dbx and read the corresponding output, bypassing the
  217.  *  Xt input procedure, readDbx.
  218.  */
  219. int QueryDbx(command, do_match)
  220. char *command;
  221. Boolean do_match;
  222. {
  223.     char s[LINESIZ], *output;
  224.     int  outputsize;
  225.  
  226.     Echo = FALSE;
  227.     writeDbx(command);
  228.     /* allocate one block to 'output' to begin with */
  229.     outputsize = BUFSIZ;
  230.     output = XtMalloc(outputsize);
  231.     strcpy(output, "");
  232.  
  233.     while (fgets(s, LINESIZ, dbxfp) == NULL);
  234.     do {
  235.     if (strlen(output) + strlen(s) > outputsize) {
  236.         output = XtRealloc(output, outputsize + BUFSIZ);
  237.         outputsize += BUFSIZ;
  238.     }
  239.         strcat(output, s);
  240.     } while (fgets(s, LINESIZ, dbxfp));
  241.     Echo = TRUE;
  242.     if (do_match)
  243.         return match(output_pattern, output);
  244.     else
  245.         return -1;
  246. }
  247.