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