File: parser.c

package info (click to toggle)
xxgdb 1.12-13
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 596 kB
  • ctags: 782
  • sloc: ansic: 8,600; makefile: 48; sh: 10
file content (466 lines) | stat: -rw-r--r-- 12,916 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
/*****************************************************************************
 *
 *  xdbx - X Window System interface to the dbx debugger
 *
 *  Copyright 1989 The University of Texas at Austin
 *  Copyright 1990 Microelectronics and Computer Technology Corporation
 *
 *  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, and that the name of The University of Texas
 *  and Microelectronics and Computer Technology Corporation (MCC) not be 
 *  used in advertising or publicity pertaining to distribution of
 *  the software without specific, written prior permission.  The
 *  University of Texas and MCC makes no representations about the 
 *  suitability of this software for any purpose.  It is provided "as is" 
 *  without express or implied warranty.
 *
 *  THE UNIVERSITY OF TEXAS AND MCC DISCLAIMS ALL WARRANTIES WITH REGARD TO
 *  THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
 *  FITNESS, IN NO EVENT SHALL THE UNIVERSITY OF TEXAS OR MCC BE LIABLE FOR
 *  ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
 *  RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
 *  CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
 *  CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 *  Author:  	Po Cheung
 *  Created:   	March 10, 1989
 * 
 *****************************************************************************
 * 
 *  xxgdb - X Window System interface to the gdb debugger
 *  
 * 	Copyright 1990,1993 Thomson Consumer Electronics, Inc.
 *  
 *  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, and that the name of Thomson Consumer
 *  Electronics (TCE) not be used in advertising or publicity pertaining
 *  to distribution of the software without specific, written prior
 *  permission.  TCE makes no representations about the suitability of
 *  this software for any purpose.  It is provided "as is" without express
 *  or implied warranty.
 *
 *  TCE DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
 *  ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT
 *  SHALL TCE BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES
 *  OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
 *  WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
 *  ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
 *  SOFTWARE.
 *
 *  Adaptation to GDB:  Pierre Willard
 *  XXGDB Created:   	December, 1990
 *
 *****************************************************************************/

/*  parser.c:
 *
 *    Parse output messages from dbx using regular expression pattern matching,
 *    and take appropriate action.
 *
 *    compile():	Compile the regular expressions in a table.
 *    match():		Try to best match a given string with the regular
 *			expressions found in the table and return an index.
 *    parser_init():	Initialization.
 *    parse():		Parse the dbx output and invoke the appropriate action
 *			handler.
 *    filter():		Modify the dbx output before it gets displayed on the
 *			dialog window.
 *    query_dbx():	Send a query command to dbx and process it.
 */

#include <stdlib.h>

#include	"global.h"
#include	"regex.h"
#ifdef GDB
#include	"gdb_regex.h"
#else
#ifdef BSD
#ifdef MIPS
#include	"mips_regex.h"
#else
#include	"bsd_regex.h"
#endif
#else
#include	"sun_regex.h"
#endif
#endif /* GDB */

#define BYTEWIDTH       8
#define RE_BUFFER       100

Tokens 	Token;			/* gloabl token structure */
#ifndef GDB
Boolean	Parse = True;		/* Parse flag for parse routine */
#endif

/*
 *  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.
 */
