[RFVv4,1/5] New cli-utils.h/.c function extract_info_print_args

Message ID 20181025201107.24828-2-philippe.waroquiers@skynet.be
State New, archived
Headers

Commit Message

Philippe Waroquiers Oct. 25, 2018, 8:11 p.m. UTC
  New cli-utils.h/.c function extract_info_print_args factorizes
the extraction of the args '[-q] [-t TYPEREGEXP] [NAMEREGEXP]'.
New cli-utils.h/.c function report_unrecognized_option_error
factorizes reporting an unknown option for a command.

These functions will be used by the commands
  info [args|functions|locals|variables]

As extract_info_print_args will be used for 'info functions|variables' which
already have the NAMEREGEXP arg, it provides a backward compatible
behaviour.

cli-utils.c has a new static function extract_arg_maybe_quoted
that extracts an argument, possibly quoted.  The behaviour of this
function is similar to the parsing done by gdb_argv.

gdb/ChangeLog
2018-10-25  Philippe Waroquiers  <philippe.waroquiers@skynet.be>

	* cli-utils.c (extract_arg_maybe_quoted): New function.
	(extract_info_print_args): New function.
	(info_print_args_help): New function.
        (report_unrecognized_option_error): New function.
	* cli-utils.h (extract_arg_maybe_quoted): New function.
	(extract_info_print_args): New function.
	(info_print_args_help): New function.
        (report_unrecognized_option_error): New function.
---
 gdb/cli/cli-utils.c | 135 ++++++++++++++++++++++++++++++++++++++++++++
 gdb/cli/cli-utils.h |  30 ++++++++++
 2 files changed, 165 insertions(+)
  

Comments

Pedro Alves Oct. 26, 2018, 6:04 p.m. UTC | #1
Hi Philippe,

This is OK with the below fixed.

On 10/25/2018 09:11 PM, Philippe Waroquiers wrote:

> gdb/ChangeLog
> 2018-10-25  Philippe Waroquiers  <philippe.waroquiers@skynet.be>
> 
> 	* cli-utils.c (extract_arg_maybe_quoted): New function.
> 	(extract_info_print_args): New function.
> 	(info_print_args_help): New function.
>         (report_unrecognized_option_error): New function.
> 	* cli-utils.h (extract_arg_maybe_quoted): New function.
> 	(extract_info_print_args): New function.
> 	(info_print_args_help): New function.
>         (report_unrecognized_option_error): New function.

Noticed some leading whitespaces instead of tabs here.  Make
sure those are fixed when this copied to the ChangeLog file.

