1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
286470930SIngo Molnar /*
386470930SIngo Molnar * builtin-help.c
486470930SIngo Molnar *
586470930SIngo Molnar * Builtin help command
686470930SIngo Molnar */
78520a98dSArnaldo Carvalho de Melo #include "util/cache.h"
841840d21STaeung Song #include "util/config.h"
98520a98dSArnaldo Carvalho de Melo #include "util/strbuf.h"
1086470930SIngo Molnar #include "builtin.h"
114b6ab94eSJosh Poimboeuf #include <subcmd/exec-cmd.h>
1286470930SIngo Molnar #include "common-cmds.h"
134b6ab94eSJosh Poimboeuf #include <subcmd/parse-options.h>
144b6ab94eSJosh Poimboeuf #include <subcmd/run-command.h>
154b6ab94eSJosh Poimboeuf #include <subcmd/help.h>
1684f5d36fSJiri Olsa #include "util/debug.h"
17*0adea51aSIan Rogers #include "util/util.h"
18877a7a11SArnaldo Carvalho de Melo #include <linux/kernel.h>
198520a98dSArnaldo Carvalho de Melo #include <linux/string.h>
207f7c536fSArnaldo Carvalho de Melo #include <linux/zalloc.h>
21a43783aeSArnaldo Carvalho de Melo #include <errno.h>
227a8ef4c4SArnaldo Carvalho de Melo #include <limits.h>
23215a0d30SArnaldo Carvalho de Melo #include <stdio.h>
248520a98dSArnaldo Carvalho de Melo #include <stdlib.h>
257a8ef4c4SArnaldo Carvalho de Melo #include <string.h>
267a8ef4c4SArnaldo Carvalho de Melo #include <sys/types.h>
277a8ef4c4SArnaldo Carvalho de Melo #include <sys/stat.h>
2886470930SIngo Molnar #include <unistd.h>
2986470930SIngo Molnar
3086470930SIngo Molnar static struct man_viewer_list {
317909675dSArnaldo Carvalho de Melo struct man_viewer_list *next;
3286470930SIngo Molnar char name[0];
3386470930SIngo Molnar } *man_viewer_list;
3486470930SIngo Molnar
3586470930SIngo Molnar static struct man_viewer_info_list {
3686470930SIngo Molnar struct man_viewer_info_list *next;
377909675dSArnaldo Carvalho de Melo const char *info;
3886470930SIngo Molnar char name[0];
3986470930SIngo Molnar } *man_viewer_info_list;
4086470930SIngo Molnar
41cc584821SDavid Ahern enum help_format {
4286470930SIngo Molnar HELP_FORMAT_NONE,
4386470930SIngo Molnar HELP_FORMAT_MAN,
4486470930SIngo Molnar HELP_FORMAT_INFO,
4586470930SIngo Molnar HELP_FORMAT_WEB,
4686470930SIngo Molnar };
4786470930SIngo Molnar
parse_help_format(const char * format)4886470930SIngo Molnar static enum help_format parse_help_format(const char *format)
4986470930SIngo Molnar {
5086470930SIngo Molnar if (!strcmp(format, "man"))
5186470930SIngo Molnar return HELP_FORMAT_MAN;
5286470930SIngo Molnar if (!strcmp(format, "info"))
5386470930SIngo Molnar return HELP_FORMAT_INFO;
5486470930SIngo Molnar if (!strcmp(format, "web") || !strcmp(format, "html"))
55cc584821SDavid Ahern return HELP_FORMAT_WEB;
56cc584821SDavid Ahern
57cc584821SDavid Ahern pr_err("unrecognized help format '%s'", format);
5886470930SIngo Molnar return HELP_FORMAT_NONE;
5986470930SIngo Molnar }
6086470930SIngo Molnar
get_man_viewer_info(const char * name)6186470930SIngo Molnar static const char *get_man_viewer_info(const char *name)
6286470930SIngo Molnar {
6386470930SIngo Molnar struct man_viewer_info_list *viewer;
64c1e53017SThiago Farina
6586470930SIngo Molnar for (viewer = man_viewer_info_list; viewer; viewer = viewer->next) {
6686470930SIngo Molnar if (!strcasecmp(name, viewer->name))
6786470930SIngo Molnar return viewer->info;
6886470930SIngo Molnar }
6986470930SIngo Molnar return NULL;
7086470930SIngo Molnar }
7186470930SIngo Molnar
check_emacsclient_version(void)7286470930SIngo Molnar static int check_emacsclient_version(void)
7386470930SIngo Molnar {
7486470930SIngo Molnar struct strbuf buffer = STRBUF_INIT;
7586470930SIngo Molnar struct child_process ec_process;
7686470930SIngo Molnar const char *argv_ec[] = { "emacsclient", "--version", NULL };
77b72ca403SMasami Hiramatsu int version;
7886470930SIngo Molnar int ret = -1;
7986470930SIngo Molnar
8086470930SIngo Molnar /* emacsclient prints its version number on stderr */
8186470930SIngo Molnar memset(&ec_process, 0, sizeof(ec_process));
8286470930SIngo Molnar ec_process.argv = argv_ec;
8386470930SIngo Molnar ec_process.err = -1;
8486470930SIngo Molnar ec_process.stdout_to_stderr = 1;
8586470930SIngo Molnar if (start_command(&ec_process)) {
8686470930SIngo Molnar fprintf(stderr, "Failed to start emacsclient.\n");
8786470930SIngo Molnar return -1;
88b72ca403SMasami Hiramatsu }
89b72ca403SMasami Hiramatsu if (strbuf_read(&buffer, ec_process.err, 20) < 0) {
90b72ca403SMasami Hiramatsu fprintf(stderr, "Failed to read emacsclient version\n");
91b72ca403SMasami Hiramatsu goto out;
9286470930SIngo Molnar }
9386470930SIngo Molnar close(ec_process.err);
9486470930SIngo Molnar
9586470930SIngo Molnar /*
9686470930SIngo Molnar * Don't bother checking return value, because "emacsclient --version"
9786470930SIngo Molnar * seems to always exits with code 1.
9886470930SIngo Molnar */
9986470930SIngo Molnar finish_command(&ec_process);
1008e99b6d4SArnaldo Carvalho de Melo
10186470930SIngo Molnar if (!strstarts(buffer.buf, "emacsclient")) {
102b72ca403SMasami Hiramatsu fprintf(stderr, "Failed to parse emacsclient version.\n");
10386470930SIngo Molnar goto out;
10486470930SIngo Molnar }
105d1130686SArnaldo Carvalho de Melo
10686470930SIngo Molnar version = atoi(buffer.buf + strlen("emacsclient"));
10786470930SIngo Molnar
10886470930SIngo Molnar if (version < 22) {
10986470930SIngo Molnar fprintf(stderr,
11086470930SIngo Molnar "emacsclient version '%d' too old (< 22).\n",
111b72ca403SMasami Hiramatsu version);
112b72ca403SMasami Hiramatsu } else
113b72ca403SMasami Hiramatsu ret = 0;
11486470930SIngo Molnar out:
115b72ca403SMasami Hiramatsu strbuf_release(&buffer);
11686470930SIngo Molnar return ret;
11786470930SIngo Molnar }
118881c362dSArnaldo Carvalho de Melo
exec_failed(const char * cmd)11986470930SIngo Molnar static void exec_failed(const char *cmd)
120ede395d2SMasami Hiramatsu {
121881c362dSArnaldo Carvalho de Melo char sbuf[STRERR_BUFSIZE];
122881c362dSArnaldo Carvalho de Melo pr_warning("failed to exec '%s': %s", cmd, str_error_r(errno, sbuf, sizeof(sbuf)));
123ede395d2SMasami Hiramatsu }
124881c362dSArnaldo Carvalho de Melo
exec_woman_emacs(const char * path,const char * page)125881c362dSArnaldo Carvalho de Melo static void exec_woman_emacs(const char *path, const char *page)
12686470930SIngo Molnar {
12786470930SIngo Molnar if (!check_emacsclient_version()) {
128a610f5cbSArnaldo Carvalho de Melo /* This works only with emacsclient version >= 22. */
12986470930SIngo Molnar char *man_page;
13086470930SIngo Molnar
13186470930SIngo Molnar if (!path)
132a610f5cbSArnaldo Carvalho de Melo path = "emacsclient";
133a610f5cbSArnaldo Carvalho de Melo if (asprintf(&man_page, "(woman \"%s\")", page) > 0) {
134a610f5cbSArnaldo Carvalho de Melo execlp(path, "emacsclient", "-e", man_page, NULL);
135a610f5cbSArnaldo Carvalho de Melo free(man_page);
136881c362dSArnaldo Carvalho de Melo }
13786470930SIngo Molnar exec_failed(path);
13886470930SIngo Molnar }
13986470930SIngo Molnar }
14086470930SIngo Molnar
exec_man_konqueror(const char * path,const char * page)14186470930SIngo Molnar static void exec_man_konqueror(const char *path, const char *page)
14286470930SIngo Molnar {
143ede395d2SMasami Hiramatsu const char *display = getenv("DISPLAY");
14486470930SIngo Molnar
145a610f5cbSArnaldo Carvalho de Melo if (display && *display) {
14686470930SIngo Molnar char *man_page;
14786470930SIngo Molnar const char *filename = "kfmclient";
14886470930SIngo Molnar
14986470930SIngo Molnar /* It's simpler to launch konqueror using kfmclient. */
15086470930SIngo Molnar if (path) {
15186470930SIngo Molnar const char *file = strrchr(path, '/');
15286470930SIngo Molnar if (file && !strcmp(file + 1, "konqueror")) {
15386470930SIngo Molnar char *new = strdup(path);
15486470930SIngo Molnar char *dest = strrchr(new, '/');
15586470930SIngo Molnar
15686470930SIngo Molnar /* strlen("konqueror") == strlen("kfmclient") */
15786470930SIngo Molnar strcpy(dest + 1, "kfmclient");
15886470930SIngo Molnar path = new;
15986470930SIngo Molnar }
16086470930SIngo Molnar if (file)
16186470930SIngo Molnar filename = file;
16286470930SIngo Molnar } else
163a610f5cbSArnaldo Carvalho de Melo path = "kfmclient";
164a610f5cbSArnaldo Carvalho de Melo if (asprintf(&man_page, "man:%s(1)", page) > 0) {
165a610f5cbSArnaldo Carvalho de Melo execlp(path, filename, "newTab", man_page, NULL);
166a610f5cbSArnaldo Carvalho de Melo free(man_page);
167881c362dSArnaldo Carvalho de Melo }
16886470930SIngo Molnar exec_failed(path);
16986470930SIngo Molnar }
17086470930SIngo Molnar }
17186470930SIngo Molnar
exec_man_man(const char * path,const char * page)17286470930SIngo Molnar static void exec_man_man(const char *path, const char *page)
17386470930SIngo Molnar {
17486470930SIngo Molnar if (!path)
17586470930SIngo Molnar path = "man";
176881c362dSArnaldo Carvalho de Melo execlp(path, "man", page, NULL);
17786470930SIngo Molnar exec_failed(path);
17886470930SIngo Molnar }
17986470930SIngo Molnar
exec_man_cmd(const char * cmd,const char * page)18086470930SIngo Molnar static void exec_man_cmd(const char *cmd, const char *page)
181a610f5cbSArnaldo Carvalho de Melo {
182ede395d2SMasami Hiramatsu char *shell_cmd;
183a610f5cbSArnaldo Carvalho de Melo
184a610f5cbSArnaldo Carvalho de Melo if (asprintf(&shell_cmd, "%s %s", cmd, page) > 0) {
185a610f5cbSArnaldo Carvalho de Melo execl("/bin/sh", "sh", "-c", shell_cmd, NULL);
186a610f5cbSArnaldo Carvalho de Melo free(shell_cmd);
187881c362dSArnaldo Carvalho de Melo }
18886470930SIngo Molnar exec_failed(cmd);
18986470930SIngo Molnar }
19086470930SIngo Molnar
add_man_viewer(const char * name)19186470930SIngo Molnar static void add_man_viewer(const char *name)
19286470930SIngo Molnar {
19386470930SIngo Molnar struct man_viewer_list **p = &man_viewer_list;
19486470930SIngo Molnar size_t len = strlen(name);
19586470930SIngo Molnar
19686470930SIngo Molnar while (*p)
19736479484SArnaldo Carvalho de Melo p = &((*p)->next);
198b6313899SArnaldo Carvalho de Melo *p = zalloc(sizeof(**p) + len + 1);
19986470930SIngo Molnar strcpy((*p)->name, name);
20086470930SIngo Molnar }
20186470930SIngo Molnar
supported_man_viewer(const char * name,size_t len)20286470930SIngo Molnar static int supported_man_viewer(const char *name, size_t len)
20386470930SIngo Molnar {
20486470930SIngo Molnar return (!strncasecmp("man", name, len) ||
20586470930SIngo Molnar !strncasecmp("woman", name, len) ||
20686470930SIngo Molnar !strncasecmp("konqueror", name, len));
20786470930SIngo Molnar }
20886470930SIngo Molnar
do_add_man_viewer_info(const char * name,size_t len,const char * value)20986470930SIngo Molnar static void do_add_man_viewer_info(const char *name,
21086470930SIngo Molnar size_t len,
21186470930SIngo Molnar const char *value)
21236479484SArnaldo Carvalho de Melo {
21386470930SIngo Molnar struct man_viewer_info_list *new = zalloc(sizeof(*new) + len + 1);
21486470930SIngo Molnar
21586470930SIngo Molnar strncpy(new->name, name, len);
21686470930SIngo Molnar new->info = strdup(value);
21786470930SIngo Molnar new->next = man_viewer_info_list;
21886470930SIngo Molnar man_viewer_info_list = new;
21986470930SIngo Molnar }
22086e474ffSArnaldo Carvalho de Melo
unsupported_man_viewer(const char * name,const char * var)22186e474ffSArnaldo Carvalho de Melo static void unsupported_man_viewer(const char *name, const char *var)
22286e474ffSArnaldo Carvalho de Melo {
22386e474ffSArnaldo Carvalho de Melo pr_warning("'%s': path for unsupported man viewer.\n"
22486e474ffSArnaldo Carvalho de Melo "Please consider using 'man.<tool>.%s' instead.", name, var);
22586e474ffSArnaldo Carvalho de Melo }
22686470930SIngo Molnar
add_man_viewer_path(const char * name,size_t len,const char * value)22786470930SIngo Molnar static int add_man_viewer_path(const char *name,
22886470930SIngo Molnar size_t len,
22986470930SIngo Molnar const char *value)
23086470930SIngo Molnar {
23186470930SIngo Molnar if (supported_man_viewer(name, len))
23286470930SIngo Molnar do_add_man_viewer_info(name, len, value);
23386e474ffSArnaldo Carvalho de Melo else
23486470930SIngo Molnar unsupported_man_viewer(name, "cmd");
23586470930SIngo Molnar
23686470930SIngo Molnar return 0;
23786470930SIngo Molnar }
23886470930SIngo Molnar
add_man_viewer_cmd(const char * name,size_t len,const char * value)23986470930SIngo Molnar static int add_man_viewer_cmd(const char *name,
24086470930SIngo Molnar size_t len,
24186470930SIngo Molnar const char *value)
24286470930SIngo Molnar {
24386e474ffSArnaldo Carvalho de Melo if (supported_man_viewer(name, len))
24486470930SIngo Molnar unsupported_man_viewer(name, "path");
24586470930SIngo Molnar else
24686470930SIngo Molnar do_add_man_viewer_info(name, len, value);
24786470930SIngo Molnar
24886470930SIngo Molnar return 0;
24986470930SIngo Molnar }
25086470930SIngo Molnar
add_man_viewer_info(const char * var,const char * value)25186470930SIngo Molnar static int add_man_viewer_info(const char *var, const char *value)
25286470930SIngo Molnar {
25386470930SIngo Molnar const char *name = var + 4;
25486470930SIngo Molnar const char *subkey = strrchr(name, '.');
25562d94b00SArnaldo Carvalho de Melo
25662d94b00SArnaldo Carvalho de Melo if (!subkey) {
25762d94b00SArnaldo Carvalho de Melo pr_err("Config with no key for man viewer: %s", name);
25862d94b00SArnaldo Carvalho de Melo return -1;
25986470930SIngo Molnar }
26086470930SIngo Molnar
26186470930SIngo Molnar if (!strcmp(subkey, ".path")) {
26286470930SIngo Molnar if (!value)
26386470930SIngo Molnar return config_error_nonbool(var);
26486470930SIngo Molnar return add_man_viewer_path(name, subkey - name, value);
26586470930SIngo Molnar }
26686470930SIngo Molnar if (!strcmp(subkey, ".cmd")) {
26786470930SIngo Molnar if (!value)
26886470930SIngo Molnar return config_error_nonbool(var);
26986470930SIngo Molnar return add_man_viewer_cmd(name, subkey - name, value);
27086470930SIngo Molnar }
27159913aabSArnaldo Carvalho de Melo
27286470930SIngo Molnar pr_warning("'%s': unsupported man viewer sub key.", subkey);
27386470930SIngo Molnar return 0;
27486470930SIngo Molnar }
27586470930SIngo Molnar
perf_help_config(const char * var,const char * value,void * cb)27686470930SIngo Molnar static int perf_help_config(const char *var, const char *value, void *cb)
2772bae1d1bSArnaldo Carvalho de Melo {
2782bae1d1bSArnaldo Carvalho de Melo enum help_format *help_formatp = cb;
27986470930SIngo Molnar
28086470930SIngo Molnar if (!strcmp(var, "help.format")) {
28186470930SIngo Molnar if (!value)
2822bae1d1bSArnaldo Carvalho de Melo return config_error_nonbool(var);
2832bae1d1bSArnaldo Carvalho de Melo *help_formatp = parse_help_format(value);
284cc584821SDavid Ahern if (*help_formatp == HELP_FORMAT_NONE)
28586470930SIngo Molnar return -1;
28686470930SIngo Molnar return 0;
28786470930SIngo Molnar }
28886470930SIngo Molnar if (!strcmp(var, "man.viewer")) {
28986470930SIngo Molnar if (!value)
29086470930SIngo Molnar return config_error_nonbool(var);
29186470930SIngo Molnar add_man_viewer(value);
29286470930SIngo Molnar return 0;
2932f0af860SNamhyung Kim }
29486470930SIngo Molnar if (strstarts(var, "man."))
29586470930SIngo Molnar return add_man_viewer_info(var, value);
296b8cbb349SWang Nan
29786470930SIngo Molnar return 0;
29886470930SIngo Molnar }
29986470930SIngo Molnar
30086470930SIngo Molnar static struct cmdnames main_cmds, other_cmds;
30186470930SIngo Molnar
list_common_cmds_help(void)30286470930SIngo Molnar void list_common_cmds_help(void)
303f37a291cSIngo Molnar {
30486470930SIngo Molnar unsigned int i, longest = 0;
30586470930SIngo Molnar
30686470930SIngo Molnar for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
30786470930SIngo Molnar if (longest < strlen(common_cmds[i].name))
30886470930SIngo Molnar longest = strlen(common_cmds[i].name);
30986470930SIngo Molnar }
31086470930SIngo Molnar
31186470930SIngo Molnar puts(" The most commonly used perf commands are:");
3123912f2abSAmerigo Wang for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
31386470930SIngo Molnar printf(" %-*s ", longest, common_cmds[i].name);
31486470930SIngo Molnar puts(common_cmds[i].help);
31586470930SIngo Molnar }
31686470930SIngo Molnar }
31786470930SIngo Molnar
cmd_to_page(const char * perf_cmd)31886470930SIngo Molnar static const char *cmd_to_page(const char *perf_cmd)
319a610f5cbSArnaldo Carvalho de Melo {
320a610f5cbSArnaldo Carvalho de Melo char *s;
32186470930SIngo Molnar
32286470930SIngo Molnar if (!perf_cmd)
3232f0af860SNamhyung Kim return "perf";
32486470930SIngo Molnar else if (strstarts(perf_cmd, "perf"))
325a610f5cbSArnaldo Carvalho de Melo return perf_cmd;
326a610f5cbSArnaldo Carvalho de Melo
32786470930SIngo Molnar return asprintf(&s, "perf-%s", perf_cmd) < 0 ? NULL : s;
32886470930SIngo Molnar }
32986470930SIngo Molnar
setup_man_path(void)33086470930SIngo Molnar static void setup_man_path(void)
331a610f5cbSArnaldo Carvalho de Melo {
33286470930SIngo Molnar char *new_path;
33386470930SIngo Molnar const char *old_path = getenv("MANPATH");
33486470930SIngo Molnar
33586470930SIngo Molnar /* We should always put ':' after our path. If there is no
33686470930SIngo Molnar * old_path, the ':' at the end will let 'man' to try
33786470930SIngo Molnar * system-wide paths after ours to find the manual page. If
338a610f5cbSArnaldo Carvalho de Melo * there is old_path, we need ':' as delimiter. */
339a610f5cbSArnaldo Carvalho de Melo if (asprintf(&new_path, "%s:%s", system_path(PERF_MAN_PATH), old_path ?: "") > 0) {
340a610f5cbSArnaldo Carvalho de Melo setenv("MANPATH", new_path, 1);
341a610f5cbSArnaldo Carvalho de Melo free(new_path);
34262d94b00SArnaldo Carvalho de Melo } else {
343a610f5cbSArnaldo Carvalho de Melo pr_err("Unable to setup man path");
34486470930SIngo Molnar }
34586470930SIngo Molnar }
34686470930SIngo Molnar
exec_viewer(const char * name,const char * page)34786470930SIngo Molnar static void exec_viewer(const char *name, const char *page)
34886470930SIngo Molnar {
34986470930SIngo Molnar const char *info = get_man_viewer_info(name);
35086470930SIngo Molnar
35186470930SIngo Molnar if (!strcasecmp(name, "man"))
35286470930SIngo Molnar exec_man_man(info, page);
35386470930SIngo Molnar else if (!strcasecmp(name, "woman"))
35486470930SIngo Molnar exec_woman_emacs(info, page);
35586470930SIngo Molnar else if (!strcasecmp(name, "konqueror"))
35686470930SIngo Molnar exec_man_konqueror(info, page);
35786470930SIngo Molnar else if (info)
35886470930SIngo Molnar exec_man_cmd(info, page);
35959913aabSArnaldo Carvalho de Melo else
36086470930SIngo Molnar pr_warning("'%s': unknown man viewer.", name);
36186470930SIngo Molnar }
362cc584821SDavid Ahern
show_man_page(const char * perf_cmd)36386470930SIngo Molnar static int show_man_page(const char *perf_cmd)
36486470930SIngo Molnar {
36586470930SIngo Molnar struct man_viewer_list *viewer;
36686470930SIngo Molnar const char *page = cmd_to_page(perf_cmd);
36786470930SIngo Molnar const char *fallback = getenv("PERF_MAN_VIEWER");
36886470930SIngo Molnar
36986470930SIngo Molnar setup_man_path();
37086470930SIngo Molnar for (viewer = man_viewer_list; viewer; viewer = viewer->next)
371c1e53017SThiago Farina exec_viewer(viewer->name, page); /* will return when unable */
37286470930SIngo Molnar
37386470930SIngo Molnar if (fallback)
37486470930SIngo Molnar exec_viewer(fallback, page);
375cc584821SDavid Ahern exec_viewer("man", page);
376cc584821SDavid Ahern
377cc584821SDavid Ahern pr_err("no man viewer handled the request");
37886470930SIngo Molnar return -1;
37986470930SIngo Molnar }
380cc584821SDavid Ahern
show_info_page(const char * perf_cmd)38186470930SIngo Molnar static int show_info_page(const char *perf_cmd)
38286470930SIngo Molnar {
38386470930SIngo Molnar const char *page = cmd_to_page(perf_cmd);
38486470930SIngo Molnar setenv("INFOPATH", system_path(PERF_INFO_PATH), 1);
385cc584821SDavid Ahern execlp("info", "info", "perfman", page, NULL);
38686470930SIngo Molnar return -1;
38786470930SIngo Molnar }
388a610f5cbSArnaldo Carvalho de Melo
get_html_page_path(char ** page_path,const char * page)38986470930SIngo Molnar static int get_html_page_path(char **page_path, const char *page)
39086470930SIngo Molnar {
39186470930SIngo Molnar struct stat st;
39286470930SIngo Molnar const char *html_path = system_path(PERF_HTML_PATH);
39386470930SIngo Molnar char path[PATH_MAX];
39486470930SIngo Molnar
395cc584821SDavid Ahern /* Check that we have a perf documentation directory. */
396cc584821SDavid Ahern if (stat(mkpath(path, sizeof(path), "%s/perf.html", html_path), &st)
397cc584821SDavid Ahern || !S_ISREG(st.st_mode)) {
398cc584821SDavid Ahern pr_err("'%s': not a documentation directory.", html_path);
39986470930SIngo Molnar return -1;
400a610f5cbSArnaldo Carvalho de Melo }
40186470930SIngo Molnar
40286470930SIngo Molnar return asprintf(page_path, "%s/%s.html", html_path, page);
40386470930SIngo Molnar }
40486470930SIngo Molnar
40586470930SIngo Molnar /*
40686470930SIngo Molnar * If open_html is not defined in a platform-specific way (see for
40786470930SIngo Molnar * example compat/mingw.h), we use the script web--browse to display
40886470930SIngo Molnar * HTML.
40986470930SIngo Molnar */
41086470930SIngo Molnar #ifndef open_html
open_html(const char * path)41146113a54SJosh Poimboeuf static void open_html(const char *path)
41286470930SIngo Molnar {
41386470930SIngo Molnar execl_cmd("web--browse", "-c", "help.browser", path, NULL);
41486470930SIngo Molnar }
415cc584821SDavid Ahern #endif
41686470930SIngo Molnar
show_html_page(const char * perf_cmd)41786470930SIngo Molnar static int show_html_page(const char *perf_cmd)
418a610f5cbSArnaldo Carvalho de Melo {
41986470930SIngo Molnar const char *page = cmd_to_page(perf_cmd);
420a610f5cbSArnaldo Carvalho de Melo char *page_path; /* it leaks but we exec bellow */
421cc584821SDavid Ahern
42286470930SIngo Molnar if (get_html_page_path(&page_path, page) < 0)
423a610f5cbSArnaldo Carvalho de Melo return -1;
424cc584821SDavid Ahern
425cc584821SDavid Ahern open_html(page_path);
42686470930SIngo Molnar
42786470930SIngo Molnar return 0;
428b0ad8ea6SArnaldo Carvalho de Melo }
42986470930SIngo Molnar
cmd_help(int argc,const char ** argv)4302bae1d1bSArnaldo Carvalho de Melo int cmd_help(int argc, const char **argv)
431670ab5d2SNamhyung Kim {
4322bae1d1bSArnaldo Carvalho de Melo bool show_all = false;
4332bae1d1bSArnaldo Carvalho de Melo enum help_format help_format = HELP_FORMAT_MAN;
4342bae1d1bSArnaldo Carvalho de Melo struct option builtin_help_options[] = {
4352bae1d1bSArnaldo Carvalho de Melo OPT_BOOLEAN('a', "all", &show_all, "print all available commands"),
4362bae1d1bSArnaldo Carvalho de Melo OPT_SET_UINT('m', "man", &help_format, "show man page", HELP_FORMAT_MAN),
4372bae1d1bSArnaldo Carvalho de Melo OPT_SET_UINT('w', "web", &help_format, "show manual in web browser",
4382bae1d1bSArnaldo Carvalho de Melo HELP_FORMAT_WEB),
4392bae1d1bSArnaldo Carvalho de Melo OPT_SET_UINT('i', "info", &help_format, "show info page",
4402bae1d1bSArnaldo Carvalho de Melo HELP_FORMAT_INFO),
441e24a1108SYunlong Song OPT_END(),
442e24a1108SYunlong Song };
443e24a1108SYunlong Song const char * const builtin_help_subcommands[] = {
44435563771SArnaldo Carvalho de Melo "buildid-cache", "buildid-list", "diff", "evlist", "help", "list",
445e24a1108SYunlong Song "record", "report", "bench", "stat", "timechart", "top", "annotate",
446e24a1108SYunlong Song "script", "sched", "kallsyms", "kmem", "lock", "kvm", "test", "inject", "mem", "data",
447e24a1108SYunlong Song #ifdef HAVE_LIBELF_SUPPORT
44822e9af4eSJin Yao "probe",
449e24a1108SYunlong Song #endif
450e24a1108SYunlong Song #if defined(HAVE_LIBAUDIT_SUPPORT) || defined(HAVE_SYSCALL_TABLE_SUPPORT)
451e24a1108SYunlong Song "trace",
452e24a1108SYunlong Song #endif
4532bae1d1bSArnaldo Carvalho de Melo NULL };
4542bae1d1bSArnaldo Carvalho de Melo const char *builtin_help_usage[] = {
4552bae1d1bSArnaldo Carvalho de Melo "perf help [--all] [--man|--web|--info] [command]",
456ecc4c561SArnaldo Carvalho de Melo NULL
457f37a291cSIngo Molnar };
45886470930SIngo Molnar int rc;
45986470930SIngo Molnar
460ecc4c561SArnaldo Carvalho de Melo load_command_list("perf-", &main_cmds, &other_cmds);
461ecc4c561SArnaldo Carvalho de Melo
462ecc4c561SArnaldo Carvalho de Melo rc = perf_config(perf_help_config, &help_format);
46386470930SIngo Molnar if (rc)
464e24a1108SYunlong Song return rc;
465e24a1108SYunlong Song
46686470930SIngo Molnar argc = parse_options_subcommand(argc, argv, builtin_help_options,
46786470930SIngo Molnar builtin_help_subcommands, builtin_help_usage, 0);
4683a134ae9SYunlong Song
46986470930SIngo Molnar if (show_all) {
47086470930SIngo Molnar printf("\n Usage: %s\n\n", perf_usage_string);
47186470930SIngo Molnar list_commands("perf commands", &main_cmds, &other_cmds);
47286470930SIngo Molnar printf(" %s\n\n", perf_more_info_string);
47386470930SIngo Molnar return 0;
47486470930SIngo Molnar }
47586470930SIngo Molnar
47686470930SIngo Molnar if (!argv[0]) {
47786470930SIngo Molnar printf("\n usage: %s\n\n", perf_usage_string);
47886470930SIngo Molnar list_common_cmds_help();
47986470930SIngo Molnar printf("\n %s\n\n", perf_more_info_string);
48086470930SIngo Molnar return 0;
48186470930SIngo Molnar }
48286470930SIngo Molnar
483cc584821SDavid Ahern switch (help_format) {
48486470930SIngo Molnar case HELP_FORMAT_MAN:
48586470930SIngo Molnar rc = show_man_page(argv[0]);
486cc584821SDavid Ahern break;
48786470930SIngo Molnar case HELP_FORMAT_INFO:
48886470930SIngo Molnar rc = show_info_page(argv[0]);
489cc584821SDavid Ahern break;
490cc584821SDavid Ahern case HELP_FORMAT_WEB:
491cc584821SDavid Ahern rc = show_html_page(argv[0]);
492cc584821SDavid Ahern break;
49383a0944fSIngo Molnar case HELP_FORMAT_NONE:
494cc584821SDavid Ahern /* fall-through */
49586470930SIngo Molnar default:
49686470930SIngo Molnar rc = -1;
49786470930SIngo Molnar break;
498cc584821SDavid Ahern }
49986470930SIngo Molnar
500 return rc;
501 }
502