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