home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb.new / gdb-4.0 / gdb / utils.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-24  |  29.9 KB  |  1,310 lines

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