]>
Commit | Line | Data |
---|---|---|
7b4ac7e1 | 1 | head 1.2; |
2 | access ; | |
3 | symbols RMS-has:1.2; | |
4 | locks ; strict; | |
5 | comment @ * @; | |
6 | ||
7 | ||
8 | 1.2 | |
9 | date 88.01.26.05.11.12; author gnu; state Exp; | |
10 | branches ; | |
11 | next 1.1; | |
12 | ||
13 | 1.1 | |
14 | date 88.01.21.05.11.11; author gnu; state Exp; | |
15 | branches ; | |
16 | next ; | |
17 | ||
18 | ||
19 | desc | |
20 | @From RMS's development sources on wheaties, 20Jan88 | |
21 | @ | |
22 | ||
23 | ||
24 | 1.2 | |
25 | log | |
26 | @Avoid using TIOCFLUSH if it is not defined. | |
27 | @ | |
28 | text | |
29 | @/* General utility routines for GDB, the GNU debugger. | |
30 | Copyright (C) 1986 Free Software Foundation, Inc. | |
31 | ||
32 | GDB is distributed in the hope that it will be useful, but WITHOUT ANY | |
33 | WARRANTY. No author or distributor accepts responsibility to anyone | |
34 | for the consequences of using it or for whether it serves any | |
35 | particular purpose or works at all, unless he says so in writing. | |
36 | Refer to the GDB General Public License for full details. | |
37 | ||
38 | Everyone is granted permission to copy, modify and redistribute GDB, | |
39 | but only under the conditions described in the GDB General Public | |
40 | License. A copy of this license is supposed to have been given to you | |
41 | along with GDB so you can know your rights and responsibilities. It | |
42 | should be in a file named COPYING. Among other things, the copyright | |
43 | notice and this notice must be preserved on all copies. | |
44 | ||
45 | In other words, go ahead and share GDB, but don't try to stop | |
46 | anyone else from sharing it farther. Help stamp out software hoarding! | |
47 | */ | |
48 | ||
49 | #include <stdio.h> | |
50 | #include <sys/ioctl.h> | |
51 | #include "defs.h" | |
52 | ||
53 | void error (); | |
54 | void fatal (); | |
55 | ||
56 | /* Chain of cleanup actions established with make_cleanup, | |
57 | to be executed if an error happens. */ | |
58 | ||
59 | static struct cleanup *cleanup_chain; | |
60 | ||
61 | /* Nonzero means a quit has been requested. */ | |
62 | ||
63 | int quit_flag; | |
64 | ||
65 | /* Nonzero means quit immediately if Control-C is typed now, | |
66 | rather than waiting until QUIT is executed. */ | |
67 | ||
68 | int immediate_quit; | |
69 | \f | |
70 | /* Add a new cleanup to the cleanup_chain, | |
71 | and return the previous chain pointer | |
72 | to be passed later to do_cleanups or discard_cleanups. | |
73 | Args are FUNCTION to clean up with, and ARG to pass to it. */ | |
74 | ||
75 | struct cleanup * | |
76 | make_cleanup (function, arg) | |
77 | void (*function) (); | |
78 | int arg; | |
79 | { | |
80 | register struct cleanup *new | |
81 | = (struct cleanup *) xmalloc (sizeof (struct cleanup)); | |
82 | register struct cleanup *old_chain = cleanup_chain; | |
83 | ||
84 | new->next = cleanup_chain; | |
85 | new->function = function; | |
86 | new->arg = arg; | |
87 | cleanup_chain = new; | |
88 | ||
89 | return old_chain; | |
90 | } | |
91 | ||
92 | /* Discard cleanups and do the actions they describe | |
93 | until we get back to the point OLD_CHAIN in the cleanup_chain. */ | |
94 | ||
95 | void | |
96 | do_cleanups (old_chain) | |
97 | register struct cleanup *old_chain; | |
98 | { | |
99 | register struct cleanup *ptr; | |
100 | while ((ptr = cleanup_chain) != old_chain) | |
101 | { | |
102 | (*ptr->function) (ptr->arg); | |
103 | cleanup_chain = ptr->next; | |
104 | free (ptr); | |
105 | } | |
106 | } | |
107 | ||
108 | /* Discard cleanups, not doing the actions they describe, | |
109 | until we get back to the point OLD_CHAIN in the cleanup_chain. */ | |
110 | ||
111 | void | |
112 | discard_cleanups (old_chain) | |
113 | register struct cleanup *old_chain; | |
114 | { | |
115 | register struct cleanup *ptr; | |
116 | while ((ptr = cleanup_chain) != old_chain) | |
117 | { | |
118 | cleanup_chain = ptr->next; | |
119 | free (ptr); | |
120 | } | |
121 | } | |
122 | ||
123 | /* This function is useful for cleanups. | |
124 | Do | |
125 | ||
126 | foo = xmalloc (...); | |
127 | old_chain = make_cleanup (free_current_contents, &foo); | |
128 | ||
129 | to arrange to free the object thus allocated. */ | |
130 | ||
131 | void | |
132 | free_current_contents (location) | |
133 | char **location; | |
134 | { | |
135 | free (*location); | |
136 | } | |
137 | \f | |
138 | /* Generally useful subroutines used throughout the program. */ | |
139 | ||
140 | /* Like malloc but get error if no storage available. */ | |
141 | ||
142 | char * | |
143 | xmalloc (size) | |
144 | long size; | |
145 | { | |
146 | register char *val = (char *) malloc (size); | |
147 | if (!val) | |
148 | fatal ("virtual memory exhausted.", 0); | |
149 | return val; | |
150 | } | |
151 | ||
152 | /* Like realloc but get error if no storage available. */ | |
153 | ||
154 | char * | |
155 | xrealloc (ptr, size) | |
156 | char *ptr; | |
157 | long size; | |
158 | { | |
159 | register char *val = (char *) realloc (ptr, size); | |
160 | if (!val) | |
161 | fatal ("virtual memory exhausted.", 0); | |
162 | return val; | |
163 | } | |
164 | ||
165 | /* Print the system error message for errno, and also mention STRING | |
166 | as the file name for which the error was encountered. | |
167 | Then return to command level. */ | |
168 | ||
169 | void | |
170 | perror_with_name (string) | |
171 | char *string; | |
172 | { | |
173 | extern int sys_nerr; | |
174 | extern char *sys_errlist[]; | |
175 | extern int errno; | |
176 | char *err; | |
177 | char *combined; | |
178 | ||
179 | if (errno < sys_nerr) | |
180 | err = sys_errlist[errno]; | |
181 | else | |
182 | err = "unknown error"; | |
183 | ||
184 | combined = (char *) alloca (strlen (err) + strlen (string) + 3); | |
185 | strcpy (combined, string); | |
186 | strcat (combined, ": "); | |
187 | strcat (combined, err); | |
188 | ||
189 | error ("%s.", combined); | |
190 | } | |
191 | ||
192 | /* Print the system error message for ERRCODE, and also mention STRING | |
193 | as the file name for which the error was encountered. */ | |
194 | ||
195 | void | |
196 | print_sys_errmsg (string, errcode) | |
197 | char *string; | |
198 | int errcode; | |
199 | { | |
200 | extern int sys_nerr; | |
201 | extern char *sys_errlist[]; | |
202 | char *err; | |
203 | char *combined; | |
204 | ||
205 | if (errcode < sys_nerr) | |
206 | err = sys_errlist[errcode]; | |
207 | else | |
208 | err = "unknown error"; | |
209 | ||
210 | combined = (char *) alloca (strlen (err) + strlen (string) + 3); | |
211 | strcpy (combined, string); | |
212 | strcat (combined, ": "); | |
213 | strcat (combined, err); | |
214 | ||
215 | printf ("%s.\n", combined); | |
216 | } | |
217 | ||
218 | void | |
219 | quit () | |
220 | { | |
221 | fflush (stdout); | |
222 | #ifdef TIOCFLUSH | |
223 | ioctl (fileno (stdout), TIOCFLUSH, 0); | |
224 | #endif | |
225 | error ("Quit"); | |
226 | } | |
227 | ||
228 | /* Control C comes here */ | |
229 | ||
230 | void | |
231 | request_quit () | |
232 | { | |
233 | quit_flag = 1; | |
234 | if (immediate_quit) | |
235 | quit (); | |
236 | } | |
237 | ||
238 | /* Print an error message and return to command level. | |
239 | STRING is the error message, used as a fprintf string, | |
240 | and ARG is passed as an argument to it. */ | |
241 | ||
242 | void | |
243 | error (string, arg1, arg2, arg3) | |
244 | char *string; | |
245 | int arg1, arg2, arg3; | |
246 | { | |
247 | fflush (stdout); | |
248 | fprintf (stderr, string, arg1, arg2, arg3); | |
249 | fprintf (stderr, "\n"); | |
250 | return_to_top_level (); | |
251 | } | |
252 | ||
253 | /* Print an error message and exit reporting failure. | |
254 | This is for a error that we cannot continue from. | |
255 | STRING and ARG are passed to fprintf. */ | |
256 | ||
257 | void | |
258 | fatal (string, arg) | |
259 | char *string; | |
260 | int arg; | |
261 | { | |
262 | fprintf (stderr, "gdb: "); | |
263 | fprintf (stderr, string, arg); | |
264 | fprintf (stderr, "\n"); | |
265 | exit (1); | |
266 | } | |
267 | ||
268 | /* Make a copy of the string at PTR with SIZE characters | |
269 | (and add a null character at the end in the copy). | |
270 | Uses malloc to get the space. Returns the address of the copy. */ | |
271 | ||
272 | char * | |
273 | savestring (ptr, size) | |
274 | char *ptr; | |
275 | int size; | |
276 | { | |
277 | register char *p = (char *) xmalloc (size + 1); | |
278 | bcopy (ptr, p, size); | |
279 | p[size] = 0; | |
280 | return p; | |
281 | } | |
282 | ||
283 | char * | |
284 | concat (s1, s2, s3) | |
285 | char *s1, *s2, *s3; | |
286 | { | |
287 | register int len = strlen (s1) + strlen (s2) + strlen (s3) + 1; | |
288 | register char *val = (char *) xmalloc (len); | |
289 | strcpy (val, s1); | |
290 | strcat (val, s2); | |
291 | strcat (val, s3); | |
292 | return val; | |
293 | } | |
294 | ||
295 | void | |
296 | print_spaces (n, file) | |
297 | register int n; | |
298 | register FILE *file; | |
299 | { | |
300 | while (n-- > 0) | |
301 | fputc (' ', file); | |
302 | } | |
303 | ||
304 | /* Ask user a y-or-n question and return 1 iff answer is yes. | |
305 | Takes three args which are given to printf to print the question. | |
306 | The first, a control string, should end in "? ". | |
307 | It should not say how to answer, because we do that. */ | |
308 | ||
309 | int | |
310 | query (ctlstr, arg1, arg2) | |
311 | char *ctlstr; | |
312 | { | |
313 | register int answer; | |
314 | ||
315 | /* Automatically answer "yes" if input is not from a terminal. */ | |
316 | if (!input_from_terminal_p ()) | |
317 | return 1; | |
318 | ||
319 | while (1) | |
320 | { | |
321 | printf (ctlstr, arg1, arg2); | |
322 | printf ("(y or n) "); | |
323 | fflush (stdout); | |
324 | answer = fgetc (stdin); | |
325 | clearerr (stdin); /* in case of C-d */ | |
326 | if (answer != '\n') | |
327 | while (fgetc (stdin) != '\n') clearerr (stdin); | |
328 | if (answer >= 'a') | |
329 | answer -= 040; | |
330 | if (answer == 'Y') | |
331 | return 1; | |
332 | if (answer == 'N') | |
333 | return 0; | |
334 | printf ("Please answer y or n.\n"); | |
335 | } | |
336 | } | |
337 | \f | |
338 | /* Parse a C escape sequence. STRING_PTR points to a variable | |
339 | containing a pointer to the string to parse. That pointer | |
340 | is updated past the characters we use. The value of the | |
341 | escape sequence is returned. | |
342 | ||
343 | A negative value means the sequence \ newline was seen, | |
344 | which is supposed to be equivalent to nothing at all. | |
345 | ||
346 | If \ is followed by a null character, we return a negative | |
347 | value and leave the string pointer pointing at the null character. | |
348 | ||
349 | If \ is followed by 000, we return 0 and leave the string pointer | |
350 | after the zeros. A value of 0 does not mean end of string. */ | |
351 | ||
352 | int | |
353 | parse_escape (string_ptr) | |
354 | char **string_ptr; | |
355 | { | |
356 | register int c = *(*string_ptr)++; | |
357 | switch (c) | |
358 | { | |
359 | case 'a': | |
360 | return '\a'; | |
361 | case 'b': | |
362 | return '\b'; | |
363 | case 'e': | |
364 | return 033; | |
365 | case 'f': | |
366 | return '\f'; | |
367 | case 'n': | |
368 | return '\n'; | |
369 | case 'r': | |
370 | return '\r'; | |
371 | case 't': | |
372 | return '\t'; | |
373 | case 'v': | |
374 | return '\v'; | |
375 | case '\n': | |
376 | return -2; | |
377 | case 0: | |
378 | (*string_ptr)--; | |
379 | return 0; | |
380 | case '^': | |
381 | c = *(*string_ptr)++; | |
382 | if (c == '\\') | |
383 | c = parse_escape (string_ptr); | |
384 | if (c == '?') | |
385 | return 0177; | |
386 | return (c & 0200) | (c & 037); | |
387 | ||
388 | case '0': | |
389 | case '1': | |
390 | case '2': | |
391 | case '3': | |
392 | case '4': | |
393 | case '5': | |
394 | case '6': | |
395 | case '7': | |
396 | { | |
397 | register int i = c - '0'; | |
398 | register int count = 0; | |
399 | while (++count < 3) | |
400 | { | |
401 | if ((c = *(*string_ptr)++) >= '0' && c <= '7') | |
402 | { | |
403 | i *= 8; | |
404 | i += c - '0'; | |
405 | } | |
406 | else | |
407 | { | |
408 | (*string_ptr)--; | |
409 | break; | |
410 | } | |
411 | } | |
412 | return i; | |
413 | } | |
414 | default: | |
415 | return c; | |
416 | } | |
417 | } | |
418 | \f | |
419 | void | |
420 | printchar (ch, stream) | |
421 | unsigned char ch; | |
422 | FILE *stream; | |
423 | { | |
424 | register int c = ch; | |
425 | if (c < 040 || c >= 0177) | |
426 | { | |
427 | if (c == '\n') | |
428 | fprintf (stream, "\\n"); | |
429 | else if (c == '\b') | |
430 | fprintf (stream, "\\b"); | |
431 | else if (c == '\t') | |
432 | fprintf (stream, "\\t"); | |
433 | else if (c == '\f') | |
434 | fprintf (stream, "\\f"); | |
435 | else if (c == '\r') | |
436 | fprintf (stream, "\\r"); | |
437 | else if (c == 033) | |
438 | fprintf (stream, "\\e"); | |
439 | else if (c == '\a') | |
440 | fprintf (stream, "\\a"); | |
441 | else | |
442 | fprintf (stream, "\\%03o", c); | |
443 | } | |
444 | else | |
445 | { | |
446 | if (c == '\\' || c == '"' || c == '\'') | |
447 | fputc ('\\', stream); | |
448 | fputc (c, stream); | |
449 | } | |
450 | } | |
451 | @ | |
452 | ||
453 | ||
454 | 1.1 | |
455 | log | |
456 | @Initial revision | |
457 | @ | |
458 | text | |
459 | @d194 1 | |
460 | d196 1 | |
461 | @ |