blob: 25d20628212ed4691ad8ca3cc3c432e5dbffcd13 [file] [log] [blame]
Ingo Molnar07800602009-04-20 15:00:56 +02001/*
2 * builtin-help.c
3 *
4 * Builtin help command
5 */
Ingo Molnarf37a2912009-07-01 12:37:06 +02006#include "perf.h"
Ingo Molnar148be2c2009-04-27 08:02:14 +02007#include "util/cache.h"
Ingo Molnar07800602009-04-20 15:00:56 +02008#include "builtin.h"
Ingo Molnar148be2c2009-04-27 08:02:14 +02009#include "util/exec_cmd.h"
Ingo Molnar07800602009-04-20 15:00:56 +020010#include "common-cmds.h"
Ingo Molnar148be2c2009-04-27 08:02:14 +020011#include "util/parse-options.h"
12#include "util/run-command.h"
13#include "util/help.h"
Jiri Olsa84f5d362014-07-14 23:46:48 +020014#include "util/debug.h"
Ingo Molnar07800602009-04-20 15:00:56 +020015
16static struct man_viewer_list {
17 struct man_viewer_list *next;
18 char name[FLEX_ARRAY];
19} *man_viewer_list;
20
21static struct man_viewer_info_list {
22 struct man_viewer_info_list *next;
23 const char *info;
24 char name[FLEX_ARRAY];
25} *man_viewer_info_list;
26
27enum help_format {
David Aherncc584822012-08-26 12:24:45 -060028 HELP_FORMAT_NONE,
Ingo Molnar07800602009-04-20 15:00:56 +020029 HELP_FORMAT_MAN,
30 HELP_FORMAT_INFO,
31 HELP_FORMAT_WEB,
32};
33
Ingo Molnar07800602009-04-20 15:00:56 +020034static enum help_format parse_help_format(const char *format)
35{
36 if (!strcmp(format, "man"))
37 return HELP_FORMAT_MAN;
38 if (!strcmp(format, "info"))
39 return HELP_FORMAT_INFO;
40 if (!strcmp(format, "web") || !strcmp(format, "html"))
41 return HELP_FORMAT_WEB;
David Aherncc584822012-08-26 12:24:45 -060042
43 pr_err("unrecognized help format '%s'", format);
44 return HELP_FORMAT_NONE;
Ingo Molnar07800602009-04-20 15:00:56 +020045}
46
47static const char *get_man_viewer_info(const char *name)
48{
49 struct man_viewer_info_list *viewer;
50
Thiago Farinac1e53012009-11-03 08:28:45 -050051 for (viewer = man_viewer_info_list; viewer; viewer = viewer->next) {
Ingo Molnar07800602009-04-20 15:00:56 +020052 if (!strcasecmp(name, viewer->name))
53 return viewer->info;
54 }
55 return NULL;
56}
57
58static int check_emacsclient_version(void)
59{
60 struct strbuf buffer = STRBUF_INIT;
61 struct child_process ec_process;
62 const char *argv_ec[] = { "emacsclient", "--version", NULL };
63 int version;
64
65 /* emacsclient prints its version number on stderr */
66 memset(&ec_process, 0, sizeof(ec_process));
67 ec_process.argv = argv_ec;
68 ec_process.err = -1;
69 ec_process.stdout_to_stderr = 1;
70 if (start_command(&ec_process)) {
71 fprintf(stderr, "Failed to start emacsclient.\n");
72 return -1;
73 }
74 strbuf_read(&buffer, ec_process.err, 20);
75 close(ec_process.err);
76
77 /*
78 * Don't bother checking return value, because "emacsclient --version"
79 * seems to always exits with code 1.
80 */
81 finish_command(&ec_process);
82
83 if (prefixcmp(buffer.buf, "emacsclient")) {
84 fprintf(stderr, "Failed to parse emacsclient version.\n");
85 strbuf_release(&buffer);
86 return -1;
87 }
88
89 strbuf_remove(&buffer, 0, strlen("emacsclient"));
90 version = atoi(buffer.buf);
91
92 if (version < 22) {
93 fprintf(stderr,
94 "emacsclient version '%d' too old (< 22).\n",
95 version);
96 strbuf_release(&buffer);
97 return -1;
98 }
99
100 strbuf_release(&buffer);
101 return 0;
102}
103
Thiago Farinac1e53012009-11-03 08:28:45 -0500104static void exec_woman_emacs(const char *path, const char *page)
Ingo Molnar07800602009-04-20 15:00:56 +0200105{
Masami Hiramatsuede395d2014-08-14 02:22:53 +0000106 char sbuf[STRERR_BUFSIZE];
107
Ingo Molnar07800602009-04-20 15:00:56 +0200108 if (!check_emacsclient_version()) {
109 /* This works only with emacsclient version >= 22. */
110 struct strbuf man_page = STRBUF_INIT;
111
112 if (!path)
113 path = "emacsclient";
114 strbuf_addf(&man_page, "(woman \"%s\")", page);
115 execlp(path, "emacsclient", "-e", man_page.buf, NULL);
Masami Hiramatsuede395d2014-08-14 02:22:53 +0000116 warning("failed to exec '%s': %s", path,
117 strerror_r(errno, sbuf, sizeof(sbuf)));
Ingo Molnar07800602009-04-20 15:00:56 +0200118 }
119}
120
Thiago Farinac1e53012009-11-03 08:28:45 -0500121static void exec_man_konqueror(const char *path, const char *page)
Ingo Molnar07800602009-04-20 15:00:56 +0200122{
123 const char *display = getenv("DISPLAY");
Masami Hiramatsuede395d2014-08-14 02:22:53 +0000124
Ingo Molnar07800602009-04-20 15:00:56 +0200125 if (display && *display) {
126 struct strbuf man_page = STRBUF_INIT;
127 const char *filename = "kfmclient";
Masami Hiramatsuede395d2014-08-14 02:22:53 +0000128 char sbuf[STRERR_BUFSIZE];
Ingo Molnar07800602009-04-20 15:00:56 +0200129
130 /* It's simpler to launch konqueror using kfmclient. */
131 if (path) {
132 const char *file = strrchr(path, '/');
133 if (file && !strcmp(file + 1, "konqueror")) {
134 char *new = strdup(path);
135 char *dest = strrchr(new, '/');
136
137 /* strlen("konqueror") == strlen("kfmclient") */
138 strcpy(dest + 1, "kfmclient");
139 path = new;
140 }
141 if (file)
142 filename = file;
143 } else
144 path = "kfmclient";
145 strbuf_addf(&man_page, "man:%s(1)", page);
146 execlp(path, filename, "newTab", man_page.buf, NULL);
Masami Hiramatsuede395d2014-08-14 02:22:53 +0000147 warning("failed to exec '%s': %s", path,
148 strerror_r(errno, sbuf, sizeof(sbuf)));
Ingo Molnar07800602009-04-20 15:00:56 +0200149 }
150}
151
Thiago Farinac1e53012009-11-03 08:28:45 -0500152static void exec_man_man(const char *path, const char *page)
Ingo Molnar07800602009-04-20 15:00:56 +0200153{
Masami Hiramatsuede395d2014-08-14 02:22:53 +0000154 char sbuf[STRERR_BUFSIZE];
155
Ingo Molnar07800602009-04-20 15:00:56 +0200156 if (!path)
157 path = "man";
158 execlp(path, "man", page, NULL);
Masami Hiramatsuede395d2014-08-14 02:22:53 +0000159 warning("failed to exec '%s': %s", path,
160 strerror_r(errno, sbuf, sizeof(sbuf)));
Ingo Molnar07800602009-04-20 15:00:56 +0200161}
162
163static void exec_man_cmd(const char *cmd, const char *page)
164{
165 struct strbuf shell_cmd = STRBUF_INIT;
Masami Hiramatsuede395d2014-08-14 02:22:53 +0000166 char sbuf[STRERR_BUFSIZE];
167
Ingo Molnar07800602009-04-20 15:00:56 +0200168 strbuf_addf(&shell_cmd, "%s %s", cmd, page);
169 execl("/bin/sh", "sh", "-c", shell_cmd.buf, NULL);
Masami Hiramatsuede395d2014-08-14 02:22:53 +0000170 warning("failed to exec '%s': %s", cmd,
171 strerror_r(errno, sbuf, sizeof(sbuf)));
Ingo Molnar07800602009-04-20 15:00:56 +0200172}
173
174static void add_man_viewer(const char *name)
175{
176 struct man_viewer_list **p = &man_viewer_list;
177 size_t len = strlen(name);
178
179 while (*p)
180 p = &((*p)->next);
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200181 *p = zalloc(sizeof(**p) + len + 1);
Ingo Molnar07800602009-04-20 15:00:56 +0200182 strncpy((*p)->name, name, len);
183}
184
185static int supported_man_viewer(const char *name, size_t len)
186{
187 return (!strncasecmp("man", name, len) ||
188 !strncasecmp("woman", name, len) ||
189 !strncasecmp("konqueror", name, len));
190}
191
192static void do_add_man_viewer_info(const char *name,
193 size_t len,
194 const char *value)
195{
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200196 struct man_viewer_info_list *new = zalloc(sizeof(*new) + len + 1);
Ingo Molnar07800602009-04-20 15:00:56 +0200197
198 strncpy(new->name, name, len);
199 new->info = strdup(value);
200 new->next = man_viewer_info_list;
201 man_viewer_info_list = new;
202}
203
204static int add_man_viewer_path(const char *name,
205 size_t len,
206 const char *value)
207{
208 if (supported_man_viewer(name, len))
209 do_add_man_viewer_info(name, len, value);
210 else
211 warning("'%s': path for unsupported man viewer.\n"
212 "Please consider using 'man.<tool>.cmd' instead.",
213 name);
214
215 return 0;
216}
217
218static int add_man_viewer_cmd(const char *name,
219 size_t len,
220 const char *value)
221{
222 if (supported_man_viewer(name, len))
223 warning("'%s': cmd for supported man viewer.\n"
224 "Please consider using 'man.<tool>.path' instead.",
225 name);
226 else
227 do_add_man_viewer_info(name, len, value);
228
229 return 0;
230}
231
232static int add_man_viewer_info(const char *var, const char *value)
233{
234 const char *name = var + 4;
235 const char *subkey = strrchr(name, '.');
236
237 if (!subkey)
238 return error("Config with no key for man viewer: %s", name);
239
240 if (!strcmp(subkey, ".path")) {
241 if (!value)
242 return config_error_nonbool(var);
243 return add_man_viewer_path(name, subkey - name, value);
244 }
245 if (!strcmp(subkey, ".cmd")) {
246 if (!value)
247 return config_error_nonbool(var);
248 return add_man_viewer_cmd(name, subkey - name, value);
249 }
250
251 warning("'%s': unsupported man viewer sub key.", subkey);
252 return 0;
253}
254
255static int perf_help_config(const char *var, const char *value, void *cb)
256{
Arnaldo Carvalho de Melo2bae1d12012-10-01 15:20:58 -0300257 enum help_format *help_formatp = cb;
258
Ingo Molnar07800602009-04-20 15:00:56 +0200259 if (!strcmp(var, "help.format")) {
260 if (!value)
261 return config_error_nonbool(var);
Arnaldo Carvalho de Melo2bae1d12012-10-01 15:20:58 -0300262 *help_formatp = parse_help_format(value);
263 if (*help_formatp == HELP_FORMAT_NONE)
David Aherncc584822012-08-26 12:24:45 -0600264 return -1;
Ingo Molnar07800602009-04-20 15:00:56 +0200265 return 0;
266 }
267 if (!strcmp(var, "man.viewer")) {
268 if (!value)
269 return config_error_nonbool(var);
270 add_man_viewer(value);
271 return 0;
272 }
273 if (!prefixcmp(var, "man."))
274 return add_man_viewer_info(var, value);
275
276 return perf_default_config(var, value, cb);
277}
278
279static struct cmdnames main_cmds, other_cmds;
280
281void list_common_cmds_help(void)
282{
Ingo Molnarf37a2912009-07-01 12:37:06 +0200283 unsigned int i, longest = 0;
Ingo Molnar07800602009-04-20 15:00:56 +0200284
285 for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
286 if (longest < strlen(common_cmds[i].name))
287 longest = strlen(common_cmds[i].name);
288 }
289
Ingo Molnar502fc5c2009-03-13 03:20:49 +0100290 puts(" The most commonly used perf commands are:");
Ingo Molnar07800602009-04-20 15:00:56 +0200291 for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
Amerigo Wang3912f2a2009-12-14 03:23:56 -0500292 printf(" %-*s ", longest, common_cmds[i].name);
Ingo Molnar07800602009-04-20 15:00:56 +0200293 puts(common_cmds[i].help);
294 }
295}
296
297static int is_perf_command(const char *s)
298{
299 return is_in_cmdlist(&main_cmds, s) ||
300 is_in_cmdlist(&other_cmds, s);
301}
302
303static const char *prepend(const char *prefix, const char *cmd)
304{
305 size_t pre_len = strlen(prefix);
306 size_t cmd_len = strlen(cmd);
307 char *p = malloc(pre_len + cmd_len + 1);
308 memcpy(p, prefix, pre_len);
309 strcpy(p + pre_len, cmd);
310 return p;
311}
312
313static const char *cmd_to_page(const char *perf_cmd)
314{
315 if (!perf_cmd)
316 return "perf";
317 else if (!prefixcmp(perf_cmd, "perf"))
318 return perf_cmd;
Ingo Molnar07800602009-04-20 15:00:56 +0200319 else
Ingo Molnarc1c23652009-05-30 12:38:51 +0200320 return prepend("perf-", perf_cmd);
Ingo Molnar07800602009-04-20 15:00:56 +0200321}
322
323static void setup_man_path(void)
324{
325 struct strbuf new_path = STRBUF_INIT;
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 strbuf_addstr(&new_path, system_path(PERF_MAN_PATH));
333 strbuf_addch(&new_path, ':');
334 if (old_path)
335 strbuf_addstr(&new_path, old_path);
336
337 setenv("MANPATH", new_path.buf, 1);
338
339 strbuf_release(&new_path);
340}
341
342static void exec_viewer(const char *name, const char *page)
343{
344 const char *info = get_man_viewer_info(name);
345
346 if (!strcasecmp(name, "man"))
347 exec_man_man(info, page);
348 else if (!strcasecmp(name, "woman"))
349 exec_woman_emacs(info, page);
350 else if (!strcasecmp(name, "konqueror"))
351 exec_man_konqueror(info, page);
352 else if (info)
353 exec_man_cmd(info, page);
354 else
355 warning("'%s': unknown man viewer.", name);
356}
357
David Aherncc584822012-08-26 12:24:45 -0600358static int show_man_page(const char *perf_cmd)
Ingo Molnar07800602009-04-20 15:00:56 +0200359{
360 struct man_viewer_list *viewer;
361 const char *page = cmd_to_page(perf_cmd);
362 const char *fallback = getenv("PERF_MAN_VIEWER");
363
364 setup_man_path();
365 for (viewer = man_viewer_list; viewer; viewer = viewer->next)
Ingo Molnar07800602009-04-20 15:00:56 +0200366 exec_viewer(viewer->name, page); /* will return when unable */
Thiago Farinac1e53012009-11-03 08:28:45 -0500367
Ingo Molnar07800602009-04-20 15:00:56 +0200368 if (fallback)
369 exec_viewer(fallback, page);
370 exec_viewer("man", page);
David Aherncc584822012-08-26 12:24:45 -0600371
372 pr_err("no man viewer handled the request");
373 return -1;
Ingo Molnar07800602009-04-20 15:00:56 +0200374}
375
David Aherncc584822012-08-26 12:24:45 -0600376static int show_info_page(const char *perf_cmd)
Ingo Molnar07800602009-04-20 15:00:56 +0200377{
378 const char *page = cmd_to_page(perf_cmd);
379 setenv("INFOPATH", system_path(PERF_INFO_PATH), 1);
380 execlp("info", "info", "perfman", page, NULL);
David Aherncc584822012-08-26 12:24:45 -0600381 return -1;
Ingo Molnar07800602009-04-20 15:00:56 +0200382}
383
David Aherncc584822012-08-26 12:24:45 -0600384static int get_html_page_path(struct strbuf *page_path, const char *page)
Ingo Molnar07800602009-04-20 15:00:56 +0200385{
386 struct stat st;
387 const char *html_path = system_path(PERF_HTML_PATH);
388
389 /* Check that we have a perf documentation directory. */
390 if (stat(mkpath("%s/perf.html", html_path), &st)
David Aherncc584822012-08-26 12:24:45 -0600391 || !S_ISREG(st.st_mode)) {
392 pr_err("'%s': not a documentation directory.", html_path);
393 return -1;
394 }
Ingo Molnar07800602009-04-20 15:00:56 +0200395
396 strbuf_init(page_path, 0);
397 strbuf_addf(page_path, "%s/%s.html", html_path, page);
David Aherncc584822012-08-26 12:24:45 -0600398
399 return 0;
Ingo Molnar07800602009-04-20 15:00:56 +0200400}
401
402/*
403 * If open_html is not defined in a platform-specific way (see for
404 * example compat/mingw.h), we use the script web--browse to display
405 * HTML.
406 */
407#ifndef open_html
Ingo Molnar16f762a2009-05-27 09:10:38 +0200408static void open_html(const char *path)
Ingo Molnar07800602009-04-20 15:00:56 +0200409{
410 execl_perf_cmd("web--browse", "-c", "help.browser", path, NULL);
411}
412#endif
413
David Aherncc584822012-08-26 12:24:45 -0600414static int show_html_page(const char *perf_cmd)
Ingo Molnar07800602009-04-20 15:00:56 +0200415{
416 const char *page = cmd_to_page(perf_cmd);
417 struct strbuf page_path; /* it leaks but we exec bellow */
418
David Aherncc584822012-08-26 12:24:45 -0600419 if (get_html_page_path(&page_path, page) != 0)
420 return -1;
Ingo Molnar07800602009-04-20 15:00:56 +0200421
422 open_html(page_path.buf);
David Aherncc584822012-08-26 12:24:45 -0600423
424 return 0;
Ingo Molnar07800602009-04-20 15:00:56 +0200425}
426
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300427int cmd_help(int argc, const char **argv, const char *prefix __maybe_unused)
Ingo Molnar07800602009-04-20 15:00:56 +0200428{
Arnaldo Carvalho de Melo2bae1d12012-10-01 15:20:58 -0300429 bool show_all = false;
Namhyung Kim670ab5d2012-10-22 16:12:23 +0900430 enum help_format help_format = HELP_FORMAT_MAN;
Arnaldo Carvalho de Melo2bae1d12012-10-01 15:20:58 -0300431 struct option builtin_help_options[] = {
432 OPT_BOOLEAN('a', "all", &show_all, "print all available commands"),
433 OPT_SET_UINT('m', "man", &help_format, "show man page", HELP_FORMAT_MAN),
434 OPT_SET_UINT('w', "web", &help_format, "show manual in web browser",
435 HELP_FORMAT_WEB),
436 OPT_SET_UINT('i', "info", &help_format, "show info page",
437 HELP_FORMAT_INFO),
438 OPT_END(),
439 };
440 const char * const builtin_help_usage[] = {
441 "perf help [--all] [--man|--web|--info] [command]",
442 NULL
443 };
Ingo Molnar07800602009-04-20 15:00:56 +0200444 const char *alias;
David Aherncc584822012-08-26 12:24:45 -0600445 int rc = 0;
Ingo Molnarf37a2912009-07-01 12:37:06 +0200446
Ingo Molnar07800602009-04-20 15:00:56 +0200447 load_command_list("perf-", &main_cmds, &other_cmds);
448
Arnaldo Carvalho de Melo2bae1d12012-10-01 15:20:58 -0300449 perf_config(perf_help_config, &help_format);
Ingo Molnar07800602009-04-20 15:00:56 +0200450
451 argc = parse_options(argc, argv, builtin_help_options,
452 builtin_help_usage, 0);
453
454 if (show_all) {
Ingo Molnar502fc5c2009-03-13 03:20:49 +0100455 printf("\n usage: %s\n\n", perf_usage_string);
Ingo Molnar07800602009-04-20 15:00:56 +0200456 list_commands("perf commands", &main_cmds, &other_cmds);
Ingo Molnar502fc5c2009-03-13 03:20:49 +0100457 printf(" %s\n\n", perf_more_info_string);
Ingo Molnar07800602009-04-20 15:00:56 +0200458 return 0;
459 }
460
461 if (!argv[0]) {
Ingo Molnar502fc5c2009-03-13 03:20:49 +0100462 printf("\n usage: %s\n\n", perf_usage_string);
Ingo Molnar07800602009-04-20 15:00:56 +0200463 list_common_cmds_help();
Ingo Molnar502fc5c2009-03-13 03:20:49 +0100464 printf("\n %s\n\n", perf_more_info_string);
Ingo Molnar07800602009-04-20 15:00:56 +0200465 return 0;
466 }
467
468 alias = alias_lookup(argv[0]);
469 if (alias && !is_perf_command(argv[0])) {
470 printf("`perf %s' is aliased to `%s'\n", argv[0], alias);
471 return 0;
472 }
473
474 switch (help_format) {
475 case HELP_FORMAT_MAN:
David Aherncc584822012-08-26 12:24:45 -0600476 rc = show_man_page(argv[0]);
Ingo Molnar07800602009-04-20 15:00:56 +0200477 break;
478 case HELP_FORMAT_INFO:
David Aherncc584822012-08-26 12:24:45 -0600479 rc = show_info_page(argv[0]);
Ingo Molnar07800602009-04-20 15:00:56 +0200480 break;
481 case HELP_FORMAT_WEB:
David Aherncc584822012-08-26 12:24:45 -0600482 rc = show_html_page(argv[0]);
483 break;
484 case HELP_FORMAT_NONE:
485 /* fall-through */
Ingo Molnar83a09442009-08-15 12:26:57 +0200486 default:
David Aherncc584822012-08-26 12:24:45 -0600487 rc = -1;
Ingo Molnar07800602009-04-20 15:00:56 +0200488 break;
489 }
490
David Aherncc584822012-08-26 12:24:45 -0600491 return rc;
Ingo Molnar07800602009-04-20 15:00:56 +0200492}