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