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