File: gdb_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 (1381 lines) | stat: -rw-r--r-- 37,972 bytes parent folder | download | duplicates (6)
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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
/*****************************************************************************
 *
 *  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
 *
 *****************************************************************************/

/*  gdb_parser.c:
 *
 *	WARNING : gdb_parser.c is included by parser.c for GDB.
 *
 *    Parse output messages from dbx using regular expression pattern matching,
 *    and take appropriate action.
 *
 *    parse():		Parse the dbx output and invoke the appropriate action
 *			handler.
 *    filter():		Modify the dbx output before it gets displayed on the
 *			dialog window.
 *    gdb_source_command():	Test for source command.
 */

/*
 * iand	94/02/10 cope better with non-blocking I/O.  Exit when pty is closed rather
 *		 than spinning in a loop.
 *
 */

#include 	<string.h>
#include 	<stdio.h>
#include	<errno.h>

extern Boolean	Prompt;			/* True when gdb prompt arrives */

/*--------------------------------------------------------------------------+
|																			|
|	Function to output a message and ring bell when a answer				|
|	from gdb has not been recognized (possibly because of bug				|
|	in xxgdb which does not anticipate this particular answer).				|
|																			|
|	ECHO_ON means that all the output from gdb was displayed				|
|	on the dialog window (so no need to display any error message).			|
|	In particular we can arrive here because the user typed a				|
|	wrong command.															|
|																			|
|	ECHO_OFF means that the gdb command was issued internally by			|
|	xxgdb, and that the user has no knowledge of it.						|
|																			|
|	ECHO_ON and FILTER_ON means that only part of the						|
|	gdb answer to the command is displayed in the dialog window.			|
|	What is displayed or not is chosen by the filter() function.			|
|	In fact, filter() in this case will display gdb error messages			|
|	from the command sent internally by xxgdb.								|
|																			|
|	This function will only display error messages when echo and			|
|	filter are off. This is more for xxgdb debug than for the user.			|
|																			|
+--------------------------------------------------------------------------*/	
void unknown_output (outputstr, command, flags)
char *outputstr;
char *command;
int flags;
{
	if (command)
		{
		if (debug)
			fprintf(stderr, "\noutput from \"%s\" is not recognized\n", command);
	
		/* if the command was completely silent, we output this
		error message */
		
		if ((flags & (ECHO_ON | FILTER_ON)) == 0)
			{
			AppendDialogText("xxgdb error: output from \"");
			AppendDialogText(command);
			AppendDialogText("\" command is not recognized.\n");
			}
		}
		
	if (outputstr)
		if ((flags & (ECHO_ON | FILTER_ON)) == 0)
			AppendDialogText(outputstr);

	bell(0);	/* ring the bell in ALL cases */
}

/*--------------------------------------------------------------------------+
|																			|
|	Function to remove all 'Reading in symbols' message						|
|	from a string.															|
|																			|
|	This function is used in parser() before matching the output			|
|	because this message can happen any time.								|
|																			|
+--------------------------------------------------------------------------*/
void filter_reading_symbols(output)
char *output;
{
struct re_registers regs;
int	r;
char *p1;
char *p2;

		/* test for reading symbols message */
	
	while (re_match(output_pattern[O_READING_SYMBOLS].buf,output,strlen(output),0,&regs) > 0)
		{
		/* we found a "Reading in symbols for 	...done." pattern */
		
		r = output_pattern[O_READING_SYMBOLS].reg_token[TK_MESG];
		p1=  output+regs.start[r];
		p2 = output+regs.end[r];
		
		/* remove "Reading in symbols for 	...done." */
		
		while((*(p1++) = *(p2++)));
		}
}

