[RFA 2/3] New function cli-utils.c:extract_arg_const

Joel Brobecker brobecker@adacore.com
Mon Nov 11 06:39:00 GMT 2013


Hello,

This function provides the exact same functionality as extract_arg,
except that it takes a "const char**" instead of a "char **". This
will be useful in the context of my next patch.

gdb/ChangeLog:

        * cli/cli-utils.h (extract_arg_const): Add declaration.
        * cli/cli-utils.c (extract_arg_const): New function.

Tested on x86_64-linux. OK to push?

Thank you,
-- 
Joel

---
 gdb/cli/cli-utils.c | 30 ++++++++++++++++++++++++++++++
 gdb/cli/cli-utils.h |  7 +++++++
 2 files changed, 37 insertions(+)

diff --git a/gdb/cli/cli-utils.c b/gdb/cli/cli-utils.c
index f74e6b1..98afe1f 100644
--- a/gdb/cli/cli-utils.c
+++ b/gdb/cli/cli-utils.c
@@ -289,6 +289,36 @@ extract_arg (char **arg)
 
 /* See documentation in cli-utils.h.  */
 
+char *
+extract_arg_const (const char **arg)
+{
+  const char *result;
+  char *copy;
+
+  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;
+
+  copy = xmalloc (*arg - result + 1);
+  memcpy (copy, result, *arg - result);
+  copy[*arg - result] = '\0';
+
+  return copy;
+}
+
+/* See documentation in cli-utils.h.  */
+
 int
 check_for_argument (char **str, char *arg, int arg_len)
 {
diff --git a/gdb/cli/cli-utils.h b/gdb/cli/cli-utils.h
index 152fb89..ebae2d2 100644
--- a/gdb/cli/cli-utils.h
+++ b/gdb/cli/cli-utils.h
@@ -118,6 +118,13 @@ extern char *remove_trailing_whitespace (const char *start, char *s);
 
 extern char *extract_arg (char **arg);
 
+/* A const-correct version of "extract_arg".
+
+   Since the returned value is xmalloc'd, it eventually needs to be
+   xfree'ed, which prevents us from making it const as well.  */
+
+extern char *extract_arg_const (const char **arg);
+
 /* A helper function that looks for an argument at the start of a
    string.  The argument must also either be at the end of the string,
    or be followed by whitespace.  Returns 1 if it finds the argument,
-- 
1.8.1.2



More information about the Gdb-patches mailing list