"Fossies" - the Fresh Open Source Software Archive 
Member "xxgdb-1.12/parser.c" (16 Nov 1994, 12916 Bytes) of package /linux/misc/old/xxgdb-1.12.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
1 /*****************************************************************************
2 *
3 * xdbx - X Window System interface to the dbx debugger
4 *
5 * Copyright 1989 The University of Texas at Austin
6 * Copyright 1990 Microelectronics and Computer Technology Corporation
7 *
8 * Permission to use, copy, modify, and distribute this software and its
9 * documentation for any purpose and without fee is hereby granted,
10 * provided that the above copyright notice appear in all copies and that
11 * both that copyright notice and this permission notice appear in
12 * supporting documentation, and that the name of The University of Texas
13 * and Microelectronics and Computer Technology Corporation (MCC) not be
14 * used in advertising or publicity pertaining to distribution of
15 * the software without specific, written prior permission. The
16 * University of Texas and MCC makes no representations about the
17 * suitability of this software for any purpose. It is provided "as is"
18 * without express or implied warranty.
19 *
20 * THE UNIVERSITY OF TEXAS AND MCC DISCLAIMS ALL WARRANTIES WITH REGARD TO
21 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
22 * FITNESS, IN NO EVENT SHALL THE UNIVERSITY OF TEXAS OR MCC BE LIABLE FOR
23 * ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
24 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
25 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
26 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
27 *
28 * Author: Po Cheung
29 * Created: March 10, 1989
30 *
31 *****************************************************************************
32 *
33 * xxgdb - X Window System interface to the gdb debugger
34 *
35 * Copyright 1990,1993 Thomson Consumer Electronics, Inc.
36 *
37 * Permission to use, copy, modify, and distribute this software and its
38 * documentation for any purpose and without fee is hereby granted,
39 * provided that the above copyright notice appear in all copies and that
40 * both that copyright notice and this permission notice appear in
41 * supporting documentation, and that the name of Thomson Consumer
42 * Electronics (TCE) not be used in advertising or publicity pertaining
43 * to distribution of the software without specific, written prior
44 * permission. TCE makes no representations about the suitability of
45 * this software for any purpose. It is provided "as is" without express
46 * or implied warranty.
47 *
48 * TCE DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
49 * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT
50 * SHALL TCE BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES
51 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
52 * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
53 * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
54 * SOFTWARE.
55 *
56 * Adaptation to GDB: Pierre Willard
57 * XXGDB Created: December, 1990
58 *
59 *****************************************************************************/
60
61 /* parser.c:
62 *
63 * Parse output messages from dbx using regular expression pattern matching,
64 * and take appropriate action.
65 *
66 * compile(): Compile the regular expressions in a table.
67 * match(): Try to best match a given string with the regular
68 * expressions found in the table and return an index.
69 * parser_init(): Initialization.
70 * parse(): Parse the dbx output and invoke the appropriate action
71 * handler.
72 * filter(): Modify the dbx output before it gets displayed on the
73 * dialog window.
74 * query_dbx(): Send a query command to dbx and process it.
75 */
76
77 #include <stdlib.h>
78
79 #include "global.h"
80 #include "regex.h"
81 #ifdef GDB
82 #include "gdb_regex.h"
83 #else
84 #ifdef BSD
85 #ifdef MIPS
86 #include "mips_regex.h"
87 #else
88 #include "bsd_regex.h"
89 #endif
90 #else
91 #include "sun_regex.h"
92 #endif
93 #endif /* GDB */
94
95 #define BYTEWIDTH 8
96 #define RE_BUFFER 100
97
98 Tokens Token; /* gloabl token structure */
99 #ifndef GDB
100 Boolean Parse = True; /* Parse flag for parse routine */
101 #endif
102
103 /*
104 * Compile all the regular expression patterns in a pattern table.
105 * A pattern table is an array of pattern records.
106 * Each pattern record consists of a regular
107 * expression, a buffer for the to-be-compiled regular expression,
108 * and an array to associate a given token with a matched register number.
109 */
110 static void compile(patternTable)
111 PatternRec *patternTable;
112 {
113 PatternRec *p;
114 char fastmap[(1 << BYTEWIDTH)];
115 int i;
116
117 for (i=0; patternTable[i].pat; i++) {
118 p = &patternTable[i];
119 p->buf = (struct re_pattern_buffer *)
120 XtMalloc (sizeof (struct re_pattern_buffer));
121 p->buf->allocated = RE_BUFFER;
122 p->buf->buffer = (char *) XtMalloc (p->buf->allocated);
123 p->buf->fastmap = fastmap;
124 p->buf->translate = NULL;
125 re_compile_pattern(p->pat, strlen(p->pat), p->buf);
126 re_compile_fastmap(p->buf);
127 }
128 }
129
130 /*
131 * This routine tries to match a given string with each regular
132 * expression in a given pattern table. The best match is found, and
133 * the function returns an index to the pattern table.
134 */
135 #ifndef GDB /* for GDB, match is called from gdb_parser.c */
136 static
137 #endif
138 int match(patternTable, string, type)
139 PatternRec *patternTable;
140 char *string;
141 int type;
142 {
143 struct re_registers regs;
144 int m, bestmatch = -1, index = -1, i, j, r, start, n;
145 char *s;
146
147 if (strcmp(string, "") == 0) return -1;
148 for (i=0; patternTable[i].pat; i++) {
149 if (type != C_ANY && type != i)
150 continue;
151 m = re_match(patternTable[i].buf, string, strlen(string), 0, ®s);
152 if (m == -2 ) { /* match error - failure stack overflow */
153 #ifdef GDB
154 fprintf(stderr, "xxgdb error: regular expression matching failed \
155 (failure stack overflow)\n");
156 #else
157 fprintf(stderr, "xdbx error: regular expression matching failed \
158 (failure stack overflow)\n");
159 #endif
160 return (-1);
161 }
162 if (m > bestmatch) {
163 bestmatch = m;
164 index = i;
165 #ifdef GDB
166 /* for GDB, free memory (if not done earlier) */
167 XtFree(Token.mesg);
168 XtFree(Token.file);
169 XtFree(Token.func);
170 XtFree(Token.display);
171 #endif /* GDB */
172 Token.mesg = Token.file = Token.func = Token.display = NULL;
173 Token.line = Token.stop = 0;
174 for (j=0; j<NTOKENS; j++) {
175 if ((r = patternTable[i].reg_token[j]) >= 0) {
176 start = regs.start[r];
177 if ((n = regs.end[r] - start) > 0) {
178 #ifdef GDB
179 /* The following error could happen if the pattern table is not correct,
180 better test it here.. */
181
182 if ( n > strlen(string)) /* Something is wrong here ! */
183 {
184 fprintf(stderr,"Error match() : n = %d is too big\n",n);
185 n = 0;
186 }
187 #endif /* GDB */
188 s = (char *) XtMalloc ((n+1) * sizeof(char));
189 strncpy(s, string+start, n);
190 s[n] = '\0';
191 switch (j) {
192 case TK_MESG: Token.mesg = s; break;
193 case TK_STOP: Token.stop = atoi(s); XtFree(s); break;
194 case TK_FUNC: Token.func = s; break;
195 case TK_LINE: Token.line = atoi(s); XtFree(s); break;
196 case TK_FILE: Token.file = s; break;
197 case TK_DISP: Token.display = s; break;
198 }
199 }
200 }
201 }
202 }
203 }
204 return index;
205 }
206
207 /* Compile the regular expressions in the output and command pattern tables. */
208
209 void parser_init()
210 {
211 compile(output_pattern);
212 compile(command_pattern);
213 compile(dataPattern);
214 }
215
216
217 #ifdef GDB
218
219 #include "gdb_parser.c"
220
221 #else /*>>>>>>>>>> ALL THE FOLLOWING IS NOT COMPILED FOR GDB <<<<<<<<<<<<<<<<<<<*/
222
223 /* This routine first parses the command string.
224 * If the command is one of run, cont, next, step, stop at, stop in,
225 * where, up, or down, it parses the dbx output to decide what action
226 * to take and dispatch it to one of the handlers.
227 * For other commands, the appropriate handler is called.
228 *
229 * !!! This routine has to be re-entrant.
230 */
231 void parse(output, command)
232 char *output;
233 char *command;
234 {
235 int command_type;
236 char *output_string;
237
238 if (debug) {
239 fprintf(stderr, "parse(output = %s, command = %s)\n", output, command);
240 }
241
242 /* Make a local copy of `output' and use that instead */
243 output_string = XtNewString(output);
244 if (output) strcpy(output, "");
245
246 if (!command) {
247 if (match(output_pattern, output_string, O_DEBUG) != -1)
248 debug_handler();
249 debug_init();
250 return;
251 }
252 if (!Parse)
253 return;
254
255 command_type = match(command_pattern, command, C_ANY);
256 switch (command_type) {
257 case C_EXEC:
258 if (match(output_pattern, output_string, O_EXEC_GDB) != -1)
259 exec_handler();
260 else if (match(output_pattern, output_string, O_DONE) != -1)
261 done_handler();
262 else
263 bell(0);
264 break;
265 case C_STOPAT:
266 if (match(output_pattern, output_string, O_STOPAT) != -1)
267 stop_at_handler();
268 else
269 bell(0);
270 break;
271 case C_STOPIN:
272 if (match(output_pattern, output_string, O_STOPIN) != -1)
273 stop_in_handler();
274 else
275 bell(0);
276 break;
277 case C_UPDOWN:
278 if (match(output_pattern, output_string, O_UPDOWN) != -1)
279 updown_handler();
280 else
281 bell(0);
282 break;
283 case C_SEARCH:
284 if (match(output_pattern, output_string, O_SEARCH) != -1)
285 search_handler();
286 else
287 bell(0);
288 break;
289 case C_DELETE:
290 delete_handler();
291 break;
292 case C_FILE:
293 if (match(output_pattern, output_string, O_FILE) != -1)
294 file_handler(); /* command was 'file' */
295 else
296 LoadCurrentFile(); /* command was 'file ...' */
297 break;
298 case C_LIST:
299 if (match(output_pattern, output_string, O_LIST) != -1)
300 list_handler();
301 else
302 bell(0);
303 break;
304 case C_FUNC:
305 #ifdef MIPS
306 if (match(output_pattern, output_string, O_FUNC) != -1)
307 #else
308 if (strcmp(output_string, "") == 0)
309 #endif
310 func_handler();
311 else
312 bell(0);
313 break;
314 case C_USE:
315 use_handler(output_string);
316 break;
317
318 #ifndef BSD
319 case C_PRINT:
320 if (match(output_pattern, output_string, O_PRINT) != -1)
321 print_handler(output_string);
322 else
323 bell(0);
324 break;
325 case C_DEBUG:
326 if (match(output_pattern, output_string, O_DEBUG) != -1)
327 debug_handler();
328 else
329 bell(0);
330 break;
331 case C_CD:
332 if (strcmp(output_string, "") == 0)
333 cd_handler();
334 else
335 bell(0);
336 break;
337 case C_PWD:
338 pwd_handler(output_string);
339 break;
340 case C_DISPLAY:
341 if (strcmp(output_string, "") == 0 ||
342 match(output_pattern, output_string, O_PRINT) != -1)
343 display_handler();
344 else
345 bell(0);
346 break;
347 #endif
348 #ifdef BSD
349 case C_STATUS:
350 break;
351 #endif
352
353 default:
354 break;
355 }
356 XtFree(output_string);
357 }
358
359 /* This function edits the dbx output so that unnecessary information is
360 * not displayed on the dialog window.
361 * It filters away the some output returned by the execution commands;
362 * output from the search commands, and the display command.
363 * On Sun dbx, it also filters away part of the output returned by the
364 * up and down commands.
365 */
366 void filter(string, output, command)
367 char *string, *output, *command;
368 {
369 struct re_registers regs;
370 char *p;
371 int r;
372 static Boolean deleteRest = False;
373 int command_type = -1;
374
375 if (output == NULL || strcmp(output, "") == 0)
376 return;
377
378
379 #ifdef BSD
380 if (!command) {
381 AppendDialogText(string);
382 return;
383 }
384 #endif
385
386 if (command)
387 command_type = match(command_pattern, command, C_ANY);
388 if (command_type == C_EXEC) {
389 if (re_match(output_pattern[O_EXEC_GDB].buf, string, strlen(string), 0,
390 ®s) > 0) {
391 r = output_pattern[O_EXEC_GDB].reg_token[TK_MESG];
392 for (p=string+regs.start[r]; p!=string && *(p-1) != '\n'; p--);
393 strcpy(p, "");
394 if (!Prompt)
395 deleteRest = True;
396 }
397 else if (deleteRest) {
398 strcpy(string, "");
399 if (Prompt)
400 deleteRest = False;
401 }
402 AppendDialogText(string);
403 return;
404 }
405
406 if (Prompt) {
407 char *s;
408
409 s = XtNewString(output);
410 switch (command_type) {
411 #ifndef BSD
412 case C_UPDOWN:
413 if (match(output_pattern, s, O_UPDOWN) != -1)
414 strcpy(s, Token.mesg);
415 break;
416 case C_DISPLAY:
417 if (match(output_pattern, s, O_PRINT) != -1)
418 strcpy(s, "");
419 break;
420 #endif
421 #ifdef MIPS
422 case C_UPDOWN:
423 if (match(output_pattern, s, O_UPDOWN) != -1)
424 strcpy(s, Token.mesg);
425 strcat(s, "\n");
426 break;
427 case C_FUNC:
428 if (match(output_pattern, s, O_FUNC) != -1)
429 strcpy(s, "");
430 break;
431 #endif
432 case C_SEARCH:
433 if (match(output_pattern, s, O_SEARCH) != -1)
434 strcpy(s, "");
435 break;
436 case C_LIST:
437 if (match(output_pattern, s, O_LIST) != -1)
438 strcpy(s, "");
439 break;
440 default:
441 s = XtNewString(string); /* append 'string' only */
442 break;
443 }
444 AppendDialogText(s);
445 XtFree(s);
446 }
447 else {
448 switch (command_type) {
449 #ifndef BSD
450 case C_UPDOWN:
451 case C_DISPLAY:
452 #endif
453 #ifdef MIPS
454 case C_UPDOWN:
455 case C_FUNC:
456 #endif
457 case C_SEARCH:
458 case C_LIST:
459 break;
460 default:
461 AppendDialogText(string);
462 break;
463 }
464 }
465 }
466 #endif /* NOT GDB */