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