Add README.
[binutils-gdb.git] / gdb / gdbserver / remote-gutils.c
1 /* General utility routines for the remote server for GDB.
2 Copyright (C) 1986, 1989, 1993 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20 #include <stdio.h>
21 #include <sys/ioctl.h>
22 #include "defs.h"
23 #include <setjmp.h>
24
25 void error ();
26 void fatal ();
27
28 /* Chain of cleanup actions established with make_cleanup,
29 to be executed if an error happens. */
30
31 static struct cleanup *cleanup_chain;
32
33 /* Nonzero means a quit has been requested. */
34
35 int quit_flag;
36
37 /* Nonzero means quit immediately if Control-C is typed now,
38 rather than waiting until QUIT is executed. */
39
40 int immediate_quit;
41 \f
42 /* Add a new cleanup to the cleanup_chain,
43 and return the previous chain pointer
44 to be passed later to do_cleanups or discard_cleanups.
45 Args are FUNCTION to clean up with, and ARG to pass to it. */
46
47 struct cleanup *
48 make_cleanup (function, arg)
49 void (*function) ();
50 PTR arg;
51 {
52 register struct cleanup *new
53 = (struct cleanup *) xmalloc (sizeof (struct cleanup));
54 register struct cleanup *old_chain = cleanup_chain;
55
56 new->next = cleanup_chain;
57 new->function = function;
58 new->arg = arg;
59 cleanup_chain = new;
60
61 return old_chain;
62 }
63
64 /* Discard cleanups and do the actions they describe
65 until we get back to the point OLD_CHAIN in the cleanup_chain. */
66
67 void
68 do_cleanups (old_chain)
69 register struct cleanup *old_chain;
70 {
71 register struct cleanup *ptr;
72 while ((ptr = cleanup_chain) != old_chain)
73 {
74 (*ptr->function) (ptr->arg);
75 cleanup_chain = ptr->next;
76 free (ptr);
77 }
78 }
79
80 /* Discard cleanups, not doing the actions they describe,
81 until we get back to the point OLD_CHAIN in the cleanup_chain. */
82
83 void
84 discard_cleanups (old_chain)
85 register struct cleanup *old_chain;
86 {
87 register struct cleanup *ptr;
88 while ((ptr = cleanup_chain) != old_chain)
89 {
90 cleanup_chain = ptr->next;
91 free (ptr);
92 }
93 }
94
95 /* This function is useful for cleanups.
96 Do
97
98 foo = xmalloc (...);
99 old_chain = make_cleanup (free_current_contents, &foo);
100
101 to arrange to free the object thus allocated. */
102
103 void
104 free_current_contents (location)
105 char **location;
106 {
107 free (*location);
108 }
109 \f
110 /* Generally useful subroutines used throughout the program. */
111
112 /* Like malloc but get error if no storage available. */
113
114 PTR
115 xmalloc (size)
116 long size;
117 {
118 register char *val = (char *) malloc (size);
119 if (!val)
120 fatal ("virtual memory exhausted.", 0);
121 return val;
122 }
123
124 /* Like realloc but get error if no storage available. */
125
126 PTR
127 xrealloc (ptr, size)
128 PTR ptr;
129 long size;
130 {
131 register char *val = (char *) realloc (ptr, size);
132 if (!val)
133 fatal ("virtual memory exhausted.", 0);
134 return val;
135 }
136
137 /* Print the system error message for errno, and also mention STRING
138 as the file name for which the error was encountered.
139 Then return to command level. */
140
141 void
142 perror_with_name (string)
143 char *string;
144 {
145 extern int sys_nerr;
146 extern char *sys_errlist[];
147 extern int errno;
148 char *err;
149 char *combined;
150
151 if (errno < sys_nerr)
152 err = sys_errlist[errno];
153 else
154 err = "unknown error";
155
156 combined = (char *) alloca (strlen (err) + strlen (string) + 3);
157 strcpy (combined, string);
158 strcat (combined, ": ");
159 strcat (combined, err);
160
161 error ("%s.", combined);
162 }
163
164 /* Print the system error message for ERRCODE, and also mention STRING
165 as the file name for which the error was encountered. */
166
167 void
168 print_sys_errmsg (string, errcode)
169 char *string;
170 int errcode;
171 {
172 extern int sys_nerr;
173 extern char *sys_errlist[];
174 char *err;
175 char *combined;
176
177 if (errcode < sys_nerr)
178 err = sys_errlist[errcode];
179 else
180 err = "unknown error";
181
182 combined = (char *) alloca (strlen (err) + strlen (string) + 3);
183 strcpy (combined, string);
184 strcat (combined, ": ");
185 strcat (combined, err);
186
187 printf ("%s.\n", combined);
188 }
189
190 void
191 quit ()
192 {
193 fflush (stdout);
194 ioctl (fileno (stdout), TIOCFLUSH, 0);
195 error ("Quit");
196 }
197
198 /* Control C comes here */
199
200 void
201 request_quit (ignored)
202 int ignored;
203 {
204 quit_flag = 1;
205 if (immediate_quit)
206 quit ();
207 }
208
209 /* Print an error message and return to command level.
210 STRING is the error message, used as a fprintf string,
211 and ARG is passed as an argument to it. */
212
213 NORETURN void
214 error (string, arg1, arg2, arg3)
215 char *string;
216 int arg1, arg2, arg3;
217 {
218 extern jmp_buf toplevel;
219
220 fflush (stdout);
221 fprintf (stderr, string, arg1, arg2, arg3);
222 fprintf (stderr, "\n");
223 longjmp(toplevel, 1);
224 }
225
226 /* Print an error message and exit reporting failure.
227 This is for a error that we cannot continue from.
228 STRING and ARG are passed to fprintf. */
229
230 void
231 fatal (string, arg)
232 char *string;
233 int arg;
234 {
235 fprintf (stderr, "gdb: ");
236 fprintf (stderr, string, arg);
237 fprintf (stderr, "\n");
238 exit (1);
239 }
240
241 /* Make a copy of the string at PTR with SIZE characters
242 (and add a null character at the end in the copy).
243 Uses malloc to get the space. Returns the address of the copy. */
244
245 char *
246 savestring (ptr, size)
247 const char *ptr;
248 int size;
249 {
250 register char *p = (char *) xmalloc (size + 1);
251 bcopy (ptr, p, size);
252 p[size] = 0;
253 return p;
254 }
255
256 void
257 print_spaces (n, file)
258 register int n;
259 register FILE *file;
260 {
261 while (n-- > 0)
262 fputc (' ', file);
263 }
264
265 /* Ask user a y-or-n question and return 1 iff answer is yes.
266 Takes three args which are given to printf to print the question.
267 The first, a control string, should end in "? ".
268 It should not say how to answer, because we do that. */
269
270 int
271 query (ctlstr, arg1, arg2)
272 char *ctlstr;
273 {
274 register int answer;
275
276 /* Automatically answer "yes" if input is not from a terminal. */
277 /***********if (!input_from_terminal_p ())
278 return 1; *************************/
279
280 while (1)
281 {
282 printf (ctlstr, arg1, arg2);
283 printf ("(y or n) ");
284 fflush (stdout);
285 answer = fgetc (stdin);
286 clearerr (stdin); /* in case of C-d */
287 if (answer != '\n')
288 while (fgetc (stdin) != '\n')
289 clearerr (stdin);
290 if (answer >= 'a')
291 answer -= 040;
292 if (answer == 'Y')
293 return 1;
294 if (answer == 'N')
295 return 0;
296 printf ("Please answer y or n.\n");
297 }
298 }
299 \f
300 /* Parse a C escape sequence. STRING_PTR points to a variable
301 containing a pointer to the string to parse. That pointer
302 is updated past the characters we use. The value of the
303 escape sequence is returned.
304
305 A negative value means the sequence \ newline was seen,
306 which is supposed to be equivalent to nothing at all.
307
308 If \ is followed by a null character, we return a negative
309 value and leave the string pointer pointing at the null character.
310
311 If \ is followed by 000, we return 0 and leave the string pointer
312 after the zeros. A value of 0 does not mean end of string. */
313
314 int
315 parse_escape (string_ptr)
316 char **string_ptr;
317 {
318 register int c = *(*string_ptr)++;
319 switch (c)
320 {
321 case 'a':
322 return '\a';
323 case 'b':
324 return '\b';
325 case 'e':
326 return 033;
327 case 'f':
328 return '\f';
329 case 'n':
330 return '\n';
331 case 'r':
332 return '\r';
333 case 't':
334 return '\t';
335 case 'v':
336 return '\v';
337 case '\n':
338 return -2;
339 case 0:
340 (*string_ptr)--;
341 return 0;
342 case '^':
343 c = *(*string_ptr)++;
344 if (c == '\\')
345 c = parse_escape (string_ptr);
346 if (c == '?')
347 return 0177;
348 return (c & 0200) | (c & 037);
349
350 case '0':
351 case '1':
352 case '2':
353 case '3':
354 case '4':
355 case '5':
356 case '6':
357 case '7':
358 {
359 register int i = c - '0';
360 register int count = 0;
361 while (++count < 3)
362 {
363 if ((c = *(*string_ptr)++) >= '0' && c <= '7')
364 {
365 i *= 8;
366 i += c - '0';
367 }
368 else
369 {
370 (*string_ptr)--;
371 break;
372 }
373 }
374 return i;
375 }
376 default:
377 return c;
378 }
379 }
380 \f
381 void
382 printchar (ch, stream)
383 unsigned char ch;
384 FILE *stream;
385 {
386 register int c = ch;
387 if (c < 040 || c >= 0177)
388 {
389 if (c == '\n')
390 fprintf (stream, "\\n");
391 else if (c == '\b')
392 fprintf (stream, "\\b");
393 else if (c == '\t')
394 fprintf (stream, "\\t");
395 else if (c == '\f')
396 fprintf (stream, "\\f");
397 else if (c == '\r')
398 fprintf (stream, "\\r");
399 else if (c == 033)
400 fprintf (stream, "\\e");
401 else if (c == '\a')
402 fprintf (stream, "\\a");
403 else
404 fprintf (stream, "\\%03o", c);
405 }
406 else
407 {
408 if (c == '\\' || c == '"' || c == '\'')
409 fputc ('\\', stream);
410 fputc (c, stream);
411 }
412 }