* utils.c (strcmp_iw): Add a hack to allow "FOO(ARGS)" to
[deliverable/binutils-gdb.git] / gdb / utils.c
index 554cb6a1e03ea6d93fabaa6f85301538f7ef5c07..b99e4f942467ea8d4a060491d7056dd48a52ed14 100644 (file)
@@ -18,10 +18,11 @@ along with this program; if not, write to the Free Software
 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
 
 #include "defs.h"
-
+#if !defined(__GO32__)
 #include <sys/ioctl.h>
 #include <sys/param.h>
 #include <pwd.h>
+#endif
 #include <varargs.h>
 #include <ctype.h>
 #include <string.h>
@@ -31,6 +32,7 @@ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
 #include "terminal.h"
 #include "bfd.h"
 #include "target.h"
+#include "demangle.h"
 
 /* Prototypes for local functions */
 
@@ -251,10 +253,10 @@ error (va_alist)
   wrap_here("");                       /* Force out any buffered output */
   fflush (stdout);
   if (error_pre_print)
-    fprintf (stderr, error_pre_print);
+    fprintf_filtered (stderr, error_pre_print);
   string = va_arg (args, char *);
-  vfprintf (stderr, string, args);
-  fprintf (stderr, "\n");
+  vfprintf_filtered (stderr, string, args);
+  fprintf_filtered (stderr, "\n");
   va_end (args);
   return_to_top_level ();
 }
@@ -391,7 +393,7 @@ print_sys_errmsg (string, errcode)
   strcat (combined, ": ");
   strcat (combined, err);
 
-  printf ("%s.\n", combined);
+  fprintf (stderr, "%s.\n", combined);
 }
 
 /* Control C eventually causes this to be called, at a convenient time.  */
@@ -401,6 +403,7 @@ quit ()
 {
   target_terminal_ours ();
   wrap_here ((char *)0);               /* Force out any pending output */
+#if !defined(__GO32__)
 #ifdef HAVE_TERMIO
   ioctl (fileno (stdout), TCFLSH, 1);
 #else /* not HAVE_TERMIO */
@@ -411,6 +414,7 @@ quit ()
 #else
   error ("Quit (expect signal %d when inferior is resumed)", SIGINT);
 #endif /* TIOCGPGRP */
+#endif
 }
 
 /* Control C comes here */
@@ -504,7 +508,7 @@ init_malloc (md)
       warning ("internal error: failed to install memory consistency checks");
     }
 
-  (void) mmtrace ();
+  mmtrace ();
 }
 
 #endif /* Have mmalloc and want corruption checking  */
@@ -630,7 +634,7 @@ savestring (ptr, size)
      int size;
 {
   register char *p = (char *) xmalloc (size + 1);
-  bcopy (ptr, p, size);
+  memcpy (p, ptr, size);
   p[size] = 0;
   return p;
 }
@@ -642,7 +646,7 @@ msavestring (md, ptr, size)
      int size;
 {
   register char *p = (char *) xmmalloc (md, size + 1);
-  bcopy (ptr, p, size);
+  memcpy (p, ptr, size);
   p[size] = 0;
   return p;
 }
@@ -695,11 +699,13 @@ query (va_alist)
 
   while (1)
     {
+      wrap_here ("");          /* Flush any buffered output */
+      fflush (stdout);
       va_start (args);
       ctlstr = va_arg (args, char *);
-      vfprintf (stdout, ctlstr, args);
+      vfprintf_filtered (stdout, ctlstr, args);
       va_end (args);
-      printf ("(y or n) ");
+      printf_filtered ("(y or n) ");
       fflush (stdout);
       answer = fgetc (stdin);
       clearerr (stdin);                /* in case of C-d */
@@ -718,7 +724,7 @@ query (va_alist)
        return 1;
       if (answer == 'N')
        return 0;
-      printf ("Please answer y or n.\n");
+      printf_filtered ("Please answer y or n.\n");
     }
 }
 
