xref: /kvmtool/kvm-cmd.c (revision 2a24f96d648d5a281ae506ca6125404d1edbe7f5)
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*2a24f96dSSasha Levin #include "kvm/builtin-debug.h"
9*2a24f96dSSasha Levin #include "kvm/builtin-pause.h"
10*2a24f96dSSasha Levin #include "kvm/builtin-balloon.h"
11*2a24f96dSSasha Levin #include "kvm/builtin-list.h"
12*2a24f96dSSasha Levin #include "kvm/builtin-version.h"
13*2a24f96dSSasha Levin #include "kvm/builtin-help.h"
14ca379b83SPekka Enberg #include "kvm/kvm-cmd.h"
15*2a24f96dSSasha Levin #include "kvm/builtin-run.h"
16f6677a1dSAmerigo Wang 
17f6677a1dSAmerigo Wang struct cmd_struct kvm_commands[] = {
182947270eSSasha Levin 	{ "pause",	kvm_cmd_pause,		NULL,         0 },
19ca379b83SPekka Enberg 	{ "debug",	kvm_cmd_debug,		NULL,         0 },
205cac5d9cSSasha Levin 	{ "balloon",	kvm_cmd_balloon,	NULL,         0 },
219dba6721SSasha Levin 	{ "list",	kvm_cmd_list,		NULL,         0 },
2216b2bb87SSasha Levin 	{ "version",	kvm_cmd_version,	NULL,         0 },
23f6677a1dSAmerigo Wang 	{ "help",	kvm_cmd_help,		NULL,         0 },
24f6677a1dSAmerigo Wang 	{ "run",	kvm_cmd_run,		kvm_run_help, 0 },
25f6677a1dSAmerigo Wang 	{ NULL,		NULL,			NULL,         0 },
26f6677a1dSAmerigo Wang };
270a936c54SPrasad Joshi 
280ea58e5bSPekka Enberg /*
290ea58e5bSPekka Enberg  * kvm_get_command: Searches the command in an array of the commands and
300ea58e5bSPekka Enberg  * returns a pointer to cmd_struct if a match is found.
310ea58e5bSPekka Enberg  *
320ea58e5bSPekka Enberg  * Input parameters:
330ea58e5bSPekka Enberg  * command: Array of possible commands. The last entry in the array must be
340ea58e5bSPekka Enberg  *          NULL.
350ea58e5bSPekka Enberg  * cmd: A string command to search in the array
360ea58e5bSPekka Enberg  *
370ea58e5bSPekka Enberg  * Return Value:
380ea58e5bSPekka Enberg  * NULL: If the cmd is not matched with any of the command in the command array
390ea58e5bSPekka Enberg  * p: Pointer to cmd_struct of the matching command
400a936c54SPrasad Joshi  */
41f6677a1dSAmerigo Wang struct cmd_struct *kvm_get_command(struct cmd_struct *command,
420a936c54SPrasad Joshi 		const char *cmd)
430a936c54SPrasad Joshi {
440a936c54SPrasad Joshi 	struct cmd_struct *p = command;
450a936c54SPrasad Joshi 
460a936c54SPrasad Joshi 	while (p->cmd) {
470a936c54SPrasad Joshi 		if (!strcmp(p->cmd, cmd))
480a936c54SPrasad Joshi 			return p;
490a936c54SPrasad Joshi 		p++;
500a936c54SPrasad Joshi 	}
510a936c54SPrasad Joshi 	return NULL;
520a936c54SPrasad Joshi }
530a936c54SPrasad Joshi 
540a936c54SPrasad Joshi int handle_command(struct cmd_struct *command, int argc, const char **argv)
550a936c54SPrasad Joshi {
560a936c54SPrasad Joshi 	struct cmd_struct *p;
570a936c54SPrasad Joshi 	const char *prefix = NULL;
580a936c54SPrasad Joshi 
590a936c54SPrasad Joshi 	if (!argv || !*argv) {
600a936c54SPrasad Joshi 		p = kvm_get_command(command, "help");
610a936c54SPrasad Joshi 		assert(p);
620a936c54SPrasad Joshi 		return p->fn(argc, argv, prefix);
630a936c54SPrasad Joshi 	}
640a936c54SPrasad Joshi 
650a936c54SPrasad Joshi 	p = kvm_get_command(command, argv[0]);
660a936c54SPrasad Joshi 	if (!p) {
670a936c54SPrasad Joshi 		p = kvm_get_command(command, "help");
680a936c54SPrasad Joshi 		assert(p);
690a936c54SPrasad Joshi 		p->fn(0, NULL, prefix);
700a936c54SPrasad Joshi 		return EINVAL;
710a936c54SPrasad Joshi 	}
720a936c54SPrasad Joshi 
730a936c54SPrasad Joshi 	return p->fn(argc - 1, &argv[1], prefix);
740a936c54SPrasad Joshi }
75