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