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