[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Xen-changelog] [xen-unstable] xl: treat sub-command main function like a regular C main() function



# HG changeset patch
# User Ian Campbell <ian.campbell@xxxxxxxxxx>
# Date 1282671007 -3600
# Node ID 1deee01bbfa411f2dfc7ede677b05e8e77b83b93
# Parent  aaeeed47c52b78f48dc1ad6ed1b314f64f5d9c32
xl: treat sub-command main function like a regular C main() function

Currently xl passes the entire argc+argv to each subcommand and relies
on the preservation of the global optind variable to ensure that the
subcommand correctly handles argument parsing (e.g. accounting for "xl
[command]" vs "xl -v [command]").

This requirement for individual sub-commands to parse arguments
relative to optind is subtle and prone to being forgotten (or simply
not expected). Several sub-commands have recently been broken in this
way (now fixed).

Therefore arrange that the argv+argc passed to the sub-commands looks
like you would expect for a regular C main function and includes
argv[0] equal to the command name with command specific arguments in
argv[1] onwards.

Since all sub-commands (currently) correctly obey the optind it is
sufficient to reset it to 1 (as described in getopt(3)) in order to
not break the sub-commands' own argument parsing.

Signed-off-by: Ian Campbell <ian.campbell@xxxxxxxxxx>
Signed-off-by: Ian Jackson <ian.jackson@xxxxxxxxxxxxx>
---
 tools/libxl/xl.c |    9 +++++++--
 1 files changed, 7 insertions(+), 2 deletions(-)

diff -r aaeeed47c52b -r 1deee01bbfa4 tools/libxl/xl.c
--- a/tools/libxl/xl.c  Tue Aug 24 18:29:21 2010 +0100
+++ b/tools/libxl/xl.c  Tue Aug 24 18:30:07 2010 +0100
@@ -53,7 +53,7 @@ int main(int argc, char **argv)
         }
     }
 
-    cmd = argv[optind++];
+    cmd = argv[optind];
 
     if (!cmd) {
         help(NULL);
@@ -69,13 +69,18 @@ int main(int argc, char **argv)
         exit(1);
     }
 
+    /* Reset options for per-command use of getopt. */
+    argv += optind;
+    argc -= optind;
+    optind = 1;
+
     srand(time(0));
 
     cspec = cmdtable_lookup(cmd);
     if (cspec)
         ret = cspec->cmd_impl(argc, argv);
     else if (!strcmp(cmd, "help")) {
-        help(argv[optind]);
+        help(argv[1]);
         ret = 0;
     } else {
         fprintf(stderr, "command not implemented\n");

_______________________________________________
Xen-changelog mailing list
Xen-changelog@xxxxxxxxxxxxxxxxxxx
http://lists.xensource.com/xen-changelog


 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.