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