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