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