Skip to contentSkip to conversation

linux-user: Option parser cleanup

PATCH In review Meador Inge 2 participants 9 messages Mar 27, 2012 – Jun 8, 2012

lore
Share & tools
lore
#0 lore raw

cover linux-user: Option parser cleanup

This series is focused at cleaning up a few issues in the GNU/Linux usermode driver's option parsing. -h has been fixed to exit with 0, a -help option has been added, proper error messages have been added for unknown options and bad arguments, and --foo is now excepted in addition to -foo.

Meador Inge (4):
linux-user: Exit 0 when -h is used
linux-user: Add -help
linux-user: Add proper error messages for bad options
linux-user: Treat --foo options the same as -foo

 linux-user/main.c |   30 ++++++++++++++++++++----------
 1 files changed, 20 insertions(+), 10 deletions(-)
Signature · 1 line
1.7.7.6
#3 lore raw

PATCH 2/4 linux-user: Add -help

This option is already available on the system mode binaries. It would be better if long options were supported (i.e. --help), but this is okay for now.

---
 linux-user/main.c |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)
linux-user/main.c +2 -0
index aabce83..570178a 100644
@@ -3104,6 +3104,8 @@ struct qemu_argument {
 };
 
 struct qemu_argument arg_table[] = {
+    {"help",       "",                 false, handle_arg_help,
+     "",           ""},
     {"h",          "",                 false, handle_arg_help,
      "",           "print this help"},
     {"g",          "QEMU_GDB",         true,  handle_arg_gdb,
Signature · 1 line
1.7.7.6
#1 lore raw

PATCH 1/4 linux-user: Exit 0 when -h is used

In reply to Meador Inge
---
 linux-user/main.c |   20 ++++++++++----------
 1 files changed, 10 insertions(+), 10 deletions(-)
linux-user/main.c +10 -10
index 962677e..aabce83 100644
@@ -51,7 +51,7 @@ int have_guest_base;
 unsigned long reserved_va;
 #endif
 
-static void usage(void);
+static void usage(int exitcode);
 
 static const char *interp_prefix = CONFIG_QEMU_INTERP_PREFIX;
 const char *qemu_uname_release = CONFIG_UNAME_RELEASE;
@@ -2926,7 +2926,7 @@ void init_task_state(TaskState *ts)
 
 static void handle_arg_help(const char *arg)
 {
-    usage();
+    usage(0);
 }
 
 static void handle_arg_log(const char *arg)
@@ -2956,7 +2956,7 @@ static void handle_arg_set_env(const char *arg)
     r = p = strdup(arg);
     while ((token = strsep(&p, ",")) != NULL) {
         if (envlist_setenv(envlist, token) != 0) {
-            usage();
+            usage(1);
         }
     }
     free(r);
@@ -2968,7 +2968,7 @@ static void handle_arg_unset_env(const char *arg)
     r = p = strdup(arg);
     while ((token = strsep(&p, ",")) != NULL) {
         if (envlist_unsetenv(envlist, token) != 0) {
-            usage();
+            usage(1);
         }
     }
     free(r);
@@ -2984,7 +2984,7 @@ static void handle_arg_stack_size(const char *arg)
     char *p;
     guest_stack_size = strtoul(arg, &p, 0);
     if (guest_stack_size == 0) {
-        usage();
+        usage(1);
     }
 
     if (*p == 'M') {
@@ -3143,7 +3143,7 @@ struct qemu_argument arg_table[] = {
     {NULL, NULL, false, NULL, NULL, NULL}
 };
 
-static void usage(void)
+static void usage(int exitcode)
 {
     struct qemu_argument *arginfo;
     int maxarglen;
@@ -3204,7 +3204,7 @@ static void usage(void)
            "Note that if you provide several changes to a single variable\n"
            "the last change will stay in effect.\n");
 
-    exit(1);
+    exit(exitcode);
 }
 
 static int parse_args(int argc, char **argv)
@@ -3243,7 +3243,7 @@ static int parse_args(int argc, char **argv)
             if (!strcmp(r, arginfo->argv)) {
                 if (arginfo->has_arg) {
                     if (optind >= argc) {
-                        usage();
+                        usage(1);
                     }
                     arginfo->handle_opt(argv[optind]);
                     optind++;
@@ -3256,12 +3256,12 @@ static int parse_args(int argc, char **argv)
 
         /* no option matched the current argv */
         if (arginfo->handle_opt == NULL) {
-            usage();
+            usage(1);
         }
     }
 
     if (optind >= argc) {
-        usage();
+        usage(1);
     }
 
     filename = argv[optind];
Signature · 1 line
1.7.7.6
#4 lore raw

PATCH 3/4 linux-user: Add proper error messages for bad options

In reply to Meador Inge

This patch adds better support for diagnosing option parser errors. The previous implementation just printed the usage text and exited when a bad option or argument was found. This made it very difficult to determine why the usage was being displayed and it was doubly confusing for cases like '--help' (it wasn't clear that --help was actually an error).

---
 linux-user/main.c |   10 +++++++---
 1 files changed, 7 insertions(+), 3 deletions(-)
linux-user/main.c +7 -3
index 570178a..9616d8e 100644
@@ -3245,7 +3245,9 @@ static int parse_args(int argc, char **argv)
             if (!strcmp(r, arginfo->argv)) {
                 if (arginfo->has_arg) {
                     if (optind >= argc) {
-                        usage(1);
+                        (void) fprintf(stderr,
+                            "qemu: missing argument for option '%s'\n", r);
+                        exit(1);
                     }
                     arginfo->handle_opt(argv[optind]);
                     optind++;
@@ -3258,12 +3260,14 @@ static int parse_args(int argc, char **argv)
 
         /* no option matched the current argv */
         if (arginfo->handle_opt == NULL) {
-            usage(1);
+            (void) fprintf(stderr, "qemu: unknown option '%s'\n", r);
+            exit(1);
         }
     }
 
     if (optind >= argc) {
-        usage(1);
+        (void) fprintf(stderr, "qemu: invalid option '%s'\n", r);
+        exit(1);
     }
 
     filename = argv[optind];
Signature · 1 line
1.7.7.6
#5 lore raw

PATCH 4/4 linux-user: Treat --foo options the same as -foo

In reply to Meador Inge

The system mode binaries provide a similiar alias and it makes common options like --version and --help work as expected.

---
 linux-user/main.c |    4 ++++
 1 files changed, 4 insertions(+), 0 deletions(-)
linux-user/main.c +4 -0
index 9616d8e..8f6ca0d 100644
@@ -3240,6 +3240,10 @@ static int parse_args(int argc, char **argv)
         if (!strcmp(r, "-")) {
             break;
         }
+        /* Treat --foo the same as -foo.  */
+        if (r[0] == '-') {
+            r++;
+        }
 
         for (arginfo = arg_table; arginfo->handle_opt != NULL; arginfo++) {
             if (!strcmp(r, arginfo->argv)) {
Signature · 1 line
1.7.7.6
#6 lore raw
In reply to Meador Inge

Ping. Any comments on this series?

Quoted 15 lines · Meador Inge

This series is focused at cleaning up a few issues in the GNU/Linux usermode driver's option parsing. -h has been fixed to exit with 0, a -help option has been added, proper error messages have been added for unknown options and bad arguments, and --foo is now excepted in addition to -foo.

Meador Inge (4):
linux-user: Exit 0 when -h is used
linux-user: Add -help
linux-user: Add proper error messages for bad options
linux-user: Treat --foo options the same as -foo

 linux-user/main.c |   30 ++++++++++++++++++++----------
 1 files changed, 20 insertions(+), 10 deletions(-)
Signature · 3 lines
Meador Inge
CodeSourcery / Mentor Embedded
http://www.mentor.com/embedded-software
#7 lore raw

Ping ^ 2.

Quoted 20 lines · Meador Inge

Ping. Any comments on this series?

Quoted 15 lines · Meador Inge

This series is focused at cleaning up a few issues in the GNU/Linux usermode driver's option parsing. -h has been fixed to exit with 0, a -help option has been added, proper error messages have been added for unknown options and bad arguments, and --foo is now excepted in addition to -foo.

Meador Inge (4):
linux-user: Exit 0 when -h is used
linux-user: Add -help
linux-user: Add proper error messages for bad options
linux-user: Treat --foo options the same as -foo

 linux-user/main.c |   30 ++++++++++++++++++++----------
 1 files changed, 20 insertions(+), 10 deletions(-)
Signature · 3 lines
Meador Inge
CodeSourcery / Mentor Embedded
http://www.mentor.com/embedded-software
Reviewed-by on v1 1/4
#2 lore raw

Re: [Qemu-devel] [PATCH 1/4] linux-user: Exit 0 when -h is used

In reply to Meador Inge

Am 28.03.2012 00:44, schrieb Meador Inge:

Quoted 4 lines
---
 linux-user/main.c |   20 ++++++++++----------
 1 files changed, 10 insertions(+), 10 deletions(-)

/-F

Signature · 2 lines
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Jeff Hawn, Jennifer Guild, Felix Imendörffer; HRB 16746 AG Nürnberg
#8 lore raw
In reply to Meador Inge

Ping ^ 3.

Quoted 25 lines · Meador Inge

Ping ^ 2.

Quoted 20 lines · Meador Inge

Ping. Any comments on this series?

Quoted 15 lines · Meador Inge

This series is focused at cleaning up a few issues in the GNU/Linux usermode driver's option parsing. -h has been fixed to exit with 0, a -help option has been added, proper error messages have been added for unknown options and bad arguments, and --foo is now excepted in addition to -foo.

Meador Inge (4):
linux-user: Exit 0 when -h is used
linux-user: Add -help
linux-user: Add proper error messages for bad options
linux-user: Treat --foo options the same as -foo

 linux-user/main.c |   30 ++++++++++++++++++++----------
 1 files changed, 20 insertions(+), 10 deletions(-)
Signature · 3 lines
Meador Inge
CodeSourcery / Mentor Embedded
http://www.mentor.com/embedded-software