* aoutx.h (add_to_stringtab): Use BFD_ASSERT not assert. This
[deliverable/binutils-gdb.git] / gdb / utils.c
CommitLineData
bd5635a1 1/* General utility routines for GDB, the GNU debugger.
7919c3ed 2 Copyright 1986, 1989, 1990, 1991, 1992 Free Software Foundation, Inc.
bd5635a1
RP
3
4This file is part of GDB.
5
351b221d 6This program is free software; you can redistribute it and/or modify
bd5635a1 7it under the terms of the GNU General Public License as published by
351b221d
JG
8the Free Software Foundation; either version 2 of the License, or
9(at your option) any later version.
bd5635a1 10
351b221d 11This program is distributed in the hope that it will be useful,
bd5635a1
RP
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
351b221d
JG
17along with this program; if not, write to the Free Software
18Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
bd5635a1 19
d747e0af 20#include "defs.h"
51b57ded 21#if !defined(__GO32__)
bd5635a1
RP
22#include <sys/ioctl.h>
23#include <sys/param.h>
24#include <pwd.h>
51b57ded 25#endif
2bc2e684
FF
26#include <varargs.h>
27#include <ctype.h>
28#include <string.h>
29
bd5635a1
RP
30#include "signals.h"
31#include "gdbcmd.h"
159dd2aa 32#include "serial.h"
199b2450 33#include "terminal.h" /* For job_control */
bd5635a1
RP
34#include "bfd.h"
35#include "target.h"
bcf2e6ab 36#include "demangle.h"
bd5d07d9
FF
37#include "expression.h"
38#include "language.h"
1c95d7ab 39#include "annotate.h"
bd5635a1 40
d8742f46
JK
41#include "readline.h"
42
43/* readline defines this. */
44#undef savestring
45
7919c3ed
JG
46/* Prototypes for local functions */
47
65ce5df4
JG
48#if defined (NO_MMALLOC) || defined (NO_MMALLOC_CHECK)
49#else
3624c875 50
7919c3ed
JG
51static void
52malloc_botch PARAMS ((void));
3624c875 53
65ce5df4 54#endif /* NO_MMALLOC, etc */
7919c3ed
JG
55
56static void
57fatal_dump_core (); /* Can't prototype with <varargs.h> usage... */
58
59static void
60prompt_for_continue PARAMS ((void));
61
62static void
63set_width_command PARAMS ((char *, int, struct cmd_list_element *));
64
bd5635a1
RP
65/* If this definition isn't overridden by the header files, assume
66 that isatty and fileno exist on this system. */
67#ifndef ISATTY
68#define ISATTY(FP) (isatty (fileno (FP)))
69#endif
70
bd5635a1
RP
71/* Chain of cleanup actions established with make_cleanup,
72 to be executed if an error happens. */
73
74static struct cleanup *cleanup_chain;
75
76/* Nonzero means a quit has been requested. */
77
78int quit_flag;
79
159dd2aa
JK
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. */
bd5635a1
RP
90
91int immediate_quit;
92
93/* Nonzero means that encoded C++ names should be printed out in their
94 C++ form rather than raw. */
95
96int demangle = 1;
97
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. */
101
102int asm_demangle = 0;
103
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.) */
107
108int sevenbit_strings = 0;
81066208
JG
109
110/* String to be printed before error messages, if any. */
111
112char *error_pre_print;
3624c875 113char *warning_pre_print = "\nwarning: ";
bd5635a1
RP
114\f
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. */
119
120struct cleanup *
121make_cleanup (function, arg)
7919c3ed
JG
122 void (*function) PARAMS ((PTR));
123 PTR arg;
bd5635a1
RP
124{
125 register struct cleanup *new
126 = (struct cleanup *) xmalloc (sizeof (struct cleanup));
127 register struct cleanup *old_chain = cleanup_chain;
128
129 new->next = cleanup_chain;
130 new->function = function;
131 new->arg = arg;
132 cleanup_chain = new;
133
134 return old_chain;
135}
136
137/* Discard cleanups and do the actions they describe
138 until we get back to the point OLD_CHAIN in the cleanup_chain. */
139
140void
141do_cleanups (old_chain)
142 register struct cleanup *old_chain;
143{
144 register struct cleanup *ptr;
145 while ((ptr = cleanup_chain) != old_chain)
146 {
5e5215eb 147 cleanup_chain = ptr->next; /* Do this first incase recursion */
bd5635a1 148 (*ptr->function) (ptr->arg);
bd5635a1
RP
149 free (ptr);
150 }
151}
152
153/* Discard cleanups, not doing the actions they describe,
154 until we get back to the point OLD_CHAIN in the cleanup_chain. */
155
156void
157discard_cleanups (old_chain)
158 register struct cleanup *old_chain;
159{
160 register struct cleanup *ptr;
161 while ((ptr = cleanup_chain) != old_chain)
162 {
163 cleanup_chain = ptr->next;
be772100 164 free ((PTR)ptr);
bd5635a1
RP
165 }
166}
167
168/* Set the cleanup_chain to 0, and return the old cleanup chain. */
169struct cleanup *
170save_cleanups ()
171{
172 struct cleanup *old_chain = cleanup_chain;
173
174 cleanup_chain = 0;
175 return old_chain;
176}
177
178/* Restore the cleanup chain from a previously saved chain. */
179void
180restore_cleanups (chain)
181 struct cleanup *chain;
182{
183 cleanup_chain = chain;
184}
185
186/* This function is useful for cleanups.
187 Do
188
189 foo = xmalloc (...);
190 old_chain = make_cleanup (free_current_contents, &foo);
191
192 to arrange to free the object thus allocated. */
193
194void
195free_current_contents (location)
196 char **location;
197{
198 free (*location);
199}
088c3a0b
JG
200
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. */
207
208/* ARGSUSED */
209void
210null_cleanup (arg)
211 char **arg;
212{
213}
214
bd5635a1 215\f
2bc2e684
FF
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. */
219
220void
221warning_setup ()
222{
223 target_terminal_ours ();
224 wrap_here(""); /* Force out any buffered output */
199b2450 225 gdb_flush (gdb_stdout);
2bc2e684
FF
226}
227
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. */
233
234/* VARARGS */
235void
236warning (va_alist)
237 va_dcl
238{
239 va_list args;
240 char *string;
241
242 va_start (args);
243 target_terminal_ours ();
244 wrap_here(""); /* Force out any buffered output */
199b2450 245 gdb_flush (gdb_stdout);
2bc2e684 246 if (warning_pre_print)
199b2450 247 fprintf_unfiltered (gdb_stderr, warning_pre_print);
2bc2e684 248 string = va_arg (args, char *);
199b2450
TL
249 vfprintf_unfiltered (gdb_stderr, string, args);
250 fprintf_unfiltered (gdb_stderr, "\n");
2bc2e684
FF
251 va_end (args);
252}
253
a0cf4681 254/* Start the printing of an error message. Way to use this is to call
a6b26c44 255 this, output the error message (use filtered output), and then call
a0cf4681
JK
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. */
259void
260error_begin ()
261{
262 target_terminal_ours ();
263 wrap_here (""); /* Force out any buffered output */
264 gdb_flush (gdb_stdout);
265
1c95d7ab 266 annotate_error_begin ();
a0cf4681
JK
267
268 if (error_pre_print)
269 fprintf_filtered (gdb_stderr, error_pre_print);
270}
271
bd5635a1
RP
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. */
275
276/* VARARGS */
7919c3ed 277NORETURN void
bd5635a1
RP
278error (va_alist)
279 va_dcl
280{
281 va_list args;
282 char *string;
283
a0cf4681 284 error_begin ();
bd5635a1 285 va_start (args);
bd5635a1 286 string = va_arg (args, char *);
199b2450
TL
287 vfprintf_filtered (gdb_stderr, string, args);
288 fprintf_filtered (gdb_stderr, "\n");
bd5635a1 289 va_end (args);
159dd2aa 290 return_to_top_level (RETURN_ERROR);
bd5635a1
RP
291}
292
293/* Print an error message and exit reporting failure.
294 This is for a error that we cannot continue from.
7919c3ed
JG
295 The arguments are printed a la printf.
296
297 This function cannot be declared volatile (NORETURN) in an
298 ANSI environment because exit() is not declared volatile. */
bd5635a1
RP
299
300/* VARARGS */
7919c3ed 301NORETURN void
bd5635a1
RP
302fatal (va_alist)
303 va_dcl
304{
305 va_list args;
306 char *string;
307
308 va_start (args);
309 string = va_arg (args, char *);
199b2450
TL
310 fprintf_unfiltered (gdb_stderr, "\ngdb: ");
311 vfprintf_unfiltered (gdb_stderr, string, args);
312 fprintf_unfiltered (gdb_stderr, "\n");
bd5635a1
RP
313 va_end (args);
314 exit (1);
315}
316
317/* Print an error message and exit, dumping core.
318 The arguments are printed a la printf (). */
7919c3ed 319
bd5635a1 320/* VARARGS */
7919c3ed 321static void
bd5635a1
RP
322fatal_dump_core (va_alist)
323 va_dcl
324{
325 va_list args;
326 char *string;
327
328 va_start (args);
329 string = va_arg (args, char *);
330 /* "internal error" is always correct, since GDB should never dump
331 core, no matter what the input. */
199b2450
TL
332 fprintf_unfiltered (gdb_stderr, "\ngdb internal error: ");
333 vfprintf_unfiltered (gdb_stderr, string, args);
334 fprintf_unfiltered (gdb_stderr, "\n");
bd5635a1
RP
335 va_end (args);
336
337 signal (SIGQUIT, SIG_DFL);
338 kill (getpid (), SIGQUIT);
339 /* We should never get here, but just in case... */
340 exit (1);
341}
7919c3ed 342
4ace50a5
FF
343/* The strerror() function can return NULL for errno values that are
344 out of range. Provide a "safe" version that always returns a
345 printable string. */
346
347char *
348safe_strerror (errnum)
349 int errnum;
350{
351 char *msg;
352 static char buf[32];
353
354 if ((msg = strerror (errnum)) == NULL)
355 {
356 sprintf (buf, "(undocumented errno %d)", errnum);
357 msg = buf;
358 }
359 return (msg);
360}
361
362/* The strsignal() function can return NULL for signal values that are
363 out of range. Provide a "safe" version that always returns a
364 printable string. */
365
366char *
367safe_strsignal (signo)
368 int signo;
369{
370 char *msg;
371 static char buf[32];
372
373 if ((msg = strsignal (signo)) == NULL)
374 {
375 sprintf (buf, "(undocumented signal %d)", signo);
376 msg = buf;
377 }
378 return (msg);
379}
380
381
bd5635a1
RP
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. */
385
386void
387perror_with_name (string)
388 char *string;
389{
bd5635a1
RP
390 char *err;
391 char *combined;
392
4ace50a5 393 err = safe_strerror (errno);
bd5635a1
RP
394 combined = (char *) alloca (strlen (err) + strlen (string) + 3);
395 strcpy (combined, string);
396 strcat (combined, ": ");
397 strcat (combined, err);
398
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
401 unreasonable. */
8eec3310 402 bfd_set_error (bfd_error_no_error);
bd5635a1
RP
403 errno = 0;
404
405 error ("%s.", combined);
406}
407
408/* Print the system error message for ERRCODE, and also mention STRING
409 as the file name for which the error was encountered. */
410
411void
412print_sys_errmsg (string, errcode)
413 char *string;
414 int errcode;
415{
bd5635a1
RP
416 char *err;
417 char *combined;
418
4ace50a5 419 err = safe_strerror (errcode);
bd5635a1
RP
420 combined = (char *) alloca (strlen (err) + strlen (string) + 3);
421 strcpy (combined, string);
422 strcat (combined, ": ");
423 strcat (combined, err);
424
44a09a68
JK
425 /* We want anything which was printed on stdout to come out first, before
426 this message. */
427 gdb_flush (gdb_stdout);
199b2450 428 fprintf_unfiltered (gdb_stderr, "%s.\n", combined);
bd5635a1
RP
429}
430
431/* Control C eventually causes this to be called, at a convenient time. */
432
433void
434quit ()
435{
199b2450 436 serial_t gdb_stdout_serial = serial_fdopen (1);
159dd2aa 437
bd5635a1 438 target_terminal_ours ();
159dd2aa 439
44a09a68
JK
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
443 too): */
444
445 /* 1. The _filtered buffer. */
446 wrap_here ((char *)0);
447
448 /* 2. The stdio buffer. */
449 gdb_flush (gdb_stdout);
450 gdb_flush (gdb_stderr);
159dd2aa 451
44a09a68
JK
452 /* 3. The system-level buffer. */
453 SERIAL_FLUSH_OUTPUT (gdb_stdout_serial);
199b2450 454 SERIAL_UN_FDOPEN (gdb_stdout_serial);
159dd2aa 455
1c95d7ab 456 annotate_error_begin ();
a0cf4681 457
159dd2aa
JK
458 /* Don't use *_filtered; we don't want to prompt the user to continue. */
459 if (error_pre_print)
199b2450 460 fprintf_unfiltered (gdb_stderr, error_pre_print);
159dd2aa
JK
461
462 if (job_control
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)
199b2450 466 fprintf_unfiltered (gdb_stderr, "Quit\n");
159dd2aa 467 else
199b2450 468 fprintf_unfiltered (gdb_stderr,
159dd2aa
JK
469 "Quit (expect signal SIGINT when the program is resumed)\n");
470 return_to_top_level (RETURN_QUIT);
bd5635a1
RP
471}
472
bd5d07d9
FF
473
474#ifdef __GO32__
475
476/* In the absence of signals, poll keyboard for a quit.
477 Called from #define QUIT pollquit() in xm-go32.h. */
478
479void
480pollquit()
481{
482 if (kbhit ())
483 {
484 int k = getkey ();
44a09a68 485 if (k == 1) {
bd5d07d9 486 quit_flag = 1;
44a09a68
JK
487 quit();
488 }
489 else if (k == 2) {
bd5d07d9 490 immediate_quit = 1;
44a09a68
JK
491 quit ();
492 }
493 else
494 {
495 /* We just ignore it */
496 fprintf_unfiltered (gdb_stderr, "CTRL-A to quit, CTRL-B to quit harder\n");
497 }
bd5d07d9
FF
498 }
499}
500
bd5d07d9 501
44a09a68
JK
502#endif
503#ifdef __GO32__
504void notice_quit()
505{
506 if (kbhit ())
507 {
508 int k = getkey ();
509 if (k == 1) {
510 quit_flag = 1;
511 }
512 else if (k == 2)
513 {
514 immediate_quit = 1;
515 }
516 else
517 {
518 fprintf_unfiltered (gdb_stderr, "CTRL-A to quit, CTRL-B to quit harder\n");
519 }
520 }
521}
522#else
523void notice_quit()
524{
525 /* Done by signals */
526}
527#endif
bd5635a1
RP
528/* Control C comes here */
529
530void
088c3a0b
JG
531request_quit (signo)
532 int signo;
bd5635a1
RP
533{
534 quit_flag = 1;
535
44a09a68
JK
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. */
088c3a0b 539 signal (signo, request_quit);
bd5635a1
RP
540
541 if (immediate_quit)
542 quit ();
543}
3624c875
FF
544
545\f
546/* Memory management stuff (malloc friends). */
547
548#if defined (NO_MMALLOC)
549
550PTR
551mmalloc (md, size)
552 PTR md;
553 long size;
554{
555 return (malloc (size));
556}
557
558PTR
559mrealloc (md, ptr, size)
560 PTR md;
561 PTR ptr;
562 long size;
563{
4ace50a5
FF
564 if (ptr == 0) /* Guard against old realloc's */
565 return malloc (size);
566 else
567 return realloc (ptr, size);
3624c875
FF
568}
569
570void
571mfree (md, ptr)
572 PTR md;
573 PTR ptr;
574{
575 free (ptr);
576}
577
578#endif /* NO_MMALLOC */
579
580#if defined (NO_MMALLOC) || defined (NO_MMALLOC_CHECK)
581
582void
583init_malloc (md)
584 PTR md;
585{
586}
587
588#else /* have mmalloc and want corruption checking */
589
590static void
591malloc_botch ()
592{
593 fatal_dump_core ("Memory corruption");
594}
595
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.
599
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.
607
608 Returns zero on failure, non-zero on success. */
609
610void
611init_malloc (md)
612 PTR md;
613{
614 if (!mmcheck (md, malloc_botch))
615 {
616 warning ("internal error: failed to install memory consistency checks");
617 }
618
4ed3a9ea 619 mmtrace ();
3624c875
FF
620}
621
622#endif /* Have mmalloc and want corruption checking */
623
624/* Called when a memory allocation fails, with the number of bytes of
625 memory requested in SIZE. */
626
627NORETURN void
628nomem (size)
629 long size;
630{
631 if (size > 0)
632 {
633 fatal ("virtual memory exhausted: can't allocate %ld bytes.", size);
634 }
635 else
636 {
637 fatal ("virtual memory exhausted.");
638 }
639}
640
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. */
645
646PTR
647xmmalloc (md, size)
648 PTR md;
649 long size;
650{
651 register PTR val;
652
653 if (size == 0)
654 {
655 val = NULL;
656 }
657 else if ((val = mmalloc (md, size)) == NULL)
658 {
659 nomem (size);
660 }
661 return (val);
662}
663
664/* Like mrealloc but get error if no storage available. */
665
666PTR
667xmrealloc (md, ptr, size)
668 PTR md;
669 PTR ptr;
670 long size;
671{
672 register PTR val;
673
674 if (ptr != NULL)
675 {
676 val = mrealloc (md, ptr, size);
677 }
678 else
679 {
680 val = mmalloc (md, size);
681 }
682 if (val == NULL)
683 {
684 nomem (size);
685 }
686 return (val);
687}
688
689/* Like malloc but get error if no storage available, and protect against
690 the caller wanting to allocate zero bytes. */
691
692PTR
693xmalloc (size)
694 long size;
695{
199b2450 696 return (xmmalloc ((PTR) NULL, size));
3624c875
FF
697}
698
699/* Like mrealloc but get error if no storage available. */
700
701PTR
702xrealloc (ptr, size)
703 PTR ptr;
704 long size;
705{
199b2450 706 return (xmrealloc ((PTR) NULL, ptr, size));
3624c875
FF
707}
708
bd5635a1
RP
709\f
710/* My replacement for the read system call.
711 Used like `read' but keeps going if `read' returns too soon. */
712
713int
714myread (desc, addr, len)
715 int desc;
716 char *addr;
717 int len;
718{
719 register int val;
720 int orglen = len;
721
722 while (len > 0)
723 {
724 val = read (desc, addr, len);
725 if (val < 0)
726 return val;
727 if (val == 0)
728 return orglen - len;
729 len -= val;
730 addr += val;
731 }
732 return orglen;
733}
734\f
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. */
738
739char *
740savestring (ptr, size)
088c3a0b 741 const char *ptr;
bd5635a1
RP
742 int size;
743{
744 register char *p = (char *) xmalloc (size + 1);
4ed3a9ea 745 memcpy (p, ptr, size);
bd5635a1
RP
746 p[size] = 0;
747 return p;
748}
749
3624c875
FF
750char *
751msavestring (md, ptr, size)
199b2450 752 PTR md;
3624c875
FF
753 const char *ptr;
754 int size;
755{
756 register char *p = (char *) xmmalloc (md, size + 1);
4ed3a9ea 757 memcpy (p, ptr, size);
3624c875
FF
758 p[size] = 0;
759 return p;
760}
761
8aa13b87
JK
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? */
bd5635a1
RP
765char *
766strsave (ptr)
8aa13b87 767 const char *ptr;
bd5635a1
RP
768{
769 return savestring (ptr, strlen (ptr));
770}
771
3624c875
FF
772char *
773mstrsave (md, ptr)
199b2450 774 PTR md;
3624c875
FF
775 const char *ptr;
776{
777 return (msavestring (md, ptr, strlen (ptr)));
778}
779
bd5635a1
RP
780void
781print_spaces (n, file)
782 register int n;
783 register FILE *file;
784{
785 while (n-- > 0)
786 fputc (' ', file);
787}
788
8eec3310
SC
789/* Print a host address. */
790
791void
792gdb_print_address (addr, stream)
793 PTR addr;
794 GDB_FILE *stream;
795{
796
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. */
800
801 fprintf_filtered (stream, "0x%lx", (unsigned long)addr);
802}
803
bd5635a1
RP
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. */
808
809/* VARARGS */
810int
811query (va_alist)
812 va_dcl
813{
814 va_list args;
815 char *ctlstr;
816 register int answer;
817 register int ans2;
d8742f46 818 int retval;
bd5635a1
RP
819
820 /* Automatically answer "yes" if input is not from a terminal. */
821 if (!input_from_terminal_p ())
822 return 1;
823
824 while (1)
825 {
546014f7 826 wrap_here (""); /* Flush any buffered output */
199b2450 827 gdb_flush (gdb_stdout);
d8742f46
JK
828
829 if (annotation_level > 1)
830 printf_filtered ("\n\032\032pre-query\n");
831
bd5635a1
RP
832 va_start (args);
833 ctlstr = va_arg (args, char *);
199b2450 834 vfprintf_filtered (gdb_stdout, ctlstr, args);
b36e3a9b 835 va_end (args);
bcf2e6ab 836 printf_filtered ("(y or n) ");
d8742f46
JK
837
838 if (annotation_level > 1)
839 printf_filtered ("\n\032\032query\n");
840
199b2450 841 gdb_flush (gdb_stdout);
b36e3a9b
SG
842 answer = fgetc (stdin);
843 clearerr (stdin); /* in case of C-d */
844 if (answer == EOF) /* C-d */
d8742f46
JK
845 {
846 retval = 1;
847 break;
848 }
b36e3a9b
SG
849 if (answer != '\n') /* Eat rest of input line, to EOF or newline */
850 do
851 {
852 ans2 = fgetc (stdin);
853 clearerr (stdin);
854 }
855 while (ans2 != EOF && ans2 != '\n');
bd5635a1
RP
856 if (answer >= 'a')
857 answer -= 040;
858 if (answer == 'Y')
d8742f46
JK
859 {
860 retval = 1;
861 break;
862 }
bd5635a1 863 if (answer == 'N')
d8742f46
JK
864 {
865 retval = 0;
866 break;
867 }
bcf2e6ab 868 printf_filtered ("Please answer y or n.\n");
bd5635a1 869 }
d8742f46
JK
870
871 if (annotation_level > 1)
872 printf_filtered ("\n\032\032post-query\n");
873 return retval;
bd5635a1 874}
7919c3ed 875
bd5635a1
RP
876\f
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.
882
883 A negative value means the sequence \ newline was seen,
884 which is supposed to be equivalent to nothing at all.
885
886 If \ is followed by a null character, we return a negative
887 value and leave the string pointer pointing at the null character.
888
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. */
891
892int
893parse_escape (string_ptr)
894 char **string_ptr;
895{
896 register int c = *(*string_ptr)++;
897 switch (c)
898 {
899 case 'a':
2bc2e684 900 return 007; /* Bell (alert) char */
bd5635a1
RP
901 case 'b':
902 return '\b';
2bc2e684 903 case 'e': /* Escape character */
bd5635a1
RP
904 return 033;
905 case 'f':
906 return '\f';
907 case 'n':
908 return '\n';
909 case 'r':
910 return '\r';
911 case 't':
912 return '\t';
913 case 'v':
914 return '\v';
915 case '\n':
916 return -2;
917 case 0:
918 (*string_ptr)--;
919 return 0;
920 case '^':
921 c = *(*string_ptr)++;
922 if (c == '\\')
923 c = parse_escape (string_ptr);
924 if (c == '?')
925 return 0177;
926 return (c & 0200) | (c & 037);
927
928 case '0':
929 case '1':
930 case '2':
931 case '3':
932 case '4':
933 case '5':
934 case '6':
935 case '7':
936 {
937 register int i = c - '0';
938 register int count = 0;
939 while (++count < 3)
940 {
941 if ((c = *(*string_ptr)++) >= '0' && c <= '7')
942 {
943 i *= 8;
944 i += c - '0';
945 }
946 else
947 {
948 (*string_ptr)--;
949 break;
950 }
951 }
952 return i;
953 }
954 default:
955 return c;
956 }
957}
958\f
51b80b00
FF
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. */
bd5635a1
RP
963
964void
51b80b00 965gdb_printchar (c, stream, quoter)
088c3a0b 966 register int c;
bd5635a1
RP
967 FILE *stream;
968 int quoter;
969{
bd5635a1 970
7e7e2d40
JG
971 c &= 0xFF; /* Avoid sign bit follies */
972
fcdb113e
JG
973 if ( c < 0x20 || /* Low control chars */
974 (c >= 0x7F && c < 0xA0) || /* DEL, High controls */
975 (sevenbit_strings && c >= 0x80)) { /* high order bit set */
bd5635a1
RP
976 switch (c)
977 {
978 case '\n':
979 fputs_filtered ("\\n", stream);
980 break;
981 case '\b':
982 fputs_filtered ("\\b", stream);
983 break;
984 case '\t':
985 fputs_filtered ("\\t", stream);
986 break;
987 case '\f':
988 fputs_filtered ("\\f", stream);
989 break;
990 case '\r':
991 fputs_filtered ("\\r", stream);
992 break;
993 case '\033':
994 fputs_filtered ("\\e", stream);
995 break;
996 case '\007':
997 fputs_filtered ("\\a", stream);
998 break;
999 default:
1000 fprintf_filtered (stream, "\\%.3o", (unsigned int) c);
1001 break;
1002 }
2bc2e684
FF
1003 } else {
1004 if (c == '\\' || c == quoter)
1005 fputs_filtered ("\\", stream);
1006 fprintf_filtered (stream, "%c", c);
1007 }
bd5635a1
RP
1008}
1009\f
1010/* Number of lines per page or UINT_MAX if paging is disabled. */
1011static unsigned int lines_per_page;
1012/* Number of chars per line or UNIT_MAX is line folding is disabled. */
1013static unsigned int chars_per_line;
1014/* Current count of lines printed on this page, chars on this line. */
1015static unsigned int lines_printed, chars_printed;
1016
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
159dd2aa
JK
1023 the buffered output. */
1024
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. */
1028static char *wrap_buffer;
bd5635a1 1029
159dd2aa
JK
1030/* Pointer in wrap_buffer to the next character to fill. */
1031static char *wrap_pointer;
bd5635a1 1032
159dd2aa
JK
1033/* String to indent by if the wrap occurs. Must not be NULL if wrap_column
1034 is non-zero. */
1035static char *wrap_indent;
1036
1037/* Column number on the screen where wrap_buffer begins, or 0 if wrapping
1038 is not in effect. */
bd5635a1
RP
1039static int wrap_column;
1040
e1ce8aa5 1041/* ARGSUSED */
bd5635a1
RP
1042static void
1043set_width_command (args, from_tty, c)
1044 char *args;
1045 int from_tty;
1046 struct cmd_list_element *c;
1047{
1048 if (!wrap_buffer)
1049 {
1050 wrap_buffer = (char *) xmalloc (chars_per_line + 2);
1051 wrap_buffer[0] = '\0';
1052 }
1053 else
1054 wrap_buffer = (char *) xrealloc (wrap_buffer, chars_per_line + 2);
1055 wrap_pointer = wrap_buffer; /* Start it at the beginning */
1056}
1057
d974236f
JG
1058/* Wait, so the user can read what's on the screen. Prompt the user
1059 to continue by pressing RETURN. */
1060
bd5635a1
RP
1061static void
1062prompt_for_continue ()
1063{
351b221d 1064 char *ignore;
d8742f46
JK
1065 char cont_prompt[120];
1066
4dd876ac
JK
1067 if (annotation_level > 1)
1068 printf_unfiltered ("\n\032\032pre-prompt-for-continue\n");
1069
d8742f46
JK
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");
351b221d 1074
d974236f
JG
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
1077 screen. */
1078 reinitialize_more_filter ();
1079
bd5635a1 1080 immediate_quit++;
159dd2aa
JK
1081 /* On a real operating system, the user can quit with SIGINT.
1082 But not on GO32.
1083
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
1087 SIGINT. */
a94100d1
JK
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
1090 out to DOS. */
d8742f46 1091 ignore = readline (cont_prompt);
4dd876ac
JK
1092
1093 if (annotation_level > 1)
1094 printf_unfiltered ("\n\032\032post-prompt-for-continue\n");
1095
351b221d 1096 if (ignore)
159dd2aa
JK
1097 {
1098 char *p = ignore;
1099 while (*p == ' ' || *p == '\t')
1100 ++p;
1101 if (p[0] == 'q')
1102 request_quit (SIGINT);
1103 free (ignore);
1104 }
bd5635a1 1105 immediate_quit--;
d974236f
JG
1106
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 ();
1110
351b221d 1111 dont_repeat (); /* Forget prev cmd -- CR won't repeat it. */
bd5635a1
RP
1112}
1113
1114/* Reinitialize filter; ie. tell it to reset to original values. */
1115
1116void
1117reinitialize_more_filter ()
1118{
1119 lines_printed = 0;
1120 chars_printed = 0;
1121}
1122
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.
159dd2aa 1125 If INDENT is non-null, it is a string to be printed to indent the
bd5635a1
RP
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
1128 fputs_filtered().
1129
1130 If the line is already overfull, we immediately print a newline and
1131 the indentation, and disable further wrapping.
1132
2bc2e684
FF
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.
1136
159dd2aa
JK
1137 INDENT should not contain tabs, as that will mess up the char count
1138 on the next line. FIXME.
1139
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. */
bd5635a1
RP
1143
1144void
1145wrap_here(indent)
159dd2aa 1146 char *indent;
bd5635a1
RP
1147{
1148 if (wrap_buffer[0])
1149 {
1150 *wrap_pointer = '\0';
199b2450 1151 fputs (wrap_buffer, gdb_stdout);
bd5635a1
RP
1152 }
1153 wrap_pointer = wrap_buffer;
1154 wrap_buffer[0] = '\0';
2bc2e684
FF
1155 if (chars_per_line == UINT_MAX) /* No line overflow checking */
1156 {
1157 wrap_column = 0;
1158 }
1159 else if (chars_printed >= chars_per_line)
bd5635a1
RP
1160 {
1161 puts_filtered ("\n");
159dd2aa
JK
1162 if (indent != NULL)
1163 puts_filtered (indent);
bd5635a1
RP
1164 wrap_column = 0;
1165 }
1166 else
1167 {
1168 wrap_column = chars_printed;
159dd2aa
JK
1169 if (indent == NULL)
1170 wrap_indent = "";
1171 else
1172 wrap_indent = indent;
bd5635a1
RP
1173 }
1174}
1175
51b80b00
FF
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. */
1180
1181void
1182begin_line ()
1183{
1184 if (chars_printed > 0)
1185 {
1186 puts_filtered ("\n");
1187 }
1188}
1189
199b2450
TL
1190
1191GDB_FILE *
1192gdb_fopen (name, mode)
1193 char * name;
1194 char * mode;
1195{
1196 return fopen (name, mode);
1197}
1198
bd5635a1 1199void
199b2450
TL
1200gdb_flush (stream)
1201 FILE *stream;
1202{
1203 fflush (stream);
1204}
1205
44a09a68
JK
1206/* Like fputs but if FILTER is true, pause after every screenful.
1207
1208 Regardless of FILTER can wrap at points other than the final
1209 character of a line.
1210
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
1213 anything.
1214
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. */
1218
199b2450
TL
1219static void
1220fputs_maybe_filtered (linebuffer, stream, filter)
088c3a0b 1221 const char *linebuffer;
bd5635a1 1222 FILE *stream;
199b2450 1223 int filter;
bd5635a1 1224{
7919c3ed 1225 const char *lineptr;
bd5635a1
RP
1226
1227 if (linebuffer == 0)
1228 return;
1229
1230 /* Don't do any filtering if it is disabled. */
199b2450 1231 if (stream != gdb_stdout
bd5635a1
RP
1232 || (lines_per_page == UINT_MAX && chars_per_line == UINT_MAX))
1233 {
1234 fputs (linebuffer, stream);
1235 return;
1236 }
1237
1238 /* Go through and output each character. Show line extension
1239 when this is necessary; prompt user for new page when this is
1240 necessary. */
1241
1242 lineptr = linebuffer;
1243 while (*lineptr)
1244 {
1245 /* Possible new page. */
199b2450
TL
1246 if (filter &&
1247 (lines_printed >= lines_per_page - 1))
bd5635a1
RP
1248 prompt_for_continue ();
1249
1250 while (*lineptr && *lineptr != '\n')
1251 {
1252 /* Print a single line. */
1253 if (*lineptr == '\t')
1254 {
1255 if (wrap_column)
1256 *wrap_pointer++ = '\t';
1257 else
1258 putc ('\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;
1263 lineptr++;
1264 }
1265 else
1266 {
1267 if (wrap_column)
1268 *wrap_pointer++ = *lineptr;
1269 else
1270 putc (*lineptr, stream);
1271 chars_printed++;
1272 lineptr++;
1273 }
1274
1275 if (chars_printed >= chars_per_line)
1276 {
1277 unsigned int save_chars = chars_printed;
1278
1279 chars_printed = 0;
1280 lines_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. */
1284 if (wrap_column)
1285 putc ('\n', stream);
1286
1287 /* Possible new page. */
1288 if (lines_printed >= lines_per_page - 1)
1289 prompt_for_continue ();
1290
1291 /* Now output indentation and wrapped string */
1292 if (wrap_column)
1293 {
159dd2aa 1294 fputs (wrap_indent, stream);
bd5635a1
RP
1295 *wrap_pointer = '\0'; /* Null-terminate saved stuff */
1296 fputs (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 */
1308 }
1309 }
1310 }
1311
1312 if (*lineptr == '\n')
1313 {
1314 chars_printed = 0;
d11c44f1 1315 wrap_here ((char *)0); /* Spit out chars, cancel further wraps */
bd5635a1
RP
1316 lines_printed++;
1317 putc ('\n', stream);
1318 lineptr++;
1319 }
1320 }
1321}
1322
199b2450
TL
1323void
1324fputs_filtered (linebuffer, stream)
1325 const char *linebuffer;
1326 FILE *stream;
1327{
1328 fputs_maybe_filtered (linebuffer, stream, 1);
1329}
1330
1331void
1332fputs_unfiltered (linebuffer, stream)
1333 const char *linebuffer;
1334 FILE *stream;
1335{
f29351d9
JK
1336#if 0
1337
1338 /* This gets the wrap_buffer buffering wrong when called from
1339 gdb_readline (GDB was sometimes failing to print the prompt
1340 before reading input). Even at other times, it seems kind of
1341 misguided, especially now that printf_unfiltered doesn't use
1342 printf_maybe_filtered. */
1343
199b2450 1344 fputs_maybe_filtered (linebuffer, stream, 0);
f29351d9
JK
1345#else
1346 fputs (linebuffer, stream);
1347#endif
199b2450
TL
1348}
1349
1350void
1351putc_unfiltered (c)
1352 int c;
1353{
1354 char buf[2];
1355 buf[0] = c;
1356 buf[1] = 0;
1357 fputs_unfiltered (buf, gdb_stdout);
1358}
1359
1360void
1361fputc_unfiltered (c, stream)
1362 int c;
1363 FILE * stream;
1364{
1365 char buf[2];
1366 buf[0] = c;
1367 buf[1] = 0;
1368 fputs_unfiltered (buf, stream);
1369}
1370
1371
bd5635a1
RP
1372/* Print a variable number of ARGS using format FORMAT. If this
1373 information is going to put the amount written (since the last call
d974236f 1374 to REINITIALIZE_MORE_FILTER or the last page break) over the page size,
bd5635a1
RP
1375 print out a pause message and do a gdb_readline to get the users
1376 permision to continue.
1377
1378 Unlike fprintf, this function does not return a value.
1379
1380 We implement three variants, vfprintf (takes a vararg list and stream),
1381 fprintf (takes a stream to write on), and printf (the usual).
1382
1383 Note that this routine has a restriction that the length of the
1384 final output line must be less than 255 characters *or* it must be
1385 less than twice the size of the format string. This is a very
1386 arbitrary restriction, but it is an internal restriction, so I'll
1387 put it in. This means that the %s format specifier is almost
1388 useless; unless the caller can GUARANTEE that the string is short
1389 enough, fputs_filtered should be used instead.
1390
1391 Note also that a longjmp to top level may occur in this routine
1392 (since prompt_for_continue may do so) so this routine should not be
1393 called when cleanups are not in place. */
1394
d974236f
JG
1395#define MIN_LINEBUF 255
1396
199b2450
TL
1397static void
1398vfprintf_maybe_filtered (stream, format, args, filter)
bd5635a1
RP
1399 FILE *stream;
1400 char *format;
7919c3ed 1401 va_list args;
199b2450 1402 int filter;
bd5635a1 1403{
d974236f
JG
1404 char line_buf[MIN_LINEBUF+10];
1405 char *linebuffer = line_buf;
bd5635a1
RP
1406 int format_length;
1407
1408 format_length = strlen (format);
1409
bd5635a1 1410 /* Reallocate buffer to a larger size if this is necessary. */
d974236f 1411 if (format_length * 2 > MIN_LINEBUF)
bd5635a1 1412 {
d974236f 1413 linebuffer = alloca (10 + format_length * 2);
bd5635a1
RP
1414 }
1415
bd5635a1
RP
1416 /* This won't blow up if the restrictions described above are
1417 followed. */
4ed3a9ea 1418 vsprintf (linebuffer, format, args);
bd5635a1 1419
199b2450
TL
1420 fputs_maybe_filtered (linebuffer, stream, filter);
1421}
1422
1423
1424void
1425vfprintf_filtered (stream, format, args)
1426 FILE *stream;
1427 char *format;
1428 va_list args;
1429{
1430 vfprintf_maybe_filtered (stream, format, args, 1);
1431}
1432
1433void
1434vfprintf_unfiltered (stream, format, args)
1435 FILE *stream;
1436 char *format;
1437 va_list args;
1438{
69fb299e 1439 vfprintf (stream, format, args);
bd5635a1
RP
1440}
1441
51b80b00
FF
1442void
1443vprintf_filtered (format, args)
1444 char *format;
1445 va_list args;
1446{
199b2450
TL
1447 vfprintf_maybe_filtered (gdb_stdout, format, args, 1);
1448}
1449
1450void
1451vprintf_unfiltered (format, args)
1452 char *format;
1453 va_list args;
1454{
69fb299e 1455 vfprintf (gdb_stdout, format, args);
51b80b00
FF
1456}
1457
bd5635a1
RP
1458/* VARARGS */
1459void
1460fprintf_filtered (va_alist)
1461 va_dcl
1462{
546014f7 1463 va_list args;
bd5635a1
RP
1464 FILE *stream;
1465 char *format;
546014f7
PB
1466
1467 va_start (args);
1468 stream = va_arg (args, FILE *);
1469 format = va_arg (args, char *);
1470
1471 /* This won't blow up if the restrictions described above are
1472 followed. */
1473 vfprintf_filtered (stream, format, args);
1474 va_end (args);
1475}
1476
199b2450
TL
1477/* VARARGS */
1478void
1479fprintf_unfiltered (va_alist)
1480 va_dcl
1481{
1482 va_list args;
1483 FILE *stream;
1484 char *format;
1485
1486 va_start (args);
1487 stream = va_arg (args, FILE *);
1488 format = va_arg (args, char *);
1489
1490 /* This won't blow up if the restrictions described above are
1491 followed. */
1492 vfprintf_unfiltered (stream, format, args);
1493 va_end (args);
1494}
1495
546014f7 1496/* Like fprintf_filtered, but prints it's result indent.
199b2450 1497 Called as fprintfi_filtered (spaces, stream, format, ...); */
546014f7
PB
1498
1499/* VARARGS */
1500void
1501fprintfi_filtered (va_alist)
1502 va_dcl
1503{
7919c3ed 1504 va_list args;
546014f7
PB
1505 int spaces;
1506 FILE *stream;
1507 char *format;
bd5635a1
RP
1508
1509 va_start (args);
546014f7 1510 spaces = va_arg (args, int);
bd5635a1
RP
1511 stream = va_arg (args, FILE *);
1512 format = va_arg (args, char *);
546014f7 1513 print_spaces_filtered (spaces, stream);
bd5635a1
RP
1514
1515 /* This won't blow up if the restrictions described above are
1516 followed. */
7919c3ed 1517 vfprintf_filtered (stream, format, args);
bd5635a1
RP
1518 va_end (args);
1519}
1520
199b2450 1521
bd5635a1
RP
1522/* VARARGS */
1523void
1524printf_filtered (va_alist)
1525 va_dcl
1526{
1527 va_list args;
1528 char *format;
1529
1530 va_start (args);
1531 format = va_arg (args, char *);
1532
199b2450
TL
1533 vfprintf_filtered (gdb_stdout, format, args);
1534 va_end (args);
1535}
1536
1537
1538/* VARARGS */
1539void
1540printf_unfiltered (va_alist)
1541 va_dcl
1542{
1543 va_list args;
1544 char *format;
1545
1546 va_start (args);
1547 format = va_arg (args, char *);
1548
1549 vfprintf_unfiltered (gdb_stdout, format, args);
bd5635a1
RP
1550 va_end (args);
1551}
bd5635a1 1552
546014f7 1553/* Like printf_filtered, but prints it's result indented.
199b2450 1554 Called as printfi_filtered (spaces, format, ...); */
546014f7
PB
1555
1556/* VARARGS */
1557void
1558printfi_filtered (va_alist)
1559 va_dcl
1560{
1561 va_list args;
1562 int spaces;
1563 char *format;
1564
1565 va_start (args);
1566 spaces = va_arg (args, int);
1567 format = va_arg (args, char *);
199b2450
TL
1568 print_spaces_filtered (spaces, gdb_stdout);
1569 vfprintf_filtered (gdb_stdout, format, args);
546014f7
PB
1570 va_end (args);
1571}
1572
51b80b00
FF
1573/* Easy -- but watch out!
1574
1575 This routine is *not* a replacement for puts()! puts() appends a newline.
1576 This one doesn't, and had better not! */
bd5635a1
RP
1577
1578void
1579puts_filtered (string)
1580 char *string;
1581{
199b2450
TL
1582 fputs_filtered (string, gdb_stdout);
1583}
1584
1585void
1586puts_unfiltered (string)
1587 char *string;
1588{
1589 fputs_unfiltered (string, gdb_stdout);
bd5635a1
RP
1590}
1591
1592/* Return a pointer to N spaces and a null. The pointer is good
1593 until the next call to here. */
1594char *
1595n_spaces (n)
1596 int n;
1597{
1598 register char *t;
1599 static char *spaces;
1600 static int max_spaces;
1601
1602 if (n > max_spaces)
1603 {
1604 if (spaces)
1605 free (spaces);
3624c875 1606 spaces = (char *) xmalloc (n+1);
bd5635a1
RP
1607 for (t = spaces+n; t != spaces;)
1608 *--t = ' ';
1609 spaces[n] = '\0';
1610 max_spaces = n;
1611 }
1612
1613 return spaces + max_spaces - n;
1614}
1615
1616/* Print N spaces. */
1617void
1618print_spaces_filtered (n, stream)
1619 int n;
1620 FILE *stream;
1621{
1622 fputs_filtered (n_spaces (n), stream);
1623}
1624\f
1625/* C++ demangler stuff. */
bd5635a1 1626
65ce5df4
JG
1627/* fprintf_symbol_filtered attempts to demangle NAME, a symbol in language
1628 LANG, using demangling args ARG_MODE, and print it filtered to STREAM.
1629 If the name is not mangled, or the language for the name is unknown, or
1630 demangling is off, the name is printed in its "raw" form. */
1631
bd5635a1 1632void
65ce5df4 1633fprintf_symbol_filtered (stream, name, lang, arg_mode)
bd5635a1
RP
1634 FILE *stream;
1635 char *name;
65ce5df4
JG
1636 enum language lang;
1637 int arg_mode;
bd5635a1 1638{
65ce5df4 1639 char *demangled;
bd5d07d9 1640
65ce5df4 1641 if (name != NULL)
bd5d07d9 1642 {
65ce5df4
JG
1643 /* If user wants to see raw output, no problem. */
1644 if (!demangle)
bd5d07d9 1645 {
65ce5df4
JG
1646 fputs_filtered (name, stream);
1647 }
1648 else
1649 {
1650 switch (lang)
1651 {
1652 case language_cplus:
1653 demangled = cplus_demangle (name, arg_mode);
1654 break;
65ce5df4
JG
1655 case language_chill:
1656 demangled = chill_demangle (name);
1657 break;
65ce5df4
JG
1658 default:
1659 demangled = NULL;
1660 break;
1661 }
1662 fputs_filtered (demangled ? demangled : name, stream);
1663 if (demangled != NULL)
1664 {
1665 free (demangled);
1666 }
bd5d07d9 1667 }
bd5635a1
RP
1668 }
1669}
51b57ded
FF
1670
1671/* Do a strcmp() type operation on STRING1 and STRING2, ignoring any
1672 differences in whitespace. Returns 0 if they match, non-zero if they
546014f7
PB
1673 don't (slightly different than strcmp()'s range of return values).
1674
1675 As an extra hack, string1=="FOO(ARGS)" matches string2=="FOO".
2e4964ad
FF
1676 This "feature" is useful when searching for matching C++ function names
1677 (such as if the user types 'break FOO', where FOO is a mangled C++
1678 function). */
51b57ded 1679
51b80b00 1680int
51b57ded
FF
1681strcmp_iw (string1, string2)
1682 const char *string1;
1683 const char *string2;
1684{
1685 while ((*string1 != '\0') && (*string2 != '\0'))
1686 {
1687 while (isspace (*string1))
1688 {
1689 string1++;
1690 }
1691 while (isspace (*string2))
1692 {
1693 string2++;
1694 }
1695 if (*string1 != *string2)
1696 {
1697 break;
1698 }
1699 if (*string1 != '\0')
1700 {
1701 string1++;
1702 string2++;
1703 }
1704 }
546014f7 1705 return (*string1 != '\0' && *string1 != '(') || (*string2 != '\0');
51b57ded
FF
1706}
1707
bd5635a1 1708\f
bd5635a1
RP
1709void
1710_initialize_utils ()
1711{
1712 struct cmd_list_element *c;
1713
1714 c = add_set_cmd ("width", class_support, var_uinteger,
1715 (char *)&chars_per_line,
1716 "Set number of characters gdb thinks are in a line.",
1717 &setlist);
1718 add_show_from_set (c, &showlist);
d747e0af 1719 c->function.sfunc = set_width_command;
bd5635a1
RP
1720
1721 add_show_from_set
1722 (add_set_cmd ("height", class_support,
1723 var_uinteger, (char *)&lines_per_page,
1724 "Set number of lines gdb thinks are in a page.", &setlist),
1725 &showlist);
1726
1727 /* These defaults will be used if we are unable to get the correct
1728 values from termcap. */
51b57ded
FF
1729#if defined(__GO32__)
1730 lines_per_page = ScreenRows();
1731 chars_per_line = ScreenCols();
1732#else
bd5635a1
RP
1733 lines_per_page = 24;
1734 chars_per_line = 80;
a6b26c44
SS
1735/* start-sanitize-mpw */
1736#ifndef MPW
1737 /* No termcap under MPW, although might be cool to do something
1738 by looking at worksheet or console window sizes. */
1739/* end-sanitize-mpw */
bd5635a1
RP
1740 /* Initialize the screen height and width from termcap. */
1741 {
1742 char *termtype = getenv ("TERM");
1743
1744 /* Positive means success, nonpositive means failure. */
1745 int status;
1746
1747 /* 2048 is large enough for all known terminals, according to the
1748 GNU termcap manual. */
1749 char term_buffer[2048];
1750
1751 if (termtype)
1752 {
1753 status = tgetent (term_buffer, termtype);
1754 if (status > 0)
1755 {
1756 int val;
1757
1758 val = tgetnum ("li");
1759 if (val >= 0)
1760 lines_per_page = val;
1761 else
1762 /* The number of lines per page is not mentioned
1763 in the terminal description. This probably means
1764 that paging is not useful (e.g. emacs shell window),
1765 so disable paging. */
1766 lines_per_page = UINT_MAX;
1767
1768 val = tgetnum ("co");
1769 if (val >= 0)
1770 chars_per_line = val;
1771 }
1772 }
1773 }
a6b26c44
SS
1774/* start-sanitize-mpw */
1775#endif /* MPW */
1776/* end-sanitize-mpw */
bd5635a1 1777
1eeba686
PB
1778#if defined(SIGWINCH) && defined(SIGWINCH_HANDLER)
1779
4ace50a5 1780 /* If there is a better way to determine the window size, use it. */
1eeba686
PB
1781 SIGWINCH_HANDLER ();
1782#endif
51b57ded 1783#endif
2bc2e684 1784 /* If the output is not a terminal, don't paginate it. */
199b2450 1785 if (!ISATTY (gdb_stdout))
2bc2e684
FF
1786 lines_per_page = UINT_MAX;
1787
bd5635a1
RP
1788 set_width_command ((char *)NULL, 0, c);
1789
1790 add_show_from_set
1791 (add_set_cmd ("demangle", class_support, var_boolean,
1792 (char *)&demangle,
1793 "Set demangling of encoded C++ names when displaying symbols.",
f266e564
JK
1794 &setprintlist),
1795 &showprintlist);
bd5635a1
RP
1796
1797 add_show_from_set
1798 (add_set_cmd ("sevenbit-strings", class_support, var_boolean,
1799 (char *)&sevenbit_strings,
1800 "Set printing of 8-bit characters in strings as \\nnn.",
f266e564
JK
1801 &setprintlist),
1802 &showprintlist);
bd5635a1
RP
1803
1804 add_show_from_set
1805 (add_set_cmd ("asm-demangle", class_support, var_boolean,
1806 (char *)&asm_demangle,
1807 "Set demangling of C++ names in disassembly listings.",
f266e564
JK
1808 &setprintlist),
1809 &showprintlist);
bd5635a1 1810}
1eeba686
PB
1811
1812/* Machine specific function to handle SIGWINCH signal. */
1813
1814#ifdef SIGWINCH_HANDLER_BODY
1815 SIGWINCH_HANDLER_BODY
1816#endif
bd5d07d9 1817
This page took 0.27247 seconds and 4 git commands to generate.