home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb-4.5 / dist / gdb / utils.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-03  |  32.0 KB  |  1,368 lines

  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.  
  22. #include <sys/ioctl.h>
  23. #include <sys/param.h>
  24. #include <pwd.h>
  25. #include <varargs.h>
  26. #include <ctype.h>
  27. #include <string.h>
  28.  
  29. #include "signals.h"
  30. #include "gdbcmd.h"
  31. #include "terminal.h"
  32. #include "bfd.h"
  33. #include "target.h"
  34.  
  35. /* Prototypes for local functions */
  36.  
  37. #if !defined (NO_MALLOC_CHECK)
  38.  
  39. static void
  40. malloc_botch PARAMS ((void));
  41.  
  42. #endif /* NO_MALLOC_CHECK  */
  43.  
  44. static void
  45. fatal_dump_core ();    /* Can't prototype with <varargs.h> usage... */
  46.  
  47. static void
  48. prompt_for_continue PARAMS ((void));
  49.  
  50. static void 
  51. set_width_command PARAMS ((char *, int, struct cmd_list_element *));
  52.  
  53. static void
  54. vfprintf_filtered PARAMS ((FILE *, char *, va_list));
  55.  
  56. /* If this definition isn't overridden by the header files, assume
  57.    that isatty and fileno exist on this system.  */
  58. #ifndef ISATTY
  59. #define ISATTY(FP)    (isatty (fileno (FP)))
  60. #endif
  61.  
  62. /* Chain of cleanup actions established with make_cleanup,
  63.    to be executed if an error happens.  */
  64.  
  65. static struct cleanup *cleanup_chain;
  66.  
  67. /* Nonzero means a quit has been requested.  */
  68.  
  69. int quit_flag;
  70.  
  71. /* Nonzero means quit immediately if Control-C is typed now,
  72.    rather than waiting until QUIT is executed.  */
  73.  
  74. int immediate_quit;
  75.  
  76. /* Nonzero means that encoded C++ names should be printed out in their
  77.    C++ form rather than raw.  */
  78.  
  79. int demangle = 1;
  80.  
  81. /* Nonzero means that encoded C++ names should be printed out in their
  82.    C++ form even in assembler language displays.  If this is set, but
  83.    DEMANGLE is zero, names are printed raw, i.e. DEMANGLE controls.  */
  84.  
  85. int asm_demangle = 0;
  86.  
  87. /* Nonzero means that strings with character values >0x7F should be printed
  88.    as octal escapes.  Zero means just print the value (e.g. it's an
  89.    international character, and the terminal or window can cope.)  */
  90.  
  91. int sevenbit_strings = 0;
  92.  
  93. /* String to be printed before error messages, if any.  */
  94.  
  95. char *error_pre_print;
  96. char *warning_pre_print = "\nwarning: ";
  97.  
  98. /* Add a new cleanup to the cleanup_chain,
  99.    and return the previous chain pointer
  100.    to be passed later to do_cleanups or discard_cleanups.
  101.    Args are FUNCTION to clean up with, and ARG to pass to it.  */
  102.  
  103. struct cleanup *
  104. make_cleanup (function, arg)
  105.      void (*function) PARAMS ((PTR));
  106.      PTR arg;
  107. {
  108.   register struct cleanup *new
  109.     = (struct cleanup *) xmalloc (sizeof (struct cleanup));
  110.   register struct cleanup *old_chain = cleanup_chain;
  111.  
  112.   new->next = cleanup_chain;
  113.   new->function = function;
  114.   new->arg = arg;
  115.   cleanup_chain = new;
  116.  
  117.   return old_chain;
  118. }
  119.  
  120. /* Discard cleanups and do the actions they describe
  121.    until we get back to the point OLD_CHAIN in the cleanup_chain.  */
  122.  
  123. void
  124. do_cleanups (old_chain)
  125.      register struct cleanup *old_chain;
  126. {
  127.   register struct cleanup *ptr;
  128.   while ((ptr = cleanup_chain) != old_chain)
  129.     {
  130.       cleanup_chain = ptr->next;    /* Do this first incase recursion */
  131.       (*ptr->function) (ptr->arg);
  132.       free (ptr);
  133.     }
  134. }
  135.  
  136. /* Discard cleanups, not doing the actions they describe,
  137.    until we get back to the point OLD_CHAIN in the cleanup_chain.  */
  138.  
  139. void
  140. discard_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;
  147.       free ((PTR)ptr);
  148.     }
  149. }
  150.  
  151. /* Set the cleanup_chain to 0, and return the old cleanup chain.  */
  152. struct cleanup *
  153. save_cleanups ()
  154. {
  155.   struct cleanup *old_chain = cleanup_chain;
  156.  
  157.   cleanup_chain = 0;
  158.   return old_chain;
  159. }
  160.  
  161. /* Restore the cleanup chain from a previously saved chain.  */
  162. void
  163. restore_cleanups (chain)
  164.      struct cleanup *chain;
  165. {
  166.   cleanup_chain = chain;
  167. }
  168.  
  169. /* This function is useful for cleanups.
  170.    Do
  171.  
  172.      foo = xmalloc (...);
  173.      old_chain = make_cleanup (free_current_contents, &foo);
  174.  
  175.    to arrange to free the object thus allocated.  */
  176.  
  177. void
  178. free_current_contents (location)
  179.      char **location;
  180. {
  181.   free (*location);
  182. }
  183.  
  184. /* Provide a known function that does nothing, to use as a base for
  185.    for a possibly long chain of cleanups.  This is useful where we
  186.    use the cleanup chain for handling normal cleanups as well as dealing
  187.    with cleanups that need to be done as a result of a call to error().
  188.    In such cases, we may not be certain where the first cleanup is, unless
  189.    we have a do-nothing one to always use as the base. */
  190.  
  191. /* ARGSUSED */
  192. void
  193. null_cleanup (arg)
  194.     char **arg;
  195. {
  196. }
  197.  
  198.  
  199. /* Provide a hook for modules wishing to print their own warning messages
  200.    to set up the terminal state in a compatible way, without them having
  201.    to import all the target_<...> macros. */
  202.  
  203. void
  204. warning_setup ()
  205. {
  206.   target_terminal_ours ();
  207.   wrap_here("");            /* Force out any buffered output */
  208.   fflush (stdout);
  209. }
  210.  
  211. /* Print a warning message.
  212.    The first argument STRING is the warning message, used as a fprintf string,
  213.    and the remaining args are passed as arguments to it.
  214.    The primary difference between warnings and errors is that a warning
  215.    does not force the return to command level. */
  216.  
  217. /* VARARGS */
  218. void
  219. warning (va_alist)
  220.      va_dcl
  221. {
  222.   va_list args;
  223.   char *string;
  224.  
  225.   va_start (args);
  226.   target_terminal_ours ();
  227.   wrap_here("");            /* Force out any buffered output */
  228.   fflush (stdout);
  229.   if (warning_pre_print)
  230.     fprintf (stderr, warning_pre_print);
  231.   string = va_arg (args, char *);
  232.   vfprintf (stderr, string, args);
  233.   fprintf (stderr, "\n");
  234.   va_end (args);
  235. }
  236.  
  237. /* Print an error message and return to command level.
  238.    The first argument STRING is the error message, used as a fprintf string,
  239.    and the remaining args are passed as arguments to it.  */
  240.  
  241. /* VARARGS */
  242. NORETURN void
  243. error (va_alist)
  244.      va_dcl
  245. {
  246.   va_list args;
  247.   char *string;
  248.  
  249.   va_start (args);
  250.   target_terminal_ours ();
  251.   wrap_here("");            /* Force out any buffered output */
  252.   fflush (stdout);
  253.   if (error_pre_print)
  254.     fprintf (stderr, error_pre_print);
  255.   string = va_arg (args, char *);
  256.   vfprintf (stderr, string, args);
  257.   fprintf (stderr, "\n");
  258.   va_end (args);
  259.   return_to_top_level ();
  260. }
  261.  
  262. /* Print an error message and exit reporting failure.
  263.    This is for a error that we cannot continue from.
  264.    The arguments are printed a la printf.
  265.  
  266.    This function cannot be declared volatile (NORETURN) in an
  267.    ANSI environment because exit() is not declared volatile. */
  268.  
  269. /* VARARGS */
  270. NORETURN void
  271. fatal (va_alist)
  272.      va_dcl
  273. {
  274.   va_list args;
  275.   char *string;
  276.  
  277.   va_start (args);
  278.   string = va_arg (args, char *);
  279.   fprintf (stderr, "\ngdb: ");
  280.   vfprintf (stderr, string, args);
  281.   fprintf (stderr, "\n");
  282.   va_end (args);
  283.   exit (1);
  284. }
  285.  
  286. /* Print an error message and exit, dumping core.
  287.    The arguments are printed a la printf ().  */
  288.  
  289. /* VARARGS */
  290. static void
  291. fatal_dump_core (va_alist)
  292.      va_dcl
  293. {
  294.   va_list args;
  295.   char *string;
  296.  
  297.   va_start (args);
  298.   string = va_arg (args, char *);
  299.   /* "internal error" is always correct, since GDB should never dump
  300.      core, no matter what the input.  */
  301.   fprintf (stderr, "\ngdb internal error: ");
  302.   vfprintf (stderr, string, args);
  303.   fprintf (stderr, "\n");
  304.   va_end (args);
  305.  
  306.   signal (SIGQUIT, SIG_DFL);
  307.   kill (getpid (), SIGQUIT);
  308.   /* We should never get here, but just in case...  */
  309.   exit (1);
  310. }
  311.  
  312. /* Print the system error message for errno, and also mention STRING
  313.    as the file name for which the error was encountered.
  314.    Then return to command level.  */
  315.  
  316. void
  317. perror_with_name (string)
  318.      char *string;
  319. {
  320.   extern int sys_nerr;
  321.   extern char *sys_errlist[];
  322.   char *err;
  323.   char *combined;
  324.  
  325.   if (errno < sys_nerr)
  326.     err = sys_errlist[errno];
  327.   else
  328.     err = "unknown error";
  329.  
  330.   combined = (char *) alloca (strlen (err) + strlen (string) + 3);
  331.   strcpy (combined, string);
  332.   strcat (combined, ": ");
  333.   strcat (combined, err);
  334.  
  335.   /* I understand setting these is a matter of taste.  Still, some people
  336.      may clear errno but not know about bfd_error.  Doing this here is not
  337.      unreasonable. */
  338.   bfd_error = no_error;
  339.   errno = 0;
  340.  
  341.   error ("%s.", combined);
  342. }
  343.  
  344. /* Print the system error message for ERRCODE, and also mention STRING
  345.    as the file name for which the error was encountered.  */
  346.  
  347. void
  348. print_sys_errmsg (string, errcode)
  349.      char *string;
  350.      int errcode;
  351. {
  352.   extern int sys_nerr;
  353.   extern char *sys_errlist[];
  354.   char *err;
  355.   char *combined;
  356.  
  357.   if (errcode < sys_nerr)
  358.     err = sys_errlist[errcode];
  359.   else
  360.     err = "unknown error";
  361.  
  362.   combined = (char *) alloca (strlen (err) + strlen (string) + 3);
  363.   strcpy (combined, string);
  364.   strcat (combined, ": ");
  365.   strcat (combined, err);
  366.  
  367.   printf ("%s.\n", combined);
  368. }
  369.  
  370. /* Control C eventually causes this to be called, at a convenient time.  */
  371.  
  372. void
  373. quit ()
  374. {
  375.   target_terminal_ours ();
  376.   wrap_here ((char *)0);        /* Force out any pending output */
  377. #ifdef HAVE_TERMIO
  378.   ioctl (fileno (stdout), TCFLSH, 1);
  379. #else /* not HAVE_TERMIO */
  380.   ioctl (fileno (stdout), TIOCFLUSH, 0);
  381. #endif /* not HAVE_TERMIO */
  382. #ifdef TIOCGPGRP
  383.   error ("Quit");
  384. #else
  385.   error ("Quit (expect signal %d when inferior is resumed)", SIGINT);
  386. #endif /* TIOCGPGRP */
  387. }
  388.  
  389. /* Control C comes here */
  390.  
  391. void
  392. request_quit (signo)
  393.      int signo;
  394. {
  395.   quit_flag = 1;
  396.  
  397. #ifdef USG
  398.   /* Restore the signal handler.  */
  399.   signal (signo, request_quit);
  400. #endif
  401.  
  402.   if (immediate_quit)
  403.     quit ();
  404. }
  405.  
  406.  
  407. /* Memory management stuff (malloc friends).  */
  408.  
  409. #if defined (NO_MMALLOC)
  410.  
  411. PTR
  412. mmalloc (md, size)
  413.      PTR md;
  414.      long size;
  415. {
  416.   return (malloc (size));
  417. }
  418.  
  419. PTR
  420. mrealloc (md, ptr, size)
  421.      PTR md;
  422.      PTR ptr;
  423.      long size;
  424. {
  425.   if (ptr == 0)        /* Guard against old realloc's */
  426.     return malloc (size);
  427.   else
  428.     return realloc (ptr, size);
  429. }
  430.  
  431. void
  432. mfree (md, ptr)
  433.      PTR md;
  434.      PTR ptr;
  435. {
  436.   free (ptr);
  437. }
  438.  
  439. #endif    /* NO_MMALLOC */
  440.  
  441. #if defined (NO_MMALLOC) || defined (NO_MMALLOC_CHECK)
  442.  
  443. void
  444. init_malloc (md)
  445.      PTR md;
  446. {
  447. }
  448.  
  449. #else /* have mmalloc and want corruption checking  */
  450.  
  451. static void
  452. malloc_botch ()
  453. {
  454.   fatal_dump_core ("Memory corruption");
  455. }
  456.  
  457. /* Attempt to install hooks in mmalloc/mrealloc/mfree for the heap specified
  458.    by MD, to detect memory corruption.  Note that MD may be NULL to specify
  459.    the default heap that grows via sbrk.
  460.  
  461.    Note that for freshly created regions, we must call mmcheck prior to any
  462.    mallocs in the region.  Otherwise, any region which was allocated prior to
  463.    installing the checking hooks, which is later reallocated or freed, will
  464.    fail the checks!  The mmcheck function only allows initial hooks to be
  465.    installed before the first mmalloc.  However, anytime after we have called
  466.    mmcheck the first time to install the checking hooks, we can call it again
  467.    to update the function pointer to the memory corruption handler.
  468.  
  469.    Returns zero on failure, non-zero on success. */
  470.  
  471. void
  472. init_malloc (md)
  473.      PTR md;
  474. {
  475.   if (!mmcheck (md, malloc_botch))
  476.     {
  477.       warning ("internal error: failed to install memory consistency checks");
  478.     }
  479.  
  480.   (void) mmtrace ();
  481. }
  482.  
  483. #endif /* Have mmalloc and want corruption checking  */
  484.  
  485. /* Called when a memory allocation fails, with the number of bytes of
  486.    memory requested in SIZE. */
  487.  
  488. NORETURN void
  489. nomem (size)
  490.      long size;
  491. {
  492.   if (size > 0)
  493.     {
  494.       fatal ("virtual memory exhausted: can't allocate %ld bytes.", size);
  495.     }
  496.   else
  497.     {
  498.       fatal ("virtual memory exhausted.");
  499.     }
  500. }
  501.  
  502. /* Like mmalloc but get error if no storage available, and protect against
  503.    the caller wanting to allocate zero bytes.  Whether to return NULL for
  504.    a zero byte request, or translate the request into a request for one
  505.    byte of zero'd storage, is a religious issue. */
  506.  
  507. PTR
  508. xmmalloc (md, size)
  509.      PTR md;
  510.      long size;
  511. {
  512.   register PTR val;
  513.  
  514.   if (size == 0)
  515.     {
  516.       val = NULL;
  517.     }
  518.   else if ((val = mmalloc (md, size)) == NULL)
  519.     {
  520.       nomem (size);
  521.     }
  522.   return (val);
  523. }
  524.  
  525. /* Like mrealloc but get error if no storage available.  */
  526.  
  527. PTR
  528. xmrealloc (md, ptr, size)
  529.      PTR md;
  530.      PTR ptr;
  531.      long size;
  532. {
  533.   register PTR val;
  534.  
  535.   if (ptr != NULL)
  536.     {
  537.       val = mrealloc (md, ptr, size);
  538.     }
  539.   else
  540.     {
  541.       val = mmalloc (md, size);
  542.     }
  543.   if (val == NULL)
  544.     {
  545.       nomem (size);
  546.     }
  547.   return (val);
  548. }
  549.  
  550. /* Like malloc but get error if no storage available, and protect against
  551.    the caller wanting to allocate zero bytes.  */
  552.  
  553. PTR
  554. xmalloc (size)
  555.      long size;
  556. {
  557.   return (xmmalloc ((void *) NULL, size));
  558. }
  559.  
  560. /* Like mrealloc but get error if no storage available.  */
  561.  
  562. PTR
  563. xrealloc (ptr, size)
  564.      PTR ptr;
  565.      long size;
  566. {
  567.   return (xmrealloc ((void *) NULL, ptr, size));
  568. }
  569.  
  570.  
  571. /* My replacement for the read system call.
  572.    Used like `read' but keeps going if `read' returns too soon.  */
  573.  
  574. int
  575. myread (desc, addr, len)
  576.      int desc;
  577.      char *addr;
  578.      int len;
  579. {
  580.   register int val;
  581.   int orglen = len;
  582.  
  583.   while (len > 0)
  584.     {
  585.       val = read (desc, addr, len);
  586.       if (val < 0)
  587.     return val;
  588.       if (val == 0)
  589.     return orglen - len;
  590.       len -= val;
  591.       addr += val;
  592.     }
  593.   return orglen;
  594. }
  595.  
  596. /* Make a copy of the string at PTR with SIZE characters
  597.    (and add a null character at the end in the copy).
  598.    Uses malloc to get the space.  Returns the address of the copy.  */
  599.  
  600. char *
  601. savestring (ptr, size)
  602.      const char *ptr;
  603.      int size;
  604. {
  605.   register char *p = (char *) xmalloc (size + 1);
  606.   bcopy (ptr, p, size);
  607.   p[size] = 0;
  608.   return p;
  609. }
  610.  
  611. char *
  612. msavestring (md, ptr, size)
  613.      void *md;
  614.      const char *ptr;
  615.      int size;
  616. {
  617.   register char *p = (char *) xmmalloc (md, size + 1);
  618.   bcopy (ptr, p, size);
  619.   p[size] = 0;
  620.   return p;
  621. }
  622.  
  623. /* The "const" is so it compiles under DGUX (which prototypes strsave
  624.    in <string.h>.  FIXME: This should be named "xstrsave", shouldn't it?
  625.    Doesn't real strsave return NULL if out of memory?  */
  626. char *
  627. strsave (ptr)
  628.      const char *ptr;
  629. {
  630.   return savestring (ptr, strlen (ptr));
  631. }
  632.  
  633. char *
  634. mstrsave (md, ptr)
  635.      void *md;
  636.      const char *ptr;
  637. {
  638.   return (msavestring (md, ptr, strlen (ptr)));
  639. }
  640.  
  641. void
  642. print_spaces (n, file)
  643.      register int n;
  644.      register FILE *file;
  645. {
  646.   while (n-- > 0)
  647.     fputc (' ', file);
  648. }
  649.  
  650. /* Ask user a y-or-n question and return 1 iff answer is yes.
  651.    Takes three args which are given to printf to print the question.
  652.    The first, a control string, should end in "? ".
  653.    It should not say how to answer, because we do that.  */
  654.  
  655. /* VARARGS */
  656. int
  657. query (va_alist)
  658.      va_dcl
  659. {
  660.   va_list args;
  661.   char *ctlstr;
  662.   register int answer;
  663.   register int ans2;
  664.  
  665.   /* Automatically answer "yes" if input is not from a terminal.  */
  666.   if (!input_from_terminal_p ())
  667.     return 1;
  668.  
  669.   while (1)
  670.     {
  671.       va_start (args);
  672.       ctlstr = va_arg (args, char *);
  673.       vfprintf (stdout, ctlstr, args);
  674.       va_end (args);
  675.       printf ("(y or n) ");
  676.       fflush (stdout);
  677.       answer = fgetc (stdin);
  678.       clearerr (stdin);        /* in case of C-d */
  679.       if (answer == EOF)    /* C-d */
  680.         return 1;
  681.       if (answer != '\n')    /* Eat rest of input line, to EOF or newline */
  682.     do 
  683.       {
  684.         ans2 = fgetc (stdin);
  685.         clearerr (stdin);
  686.       }
  687.         while (ans2 != EOF && ans2 != '\n');
  688.       if (answer >= 'a')
  689.     answer -= 040;
  690.       if (answer == 'Y')
  691.     return 1;
  692.       if (answer == 'N')
  693.     return 0;
  694.       printf ("Please answer y or n.\n");
  695.     }
  696. }
  697.  
  698.  
  699. /* Parse a C escape sequence.  STRING_PTR points to a variable
  700.    containing a pointer to the string to parse.  That pointer
  701.    should point to the character after the \.  That pointer
  702.    is updated past the characters we use.  The value of the
  703.    escape sequence is returned.
  704.  
  705.    A negative value means the sequence \ newline was seen,
  706.    which is supposed to be equivalent to nothing at all.
  707.  
  708.    If \ is followed by a null character, we return a negative
  709.    value and leave the string pointer pointing at the null character.
  710.  
  711.    If \ is followed by 000, we return 0 and leave the string pointer
  712.    after the zeros.  A value of 0 does not mean end of string.  */
  713.  
  714. int
  715. parse_escape (string_ptr)
  716.      char **string_ptr;
  717. {
  718.   register int c = *(*string_ptr)++;
  719.   switch (c)
  720.     {
  721.     case 'a':
  722.       return 007;        /* Bell (alert) char */
  723.     case 'b':
  724.       return '\b';
  725.     case 'e':            /* Escape character */
  726.       return 033;
  727.     case 'f':
  728.       return '\f';
  729.     case 'n':
  730.       return '\n';
  731.     case 'r':
  732.       return '\r';
  733.     case 't':
  734.       return '\t';
  735.     case 'v':
  736.       return '\v';
  737.     case '\n':
  738.       return -2;
  739.     case 0:
  740.       (*string_ptr)--;
  741.       return 0;
  742.     case '^':
  743.       c = *(*string_ptr)++;
  744.       if (c == '\\')
  745.     c = parse_escape (string_ptr);
  746.       if (c == '?')
  747.     return 0177;
  748.       return (c & 0200) | (c & 037);
  749.       
  750.     case '0':
  751.     case '1':
  752.     case '2':
  753.     case '3':
  754.     case '4':
  755.     case '5':
  756.     case '6':
  757.     case '7':
  758.       {
  759.     register int i = c - '0';
  760.     register int count = 0;
  761.     while (++count < 3)
  762.       {
  763.         if ((c = *(*string_ptr)++) >= '0' && c <= '7')
  764.           {
  765.         i *= 8;
  766.         i += c - '0';
  767.           }
  768.         else
  769.           {
  770.         (*string_ptr)--;
  771.         break;
  772.           }
  773.       }
  774.     return i;
  775.       }
  776.     default:
  777.       return c;
  778.     }
  779. }
  780.  
  781. /* Print the character C on STREAM as part of the contents
  782.    of a literal string whose delimiter is QUOTER.  */
  783.  
  784. void
  785. printchar (c, stream, quoter)
  786.      register int c;
  787.      FILE *stream;
  788.      int quoter;
  789. {
  790.  
  791.   if (c < 040 || (sevenbit_strings && c >= 0177)) {
  792.     switch (c)
  793.       {
  794.       case '\n':
  795.     fputs_filtered ("\\n", stream);
  796.     break;
  797.       case '\b':
  798.     fputs_filtered ("\\b", stream);
  799.     break;
  800.       case '\t':
  801.     fputs_filtered ("\\t", stream);
  802.     break;
  803.       case '\f':
  804.     fputs_filtered ("\\f", stream);
  805.     break;
  806.       case '\r':
  807.     fputs_filtered ("\\r", stream);
  808.     break;
  809.       case '\033':
  810.     fputs_filtered ("\\e", stream);
  811.     break;
  812.       case '\007':
  813.     fputs_filtered ("\\a", stream);
  814.     break;
  815.       default:
  816.     fprintf_filtered (stream, "\\%.3o", (unsigned int) c);
  817.     break;
  818.       }
  819.   } else {
  820.     if (c == '\\' || c == quoter)
  821.       fputs_filtered ("\\", stream);
  822.     fprintf_filtered (stream, "%c", c);
  823.   }
  824. }
  825.  
  826. /* Number of lines per page or UINT_MAX if paging is disabled.  */
  827. static unsigned int lines_per_page;
  828. /* Number of chars per line or UNIT_MAX is line folding is disabled.  */
  829. static unsigned int chars_per_line;
  830. /* Current count of lines printed on this page, chars on this line.  */
  831. static unsigned int lines_printed, chars_printed;
  832.  
  833. /* Buffer and start column of buffered text, for doing smarter word-
  834.    wrapping.  When someone calls wrap_here(), we start buffering output
  835.    that comes through fputs_filtered().  If we see a newline, we just
  836.    spit it out and forget about the wrap_here().  If we see another
  837.    wrap_here(), we spit it out and remember the newer one.  If we see
  838.    the end of the line, we spit out a newline, the indent, and then
  839.    the buffered output.
  840.  
  841.    wrap_column is the column number on the screen where wrap_buffer begins.
  842.      When wrap_column is zero, wrapping is not in effect.
  843.    wrap_buffer is malloc'd with chars_per_line+2 bytes. 
  844.      When wrap_buffer[0] is null, the buffer is empty.
  845.    wrap_pointer points into it at the next character to fill.
  846.    wrap_indent is the string that should be used as indentation if the
  847.      wrap occurs.  */
  848.  
  849. static char *wrap_buffer, *wrap_pointer, *wrap_indent;
  850. static int wrap_column;
  851.  
  852. /* ARGSUSED */
  853. static void 
  854. set_width_command (args, from_tty, c)
  855.      char *args;
  856.      int from_tty;
  857.      struct cmd_list_element *c;
  858. {
  859.   if (!wrap_buffer)
  860.     {
  861.       wrap_buffer = (char *) xmalloc (chars_per_line + 2);
  862.       wrap_buffer[0] = '\0';
  863.     }
  864.   else
  865.     wrap_buffer = (char *) xrealloc (wrap_buffer, chars_per_line + 2);
  866.   wrap_pointer = wrap_buffer;    /* Start it at the beginning */
  867. }
  868.  
  869. static void
  870. prompt_for_continue ()
  871. {
  872.   char *ignore;
  873.  
  874.   immediate_quit++;
  875.   ignore = gdb_readline ("---Type <return> to continue---");
  876.   if (ignore)
  877.     free (ignore);
  878.   chars_printed = lines_printed = 0;
  879.   immediate_quit--;
  880.   dont_repeat ();        /* Forget prev cmd -- CR won't repeat it. */
  881. }
  882.  
  883. /* Reinitialize filter; ie. tell it to reset to original values.  */
  884.  
  885. void
  886. reinitialize_more_filter ()
  887. {
  888.   lines_printed = 0;
  889.   chars_printed = 0;
  890. }
  891.  
  892. /* Indicate that if the next sequence of characters overflows the line,
  893.    a newline should be inserted here rather than when it hits the end. 
  894.    If INDENT is nonzero, it is a string to be printed to indent the
  895.    wrapped part on the next line.  INDENT must remain accessible until
  896.    the next call to wrap_here() or until a newline is printed through
  897.    fputs_filtered().
  898.  
  899.    If the line is already overfull, we immediately print a newline and
  900.    the indentation, and disable further wrapping.
  901.  
  902.    If we don't know the width of lines, but we know the page height,
  903.    we must not wrap words, but should still keep track of newlines
  904.    that were explicitly printed.
  905.  
  906.    INDENT should not contain tabs, as that
  907.    will mess up the char count on the next line.  FIXME.  */
  908.  
  909. void
  910. wrap_here(indent)
  911.   char *indent;
  912. {
  913.   if (wrap_buffer[0])
  914.     {
  915.       *wrap_pointer = '\0';
  916.       fputs (wrap_buffer, stdout);
  917.     }
  918.   wrap_pointer = wrap_buffer;
  919.   wrap_buffer[0] = '\0';
  920.   if (chars_per_line == UINT_MAX)        /* No line overflow checking */
  921.     {
  922.       wrap_column = 0;
  923.     }
  924.   else if (chars_printed >= chars_per_line)
  925.     {
  926.       puts_filtered ("\n");
  927.       puts_filtered (indent);
  928.       wrap_column = 0;
  929.     }
  930.   else
  931.     {
  932.       wrap_column = chars_printed;
  933.       wrap_indent = indent;
  934.     }
  935. }
  936.  
  937. /* Like fputs but pause after every screenful, and can wrap at points
  938.    other than the final character of a line.
  939.    Unlike fputs, fputs_filtered does not return a value.
  940.    It is OK for LINEBUFFER to be NULL, in which case just don't print
  941.    anything.
  942.  
  943.    Note that a longjmp to top level may occur in this routine
  944.    (since prompt_for_continue may do so) so this routine should not be
  945.    called when cleanups are not in place.  */
  946.  
  947. void
  948. fputs_filtered (linebuffer, stream)
  949.      const char *linebuffer;
  950.      FILE *stream;
  951. {
  952.   const char *lineptr;
  953.  
  954.   if (linebuffer == 0)
  955.     return;
  956.   
  957.   /* Don't do any filtering if it is disabled.  */
  958.   if (stream != stdout
  959.    || (lines_per_page == UINT_MAX && chars_per_line == UINT_MAX))
  960.     {
  961.       fputs (linebuffer, stream);
  962.       return;
  963.     }
  964.  
  965.   /* Go through and output each character.  Show line extension
  966.      when this is necessary; prompt user for new page when this is
  967.      necessary.  */
  968.   
  969.   lineptr = linebuffer;
  970.   while (*lineptr)
  971.     {
  972.       /* Possible new page.  */
  973.       if (lines_printed >= lines_per_page - 1)
  974.     prompt_for_continue ();
  975.  
  976.       while (*lineptr && *lineptr != '\n')
  977.     {
  978.       /* Print a single line.  */
  979.       if (*lineptr == '\t')
  980.         {
  981.           if (wrap_column)
  982.         *wrap_pointer++ = '\t';
  983.           else
  984.         putc ('\t', stream);
  985.           /* Shifting right by 3 produces the number of tab stops
  986.              we have already passed, and then adding one and
  987.          shifting left 3 advances to the next tab stop.  */
  988.           chars_printed = ((chars_printed >> 3) + 1) << 3;
  989.           lineptr++;
  990.         }
  991.       else
  992.         {
  993.           if (wrap_column)
  994.         *wrap_pointer++ = *lineptr;
  995.           else
  996.             putc (*lineptr, stream);
  997.           chars_printed++;
  998.           lineptr++;
  999.         }
  1000.       
  1001.       if (chars_printed >= chars_per_line)
  1002.         {
  1003.           unsigned int save_chars = chars_printed;
  1004.  
  1005.           chars_printed = 0;
  1006.           lines_printed++;
  1007.           /* If we aren't actually wrapping, don't output newline --
  1008.          if chars_per_line is right, we probably just overflowed
  1009.          anyway; if it's wrong, let us keep going.  */
  1010.           if (wrap_column)
  1011.         putc ('\n', stream);
  1012.  
  1013.           /* Possible new page.  */
  1014.           if (lines_printed >= lines_per_page - 1)
  1015.         prompt_for_continue ();
  1016.  
  1017.           /* Now output indentation and wrapped string */
  1018.           if (wrap_column)
  1019.         {
  1020.           if (wrap_indent)
  1021.             fputs (wrap_indent, stream);
  1022.           *wrap_pointer = '\0';        /* Null-terminate saved stuff */
  1023.           fputs (wrap_buffer, stream);    /* and eject it */
  1024.           /* FIXME, this strlen is what prevents wrap_indent from
  1025.              containing tabs.  However, if we recurse to print it
  1026.              and count its chars, we risk trouble if wrap_indent is
  1027.              longer than (the user settable) chars_per_line. 
  1028.              Note also that this can set chars_printed > chars_per_line
  1029.              if we are printing a long string.  */
  1030.           chars_printed = strlen (wrap_indent)
  1031.                 + (save_chars - wrap_column);
  1032.           wrap_pointer = wrap_buffer;    /* Reset buffer */
  1033.           wrap_buffer[0] = '\0';
  1034.           wrap_column = 0;        /* And disable fancy wrap */
  1035.          }
  1036.         }
  1037.     }
  1038.  
  1039.       if (*lineptr == '\n')
  1040.     {
  1041.       chars_printed = 0;
  1042.       wrap_here ((char *)0);  /* Spit out chars, cancel further wraps */
  1043.       lines_printed++;
  1044.       putc ('\n', stream);
  1045.       lineptr++;
  1046.     }
  1047.     }
  1048. }
  1049.  
  1050.  
  1051. /* fputs_demangled is a variant of fputs_filtered that
  1052.    demangles g++ names.*/
  1053.  
  1054. void
  1055. fputs_demangled (linebuffer, stream, arg_mode)
  1056.      char *linebuffer;
  1057.      FILE *stream;
  1058.      int arg_mode;
  1059. {
  1060. #define SYMBOL_MAX 1024
  1061.  
  1062. #define SYMBOL_CHAR(c) (isascii(c) \
  1063.   && (isalnum(c) || (c) == '_' || (c) == CPLUS_MARKER))
  1064.  
  1065.   char buf[SYMBOL_MAX+1];
  1066. # define SLOP 5        /* How much room to leave in buf */
  1067.   char *p;
  1068.  
  1069.   if (linebuffer == NULL)
  1070.     return;
  1071.  
  1072.   /* If user wants to see raw output, no problem.  */
  1073.   if (!demangle) {
  1074.     fputs_filtered (linebuffer, stream);
  1075.     return;
  1076.   }
  1077.  
  1078.   p = linebuffer;
  1079.  
  1080.   while ( *p != (char) 0 ) {
  1081.     int i = 0;
  1082.  
  1083.     /* collect non-interesting characters into buf */
  1084.     while ( *p != (char) 0 && !SYMBOL_CHAR(*p) && i < (int)sizeof(buf)-SLOP ) {
  1085.       buf[i++] = *p;
  1086.       p++;
  1087.     }
  1088.     if (i > 0) {
  1089.       /* output the non-interesting characters without demangling */
  1090.       buf[i] = (char) 0;
  1091.       fputs_filtered(buf, stream);
  1092.       i = 0;  /* reset buf */
  1093.     }
  1094.  
  1095.     /* and now the interesting characters */
  1096.     while (i < SYMBOL_MAX
  1097.      && *p != (char) 0
  1098.      && SYMBOL_CHAR(*p)
  1099.      && i < (int)sizeof(buf) - SLOP) {
  1100.       buf[i++] = *p;
  1101.       p++;
  1102.     }
  1103.     buf[i] = (char) 0;
  1104.     if (i > 0) {
  1105.       char * result;
  1106.       
  1107.       if ( (result = cplus_demangle(buf, arg_mode)) != NULL ) {
  1108.     fputs_filtered(result, stream);
  1109.     free(result);
  1110.       }
  1111.       else {
  1112.     fputs_filtered(buf, stream);
  1113.       }
  1114.     }
  1115.   }
  1116. }
  1117.  
  1118. /* Print a variable number of ARGS using format FORMAT.  If this
  1119.    information is going to put the amount written (since the last call
  1120.    to INITIALIZE_MORE_FILTER or the last page break) over the page size,
  1121.    print out a pause message and do a gdb_readline to get the users
  1122.    permision to continue.
  1123.  
  1124.    Unlike fprintf, this function does not return a value.
  1125.  
  1126.    We implement three variants, vfprintf (takes a vararg list and stream),
  1127.    fprintf (takes a stream to write on), and printf (the usual).
  1128.  
  1129.    Note that this routine has a restriction that the length of the
  1130.    final output line must be less than 255 characters *or* it must be
  1131.    less than twice the size of the format string.  This is a very
  1132.    arbitrary restriction, but it is an internal restriction, so I'll
  1133.    put it in.  This means that the %s format specifier is almost
  1134.    useless; unless the caller can GUARANTEE that the string is short
  1135.    enough, fputs_filtered should be used instead.
  1136.  
  1137.    Note also that a longjmp to top level may occur in this routine
  1138.    (since prompt_for_continue may do so) so this routine should not be
  1139.    called when cleanups are not in place.  */
  1140.  
  1141. static void
  1142. vfprintf_filtered (stream, format, args)
  1143.      FILE *stream;
  1144.      char *format;
  1145.      va_list args;
  1146. {
  1147.   static char *linebuffer = (char *) 0;
  1148.   static int line_size;
  1149.   int format_length;
  1150.  
  1151.   format_length = strlen (format);
  1152.  
  1153.   /* Allocated linebuffer for the first time.  */
  1154.   if (!linebuffer)
  1155.     {
  1156.       linebuffer = (char *) xmalloc (255);
  1157.       line_size = 255;
  1158.     }
  1159.  
  1160.   /* Reallocate buffer to a larger size if this is necessary.  */
  1161.   if (format_length * 2 > line_size)
  1162.     {
  1163.       line_size = format_length * 2;
  1164.  
  1165.       /* You don't have to copy.  */
  1166.       free (linebuffer);
  1167.       linebuffer = (char *) xmalloc (line_size);
  1168.     }
  1169.  
  1170.  
  1171.   /* This won't blow up if the restrictions described above are
  1172.      followed.   */
  1173.   (void) vsprintf (linebuffer, format, args);
  1174.  
  1175.   fputs_filtered (linebuffer, stream);
  1176. }
  1177.  
  1178. /* VARARGS */
  1179. void
  1180. fprintf_filtered (va_alist)
  1181.      va_dcl
  1182. {
  1183.   FILE *stream;
  1184.   char *format;
  1185.   va_list args;
  1186.  
  1187.   va_start (args);
  1188.   stream = va_arg (args, FILE *);
  1189.   format = va_arg (args, char *);
  1190.  
  1191.   /* This won't blow up if the restrictions described above are
  1192.      followed.   */
  1193.   vfprintf_filtered (stream, format, args);
  1194.   va_end (args);
  1195. }
  1196.  
  1197. /* VARARGS */
  1198. void
  1199. printf_filtered (va_alist)
  1200.      va_dcl
  1201. {
  1202.   va_list args;
  1203.   char *format;
  1204.  
  1205.   va_start (args);
  1206.   format = va_arg (args, char *);
  1207.  
  1208.   vfprintf_filtered (stdout, format, args);
  1209.   va_end (args);
  1210. }
  1211.  
  1212. /* Easy */
  1213.  
  1214. void
  1215. puts_filtered (string)
  1216.      char *string;
  1217. {
  1218.   fputs_filtered (string, stdout);
  1219. }
  1220.  
  1221. /* Return a pointer to N spaces and a null.  The pointer is good
  1222.    until the next call to here.  */
  1223. char *
  1224. n_spaces (n)
  1225.      int n;
  1226. {
  1227.   register char *t;
  1228.   static char *spaces;
  1229.   static int max_spaces;
  1230.  
  1231.   if (n > max_spaces)
  1232.     {
  1233.       if (spaces)
  1234.     free (spaces);
  1235.       spaces = (char *) xmalloc (n+1);
  1236.       for (t = spaces+n; t != spaces;)
  1237.     *--t = ' ';
  1238.       spaces[n] = '\0';
  1239.       max_spaces = n;
  1240.     }
  1241.  
  1242.   return spaces + max_spaces - n;
  1243. }
  1244.  
  1245. /* Print N spaces.  */
  1246. void
  1247. print_spaces_filtered (n, stream)
  1248.      int n;
  1249.      FILE *stream;
  1250. {
  1251.   fputs_filtered (n_spaces (n), stream);
  1252. }
  1253.  
  1254. /* C++ demangler stuff.  */
  1255.  
  1256. /* Print NAME on STREAM, demangling if necessary.  */
  1257. void
  1258. fprint_symbol (stream, name)
  1259.      FILE *stream;
  1260.      char *name;
  1261. {
  1262.   char *demangled;
  1263.   if ((!demangle) || NULL == (demangled = cplus_demangle (name, 1)))
  1264.     fputs_filtered (name, stream);
  1265.   else
  1266.     {
  1267.       fputs_filtered (demangled, stream);
  1268.       free (demangled);
  1269.     }
  1270. }
  1271.  
  1272. void
  1273. _initialize_utils ()
  1274. {
  1275.   struct cmd_list_element *c;
  1276.  
  1277.   c = add_set_cmd ("width", class_support, var_uinteger, 
  1278.           (char *)&chars_per_line,
  1279.           "Set number of characters gdb thinks are in a line.",
  1280.           &setlist);
  1281.   add_show_from_set (c, &showlist);
  1282.   c->function.sfunc = set_width_command;
  1283.  
  1284.   add_show_from_set
  1285.     (add_set_cmd ("height", class_support,
  1286.           var_uinteger, (char *)&lines_per_page,
  1287.           "Set number of lines gdb thinks are in a page.", &setlist),
  1288.      &showlist);
  1289.   
  1290.   /* These defaults will be used if we are unable to get the correct
  1291.      values from termcap.  */
  1292.   lines_per_page = 24;
  1293.   chars_per_line = 80;
  1294.   /* Initialize the screen height and width from termcap.  */
  1295.   {
  1296.     char *termtype = getenv ("TERM");
  1297.  
  1298.     /* Positive means success, nonpositive means failure.  */
  1299.     int status;
  1300.  
  1301.     /* 2048 is large enough for all known terminals, according to the
  1302.        GNU termcap manual.  */
  1303.     char term_buffer[2048];
  1304.  
  1305.     if (termtype)
  1306.       {
  1307.     status = tgetent (term_buffer, termtype);
  1308.     if (status > 0)
  1309.       {
  1310.         int val;
  1311.         
  1312.         val = tgetnum ("li");
  1313.         if (val >= 0)
  1314.           lines_per_page = val;
  1315.         else
  1316.           /* The number of lines per page is not mentioned
  1317.          in the terminal description.  This probably means
  1318.          that paging is not useful (e.g. emacs shell window),
  1319.          so disable paging.  */
  1320.           lines_per_page = UINT_MAX;
  1321.         
  1322.         val = tgetnum ("co");
  1323.         if (val >= 0)
  1324.           chars_per_line = val;
  1325.       }
  1326.       }
  1327.   }
  1328.  
  1329. #if defined(SIGWINCH) && defined(SIGWINCH_HANDLER)
  1330.  
  1331.   /* If there is a better way to determine the window size, use it. */
  1332.   SIGWINCH_HANDLER ();
  1333. #endif
  1334.  
  1335.   /* If the output is not a terminal, don't paginate it.  */
  1336.   if (!ISATTY (stdout))
  1337.     lines_per_page = UINT_MAX;
  1338.  
  1339.   set_width_command ((char *)NULL, 0, c);
  1340.  
  1341.   add_show_from_set
  1342.     (add_set_cmd ("demangle", class_support, var_boolean, 
  1343.           (char *)&demangle,
  1344.         "Set demangling of encoded C++ names when displaying symbols.",
  1345.           &setprintlist),
  1346.      &showprintlist);
  1347.  
  1348.   add_show_from_set
  1349.     (add_set_cmd ("sevenbit-strings", class_support, var_boolean, 
  1350.           (char *)&sevenbit_strings,
  1351.    "Set printing of 8-bit characters in strings as \\nnn.",
  1352.           &setprintlist),
  1353.      &showprintlist);
  1354.  
  1355.   add_show_from_set
  1356.     (add_set_cmd ("asm-demangle", class_support, var_boolean, 
  1357.           (char *)&asm_demangle,
  1358.     "Set demangling of C++ names in disassembly listings.",
  1359.           &setprintlist),
  1360.      &showprintlist);
  1361. }
  1362.  
  1363. /* Machine specific function to handle SIGWINCH signal. */
  1364.  
  1365. #ifdef  SIGWINCH_HANDLER_BODY
  1366.         SIGWINCH_HANDLER_BODY
  1367. #endif
  1368.