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