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