This is the mail archive of the
gdb-patches@sourceware.org
mailing list for the GDB project.
[RFA 2/3] New function cli-utils.c:extract_arg_const
- From: Joel Brobecker <brobecker at adacore dot com>
- To: gdb-patches at sourceware dot org
- Date: Mon, 11 Nov 2013 10:37:34 +0400
- Subject: [RFA 2/3] New function cli-utils.c:extract_arg_const
- Authentication-results: sourceware.org; auth=none
- References: <1384151855-12926-1-git-send-email-brobecker at adacore dot com>
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