xl: allow truncation of xl subcommands
PATCH 1/1 xl: allow truncation of xl subcommands
xl: allow truncation of xl subcommands
for those of us who are used to typing "xm cr foo"
Signed-off-by Tim Deegan <[email protected]>
tools/libxl/xl.c +11 -14
@@ -37,8 +37,9 @@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 @@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 ); } }
tools/libxl/xl.h +2 -0
@@ -80,6 +80,8 @@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;
tools/libxl/xl_cmdimpl.c +11 -11
@@ -1282,6 +1282,7 @@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 @@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); } } }
tools/libxl/xl_cmdtable.c +23 -0
@@ -11,6 +11,8 @@* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. */ + +#include <string.h> #include "xl.h"
@@ -308,3 +310,24 @@ };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; +}
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel
PATCH 1/1 xl: fix command truncation
Fix the truncation code so that it always accepts an exact match,
even when one command is a prefix of another one. This fixes,
e.g., "xl list"
Signed-off-by Tim Deegan <[email protected]>
tools/libxl/xl_cmdtable.c +7 -6
@@ -316,18 +316,19 @@ {struct cmd_spec *cmd =NULL ;size_t len; -int i; +int i, count =0 ;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 ; + cmd = &cmd_table[i]; +/* Take an exact match, even if it also prefixes another command */ +if (len ==strlen (cmd->cmd_name)) +return cmd; + count++; } } -return cmd; +return (count ==1 ) ? cmd :NULL ; }
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel