debuggers.hg
changeset 21294:35da124fc66a
xl: Use command table to store command name and implementation
Signed-off-by: Yang Hongyang <yanghy@cn.fujitsu.com>
Signed-off-by: Yang Hongyang <yanghy@cn.fujitsu.com>
author | Keir Fraser <keir.fraser@citrix.com> |
---|---|
date | Tue May 04 11:28:06 2010 +0100 (2010-05-04) |
parents | 742311878ae5 |
children | ef961ddf8b33 |
files | tools/libxl/xl.c tools/libxl/xl_cmdimpl.c tools/libxl/xl_cmdimpl.h tools/libxl/xl_cmdtable.h |
line diff
1.1 --- a/tools/libxl/xl.c Tue May 04 11:23:17 2010 +0100 1.2 +++ b/tools/libxl/xl.c Tue May 04 11:28:06 2010 +0100 1.3 @@ -29,6 +29,7 @@ 1.4 1.5 #include "libxl.h" 1.6 #include "xl_cmdimpl.h" 1.7 +#include "xl_cmdtable.h" 1.8 1.9 extern struct libxl_ctx ctx; 1.10 extern int logfile; 1.11 @@ -43,6 +44,8 @@ void log_callback(void *userdata, int lo 1.12 1.13 int main(int argc, char **argv) 1.14 { 1.15 + int i; 1.16 + 1.17 if (argc < 2) { 1.18 help(NULL); 1.19 exit(1); 1.20 @@ -59,60 +62,21 @@ int main(int argc, char **argv) 1.21 1.22 srand(time(0)); 1.23 1.24 - if (!strcmp(argv[1], "create")) { 1.25 - main_create(argc - 1, argv + 1); 1.26 - } else if (!strcmp(argv[1], "list")) { 1.27 - main_list(argc - 1, argv + 1); 1.28 - } else if (!strcmp(argv[1], "list-vm")) { 1.29 - main_list_vm(argc - 1, argv + 1); 1.30 - } else if (!strcmp(argv[1], "destroy")) { 1.31 - main_destroy(argc - 1, argv + 1); 1.32 - } else if (!strcmp(argv[1], "pci-attach")) { 1.33 - main_pciattach(argc - 1, argv + 1); 1.34 - } else if (!strcmp(argv[1], "pci-detach")) { 1.35 - main_pcidetach(argc - 1, argv + 1); 1.36 - } else if (!strcmp(argv[1], "pci-list")) { 1.37 - main_pcilist(argc - 1, argv + 1); 1.38 - } else if (!strcmp(argv[1], "pause")) { 1.39 - main_pause(argc - 1, argv + 1); 1.40 - } else if (!strcmp(argv[1], "unpause")) { 1.41 - main_unpause(argc - 1, argv + 1); 1.42 - } else if (!strcmp(argv[1], "console")) { 1.43 - main_console(argc - 1, argv + 1); 1.44 - } else if (!strcmp(argv[1], "save")) { 1.45 - main_save(argc - 1, argv + 1); 1.46 - } else if (!strcmp(argv[1], "migrate")) { 1.47 - main_migrate(argc - 1, argv + 1); 1.48 - } else if (!strcmp(argv[1], "restore")) { 1.49 - main_restore(argc - 1, argv + 1); 1.50 - } else if (!strcmp(argv[1], "migrate-receive")) { 1.51 - main_migrate_receive(argc - 1, argv + 1); 1.52 - } else if (!strcmp(argv[1], "cd-insert")) { 1.53 - main_cd_insert(argc - 1, argv + 1); 1.54 - } else if (!strcmp(argv[1], "cd-eject")) { 1.55 - main_cd_eject(argc - 1, argv + 1); 1.56 - } else if (!strcmp(argv[1], "mem-set")) { 1.57 - main_memset(argc - 1, argv + 1); 1.58 - } else if (!strcmp(argv[1], "button-press")) { 1.59 - main_button_press(argc - 1, argv + 1); 1.60 - } else if (!strcmp(argv[1], "vcpu-list")) { 1.61 - main_vcpulist(argc - 1, argv + 1); 1.62 - } else if (!strcmp(argv[1], "vcpu-pin")) { 1.63 - main_vcpupin(argc - 1, argv + 1); 1.64 - } else if (!strcmp(argv[1], "vcpu-set")) { 1.65 - main_vcpuset(argc - 1, argv + 1); 1.66 - } else if (!strcmp(argv[1], "info")) { 1.67 - main_info(argc - 1, argv + 1); 1.68 - } else if (!strcmp(argv[1], "sched-credit")) { 1.69 - main_sched_credit(argc - 1, argv + 1); 1.70 - } else if (!strcmp(argv[1], "help")) { 1.71 - if (argc > 2) 1.72 - help(argv[2]); 1.73 - else 1.74 - help(NULL); 1.75 - exit(0); 1.76 - } else { 1.77 - fprintf(stderr, "command not implemented\n"); 1.78 - exit(1); 1.79 + for (i = 0; i < cmdtable_len; i++) { 1.80 + if (!strcmp(argv[1], cmd_table[i].cmd_name)) 1.81 + cmd_table[i].cmd_impl(argc - 1, argv + 1); 1.82 + } 1.83 + 1.84 + if (i >= cmdtable_len) { 1.85 + if (!strcmp(argv[1], "help")) { 1.86 + if (argc > 2) 1.87 + help(argv[2]); 1.88 + else 1.89 + help(NULL); 1.90 + exit(0); 1.91 + } else { 1.92 + fprintf(stderr, "command not implemented\n"); 1.93 + exit(1); 1.94 + } 1.95 } 1.96 }
2.1 --- a/tools/libxl/xl_cmdimpl.c Tue May 04 11:23:17 2010 +0100 2.2 +++ b/tools/libxl/xl_cmdimpl.c Tue May 04 11:28:06 2010 +0100 2.3 @@ -2790,7 +2790,7 @@ static void sched_credit_domain_output( 2.4 scinfo->cap); 2.5 } 2.6 2.7 -void main_sched_credit(int argc, char **argv) 2.8 +int main_sched_credit(int argc, char **argv) 2.9 { 2.10 struct libxl_dominfo *info; 2.11 struct libxl_sched_credit scinfo;
3.1 --- a/tools/libxl/xl_cmdimpl.h Tue May 04 11:23:17 2010 +0100 3.2 +++ b/tools/libxl/xl_cmdimpl.h Tue May 04 11:28:06 2010 +0100 3.3 @@ -34,6 +34,6 @@ int main_button_press(int argc, char **a 3.4 int main_vcpupin(int argc, char **argv); 3.5 int main_vcpuset(int argc, char **argv); 3.6 int main_memset(int argc, char **argv); 3.7 -void main_sched_credit(int argc, char **argv); 3.8 +int main_sched_credit(int argc, char **argv); 3.9 3.10 void help(char *command);
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.2 +++ b/tools/libxl/xl_cmdtable.h Tue May 04 11:28:06 2010 +0100 4.3 @@ -0,0 +1,46 @@ 4.4 +/* 4.5 + * Author Yang Hongyang <yanghy@cn.fujitsu.com> 4.6 + * 4.7 + * This program is free software; you can redistribute it and/or modify 4.8 + * it under the terms of the GNU Lesser General Public License as published 4.9 + * by the Free Software Foundation; version 2.1 only. with the special 4.10 + * exception on linking described in file LICENSE. 4.11 + * 4.12 + * This program is distributed in the hope that it will be useful, 4.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of 4.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 4.15 + * GNU Lesser General Public License for more details. 4.16 + */ 4.17 + 4.18 +#include "xl_cmdimpl.h" 4.19 + 4.20 +struct cmd_spec { 4.21 + char *cmd_name; 4.22 + int (*cmd_impl)(int argc, char **argv); 4.23 +}; 4.24 + 4.25 +struct cmd_spec cmd_table[] = { 4.26 + { "create", &main_create }, 4.27 + { "list", &main_list }, 4.28 + { "destroy", &main_destroy }, 4.29 + { "pci-attach", &main_pciattach }, 4.30 + { "pci-detach", &main_pcidetach }, 4.31 + { "pci-list", &main_pcilist }, 4.32 + { "pause", &main_pause }, 4.33 + { "unpause", &main_unpause }, 4.34 + { "console", &main_console }, 4.35 + { "save", &main_save }, 4.36 + { "restore", &main_restore }, 4.37 + { "cd-insert", &main_cd_insert }, 4.38 + { "cd-eject", &main_cd_eject }, 4.39 + { "mem-set", &main_memset }, 4.40 + { "button-press", &main_button_press }, 4.41 + { "vcpu-list", &main_vcpulist }, 4.42 + { "vcpu-pin", &main_vcpupin }, 4.43 + { "vcpu-set", &main_vcpuset }, 4.44 + { "list-vm", &main_list_vm }, 4.45 + { "info", &main_info }, 4.46 + { "sched-credit", &main_sched_credit }, 4.47 +}; 4.48 + 4.49 +int cmdtable_len = sizeof(cmd_table)/sizeof(struct cmd_spec);