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