blob: 178b88ae3d2f174a5a085f233163d54cef152337 [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"
Ingo Molnar07800602009-04-20 15:00:56 +020014
15static struct man_viewer_list {
16 struct man_viewer_list *next;
17 char name[FLEX_ARRAY];
18} *man_viewer_list;
19
20static struct man_viewer_info_list {
21 struct man_viewer_info_list *next;
22 const char *info;
23 char name[FLEX_ARRAY];
24} *man_viewer_info_list;
25
26enum help_format {
David Aherncc584822012-08-26 12:24:45 -060027 HELP_FORMAT_NONE,
Ingo Molnar07800602009-04-20 15:00:56 +020028 HELP_FORMAT_MAN,
29 HELP_FORMAT_INFO,
30 HELP_FORMAT_WEB,
31};
32
Ingo Molnar07800602009-04-20 15:00:56 +020033static enum help_format parse_help_format(const char *format)
34{
35 if (!strcmp(format, "man"))
36 return HELP_FORMAT_MAN;
37 if (!strcmp(format, "info"))
38 return HELP_FORMAT_INFO;
39 if (!strcmp(format, "web") || !strcmp(format, "html"))
40 return HELP_FORMAT_WEB;
David Aherncc584822012-08-26 12:24:45 -060041
42 pr_err("unrecognized help format '%s'", format);
43 return HELP_FORMAT_NONE;
Ingo Molnar07800602009-04-20 15:00:56 +020044}
45
46static const char *get_man_viewer_info(const char *name)
47{
48 struct man_viewer_info_list *viewer;
49
Thiago Farinac1e53012009-11-03 08:28:45 -050050 for (viewer = man_viewer_info_list; viewer; viewer = viewer->next) {
Ingo Molnar07800602009-04-20 15:00:56 +020051 if (!strcasecmp(name, viewer->name))
52 return viewer->info;
53 }
54 return NULL;
55}
56
57static int check_emacsclient_version(void)
58{
59 struct strbuf buffer = STRBUF_INIT;
60 struct child_process ec_process;
61 const char *argv_ec[] = { "emacsclient", "--version", NULL };
62 int version;
63
64 /* emacsclient prints its version number on stderr */
65 memset(&ec_process, 0, sizeof(ec_process));
66 ec_process.argv = argv_ec;
67 ec_process.err = -1;
68 ec_process.stdout_to_stderr = 1;
69 if (start_command(&ec_process)) {
70 fprintf(stderr, "Failed to start emacsclient.\n");
71 return -1;
72 }
73 strbuf_read(&buffer, ec_process.err, 20);
74 close(ec_process.err);
75
76 /*
77 * Don't bother checking return value, because "emacsclient --version"
78 * seems to always exits with code 1.
79 */
80 finish_command(&ec_process);
81
82 if (prefixcmp(buffer.buf, "emacsclient")) {
83 fprintf(stderr, "Failed to parse emacsclient version.\n");
84 strbuf_release(&buffer);
85 return -1;
86 }
87
88 strbuf_remove(&buffer, 0, strlen("emacsclient"));
89 version = atoi(buffer.buf);
90
91 if (version < 22) {
92 fprintf(stderr,
93 "emacsclient version '%d' too old (< 22).\n",
94 version);
95 strbuf_release(&buffer);
96 return -1;
97 }
98
99 strbuf_release(&buffer);
100 return 0;
101}
102
Thiago Farinac1e53012009-11-03 08:28:45 -0500103static void exec_woman_emacs(const char *path, const char *page)
Ingo Molnar07800602009-04-20 15:00:56 +0200104{
105 if (!check_emacsclient_version()) {
106 /* This works only with emacsclient version >= 22. */
107 struct strbuf man_page = STRBUF_INIT;
108
109 if (!path)
110 path = "emacsclient";
111 strbuf_addf(&man_page, "(woman \"%s\")", page);
112 execlp(path, "emacsclient", "-e", man_page.buf, NULL);
113 warning("failed to exec '%s': %s", path, strerror(errno));
114 }
115}
116
Thiago Farinac1e53012009-11-03 08:28:45 -0500117static void exec_man_konqueror(const char *path, const char *page)
Ingo Molnar07800602009-04-20 15:00:56 +0200118{
119 const char *display = getenv("DISPLAY");
120 if (display && *display) {
121 struct strbuf man_page = STRBUF_INIT;
122 const char *filename = "kfmclient";
123
124 /* It's simpler to launch konqueror using kfmclient. */
125 if (path) {
126 const char *file = strrchr(path, '/');
127 if (file && !strcmp(file + 1, "konqueror")) {
128 char *new = strdup(path);
129 char *dest = strrchr(new, '/');
130
131 /* strlen("konqueror") == strlen("kfmclient") */
132 strcpy(dest + 1, "kfmclient");
133 path = new;
134 }
135 if (file)
136 filename = file;
137 } else
138 path = "kfmclient";
139 strbuf_addf(&man_page, "man:%s(1)", page);
140 execlp(path, filename, "newTab", man_page.buf, NULL);
141 warning("failed to exec '%s': %s", path, strerror(errno));
142 }
143}
144
Thiago Farinac1e53012009-11-03 08:28:45 -0500145static void exec_man_man(const char *path, const char *page)
Ingo Molnar07800602009-04-20 15:00:56 +0200146{
147 if (!path)
148 path = "man";
149 execlp(path, "man", page, NULL);
150 warning("failed to exec '%s': %s", path, strerror(errno));
151}
152
153static void exec_man_cmd(const char *cmd, const char *page)
154{
155 struct strbuf shell_cmd = STRBUF_INIT;
156 strbuf_addf(&shell_cmd, "%s %s", cmd, page);
157 execl("/bin/sh", "sh", "-c", shell_cmd.buf, NULL);
158 warning("failed to exec '%s': %s", cmd, strerror(errno));
159}
160
161static void add_man_viewer(const char *name)
162{
163 struct man_viewer_list **p = &man_viewer_list;
164 size_t len = strlen(name);
165
166 while (*p)
167 p = &((*p)->next);
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200168 *p = zalloc(sizeof(**p) + len + 1);
Ingo Molnar07800602009-04-20 15:00:56 +0200169 strncpy((*p)->name, name, len);
170}
171
172static int supported_man_viewer(const char *name, size_t len)
173{
174 return (!strncasecmp("man", name, len) ||
175 !strncasecmp("woman", name, len) ||
176 !strncasecmp("konqueror", name, len));
177}
178
179static void do_add_man_viewer_info(const char *name,
180 size_t len,
181 const char *value)
182{
Arnaldo Carvalho de Melo36479482009-11-24 12:05:16 -0200183 struct man_viewer_info_list *new = zalloc(sizeof(*new) + len + 1);
Ingo Molnar07800602009-04-20 15:00:56 +0200184
185 strncpy(new->name, name, len);
186 new->info = strdup(value);
187 new->next = man_viewer_info_list;
188 man_viewer_info_list = new;
189}
190
191static int add_man_viewer_path(const char *name,
192 size_t len,
193 const char *value)
194{
195 if (supported_man_viewer(name, len))
196 do_add_man_viewer_info(name, len, value);
197 else
198 warning("'%s': path for unsupported man viewer.\n"
199 "Please consider using 'man.<tool>.cmd' instead.",
200 name);
201
202 return 0;
203}
204
205static int add_man_viewer_cmd(const char *name,
206 size_t len,
207 const char *value)
208{
209 if (supported_man_viewer(name, len))
210 warning("'%s': cmd for supported man viewer.\n"
211 "Please consider using 'man.<tool>.path' instead.",
212 name);
213 else
214 do_add_man_viewer_info(name, len, value);
215
216 return 0;
217}
218
219static int add_man_viewer_info(const char *var, const char *value)
220{
221 const char *name = var + 4;
222 const char *subkey = strrchr(name, '.');
223
224 if (!subkey)
225 return error("Config with no key for man viewer: %s", name);
226
227 if (!strcmp(subkey, ".path")) {
228 if (!value)
229 return config_error_nonbool(var);
230 return add_man_viewer_path(name, subkey - name, value);
231 }
232 if (!strcmp(subkey, ".cmd")) {
233 if (!value)
234 return config_error_nonbool(var);
235 return add_man_viewer_cmd(name, subkey - name, value);
236 }
237
238 warning("'%s': unsupported man viewer sub key.", subkey);
239 return 0;
240}
241
242static int perf_help_config(const char *var, const char *value, void *cb)
243{
Arnaldo Carvalho de Melo2bae1d12012-10-01 15:20:58 -0300244 enum help_format *help_formatp = cb;
245
Ingo Molnar07800602009-04-20 15:00:56 +0200246 if (!strcmp(var, "help.format")) {
247 if (!value)
248 return config_error_nonbool(var);
Arnaldo Carvalho de Melo2bae1d12012-10-01 15:20:58 -0300249 *help_formatp = parse_help_format(value);
250 if (*help_formatp == HELP_FORMAT_NONE)
David Aherncc584822012-08-26 12:24:45 -0600251 return -1;
Ingo Molnar07800602009-04-20 15:00:56 +0200252 return 0;
253 }
254 if (!strcmp(var, "man.viewer")) {
255 if (!value)
256 return config_error_nonbool(var);
257 add_man_viewer(value);
258 return 0;
259 }
260 if (!prefixcmp(var, "man."))
261 return add_man_viewer_info(var, value);
262
263 return perf_default_config(var, value, cb);
264}
265
266static struct cmdnames main_cmds, other_cmds;
267
268void list_common_cmds_help(void)
269{
Ingo Molnarf37a2912009-07-01 12:37:06 +0200270 unsigned int i, longest = 0;
Ingo Molnar07800602009-04-20 15:00:56 +0200271
272 for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
273 if (longest < strlen(common_cmds[i].name))
274 longest = strlen(common_cmds[i].name);
275 }
276
Ingo Molnar502fc5c2009-03-13 03:20:49 +0100277 puts(" The most commonly used perf commands are:");
Ingo Molnar07800602009-04-20 15:00:56 +0200278 for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
Amerigo Wang3912f2a2009-12-14 03:23:56 -0500279 printf(" %-*s ", longest, common_cmds[i].name);
Ingo Molnar07800602009-04-20 15:00:56 +0200280 puts(common_cmds[i].help);
281 }
282}
283
284static int is_perf_command(const char *s)
285{
286 return is_in_cmdlist(&main_cmds, s) ||
287 is_in_cmdlist(&other_cmds, s);
288}
289
290static const char *prepend(const char *prefix, const char *cmd)
291{
292 size_t pre_len = strlen(prefix);
293 size_t cmd_len = strlen(cmd);
294 char *p = malloc(pre_len + cmd_len + 1);
295 memcpy(p, prefix, pre_len);
296 strcpy(p + pre_len, cmd);
297 return p;
298}
299
300static const char *cmd_to_page(const char *perf_cmd)
301{
302 if (!perf_cmd)
303 return "perf";
304 else if (!prefixcmp(perf_cmd, "perf"))
305 return perf_cmd;
Ingo Molnar07800602009-04-20 15:00:56 +0200306 else
Ingo Molnarc1c23652009-05-30 12:38:51 +0200307 return prepend("perf-", perf_cmd);
Ingo Molnar07800602009-04-20 15:00:56 +0200308}
309
310static void setup_man_path(void)
311{
312 struct strbuf new_path = STRBUF_INIT;
313 const char *old_path = getenv("MANPATH");
314
315 /* We should always put ':' after our path. If there is no
316 * old_path, the ':' at the end will let 'man' to try
317 * system-wide paths after ours to find the manual page. If
318 * there is old_path, we need ':' as delimiter. */
319 strbuf_addstr(&new_path, system_path(PERF_MAN_PATH));
320 strbuf_addch(&new_path, ':');
321 if (old_path)
322 strbuf_addstr(&new_path, old_path);
323
324 setenv("MANPATH", new_path.buf, 1);
325
326 strbuf_release(&new_path);
327}
328
329static void exec_viewer(const char *name, const char *page)
330{
331 const char *info = get_man_viewer_info(name);
332
333 if (!strcasecmp(name, "man"))
334 exec_man_man(info, page);
335 else if (!strcasecmp(name, "woman"))
336 exec_woman_emacs(info, page);
337 else if (!strcasecmp(name, "konqueror"))
338 exec_man_konqueror(info, page);
339 else if (info)
340 exec_man_cmd(info, page);
341 else
342 warning("'%s': unknown man viewer.", name);
343}
344
David Aherncc584822012-08-26 12:24:45 -0600345static int show_man_page(const char *perf_cmd)
Ingo Molnar07800602009-04-20 15:00:56 +0200346{
347 struct man_viewer_list *viewer;
348 const char *page = cmd_to_page(perf_cmd);
349 const char *fallback = getenv("PERF_MAN_VIEWER");
350
351 setup_man_path();
352 for (viewer = man_viewer_list; viewer; viewer = viewer->next)
Ingo Molnar07800602009-04-20 15:00:56 +0200353 exec_viewer(viewer->name, page); /* will return when unable */
Thiago Farinac1e53012009-11-03 08:28:45 -0500354
Ingo Molnar07800602009-04-20 15:00:56 +0200355 if (fallback)
356 exec_viewer(fallback, page);
357 exec_viewer("man", page);
David Aherncc584822012-08-26 12:24:45 -0600358
359 pr_err("no man viewer handled the request");
360 return -1;
Ingo Molnar07800602009-04-20 15:00:56 +0200361}
362
David Aherncc584822012-08-26 12:24:45 -0600363static int show_info_page(const char *perf_cmd)
Ingo Molnar07800602009-04-20 15:00:56 +0200364{
365 const char *page = cmd_to_page(perf_cmd);
366 setenv("INFOPATH", system_path(PERF_INFO_PATH), 1);
367 execlp("info", "info", "perfman", page, NULL);
David Aherncc584822012-08-26 12:24:45 -0600368 return -1;
Ingo Molnar07800602009-04-20 15:00:56 +0200369}
370
David Aherncc584822012-08-26 12:24:45 -0600371static int get_html_page_path(struct strbuf *page_path, const char *page)
Ingo Molnar07800602009-04-20 15:00:56 +0200372{
373 struct stat st;
374 const char *html_path = system_path(PERF_HTML_PATH);
375
376 /* Check that we have a perf documentation directory. */
377 if (stat(mkpath("%s/perf.html", html_path), &st)
David Aherncc584822012-08-26 12:24:45 -0600378 || !S_ISREG(st.st_mode)) {
379 pr_err("'%s': not a documentation directory.", html_path);
380 return -1;
381 }
Ingo Molnar07800602009-04-20 15:00:56 +0200382
383 strbuf_init(page_path, 0);
384 strbuf_addf(page_path, "%s/%s.html", html_path, page);
David Aherncc584822012-08-26 12:24:45 -0600385
386 return 0;
Ingo Molnar07800602009-04-20 15:00:56 +0200387}
388
389/*
390 * If open_html is not defined in a platform-specific way (see for
391 * example compat/mingw.h), we use the script web--browse to display
392 * HTML.
393 */
394#ifndef open_html
Ingo Molnar16f762a2009-05-27 09:10:38 +0200395static void open_html(const char *path)
Ingo Molnar07800602009-04-20 15:00:56 +0200396{
397 execl_perf_cmd("web--browse", "-c", "help.browser", path, NULL);
398}
399#endif
400
David Aherncc584822012-08-26 12:24:45 -0600401static int show_html_page(const char *perf_cmd)
Ingo Molnar07800602009-04-20 15:00:56 +0200402{
403 const char *page = cmd_to_page(perf_cmd);
404 struct strbuf page_path; /* it leaks but we exec bellow */
405
David Aherncc584822012-08-26 12:24:45 -0600406 if (get_html_page_path(&page_path, page) != 0)
407 return -1;
Ingo Molnar07800602009-04-20 15:00:56 +0200408
409 open_html(page_path.buf);
David Aherncc584822012-08-26 12:24:45 -0600410
411 return 0;
Ingo Molnar07800602009-04-20 15:00:56 +0200412}
413
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300414int cmd_help(int argc, const char **argv, const char *prefix __maybe_unused)
Ingo Molnar07800602009-04-20 15:00:56 +0200415{
Arnaldo Carvalho de Melo2bae1d12012-10-01 15:20:58 -0300416 bool show_all = false;
Namhyung Kim670ab5d2012-10-22 16:12:23 +0900417 enum help_format help_format = HELP_FORMAT_MAN;
Arnaldo Carvalho de Melo2bae1d12012-10-01 15:20:58 -0300418 struct option builtin_help_options[] = {
419 OPT_BOOLEAN('a', "all", &show_all, "print all available commands"),
420 OPT_SET_UINT('m', "man", &help_format, "show man page", HELP_FORMAT_MAN),
421 OPT_SET_UINT('w', "web", &help_format, "show manual in web browser",
422 HELP_FORMAT_WEB),
423 OPT_SET_UINT('i', "info", &help_format, "show info page",
424 HELP_FORMAT_INFO),
425 OPT_END(),
426 };
427 const char * const builtin_help_usage[] = {
428 "perf help [--all] [--man|--web|--info] [command]",
429 NULL
430 };
Ingo Molnar07800602009-04-20 15:00:56 +0200431 const char *alias;
David Aherncc584822012-08-26 12:24:45 -0600432 int rc = 0;
Ingo Molnarf37a2912009-07-01 12:37:06 +0200433
Ingo Molnar07800602009-04-20 15:00:56 +0200434 load_command_list("perf-", &main_cmds, &other_cmds);
435
Arnaldo Carvalho de Melo2bae1d12012-10-01 15:20:58 -0300436 perf_config(perf_help_config, &help_format);
Ingo Molnar07800602009-04-20 15:00:56 +0200437
438 argc = parse_options(argc, argv, builtin_help_options,
439 builtin_help_usage, 0);
440
441 if (show_all) {
Ingo Molnar502fc5c2009-03-13 03:20:49 +0100442 printf("\n usage: %s\n\n", perf_usage_string);
Ingo Molnar07800602009-04-20 15:00:56 +0200443 list_commands("perf commands", &main_cmds, &other_cmds);
Ingo Molnar502fc5c2009-03-13 03:20:49 +0100444 printf(" %s\n\n", perf_more_info_string);
Ingo Molnar07800602009-04-20 15:00:56 +0200445 return 0;
446 }
447
448 if (!argv[0]) {
Ingo Molnar502fc5c2009-03-13 03:20:49 +0100449 printf("\n usage: %s\n\n", perf_usage_string);
Ingo Molnar07800602009-04-20 15:00:56 +0200450 list_common_cmds_help();
Ingo Molnar502fc5c2009-03-13 03:20:49 +0100451 printf("\n %s\n\n", perf_more_info_string);
Ingo Molnar07800602009-04-20 15:00:56 +0200452 return 0;
453 }
454
455 alias = alias_lookup(argv[0]);
456 if (alias && !is_perf_command(argv[0])) {
457 printf("`perf %s' is aliased to `%s'\n", argv[0], alias);
458 return 0;
459 }
460
461 switch (help_format) {
462 case HELP_FORMAT_MAN:
David Aherncc584822012-08-26 12:24:45 -0600463 rc = show_man_page(argv[0]);
Ingo Molnar07800602009-04-20 15:00:56 +0200464 break;
465 case HELP_FORMAT_INFO:
David Aherncc584822012-08-26 12:24:45 -0600466 rc = show_info_page(argv[0]);
Ingo Molnar07800602009-04-20 15:00:56 +0200467 break;
468 case HELP_FORMAT_WEB:
David Aherncc584822012-08-26 12:24:45 -0600469 rc = show_html_page(argv[0]);
470 break;
471 case HELP_FORMAT_NONE:
472 /* fall-through */
Ingo Molnar83a09442009-08-15 12:26:57 +0200473 default:
David Aherncc584822012-08-26 12:24:45 -0600474 rc = -1;
Ingo Molnar07800602009-04-20 15:00:56 +0200475 break;
476 }
477
David Aherncc584822012-08-26 12:24:45 -0600478 return rc;
Ingo Molnar07800602009-04-20 15:00:56 +0200479}