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