10a936c54SPrasad Joshi #include <stdio.h> 20a936c54SPrasad Joshi #include <string.h> 30a936c54SPrasad Joshi #include <errno.h> 40a936c54SPrasad Joshi 50a936c54SPrasad Joshi #include <assert.h> 60a936c54SPrasad Joshi 70a936c54SPrasad Joshi /* user defined header files */ 80a936c54SPrasad Joshi #include <kvm/kvm-cmd.h> 90a936c54SPrasad Joshi 10*0ea58e5bSPekka Enberg /* 11*0ea58e5bSPekka Enberg * kvm_get_command: Searches the command in an array of the commands and 12*0ea58e5bSPekka Enberg * returns a pointer to cmd_struct if a match is found. 13*0ea58e5bSPekka Enberg * 14*0ea58e5bSPekka Enberg * Input parameters: 15*0ea58e5bSPekka Enberg * command: Array of possible commands. The last entry in the array must be 16*0ea58e5bSPekka Enberg * NULL. 17*0ea58e5bSPekka Enberg * cmd: A string command to search in the array 18*0ea58e5bSPekka Enberg * 19*0ea58e5bSPekka Enberg * Return Value: 20*0ea58e5bSPekka Enberg * NULL: If the cmd is not matched with any of the command in the command array 21*0ea58e5bSPekka Enberg * p: Pointer to cmd_struct of the matching command 220a936c54SPrasad Joshi */ 230a936c54SPrasad Joshi static struct cmd_struct *kvm_get_command(struct cmd_struct *command, 240a936c54SPrasad Joshi const char *cmd) 250a936c54SPrasad Joshi { 260a936c54SPrasad Joshi struct cmd_struct *p = command; 270a936c54SPrasad Joshi 280a936c54SPrasad Joshi while (p->cmd) { 290a936c54SPrasad Joshi if (!strcmp(p->cmd, cmd)) 300a936c54SPrasad Joshi return p; 310a936c54SPrasad Joshi p++; 320a936c54SPrasad Joshi } 330a936c54SPrasad Joshi return NULL; 340a936c54SPrasad Joshi } 350a936c54SPrasad Joshi 360a936c54SPrasad Joshi int handle_command(struct cmd_struct *command, int argc, const char **argv) 370a936c54SPrasad Joshi { 380a936c54SPrasad Joshi struct cmd_struct *p; 390a936c54SPrasad Joshi const char *prefix = NULL; 400a936c54SPrasad Joshi 410a936c54SPrasad Joshi if (!argv || !*argv) { 420a936c54SPrasad Joshi p = kvm_get_command(command, "help"); 430a936c54SPrasad Joshi assert(p); 440a936c54SPrasad Joshi return p->fn(argc, argv, prefix); 450a936c54SPrasad Joshi } 460a936c54SPrasad Joshi 470a936c54SPrasad Joshi p = kvm_get_command(command, argv[0]); 480a936c54SPrasad Joshi if (!p) { 490a936c54SPrasad Joshi p = kvm_get_command(command, "help"); 500a936c54SPrasad Joshi assert(p); 510a936c54SPrasad Joshi p->fn(0, NULL, prefix); 520a936c54SPrasad Joshi return EINVAL; 530a936c54SPrasad Joshi } 540a936c54SPrasad Joshi 550a936c54SPrasad Joshi return p->fn(argc - 1, &argv[1], prefix); 560a936c54SPrasad Joshi } 57