/*--------------------------------------------------------------------------+
|																			|
| *	 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, flags)
char *output;
char *command;
int flags;
{
    int  command_type;
    char *output_string;

    if (debug) {
    char *temp;
    if(!command)temp="";else temp=command;
	fprintf(stderr, "parse(output = %s, command = %s, flags = %d)\n", output, temp, flags);
    }

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

	/* test for GDB start-up */

    if (!command) {
/* (PW)28AUG91 : do no test for O_DEBUG pattern because of gdb 4.0
who most of the times displays nothing before the promt.
if (match(output_pattern, output_string, O_DEBUG) != -1)
*/
		{
   		query_gdb_directories();	/* will tell if running gdb 4.0 */
   		
	    debug_handler();	/* some init to gdb, and try to display main() */
	    
	    /* test if a core file was used in input arguments,
	    an display bomb if necessary. */
	    
	    if (match(output_pattern, output_string, O_CORE_FILE) != -1)
			core_file_handler();
	    }
	debug_init();			/* read .gdbinit file (if any) */
	XtFree(output_string);
	return;
    }
    
    /* if not GDB start-up */
    
	if (match(output_pattern, output_string, O_BELL) != -1)
		{
		/* test if this is 'show undefined'. If yes then we are
		not executing the new gdb (4.0).
			(see show_is_undefined in gdb_handler.c)
		*/
		if (match(output_pattern, output_string, O_UNDEF_SHOW) != -1)
			show_is_undefined();
		else
			unknown_output (output_string,command, flags);
    	XtFree(output_string);
		return;
		}
	
    command_type = match(command_pattern, command, C_ANY);
    
    /* remove all "Reading in symbols for pw.c...done."  */

	filter_reading_symbols(output_string);
        	
    switch (command_type) {
      	case C_EXEC: 
		case C_FINISH:
		{
		char * message;
		int signal = 0;
		if (debug) {
			fprintf(stderr, "C_EXEC or C_FINISH\n");
		}
		message = 0;
		if (match(output_pattern, output_string, O_RECEIVED_SIGNAL) != -1)
			{
			message = XtNewString(Token.mesg);
			signal = Token.stop;	/* signal number received */
			}
			
		/* warning : the order of the matching tests is important */
		
	    if ((match(output_pattern, output_string, O_EXEC_MESS_AFTER) != -1)
	    	|| (match(output_pattern, output_string, O_EXEC_MESS_BEFORE) != -1)
	    	|| (match(output_pattern, output_string, O_EXEC_GDB) != -1))
			{
			exec_handler(message,signal);
			}
	    else 
	    	{
			if (match(output_pattern, output_string, O_DONE) != -1)
				done_handler(message,signal);
			else
				unknown_output(output_string, command, flags);
			}
			
		if (message)
			{
			bell(0);
			XtFree(message);
			}
		}
	    break;
	    
	case C_UPDOWN:
		if (debug) {
			fprintf(stderr, "C_UPDOWN\n");
		}
	    if (match(output_pattern, output_string, O_UPDOWN) != -1)
			updown_handler();
	    else if (match(output_pattern, output_string, O_UPDOWN_NOSOURCE) != -1)
			/* here Token.msg and Token.func are updated. Do nothing with
			them. We are in a function with no source information. */
			bell(0);
		else
			unknown_output (output_string, command, flags);
	    break;
	case C_SEARCH:
		if (debug) {
			fprintf(stderr, "C_SEARCH\n");
		}
	    if (match(output_pattern, output_string, O_SEARCH) != -1)
		search_handler();
	    else
			unknown_output(output_string, command, flags);
	    break;
	case C_DELETE:
		if (debug) {
			fprintf(stderr, "C_DELETE\n");
		}
	    delete_handler(); 
	    break;
	case C_LIST:
		if (debug) {
			fprintf(stderr, "C_LIST\n");
		}
	    if (match(output_pattern, output_string, O_LIST) != -1)
	        list_handler();
	    else
			unknown_output(output_string, command, flags);
	    break;
	    
	case C_BREAK: 
		if (debug) {
			fprintf(stderr, "C_BREAK\n");
		}
	    if (match(output_pattern, output_string, O_BREAK) != -1)
			break_handler();
	    else
			unknown_output(output_string, command, flags);
	    break;
	    
	case C_INFO_DIR:
		if (debug) {
			fprintf(stderr, "C_INFO_DIR\n");
		}
	    if (match(output_pattern, output_string, O_INFO_DIR) != -1)
	        info_dir_handler();
	    else
			unknown_output(output_string, command, flags);
		break;
		
	case C_DIRECTORY:
		if (debug) {
			fprintf(stderr, "C_DIRECTORY\n");
		}
	    directory_handler(); 
	    break;
	    
	case C_INFO_LINE:
		if (debug) {
			fprintf(stderr, "C_INFO_LINE\n");
		}
	    if (match(output_pattern, output_string, O_INFO_LINE) != -1)
	    	info_line_handler();		/* command was 'info line' */
	    else
			unknown_output(output_string, command, flags);
	    break;
	    
	case C_INFO_BREAK:
		if (debug) {
			fprintf(stderr, "C_INFO_BREAK\n");
		}
	    info_break_handler(output_string);
	    break;

	case C_DISPLAY:	/* means "display foo\n" command */
		if (debug) {
			fprintf(stderr, "C_DISPLAY\n");
		}
		{
	    if ((strcmp(output_string, "") == 0) ||
	    	(match(output_pattern, output_string, O_DISPLAY) != -1))
	    	display_handler();
	    else
			unknown_output(output_string, command, flags);
		}
	    break;
	    
	case C_UNDISPLAY:
		if (debug) {
			fprintf(stderr, "C_UNDISPLAY\n");
		}
	    if (strcmp(output_string, "") == 0)
			display_handler();
	    else
			unknown_output(output_string, command, flags);
	    break;

	case C_DISPLAY_INFO: /* means "display\n" command */
		if (debug) {
			fprintf(stderr, "C_DISPLAY_INFO\n");
		}
		{
	    if ((strcmp(output_string, "") == 0) ||
	    	(match(output_pattern, output_string, O_DISPLAY_INFO) != -1))
	    	display_info_handler();
	    else
			unknown_output(output_string, command, flags);
		}
	    break;
	    
	case C_CD:
		if (debug) {
			fprintf(stderr, "C_CD\n");
		}
	   if (new_gdb4()) /* this fixes where cd and pwd dont work right for 4.x
						(cd doesn't do anything but beep, pwd doesn't work
						when cwd is not cannonical) */
	        query_gdb ("pwd\n", PARSE_ON);
	    else if (match(output_pattern, output_string, O_CD) != -1)
	    	cd_handler(Token.mesg); 
	    else
			unknown_output(output_string, command, flags);
	    break;
	    
	case C_PWD:
		if (debug) {
			fprintf(stderr, "C_PWD\n");
		}
	    if (match(output_pattern, output_string, O_PWD) != -1)
			pwd_handler(Token.display? Token.display : Token.mesg);
	    else
			unknown_output(output_string, command, flags);
	    break;

	case C_FRAME_CURR:
		if (debug) {
			fprintf(stderr, "C_FRAME_CURR\n");
		}
	    if (match(output_pattern, output_string, O_FRAME_CURR) != -1)
			frame_curr_handler();
	    else
			unknown_output(output_string, command, flags);
	    break;

	case C_PRINT:
		if (debug) {
			fprintf(stderr, "C_PRINT\n");
		}
		{
		/* for GDB, the label of the popup display is the expression
		string which is printed instead of $n */
		char * prtname;
		prtname = 0;
		if ((Token.mesg) && (PopupMode))
			prtname = XtNewString(Token.mesg);
			
	    if (match(output_pattern, output_string, O_PRINT) != -1)
	    	{
	    	if (prtname)
	    		{
				XtFree(Token.mesg);
				Token.mesg = prtname;
				prtname = 0;			/* not to XtFree twice this string */
				}
	    	print_handler(output_string);
	    	}
	    else
			unknown_output(output_string, command, flags);
		
		/* if PopupMode was true but GDB found an error in print
		statement (ie match() returned -1), PopupMode was never reset */
		PopupMode = FALSE;	 
		
		XtFree(prtname);
		}
	    break;
	    
	case C_SYMBOL_FILE:
	case C_FILE:
		if (debug) {
			fprintf(stderr, "C_FILE or C_SYMBOL_FILE\n");
		}
		debug_handler(); 
	    break;

	case C_SOURCE:	/* WE SHOULD NEVER ARRIVE HERE */
		if (debug) {
			fprintf(stderr, "C_SOURCE - WE SHOULD NEVER ARRIVE HERE\n");
		}
		break;

	case C_EXEC_FILE:
		if (debug) {
			fprintf(stderr, "C_EXEC_FILE\n");
		}
		break;
		
	case C_CORE_FILE:
		if (debug) {
			fprintf(stderr, "C_CORE_FILE\n");
		}
	    if (match(output_pattern, output_string, O_CORE_FILE) != -1)
			core_file_handler();
	    else
			unknown_output(output_string, command, flags);
	    break;
	    
	/* in case of 'info source', we do not call unknown_output()
	if the pattern is not matched, because we only test for the
	'compilation directory' line. When there is no compilation
	directory, there is no match and there is no error...

	(PW)8JUN93 : we also test for current source and current
	source full pathname.
	*/
	
	case C_INFO_SOURCE:
		if (debug) {
			fprintf(stderr, "C_INFO_SOURCE\n");
		}
		cdir[0] = 0;
		source_fullpath[0] = 0;
		source_path[0] = 0;
	    if (match(output_pattern, output_string, O_INFO_SOURCE) != -1)
	    	info_source_handler(Token.mesg, Token.file, Token.func);
	    break;

	default:
		if (debug) {
			fprintf(stderr, "C UNKNOWN\n");
		}
	    break;
    }
    XtFree(output_string);
}

