All of lore.kernel.org
 help / color / mirror / Atom feed
From: Yang Hongyang <yanghy@cn.fujitsu.com>
To: xen-devel@lists.xensource.com
Subject: [PATCH 2/3]xl: Use command table to store command name and implementation
Date: Thu, 29 Apr 2010 17:23:56 +0800	[thread overview]
Message-ID: <4BD9502C.4020409@cn.fujitsu.com> (raw)

Use command table to store command name and implementation

Signed-off-by: Yang Hongyang <yanghy@cn.fujitsu.com>

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@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 },
+};
+
+int cmdtable_len = sizeof(cmd_table)/sizeof(struct cmd_spec);

-- 
Regards
Yang Hongyang

                 reply	other threads:[~2010-04-29  9:23 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4BD9502C.4020409@cn.fujitsu.com \
    --to=yanghy@cn.fujitsu.com \
    --cc=xen-devel@lists.xensource.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.