home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 9 / FreshFishVol9-CD2.bin / bbs / gnu / gdb-4.14-src.lha / gdb-4.14 / gdb / utils.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-04  |  42.0 KB  |  1,815 lines

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