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