* utils.c (printchar): Print 0x7F and 0x80-0x9F in \nnn notation
[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
4ace50a5
FF
312/* The strerror() function can return NULL for errno values that are
313 out of range. Provide a "safe" version that always returns a
314 printable string. */
315
316char *
317safe_strerror (errnum)
318 int errnum;
319{
320 char *msg;
321 static char buf[32];
322
323 if ((msg = strerror (errnum)) == NULL)
324 {
325 sprintf (buf, "(undocumented errno %d)", errnum);
326 msg = buf;
327 }
328 return (msg);
329}
330
331/* The strsignal() function can return NULL for signal values that are
332 out of range. Provide a "safe" version that always returns a
333 printable string. */
334
335char *
336safe_strsignal (signo)
337 int signo;
338{
339 char *msg;
340 static char buf[32];
341
342 if ((msg = strsignal (signo)) == NULL)
343 {
344 sprintf (buf, "(undocumented signal %d)", signo);
345 msg = buf;
346 }
347 return (msg);
348}
349
350
bd5635a1
RP
351/* Print the system error message for errno, and also mention STRING
352 as the file name for which the error was encountered.
353 Then return to command level. */
354
355void
356perror_with_name (string)
357 char *string;
358{
bd5635a1
RP
359 char *err;
360 char *combined;
361
4ace50a5 362 err = safe_strerror (errno);
bd5635a1
RP
363 combined = (char *) alloca (strlen (err) + strlen (string) + 3);
364 strcpy (combined, string);
365 strcat (combined, ": ");
366 strcat (combined, err);
367
368 /* I understand setting these is a matter of taste. Still, some people
369 may clear errno but not know about bfd_error. Doing this here is not
370 unreasonable. */
371 bfd_error = no_error;
372 errno = 0;
373
374 error ("%s.", combined);
375}
376
377/* Print the system error message for ERRCODE, and also mention STRING
378 as the file name for which the error was encountered. */
379
380void
381print_sys_errmsg (string, errcode)
382 char *string;
383 int errcode;
384{
bd5635a1
RP
385 char *err;
386 char *combined;
387
4ace50a5 388 err = safe_strerror (errcode);
bd5635a1
RP
389 combined = (char *) alloca (strlen (err) + strlen (string) + 3);
390 strcpy (combined, string);
391 strcat (combined, ": ");
392 strcat (combined, err);
393
394 printf ("%s.\n", combined);
395}
396
397/* Control C eventually causes this to be called, at a convenient time. */
398
399void
400quit ()
401{
402 target_terminal_ours ();
d11c44f1 403 wrap_here ((char *)0); /* Force out any pending output */
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 */
414}
415
416/* Control C comes here */
417
418void
088c3a0b
JG
419request_quit (signo)
420 int signo;
bd5635a1
RP
421{
422 quit_flag = 1;
423
424#ifdef USG
425 /* Restore the signal handler. */
088c3a0b 426 signal (signo, request_quit);
bd5635a1
RP
427#endif
428
429 if (immediate_quit)
430 quit ();
431}
3624c875
FF
432
433\f
434/* Memory management stuff (malloc friends). */
435
436#if defined (NO_MMALLOC)
437
438PTR
439mmalloc (md, size)
440 PTR md;
441 long size;
442{
443 return (malloc (size));
444}
445
446PTR
447mrealloc (md, ptr, size)
448 PTR md;
449 PTR ptr;
450 long size;
451{
4ace50a5
FF
452 if (ptr == 0) /* Guard against old realloc's */
453 return malloc (size);
454 else
455 return realloc (ptr, size);
3624c875
FF
456}
457
458void
459mfree (md, ptr)
460 PTR md;
461 PTR ptr;
462{
463 free (ptr);
464}
465
466#endif /* NO_MMALLOC */
467
468#if defined (NO_MMALLOC) || defined (NO_MMALLOC_CHECK)
469
470void
471init_malloc (md)
472 PTR md;
473{
474}
475
476#else /* have mmalloc and want corruption checking */
477
478static void
479malloc_botch ()
480{
481 fatal_dump_core ("Memory corruption");
482}
483
484/* Attempt to install hooks in mmalloc/mrealloc/mfree for the heap specified
485 by MD, to detect memory corruption. Note that MD may be NULL to specify
486 the default heap that grows via sbrk.
487
488 Note that for freshly created regions, we must call mmcheck prior to any
489 mallocs in the region. Otherwise, any region which was allocated prior to
490 installing the checking hooks, which is later reallocated or freed, will
491 fail the checks! The mmcheck function only allows initial hooks to be
492 installed before the first mmalloc. However, anytime after we have called
493 mmcheck the first time to install the checking hooks, we can call it again
494 to update the function pointer to the memory corruption handler.
495
496 Returns zero on failure, non-zero on success. */
497
498void
499init_malloc (md)
500 PTR md;
501{
502 if (!mmcheck (md, malloc_botch))
503 {
504 warning ("internal error: failed to install memory consistency checks");
505 }
506
507 (void) mmtrace ();
508}
509
510#endif /* Have mmalloc and want corruption checking */
511
512/* Called when a memory allocation fails, with the number of bytes of
513 memory requested in SIZE. */
514
515NORETURN void
516nomem (size)
517 long size;
518{
519 if (size > 0)
520 {
521 fatal ("virtual memory exhausted: can't allocate %ld bytes.", size);
522 }
523 else
524 {
525 fatal ("virtual memory exhausted.");
526 }
527}
528
529/* Like mmalloc but get error if no storage available, and protect against
530 the caller wanting to allocate zero bytes. Whether to return NULL for
531 a zero byte request, or translate the request into a request for one
532 byte of zero'd storage, is a religious issue. */
533
534PTR
535xmmalloc (md, size)
536 PTR md;
537 long size;
538{
539 register PTR val;
540
541 if (size == 0)
542 {
543 val = NULL;
544 }
545 else if ((val = mmalloc (md, size)) == NULL)
546 {
547 nomem (size);
548 }
549 return (val);
550}
551
552/* Like mrealloc but get error if no storage available. */
553
554PTR
555xmrealloc (md, ptr, size)
556 PTR md;
557 PTR ptr;
558 long size;
559{
560 register PTR val;
561
562 if (ptr != NULL)
563 {
564 val = mrealloc (md, ptr, size);
565 }
566 else
567 {
568 val = mmalloc (md, size);
569 }
570 if (val == NULL)
571 {
572 nomem (size);
573 }
574 return (val);
575}
576
577/* Like malloc but get error if no storage available, and protect against
578 the caller wanting to allocate zero bytes. */
579
580PTR
581xmalloc (size)
582 long size;
583{
584 return (xmmalloc ((void *) NULL, size));
585}
586
587/* Like mrealloc but get error if no storage available. */
588
589PTR
590xrealloc (ptr, size)
591 PTR ptr;
592 long size;
593{
594 return (xmrealloc ((void *) NULL, ptr, size));
595}
596
bd5635a1
RP
597\f
598/* My replacement for the read system call.
599 Used like `read' but keeps going if `read' returns too soon. */
600
601int
602myread (desc, addr, len)
603 int desc;
604 char *addr;
605 int len;
606{
607 register int val;
608 int orglen = len;
609
610 while (len > 0)
611 {
612 val = read (desc, addr, len);
613 if (val < 0)
614 return val;
615 if (val == 0)
616 return orglen - len;
617 len -= val;
618 addr += val;
619 }
620 return orglen;
621}
622\f
623/* Make a copy of the string at PTR with SIZE characters
624 (and add a null character at the end in the copy).
625 Uses malloc to get the space. Returns the address of the copy. */
626
627char *
628savestring (ptr, size)
088c3a0b 629 const char *ptr;
bd5635a1
RP
630 int size;
631{
632 register char *p = (char *) xmalloc (size + 1);
633 bcopy (ptr, p, size);
634 p[size] = 0;
635 return p;
636}
637
3624c875
FF
638char *
639msavestring (md, ptr, size)
640 void *md;
641 const char *ptr;
642 int size;
643{
644 register char *p = (char *) xmmalloc (md, size + 1);
645 bcopy (ptr, p, size);
646 p[size] = 0;
647 return p;
648}
649
8aa13b87
JK
650/* The "const" is so it compiles under DGUX (which prototypes strsave
651 in <string.h>. FIXME: This should be named "xstrsave", shouldn't it?
652 Doesn't real strsave return NULL if out of memory? */
bd5635a1
RP
653char *
654strsave (ptr)
8aa13b87 655 const char *ptr;
bd5635a1
RP
656{
657 return savestring (ptr, strlen (ptr));
658}
659
3624c875
FF
660char *
661mstrsave (md, ptr)
662 void *md;
663 const char *ptr;
664{
665 return (msavestring (md, ptr, strlen (ptr)));
666}
667
bd5635a1
RP
668void
669print_spaces (n, file)
670 register int n;
671 register FILE *file;
672{
673 while (n-- > 0)
674 fputc (' ', file);
675}
676
677/* Ask user a y-or-n question and return 1 iff answer is yes.
678 Takes three args which are given to printf to print the question.
679 The first, a control string, should end in "? ".
680 It should not say how to answer, because we do that. */
681
682/* VARARGS */
683int
684query (va_alist)
685 va_dcl
686{
687 va_list args;
688 char *ctlstr;
689 register int answer;
690 register int ans2;
691
692 /* Automatically answer "yes" if input is not from a terminal. */
693 if (!input_from_terminal_p ())
694 return 1;
695
696 while (1)
697 {
698 va_start (args);
699 ctlstr = va_arg (args, char *);
700 vfprintf (stdout, ctlstr, args);
701 va_end (args);
702 printf ("(y or n) ");
703 fflush (stdout);
704 answer = fgetc (stdin);
705 clearerr (stdin); /* in case of C-d */
706 if (answer == EOF) /* C-d */
707 return 1;
708 if (answer != '\n') /* Eat rest of input line, to EOF or newline */
709 do
710 {
711 ans2 = fgetc (stdin);
712 clearerr (stdin);
713 }
714 while (ans2 != EOF && ans2 != '\n');
715 if (answer >= 'a')
716 answer -= 040;
717 if (answer == 'Y')
718 return 1;
719 if (answer == 'N')
720 return 0;
721 printf ("Please answer y or n.\n");
722 }
723}
7919c3ed 724
bd5635a1
RP
725\f
726/* Parse a C escape sequence. STRING_PTR points to a variable
727 containing a pointer to the string to parse. That pointer
728 should point to the character after the \. That pointer
729 is updated past the characters we use. The value of the
730 escape sequence is returned.
731
732 A negative value means the sequence \ newline was seen,
733 which is supposed to be equivalent to nothing at all.
734
735 If \ is followed by a null character, we return a negative
736 value and leave the string pointer pointing at the null character.
737
738 If \ is followed by 000, we return 0 and leave the string pointer
739 after the zeros. A value of 0 does not mean end of string. */
740
741int
742parse_escape (string_ptr)
743 char **string_ptr;
744{
745 register int c = *(*string_ptr)++;
746 switch (c)
747 {
748 case 'a':
2bc2e684 749 return 007; /* Bell (alert) char */
bd5635a1
RP
750 case 'b':
751 return '\b';
2bc2e684 752 case 'e': /* Escape character */
bd5635a1
RP
753 return 033;
754 case 'f':
755 return '\f';
756 case 'n':
757 return '\n';
758 case 'r':
759 return '\r';
760 case 't':
761 return '\t';
762 case 'v':
763 return '\v';
764 case '\n':
765 return -2;
766 case 0:
767 (*string_ptr)--;
768 return 0;
769 case '^':
770 c = *(*string_ptr)++;
771 if (c == '\\')
772 c = parse_escape (string_ptr);
773 if (c == '?')
774 return 0177;
775 return (c & 0200) | (c & 037);
776
777 case '0':
778 case '1':
779 case '2':
780 case '3':
781 case '4':
782 case '5':
783 case '6':
784 case '7':
785 {
786 register int i = c - '0';
787 register int count = 0;
788 while (++count < 3)
789 {
790 if ((c = *(*string_ptr)++) >= '0' && c <= '7')
791 {
792 i *= 8;
793 i += c - '0';
794 }
795 else
796 {
797 (*string_ptr)--;
798 break;
799 }
800 }
801 return i;
802 }
803 default:
804 return c;
805 }
806}
807\f
088c3a0b 808/* Print the character C on STREAM as part of the contents
bd5635a1
RP
809 of a literal string whose delimiter is QUOTER. */
810
811void
088c3a0b
JG
812printchar (c, stream, quoter)
813 register int c;
bd5635a1
RP
814 FILE *stream;
815 int quoter;
816{
bd5635a1 817
fcdb113e
JG
818 if ( c < 0x20 || /* Low control chars */
819 (c >= 0x7F && c < 0xA0) || /* DEL, High controls */
820 (sevenbit_strings && c >= 0x80)) { /* high order bit set */
bd5635a1
RP
821 switch (c)
822 {
823 case '\n':
824 fputs_filtered ("\\n", stream);
825 break;
826 case '\b':
827 fputs_filtered ("\\b", stream);
828 break;
829 case '\t':
830 fputs_filtered ("\\t", stream);
831 break;
832 case '\f':
833 fputs_filtered ("\\f", stream);
834 break;
835 case '\r':
836 fputs_filtered ("\\r", stream);
837 break;
838 case '\033':
839 fputs_filtered ("\\e", stream);
840 break;
841 case '\007':
842 fputs_filtered ("\\a", stream);
843 break;
844 default:
845 fprintf_filtered (stream, "\\%.3o", (unsigned int) c);
846 break;
847 }
2bc2e684
FF
848 } else {
849 if (c == '\\' || c == quoter)
850 fputs_filtered ("\\", stream);
851 fprintf_filtered (stream, "%c", c);
852 }
bd5635a1
RP
853}
854\f
855/* Number of lines per page or UINT_MAX if paging is disabled. */
856static unsigned int lines_per_page;
857/* Number of chars per line or UNIT_MAX is line folding is disabled. */
858static unsigned int chars_per_line;
859/* Current count of lines printed on this page, chars on this line. */
860static unsigned int lines_printed, chars_printed;
861
862/* Buffer and start column of buffered text, for doing smarter word-
863 wrapping. When someone calls wrap_here(), we start buffering output
864 that comes through fputs_filtered(). If we see a newline, we just
865 spit it out and forget about the wrap_here(). If we see another
866 wrap_here(), we spit it out and remember the newer one. If we see
867 the end of the line, we spit out a newline, the indent, and then
868 the buffered output.
869
870 wrap_column is the column number on the screen where wrap_buffer begins.
871 When wrap_column is zero, wrapping is not in effect.
872 wrap_buffer is malloc'd with chars_per_line+2 bytes.
873 When wrap_buffer[0] is null, the buffer is empty.
874 wrap_pointer points into it at the next character to fill.
875 wrap_indent is the string that should be used as indentation if the
876 wrap occurs. */
877
878static char *wrap_buffer, *wrap_pointer, *wrap_indent;
879static int wrap_column;
880
e1ce8aa5 881/* ARGSUSED */
bd5635a1
RP
882static void
883set_width_command (args, from_tty, c)
884 char *args;
885 int from_tty;
886 struct cmd_list_element *c;
887{
888 if (!wrap_buffer)
889 {
890 wrap_buffer = (char *) xmalloc (chars_per_line + 2);
891 wrap_buffer[0] = '\0';
892 }
893 else
894 wrap_buffer = (char *) xrealloc (wrap_buffer, chars_per_line + 2);
895 wrap_pointer = wrap_buffer; /* Start it at the beginning */
896}
897
898static void
899prompt_for_continue ()
900{
351b221d
JG
901 char *ignore;
902
bd5635a1 903 immediate_quit++;
351b221d
JG
904 ignore = gdb_readline ("---Type <return> to continue---");
905 if (ignore)
906 free (ignore);
bd5635a1
RP
907 chars_printed = lines_printed = 0;
908 immediate_quit--;
351b221d 909 dont_repeat (); /* Forget prev cmd -- CR won't repeat it. */
bd5635a1
RP
910}
911
912/* Reinitialize filter; ie. tell it to reset to original values. */
913
914void
915reinitialize_more_filter ()
916{
917 lines_printed = 0;
918 chars_printed = 0;
919}
920
921/* Indicate that if the next sequence of characters overflows the line,
922 a newline should be inserted here rather than when it hits the end.
923 If INDENT is nonzero, it is a string to be printed to indent the
924 wrapped part on the next line. INDENT must remain accessible until
925 the next call to wrap_here() or until a newline is printed through
926 fputs_filtered().
927
928 If the line is already overfull, we immediately print a newline and
929 the indentation, and disable further wrapping.
930
2bc2e684
FF
931 If we don't know the width of lines, but we know the page height,
932 we must not wrap words, but should still keep track of newlines
933 that were explicitly printed.
934
bd5635a1
RP
935 INDENT should not contain tabs, as that
936 will mess up the char count on the next line. FIXME. */
937
938void
939wrap_here(indent)
940 char *indent;
941{
942 if (wrap_buffer[0])
943 {
944 *wrap_pointer = '\0';
945 fputs (wrap_buffer, stdout);
946 }
947 wrap_pointer = wrap_buffer;
948 wrap_buffer[0] = '\0';
2bc2e684
FF
949 if (chars_per_line == UINT_MAX) /* No line overflow checking */
950 {
951 wrap_column = 0;
952 }
953 else if (chars_printed >= chars_per_line)
bd5635a1
RP
954 {
955 puts_filtered ("\n");
956 puts_filtered (indent);
957 wrap_column = 0;
958 }
959 else
960 {
961 wrap_column = chars_printed;
962 wrap_indent = indent;
963 }
964}
965
966/* Like fputs but pause after every screenful, and can wrap at points
967 other than the final character of a line.
968 Unlike fputs, fputs_filtered does not return a value.
969 It is OK for LINEBUFFER to be NULL, in which case just don't print
970 anything.
971
972 Note that a longjmp to top level may occur in this routine
973 (since prompt_for_continue may do so) so this routine should not be
974 called when cleanups are not in place. */
975
976void
977fputs_filtered (linebuffer, stream)
088c3a0b 978 const char *linebuffer;
bd5635a1
RP
979 FILE *stream;
980{
7919c3ed 981 const char *lineptr;
bd5635a1
RP
982
983 if (linebuffer == 0)
984 return;
985
986 /* Don't do any filtering if it is disabled. */
987 if (stream != stdout
988 || (lines_per_page == UINT_MAX && chars_per_line == UINT_MAX))
989 {
990 fputs (linebuffer, stream);
991 return;
992 }
993
994 /* Go through and output each character. Show line extension
995 when this is necessary; prompt user for new page when this is
996 necessary. */
997
998 lineptr = linebuffer;
999 while (*lineptr)
1000 {
1001 /* Possible new page. */
1002 if (lines_printed >= lines_per_page - 1)
1003 prompt_for_continue ();
1004
1005 while (*lineptr && *lineptr != '\n')
1006 {
1007 /* Print a single line. */
1008 if (*lineptr == '\t')
1009 {
1010 if (wrap_column)
1011 *wrap_pointer++ = '\t';
1012 else
1013 putc ('\t', stream);
1014 /* Shifting right by 3 produces the number of tab stops
1015 we have already passed, and then adding one and
1016 shifting left 3 advances to the next tab stop. */
1017 chars_printed = ((chars_printed >> 3) + 1) << 3;
1018 lineptr++;
1019 }
1020 else
1021 {
1022 if (wrap_column)
1023 *wrap_pointer++ = *lineptr;
1024 else
1025 putc (*lineptr, stream);
1026 chars_printed++;
1027 lineptr++;
1028 }
1029
1030 if (chars_printed >= chars_per_line)
1031 {
1032 unsigned int save_chars = chars_printed;
1033
1034 chars_printed = 0;
1035 lines_printed++;
1036 /* If we aren't actually wrapping, don't output newline --
1037 if chars_per_line is right, we probably just overflowed
1038 anyway; if it's wrong, let us keep going. */
1039 if (wrap_column)
1040 putc ('\n', stream);
1041
1042 /* Possible new page. */
1043 if (lines_printed >= lines_per_page - 1)
1044 prompt_for_continue ();
1045
1046 /* Now output indentation and wrapped string */
1047 if (wrap_column)
1048 {
1049 if (wrap_indent)
1050 fputs (wrap_indent, stream);
1051 *wrap_pointer = '\0'; /* Null-terminate saved stuff */
1052 fputs (wrap_buffer, stream); /* and eject it */
1053 /* FIXME, this strlen is what prevents wrap_indent from
1054 containing tabs. However, if we recurse to print it
1055 and count its chars, we risk trouble if wrap_indent is
1056 longer than (the user settable) chars_per_line.
1057 Note also that this can set chars_printed > chars_per_line
1058 if we are printing a long string. */
1059 chars_printed = strlen (wrap_indent)
1060 + (save_chars - wrap_column);
1061 wrap_pointer = wrap_buffer; /* Reset buffer */
1062 wrap_buffer[0] = '\0';
1063 wrap_column = 0; /* And disable fancy wrap */
1064 }
1065 }
1066 }
1067
1068 if (*lineptr == '\n')
1069 {
1070 chars_printed = 0;
d11c44f1 1071 wrap_here ((char *)0); /* Spit out chars, cancel further wraps */
bd5635a1
RP
1072 lines_printed++;
1073 putc ('\n', stream);
1074 lineptr++;
1075 }
1076 }
1077}
1078
1079
1080/* fputs_demangled is a variant of fputs_filtered that
1081 demangles g++ names.*/
1082
1083void
1084fputs_demangled (linebuffer, stream, arg_mode)
1085 char *linebuffer;
1086 FILE *stream;
1087 int arg_mode;
1088{
bd5635a1
RP
1089#define SYMBOL_MAX 1024
1090
f88e7af8
JK
1091#define SYMBOL_CHAR(c) (isascii(c) \
1092 && (isalnum(c) || (c) == '_' || (c) == CPLUS_MARKER))
bd5635a1
RP
1093
1094 char buf[SYMBOL_MAX+1];
1095# define SLOP 5 /* How much room to leave in buf */
1096 char *p;
1097
1098 if (linebuffer == NULL)
1099 return;
1100
1101 /* If user wants to see raw output, no problem. */
1102 if (!demangle) {
1103 fputs_filtered (linebuffer, stream);
bdbd5f50 1104 return;
bd5635a1
RP
1105 }
1106
1107 p = linebuffer;
1108
1109 while ( *p != (char) 0 ) {
1110 int i = 0;
1111
1112 /* collect non-interesting characters into buf */
1113 while ( *p != (char) 0 && !SYMBOL_CHAR(*p) && i < (int)sizeof(buf)-SLOP ) {
1114 buf[i++] = *p;
1115 p++;
1116 }
1117 if (i > 0) {
1118 /* output the non-interesting characters without demangling */
1119 buf[i] = (char) 0;
1120 fputs_filtered(buf, stream);
1121 i = 0; /* reset buf */
1122 }
1123
1124 /* and now the interesting characters */
1125 while (i < SYMBOL_MAX
1126 && *p != (char) 0
1127 && SYMBOL_CHAR(*p)
1128 && i < (int)sizeof(buf) - SLOP) {
1129 buf[i++] = *p;
1130 p++;
1131 }
1132 buf[i] = (char) 0;
1133 if (i > 0) {
1134 char * result;
1135
1136 if ( (result = cplus_demangle(buf, arg_mode)) != NULL ) {
1137 fputs_filtered(result, stream);
1138 free(result);
1139 }
1140 else {
1141 fputs_filtered(buf, stream);
1142 }
1143 }
1144 }
1145}
1146
1147/* Print a variable number of ARGS using format FORMAT. If this
1148 information is going to put the amount written (since the last call
1149 to INITIALIZE_MORE_FILTER or the last page break) over the page size,
1150 print out a pause message and do a gdb_readline to get the users
1151 permision to continue.
1152
1153 Unlike fprintf, this function does not return a value.
1154
1155 We implement three variants, vfprintf (takes a vararg list and stream),
1156 fprintf (takes a stream to write on), and printf (the usual).
1157
1158 Note that this routine has a restriction that the length of the
1159 final output line must be less than 255 characters *or* it must be
1160 less than twice the size of the format string. This is a very
1161 arbitrary restriction, but it is an internal restriction, so I'll
1162 put it in. This means that the %s format specifier is almost
1163 useless; unless the caller can GUARANTEE that the string is short
1164 enough, fputs_filtered should be used instead.
1165
1166 Note also that a longjmp to top level may occur in this routine
1167 (since prompt_for_continue may do so) so this routine should not be
1168 called when cleanups are not in place. */
1169
7919c3ed 1170static void
bd5635a1 1171vfprintf_filtered (stream, format, args)
bd5635a1
RP
1172 FILE *stream;
1173 char *format;
7919c3ed 1174 va_list args;
bd5635a1
RP
1175{
1176 static char *linebuffer = (char *) 0;
1177 static int line_size;
1178 int format_length;
1179
1180 format_length = strlen (format);
1181
1182 /* Allocated linebuffer for the first time. */
1183 if (!linebuffer)
1184 {
1185 linebuffer = (char *) xmalloc (255);
1186 line_size = 255;
1187 }
1188
1189 /* Reallocate buffer to a larger size if this is necessary. */
1190 if (format_length * 2 > line_size)
1191 {
1192 line_size = format_length * 2;
1193
1194 /* You don't have to copy. */
1195 free (linebuffer);
1196 linebuffer = (char *) xmalloc (line_size);
1197 }
1198
1199
1200 /* This won't blow up if the restrictions described above are
1201 followed. */
bd5635a1 1202 (void) vsprintf (linebuffer, format, args);
bd5635a1
RP
1203
1204 fputs_filtered (linebuffer, stream);
1205}
1206
bd5635a1
RP
1207/* VARARGS */
1208void
1209fprintf_filtered (va_alist)
1210 va_dcl
1211{
bd5635a1
RP
1212 FILE *stream;
1213 char *format;
7919c3ed 1214 va_list args;
bd5635a1
RP
1215
1216 va_start (args);
1217 stream = va_arg (args, FILE *);
1218 format = va_arg (args, char *);
1219
1220 /* This won't blow up if the restrictions described above are
1221 followed. */
7919c3ed 1222 vfprintf_filtered (stream, format, args);
bd5635a1
RP
1223 va_end (args);
1224}
1225
1226/* VARARGS */
1227void
1228printf_filtered (va_alist)
1229 va_dcl
1230{
1231 va_list args;
1232 char *format;
1233
1234 va_start (args);
1235 format = va_arg (args, char *);
1236
7919c3ed 1237 vfprintf_filtered (stdout, format, args);
bd5635a1
RP
1238 va_end (args);
1239}
bd5635a1
RP
1240
1241/* Easy */
1242
1243void
1244puts_filtered (string)
1245 char *string;
1246{
1247 fputs_filtered (string, stdout);
1248}
1249
1250/* Return a pointer to N spaces and a null. The pointer is good
1251 until the next call to here. */
1252char *
1253n_spaces (n)
1254 int n;
1255{
1256 register char *t;
1257 static char *spaces;
1258 static int max_spaces;
1259
1260 if (n > max_spaces)
1261 {
1262 if (spaces)
1263 free (spaces);
3624c875 1264 spaces = (char *) xmalloc (n+1);
bd5635a1
RP
1265 for (t = spaces+n; t != spaces;)
1266 *--t = ' ';
1267 spaces[n] = '\0';
1268 max_spaces = n;
1269 }
1270
1271 return spaces + max_spaces - n;
1272}
1273
1274/* Print N spaces. */
1275void
1276print_spaces_filtered (n, stream)
1277 int n;
1278 FILE *stream;
1279{
1280 fputs_filtered (n_spaces (n), stream);
1281}
1282\f
1283/* C++ demangler stuff. */
bd5635a1
RP
1284
1285/* Print NAME on STREAM, demangling if necessary. */
1286void
1287fprint_symbol (stream, name)
1288 FILE *stream;
1289 char *name;
1290{
1291 char *demangled;
1292 if ((!demangle) || NULL == (demangled = cplus_demangle (name, 1)))
1293 fputs_filtered (name, stream);
1294 else
1295 {
1296 fputs_filtered (demangled, stream);
1297 free (demangled);
1298 }
1299}
1300\f
bd5635a1
RP
1301void
1302_initialize_utils ()
1303{
1304 struct cmd_list_element *c;
1305
1306 c = add_set_cmd ("width", class_support, var_uinteger,
1307 (char *)&chars_per_line,
1308 "Set number of characters gdb thinks are in a line.",
1309 &setlist);
1310 add_show_from_set (c, &showlist);
d747e0af 1311 c->function.sfunc = set_width_command;
bd5635a1
RP
1312
1313 add_show_from_set
1314 (add_set_cmd ("height", class_support,
1315 var_uinteger, (char *)&lines_per_page,
1316 "Set number of lines gdb thinks are in a page.", &setlist),
1317 &showlist);
1318
1319 /* These defaults will be used if we are unable to get the correct
1320 values from termcap. */
1321 lines_per_page = 24;
1322 chars_per_line = 80;
1323 /* Initialize the screen height and width from termcap. */
1324 {
1325 char *termtype = getenv ("TERM");
1326
1327 /* Positive means success, nonpositive means failure. */
1328 int status;
1329
1330 /* 2048 is large enough for all known terminals, according to the
1331 GNU termcap manual. */
1332 char term_buffer[2048];
1333
1334 if (termtype)
1335 {
1336 status = tgetent (term_buffer, termtype);
1337 if (status > 0)
1338 {
1339 int val;
1340
1341 val = tgetnum ("li");
1342 if (val >= 0)
1343 lines_per_page = val;
1344 else
1345 /* The number of lines per page is not mentioned
1346 in the terminal description. This probably means
1347 that paging is not useful (e.g. emacs shell window),
1348 so disable paging. */
1349 lines_per_page = UINT_MAX;
1350
1351 val = tgetnum ("co");
1352 if (val >= 0)
1353 chars_per_line = val;
1354 }
1355 }
1356 }
1357
1eeba686
PB
1358#if defined(SIGWINCH) && defined(SIGWINCH_HANDLER)
1359
4ace50a5 1360 /* If there is a better way to determine the window size, use it. */
1eeba686
PB
1361 SIGWINCH_HANDLER ();
1362#endif
1363
2bc2e684
FF
1364 /* If the output is not a terminal, don't paginate it. */
1365 if (!ISATTY (stdout))
1366 lines_per_page = UINT_MAX;
1367
bd5635a1
RP
1368 set_width_command ((char *)NULL, 0, c);
1369
1370 add_show_from_set
1371 (add_set_cmd ("demangle", class_support, var_boolean,
1372 (char *)&demangle,
1373 "Set demangling of encoded C++ names when displaying symbols.",
f266e564
JK
1374 &setprintlist),
1375 &showprintlist);
bd5635a1
RP
1376
1377 add_show_from_set
1378 (add_set_cmd ("sevenbit-strings", class_support, var_boolean,
1379 (char *)&sevenbit_strings,
1380 "Set printing of 8-bit characters in strings as \\nnn.",
f266e564
JK
1381 &setprintlist),
1382 &showprintlist);
bd5635a1
RP
1383
1384 add_show_from_set
1385 (add_set_cmd ("asm-demangle", class_support, var_boolean,
1386 (char *)&asm_demangle,
1387 "Set demangling of C++ names in disassembly listings.",
f266e564
JK
1388 &setprintlist),
1389 &showprintlist);
bd5635a1 1390}
1eeba686
PB
1391
1392/* Machine specific function to handle SIGWINCH signal. */
1393
1394#ifdef SIGWINCH_HANDLER_BODY
1395 SIGWINCH_HANDLER_BODY
1396#endif
This page took 0.173318 seconds and 4 git commands to generate.