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