This is an archived copy of the Xen.org mailing list, which we have preserved to ensure that existing links to archives are not broken. The live archive, which contains the latest emails, can be found at http://lists.xen.org/
Home Products Support Community News


[Xen-devel] [PATCH 2 of 2] xl: treat sub-command main function like a re

To: xen-devel@xxxxxxxxxxxxxxxxxxx
Subject: [Xen-devel] [PATCH 2 of 2] xl: treat sub-command main function like a regular C main() function
From: Ian Campbell <ian.campbell@xxxxxxxxxx>
Date: Mon, 23 Aug 2010 15:50:14 +0100
Cc: Ian Campbell <ian.campbell@xxxxxxxxxx>
Delivery-date: Mon, 23 Aug 2010 07:53:39 -0700
Envelope-to: www-data@xxxxxxxxxxxxxxxxxxx
In-reply-to: <patchbomb.1282575012@xxxxxxxxxxxxxxxxxxxxx>
List-help: <mailto:xen-devel-request@lists.xensource.com?subject=help>
List-id: Xen developer discussion <xen-devel.lists.xensource.com>
List-post: <mailto:xen-devel@lists.xensource.com>
List-subscribe: <http://lists.xensource.com/mailman/listinfo/xen-devel>, <mailto:xen-devel-request@lists.xensource.com?subject=subscribe>
List-unsubscribe: <http://lists.xensource.com/mailman/listinfo/xen-devel>, <mailto:xen-devel-request@lists.xensource.com?subject=unsubscribe>
Sender: xen-devel-bounces@xxxxxxxxxxxxxxxxxxx
# HG changeset patch
# User Ian Campbell <ian.campbell@xxxxxxxxxx>
# Date 1282574865 -3600
# Node ID c336fbc9175a6b5fec8a5cdf67bbe8354aacd5d0
# Parent  2210bb76868ff58c1c97738f43c52b3e893dd178
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>

diff -r 2210bb76868f -r c336fbc9175a tools/libxl/xl.c
--- a/tools/libxl/xl.c  Mon Aug 23 15:34:14 2010 +0100
+++ b/tools/libxl/xl.c  Mon Aug 23 15:47:45 2010 +0100
@@ -53,7 +53,7 @@ int main(int argc, char **argv)
-    cmd = argv[optind++];
+    cmd = argv[optind];
     if (!cmd) {
@@ -69,13 +69,18 @@ int main(int argc, char **argv)
+    /* Reset options for per-command use of getopt. */
+    argv += optind;
+    argc -= optind;
+    optind = 1;
     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-devel mailing list

<Prev in Thread] Current Thread [Next in Thread>