20000-05-18 H.J. Lu (hjl@gnu.org)
[binutils-gdb.git] / gdb / utils.c
index 1111eeacd2534ada0513e80ba7b40f3309d5c4a1..45fdb1dd47b8055dd5858fac1ee51e0ccade7802 100644 (file)
@@ -1,5 +1,6 @@
 /* General utility routines for GDB, the GNU debugger.
-   Copyright 1986, 89, 90, 91, 92, 95, 96, 1998 Free Software Foundation, Inc.
+   Copyright 1986, 1989, 1990-1992, 1995, 1996, 1998, 2000
+   Free Software Foundation, Inc.
 
    This file is part of GDB.
 
 #include <term.h>
 #endif
 
+#ifdef __GO32__
+#include <pc.h>
+#endif
+
 /* SunOS's curses.h has a '#define reg register' in it.  Thank you Sun. */
 #ifdef reg
 #undef reg
@@ -57,14 +62,14 @@ void (*error_begin_hook) PARAMS ((void));
 
 /* Holds the last error message issued by gdb */
 
-static GDB_FILE *gdb_lasterr;
+static struct ui_file *gdb_lasterr;
 
 /* Prototypes for local functions */
 
-static void vfprintf_maybe_filtered PARAMS ((GDB_FILE *, const char *,
-                                            va_list, int));
+static void vfprintf_maybe_filtered (struct ui_file *, const char *,
+                                    va_list, int);
 
-static void fputs_maybe_filtered PARAMS ((const char *, GDB_FILE *, int));
+static void fputs_maybe_filtered (const char *, struct ui_file *, int);
 
 #if defined (USE_MMALLOC) && !defined (NO_MMCHECK)
 static void malloc_botch PARAMS ((void));
@@ -79,10 +84,6 @@ set_width_command PARAMS ((char *, int, struct cmd_list_element *));
 static void
 set_width PARAMS ((void));
 
-#ifndef GDB_FILE_ISATTY
-#define GDB_FILE_ISATTY(GDB_FILE_PTR)   (gdb_file_isatty(GDB_FILE_PTR))
-#endif
-
 /* Chain of cleanup actions established with make_cleanup,
    to be executed if an error happens.  */
 
@@ -159,41 +160,31 @@ int pagination_enabled = 1;
    Args are FUNCTION to clean up with, and ARG to pass to it.  */
 
 struct cleanup *
-make_cleanup (function, arg)
-     void (*function) PARAMS ((PTR));
-     PTR arg;
+make_cleanup (make_cleanup_ftype *function, void *arg)
 {
   return make_my_cleanup (&cleanup_chain, function, arg);
 }
 
 struct cleanup *
-make_final_cleanup (function, arg)
-     void (*function) PARAMS ((PTR));
-     PTR arg;
+make_final_cleanup (make_cleanup_ftype *function, void *arg)
 {
   return make_my_cleanup (&final_cleanup_chain, function, arg);
 }
 
 struct cleanup *
-make_run_cleanup (function, arg)
-     void (*function) PARAMS ((PTR));
-     PTR arg;
+make_run_cleanup (make_cleanup_ftype *function, void *arg)
 {
   return make_my_cleanup (&run_cleanup_chain, function, arg);
 }
 
 struct cleanup *
-make_exec_cleanup (function, arg)
-     void (*function) PARAMS ((PTR));
-     PTR arg;
+make_exec_cleanup (make_cleanup_ftype *function, void *arg)
 {
   return make_my_cleanup (&exec_cleanup_chain, function, arg);
 }
 
 struct cleanup *
-make_exec_error_cleanup (function, arg)
-     void (*function) PARAMS ((PTR));
-     PTR arg;
+make_exec_error_cleanup (make_cleanup_ftype *function, void *arg)
 {
   return make_my_cleanup (&exec_error_cleanup_chain, function, arg);
 }
@@ -213,22 +204,32 @@ make_cleanup_freeargv (arg)
 }
 
 static void
-do_gdb_file_delete (void *arg)
+do_bfd_close_cleanup (void *arg)
 {
-  gdb_file_delete (arg);
+  bfd_close (arg);
 }
 
 struct cleanup *
-make_cleanup_gdb_file_delete (struct gdb_file *arg)
+make_cleanup_bfd_close (bfd *abfd)
 {
-  return make_my_cleanup (&cleanup_chain, do_gdb_file_delete, arg);
+  return make_cleanup (do_bfd_close_cleanup, abfd);
+}
+
+static void
+do_ui_file_delete (void *arg)
+{
+  ui_file_delete (arg);
 }
 
 struct cleanup *