#ifdef NEED_STRSTR
/*--------------------------------------------------------------------------+
|																			|
|	Some systems DO NOT have the ANSI strstr function						|
|																			|
+--------------------------------------------------------------------------*/	
char *
strstr (source, substr)
	char *source;
	char *substr;
{
char *src;
char *sub;

	if (!source || !substr)
		return NULL;
		
	while (*source)
		{
		for (src = source, sub = substr;
				(*src) && (*sub) && (*src == *sub);
					src++,sub++);
		if (!*sub)
			return source;
			
		source++;
		}
		
	return NULL;
}
#endif

/*--------------------------------------------------------------------------+
|																			|
|	Function to filter all the display information in a string				|
|																			|
|	input : string pointer,													|
|			already_taken_care is number of char in string already 			|
|			   processed (eg, displayed).									|
|																			|
|	output : none.															|
|																			|
|	See O_EXEC_DISPLAY in gdb_regex.h for the display pattern.				|
|																			|
|	Take care when GDB send some message after '\032\032...\n'				|
|	which is not a display line.											|
|																			|
| (gdb) finish																|
| Run till exit from #0	 foo (n=1) (pw.c line 9)							|
| main () (pw.c line 41)													|
| /usr1/gnu_sun4/xxgdb/pw.c:41:590:beg:0x232c								|
| 1: i = 1																	|
| Value returned is $1 = 1													|
| (gdb)																		|
|																			|
+--------------------------------------------------------------------------*/
void filter_display_info(output, already_taken_care)
char *output;
unsigned int already_taken_care;
{
	struct re_registers regs;
	int	r;
	char *p;
	char *p1;
	char *p2;
	char *cp_output;
	int begin_struct;

	p = cp_output = XtNewString(output);

	p1 = strstr(p,"\032\032");		/* find beginning of special gdb line */
	
	if ((p1 == 0) || ((p2 = strchr(p1+1,'\n')) == 0))
		{
		AppendDialogText(p + already_taken_care);		/* something wrong here */
		XtFree(cp_output);
		return;
		}
		
	*p1 = 0;
	
	if (p1 > (p + already_taken_care)) {
		AppendDialogText(p + already_taken_care);		/* print everything before that line */
	}
	p = p2 + 1;											/* end of that line + skip \n */

	/* test for beginning of a display */
	
	while (re_match(output_pattern[O_EXEC_DISPLAY].buf,p,strlen(p),0,&regs) > 0)
		{
		/* we found a "X:....\n" pattern */
		
		r = output_pattern[O_EXEC_DISPLAY].reg_token[TK_DISP];
		p1=  p+regs.start[r];
		p2 = p+regs.end[r];
					
		/* count number of { and } : if not equal, the next lines are part of this display */
		begin_struct = 0;
		while (p1 < p2)
			{
			switch(*(p1++))
				{
				case '{':
					begin_struct++;
					break;
				case '}':
					begin_struct--;
					break;
				}
			}
			
		p1=p+regs.start[r];
		*p1 = 0;
		if (p != p1) {
			/* do not print anything already printed */
			if (p < (cp_output + already_taken_care)) {
				p = cp_output + already_taken_care;
			}
			if (p < p1) {
				AppendDialogText(p);	/* print what is before display */
			}
		}
		p = p2;						/* skip display text */
					
		if (begin_struct)	/* skip the whole data displayed */
			{
			do				/* find the last '}' */
				{
				switch(*(p++))
					{
					case '{':
						begin_struct++;
						break;
					case '}':
						begin_struct--;
						break;
					}
				}
			while (begin_struct);
			
			/* now skip until end of line */
			while (*(p++) != '\n');
			}
		}
	
	if (p < (cp_output + already_taken_care)) {
		p = cp_output + already_taken_care;	/* do not print anything already printed */
	}
	AppendDialogText(p);		/* print what is after display */
	XtFree(cp_output);
}

