7 #include "util/cache.h"
9 #include <subcmd/exec-cmd.h>
10 #include "common-cmds.h"
11 #include <subcmd/parse-options.h>
12 #include <subcmd/run-command.h>
13 #include <subcmd/help.h>
14 #include "util/debug.h"
16 static struct man_viewer_list
{
17 struct man_viewer_list
*next
;
18 char name
[FLEX_ARRAY
];
21 static struct man_viewer_info_list
{
22 struct man_viewer_info_list
*next
;
24 char name
[FLEX_ARRAY
];
25 } *man_viewer_info_list
;
34 static enum help_format
parse_help_format(const char *format
)
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
;
43 pr_err("unrecognized help format '%s'", format
);
44 return HELP_FORMAT_NONE
;
47 static const char *get_man_viewer_info(const char *name
)
49 struct man_viewer_info_list
*viewer
;
51 for (viewer
= man_viewer_info_list
; viewer
; viewer
= viewer
->next
) {
52 if (!strcasecmp(name
, viewer
->name
))
58 static int check_emacsclient_version(void)
60 struct strbuf buffer
= STRBUF_INIT
;
61 struct child_process ec_process
;
62 const char *argv_ec
[] = { "emacsclient", "--version", NULL
};
65 /* emacsclient prints its version number on stderr */
66 memset(&ec_process
, 0, sizeof(ec_process
));
67 ec_process
.argv
= argv_ec
;
69 ec_process
.stdout_to_stderr
= 1;
70 if (start_command(&ec_process
)) {
71 fprintf(stderr
, "Failed to start emacsclient.\n");
74 strbuf_read(&buffer
, ec_process
.err
, 20);
75 close(ec_process
.err
);
78 * Don't bother checking return value, because "emacsclient --version"
79 * seems to always exits with code 1.
81 finish_command(&ec_process
);
83 if (prefixcmp(buffer
.buf
, "emacsclient")) {
84 fprintf(stderr
, "Failed to parse emacsclient version.\n");
85 strbuf_release(&buffer
);
89 version
= atoi(buffer
.buf
+ strlen("emacsclient"));
93 "emacsclient version '%d' too old (< 22).\n",
95 strbuf_release(&buffer
);
99 strbuf_release(&buffer
);
103 static void exec_woman_emacs(const char *path
, const char *page
)
105 char sbuf
[STRERR_BUFSIZE
];
107 if (!check_emacsclient_version()) {
108 /* This works only with emacsclient version >= 22. */
109 struct strbuf man_page
= STRBUF_INIT
;
112 path
= "emacsclient";
113 strbuf_addf(&man_page
, "(woman \"%s\")", page
);
114 execlp(path
, "emacsclient", "-e", man_page
.buf
, NULL
);
115 warning("failed to exec '%s': %s", path
,
116 strerror_r(errno
, sbuf
, sizeof(sbuf
)));
120 static void exec_man_konqueror(const char *path
, const char *page
)
122 const char *display
= getenv("DISPLAY");
124 if (display
&& *display
) {
125 struct strbuf man_page
= STRBUF_INIT
;
126 const char *filename
= "kfmclient";
127 char sbuf
[STRERR_BUFSIZE
];
129 /* It's simpler to launch konqueror using kfmclient. */
131 const char *file
= strrchr(path
, '/');
132 if (file
&& !strcmp(file
+ 1, "konqueror")) {
133 char *new = strdup(path
);
134 char *dest
= strrchr(new, '/');
136 /* strlen("konqueror") == strlen("kfmclient") */
137 strcpy(dest
+ 1, "kfmclient");
144 strbuf_addf(&man_page
, "man:%s(1)", page
);
145 execlp(path
, filename
, "newTab", man_page
.buf
, NULL
);
146 warning("failed to exec '%s': %s", path
,
147 strerror_r(errno
, sbuf
, sizeof(sbuf
)));
151 static void exec_man_man(const char *path
, const char *page
)
153 char sbuf
[STRERR_BUFSIZE
];
157 execlp(path
, "man", page
, NULL
);
158 warning("failed to exec '%s': %s", path
,
159 strerror_r(errno
, sbuf
, sizeof(sbuf
)));
162 static void exec_man_cmd(const char *cmd
, const char *page
)
164 struct strbuf shell_cmd
= STRBUF_INIT
;
165 char sbuf
[STRERR_BUFSIZE
];
167 strbuf_addf(&shell_cmd
, "%s %s", cmd
, page
);
168 execl("/bin/sh", "sh", "-c", shell_cmd
.buf
, NULL
);
169 warning("failed to exec '%s': %s", cmd
,
170 strerror_r(errno
, sbuf
, sizeof(sbuf
)));
173 static void add_man_viewer(const char *name
)
175 struct man_viewer_list
**p
= &man_viewer_list
;
176 size_t len
= strlen(name
);
180 *p
= zalloc(sizeof(**p
) + len
+ 1);
181 strncpy((*p
)->name
, name
, len
);
184 static int supported_man_viewer(const char *name
, size_t len
)
186 return (!strncasecmp("man", name
, len
) ||
187 !strncasecmp("woman", name
, len
) ||
188 !strncasecmp("konqueror", name
, len
));
191 static void do_add_man_viewer_info(const char *name
,
195 struct man_viewer_info_list
*new = zalloc(sizeof(*new) + len
+ 1);
197 strncpy(new->name
, name
, len
);
198 new->info
= strdup(value
);
199 new->next
= man_viewer_info_list
;
200 man_viewer_info_list
= new;
203 static int add_man_viewer_path(const char *name
,
207 if (supported_man_viewer(name
, len
))
208 do_add_man_viewer_info(name
, len
, value
);
210 warning("'%s': path for unsupported man viewer.\n"
211 "Please consider using 'man.<tool>.cmd' instead.",
217 static int add_man_viewer_cmd(const char *name
,
221 if (supported_man_viewer(name
, len
))
222 warning("'%s': cmd for supported man viewer.\n"
223 "Please consider using 'man.<tool>.path' instead.",
226 do_add_man_viewer_info(name
, len
, value
);
231 static int add_man_viewer_info(const char *var
, const char *value
)
233 const char *name
= var
+ 4;
234 const char *subkey
= strrchr(name
, '.');
237 return error("Config with no key for man viewer: %s", name
);
239 if (!strcmp(subkey
, ".path")) {
241 return config_error_nonbool(var
);
242 return add_man_viewer_path(name
, subkey
- name
, value
);
244 if (!strcmp(subkey
, ".cmd")) {
246 return config_error_nonbool(var
);
247 return add_man_viewer_cmd(name
, subkey
- name
, value
);
250 warning("'%s': unsupported man viewer sub key.", subkey
);
254 static int perf_help_config(const char *var
, const char *value
, void *cb
)
256 enum help_format
*help_formatp
= cb
;
258 if (!strcmp(var
, "help.format")) {
260 return config_error_nonbool(var
);
261 *help_formatp
= parse_help_format(value
);
262 if (*help_formatp
== HELP_FORMAT_NONE
)
266 if (!strcmp(var
, "man.viewer")) {
268 return config_error_nonbool(var
);
269 add_man_viewer(value
);
272 if (!prefixcmp(var
, "man."))
273 return add_man_viewer_info(var
, value
);
278 static struct cmdnames main_cmds
, other_cmds
;
280 void list_common_cmds_help(void)
282 unsigned int i
, longest
= 0;
284 for (i
= 0; i
< ARRAY_SIZE(common_cmds
); i
++) {
285 if (longest
< strlen(common_cmds
[i
].name
))
286 longest
= strlen(common_cmds
[i
].name
);
289 puts(" The most commonly used perf commands are:");
290 for (i
= 0; i
< ARRAY_SIZE(common_cmds
); i
++) {
291 printf(" %-*s ", longest
, common_cmds
[i
].name
);
292 puts(common_cmds
[i
].help
);
296 static int is_perf_command(const char *s
)
298 return is_in_cmdlist(&main_cmds
, s
) ||
299 is_in_cmdlist(&other_cmds
, s
);
302 static const char *prepend(const char *prefix
, const char *cmd
)
304 size_t pre_len
= strlen(prefix
);
305 size_t cmd_len
= strlen(cmd
);
306 char *p
= malloc(pre_len
+ cmd_len
+ 1);
307 memcpy(p
, prefix
, pre_len
);
308 strcpy(p
+ pre_len
, cmd
);
312 static const char *cmd_to_page(const char *perf_cmd
)
316 else if (!prefixcmp(perf_cmd
, "perf"))
319 return prepend("perf-", perf_cmd
);
322 static void setup_man_path(void)
324 struct strbuf new_path
= STRBUF_INIT
;
325 const char *old_path
= getenv("MANPATH");
327 /* We should always put ':' after our path. If there is no
328 * old_path, the ':' at the end will let 'man' to try
329 * system-wide paths after ours to find the manual page. If
330 * there is old_path, we need ':' as delimiter. */
331 strbuf_addstr(&new_path
, system_path(PERF_MAN_PATH
));
332 strbuf_addch(&new_path
, ':');
334 strbuf_addstr(&new_path
, old_path
);
336 setenv("MANPATH", new_path
.buf
, 1);
338 strbuf_release(&new_path
);
341 static void exec_viewer(const char *name
, const char *page
)
343 const char *info
= get_man_viewer_info(name
);
345 if (!strcasecmp(name
, "man"))
346 exec_man_man(info
, page
);
347 else if (!strcasecmp(name
, "woman"))
348 exec_woman_emacs(info
, page
);
349 else if (!strcasecmp(name
, "konqueror"))
350 exec_man_konqueror(info
, page
);
352 exec_man_cmd(info
, page
);
354 warning("'%s': unknown man viewer.", name
);
357 static int show_man_page(const char *perf_cmd
)
359 struct man_viewer_list
*viewer
;
360 const char *page
= cmd_to_page(perf_cmd
);
361 const char *fallback
= getenv("PERF_MAN_VIEWER");
364 for (viewer
= man_viewer_list
; viewer
; viewer
= viewer
->next
)
365 exec_viewer(viewer
->name
, page
); /* will return when unable */
368 exec_viewer(fallback
, page
);
369 exec_viewer("man", page
);
371 pr_err("no man viewer handled the request");
375 static int show_info_page(const char *perf_cmd
)
377 const char *page
= cmd_to_page(perf_cmd
);
378 setenv("INFOPATH", system_path(PERF_INFO_PATH
), 1);
379 execlp("info", "info", "perfman", page
, NULL
);
383 static int get_html_page_path(struct strbuf
*page_path
, const char *page
)
386 const char *html_path
= system_path(PERF_HTML_PATH
);
388 /* Check that we have a perf documentation directory. */
389 if (stat(mkpath("%s/perf.html", html_path
), &st
)
390 || !S_ISREG(st
.st_mode
)) {
391 pr_err("'%s': not a documentation directory.", html_path
);
395 strbuf_init(page_path
, 0);
396 strbuf_addf(page_path
, "%s/%s.html", html_path
, page
);
402 * If open_html is not defined in a platform-specific way (see for
403 * example compat/mingw.h), we use the script web--browse to display
407 static void open_html(const char *path
)
409 execl_cmd("web--browse", "-c", "help.browser", path
, NULL
);
413 static int show_html_page(const char *perf_cmd
)
415 const char *page
= cmd_to_page(perf_cmd
);
416 struct strbuf page_path
; /* it leaks but we exec bellow */
418 if (get_html_page_path(&page_path
, page
) != 0)
421 open_html(page_path
.buf
);
426 int cmd_help(int argc
, const char **argv
, const char *prefix __maybe_unused
)
428 bool show_all
= false;
429 enum help_format help_format
= HELP_FORMAT_MAN
;
430 struct option builtin_help_options
[] = {
431 OPT_BOOLEAN('a', "all", &show_all
, "print all available commands"),
432 OPT_SET_UINT('m', "man", &help_format
, "show man page", HELP_FORMAT_MAN
),
433 OPT_SET_UINT('w', "web", &help_format
, "show manual in web browser",
435 OPT_SET_UINT('i', "info", &help_format
, "show info page",
439 const char * const builtin_help_subcommands
[] = {
440 "buildid-cache", "buildid-list", "diff", "evlist", "help", "list",
441 "record", "report", "bench", "stat", "timechart", "top", "annotate",
442 "script", "sched", "kmem", "lock", "kvm", "test", "inject", "mem", "data",
443 #ifdef HAVE_LIBELF_SUPPORT
446 #ifdef HAVE_LIBAUDIT_SUPPORT
450 const char *builtin_help_usage
[] = {
451 "perf help [--all] [--man|--web|--info] [command]",
457 load_command_list("perf-", &main_cmds
, &other_cmds
);
459 perf_config(perf_help_config
, &help_format
);
461 argc
= parse_options_subcommand(argc
, argv
, builtin_help_options
,
462 builtin_help_subcommands
, builtin_help_usage
, 0);
465 printf("\n Usage: %s\n\n", perf_usage_string
);
466 list_commands("perf commands", &main_cmds
, &other_cmds
);
467 printf(" %s\n\n", perf_more_info_string
);
472 printf("\n usage: %s\n\n", perf_usage_string
);
473 list_common_cmds_help();
474 printf("\n %s\n\n", perf_more_info_string
);
478 alias
= alias_lookup(argv
[0]);
479 if (alias
&& !is_perf_command(argv
[0])) {
480 printf("`perf %s' is aliased to `%s'\n", argv
[0], alias
);
484 switch (help_format
) {
485 case HELP_FORMAT_MAN
:
486 rc
= show_man_page(argv
[0]);
488 case HELP_FORMAT_INFO
:
489 rc
= show_info_page(argv
[0]);
491 case HELP_FORMAT_WEB
:
492 rc
= show_html_page(argv
[0]);
494 case HELP_FORMAT_NONE
: