*** empty log message ***
[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 mtrace ();
276 }
277 #endif /* Have mcheck(). */
278
279 /* Like malloc but get error if no storage available. */
280
281 #ifdef __STDC__
282 void *
283 #else
284 char *
285 #endif
286 xmalloc (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__
305 void *
306 #else
307 char *
308 #endif
309 xrealloc (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
323 void
324 perror_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
354 void
355 print_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
379 void
380 quit ()
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
397 void
398 request_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
414 int
415 myread (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
440 char *
441 savestring (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
451 char *
452 strsave (ptr)
453 char *ptr;
454 {
455 return savestring (ptr, strlen (ptr));
456 }
457
458 char *
459 concat (s1, s2, s3)
460 char *s1, *s2, *s3;
461 {
462 register int len = strlen (s1) + strlen (s2) + strlen (s3) + 1;
463 register char *val = (char *) xmalloc (len);
464 strcpy (val, s1);
465 strcat (val, s2);
466 strcat (val, s3);
467 return val;
468 }
469
470 void
471 print_spaces (n, file)
472 register int n;
473 register FILE *file;
474 {
475 while (n-- > 0)
476 fputc (' ', file);
477 }
478
479 /* Ask user a y-or-n question and return 1 iff answer is yes.
480 Takes three args which are given to printf to print the question.
481 The first, a control string, should end in "? ".
482 It should not say how to answer, because we do that. */
483
484 /* VARARGS */
485 int
486 query (va_alist)
487 va_dcl
488 {
489 va_list args;
490 char *ctlstr;
491 register int answer;
492 register int ans2;
493
494 /* Automatically answer "yes" if input is not from a terminal. */
495 if (!input_from_terminal_p ())
496 return 1;
497
498 while (1)
499 {
500 va_start (args);
501 ctlstr = va_arg (args, char *);
502 vfprintf (stdout, ctlstr, args);
503 va_end (args);
504 printf ("(y or n) ");
505 fflush (stdout);
506 answer = fgetc (stdin);
507 clearerr (stdin); /* in case of C-d */
508 if (answer == EOF) /* C-d */
509 return 1;
510 if (answer != '\n') /* Eat rest of input line, to EOF or newline */
511 do
512 {
513 ans2 = fgetc (stdin);
514 clearerr (stdin);
515 }
516 while (ans2 != EOF && ans2 != '\n');
517 if (answer >= 'a')
518 answer -= 040;
519 if (answer == 'Y')
520 return 1;
521 if (answer == 'N')
522 return 0;
523 printf ("Please answer y or n.\n");
524 }
525 }
526 \f
527 /* Parse a C escape sequence. STRING_PTR points to a variable
528 containing a pointer to the string to parse. That pointer
529 should point to the character after the \. That pointer
530 is updated past the characters we use. The value of the
531 escape sequence is returned.
532
533 A negative value means the sequence \ newline was seen,
534 which is supposed to be equivalent to nothing at all.
535
536 If \ is followed by a null character, we return a negative
537 value and leave the string pointer pointing at the null character.
538
539 If \ is followed by 000, we return 0 and leave the string pointer
540 after the zeros. A value of 0 does not mean end of string. */
541
542 int
543 parse_escape (string_ptr)
544 char **string_ptr;
545 {
546 register int c = *(*string_ptr)++;
547 switch (c)
548 {
549 case 'a':
550 return '\a';
551 case 'b':
552 return '\b';
553 case 'e':
554 return 033;
555 case 'f':
556 return '\f';
557 case 'n':
558 return '\n';
559 case 'r':
560 return '\r';
561 case 't':
562 return '\t';
563 case 'v':
564 return '\v';
565 case '\n':
566 return -2;
567 case 0:
568 (*string_ptr)--;
569 return 0;
570 case '^':
571 c = *(*string_ptr)++;
572 if (c == '\\')
573 c = parse_escape (string_ptr);
574 if (c == '?')
575 return 0177;
576 return (c & 0200) | (c & 037);
577
578 case '0':
579 case '1':
580 case '2':
581 case '3':
582 case '4':
583 case '5':
584 case '6':
585 case '7':
586 {
587 register int i = c - '0';
588 register int count = 0;
589 while (++count < 3)
590 {
591 if ((c = *(*string_ptr)++) >= '0' && c <= '7')
592 {
593 i *= 8;
594 i += c - '0';
595 }
596 else
597 {
598 (*string_ptr)--;
599 break;
600 }
601 }
602 return i;
603 }
604 default:
605 return c;
606 }
607 }
608 \f
609 /* Print the character CH on STREAM as part of the contents
610 of a literal string whose delimiter is QUOTER. */
611
612 void
613 printchar (ch, stream, quoter)
614 unsigned char ch;
615 FILE *stream;
616 int quoter;
617 {
618 register int c = ch;
619
620 if (c < 040 || (sevenbit_strings && c >= 0177))
621 switch (c)
622 {
623 case '\n':
624 fputs_filtered ("\\n", stream);
625 break;
626 case '\b':
627 fputs_filtered ("\\b", stream);
628 break;
629 case '\t':
630 fputs_filtered ("\\t", stream);
631 break;
632 case '\f':
633 fputs_filtered ("\\f", stream);
634 break;
635 case '\r':
636 fputs_filtered ("\\r", stream);
637 break;
638 case '\033':
639 fputs_filtered ("\\e", stream);
640 break;
641 case '\007':
642 fputs_filtered ("\\a", stream);
643 break;
644 default:
645 fprintf_filtered (stream, "\\%.3o", (unsigned int) c);
646 break;
647 }
648 else
649 {
650 if (c == '\\' || c == quoter)
651 fputs_filtered ("\\", stream);
652 fprintf_filtered (stream, "%c", c);
653 }
654 }
655 \f
656 /* Number of lines per page or UINT_MAX if paging is disabled. */
657 static unsigned int lines_per_page;
658 /* Number of chars per line or UNIT_MAX is line folding is disabled. */
659 static unsigned int chars_per_line;
660 /* Current count of lines printed on this page, chars on this line. */
661 static unsigned int lines_printed, chars_printed;
662
663 /* Buffer and start column of buffered text, for doing smarter word-
664 wrapping. When someone calls wrap_here(), we start buffering output
665 that comes through fputs_filtered(). If we see a newline, we just
666 spit it out and forget about the wrap_here(). If we see another
667 wrap_here(), we spit it out and remember the newer one. If we see
668 the end of the line, we spit out a newline, the indent, and then
669 the buffered output.
670
671 wrap_column is the column number on the screen where wrap_buffer begins.
672 When wrap_column is zero, wrapping is not in effect.
673 wrap_buffer is malloc'd with chars_per_line+2 bytes.
674 When wrap_buffer[0] is null, the buffer is empty.
675 wrap_pointer points into it at the next character to fill.
676 wrap_indent is the string that should be used as indentation if the
677 wrap occurs. */
678
679 static char *wrap_buffer, *wrap_pointer, *wrap_indent;
680 static int wrap_column;
681
682 /* Get the number of lines to print with commands like "list".
683 This is based on guessing how many long (i.e. more than chars_per_line
684 characters) lines there will be. To be completely correct, "list"
685 and friends should be rewritten to count characters and see where
686 things are wrapping, but that would be a fair amount of work. */
687 int
688 lines_to_list ()
689 {
690 /* RMS didn't like the following algorithm. Let's set it back to
691 10 and see if anyone else complains. */
692 /* return lines_per_page == UINT_MAX ? 10 : lines_per_page / 2; */
693 return 10;
694 }
695
696 static void
697 set_width_command (args, from_tty, c)
698 char *args;
699 int from_tty;
700 struct cmd_list_element *c;
701 {
702 if (!wrap_buffer)
703 {
704 wrap_buffer = (char *) xmalloc (chars_per_line + 2);
705 wrap_buffer[0] = '\0';
706 }
707 else
708 wrap_buffer = (char *) xrealloc (wrap_buffer, chars_per_line + 2);
709 wrap_pointer = wrap_buffer; /* Start it at the beginning */
710 }
711
712 static void
713 prompt_for_continue ()
714 {
715 immediate_quit++;
716 gdb_readline ("---Type <return> to continue---", 0);
717 chars_printed = lines_printed = 0;
718 immediate_quit--;
719 }
720
721 /* Reinitialize filter; ie. tell it to reset to original values. */
722
723 void
724 reinitialize_more_filter ()
725 {
726 lines_printed = 0;
727 chars_printed = 0;
728 }
729
730 /* Indicate that if the next sequence of characters overflows the line,
731 a newline should be inserted here rather than when it hits the end.
732 If INDENT is nonzero, it is a string to be printed to indent the
733 wrapped part on the next line. INDENT must remain accessible until
734 the next call to wrap_here() or until a newline is printed through
735 fputs_filtered().
736
737 If the line is already overfull, we immediately print a newline and
738 the indentation, and disable further wrapping.
739
740 INDENT should not contain tabs, as that
741 will mess up the char count on the next line. FIXME. */
742
743 void
744 wrap_here(indent)
745 char *indent;
746 {
747 if (wrap_buffer[0])
748 {
749 *wrap_pointer = '\0';
750 fputs (wrap_buffer, stdout);
751 }
752 wrap_pointer = wrap_buffer;
753 wrap_buffer[0] = '\0';
754 if (chars_printed >= chars_per_line)
755 {
756 puts_filtered ("\n");
757 puts_filtered (indent);
758 wrap_column = 0;
759 }
760 else
761 {
762 wrap_column = chars_printed;
763 wrap_indent = indent;
764 }
765 }
766
767 /* Like fputs but pause after every screenful, and can wrap at points
768 other than the final character of a line.
769 Unlike fputs, fputs_filtered does not return a value.
770 It is OK for LINEBUFFER to be NULL, in which case just don't print
771 anything.
772
773 Note that a longjmp to top level may occur in this routine
774 (since prompt_for_continue may do so) so this routine should not be
775 called when cleanups are not in place. */
776
777 void
778 fputs_filtered (linebuffer, stream)
779 char *linebuffer;
780 FILE *stream;
781 {
782 char *lineptr;
783
784 if (linebuffer == 0)
785 return;
786
787 /* Don't do any filtering if it is disabled. */
788 if (stream != stdout
789 || (lines_per_page == UINT_MAX && chars_per_line == UINT_MAX))
790 {
791 fputs (linebuffer, stream);
792 return;
793 }
794
795 /* Go through and output each character. Show line extension
796 when this is necessary; prompt user for new page when this is
797 necessary. */
798
799 lineptr = linebuffer;
800 while (*lineptr)
801 {
802 /* Possible new page. */
803 if (lines_printed >= lines_per_page - 1)
804 prompt_for_continue ();
805
806 while (*lineptr && *lineptr != '\n')
807 {
808 /* Print a single line. */
809 if (*lineptr == '\t')
810 {
811 if (wrap_column)
812 *wrap_pointer++ = '\t';
813 else
814 putc ('\t', stream);
815 /* Shifting right by 3 produces the number of tab stops
816 we have already passed, and then adding one and
817 shifting left 3 advances to the next tab stop. */
818 chars_printed = ((chars_printed >> 3) + 1) << 3;
819 lineptr++;
820 }
821 else
822 {
823 if (wrap_column)
824 *wrap_pointer++ = *lineptr;
825 else
826 putc (*lineptr, stream);
827 chars_printed++;
828 lineptr++;
829 }
830
831 if (chars_printed >= chars_per_line)
832 {
833 unsigned int save_chars = chars_printed;
834
835 chars_printed = 0;
836 lines_printed++;
837 /* If we aren't actually wrapping, don't output newline --
838 if chars_per_line is right, we probably just overflowed
839 anyway; if it's wrong, let us keep going. */
840 if (wrap_column)
841 putc ('\n', stream);
842
843 /* Possible new page. */
844 if (lines_printed >= lines_per_page - 1)
845 prompt_for_continue ();
846
847 /* Now output indentation and wrapped string */
848 if (wrap_column)
849 {
850 if (wrap_indent)
851 fputs (wrap_indent, stream);
852 *wrap_pointer = '\0'; /* Null-terminate saved stuff */
853 fputs (wrap_buffer, stream); /* and eject it */
854 /* FIXME, this strlen is what prevents wrap_indent from
855 containing tabs. However, if we recurse to print it
856 and count its chars, we risk trouble if wrap_indent is
857 longer than (the user settable) chars_per_line.
858 Note also that this can set chars_printed > chars_per_line
859 if we are printing a long string. */
860 chars_printed = strlen (wrap_indent)
861 + (save_chars - wrap_column);
862 wrap_pointer = wrap_buffer; /* Reset buffer */
863 wrap_buffer[0] = '\0';
864 wrap_column = 0; /* And disable fancy wrap */
865 }
866 }
867 }
868
869 if (*lineptr == '\n')
870 {
871 chars_printed = 0;
872 wrap_here (""); /* Spit out chars, cancel further wraps */
873 lines_printed++;
874 putc ('\n', stream);
875 lineptr++;
876 }
877 }
878 }
879
880
881 /* fputs_demangled is a variant of fputs_filtered that
882 demangles g++ names.*/
883
884 void
885 fputs_demangled (linebuffer, stream, arg_mode)
886 char *linebuffer;
887 FILE *stream;
888 int arg_mode;
889 {
890 #ifdef __STDC__
891 extern char *cplus_demangle (const char *, int);
892 #else
893 extern char *cplus_demangle ();
894 #endif
895 #define SYMBOL_MAX 1024
896
897 #define SYMBOL_CHAR(c) (isascii(c) && (isalnum(c) || (c) == '_' || (c) == '$'))
898
899 char buf[SYMBOL_MAX+1];
900 # define SLOP 5 /* How much room to leave in buf */
901 char *p;
902
903 if (linebuffer == NULL)
904 return;
905
906 /* If user wants to see raw output, no problem. */
907 if (!demangle) {
908 fputs_filtered (linebuffer, stream);
909 }
910
911 p = linebuffer;
912
913 while ( *p != (char) 0 ) {
914 int i = 0;
915
916 /* collect non-interesting characters into buf */
917 while ( *p != (char) 0 && !SYMBOL_CHAR(*p) && i < (int)sizeof(buf)-SLOP ) {
918 buf[i++] = *p;
919 p++;
920 }
921 if (i > 0) {
922 /* output the non-interesting characters without demangling */
923 buf[i] = (char) 0;
924 fputs_filtered(buf, stream);
925 i = 0; /* reset buf */
926 }
927
928 /* and now the interesting characters */
929 while (i < SYMBOL_MAX
930 && *p != (char) 0
931 && SYMBOL_CHAR(*p)
932 && i < (int)sizeof(buf) - SLOP) {
933 buf[i++] = *p;
934 p++;
935 }
936 buf[i] = (char) 0;
937 if (i > 0) {
938 char * result;
939
940 if ( (result = cplus_demangle(buf, arg_mode)) != NULL ) {
941 fputs_filtered(result, stream);
942 free(result);
943 }
944 else {
945 fputs_filtered(buf, stream);
946 }
947 }
948 }
949 }
950
951 /* Print a variable number of ARGS using format FORMAT. If this
952 information is going to put the amount written (since the last call
953 to INITIALIZE_MORE_FILTER or the last page break) over the page size,
954 print out a pause message and do a gdb_readline to get the users
955 permision to continue.
956
957 Unlike fprintf, this function does not return a value.
958
959 We implement three variants, vfprintf (takes a vararg list and stream),
960 fprintf (takes a stream to write on), and printf (the usual).
961
962 Note that this routine has a restriction that the length of the
963 final output line must be less than 255 characters *or* it must be
964 less than twice the size of the format string. This is a very
965 arbitrary restriction, but it is an internal restriction, so I'll
966 put it in. This means that the %s format specifier is almost
967 useless; unless the caller can GUARANTEE that the string is short
968 enough, fputs_filtered should be used instead.
969
970 Note also that a longjmp to top level may occur in this routine
971 (since prompt_for_continue may do so) so this routine should not be
972 called when cleanups are not in place. */
973
974 #if !defined(MISSING_VPRINTF) || defined (vsprintf)
975 /* VARARGS */
976 void
977 vfprintf_filtered (stream, format, args)
978 va_list args;
979 #else
980 void fprintf_filtered (stream, format, arg1, arg2, arg3, arg4, arg5, arg6)
981 #endif
982 FILE *stream;
983 char *format;
984 {
985 static char *linebuffer = (char *) 0;
986 static int line_size;
987 int format_length;
988
989 format_length = strlen (format);
990
991 /* Allocated linebuffer for the first time. */
992 if (!linebuffer)
993 {
994 linebuffer = (char *) xmalloc (255);
995 line_size = 255;
996 }
997
998 /* Reallocate buffer to a larger size if this is necessary. */
999 if (format_length * 2 > line_size)
1000 {
1001 line_size = format_length * 2;
1002
1003 /* You don't have to copy. */
1004 free (linebuffer);
1005 linebuffer = (char *) xmalloc (line_size);
1006 }
1007
1008
1009 /* This won't blow up if the restrictions described above are
1010 followed. */
1011 #if !defined(MISSING_VPRINTF) || defined (vsprintf)
1012 (void) vsprintf (linebuffer, format, args);
1013 #else
1014 (void) sprintf (linebuffer, format, arg1, arg2, arg3, arg4, arg5, arg6);
1015 #endif
1016
1017 fputs_filtered (linebuffer, stream);
1018 }
1019
1020 #if !defined(MISSING_VPRINTF) || defined (vsprintf)
1021 /* VARARGS */
1022 void
1023 fprintf_filtered (va_alist)
1024 va_dcl
1025 {
1026 va_list args;
1027 FILE *stream;
1028 char *format;
1029
1030 va_start (args);
1031 stream = va_arg (args, FILE *);
1032 format = va_arg (args, char *);
1033
1034 /* This won't blow up if the restrictions described above are
1035 followed. */
1036 (void) vfprintf_filtered (stream, format, args);
1037 va_end (args);
1038 }
1039
1040 /* VARARGS */
1041 void
1042 printf_filtered (va_alist)
1043 va_dcl
1044 {
1045 va_list args;
1046 char *format;
1047
1048 va_start (args);
1049 format = va_arg (args, char *);
1050
1051 (void) vfprintf_filtered (stdout, format, args);
1052 va_end (args);
1053 }
1054 #else
1055 void
1056 printf_filtered (format, arg1, arg2, arg3, arg4, arg5, arg6)
1057 char *format;
1058 int arg1, arg2, arg3, arg4, arg5, arg6;
1059 {
1060 fprintf_filtered (stdout, format, arg1, arg2, arg3, arg4, arg5, arg6);
1061 }
1062 #endif
1063
1064 /* Easy */
1065
1066 void
1067 puts_filtered (string)
1068 char *string;
1069 {
1070 fputs_filtered (string, stdout);
1071 }
1072
1073 /* Return a pointer to N spaces and a null. The pointer is good
1074 until the next call to here. */
1075 char *
1076 n_spaces (n)
1077 int n;
1078 {
1079 register char *t;
1080 static char *spaces;
1081 static int max_spaces;
1082
1083 if (n > max_spaces)
1084 {
1085 if (spaces)
1086 free (spaces);
1087 spaces = malloc (n+1);
1088 for (t = spaces+n; t != spaces;)
1089 *--t = ' ';
1090 spaces[n] = '\0';
1091 max_spaces = n;
1092 }
1093
1094 return spaces + max_spaces - n;
1095 }
1096
1097 /* Print N spaces. */
1098 void
1099 print_spaces_filtered (n, stream)
1100 int n;
1101 FILE *stream;
1102 {
1103 fputs_filtered (n_spaces (n), stream);
1104 }
1105 \f
1106 /* C++ demangler stuff. */
1107 char *cplus_demangle ();
1108
1109 /* Print NAME on STREAM, demangling if necessary. */
1110 void
1111 fprint_symbol (stream, name)
1112 FILE *stream;
1113 char *name;
1114 {
1115 char *demangled;
1116 if ((!demangle) || NULL == (demangled = cplus_demangle (name, 1)))
1117 fputs_filtered (name, stream);
1118 else
1119 {
1120 fputs_filtered (demangled, stream);
1121 free (demangled);
1122 }
1123 }
1124 \f
1125 #if !defined (USG_UTILS)
1126 #define USG_UTILS defined (USG)
1127 #endif
1128
1129 #if USG_UTILS
1130 bcopy (from, to, count)
1131 char *from, *to;
1132 {
1133 memcpy (to, from, count);
1134 }
1135
1136 bcmp (from, to, count)
1137 {
1138 return (memcmp (to, from, count));
1139 }
1140
1141 bzero (to, count)
1142 char *to;
1143 {
1144 while (count--)
1145 *to++ = 0;
1146 }
1147
1148 getwd (buf)
1149 char *buf;
1150 {
1151 getcwd (buf, MAXPATHLEN);
1152 }
1153
1154 char *
1155 index (s, c)
1156 char *s;
1157 {
1158 char *strchr ();
1159 return strchr (s, c);
1160 }
1161
1162 char *
1163 rindex (s, c)
1164 char *s;
1165 {
1166 char *strrchr ();
1167 return strrchr (s, c);
1168 }
1169 #endif /* USG_UTILS. */
1170
1171 #if !defined (QUEUE_MISSING)
1172 #define QUEUE_MISSING defined (USG)
1173 #endif
1174
1175 #if QUEUE_MISSING
1176 /* Queue routines */
1177
1178 struct queue {
1179 struct queue *forw;
1180 struct queue *back;
1181 };
1182
1183 insque (item, after)
1184 struct queue *item;
1185 struct queue *after;
1186 {
1187 item->forw = after->forw;
1188 after->forw->back = item;
1189
1190 item->back = after;
1191 after->forw = item;
1192 }
1193
1194 remque (item)
1195 struct queue *item;
1196 {
1197 item->forw->back = item->back;
1198 item->back->forw = item->forw;
1199 }
1200 #endif /* QUEUE_MISSING */
1201 \f
1202 /* Simple implementation of strstr, since some implementations lack it. */
1203 char *
1204 strstr (in, find)
1205 const char *in, *find;
1206 {
1207 register char *p = in - 1;
1208
1209 while (0 != (p = strchr (p+1, *find))) {
1210 if (strcmp (p, find))
1211 return p;
1212 }
1213 return 0;
1214 }
1215 \f
1216 void
1217 _initialize_utils ()
1218 {
1219 struct cmd_list_element *c;
1220
1221 c = add_set_cmd ("width", class_support, var_uinteger,
1222 (char *)&chars_per_line,
1223 "Set number of characters gdb thinks are in a line.",
1224 &setlist);
1225 add_show_from_set (c, &showlist);
1226 c->function = set_width_command;
1227
1228 add_show_from_set
1229 (add_set_cmd ("height", class_support,
1230 var_uinteger, (char *)&lines_per_page,
1231 "Set number of lines gdb thinks are in a page.", &setlist),
1232 &showlist);
1233
1234 /* These defaults will be used if we are unable to get the correct
1235 values from termcap. */
1236 lines_per_page = 24;
1237 chars_per_line = 80;
1238 /* Initialize the screen height and width from termcap. */
1239 {
1240 char *termtype = getenv ("TERM");
1241
1242 /* Positive means success, nonpositive means failure. */
1243 int status;
1244
1245 /* 2048 is large enough for all known terminals, according to the
1246 GNU termcap manual. */
1247 char term_buffer[2048];
1248
1249 if (termtype)
1250 {
1251 status = tgetent (term_buffer, termtype);
1252 if (status > 0)
1253 {
1254 int val;
1255
1256 val = tgetnum ("li");
1257 if (val >= 0)
1258 lines_per_page = val;
1259 else
1260 /* The number of lines per page is not mentioned
1261 in the terminal description. This probably means
1262 that paging is not useful (e.g. emacs shell window),
1263 so disable paging. */
1264 lines_per_page = UINT_MAX;
1265
1266 val = tgetnum ("co");
1267 if (val >= 0)
1268 chars_per_line = val;
1269 }
1270 }
1271 }
1272
1273 set_width_command ((char *)NULL, 0, c);
1274
1275 add_show_from_set
1276 (add_set_cmd ("demangle", class_support, var_boolean,
1277 (char *)&demangle,
1278 "Set demangling of encoded C++ names when displaying symbols.",
1279 &setprintlist),
1280 &showprintlist);
1281
1282 add_show_from_set
1283 (add_set_cmd ("sevenbit-strings", class_support, var_boolean,
1284 (char *)&sevenbit_strings,
1285 "Set printing of 8-bit characters in strings as \\nnn.",
1286 &setprintlist),
1287 &showprintlist);
1288
1289 add_show_from_set
1290 (add_set_cmd ("asm-demangle", class_support, var_boolean,
1291 (char *)&asm_demangle,
1292 "Set demangling of C++ names in disassembly listings.",
1293 &setprintlist),
1294 &showprintlist);
1295 }
This page took 0.054313 seconds and 4 git commands to generate.