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