/*--------------------------------------------------------------------------+
|																			|
| *	 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;
    char 		*p2;
    static 		Boolean	deleteRest = False;
	static 		Boolean for_gdb_only = False;
    int			command_type = -1;
	static 		unsigned int already_taken_care = 0;	
	Boolean		previous_for_gdb_only;

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

/* for GDB, the only things we want to filter are:

	- the line displayed because of the -fullname  option :
	"\032\032/usr1/gnu_sun4/xdbx/pw.c:6:40:beg:0x22b0\n",
	
	- the displayed info which goes into the display window,
	
	- list and search outputs

*/
	if (!string)
		string = "";
		
    if (command)
    	command_type = match(command_pattern, command, C_ANY);
    	
    if ((command_type == C_EXEC)||(command_type == C_FINISH))
    	{
		if ((re_match(output_pattern[O_EXEC_MESS_AFTER].buf,output,strlen(output),0,&regs) > 0)
				|| (re_match(output_pattern[O_EXEC_MESS_BEFORE].buf,output,strlen(output),0,&regs) > 0)
				|| (re_match(output_pattern[O_EXEC_GDB].buf,output,strlen(output),0,&regs) > 0))
			{
			/* Remove display messages from output and print what is not a display */
			
			if (Prompt) {
				filter_display_info (output, already_taken_care);	
				for_gdb_only = False;
				already_taken_care = 0;
				deleteRest = False;
		    } else {
				deleteRest = True;
			}
			return;
			}
		else  /* does not match exec pattern yet */
			{
			if (deleteRest)	 /* if already matched before */
				{
				/* Remove display messages from output and print what is not a display */
				if (Prompt)
					{
					filter_display_info (output, already_taken_care);	
					for_gdb_only = False;
					already_taken_care = 0;
					deleteRest = False;
					}
				return;
				}
			}
		}
		

	/* remember what was the output length was output was matched */
	already_taken_care = strlen(output);

	/* filter any line starting with \032\032 */

	/* (PW)18NOV94: we have a problem with some system where the \032\032 comes in
	   several times. In that case we might end-up showing ^Z^Z in dialog window.
	   SO now do no test for end of line, and note that all following outputs are 
	   for gdb, not for the user.
	   Here we assume that at least the double ^Z^Z is read in one piece.
	   */
	
	previous_for_gdb_only = for_gdb_only;

	p = strchr(string,'\032');
	if (p && (*(p+1) == '\032') && (p == string || *(p-1) == '\n') /* && (p2 = strchr(p,'\n'))*/) {
		if ((p2 = strchr(p,'\n'))) {
			while ((*(p++) = *(++p2)));
		} else {
			*p = 0;
		}
		for_gdb_only = True;
	}

	if ((command_type == C_EXEC)||(command_type == C_FINISH)) {
		if (!previous_for_gdb_only) {
			AppendDialogText(string);
		}
		if (Prompt) {
			for_gdb_only = False;
			already_taken_care = 0;
			deleteRest = False;
		}
		return;
	}
		
    if (Prompt)
    	{
		char *s;

		for_gdb_only = False;
		already_taken_care = 0;
		deleteRest = False;

		s = XtNewString(output);
		switch (command_type)
			{
			case C_DISPLAY:
			    if (match(output_pattern, s, O_DISPLAY) != -1)
				strcpy(s, "");
			    break;

			case C_DISPLAY_INFO:
   				 /* (PW)7MAY91 : display error messages */
			    if (match(output_pattern, s, O_DISPLAY_INFO) != -1)
					{    
    				if (Token.mesg && strcmp(Token.mesg, ""))	
   				    	{
						AppendDialogText(Token.mesg);
						bell(0);
    					}		
					strcpy(s, "");
    				}
			    break;
			    
			case C_SEARCH:
			    if (match(output_pattern, s, O_SEARCH) != -1)
				strcpy(s, "");
			    break;
			case C_LIST:
   				 /* (PW)22MAY91 : display messages ") */
			    if (match(output_pattern, s, O_LIST) != -1)
					{    
    				if (Token.mesg && strcmp(Token.mesg, ""))	
   				    	{
						AppendDialogText(Token.mesg);
						if (strstr(Token.mesg,"Source file is more recent than executable."))
							bell(0); /* Warn user WYSIWYG not true */
    					}		
					strcpy(s, "");
    				}
			    break;
			case C_PRINT:
				if	(PopupMode &&	/* if print goes in a window, don't display here */
			    	(match(output_pattern, s, O_PRINT) != -1))
				strcpy(s, "");
				break;
				
			default:
				XtFree(s);
			    s = XtNewString(string);		/* append 'string' only */
			    break;
			}
		AppendDialogText(s);
		XtFree(s);
	    }
	else	/* no prompt yet */
		{
		switch (command_type)
			{
			case C_DISPLAY:
			case C_DISPLAY_INFO:
			case C_SEARCH:
			case C_LIST:
			case C_PRINT:
				break;
			default:
				if (!previous_for_gdb_only) {
					AppendDialogText(string);
				}
				break;
			}
		}

	return;
}


