Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Yao Qi <yao@codesourcery.com>
To: Pedro Alves <palves@redhat.com>
Cc: <gdb-patches@sourceware.org>
Subject: Re: [PATCH 1/2] Use mi_getopt_silent
Date: Tue, 27 Aug 2013 03:30:00 -0000	[thread overview]
Message-ID: <521C1CF9.3060907@codesourcery.com> (raw)
In-Reply-To: <521B80B5.1020108@redhat.com>

On 08/27/2013 12:22 AM, Pedro Alves wrote:
> Here's what I suggest:
> 
>   	* mi/mi-getopt.c (mi_getopt): Rename to ...
>   	(mi_getopt_1): ... this.  Add parameter 'error_on_unknown'.
>   	(mi_getopt): Reimplement as wrapper around mi_getopt_1.
>   	(mi_getopt_silent): New function.
> 

OK.

>> >	* mi/mi-getopt.h (mi_getopt_silent): Declare.
> 
> 
>> >--- a/gdb/mi/mi-getopt.c
>> >+++ b/gdb/mi/mi-getopt.c
>> >@@ -21,11 +21,14 @@
>> >  #include "mi-getopt.h"
>> >  #include "gdb_string.h"
>> >  
>> >-int
>> >-mi_getopt (const char *prefix,
>> >-	   int argc, char **argv,
>> >-	   const struct mi_opt *opts,
>> >-	   int *oind, char **oarg)
>> >+/* See comments about mi_getopt and mi_getopt_silent in mi-getopt.h.
>> >+   When there is an unknown option, if ERROR_ON_UNKNOWN is true, it
>> >+   throws an error, otherwise return -1.  */
> ("throws" -> "returns").  I'd suggest:
> 

I assume you meant "return" -> "returns" here.

>     If an unknown option is encountered, if ERROR_ON_UNKNOWN is true,
>     throw an error, otherwise return -1.  */
> 

Anyway, copy this into the patch.  I also rename mi_getopt_silent to
mi_getopt_allow_unknown.  Patch below is what I committed.

-- 
Yao (齐尧)

gdb:

2013-08-27  Yao Qi  <yao@codesourcery.com>

	* mi/mi-cmd-stack.c (parse_no_frames_option): Remove.
	(mi_cmd_stack_list_args): Use mi_getopt_allow_unknown to
	handle options.
	* mi/mi-getopt.c (mi_getopt): Rename to ...
	(mi_getopt_1): ... this.  Add parameter 'error_on_unknown'.
	(mi_getopt): Reimplement as wrapper around mi_getopt_1.
	(mi_getopt_allow_unknown): New function.
	* mi/mi-getopt.h (mi_getopt_allow_unknown): Declare.
---
 gdb/mi/mi-cmd-stack.c |   50 ++++++++++++++++++++++++++++++------------------
 gdb/mi/mi-getopt.c    |   35 ++++++++++++++++++++++++++++-----
 gdb/mi/mi-getopt.h    |    8 +++++-
 3 files changed, 66 insertions(+), 27 deletions(-)

diff --git a/gdb/mi/mi-cmd-stack.c b/gdb/mi/mi-cmd-stack.c
index e542fc1..2df97e2 100644
--- a/gdb/mi/mi-cmd-stack.c
+++ b/gdb/mi/mi-cmd-stack.c
@@ -54,17 +54,6 @@ mi_cmd_enable_frame_filters (char *command, char **argv, int argc)
   frame_filters = 1;
 }
 
-/* Parse the --no-frame-filters option in commands where we cannot use
-   mi_getopt. */
-static int
-parse_no_frames_option (const char *arg)
-{
-  if (arg && (strcmp (arg, "--no-frame-filters") == 0))
-    return 1;
-
-  return 0;
-}
-
 /* Print a list of the stack frames.  Args can be none, in which case
    we want to print the whole backtrace, or a pair of numbers
    specifying the frame numbers at which to start and stop the
@@ -284,19 +273,42 @@ mi_cmd_stack_list_args (char *command, char **argv, int argc)
   enum print_values print_values;
   struct ui_out *uiout = current_uiout;
   int raw_arg = 0;
+  int oind = 0;
   enum py_bt_status result = PY_BT_ERROR;
+  enum opt
+  {
+    NO_FRAME_FILTERS,
+  };
+  static const struct mi_opt opts[] =
+    {
+      {"-no-frame-filters", NO_FRAME_FILTERS, 0},
+      { 0, 0, 0 }
+    };
 
-  if (argc > 0)
-    raw_arg = parse_no_frames_option (argv[0]);
+  while (1)
+    {
+      char *oarg;
+      int opt = mi_getopt_allow_unknown ("-stack-list-args", argc, argv,
+					 opts, &oind, &oarg);
+
+      if (opt < 0)
+	break;
+      switch ((enum opt) opt)
+	{
+	case NO_FRAME_FILTERS:
+	  raw_arg = oind;
+	  break;
+	}
+    }
 
-  if (argc < 1 || (argc > 3 && ! raw_arg) || (argc == 2 && ! raw_arg))
-    error (_("-stack-list-arguments: Usage: " \
+  if (argc - oind != 1 && argc - oind != 3)
+    error (_("-stack-list-arguments: Usage: "	\
 	     "[--no-frame-filters] PRINT_VALUES [FRAME_LOW FRAME_HIGH]"));
 
-  if (argc >= 3)
+  if (argc - oind == 3)
     {
-      frame_low = atoi (argv[1 + raw_arg]);
-      frame_high = atoi (argv[2 + raw_arg]);
+      frame_low = atoi (argv[1 + oind]);
+      frame_high = atoi (argv[2 + oind]);
     }
   else
     {
@@ -306,7 +318,7 @@ mi_cmd_stack_list_args (char *command, char **argv, int argc)
       frame_high = -1;
     }
 
-  print_values = mi_parse_print_values (argv[raw_arg]);
+  print_values = mi_parse_print_values (argv[oind]);
 
   /* Let's position fi on the frame at which to start the
      display. Could be the innermost frame if the whole stack needs
diff --git a/gdb/mi/mi-getopt.c b/gdb/mi/mi-getopt.c
index a1e2ccc..f207249 100644
--- a/gdb/mi/mi-getopt.c
+++ b/gdb/mi/mi-getopt.c
@@ -21,11 +21,14 @@
 #include "mi-getopt.h"
 #include "gdb_string.h"
 
-int
-mi_getopt (const char *prefix,
-	   int argc, char **argv,
-	   const struct mi_opt *opts,
-	   int *oind, char **oarg)
+/* See comments about mi_getopt and mi_getopt_silent in mi-getopt.h.
+   When there is an unknown option, if ERROR_ON_UNKNOWN is true,
+   throw an error, otherwise return -1.  */
+
+static int
+mi_getopt_1 (const char *prefix, int argc, char **argv,
+	     const struct mi_opt *opts, int *oind, char **oarg,
+	     int error_on_unknown)
 {
   char *arg;
   const struct mi_opt *opt;
@@ -71,7 +74,27 @@ mi_getopt (const char *prefix,
 	  return opt->index;
 	}
     }