#ifndef GDB	/* for GDB, match is called from gdb_parser.c */
static
#endif
int match(patternTable, string, type)
    PatternRec 	*patternTable;
    char 	*string;
    int		type;
{
    struct re_registers	regs;
    int			m, bestmatch = -1, index = -1, i, j, r, start, n;
    char 		*s;

    if (strcmp(string, "") == 0) return -1;
    for (i=0; patternTable[i].pat; i++) {
	if (type != C_ANY && type != i)
	    continue;
	m = re_match(patternTable[i].buf, string, strlen(string), 0, &regs);
	if (m == -2 ) {		/* match error - failure stack overflow */
#ifdef GDB
	    fprintf(stderr, "xxgdb error: regular expression matching failed \
(failure stack overflow)\n");
#else
	    fprintf(stderr, "xdbx error: regular expression matching failed \
(failure stack overflow)\n");
#endif
	    return (-1);
	}
	if (m > bestmatch) {
	    bestmatch = m;
	    index = i;
#ifdef GDB
		/* for GDB, free memory (if not done earlier) */
		XtFree(Token.mesg);
		XtFree(Token.file);
		XtFree(Token.func);
		XtFree(Token.display);
#endif /* GDB */
	    Token.mesg = Token.file = Token.func = Token.display = 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) {
#ifdef GDB
		/* The following error could happen if the pattern table is not correct,
		better test it here.. */
		
			if ( n > strlen(string))	/* Something is wrong here ! */
				{
				fprintf(stderr,"Error match() : n = %d is too big\n",n);
				n = 0;
				}
#endif /* GDB */
			s = (char *) XtMalloc ((n+1) * sizeof(char));
			strncpy(s, string+start, n);
			s[n] = '\0';
		    	switch (j) {
			  case TK_MESG: Token.mesg = s; break;
			  case TK_STOP: Token.stop = atoi(s); XtFree(s); break;
			  case TK_FUNC: Token.func = s; break;
			  case TK_LINE: Token.line = atoi(s); XtFree(s); break;
			  case TK_FILE: Token.file = s; break;
			  case TK_DISP: Token.display = s; break;
		    	}
		    }
		}
	    }
	}
    }
    return index;
}

/*  Compile the regular expressions in the output and command pattern tables. */

void parser_init()
{
    compile(output_pattern);
    compile(command_pattern);
    compile(dataPattern);
}


#ifdef GDB

#include "gdb_parser.c"

#else /*>>>>>>>>>> ALL THE FOLLOWING IS NOT COMPILED FOR GDB <<<<<<<<<<<<<<<<<<<*/

/*  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.
 *
 *  !!! This routine has to be re-entrant.
 */
void parse(output, command)
char *output;
char *command;
{
    int  command_type;
    char *output_string;

    if (debug) {
	fprintf(stderr, "parse(output = %s, command = %s)\n", output, command);
    }

    /* Make a local copy of `output' and use that instead */
    output_string = XtNewString(output);
    if (output) strcpy(output, "");

    if (!command) {
	if (match(output_pattern, output_string, O_DEBUG) != -1)
	    debug_handler(); 
	debug_init();
	return;
    }
    if (!Parse)
	return;
	
    command_type = match(command_pattern, command, C_ANY);
    switch (command_type) {
      	case C_EXEC: 
	    if (match(output_pattern, output_string, O_EXEC_GDB) != -1)
		exec_handler();
	    else if (match(output_pattern, output_string, O_DONE) != -1)
		done_handler();
	    else
		bell(0);
	    break;
	case C_STOPAT: 
	    if (match(output_pattern, output_string, O_STOPAT) != -1)
		stop_at_handler();
	    else
		bell(0);
	    break;
	case C_STOPIN: 
	    if (match(output_pattern, output_string, O_STOPIN) != -1)
		stop_in_handler();
	    else
		bell(0);
	    break;
	case C_UPDOWN:
	    if (match(output_pattern, output_string, O_UPDOWN) != -1)
		updown_handler();
	    else
		bell(0);
	    break;
	case C_SEARCH:
	    if (match(output_pattern, output_string, O_SEARCH) != -1)
		search_handler();
	    else
		bell(0);
	    break;
      	case C_DELETE:
	    delete_handler(); 
	    break;
	case C_FILE:
	    if (match(output_pattern, output_string, O_FILE) != -1)
	    	file_handler();		/* command was 'file' */
	    else
	    	LoadCurrentFile();	/* command was 'file ...' */
	    break;
	case C_LIST:
	    if (match(output_pattern, output_string, O_LIST) != -1)
	        list_handler();
	    else
		bell(0);
	    break;
	case C_FUNC:
#ifdef MIPS
	    if (match(output_pattern, output_string, O_FUNC) != -1)
#else
	    if (strcmp(output_string, "") == 0)
#endif
	    	func_handler(); 
	    else
		bell(0);
	    break;
	case C_USE:
	    use_handler(output_string); 
	    break;
	    
#ifndef BSD
	case C_PRINT:
	    if (match(output_pattern, output_string, O_PRINT) != -1)
	    	print_handler(output_string);
	    else
		bell(0);
	    break;
	case C_DEBUG:
	    if (match(output_pattern, output_string, O_DEBUG) != -1)
		debug_handler(); 
	    else
		bell(0);
	    break;
	case C_CD:
	    if (strcmp(output_string, "") == 0)
	    	cd_handler(); 
	    else
		bell(0);
	    break;
	case C_PWD:
	    pwd_handler(output_string);
	    break;
	case C_DISPLAY:
	    if (strcmp(output_string, "") == 0 ||
	    	match(output_pattern, output_string, O_PRINT) != -1)
	    	display_handler();
	    else
		bell(0);
	    break;
#endif
#ifdef BSD
	case C_STATUS:
	    break;
#endif

	default:
	    break;
    }
    XtFree(output_string);
}

/*  This function edits the dbx output so that unnecessary information is 
 *  not displayed on the dialog window.
 *  It filters away the some output returned by the execution commands; 
 *  output from the search commands, and the display command.
 *  On Sun dbx, it also filters away part of the output returned by the 
 *  up and down commands.
 */
void filter(string, output, command)
char *string, *output, *command;
{
    struct re_registers regs;
    char 		*p;
    int			r;
    static Boolean	deleteRest = False;
    int			command_type = -1;

    if (output == NULL || strcmp(output, "") == 0) 
	return;


#ifdef BSD
    if (!command) {
	AppendDialogText(string);
	return;
    }
#endif

    if (command)
    	command_type = match(command_pattern, command, C_ANY);
    if (command_type == C_EXEC) {
	if (re_match(output_pattern[O_EXEC_GDB].buf, string, strlen(string), 0, 
	&regs) > 0) {
	    r = output_pattern[O_EXEC_GDB].reg_token[TK_MESG];
            for (p=string+regs.start[r]; p!=string && *(p-1) != '\n'; p--);
	    strcpy(p, "");
	    if (!Prompt)
		deleteRest = True;
	}
	else if (deleteRest) {
	    strcpy(string, "");
	    if (Prompt)
		deleteRest = False;
	}
	AppendDialogText(string);
	return;
    }

    if (Prompt) {
	char *s;

	s = XtNewString(output);
	switch (command_type) {
#ifndef BSD
	case C_UPDOWN:
	    if (match(output_pattern, s, O_UPDOWN) != -1)
		strcpy(s, Token.mesg);    
	    break;
	case C_DISPLAY:
	    if (match(output_pattern, s, O_PRINT) != -1)
		strcpy(s, "");
	    break;
#endif
#ifdef MIPS
	case C_UPDOWN:
	    if (match(output_pattern, s, O_UPDOWN) != -1)
		strcpy(s, Token.mesg);    
		strcat(s, "\n");
	    break;
	case C_FUNC:
	    if (match(output_pattern, s, O_FUNC) != -1)
		strcpy(s, "");
	    break;
#endif
	case C_SEARCH:
	    if (match(output_pattern, s, O_SEARCH) != -1)
		strcpy(s, "");
	    break;
	case C_LIST:
	    if (match(output_pattern, s, O_LIST) != -1)
		strcpy(s, "");
	    break;
	default:
	    s = XtNewString(string);		/* append 'string' only */
	    break;
	}
	AppendDialogText(s);
	XtFree(s);
    }
    else {
	switch (command_type) {
#ifndef BSD
	case C_UPDOWN:
	case C_DISPLAY:
#endif
#ifdef MIPS
	case C_UPDOWN:
	case C_FUNC:
#endif
	case C_SEARCH:
	case C_LIST:
	    break;
	default:
	    AppendDialogText(string);
	    break;
	}
    }
}
#endif /* NOT GDB */