aboutsummaryrefslogtreecommitdiffstats
path: root/tools/libxl
diff options
context:
space:
mode:
authorKeir Fraser <keir.fraser@citrix.com>2010-06-18 14:09:14 +0100
committerKeir Fraser <keir.fraser@citrix.com>2010-06-18 14:09:14 +0100
commitf20cacf6dc20a60d5512b08aba49b4d6bc1cdc7f (patch)
treecd56ef2721a558ebee781b87a623452f684fa592 /tools/libxl
parent20ad53b196b3a11969e6891f70647788a339605a (diff)
downloadxen-f20cacf6dc20a60d5512b08aba49b4d6bc1cdc7f.tar.gz
xen-f20cacf6dc20a60d5512b08aba49b4d6bc1cdc7f.tar.bz2
xen-f20cacf6dc20a60d5512b08aba49b4d6bc1cdc7f.zip
xl: allow truncation of xl subcommands
for those of us who are used to typing "xm cr foo" Signed-off-by: Tim Deegan <Tim.Deegan@citrix.com>
Diffstat (limited to 'tools/libxl')
-rw-r--r--tools/libxl/xl.c25
-rw-r--r--tools/libxl/xl.h2
-rw-r--r--tools/libxl/xl_cmdimpl.c22
-rw-r--r--tools/libxl/xl_cmdtable.c23
4 files changed, 47 insertions, 25 deletions
diff --git a/tools/libxl/xl.c b/tools/libxl/xl.c
index 226b013513..d00dcefe1d 100644
--- a/tools/libxl/xl.c
+++ b/tools/libxl/xl.c
@@ -37,8 +37,9 @@ static xentoollog_level minmsglevel = XTL_PROGRESS;
int main(int argc, char **argv)
{
- int opt = 0, i;
+ int opt = 0;
char *cmd = 0;
+ struct cmd_spec *cspec;
while ((opt = getopt(argc, argv, "+v")) >= 0) {
switch (opt) {
@@ -69,18 +70,14 @@ int main(int argc, char **argv)
srand(time(0));
- for (i = 0; i < cmdtable_len; i++) {
- if (!strcmp(cmd, cmd_table[i].cmd_name))
- cmd_table[i].cmd_impl(argc, argv);
- }
-
- if (i >= cmdtable_len) {
- if (!strcmp(cmd, "help")) {
- help(argv[2]);
- exit(0);
- } else {
- fprintf(stderr, "command not implemented\n");
- exit(1);
- }
+ cspec = cmdtable_lookup(cmd);
+ if (cspec)
+ return cspec->cmd_impl(argc, argv);
+ else if (!strcmp(cmd, "help")) {
+ help(argv[2]);
+ exit(0);
+ } else {
+ fprintf(stderr, "command not implemented\n");
+ exit(1);
}
}
diff --git a/tools/libxl/xl.h b/tools/libxl/xl.h
index 88561b5a82..df5ce03087 100644
--- a/tools/libxl/xl.h
+++ b/tools/libxl/xl.h
@@ -80,6 +80,8 @@ void help(char *command);
extern struct cmd_spec cmd_table[];
extern int cmdtable_len;
+/* Look up a command in the table, allowing unambiguous truncation */
+struct cmd_spec *cmdtable_lookup(const char *s);
extern struct libxl_ctx ctx;
extern xentoollog_logger_stdiostream *logger;
diff --git a/tools/libxl/xl_cmdimpl.c b/tools/libxl/xl_cmdimpl.c
index d10c599c1d..b0e1341aeb 100644
--- a/tools/libxl/xl_cmdimpl.c
+++ b/tools/libxl/xl_cmdimpl.c
@@ -1282,6 +1282,7 @@ error_out:
void help(char *command)
{
int i;
+ struct cmd_spec *cmd;
if (!command || !strcmp(command, "help")) {
printf("Usage xl <subcommand> [args]\n\n");
@@ -1290,18 +1291,17 @@ void help(char *command)
printf(" %-20s%s\n",
cmd_table[i].cmd_name, cmd_table[i].cmd_desc);
} else {
- for (i = 0; i < cmdtable_len; i++)
- if (!strcmp(command, cmd_table[i].cmd_name))
- break;
- if (i == cmdtable_len) {
- printf("command not implemented\n");
- } else {
+ cmd = cmdtable_lookup(command);
+ if (cmd) {
printf("Usage: xl %s %s\n\n%s.\n\n",
- cmd_table[i].cmd_name,
- cmd_table[i].cmd_usage,
- cmd_table[i].cmd_desc);
- if (cmd_table[i].cmd_option)
- printf("Options:\n\n%s\n", cmd_table[i].cmd_option);
+ cmd->cmd_name,
+ cmd->cmd_usage,
+ cmd->cmd_desc);
+ if (cmd->cmd_option)
+ printf("Options:\n\n%s\n", cmd->cmd_option);
+ }
+ else {
+ printf("command \"%s\" not implemented\n", command);
}
}
}
diff --git a/tools/libxl/xl_cmdtable.c b/tools/libxl/xl_cmdtable.c
index d14993e25b..da00cda663 100644
--- a/tools/libxl/xl_cmdtable.c
+++ b/tools/libxl/xl_cmdtable.c
@@ -12,6 +12,8 @@
* GNU Lesser General Public License for more details.
*/
+#include <string.h>
+
#include "xl.h"
struct cmd_spec cmd_table[] = {
@@ -308,3 +310,24 @@ struct cmd_spec cmd_table[] = {
};
int cmdtable_len = sizeof(cmd_table)/sizeof(struct cmd_spec);
+
+/* Look up a command in the table, allowing unambiguous truncation */
+struct cmd_spec *cmdtable_lookup(const char *s)
+{
+ struct cmd_spec *cmd = NULL;
+ size_t len;
+ int i;
+
+ if (!s)
+ return NULL;
+ len = strlen(s);
+ for (i = 0; i < cmdtable_len; i++) {
+ if (!strncmp(s, cmd_table[i].cmd_name, len)) {
+ if (cmd == NULL)
+ cmd = &cmd_table[i];
+ else
+ return NULL;
+ }
+ }
+ return cmd;
+}