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