]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/utils.c
ed764e5c0b2df7083c2c21841ce2af962557846a
[thirdparty/binutils-gdb.git] / gdb / utils.c
1 /* General utility routines for GDB, the GNU debugger.
2 Copyright 1986, 1989, 1990, 1991, 1992, 1995 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
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.
10
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.
15
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. */
19
20 #include "defs.h"
21 #if !defined(__GO32__) && !defined(WIN32)
22 #include <sys/ioctl.h>
23 #include <sys/param.h>
24 #include <pwd.h>
25 #endif
26 #ifdef ANSI_PROTOTYPES
27 #include <stdarg.h>
28 #else
29 #include <varargs.h>
30 #endif
31 #include <ctype.h>
32 #include <string.h>
33
34 #include "signals.h"
35 #include "gdbcmd.h"
36 #include "serial.h"
37 #include "bfd.h"
38 #include "target.h"
39 #include "demangle.h"
40 #include "expression.h"
41 #include "language.h"
42 #include "annotate.h"
43
44 #include "readline.h"
45
46 /* readline defines this. */
47 #undef savestring
48
49 /* Prototypes for local functions */
50
51 #if defined (NO_MMALLOC) || defined (NO_MMALLOC_CHECK)
52 #else
53
54 static void
55 malloc_botch PARAMS ((void));
56
57 #endif /* NO_MMALLOC, etc */
58
59 static void
60 fatal_dump_core PARAMS((char *, ...));
61
62 static void
63 prompt_for_continue PARAMS ((void));
64
65 static void
66 set_width_command PARAMS ((char *, int, struct cmd_list_element *));
67
68 /* If this definition isn't overridden by the header files, assume
69 that isatty and fileno exist on this system. */
70 #ifndef ISATTY
71 #define ISATTY(FP) (isatty (fileno (FP)))
72 #endif
73
74 /* Chain of cleanup actions established with make_cleanup,
75 to be executed if an error happens. */
76
77 static struct cleanup *cleanup_chain;
78
79 /* Nonzero if we have job control. */
80
81 int job_control;
82
83 /* Nonzero means a quit has been requested. */
84
85 int quit_flag;
86
87 /* Nonzero means quit immediately if Control-C is typed now, rather
88 than waiting until QUIT is executed. Be careful in setting this;
89 code which executes with immediate_quit set has to be very careful
90 about being able to deal with being interrupted at any time. It is
91 almost always better to use QUIT; the only exception I can think of
92 is being able to quit out of a system call (using EINTR loses if
93 the SIGINT happens between the previous QUIT and the system call).
94 To immediately quit in the case in which a SIGINT happens between
95 the previous QUIT and setting immediate_quit (desirable anytime we
96 expect to block), call QUIT after setting immediate_quit. */
97
98 int immediate_quit;
99
100 /* Nonzero means that encoded C++ names should be printed out in their
101 C++ form rather than raw. */
102
103 int demangle = 1;
104
105 /* Nonzero means that encoded C++ names should be printed out in their
106 C++ form even in assembler language displays. If this is set, but
107 DEMANGLE is zero, names are printed raw, i.e. DEMANGLE controls. */
108
109 int asm_demangle = 0;
110
111 /* Nonzero means that strings with character values >0x7F should be printed
112 as octal escapes. Zero means just print the value (e.g. it's an
113 international character, and the terminal or window can cope.) */
114
115 int sevenbit_strings = 0;
116
117 /* String to be printed before error messages, if any. */
118
119 char *error_pre_print;
120
121 /* String to be printed before quit messages, if any. */
122
123 char *quit_pre_print;
124
125 /* String to be printed before warning messages, if any. */
126
127 char *warning_pre_print = "\nwarning: ";
128 \f
129 /* Add a new cleanup to the cleanup_chain,
130 and return the previous chain pointer
131 to be passed later to do_cleanups or discard_cleanups.
132 Args are FUNCTION to clean up with, and ARG to pass to it. */
133
134 struct cleanup *
135 make_cleanup (function, arg)
136 void (*function) PARAMS ((PTR));
137 PTR arg;
138 {
139 register struct cleanup *new
140 = (struct cleanup *) xmalloc (sizeof (struct cleanup));
141 register struct cleanup *old_chain = cleanup_chain;
142
143 new->next = cleanup_chain;
144 new->function = function;
145 new->arg = arg;
146 cleanup_chain = new;
147
148 return old_chain;
149 }
150
151 /* Discard cleanups and do the actions they describe
152 until we get back to the point OLD_CHAIN in the cleanup_chain. */
153
154 void
155 do_cleanups (old_chain)
156 register struct cleanup *old_chain;
157 {
158 register struct cleanup *ptr;
159 while ((ptr = cleanup_chain) != old_chain)
160 {
161 cleanup_chain = ptr->next; /* Do this first incase recursion */
162 (*ptr->function) (ptr->arg);
163 free (ptr);
164 }
165 }
166
167 /* Discard cleanups, not doing the actions they describe,
168 until we get back to the point OLD_CHAIN in the cleanup_chain. */
169
170 void
171 discard_cleanups (old_chain)
172 register struct cleanup *old_chain;
173 {
174 register struct cleanup *ptr;
175 while ((ptr = cleanup_chain) != old_chain)
176 {
177 cleanup_chain = ptr->next;
178 free ((PTR)ptr);
179 }
180 }
181
182 /* Set the cleanup_chain to 0, and return the old cleanup chain. */
183 struct cleanup *
184 save_cleanups ()
185 {
186 struct cleanup *old_chain = cleanup_chain;
187
188 cleanup_chain = 0;
189 return old_chain;
190 }
191
192 /* Restore the cleanup chain from a previously saved chain. */
193 void
194 restore_cleanups (chain)
195 struct cleanup *chain;
196 {
197 cleanup_chain = chain;
198 }
199
200 /* This function is useful for cleanups.
201 Do
202
203 foo = xmalloc (...);
204 old_chain = make_cleanup (free_current_contents, &foo);
205
206 to arrange to free the object thus allocated. */
207
208 void
209 free_current_contents (location)
210 char **location;
211 {
212 free (*location);
213 }
214
215 /* Provide a known function that does nothing, to use as a base for
216 for a possibly long chain of cleanups. This is useful where we
217 use the cleanup chain for handling normal cleanups as well as dealing
218 with cleanups that need to be done as a result of a call to error().
219 In such cases, we may not be certain where the first cleanup is, unless
220 we have a do-nothing one to always use as the base. */
221
222 /* ARGSUSED */
223 void
224 null_cleanup (arg)
225 char **arg;
226 {
227 }
228
229 \f
230 /* Print a warning message. Way to use this is to call warning_begin,
231 output the warning message (use unfiltered output to gdb_stderr),
232 ending in a newline. There is not currently a warning_end that you
233 call afterwards, but such a thing might be added if it is useful
234 for a GUI to separate warning messages from other output.
235
236 FIXME: Why do warnings use unfiltered output and errors filtered?
237 Is this anything other than a historical accident? */
238
239 void
240 warning_begin ()
241 {
242 target_terminal_ours ();
243 wrap_here(""); /* Force out any buffered output */
244 gdb_flush (gdb_stdout);
245 if (warning_pre_print)
246 fprintf_unfiltered (gdb_stderr, warning_pre_print);
247 }
248
249 /* Print a warning message.
250 The first argument STRING is the warning message, used as a fprintf string,
251 and the remaining args are passed as arguments to it.
252 The primary difference between warnings and errors is that a warning
253 does not force the return to command level. */
254
255 /* VARARGS */
256 void
257 #ifdef ANSI_PROTOTYPES
258 warning (char *string, ...)
259 #else
260 warning (va_alist)
261 va_dcl
262 #endif
263 {
264 va_list args;
265 #ifdef ANSI_PROTOTYPES
266 va_start (args, string);
267 #else
268 char *string;
269
270 va_start (args);
271 string = va_arg (args, char *);
272 #endif
273 warning_begin ();
274 vfprintf_unfiltered (gdb_stderr, string, args);
275 fprintf_unfiltered (gdb_stderr, "\n");
276 va_end (args);
277 }
278
279 /* Start the printing of an error message. Way to use this is to call
280 this, output the error message (use filtered output to gdb_stderr
281 (FIXME: Some callers, like memory_error, use gdb_stdout)), ending
282 in a newline, and then call return_to_top_level (RETURN_ERROR).
283 error() provides a convenient way to do this for the special case
284 that the error message can be formatted with a single printf call,
285 but this is more general. */
286 void
287 error_begin ()
288 {
289 target_terminal_ours ();
290 wrap_here (""); /* Force out any buffered output */
291 gdb_flush (gdb_stdout);
292
293 annotate_error_begin ();
294
295 if (error_pre_print)
296 fprintf_filtered (gdb_stderr, error_pre_print);
297 }
298
299 /* Print an error message and return to command level.
300 The first argument STRING is the error message, used as a fprintf string,
301 and the remaining args are passed as arguments to it. */
302
303 #ifdef ANSI_PROTOTYPES
304 NORETURN void
305 error (char *string, ...)
306 #else
307 error (va_alist)
308 va_dcl
309 #endif
310 {
311 #ifdef ANSI_PROTOTYPES
312 va_list args;
313 va_start (args, string);
314 #else
315 va_start (args);
316 #endif
317 if (error_hook)
318 error_hook();
319 else
320 {
321 char *string1;
322 error_begin ();
323 #ifdef ANSI_PROTOTYPES
324 vfprintf_filtered (gdb_stderr, string, args);
325 #else
326 string1 = va_arg (args, char *);
327 vfprintf_filtered (gdb_stderr, string1, args);
328 #endif
329 fprintf_filtered (gdb_stderr, "\n");
330 va_end (args);
331 return_to_top_level (RETURN_ERROR);
332 }
333 }
334
335
336 /* Print an error message and exit reporting failure.
337 This is for a error that we cannot continue from.
338 The arguments are printed a la printf.
339
340 This function cannot be declared volatile (NORETURN) in an
341 ANSI environment because exit() is not declared volatile. */
342
343 /* VARARGS */
344 NORETURN void
345 #ifdef ANSI_PROTOTYPES
346 fatal (char *string, ...)
347 #else
348 fatal (va_alist)
349 va_dcl
350 #endif
351 {
352 va_list args;
353 #ifdef ANSI_PROTOTYPES
354 va_start (args, string);
355 #else
356 char *string;
357 va_start (args);
358 string = va_arg (args, char *);
359 #endif
360 fprintf_unfiltered (gdb_stderr, "\ngdb: ");
361 vfprintf_unfiltered (gdb_stderr, string, args);
362 fprintf_unfiltered (gdb_stderr, "\n");
363 va_end (args);
364 exit (1);
365 }
366
367 /* Print an error message and exit, dumping core.
368 The arguments are printed a la printf (). */
369
370 /* VARARGS */
371 static void
372 #ifdef ANSI_PROTOTYPES
373 fatal_dump_core (char *string, ...)
374 #else
375 fatal_dump_core (va_alist)
376 va_dcl
377 #endif
378 {
379 va_list args;
380 #ifdef ANSI_PROTOTYPES
381 va_start (args, string);
382 #else
383 char *string;
384
385 va_start (args);
386 string = va_arg (args, char *);
387 #endif
388 /* "internal error" is always correct, since GDB should never dump
389 core, no matter what the input. */
390 fprintf_unfiltered (gdb_stderr, "\ngdb internal error: ");
391 vfprintf_unfiltered (gdb_stderr, string, args);
392 fprintf_unfiltered (gdb_stderr, "\n");
393 va_end (args);
394
395 signal (SIGQUIT, SIG_DFL);
396 kill (getpid (), SIGQUIT);
397 /* We should never get here, but just in case... */
398 exit (1);
399 }
400
401 /* The strerror() function can return NULL for errno values that are
402 out of range. Provide a "safe" version that always returns a
403 printable string. */
404
405 char *
406 safe_strerror (errnum)
407 int errnum;
408 {
409 char *msg;
410 static char buf[32];
411
412 if ((msg = strerror (errnum)) == NULL)
413 {
414 sprintf (buf, "(undocumented errno %d)", errnum);
415 msg = buf;
416 }
417 return (msg);
418 }
419
420 /* The strsignal() function can return NULL for signal values that are
421 out of range. Provide a "safe" version that always returns a
422 printable string. */
423
424 char *
425 safe_strsignal (signo)
426 int signo;
427 {
428 char *msg;
429 static char buf[32];
430
431 if ((msg = strsignal (signo)) == NULL)
432 {
433 sprintf (buf, "(undocumented signal %d)", signo);
434 msg = buf;
435 }
436 return (msg);
437 }
438
439
440 /* Print the system error message for errno, and also mention STRING
441 as the file name for which the error was encountered.
442 Then return to command level. */
443
444 void
445 perror_with_name (string)
446 char *string;
447 {
448 char *err;
449 char *combined;
450
451 err = safe_strerror (errno);
452 combined = (char *) alloca (strlen (err) + strlen (string) + 3);
453 strcpy (combined, string);
454 strcat (combined, ": ");
455 strcat (combined, err);
456
457 /* I understand setting these is a matter of taste. Still, some people
458 may clear errno but not know about bfd_error. Doing this here is not
459 unreasonable. */
460 bfd_set_error (bfd_error_no_error);
461 errno = 0;
462
463 error ("%s.", combined);
464 }
465
466 /* Print the system error message for ERRCODE, and also mention STRING
467 as the file name for which the error was encountered. */
468
469 void
470 print_sys_errmsg (string, errcode)
471 char *string;
472 int errcode;
473 {
474 char *err;
475 char *combined;
476
477 err = safe_strerror (errcode);
478 combined = (char *) alloca (strlen (err) + strlen (string) + 3);
479 strcpy (combined, string);
480 strcat (combined, ": ");
481 strcat (combined, err);
482
483 /* We want anything which was printed on stdout to come out first, before
484 this message. */
485 gdb_flush (gdb_stdout);
486 fprintf_unfiltered (gdb_stderr, "%s.\n", combined);
487 }
488
489 /* Control C eventually causes this to be called, at a convenient time. */
490
491 void
492 quit ()
493 {
494 serial_t gdb_stdout_serial = serial_fdopen (1);
495
496 target_terminal_ours ();
497
498 /* We want all output to appear now, before we print "Quit". We
499 have 3 levels of buffering we have to flush (it's possible that
500 some of these should be changed to flush the lower-level ones
501 too): */
502
503 /* 1. The _filtered buffer. */
504 wrap_here ((char *)0);
505
506 /* 2. The stdio buffer. */
507 gdb_flush (gdb_stdout);
508 gdb_flush (gdb_stderr);
509
510 /* 3. The system-level buffer. */
511 SERIAL_FLUSH_OUTPUT (gdb_stdout_serial);
512 SERIAL_UN_FDOPEN (gdb_stdout_serial);
513
514 annotate_error_begin ();
515
516 /* Don't use *_filtered; we don't want to prompt the user to continue. */
517 if (quit_pre_print)
518 fprintf_unfiltered (gdb_stderr, quit_pre_print);
519
520 if (job_control
521 /* If there is no terminal switching for this target, then we can't
522 possibly get screwed by the lack of job control. */
523 || current_target.to_terminal_ours == NULL)
524 fprintf_unfiltered (gdb_stderr, "Quit\n");
525 else
526 fprintf_unfiltered (gdb_stderr,
527 "Quit (expect signal SIGINT when the program is resumed)\n");
528 return_to_top_level (RETURN_QUIT);
529 }
530
531
532 #if defined(__GO32__)||defined(WIN32)
533
534 /* In the absence of signals, poll keyboard for a quit.
535 Called from #define QUIT pollquit() in xm-go32.h. */
536
537 void
538 pollquit()
539 {
540 if (kbhit ())
541 {
542 int k = getkey ();
543 if (k == 1) {
544 quit_flag = 1;
545 quit();
546 }
547 else if (k == 2) {
548 immediate_quit = 1;
549 quit ();
550 }
551 else
552 {
553 /* We just ignore it */
554 fprintf_unfiltered (gdb_stderr, "CTRL-A to quit, CTRL-B to quit harder\n");
555 }
556 }
557 }
558
559
560 #endif
561 #if defined(__GO32__)||defined(WIN32)
562 void notice_quit()
563 {
564 if (kbhit ())
565 {
566 int k = getkey ();
567 if (k == 1) {
568 quit_flag = 1;
569 }
570 else if (k == 2)
571 {
572 immediate_quit = 1;
573 }
574 else
575 {
576 fprintf_unfiltered (gdb_stderr, "CTRL-A to quit, CTRL-B to quit harder\n");
577 }
578 }
579 }
580 #else
581 void notice_quit()
582 {
583 /* Done by signals */
584 }
585 #endif
586 /* Control C comes here */
587
588 void
589 request_quit (signo)
590 int signo;
591 {
592 quit_flag = 1;
593
594 /* Restore the signal handler. Harmless with BSD-style signals, needed
595 for System V-style signals. So just always do it, rather than worrying
596 about USG defines and stuff like that. */
597 signal (signo, request_quit);
598
599 #ifdef REQUEST_QUIT
600 REQUEST_QUIT;
601 #else
602 if (immediate_quit)
603 quit ();
604 #endif
605 }
606
607 \f
608 /* Memory management stuff (malloc friends). */
609
610 #if defined (NO_MMALLOC)
611
612 /* Make a substitute size_t for non-ANSI compilers. */
613
614 #ifdef _AIX
615 #include <stddef.h>
616 #else /* Not AIX */
617 #ifndef __STDC__
618 #ifndef size_t
619 #define size_t unsigned int
620 #endif
621 #endif
622 #endif /* Not AIX */
623
624 PTR
625 mmalloc (md, size)
626 PTR md;
627 size_t size;
628 {
629 return malloc (size);
630 }
631
632 PTR
633 mrealloc (md, ptr, size)
634 PTR md;
635 PTR ptr;
636 size_t size;
637 {
638 if (ptr == 0) /* Guard against old realloc's */
639 return malloc (size);
640 else
641 return realloc (ptr, size);
642 }
643
644 void
645 mfree (md, ptr)
646 PTR md;
647 PTR ptr;
648 {
649 free (ptr);
650 }
651
652 #endif /* NO_MMALLOC */
653
654 #if defined (NO_MMALLOC) || defined (NO_MMALLOC_CHECK)
655
656 void
657 init_malloc (md)
658 PTR md;
659 {
660 }
661
662 #else /* have mmalloc and want corruption checking */
663
664 static void
665 malloc_botch ()
666 {
667 fatal_dump_core ("Memory corruption");
668 }
669
670 /* Attempt to install hooks in mmalloc/mrealloc/mfree for the heap specified
671 by MD, to detect memory corruption. Note that MD may be NULL to specify
672 the default heap that grows via sbrk.
673
674 Note that for freshly created regions, we must call mmcheck prior to any
675 mallocs in the region. Otherwise, any region which was allocated prior to
676 installing the checking hooks, which is later reallocated or freed, will
677 fail the checks! The mmcheck function only allows initial hooks to be
678 installed before the first mmalloc. However, anytime after we have called
679 mmcheck the first time to install the checking hooks, we can call it again
680 to update the function pointer to the memory corruption handler.
681
682 Returns zero on failure, non-zero on success. */
683
684 void
685 init_malloc (md)
686 PTR md;
687 {
688 if (!mmcheck (md, malloc_botch))
689 {
690 warning ("internal error: failed to install memory consistency checks");
691 }
692
693 mmtrace ();
694 }
695
696 #endif /* Have mmalloc and want corruption checking */
697
698 /* Called when a memory allocation fails, with the number of bytes of
699 memory requested in SIZE. */
700
701 NORETURN void
702 nomem (size)
703 long size;
704 {
705 if (size > 0)
706 {
707 fatal ("virtual memory exhausted: can't allocate %ld bytes.", size);
708 }
709 else
710 {
711 fatal ("virtual memory exhausted.");
712 }
713 }
714
715 /* Like mmalloc but get error if no storage available, and protect against
716 the caller wanting to allocate zero bytes. Whether to return NULL for
717 a zero byte request, or translate the request into a request for one
718 byte of zero'd storage, is a religious issue. */
719
720 PTR
721 xmmalloc (md, size)
722 PTR md;
723 long size;
724 {
725 register PTR val;
726
727 if (size == 0)
728 {
729 val = NULL;
730 }
731 else if ((val = mmalloc (md, size)) == NULL)
732 {
733 nomem (size);
734 }
735 return (val);
736 }
737
738 /* Like mrealloc but get error if no storage available. */
739
740 PTR
741 xmrealloc (md, ptr, size)
742 PTR md;
743 PTR ptr;
744 long size;
745 {
746 register PTR val;
747
748 if (ptr != NULL)
749 {
750 val = mrealloc (md, ptr, size);
751 }
752 else
753 {
754 val = mmalloc (md, size);
755 }
756 if (val == NULL)
757 {
758 nomem (size);
759 }
760 return (val);
761 }
762
763 /* Like malloc but get error if no storage available, and protect against
764 the caller wanting to allocate zero bytes. */
765
766 PTR
767 xmalloc (size)
768 long size;
769 {
770 return (xmmalloc ((PTR) NULL, size));
771 }
772
773 /* Like mrealloc but get error if no storage available. */
774
775 PTR
776 xrealloc (ptr, size)
777 PTR ptr;
778 long size;
779 {
780 return (xmrealloc ((PTR) NULL, ptr, size));
781 }
782
783 \f
784 /* My replacement for the read system call.
785 Used like `read' but keeps going if `read' returns too soon. */
786
787 int
788 myread (desc, addr, len)
789 int desc;
790 char *addr;
791 int len;
792 {
793 register int val;
794 int orglen = len;
795
796 while (len > 0)
797 {
798 val = read (desc, addr, len);
799 if (val < 0)
800 return val;
801 if (val == 0)
802 return orglen - len;
803 len -= val;
804 addr += val;
805 }
806 return orglen;
807 }
808 \f
809 /* Make a copy of the string at PTR with SIZE characters
810 (and add a null character at the end in the copy).
811 Uses malloc to get the space. Returns the address of the copy. */
812
813 char *
814 savestring (ptr, size)
815 const char *ptr;
816 int size;
817 {
818 register char *p = (char *) xmalloc (size + 1);
819 memcpy (p, ptr, size);
820 p[size] = 0;
821 return p;
822 }
823
824 char *
825 msavestring (md, ptr, size)
826 PTR md;
827 const char *ptr;
828 int size;
829 {
830 register char *p = (char *) xmmalloc (md, size + 1);
831 memcpy (p, ptr, size);
832 p[size] = 0;
833 return p;
834 }
835
836 /* The "const" is so it compiles under DGUX (which prototypes strsave
837 in <string.h>. FIXME: This should be named "xstrsave", shouldn't it?
838 Doesn't real strsave return NULL if out of memory? */
839 char *
840 strsave (ptr)
841 const char *ptr;
842 {
843 return savestring (ptr, strlen (ptr));
844 }
845
846 char *
847 mstrsave (md, ptr)
848 PTR md;
849 const char *ptr;
850 {
851 return (msavestring (md, ptr, strlen (ptr)));
852 }
853
854 void
855 print_spaces (n, file)
856 register int n;
857 register FILE *file;
858 {
859 while (n-- > 0)
860 fputc (' ', file);
861 }
862
863 /* Print a host address. */
864
865 void
866 gdb_print_address (addr, stream)
867 PTR addr;
868 GDB_FILE *stream;
869 {
870
871 /* We could use the %p conversion specifier to fprintf if we had any
872 way of knowing whether this host supports it. But the following
873 should work on the Alpha and on 32 bit machines. */
874
875 fprintf_filtered (stream, "0x%lx", (unsigned long)addr);
876 }
877
878 /* Ask user a y-or-n question and return 1 iff answer is yes.
879 Takes three args which are given to printf to print the question.
880 The first, a control string, should end in "? ".
881 It should not say how to answer, because we do that. */
882
883 /* VARARGS */
884 int
885 #ifdef ANSI_PROTOTYPES
886 query (char *ctlstr, ...)
887 #else
888 query (va_alist)
889 va_dcl
890 #endif
891 {
892 va_list args;
893 register int answer;
894 register int ans2;
895 int retval;
896
897 #ifdef ANSI_PROTOTYPES
898 va_start (args, ctlstr);
899 #else
900 char *ctlstr;
901 va_start (args);
902 ctlstr = va_arg (args, char *);
903 #endif
904
905 if (query_hook)
906 {
907 return query_hook (ctlstr, args);
908 }
909
910 /* Automatically answer "yes" if input is not from a terminal. */
911 if (!input_from_terminal_p ())
912 return 1;
913 #ifdef MPW
914 /* FIXME Automatically answer "yes" if called from MacGDB. */
915 if (mac_app)
916 return 1;
917 #endif /* MPW */
918
919 while (1)
920 {
921 wrap_here (""); /* Flush any buffered output */
922 gdb_flush (gdb_stdout);
923
924 if (annotation_level > 1)
925 printf_filtered ("\n\032\032pre-query\n");
926
927 vfprintf_filtered (gdb_stdout, ctlstr, args);
928 printf_filtered ("(y or n) ");
929
930 if (annotation_level > 1)
931 printf_filtered ("\n\032\032query\n");
932
933 #ifdef MPW
934 /* If not in MacGDB, move to a new line so the entered line doesn't
935 have a prompt on the front of it. */
936 if (!mac_app)
937 fputs_unfiltered ("\n", gdb_stdout);
938 #endif /* MPW */
939
940 gdb_flush (gdb_stdout);
941 answer = fgetc (stdin);
942 clearerr (stdin); /* in case of C-d */
943 if (answer == EOF) /* C-d */
944 {
945 retval = 1;
946 break;
947 }
948 if (answer != '\n') /* Eat rest of input line, to EOF or newline */
949 do
950 {
951 ans2 = fgetc (stdin);
952 clearerr (stdin);
953 }
954 while (ans2 != EOF && ans2 != '\n');
955 if (answer >= 'a')
956 answer -= 040;
957 if (answer == 'Y')
958 {
959 retval = 1;
960 break;
961 }
962 if (answer == 'N')
963 {
964 retval = 0;
965 break;
966 }
967 printf_filtered ("Please answer y or n.\n");
968 }
969
970 if (annotation_level > 1)
971 printf_filtered ("\n\032\032post-query\n");
972 return retval;
973 }
974
975 \f
976 /* Parse a C escape sequence. STRING_PTR points to a variable
977 containing a pointer to the string to parse. That pointer
978 should point to the character after the \. That pointer
979 is updated past the characters we use. The value of the
980 escape sequence is returned.
981
982 A negative value means the sequence \ newline was seen,
983 which is supposed to be equivalent to nothing at all.
984
985 If \ is followed by a null character, we return a negative
986 value and leave the string pointer pointing at the null character.
987
988 If \ is followed by 000, we return 0 and leave the string pointer
989 after the zeros. A value of 0 does not mean end of string. */
990
991 int
992 parse_escape (string_ptr)
993 char **string_ptr;
994 {
995 register int c = *(*string_ptr)++;
996 switch (c)
997 {
998 case 'a':
999 return 007; /* Bell (alert) char */
1000 case 'b':
1001 return '\b';
1002 case 'e': /* Escape character */
1003 return 033;
1004 case 'f':
1005 return '\f';
1006 case 'n':
1007 return '\n';
1008 case 'r':
1009 return '\r';
1010 case 't':
1011 return '\t';
1012 case 'v':
1013 return '\v';
1014 case '\n':
1015 return -2;
1016 case 0:
1017 (*string_ptr)--;
1018 return 0;
1019 case '^':
1020 c = *(*string_ptr)++;
1021 if (c == '\\')
1022 c = parse_escape (string_ptr);
1023 if (c == '?')
1024 return 0177;
1025 return (c & 0200) | (c & 037);
1026
1027 case '0':
1028 case '1':
1029 case '2':
1030 case '3':
1031 case '4':
1032 case '5':
1033 case '6':
1034 case '7':
1035 {
1036 register int i = c - '0';
1037 register int count = 0;
1038 while (++count < 3)
1039 {
1040 if ((c = *(*string_ptr)++) >= '0' && c <= '7')
1041 {
1042 i *= 8;
1043 i += c - '0';
1044 }
1045 else
1046 {
1047 (*string_ptr)--;
1048 break;
1049 }
1050 }
1051 return i;
1052 }
1053 default:
1054 return c;
1055 }
1056 }
1057 \f
1058 /* Print the character C on STREAM as part of the contents of a literal
1059 string whose delimiter is QUOTER. Note that this routine should only
1060 be call for printing things which are independent of the language
1061 of the program being debugged. */
1062
1063 void
1064 gdb_printchar (c, stream, quoter)
1065 register int c;
1066 FILE *stream;
1067 int quoter;
1068 {
1069
1070 c &= 0xFF; /* Avoid sign bit follies */
1071
1072 if ( c < 0x20 || /* Low control chars */
1073 (c >= 0x7F && c < 0xA0) || /* DEL, High controls */
1074 (sevenbit_strings && c >= 0x80)) { /* high order bit set */
1075 switch (c)
1076 {
1077 case '\n':
1078 fputs_filtered ("\\n", stream);
1079 break;
1080 case '\b':
1081 fputs_filtered ("\\b", stream);
1082 break;
1083 case '\t':
1084 fputs_filtered ("\\t", stream);
1085 break;
1086 case '\f':
1087 fputs_filtered ("\\f", stream);
1088 break;
1089 case '\r':
1090 fputs_filtered ("\\r", stream);
1091 break;
1092 case '\033':
1093 fputs_filtered ("\\e", stream);
1094 break;
1095 case '\007':
1096 fputs_filtered ("\\a", stream);
1097 break;
1098 default:
1099 fprintf_filtered (stream, "\\%.3o", (unsigned int) c);
1100 break;
1101 }
1102 } else {
1103 if (c == '\\' || c == quoter)
1104 fputs_filtered ("\\", stream);
1105 fprintf_filtered (stream, "%c", c);
1106 }
1107 }
1108 \f
1109 /* Number of lines per page or UINT_MAX if paging is disabled. */
1110 static unsigned int lines_per_page;
1111 /* Number of chars per line or UNIT_MAX is line folding is disabled. */
1112 static unsigned int chars_per_line;
1113 /* Current count of lines printed on this page, chars on this line. */
1114 static unsigned int lines_printed, chars_printed;
1115
1116 /* Buffer and start column of buffered text, for doing smarter word-
1117 wrapping. When someone calls wrap_here(), we start buffering output
1118 that comes through fputs_filtered(). If we see a newline, we just
1119 spit it out and forget about the wrap_here(). If we see another
1120 wrap_here(), we spit it out and remember the newer one. If we see
1121 the end of the line, we spit out a newline, the indent, and then
1122 the buffered output. */
1123
1124 /* Malloc'd buffer with chars_per_line+2 bytes. Contains characters which
1125 are waiting to be output (they have already been counted in chars_printed).
1126 When wrap_buffer[0] is null, the buffer is empty. */
1127 static char *wrap_buffer;
1128
1129 /* Pointer in wrap_buffer to the next character to fill. */
1130 static char *wrap_pointer;
1131
1132 /* String to indent by if the wrap occurs. Must not be NULL if wrap_column
1133 is non-zero. */
1134 static char *wrap_indent;
1135
1136 /* Column number on the screen where wrap_buffer begins, or 0 if wrapping
1137 is not in effect. */
1138 static int wrap_column;
1139
1140 /* ARGSUSED */
1141 static void
1142 set_width_command (args, from_tty, c)
1143 char *args;
1144 int from_tty;
1145 struct cmd_list_element *c;
1146 {
1147 if (!wrap_buffer)
1148 {
1149 wrap_buffer = (char *) xmalloc (chars_per_line + 2);
1150 wrap_buffer[0] = '\0';
1151 }
1152 else
1153 wrap_buffer = (char *) xrealloc (wrap_buffer, chars_per_line + 2);
1154 wrap_pointer = wrap_buffer; /* Start it at the beginning */
1155 }
1156
1157 /* Wait, so the user can read what's on the screen. Prompt the user
1158 to continue by pressing RETURN. */
1159
1160 static void
1161 prompt_for_continue ()
1162 {
1163 char *ignore;
1164 char cont_prompt[120];
1165
1166 if (annotation_level > 1)
1167 printf_unfiltered ("\n\032\032pre-prompt-for-continue\n");
1168
1169 strcpy (cont_prompt,
1170 "---Type <return> to continue, or q <return> to quit---");
1171 if (annotation_level > 1)
1172 strcat (cont_prompt, "\n\032\032prompt-for-continue\n");
1173
1174 /* We must do this *before* we call gdb_readline, else it will eventually
1175 call us -- thinking that we're trying to print beyond the end of the
1176 screen. */
1177 reinitialize_more_filter ();
1178
1179 immediate_quit++;
1180 /* On a real operating system, the user can quit with SIGINT.
1181 But not on GO32.
1182
1183 'q' is provided on all systems so users don't have to change habits
1184 from system to system, and because telling them what to do in
1185 the prompt is more user-friendly than expecting them to think of
1186 SIGINT. */
1187 /* Call readline, not gdb_readline, because GO32 readline handles control-C
1188 whereas control-C to gdb_readline will cause the user to get dumped
1189 out to DOS. */
1190 ignore = readline (cont_prompt);
1191
1192 if (annotation_level > 1)
1193 printf_unfiltered ("\n\032\032post-prompt-for-continue\n");
1194
1195 if (ignore)
1196 {
1197 char *p = ignore;
1198 while (*p == ' ' || *p == '\t')
1199 ++p;
1200 if (p[0] == 'q')
1201 request_quit (SIGINT);
1202 free (ignore);
1203 }
1204 immediate_quit--;
1205
1206 /* Now we have to do this again, so that GDB will know that it doesn't
1207 need to save the ---Type <return>--- line at the top of the screen. */
1208 reinitialize_more_filter ();
1209
1210 dont_repeat (); /* Forget prev cmd -- CR won't repeat it. */
1211 }
1212
1213 /* Reinitialize filter; ie. tell it to reset to original values. */
1214
1215 void
1216 reinitialize_more_filter ()
1217 {
1218 lines_printed = 0;
1219 chars_printed = 0;
1220 }
1221
1222 /* Indicate that if the next sequence of characters overflows the line,
1223 a newline should be inserted here rather than when it hits the end.
1224 If INDENT is non-null, it is a string to be printed to indent the
1225 wrapped part on the next line. INDENT must remain accessible until
1226 the next call to wrap_here() or until a newline is printed through
1227 fputs_filtered().
1228
1229 If the line is already overfull, we immediately print a newline and
1230 the indentation, and disable further wrapping.
1231
1232 If we don't know the width of lines, but we know the page height,
1233 we must not wrap words, but should still keep track of newlines
1234 that were explicitly printed.
1235
1236 INDENT should not contain tabs, as that will mess up the char count
1237 on the next line. FIXME.
1238
1239 This routine is guaranteed to force out any output which has been
1240 squirreled away in the wrap_buffer, so wrap_here ((char *)0) can be
1241 used to force out output from the wrap_buffer. */
1242
1243 void
1244 wrap_here(indent)
1245 char *indent;
1246 {
1247 /* This should have been allocated, but be paranoid anyway. */
1248 if (!wrap_buffer)
1249 abort ();
1250
1251 if (wrap_buffer[0])
1252 {
1253 *wrap_pointer = '\0';
1254 fputs_unfiltered (wrap_buffer, gdb_stdout);
1255 }
1256 wrap_pointer = wrap_buffer;
1257 wrap_buffer[0] = '\0';
1258 if (chars_per_line == UINT_MAX) /* No line overflow checking */
1259 {
1260 wrap_column = 0;
1261 }
1262 else if (chars_printed >= chars_per_line)
1263 {
1264 puts_filtered ("\n");
1265 if (indent != NULL)
1266 puts_filtered (indent);
1267 wrap_column = 0;
1268 }
1269 else
1270 {
1271 wrap_column = chars_printed;
1272 if (indent == NULL)
1273 wrap_indent = "";
1274 else
1275 wrap_indent = indent;
1276 }
1277 }
1278
1279 /* Ensure that whatever gets printed next, using the filtered output
1280 commands, starts at the beginning of the line. I.E. if there is
1281 any pending output for the current line, flush it and start a new
1282 line. Otherwise do nothing. */
1283
1284 void
1285 begin_line ()
1286 {
1287 if (chars_printed > 0)
1288 {
1289 puts_filtered ("\n");
1290 }
1291 }
1292
1293
1294 GDB_FILE *
1295 gdb_fopen (name, mode)
1296 char * name;
1297 char * mode;
1298 {
1299 return fopen (name, mode);
1300 }
1301
1302 void
1303 gdb_flush (stream)
1304 FILE *stream;
1305 {
1306 if (flush_hook)
1307 {
1308 flush_hook (stream);
1309 return;
1310 }
1311
1312 fflush (stream);
1313 }
1314
1315 /* Like fputs but if FILTER is true, pause after every screenful.
1316
1317 Regardless of FILTER can wrap at points other than the final
1318 character of a line.
1319
1320 Unlike fputs, fputs_maybe_filtered does not return a value.
1321 It is OK for LINEBUFFER to be NULL, in which case just don't print
1322 anything.
1323
1324 Note that a longjmp to top level may occur in this routine (only if
1325 FILTER is true) (since prompt_for_continue may do so) so this
1326 routine should not be called when cleanups are not in place. */
1327
1328 static void
1329 fputs_maybe_filtered (linebuffer, stream, filter)
1330 const char *linebuffer;
1331 FILE *stream;
1332 int filter;
1333 {
1334 const char *lineptr;
1335
1336 if (linebuffer == 0)
1337 return;
1338
1339 /* Don't do any filtering if it is disabled. */
1340 if (stream != gdb_stdout
1341 || (lines_per_page == UINT_MAX && chars_per_line == UINT_MAX))
1342 {
1343 fputs_unfiltered (linebuffer, stream);
1344 return;
1345 }
1346
1347 /* Go through and output each character. Show line extension
1348 when this is necessary; prompt user for new page when this is
1349 necessary. */
1350
1351 lineptr = linebuffer;
1352 while (*lineptr)
1353 {
1354 /* Possible new page. */
1355 if (filter &&
1356 (lines_printed >= lines_per_page - 1))
1357 prompt_for_continue ();
1358
1359 while (*lineptr && *lineptr != '\n')
1360 {
1361 /* Print a single line. */
1362 if (*lineptr == '\t')
1363 {
1364 if (wrap_column)
1365 *wrap_pointer++ = '\t';
1366 else
1367 fputc_unfiltered ('\t', stream);
1368 /* Shifting right by 3 produces the number of tab stops
1369 we have already passed, and then adding one and
1370 shifting left 3 advances to the next tab stop. */
1371 chars_printed = ((chars_printed >> 3) + 1) << 3;
1372 lineptr++;
1373 }
1374 else
1375 {
1376 if (wrap_column)
1377 *wrap_pointer++ = *lineptr;
1378 else
1379 fputc_unfiltered (*lineptr, stream);
1380 chars_printed++;
1381 lineptr++;
1382 }
1383
1384 if (chars_printed >= chars_per_line)
1385 {
1386 unsigned int save_chars = chars_printed;
1387
1388 chars_printed = 0;
1389 lines_printed++;
1390 /* If we aren't actually wrapping, don't output newline --
1391 if chars_per_line is right, we probably just overflowed
1392 anyway; if it's wrong, let us keep going. */
1393 if (wrap_column)
1394 fputc_unfiltered ('\n', stream);
1395
1396 /* Possible new page. */
1397 if (lines_printed >= lines_per_page - 1)
1398 prompt_for_continue ();
1399
1400 /* Now output indentation and wrapped string */
1401 if (wrap_column)
1402 {
1403 fputs_unfiltered (wrap_indent, stream);
1404 *wrap_pointer = '\0'; /* Null-terminate saved stuff */
1405 fputs_unfiltered (wrap_buffer, stream); /* and eject it */
1406 /* FIXME, this strlen is what prevents wrap_indent from
1407 containing tabs. However, if we recurse to print it
1408 and count its chars, we risk trouble if wrap_indent is
1409 longer than (the user settable) chars_per_line.
1410 Note also that this can set chars_printed > chars_per_line
1411 if we are printing a long string. */
1412 chars_printed = strlen (wrap_indent)
1413 + (save_chars - wrap_column);
1414 wrap_pointer = wrap_buffer; /* Reset buffer */
1415 wrap_buffer[0] = '\0';
1416 wrap_column = 0; /* And disable fancy wrap */
1417 }
1418 }
1419 }
1420
1421 if (*lineptr == '\n')
1422 {
1423 chars_printed = 0;
1424 wrap_here ((char *)0); /* Spit out chars, cancel further wraps */
1425 lines_printed++;
1426 fputc_unfiltered ('\n', stream);
1427 lineptr++;
1428 }
1429 }
1430 }
1431
1432 void
1433 fputs_filtered (linebuffer, stream)
1434 const char *linebuffer;
1435 FILE *stream;
1436 {
1437 fputs_maybe_filtered (linebuffer, stream, 1);
1438 }
1439
1440 int
1441 putchar_unfiltered (c)
1442 int c;
1443 {
1444 char buf[2];
1445
1446 buf[0] = c;
1447 buf[1] = 0;
1448 fputs_unfiltered (buf, gdb_stdout);
1449 return c;
1450 }
1451
1452 int
1453 fputc_unfiltered (c, stream)
1454 int c;
1455 FILE * stream;
1456 {
1457 char buf[2];
1458
1459 buf[0] = c;
1460 buf[1] = 0;
1461 fputs_unfiltered (buf, stream);
1462 return c;
1463 }
1464
1465
1466 /* Print a variable number of ARGS using format FORMAT. If this
1467 information is going to put the amount written (since the last call
1468 to REINITIALIZE_MORE_FILTER or the last page break) over the page size,
1469 call prompt_for_continue to get the users permision to continue.
1470
1471 Unlike fprintf, this function does not return a value.
1472
1473 We implement three variants, vfprintf (takes a vararg list and stream),
1474 fprintf (takes a stream to write on), and printf (the usual).
1475
1476 Note also that a longjmp to top level may occur in this routine
1477 (since prompt_for_continue may do so) so this routine should not be
1478 called when cleanups are not in place. */
1479
1480 static void
1481 vfprintf_maybe_filtered (stream, format, args, filter)
1482 FILE *stream;
1483 char *format;
1484 va_list args;
1485 int filter;
1486 {
1487 char *linebuffer;
1488 struct cleanup *old_cleanups;
1489
1490 vasprintf (&linebuffer, format, args);
1491 if (linebuffer == NULL)
1492 {
1493 fputs_unfiltered ("\ngdb: virtual memory exhausted.\n", gdb_stderr);
1494 exit (1);
1495 }
1496 old_cleanups = make_cleanup (free, linebuffer);
1497 fputs_maybe_filtered (linebuffer, stream, filter);
1498 do_cleanups (old_cleanups);
1499 }
1500
1501
1502 void
1503 vfprintf_filtered (stream, format, args)
1504 FILE *stream;
1505 char *format;
1506 va_list args;
1507 {
1508 vfprintf_maybe_filtered (stream, format, args, 1);
1509 }
1510
1511 void
1512 vfprintf_unfiltered (stream, format, args)
1513 FILE *stream;
1514 char *format;
1515 va_list args;
1516 {
1517 char *linebuffer;
1518 struct cleanup *old_cleanups;
1519
1520 vasprintf (&linebuffer, format, args);
1521 if (linebuffer == NULL)
1522 {
1523 fputs_unfiltered ("\ngdb: virtual memory exhausted.\n", gdb_stderr);
1524 exit (1);
1525 }
1526 old_cleanups = make_cleanup (free, linebuffer);
1527 fputs_unfiltered (linebuffer, stream);
1528 do_cleanups (old_cleanups);
1529 }
1530
1531 void
1532 vprintf_filtered (format, args)
1533 char *format;
1534 va_list args;
1535 {
1536 vfprintf_maybe_filtered (gdb_stdout, format, args, 1);
1537 }
1538
1539 void
1540 vprintf_unfiltered (format, args)
1541 char *format;
1542 va_list args;
1543 {
1544 vfprintf_unfiltered (gdb_stdout, format, args);
1545 }
1546
1547 /* VARARGS */
1548 void
1549 #ifdef ANSI_PROTOTYPES
1550 fprintf_filtered (FILE *stream, char *format, ...)
1551 #else
1552 fprintf_filtered (va_alist)
1553 va_dcl
1554 #endif
1555 {
1556 va_list args;
1557 #ifdef ANSI_PROTOTYPES
1558 va_start (args, format);
1559 #else
1560 FILE *stream;
1561 char *format;
1562
1563 va_start (args);
1564 stream = va_arg (args, FILE *);
1565 format = va_arg (args, char *);
1566 #endif
1567 vfprintf_filtered (stream, format, args);
1568 va_end (args);
1569 }
1570
1571 /* VARARGS */
1572 void
1573 #ifdef ANSI_PROTOTYPES
1574 fprintf_unfiltered (FILE *stream, char *format, ...)
1575 #else
1576 fprintf_unfiltered (va_alist)
1577 va_dcl
1578 #endif
1579 {
1580 va_list args;
1581 #ifdef ANSI_PROTOTYPES
1582 va_start (args, format);
1583 #else
1584 FILE *stream;
1585 char *format;
1586
1587 va_start (args);
1588 stream = va_arg (args, FILE *);
1589 format = va_arg (args, char *);
1590 #endif
1591 vfprintf_unfiltered (stream, format, args);
1592 va_end (args);
1593 }
1594
1595 /* Like fprintf_filtered, but prints its result indented.
1596 Called as fprintfi_filtered (spaces, stream, format, ...); */
1597
1598 /* VARARGS */
1599 void
1600 #ifdef ANSI_PROTOTYPES
1601 fprintfi_filtered (int spaces, FILE *stream, char *format, ...)
1602 #else
1603 fprintfi_filtered (va_alist)
1604 va_dcl
1605 #endif
1606 {
1607 va_list args;
1608 #ifdef ANSI_PROTOTYPES
1609 va_start (args, format);
1610 #else
1611 int spaces;
1612 FILE *stream;
1613 char *format;
1614
1615 va_start (args);
1616 spaces = va_arg (args, int);
1617 stream = va_arg (args, FILE *);
1618 format = va_arg (args, char *);
1619 #endif
1620 print_spaces_filtered (spaces, stream);
1621
1622 vfprintf_filtered (stream, format, args);
1623 va_end (args);
1624 }
1625
1626
1627 /* VARARGS */
1628 void
1629 #ifdef ANSI_PROTOTYPES
1630 printf_filtered (char *format, ...)
1631 #else
1632 printf_filtered (va_alist)
1633 va_dcl
1634 #endif
1635 {
1636 va_list args;
1637 #ifdef ANSI_PROTOTYPES
1638 va_start (args, format);
1639 #else
1640 char *format;
1641
1642 va_start (args);
1643 format = va_arg (args, char *);
1644 #endif
1645 vfprintf_filtered (gdb_stdout, format, args);
1646 va_end (args);
1647 }
1648
1649
1650 /* VARARGS */
1651 void
1652 #ifdef ANSI_PROTOTYPES
1653 printf_unfiltered (char *format, ...)
1654 #else
1655 printf_unfiltered (va_alist)
1656 va_dcl
1657 #endif
1658 {
1659 va_list args;
1660 #ifdef ANSI_PROTOTYPES
1661 va_start (args, format);
1662 #else
1663 char *format;
1664
1665 va_start (args);
1666 format = va_arg (args, char *);
1667 #endif
1668 vfprintf_unfiltered (gdb_stdout, format, args);
1669 va_end (args);
1670 }
1671
1672 /* Like printf_filtered, but prints it's result indented.
1673 Called as printfi_filtered (spaces, format, ...); */
1674
1675 /* VARARGS */
1676 void
1677 #ifdef ANSI_PROTOTYPES
1678 printfi_filtered (int spaces, char *format, ...)
1679 #else
1680 printfi_filtered (va_alist)
1681 va_dcl
1682 #endif
1683 {
1684 va_list args;
1685 #ifdef ANSI_PROTOTYPES
1686 va_start (args, format);
1687 #else
1688 int spaces;
1689 char *format;
1690
1691 va_start (args);
1692 spaces = va_arg (args, int);
1693 format = va_arg (args, char *);
1694 #endif
1695 print_spaces_filtered (spaces, gdb_stdout);
1696 vfprintf_filtered (gdb_stdout, format, args);
1697 va_end (args);
1698 }
1699
1700 /* Easy -- but watch out!
1701
1702 This routine is *not* a replacement for puts()! puts() appends a newline.
1703 This one doesn't, and had better not! */
1704
1705 void
1706 puts_filtered (string)
1707 char *string;
1708 {
1709 fputs_filtered (string, gdb_stdout);
1710 }
1711
1712 void
1713 puts_unfiltered (string)
1714 char *string;
1715 {
1716 fputs_unfiltered (string, gdb_stdout);
1717 }
1718
1719 /* Return a pointer to N spaces and a null. The pointer is good
1720 until the next call to here. */
1721 char *
1722 n_spaces (n)
1723 int n;
1724 {
1725 register char *t;
1726 static char *spaces;
1727 static int max_spaces;
1728
1729 if (n > max_spaces)
1730 {
1731 if (spaces)
1732 free (spaces);
1733 spaces = (char *) xmalloc (n+1);
1734 for (t = spaces+n; t != spaces;)
1735 *--t = ' ';
1736 spaces[n] = '\0';
1737 max_spaces = n;
1738 }
1739
1740 return spaces + max_spaces - n;
1741 }
1742
1743 /* Print N spaces. */
1744 void
1745 print_spaces_filtered (n, stream)
1746 int n;
1747 FILE *stream;
1748 {
1749 fputs_filtered (n_spaces (n), stream);
1750 }
1751 \f
1752 /* C++ demangler stuff. */
1753
1754 /* fprintf_symbol_filtered attempts to demangle NAME, a symbol in language
1755 LANG, using demangling args ARG_MODE, and print it filtered to STREAM.
1756 If the name is not mangled, or the language for the name is unknown, or
1757 demangling is off, the name is printed in its "raw" form. */
1758
1759 void
1760 fprintf_symbol_filtered (stream, name, lang, arg_mode)
1761 FILE *stream;
1762 char *name;
1763 enum language lang;
1764 int arg_mode;
1765 {
1766 char *demangled;
1767
1768 if (name != NULL)
1769 {
1770 /* If user wants to see raw output, no problem. */
1771 if (!demangle)
1772 {
1773 fputs_filtered (name, stream);
1774 }
1775 else
1776 {
1777 switch (lang)
1778 {
1779 case language_cplus:
1780 demangled = cplus_demangle (name, arg_mode);
1781 break;
1782 case language_chill:
1783 demangled = chill_demangle (name);
1784 break;
1785 default:
1786 demangled = NULL;
1787 break;
1788 }
1789 fputs_filtered (demangled ? demangled : name, stream);
1790 if (demangled != NULL)
1791 {
1792 free (demangled);
1793 }
1794 }
1795 }
1796 }
1797
1798 /* Do a strcmp() type operation on STRING1 and STRING2, ignoring any
1799 differences in whitespace. Returns 0 if they match, non-zero if they
1800 don't (slightly different than strcmp()'s range of return values).
1801
1802 As an extra hack, string1=="FOO(ARGS)" matches string2=="FOO".
1803 This "feature" is useful when searching for matching C++ function names
1804 (such as if the user types 'break FOO', where FOO is a mangled C++
1805 function). */
1806
1807 int
1808 strcmp_iw (string1, string2)
1809 const char *string1;
1810 const char *string2;
1811 {
1812 while ((*string1 != '\0') && (*string2 != '\0'))
1813 {
1814 while (isspace (*string1))
1815 {
1816 string1++;
1817 }
1818 while (isspace (*string2))
1819 {
1820 string2++;
1821 }
1822 if (*string1 != *string2)
1823 {
1824 break;
1825 }
1826 if (*string1 != '\0')
1827 {
1828 string1++;
1829 string2++;
1830 }
1831 }
1832 return (*string1 != '\0' && *string1 != '(') || (*string2 != '\0');
1833 }
1834
1835 \f
1836 void
1837 initialize_utils ()
1838 {
1839 struct cmd_list_element *c;
1840
1841 c = add_set_cmd ("width", class_support, var_uinteger,
1842 (char *)&chars_per_line,
1843 "Set number of characters gdb thinks are in a line.",
1844 &setlist);
1845 add_show_from_set (c, &showlist);
1846 c->function.sfunc = set_width_command;
1847
1848 add_show_from_set
1849 (add_set_cmd ("height", class_support,
1850 var_uinteger, (char *)&lines_per_page,
1851 "Set number of lines gdb thinks are in a page.", &setlist),
1852 &showlist);
1853
1854 /* These defaults will be used if we are unable to get the correct
1855 values from termcap. */
1856 #if defined(__GO32__) || defined(WIN32)
1857 lines_per_page = ScreenRows();
1858 chars_per_line = ScreenCols();
1859 #else
1860 lines_per_page = 24;
1861 chars_per_line = 80;
1862
1863 #ifndef MPW
1864 /* No termcap under MPW, although might be cool to do something
1865 by looking at worksheet or console window sizes. */
1866 /* Initialize the screen height and width from termcap. */
1867 {
1868 char *termtype = getenv ("TERM");
1869
1870 /* Positive means success, nonpositive means failure. */
1871 int status;
1872
1873 /* 2048 is large enough for all known terminals, according to the
1874 GNU termcap manual. */
1875 char term_buffer[2048];
1876
1877 if (termtype)
1878 {
1879 status = tgetent (term_buffer, termtype);
1880 if (status > 0)
1881 {
1882 int val;
1883
1884 val = tgetnum ("li");
1885 if (val >= 0)
1886 lines_per_page = val;
1887 else
1888 /* The number of lines per page is not mentioned
1889 in the terminal description. This probably means
1890 that paging is not useful (e.g. emacs shell window),
1891 so disable paging. */
1892 lines_per_page = UINT_MAX;
1893
1894 val = tgetnum ("co");
1895 if (val >= 0)
1896 chars_per_line = val;
1897 }
1898 }
1899 }
1900 #endif /* MPW */
1901
1902 #if defined(SIGWINCH) && defined(SIGWINCH_HANDLER)
1903
1904 /* If there is a better way to determine the window size, use it. */
1905 SIGWINCH_HANDLER ();
1906 #endif
1907 #endif
1908 /* If the output is not a terminal, don't paginate it. */
1909 if (!ISATTY (gdb_stdout))
1910 lines_per_page = UINT_MAX;
1911
1912 set_width_command ((char *)NULL, 0, c);
1913
1914 add_show_from_set
1915 (add_set_cmd ("demangle", class_support, var_boolean,
1916 (char *)&demangle,
1917 "Set demangling of encoded C++ names when displaying symbols.",
1918 &setprintlist),
1919 &showprintlist);
1920
1921 add_show_from_set
1922 (add_set_cmd ("sevenbit-strings", class_support, var_boolean,
1923 (char *)&sevenbit_strings,
1924 "Set printing of 8-bit characters in strings as \\nnn.",
1925 &setprintlist),
1926 &showprintlist);
1927
1928 add_show_from_set
1929 (add_set_cmd ("asm-demangle", class_support, var_boolean,
1930 (char *)&asm_demangle,
1931 "Set demangling of C++ names in disassembly listings.",
1932 &setprintlist),
1933 &showprintlist);
1934 }
1935
1936 /* Machine specific function to handle SIGWINCH signal. */
1937
1938 #ifdef SIGWINCH_HANDLER_BODY
1939 SIGWINCH_HANDLER_BODY
1940 #endif
1941