xref: /kvmtool/builtin-help.c (revision 730cf998b4e91a69863b06e23f101736911c8e2d)
1 #include <stdio.h>
2 #include <string.h>
3 
4 /* user defined headers */
5 #include <common-cmds.h>
6 
7 #include <kvm/util.h>
8 #include <kvm/kvm-cmd.h>
9 #include <kvm/builtin-help.h>
10 
11 
12 const char kvm_usage_string[] =
13 	"kvm COMMAND [ARGS]";
14 
15 const char kvm_more_info_string[] =
16 	"See 'kvm help COMMAND' for more information on a specific command.";
17 
18 
19 static void list_common_cmds_help(void)
20 {
21 	unsigned int i, longest = 0;
22 
23 	for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
24 		if (longest < strlen(common_cmds[i].name))
25 			longest = strlen(common_cmds[i].name);
26 	}
27 
28 	puts(" The most commonly used kvm commands are:");
29 	for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
30 		printf("   %-*s   ", longest, common_cmds[i].name);
31 		puts(common_cmds[i].help);
32 	}
33 }
34 
35 static void kvm_help(void)
36 {
37 	printf("\n usage: %s\n\n", kvm_usage_string);
38 	list_common_cmds_help();
39 	printf("\n %s\n\n", kvm_more_info_string);
40 }
41 
42 
43 static void help_cmd(const char *cmd)
44 {
45 	struct cmd_struct *p;
46 	p = kvm_get_command(kvm_commands, cmd);
47 	if (!p)
48 		kvm_help();
49 	else if (p->help)
50 		p->help();
51 }
52 
53 int kvm_cmd_help(int argc, const char **argv, const char *prefix)
54 {
55 	if (!argv || !*argv) {
56 		kvm_help();
57 		return 0;
58 	}
59 	help_cmd(argv[0]);
60 	return 0;
61 }
62