> +/* See documentation in cli-utils.h.  */
> +
> +const char *
> +info_print_args_help (const char *prefix,
> +		      const char *entity_kind)
> +{
> +  std::string help = string_printf (_("\
> +%sIf NAMEREGEXP is provided, only prints the %s whose name\n\
> +matches NAMEREGEXP.\n\
> +If -t TYPEREGEXP is provided, only prints the %s 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 %s can be printed.\n\
> +The flag -q disables the production of these headers and messages."),
> +				    prefix,
> +				    entity_kind, entity_kind, entity_kind);
> +
> +  /*  Note : this returns a string allocated with xstrdup, as this
> +      is typically used as argument to add_prefix_cmd, which needs a
> +      string that stays valid after destruction of the std::string.  */
> +  return xstrdup (help.c_str ());

Looks like you missed a previous comment here:

 "Keep the function's interface, but use xstrprintf instead
 of 'xstrdup + string_printf' then."

That meant you can skip the std::string and write this instead:

 const char *
 info_print_args_help (const char *prefix,
 		      const char *entity_kind)
 {
   return xstrprintf (_("\


> +/* Tell the user that ARGS starts with an option unrecognized by COMMAND,
> +   and throws an error.  */
> +

When I first read this comment I thought that the function printed to
gdb_stdout and then threw the error.  I'd suggest this tweak:

/* Throws an error telling the user that ARGS starts with an option
   unrecognized by COMMAND.  */

> +extern void report_unrecognized_option_error (const char *command,
> +					      const char *args);

Thanks,
Pedro Alves
  

Patch

diff --git a/gdb/cli/cli-utils.c b/gdb/cli/cli-utils.c
index 30ee4450f9..30f02ab167 100644
--- a/gdb/cli/cli-utils.c
+++ b/gdb/cli/cli-utils.c
@@ -23,6 +23,8 @@ 
 
 #include <ctype.h>
 
+static std::string extract_arg_maybe_quoted (const char **arg);
+
 /* See documentation in cli-utils.h.  */
 
 int
@@ -128,6 +130,76 @@  get_number (char **pp)
 
 /* See documentation in cli-utils.h.  */
 
+bool
+extract_info_print_args (const char **args,
+			 bool *quiet,
+			 std::string *regexp,
+			 std::string *t_regexp)
+{
+  /* Check for NAMEREGEXP or -- NAMEREGEXP.  */
+  if (**args != '-' || check_for_argument (args, "--", 2))
+    {
+      *args = skip_spaces (*args);
+      *regexp = *args;
+      *args = NULL;
+      return true;
+    }
+
+  if (check_for_argument (args, "-t", 2))
+    {
+      *t_regexp = extract_arg_maybe_quoted (args);
+      *args = skip_spaces (*args);
+      return true;
+    }
+
+  if (check_for_argument (args, "-q", 2))
+    {
+      *quiet = true;
+      *args = skip_spaces (*args);
+      return true;
+    }
+
+  return false;
+}
+
+/* See documentation in cli-utils.h.  */
+
+void
+report_unrecognized_option_error (const char *command, const char *args)
+{
+  std::string option = extract_arg (&args);
+
+  error (_("Unrecognized option '%s' to %s command.  "
+	   "Try \"help %s\"."), option.c_str (),
+	 command, command);
+}
+
+/* See documentation in cli-utils.h.  */
+
+const char *
+info_print_args_help (const char *prefix,
+		      const char *entity_kind)
+{
+  std::string help = string_printf (_("\
+%sIf NAMEREGEXP is provided, only prints the %s whose name\n\
+matches NAMEREGEXP.\n\
+If -t TYPEREGEXP is provided, only prints the %s 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 %s can be printed.\n\
+The flag -q disables the production of these headers and messages."),
+				    prefix,
+				    entity_kind, entity_kind, entity_kind);
+
+  /*  Note : this returns a string allocated with xstrdup, as this
+      is typically used as argument to add_prefix_cmd, which needs a
+      string that stays valid after destruction of the std::string.  */
+  return xstrdup (help.c_str ());
+}
+
+/* See documentation in cli-utils.h.  */
+
 number_or_range_parser::number_or_range_parser (const char *string)
 {
   init (string);
@@ -283,6 +355,69 @@  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.
+   The quoting and special characters are handled similarly to
+   the parsing done by gdb_argv.
+   The return value is empty if no argument was found.  */
+
+static std::string
+extract_arg_maybe_quoted (const char **arg)
+{
+  bool squote = false;
+  bool dquote = false;
+  bool bsquote = false;
+  std::string result;
+  const char *p = *arg;
+
+  /* Find the start of the argument.  */
+  p = skip_spaces (p);
+
+  /* Parse p similarly to gdb_argv buildargv function.  */
+  while (*p != '\0')
+    {
+      if (isspace (*p) && !squote && !dquote && !bsquote)
+	  break;
+      else
+	{
+	  if (bsquote)
+	    {
+	      bsquote = false;
+	      result += *p;
+	    }
+	  else if (*p == '\\')
+	      bsquote = true;
+	  else if (squote)
+	    {
+	      if (*p == '\'')
+		  squote = false;
+	      else
+		  result += *p;
+	    }
+	  else if (dquote)
+	    {
+	      if (*p == '"')
+		  dquote = false;
+	      else
+		  result += *p;
+	    }
+	  else
+	    {
+	      if (*p == '\'')
+		  squote = true;
+	      else if (*p == '"')
+		  dquote = true;
+	      else
+		  result += *p;
+	    }
+	  p++;
+	}
+    }
+
+  *arg = p;
+  return 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..c2f4d04374 100644
--- a/gdb/cli/cli-utils.h
+++ b/gdb/cli/cli-utils.h
@@ -39,6 +39,36 @@  extern int get_number (const char **);
 
 extern int get_number (char **);
 
+/* Extract from ARGS the arguments [-q] [-t TYPEREGEXP] [--] NAMEREGEXP.
+
+   The caller is responsible to initialize *QUIET to false, *REGEXP 
+   and *T_REGEXP to "".
+   extract_info_print_args can then be called iteratively to search
+   for valid arguments, as part of a 'main parsing loop' searching for
+   -q/-t/-- arguments together with other flags and options.
+
+   Returns true and updates *ARGS + one of *QUIET, *REGEXP, *T_REGEXP if
+   it finds a valid argument.
+   Returns false if no valid argument is found at the beginning of ARGS.  */
+
+extern bool extract_info_print_args (const char **args,
+				     bool *quiet,
+				     std::string *regexp,
+				     std::string *t_regexp);
+
+/* Tell the user that ARGS starts with an option unrecognized by COMMAND,
+   and throws an error.  */
+
+extern void report_unrecognized_option_error (const char *command,
+					      const char *args);
+
+
+/* Builds the help string for a command documented by PREFIX,
+   followed by the extract_info_print_args help for ENTITY_KIND.  */
+
+const char *info_print_args_help (const char *prefix,
+				  const char *entity_kind);
+
 /* Parse a number or a range.
    A number will be of the form handled by get_number.
    A range will be of the form <number1> - <number2>, and