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