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