blob: 0384d930480b47b29adbb86d93ae7c48e15f3741 [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{
106 if (!check_emacsclient_version()) {
107 /* This works only with emacsclient version >= 22. */
108 struct strbuf man_page = STRBUF_INIT;
109
110 if (!path)
111 path = "emacsclient";
112 strbuf_addf(&man_page, "(woman \"%s\")", page);
113 execlp(path, "emacsclient", "-e", man_page.buf, NULL);
114 warning("failed to exec '%s': %s", path, strerror(errno));
115 }
116}
117
Thiago Farinac1e53012009-11-03 08:28:45 -0500118static void exec_man_konqueror(const char *path, const char *page)
Ingo Molnar07800602009-04-20 15:00:56 +0200119{
120 const char *display = getenv("DISPLAY");
121 if (display && *display) {
122 struct strbuf man_page = STRBUF_INIT;
123 const char *filename = "kfmclient";
124
125 /* It's simpler to launch konqueror using kfmclient. */
126 if (path) {
127 const char *file = strrchr(path, '/');
128 if (file && !strcmp(file + 1, "konqueror")) {
129 char *new = strdup(path);
130 char *dest = strrchr(new, '/');
131
132 /* strlen("konqueror") == strlen("kfmclient") */
133 strcpy(dest + 1, "kfmclient");
134 path = new;
135 }
136 if (file)
137 filename = file;
138 } else
139 path = "kfmclient";
140 strbuf_addf(&man_page, "man:%s(1)", page);
141 execlp(path, filename, "newTab", man_page.buf, NULL);
142 warning("failed to exec '%s': %s", path, strerror(errno));
143 }
144}
145
Thiago Farinac1e53012009-11-03 08:28:45 -0500146static void exec_man_man(const char *path, const char *page)
Ingo Molnar07800602009-04-20 15:00:56 +0200147{
148 if (!path)
149 path = "man";
150 execlp(path, "man", page, NULL);
151 warning("failed to exec '%s': %s", path, strerror(errno));
152}
153
154static void exec_man_cmd(const char *cmd, const char *page)
155{
156 struct strbuf shell_cmd = STRBUF_INIT;
157 strbuf_addf(&shell_cmd, "%s %s", cmd, page);
158 execl("/bin/sh", "sh", "-c", shell_cmd.buf, NULL);
159 warning("failed to exec '%s': %s", cmd, strerror(errno));
160}
161
162static void add_man_viewer(const char *name)
163{
164 struct man_viewer_list **p = &man_viewer_list;
165 size_t len = strlen(name);
166
167 while (*p)
168 p = &((*p)->next);
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200169 *p = zalloc(sizeof(**p) + len + 1);
Ingo Molnar07800602009-04-20 15:00:56 +0200170 strncpy((*p)->name, name, len);
171}
172
173static int supported_man_viewer(const char *name, size_t len)
174{
175 return (!strncasecmp("man", name, len) ||
176 !strncasecmp("woman", name, len) ||
177 !strncasecmp("konqueror", name, len));
178}
179
180static void do_add_man_viewer_info(const char *name,
181 size_t len,
182 const char *value)
183{
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200184 struct man_viewer_info_list *new = zalloc(sizeof(*new) + len + 1);
Ingo Molnar07800602009-04-20 15:00:56 +0200185
186 strncpy(new->name, name, len);
187 new->info = strdup(value);
188 new->next = man_viewer_info_list;
189 man_viewer_info_list = new;
190}
191
192static int add_man_viewer_path(const char *name,
193 size_t len,
194 const char *value)
195{
196 if (supported_man_viewer(name, len))
197 do_add_man_viewer_info(name, len, value);
198 else
199 warning("'%s': path for unsupported man viewer.\n"
200 "Please consider using 'man.<tool>.cmd' instead.",
201 name);
202
203 return 0;
204}
205
206static int add_man_viewer_cmd(const char *name,
207 size_t len,
208 const char *value)
209{
210 if (supported_man_viewer(name, len))
211 warning("'%s': cmd for supported man viewer.\n"
212 "Please consider using 'man.<tool>.path' instead.",
213 name);
214 else
215 do_add_man_viewer_info(name, len, value);
216
217 return 0;
218}
219
220static int add_man_viewer_info(const char *var, const char *value)
221{
222 const char *name = var + 4;
223 const char *subkey = strrchr(name, '.');
224
225 if (!subkey)
226 return error("Config with no key for man viewer: %s", name);
227
228 if (!strcmp(subkey, ".path")) {
229 if (!value)
230 return config_error_nonbool(var);
231 return add_man_viewer_path(name, subkey - name, value);
232 }
233 if (!strcmp(subkey, ".cmd")) {
234 if (!value)
235 return config_error_nonbool(var);
236 return add_man_viewer_cmd(name, subkey - name, value);
237 }
238
239 warning("'%s': unsupported man viewer sub key.", subkey);
240 return 0;
241}
242
243static int perf_help_config(const char *var, const char *value, void *cb)
244{
Arnaldo Carvalho de Melo2bae1d12012-10-01 15:20:58 -0300245 enum help_format *help_formatp = cb;
246
Ingo Molnar07800602009-04-20 15:00:56 +0200247 if (!strcmp(var, "help.format")) {
248 if (!value)
249 return config_error_nonbool(var);
Arnaldo Carvalho de Melo2bae1d12012-10-01 15:20:58 -0300250 *help_formatp = parse_help_format(value);
251 if (*help_formatp == HELP_FORMAT_NONE)
David Aherncc584822012-08-26 12:24:45 -0600252 return -1;
Ingo Molnar07800602009-04-20 15:00:56 +0200253 return 0;
254 }
255 if (!strcmp(var, "man.viewer")) {
256 if (!value)
257 return config_error_nonbool(var);
258 add_man_viewer(value);
259 return 0;
260 }
261 if (!prefixcmp(var, "man."))
262 return add_man_viewer_info(var, value);
263
264 return perf_default_config(var, value, cb);
265}
266
267static struct cmdnames main_cmds, other_cmds;
268
269void list_common_cmds_help(void)
270{
Ingo Molnarf37a2912009-07-01 12:37:06 +0200271 unsigned int i, longest = 0;
Ingo Molnar07800602009-04-20 15:00:56 +0200272
273 for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
274 if (longest < strlen(common_cmds[i].name))
275 longest = strlen(common_cmds[i].name);
276 }
277
Ingo Molnar502fc5c2009-03-13 03:20:49 +0100278 puts(" The most commonly used perf commands are:");
Ingo Molnar07800602009-04-20 15:00:56 +0200279 for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
Amerigo Wang3912f2a2009-12-14 03:23:56 -0500280 printf(" %-*s ", longest, common_cmds[i].name);
Ingo Molnar07800602009-04-20 15:00:56 +0200281 puts(common_cmds[i].help);
282 }
283}
284
285static int is_perf_command(const char *s)
286{
287 return is_in_cmdlist(&main_cmds, s) ||
288 is_in_cmdlist(&other_cmds, s);
289}
290
291static const char *prepend(const char *prefix, const char *cmd)
292{
293 size_t pre_len = strlen(prefix);
294 size_t cmd_len = strlen(cmd);
295 char *p = malloc(pre_len + cmd_len + 1);
296 memcpy(p, prefix, pre_len);
297 strcpy(p + pre_len, cmd);
298 return p;
299}
300
301static const char *cmd_to_page(const char *perf_cmd)
302{
303 if (!perf_cmd)
304 return "perf";
305 else if (!prefixcmp(perf_cmd, "perf"))
306 return perf_cmd;
Ingo Molnar07800602009-04-20 15:00:56 +0200307 else
Ingo Molnarc1c23652009-05-30 12:38:51 +0200308 return prepend("perf-", perf_cmd);
Ingo Molnar07800602009-04-20 15:00:56 +0200309}
310
311static void setup_man_path(void)
312{
313 struct strbuf new_path = STRBUF_INIT;
314 const char *old_path = getenv("MANPATH");
315
316 /* We should always put ':' after our path. If there is no
317 * old_path, the ':' at the end will let 'man' to try
318 * system-wide paths after ours to find the manual page. If
319 * there is old_path, we need ':' as delimiter. */
320 strbuf_addstr(&new_path, system_path(PERF_MAN_PATH));
321 strbuf_addch(&new_path, ':');
322 if (old_path)
323 strbuf_addstr(&new_path, old_path);
324
325 setenv("MANPATH", new_path.buf, 1);
326
327 strbuf_release(&new_path);
328}
329
330static void exec_viewer(const char *name, const char *page)
331{
332 const char *info = get_man_viewer_info(name);
333
334 if (!strcasecmp(name, "man"))
335 exec_man_man(info, page);
336 else if (!strcasecmp(name, "woman"))
337 exec_woman_emacs(info, page);
338 else if (!strcasecmp(name, "konqueror"))
339 exec_man_konqueror(info, page);
340 else if (info)
341 exec_man_cmd(info, page);
342 else
343 warning("'%s': unknown man viewer.", name);
344}
345
David Aherncc584822012-08-26 12:24:45 -0600346static int show_man_page(const char *perf_cmd)
Ingo Molnar07800602009-04-20 15:00:56 +0200347{
348 struct man_viewer_list *viewer;
349 const char *page = cmd_to_page(perf_cmd);
350 const char *fallback = getenv("PERF_MAN_VIEWER");
351
352 setup_man_path();
353 for (viewer = man_viewer_list; viewer; viewer = viewer->next)
Ingo Molnar07800602009-04-20 15:00:56 +0200354 exec_viewer(viewer->name, page); /* will return when unable */
Thiago Farinac1e53012009-11-03 08:28:45 -0500355
Ingo Molnar07800602009-04-20 15:00:56 +0200356 if (fallback)
357 exec_viewer(fallback, page);
358 exec_viewer("man", page);
David Aherncc584822012-08-26 12:24:45 -0600359
360 pr_err("no man viewer handled the request");
361 return -1;
Ingo Molnar07800602009-04-20 15:00:56 +0200362}
363
David Aherncc584822012-08-26 12:24:45 -0600364static int show_info_page(const char *perf_cmd)
Ingo Molnar07800602009-04-20 15:00:56 +0200365{
366 const char *page = cmd_to_page(perf_cmd);
367 setenv("INFOPATH", system_path(PERF_INFO_PATH), 1);
368 execlp("info", "info", "perfman", page, NULL);
David Aherncc584822012-08-26 12:24:45 -0600369 return -1;
Ingo Molnar07800602009-04-20 15:00:56 +0200370}
371
David Aherncc584822012-08-26 12:24:45 -0600372static int get_html_page_path(struct strbuf *page_path, const char *page)
Ingo Molnar07800602009-04-20 15:00:56 +0200373{
374 struct stat st;
375 const char *html_path = system_path(PERF_HTML_PATH);
376
377 /* Check that we have a perf documentation directory. */
378 if (stat(mkpath("%s/perf.html", html_path), &st)
David Aherncc584822012-08-26 12:24:45 -0600379 || !S_ISREG(st.st_mode)) {
380 pr_err("'%s': not a documentation directory.", html_path);
381 return -1;
382 }
Ingo Molnar07800602009-04-20 15:00:56 +0200383
384 strbuf_init(page_path, 0);
385 strbuf_addf(page_path, "%s/%s.html", html_path, page);
David Aherncc584822012-08-26 12:24:45 -0600386
387 return 0;
Ingo Molnar07800602009-04-20 15:00:56 +0200388}
389
390/*
391 * If open_html is not defined in a platform-specific way (see for
392 * example compat/mingw.h), we use the script web--browse to display
393 * HTML.
394 */
395#ifndef open_html
Ingo Molnar16f762a2009-05-27 09:10:38 +0200396static void open_html(const char *path)
Ingo Molnar07800602009-04-20 15:00:56 +0200397{
398 execl_perf_cmd("web--browse", "-c", "help.browser", path, NULL);
399}
400#endif
401
David Aherncc584822012-08-26 12:24:45 -0600402static int show_html_page(const char *perf_cmd)
Ingo Molnar07800602009-04-20 15:00:56 +0200403{
404 const char *page = cmd_to_page(perf_cmd);
405 struct strbuf page_path; /* it leaks but we exec bellow */
406
David Aherncc584822012-08-26 12:24:45 -0600407 if (get_html_page_path(&page_path, page) != 0)
408 return -1;
Ingo Molnar07800602009-04-20 15:00:56 +0200409
410 open_html(page_path.buf);
David Aherncc584822012-08-26 12:24:45 -0600411
412 return 0;
Ingo Molnar07800602009-04-20 15:00:56 +0200413}
414
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300415int cmd_help(int argc, const char **argv, const char *prefix __maybe_unused)
Ingo Molnar07800602009-04-20 15:00:56 +0200416{
Arnaldo Carvalho de Melo2bae1d12012-10-01 15:20:58 -0300417 bool show_all = false;
Namhyung Kim670ab5d2012-10-22 16:12:23 +0900418 enum help_format help_format = HELP_FORMAT_MAN;
Arnaldo Carvalho de Melo2bae1d12012-10-01 15:20:58 -0300419 struct option builtin_help_options[] = {
420 OPT_BOOLEAN('a', "all", &show_all, "print all available commands"),
421 OPT_SET_UINT('m', "man", &help_format, "show man page", HELP_FORMAT_MAN),
422 OPT_SET_UINT('w', "web", &help_format, "show manual in web browser",
423 HELP_FORMAT_WEB),
424 OPT_SET_UINT('i', "info", &help_format, "show info page",
425 HELP_FORMAT_INFO),
426 OPT_END(),
427 };
428 const char * const builtin_help_usage[] = {
429 "perf help [--all] [--man|--web|--info] [command]",
430 NULL
431 };
Ingo Molnar07800602009-04-20 15:00:56 +0200432 const char *alias;
David Aherncc584822012-08-26 12:24:45 -0600433 int rc = 0;
Ingo Molnarf37a2912009-07-01 12:37:06 +0200434
Ingo Molnar07800602009-04-20 15:00:56 +0200435 load_command_list("perf-", &main_cmds, &other_cmds);
436
Arnaldo Carvalho de Melo2bae1d12012-10-01 15:20:58 -0300437 perf_config(perf_help_config, &help_format);
Ingo Molnar07800602009-04-20 15:00:56 +0200438
439 argc = parse_options(argc, argv, builtin_help_options,
440 builtin_help_usage, 0);
441
442 if (show_all) {
Ingo Molnar502fc5c2009-03-13 03:20:49 +0100443 printf("\n usage: %s\n\n", perf_usage_string);
Ingo Molnar07800602009-04-20 15:00:56 +0200444 list_commands("perf commands", &main_cmds, &other_cmds);
Ingo Molnar502fc5c2009-03-13 03:20:49 +0100445 printf(" %s\n\n", perf_more_info_string);
Ingo Molnar07800602009-04-20 15:00:56 +0200446 return 0;
447 }
448
449 if (!argv[0]) {
Ingo Molnar502fc5c2009-03-13 03:20:49 +0100450 printf("\n usage: %s\n\n", perf_usage_string);
Ingo Molnar07800602009-04-20 15:00:56 +0200451 list_common_cmds_help();
Ingo Molnar502fc5c2009-03-13 03:20:49 +0100452 printf("\n %s\n\n", perf_more_info_string);
Ingo Molnar07800602009-04-20 15:00:56 +0200453 return 0;
454 }
455
456 alias = alias_lookup(argv[0]);
457 if (alias && !is_perf_command(argv[0])) {
458 printf("`perf %s' is aliased to `%s'\n", argv[0], alias);
459 return 0;
460 }
461
462 switch (help_format) {
463 case HELP_FORMAT_MAN:
David Aherncc584822012-08-26 12:24:45 -0600464 rc = show_man_page(argv[0]);
Ingo Molnar07800602009-04-20 15:00:56 +0200465 break;
466 case HELP_FORMAT_INFO:
David Aherncc584822012-08-26 12:24:45 -0600467 rc = show_info_page(argv[0]);
Ingo Molnar07800602009-04-20 15:00:56 +0200468 break;
469 case HELP_FORMAT_WEB:
David Aherncc584822012-08-26 12:24:45 -0600470 rc = show_html_page(argv[0]);
471 break;
472 case HELP_FORMAT_NONE:
473 /* fall-through */
Ingo Molnar83a09442009-08-15 12:26:57 +0200474 default:
David Aherncc584822012-08-26 12:24:45 -0600475 rc = -1;
Ingo Molnar07800602009-04-20 15:00:56 +0200476 break;
477 }
478
David Aherncc584822012-08-26 12:24:45 -0600479 return rc;
Ingo Molnar07800602009-04-20 15:00:56 +0200480}