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