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