xref: /openbmc/linux/tools/perf/builtin-help.c (revision 670ab5d21c7e168c89a36fdd2c69fb7af63d35a1)
186470930SIngo Molnar /*
286470930SIngo Molnar  * builtin-help.c
386470930SIngo Molnar  *
486470930SIngo Molnar  * Builtin help command
586470930SIngo Molnar  */
6f37a291cSIngo Molnar #include "perf.h"
786470930SIngo Molnar #include "util/cache.h"
886470930SIngo Molnar #include "builtin.h"
986470930SIngo Molnar #include "util/exec_cmd.h"
1086470930SIngo Molnar #include "common-cmds.h"
1186470930SIngo Molnar #include "util/parse-options.h"
1286470930SIngo Molnar #include "util/run-command.h"
1386470930SIngo Molnar #include "util/help.h"
1486470930SIngo Molnar 
1586470930SIngo Molnar static struct man_viewer_list {
1686470930SIngo Molnar 	struct man_viewer_list *next;
1786470930SIngo Molnar 	char name[FLEX_ARRAY];
1886470930SIngo Molnar } *man_viewer_list;
1986470930SIngo Molnar 
2086470930SIngo Molnar static struct man_viewer_info_list {
2186470930SIngo Molnar 	struct man_viewer_info_list *next;
2286470930SIngo Molnar 	const char *info;
2386470930SIngo Molnar 	char name[FLEX_ARRAY];
2486470930SIngo Molnar } *man_viewer_info_list;
2586470930SIngo Molnar 
2686470930SIngo Molnar enum help_format {
27cc584821SDavid Ahern 	HELP_FORMAT_NONE,
2886470930SIngo Molnar 	HELP_FORMAT_MAN,
2986470930SIngo Molnar 	HELP_FORMAT_INFO,
3086470930SIngo Molnar 	HELP_FORMAT_WEB,
3186470930SIngo Molnar };
3286470930SIngo Molnar 
3386470930SIngo Molnar static enum help_format parse_help_format(const char *format)
3486470930SIngo Molnar {
3586470930SIngo Molnar 	if (!strcmp(format, "man"))
3686470930SIngo Molnar 		return HELP_FORMAT_MAN;
3786470930SIngo Molnar 	if (!strcmp(format, "info"))
3886470930SIngo Molnar 		return HELP_FORMAT_INFO;
3986470930SIngo Molnar 	if (!strcmp(format, "web") || !strcmp(format, "html"))
4086470930SIngo Molnar 		return HELP_FORMAT_WEB;
41cc584821SDavid Ahern 
42cc584821SDavid Ahern 	pr_err("unrecognized help format '%s'", format);
43cc584821SDavid Ahern 	return HELP_FORMAT_NONE;
4486470930SIngo Molnar }
4586470930SIngo Molnar 
4686470930SIngo Molnar static const char *get_man_viewer_info(const char *name)
4786470930SIngo Molnar {
4886470930SIngo Molnar 	struct man_viewer_info_list *viewer;
4986470930SIngo Molnar 
50c1e53017SThiago Farina 	for (viewer = man_viewer_info_list; viewer; viewer = viewer->next) {
5186470930SIngo Molnar 		if (!strcasecmp(name, viewer->name))
5286470930SIngo Molnar 			return viewer->info;
5386470930SIngo Molnar 	}
5486470930SIngo Molnar 	return NULL;
5586470930SIngo Molnar }
5686470930SIngo Molnar 
5786470930SIngo Molnar static int check_emacsclient_version(void)
5886470930SIngo Molnar {
5986470930SIngo Molnar 	struct strbuf buffer = STRBUF_INIT;
6086470930SIngo Molnar 	struct child_process ec_process;
6186470930SIngo Molnar 	const char *argv_ec[] = { "emacsclient", "--version", NULL };
6286470930SIngo Molnar 	int version;
6386470930SIngo Molnar 
6486470930SIngo Molnar 	/* emacsclient prints its version number on stderr */
6586470930SIngo Molnar 	memset(&ec_process, 0, sizeof(ec_process));
6686470930SIngo Molnar 	ec_process.argv = argv_ec;
6786470930SIngo Molnar 	ec_process.err = -1;
6886470930SIngo Molnar 	ec_process.stdout_to_stderr = 1;
6986470930SIngo Molnar 	if (start_command(&ec_process)) {
7086470930SIngo Molnar 		fprintf(stderr, "Failed to start emacsclient.\n");
7186470930SIngo Molnar 		return -1;
7286470930SIngo Molnar 	}
7386470930SIngo Molnar 	strbuf_read(&buffer, ec_process.err, 20);
7486470930SIngo Molnar 	close(ec_process.err);
7586470930SIngo Molnar 
7686470930SIngo Molnar 	/*
7786470930SIngo Molnar 	 * Don't bother checking return value, because "emacsclient --version"
7886470930SIngo Molnar 	 * seems to always exits with code 1.
7986470930SIngo Molnar 	 */
8086470930SIngo Molnar 	finish_command(&ec_process);
8186470930SIngo Molnar 
8286470930SIngo Molnar 	if (prefixcmp(buffer.buf, "emacsclient")) {
8386470930SIngo Molnar 		fprintf(stderr, "Failed to parse emacsclient version.\n");
8486470930SIngo Molnar 		strbuf_release(&buffer);
8586470930SIngo Molnar 		return -1;
8686470930SIngo Molnar 	}
8786470930SIngo Molnar 
8886470930SIngo Molnar 	strbuf_remove(&buffer, 0, strlen("emacsclient"));
8986470930SIngo Molnar 	version = atoi(buffer.buf);
9086470930SIngo Molnar 
9186470930SIngo Molnar 	if (version < 22) {
9286470930SIngo Molnar 		fprintf(stderr,
9386470930SIngo Molnar 			"emacsclient version '%d' too old (< 22).\n",
9486470930SIngo Molnar 			version);
9586470930SIngo Molnar 		strbuf_release(&buffer);
9686470930SIngo Molnar 		return -1;
9786470930SIngo Molnar 	}
9886470930SIngo Molnar 
9986470930SIngo Molnar 	strbuf_release(&buffer);
10086470930SIngo Molnar 	return 0;
10186470930SIngo Molnar }
10286470930SIngo Molnar 
10386470930SIngo Molnar static void exec_woman_emacs(const char *path, const char *page)
10486470930SIngo Molnar {
10586470930SIngo Molnar 	if (!check_emacsclient_version()) {
10686470930SIngo Molnar 		/* This works only with emacsclient version >= 22. */
10786470930SIngo Molnar 		struct strbuf man_page = STRBUF_INIT;
10886470930SIngo Molnar 
10986470930SIngo Molnar 		if (!path)
11086470930SIngo Molnar 			path = "emacsclient";
11186470930SIngo Molnar 		strbuf_addf(&man_page, "(woman \"%s\")", page);
11286470930SIngo Molnar 		execlp(path, "emacsclient", "-e", man_page.buf, NULL);
11386470930SIngo Molnar 		warning("failed to exec '%s': %s", path, strerror(errno));
11486470930SIngo Molnar 	}
11586470930SIngo Molnar }
11686470930SIngo Molnar 
11786470930SIngo Molnar static void exec_man_konqueror(const char *path, const char *page)
11886470930SIngo Molnar {
11986470930SIngo Molnar 	const char *display = getenv("DISPLAY");
12086470930SIngo Molnar 	if (display && *display) {
12186470930SIngo Molnar 		struct strbuf man_page = STRBUF_INIT;
12286470930SIngo Molnar 		const char *filename = "kfmclient";
12386470930SIngo Molnar 
12486470930SIngo Molnar 		/* It's simpler to launch konqueror using kfmclient. */
12586470930SIngo Molnar 		if (path) {
12686470930SIngo Molnar 			const char *file = strrchr(path, '/');
12786470930SIngo Molnar 			if (file && !strcmp(file + 1, "konqueror")) {
12886470930SIngo Molnar 				char *new = strdup(path);
12986470930SIngo Molnar 				char *dest = strrchr(new, '/');
13086470930SIngo Molnar 
13186470930SIngo Molnar 				/* strlen("konqueror") == strlen("kfmclient") */
13286470930SIngo Molnar 				strcpy(dest + 1, "kfmclient");
13386470930SIngo Molnar 				path = new;
13486470930SIngo Molnar 			}
13586470930SIngo Molnar 			if (file)
13686470930SIngo Molnar 				filename = file;
13786470930SIngo Molnar 		} else
13886470930SIngo Molnar 			path = "kfmclient";
13986470930SIngo Molnar 		strbuf_addf(&man_page, "man:%s(1)", page);
14086470930SIngo Molnar 		execlp(path, filename, "newTab", man_page.buf, NULL);
14186470930SIngo Molnar 		warning("failed to exec '%s': %s", path, strerror(errno));
14286470930SIngo Molnar 	}
14386470930SIngo Molnar }
14486470930SIngo Molnar 
14586470930SIngo Molnar static void exec_man_man(const char *path, const char *page)
14686470930SIngo Molnar {
14786470930SIngo Molnar 	if (!path)
14886470930SIngo Molnar 		path = "man";
14986470930SIngo Molnar 	execlp(path, "man", page, NULL);
15086470930SIngo Molnar 	warning("failed to exec '%s': %s", path, strerror(errno));
15186470930SIngo Molnar }
15286470930SIngo Molnar 
15386470930SIngo Molnar static void exec_man_cmd(const char *cmd, const char *page)
15486470930SIngo Molnar {
15586470930SIngo Molnar 	struct strbuf shell_cmd = STRBUF_INIT;
15686470930SIngo Molnar 	strbuf_addf(&shell_cmd, "%s %s", cmd, page);
15786470930SIngo Molnar 	execl("/bin/sh", "sh", "-c", shell_cmd.buf, NULL);
15886470930SIngo Molnar 	warning("failed to exec '%s': %s", cmd, strerror(errno));
15986470930SIngo Molnar }
16086470930SIngo Molnar 
16186470930SIngo Molnar static void add_man_viewer(const char *name)
16286470930SIngo Molnar {
16386470930SIngo Molnar 	struct man_viewer_list **p = &man_viewer_list;
16486470930SIngo Molnar 	size_t len = strlen(name);
16586470930SIngo Molnar 
16686470930SIngo Molnar 	while (*p)
16786470930SIngo Molnar 		p = &((*p)->next);
16836479484SArnaldo Carvalho de Melo 	*p = zalloc(sizeof(**p) + len + 1);
16986470930SIngo Molnar 	strncpy((*p)->name, name, len);
17086470930SIngo Molnar }
17186470930SIngo Molnar 
17286470930SIngo Molnar static int supported_man_viewer(const char *name, size_t len)
17386470930SIngo Molnar {
17486470930SIngo Molnar 	return (!strncasecmp("man", name, len) ||
17586470930SIngo Molnar 		!strncasecmp("woman", name, len) ||
17686470930SIngo Molnar 		!strncasecmp("konqueror", name, len));
17786470930SIngo Molnar }
17886470930SIngo Molnar 
17986470930SIngo Molnar static void do_add_man_viewer_info(const char *name,
18086470930SIngo Molnar 				   size_t len,
18186470930SIngo Molnar 				   const char *value)
18286470930SIngo Molnar {
18336479484SArnaldo Carvalho de Melo 	struct man_viewer_info_list *new = zalloc(sizeof(*new) + len + 1);
18486470930SIngo Molnar 
18586470930SIngo Molnar 	strncpy(new->name, name, len);
18686470930SIngo Molnar 	new->info = strdup(value);
18786470930SIngo Molnar 	new->next = man_viewer_info_list;
18886470930SIngo Molnar 	man_viewer_info_list = new;
18986470930SIngo Molnar }
19086470930SIngo Molnar 
19186470930SIngo Molnar static int add_man_viewer_path(const char *name,
19286470930SIngo Molnar 			       size_t len,
19386470930SIngo Molnar 			       const char *value)
19486470930SIngo Molnar {
19586470930SIngo Molnar 	if (supported_man_viewer(name, len))
19686470930SIngo Molnar 		do_add_man_viewer_info(name, len, value);
19786470930SIngo Molnar 	else
19886470930SIngo Molnar 		warning("'%s': path for unsupported man viewer.\n"
19986470930SIngo Molnar 			"Please consider using 'man.<tool>.cmd' instead.",
20086470930SIngo Molnar 			name);
20186470930SIngo Molnar 
20286470930SIngo Molnar 	return 0;
20386470930SIngo Molnar }
20486470930SIngo Molnar 
20586470930SIngo Molnar static int add_man_viewer_cmd(const char *name,
20686470930SIngo Molnar 			      size_t len,
20786470930SIngo Molnar 			      const char *value)
20886470930SIngo Molnar {
20986470930SIngo Molnar 	if (supported_man_viewer(name, len))
21086470930SIngo Molnar 		warning("'%s': cmd for supported man viewer.\n"
21186470930SIngo Molnar 			"Please consider using 'man.<tool>.path' instead.",
21286470930SIngo Molnar 			name);
21386470930SIngo Molnar 	else
21486470930SIngo Molnar 		do_add_man_viewer_info(name, len, value);
21586470930SIngo Molnar 
21686470930SIngo Molnar 	return 0;
21786470930SIngo Molnar }
21886470930SIngo Molnar 
21986470930SIngo Molnar static int add_man_viewer_info(const char *var, const char *value)
22086470930SIngo Molnar {
22186470930SIngo Molnar 	const char *name = var + 4;
22286470930SIngo Molnar 	const char *subkey = strrchr(name, '.');
22386470930SIngo Molnar 
22486470930SIngo Molnar 	if (!subkey)
22586470930SIngo Molnar 		return error("Config with no key for man viewer: %s", name);
22686470930SIngo Molnar 
22786470930SIngo Molnar 	if (!strcmp(subkey, ".path")) {
22886470930SIngo Molnar 		if (!value)
22986470930SIngo Molnar 			return config_error_nonbool(var);
23086470930SIngo Molnar 		return add_man_viewer_path(name, subkey - name, value);
23186470930SIngo Molnar 	}
23286470930SIngo Molnar 	if (!strcmp(subkey, ".cmd")) {
23386470930SIngo Molnar 		if (!value)
23486470930SIngo Molnar 			return config_error_nonbool(var);
23586470930SIngo Molnar 		return add_man_viewer_cmd(name, subkey - name, value);
23686470930SIngo Molnar 	}
23786470930SIngo Molnar 
23886470930SIngo Molnar 	warning("'%s': unsupported man viewer sub key.", subkey);
23986470930SIngo Molnar 	return 0;
24086470930SIngo Molnar }
24186470930SIngo Molnar 
24286470930SIngo Molnar static int perf_help_config(const char *var, const char *value, void *cb)
24386470930SIngo Molnar {
2442bae1d1bSArnaldo Carvalho de Melo 	enum help_format *help_formatp = cb;
2452bae1d1bSArnaldo Carvalho de Melo 
24686470930SIngo Molnar 	if (!strcmp(var, "help.format")) {
24786470930SIngo Molnar 		if (!value)
24886470930SIngo Molnar 			return config_error_nonbool(var);
2492bae1d1bSArnaldo Carvalho de Melo 		*help_formatp = parse_help_format(value);
2502bae1d1bSArnaldo Carvalho de Melo 		if (*help_formatp == HELP_FORMAT_NONE)
251cc584821SDavid Ahern 			return -1;
25286470930SIngo Molnar 		return 0;
25386470930SIngo Molnar 	}
25486470930SIngo Molnar 	if (!strcmp(var, "man.viewer")) {
25586470930SIngo Molnar 		if (!value)
25686470930SIngo Molnar 			return config_error_nonbool(var);
25786470930SIngo Molnar 		add_man_viewer(value);
25886470930SIngo Molnar 		return 0;
25986470930SIngo Molnar 	}
26086470930SIngo Molnar 	if (!prefixcmp(var, "man."))
26186470930SIngo Molnar 		return add_man_viewer_info(var, value);
26286470930SIngo Molnar 
26386470930SIngo Molnar 	return perf_default_config(var, value, cb);
26486470930SIngo Molnar }
26586470930SIngo Molnar 
26686470930SIngo Molnar static struct cmdnames main_cmds, other_cmds;
26786470930SIngo Molnar 
26886470930SIngo Molnar void list_common_cmds_help(void)
26986470930SIngo Molnar {
270f37a291cSIngo Molnar 	unsigned int i, longest = 0;
27186470930SIngo Molnar 
27286470930SIngo Molnar 	for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
27386470930SIngo Molnar 		if (longest < strlen(common_cmds[i].name))
27486470930SIngo Molnar 			longest = strlen(common_cmds[i].name);
27586470930SIngo Molnar 	}
27686470930SIngo Molnar 
27786470930SIngo Molnar 	puts(" The most commonly used perf commands are:");
27886470930SIngo Molnar 	for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
2793912f2abSAmerigo Wang 		printf("   %-*s   ", longest, common_cmds[i].name);
28086470930SIngo Molnar 		puts(common_cmds[i].help);
28186470930SIngo Molnar 	}
28286470930SIngo Molnar }
28386470930SIngo Molnar 
28486470930SIngo Molnar static int is_perf_command(const char *s)
28586470930SIngo Molnar {
28686470930SIngo Molnar 	return is_in_cmdlist(&main_cmds, s) ||
28786470930SIngo Molnar 		is_in_cmdlist(&other_cmds, s);
28886470930SIngo Molnar }
28986470930SIngo Molnar 
29086470930SIngo Molnar static const char *prepend(const char *prefix, const char *cmd)
29186470930SIngo Molnar {
29286470930SIngo Molnar 	size_t pre_len = strlen(prefix);
29386470930SIngo Molnar 	size_t cmd_len = strlen(cmd);
29486470930SIngo Molnar 	char *p = malloc(pre_len + cmd_len + 1);
29586470930SIngo Molnar 	memcpy(p, prefix, pre_len);
29686470930SIngo Molnar 	strcpy(p + pre_len, cmd);
29786470930SIngo Molnar 	return p;
29886470930SIngo Molnar }
29986470930SIngo Molnar 
30086470930SIngo Molnar static const char *cmd_to_page(const char *perf_cmd)
30186470930SIngo Molnar {
30286470930SIngo Molnar 	if (!perf_cmd)
30386470930SIngo Molnar 		return "perf";
30486470930SIngo Molnar 	else if (!prefixcmp(perf_cmd, "perf"))
30586470930SIngo Molnar 		return perf_cmd;
30686470930SIngo Molnar 	else
30786470930SIngo Molnar 		return prepend("perf-", perf_cmd);
30886470930SIngo Molnar }
30986470930SIngo Molnar 
31086470930SIngo Molnar static void setup_man_path(void)
31186470930SIngo Molnar {
31286470930SIngo Molnar 	struct strbuf new_path = STRBUF_INIT;
31386470930SIngo Molnar 	const char *old_path = getenv("MANPATH");
31486470930SIngo Molnar 
31586470930SIngo Molnar 	/* We should always put ':' after our path. If there is no
31686470930SIngo Molnar 	 * old_path, the ':' at the end will let 'man' to try
31786470930SIngo Molnar 	 * system-wide paths after ours to find the manual page. If
31886470930SIngo Molnar 	 * there is old_path, we need ':' as delimiter. */
31986470930SIngo Molnar 	strbuf_addstr(&new_path, system_path(PERF_MAN_PATH));
32086470930SIngo Molnar 	strbuf_addch(&new_path, ':');
32186470930SIngo Molnar 	if (old_path)
32286470930SIngo Molnar 		strbuf_addstr(&new_path, old_path);
32386470930SIngo Molnar 
32486470930SIngo Molnar 	setenv("MANPATH", new_path.buf, 1);
32586470930SIngo Molnar 
32686470930SIngo Molnar 	strbuf_release(&new_path);
32786470930SIngo Molnar }
32886470930SIngo Molnar 
32986470930SIngo Molnar static void exec_viewer(const char *name, const char *page)
33086470930SIngo Molnar {
33186470930SIngo Molnar 	const char *info = get_man_viewer_info(name);
33286470930SIngo Molnar 
33386470930SIngo Molnar 	if (!strcasecmp(name, "man"))
33486470930SIngo Molnar 		exec_man_man(info, page);
33586470930SIngo Molnar 	else if (!strcasecmp(name, "woman"))
33686470930SIngo Molnar 		exec_woman_emacs(info, page);
33786470930SIngo Molnar 	else if (!strcasecmp(name, "konqueror"))
33886470930SIngo Molnar 		exec_man_konqueror(info, page);
33986470930SIngo Molnar 	else if (info)
34086470930SIngo Molnar 		exec_man_cmd(info, page);
34186470930SIngo Molnar 	else
34286470930SIngo Molnar 		warning("'%s': unknown man viewer.", name);
34386470930SIngo Molnar }
34486470930SIngo Molnar 
345cc584821SDavid Ahern static int show_man_page(const char *perf_cmd)
34686470930SIngo Molnar {
34786470930SIngo Molnar 	struct man_viewer_list *viewer;
34886470930SIngo Molnar 	const char *page = cmd_to_page(perf_cmd);
34986470930SIngo Molnar 	const char *fallback = getenv("PERF_MAN_VIEWER");
35086470930SIngo Molnar 
35186470930SIngo Molnar 	setup_man_path();
35286470930SIngo Molnar 	for (viewer = man_viewer_list; viewer; viewer = viewer->next)
35386470930SIngo Molnar 		exec_viewer(viewer->name, page); /* will return when unable */
354c1e53017SThiago Farina 
35586470930SIngo Molnar 	if (fallback)
35686470930SIngo Molnar 		exec_viewer(fallback, page);
35786470930SIngo Molnar 	exec_viewer("man", page);
358cc584821SDavid Ahern 
359cc584821SDavid Ahern 	pr_err("no man viewer handled the request");
360cc584821SDavid Ahern 	return -1;
36186470930SIngo Molnar }
36286470930SIngo Molnar 
363cc584821SDavid Ahern static int show_info_page(const char *perf_cmd)
36486470930SIngo Molnar {
36586470930SIngo Molnar 	const char *page = cmd_to_page(perf_cmd);
36686470930SIngo Molnar 	setenv("INFOPATH", system_path(PERF_INFO_PATH), 1);
36786470930SIngo Molnar 	execlp("info", "info", "perfman", page, NULL);
368cc584821SDavid Ahern 	return -1;
36986470930SIngo Molnar }
37086470930SIngo Molnar 
371cc584821SDavid Ahern static int get_html_page_path(struct strbuf *page_path, const char *page)
37286470930SIngo Molnar {
37386470930SIngo Molnar 	struct stat st;
37486470930SIngo Molnar 	const char *html_path = system_path(PERF_HTML_PATH);
37586470930SIngo Molnar 
37686470930SIngo Molnar 	/* Check that we have a perf documentation directory. */
37786470930SIngo Molnar 	if (stat(mkpath("%s/perf.html", html_path), &st)
378cc584821SDavid Ahern 	    || !S_ISREG(st.st_mode)) {
379cc584821SDavid Ahern 		pr_err("'%s': not a documentation directory.", html_path);
380cc584821SDavid Ahern 		return -1;
381cc584821SDavid Ahern 	}
38286470930SIngo Molnar 
38386470930SIngo Molnar 	strbuf_init(page_path, 0);
38486470930SIngo Molnar 	strbuf_addf(page_path, "%s/%s.html", html_path, page);
385cc584821SDavid Ahern 
386cc584821SDavid Ahern 	return 0;
38786470930SIngo Molnar }
38886470930SIngo Molnar 
38986470930SIngo Molnar /*
39086470930SIngo Molnar  * If open_html is not defined in a platform-specific way (see for
39186470930SIngo Molnar  * example compat/mingw.h), we use the script web--browse to display
39286470930SIngo Molnar  * HTML.
39386470930SIngo Molnar  */
39486470930SIngo Molnar #ifndef open_html
39586470930SIngo Molnar static void open_html(const char *path)
39686470930SIngo Molnar {
39786470930SIngo Molnar 	execl_perf_cmd("web--browse", "-c", "help.browser", path, NULL);
39886470930SIngo Molnar }
39986470930SIngo Molnar #endif
40086470930SIngo Molnar 
401cc584821SDavid Ahern static int show_html_page(const char *perf_cmd)
40286470930SIngo Molnar {
40386470930SIngo Molnar 	const char *page = cmd_to_page(perf_cmd);
40486470930SIngo Molnar 	struct strbuf page_path; /* it leaks but we exec bellow */
40586470930SIngo Molnar 
406cc584821SDavid Ahern 	if (get_html_page_path(&page_path, page) != 0)
407cc584821SDavid Ahern 		return -1;
40886470930SIngo Molnar 
40986470930SIngo Molnar 	open_html(page_path.buf);
410cc584821SDavid Ahern 
411cc584821SDavid Ahern 	return 0;
41286470930SIngo Molnar }
41386470930SIngo Molnar 
4141d037ca1SIrina Tirdea int cmd_help(int argc, const char **argv, const char *prefix __maybe_unused)
41586470930SIngo Molnar {
4162bae1d1bSArnaldo Carvalho de Melo 	bool show_all = false;
417*670ab5d2SNamhyung Kim 	enum help_format help_format = HELP_FORMAT_MAN;
4182bae1d1bSArnaldo Carvalho de Melo 	struct option builtin_help_options[] = {
4192bae1d1bSArnaldo Carvalho de Melo 	OPT_BOOLEAN('a', "all", &show_all, "print all available commands"),
4202bae1d1bSArnaldo Carvalho de Melo 	OPT_SET_UINT('m', "man", &help_format, "show man page", HELP_FORMAT_MAN),
4212bae1d1bSArnaldo Carvalho de Melo 	OPT_SET_UINT('w', "web", &help_format, "show manual in web browser",
4222bae1d1bSArnaldo Carvalho de Melo 			HELP_FORMAT_WEB),
4232bae1d1bSArnaldo Carvalho de Melo 	OPT_SET_UINT('i', "info", &help_format, "show info page",
4242bae1d1bSArnaldo Carvalho de Melo 			HELP_FORMAT_INFO),
4252bae1d1bSArnaldo Carvalho de Melo 	OPT_END(),
4262bae1d1bSArnaldo Carvalho de Melo 	};
4272bae1d1bSArnaldo Carvalho de Melo 	const char * const builtin_help_usage[] = {
4282bae1d1bSArnaldo Carvalho de Melo 		"perf help [--all] [--man|--web|--info] [command]",
4292bae1d1bSArnaldo Carvalho de Melo 		NULL
4302bae1d1bSArnaldo Carvalho de Melo 	};
43186470930SIngo Molnar 	const char *alias;
432cc584821SDavid Ahern 	int rc = 0;
433f37a291cSIngo Molnar 
43486470930SIngo Molnar 	load_command_list("perf-", &main_cmds, &other_cmds);
43586470930SIngo Molnar 
4362bae1d1bSArnaldo Carvalho de Melo 	perf_config(perf_help_config, &help_format);
43786470930SIngo Molnar 
43886470930SIngo Molnar 	argc = parse_options(argc, argv, builtin_help_options,
43986470930SIngo Molnar 			builtin_help_usage, 0);
44086470930SIngo Molnar 
44186470930SIngo Molnar 	if (show_all) {
44286470930SIngo Molnar 		printf("\n usage: %s\n\n", perf_usage_string);
44386470930SIngo Molnar 		list_commands("perf commands", &main_cmds, &other_cmds);
44486470930SIngo Molnar 		printf(" %s\n\n", perf_more_info_string);
44586470930SIngo Molnar 		return 0;
44686470930SIngo Molnar 	}
44786470930SIngo Molnar 
44886470930SIngo Molnar 	if (!argv[0]) {
44986470930SIngo Molnar 		printf("\n usage: %s\n\n", perf_usage_string);
45086470930SIngo Molnar 		list_common_cmds_help();
45186470930SIngo Molnar 		printf("\n %s\n\n", perf_more_info_string);
45286470930SIngo Molnar 		return 0;
45386470930SIngo Molnar 	}
45486470930SIngo Molnar 
45586470930SIngo Molnar 	alias = alias_lookup(argv[0]);
45686470930SIngo Molnar 	if (alias && !is_perf_command(argv[0])) {
45786470930SIngo Molnar 		printf("`perf %s' is aliased to `%s'\n", argv[0], alias);
45886470930SIngo Molnar 		return 0;
45986470930SIngo Molnar 	}
46086470930SIngo Molnar 
46186470930SIngo Molnar 	switch (help_format) {
46286470930SIngo Molnar 	case HELP_FORMAT_MAN:
463cc584821SDavid Ahern 		rc = show_man_page(argv[0]);
46486470930SIngo Molnar 		break;
46586470930SIngo Molnar 	case HELP_FORMAT_INFO:
466cc584821SDavid Ahern 		rc = show_info_page(argv[0]);
46786470930SIngo Molnar 		break;
46886470930SIngo Molnar 	case HELP_FORMAT_WEB:
469cc584821SDavid Ahern 		rc = show_html_page(argv[0]);
470cc584821SDavid Ahern 		break;
471cc584821SDavid Ahern 	case HELP_FORMAT_NONE:
472cc584821SDavid Ahern 		/* fall-through */
47383a0944fSIngo Molnar 	default:
474cc584821SDavid Ahern 		rc = -1;
47586470930SIngo Molnar 		break;
47686470930SIngo Molnar 	}
47786470930SIngo Molnar 
478cc584821SDavid Ahern 	return rc;
47986470930SIngo Molnar }
480