-  error (_("%s: Unknown option ``%s''"), prefix, arg + 1);
+
+  if (error_on_unknown)
+    error (_("%s: Unknown option ``%s''"), prefix, arg + 1);
+  else
+    return -1;
+}
+
+int
+mi_getopt (const char *prefix,
+	   int argc, char **argv,
+	   const struct mi_opt *opts,
+	   int *oind, char **oarg)
+{
+  return mi_getopt_1 (prefix, argc, argv, opts, oind, oarg, 1);
+}
+
+int
+mi_getopt_allow_unknown (const char *prefix, int argc, char **argv,
+			 const struct mi_opt *opts, int *oind, char **oarg)
+{
+  return mi_getopt_1 (prefix, argc, argv, opts, oind, oarg, 0);
 }
 
 int 
diff --git a/gdb/mi/mi-getopt.h b/gdb/mi/mi-getopt.h
index 9600353..7db0bf4 100644
--- a/gdb/mi/mi-getopt.h
+++ b/gdb/mi/mi-getopt.h
@@ -46,11 +46,15 @@ struct mi_opt
    If ARGV[OPTIND] is not an option, -1 is returned and OPTIND updated
    to specify the non-option argument.  OPTARG is set to NULL.
 
-   mi_getopt() calls ``error("%s: Unknown option %c", prefix,
-   option)'' if an unknown option is encountered.  */
+   If an unknown option is encountered, mi_getopt() calls
+   ``error("%s: Unknown option %c", prefix, option)'' while
+   mi_getopt_allow_unknown returns -1.  */
 
 extern int mi_getopt (const char *prefix, int argc, char **argv,
 		      const struct mi_opt *opt, int *optind, char **optarg);
+extern int mi_getopt_allow_unknown (const char *prefix, int argc,
+				    char **argv, const struct mi_opt *opts,
+				    int *oind, char **oarg);
 
 /* mi_valid_noargs determines if ARGC/ARGV are a valid set of
    parameters to satisfy an MI function that is not supposed to
-- 
1.7.7.6


  reply	other threads:[~2013-08-27  3:30 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-07-03  1:48 [PATCH 0/2] New option --skip-unavailable to -stack-list-XXX commands Yao Qi
2013-07-03  1:48 ` [PATCH 2/2] Add options to skip unavailable locals Yao Qi
2013-07-03  4:14   ` asmwarrior
2013-07-03  5:21     ` Yao Qi
2013-07-03 19:22   ` Eli Zaretskii
2013-07-22  1:38     ` Yao Qi
2013-08-01 12:33       ` Pedro Alves
2013-08-01 13:59         ` Yao Qi
2013-08-01 14:47           ` Pedro Alves
2013-08-25  3:43         ` [PATCH 0/2 v2] " Yao Qi
2013-08-25  3:43           ` [PATCH 2/2] " Yao Qi
2013-08-26 16:40             ` Pedro Alves
2013-08-27  5:22               ` Yao Qi
2013-08-25  3:43           ` [PATCH 1/2] Use mi_getopt_silent Yao Qi
2013-08-26  8:58             ` Agovic, Sanimir
2013-08-26 10:18               ` Yao Qi
2013-08-26 16:01                 ` Pedro Alves
2013-08-26 16:22             ` Pedro Alves
2013-08-27  3:30               ` Yao Qi [this message]
2013-08-27 11:43                 ` Pedro Alves
2013-07-03  1:48 ` [PATCH 1/2] Use mi_getopt in mi_cmd_stack_list_locals and mi_cmd_stack_list_variables Yao Qi
2013-07-29 18:17   ` Pedro Alves
2013-07-31  7:01     ` Yao Qi
2013-07-31 12:23       ` Pedro Alves
2013-08-01  6:45         ` Yao Qi
2013-07-29  9:33 ` [ping]: [PATCH 0/2] New option --skip-unavailable to -stack-list-XXX commands Yao Qi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=521C1CF9.3060907@codesourcery.com \
    --to=yao@codesourcery.com \
    --cc=gdb-patches@sourceware.org \
    --cc=palves@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox