* bfd.c (union tdata): Add nlm_obj_data;
[deliverable/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"
bd5d07d9
FF
36#include "expression.h"
37#include "language.h"
bd5635a1 38
7919c3ed
JG
39/* Prototypes for local functions */
40
65ce5df4
JG
41#if defined (NO_MMALLOC) || defined (NO_MMALLOC_CHECK)
42#else
3624c875 43
7919c3ed
JG
44static void
45malloc_botch PARAMS ((void));
3624c875 46
65ce5df4 47#endif /* NO_MMALLOC, etc */
7919c3ed
JG
48
49static void
50fatal_dump_core (); /* Can't prototype with <varargs.h> usage... */
51
52static void
53prompt_for_continue PARAMS ((void));
54
55static void
56set_width_command PARAMS ((char *, int, struct cmd_list_element *));
57
bd5635a1
RP
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
67static struct cleanup *cleanup_chain;
68
69/* Nonzero means a quit has been requested. */
70
71int quit_flag;
72
73/* Nonzero means quit immediately if Control-C is typed now,
74 rather than waiting until QUIT is executed. */
75
76int immediate_quit;
77
78/* Nonzero means that encoded C++ names should be printed out in their
79 C++ form rather than raw. */
80
81int 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
87int 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
93int sevenbit_strings = 0;
81066208
JG
94
95/* String to be printed before error messages, if any. */
96
97char *error_pre_print;
3624c875 98char *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
105struct cleanup *
106make_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
125void
126do_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
141void
142discard_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. */
154struct cleanup *
155save_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. */
164void
165restore_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
179void
180free_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 */
194void
195null_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
205void
206warning_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 */
220void
221warning (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 244NORETURN void
bd5635a1
RP
245error (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 272NORETURN void
bd5635a1
RP
273fatal (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 292static void
bd5635a1
RP
293fatal_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
318char *
319safe_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
337char *
338safe_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
357void
358perror_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
382void
383print_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
401void
402quit ()
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 */
bd5d07d9
FF
417#else
418 error ("Quit");
51b57ded 419#endif
bd5635a1
RP
420}
421
bd5d07d9
FF
422
423#ifdef __GO32__
424
425/* In the absence of signals, poll keyboard for a quit.
426 Called from #define QUIT pollquit() in xm-go32.h. */
427
428void
429pollquit()
430{
431 if (kbhit ())
432 {
433 int k = getkey ();
434 if (k == 1)
435 quit_flag = 1;
436 else if (k == 2)
437 immediate_quit = 1;
438 quit ();
439 }
440}
441
442#endif
443
bd5635a1
RP
444/* Control C comes here */
445
446void
088c3a0b
JG
447request_quit (signo)
448 int signo;
bd5635a1
RP
449{
450 quit_flag = 1;
451
452#ifdef USG
453 /* Restore the signal handler. */
088c3a0b 454 signal (signo, request_quit);
bd5635a1
RP
455#endif
456
457 if (immediate_quit)
458 quit ();
459}
3624c875
FF
460
461\f
462/* Memory management stuff (malloc friends). */
463
464#if defined (NO_MMALLOC)
465
466PTR
467mmalloc (md, size)
468 PTR md;
469 long size;
470{
471 return (malloc (size));
472}
473
474PTR
475mrealloc (md, ptr, size)
476 PTR md;
477 PTR ptr;
478 long size;
479{
4ace50a5
FF
480 if (ptr == 0) /* Guard against old realloc's */
481 return malloc (size);
482 else
483 return realloc (ptr, size);
3624c875
FF
484}
485
486void
487mfree (md, ptr)
488 PTR md;
489 PTR ptr;
490{
491 free (ptr);
492}
493
494#endif /* NO_MMALLOC */
495
496#if defined (NO_MMALLOC) || defined (NO_MMALLOC_CHECK)
497
498void
499init_malloc (md)
500 PTR md;
501{
502}
503
504#else /* have mmalloc and want corruption checking */
505
506static void
507malloc_botch ()
508{
509 fatal_dump_core ("Memory corruption");
510}
511
512/* Attempt to install hooks in mmalloc/mrealloc/mfree for the heap specified
513 by MD, to detect memory corruption. Note that MD may be NULL to specify
514 the default heap that grows via sbrk.
515
516 Note that for freshly created regions, we must call mmcheck prior to any
517 mallocs in the region. Otherwise, any region which was allocated prior to
518 installing the checking hooks, which is later reallocated or freed, will
519 fail the checks! The mmcheck function only allows initial hooks to be
520 installed before the first mmalloc. However, anytime after we have called
521 mmcheck the first time to install the checking hooks, we can call it again
522 to update the function pointer to the memory corruption handler.
523
524 Returns zero on failure, non-zero on success. */
525
526void
527init_malloc (md)
528 PTR md;
529{
530 if (!mmcheck (md, malloc_botch))
531 {
532 warning ("internal error: failed to install memory consistency checks");
533 }
534
4ed3a9ea 535 mmtrace ();
3624c875
FF
536}
537
538#endif /* Have mmalloc and want corruption checking */
539
540/* Called when a memory allocation fails, with the number of bytes of
541 memory requested in SIZE. */
542
543NORETURN void
544nomem (size)
545 long size;
546{
547 if (size > 0)
548 {
549 fatal ("virtual memory exhausted: can't allocate %ld bytes.", size);
550 }
551 else
552 {
553 fatal ("virtual memory exhausted.");
554 }
555}
556
557/* Like mmalloc but get error if no storage available, and protect against
558 the caller wanting to allocate zero bytes. Whether to return NULL for
559 a zero byte request, or translate the request into a request for one
560 byte of zero'd storage, is a religious issue. */
561
562PTR
563xmmalloc (md, size)
564 PTR md;
565 long size;
566{
567 register PTR val;
568
569 if (size == 0)
570 {
571 val = NULL;
572 }
573 else if ((val = mmalloc (md, size)) == NULL)
574 {
575 nomem (size);
576 }
577 return (val);
578}
579
580/* Like mrealloc but get error if no storage available. */
581
582PTR
583xmrealloc (md, ptr, size)
584 PTR md;
585 PTR ptr;
586 long size;
587{
588 register PTR val;
589
590 if (ptr != NULL)
591 {
592 val = mrealloc (md, ptr, size);
593 }
594 else
595 {
596 val = mmalloc (md, size);
597 }
598 if (val == NULL)
599 {
600 nomem (size);
601 }
602 return (val);
603}
604
605/* Like malloc but get error if no storage available, and protect against
606 the caller wanting to allocate zero bytes. */
607
608PTR
609xmalloc (size)
610 long size;
611{
612 return (xmmalloc ((void *) NULL, size));
613}
614
615/* Like mrealloc but get error if no storage available. */
616
617PTR
618xrealloc (ptr, size)
619 PTR ptr;
620 long size;
621{
622 return (xmrealloc ((void *) NULL, ptr, size));
623}
624
bd5635a1
RP
625\f
626/* My replacement for the read system call.
627 Used like `read' but keeps going if `read' returns too soon. */
628
629int
630myread (desc, addr, len)
631 int desc;
632 char *addr;
633 int len;
634{
635 register int val;
636 int orglen = len;
637
638 while (len > 0)
639 {
640 val = read (desc, addr, len);
641 if (val < 0)
642 return val;
643 if (val == 0)
644 return orglen - len;
645 len -= val;
646 addr += val;
647 }
648 return orglen;
649}
650\f
651/* Make a copy of the string at PTR with SIZE characters
652 (and add a null character at the end in the copy).
653 Uses malloc to get the space. Returns the address of the copy. */
654
655char *
656savestring (ptr, size)
088c3a0b 657 const char *ptr;
bd5635a1
RP
658 int size;
659{
660 register char *p = (char *) xmalloc (size + 1);
4ed3a9ea 661 memcpy (p, ptr, size);
bd5635a1
RP
662 p[size] = 0;
663 return p;
664}
665
3624c875
FF
666char *
667msavestring (md, ptr, size)
668 void *md;
669 const char *ptr;
670 int size;
671{
672 register char *p = (char *) xmmalloc (md, size + 1);
4ed3a9ea 673 memcpy (p, ptr, size);
3624c875
FF
674 p[size] = 0;
675 return p;
676}
677
8aa13b87
JK
678/* The "const" is so it compiles under DGUX (which prototypes strsave
679 in <string.h>. FIXME: This should be named "xstrsave", shouldn't it?
680 Doesn't real strsave return NULL if out of memory? */
bd5635a1
RP
681char *
682strsave (ptr)
8aa13b87 683 const char *ptr;
bd5635a1
RP
684{
685 return savestring (ptr, strlen (ptr));
686}
687
3624c875
FF
688char *
689mstrsave (md, ptr)
690 void *md;
691 const char *ptr;
692{
693 return (msavestring (md, ptr, strlen (ptr)));
694}
695
bd5635a1
RP
696void
697print_spaces (n, file)
698 register int n;
699 register FILE *file;
700{
701 while (n-- > 0)
702 fputc (' ', file);
703}
704
705/* Ask user a y-or-n question and return 1 iff answer is yes.
706 Takes three args which are given to printf to print the question.
707 The first, a control string, should end in "? ".
708 It should not say how to answer, because we do that. */
709
710/* VARARGS */
711int
712query (va_alist)
713 va_dcl
714{
715 va_list args;
716 char *ctlstr;
717 register int answer;
718 register int ans2;
719
720 /* Automatically answer "yes" if input is not from a terminal. */
721 if (!input_from_terminal_p ())
722 return 1;
723
724 while (1)
725 {
546014f7
PB
726 wrap_here (""); /* Flush any buffered output */
727 fflush (stdout);
bd5635a1
RP
728 va_start (args);
729 ctlstr = va_arg (args, char *);
bcf2e6ab 730 vfprintf_filtered (stdout, ctlstr, args);
b36e3a9b 731 va_end (args);
bcf2e6ab 732 printf_filtered ("(y or n) ");
b36e3a9b
SG
733 fflush (stdout);
734 answer = fgetc (stdin);
735 clearerr (stdin); /* in case of C-d */
736 if (answer == EOF) /* C-d */
737 return 1;
738 if (answer != '\n') /* Eat rest of input line, to EOF or newline */
739 do
740 {
741 ans2 = fgetc (stdin);
742 clearerr (stdin);
743 }
744 while (ans2 != EOF && ans2 != '\n');
bd5635a1
RP
745 if (answer >= 'a')
746 answer -= 040;
747 if (answer == 'Y')
748 return 1;
749 if (answer == 'N')
750 return 0;
bcf2e6ab 751 printf_filtered ("Please answer y or n.\n");
bd5635a1
RP
752 }
753}
7919c3ed 754
bd5635a1
RP
755\f
756/* Parse a C escape sequence. STRING_PTR points to a variable
757 containing a pointer to the string to parse. That pointer
758 should point to the character after the \. That pointer
759 is updated past the characters we use. The value of the
760 escape sequence is returned.
761
762 A negative value means the sequence \ newline was seen,
763 which is supposed to be equivalent to nothing at all.
764
765 If \ is followed by a null character, we return a negative
766 value and leave the string pointer pointing at the null character.
767
768 If \ is followed by 000, we return 0 and leave the string pointer
769 after the zeros. A value of 0 does not mean end of string. */
770
771int
772parse_escape (string_ptr)
773 char **string_ptr;
774{
775 register int c = *(*string_ptr)++;
776 switch (c)
777 {
778 case 'a':
2bc2e684 779 return 007; /* Bell (alert) char */
bd5635a1
RP
780 case 'b':
781 return '\b';
2bc2e684 782 case 'e': /* Escape character */
bd5635a1
RP
783 return 033;
784 case 'f':
785 return '\f';
786 case 'n':
787 return '\n';
788 case 'r':
789 return '\r';
790 case 't':
791 return '\t';
792 case 'v':
793 return '\v';
794 case '\n':
795 return -2;
796 case 0:
797 (*string_ptr)--;
798 return 0;
799 case '^':
800 c = *(*string_ptr)++;
801 if (c == '\\')
802 c = parse_escape (string_ptr);
803 if (c == '?')
804 return 0177;
805 return (c & 0200) | (c & 037);
806
807 case '0':
808 case '1':
809 case '2':
810 case '3':
811 case '4':
812 case '5':
813 case '6':
814 case '7':
815 {
816 register int i = c - '0';
817 register int count = 0;
818 while (++count < 3)
819 {
820 if ((c = *(*string_ptr)++) >= '0' && c <= '7')
821 {
822 i *= 8;
823 i += c - '0';
824 }
825 else
826 {
827 (*string_ptr)--;
828 break;
829 }
830 }
831 return i;
832 }
833 default:
834 return c;
835 }
836}
837\f
51b80b00
FF
838/* Print the character C on STREAM as part of the contents of a literal
839 string whose delimiter is QUOTER. Note that this routine should only
840 be call for printing things which are independent of the language
841 of the program being debugged. */
bd5635a1
RP
842
843void
51b80b00 844gdb_printchar (c, stream, quoter)
088c3a0b 845 register int c;
bd5635a1
RP
846 FILE *stream;
847 int quoter;
848{
bd5635a1 849
7e7e2d40
JG
850 c &= 0xFF; /* Avoid sign bit follies */
851
fcdb113e
JG
852 if ( c < 0x20 || /* Low control chars */
853 (c >= 0x7F && c < 0xA0) || /* DEL, High controls */
854 (sevenbit_strings && c >= 0x80)) { /* high order bit set */
bd5635a1
RP
855 switch (c)
856 {
857 case '\n':
858 fputs_filtered ("\\n", stream);
859 break;
860 case '\b':
861 fputs_filtered ("\\b", stream);
862 break;
863 case '\t':
864 fputs_filtered ("\\t", stream);
865 break;
866 case '\f':
867 fputs_filtered ("\\f", stream);
868 break;
869 case '\r':
870 fputs_filtered ("\\r", stream);
871 break;
872 case '\033':
873 fputs_filtered ("\\e", stream);
874 break;
875 case '\007':
876 fputs_filtered ("\\a", stream);
877 break;
878 default:
879 fprintf_filtered (stream, "\\%.3o", (unsigned int) c);
880 break;
881 }
2bc2e684
FF
882 } else {
883 if (c == '\\' || c == quoter)
884 fputs_filtered ("\\", stream);
885 fprintf_filtered (stream, "%c", c);
886 }
bd5635a1
RP
887}
888\f
889/* Number of lines per page or UINT_MAX if paging is disabled. */
890static unsigned int lines_per_page;
891/* Number of chars per line or UNIT_MAX is line folding is disabled. */
892static unsigned int chars_per_line;
893/* Current count of lines printed on this page, chars on this line. */
894static unsigned int lines_printed, chars_printed;
895
896/* Buffer and start column of buffered text, for doing smarter word-
897 wrapping. When someone calls wrap_here(), we start buffering output
898 that comes through fputs_filtered(). If we see a newline, we just
899 spit it out and forget about the wrap_here(). If we see another
900 wrap_here(), we spit it out and remember the newer one. If we see
901 the end of the line, we spit out a newline, the indent, and then
902 the buffered output.
903
904 wrap_column is the column number on the screen where wrap_buffer begins.
905 When wrap_column is zero, wrapping is not in effect.
906 wrap_buffer is malloc'd with chars_per_line+2 bytes.
907 When wrap_buffer[0] is null, the buffer is empty.
908 wrap_pointer points into it at the next character to fill.
909 wrap_indent is the string that should be used as indentation if the
910 wrap occurs. */
911
912static char *wrap_buffer, *wrap_pointer, *wrap_indent;
913static int wrap_column;
914
e1ce8aa5 915/* ARGSUSED */
bd5635a1
RP
916static void
917set_width_command (args, from_tty, c)
918 char *args;
919 int from_tty;
920 struct cmd_list_element *c;
921{
922 if (!wrap_buffer)
923 {
924 wrap_buffer = (char *) xmalloc (chars_per_line + 2);
925 wrap_buffer[0] = '\0';
926 }
927 else
928 wrap_buffer = (char *) xrealloc (wrap_buffer, chars_per_line + 2);
929 wrap_pointer = wrap_buffer; /* Start it at the beginning */
930}
931
d974236f
JG
932/* Wait, so the user can read what's on the screen. Prompt the user
933 to continue by pressing RETURN. */
934
bd5635a1
RP
935static void
936prompt_for_continue ()
937{
351b221d
JG
938 char *ignore;
939
d974236f
JG
940 /* We must do this *before* we call gdb_readline, else it will eventually
941 call us -- thinking that we're trying to print beyond the end of the
942 screen. */
943 reinitialize_more_filter ();
944
bd5635a1 945 immediate_quit++;
351b221d
JG
946 ignore = gdb_readline ("---Type <return> to continue---");
947 if (ignore)
948 free (ignore);
bd5635a1 949 immediate_quit--;
d974236f
JG
950
951 /* Now we have to do this again, so that GDB will know that it doesn't
952 need to save the ---Type <return>--- line at the top of the screen. */
953 reinitialize_more_filter ();
954
351b221d 955 dont_repeat (); /* Forget prev cmd -- CR won't repeat it. */
bd5635a1
RP
956}
957
958/* Reinitialize filter; ie. tell it to reset to original values. */
959
960void
961reinitialize_more_filter ()
962{
963 lines_printed = 0;
964 chars_printed = 0;
965}
966
967/* Indicate that if the next sequence of characters overflows the line,
968 a newline should be inserted here rather than when it hits the end.
969 If INDENT is nonzero, it is a string to be printed to indent the
970 wrapped part on the next line. INDENT must remain accessible until
971 the next call to wrap_here() or until a newline is printed through
972 fputs_filtered().
973
974 If the line is already overfull, we immediately print a newline and
975 the indentation, and disable further wrapping.
976
2bc2e684
FF
977 If we don't know the width of lines, but we know the page height,
978 we must not wrap words, but should still keep track of newlines
979 that were explicitly printed.
980
bd5635a1
RP
981 INDENT should not contain tabs, as that
982 will mess up the char count on the next line. FIXME. */
983
984void
985wrap_here(indent)
986 char *indent;
987{
988 if (wrap_buffer[0])
989 {
990 *wrap_pointer = '\0';
991 fputs (wrap_buffer, stdout);
992 }
993 wrap_pointer = wrap_buffer;
994 wrap_buffer[0] = '\0';
2bc2e684
FF
995 if (chars_per_line == UINT_MAX) /* No line overflow checking */
996 {
997 wrap_column = 0;
998 }
999 else if (chars_printed >= chars_per_line)
bd5635a1
RP
1000 {
1001 puts_filtered ("\n");
1002 puts_filtered (indent);
1003 wrap_column = 0;
1004 }
1005 else
1006 {
1007 wrap_column = chars_printed;
1008 wrap_indent = indent;
1009 }
1010}
1011
51b80b00
FF
1012/* Ensure that whatever gets printed next, using the filtered output
1013 commands, starts at the beginning of the line. I.E. if there is
1014 any pending output for the current line, flush it and start a new
1015 line. Otherwise do nothing. */
1016
1017void
1018begin_line ()
1019{
1020 if (chars_printed > 0)
1021 {
1022 puts_filtered ("\n");
1023 }
1024}
1025
bd5635a1
RP
1026/* Like fputs but pause after every screenful, and can wrap at points
1027 other than the final character of a line.
1028 Unlike fputs, fputs_filtered does not return a value.
1029 It is OK for LINEBUFFER to be NULL, in which case just don't print
1030 anything.
1031
1032 Note that a longjmp to top level may occur in this routine
1033 (since prompt_for_continue may do so) so this routine should not be
1034 called when cleanups are not in place. */
1035
1036void
1037fputs_filtered (linebuffer, stream)
088c3a0b 1038 const char *linebuffer;
bd5635a1
RP
1039 FILE *stream;
1040{
7919c3ed 1041 const char *lineptr;
bd5635a1
RP
1042
1043 if (linebuffer == 0)
1044 return;
1045
1046 /* Don't do any filtering if it is disabled. */
1047 if (stream != stdout
1048 || (lines_per_page == UINT_MAX && chars_per_line == UINT_MAX))
1049 {
1050 fputs (linebuffer, stream);
1051 return;
1052 }
1053
1054 /* Go through and output each character. Show line extension
1055 when this is necessary; prompt user for new page when this is
1056 necessary. */
1057
1058 lineptr = linebuffer;
1059 while (*lineptr)
1060 {
1061 /* Possible new page. */
1062 if (lines_printed >= lines_per_page - 1)
1063 prompt_for_continue ();
1064
1065 while (*lineptr && *lineptr != '\n')
1066 {
1067 /* Print a single line. */
1068 if (*lineptr == '\t')
1069 {
1070 if (wrap_column)
1071 *wrap_pointer++ = '\t';
1072 else
1073 putc ('\t', stream);
1074 /* Shifting right by 3 produces the number of tab stops
1075 we have already passed, and then adding one and
1076 shifting left 3 advances to the next tab stop. */
1077 chars_printed = ((chars_printed >> 3) + 1) << 3;
1078 lineptr++;
1079 }
1080 else
1081 {
1082 if (wrap_column)
1083 *wrap_pointer++ = *lineptr;
1084 else
1085 putc (*lineptr, stream);
1086 chars_printed++;
1087 lineptr++;
1088 }
1089
1090 if (chars_printed >= chars_per_line)
1091 {
1092 unsigned int save_chars = chars_printed;
1093
1094 chars_printed = 0;
1095 lines_printed++;
1096 /* If we aren't actually wrapping, don't output newline --
1097 if chars_per_line is right, we probably just overflowed
1098 anyway; if it's wrong, let us keep going. */
1099 if (wrap_column)
1100 putc ('\n', stream);
1101
1102 /* Possible new page. */
1103 if (lines_printed >= lines_per_page - 1)
1104 prompt_for_continue ();
1105
1106 /* Now output indentation and wrapped string */
1107 if (wrap_column)
1108 {
1109 if (wrap_indent)
1110 fputs (wrap_indent, stream);
1111 *wrap_pointer = '\0'; /* Null-terminate saved stuff */
1112 fputs (wrap_buffer, stream); /* and eject it */
1113 /* FIXME, this strlen is what prevents wrap_indent from
1114 containing tabs. However, if we recurse to print it
1115 and count its chars, we risk trouble if wrap_indent is
1116 longer than (the user settable) chars_per_line.
1117 Note also that this can set chars_printed > chars_per_line
1118 if we are printing a long string. */
1119 chars_printed = strlen (wrap_indent)
1120 + (save_chars - wrap_column);
1121 wrap_pointer = wrap_buffer; /* Reset buffer */
1122 wrap_buffer[0] = '\0';
1123 wrap_column = 0; /* And disable fancy wrap */
1124 }
1125 }
1126 }
1127
1128 if (*lineptr == '\n')
1129 {
1130 chars_printed = 0;
d11c44f1 1131 wrap_here ((char *)0); /* Spit out chars, cancel further wraps */
bd5635a1
RP
1132 lines_printed++;
1133 putc ('\n', stream);
1134 lineptr++;
1135 }
1136 }
1137}
1138
bd5635a1
RP
1139/* Print a variable number of ARGS using format FORMAT. If this
1140 information is going to put the amount written (since the last call
d974236f 1141 to REINITIALIZE_MORE_FILTER or the last page break) over the page size,
bd5635a1
RP
1142 print out a pause message and do a gdb_readline to get the users
1143 permision to continue.
1144
1145 Unlike fprintf, this function does not return a value.
1146
1147 We implement three variants, vfprintf (takes a vararg list and stream),
1148 fprintf (takes a stream to write on), and printf (the usual).
1149
1150 Note that this routine has a restriction that the length of the
1151 final output line must be less than 255 characters *or* it must be
1152 less than twice the size of the format string. This is a very
1153 arbitrary restriction, but it is an internal restriction, so I'll
1154 put it in. This means that the %s format specifier is almost
1155 useless; unless the caller can GUARANTEE that the string is short
1156 enough, fputs_filtered should be used instead.
1157
1158 Note also that a longjmp to top level may occur in this routine
1159 (since prompt_for_continue may do so) so this routine should not be
1160 called when cleanups are not in place. */
1161
d974236f
JG
1162#define MIN_LINEBUF 255
1163
a8e033f2 1164void
bd5635a1 1165vfprintf_filtered (stream, format, args)
bd5635a1
RP
1166 FILE *stream;
1167 char *format;
7919c3ed 1168 va_list args;
bd5635a1 1169{
d974236f
JG
1170 char line_buf[MIN_LINEBUF+10];
1171 char *linebuffer = line_buf;
bd5635a1
RP
1172 int format_length;
1173
1174 format_length = strlen (format);
1175
bd5635a1 1176 /* Reallocate buffer to a larger size if this is necessary. */
d974236f 1177 if (format_length * 2 > MIN_LINEBUF)
bd5635a1 1178 {
d974236f 1179 linebuffer = alloca (10 + format_length * 2);
bd5635a1
RP
1180 }
1181
bd5635a1
RP
1182 /* This won't blow up if the restrictions described above are
1183 followed. */
4ed3a9ea 1184 vsprintf (linebuffer, format, args);
bd5635a1
RP
1185
1186 fputs_filtered (linebuffer, stream);
1187}
1188
51b80b00
FF
1189void
1190vprintf_filtered (format, args)
1191 char *format;
1192 va_list args;
1193{
1194 vfprintf_filtered (stdout, format, args);
1195}
1196
bd5635a1
RP
1197/* VARARGS */
1198void
1199fprintf_filtered (va_alist)
1200 va_dcl
1201{
546014f7 1202 va_list args;
bd5635a1
RP
1203 FILE *stream;
1204 char *format;
546014f7
PB
1205
1206 va_start (args);
1207 stream = va_arg (args, FILE *);
1208 format = va_arg (args, char *);
1209
1210 /* This won't blow up if the restrictions described above are
1211 followed. */
1212 vfprintf_filtered (stream, format, args);
1213 va_end (args);
1214}
1215
1216/* Like fprintf_filtered, but prints it's result indent.
1217 Called as fprintfi_filtered (spaces, format, arg1, arg2, ...); */
1218
1219/* VARARGS */
1220void
1221fprintfi_filtered (va_alist)
1222 va_dcl
1223{
7919c3ed 1224 va_list args;
546014f7
PB
1225 int spaces;
1226 FILE *stream;
1227 char *format;
bd5635a1
RP
1228
1229 va_start (args);
546014f7 1230 spaces = va_arg (args, int);
bd5635a1
RP
1231 stream = va_arg (args, FILE *);
1232 format = va_arg (args, char *);
546014f7 1233 print_spaces_filtered (spaces, stream);
bd5635a1
RP
1234
1235 /* This won't blow up if the restrictions described above are
1236 followed. */
7919c3ed 1237 vfprintf_filtered (stream, format, args);
bd5635a1
RP
1238 va_end (args);
1239}
1240
1241/* VARARGS */
1242void
1243printf_filtered (va_alist)
1244 va_dcl
1245{
1246 va_list args;
1247 char *format;
1248
1249 va_start (args);
1250 format = va_arg (args, char *);
1251
7919c3ed 1252 vfprintf_filtered (stdout, format, args);
bd5635a1
RP
1253 va_end (args);
1254}
bd5635a1 1255
546014f7
PB
1256/* Like printf_filtered, but prints it's result indented.
1257 Called as printfi_filtered (spaces, format, arg1, arg2, ...); */
1258
1259/* VARARGS */
1260void
1261printfi_filtered (va_alist)
1262 va_dcl
1263{
1264 va_list args;
1265 int spaces;
1266 char *format;
1267
1268 va_start (args);
1269 spaces = va_arg (args, int);
1270 format = va_arg (args, char *);
1271 print_spaces_filtered (spaces, stdout);
1272 vfprintf_filtered (stdout, format, args);
1273 va_end (args);
1274}
1275
51b80b00
FF
1276/* Easy -- but watch out!
1277
1278 This routine is *not* a replacement for puts()! puts() appends a newline.
1279 This one doesn't, and had better not! */
bd5635a1
RP
1280
1281void
1282puts_filtered (string)
1283 char *string;
1284{
1285 fputs_filtered (string, stdout);
1286}
1287
1288/* Return a pointer to N spaces and a null. The pointer is good
1289 until the next call to here. */
1290char *
1291n_spaces (n)
1292 int n;
1293{
1294 register char *t;
1295 static char *spaces;
1296 static int max_spaces;
1297
1298 if (n > max_spaces)
1299 {
1300 if (spaces)
1301 free (spaces);
3624c875 1302 spaces = (char *) xmalloc (n+1);
bd5635a1
RP
1303 for (t = spaces+n; t != spaces;)
1304 *--t = ' ';
1305 spaces[n] = '\0';
1306 max_spaces = n;
1307 }
1308
1309 return spaces + max_spaces - n;
1310}
1311
1312/* Print N spaces. */
1313void
1314print_spaces_filtered (n, stream)
1315 int n;
1316 FILE *stream;
1317{
1318 fputs_filtered (n_spaces (n), stream);
1319}
1320\f
1321/* C++ demangler stuff. */
bd5635a1 1322
65ce5df4
JG
1323/* fprintf_symbol_filtered attempts to demangle NAME, a symbol in language
1324 LANG, using demangling args ARG_MODE, and print it filtered to STREAM.
1325 If the name is not mangled, or the language for the name is unknown, or
1326 demangling is off, the name is printed in its "raw" form. */
1327
bd5635a1 1328void
65ce5df4 1329fprintf_symbol_filtered (stream, name, lang, arg_mode)
bd5635a1
RP
1330 FILE *stream;
1331 char *name;
65ce5df4
JG
1332 enum language lang;
1333 int arg_mode;
bd5635a1 1334{
65ce5df4 1335 char *demangled;
bd5d07d9 1336
65ce5df4 1337 if (name != NULL)
bd5d07d9 1338 {
65ce5df4
JG
1339 /* If user wants to see raw output, no problem. */
1340 if (!demangle)
bd5d07d9 1341 {
65ce5df4
JG
1342 fputs_filtered (name, stream);
1343 }
1344 else
1345 {
1346 switch (lang)
1347 {
1348 case language_cplus:
1349 demangled = cplus_demangle (name, arg_mode);
1350 break;
65ce5df4
JG
1351 case language_chill:
1352 demangled = chill_demangle (name);
1353 break;
65ce5df4
JG
1354 default:
1355 demangled = NULL;
1356 break;
1357 }
1358 fputs_filtered (demangled ? demangled : name, stream);
1359 if (demangled != NULL)
1360 {
1361 free (demangled);
1362 }
bd5d07d9 1363 }
bd5635a1
RP
1364 }
1365}
51b57ded
FF
1366
1367/* Do a strcmp() type operation on STRING1 and STRING2, ignoring any
1368 differences in whitespace. Returns 0 if they match, non-zero if they
546014f7
PB
1369 don't (slightly different than strcmp()'s range of return values).
1370
1371 As an extra hack, string1=="FOO(ARGS)" matches string2=="FOO".
2e4964ad
FF
1372 This "feature" is useful when searching for matching C++ function names
1373 (such as if the user types 'break FOO', where FOO is a mangled C++
1374 function). */
51b57ded 1375
51b80b00 1376int
51b57ded
FF
1377strcmp_iw (string1, string2)
1378 const char *string1;
1379 const char *string2;
1380{
1381 while ((*string1 != '\0') && (*string2 != '\0'))
1382 {
1383 while (isspace (*string1))
1384 {
1385 string1++;
1386 }
1387 while (isspace (*string2))
1388 {
1389 string2++;
1390 }
1391 if (*string1 != *string2)
1392 {
1393 break;
1394 }
1395 if (*string1 != '\0')
1396 {
1397 string1++;
1398 string2++;
1399 }
1400 }
546014f7 1401 return (*string1 != '\0' && *string1 != '(') || (*string2 != '\0');
51b57ded
FF
1402}
1403
bd5635a1 1404\f
bd5635a1
RP
1405void
1406_initialize_utils ()
1407{
1408 struct cmd_list_element *c;
1409
1410 c = add_set_cmd ("width", class_support, var_uinteger,
1411 (char *)&chars_per_line,
1412 "Set number of characters gdb thinks are in a line.",
1413 &setlist);
1414 add_show_from_set (c, &showlist);
d747e0af 1415 c->function.sfunc = set_width_command;
bd5635a1
RP
1416
1417 add_show_from_set
1418 (add_set_cmd ("height", class_support,
1419 var_uinteger, (char *)&lines_per_page,
1420 "Set number of lines gdb thinks are in a page.", &setlist),
1421 &showlist);
1422
1423 /* These defaults will be used if we are unable to get the correct
1424 values from termcap. */
51b57ded
FF
1425#if defined(__GO32__)
1426 lines_per_page = ScreenRows();
1427 chars_per_line = ScreenCols();
1428#else
bd5635a1
RP
1429 lines_per_page = 24;
1430 chars_per_line = 80;
1431 /* Initialize the screen height and width from termcap. */
1432 {
1433 char *termtype = getenv ("TERM");
1434
1435 /* Positive means success, nonpositive means failure. */
1436 int status;
1437
1438 /* 2048 is large enough for all known terminals, according to the
1439 GNU termcap manual. */
1440 char term_buffer[2048];
1441
1442 if (termtype)
1443 {
1444 status = tgetent (term_buffer, termtype);
1445 if (status > 0)
1446 {
1447 int val;
1448
1449 val = tgetnum ("li");
1450 if (val >= 0)
1451 lines_per_page = val;
1452 else
1453 /* The number of lines per page is not mentioned
1454 in the terminal description. This probably means
1455 that paging is not useful (e.g. emacs shell window),
1456 so disable paging. */
1457 lines_per_page = UINT_MAX;
1458
1459 val = tgetnum ("co");
1460 if (val >= 0)
1461 chars_per_line = val;
1462 }
1463 }
1464 }
1465
1eeba686
PB
1466#if defined(SIGWINCH) && defined(SIGWINCH_HANDLER)
1467
4ace50a5 1468 /* If there is a better way to determine the window size, use it. */
1eeba686
PB
1469 SIGWINCH_HANDLER ();
1470#endif
51b57ded 1471#endif
2bc2e684
FF
1472 /* If the output is not a terminal, don't paginate it. */
1473 if (!ISATTY (stdout))
1474 lines_per_page = UINT_MAX;
1475
bd5635a1
RP
1476 set_width_command ((char *)NULL, 0, c);
1477
1478 add_show_from_set
1479 (add_set_cmd ("demangle", class_support, var_boolean,
1480 (char *)&demangle,
1481 "Set demangling of encoded C++ names when displaying symbols.",
f266e564
JK
1482 &setprintlist),
1483 &showprintlist);
bd5635a1
RP
1484
1485 add_show_from_set
1486 (add_set_cmd ("sevenbit-strings", class_support, var_boolean,
1487 (char *)&sevenbit_strings,
1488 "Set printing of 8-bit characters in strings as \\nnn.",
f266e564
JK
1489 &setprintlist),
1490 &showprintlist);
bd5635a1
RP
1491
1492 add_show_from_set
1493 (add_set_cmd ("asm-demangle", class_support, var_boolean,
1494 (char *)&asm_demangle,
1495 "Set demangling of C++ names in disassembly listings.",
f266e564
JK
1496 &setprintlist),
1497 &showprintlist);
bd5635a1 1498}
1eeba686
PB
1499
1500/* Machine specific function to handle SIGWINCH signal. */
1501
1502#ifdef SIGWINCH_HANDLER_BODY
1503 SIGWINCH_HANDLER_BODY
1504#endif
bd5d07d9 1505
This page took 0.253306 seconds and 4 git commands to generate.