Commit | Line | Data |
---|---|---|
bd5635a1 | 1 | /* General utility routines for GDB, the GNU debugger. |
7919c3ed | 2 | Copyright 1986, 1989, 1990, 1991, 1992 Free Software Foundation, Inc. |
bd5635a1 RP |
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 | 19 | |
d747e0af | 20 | #include "defs.h" |
51b57ded | 21 | #if !defined(__GO32__) |
bd5635a1 RP |
22 | #include <sys/ioctl.h> |
23 | #include <sys/param.h> | |
24 | #include <pwd.h> | |
51b57ded | 25 | #endif |
2bc2e684 FF |
26 | #include <varargs.h> |
27 | #include <ctype.h> | |
28 | #include <string.h> | |
29 | ||
bd5635a1 RP |
30 | #include "signals.h" |
31 | #include "gdbcmd.h" | |
32 | #include "terminal.h" | |
bd5635a1 RP |
33 | #include "bfd.h" |
34 | #include "target.h" | |
bcf2e6ab | 35 | #include "demangle.h" |
bd5635a1 | 36 | |
7919c3ed JG |
37 | /* Prototypes for local functions */ |
38 | ||
39 | #if !defined (NO_MALLOC_CHECK) | |
3624c875 | 40 | |
7919c3ed JG |
41 | static void |
42 | malloc_botch PARAMS ((void)); | |
3624c875 | 43 | |
7919c3ed JG |
44 | #endif /* NO_MALLOC_CHECK */ |
45 | ||
46 | static void | |
47 | fatal_dump_core (); /* Can't prototype with <varargs.h> usage... */ | |
48 | ||
49 | static void | |
50 | prompt_for_continue PARAMS ((void)); | |
51 | ||
52 | static void | |
53 | set_width_command PARAMS ((char *, int, struct cmd_list_element *)); | |
54 | ||
55 | static void | |
56 | vfprintf_filtered PARAMS ((FILE *, char *, va_list)); | |
bd5635a1 RP |
57 | |
58 | /* If this definition isn't overridden by the header files, assume | |
59 | that isatty and fileno exist on this system. */ | |
60 | #ifndef ISATTY | |
61 | #define ISATTY(FP) (isatty (fileno (FP))) | |
62 | #endif | |
63 | ||
bd5635a1 RP |
64 | /* Chain of cleanup actions established with make_cleanup, |
65 | to be executed if an error happens. */ | |
66 | ||
67 | static struct cleanup *cleanup_chain; | |
68 | ||
69 | /* Nonzero means a quit has been requested. */ | |
70 | ||
71 | int quit_flag; | |
72 | ||
73 | /* Nonzero means quit immediately if Control-C is typed now, | |
74 | rather than waiting until QUIT is executed. */ | |
75 | ||
76 | int immediate_quit; | |
77 | ||
78 | /* Nonzero means that encoded C++ names should be printed out in their | |
79 | C++ form rather than raw. */ | |
80 | ||
81 | int demangle = 1; | |
82 | ||
83 | /* Nonzero means that encoded C++ names should be printed out in their | |
84 | C++ form even in assembler language displays. If this is set, but | |
85 | DEMANGLE is zero, names are printed raw, i.e. DEMANGLE controls. */ | |
86 | ||
87 | int asm_demangle = 0; | |
88 | ||
89 | /* Nonzero means that strings with character values >0x7F should be printed | |
90 | as octal escapes. Zero means just print the value (e.g. it's an | |
91 | international character, and the terminal or window can cope.) */ | |
92 | ||
93 | int sevenbit_strings = 0; | |
81066208 JG |
94 | |
95 | /* String to be printed before error messages, if any. */ | |
96 | ||
97 | char *error_pre_print; | |
3624c875 | 98 | char *warning_pre_print = "\nwarning: "; |
bd5635a1 RP |
99 | \f |
100 | /* Add a new cleanup to the cleanup_chain, | |
101 | and return the previous chain pointer | |
102 | to be passed later to do_cleanups or discard_cleanups. | |
103 | Args are FUNCTION to clean up with, and ARG to pass to it. */ | |
104 | ||
105 | struct cleanup * | |
106 | make_cleanup (function, arg) | |
7919c3ed JG |
107 | void (*function) PARAMS ((PTR)); |
108 | PTR arg; | |
bd5635a1 RP |
109 | { |
110 | register struct cleanup *new | |
111 | = (struct cleanup *) xmalloc (sizeof (struct cleanup)); | |
112 | register struct cleanup *old_chain = cleanup_chain; | |
113 | ||
114 | new->next = cleanup_chain; | |
115 | new->function = function; | |
116 | new->arg = arg; | |
117 | cleanup_chain = new; | |
118 | ||
119 | return old_chain; | |
120 | } | |
121 | ||
122 | /* Discard cleanups and do the actions they describe | |
123 | until we get back to the point OLD_CHAIN in the cleanup_chain. */ | |
124 | ||
125 | void | |
126 | do_cleanups (old_chain) | |
127 | register struct cleanup *old_chain; | |
128 | { | |
129 | register struct cleanup *ptr; | |
130 | while ((ptr = cleanup_chain) != old_chain) | |
131 | { | |
5e5215eb | 132 | cleanup_chain = ptr->next; /* Do this first incase recursion */ |
bd5635a1 | 133 | (*ptr->function) (ptr->arg); |
bd5635a1 RP |
134 | free (ptr); |
135 | } | |
136 | } | |
137 | ||
138 | /* Discard cleanups, not doing the actions they describe, | |
139 | until we get back to the point OLD_CHAIN in the cleanup_chain. */ | |
140 | ||
141 | void | |
142 | discard_cleanups (old_chain) | |
143 | register struct cleanup *old_chain; | |
144 | { | |
145 | register struct cleanup *ptr; | |
146 | while ((ptr = cleanup_chain) != old_chain) | |
147 | { | |
148 | cleanup_chain = ptr->next; | |
be772100 | 149 | free ((PTR)ptr); |
bd5635a1 RP |
150 | } |
151 | } | |
152 | ||
153 | /* Set the cleanup_chain to 0, and return the old cleanup chain. */ | |
154 | struct cleanup * | |
155 | save_cleanups () | |
156 | { | |
157 | struct cleanup *old_chain = cleanup_chain; | |
158 | ||
159 | cleanup_chain = 0; | |
160 | return old_chain; | |
161 | } | |
162 | ||
163 | /* Restore the cleanup chain from a previously saved chain. */ | |
164 | void | |
165 | restore_cleanups (chain) | |
166 | struct cleanup *chain; | |
167 | { | |
168 | cleanup_chain = chain; | |
169 | } | |
170 | ||
171 | /* This function is useful for cleanups. | |
172 | Do | |
173 | ||
174 | foo = xmalloc (...); | |
175 | old_chain = make_cleanup (free_current_contents, &foo); | |
176 | ||
177 | to arrange to free the object thus allocated. */ | |
178 | ||
179 | void | |
180 | free_current_contents (location) | |
181 | char **location; | |
182 | { | |
183 | free (*location); | |
184 | } | |
088c3a0b JG |
185 | |
186 | /* Provide a known function that does nothing, to use as a base for | |
187 | for a possibly long chain of cleanups. This is useful where we | |
188 | use the cleanup chain for handling normal cleanups as well as dealing | |
189 | with cleanups that need to be done as a result of a call to error(). | |
190 | In such cases, we may not be certain where the first cleanup is, unless | |
191 | we have a do-nothing one to always use as the base. */ | |
192 | ||
193 | /* ARGSUSED */ | |
194 | void | |
195 | null_cleanup (arg) | |
196 | char **arg; | |
197 | { | |
198 | } | |
199 | ||
bd5635a1 | 200 | \f |
2bc2e684 FF |
201 | /* Provide a hook for modules wishing to print their own warning messages |
202 | to set up the terminal state in a compatible way, without them having | |
203 | to import all the target_<...> macros. */ | |
204 | ||
205 | void | |
206 | warning_setup () | |
207 | { | |
208 | target_terminal_ours (); | |
209 | wrap_here(""); /* Force out any buffered output */ | |
210 | fflush (stdout); | |
211 | } | |
212 | ||
213 | /* Print a warning message. | |
214 | The first argument STRING is the warning message, used as a fprintf string, | |
215 | and the remaining args are passed as arguments to it. | |
216 | The primary difference between warnings and errors is that a warning | |
217 | does not force the return to command level. */ | |
218 | ||
219 | /* VARARGS */ | |
220 | void | |
221 | warning (va_alist) | |
222 | va_dcl | |
223 | { | |
224 | va_list args; | |
225 | char *string; | |
226 | ||
227 | va_start (args); | |
228 | target_terminal_ours (); | |
229 | wrap_here(""); /* Force out any buffered output */ | |
230 | fflush (stdout); | |
231 | if (warning_pre_print) | |
232 | fprintf (stderr, warning_pre_print); | |
233 | string = va_arg (args, char *); | |
234 | vfprintf (stderr, string, args); | |
235 | fprintf (stderr, "\n"); | |
236 | va_end (args); | |
237 | } | |
238 | ||
bd5635a1 RP |
239 | /* Print an error message and return to command level. |
240 | The first argument STRING is the error message, used as a fprintf string, | |
241 | and the remaining args are passed as arguments to it. */ | |
242 | ||
243 | /* VARARGS */ | |
7919c3ed | 244 | NORETURN void |
bd5635a1 RP |
245 | error (va_alist) |
246 | va_dcl | |
247 | { | |
248 | va_list args; | |
249 | char *string; | |
250 | ||
251 | va_start (args); | |
252 | target_terminal_ours (); | |
2bc2e684 | 253 | wrap_here(""); /* Force out any buffered output */ |
bd5635a1 | 254 | fflush (stdout); |
81066208 | 255 | if (error_pre_print) |
bcf2e6ab | 256 | fprintf_filtered (stderr, error_pre_print); |
bd5635a1 | 257 | string = va_arg (args, char *); |
bcf2e6ab SG |
258 | vfprintf_filtered (stderr, string, args); |
259 | fprintf_filtered (stderr, "\n"); | |
bd5635a1 RP |
260 | va_end (args); |
261 | return_to_top_level (); | |
262 | } | |
263 | ||
264 | /* Print an error message and exit reporting failure. | |
265 | This is for a error that we cannot continue from. | |
7919c3ed JG |
266 | The arguments are printed a la printf. |
267 | ||
268 | This function cannot be declared volatile (NORETURN) in an | |
269 | ANSI environment because exit() is not declared volatile. */ | |
bd5635a1 RP |
270 | |
271 | /* VARARGS */ | |
7919c3ed | 272 | NORETURN void |
bd5635a1 RP |
273 | fatal (va_alist) |
274 | va_dcl | |
275 | { | |
276 | va_list args; | |
277 | char *string; | |
278 | ||
279 | va_start (args); | |
280 | string = va_arg (args, char *); | |
3624c875 | 281 | fprintf (stderr, "\ngdb: "); |
bd5635a1 RP |
282 | vfprintf (stderr, string, args); |
283 | fprintf (stderr, "\n"); | |
284 | va_end (args); | |
285 | exit (1); | |
286 | } | |
287 | ||
288 | /* Print an error message and exit, dumping core. | |
289 | The arguments are printed a la printf (). */ | |
7919c3ed | 290 | |
bd5635a1 | 291 | /* VARARGS */ |
7919c3ed | 292 | static void |
bd5635a1 RP |
293 | fatal_dump_core (va_alist) |
294 | va_dcl | |
295 | { | |
296 | va_list args; | |
297 | char *string; | |
298 | ||
299 | va_start (args); | |
300 | string = va_arg (args, char *); | |
301 | /* "internal error" is always correct, since GDB should never dump | |
302 | core, no matter what the input. */ | |
3624c875 | 303 | fprintf (stderr, "\ngdb internal error: "); |
bd5635a1 RP |
304 | vfprintf (stderr, string, args); |
305 | fprintf (stderr, "\n"); | |
306 | va_end (args); | |
307 | ||
308 | signal (SIGQUIT, SIG_DFL); | |
309 | kill (getpid (), SIGQUIT); | |
310 | /* We should never get here, but just in case... */ | |
311 | exit (1); | |
312 | } | |
7919c3ed | 313 | |
4ace50a5 FF |
314 | /* The strerror() function can return NULL for errno values that are |
315 | out of range. Provide a "safe" version that always returns a | |
316 | printable string. */ | |
317 | ||
318 | char * | |
319 | safe_strerror (errnum) | |
320 | int errnum; | |
321 | { | |
322 | char *msg; | |
323 | static char buf[32]; | |
324 | ||
325 | if ((msg = strerror (errnum)) == NULL) | |
326 | { | |
327 | sprintf (buf, "(undocumented errno %d)", errnum); | |
328 | msg = buf; | |
329 | } | |
330 | return (msg); | |
331 | } | |
332 | ||
333 | /* The strsignal() function can return NULL for signal values that are | |
334 | out of range. Provide a "safe" version that always returns a | |
335 | printable string. */ | |
336 | ||
337 | char * | |
338 | safe_strsignal (signo) | |
339 | int signo; | |
340 | { | |
341 | char *msg; | |
342 | static char buf[32]; | |
343 | ||
344 | if ((msg = strsignal (signo)) == NULL) | |
345 | { | |
346 | sprintf (buf, "(undocumented signal %d)", signo); | |
347 | msg = buf; | |
348 | } | |
349 | return (msg); | |
350 | } | |
351 | ||
352 | ||
bd5635a1 RP |
353 | /* Print the system error message for errno, and also mention STRING |
354 | as the file name for which the error was encountered. | |
355 | Then return to command level. */ | |
356 | ||
357 | void | |
358 | perror_with_name (string) | |
359 | char *string; | |
360 | { | |
bd5635a1 RP |
361 | char *err; |
362 | char *combined; | |
363 | ||
4ace50a5 | 364 | err = safe_strerror (errno); |
bd5635a1 RP |
365 | combined = (char *) alloca (strlen (err) + strlen (string) + 3); |
366 | strcpy (combined, string); | |
367 | strcat (combined, ": "); | |
368 | strcat (combined, err); | |
369 | ||
370 | /* I understand setting these is a matter of taste. Still, some people | |
371 | may clear errno but not know about bfd_error. Doing this here is not | |
372 | unreasonable. */ | |
373 | bfd_error = no_error; | |
374 | errno = 0; | |
375 | ||
376 | error ("%s.", combined); | |
377 | } | |
378 | ||
379 | /* Print the system error message for ERRCODE, and also mention STRING | |
380 | as the file name for which the error was encountered. */ | |
381 | ||
382 | void | |
383 | print_sys_errmsg (string, errcode) | |
384 | char *string; | |
385 | int errcode; | |
386 | { | |
bd5635a1 RP |
387 | char *err; |
388 | char *combined; | |
389 | ||
4ace50a5 | 390 | err = safe_strerror (errcode); |
bd5635a1 RP |
391 | combined = (char *) alloca (strlen (err) + strlen (string) + 3); |
392 | strcpy (combined, string); | |
393 | strcat (combined, ": "); | |
394 | strcat (combined, err); | |
395 | ||
bcf2e6ab | 396 | fprintf (stderr, "%s.\n", combined); |
bd5635a1 RP |
397 | } |
398 | ||
399 | /* Control C eventually causes this to be called, at a convenient time. */ | |
400 | ||
401 | void | |
402 | quit () | |
403 | { | |
404 | target_terminal_ours (); | |
d11c44f1 | 405 | wrap_here ((char *)0); /* Force out any pending output */ |
51b57ded | 406 | #if !defined(__GO32__) |
bd5635a1 RP |
407 | #ifdef HAVE_TERMIO |
408 | ioctl (fileno (stdout), TCFLSH, 1); | |
409 | #else /* not HAVE_TERMIO */ | |
410 | ioctl (fileno (stdout), TIOCFLUSH, 0); | |
411 | #endif /* not HAVE_TERMIO */ | |
412 | #ifdef TIOCGPGRP | |
413 | error ("Quit"); | |
414 | #else | |
415 | error ("Quit (expect signal %d when inferior is resumed)", SIGINT); | |
416 | #endif /* TIOCGPGRP */ | |
51b57ded | 417 | #endif |
bd5635a1 RP |
418 | } |
419 | ||
420 | /* Control C comes here */ | |
421 | ||
422 | void | |
088c3a0b JG |
423 | request_quit (signo) |
424 | int signo; | |
bd5635a1 RP |
425 | { |
426 | quit_flag = 1; | |
427 | ||
428 | #ifdef USG | |
429 | /* Restore the signal handler. */ | |
088c3a0b | 430 | signal (signo, request_quit); |
bd5635a1 RP |
431 | #endif |
432 | ||
433 | if (immediate_quit) | |
434 | quit (); | |
435 | } | |
3624c875 FF |
436 | |
437 | \f | |
438 | /* Memory management stuff (malloc friends). */ | |
439 | ||
440 | #if defined (NO_MMALLOC) | |
441 | ||
442 | PTR | |
443 | mmalloc (md, size) | |
444 | PTR md; | |
445 | long size; | |
446 | { | |
447 | return (malloc (size)); | |
448 | } | |
449 | ||
450 | PTR | |
451 | mrealloc (md, ptr, size) | |
452 | PTR md; | |
453 | PTR ptr; | |
454 | long size; | |
455 | { | |
4ace50a5 FF |
456 | if (ptr == 0) /* Guard against old realloc's */ |
457 | return malloc (size); | |
458 | else | |
459 | return realloc (ptr, size); | |
3624c875 FF |
460 | } |
461 | ||
462 | void | |
463 | mfree (md, ptr) | |
464 | PTR md; | |
465 | PTR ptr; | |
466 | { | |
467 | free (ptr); | |
468 | } | |
469 | ||
470 | #endif /* NO_MMALLOC */ | |
471 | ||
472 | #if defined (NO_MMALLOC) || defined (NO_MMALLOC_CHECK) | |
473 | ||
474 | void | |
475 | init_malloc (md) | |
476 | PTR md; | |
477 | { | |
478 | } | |
479 | ||
480 | #else /* have mmalloc and want corruption checking */ | |
481 | ||
482 | static void | |
483 | malloc_botch () | |
484 | { | |
485 | fatal_dump_core ("Memory corruption"); | |
486 | } | |
487 | ||
488 | /* Attempt to install hooks in mmalloc/mrealloc/mfree for the heap specified | |
489 | by MD, to detect memory corruption. Note that MD may be NULL to specify | |
490 | the default heap that grows via sbrk. | |
491 | ||
492 | Note that for freshly created regions, we must call mmcheck prior to any | |
493 | mallocs in the region. Otherwise, any region which was allocated prior to | |
494 | installing the checking hooks, which is later reallocated or freed, will | |
495 | fail the checks! The mmcheck function only allows initial hooks to be | |
496 | installed before the first mmalloc. However, anytime after we have called | |
497 | mmcheck the first time to install the checking hooks, we can call it again | |
498 | to update the function pointer to the memory corruption handler. | |
499 | ||
500 | Returns zero on failure, non-zero on success. */ | |
501 | ||
502 | void | |
503 | init_malloc (md) | |
504 | PTR md; | |
505 | { | |
506 | if (!mmcheck (md, malloc_botch)) | |
507 | { | |
508 | warning ("internal error: failed to install memory consistency checks"); | |
509 | } | |
510 | ||
4ed3a9ea | 511 | mmtrace (); |
3624c875 FF |
512 | } |
513 | ||
514 | #endif /* Have mmalloc and want corruption checking */ | |
515 | ||
516 | /* Called when a memory allocation fails, with the number of bytes of | |
517 | memory requested in SIZE. */ | |
518 | ||
519 | NORETURN void | |
520 | nomem (size) | |
521 | long size; | |
522 | { | |
523 | if (size > 0) | |
524 | { | |
525 | fatal ("virtual memory exhausted: can't allocate %ld bytes.", size); | |
526 | } | |
527 | else | |
528 | { | |
529 | fatal ("virtual memory exhausted."); | |
530 | } | |
531 | } | |
532 | ||
533 | /* Like mmalloc but get error if no storage available, and protect against | |
534 | the caller wanting to allocate zero bytes. Whether to return NULL for | |
535 | a zero byte request, or translate the request into a request for one | |
536 | byte of zero'd storage, is a religious issue. */ | |
537 | ||
538 | PTR | |
539 | xmmalloc (md, size) | |
540 | PTR md; | |
541 | long size; | |
542 | { | |
543 | register PTR val; | |
544 | ||
545 | if (size == 0) | |
546 | { | |
547 | val = NULL; | |
548 | } | |
549 | else if ((val = mmalloc (md, size)) == NULL) | |
550 | { | |
551 | nomem (size); | |
552 | } | |
553 | return (val); | |
554 | } | |
555 | ||
556 | /* Like mrealloc but get error if no storage available. */ | |
557 | ||
558 | PTR | |
559 | xmrealloc (md, ptr, size) | |
560 | PTR md; | |
561 | PTR ptr; | |
562 | long size; | |
563 | { | |
564 | register PTR val; | |
565 | ||
566 | if (ptr != NULL) | |
567 | { | |
568 | val = mrealloc (md, ptr, size); | |
569 | } | |
570 | else | |
571 | { | |
572 | val = mmalloc (md, size); | |
573 | } | |
574 | if (val == NULL) | |
575 | { | |
576 | nomem (size); | |
577 | } | |
578 | return (val); | |
579 | } | |
580 | ||
581 | /* Like malloc but get error if no storage available, and protect against | |
582 | the caller wanting to allocate zero bytes. */ | |
583 | ||
584 | PTR | |
585 | xmalloc (size) | |
586 | long size; | |
587 | { | |
588 | return (xmmalloc ((void *) NULL, size)); | |
589 | } | |
590 | ||
591 | /* Like mrealloc but get error if no storage available. */ | |
592 | ||
593 | PTR | |
594 | xrealloc (ptr, size) | |
595 | PTR ptr; | |
596 | long size; | |
597 | { | |
598 | return (xmrealloc ((void *) NULL, ptr, size)); | |
599 | } | |
600 | ||
bd5635a1 RP |
601 | \f |
602 | /* My replacement for the read system call. | |
603 | Used like `read' but keeps going if `read' returns too soon. */ | |
604 | ||
605 | int | |
606 | myread (desc, addr, len) | |
607 | int desc; | |
608 | char *addr; | |
609 | int len; | |
610 | { | |
611 | register int val; | |
612 | int orglen = len; | |
613 | ||
614 | while (len > 0) | |
615 | { | |
616 | val = read (desc, addr, len); | |
617 | if (val < 0) | |
618 | return val; | |
619 | if (val == 0) | |
620 | return orglen - len; | |
621 | len -= val; | |
622 | addr += val; | |
623 | } | |
624 | return orglen; | |
625 | } | |
626 | \f | |
627 | /* Make a copy of the string at PTR with SIZE characters | |
628 | (and add a null character at the end in the copy). | |
629 | Uses malloc to get the space. Returns the address of the copy. */ | |
630 | ||
631 | char * | |
632 | savestring (ptr, size) | |
088c3a0b | 633 | const char *ptr; |
bd5635a1 RP |
634 | int size; |
635 | { | |
636 | register char *p = (char *) xmalloc (size + 1); | |
4ed3a9ea | 637 | memcpy (p, ptr, size); |
bd5635a1 RP |
638 | p[size] = 0; |
639 | return p; | |
640 | } | |
641 | ||
3624c875 FF |
642 | char * |
643 | msavestring (md, ptr, size) | |
644 | void *md; | |
645 | const char *ptr; | |
646 | int size; | |
647 | { | |
648 | register char *p = (char *) xmmalloc (md, size + 1); | |
4ed3a9ea | 649 | memcpy (p, ptr, size); |
3624c875 FF |
650 | p[size] = 0; |
651 | return p; | |
652 | } | |
653 | ||
8aa13b87 JK |
654 | /* The "const" is so it compiles under DGUX (which prototypes strsave |
655 | in <string.h>. FIXME: This should be named "xstrsave", shouldn't it? | |
656 | Doesn't real strsave return NULL if out of memory? */ | |
bd5635a1 RP |
657 | char * |
658 | strsave (ptr) | |
8aa13b87 | 659 | const char *ptr; |
bd5635a1 RP |
660 | { |
661 | return savestring (ptr, strlen (ptr)); | |
662 | } | |
663 | ||
3624c875 FF |
664 | char * |
665 | mstrsave (md, ptr) | |
666 | void *md; | |
667 | const char *ptr; | |
668 | { | |
669 | return (msavestring (md, ptr, strlen (ptr))); | |
670 | } | |
671 | ||
bd5635a1 RP |
672 | void |
673 | print_spaces (n, file) | |
674 | register int n; | |
675 | register FILE *file; | |
676 | { | |
677 | while (n-- > 0) | |
678 | fputc (' ', file); | |
679 | } | |
680 | ||
681 | /* Ask user a y-or-n question and return 1 iff answer is yes. | |
682 | Takes three args which are given to printf to print the question. | |
683 | The first, a control string, should end in "? ". | |
684 | It should not say how to answer, because we do that. */ | |
685 | ||
686 | /* VARARGS */ | |
687 | int | |
688 | query (va_alist) | |
689 | va_dcl | |
690 | { | |
691 | va_list args; | |
692 | char *ctlstr; | |
693 | register int answer; | |
694 | register int ans2; | |
695 | ||
696 | /* Automatically answer "yes" if input is not from a terminal. */ | |
697 | if (!input_from_terminal_p ()) | |
698 | return 1; | |
699 | ||
700 | while (1) | |
701 | { | |
546014f7 PB |
702 | wrap_here (""); /* Flush any buffered output */ |
703 | fflush (stdout); | |
bd5635a1 RP |
704 | va_start (args); |
705 | ctlstr = va_arg (args, char *); | |
3e3b0eb9 | 706 | energize_query (ctlstr, args); |
bcf2e6ab | 707 | vfprintf_filtered (stdout, ctlstr, args); |
bcf2e6ab | 708 | printf_filtered ("(y or n) "); |
3e3b0eb9 SG |
709 | if (energize) |
710 | { | |
711 | char *buf; | |
712 | ||
713 | buf = energize_command_line_input(0, 0); | |
714 | answer = buf ? *buf : 'Y'; | |
715 | energize_acknowledge_query(buf); | |
716 | } | |
717 | else | |
718 | { | |
719 | fflush (stdout); | |
720 | answer = fgetc (stdin); | |
721 | clearerr (stdin); /* in case of C-d */ | |
722 | if (answer == EOF) /* C-d */ | |
723 | return 1; | |
724 | if (answer != '\n') /* Eat rest of input line, to EOF or newline */ | |
725 | do | |
726 | { | |
727 | ans2 = fgetc (stdin); | |
728 | clearerr (stdin); | |
729 | } | |
730 | while (ans2 != EOF && ans2 != '\n'); | |
731 | } | |
bd5635a1 RP |
732 | if (answer >= 'a') |
733 | answer -= 040; | |
734 | if (answer == 'Y') | |
735 | return 1; | |
736 | if (answer == 'N') | |
737 | return 0; | |
bcf2e6ab | 738 | printf_filtered ("Please answer y or n.\n"); |
3e3b0eb9 | 739 | va_end (args); |
bd5635a1 RP |
740 | } |
741 | } | |
7919c3ed | 742 | |
bd5635a1 RP |
743 | \f |
744 | /* Parse a C escape sequence. STRING_PTR points to a variable | |
745 | containing a pointer to the string to parse. That pointer | |
746 | should point to the character after the \. That pointer | |
747 | is updated past the characters we use. The value of the | |
748 | escape sequence is returned. | |
749 | ||
750 | A negative value means the sequence \ newline was seen, | |
751 | which is supposed to be equivalent to nothing at all. | |
752 | ||
753 | If \ is followed by a null character, we return a negative | |
754 | value and leave the string pointer pointing at the null character. | |
755 | ||
756 | If \ is followed by 000, we return 0 and leave the string pointer | |
757 | after the zeros. A value of 0 does not mean end of string. */ | |
758 | ||
759 | int | |
760 | parse_escape (string_ptr) | |
761 | char **string_ptr; | |
762 | { | |
763 | register int c = *(*string_ptr)++; | |
764 | switch (c) | |
765 | { | |
766 | case 'a': | |
2bc2e684 | 767 | return 007; /* Bell (alert) char */ |
bd5635a1 RP |
768 | case 'b': |
769 | return '\b'; | |
2bc2e684 | 770 | case 'e': /* Escape character */ |
bd5635a1 RP |
771 | return 033; |
772 | case 'f': | |
773 | return '\f'; | |
774 | case 'n': | |
775 | return '\n'; | |
776 | case 'r': | |
777 | return '\r'; | |
778 | case 't': | |
779 | return '\t'; | |
780 | case 'v': | |
781 | return '\v'; | |
782 | case '\n': | |
783 | return -2; | |
784 | case 0: | |
785 | (*string_ptr)--; | |
786 | return 0; | |
787 | case '^': | |
788 | c = *(*string_ptr)++; | |
789 | if (c == '\\') | |
790 | c = parse_escape (string_ptr); | |
791 | if (c == '?') | |
792 | return 0177; | |
793 | return (c & 0200) | (c & 037); | |
794 | ||
795 | case '0': | |
796 | case '1': | |
797 | case '2': | |
798 | case '3': | |
799 | case '4': | |
800 | case '5': | |
801 | case '6': | |
802 | case '7': | |
803 | { | |
804 | register int i = c - '0'; | |
805 | register int count = 0; | |
806 | while (++count < 3) | |
807 | { | |
808 | if ((c = *(*string_ptr)++) >= '0' && c <= '7') | |
809 | { | |
810 | i *= 8; | |
811 | i += c - '0'; | |
812 | } | |
813 | else | |
814 | { | |
815 | (*string_ptr)--; | |
816 | break; | |
817 | } | |
818 | } | |
819 | return i; | |
820 | } | |
821 | default: | |
822 | return c; | |
823 | } | |
824 | } | |
825 | \f | |
088c3a0b | 826 | /* Print the character C on STREAM as part of the contents |
bd5635a1 RP |
827 | of a literal string whose delimiter is QUOTER. */ |
828 | ||
829 | void | |
088c3a0b JG |
830 | printchar (c, stream, quoter) |
831 | register int c; | |
bd5635a1 RP |
832 | FILE *stream; |
833 | int quoter; | |
834 | { | |
bd5635a1 | 835 | |
7e7e2d40 JG |
836 | c &= 0xFF; /* Avoid sign bit follies */ |
837 | ||
fcdb113e JG |
838 | if ( c < 0x20 || /* Low control chars */ |
839 | (c >= 0x7F && c < 0xA0) || /* DEL, High controls */ | |
840 | (sevenbit_strings && c >= 0x80)) { /* high order bit set */ | |
bd5635a1 RP |
841 | switch (c) |
842 | { | |
843 | case '\n': | |
844 | fputs_filtered ("\\n", stream); | |
845 | break; | |
846 | case '\b': | |
847 | fputs_filtered ("\\b", stream); | |
848 | break; | |
849 | case '\t': | |
850 | fputs_filtered ("\\t", stream); | |
851 | break; | |
852 | case '\f': | |
853 | fputs_filtered ("\\f", stream); | |
854 | break; | |
855 | case '\r': | |
856 | fputs_filtered ("\\r", stream); | |
857 | break; | |
858 | case '\033': | |
859 | fputs_filtered ("\\e", stream); | |
860 | break; | |
861 | case '\007': | |
862 | fputs_filtered ("\\a", stream); | |
863 | break; | |
864 | default: | |
865 | fprintf_filtered (stream, "\\%.3o", (unsigned int) c); | |
866 | break; | |
867 | } | |
2bc2e684 FF |
868 | } else { |
869 | if (c == '\\' || c == quoter) | |
870 | fputs_filtered ("\\", stream); | |
871 | fprintf_filtered (stream, "%c", c); | |
872 | } | |
bd5635a1 RP |
873 | } |
874 | \f | |
875 | /* Number of lines per page or UINT_MAX if paging is disabled. */ | |
876 | static unsigned int lines_per_page; | |
877 | /* Number of chars per line or UNIT_MAX is line folding is disabled. */ | |
878 | static unsigned int chars_per_line; | |
879 | /* Current count of lines printed on this page, chars on this line. */ | |
880 | static unsigned int lines_printed, chars_printed; | |
881 | ||
882 | /* Buffer and start column of buffered text, for doing smarter word- | |
883 | wrapping. When someone calls wrap_here(), we start buffering output | |
884 | that comes through fputs_filtered(). If we see a newline, we just | |
885 | spit it out and forget about the wrap_here(). If we see another | |
886 | wrap_here(), we spit it out and remember the newer one. If we see | |
887 | the end of the line, we spit out a newline, the indent, and then | |
888 | the buffered output. | |
889 | ||
890 | wrap_column is the column number on the screen where wrap_buffer begins. | |
891 | When wrap_column is zero, wrapping is not in effect. | |
892 | wrap_buffer is malloc'd with chars_per_line+2 bytes. | |
893 | When wrap_buffer[0] is null, the buffer is empty. | |
894 | wrap_pointer points into it at the next character to fill. | |
895 | wrap_indent is the string that should be used as indentation if the | |
896 | wrap occurs. */ | |
897 | ||
898 | static char *wrap_buffer, *wrap_pointer, *wrap_indent; | |
899 | static int wrap_column; | |
900 | ||
e1ce8aa5 | 901 | /* ARGSUSED */ |
bd5635a1 RP |
902 | static void |
903 | set_width_command (args, from_tty, c) | |
904 | char *args; | |
905 | int from_tty; | |
906 | struct cmd_list_element *c; | |
907 | { | |
908 | if (!wrap_buffer) | |
909 | { | |
910 | wrap_buffer = (char *) xmalloc (chars_per_line + 2); | |
911 | wrap_buffer[0] = '\0'; | |
912 | } | |
913 | else | |
914 | wrap_buffer = (char *) xrealloc (wrap_buffer, chars_per_line + 2); | |
915 | wrap_pointer = wrap_buffer; /* Start it at the beginning */ | |
916 | } | |
917 | ||
918 | static void | |
919 | prompt_for_continue () | |
920 | { | |
351b221d JG |
921 | char *ignore; |
922 | ||
bd5635a1 | 923 | immediate_quit++; |
351b221d JG |
924 | ignore = gdb_readline ("---Type <return> to continue---"); |
925 | if (ignore) | |
926 | free (ignore); | |
bd5635a1 RP |
927 | chars_printed = lines_printed = 0; |
928 | immediate_quit--; | |
351b221d | 929 | dont_repeat (); /* Forget prev cmd -- CR won't repeat it. */ |
bd5635a1 RP |
930 | } |
931 | ||
932 | /* Reinitialize filter; ie. tell it to reset to original values. */ | |
933 | ||
934 | void | |
935 | reinitialize_more_filter () | |
936 | { | |
937 | lines_printed = 0; | |
938 | chars_printed = 0; | |
939 | } | |
940 | ||
941 | /* Indicate that if the next sequence of characters overflows the line, | |
942 | a newline should be inserted here rather than when it hits the end. | |
943 | If INDENT is nonzero, it is a string to be printed to indent the | |
944 | wrapped part on the next line. INDENT must remain accessible until | |
945 | the next call to wrap_here() or until a newline is printed through | |
946 | fputs_filtered(). | |
947 | ||
948 | If the line is already overfull, we immediately print a newline and | |
949 | the indentation, and disable further wrapping. | |
950 | ||
2bc2e684 FF |
951 | If we don't know the width of lines, but we know the page height, |
952 | we must not wrap words, but should still keep track of newlines | |
953 | that were explicitly printed. | |
954 | ||
bd5635a1 RP |
955 | INDENT should not contain tabs, as that |
956 | will mess up the char count on the next line. FIXME. */ | |
957 | ||
958 | void | |
959 | wrap_here(indent) | |
960 | char *indent; | |
961 | { | |
962 | if (wrap_buffer[0]) | |
963 | { | |
964 | *wrap_pointer = '\0'; | |
965 | fputs (wrap_buffer, stdout); | |
966 | } | |
967 | wrap_pointer = wrap_buffer; | |
968 | wrap_buffer[0] = '\0'; | |
2bc2e684 FF |
969 | if (chars_per_line == UINT_MAX) /* No line overflow checking */ |
970 | { | |
971 | wrap_column = 0; | |
972 | } | |
973 | else if (chars_printed >= chars_per_line) | |
bd5635a1 RP |
974 | { |
975 | puts_filtered ("\n"); | |
976 | puts_filtered (indent); | |
977 | wrap_column = 0; | |
978 | } | |
979 | else | |
980 | { | |
981 | wrap_column = chars_printed; | |
982 | wrap_indent = indent; | |
983 | } | |
984 | } | |
985 | ||
986 | /* Like fputs but pause after every screenful, and can wrap at points | |
987 | other than the final character of a line. | |
988 | Unlike fputs, fputs_filtered does not return a value. | |
989 | It is OK for LINEBUFFER to be NULL, in which case just don't print | |
990 | anything. | |
991 | ||
992 | Note that a longjmp to top level may occur in this routine | |
993 | (since prompt_for_continue may do so) so this routine should not be | |
994 | called when cleanups are not in place. */ | |
995 | ||
996 | void | |
997 | fputs_filtered (linebuffer, stream) | |
088c3a0b | 998 | const char *linebuffer; |
bd5635a1 RP |
999 | FILE *stream; |
1000 | { | |
7919c3ed | 1001 | const char *lineptr; |
bd5635a1 RP |
1002 | |
1003 | if (linebuffer == 0) | |
1004 | return; | |
1005 | ||
3e3b0eb9 SG |
1006 | if (energize) |
1007 | { | |
1008 | energize_fputs(linebuffer); | |
1009 | return; | |
1010 | } | |
1011 | ||
bd5635a1 RP |
1012 | /* Don't do any filtering if it is disabled. */ |
1013 | if (stream != stdout | |
1014 | || (lines_per_page == UINT_MAX && chars_per_line == UINT_MAX)) | |
1015 | { | |
1016 | fputs (linebuffer, stream); | |
1017 | return; | |
1018 | } | |
1019 | ||
1020 | /* Go through and output each character. Show line extension | |
1021 | when this is necessary; prompt user for new page when this is | |
1022 | necessary. */ | |
1023 | ||
1024 | lineptr = linebuffer; | |
1025 | while (*lineptr) | |
1026 | { | |
1027 | /* Possible new page. */ | |
1028 | if (lines_printed >= lines_per_page - 1) | |
1029 | prompt_for_continue (); | |
1030 | ||
1031 | while (*lineptr && *lineptr != '\n') | |
1032 | { | |
1033 | /* Print a single line. */ | |
1034 | if (*lineptr == '\t') | |
1035 | { | |
1036 | if (wrap_column) | |
1037 | *wrap_pointer++ = '\t'; | |
1038 | else | |
1039 | putc ('\t', stream); | |
1040 | /* Shifting right by 3 produces the number of tab stops | |
1041 | we have already passed, and then adding one and | |
1042 | shifting left 3 advances to the next tab stop. */ | |
1043 | chars_printed = ((chars_printed >> 3) + 1) << 3; | |
1044 | lineptr++; | |
1045 | } | |
1046 | else | |
1047 | { | |
1048 | if (wrap_column) | |
1049 | *wrap_pointer++ = *lineptr; | |
1050 | else | |
1051 | putc (*lineptr, stream); | |
1052 | chars_printed++; | |
1053 | lineptr++; | |
1054 | } | |
1055 | ||
1056 | if (chars_printed >= chars_per_line) | |
1057 | { | |
1058 | unsigned int save_chars = chars_printed; | |
1059 | ||
1060 | chars_printed = 0; | |
1061 | lines_printed++; | |
1062 | /* If we aren't actually wrapping, don't output newline -- | |
1063 | if chars_per_line is right, we probably just overflowed | |
1064 | anyway; if it's wrong, let us keep going. */ | |
1065 | if (wrap_column) | |
1066 | putc ('\n', stream); | |
1067 | ||
1068 | /* Possible new page. */ | |
1069 | if (lines_printed >= lines_per_page - 1) | |
1070 | prompt_for_continue (); | |
1071 | ||
1072 | /* Now output indentation and wrapped string */ | |
1073 | if (wrap_column) | |
1074 | { | |
1075 | if (wrap_indent) | |
1076 | fputs (wrap_indent, stream); | |
1077 | *wrap_pointer = '\0'; /* Null-terminate saved stuff */ | |
1078 | fputs (wrap_buffer, stream); /* and eject it */ | |
1079 | /* FIXME, this strlen is what prevents wrap_indent from | |
1080 | containing tabs. However, if we recurse to print it | |
1081 | and count its chars, we risk trouble if wrap_indent is | |
1082 | longer than (the user settable) chars_per_line. | |
1083 | Note also that this can set chars_printed > chars_per_line | |
1084 | if we are printing a long string. */ | |
1085 | chars_printed = strlen (wrap_indent) | |
1086 | + (save_chars - wrap_column); | |
1087 | wrap_pointer = wrap_buffer; /* Reset buffer */ | |
1088 | wrap_buffer[0] = '\0'; | |
1089 | wrap_column = 0; /* And disable fancy wrap */ | |
1090 | } | |
1091 | } | |
1092 | } | |
1093 | ||
1094 | if (*lineptr == '\n') | |
1095 | { | |
1096 | chars_printed = 0; | |
d11c44f1 | 1097 | wrap_here ((char *)0); /* Spit out chars, cancel further wraps */ |
bd5635a1 RP |
1098 | lines_printed++; |
1099 | putc ('\n', stream); | |
1100 | lineptr++; | |
1101 | } | |
1102 | } | |
1103 | } | |
1104 | ||
1105 | ||
1106 | /* fputs_demangled is a variant of fputs_filtered that | |
1107 | demangles g++ names.*/ | |
1108 | ||
1109 | void | |
1110 | fputs_demangled (linebuffer, stream, arg_mode) | |
1111 | char *linebuffer; | |
1112 | FILE *stream; | |
1113 | int arg_mode; | |
1114 | { | |
bd5635a1 RP |
1115 | #define SYMBOL_MAX 1024 |
1116 | ||
f88e7af8 JK |
1117 | #define SYMBOL_CHAR(c) (isascii(c) \ |
1118 | && (isalnum(c) || (c) == '_' || (c) == CPLUS_MARKER)) | |
bd5635a1 RP |
1119 | |
1120 | char buf[SYMBOL_MAX+1]; | |
0f552c5f | 1121 | # define DMSLOP 5 /* How much room to leave in buf */ |
bd5635a1 RP |
1122 | char *p; |
1123 | ||
1124 | if (linebuffer == NULL) | |
1125 | return; | |
1126 | ||
1127 | /* If user wants to see raw output, no problem. */ | |
1128 | if (!demangle) { | |
1129 | fputs_filtered (linebuffer, stream); | |
bdbd5f50 | 1130 | return; |
bd5635a1 RP |
1131 | } |
1132 | ||
1133 | p = linebuffer; | |
1134 | ||
1135 | while ( *p != (char) 0 ) { | |
1136 | int i = 0; | |
1137 | ||
1138 | /* collect non-interesting characters into buf */ | |
0f552c5f | 1139 | while (*p != (char) 0 && !SYMBOL_CHAR(*p) && i < (int)sizeof(buf)-DMSLOP ) { |
bd5635a1 RP |
1140 | buf[i++] = *p; |
1141 | p++; | |
1142 | } | |
1143 | if (i > 0) { | |
1144 | /* output the non-interesting characters without demangling */ | |
1145 | buf[i] = (char) 0; | |
1146 | fputs_filtered(buf, stream); | |
1147 | i = 0; /* reset buf */ | |
1148 | } | |
1149 | ||
1150 | /* and now the interesting characters */ | |
1151 | while (i < SYMBOL_MAX | |
1152 | && *p != (char) 0 | |
1153 | && SYMBOL_CHAR(*p) | |
0f552c5f | 1154 | && i < (int)sizeof(buf) - DMSLOP) { |
bd5635a1 RP |
1155 | buf[i++] = *p; |
1156 | p++; | |
1157 | } | |
1158 | buf[i] = (char) 0; | |
1159 | if (i > 0) { | |
1160 | char * result; | |
1161 | ||
1162 | if ( (result = cplus_demangle(buf, arg_mode)) != NULL ) { | |
1163 | fputs_filtered(result, stream); | |
1164 | free(result); | |
1165 | } | |
1166 | else { | |
1167 | fputs_filtered(buf, stream); | |
1168 | } | |
1169 | } | |
1170 | } | |
1171 | } | |
1172 | ||
1173 | /* Print a variable number of ARGS using format FORMAT. If this | |
1174 | information is going to put the amount written (since the last call | |
1175 | to INITIALIZE_MORE_FILTER or the last page break) over the page size, | |
1176 | print out a pause message and do a gdb_readline to get the users | |
1177 | permision to continue. | |
1178 | ||
1179 | Unlike fprintf, this function does not return a value. | |
1180 | ||
1181 | We implement three variants, vfprintf (takes a vararg list and stream), | |
1182 | fprintf (takes a stream to write on), and printf (the usual). | |
1183 | ||
1184 | Note that this routine has a restriction that the length of the | |
1185 | final output line must be less than 255 characters *or* it must be | |
1186 | less than twice the size of the format string. This is a very | |
1187 | arbitrary restriction, but it is an internal restriction, so I'll | |
1188 | put it in. This means that the %s format specifier is almost | |
1189 | useless; unless the caller can GUARANTEE that the string is short | |
1190 | enough, fputs_filtered should be used instead. | |
1191 | ||
1192 | Note also that a longjmp to top level may occur in this routine | |
1193 | (since prompt_for_continue may do so) so this routine should not be | |
1194 | called when cleanups are not in place. */ | |
1195 | ||
7919c3ed | 1196 | static void |
bd5635a1 | 1197 | vfprintf_filtered (stream, format, args) |
bd5635a1 RP |
1198 | FILE *stream; |
1199 | char *format; | |
7919c3ed | 1200 | va_list args; |
bd5635a1 RP |
1201 | { |
1202 | static char *linebuffer = (char *) 0; | |
1203 | static int line_size; | |
1204 | int format_length; | |
1205 | ||
1206 | format_length = strlen (format); | |
1207 | ||
1208 | /* Allocated linebuffer for the first time. */ | |
1209 | if (!linebuffer) | |
1210 | { | |
1211 | linebuffer = (char *) xmalloc (255); | |
1212 | line_size = 255; | |
1213 | } | |
1214 | ||
1215 | /* Reallocate buffer to a larger size if this is necessary. */ | |
1216 | if (format_length * 2 > line_size) | |
1217 | { | |
1218 | line_size = format_length * 2; | |
1219 | ||
1220 | /* You don't have to copy. */ | |
1221 | free (linebuffer); | |
1222 | linebuffer = (char *) xmalloc (line_size); | |
1223 | } | |
1224 | ||
1225 | ||
1226 | /* This won't blow up if the restrictions described above are | |
1227 | followed. */ | |
4ed3a9ea | 1228 | vsprintf (linebuffer, format, args); |
bd5635a1 RP |
1229 | |
1230 | fputs_filtered (linebuffer, stream); | |
1231 | } | |
1232 | ||
bd5635a1 RP |
1233 | /* VARARGS */ |
1234 | void | |
1235 | fprintf_filtered (va_alist) | |
1236 | va_dcl | |
1237 | { | |
546014f7 | 1238 | va_list args; |
bd5635a1 RP |
1239 | FILE *stream; |
1240 | char *format; | |
546014f7 PB |
1241 | |
1242 | va_start (args); | |
1243 | stream = va_arg (args, FILE *); | |
1244 | format = va_arg (args, char *); | |
1245 | ||
1246 | /* This won't blow up if the restrictions described above are | |
1247 | followed. */ | |
1248 | vfprintf_filtered (stream, format, args); | |
1249 | va_end (args); | |
1250 | } | |
1251 | ||
1252 | /* Like fprintf_filtered, but prints it's result indent. | |
1253 | Called as fprintfi_filtered (spaces, format, arg1, arg2, ...); */ | |
1254 | ||
1255 | /* VARARGS */ | |
1256 | void | |
1257 | fprintfi_filtered (va_alist) | |
1258 | va_dcl | |
1259 | { | |
7919c3ed | 1260 | va_list args; |
546014f7 PB |
1261 | int spaces; |
1262 | FILE *stream; | |
1263 | char *format; | |
bd5635a1 RP |
1264 | |
1265 | va_start (args); | |
546014f7 | 1266 | spaces = va_arg (args, int); |
bd5635a1 RP |
1267 | stream = va_arg (args, FILE *); |
1268 | format = va_arg (args, char *); | |
546014f7 | 1269 | print_spaces_filtered (spaces, stream); |
bd5635a1 RP |
1270 | |
1271 | /* This won't blow up if the restrictions described above are | |
1272 | followed. */ | |
7919c3ed | 1273 | vfprintf_filtered (stream, format, args); |
bd5635a1 RP |
1274 | va_end (args); |
1275 | } | |
1276 | ||
1277 | /* VARARGS */ | |
1278 | void | |
1279 | printf_filtered (va_alist) | |
1280 | va_dcl | |
1281 | { | |
1282 | va_list args; | |
1283 | char *format; | |
1284 | ||
1285 | va_start (args); | |
1286 | format = va_arg (args, char *); | |
1287 | ||
7919c3ed | 1288 | vfprintf_filtered (stdout, format, args); |
bd5635a1 RP |
1289 | va_end (args); |
1290 | } | |
bd5635a1 | 1291 | |
546014f7 PB |
1292 | /* Like printf_filtered, but prints it's result indented. |
1293 | Called as printfi_filtered (spaces, format, arg1, arg2, ...); */ | |
1294 | ||
1295 | /* VARARGS */ | |
1296 | void | |
1297 | printfi_filtered (va_alist) | |
1298 | va_dcl | |
1299 | { | |
1300 | va_list args; | |
1301 | int spaces; | |
1302 | char *format; | |
1303 | ||
1304 | va_start (args); | |
1305 | spaces = va_arg (args, int); | |
1306 | format = va_arg (args, char *); | |
1307 | print_spaces_filtered (spaces, stdout); | |
1308 | vfprintf_filtered (stdout, format, args); | |
1309 | va_end (args); | |
1310 | } | |
1311 | ||
bd5635a1 RP |
1312 | /* Easy */ |
1313 | ||
1314 | void | |
1315 | puts_filtered (string) | |
1316 | char *string; | |
1317 | { | |
1318 | fputs_filtered (string, stdout); | |
1319 | } | |
1320 | ||
1321 | /* Return a pointer to N spaces and a null. The pointer is good | |
1322 | until the next call to here. */ | |
1323 | char * | |
1324 | n_spaces (n) | |
1325 | int n; | |
1326 | { | |
1327 | register char *t; | |
1328 | static char *spaces; | |
1329 | static int max_spaces; | |
1330 | ||
1331 | if (n > max_spaces) | |
1332 | { | |
1333 | if (spaces) | |
1334 | free (spaces); | |
3624c875 | 1335 | spaces = (char *) xmalloc (n+1); |
bd5635a1 RP |
1336 | for (t = spaces+n; t != spaces;) |
1337 | *--t = ' '; | |
1338 | spaces[n] = '\0'; | |
1339 | max_spaces = n; | |
1340 | } | |
1341 | ||
1342 | return spaces + max_spaces - n; | |
1343 | } | |
1344 | ||
1345 | /* Print N spaces. */ | |
1346 | void | |
1347 | print_spaces_filtered (n, stream) | |
1348 | int n; | |
1349 | FILE *stream; | |
1350 | { | |
1351 | fputs_filtered (n_spaces (n), stream); | |
1352 | } | |
1353 | \f | |
1354 | /* C++ demangler stuff. */ | |
bd5635a1 | 1355 | |
51b57ded FF |
1356 | /* Make a copy of a symbol, applying C++ demangling if demangling is enabled |
1357 | and a demangled version exists. Note that the value returned from | |
1358 | cplus_demangle is already allocated in malloc'd memory. */ | |
1359 | ||
1360 | char * | |
1361 | strdup_demangled (name) | |
1362 | const char *name; | |
1363 | { | |
1364 | char *demangled = NULL; | |
1365 | ||
1366 | if (demangle) | |
1367 | { | |
1368 | demangled = cplus_demangle (name, DMGL_PARAMS | DMGL_ANSI); | |
1369 | } | |
1370 | return ((demangled != NULL) ? demangled : strdup (name)); | |
1371 | } | |
1372 | ||
1373 | ||
bd5635a1 RP |
1374 | /* Print NAME on STREAM, demangling if necessary. */ |
1375 | void | |
1376 | fprint_symbol (stream, name) | |
1377 | FILE *stream; | |
1378 | char *name; | |
1379 | { | |
1380 | char *demangled; | |
bcf2e6ab SG |
1381 | if ((!demangle) |
1382 | || NULL == (demangled = cplus_demangle (name, DMGL_PARAMS | DMGL_ANSI))) | |
bd5635a1 RP |
1383 | fputs_filtered (name, stream); |
1384 | else | |
1385 | { | |
1386 | fputs_filtered (demangled, stream); | |
1387 | free (demangled); | |
1388 | } | |
1389 | } | |
51b57ded FF |
1390 | |
1391 | /* Do a strcmp() type operation on STRING1 and STRING2, ignoring any | |
1392 | differences in whitespace. Returns 0 if they match, non-zero if they | |
546014f7 PB |
1393 | don't (slightly different than strcmp()'s range of return values). |
1394 | ||
1395 | As an extra hack, string1=="FOO(ARGS)" matches string2=="FOO". | |
1396 | This "feature" is useful for demangle_and_match(), which is used | |
1397 | when searching for matching C++ function names (such as if the | |
1398 | user types 'break FOO', where FOO is a mangled C++ function). */ | |
51b57ded | 1399 | |
546014f7 | 1400 | static int |
51b57ded FF |
1401 | strcmp_iw (string1, string2) |
1402 | const char *string1; | |
1403 | const char *string2; | |
1404 | { | |
1405 | while ((*string1 != '\0') && (*string2 != '\0')) | |
1406 | { | |
1407 | while (isspace (*string1)) | |
1408 | { | |
1409 | string1++; | |
1410 | } | |
1411 | while (isspace (*string2)) | |
1412 | { | |
1413 | string2++; | |
1414 | } | |
1415 | if (*string1 != *string2) | |
1416 | { | |
1417 | break; | |
1418 | } | |
1419 | if (*string1 != '\0') | |
1420 | { | |
1421 | string1++; | |
1422 | string2++; | |
1423 | } | |
1424 | } | |
546014f7 | 1425 | return (*string1 != '\0' && *string1 != '(') || (*string2 != '\0'); |
51b57ded FF |
1426 | } |
1427 | ||
1428 | /* Demangle NAME and compare the result with LOOKFOR, ignoring any differences | |
1429 | in whitespace. | |
1430 | ||
1431 | If a match is found, returns a pointer to the demangled version of NAME | |
1432 | in malloc'd memory, which needs to be freed by the caller after use. | |
1433 | If a match is not found, returns NULL. | |
1434 | ||
1435 | OPTIONS is a flags word that controls the demangling process and is just | |
1436 | passed on to the demangler. | |
1437 | ||
1438 | When the caller sees a non-NULL result, it knows that NAME is the mangled | |
1439 | equivalent of LOOKFOR, and it can use either NAME, the "official demangled" | |
1440 | version of NAME (the return value) or the "unofficial demangled" version | |
1441 | of NAME (LOOKFOR, which it already knows). */ | |
1442 | ||
1443 | char * | |
1444 | demangle_and_match (name, lookfor, options) | |
1445 | const char *name; | |
1446 | const char *lookfor; | |
1447 | int options; | |
1448 | { | |
1449 | char *demangled; | |
1450 | ||
1451 | if ((demangled = cplus_demangle (name, options)) != NULL) | |
1452 | { | |
1453 | if (strcmp_iw (demangled, lookfor) != 0) | |
1454 | { | |
1455 | free (demangled); | |
1456 | demangled = NULL; | |
1457 | } | |
1458 | } | |
1459 | return (demangled); | |
1460 | } | |
1461 | ||
bd5635a1 | 1462 | \f |
bd5635a1 RP |
1463 | void |
1464 | _initialize_utils () | |
1465 | { | |
1466 | struct cmd_list_element *c; | |
1467 | ||
1468 | c = add_set_cmd ("width", class_support, var_uinteger, | |
1469 | (char *)&chars_per_line, | |
1470 | "Set number of characters gdb thinks are in a line.", | |
1471 | &setlist); | |
1472 | add_show_from_set (c, &showlist); | |
d747e0af | 1473 | c->function.sfunc = set_width_command; |
bd5635a1 RP |
1474 | |
1475 | add_show_from_set | |
1476 | (add_set_cmd ("height", class_support, | |
1477 | var_uinteger, (char *)&lines_per_page, | |
1478 | "Set number of lines gdb thinks are in a page.", &setlist), | |
1479 | &showlist); | |
1480 | ||
1481 | /* These defaults will be used if we are unable to get the correct | |
1482 | values from termcap. */ | |
51b57ded FF |
1483 | #if defined(__GO32__) |
1484 | lines_per_page = ScreenRows(); | |
1485 | chars_per_line = ScreenCols(); | |
1486 | #else | |
bd5635a1 RP |
1487 | lines_per_page = 24; |
1488 | chars_per_line = 80; | |
1489 | /* Initialize the screen height and width from termcap. */ | |
1490 | { | |
1491 | char *termtype = getenv ("TERM"); | |
1492 | ||
1493 | /* Positive means success, nonpositive means failure. */ | |
1494 | int status; | |
1495 | ||
1496 | /* 2048 is large enough for all known terminals, according to the | |
1497 | GNU termcap manual. */ | |
1498 | char term_buffer[2048]; | |
1499 | ||
1500 | if (termtype) | |
1501 | { | |
1502 | status = tgetent (term_buffer, termtype); | |
1503 | if (status > 0) | |
1504 | { | |
1505 | int val; | |
1506 | ||
1507 | val = tgetnum ("li"); | |
1508 | if (val >= 0) | |
1509 | lines_per_page = val; | |
1510 | else | |
1511 | /* The number of lines per page is not mentioned | |
1512 | in the terminal description. This probably means | |
1513 | that paging is not useful (e.g. emacs shell window), | |
1514 | so disable paging. */ | |
1515 | lines_per_page = UINT_MAX; | |
1516 | ||
1517 | val = tgetnum ("co"); | |
1518 | if (val >= 0) | |
1519 | chars_per_line = val; | |
1520 | } | |
1521 | } | |
1522 | } | |
1523 | ||
1eeba686 PB |
1524 | #if defined(SIGWINCH) && defined(SIGWINCH_HANDLER) |
1525 | ||
4ace50a5 | 1526 | /* If there is a better way to determine the window size, use it. */ |
1eeba686 PB |
1527 | SIGWINCH_HANDLER (); |
1528 | #endif | |
51b57ded | 1529 | #endif |
2bc2e684 FF |
1530 | /* If the output is not a terminal, don't paginate it. */ |
1531 | if (!ISATTY (stdout)) | |
1532 | lines_per_page = UINT_MAX; | |
1533 | ||
bd5635a1 RP |
1534 | set_width_command ((char *)NULL, 0, c); |
1535 | ||
1536 | add_show_from_set | |
1537 | (add_set_cmd ("demangle", class_support, var_boolean, | |
1538 | (char *)&demangle, | |
1539 | "Set demangling of encoded C++ names when displaying symbols.", | |
f266e564 JK |
1540 | &setprintlist), |
1541 | &showprintlist); | |
bd5635a1 RP |
1542 | |
1543 | add_show_from_set | |
1544 | (add_set_cmd ("sevenbit-strings", class_support, var_boolean, | |
1545 | (char *)&sevenbit_strings, | |
1546 | "Set printing of 8-bit characters in strings as \\nnn.", | |
f266e564 JK |
1547 | &setprintlist), |
1548 | &showprintlist); | |
bd5635a1 RP |
1549 | |
1550 | add_show_from_set | |
1551 | (add_set_cmd ("asm-demangle", class_support, var_boolean, | |
1552 | (char *)&asm_demangle, | |
1553 | "Set demangling of C++ names in disassembly listings.", | |
f266e564 JK |
1554 | &setprintlist), |
1555 | &showprintlist); | |
bd5635a1 | 1556 | } |
1eeba686 PB |
1557 | |
1558 | /* Machine specific function to handle SIGWINCH signal. */ | |
1559 | ||
1560 | #ifdef SIGWINCH_HANDLER_BODY | |
1561 | SIGWINCH_HANDLER_BODY | |
1562 | #endif |