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