]>
Commit | Line | Data |
---|---|---|
bd5635a1 RP |
1 | /* General utility routines for GDB, the GNU debugger. |
2 | Copyright (C) 1986, 1989, 1990, 1991 Free Software Foundation, Inc. | |
3 | ||
4 | This file is part of GDB. | |
5 | ||
351b221d | 6 | This program is free software; you can redistribute it and/or modify |
bd5635a1 | 7 | it under the terms of the GNU General Public License as published by |
351b221d JG |
8 | the Free Software Foundation; either version 2 of the License, or |
9 | (at your option) any later version. | |
bd5635a1 | 10 | |
351b221d | 11 | This program is distributed in the hope that it will be useful, |
bd5635a1 RP |
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 | |
351b221d JG |
17 | along with this program; if not, write to the Free Software |
18 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
bd5635a1 RP |
19 | |
20 | #include <stdio.h> | |
21 | #include <sys/ioctl.h> | |
22 | #include <sys/param.h> | |
23 | #include <pwd.h> | |
2bc2e684 FF |
24 | #include <varargs.h> |
25 | #include <ctype.h> | |
26 | #include <string.h> | |
27 | ||
bd5635a1 | 28 | #include "defs.h" |
bd5635a1 RP |
29 | #include "signals.h" |
30 | #include "gdbcmd.h" | |
31 | #include "terminal.h" | |
bd5635a1 RP |
32 | #include "bfd.h" |
33 | #include "target.h" | |
34 | ||
35 | extern volatile void return_to_top_level (); | |
36 | extern volatile void exit (); | |
37 | extern char *gdb_readline (); | |
38 | extern char *getenv(); | |
39 | extern char *malloc(); | |
40 | extern char *realloc(); | |
41 | ||
42 | /* If this definition isn't overridden by the header files, assume | |
43 | that isatty and fileno exist on this system. */ | |
44 | #ifndef ISATTY | |
45 | #define ISATTY(FP) (isatty (fileno (FP))) | |
46 | #endif | |
47 | ||
bd5635a1 RP |
48 | /* Chain of cleanup actions established with make_cleanup, |
49 | to be executed if an error happens. */ | |
50 | ||
51 | static struct cleanup *cleanup_chain; | |
52 | ||
53 | /* Nonzero means a quit has been requested. */ | |
54 | ||
55 | int quit_flag; | |
56 | ||
57 | /* Nonzero means quit immediately if Control-C is typed now, | |
58 | rather than waiting until QUIT is executed. */ | |
59 | ||
60 | int immediate_quit; | |
61 | ||
62 | /* Nonzero means that encoded C++ names should be printed out in their | |
63 | C++ form rather than raw. */ | |
64 | ||
65 | int demangle = 1; | |
66 | ||
67 | /* Nonzero means that encoded C++ names should be printed out in their | |
68 | C++ form even in assembler language displays. If this is set, but | |
69 | DEMANGLE is zero, names are printed raw, i.e. DEMANGLE controls. */ | |
70 | ||
71 | int asm_demangle = 0; | |
72 | ||
73 | /* Nonzero means that strings with character values >0x7F should be printed | |
74 | as octal escapes. Zero means just print the value (e.g. it's an | |
75 | international character, and the terminal or window can cope.) */ | |
76 | ||
77 | int sevenbit_strings = 0; | |
81066208 JG |
78 | |
79 | /* String to be printed before error messages, if any. */ | |
80 | ||
81 | char *error_pre_print; | |
2bc2e684 | 82 | char *warning_pre_print; |
bd5635a1 RP |
83 | \f |
84 | /* Add a new cleanup to the cleanup_chain, | |
85 | and return the previous chain pointer | |
86 | to be passed later to do_cleanups or discard_cleanups. | |
87 | Args are FUNCTION to clean up with, and ARG to pass to it. */ | |
88 | ||
89 | struct cleanup * | |
90 | make_cleanup (function, arg) | |
91 | void (*function) (); | |
92 | int arg; | |
93 | { | |
94 | register struct cleanup *new | |
95 | = (struct cleanup *) xmalloc (sizeof (struct cleanup)); | |
96 | register struct cleanup *old_chain = cleanup_chain; | |
97 | ||
98 | new->next = cleanup_chain; | |
99 | new->function = function; | |
100 | new->arg = arg; | |
101 | cleanup_chain = new; | |
102 | ||
103 | return old_chain; | |
104 | } | |
105 | ||
106 | /* Discard cleanups and do the actions they describe | |
107 | until we get back to the point OLD_CHAIN in the cleanup_chain. */ | |
108 | ||
109 | void | |
110 | do_cleanups (old_chain) | |
111 | register struct cleanup *old_chain; | |
112 | { | |
113 | register struct cleanup *ptr; | |
114 | while ((ptr = cleanup_chain) != old_chain) | |
115 | { | |
5e5215eb | 116 | cleanup_chain = ptr->next; /* Do this first incase recursion */ |
bd5635a1 | 117 | (*ptr->function) (ptr->arg); |
bd5635a1 RP |
118 | free (ptr); |
119 | } | |
120 | } | |
121 | ||
122 | /* Discard cleanups, not doing the actions they describe, | |
123 | until we get back to the point OLD_CHAIN in the cleanup_chain. */ | |
124 | ||
125 | void | |
126 | discard_cleanups (old_chain) | |
127 | register struct cleanup *old_chain; | |
128 | { | |
129 | register struct cleanup *ptr; | |
130 | while ((ptr = cleanup_chain) != old_chain) | |
131 | { | |
132 | cleanup_chain = ptr->next; | |
133 | free (ptr); | |
134 | } | |
135 | } | |
136 | ||
137 | /* Set the cleanup_chain to 0, and return the old cleanup chain. */ | |
138 | struct cleanup * | |
139 | save_cleanups () | |
140 | { | |
141 | struct cleanup *old_chain = cleanup_chain; | |
142 | ||
143 | cleanup_chain = 0; | |
144 | return old_chain; | |
145 | } | |
146 | ||
147 | /* Restore the cleanup chain from a previously saved chain. */ | |
148 | void | |
149 | restore_cleanups (chain) | |
150 | struct cleanup *chain; | |
151 | { | |
152 | cleanup_chain = chain; | |
153 | } | |
154 | ||
155 | /* This function is useful for cleanups. | |
156 | Do | |
157 | ||
158 | foo = xmalloc (...); | |
159 | old_chain = make_cleanup (free_current_contents, &foo); | |
160 | ||
161 | to arrange to free the object thus allocated. */ | |
162 | ||
163 | void | |
164 | free_current_contents (location) | |
165 | char **location; | |
166 | { | |
167 | free (*location); | |
168 | } | |
169 | \f | |
2bc2e684 FF |
170 | /* Provide a hook for modules wishing to print their own warning messages |
171 | to set up the terminal state in a compatible way, without them having | |
172 | to import all the target_<...> macros. */ | |
173 | ||
174 | void | |
175 | warning_setup () | |
176 | { | |
177 | target_terminal_ours (); | |
178 | wrap_here(""); /* Force out any buffered output */ | |
179 | fflush (stdout); | |
180 | } | |
181 | ||
182 | /* Print a warning message. | |
183 | The first argument STRING is the warning message, used as a fprintf string, | |
184 | and the remaining args are passed as arguments to it. | |
185 | The primary difference between warnings and errors is that a warning | |
186 | does not force the return to command level. */ | |
187 | ||
188 | /* VARARGS */ | |
189 | void | |
190 | warning (va_alist) | |
191 | va_dcl | |
192 | { | |
193 | va_list args; | |
194 | char *string; | |
195 | ||
196 | va_start (args); | |
197 | target_terminal_ours (); | |
198 | wrap_here(""); /* Force out any buffered output */ | |
199 | fflush (stdout); | |
200 | if (warning_pre_print) | |
201 | fprintf (stderr, warning_pre_print); | |
202 | string = va_arg (args, char *); | |
203 | vfprintf (stderr, string, args); | |
204 | fprintf (stderr, "\n"); | |
205 | va_end (args); | |
206 | } | |
207 | ||
bd5635a1 RP |
208 | /* Print an error message and return to command level. |
209 | The first argument STRING is the error message, used as a fprintf string, | |
210 | and the remaining args are passed as arguments to it. */ | |
211 | ||
212 | /* VARARGS */ | |
ee1ad69c | 213 | volatile void |
bd5635a1 RP |
214 | error (va_alist) |
215 | va_dcl | |
216 | { | |
217 | va_list args; | |
218 | char *string; | |
219 | ||
220 | va_start (args); | |
221 | target_terminal_ours (); | |
2bc2e684 | 222 | wrap_here(""); /* Force out any buffered output */ |
bd5635a1 | 223 | fflush (stdout); |
81066208 JG |
224 | if (error_pre_print) |
225 | fprintf (stderr, error_pre_print); | |
bd5635a1 RP |
226 | string = va_arg (args, char *); |
227 | vfprintf (stderr, string, args); | |
228 | fprintf (stderr, "\n"); | |
229 | va_end (args); | |
230 | return_to_top_level (); | |
231 | } | |
232 | ||
233 | /* Print an error message and exit reporting failure. | |
234 | This is for a error that we cannot continue from. | |
235 | The arguments are printed a la printf. */ | |
236 | ||
237 | /* VARARGS */ | |
ee1ad69c | 238 | volatile void |
bd5635a1 RP |
239 | fatal (va_alist) |
240 | va_dcl | |
241 | { | |
242 | va_list args; | |
243 | char *string; | |
244 | ||
245 | va_start (args); | |
246 | string = va_arg (args, char *); | |
247 | fprintf (stderr, "gdb: "); | |
248 | vfprintf (stderr, string, args); | |
249 | fprintf (stderr, "\n"); | |
250 | va_end (args); | |
251 | exit (1); | |
252 | } | |
253 | ||
254 | /* Print an error message and exit, dumping core. | |
255 | The arguments are printed a la printf (). */ | |
256 | /* VARARGS */ | |
257 | void | |
258 | fatal_dump_core (va_alist) | |
259 | va_dcl | |
260 | { | |
261 | va_list args; | |
262 | char *string; | |
263 | ||
264 | va_start (args); | |
265 | string = va_arg (args, char *); | |
266 | /* "internal error" is always correct, since GDB should never dump | |
267 | core, no matter what the input. */ | |
268 | fprintf (stderr, "gdb internal error: "); | |
269 | vfprintf (stderr, string, args); | |
270 | fprintf (stderr, "\n"); | |
271 | va_end (args); | |
272 | ||
273 | signal (SIGQUIT, SIG_DFL); | |
274 | kill (getpid (), SIGQUIT); | |
275 | /* We should never get here, but just in case... */ | |
276 | exit (1); | |
277 | } | |
278 | \f | |
279 | /* Memory management stuff (malloc friends). */ | |
280 | ||
281 | #if defined (NO_MALLOC_CHECK) | |
282 | void | |
283 | init_malloc () | |
284 | {} | |
285 | #else /* Have mcheck(). */ | |
286 | static void | |
287 | malloc_botch () | |
288 | { | |
289 | fatal_dump_core ("Memory corruption"); | |
290 | } | |
291 | ||
292 | void | |
293 | init_malloc () | |
294 | { | |
295 | mcheck (malloc_botch); | |
f266e564 | 296 | mtrace (); |
bd5635a1 RP |
297 | } |
298 | #endif /* Have mcheck(). */ | |
299 | ||
300 | /* Like malloc but get error if no storage available. */ | |
301 | ||
302 | #ifdef __STDC__ | |
303 | void * | |
304 | #else | |
305 | char * | |
306 | #endif | |
307 | xmalloc (size) | |
308 | long size; | |
309 | { | |
310 | register char *val; | |
311 | ||
312 | /* At least one place (dbxread.c:condense_misc_bunches where misc_count == 0) | |
313 | GDB wants to allocate zero bytes. */ | |
314 | if (size == 0) | |
315 | return NULL; | |
316 | ||
317 | val = (char *) malloc (size); | |
318 | if (!val) | |
319 | fatal ("virtual memory exhausted.", 0); | |
320 | return val; | |
321 | } | |
322 | ||
323 | /* Like realloc but get error if no storage available. */ | |
324 | ||
325 | #ifdef __STDC__ | |
326 | void * | |
327 | #else | |
328 | char * | |
329 | #endif | |
330 | xrealloc (ptr, size) | |
331 | char *ptr; | |
332 | long size; | |
333 | { | |
334 | register char *val = (char *) realloc (ptr, size); | |
335 | if (!val) | |
336 | fatal ("virtual memory exhausted.", 0); | |
337 | return val; | |
338 | } | |
339 | ||
340 | /* Print the system error message for errno, and also mention STRING | |
341 | as the file name for which the error was encountered. | |
342 | Then return to command level. */ | |
343 | ||
344 | void | |
345 | perror_with_name (string) | |
346 | char *string; | |
347 | { | |
348 | extern int sys_nerr; | |
349 | extern char *sys_errlist[]; | |
350 | char *err; | |
351 | char *combined; | |
352 | ||
353 | if (errno < sys_nerr) | |
354 | err = sys_errlist[errno]; | |
355 | else | |
356 | err = "unknown error"; | |
357 | ||
358 | combined = (char *) alloca (strlen (err) + strlen (string) + 3); | |
359 | strcpy (combined, string); | |
360 | strcat (combined, ": "); | |
361 | strcat (combined, err); | |
362 | ||
363 | /* I understand setting these is a matter of taste. Still, some people | |
364 | may clear errno but not know about bfd_error. Doing this here is not | |
365 | unreasonable. */ | |
366 | bfd_error = no_error; | |
367 | errno = 0; | |
368 | ||
369 | error ("%s.", combined); | |
370 | } | |
371 | ||
372 | /* Print the system error message for ERRCODE, and also mention STRING | |
373 | as the file name for which the error was encountered. */ | |
374 | ||
375 | void | |
376 | print_sys_errmsg (string, errcode) | |
377 | char *string; | |
378 | int errcode; | |
379 | { | |
380 | extern int sys_nerr; | |
381 | extern char *sys_errlist[]; | |
382 | char *err; | |
383 | char *combined; | |
384 | ||
385 | if (errcode < sys_nerr) | |
386 | err = sys_errlist[errcode]; | |
387 | else | |
388 | err = "unknown error"; | |
389 | ||
390 | combined = (char *) alloca (strlen (err) + strlen (string) + 3); | |
391 | strcpy (combined, string); | |
392 | strcat (combined, ": "); | |
393 | strcat (combined, err); | |
394 | ||
395 | printf ("%s.\n", combined); | |
396 | } | |
397 | ||
398 | /* Control C eventually causes this to be called, at a convenient time. */ | |
399 | ||
400 | void | |
401 | quit () | |
402 | { | |
403 | target_terminal_ours (); | |
d11c44f1 | 404 | wrap_here ((char *)0); /* Force out any pending output */ |
bd5635a1 RP |
405 | #ifdef HAVE_TERMIO |
406 | ioctl (fileno (stdout), TCFLSH, 1); | |
407 | #else /* not HAVE_TERMIO */ | |
408 | ioctl (fileno (stdout), TIOCFLUSH, 0); | |
409 | #endif /* not HAVE_TERMIO */ | |
410 | #ifdef TIOCGPGRP | |
411 | error ("Quit"); | |
412 | #else | |
413 | error ("Quit (expect signal %d when inferior is resumed)", SIGINT); | |
414 | #endif /* TIOCGPGRP */ | |
415 | } | |
416 | ||
417 | /* Control C comes here */ | |
418 | ||
419 | void | |
420 | request_quit () | |
421 | { | |
422 | quit_flag = 1; | |
423 | ||
424 | #ifdef USG | |
425 | /* Restore the signal handler. */ | |
426 | signal (SIGINT, request_quit); | |
427 | #endif | |
428 | ||
429 | if (immediate_quit) | |
430 | quit (); | |
431 | } | |
432 | \f | |
433 | /* My replacement for the read system call. | |
434 | Used like `read' but keeps going if `read' returns too soon. */ | |
435 | ||
436 | int | |
437 | myread (desc, addr, len) | |
438 | int desc; | |
439 | char *addr; | |
440 | int len; | |
441 | { | |
442 | register int val; | |
443 | int orglen = len; | |
444 | ||
445 | while (len > 0) | |
446 | { | |
447 | val = read (desc, addr, len); | |
448 | if (val < 0) | |
449 | return val; | |
450 | if (val == 0) | |
451 | return orglen - len; | |
452 | len -= val; | |
453 | addr += val; | |
454 | } | |
455 | return orglen; | |
456 | } | |
457 | \f | |
458 | /* Make a copy of the string at PTR with SIZE characters | |
459 | (and add a null character at the end in the copy). | |
460 | Uses malloc to get the space. Returns the address of the copy. */ | |
461 | ||
462 | char * | |
463 | savestring (ptr, size) | |
464 | char *ptr; | |
465 | int size; | |
466 | { | |
467 | register char *p = (char *) xmalloc (size + 1); | |
468 | bcopy (ptr, p, size); | |
469 | p[size] = 0; | |
470 | return p; | |
471 | } | |
472 | ||
8aa13b87 JK |
473 | /* The "const" is so it compiles under DGUX (which prototypes strsave |
474 | in <string.h>. FIXME: This should be named "xstrsave", shouldn't it? | |
475 | Doesn't real strsave return NULL if out of memory? */ | |
bd5635a1 RP |
476 | char * |
477 | strsave (ptr) | |
8aa13b87 | 478 | const char *ptr; |
bd5635a1 RP |
479 | { |
480 | return savestring (ptr, strlen (ptr)); | |
481 | } | |
482 | ||
bd5635a1 RP |
483 | void |
484 | print_spaces (n, file) | |
485 | register int n; | |
486 | register FILE *file; | |
487 | { | |
488 | while (n-- > 0) | |
489 | fputc (' ', file); | |
490 | } | |
491 | ||
492 | /* Ask user a y-or-n question and return 1 iff answer is yes. | |
493 | Takes three args which are given to printf to print the question. | |
494 | The first, a control string, should end in "? ". | |
495 | It should not say how to answer, because we do that. */ | |
496 | ||
497 | /* VARARGS */ | |
498 | int | |
499 | query (va_alist) | |
500 | va_dcl | |
501 | { | |
502 | va_list args; | |
503 | char *ctlstr; | |
504 | register int answer; | |
505 | register int ans2; | |
506 | ||
507 | /* Automatically answer "yes" if input is not from a terminal. */ | |
508 | if (!input_from_terminal_p ()) | |
509 | return 1; | |
510 | ||
511 | while (1) | |
512 | { | |
513 | va_start (args); | |
514 | ctlstr = va_arg (args, char *); | |
515 | vfprintf (stdout, ctlstr, args); | |
516 | va_end (args); | |
517 | printf ("(y or n) "); | |
518 | fflush (stdout); | |
519 | answer = fgetc (stdin); | |
520 | clearerr (stdin); /* in case of C-d */ | |
521 | if (answer == EOF) /* C-d */ | |
522 | return 1; | |
523 | if (answer != '\n') /* Eat rest of input line, to EOF or newline */ | |
524 | do | |
525 | { | |
526 | ans2 = fgetc (stdin); | |
527 | clearerr (stdin); | |
528 | } | |
529 | while (ans2 != EOF && ans2 != '\n'); | |
530 | if (answer >= 'a') | |
531 | answer -= 040; | |
532 | if (answer == 'Y') | |
533 | return 1; | |
534 | if (answer == 'N') | |
535 | return 0; | |
536 | printf ("Please answer y or n.\n"); | |
537 | } | |
538 | } | |
539 | \f | |
540 | /* Parse a C escape sequence. STRING_PTR points to a variable | |
541 | containing a pointer to the string to parse. That pointer | |
542 | should point to the character after the \. That pointer | |
543 | is updated past the characters we use. The value of the | |
544 | escape sequence is returned. | |
545 | ||
546 | A negative value means the sequence \ newline was seen, | |
547 | which is supposed to be equivalent to nothing at all. | |
548 | ||
549 | If \ is followed by a null character, we return a negative | |
550 | value and leave the string pointer pointing at the null character. | |
551 | ||
552 | If \ is followed by 000, we return 0 and leave the string pointer | |
553 | after the zeros. A value of 0 does not mean end of string. */ | |
554 | ||
555 | int | |
556 | parse_escape (string_ptr) | |
557 | char **string_ptr; | |
558 | { | |
559 | register int c = *(*string_ptr)++; | |
560 | switch (c) | |
561 | { | |
562 | case 'a': | |
2bc2e684 | 563 | return 007; /* Bell (alert) char */ |
bd5635a1 RP |
564 | case 'b': |
565 | return '\b'; | |
2bc2e684 | 566 | case 'e': /* Escape character */ |
bd5635a1 RP |
567 | return 033; |
568 | case 'f': | |
569 | return '\f'; | |
570 | case 'n': | |
571 | return '\n'; | |
572 | case 'r': | |
573 | return '\r'; | |
574 | case 't': | |
575 | return '\t'; | |
576 | case 'v': | |
577 | return '\v'; | |
578 | case '\n': | |
579 | return -2; | |
580 | case 0: | |
581 | (*string_ptr)--; | |
582 | return 0; | |
583 | case '^': | |
584 | c = *(*string_ptr)++; | |
585 | if (c == '\\') | |
586 | c = parse_escape (string_ptr); | |
587 | if (c == '?') | |
588 | return 0177; | |
589 | return (c & 0200) | (c & 037); | |
590 | ||
591 | case '0': | |
592 | case '1': | |
593 | case '2': | |
594 | case '3': | |
595 | case '4': | |
596 | case '5': | |
597 | case '6': | |
598 | case '7': | |
599 | { | |
600 | register int i = c - '0'; | |
601 | register int count = 0; | |
602 | while (++count < 3) | |
603 | { | |
604 | if ((c = *(*string_ptr)++) >= '0' && c <= '7') | |
605 | { | |
606 | i *= 8; | |
607 | i += c - '0'; | |
608 | } | |
609 | else | |
610 | { | |
611 | (*string_ptr)--; | |
612 | break; | |
613 | } | |
614 | } | |
615 | return i; | |
616 | } | |
617 | default: | |
618 | return c; | |
619 | } | |
620 | } | |
621 | \f | |
622 | /* Print the character CH on STREAM as part of the contents | |
623 | of a literal string whose delimiter is QUOTER. */ | |
624 | ||
625 | void | |
626 | printchar (ch, stream, quoter) | |
627 | unsigned char ch; | |
628 | FILE *stream; | |
629 | int quoter; | |
630 | { | |
631 | register int c = ch; | |
632 | ||
2bc2e684 | 633 | if (c < 040 || (sevenbit_strings && c >= 0177)) { |
bd5635a1 RP |
634 | switch (c) |
635 | { | |
636 | case '\n': | |
637 | fputs_filtered ("\\n", stream); | |
638 | break; | |
639 | case '\b': | |
640 | fputs_filtered ("\\b", stream); | |
641 | break; | |
642 | case '\t': | |
643 | fputs_filtered ("\\t", stream); | |
644 | break; | |
645 | case '\f': | |
646 | fputs_filtered ("\\f", stream); | |
647 | break; | |
648 | case '\r': | |
649 | fputs_filtered ("\\r", stream); | |
650 | break; | |
651 | case '\033': | |
652 | fputs_filtered ("\\e", stream); | |
653 | break; | |
654 | case '\007': | |
655 | fputs_filtered ("\\a", stream); | |
656 | break; | |
657 | default: | |
658 | fprintf_filtered (stream, "\\%.3o", (unsigned int) c); | |
659 | break; | |
660 | } | |
2bc2e684 FF |
661 | } else { |
662 | if (c == '\\' || c == quoter) | |
663 | fputs_filtered ("\\", stream); | |
664 | fprintf_filtered (stream, "%c", c); | |
665 | } | |
bd5635a1 RP |
666 | } |
667 | \f | |
668 | /* Number of lines per page or UINT_MAX if paging is disabled. */ | |
669 | static unsigned int lines_per_page; | |
670 | /* Number of chars per line or UNIT_MAX is line folding is disabled. */ | |
671 | static unsigned int chars_per_line; | |
672 | /* Current count of lines printed on this page, chars on this line. */ | |
673 | static unsigned int lines_printed, chars_printed; | |
674 | ||
675 | /* Buffer and start column of buffered text, for doing smarter word- | |
676 | wrapping. When someone calls wrap_here(), we start buffering output | |
677 | that comes through fputs_filtered(). If we see a newline, we just | |
678 | spit it out and forget about the wrap_here(). If we see another | |
679 | wrap_here(), we spit it out and remember the newer one. If we see | |
680 | the end of the line, we spit out a newline, the indent, and then | |
681 | the buffered output. | |
682 | ||
683 | wrap_column is the column number on the screen where wrap_buffer begins. | |
684 | When wrap_column is zero, wrapping is not in effect. | |
685 | wrap_buffer is malloc'd with chars_per_line+2 bytes. | |
686 | When wrap_buffer[0] is null, the buffer is empty. | |
687 | wrap_pointer points into it at the next character to fill. | |
688 | wrap_indent is the string that should be used as indentation if the | |
689 | wrap occurs. */ | |
690 | ||
691 | static char *wrap_buffer, *wrap_pointer, *wrap_indent; | |
692 | static int wrap_column; | |
693 | ||
e1ce8aa5 | 694 | /* ARGSUSED */ |
bd5635a1 RP |
695 | static void |
696 | set_width_command (args, from_tty, c) | |
697 | char *args; | |
698 | int from_tty; | |
699 | struct cmd_list_element *c; | |
700 | { | |
701 | if (!wrap_buffer) | |
702 | { | |
703 | wrap_buffer = (char *) xmalloc (chars_per_line + 2); | |
704 | wrap_buffer[0] = '\0'; | |
705 | } | |
706 | else | |
707 | wrap_buffer = (char *) xrealloc (wrap_buffer, chars_per_line + 2); | |
708 | wrap_pointer = wrap_buffer; /* Start it at the beginning */ | |
709 | } | |
710 | ||
711 | static void | |
712 | prompt_for_continue () | |
713 | { | |
351b221d JG |
714 | char *ignore; |
715 | ||
bd5635a1 | 716 | immediate_quit++; |
351b221d JG |
717 | ignore = gdb_readline ("---Type <return> to continue---"); |
718 | if (ignore) | |
719 | free (ignore); | |
bd5635a1 RP |
720 | chars_printed = lines_printed = 0; |
721 | immediate_quit--; | |
351b221d | 722 | dont_repeat (); /* Forget prev cmd -- CR won't repeat it. */ |
bd5635a1 RP |
723 | } |
724 | ||
725 | /* Reinitialize filter; ie. tell it to reset to original values. */ | |
726 | ||
727 | void | |
728 | reinitialize_more_filter () | |
729 | { | |
730 | lines_printed = 0; | |
731 | chars_printed = 0; | |
732 | } | |
733 | ||
734 | /* Indicate that if the next sequence of characters overflows the line, | |
735 | a newline should be inserted here rather than when it hits the end. | |
736 | If INDENT is nonzero, it is a string to be printed to indent the | |
737 | wrapped part on the next line. INDENT must remain accessible until | |
738 | the next call to wrap_here() or until a newline is printed through | |
739 | fputs_filtered(). | |
740 | ||
741 | If the line is already overfull, we immediately print a newline and | |
742 | the indentation, and disable further wrapping. | |
743 | ||
2bc2e684 FF |
744 | If we don't know the width of lines, but we know the page height, |
745 | we must not wrap words, but should still keep track of newlines | |
746 | that were explicitly printed. | |
747 | ||
bd5635a1 RP |
748 | INDENT should not contain tabs, as that |
749 | will mess up the char count on the next line. FIXME. */ | |
750 | ||
751 | void | |
752 | wrap_here(indent) | |
753 | char *indent; | |
754 | { | |
755 | if (wrap_buffer[0]) | |
756 | { | |
757 | *wrap_pointer = '\0'; | |
758 | fputs (wrap_buffer, stdout); | |
759 | } | |
760 | wrap_pointer = wrap_buffer; | |
761 | wrap_buffer[0] = '\0'; | |
2bc2e684 FF |
762 | if (chars_per_line == UINT_MAX) /* No line overflow checking */ |
763 | { | |
764 | wrap_column = 0; | |
765 | } | |
766 | else if (chars_printed >= chars_per_line) | |
bd5635a1 RP |
767 | { |
768 | puts_filtered ("\n"); | |
769 | puts_filtered (indent); | |
770 | wrap_column = 0; | |
771 | } | |
772 | else | |
773 | { | |
774 | wrap_column = chars_printed; | |
775 | wrap_indent = indent; | |
776 | } | |
777 | } | |
778 | ||
779 | /* Like fputs but pause after every screenful, and can wrap at points | |
780 | other than the final character of a line. | |
781 | Unlike fputs, fputs_filtered does not return a value. | |
782 | It is OK for LINEBUFFER to be NULL, in which case just don't print | |
783 | anything. | |
784 | ||
785 | Note that a longjmp to top level may occur in this routine | |
786 | (since prompt_for_continue may do so) so this routine should not be | |
787 | called when cleanups are not in place. */ | |
788 | ||
789 | void | |
790 | fputs_filtered (linebuffer, stream) | |
791 | char *linebuffer; | |
792 | FILE *stream; | |
793 | { | |
794 | char *lineptr; | |
795 | ||
796 | if (linebuffer == 0) | |
797 | return; | |
798 | ||
799 | /* Don't do any filtering if it is disabled. */ | |
800 | if (stream != stdout | |
801 | || (lines_per_page == UINT_MAX && chars_per_line == UINT_MAX)) | |
802 | { | |
803 | fputs (linebuffer, stream); | |
804 | return; | |
805 | } | |
806 | ||
807 | /* Go through and output each character. Show line extension | |
808 | when this is necessary; prompt user for new page when this is | |
809 | necessary. */ | |
810 | ||
811 | lineptr = linebuffer; | |
812 | while (*lineptr) | |
813 | { | |
814 | /* Possible new page. */ | |
815 | if (lines_printed >= lines_per_page - 1) | |
816 | prompt_for_continue (); | |
817 | ||
818 | while (*lineptr && *lineptr != '\n') | |
819 | { | |
820 | /* Print a single line. */ | |
821 | if (*lineptr == '\t') | |
822 | { | |
823 | if (wrap_column) | |
824 | *wrap_pointer++ = '\t'; | |
825 | else | |
826 | putc ('\t', stream); | |
827 | /* Shifting right by 3 produces the number of tab stops | |
828 | we have already passed, and then adding one and | |
829 | shifting left 3 advances to the next tab stop. */ | |
830 | chars_printed = ((chars_printed >> 3) + 1) << 3; | |
831 | lineptr++; | |
832 | } | |
833 | else | |
834 | { | |
835 | if (wrap_column) | |
836 | *wrap_pointer++ = *lineptr; | |
837 | else | |
838 | putc (*lineptr, stream); | |
839 | chars_printed++; | |
840 | lineptr++; | |
841 | } | |
842 | ||
843 | if (chars_printed >= chars_per_line) | |
844 | { | |
845 | unsigned int save_chars = chars_printed; | |
846 | ||
847 | chars_printed = 0; | |
848 | lines_printed++; | |
849 | /* If we aren't actually wrapping, don't output newline -- | |
850 | if chars_per_line is right, we probably just overflowed | |
851 | anyway; if it's wrong, let us keep going. */ | |
852 | if (wrap_column) | |
853 | putc ('\n', stream); | |
854 | ||
855 | /* Possible new page. */ | |
856 | if (lines_printed >= lines_per_page - 1) | |
857 | prompt_for_continue (); | |
858 | ||
859 | /* Now output indentation and wrapped string */ | |
860 | if (wrap_column) | |
861 | { | |
862 | if (wrap_indent) | |
863 | fputs (wrap_indent, stream); | |
864 | *wrap_pointer = '\0'; /* Null-terminate saved stuff */ | |
865 | fputs (wrap_buffer, stream); /* and eject it */ | |
866 | /* FIXME, this strlen is what prevents wrap_indent from | |
867 | containing tabs. However, if we recurse to print it | |
868 | and count its chars, we risk trouble if wrap_indent is | |
869 | longer than (the user settable) chars_per_line. | |
870 | Note also that this can set chars_printed > chars_per_line | |
871 | if we are printing a long string. */ | |
872 | chars_printed = strlen (wrap_indent) | |
873 | + (save_chars - wrap_column); | |
874 | wrap_pointer = wrap_buffer; /* Reset buffer */ | |
875 | wrap_buffer[0] = '\0'; | |
876 | wrap_column = 0; /* And disable fancy wrap */ | |
877 | } | |
878 | } | |
879 | } | |
880 | ||
881 | if (*lineptr == '\n') | |
882 | { | |
883 | chars_printed = 0; | |
d11c44f1 | 884 | wrap_here ((char *)0); /* Spit out chars, cancel further wraps */ |
bd5635a1 RP |
885 | lines_printed++; |
886 | putc ('\n', stream); | |
887 | lineptr++; | |
888 | } | |
889 | } | |
890 | } | |
891 | ||
892 | ||
893 | /* fputs_demangled is a variant of fputs_filtered that | |
894 | demangles g++ names.*/ | |
895 | ||
896 | void | |
897 | fputs_demangled (linebuffer, stream, arg_mode) | |
898 | char *linebuffer; | |
899 | FILE *stream; | |
900 | int arg_mode; | |
901 | { | |
902 | #ifdef __STDC__ | |
903 | extern char *cplus_demangle (const char *, int); | |
904 | #else | |
905 | extern char *cplus_demangle (); | |
906 | #endif | |
907 | #define SYMBOL_MAX 1024 | |
908 | ||
f88e7af8 JK |
909 | #define SYMBOL_CHAR(c) (isascii(c) \ |
910 | && (isalnum(c) || (c) == '_' || (c) == CPLUS_MARKER)) | |
bd5635a1 RP |
911 | |
912 | char buf[SYMBOL_MAX+1]; | |
913 | # define SLOP 5 /* How much room to leave in buf */ | |
914 | char *p; | |
915 | ||
916 | if (linebuffer == NULL) | |
917 | return; | |
918 | ||
919 | /* If user wants to see raw output, no problem. */ | |
920 | if (!demangle) { | |
921 | fputs_filtered (linebuffer, stream); | |
bdbd5f50 | 922 | return; |
bd5635a1 RP |
923 | } |
924 | ||
925 | p = linebuffer; | |
926 | ||
927 | while ( *p != (char) 0 ) { | |
928 | int i = 0; | |
929 | ||
930 | /* collect non-interesting characters into buf */ | |
931 | while ( *p != (char) 0 && !SYMBOL_CHAR(*p) && i < (int)sizeof(buf)-SLOP ) { | |
932 | buf[i++] = *p; | |
933 | p++; | |
934 | } | |
935 | if (i > 0) { | |
936 | /* output the non-interesting characters without demangling */ | |
937 | buf[i] = (char) 0; | |
938 | fputs_filtered(buf, stream); | |
939 | i = 0; /* reset buf */ | |
940 | } | |
941 | ||
942 | /* and now the interesting characters */ | |
943 | while (i < SYMBOL_MAX | |
944 | && *p != (char) 0 | |
945 | && SYMBOL_CHAR(*p) | |
946 | && i < (int)sizeof(buf) - SLOP) { | |
947 | buf[i++] = *p; | |
948 | p++; | |
949 | } | |
950 | buf[i] = (char) 0; | |
951 | if (i > 0) { | |
952 | char * result; | |
953 | ||
954 | if ( (result = cplus_demangle(buf, arg_mode)) != NULL ) { | |
955 | fputs_filtered(result, stream); | |
956 | free(result); | |
957 | } | |
958 | else { | |
959 | fputs_filtered(buf, stream); | |
960 | } | |
961 | } | |
962 | } | |
963 | } | |
964 | ||
965 | /* Print a variable number of ARGS using format FORMAT. If this | |
966 | information is going to put the amount written (since the last call | |
967 | to INITIALIZE_MORE_FILTER or the last page break) over the page size, | |
968 | print out a pause message and do a gdb_readline to get the users | |
969 | permision to continue. | |
970 | ||
971 | Unlike fprintf, this function does not return a value. | |
972 | ||
973 | We implement three variants, vfprintf (takes a vararg list and stream), | |
974 | fprintf (takes a stream to write on), and printf (the usual). | |
975 | ||
976 | Note that this routine has a restriction that the length of the | |
977 | final output line must be less than 255 characters *or* it must be | |
978 | less than twice the size of the format string. This is a very | |
979 | arbitrary restriction, but it is an internal restriction, so I'll | |
980 | put it in. This means that the %s format specifier is almost | |
981 | useless; unless the caller can GUARANTEE that the string is short | |
982 | enough, fputs_filtered should be used instead. | |
983 | ||
984 | Note also that a longjmp to top level may occur in this routine | |
985 | (since prompt_for_continue may do so) so this routine should not be | |
986 | called when cleanups are not in place. */ | |
987 | ||
bd5635a1 RP |
988 | /* VARARGS */ |
989 | void | |
990 | vfprintf_filtered (stream, format, args) | |
991 | va_list args; | |
bd5635a1 RP |
992 | FILE *stream; |
993 | char *format; | |
994 | { | |
995 | static char *linebuffer = (char *) 0; | |
996 | static int line_size; | |
997 | int format_length; | |
998 | ||
999 | format_length = strlen (format); | |
1000 | ||
1001 | /* Allocated linebuffer for the first time. */ | |
1002 | if (!linebuffer) | |
1003 | { | |
1004 | linebuffer = (char *) xmalloc (255); | |
1005 | line_size = 255; | |
1006 | } | |
1007 | ||
1008 | /* Reallocate buffer to a larger size if this is necessary. */ | |
1009 | if (format_length * 2 > line_size) | |
1010 | { | |
1011 | line_size = format_length * 2; | |
1012 | ||
1013 | /* You don't have to copy. */ | |
1014 | free (linebuffer); | |
1015 | linebuffer = (char *) xmalloc (line_size); | |
1016 | } | |
1017 | ||
1018 | ||
1019 | /* This won't blow up if the restrictions described above are | |
1020 | followed. */ | |
bd5635a1 | 1021 | (void) vsprintf (linebuffer, format, args); |
bd5635a1 RP |
1022 | |
1023 | fputs_filtered (linebuffer, stream); | |
1024 | } | |
1025 | ||
bd5635a1 RP |
1026 | /* VARARGS */ |
1027 | void | |
1028 | fprintf_filtered (va_alist) | |
1029 | va_dcl | |
1030 | { | |
1031 | va_list args; | |
1032 | FILE *stream; | |
1033 | char *format; | |
1034 | ||
1035 | va_start (args); | |
1036 | stream = va_arg (args, FILE *); | |
1037 | format = va_arg (args, char *); | |
1038 | ||
1039 | /* This won't blow up if the restrictions described above are | |
1040 | followed. */ | |
1041 | (void) vfprintf_filtered (stream, format, args); | |
1042 | va_end (args); | |
1043 | } | |
1044 | ||
1045 | /* VARARGS */ | |
1046 | void | |
1047 | printf_filtered (va_alist) | |
1048 | va_dcl | |
1049 | { | |
1050 | va_list args; | |
1051 | char *format; | |
1052 | ||
1053 | va_start (args); | |
1054 | format = va_arg (args, char *); | |
1055 | ||
1056 | (void) vfprintf_filtered (stdout, format, args); | |
1057 | va_end (args); | |
1058 | } | |
bd5635a1 RP |
1059 | |
1060 | /* Easy */ | |
1061 | ||
1062 | void | |
1063 | puts_filtered (string) | |
1064 | char *string; | |
1065 | { | |
1066 | fputs_filtered (string, stdout); | |
1067 | } | |
1068 | ||
1069 | /* Return a pointer to N spaces and a null. The pointer is good | |
1070 | until the next call to here. */ | |
1071 | char * | |
1072 | n_spaces (n) | |
1073 | int n; | |
1074 | { | |
1075 | register char *t; | |
1076 | static char *spaces; | |
1077 | static int max_spaces; | |
1078 | ||
1079 | if (n > max_spaces) | |
1080 | { | |
1081 | if (spaces) | |
1082 | free (spaces); | |
1083 | spaces = malloc (n+1); | |
1084 | for (t = spaces+n; t != spaces;) | |
1085 | *--t = ' '; | |
1086 | spaces[n] = '\0'; | |
1087 | max_spaces = n; | |
1088 | } | |
1089 | ||
1090 | return spaces + max_spaces - n; | |
1091 | } | |
1092 | ||
1093 | /* Print N spaces. */ | |
1094 | void | |
1095 | print_spaces_filtered (n, stream) | |
1096 | int n; | |
1097 | FILE *stream; | |
1098 | { | |
1099 | fputs_filtered (n_spaces (n), stream); | |
1100 | } | |
1101 | \f | |
1102 | /* C++ demangler stuff. */ | |
1103 | char *cplus_demangle (); | |
1104 | ||
1105 | /* Print NAME on STREAM, demangling if necessary. */ | |
1106 | void | |
1107 | fprint_symbol (stream, name) | |
1108 | FILE *stream; | |
1109 | char *name; | |
1110 | { | |
1111 | char *demangled; | |
1112 | if ((!demangle) || NULL == (demangled = cplus_demangle (name, 1))) | |
1113 | fputs_filtered (name, stream); | |
1114 | else | |
1115 | { | |
1116 | fputs_filtered (demangled, stream); | |
1117 | free (demangled); | |
1118 | } | |
1119 | } | |
1120 | \f | |
bd5635a1 RP |
1121 | void |
1122 | _initialize_utils () | |
1123 | { | |
1124 | struct cmd_list_element *c; | |
1125 | ||
1126 | c = add_set_cmd ("width", class_support, var_uinteger, | |
1127 | (char *)&chars_per_line, | |
1128 | "Set number of characters gdb thinks are in a line.", | |
1129 | &setlist); | |
1130 | add_show_from_set (c, &showlist); | |
1131 | c->function = set_width_command; | |
1132 | ||
1133 | add_show_from_set | |
1134 | (add_set_cmd ("height", class_support, | |
1135 | var_uinteger, (char *)&lines_per_page, | |
1136 | "Set number of lines gdb thinks are in a page.", &setlist), | |
1137 | &showlist); | |
1138 | ||
1139 | /* These defaults will be used if we are unable to get the correct | |
1140 | values from termcap. */ | |
1141 | lines_per_page = 24; | |
1142 | chars_per_line = 80; | |
1143 | /* Initialize the screen height and width from termcap. */ | |
1144 | { | |
1145 | char *termtype = getenv ("TERM"); | |
1146 | ||
1147 | /* Positive means success, nonpositive means failure. */ | |
1148 | int status; | |
1149 | ||
1150 | /* 2048 is large enough for all known terminals, according to the | |
1151 | GNU termcap manual. */ | |
1152 | char term_buffer[2048]; | |
1153 | ||
1154 | if (termtype) | |
1155 | { | |
1156 | status = tgetent (term_buffer, termtype); | |
1157 | if (status > 0) | |
1158 | { | |
1159 | int val; | |
1160 | ||
1161 | val = tgetnum ("li"); | |
1162 | if (val >= 0) | |
1163 | lines_per_page = val; | |
1164 | else | |
1165 | /* The number of lines per page is not mentioned | |
1166 | in the terminal description. This probably means | |
1167 | that paging is not useful (e.g. emacs shell window), | |
1168 | so disable paging. */ | |
1169 | lines_per_page = UINT_MAX; | |
1170 | ||
1171 | val = tgetnum ("co"); | |
1172 | if (val >= 0) | |
1173 | chars_per_line = val; | |
1174 | } | |
1175 | } | |
1176 | } | |
1177 | ||
2bc2e684 FF |
1178 | /* If the output is not a terminal, don't paginate it. */ |
1179 | if (!ISATTY (stdout)) | |
1180 | lines_per_page = UINT_MAX; | |
1181 | ||
bd5635a1 RP |
1182 | set_width_command ((char *)NULL, 0, c); |
1183 | ||
1184 | add_show_from_set | |
1185 | (add_set_cmd ("demangle", class_support, var_boolean, | |
1186 | (char *)&demangle, | |
1187 | "Set demangling of encoded C++ names when displaying symbols.", | |
f266e564 JK |
1188 | &setprintlist), |
1189 | &showprintlist); | |
bd5635a1 RP |
1190 | |
1191 | add_show_from_set | |
1192 | (add_set_cmd ("sevenbit-strings", class_support, var_boolean, | |
1193 | (char *)&sevenbit_strings, | |
1194 | "Set printing of 8-bit characters in strings as \\nnn.", | |
f266e564 JK |
1195 | &setprintlist), |
1196 | &showprintlist); | |
bd5635a1 RP |
1197 | |
1198 | add_show_from_set | |
1199 | (add_set_cmd ("asm-demangle", class_support, var_boolean, | |
1200 | (char *)&asm_demangle, | |
1201 | "Set demangling of C++ names in disassembly listings.", | |
f266e564 JK |
1202 | &setprintlist), |
1203 | &showprintlist); | |
bd5635a1 | 1204 | } |