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>
33 #include "terminal.h" /* For job_control */
37 #include "expression.h"
43 /* readline defines this. */
46 /* Prototypes for local functions */
48 #if defined (NO_MMALLOC) || defined (NO_MMALLOC_CHECK)
52 malloc_botch PARAMS ((void));
54 #endif /* NO_MMALLOC, etc */
57 fatal_dump_core (); /* Can't prototype with <varargs.h> usage... */
60 prompt_for_continue PARAMS ((void));
63 set_width_command PARAMS ((char *, int, struct cmd_list_element *));
65 /* If this definition isn't overridden by the header files, assume
66 that isatty and fileno exist on this system. */
68 #define ISATTY(FP) (isatty (fileno (FP)))
71 /* Chain of cleanup actions established with make_cleanup,
72 to be executed if an error happens. */
74 static struct cleanup *cleanup_chain;
76 /* Nonzero means a quit has been requested. */
80 /* Nonzero means quit immediately if Control-C is typed now, rather
81 than waiting until QUIT is executed. Be careful in setting this;
82 code which executes with immediate_quit set has to be very careful
83 about being able to deal with being interrupted at any time. It is
84 almost always better to use QUIT; the only exception I can think of
85 is being able to quit out of a system call (using EINTR loses if
86 the SIGINT happens between the previous QUIT and the system call).
87 To immediately quit in the case in which a SIGINT happens between
88 the previous QUIT and setting immediate_quit (desirable anytime we
89 expect to block), call QUIT after setting immediate_quit. */
93 /* Nonzero means that encoded C++ names should be printed out in their
94 C++ form rather than raw. */
98 /* Nonzero means that encoded C++ names should be printed out in their
99 C++ form even in assembler language displays. If this is set, but
100 DEMANGLE is zero, names are printed raw, i.e. DEMANGLE controls. */
102 int asm_demangle = 0;
104 /* Nonzero means that strings with character values >0x7F should be printed
105 as octal escapes. Zero means just print the value (e.g. it's an
106 international character, and the terminal or window can cope.) */
108 int sevenbit_strings = 0;
110 /* String to be printed before error messages, if any. */
112 char *error_pre_print;
113 char *warning_pre_print = "\nwarning: ";
115 /* Add a new cleanup to the cleanup_chain,
116 and return the previous chain pointer
117 to be passed later to do_cleanups or discard_cleanups.
118 Args are FUNCTION to clean up with, and ARG to pass to it. */
121 make_cleanup (function, arg)
122 void (*function) PARAMS ((PTR));
125 register struct cleanup *new
126 = (struct cleanup *) xmalloc (sizeof (struct cleanup));
127 register struct cleanup *old_chain = cleanup_chain;
129 new->next = cleanup_chain;
130 new->function = function;
137 /* Discard cleanups and do the actions they describe
138 until we get back to the point OLD_CHAIN in the cleanup_chain. */
141 do_cleanups (old_chain)
142 register struct cleanup *old_chain;
144 register struct cleanup *ptr;
145 while ((ptr = cleanup_chain) != old_chain)
147 cleanup_chain = ptr->next; /* Do this first incase recursion */
148 (*ptr->function) (ptr->arg);
153 /* Discard cleanups, not doing the actions they describe,
154 until we get back to the point OLD_CHAIN in the cleanup_chain. */
157 discard_cleanups (old_chain)
158 register struct cleanup *old_chain;
160 register struct cleanup *ptr;
161 while ((ptr = cleanup_chain) != old_chain)
163 cleanup_chain = ptr->next;
168 /* Set the cleanup_chain to 0, and return the old cleanup chain. */
172 struct cleanup *old_chain = cleanup_chain;
178 /* Restore the cleanup chain from a previously saved chain. */
180 restore_cleanups (chain)
181 struct cleanup *chain;
183 cleanup_chain = chain;
186 /* This function is useful for cleanups.
190 old_chain = make_cleanup (free_current_contents, &foo);
192 to arrange to free the object thus allocated. */
195 free_current_contents (location)
201 /* Provide a known function that does nothing, to use as a base for
202 for a possibly long chain of cleanups. This is useful where we
203 use the cleanup chain for handling normal cleanups as well as dealing
204 with cleanups that need to be done as a result of a call to error().
205 In such cases, we may not be certain where the first cleanup is, unless
206 we have a do-nothing one to always use as the base. */
216 /* Provide a hook for modules wishing to print their own warning messages
217 to set up the terminal state in a compatible way, without them having
218 to import all the target_<...> macros. */
223 target_terminal_ours ();
224 wrap_here(""); /* Force out any buffered output */
225 gdb_flush (gdb_stdout);
228 /* Print a warning message.
229 The first argument STRING is the warning message, used as a fprintf string,
230 and the remaining args are passed as arguments to it.
231 The primary difference between warnings and errors is that a warning
232 does not force the return to command level. */
243 target_terminal_ours ();
244 wrap_here(""); /* Force out any buffered output */
245 gdb_flush (gdb_stdout);
246 if (warning_pre_print)
247 fprintf_unfiltered (gdb_stderr, warning_pre_print);
248 string = va_arg (args, char *);
249 vfprintf_unfiltered (gdb_stderr, string, args);
250 fprintf_unfiltered (gdb_stderr, "\n");
254 /* Start the printing of an error message. Way to use this is to call
255 this, output the error message (use filtered output), and then call
256 return_to_top_level (RETURN_ERROR). error() provides a convenient way to
257 do this for the special case that the error message can be formatted with
258 a single printf call, but this is more general. */
262 target_terminal_ours ();
263 wrap_here (""); /* Force out any buffered output */
264 gdb_flush (gdb_stdout);
266 annotate_error_begin ();
269 fprintf_filtered (gdb_stderr, error_pre_print);
272 /* Print an error message and return to command level.
273 The first argument STRING is the error message, used as a fprintf string,
274 and the remaining args are passed as arguments to it. */
286 string = va_arg (args, char *);
287 vfprintf_filtered (gdb_stderr, string, args);
288 fprintf_filtered (gdb_stderr, "\n");
290 return_to_top_level (RETURN_ERROR);
293 /* Print an error message and exit reporting failure.
294 This is for a error that we cannot continue from.
295 The arguments are printed a la printf.
297 This function cannot be declared volatile (NORETURN) in an
298 ANSI environment because exit() is not declared volatile. */
309 string = va_arg (args, char *);
310 fprintf_unfiltered (gdb_stderr, "\ngdb: ");
311 vfprintf_unfiltered (gdb_stderr, string, args);
312 fprintf_unfiltered (gdb_stderr, "\n");
317 /* Print an error message and exit, dumping core.
318 The arguments are printed a la printf (). */
322 fatal_dump_core (va_alist)
329 string = va_arg (args, char *);
330 /* "internal error" is always correct, since GDB should never dump
331 core, no matter what the input. */
332 fprintf_unfiltered (gdb_stderr, "\ngdb internal error: ");
333 vfprintf_unfiltered (gdb_stderr, string, args);
334 fprintf_unfiltered (gdb_stderr, "\n");
337 signal (SIGQUIT, SIG_DFL);
338 kill (getpid (), SIGQUIT);
339 /* We should never get here, but just in case... */
343 /* The strerror() function can return NULL for errno values that are
344 out of range. Provide a "safe" version that always returns a
348 safe_strerror (errnum)
354 if ((msg = strerror (errnum)) == NULL)
356 sprintf (buf, "(undocumented errno %d)", errnum);
362 /* The strsignal() function can return NULL for signal values that are
363 out of range. Provide a "safe" version that always returns a
367 safe_strsignal (signo)
373 if ((msg = strsignal (signo)) == NULL)
375 sprintf (buf, "(undocumented signal %d)", signo);
382 /* Print the system error message for errno, and also mention STRING
383 as the file name for which the error was encountered.
384 Then return to command level. */
387 perror_with_name (string)
393 err = safe_strerror (errno);
394 combined = (char *) alloca (strlen (err) + strlen (string) + 3);
395 strcpy (combined, string);
396 strcat (combined, ": ");
397 strcat (combined, err);
399 /* I understand setting these is a matter of taste. Still, some people
400 may clear errno but not know about bfd_error. Doing this here is not
402 bfd_set_error (bfd_error_no_error);
405 error ("%s.", combined);
408 /* Print the system error message for ERRCODE, and also mention STRING
409 as the file name for which the error was encountered. */
412 print_sys_errmsg (string, errcode)
419 err = safe_strerror (errcode);
420 combined = (char *) alloca (strlen (err) + strlen (string) + 3);
421 strcpy (combined, string);
422 strcat (combined, ": ");
423 strcat (combined, err);
425 /* We want anything which was printed on stdout to come out first, before
427 gdb_flush (gdb_stdout);
428 fprintf_unfiltered (gdb_stderr, "%s.\n", combined);
431 /* Control C eventually causes this to be called, at a convenient time. */
436 serial_t gdb_stdout_serial = serial_fdopen (1);
438 target_terminal_ours ();
440 /* We want all output to appear now, before we print "Quit". We
441 have 3 levels of buffering we have to flush (it's possible that
442 some of these should be changed to flush the lower-level ones
445 /* 1. The _filtered buffer. */
446 wrap_here ((char *)0);
448 /* 2. The stdio buffer. */
449 gdb_flush (gdb_stdout);
450 gdb_flush (gdb_stderr);
452 /* 3. The system-level buffer. */
453 SERIAL_FLUSH_OUTPUT (gdb_stdout_serial);
454 SERIAL_UN_FDOPEN (gdb_stdout_serial);
456 annotate_error_begin ();
458 /* Don't use *_filtered; we don't want to prompt the user to continue. */
460 fprintf_unfiltered (gdb_stderr, error_pre_print);
463 /* If there is no terminal switching for this target, then we can't
464 possibly get screwed by the lack of job control. */
465 || current_target->to_terminal_ours == NULL)
466 fprintf_unfiltered (gdb_stderr, "Quit\n");
468 fprintf_unfiltered (gdb_stderr,
469 "Quit (expect signal SIGINT when the program is resumed)\n");
470 return_to_top_level (RETURN_QUIT);
476 /* In the absence of signals, poll keyboard for a quit.
477 Called from #define QUIT pollquit() in xm-go32.h. */
495 /* We just ignore it */
496 fprintf_unfiltered (gdb_stderr, "CTRL-A to quit, CTRL-B to quit harder\n");
518 fprintf_unfiltered (gdb_stderr, "CTRL-A to quit, CTRL-B to quit harder\n");
525 /* Done by signals */
528 /* Control C comes here */
536 /* Restore the signal handler. Harmless with BSD-style signals, needed
537 for System V-style signals. So just always do it, rather than worrying
538 about USG defines and stuff like that. */
539 signal (signo, request_quit);
546 /* Memory management stuff (malloc friends). */
548 #if defined (NO_MMALLOC)
555 return (malloc (size));
559 mrealloc (md, ptr, size)
564 if (ptr == 0) /* Guard against old realloc's */
565 return malloc (size);
567 return realloc (ptr, size);
578 #endif /* NO_MMALLOC */
580 #if defined (NO_MMALLOC) || defined (NO_MMALLOC_CHECK)
588 #else /* have mmalloc and want corruption checking */
593 fatal_dump_core ("Memory corruption");
596 /* Attempt to install hooks in mmalloc/mrealloc/mfree for the heap specified
597 by MD, to detect memory corruption. Note that MD may be NULL to specify
598 the default heap that grows via sbrk.
600 Note that for freshly created regions, we must call mmcheck prior to any
601 mallocs in the region. Otherwise, any region which was allocated prior to
602 installing the checking hooks, which is later reallocated or freed, will
603 fail the checks! The mmcheck function only allows initial hooks to be
604 installed before the first mmalloc. However, anytime after we have called
605 mmcheck the first time to install the checking hooks, we can call it again
606 to update the function pointer to the memory corruption handler.
608 Returns zero on failure, non-zero on success. */
614 if (!mmcheck (md, malloc_botch))
616 warning ("internal error: failed to install memory consistency checks");
622 #endif /* Have mmalloc and want corruption checking */
624 /* Called when a memory allocation fails, with the number of bytes of
625 memory requested in SIZE. */
633 fatal ("virtual memory exhausted: can't allocate %ld bytes.", size);
637 fatal ("virtual memory exhausted.");
641 /* Like mmalloc but get error if no storage available, and protect against
642 the caller wanting to allocate zero bytes. Whether to return NULL for
643 a zero byte request, or translate the request into a request for one
644 byte of zero'd storage, is a religious issue. */
657 else if ((val = mmalloc (md, size)) == NULL)
664 /* Like mrealloc but get error if no storage available. */
667 xmrealloc (md, ptr, size)
676 val = mrealloc (md, ptr, size);
680 val = mmalloc (md, size);
689 /* Like malloc but get error if no storage available, and protect against
690 the caller wanting to allocate zero bytes. */
696 return (xmmalloc ((PTR) NULL, size));
699 /* Like mrealloc but get error if no storage available. */
706 return (xmrealloc ((PTR) NULL, ptr, size));
710 /* My replacement for the read system call.
711 Used like `read' but keeps going if `read' returns too soon. */
714 myread (desc, addr, len)
724 val = read (desc, addr, len);
735 /* Make a copy of the string at PTR with SIZE characters
736 (and add a null character at the end in the copy).
737 Uses malloc to get the space. Returns the address of the copy. */
740 savestring (ptr, size)
744 register char *p = (char *) xmalloc (size + 1);
745 memcpy (p, ptr, size);
751 msavestring (md, ptr, size)
756 register char *p = (char *) xmmalloc (md, size + 1);
757 memcpy (p, ptr, size);
762 /* The "const" is so it compiles under DGUX (which prototypes strsave
763 in <string.h>. FIXME: This should be named "xstrsave", shouldn't it?
764 Doesn't real strsave return NULL if out of memory? */
769 return savestring (ptr, strlen (ptr));
777 return (msavestring (md, ptr, strlen (ptr)));
781 print_spaces (n, file)
789 /* Print a host address. */
792 gdb_print_address (addr, stream)
797 /* We could use the %p conversion specifier to fprintf if we had any
798 way of knowing whether this host supports it. But the following
799 should work on the Alpha and on 32 bit machines. */
801 fprintf_filtered (stream, "0x%lx", (unsigned long)addr);
804 /* Ask user a y-or-n question and return 1 iff answer is yes.
805 Takes three args which are given to printf to print the question.
806 The first, a control string, should end in "? ".
807 It should not say how to answer, because we do that. */
820 /* Automatically answer "yes" if input is not from a terminal. */
821 if (!input_from_terminal_p ())
826 wrap_here (""); /* Flush any buffered output */
827 gdb_flush (gdb_stdout);
829 if (annotation_level > 1)
830 printf_filtered ("\n\032\032pre-query\n");
833 ctlstr = va_arg (args, char *);
834 vfprintf_filtered (gdb_stdout, ctlstr, args);
836 printf_filtered ("(y or n) ");
838 if (annotation_level > 1)
839 printf_filtered ("\n\032\032query\n");
841 gdb_flush (gdb_stdout);
842 answer = fgetc (stdin);
843 clearerr (stdin); /* in case of C-d */
844 if (answer == EOF) /* C-d */
849 if (answer != '\n') /* Eat rest of input line, to EOF or newline */
852 ans2 = fgetc (stdin);
855 while (ans2 != EOF && ans2 != '\n');
868 printf_filtered ("Please answer y or n.\n");
871 if (annotation_level > 1)
872 printf_filtered ("\n\032\032post-query\n");
877 /* Parse a C escape sequence. STRING_PTR points to a variable
878 containing a pointer to the string to parse. That pointer
879 should point to the character after the \. That pointer
880 is updated past the characters we use. The value of the
881 escape sequence is returned.
883 A negative value means the sequence \ newline was seen,
884 which is supposed to be equivalent to nothing at all.
886 If \ is followed by a null character, we return a negative
887 value and leave the string pointer pointing at the null character.
889 If \ is followed by 000, we return 0 and leave the string pointer
890 after the zeros. A value of 0 does not mean end of string. */
893 parse_escape (string_ptr)
896 register int c = *(*string_ptr)++;
900 return 007; /* Bell (alert) char */
903 case 'e': /* Escape character */
921 c = *(*string_ptr)++;
923 c = parse_escape (string_ptr);
926 return (c & 0200) | (c & 037);
937 register int i = c - '0';
938 register int count = 0;
941 if ((c = *(*string_ptr)++) >= '0' && c <= '7')
959 /* Print the character C on STREAM as part of the contents of a literal
960 string whose delimiter is QUOTER. Note that this routine should only
961 be call for printing things which are independent of the language
962 of the program being debugged. */
965 gdb_printchar (c, stream, quoter)
971 c &= 0xFF; /* Avoid sign bit follies */
973 if ( c < 0x20 || /* Low control chars */
974 (c >= 0x7F && c < 0xA0) || /* DEL, High controls */
975 (sevenbit_strings && c >= 0x80)) { /* high order bit set */
979 fputs_filtered ("\\n", stream);
982 fputs_filtered ("\\b", stream);
985 fputs_filtered ("\\t", stream);
988 fputs_filtered ("\\f", stream);
991 fputs_filtered ("\\r", stream);
994 fputs_filtered ("\\e", stream);
997 fputs_filtered ("\\a", stream);
1000 fprintf_filtered (stream, "\\%.3o", (unsigned int) c);
1004 if (c == '\\' || c == quoter)
1005 fputs_filtered ("\\", stream);
1006 fprintf_filtered (stream, "%c", c);
1010 /* Number of lines per page or UINT_MAX if paging is disabled. */
1011 static unsigned int lines_per_page;
1012 /* Number of chars per line or UNIT_MAX is line folding is disabled. */
1013 static unsigned int chars_per_line;
1014 /* Current count of lines printed on this page, chars on this line. */
1015 static unsigned int lines_printed, chars_printed;
1017 /* Buffer and start column of buffered text, for doing smarter word-
1018 wrapping. When someone calls wrap_here(), we start buffering output
1019 that comes through fputs_filtered(). If we see a newline, we just
1020 spit it out and forget about the wrap_here(). If we see another
1021 wrap_here(), we spit it out and remember the newer one. If we see
1022 the end of the line, we spit out a newline, the indent, and then
1023 the buffered output. */
1025 /* Malloc'd buffer with chars_per_line+2 bytes. Contains characters which
1026 are waiting to be output (they have already been counted in chars_printed).
1027 When wrap_buffer[0] is null, the buffer is empty. */
1028 static char *wrap_buffer;
1030 /* Pointer in wrap_buffer to the next character to fill. */
1031 static char *wrap_pointer;
1033 /* String to indent by if the wrap occurs. Must not be NULL if wrap_column
1035 static char *wrap_indent;
1037 /* Column number on the screen where wrap_buffer begins, or 0 if wrapping
1038 is not in effect. */
1039 static int wrap_column;
1043 set_width_command (args, from_tty, c)
1046 struct cmd_list_element *c;
1050 wrap_buffer = (char *) xmalloc (chars_per_line + 2);
1051 wrap_buffer[0] = '\0';
1054 wrap_buffer = (char *) xrealloc (wrap_buffer, chars_per_line + 2);
1055 wrap_pointer = wrap_buffer; /* Start it at the beginning */
1058 /* Wait, so the user can read what's on the screen. Prompt the user
1059 to continue by pressing RETURN. */
1062 prompt_for_continue ()
1065 char cont_prompt[120];
1067 if (annotation_level > 1)
1068 printf_unfiltered ("\n\032\032pre-prompt-for-continue\n");
1070 strcpy (cont_prompt,
1071 "---Type <return> to continue, or q <return> to quit---");
1072 if (annotation_level > 1)
1073 strcat (cont_prompt, "\n\032\032prompt-for-continue\n");
1075 /* We must do this *before* we call gdb_readline, else it will eventually
1076 call us -- thinking that we're trying to print beyond the end of the
1078 reinitialize_more_filter ();
1081 /* On a real operating system, the user can quit with SIGINT.
1084 'q' is provided on all systems so users don't have to change habits
1085 from system to system, and because telling them what to do in
1086 the prompt is more user-friendly than expecting them to think of
1088 /* Call readline, not gdb_readline, because GO32 readline handles control-C
1089 whereas control-C to gdb_readline will cause the user to get dumped
1091 ignore = readline (cont_prompt);
1093 if (annotation_level > 1)
1094 printf_unfiltered ("\n\032\032post-prompt-for-continue\n");
1099 while (*p == ' ' || *p == '\t')
1102 request_quit (SIGINT);
1107 /* Now we have to do this again, so that GDB will know that it doesn't
1108 need to save the ---Type <return>--- line at the top of the screen. */
1109 reinitialize_more_filter ();
1111 dont_repeat (); /* Forget prev cmd -- CR won't repeat it. */
1114 /* Reinitialize filter; ie. tell it to reset to original values. */
1117 reinitialize_more_filter ()
1123 /* Indicate that if the next sequence of characters overflows the line,
1124 a newline should be inserted here rather than when it hits the end.
1125 If INDENT is non-null, it is a string to be printed to indent the
1126 wrapped part on the next line. INDENT must remain accessible until
1127 the next call to wrap_here() or until a newline is printed through
1130 If the line is already overfull, we immediately print a newline and
1131 the indentation, and disable further wrapping.
1133 If we don't know the width of lines, but we know the page height,
1134 we must not wrap words, but should still keep track of newlines
1135 that were explicitly printed.
1137 INDENT should not contain tabs, as that will mess up the char count
1138 on the next line. FIXME.
1140 This routine is guaranteed to force out any output which has been
1141 squirreled away in the wrap_buffer, so wrap_here ((char *)0) can be
1142 used to force out output from the wrap_buffer. */
1150 *wrap_pointer = '\0';
1151 fputs_unfiltered (wrap_buffer, gdb_stdout);
1153 wrap_pointer = wrap_buffer;
1154 wrap_buffer[0] = '\0';
1155 if (chars_per_line == UINT_MAX) /* No line overflow checking */
1159 else if (chars_printed >= chars_per_line)
1161 puts_filtered ("\n");
1163 puts_filtered (indent);
1168 wrap_column = chars_printed;
1172 wrap_indent = indent;
1176 /* Ensure that whatever gets printed next, using the filtered output
1177 commands, starts at the beginning of the line. I.E. if there is
1178 any pending output for the current line, flush it and start a new
1179 line. Otherwise do nothing. */
1184 if (chars_printed > 0)
1186 puts_filtered ("\n");
1192 gdb_fopen (name, mode)
1196 return fopen (name, mode);
1206 /* Like fputs but if FILTER is true, pause after every screenful.
1208 Regardless of FILTER can wrap at points other than the final
1209 character of a line.
1211 Unlike fputs, fputs_maybe_filtered does not return a value.
1212 It is OK for LINEBUFFER to be NULL, in which case just don't print
1215 Note that a longjmp to top level may occur in this routine (only if
1216 FILTER is true) (since prompt_for_continue may do so) so this
1217 routine should not be called when cleanups are not in place. */
1220 fputs_maybe_filtered (linebuffer, stream, filter)
1221 const char *linebuffer;
1225 const char *lineptr;
1227 if (linebuffer == 0)
1230 /* Don't do any filtering if it is disabled. */
1231 if (stream != gdb_stdout
1232 || (lines_per_page == UINT_MAX && chars_per_line == UINT_MAX))
1234 fputs_unfiltered (linebuffer, stream);
1238 /* Go through and output each character. Show line extension
1239 when this is necessary; prompt user for new page when this is
1242 lineptr = linebuffer;
1245 /* Possible new page. */
1247 (lines_printed >= lines_per_page - 1))
1248 prompt_for_continue ();
1250 while (*lineptr && *lineptr != '\n')
1252 /* Print a single line. */
1253 if (*lineptr == '\t')
1256 *wrap_pointer++ = '\t';
1258 fputc_unfiltered ('\t', stream);
1259 /* Shifting right by 3 produces the number of tab stops
1260 we have already passed, and then adding one and
1261 shifting left 3 advances to the next tab stop. */
1262 chars_printed = ((chars_printed >> 3) + 1) << 3;
1268 *wrap_pointer++ = *lineptr;
1270 fputc_unfiltered (*lineptr, stream);
1275 if (chars_printed >= chars_per_line)
1277 unsigned int save_chars = chars_printed;
1281 /* If we aren't actually wrapping, don't output newline --
1282 if chars_per_line is right, we probably just overflowed
1283 anyway; if it's wrong, let us keep going. */
1285 fputc_unfiltered ('\n', stream);
1287 /* Possible new page. */
1288 if (lines_printed >= lines_per_page - 1)
1289 prompt_for_continue ();
1291 /* Now output indentation and wrapped string */
1294 fputs_unfiltered (wrap_indent, stream);
1295 *wrap_pointer = '\0'; /* Null-terminate saved stuff */
1296 fputs_unfiltered (wrap_buffer, stream); /* and eject it */
1297 /* FIXME, this strlen is what prevents wrap_indent from
1298 containing tabs. However, if we recurse to print it
1299 and count its chars, we risk trouble if wrap_indent is
1300 longer than (the user settable) chars_per_line.
1301 Note also that this can set chars_printed > chars_per_line
1302 if we are printing a long string. */
1303 chars_printed = strlen (wrap_indent)
1304 + (save_chars - wrap_column);
1305 wrap_pointer = wrap_buffer; /* Reset buffer */
1306 wrap_buffer[0] = '\0';
1307 wrap_column = 0; /* And disable fancy wrap */
1312 if (*lineptr == '\n')
1315 wrap_here ((char *)0); /* Spit out chars, cancel further wraps */
1317 fputc_unfiltered ('\n', stream);
1324 fputs_filtered (linebuffer, stream)
1325 const char *linebuffer;
1328 fputs_maybe_filtered (linebuffer, stream, 1);
1331 #ifndef FPUTS_UNFILTERED_OVERRIDE
1333 fputs_unfiltered (linebuffer, stream)
1334 const char *linebuffer;
1337 fputs (linebuffer, stream);
1339 #endif /* FPUTS_UNFILTERED_OVERRIDE */
1348 fputs_unfiltered (buf, gdb_stdout);
1352 fputc_unfiltered (c, stream)
1359 fputs_unfiltered (buf, stream);
1363 /* Print a variable number of ARGS using format FORMAT. If this
1364 information is going to put the amount written (since the last call
1365 to REINITIALIZE_MORE_FILTER or the last page break) over the page size,
1366 call prompt_for_continue to get the users permision to continue.
1368 Unlike fprintf, this function does not return a value.
1370 We implement three variants, vfprintf (takes a vararg list and stream),
1371 fprintf (takes a stream to write on), and printf (the usual).
1373 Note also that a longjmp to top level may occur in this routine
1374 (since prompt_for_continue may do so) so this routine should not be
1375 called when cleanups are not in place. */
1378 vfprintf_maybe_filtered (stream, format, args, filter)
1385 struct cleanup *old_cleanups;
1387 vasprintf (&linebuffer, format, args);
1388 if (linebuffer == NULL)
1389 fatal ("virtual memory exhausted.");
1390 old_cleanups = make_cleanup (free, linebuffer);
1391 fputs_maybe_filtered (linebuffer, stream, filter);
1392 do_cleanups (old_cleanups);
1397 vfprintf_filtered (stream, format, args)
1402 vfprintf_maybe_filtered (stream, format, args, 1);
1406 vfprintf_unfiltered (stream, format, args)
1412 struct cleanup *old_cleanups;
1414 vasprintf (&linebuffer, format, args);
1415 if (linebuffer == NULL)
1416 fatal ("virtual memory exhausted.");
1417 old_cleanups = make_cleanup (free, linebuffer);
1418 fputs_unfiltered (linebuffer, stream);
1419 do_cleanups (old_cleanups);
1423 vprintf_filtered (format, args)
1427 vfprintf_maybe_filtered (gdb_stdout, format, args, 1);
1431 vprintf_unfiltered (format, args)
1435 vfprintf_unfiltered (gdb_stdout, format, args);
1440 fprintf_filtered (va_alist)
1448 stream = va_arg (args, FILE *);
1449 format = va_arg (args, char *);
1451 vfprintf_filtered (stream, format, args);
1457 fprintf_unfiltered (va_alist)
1465 stream = va_arg (args, FILE *);
1466 format = va_arg (args, char *);
1468 vfprintf_unfiltered (stream, format, args);
1472 /* Like fprintf_filtered, but prints its result indented.
1473 Called as fprintfi_filtered (spaces, stream, format, ...); */
1477 fprintfi_filtered (va_alist)
1486 spaces = va_arg (args, int);
1487 stream = va_arg (args, FILE *);
1488 format = va_arg (args, char *);
1489 print_spaces_filtered (spaces, stream);
1491 vfprintf_filtered (stream, format, args);
1498 printf_filtered (va_alist)
1505 format = va_arg (args, char *);
1507 vfprintf_filtered (gdb_stdout, format, args);
1514 printf_unfiltered (va_alist)
1521 format = va_arg (args, char *);
1523 vfprintf_unfiltered (gdb_stdout, format, args);
1527 /* Like printf_filtered, but prints it's result indented.
1528 Called as printfi_filtered (spaces, format, ...); */
1532 printfi_filtered (va_alist)
1540 spaces = va_arg (args, int);
1541 format = va_arg (args, char *);
1542 print_spaces_filtered (spaces, gdb_stdout);
1543 vfprintf_filtered (gdb_stdout, format, args);
1547 /* Easy -- but watch out!
1549 This routine is *not* a replacement for puts()! puts() appends a newline.
1550 This one doesn't, and had better not! */
1553 puts_filtered (string)
1556 fputs_filtered (string, gdb_stdout);
1560 puts_unfiltered (string)
1563 fputs_unfiltered (string, gdb_stdout);
1566 /* Return a pointer to N spaces and a null. The pointer is good
1567 until the next call to here. */
1573 static char *spaces;
1574 static int max_spaces;
1580 spaces = (char *) xmalloc (n+1);
1581 for (t = spaces+n; t != spaces;)
1587 return spaces + max_spaces - n;
1590 /* Print N spaces. */
1592 print_spaces_filtered (n, stream)
1596 fputs_filtered (n_spaces (n), stream);
1599 /* C++ demangler stuff. */
1601 /* fprintf_symbol_filtered attempts to demangle NAME, a symbol in language
1602 LANG, using demangling args ARG_MODE, and print it filtered to STREAM.
1603 If the name is not mangled, or the language for the name is unknown, or
1604 demangling is off, the name is printed in its "raw" form. */
1607 fprintf_symbol_filtered (stream, name, lang, arg_mode)
1617 /* If user wants to see raw output, no problem. */
1620 fputs_filtered (name, stream);
1626 case language_cplus:
1627 demangled = cplus_demangle (name, arg_mode);
1629 case language_chill:
1630 demangled = chill_demangle (name);
1636 fputs_filtered (demangled ? demangled : name, stream);
1637 if (demangled != NULL)
1645 /* Do a strcmp() type operation on STRING1 and STRING2, ignoring any
1646 differences in whitespace. Returns 0 if they match, non-zero if they
1647 don't (slightly different than strcmp()'s range of return values).
1649 As an extra hack, string1=="FOO(ARGS)" matches string2=="FOO".
1650 This "feature" is useful when searching for matching C++ function names
1651 (such as if the user types 'break FOO', where FOO is a mangled C++
1655 strcmp_iw (string1, string2)
1656 const char *string1;
1657 const char *string2;
1659 while ((*string1 != '\0') && (*string2 != '\0'))
1661 while (isspace (*string1))
1665 while (isspace (*string2))
1669 if (*string1 != *string2)
1673 if (*string1 != '\0')
1679 return (*string1 != '\0' && *string1 != '(') || (*string2 != '\0');
1684 _initialize_utils ()
1686 struct cmd_list_element *c;
1688 c = add_set_cmd ("width", class_support, var_uinteger,
1689 (char *)&chars_per_line,
1690 "Set number of characters gdb thinks are in a line.",
1692 add_show_from_set (c, &showlist);
1693 c->function.sfunc = set_width_command;
1696 (add_set_cmd ("height", class_support,
1697 var_uinteger, (char *)&lines_per_page,
1698 "Set number of lines gdb thinks are in a page.", &setlist),
1701 /* These defaults will be used if we are unable to get the correct
1702 values from termcap. */
1703 #if defined(__GO32__)
1704 lines_per_page = ScreenRows();
1705 chars_per_line = ScreenCols();
1707 lines_per_page = 24;
1708 chars_per_line = 80;
1709 /* start-sanitize-mpw */
1711 /* No termcap under MPW, although might be cool to do something
1712 by looking at worksheet or console window sizes. */
1713 /* end-sanitize-mpw */
1714 /* Initialize the screen height and width from termcap. */
1716 char *termtype = getenv ("TERM");
1718 /* Positive means success, nonpositive means failure. */
1721 /* 2048 is large enough for all known terminals, according to the
1722 GNU termcap manual. */
1723 char term_buffer[2048];
1727 status = tgetent (term_buffer, termtype);
1732 val = tgetnum ("li");
1734 lines_per_page = val;
1736 /* The number of lines per page is not mentioned
1737 in the terminal description. This probably means
1738 that paging is not useful (e.g. emacs shell window),
1739 so disable paging. */
1740 lines_per_page = UINT_MAX;
1742 val = tgetnum ("co");
1744 chars_per_line = val;
1748 /* start-sanitize-mpw */
1750 /* end-sanitize-mpw */
1752 #if defined(SIGWINCH) && defined(SIGWINCH_HANDLER)
1754 /* If there is a better way to determine the window size, use it. */
1755 SIGWINCH_HANDLER ();
1758 /* If the output is not a terminal, don't paginate it. */
1759 if (!ISATTY (gdb_stdout))
1760 lines_per_page = UINT_MAX;
1762 set_width_command ((char *)NULL, 0, c);
1765 (add_set_cmd ("demangle", class_support, var_boolean,
1767 "Set demangling of encoded C++ names when displaying symbols.",
1772 (add_set_cmd ("sevenbit-strings", class_support, var_boolean,
1773 (char *)&sevenbit_strings,
1774 "Set printing of 8-bit characters in strings as \\nnn.",
1779 (add_set_cmd ("asm-demangle", class_support, var_boolean,
1780 (char *)&asm_demangle,
1781 "Set demangling of C++ names in disassembly listings.",
1786 /* Machine specific function to handle SIGWINCH signal. */
1788 #ifdef SIGWINCH_HANDLER_BODY
1789 SIGWINCH_HANDLER_BODY