Loading...
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * builtin-help.c
4 *
5 * Builtin help command
6 */
7#include "util/cache.h"
8#include "util/config.h"
9#include "util/strbuf.h"
10#include "builtin.h"
11#include <subcmd/exec-cmd.h>
12#include "common-cmds.h"
13#include <subcmd/parse-options.h>
14#include <subcmd/run-command.h>
15#include <subcmd/help.h>
16#include "util/debug.h"
17#include "util/util.h"
18#include <linux/kernel.h>
19#include <linux/string.h>
20#include <linux/zalloc.h>
21#include <errno.h>
22#include <limits.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <sys/types.h>
27#include <sys/stat.h>
28#include <unistd.h>
29
30static struct man_viewer_list {
31 struct man_viewer_list *next;
32 char name[0];
33} *man_viewer_list;
34
35static struct man_viewer_info_list {
36 struct man_viewer_info_list *next;
37 const char *info;
38 char name[0];
39} *man_viewer_info_list;
40
41enum help_format {
42 HELP_FORMAT_NONE,
43 HELP_FORMAT_MAN,
44 HELP_FORMAT_INFO,
45 HELP_FORMAT_WEB,
46};
47
48static enum help_format parse_help_format(const char *format)
49{
50 if (!strcmp(format, "man"))
51 return HELP_FORMAT_MAN;
52 if (!strcmp(format, "info"))
53 return HELP_FORMAT_INFO;
54 if (!strcmp(format, "web") || !strcmp(format, "html"))
55 return HELP_FORMAT_WEB;
56
57 pr_err("unrecognized help format '%s'", format);
58 return HELP_FORMAT_NONE;
59}
60
61static const char *get_man_viewer_info(const char *name)
62{
63 struct man_viewer_info_list *viewer;
64
65 for (viewer = man_viewer_info_list; viewer; viewer = viewer->next) {
66 if (!strcasecmp(name, viewer->name))
67 return viewer->info;
68 }
69 return NULL;
70}
71
72static int check_emacsclient_version(void)
73{
74 struct strbuf buffer = STRBUF_INIT;
75 struct child_process ec_process;
76 const char *argv_ec[] = { "emacsclient", "--version", NULL };
77 int version;
78 int ret = -1;
79
80 /* emacsclient prints its version number on stderr */
81 memset(&ec_process, 0, sizeof(ec_process));
82 ec_process.argv = argv_ec;
83 ec_process.err = -1;
84 ec_process.stdout_to_stderr = 1;
85 if (start_command(&ec_process)) {
86 fprintf(stderr, "Failed to start emacsclient.\n");
87 return -1;
88 }
89 if (strbuf_read(&buffer, ec_process.err, 20) < 0) {
90 fprintf(stderr, "Failed to read emacsclient version\n");
91 goto out;
92 }
93 close(ec_process.err);
94
95 /*
96 * Don't bother checking return value, because "emacsclient --version"
97 * seems to always exits with code 1.
98 */
99 finish_command(&ec_process);
100
101 if (!strstarts(buffer.buf, "emacsclient")) {
102 fprintf(stderr, "Failed to parse emacsclient version.\n");
103 goto out;
104 }
105
106 version = atoi(buffer.buf + strlen("emacsclient"));
107
108 if (version < 22) {
109 fprintf(stderr,
110 "emacsclient version '%d' too old (< 22).\n",
111 version);
112 } else
113 ret = 0;
114out:
115 strbuf_release(&buffer);
116 return ret;
117}
118
119static void exec_failed(const char *cmd)
120{
121 char sbuf[STRERR_BUFSIZE];
122 pr_warning("failed to exec '%s': %s", cmd, str_error_r(errno, sbuf, sizeof(sbuf)));
123}
124
125static void exec_woman_emacs(const char *path, const char *page)
126{
127 if (!check_emacsclient_version()) {
128 /* This works only with emacsclient version >= 22. */
129 char *man_page;
130
131 if (!path)
132 path = "emacsclient";
133 if (asprintf(&man_page, "(woman \"%s\")", page) > 0) {
134 execlp(path, "emacsclient", "-e", man_page, NULL);
135 free(man_page);
136 }
137 exec_failed(path);
138 }
139}
140
141static void exec_man_konqueror(const char *path, const char *page)
142{
143 const char *display = getenv("DISPLAY");
144
145 if (display && *display) {
146 char *man_page;
147 const char *filename = "kfmclient";
148
149 /* It's simpler to launch konqueror using kfmclient. */
150 if (path) {
151 const char *file = strrchr(path, '/');
152 if (file && !strcmp(file + 1, "konqueror")) {
153 char *new = strdup(path);
154 char *dest = strrchr(new, '/');
155
156 /* strlen("konqueror") == strlen("kfmclient") */
157 strcpy(dest + 1, "kfmclient");
158 path = new;
159 }
160 if (file)
161 filename = file;
162 } else
163 path = "kfmclient";
164 if (asprintf(&man_page, "man:%s(1)", page) > 0) {
165 execlp(path, filename, "newTab", man_page, NULL);
166 free(man_page);
167 }
168 exec_failed(path);
169 }
170}
171
172static void exec_man_man(const char *path, const char *page)
173{
174 if (!path)
175 path = "man";
176 execlp(path, "man", page, NULL);
177 exec_failed(path);
178}
179
180static void exec_man_cmd(const char *cmd, const char *page)
181{
182 char *shell_cmd;
183
184 if (asprintf(&shell_cmd, "%s %s", cmd, page) > 0) {
185 execl("/bin/sh", "sh", "-c", shell_cmd, NULL);
186 free(shell_cmd);
187 }
188 exec_failed(cmd);
189}
190
191static void add_man_viewer(const char *name)
192{
193 struct man_viewer_list **p = &man_viewer_list;
194 size_t len = strlen(name);
195
196 while (*p)
197 p = &((*p)->next);
198 *p = zalloc(sizeof(**p) + len + 1);
199 strcpy((*p)->name, name);
200}
201
202static int supported_man_viewer(const char *name, size_t len)
203{
204 return (!strncasecmp("man", name, len) ||
205 !strncasecmp("woman", name, len) ||
206 !strncasecmp("konqueror", name, len));
207}
208
209static void do_add_man_viewer_info(const char *name,
210 size_t len,
211 const char *value)
212{
213 struct man_viewer_info_list *new = zalloc(sizeof(*new) + len + 1);
214
215 strncpy(new->name, name, len);
216 new->info = strdup(value);
217 new->next = man_viewer_info_list;
218 man_viewer_info_list = new;
219}
220
221static void unsupported_man_viewer(const char *name, const char *var)
222{
223 pr_warning("'%s': path for unsupported man viewer.\n"
224 "Please consider using 'man.<tool>.%s' instead.", name, var);
225}
226
227static int add_man_viewer_path(const char *name,
228 size_t len,
229 const char *value)
230{
231 if (supported_man_viewer(name, len))
232 do_add_man_viewer_info(name, len, value);
233 else
234 unsupported_man_viewer(name, "cmd");
235
236 return 0;
237}
238
239static int add_man_viewer_cmd(const char *name,
240 size_t len,
241 const char *value)
242{
243 if (supported_man_viewer(name, len))
244 unsupported_man_viewer(name, "path");
245 else
246 do_add_man_viewer_info(name, len, value);
247
248 return 0;
249}
250
251static int add_man_viewer_info(const char *var, const char *value)
252{
253 const char *name = var + 4;
254 const char *subkey = strrchr(name, '.');
255
256 if (!subkey) {
257 pr_err("Config with no key for man viewer: %s", name);
258 return -1;
259 }
260
261 if (!strcmp(subkey, ".path")) {
262 if (!value)
263 return config_error_nonbool(var);
264 return add_man_viewer_path(name, subkey - name, value);
265 }
266 if (!strcmp(subkey, ".cmd")) {
267 if (!value)
268 return config_error_nonbool(var);
269 return add_man_viewer_cmd(name, subkey - name, value);
270 }
271
272 pr_warning("'%s': unsupported man viewer sub key.", subkey);
273 return 0;
274}
275
276static int perf_help_config(const char *var, const char *value, void *cb)
277{
278 enum help_format *help_formatp = cb;
279
280 if (!strcmp(var, "help.format")) {
281 if (!value)
282 return config_error_nonbool(var);
283 *help_formatp = parse_help_format(value);
284 if (*help_formatp == HELP_FORMAT_NONE)
285 return -1;
286 return 0;
287 }
288 if (!strcmp(var, "man.viewer")) {
289 if (!value)
290 return config_error_nonbool(var);
291 add_man_viewer(value);
292 return 0;
293 }
294 if (strstarts(var, "man."))
295 return add_man_viewer_info(var, value);
296
297 return 0;
298}
299
300static struct cmdnames main_cmds, other_cmds;
301
302void list_common_cmds_help(void)
303{
304 unsigned int i, longest = 0;
305
306 for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
307 if (longest < strlen(common_cmds[i].name))
308 longest = strlen(common_cmds[i].name);
309 }
310
311 puts(" The most commonly used perf commands are:");
312 for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
313 printf(" %-*s ", longest, common_cmds[i].name);
314 puts(common_cmds[i].help);
315 }
316}
317
318static const char *cmd_to_page(const char *perf_cmd)
319{
320 char *s;
321
322 if (!perf_cmd)
323 return "perf";
324 else if (strstarts(perf_cmd, "perf"))
325 return perf_cmd;
326
327 return asprintf(&s, "perf-%s", perf_cmd) < 0 ? NULL : s;
328}
329
330static void setup_man_path(void)
331{
332 char *new_path;
333 const char *old_path = getenv("MANPATH");
334
335 /* We should always put ':' after our path. If there is no
336 * old_path, the ':' at the end will let 'man' to try
337 * system-wide paths after ours to find the manual page. If
338 * there is old_path, we need ':' as delimiter. */
339 if (asprintf(&new_path, "%s:%s", system_path(PERF_MAN_PATH), old_path ?: "") > 0) {
340 setenv("MANPATH", new_path, 1);
341 free(new_path);
342 } else {
343 pr_err("Unable to setup man path");
344 }
345}
346
347static void exec_viewer(const char *name, const char *page)
348{
349 const char *info = get_man_viewer_info(name);
350
351 if (!strcasecmp(name, "man"))
352 exec_man_man(info, page);
353 else if (!strcasecmp(name, "woman"))
354 exec_woman_emacs(info, page);
355 else if (!strcasecmp(name, "konqueror"))
356 exec_man_konqueror(info, page);
357 else if (info)
358 exec_man_cmd(info, page);
359 else
360 pr_warning("'%s': unknown man viewer.", name);
361}
362
363static int show_man_page(const char *perf_cmd)
364{
365 struct man_viewer_list *viewer;
366 const char *page = cmd_to_page(perf_cmd);
367 const char *fallback = getenv("PERF_MAN_VIEWER");
368
369 setup_man_path();
370 for (viewer = man_viewer_list; viewer; viewer = viewer->next)
371 exec_viewer(viewer->name, page); /* will return when unable */
372
373 if (fallback)
374 exec_viewer(fallback, page);
375 exec_viewer("man", page);
376
377 pr_err("no man viewer handled the request");
378 return -1;
379}
380
381static int show_info_page(const char *perf_cmd)
382{
383 const char *page = cmd_to_page(perf_cmd);
384 setenv("INFOPATH", system_path(PERF_INFO_PATH), 1);
385 execlp("info", "info", "perfman", page, NULL);
386 return -1;
387}
388
389static int get_html_page_path(char **page_path, const char *page)
390{
391 struct stat st;
392 const char *html_path = system_path(PERF_HTML_PATH);
393 char path[PATH_MAX];
394
395 /* Check that we have a perf documentation directory. */
396 if (stat(mkpath(path, sizeof(path), "%s/perf.html", html_path), &st)
397 || !S_ISREG(st.st_mode)) {
398 pr_err("'%s': not a documentation directory.", html_path);
399 return -1;
400 }
401
402 return asprintf(page_path, "%s/%s.html", html_path, page);
403}
404
405/*
406 * If open_html is not defined in a platform-specific way (see for
407 * example compat/mingw.h), we use the script web--browse to display
408 * HTML.
409 */
410#ifndef open_html
411static void open_html(const char *path)
412{
413 execl_cmd("web--browse", "-c", "help.browser", path, NULL);
414}
415#endif
416
417static int show_html_page(const char *perf_cmd)
418{
419 const char *page = cmd_to_page(perf_cmd);
420 char *page_path; /* it leaks but we exec below */
421
422 if (get_html_page_path(&page_path, page) < 0)
423 return -1;
424
425 open_html(page_path);
426
427 return 0;
428}
429
430int cmd_help(int argc, const char **argv)
431{
432 bool show_all = false;
433 enum help_format help_format = HELP_FORMAT_MAN;
434 struct option builtin_help_options[] = {
435 OPT_BOOLEAN('a', "all", &show_all, "print all available commands"),
436 OPT_SET_UINT('m', "man", &help_format, "show man page", HELP_FORMAT_MAN),
437 OPT_SET_UINT('w', "web", &help_format, "show manual in web browser",
438 HELP_FORMAT_WEB),
439 OPT_SET_UINT('i', "info", &help_format, "show info page",
440 HELP_FORMAT_INFO),
441 OPT_END(),
442 };
443 const char * const builtin_help_subcommands[] = {
444 "buildid-cache", "buildid-list", "diff", "evlist", "help", "list",
445 "record", "report", "bench", "stat", "timechart", "top", "annotate",
446 "script", "sched", "kallsyms", "kmem", "lock", "kvm", "test", "inject", "mem", "data",
447#ifdef HAVE_LIBELF_SUPPORT
448 "probe",
449#endif
450#if defined(HAVE_LIBAUDIT_SUPPORT) || defined(HAVE_SYSCALL_TABLE_SUPPORT)
451 "trace",
452#endif
453 NULL };
454 const char *builtin_help_usage[] = {
455 "perf help [--all] [--man|--web|--info] [command]",
456 NULL
457 };
458 int rc;
459
460 load_command_list("perf-", &main_cmds, &other_cmds);
461
462 rc = perf_config(perf_help_config, &help_format);
463 if (rc)
464 return rc;
465
466 argc = parse_options_subcommand(argc, argv, builtin_help_options,
467 builtin_help_subcommands, builtin_help_usage, 0);
468
469 if (show_all) {
470 printf("\n Usage: %s\n\n", perf_usage_string);
471 list_commands("perf commands", &main_cmds, &other_cmds);
472 printf(" %s\n\n", perf_more_info_string);
473 return 0;
474 }
475
476 if (!argv[0]) {
477 printf("\n usage: %s\n\n", perf_usage_string);
478 list_common_cmds_help();
479 printf("\n %s\n\n", perf_more_info_string);
480 return 0;
481 }
482
483 switch (help_format) {
484 case HELP_FORMAT_MAN:
485 rc = show_man_page(argv[0]);
486 break;
487 case HELP_FORMAT_INFO:
488 rc = show_info_page(argv[0]);
489 break;
490 case HELP_FORMAT_WEB:
491 rc = show_html_page(argv[0]);
492 break;
493 case HELP_FORMAT_NONE:
494 /* fall-through */
495 default:
496 rc = -1;
497 break;
498 }
499
500 return rc;
501}
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * builtin-help.c
4 *
5 * Builtin help command
6 */
7#include "perf.h"
8#include "util/config.h"
9#include "builtin.h"
10#include <subcmd/exec-cmd.h>
11#include "common-cmds.h"
12#include <subcmd/parse-options.h>
13#include <subcmd/run-command.h>
14#include <subcmd/help.h>
15#include "util/debug.h"
16#include <linux/kernel.h>
17#include <errno.h>
18#include <stdio.h>
19#include <sys/types.h>
20#include <sys/stat.h>
21#include <unistd.h>
22
23static struct man_viewer_list {
24 struct man_viewer_list *next;
25 char name[0];
26} *man_viewer_list;
27
28static struct man_viewer_info_list {
29 struct man_viewer_info_list *next;
30 const char *info;
31 char name[0];
32} *man_viewer_info_list;
33
34enum help_format {
35 HELP_FORMAT_NONE,
36 HELP_FORMAT_MAN,
37 HELP_FORMAT_INFO,
38 HELP_FORMAT_WEB,
39};
40
41static enum help_format parse_help_format(const char *format)
42{
43 if (!strcmp(format, "man"))
44 return HELP_FORMAT_MAN;
45 if (!strcmp(format, "info"))
46 return HELP_FORMAT_INFO;
47 if (!strcmp(format, "web") || !strcmp(format, "html"))
48 return HELP_FORMAT_WEB;
49
50 pr_err("unrecognized help format '%s'", format);
51 return HELP_FORMAT_NONE;
52}
53
54static const char *get_man_viewer_info(const char *name)
55{
56 struct man_viewer_info_list *viewer;
57
58 for (viewer = man_viewer_info_list; viewer; viewer = viewer->next) {
59 if (!strcasecmp(name, viewer->name))
60 return viewer->info;
61 }
62 return NULL;
63}
64
65static int check_emacsclient_version(void)
66{
67 struct strbuf buffer = STRBUF_INIT;
68 struct child_process ec_process;
69 const char *argv_ec[] = { "emacsclient", "--version", NULL };
70 int version;
71 int ret = -1;
72
73 /* emacsclient prints its version number on stderr */
74 memset(&ec_process, 0, sizeof(ec_process));
75 ec_process.argv = argv_ec;
76 ec_process.err = -1;
77 ec_process.stdout_to_stderr = 1;
78 if (start_command(&ec_process)) {
79 fprintf(stderr, "Failed to start emacsclient.\n");
80 return -1;
81 }
82 if (strbuf_read(&buffer, ec_process.err, 20) < 0) {
83 fprintf(stderr, "Failed to read emacsclient version\n");
84 goto out;
85 }
86 close(ec_process.err);
87
88 /*
89 * Don't bother checking return value, because "emacsclient --version"
90 * seems to always exits with code 1.
91 */
92 finish_command(&ec_process);
93
94 if (!strstarts(buffer.buf, "emacsclient")) {
95 fprintf(stderr, "Failed to parse emacsclient version.\n");
96 goto out;
97 }
98
99 version = atoi(buffer.buf + strlen("emacsclient"));
100
101 if (version < 22) {
102 fprintf(stderr,
103 "emacsclient version '%d' too old (< 22).\n",
104 version);
105 } else
106 ret = 0;
107out:
108 strbuf_release(&buffer);
109 return ret;
110}
111
112static void exec_failed(const char *cmd)
113{
114 char sbuf[STRERR_BUFSIZE];
115 pr_warning("failed to exec '%s': %s", cmd, str_error_r(errno, sbuf, sizeof(sbuf)));
116}
117
118static void exec_woman_emacs(const char *path, const char *page)
119{
120 if (!check_emacsclient_version()) {
121 /* This works only with emacsclient version >= 22. */
122 char *man_page;
123
124 if (!path)
125 path = "emacsclient";
126 if (asprintf(&man_page, "(woman \"%s\")", page) > 0) {
127 execlp(path, "emacsclient", "-e", man_page, NULL);
128 free(man_page);
129 }
130 exec_failed(path);
131 }
132}
133
134static void exec_man_konqueror(const char *path, const char *page)
135{
136 const char *display = getenv("DISPLAY");
137
138 if (display && *display) {
139 char *man_page;
140 const char *filename = "kfmclient";
141
142 /* It's simpler to launch konqueror using kfmclient. */
143 if (path) {
144 const char *file = strrchr(path, '/');
145 if (file && !strcmp(file + 1, "konqueror")) {
146 char *new = strdup(path);
147 char *dest = strrchr(new, '/');
148
149 /* strlen("konqueror") == strlen("kfmclient") */
150 strcpy(dest + 1, "kfmclient");
151 path = new;
152 }
153 if (file)
154 filename = file;
155 } else
156 path = "kfmclient";
157 if (asprintf(&man_page, "man:%s(1)", page) > 0) {
158 execlp(path, filename, "newTab", man_page, NULL);
159 free(man_page);
160 }
161 exec_failed(path);
162 }
163}
164
165static void exec_man_man(const char *path, const char *page)
166{
167 if (!path)
168 path = "man";
169 execlp(path, "man", page, NULL);
170 exec_failed(path);
171}
172
173static void exec_man_cmd(const char *cmd, const char *page)
174{
175 char *shell_cmd;
176
177 if (asprintf(&shell_cmd, "%s %s", cmd, page) > 0) {
178 execl("/bin/sh", "sh", "-c", shell_cmd, NULL);
179 free(shell_cmd);
180 }
181 exec_failed(cmd);
182}
183
184static void add_man_viewer(const char *name)
185{
186 struct man_viewer_list **p = &man_viewer_list;
187 size_t len = strlen(name);
188
189 while (*p)
190 p = &((*p)->next);
191 *p = zalloc(sizeof(**p) + len + 1);
192 strncpy((*p)->name, name, len);
193}
194
195static int supported_man_viewer(const char *name, size_t len)
196{
197 return (!strncasecmp("man", name, len) ||
198 !strncasecmp("woman", name, len) ||
199 !strncasecmp("konqueror", name, len));
200}
201
202static void do_add_man_viewer_info(const char *name,
203 size_t len,
204 const char *value)
205{
206 struct man_viewer_info_list *new = zalloc(sizeof(*new) + len + 1);
207
208 strncpy(new->name, name, len);
209 new->info = strdup(value);
210 new->next = man_viewer_info_list;
211 man_viewer_info_list = new;
212}
213
214static void unsupported_man_viewer(const char *name, const char *var)
215{
216 pr_warning("'%s': path for unsupported man viewer.\n"
217 "Please consider using 'man.<tool>.%s' instead.", name, var);
218}
219
220static int add_man_viewer_path(const char *name,
221 size_t len,
222 const char *value)
223{
224 if (supported_man_viewer(name, len))
225 do_add_man_viewer_info(name, len, value);
226 else
227 unsupported_man_viewer(name, "cmd");
228
229 return 0;
230}
231
232static int add_man_viewer_cmd(const char *name,
233 size_t len,
234 const char *value)
235{
236 if (supported_man_viewer(name, len))
237 unsupported_man_viewer(name, "path");
238 else
239 do_add_man_viewer_info(name, len, value);
240
241 return 0;
242}
243
244static int add_man_viewer_info(const char *var, const char *value)
245{
246 const char *name = var + 4;
247 const char *subkey = strrchr(name, '.');
248
249 if (!subkey) {
250 pr_err("Config with no key for man viewer: %s", name);
251 return -1;
252 }
253
254 if (!strcmp(subkey, ".path")) {
255 if (!value)
256 return config_error_nonbool(var);
257 return add_man_viewer_path(name, subkey - name, value);
258 }
259 if (!strcmp(subkey, ".cmd")) {
260 if (!value)
261 return config_error_nonbool(var);
262 return add_man_viewer_cmd(name, subkey - name, value);
263 }
264
265 pr_warning("'%s': unsupported man viewer sub key.", subkey);
266 return 0;
267}
268
269static int perf_help_config(const char *var, const char *value, void *cb)
270{
271 enum help_format *help_formatp = cb;
272
273 if (!strcmp(var, "help.format")) {
274 if (!value)
275 return config_error_nonbool(var);
276 *help_formatp = parse_help_format(value);
277 if (*help_formatp == HELP_FORMAT_NONE)
278 return -1;
279 return 0;
280 }
281 if (!strcmp(var, "man.viewer")) {
282 if (!value)
283 return config_error_nonbool(var);
284 add_man_viewer(value);
285 return 0;
286 }
287 if (strstarts(var, "man."))
288 return add_man_viewer_info(var, value);
289
290 return 0;
291}
292
293static struct cmdnames main_cmds, other_cmds;
294
295void list_common_cmds_help(void)
296{
297 unsigned int i, longest = 0;
298
299 for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
300 if (longest < strlen(common_cmds[i].name))
301 longest = strlen(common_cmds[i].name);
302 }
303
304 puts(" The most commonly used perf commands are:");
305 for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
306 printf(" %-*s ", longest, common_cmds[i].name);
307 puts(common_cmds[i].help);
308 }
309}
310
311static const char *cmd_to_page(const char *perf_cmd)
312{
313 char *s;
314
315 if (!perf_cmd)
316 return "perf";
317 else if (strstarts(perf_cmd, "perf"))
318 return perf_cmd;
319
320 return asprintf(&s, "perf-%s", perf_cmd) < 0 ? NULL : s;
321}
322
323static void setup_man_path(void)
324{
325 char *new_path;
326 const char *old_path = getenv("MANPATH");
327
328 /* We should always put ':' after our path. If there is no
329 * old_path, the ':' at the end will let 'man' to try
330 * system-wide paths after ours to find the manual page. If
331 * there is old_path, we need ':' as delimiter. */
332 if (asprintf(&new_path, "%s:%s", system_path(PERF_MAN_PATH), old_path ?: "") > 0) {
333 setenv("MANPATH", new_path, 1);
334 free(new_path);
335 } else {
336 pr_err("Unable to setup man path");
337 }
338}
339
340static void exec_viewer(const char *name, const char *page)
341{
342 const char *info = get_man_viewer_info(name);
343
344 if (!strcasecmp(name, "man"))
345 exec_man_man(info, page);
346 else if (!strcasecmp(name, "woman"))
347 exec_woman_emacs(info, page);
348 else if (!strcasecmp(name, "konqueror"))
349 exec_man_konqueror(info, page);
350 else if (info)
351 exec_man_cmd(info, page);
352 else
353 pr_warning("'%s': unknown man viewer.", name);
354}
355
356static int show_man_page(const char *perf_cmd)
357{
358 struct man_viewer_list *viewer;
359 const char *page = cmd_to_page(perf_cmd);
360 const char *fallback = getenv("PERF_MAN_VIEWER");
361
362 setup_man_path();
363 for (viewer = man_viewer_list; viewer; viewer = viewer->next)
364 exec_viewer(viewer->name, page); /* will return when unable */
365
366 if (fallback)
367 exec_viewer(fallback, page);
368 exec_viewer("man", page);
369
370 pr_err("no man viewer handled the request");
371 return -1;
372}
373
374static int show_info_page(const char *perf_cmd)
375{
376 const char *page = cmd_to_page(perf_cmd);
377 setenv("INFOPATH", system_path(PERF_INFO_PATH), 1);
378 execlp("info", "info", "perfman", page, NULL);
379 return -1;
380}
381
382static int get_html_page_path(char **page_path, const char *page)
383{
384 struct stat st;
385 const char *html_path = system_path(PERF_HTML_PATH);
386
387 /* Check that we have a perf documentation directory. */
388 if (stat(mkpath("%s/perf.html", html_path), &st)
389 || !S_ISREG(st.st_mode)) {
390 pr_err("'%s': not a documentation directory.", html_path);
391 return -1;
392 }
393
394 return asprintf(page_path, "%s/%s.html", html_path, page);
395}
396
397/*
398 * If open_html is not defined in a platform-specific way (see for
399 * example compat/mingw.h), we use the script web--browse to display
400 * HTML.
401 */
402#ifndef open_html
403static void open_html(const char *path)
404{
405 execl_cmd("web--browse", "-c", "help.browser", path, NULL);
406}
407#endif
408
409static int show_html_page(const char *perf_cmd)
410{
411 const char *page = cmd_to_page(perf_cmd);
412 char *page_path; /* it leaks but we exec bellow */
413
414 if (get_html_page_path(&page_path, page) < 0)
415 return -1;
416
417 open_html(page_path);
418
419 return 0;
420}
421
422int cmd_help(int argc, const char **argv)
423{
424 bool show_all = false;
425 enum help_format help_format = HELP_FORMAT_MAN;
426 struct option builtin_help_options[] = {
427 OPT_BOOLEAN('a', "all", &show_all, "print all available commands"),
428 OPT_SET_UINT('m', "man", &help_format, "show man page", HELP_FORMAT_MAN),
429 OPT_SET_UINT('w', "web", &help_format, "show manual in web browser",
430 HELP_FORMAT_WEB),
431 OPT_SET_UINT('i', "info", &help_format, "show info page",
432 HELP_FORMAT_INFO),
433 OPT_END(),
434 };
435 const char * const builtin_help_subcommands[] = {
436 "buildid-cache", "buildid-list", "diff", "evlist", "help", "list",
437 "record", "report", "bench", "stat", "timechart", "top", "annotate",
438 "script", "sched", "kallsyms", "kmem", "lock", "kvm", "test", "inject", "mem", "data",
439#ifdef HAVE_LIBELF_SUPPORT
440 "probe",
441#endif
442#if defined(HAVE_LIBAUDIT_SUPPORT) || defined(HAVE_SYSCALL_TABLE_SUPPORT)
443 "trace",
444#endif
445 NULL };
446 const char *builtin_help_usage[] = {
447 "perf help [--all] [--man|--web|--info] [command]",
448 NULL
449 };
450 int rc;
451
452 load_command_list("perf-", &main_cmds, &other_cmds);
453
454 rc = perf_config(perf_help_config, &help_format);
455 if (rc)
456 return rc;
457
458 argc = parse_options_subcommand(argc, argv, builtin_help_options,
459 builtin_help_subcommands, builtin_help_usage, 0);
460
461 if (show_all) {
462 printf("\n Usage: %s\n\n", perf_usage_string);
463 list_commands("perf commands", &main_cmds, &other_cmds);
464 printf(" %s\n\n", perf_more_info_string);
465 return 0;
466 }
467
468 if (!argv[0]) {
469 printf("\n usage: %s\n\n", perf_usage_string);
470 list_common_cmds_help();
471 printf("\n %s\n\n", perf_more_info_string);
472 return 0;
473 }
474
475 switch (help_format) {
476 case HELP_FORMAT_MAN:
477 rc = show_man_page(argv[0]);
478 break;
479 case HELP_FORMAT_INFO:
480 rc = show_info_page(argv[0]);
481 break;
482 case HELP_FORMAT_WEB:
483 rc = show_html_page(argv[0]);
484 break;
485 case HELP_FORMAT_NONE:
486 /* fall-through */
487 default:
488 rc = -1;
489 break;
490 }
491
492 return rc;
493}