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