/*--------------------------------------------------------------------------+
|																			|
|	Function to filter 'source' command										|
|																			|
|	input : command (from .gdbinit or source command or keyboard),			|
|			echo is TRUE if source command must be echoed.					|
|																			|
|	output : TRUE if source command was recognized							|
|																			|
|	In case source command is recognized, it is executed here.				|
|																			|
+--------------------------------------------------------------------------*/
int gdb_source_command(command,echo)
char *command;
int echo;
{
	if (command && (match(command_pattern, command, C_SOURCE) != -1))
		{
		if (echo)
			AppendDialogText(command);
		source_handler();	
		return TRUE;
		}
		
	return FALSE;
}

/*--------------------------------------------------------------------------+
|																			|
|	Function to filter 'define' & 'document' commands						|
|																			|
|	input : command (from .gdbinit or source command),						|
|			fp = file pointer.												|
|																			|
|	output : TRUE if define or document command was recognized				|
|																			|
|	In case the command is recognized, it is executed here.					|
|																			|
+--------------------------------------------------------------------------*/
static int command_sent = 0;	/* flag gdb is busy : send no more command to gdb. */

int gdb_define_command(command,fp)
char *command;
FILE *fp;
{
char s[LINESIZ];
int error_cmd;
int end_found;

	if ((command && (match(command_pattern, command, C_DEFINE) != -1))
		|| (command && (match(command_pattern, command, C_DOCUMENT) != -1)))
		{
		AppendDialogText(command);
		
		/* because silly gdb 4.0 displays nothing with def/doc command when
		confirm is on (possibly a gdb bug) , I just reset confirm to on
		just for this command !. */
		
		if (new_gdb4()) 
			query_gdb("set confirm on\n",	PARSE_OFF | ECHO_OFF | FILTER_OFF);

		write_dbx (command);
		
/*
				gdb could ask :
			
					"Redefine command \"%s\"? "
					"Really redefine built-in command \"%s\"? "
					
				gdb could display 
				
					"Type commands for definition of \"%s\".\n"
					"End with a line saying just \"end\"
				
				or
					"Type documentation for \"%s\".\n"
					"End with a line saying just \"end\".\n"
					
				or
				 	"Command \"%s\" is built-in."
*/
	
		error_cmd = FALSE;
				
		/* read message from gdb */
		
		while (1)
			if (fgets(s, LINESIZ, dbxfp))
				{
				if (debug)
					fprintf(stderr, "=>%s", s);
				
				if (strstr(s," is built-in."))	/* error for document of built-in command */
					{
					AppendDialogText(s);
					error_cmd = TRUE;
					bell(0);
					break;
					}
			
				if (strstr(s,"Redefine command ")
				 || strstr(s,"Really redefine built-in command "))
					write_dbx ("y\n"); 	/* answer to question if any */
				else
					{
					if (strcmp(s,"End with a line saying just \"end\".\n") == 0)
						break;
					}
				}
		
		/* write command definition */
		
		end_found = FALSE;
		
		while (fgets (s, LINESIZ, fp))
			{
			if (!error_cmd)
				{
				AppendDialogText(s);
				write_dbx (s);
				}
			
			if (match(command_pattern, s, C_END) != -1)
				{
				end_found = TRUE;
				break;
				}
			}
			
		if ((!error_cmd) && (!end_found))
			{
			AppendDialogText("Error : missing \"end\" in file.\n");
			bell(0);
			write_dbx ("end\n");
			}
		
		command_sent++;				/* flag gdb is busy (because of read_gdb) */
		insert_command("end\n");	/* insert 'end' at head of queue */
		read_until_prompt (PARSE_OFF | ECHO_ON | FILTER_OFF);	/* return when prompt */

		if (new_gdb4()) /* reset confirm to off */
			query_gdb("set confirm off\n",	PARSE_OFF | ECHO_OFF | FILTER_OFF);

		return TRUE;
		}
		
	return FALSE;
}


#ifndef NO_MY_FGETS
/*
 * cope with non-blocking I/O correctly
 * ie: exit if child closes pty, but return if would block.
 *
 *	This function seems to fail on some systems (??)
 *
 */
static char *
#ifdef __STDC__
my_fgets(char *buf, int size, FILE *f)
#else
my_fgets(buf, size, f)
char *buf;
int size;
FILE *f;
#endif
{
	char *orig_buf = buf;

	while(size > 1) {
		int cc;

		cc = read(fileno(f), buf, 1);
		if(cc == -1) {
			if(errno == EAGAIN || errno == EWOULDBLOCK) {
				break;
			}
			perror("read from gdb");
			exit(1);
			/*NOTREACHED*/
		}
		if(cc == 0) {
#ifdef READ_ZERO_NOT_EOF   /* for RS6000 */
			break;
#else
			(void) fprintf(stderr, "EOF from gdb\n");
			exit(1);
#endif
			/*NOTREACHED*/
		}
		buf++;
		size--;
		if(*(buf-1) == '\n')
			break;
	}

	*buf = '\0';
	if (buf == orig_buf) {
		return NULL;
	} else {
		return(orig_buf);
	}
}
#endif /* NO_MY_FGETS */

/*--------------------------------------------------------------------------+
|																			|
|	Read gdb output until fgets returns no string.							|
|																			|
|	The ouptut is process according to flags.								|
|																			|
|	In case the gdb prompt is found, the command on top of the				|
|	command queue (ie, the one which is assumed to correspond				|
|	to this output) is remove from the queue, and 'command_sent'			|
|	is decremented to say that gdb is no longer busy.						|
|																			|
+--------------------------------------------------------------------------*/
static int parse_flags = PARSE_ON | ECHO_ON | FILTER_ON; /* last flags used in read_gdb */