-make_my_cleanup (pmy_chain, function, arg)
-     struct cleanup **pmy_chain;
-     void (*function) PARAMS ((PTR));
-     PTR arg;
+make_cleanup_ui_file_delete (struct ui_file *arg)
+{
+  return make_my_cleanup (&cleanup_chain, do_ui_file_delete, arg);
+}
+
+struct cleanup *
+make_my_cleanup (struct cleanup **pmy_chain, make_cleanup_ftype *function,
+                void *arg)
 {
   register struct cleanup *new
   = (struct cleanup *) xmalloc (sizeof (struct cleanup));
@@ -327,7 +328,7 @@ discard_my_cleanups (pmy_chain, old_chain)
   while ((ptr = *pmy_chain) != old_chain)
     {
       *pmy_chain = ptr->next;
-      free ((PTR) ptr);
+      free (ptr);
     }
 }
 
@@ -386,10 +387,16 @@ restore_my_cleanups (pmy_chain, chain)
    to arrange to free the object thus allocated.  */
 
 void
-free_current_contents (location)
-     char **location;
+free_current_contents (void *ptr)
 {
-  free (*location);
+  void **location = ptr;
+  if (location == NULL)
+    internal_error ("free_current_contents: NULL pointer");
+  if (*location != NULL)
+    {
+      free (*location);
+      *location = NULL;
+    }
 }
 
 /* Provide a known function that does nothing, to use as a base for
@@ -401,8 +408,7 @@ free_current_contents (location)
 
 /* ARGSUSED */
 void
-null_cleanup (arg)
-     PTR arg;
+null_cleanup (void *arg)
 {
 }
 
@@ -468,7 +474,7 @@ discard_all_continuations ()
     }
 }
 
-/* Add a continuation to the continuation list, the gloabl list
+/* Add a continuation to the continuation list, the global list
    intermediate_continuation. The new continuation will be added at the front.*/
 void
 add_intermediate_continuation (continuation_hook, arg_list)
@@ -617,7 +623,7 @@ verror (const char *string, va_list args)
      va_list twice which works on some platforms and fails miserably on
      others. */
   /* Save it as the last error */
-  gdb_file_rewind (gdb_lasterr);
+  ui_file_rewind (gdb_lasterr);
   vfprintf_filtered (gdb_lasterr, string, args);
   /* Retrieve the last error and print it to gdb_stderr */
   err_string = error_last_message ();
@@ -638,10 +644,10 @@ error (const char *string,...)
 }
 
 NORETURN void
-error_stream (GDB_FILE *stream)
+error_stream (struct ui_file *stream)
 {
   long size;
-  char *msg = gdb_file_xstrdup (stream, &size);
+  char *msg = ui_file_xstrdup (stream, &size);
   make_cleanup (free, msg);
   error ("%s", msg);
 }
@@ -652,7 +658,7 @@ char *
 error_last_message (void)
 {
   long len;
-  return gdb_file_xstrdup (gdb_lasterr, &len);
+  return ui_file_xstrdup (gdb_lasterr, &len);
 }
   
 /* This is to be called by main() at the very beginning */
@@ -691,6 +697,7 @@ internal_verror (const char *fmt, va_list ap)
     }
 
   /* Try to get the message out */
+  target_terminal_ours ();
   fputs_unfiltered ("gdb-internal-error: ", gdb_stderr);
   vfprintf_unfiltered (gdb_stderr, fmt, ap);
   fputs_unfiltered ("\n", gdb_stderr);
@@ -731,6 +738,7 @@ internal_error (char *string, ...)
 {
   va_list ap;
   va_start (ap, string);
+
   internal_verror (string, ap);
   va_end (ap);
 }
@@ -931,8 +939,8 @@ request_quit (signo)
 
 #if !defined (USE_MMALLOC)
 
-void *
-mcalloc (void *md, size_t number, size_t size)
+PTR
+mcalloc (PTR md, size_t number, size_t size)
 {
   return calloc (number, size);
 }
@@ -970,8 +978,7 @@ mfree (md, ptr)
 #if !defined (USE_MMALLOC) || defined (NO_MMCHECK)
 
 void
-init_malloc (md)
-     PTR md;
+init_malloc (void *md)
 {
 }
 
@@ -1003,8 +1010,7 @@ malloc_botch ()
 #endif
 
 void
