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

[Xen-devel] [PATCH 2/3]xl: Use command table to store command name and implementation



Use command table to store command name and implementation

Signed-off-by: Yang Hongyang <yanghy@xxxxxxxxxxxxxx>

diff -r d23a0a1823ef -r b695c51b8345 tools/libxl/xl.c
--- a/tools/libxl/xl.c  Thu Apr 29 22:07:08 2010 +0800
+++ b/tools/libxl/xl.c  Thu Apr 29 23:07:31 2010 +0800
@@ -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 @@
 
 int main(int argc, char **argv)
 {
+    int i;
+
     if (argc < 2) {
         help(NULL);
         exit(1);
@@ -59,58 +62,21 @@
 
     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], "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);
+        }
     }
 }
diff -r d23a0a1823ef -r b695c51b8345 tools/libxl/xl_cmdtable.h
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tools/libxl/xl_cmdtable.h Thu Apr 29 23:07:31 2010 +0800
@@ -0,0 +1,45 @@
+/*
+ * Author Yang Hongyang <yanghy@xxxxxxxxxxxxxx>
+ *
+ * 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 },
+};
+
+int cmdtable_len = sizeof(cmd_table)/sizeof(struct cmd_spec);

-- 
Regards
Yang Hongyang

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


 


Rackspace

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