void read_gdb (flags)
int flags;
{
    static char *output = NULL; 	/* buffer for gdb output */
    static char *next_string = NULL;
    static char *command;
    char 	*string = NULL;
	char 	*prompt_pointer;
    char 	s[LINESIZ];
	int before_prompt;
    Boolean 	more;
	Boolean		local_prompt;
	parse_flags = flags; 	/* just remember flags for read_dbx() */
	
    more = True;
	Prompt = False;
	local_prompt = False;

    while (more) {

		/* keep reading until no more or until prompt arrives */
#ifdef NO_MY_FGETS
		while ((more = (fgets(s, LINESIZ, dbxfp) != NULL)))
#else
		while ((more = (my_fgets(s, LINESIZ, dbxfp) != NULL)))
#endif
		{
			if (debug)
				fprintf(stderr, "=>%s", s);

			/* new gdb might add '>' characters in front of input for define commands,
			   we have to test for them here ! 
			   */
			
			/* receive prompt? */

#if 1		/* (PW)16NOV94 : new gdb might add '>' characters in front of input 
			   when defining commands, we have to test for them here ! 
			   so use strstr() to catch all prompts...
			   */
			if ((prompt_pointer = strstr(s, dbxprompt)) != NULL)
#else
			if (strncmp(s, dbxprompt, strlen(dbxprompt)) == 0)
#endif
			{
				Prompt = True;
				local_prompt = True;
				before_prompt = prompt_pointer - s;
			
				/* more stuff behind prompt? */
				if (s[before_prompt+ strlen(dbxprompt)]) {
					/* remember it */
					next_string = XtNewString(s+before_prompt+strlen(dbxprompt));
				}
				/* destroy contents */
				strcpy(s, "");
				if (debug)
					fprintf(stderr, " Prompt detected\n");
			}
		    
			string = concat(string, s);
			strcpy(s, "");

			if (local_prompt) {  /* do not read next line if prompt is found */
				break;
			}
		}

		output = concat(output, string);
	
		command = get_command();			/* read queue's top command  */
		
	if (flags & FILTER_ON) {
	    filter (string, output, command);
	}
	    
	if ((flags & ECHO_ON) && local_prompt) {
	    AppendDialogText(xdbxprompt);
	}
	    
	if (string) {
	    XtFree(string);
	    string = NULL;
	}
	
	if (next_string) {
	    string = concat(string, next_string);
	    XtFree(next_string);
	    next_string = NULL;
	}
	
	if ((flags & PARSE_ON) && local_prompt) {
	    parse (output, command, flags);
	}
	
	/* note : it is important to decrement 'command_sent' AFTER
	parse() is executed, so that no other commands will mix-up
	the commands sent by the function handlers executed by parse().
	*/
	
	if (local_prompt) {
	    delete_command();		/* remove top command from queue */
	    if (command && command_sent)
			command_sent--;
	    XtFree(output);
	    output = NULL;
		local_prompt = False;
	}
	}
}

/*--------------------------------------------------------------------------+
| *																			|
| *	 This is a callback procedure invoked everytime when input is pending	|
| *	 on the file descriptor to dbx.											|
| *	 o reads all the data available on the file descriptor line by line		|
| *	   into local variable 'string' and global variable 'output'.			|
| *	   'output' records the entire dbx output whereas 'string' records		|
| *	   only the data read in this invocation of read_dbx().					|
| *	 o in Echo mode, the contents in 'string' is edited by filter()			|
| *	   before it gets displayed on the dialog window.						|
| *	 o once the dbx prompt is read, calls parse() to analyse the dbx output |
| *	   and take appropriate action.											|
| *																			|
+--------------------------------------------------------------------------*/
static volatile int in_read_dbx;

