Automatic date update in version.in
[platform/upstream/binutils.git] / gdb / cli / cli-utils.c
index 2cce068..15f7894 100644 (file)
@@ -1,6 +1,6 @@
 /* CLI utilities.
 
-   Copyright (c) 2011 Free Software Foundation, Inc.
+   Copyright (C) 2011-2014 Free Software Foundation, Inc.
 
    This file is part of GDB.
 
@@ -19,9 +19,7 @@
 
 #include "defs.h"
 #include "cli/cli-utils.h"
-#include "gdb_string.h"
 #include "value.h"
-#include "gdb_assert.h"
 
 #include <ctype.h>
 
    commonly this is `-'.  If you don't want a trailer, use \0.  */
 
 static int
-get_number_trailer (char **pp, int trailer)
+get_number_trailer (const char **pp, int trailer)
 {
   int retval = 0;      /* default */
-  char *p = *pp;
+  const char *p = *pp;
 
   if (*p == '$')
     {
@@ -50,7 +48,7 @@ get_number_trailer (char **pp, int trailer)
            retval = value_as_long (val);
          else
            {
-             printf_filtered (_("History value must have integer type."));
+             printf_filtered (_("History value must have integer type.\n"));
              retval = 0;
            }
        }
@@ -59,7 +57,7 @@ get_number_trailer (char **pp, int trailer)
          /* Internal variable.  Make a copy of the name, so we can
             null-terminate it to pass to lookup_internalvar().  */
          char *varname;
-         char *start = ++p;
+         const char *start = ++p;
          LONGEST val;
 
          while (isalnum (*p) || *p == '_')
@@ -102,7 +100,7 @@ get_number_trailer (char **pp, int trailer)
        ++p;
       retval = 0;
     }
-  p = skip_spaces (p);
+  p = skip_spaces_const (p);
   *pp = p;
   return retval;
 }
@@ -110,16 +108,29 @@ get_number_trailer (char **pp, int trailer)
 /* See documentation in cli-utils.h.  */
 
 int
-get_number (char **pp)
+get_number_const (const char **pp)
 {
   return get_number_trailer (pp, '\0');
 }
 
 /* See documentation in cli-utils.h.  */
 
+int
+get_number (char **pp)
+{
+  int result;
+  const char *p = *pp;
+
+  result = get_number_trailer (&p, '\0');
+  *pp = (char *) p;
+  return result;
+}
+
+/* See documentation in cli-utils.h.  */
+
 void
 init_number_or_range (struct get_number_or_range_state *state,
-                     char *string)
+                     const char *string)
 {
   memset (state, 0, sizeof (*state));
   state->string = string;
@@ -137,15 +148,15 @@ get_number_or_range (struct get_number_or_range_state *state)
       state->last_retval = get_number_trailer (&state->string, '-');
       if (*state->string == '-')
        {
-         char **temp;
+         const char **temp;
 
          /* This is the start of a range (<number1> - <number2>).
             Skip the '-', parse and remember the second number,
             and also remember the end of the final token.  */
 
          temp = &state->end_ptr; 
-         state->end_ptr = skip_spaces (state->string + 1);
-         state->end_value = get_number (temp);
+         state->end_ptr = skip_spaces_const (state->string + 1);
+         state->end_value = get_number_const (temp);
          if (state->end_value < state->last_retval) 
            {
              error (_("inverted range"));
@@ -191,7 +202,7 @@ get_number_or_range (struct get_number_or_range_state *state)
    no arguments.  */
 
 int
-number_is_in_list (char *list, int number)
+number_is_in_list (const char *list, int number)
 {
   struct get_number_or_range_state state;
 
@@ -223,10 +234,22 @@ skip_spaces (char *chp)
   return chp;
 }
 
+/* A const-correct version of the above.  */
+
+const char *
+skip_spaces_const (const char *chp)
+{
+  if (chp == NULL)
+    return NULL;
+  while (*chp && isspace (*chp))
+    chp++;
+  return chp;
+}
+
 /* See documentation in cli-utils.h.  */
 
-char *
-skip_to_space (char *chp)
+const char *
+skip_to_space_const (const char *chp)
 {
   if (chp == NULL)
     return NULL;
@@ -234,3 +257,66 @@ skip_to_space (char *chp)
     chp++;
   return chp;
 }
+
+/* See documentation in cli-utils.h.  */
+
+char *
+remove_trailing_whitespace (const char *start, char *s)
+{
+  while (s > start && isspace (*(s - 1)))
+    --s;
+
+  return s;
+}
+
+/* See documentation in cli-utils.h.  */
+
+char *
+extract_arg_const (const char **arg)
+{
+  const char *result;
+
+  if (!*arg)
+    return NULL;
+
+  /* Find the start of the argument.  */
+  *arg = skip_spaces_const (*arg);
+  if (!**arg)
+    return NULL;
+  result = *arg;
+
+  /* Find the end of the argument.  */
+  *arg = skip_to_space_const (*arg + 1);
+
+  if (result == *arg)
+    return NULL;
+
+  return savestring (result, *arg - result);
+}
+
+/* See documentation in cli-utils.h.  */
+
+char *
+extract_arg (char **arg)
+{
+  const char *arg_const = *arg;
+  char *result;
+
+  result = extract_arg_const (&arg_const);
+  *arg += arg_const - *arg;
+  return result;
+}
+
+/* See documentation in cli-utils.h.  */
+
+int
+check_for_argument (char **str, char *arg, int arg_len)
+{
+  if (strncmp (*str, arg, arg_len) == 0
+      && ((*str)[arg_len] == '\0' || isspace ((*str)[arg_len])))
+    {
+      *str += arg_len;
+      return 1;
+    }
+  return 0;
+}