From patchwork Sun Aug 26 16:53:54 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philippe Waroquiers X-Patchwork-Id: 29054 Received: (qmail 129464 invoked by alias); 26 Aug 2018 16:54:15 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 129355 invoked by uid 89); 26 Aug 2018 16:54:14 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-26.4 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_LOW, SPF_PASS autolearn=ham version=3.3.2 spammy=sk:number_, disables, sk:extract X-HELO: mailsec113.isp.belgacom.be Received: from mailsec113.isp.belgacom.be (HELO mailsec113.isp.belgacom.be) (195.238.20.109) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Sun, 26 Aug 2018 16:54:13 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=skynet.be; i=@skynet.be; q=dns/txt; s=securemail; t=1535302453; x=1566838453; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=Kc7THJm4B1LwSEQGoZZHJTjcDHfg8Ljveel96X0yqJo=; b=Dge3irAbVcjkxCR/RRearcV1v0/lt3CAjOf/ut+/i+k3kmm9q133LoF6 s9S05oA2A0C9HIWe4ss3h4kCiKxqbQ==; Received: from 217.24-133-109.adsl-dyn.isp.belgacom.be (HELO md.home) ([109.133.24.217]) by relay.skynet.be with ESMTP/TLS/DHE-RSA-AES128-GCM-SHA256; 26 Aug 2018 18:54:11 +0200 From: Philippe Waroquiers To: gdb-patches@sourceware.org Cc: Philippe Waroquiers Subject: [RFAv2 1/6] New cli-utils.h/.c function extract_info_print_args Date: Sun, 26 Aug 2018 18:53:54 +0200 Message-Id: <20180826165359.1600-2-philippe.waroquiers@skynet.be> In-Reply-To: <20180826165359.1600-1-philippe.waroquiers@skynet.be> References: <20180826165359.1600-1-philippe.waroquiers@skynet.be> X-IsSubscribed: yes New cli-utils.h/.c function extract_info_print_args factorises the extraction of the args '[-q] [-t TYPEREGEXP] [NAMEREGEXP]'. This function will be used by the commands info [args|functions|locals|variables] cli-utils.c has a new static function extract_arg_maybe_quoted that extracts an argument, possibly single quoted. gdb/ChangeLog 2018-08-26 Philippe Waroquiers * cli-utils.c (extract_arg_maybe_quoted): New function. (extract_info_print_args): New function. * cli-utils.h (extract_arg_maybe_quoted): New function. (extract_info_print_args): New function. (INFO_PRINT_ARGS_HELP): new macro. --- gdb/cli/cli-utils.c | 113 ++++++++++++++++++++++++++++++++++++++++++++ gdb/cli/cli-utils.h | 20 ++++++++ 2 files changed, 133 insertions(+) diff --git a/gdb/cli/cli-utils.c b/gdb/cli/cli-utils.c index 98b7414991..7adbab46a1 100644 --- a/gdb/cli/cli-utils.c +++ b/gdb/cli/cli-utils.c @@ -23,6 +23,9 @@ #include +static std::string +extract_arg_maybe_quoted (const char **arg); + /* See documentation in cli-utils.h. */ int @@ -125,6 +128,56 @@ get_number (char **pp) return result; } +/* See documentation in cli-utils.h. */ + +void +extract_info_print_args (const char* command, + const char **args, + bool *quiet, + std::string *regexp, + std::string *t_regexp) +{ + *quiet = false; + *regexp = ""; + *t_regexp = ""; + + while (*args != NULL) + { + if (check_for_argument (args, "--", 2)) + { + *args = skip_spaces (*args); + break; + } + + if (check_for_argument (args, "-t", 2)) + { + *t_regexp = extract_arg_maybe_quoted (args); + *args = skip_spaces (*args); + continue; + } + + if (check_for_argument (args, "-q", 2)) + { + *quiet = true; + *args = skip_spaces (*args); + continue; + } + + if (**args != '-') + break; + + std::string option = extract_arg (args); + error (_("Unrecognized option '%s' to %s command. " + "Try \"help %s\"."), option.c_str (), + command, command); + } + + if (*args != NULL && **args != '\000') + *regexp = extract_arg (args); + +} + + /* See documentation in cli-utils.h. */ number_or_range_parser::number_or_range_parser (const char *string) @@ -282,6 +335,66 @@ remove_trailing_whitespace (const char *start, const char *s) return s; } +/* A helper function to extract an argument from *ARG. An argument is + delimited by whitespace, but it can also be optionally quoted using + single quote characters. The return value is empty if no argument + was found. */ + +static std::string +extract_arg_maybe_quoted (const char **arg) +{ + if (!*arg) + return std::string (); + + /* Find the start of the argument. */ + *arg = skip_spaces (*arg); + if (!**arg) + return std::string (); + + if (**arg == '\'') + { + /* Quoted argument. */ + const char *orig_arg = *arg; + std::string result; + const char *p; + + (*arg)++; /* Skip starting quote. */ + + /* Find the end of the quoted argument. */ + for (p = *arg; *p != '\0'; p++) + { + if (*p == '\'') + { + (*arg)++; /* Skip ending quote. */ + return result; + } + + if (*p == '\\' && p[1] == '\'') + { + /* Escaped quote. */ + p++; /* Skip escape char. */ + (*arg)++; /* Skip escape char. */ + } + + result = result + *p; + (*arg)++; + } + error (_("Unterminated quoted argument in %s"), orig_arg); + } + else + { + const char *result = *arg; + + /* Find the end of the argument. */ + *arg = skip_to_space (*arg + 1); + + if (result == *arg) + return std::string (); + + return std::string (result, *arg - result); + } +} + /* See documentation in cli-utils.h. */ std::string diff --git a/gdb/cli/cli-utils.h b/gdb/cli/cli-utils.h index fa7d02d719..602a36314d 100644 --- a/gdb/cli/cli-utils.h +++ b/gdb/cli/cli-utils.h @@ -39,6 +39,26 @@ extern int get_number (const char **); extern int get_number (char **); +/* Extract from args the arguments [-q] [-t TYPEREGEXP] [--] NAMEREGEXP. + Only the above flags are accepted. Any other flag will raise an error. + COMMAND is used to produce the error message if an invalid flag is + given. */ +extern void extract_info_print_args (const char* command, + const char **args, + bool *quiet, + std::string *regexp, + std::string *t_regexp); + +#define INFO_PRINT_ARGS_HELP(entity_kind) \ +"If NAMEREGEXP is provided, only prints the " entity_kind " whose name\n\ +matches NAMEREGEXP.\n\ +If -t TYPEREGEXP is provided, only prints the " entity_kind " whose type\n\ +matches TYPEREGEXP. Note that the matching is done with the type\n\ +printed by the 'whatis' command.\n\ +By default, the command might produce headers and/or messages indicating\n\ +why no " entity_kind " can be printed.\n\ +The flag -q disables the production of these headers and messages." + /* Parse a number or a range. A number will be of the form handled by get_number. A range will be of the form - , and