/* ARGSUSED */
void read_dbx(master, source, id)
XtPointer master;
int 	  *source;
XtInputId *id;
{
	if (in_read_dbx++)	/* already running */
		return;

	do {
		read_gdb (parse_flags);

		/* do not write any more command, if prompt was not seen yet */

		if (Prompt) {
			write_dbx_available ();
		}

	} while (--in_read_dbx);
}

/*--------------------------------------------------------------------------+
|																			|
|	Read gdb output until prompt.											|
|	The ouptut is process according to flags.								|
|																			|
+--------------------------------------------------------------------------*/
void read_until_prompt (flags)
int flags;
{
    Prompt = False;
    while (!Prompt)
        read_gdb (flags);
}

/*--------------------------------------------------------------------------+
|																			|
| *	 Sends a command to gdb and read the corresponding output, directly		|
| *	 invoking the Xt input procedure, read_gdb().							|
| *																			|
| * Same as query_dbx() in dbx.c except that echo, filter and parse.		|
| *	 are passed in argument flags:											|
| * PARSE_ON | PARSE_OFF | ECHO_ON | ECHO_OFF | FILTER_ON | FILTER_OFF		|
| *																			|
+--------------------------------------------------------------------------*/
void query_gdb(command, flags)
char *command;
int flags;
{
	command_sent++;				/* flag gdb is busy */
	
    write_dbx(command);			/* send command to gdb */
    
    insert_command(command);	/* insert at head of queue */

	read_until_prompt (flags);	/* return when prompt */
	
	/* test if we are at the top command, and if some commands are waiting */
	
	write_dbx_available ();	/* send next waiting command */
}

/*--------------------------------------------------------------------------+
|																			|
|	Function to send the first command of the command queue to gdb only in	|
|	case gdb is waiting for a command.										|
|																			|
|	WARNING : we must test for 'source' command.							|
|																			|
|	The commands in the command queue are from the keyboard and from		|
|	the buttons.															|
|																			|
+--------------------------------------------------------------------------*/
void write_dbx_available()
{
char *command;

	if ((command = get_command ()))
		{
		if (command_sent++ == 0)	
			{
			/* here, the source command is already displayed, say echo command = FALSE */
			
			if (gdb_source_command (command, FALSE))
				{
				delete_command();		/* remove source command from top of queue */
				command_sent--;
				/* tell gdb to send its prompt */
				query_gdb(" \n", PARSE_OFF | ECHO_ON | FILTER_OFF);
				return;
				}

			if (xxgdb_command (command, FALSE))
				{
				delete_command();		/* remove xxgdb command from top of queue */
				command_sent--;
				/* tell gdb to send its prompt */
				query_gdb(" \n", PARSE_OFF | ECHO_ON | FILTER_OFF);
				return;
				}

			parse_flags = PARSE_ON | ECHO_ON | FILTER_ON; /* for read_gdb */
			write_dbx (command);
		    Prompt = False;
			return;					/* return with command_sent incremented */
			}
		else
			{
			if (command_sent)	/* should never be zero ! (just to be safe) */
				command_sent--;	/* command is not sent, restore previous value */
			return;
			}
		}
}


/*--------------------------------------------------------------------------+
|																			|
|	Function to filter xxgdb only commands									|
|																			|
|	input : command (from .gdbinit or source command or keyboard),			|
|			echo is TRUE if source command must be echoed.					|
|																			|
|	output : TRUE if source command was recognized							|
|																			|
|	In case source command is recognized, it is executed here.				|
|																			|
+--------------------------------------------------------------------------*/
int xxgdb_command (command, echo)
char *command;
int echo;
{
#ifdef CREATE_IO_WINDOW
	if (command) {
             if(!strcmp(command,"iowin\n")) /* test for 'iowin' command */
		{
		if (echo)
			AppendDialogText(command);
		if (!iowinpid)
			{
			char ttycommand[50];
			create_io_window ();
			sprintf (ttycommand, "tty %s\n", iowintty);
			query_gdb (ttycommand, PARSE_OFF | ECHO_OFF | FILTER_OFF);
			}
		return TRUE;
		}
	     if(!strcmp(command,"noiowin\n")) {
		if (iowinpid) close_io_window ();
		return TRUE;
	     }
#endif /* CREATE_IO_WINDOW */
	}
		
	return FALSE;
}