xref: /kvmtool/kvm-cmd.c (revision ca379b8386b64395d139846a4ec141d755d76c24)
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 */
8*ca379b83SPekka Enberg #include "kvm/kvm-debug.h"
9*ca379b83SPekka Enberg #include "kvm/kvm-help.h"
10*ca379b83SPekka Enberg #include "kvm/kvm-cmd.h"
11*ca379b83SPekka Enberg #include "kvm/kvm-run.h"
12f6677a1dSAmerigo Wang 
13f6677a1dSAmerigo Wang struct cmd_struct kvm_commands[] = {
14*ca379b83SPekka Enberg 	{ "debug", kvm_cmd_debug, NULL,         0 },
15f6677a1dSAmerigo Wang 	{ "help",  kvm_cmd_help,  NULL,         0 },
16f6677a1dSAmerigo Wang 	{ "run",   kvm_cmd_run,   kvm_run_help, 0 },
17f6677a1dSAmerigo Wang 	{ NULL,    NULL,          NULL,         0 },
18f6677a1dSAmerigo Wang };
190a936c54SPrasad Joshi 
200ea58e5bSPekka Enberg /*
210ea58e5bSPekka Enberg  * kvm_get_command: Searches the command in an array of the commands and
220ea58e5bSPekka Enberg  * returns a pointer to cmd_struct if a match is found.
230ea58e5bSPekka Enberg  *
240ea58e5bSPekka Enberg  * Input parameters:
250ea58e5bSPekka Enberg  * command: Array of possible commands. The last entry in the array must be
260ea58e5bSPekka Enberg  *          NULL.
270ea58e5bSPekka Enberg  * cmd: A string command to search in the array
280ea58e5bSPekka Enberg  *
290ea58e5bSPekka Enberg  * Return Value:
300ea58e5bSPekka Enberg  * NULL: If the cmd is not matched with any of the command in the command array
310ea58e5bSPekka Enberg  * p: Pointer to cmd_struct of the matching command
320a936c54SPrasad Joshi  */
33f6677a1dSAmerigo Wang struct cmd_struct *kvm_get_command(struct cmd_struct *command,
340a936c54SPrasad Joshi 		const char *cmd)
350a936c54SPrasad Joshi {
360a936c54SPrasad Joshi 	struct cmd_struct *p = command;
370a936c54SPrasad Joshi 
380a936c54SPrasad Joshi 	while (p->cmd) {
390a936c54SPrasad Joshi 		if (!strcmp(p->cmd, cmd))
400a936c54SPrasad Joshi 			return p;
410a936c54SPrasad Joshi 		p++;
420a936c54SPrasad Joshi 	}
430a936c54SPrasad Joshi 	return NULL;
440a936c54SPrasad Joshi }
450a936c54SPrasad Joshi 
460a936c54SPrasad Joshi int handle_command(struct cmd_struct *command, int argc, const char **argv)
470a936c54SPrasad Joshi {
480a936c54SPrasad Joshi 	struct cmd_struct *p;
490a936c54SPrasad Joshi 	const char *prefix = NULL;
500a936c54SPrasad Joshi 
510a936c54SPrasad Joshi 	if (!argv || !*argv) {
520a936c54SPrasad Joshi 		p = kvm_get_command(command, "help");
530a936c54SPrasad Joshi 		assert(p);
540a936c54SPrasad Joshi 		return p->fn(argc, argv, prefix);
550a936c54SPrasad Joshi 	}
560a936c54SPrasad Joshi 
570a936c54SPrasad Joshi 	p = kvm_get_command(command, argv[0]);
580a936c54SPrasad Joshi 	if (!p) {
590a936c54SPrasad Joshi 		p = kvm_get_command(command, "help");
600a936c54SPrasad Joshi 		assert(p);
610a936c54SPrasad Joshi 		p->fn(0, NULL, prefix);
620a936c54SPrasad Joshi 		return EINVAL;
630a936c54SPrasad Joshi 	}
640a936c54SPrasad Joshi 
650a936c54SPrasad Joshi 	return p->fn(argc - 1, &argv[1], prefix);
660a936c54SPrasad Joshi }
67