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