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