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 /*
62  *  dbx.c
63  *
64  *    Handle dbx command initialization file (.dbxinit) and communication
65  *    between dbx and xdbx.
66  *
67  *    dbx_init():	Handle .dbxinit
68  *    debug_init():
69  *    read_dbx():	Read dbx output, parse and filter it before displaying
70  *			onto the dialog window.
71  *    write_dbx():	Send a command to dbx.
72  *    query_dbx():	Send a command to dbx and process it.
73  */
74 #include <stdio.h>
75 #include <unistd.h>
76 #include "global.h"
77 
78 Boolean	Prompt;			/* True when dbx prompt arrives */
79 char 	*concat();
80 char	*dbxprompt;
81 char	*xdbxprompt;
82 
83 /*  Given a dbx command initialization file, this routine executes each dbx
84  *  command in the file.  It sends the command to dbx, and calls read_dbx()
85  *  directly to process output returned from dbx.
86  */
87 
dbx_init(xdbxinit)88 static void dbx_init(xdbxinit)
89 char *xdbxinit;
90 {
91 #ifndef GDB
92     FILE *fp;
93     char s[LINESIZ];
94 #endif /* not GDB */
95 
96     if (strcmp(xdbxinit, "") == 0)
97 	return;
98 
99 #ifdef GDB
100 
101 	read_source_file(xdbxinit);
102 
103 #else /* not GDB */
104     if (fp = fopen(xdbxinit, "r")) {
105 		while (fgets(s, LINESIZ, fp)) {
106 			send_command(s);
107 			AppendDialogText(s);
108 			Prompt = False;
109 			while (!Prompt)
110 				read_dbx();
111 		}
112 	fclose(fp);
113     }
114 #endif /* not GDB */
115 }
116 
117 /*
118  *  This routine is called after getting the first dbx prompt.
119  *  > check the use list to create a list of directories for searching
120  *    source files.
121  *  > ask dbx for the source file and display it if it exists.
122  *  > open the command initialization file and executed the commands;
123  *    if Tstartup is true, remove the initialization file.
124  */
debug_init()125 void debug_init()
126 {
127     static visited = False;
128 
129     if (!visited) {
130 	visited = True;
131 	dbx_init(xdbxinit);
132 	if (Tstartup)
133 	    unlink(xdbxinit);
134 	strcpy(xdbxinit, "");
135     }
136 }
137 
138 #ifndef GDB
139 /*
140  *  This is a callback procedure invoked everytime when input is pending
141  *  on the file descriptor to dbx.
142  *  o reads all the data available on the file descriptor line by line
143  *    into local variable 'string' and global variable 'output'.
144  *    'output' records the entire dbx output whereas 'string' records
145  *    only the data read in this invocation of read_dbx().
146  *  o in Echo mode, the contents in 'string' is edited by filter()
147  *    before it gets displayed on the dialog window.
148  *  o once the dbx prompt is read, calls parse() to analyse the dbx output
149  *    and take appropriate action.
150  */
151 /* ARGSUSED */
read_dbx(master,source,id)152 void read_dbx(master, source, id)
153 XtPointer master;
154 int 	  *source;
155 XtInputId *id;
156 {
157     static char *output = NULL; 	/* buffer for dbx output */
158     static char *next_string = NULL;
159     static char *command;
160     char 	*string = NULL;
161     char 	s[LINESIZ];
162     Boolean 	more;
163 
164     more = True;
165     while (more) {
166 	Prompt = False;
167 	/* keep reading until no more or until prompt arrives */
168 	while (more = fgets(s, LINESIZ, dbxfp) && !Prompt) {
169 	    if (debug)
170 		fprintf(stderr, "=>%s", s);
171 	    /* receive prompt? */
172 	    if (strncmp(s, dbxprompt, strlen(dbxprompt)) == 0) {
173 		Prompt = True;
174 		/* more stuff behind prompt? */
175 		if (s[strlen(dbxprompt)])
176 		    /* remember it */
177 		    next_string = XtNewString(s+strlen(dbxprompt));
178 		/* destroy contents */
179 		strcpy(s, "");
180 	    }
181 	    string = concat(string, s);
182 	    strcpy(s, "");
183 	}
184 	output = concat(output, string);
185 	command = get_command();
186 
187 	if (Echo) {
188 	    filter(string, output, command);
189 	    if (Prompt) AppendDialogText(xdbxprompt);
190 	}
191 	if (string) {
192 	    XtFree(string);
193 	    string = NULL;
194 	}
195 	if (next_string) {
196 	    string = concat(string, next_string);
197 	    XtFree(next_string);
198 	    next_string = NULL;
199 	}
200 	if (Prompt) {
201 	    parse(output, command);
202 	    delete_command();
203 	    XtFree(output);
204 	    output = NULL;
205 	}
206     }
207 }
208 #endif /* not GDB */
209 
210 /*  Write string s to dbx, and flush the output.
211  *
212  *	18NOV94: it could happen that the string is too big for
213  *	the IO system to handle at once. Just wait on try again.
214  *	Do not try too soon else an error will be returned by
215  *	write().
216  *
217  */
218 
write_dbx(s)219 void write_dbx(s)
220 char *s;
221 {
222 	int nbwritten;
223 	int strl = strlen(s);
224 	if (debug) {
225 		fprintf(stderr, ">>%s", s);		/* (PW) see what is sent to GDB */
226 	}
227 	while (strl) {
228 		nbwritten = write (fileno(dbxfp), s, strl);
229 		if (nbwritten == -1) {
230 			perror("write dbxfp");
231 			exit(-1);
232 		}
233 		strl -= nbwritten;
234 		if (strl == 0) {
235 			break;	/* nothing more */
236 		}
237 		s += nbwritten;
238 		if (debug) {
239 			fprintf(stderr, ">> sleep in write_dbx()\n");
240 		}
241 		sleep(1);		/* give gdb time to read his buffer */
242 	}
243 }
244 
245 
246 #ifndef GDB
247 /*  Sends a command to dbx and read the corresponding output, directly
248  *  invoking the Xt input procedure, read_dbx().
249  */
query_dbx(command)250 void query_dbx(command)
251 char *command;
252 {
253     write_dbx(command);
254     insert_command(command);
255 
256     Echo = False;
257     Prompt = False;
258     while (!Prompt)
259         read_dbx();
260 
261     Parse = True;	/* Always reset Parse and Echo to True */
262     Echo = True;
263 }
264 #endif
265