home *** CD-ROM | disk | FTP | other *** search
- /******************************************************************************
- *
- * xdbx - X Window System interface to dbx
- *
- * Copyright 1989 The University of Texas at Austin
- *
- * Author: Po Cheung
- * Date: March 10, 1989
- *
- * Permission to use, copy, modify, and distribute this software and
- * its documentation for any purpose and without fee is hereby granted,
- * provided that the above copyright notice appear in all copies and that
- * both that copyright notice and this permission notice appear in
- * supporting documentation. The University of Texas at Austin makes no
- * representations about the suitability of this software for any purpose.
- * It is provided "as is" without express or implied warranty.
- *
- ******************************************************************************/
-
-
- #include "global.h"
- #include "regex.h"
-
- #ifdef BSD
- #include "bsd_regex.h"
- #else
- #include "sun_regex.h"
- #endif
-
- #define BYTEWIDTH 8
- #define RE_BUFFER 100
-
- Tokens token; /* token structure */
-
- /*
- * Compile all the regular expression patterns in a pattern table.
- * A pattern table is an array of pattern records.
- * Each pattern record consists of a regular
- * expression, a buffer for the to-be-compiled regular expression,
- * and an array to associate a given token with a matched register number.
- */
- static void compile(patternTable)
- PatternRec *patternTable;
- {
- PatternRec *p;
- char fastmap[(1 << BYTEWIDTH)];
- int i;
-
- for (i=0; patternTable[i].pat; i++) {
- p = &patternTable[i];
- p->buf = (struct re_pattern_buffer *)
- XtMalloc (sizeof (struct re_pattern_buffer));
- p->buf->allocated = RE_BUFFER;
- p->buf->buffer = (char *) XtMalloc (p->buf->allocated);
- p->buf->fastmap = fastmap;
- p->buf->translate = NULL;
- re_compile_pattern(p->pat, strlen(p->pat), p->buf);
- re_compile_fastmap(p->buf);
- }
- }
-
- /*
- * This routine tries to match a given string with each regular
- * expression in a given pattern table. The best match is found, and
- * the function returns an index to the pattern table.
- */
- static int match(patternTable, string)
- PatternRec *patternTable;
- char *string;
- {
- struct re_registers regs;
- int m, bestmatch = -1, index = -1, i, j, r, start, n;
- char *s;
-
- if (strcmp(string, "") == NULL) return -1;
- for (i=0; patternTable[i].pat; i++) {
- m = re_match(patternTable[i].buf, string, strlen(string), 0, ®s);
- if (m > bestmatch) {
- bestmatch = m;
- index = i;
- token.mesg = token.file = token.func = NULL;
- token.line = token.stop = 0;
- for (j=0; j<NTOKENS; j++) {
- if ((r = patternTable[i].reg_token[j]) >= 0) {
- start = regs.start[r];
- if ((n = regs.end[r] - start) > 0) {
- s = (char *) XtMalloc ((n+1) * sizeof(char));
- strncpy(s, string+start, n);
- s[n] = '\0';
- switch (j) {
- case 0: token.mesg = s; break;
- case 1: token.stop = atoi(s); XtFree(s); break;
- case 2: token.func = s; break;
- case 3: token.line = atoi(s); XtFree(s); break;
- case 4: token.file = s; break;
- }
- }
- }
- }
- }
- }
- return index;
- }
-
- /* PUBLIC ROUTINES */
-
- /* Compile the regular expressions in the output and command pattern tables. */
-
- void parser_init()
- {
- compile(output_pattern);
- compile(command_pattern);
- }
-
-
- /* This routine first parses the command string.
- * If the command is one of run, cont, next, step, stop at, stop in,
- * where, up, or down, it parses the dbx output to decide what action
- * to take and dispatch it to one of the handlers.
- * For other commands, the appropriate handler is called.
- */
- void parse(output)
- char *output;
- {
- int handler, command_flag;
-
- command_flag = match(command_pattern, Command);
- switch (command_flag) {
- case C_EXEC:
- case C_STOPAT:
- case C_STOPIN:
- case C_UPDOWN:
- handler = match(output_pattern, output);
- switch (handler) {
- case O_EXEC : exec_handler(&token, FALSE); break;
- case O_DONE : exec_handler(&token, TRUE); break;
- case O_STOPAT: stop_at_handler(&token); break;
- case O_STOPIN: stop_in_handler(&token); break;
- case O_UPDOWN: updown_handler(&token); break;
- case O_BELL : Bell(0); break;
- }
- break;
- case C_DELETE:
- delete_handler();
- break;
- case C_FUNC:
- if (strcmp(output, "") == NULL)
- func_handler(&token);
- break;
- case C_FILE:
- file_handler(&token);
- break;
- case C_DEBUG:
- debug_handler();
- break;
- case C_CD:
- if (strcmp(output, "") == NULL)
- cd_handler();
- break;
- }
- }
-
-
- /* This function edits the dbx output so that unnecessary information is
- * not displayed on the dialog window.
- * It filters away the information about the halted execution point,
- * returned by the execution commands.
- * On Sun dbx, it also filters away the output returned by the up and
- * down commands.
- */
- void filter(s)
- char *s;
- {
- struct re_registers regs;
- char *p, *q;
- int r;
- static Boolean DeleteNextLine = FALSE;
-
- if ((s && strcmp(s, "") == NULL) || strcmp(Command, "") == NULL)
- return;
-
- #ifndef BSD /* for Sun dbx only */
- if (re_match(command_pattern[C_UPDOWN].buf, Command, strlen(Command),
- 0, NULL) > 0) {
- if (re_match(output_pattern[O_BELL].buf, s, strlen(s), 0, ®s) < 0) {
- if (strcmp(s+strlen(s)-strlen(PROMPT), PROMPT) == NULL)
- strcpy(s, PROMPT);
- else
- strcpy(s, "");
- }
- return;
- }
- #endif
-
- if (re_match(command_pattern[C_EXEC].buf, Command, strlen(Command),
- 0, NULL) > 0
- && re_match(output_pattern[O_EXEC].buf, s, strlen(s), 0, ®s) > 0) {
- r = output_pattern[O_EXEC].reg_token[TK_MESG];
- for (p=s+regs.start[r]; p!=s && *(p-1) != '\n'; p--);
- if (q = (char *) strchr(s+regs.end[r]+1, '\n'))
- strcpy(p, q+1);
- else {
- DeleteNextLine = TRUE;
- *p = '\0';
- }
- return;
- }
-
- if (DeleteNextLine) {
- p = (char *) strchr(s, '\n');
- strcpy(s, p+1);
- DeleteNextLine = FALSE;
- }
- }
-
-
- /* Sends a command to dbx and read the corresponding output, bypassing the
- * Xt input procedure, readDbx.
- */
- int QueryDbx(command, do_match)
- char *command;
- Boolean do_match;
- {
- char s[LINESIZ], *output;
- int outputsize;
-
- Echo = FALSE;
- writeDbx(command);
- /* allocate one block to 'output' to begin with */
- outputsize = BUFSIZ;
- output = XtMalloc(outputsize);
- strcpy(output, "");
-
- while (fgets(s, LINESIZ, dbxfp) == NULL);
- do {
- if (strlen(output) + strlen(s) > outputsize) {
- output = XtRealloc(output, outputsize + BUFSIZ);
- outputsize += BUFSIZ;
- }
- strcat(output, s);
- } while (fgets(s, LINESIZ, dbxfp));
- Echo = TRUE;
- if (do_match)
- return match(output_pattern, output);
- else
- return -1;
- }
-