-init_malloc (md)
-     PTR md;
+init_malloc (void *md)
 {
   if (!mmcheckf (md, malloc_botch, MMCHECK_FORCE))
     {
@@ -1161,10 +1167,7 @@ savestring (ptr, size)
 }
 
 char *
-msavestring (md, ptr, size)
-     PTR md;
-     const char *ptr;
-     int size;
+msavestring (void *md, const char *ptr, int size)
 {
   register char *p = (char *) xmmalloc (md, size + 1);
   memcpy (p, ptr, size);
@@ -1183,9 +1186,7 @@ strsave (ptr)
 }
 
 char *
-mstrsave (md, ptr)
-     PTR md;
-     const char *ptr;
+mstrsave (void *md, const char *ptr)
 {
   return (msavestring (md, ptr, strlen (ptr)));
 }
@@ -1193,7 +1194,7 @@ mstrsave (md, ptr)
 void
 print_spaces (n, file)
      register int n;
-     register GDB_FILE *file;
+     register struct ui_file *file;
 {
   fputs_unfiltered (n_spaces (n), file);
 }
@@ -1201,7 +1202,7 @@ print_spaces (n, file)
 /* Print a host address.  */
 
 void
-gdb_print_host_address (void *addr, struct gdb_file *stream)
+gdb_print_host_address (void *addr, struct ui_file *stream)
 {
 
   /* We could use the %p conversion specifier to fprintf if we had any
@@ -1405,14 +1406,14 @@ parse_escape (string_ptr)
    be call for printing things which are independent of the language
    of the program being debugged. */
 
-static void printchar PARAMS ((int c, void (*do_fputs) (const char *, GDB_FILE*), void (*do_fprintf) (GDB_FILE*, const char *, ...), GDB_FILE *stream, int quoter));
+static void printchar (int c, void (*do_fputs) (const char *, struct ui_file*), void (*do_fprintf) (struct ui_file*, const char *, ...), struct ui_file *stream, int quoter);
 
 static void
 printchar (c, do_fputs, do_fprintf, stream, quoter)
      int c;
-     void (*do_fputs) PARAMS ((const char *, GDB_FILE*));
-     void (*do_fprintf) PARAMS ((GDB_FILE*, const char *, ...));
-     GDB_FILE *stream;
+     void (*do_fputs) PARAMS ((const char *, struct ui_file*));
+     void (*do_fprintf) PARAMS ((struct ui_file*, const char *, ...));
+     struct ui_file *stream;
      int quoter;
 {
 
@@ -1467,7 +1468,7 @@ void
 fputstr_filtered (str, quoter, stream)
      const char *str;
      int quoter;
-     GDB_FILE *stream;
+     struct ui_file *stream;
 {
   while (*str)
     printchar (*str++, fputs_filtered, fprintf_filtered, stream, quoter);
@@ -1477,7 +1478,7 @@ void
 fputstr_unfiltered (str, quoter, stream)
      const char *str;
      int quoter;
-     GDB_FILE *stream;
+     struct ui_file *stream;
 {
   while (*str)
     printchar (*str++, fputs_unfiltered, fprintf_unfiltered, stream, quoter);
@@ -1488,7 +1489,7 @@ fputstrn_unfiltered (str, n, quoter, stream)
      const char *str;
      int n;
      int quoter;
-     GDB_FILE *stream;
+     struct ui_file *stream;
 {
   int i;
   for (i = 0; i < n; i++)
@@ -1598,7 +1599,7 @@ init_page_info ()
 #endif
 #endif
       /* If the output is not a terminal, don't paginate it.  */
-      if (!GDB_FILE_ISATTY (gdb_stdout))
+      if (!ui_file_isatty (gdb_stdout))
        lines_per_page = UINT_MAX;
     }                          /* the command_line_version */
   set_width ();
@@ -1788,7 +1789,7 @@ begin_line ()
 static void
 fputs_maybe_filtered (linebuffer, stream, filter)
      const char *linebuffer;
-     GDB_FILE *stream;
+     struct ui_file *stream;
      int filter;
 {
   const char *lineptr;
@@ -1892,7 +1893,7 @@ fputs_maybe_filtered (linebuffer, stream, filter)
 void
 fputs_filtered (linebuffer, stream)
      const char *linebuffer;
-     GDB_FILE *stream;
+     struct ui_file *stream;
 {
   fputs_maybe_filtered (linebuffer, stream, 1);
 }
@@ -1902,24 +1903,24 @@ putchar_unfiltered (c)
      int c;
 {
   char buf = c;
-  gdb_file_write (gdb_stdout, &buf, 1);
+  ui_file_write (gdb_stdout, &buf, 1);
   return c;
 }
 
 int
 fputc_unfiltered (c, stream)
      int c;
-     GDB_FILE *stream;
+     struct ui_file *stream;
 {
   char buf = c;
-  gdb_file_write (stream, &buf, 1);
+  ui_file_write (stream, &buf, 1);
   return c;
 }
 
 int
 fputc_filtered (c, stream)
      int c;
-     GDB_FILE *stream;
+     struct ui_file *stream;
 {
   char buf[2];
 
@@ -2033,7 +2034,7 @@ puts_debug (prefix, string, suffix)
 
 static void
 vfprintf_maybe_filtered (stream, format, args, filter)
-     GDB_FILE *stream;
+     struct ui_file *stream;
      const char *format;
      va_list args;
      int filter;
@@ -2055,7 +2056,7 @@ vfprintf_maybe_filtered (stream, format, args, filter)
 
 void
 vfprintf_filtered (stream, format, args)
-     GDB_FILE *stream;
+     struct ui_file *stream;
      const char *format;
      va_list args;
 {
@@ -2064,7 +2065,7 @@ vfprintf_filtered (stream, format, args)
 
 void
 vfprintf_unfiltered (stream, format, args)
-     GDB_FILE *stream;
+     struct ui_file *stream;
      const char *format;
      va_list args;
 {
@@ -2099,7 +2100,7 @@ vprintf_unfiltered (format, args)
 }
 
 void
-fprintf_filtered (GDB_FILE * stream, const char *format,...)
+fprintf_filtered (struct ui_file * stream, const char *format,...)
 {
   va_list args;
   va_start (args, format);
@@ -2108,7 +2109,7 @@ fprintf_filtered (GDB_FILE * stream, const char *format,...)
 }
 
 void
-fprintf_unfiltered (GDB_FILE * stream, const char *format,...)
+fprintf_unfiltered (struct ui_file * stream, const char *format,...)
 {
   va_list args;
   va_start (args, format);
@@ -2120,7 +2121,7 @@ fprintf_unfiltered (GDB_FILE * stream, const char *format,...)
    Called as fprintfi_filtered (spaces, stream, format, ...);  */
 
 void
-fprintfi_filtered (int spaces, GDB_FILE * stream, const char *format,...)
+fprintfi_filtered (int spaces, struct ui_file * stream, const char *format,...)
 {
   va_list args;
   va_start (args, format);
@@ -2210,7 +2211,7 @@ n_spaces (n)
 void
 print_spaces_filtered (n, stream)
      int n;
-     GDB_FILE *stream;
+     struct ui_file *stream;
 {
   fputs_filtered (n_spaces (n), stream);
 }
@@ -2224,7 +2225,7 @@ print_spaces_filtered (n, stream)
 
 void
 fprintf_symbol_filtered (stream, name, lang, arg_mode)
-     GDB_FILE *stream;
+     struct ui_file *stream;
      char *name;
      enum language lang;
      int arg_mode;
@@ -2365,7 +2366,7 @@ initialize_utils ()
   init_page_info ();
 
   /* If the output is not a terminal, don't paginate it.  */
-  if (!GDB_FILE_ISATTY (gdb_stdout))
+  if (!ui_file_isatty (gdb_stdout))
     lines_per_page = UINT_MAX;
 
   set_width_command ((char *) NULL, 0, c);
@@ -2382,6 +2383,7 @@ initialize_utils ()
                  var_boolean, (char *) &pagination_enabled,
                  "Set state of pagination.", &setlist),
      &showlist);
+
   if (xdb_commands)
     {
       add_com ("am", class_support, pagination_on_command,
@@ -2741,7 +2743,8 @@ floatformat_from_doublest (fmt, from, to)
   unsigned char *uto = (unsigned char *) to;
 
   memcpy (&dfrom, from, sizeof (dfrom));
-  memset (uto, 0, fmt->totalsize / FLOATFORMAT_CHAR_BIT);
+  memset (uto, 0, (fmt->totalsize + FLOATFORMAT_CHAR_BIT - 1) 
+                    / FLOATFORMAT_CHAR_BIT);
   if (dfrom == 0)
     return;                    /* Result is zero */
   if (dfrom != dfrom)          /* Result is NaN */
@@ -2790,7 +2793,7 @@ floatformat_from_doublest (fmt, from, to)
       mant_bits = mant_bits_left < 32 ? mant_bits_left : 32;
 
       mant *= 4294967296.0;
-      mant_long = (unsigned long) mant;
+      mant_long = ((unsigned long) mant) & 0xffffffffL;
       mant -= mant_long;
 
       /* If the integer bit is implicit, then we need to discard it.
@@ -2801,6 +2804,7 @@ floatformat_from_doublest (fmt, from, to)
          && fmt->intbit == floatformat_intbit_no)
        {
          mant_long <<= 1;
+         mant_long &= 0xffffffffL;
          mant_bits -= 1;
        }
 
@@ -3023,20 +3027,3 @@ preg_nz (reg)
     }
   return preg_str;
 }
-
-/* Helper functions for INNER_THAN */
-int
-core_addr_lessthan (lhs, rhs)
-     CORE_ADDR lhs;
-     CORE_ADDR rhs;
-{
-  return (lhs < rhs);
-}
-
-int
-core_addr_greaterthan (lhs, rhs)
-     CORE_ADDR lhs;
-     CORE_ADDR rhs;
-{
-  return (lhs > rhs);
-}