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