1 #include <stdio.h> 2 #include <string.h> 3 #include <errno.h> 4 5 #include <assert.h> 6 7 /* user defined header files */ 8 #include "kvm/builtin-debug.h" 9 #include "kvm/builtin-pause.h" 10 #include "kvm/builtin-resume.h" 11 #include "kvm/builtin-balloon.h" 12 #include "kvm/builtin-list.h" 13 #include "kvm/builtin-version.h" 14 #include "kvm/builtin-setup.h" 15 #include "kvm/builtin-stop.h" 16 #include "kvm/builtin-stat.h" 17 #include "kvm/builtin-help.h" 18 #include "kvm/kvm-cmd.h" 19 #include "kvm/builtin-run.h" 20 #include "kvm/util.h" 21 22 struct cmd_struct kvm_commands[] = { 23 { "pause", kvm_cmd_pause, kvm_pause_help, 0 }, 24 { "resume", kvm_cmd_resume, kvm_resume_help, 0 }, 25 { "debug", kvm_cmd_debug, kvm_debug_help, 0 }, 26 { "balloon", kvm_cmd_balloon, kvm_balloon_help, 0 }, 27 { "list", kvm_cmd_list, kvm_list_help, 0 }, 28 { "version", kvm_cmd_version, NULL, 0 }, 29 { "--version", kvm_cmd_version, NULL, 0 }, 30 { "stop", kvm_cmd_stop, kvm_stop_help, 0 }, 31 { "stat", kvm_cmd_stat, kvm_stat_help, 0 }, 32 { "help", kvm_cmd_help, NULL, 0 }, 33 { "setup", kvm_cmd_setup, kvm_setup_help, 0 }, 34 { "run", kvm_cmd_run, kvm_run_help, 0 }, 35 { NULL, NULL, NULL, 0 }, 36 }; 37 38 /* 39 * kvm_get_command: Searches the command in an array of the commands and 40 * returns a pointer to cmd_struct if a match is found. 41 * 42 * Input parameters: 43 * command: Array of possible commands. The last entry in the array must be 44 * NULL. 45 * cmd: A string command to search in the array 46 * 47 * Return Value: 48 * NULL: If the cmd is not matched with any of the command in the command array 49 * p: Pointer to cmd_struct of the matching command 50 */ 51 struct cmd_struct *kvm_get_command(struct cmd_struct *command, 52 const char *cmd) 53 { 54 struct cmd_struct *p = command; 55 56 while (p->cmd) { 57 if (!strcmp(p->cmd, cmd)) 58 return p; 59 p++; 60 } 61 return NULL; 62 } 63 64 int handle_command(struct cmd_struct *command, int argc, const char **argv) 65 { 66 struct cmd_struct *p; 67 const char *prefix = NULL; 68 int ret = 0; 69 70 if (!argv || !*argv) { 71 p = kvm_get_command(command, "help"); 72 assert(p); 73 return p->fn(argc, argv, prefix); 74 } 75 76 p = kvm_get_command(command, argv[0]); 77 if (!p) { 78 p = kvm_get_command(command, "help"); 79 assert(p); 80 p->fn(0, NULL, prefix); 81 return EINVAL; 82 } 83 84 ret = p->fn(argc - 1, &argv[1], prefix); 85 if (ret < 0) { 86 if (errno == EPERM) 87 die("Permission error - are you root?"); 88 } 89 90 return ret; 91 } 92