@@ -815,7 +821,11 @@ printchar (c, stream, quoter)
      int quoter;
 {
 
-  if (c < 040 || (sevenbit_strings && c >= 0177)) {
+  c &= 0xFF;                   /* Avoid sign bit follies */
+
+  if (              c < 0x20  ||               /* Low control chars */ 
+      (c >= 0x7F && c < 0xA0) ||               /* DEL, High controls */
+      (sevenbit_strings && c >= 0x80)) {       /* high order bit set */
     switch (c)
       {
       case '\n':
@@ -1090,7 +1100,7 @@ fputs_demangled (linebuffer, stream, arg_mode)
   && (isalnum(c) || (c) == '_' || (c) == CPLUS_MARKER))
 
   char buf[SYMBOL_MAX+1];
-# define SLOP 5                /* How much room to leave in buf */
+# define DMSLOP 5              /* How much room to leave in buf */
   char *p;
 
   if (linebuffer == NULL)
@@ -1108,7 +1118,7 @@ fputs_demangled (linebuffer, stream, arg_mode)
     int i = 0;
 
     /* collect non-interesting characters into buf */
-    while ( *p != (char) 0 && !SYMBOL_CHAR(*p) && i < (int)sizeof(buf)-SLOP ) {
+    while (*p != (char) 0 && !SYMBOL_CHAR(*p) && i < (int)sizeof(buf)-DMSLOP ) {
       buf[i++] = *p;
       p++;
     }
@@ -1123,7 +1133,7 @@ fputs_demangled (linebuffer, stream, arg_mode)
     while (i < SYMBOL_MAX
      && *p != (char) 0
      && SYMBOL_CHAR(*p)
-     && i < (int)sizeof(buf) - SLOP) {
+     && i < (int)sizeof(buf) - DMSLOP) {
       buf[i++] = *p;
       p++;
     }
@@ -1197,7 +1207,7 @@ vfprintf_filtered (stream, format, args)
 
   /* This won't blow up if the restrictions described above are
      followed.   */
-  (void) vsprintf (linebuffer, format, args);
+  vsprintf (linebuffer, format, args);
 
   fputs_filtered (linebuffer, stream);
 }
@@ -1207,13 +1217,38 @@ void
 fprintf_filtered (va_alist)
      va_dcl
 {
+  va_list args;
   FILE *stream;
   char *format;
+
+  va_start (args);
+  stream = va_arg (args, FILE *);
+  format = va_arg (args, char *);
+
+  /* This won't blow up if the restrictions described above are
+     followed.   */
+  vfprintf_filtered (stream, format, args);
+  va_end (args);
+}
+
+/* Like fprintf_filtered, but prints it's result indent.
+   Called as fprintfi_filtered (spaces, format, arg1, arg2, ...); */
+
+/* VARARGS */
+void
+fprintfi_filtered (va_alist)
+     va_dcl
+{
   va_list args;
+  int spaces;
+  FILE *stream;
+  char *format;
 
   va_start (args);
+  spaces = va_arg (args, int);
   stream = va_arg (args, FILE *);
   format = va_arg (args, char *);
+  print_spaces_filtered (spaces, stream);
 
   /* This won't blow up if the restrictions described above are
      followed.   */
@@ -1236,6 +1271,26 @@ printf_filtered (va_alist)
   va_end (args);
 }
 
+/* Like printf_filtered, but prints it's result indented.
+   Called as printfi_filtered (spaces, format, arg1, arg2, ...); */
+
+/* VARARGS */
+void
+printfi_filtered (va_alist)
+     va_dcl
+{
+  va_list args;
+  int spaces;
+  char *format;
+
+  va_start (args);
+  spaces = va_arg (args, int);
+  format = va_arg (args, char *);
+  print_spaces_filtered (spaces, stdout);
+  vfprintf_filtered (stdout, format, args);
+  va_end (args);
+}
+
 /* Easy */
 
 void
@@ -1280,6 +1335,24 @@ print_spaces_filtered (n, stream)
 \f
 /* C++ demangler stuff.  */
 
+/* Make a copy of a symbol, applying C++ demangling if demangling is enabled
+   and a demangled version exists.  Note that the value returned from
+   cplus_demangle is already allocated in malloc'd memory. */
+
+char *
+strdup_demangled (name)
+     const char *name;
+{
+  char *demangled = NULL;
+
+  if (demangle)
+    {
+      demangled = cplus_demangle (name, DMGL_PARAMS | DMGL_ANSI);
+    }
+  return ((demangled != NULL) ? demangled : strdup (name));
+}
+
+
 /* Print NAME on STREAM, demangling if necessary.  */
 void
 fprint_symbol (stream, name)
@@ -1287,7 +1360,8 @@ fprint_symbol (stream, name)
      char *name;
 {
   char *demangled;
-  if ((!demangle) || NULL == (demangled = cplus_demangle (name, 1)))
+  if ((!demangle)
+      || NULL == (demangled = cplus_demangle (name, DMGL_PARAMS | DMGL_ANSI)))
     fputs_filtered (name, stream);
   else
     {
@@ -1295,6 +1369,78 @@ fprint_symbol (stream, name)
       free (demangled);
     }
 }
+
+/* Do a strcmp() type operation on STRING1 and STRING2, ignoring any
+   differences in whitespace.  Returns 0 if they match, non-zero if they
+   don't (slightly different than strcmp()'s range of return values).
+   
+   As an extra hack, string1=="FOO(ARGS)" matches string2=="FOO".
+   This "feature" is useful for demangle_and_match(), which is used
+   when searching for matching C++ function names (such as if the
+   user types 'break FOO', where FOO is a mangled C++ function). */
+
+static int
+strcmp_iw (string1, string2)
+     const char *string1;
+     const char *string2;
+{
+  while ((*string1 != '\0') && (*string2 != '\0'))
+    {
+      while (isspace (*string1))
+       {
+         string1++;
+       }
+      while (isspace (*string2))
+       {
+         string2++;
+       }
+      if (*string1 != *string2)
+       {
+         break;
+       }
+      if (*string1 != '\0')
+       {
+         string1++;
+         string2++;
+       }
+    }
+  return (*string1 != '\0' && *string1 != '(') || (*string2 != '\0');
+}
+
+/* Demangle NAME and compare the result with LOOKFOR, ignoring any differences
+   in whitespace.
+   
+   If a match is found, returns a pointer to the demangled version of NAME
+   in malloc'd memory, which needs to be freed by the caller after use.
+   If a match is not found, returns NULL.
+
+   OPTIONS is a flags word that controls the demangling process and is just
+   passed on to the demangler.
+
+   When the caller sees a non-NULL result, it knows that NAME is the mangled
+   equivalent of LOOKFOR, and it can use either NAME, the "official demangled"
+   version of NAME (the return value) or the "unofficial demangled" version
+   of NAME (LOOKFOR, which it already knows). */
+
+char *
+demangle_and_match (name, lookfor, options)
+     const char *name;
+     const char *lookfor;
+     int options;
+{
+  char *demangled;
+
+  if ((demangled = cplus_demangle (name, options)) != NULL)
+    {
+      if (strcmp_iw (demangled, lookfor) != 0)
+       {
+         free (demangled);
+         demangled = NULL;
+       }
+    }
+  return (demangled);
+}
+
 \f
 void
 _initialize_utils ()
@@ -1316,6 +1462,10 @@ _initialize_utils ()
   
   /* These defaults will be used if we are unable to get the correct
      values from termcap.  */
+#if defined(__GO32__)
+  lines_per_page = ScreenRows();
+  chars_per_line = ScreenCols();
+#else  
   lines_per_page = 24;
   chars_per_line = 80;
   /* Initialize the screen height and width from termcap.  */
@@ -1358,7 +1508,7 @@ _initialize_utils ()
   /* If there is a better way to determine the window size, use it. */
   SIGWINCH_HANDLER ();
 #endif
-
+#endif
   /* If the output is not a terminal, don't paginate it.  */
   if (!ISATTY (stdout))
     lines_per_page = UINT_MAX;
This page took 0.02862 seconds and 4 git commands to generate.