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