xl: Use command table to store command name and implementation
authorKeir Fraser <keir.fraser@citrix.com>
Tue, 4 May 2010 10:28:06 +0000 (11:28 +0100)
committerKeir Fraser <keir.fraser@citrix.com>
Tue, 4 May 2010 10:28:06 +0000 (11:28 +0100)
Signed-off-by: Yang Hongyang <yanghy@cn.fujitsu.com>
tools/libxl/xl.c
tools/libxl/xl_cmdimpl.c
tools/libxl/xl_cmdimpl.h
tools/libxl/xl_cmdtable.h [new file with mode: 0644]

index 1348e3a243b16eedaf2cf00304226bb157a3f0ba..248e5e4babb8450deaaddcbfb3a21f76fe9e3c03 100644 (file)
@@ -29,6 +29,7 @@
 
 #include "libxl.h"
 #include "xl_cmdimpl.h"
+#include "xl_cmdtable.h"
 
 extern struct libxl_ctx ctx;
 extern int logfile;
@@ -43,6 +44,8 @@ void log_callback(void *userdata, int loglevel, const char *file, int line, cons
 
 int main(int argc, char **argv)
 {
+    int i;
+
     if (argc < 2) {
         help(NULL);
         exit(1);
@@ -59,60 +62,21 @@ int main(int argc, char **argv)
 
     srand(time(0));
 
-    if (!strcmp(argv[1], "create")) {
-        main_create(argc - 1, argv + 1);
-    } else if (!strcmp(argv[1], "list")) {
-        main_list(argc - 1, argv + 1);
-    } else if (!strcmp(argv[1], "list-vm")) {
-        main_list_vm(argc - 1, argv + 1);
-    } else if (!strcmp(argv[1], "destroy")) {
-        main_destroy(argc - 1, argv + 1);
-    } else if (!strcmp(argv[1], "pci-attach")) {
-        main_pciattach(argc - 1, argv + 1);
-    } else if (!strcmp(argv[1], "pci-detach")) {
-        main_pcidetach(argc - 1, argv + 1);
-    } else if (!strcmp(argv[1], "pci-list")) {
-        main_pcilist(argc - 1, argv + 1);
-    } else if (!strcmp(argv[1], "pause")) {
-        main_pause(argc - 1, argv + 1);
-    } else if (!strcmp(argv[1], "unpause")) {
-        main_unpause(argc - 1, argv + 1);
-    } else if (!strcmp(argv[1], "console")) {
-        main_console(argc - 1, argv + 1);
-    } else if (!strcmp(argv[1], "save")) {
-        main_save(argc - 1, argv + 1);
-    } else if (!strcmp(argv[1], "migrate")) {
-        main_migrate(argc - 1, argv + 1);
-    } else if (!strcmp(argv[1], "restore")) {
-        main_restore(argc - 1, argv + 1);
-    } else if (!strcmp(argv[1], "migrate-receive")) {
-        main_migrate_receive(argc - 1, argv + 1);
-    } else if (!strcmp(argv[1], "cd-insert")) {
-        main_cd_insert(argc - 1, argv + 1);
-    } else if (!strcmp(argv[1], "cd-eject")) {
-        main_cd_eject(argc - 1, argv + 1);
-    } else if (!strcmp(argv[1], "mem-set")) {
-        main_memset(argc - 1, argv + 1);
-    } else if (!strcmp(argv[1], "button-press")) {
-        main_button_press(argc - 1, argv + 1);
-    } else if (!strcmp(argv[1], "vcpu-list")) {
-        main_vcpulist(argc - 1, argv + 1);
-    } else if (!strcmp(argv[1], "vcpu-pin")) {
-        main_vcpupin(argc - 1, argv + 1);
-    } else if (!strcmp(argv[1], "vcpu-set")) {
-        main_vcpuset(argc - 1, argv + 1);
-    } else if (!strcmp(argv[1], "info")) {
-        main_info(argc - 1, argv + 1);
-    } else if (!strcmp(argv[1], "sched-credit")) {
-        main_sched_credit(argc - 1, argv + 1);
-    } else if (!strcmp(argv[1], "help")) {
-        if (argc > 2)
-            help(argv[2]);
-        else
-            help(NULL);
-        exit(0);
-    } else {
-        fprintf(stderr, "command not implemented\n");
-        exit(1);
+    for (i = 0; i < cmdtable_len; i++) {
+        if (!strcmp(argv[1], cmd_table[i].cmd_name))
+               cmd_table[i].cmd_impl(argc - 1, argv + 1);
+    }
+
+    if (i >= cmdtable_len) {
+        if (!strcmp(argv[1], "help")) {
+            if (argc > 2)
+                help(argv[2]);
+            else
+                help(NULL);
+            exit(0);
+        } else {
+            fprintf(stderr, "command not implemented\n");
+            exit(1);
+        }
     }
 }
index de1d6a229e50ffab91915a856b931363d8696b69..2fd0c604df8e4ef3d466a3740f72e8406ec455f6 100644 (file)
@@ -2790,7 +2790,7 @@ static void sched_credit_domain_output(
         scinfo->cap);
 }
 
-void main_sched_credit(int argc, char **argv)
+int main_sched_credit(int argc, char **argv)
 {
     struct libxl_dominfo *info;
     struct libxl_sched_credit scinfo;
index 79ec10f1dea546611759b41727452c5d9921ff44..b809d653976610adf9898c18c9e81aa2190771a5 100644 (file)
@@ -34,6 +34,6 @@ int main_button_press(int argc, char **argv);
 int main_vcpupin(int argc, char **argv);
 int main_vcpuset(int argc, char **argv);
 int main_memset(int argc, char **argv);
-void main_sched_credit(int argc, char **argv);
+int main_sched_credit(int argc, char **argv);
 
 void help(char *command);
diff --git a/tools/libxl/xl_cmdtable.h b/tools/libxl/xl_cmdtable.h
new file mode 100644 (file)
index 0000000..76a5437
--- /dev/null
@@ -0,0 +1,46 @@
+/*
+ * Author Yang Hongyang <yanghy@cn.fujitsu.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; version 2.1 only. with the special
+ * exception on linking described in file LICENSE.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ */
+
+#include "xl_cmdimpl.h"
+
+struct cmd_spec {
+    char *cmd_name;
+    int (*cmd_impl)(int argc, char **argv);
+};
+
+struct cmd_spec cmd_table[] = {
+    { "create", &main_create },
+    { "list", &main_list },
+    { "destroy", &main_destroy },
+    { "pci-attach", &main_pciattach },
+    { "pci-detach", &main_pcidetach },
+    { "pci-list", &main_pcilist },
+    { "pause", &main_pause },
+    { "unpause", &main_unpause },
+    { "console", &main_console },
+    { "save", &main_save },
+    { "restore", &main_restore },
+    { "cd-insert", &main_cd_insert },
+    { "cd-eject", &main_cd_eject },
+    { "mem-set", &main_memset },
+    { "button-press", &main_button_press },
+    { "vcpu-list", &main_vcpulist },
+    { "vcpu-pin", &main_vcpupin },
+    { "vcpu-set", &main_vcpuset },
+    { "list-vm", &main_list_vm },
+    { "info", &main_info },
+    { "sched-credit", &main_sched_credit },
+};
+
+int cmdtable_len = sizeof(cmd_table)/sizeof(struct cmd_spec);