1 /* General utility routines for GDB, the GNU debugger.
2 Copyright 1986, 1989, 1990, 1991, 1992 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program 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 2 of the License, or
9 (at your option) any later version.
11 This program 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.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
21 #if !defined(__GO32__)
22 #include <sys/ioctl.h>
23 #include <sys/param.h>
34 /* No longer needed, I suspect. */
40 #include "expression.h"
43 /* Prototypes for local functions */
45 #if defined (NO_MMALLOC) || defined (NO_MMALLOC_CHECK)
49 malloc_botch PARAMS ((void));
51 #endif /* NO_MMALLOC, etc */
54 fatal_dump_core (); /* Can't prototype with <varargs.h> usage... */
57 prompt_for_continue PARAMS ((void));
60 set_width_command PARAMS ((char *, int, struct cmd_list_element *));
62 /* If this definition isn't overridden by the header files, assume
63 that isatty and fileno exist on this system. */
65 #define ISATTY(FP) (isatty (fileno (FP)))
68 /* Chain of cleanup actions established with make_cleanup,
69 to be executed if an error happens. */
71 static struct cleanup *cleanup_chain;
73 /* Nonzero means a quit has been requested. */
77 /* Nonzero means quit immediately if Control-C is typed now, rather
78 than waiting until QUIT is executed. Be careful in setting this;
79 code which executes with immediate_quit set has to be very careful
80 about being able to deal with being interrupted at any time. It is
81 almost always better to use QUIT; the only exception I can think of
82 is being able to quit out of a system call (using EINTR loses if
83 the SIGINT happens between the previous QUIT and the system call).
84 To immediately quit in the case in which a SIGINT happens between
85 the previous QUIT and setting immediate_quit (desirable anytime we
86 expect to block), call QUIT after setting immediate_quit. */
90 /* Nonzero means that encoded C++ names should be printed out in their
91 C++ form rather than raw. */
95 /* Nonzero means that encoded C++ names should be printed out in their
96 C++ form even in assembler language displays. If this is set, but
97 DEMANGLE is zero, names are printed raw, i.e. DEMANGLE controls. */
101 /* Nonzero means that strings with character values >0x7F should be printed
102 as octal escapes. Zero means just print the value (e.g. it's an
103 international character, and the terminal or window can cope.) */
105 int sevenbit_strings = 0;
107 /* String to be printed before error messages, if any. */
109 char *error_pre_print;
110 char *warning_pre_print = "\nwarning: ";
112 /* Add a new cleanup to the cleanup_chain,
113 and return the previous chain pointer
114 to be passed later to do_cleanups or discard_cleanups.
115 Args are FUNCTION to clean up with, and ARG to pass to it. */
118 make_cleanup (function, arg)
119 void (*function) PARAMS ((PTR));
122 register struct cleanup *new
123 = (struct cleanup *) xmalloc (sizeof (struct cleanup));
124 register struct cleanup *old_chain = cleanup_chain;
126 new->next = cleanup_chain;
127 new->function = function;
134 /* Discard cleanups and do the actions they describe
135 until we get back to the point OLD_CHAIN in the cleanup_chain. */
138 do_cleanups (old_chain)
139 register struct cleanup *old_chain;
141 register struct cleanup *ptr;
142 while ((ptr = cleanup_chain) != old_chain)
144 cleanup_chain = ptr->next; /* Do this first incase recursion */
145 (*ptr->function) (ptr->arg);
150 /* Discard cleanups, not doing the actions they describe,
151 until we get back to the point OLD_CHAIN in the cleanup_chain. */
154 discard_cleanups (old_chain)
155 register struct cleanup *old_chain;
157 register struct cleanup *ptr;
158 while ((ptr = cleanup_chain) != old_chain)
160 cleanup_chain = ptr->next;
165 /* Set the cleanup_chain to 0, and return the old cleanup chain. */
169 struct cleanup *old_chain = cleanup_chain;
175 /* Restore the cleanup chain from a previously saved chain. */
177 restore_cleanups (chain)
178 struct cleanup *chain;
180 cleanup_chain = chain;
183 /* This function is useful for cleanups.
187 old_chain = make_cleanup (free_current_contents, &foo);
189 to arrange to free the object thus allocated. */
192 free_current_contents (location)
198 /* Provide a known function that does nothing, to use as a base for
199 for a possibly long chain of cleanups. This is useful where we
200 use the cleanup chain for handling normal cleanups as well as dealing
201 with cleanups that need to be done as a result of a call to error().
202 In such cases, we may not be certain where the first cleanup is, unless
203 we have a do-nothing one to always use as the base. */
213 /* Provide a hook for modules wishing to print their own warning messages
214 to set up the terminal state in a compatible way, without them having
215 to import all the target_<...> macros. */
220 target_terminal_ours ();
221 wrap_here(""); /* Force out any buffered output */
225 /* Print a warning message.
226 The first argument STRING is the warning message, used as a fprintf string,
227 and the remaining args are passed as arguments to it.
228 The primary difference between warnings and errors is that a warning
229 does not force the return to command level. */
240 target_terminal_ours ();
241 wrap_here(""); /* Force out any buffered output */
243 if (warning_pre_print)
244 fprintf (stderr, warning_pre_print);
245 string = va_arg (args, char *);
246 vfprintf (stderr, string, args);
247 fprintf (stderr, "\n");
251 /* Print an error message and return to command level.
252 The first argument STRING is the error message, used as a fprintf string,
253 and the remaining args are passed as arguments to it. */
264 target_terminal_ours ();
265 wrap_here(""); /* Force out any buffered output */
268 fprintf_filtered (stderr, error_pre_print);
269 string = va_arg (args, char *);
270 vfprintf_filtered (stderr, string, args);
271 fprintf_filtered (stderr, "\n");
273 return_to_top_level (RETURN_ERROR);
276 /* Print an error message and exit reporting failure.
277 This is for a error that we cannot continue from.
278 The arguments are printed a la printf.
280 This function cannot be declared volatile (NORETURN) in an
281 ANSI environment because exit() is not declared volatile. */
292 string = va_arg (args, char *);
293 fprintf (stderr, "\ngdb: ");
294 vfprintf (stderr, string, args);
295 fprintf (stderr, "\n");
300 /* Print an error message and exit, dumping core.
301 The arguments are printed a la printf (). */
305 fatal_dump_core (va_alist)
312 string = va_arg (args, char *);
313 /* "internal error" is always correct, since GDB should never dump
314 core, no matter what the input. */
315 fprintf (stderr, "\ngdb internal error: ");
316 vfprintf (stderr, string, args);
317 fprintf (stderr, "\n");
320 signal (SIGQUIT, SIG_DFL);
321 kill (getpid (), SIGQUIT);
322 /* We should never get here, but just in case... */
326 /* The strerror() function can return NULL for errno values that are
327 out of range. Provide a "safe" version that always returns a
331 safe_strerror (errnum)
337 if ((msg = strerror (errnum)) == NULL)
339 sprintf (buf, "(undocumented errno %d)", errnum);
345 /* The strsignal() function can return NULL for signal values that are
346 out of range. Provide a "safe" version that always returns a
350 safe_strsignal (signo)
356 if ((msg = strsignal (signo)) == NULL)
358 sprintf (buf, "(undocumented signal %d)", signo);
365 /* Print the system error message for errno, and also mention STRING
366 as the file name for which the error was encountered.
367 Then return to command level. */
370 perror_with_name (string)
376 err = safe_strerror (errno);
377 combined = (char *) alloca (strlen (err) + strlen (string) + 3);
378 strcpy (combined, string);
379 strcat (combined, ": ");
380 strcat (combined, err);
382 /* I understand setting these is a matter of taste. Still, some people
383 may clear errno but not know about bfd_error. Doing this here is not
385 bfd_error = no_error;
388 error ("%s.", combined);
391 /* Print the system error message for ERRCODE, and also mention STRING
392 as the file name for which the error was encountered. */
395 print_sys_errmsg (string, errcode)
402 err = safe_strerror (errcode);
403 combined = (char *) alloca (strlen (err) + strlen (string) + 3);
404 strcpy (combined, string);
405 strcat (combined, ": ");
406 strcat (combined, err);
408 fprintf (stderr, "%s.\n", combined);
411 /* Control C eventually causes this to be called, at a convenient time. */
416 serial_t stdout_serial = serial_fdopen (1);
418 target_terminal_ours ();
419 wrap_here ((char *)0); /* Force out any pending output */
421 SERIAL_FLUSH_OUTPUT (stdout_serial);
423 SERIAL_UN_FDOPEN (stdout_serial);
425 /* Don't use *_filtered; we don't want to prompt the user to continue. */
427 fprintf (stderr, error_pre_print);
430 /* If there is no terminal switching for this target, then we can't
431 possibly get screwed by the lack of job control. */
432 || current_target->to_terminal_ours == NULL)
433 fprintf (stderr, "Quit\n");
436 "Quit (expect signal SIGINT when the program is resumed)\n");
437 return_to_top_level (RETURN_QUIT);
443 /* In the absence of signals, poll keyboard for a quit.
444 Called from #define QUIT pollquit() in xm-go32.h. */
462 /* Control C comes here */
471 /* Restore the signal handler. */
472 signal (signo, request_quit);
480 /* Memory management stuff (malloc friends). */
482 #if defined (NO_MMALLOC)
489 return (malloc (size));
493 mrealloc (md, ptr, size)
498 if (ptr == 0) /* Guard against old realloc's */
499 return malloc (size);
501 return realloc (ptr, size);
512 #endif /* NO_MMALLOC */
514 #if defined (NO_MMALLOC) || defined (NO_MMALLOC_CHECK)
522 #else /* have mmalloc and want corruption checking */
527 fatal_dump_core ("Memory corruption");
530 /* Attempt to install hooks in mmalloc/mrealloc/mfree for the heap specified
531 by MD, to detect memory corruption. Note that MD may be NULL to specify
532 the default heap that grows via sbrk.
534 Note that for freshly created regions, we must call mmcheck prior to any
535 mallocs in the region. Otherwise, any region which was allocated prior to
536 installing the checking hooks, which is later reallocated or freed, will
537 fail the checks! The mmcheck function only allows initial hooks to be
538 installed before the first mmalloc. However, anytime after we have called
539 mmcheck the first time to install the checking hooks, we can call it again
540 to update the function pointer to the memory corruption handler.
542 Returns zero on failure, non-zero on success. */
548 if (!mmcheck (md, malloc_botch))
550 warning ("internal error: failed to install memory consistency checks");
556 #endif /* Have mmalloc and want corruption checking */
558 /* Called when a memory allocation fails, with the number of bytes of
559 memory requested in SIZE. */
567 fatal ("virtual memory exhausted: can't allocate %ld bytes.", size);
571 fatal ("virtual memory exhausted.");
575 /* Like mmalloc but get error if no storage available, and protect against
576 the caller wanting to allocate zero bytes. Whether to return NULL for
577 a zero byte request, or translate the request into a request for one
578 byte of zero'd storage, is a religious issue. */
591 else if ((val = mmalloc (md, size)) == NULL)
598 /* Like mrealloc but get error if no storage available. */
601 xmrealloc (md, ptr, size)
610 val = mrealloc (md, ptr, size);
614 val = mmalloc (md, size);
623 /* Like malloc but get error if no storage available, and protect against
624 the caller wanting to allocate zero bytes. */
630 return (xmmalloc ((void *) NULL, size));
633 /* Like mrealloc but get error if no storage available. */
640 return (xmrealloc ((void *) NULL, ptr, size));
644 /* My replacement for the read system call.
645 Used like `read' but keeps going if `read' returns too soon. */
648 myread (desc, addr, len)
658 val = read (desc, addr, len);
669 /* Make a copy of the string at PTR with SIZE characters
670 (and add a null character at the end in the copy).
671 Uses malloc to get the space. Returns the address of the copy. */
674 savestring (ptr, size)
678 register char *p = (char *) xmalloc (size + 1);
679 memcpy (p, ptr, size);
685 msavestring (md, ptr, size)
690 register char *p = (char *) xmmalloc (md, size + 1);
691 memcpy (p, ptr, size);
696 /* The "const" is so it compiles under DGUX (which prototypes strsave
697 in <string.h>. FIXME: This should be named "xstrsave", shouldn't it?
698 Doesn't real strsave return NULL if out of memory? */
703 return savestring (ptr, strlen (ptr));
711 return (msavestring (md, ptr, strlen (ptr)));
715 print_spaces (n, file)
723 /* Ask user a y-or-n question and return 1 iff answer is yes.
724 Takes three args which are given to printf to print the question.
725 The first, a control string, should end in "? ".
726 It should not say how to answer, because we do that. */
738 /* Automatically answer "yes" if input is not from a terminal. */
739 if (!input_from_terminal_p ())
744 wrap_here (""); /* Flush any buffered output */
747 ctlstr = va_arg (args, char *);
748 vfprintf_filtered (stdout, ctlstr, args);
750 printf_filtered ("(y or n) ");
752 answer = fgetc (stdin);
753 clearerr (stdin); /* in case of C-d */
754 if (answer == EOF) /* C-d */
756 if (answer != '\n') /* Eat rest of input line, to EOF or newline */
759 ans2 = fgetc (stdin);
762 while (ans2 != EOF && ans2 != '\n');
769 printf_filtered ("Please answer y or n.\n");
774 /* Parse a C escape sequence. STRING_PTR points to a variable
775 containing a pointer to the string to parse. That pointer
776 should point to the character after the \. That pointer
777 is updated past the characters we use. The value of the
778 escape sequence is returned.
780 A negative value means the sequence \ newline was seen,
781 which is supposed to be equivalent to nothing at all.
783 If \ is followed by a null character, we return a negative
784 value and leave the string pointer pointing at the null character.
786 If \ is followed by 000, we return 0 and leave the string pointer
787 after the zeros. A value of 0 does not mean end of string. */
790 parse_escape (string_ptr)
793 register int c = *(*string_ptr)++;
797 return 007; /* Bell (alert) char */
800 case 'e': /* Escape character */
818 c = *(*string_ptr)++;
820 c = parse_escape (string_ptr);
823 return (c & 0200) | (c & 037);
834 register int i = c - '0';
835 register int count = 0;
838 if ((c = *(*string_ptr)++) >= '0' && c <= '7')
856 /* Print the character C on STREAM as part of the contents of a literal
857 string whose delimiter is QUOTER. Note that this routine should only
858 be call for printing things which are independent of the language
859 of the program being debugged. */
862 gdb_printchar (c, stream, quoter)
868 c &= 0xFF; /* Avoid sign bit follies */
870 if ( c < 0x20 || /* Low control chars */
871 (c >= 0x7F && c < 0xA0) || /* DEL, High controls */
872 (sevenbit_strings && c >= 0x80)) { /* high order bit set */
876 fputs_filtered ("\\n", stream);
879 fputs_filtered ("\\b", stream);
882 fputs_filtered ("\\t", stream);
885 fputs_filtered ("\\f", stream);
888 fputs_filtered ("\\r", stream);
891 fputs_filtered ("\\e", stream);
894 fputs_filtered ("\\a", stream);
897 fprintf_filtered (stream, "\\%.3o", (unsigned int) c);
901 if (c == '\\' || c == quoter)
902 fputs_filtered ("\\", stream);
903 fprintf_filtered (stream, "%c", c);
907 /* Number of lines per page or UINT_MAX if paging is disabled. */
908 static unsigned int lines_per_page;
909 /* Number of chars per line or UNIT_MAX is line folding is disabled. */
910 static unsigned int chars_per_line;
911 /* Current count of lines printed on this page, chars on this line. */
912 static unsigned int lines_printed, chars_printed;
914 /* Buffer and start column of buffered text, for doing smarter word-
915 wrapping. When someone calls wrap_here(), we start buffering output
916 that comes through fputs_filtered(). If we see a newline, we just
917 spit it out and forget about the wrap_here(). If we see another
918 wrap_here(), we spit it out and remember the newer one. If we see
919 the end of the line, we spit out a newline, the indent, and then
920 the buffered output. */
922 /* Malloc'd buffer with chars_per_line+2 bytes. Contains characters which
923 are waiting to be output (they have already been counted in chars_printed).
924 When wrap_buffer[0] is null, the buffer is empty. */
925 static char *wrap_buffer;
927 /* Pointer in wrap_buffer to the next character to fill. */
928 static char *wrap_pointer;
930 /* String to indent by if the wrap occurs. Must not be NULL if wrap_column
932 static char *wrap_indent;
934 /* Column number on the screen where wrap_buffer begins, or 0 if wrapping
936 static int wrap_column;
940 set_width_command (args, from_tty, c)
943 struct cmd_list_element *c;
947 wrap_buffer = (char *) xmalloc (chars_per_line + 2);
948 wrap_buffer[0] = '\0';
951 wrap_buffer = (char *) xrealloc (wrap_buffer, chars_per_line + 2);
952 wrap_pointer = wrap_buffer; /* Start it at the beginning */
955 /* Wait, so the user can read what's on the screen. Prompt the user
956 to continue by pressing RETURN. */
959 prompt_for_continue ()
963 /* We must do this *before* we call gdb_readline, else it will eventually
964 call us -- thinking that we're trying to print beyond the end of the
966 reinitialize_more_filter ();
969 /* On a real operating system, the user can quit with SIGINT.
972 'q' is provided on all systems so users don't have to change habits
973 from system to system, and because telling them what to do in
974 the prompt is more user-friendly than expecting them to think of
977 gdb_readline ("---Type <return> to continue, or q <return> to quit---");
981 while (*p == ' ' || *p == '\t')
984 request_quit (SIGINT);
989 /* Now we have to do this again, so that GDB will know that it doesn't
990 need to save the ---Type <return>--- line at the top of the screen. */
991 reinitialize_more_filter ();
993 dont_repeat (); /* Forget prev cmd -- CR won't repeat it. */
996 /* Reinitialize filter; ie. tell it to reset to original values. */
999 reinitialize_more_filter ()
1005 /* Indicate that if the next sequence of characters overflows the line,
1006 a newline should be inserted here rather than when it hits the end.
1007 If INDENT is non-null, it is a string to be printed to indent the
1008 wrapped part on the next line. INDENT must remain accessible until
1009 the next call to wrap_here() or until a newline is printed through
1012 If the line is already overfull, we immediately print a newline and
1013 the indentation, and disable further wrapping.
1015 If we don't know the width of lines, but we know the page height,
1016 we must not wrap words, but should still keep track of newlines
1017 that were explicitly printed.
1019 INDENT should not contain tabs, as that will mess up the char count
1020 on the next line. FIXME.
1022 This routine is guaranteed to force out any output which has been
1023 squirreled away in the wrap_buffer, so wrap_here ((char *)0) can be
1024 used to force out output from the wrap_buffer. */
1032 *wrap_pointer = '\0';
1033 fputs (wrap_buffer, stdout);
1035 wrap_pointer = wrap_buffer;
1036 wrap_buffer[0] = '\0';
1037 if (chars_per_line == UINT_MAX) /* No line overflow checking */
1041 else if (chars_printed >= chars_per_line)
1043 puts_filtered ("\n");
1045 puts_filtered (indent);
1050 wrap_column = chars_printed;
1054 wrap_indent = indent;
1058 /* Ensure that whatever gets printed next, using the filtered output
1059 commands, starts at the beginning of the line. I.E. if there is
1060 any pending output for the current line, flush it and start a new
1061 line. Otherwise do nothing. */
1066 if (chars_printed > 0)
1068 puts_filtered ("\n");
1072 /* Like fputs but pause after every screenful, and can wrap at points
1073 other than the final character of a line.
1074 Unlike fputs, fputs_filtered does not return a value.
1075 It is OK for LINEBUFFER to be NULL, in which case just don't print
1078 Note that a longjmp to top level may occur in this routine
1079 (since prompt_for_continue may do so) so this routine should not be
1080 called when cleanups are not in place. */
1083 fputs_filtered (linebuffer, stream)
1084 const char *linebuffer;
1087 const char *lineptr;
1089 if (linebuffer == 0)
1092 /* Don't do any filtering if it is disabled. */
1093 if (stream != stdout
1094 || (lines_per_page == UINT_MAX && chars_per_line == UINT_MAX))
1096 fputs (linebuffer, stream);
1100 /* Go through and output each character. Show line extension
1101 when this is necessary; prompt user for new page when this is
1104 lineptr = linebuffer;
1107 /* Possible new page. */
1108 if (lines_printed >= lines_per_page - 1)
1109 prompt_for_continue ();
1111 while (*lineptr && *lineptr != '\n')
1113 /* Print a single line. */
1114 if (*lineptr == '\t')
1117 *wrap_pointer++ = '\t';
1119 putc ('\t', stream);
1120 /* Shifting right by 3 produces the number of tab stops
1121 we have already passed, and then adding one and
1122 shifting left 3 advances to the next tab stop. */
1123 chars_printed = ((chars_printed >> 3) + 1) << 3;
1129 *wrap_pointer++ = *lineptr;
1131 putc (*lineptr, stream);
1136 if (chars_printed >= chars_per_line)
1138 unsigned int save_chars = chars_printed;
1142 /* If we aren't actually wrapping, don't output newline --
1143 if chars_per_line is right, we probably just overflowed
1144 anyway; if it's wrong, let us keep going. */
1146 putc ('\n', stream);
1148 /* Possible new page. */
1149 if (lines_printed >= lines_per_page - 1)
1150 prompt_for_continue ();
1152 /* Now output indentation and wrapped string */
1155 fputs (wrap_indent, stream);
1156 *wrap_pointer = '\0'; /* Null-terminate saved stuff */
1157 fputs (wrap_buffer, stream); /* and eject it */
1158 /* FIXME, this strlen is what prevents wrap_indent from
1159 containing tabs. However, if we recurse to print it
1160 and count its chars, we risk trouble if wrap_indent is
1161 longer than (the user settable) chars_per_line.
1162 Note also that this can set chars_printed > chars_per_line
1163 if we are printing a long string. */
1164 chars_printed = strlen (wrap_indent)
1165 + (save_chars - wrap_column);
1166 wrap_pointer = wrap_buffer; /* Reset buffer */
1167 wrap_buffer[0] = '\0';
1168 wrap_column = 0; /* And disable fancy wrap */
1173 if (*lineptr == '\n')
1176 wrap_here ((char *)0); /* Spit out chars, cancel further wraps */
1178 putc ('\n', stream);
1184 /* Print a variable number of ARGS using format FORMAT. If this
1185 information is going to put the amount written (since the last call
1186 to REINITIALIZE_MORE_FILTER or the last page break) over the page size,
1187 print out a pause message and do a gdb_readline to get the users
1188 permision to continue.
1190 Unlike fprintf, this function does not return a value.
1192 We implement three variants, vfprintf (takes a vararg list and stream),
1193 fprintf (takes a stream to write on), and printf (the usual).
1195 Note that this routine has a restriction that the length of the
1196 final output line must be less than 255 characters *or* it must be
1197 less than twice the size of the format string. This is a very
1198 arbitrary restriction, but it is an internal restriction, so I'll
1199 put it in. This means that the %s format specifier is almost
1200 useless; unless the caller can GUARANTEE that the string is short
1201 enough, fputs_filtered should be used instead.
1203 Note also that a longjmp to top level may occur in this routine
1204 (since prompt_for_continue may do so) so this routine should not be
1205 called when cleanups are not in place. */
1207 #define MIN_LINEBUF 255
1210 vfprintf_filtered (stream, format, args)
1215 char line_buf[MIN_LINEBUF+10];
1216 char *linebuffer = line_buf;
1219 format_length = strlen (format);
1221 /* Reallocate buffer to a larger size if this is necessary. */
1222 if (format_length * 2 > MIN_LINEBUF)
1224 linebuffer = alloca (10 + format_length * 2);
1227 /* This won't blow up if the restrictions described above are
1229 vsprintf (linebuffer, format, args);
1231 fputs_filtered (linebuffer, stream);
1235 vprintf_filtered (format, args)
1239 vfprintf_filtered (stdout, format, args);
1244 fprintf_filtered (va_alist)
1252 stream = va_arg (args, FILE *);
1253 format = va_arg (args, char *);
1255 /* This won't blow up if the restrictions described above are
1257 vfprintf_filtered (stream, format, args);
1261 /* Like fprintf_filtered, but prints it's result indent.
1262 Called as fprintfi_filtered (spaces, format, arg1, arg2, ...); */
1266 fprintfi_filtered (va_alist)
1275 spaces = va_arg (args, int);
1276 stream = va_arg (args, FILE *);
1277 format = va_arg (args, char *);
1278 print_spaces_filtered (spaces, stream);
1280 /* This won't blow up if the restrictions described above are
1282 vfprintf_filtered (stream, format, args);
1288 printf_filtered (va_alist)
1295 format = va_arg (args, char *);
1297 vfprintf_filtered (stdout, format, args);
1301 /* Like printf_filtered, but prints it's result indented.
1302 Called as printfi_filtered (spaces, format, arg1, arg2, ...); */
1306 printfi_filtered (va_alist)
1314 spaces = va_arg (args, int);
1315 format = va_arg (args, char *);
1316 print_spaces_filtered (spaces, stdout);
1317 vfprintf_filtered (stdout, format, args);
1321 /* Easy -- but watch out!
1323 This routine is *not* a replacement for puts()! puts() appends a newline.
1324 This one doesn't, and had better not! */
1327 puts_filtered (string)
1330 fputs_filtered (string, stdout);
1333 /* Return a pointer to N spaces and a null. The pointer is good
1334 until the next call to here. */
1340 static char *spaces;
1341 static int max_spaces;
1347 spaces = (char *) xmalloc (n+1);
1348 for (t = spaces+n; t != spaces;)
1354 return spaces + max_spaces - n;
1357 /* Print N spaces. */
1359 print_spaces_filtered (n, stream)
1363 fputs_filtered (n_spaces (n), stream);
1366 /* C++ demangler stuff. */
1368 /* fprintf_symbol_filtered attempts to demangle NAME, a symbol in language
1369 LANG, using demangling args ARG_MODE, and print it filtered to STREAM.
1370 If the name is not mangled, or the language for the name is unknown, or
1371 demangling is off, the name is printed in its "raw" form. */
1374 fprintf_symbol_filtered (stream, name, lang, arg_mode)
1384 /* If user wants to see raw output, no problem. */
1387 fputs_filtered (name, stream);
1393 case language_cplus:
1394 demangled = cplus_demangle (name, arg_mode);
1396 case language_chill:
1397 demangled = chill_demangle (name);
1403 fputs_filtered (demangled ? demangled : name, stream);
1404 if (demangled != NULL)
1412 /* Do a strcmp() type operation on STRING1 and STRING2, ignoring any
1413 differences in whitespace. Returns 0 if they match, non-zero if they
1414 don't (slightly different than strcmp()'s range of return values).
1416 As an extra hack, string1=="FOO(ARGS)" matches string2=="FOO".
1417 This "feature" is useful when searching for matching C++ function names
1418 (such as if the user types 'break FOO', where FOO is a mangled C++
1422 strcmp_iw (string1, string2)
1423 const char *string1;
1424 const char *string2;
1426 while ((*string1 != '\0') && (*string2 != '\0'))
1428 while (isspace (*string1))
1432 while (isspace (*string2))
1436 if (*string1 != *string2)
1440 if (*string1 != '\0')
1446 return (*string1 != '\0' && *string1 != '(') || (*string2 != '\0');
1451 _initialize_utils ()
1453 struct cmd_list_element *c;
1455 c = add_set_cmd ("width", class_support, var_uinteger,
1456 (char *)&chars_per_line,
1457 "Set number of characters gdb thinks are in a line.",
1459 add_show_from_set (c, &showlist);
1460 c->function.sfunc = set_width_command;
1463 (add_set_cmd ("height", class_support,
1464 var_uinteger, (char *)&lines_per_page,
1465 "Set number of lines gdb thinks are in a page.", &setlist),
1468 /* These defaults will be used if we are unable to get the correct
1469 values from termcap. */
1470 #if defined(__GO32__)
1471 lines_per_page = ScreenRows();
1472 chars_per_line = ScreenCols();
1474 lines_per_page = 24;
1475 chars_per_line = 80;
1476 /* Initialize the screen height and width from termcap. */
1478 char *termtype = getenv ("TERM");
1480 /* Positive means success, nonpositive means failure. */
1483 /* 2048 is large enough for all known terminals, according to the
1484 GNU termcap manual. */
1485 char term_buffer[2048];
1489 status = tgetent (term_buffer, termtype);
1494 val = tgetnum ("li");
1496 lines_per_page = val;
1498 /* The number of lines per page is not mentioned
1499 in the terminal description. This probably means
1500 that paging is not useful (e.g. emacs shell window),
1501 so disable paging. */
1502 lines_per_page = UINT_MAX;
1504 val = tgetnum ("co");
1506 chars_per_line = val;
1511 #if defined(SIGWINCH) && defined(SIGWINCH_HANDLER)
1513 /* If there is a better way to determine the window size, use it. */
1514 SIGWINCH_HANDLER ();
1517 /* If the output is not a terminal, don't paginate it. */
1518 if (!ISATTY (stdout))
1519 lines_per_page = UINT_MAX;
1521 set_width_command ((char *)NULL, 0, c);
1524 (add_set_cmd ("demangle", class_support, var_boolean,
1526 "Set demangling of encoded C++ names when displaying symbols.",
1531 (add_set_cmd ("sevenbit-strings", class_support, var_boolean,
1532 (char *)&sevenbit_strings,
1533 "Set printing of 8-bit characters in strings as \\nnn.",
1538 (add_set_cmd ("asm-demangle", class_support, var_boolean,
1539 (char *)&asm_demangle,
1540 "Set demangling of C++ names in disassembly listings.",
1545 /* Machine specific function to handle SIGWINCH signal. */
1547 #ifdef SIGWINCH_HANDLER_BODY
1548 SIGWINCH_HANDLER_BODY