7 #include "util/cache.h"
9 #include "util/exec_cmd.h"
10 #include "common-cmds.h"
11 #include "util/parse-options.h"
12 #include "util/run-command.h"
13 #include "util/help.h"
15 static struct man_viewer_list
{
16 struct man_viewer_list
*next
;
17 char name
[FLEX_ARRAY
];
20 static struct man_viewer_info_list
{
21 struct man_viewer_info_list
*next
;
23 char name
[FLEX_ARRAY
];
24 } *man_viewer_info_list
;
33 static bool show_all
= false;
34 static enum help_format help_format
= HELP_FORMAT_NONE
;
35 static struct option builtin_help_options
[] = {
36 OPT_BOOLEAN('a', "all", &show_all
, "print all available commands"),
37 OPT_SET_UINT('m', "man", &help_format
, "show man page", HELP_FORMAT_MAN
),
38 OPT_SET_UINT('w', "web", &help_format
, "show manual in web browser",
40 OPT_SET_UINT('i', "info", &help_format
, "show info page",
45 static const char * const builtin_help_usage
[] = {
46 "perf help [--all] [--man|--web|--info] [command]",
50 static enum help_format
parse_help_format(const char *format
)
52 if (!strcmp(format
, "man"))
53 return HELP_FORMAT_MAN
;
54 if (!strcmp(format
, "info"))
55 return HELP_FORMAT_INFO
;
56 if (!strcmp(format
, "web") || !strcmp(format
, "html"))
57 return HELP_FORMAT_WEB
;
59 pr_err("unrecognized help format '%s'", format
);
60 return HELP_FORMAT_NONE
;
63 static const char *get_man_viewer_info(const char *name
)
65 struct man_viewer_info_list
*viewer
;
67 for (viewer
= man_viewer_info_list
; viewer
; viewer
= viewer
->next
) {
68 if (!strcasecmp(name
, viewer
->name
))
74 static int check_emacsclient_version(void)
76 struct strbuf buffer
= STRBUF_INIT
;
77 struct child_process ec_process
;
78 const char *argv_ec
[] = { "emacsclient", "--version", NULL
};
81 /* emacsclient prints its version number on stderr */
82 memset(&ec_process
, 0, sizeof(ec_process
));
83 ec_process
.argv
= argv_ec
;
85 ec_process
.stdout_to_stderr
= 1;
86 if (start_command(&ec_process
)) {
87 fprintf(stderr
, "Failed to start emacsclient.\n");
90 strbuf_read(&buffer
, ec_process
.err
, 20);
91 close(ec_process
.err
);
94 * Don't bother checking return value, because "emacsclient --version"
95 * seems to always exits with code 1.
97 finish_command(&ec_process
);
99 if (prefixcmp(buffer
.buf
, "emacsclient")) {
100 fprintf(stderr
, "Failed to parse emacsclient version.\n");
101 strbuf_release(&buffer
);
105 strbuf_remove(&buffer
, 0, strlen("emacsclient"));
106 version
= atoi(buffer
.buf
);
110 "emacsclient version '%d' too old (< 22).\n",
112 strbuf_release(&buffer
);
116 strbuf_release(&buffer
);
120 static void exec_woman_emacs(const char *path
, const char *page
)
122 if (!check_emacsclient_version()) {
123 /* This works only with emacsclient version >= 22. */
124 struct strbuf man_page
= STRBUF_INIT
;
127 path
= "emacsclient";
128 strbuf_addf(&man_page
, "(woman \"%s\")", page
);
129 execlp(path
, "emacsclient", "-e", man_page
.buf
, NULL
);
130 warning("failed to exec '%s': %s", path
, strerror(errno
));
134 static void exec_man_konqueror(const char *path
, const char *page
)
136 const char *display
= getenv("DISPLAY");
137 if (display
&& *display
) {
138 struct strbuf man_page
= STRBUF_INIT
;
139 const char *filename
= "kfmclient";
141 /* It's simpler to launch konqueror using kfmclient. */
143 const char *file
= strrchr(path
, '/');
144 if (file
&& !strcmp(file
+ 1, "konqueror")) {
145 char *new = strdup(path
);
146 char *dest
= strrchr(new, '/');
148 /* strlen("konqueror") == strlen("kfmclient") */
149 strcpy(dest
+ 1, "kfmclient");
156 strbuf_addf(&man_page
, "man:%s(1)", page
);
157 execlp(path
, filename
, "newTab", man_page
.buf
, NULL
);
158 warning("failed to exec '%s': %s", path
, strerror(errno
));
162 static void exec_man_man(const char *path
, const char *page
)
166 execlp(path
, "man", page
, NULL
);
167 warning("failed to exec '%s': %s", path
, strerror(errno
));
170 static void exec_man_cmd(const char *cmd
, const char *page
)
172 struct strbuf shell_cmd
= STRBUF_INIT
;
173 strbuf_addf(&shell_cmd
, "%s %s", cmd
, page
);
174 execl("/bin/sh", "sh", "-c", shell_cmd
.buf
, NULL
);
175 warning("failed to exec '%s': %s", cmd
, strerror(errno
));
178 static void add_man_viewer(const char *name
)
180 struct man_viewer_list
**p
= &man_viewer_list
;
181 size_t len
= strlen(name
);
185 *p
= zalloc(sizeof(**p
) + len
+ 1);
186 strncpy((*p
)->name
, name
, len
);
189 static int supported_man_viewer(const char *name
, size_t len
)
191 return (!strncasecmp("man", name
, len
) ||
192 !strncasecmp("woman", name
, len
) ||
193 !strncasecmp("konqueror", name
, len
));
196 static void do_add_man_viewer_info(const char *name
,
200 struct man_viewer_info_list
*new = zalloc(sizeof(*new) + len
+ 1);
202 strncpy(new->name
, name
, len
);
203 new->info
= strdup(value
);
204 new->next
= man_viewer_info_list
;
205 man_viewer_info_list
= new;
208 static int add_man_viewer_path(const char *name
,
212 if (supported_man_viewer(name
, len
))
213 do_add_man_viewer_info(name
, len
, value
);
215 warning("'%s': path for unsupported man viewer.\n"
216 "Please consider using 'man.<tool>.cmd' instead.",
222 static int add_man_viewer_cmd(const char *name
,
226 if (supported_man_viewer(name
, len
))
227 warning("'%s': cmd for supported man viewer.\n"
228 "Please consider using 'man.<tool>.path' instead.",
231 do_add_man_viewer_info(name
, len
, value
);
236 static int add_man_viewer_info(const char *var
, const char *value
)
238 const char *name
= var
+ 4;
239 const char *subkey
= strrchr(name
, '.');
242 return error("Config with no key for man viewer: %s", name
);
244 if (!strcmp(subkey
, ".path")) {
246 return config_error_nonbool(var
);
247 return add_man_viewer_path(name
, subkey
- name
, value
);
249 if (!strcmp(subkey
, ".cmd")) {
251 return config_error_nonbool(var
);
252 return add_man_viewer_cmd(name
, subkey
- name
, value
);
255 warning("'%s': unsupported man viewer sub key.", subkey
);
259 static int perf_help_config(const char *var
, const char *value
, void *cb
)
261 if (!strcmp(var
, "help.format")) {
263 return config_error_nonbool(var
);
264 help_format
= parse_help_format(value
);
265 if (help_format
== HELP_FORMAT_NONE
)
269 if (!strcmp(var
, "man.viewer")) {
271 return config_error_nonbool(var
);
272 add_man_viewer(value
);
275 if (!prefixcmp(var
, "man."))
276 return add_man_viewer_info(var
, value
);
278 return perf_default_config(var
, value
, cb
);
281 static struct cmdnames main_cmds
, other_cmds
;
283 void list_common_cmds_help(void)
285 unsigned int i
, longest
= 0;
287 for (i
= 0; i
< ARRAY_SIZE(common_cmds
); i
++) {
288 if (longest
< strlen(common_cmds
[i
].name
))
289 longest
= strlen(common_cmds
[i
].name
);
292 puts(" The most commonly used perf commands are:");
293 for (i
= 0; i
< ARRAY_SIZE(common_cmds
); i
++) {
294 printf(" %-*s ", longest
, common_cmds
[i
].name
);
295 puts(common_cmds
[i
].help
);
299 static int is_perf_command(const char *s
)
301 return is_in_cmdlist(&main_cmds
, s
) ||
302 is_in_cmdlist(&other_cmds
, s
);
305 static const char *prepend(const char *prefix
, const char *cmd
)
307 size_t pre_len
= strlen(prefix
);
308 size_t cmd_len
= strlen(cmd
);
309 char *p
= malloc(pre_len
+ cmd_len
+ 1);
310 memcpy(p
, prefix
, pre_len
);
311 strcpy(p
+ pre_len
, cmd
);
315 static const char *cmd_to_page(const char *perf_cmd
)
319 else if (!prefixcmp(perf_cmd
, "perf"))
322 return prepend("perf-", perf_cmd
);
325 static void setup_man_path(void)
327 struct strbuf new_path
= STRBUF_INIT
;
328 const char *old_path
= getenv("MANPATH");
330 /* We should always put ':' after our path. If there is no
331 * old_path, the ':' at the end will let 'man' to try
332 * system-wide paths after ours to find the manual page. If
333 * there is old_path, we need ':' as delimiter. */
334 strbuf_addstr(&new_path
, system_path(PERF_MAN_PATH
));
335 strbuf_addch(&new_path
, ':');
337 strbuf_addstr(&new_path
, old_path
);
339 setenv("MANPATH", new_path
.buf
, 1);
341 strbuf_release(&new_path
);
344 static void exec_viewer(const char *name
, const char *page
)
346 const char *info
= get_man_viewer_info(name
);
348 if (!strcasecmp(name
, "man"))
349 exec_man_man(info
, page
);
350 else if (!strcasecmp(name
, "woman"))
351 exec_woman_emacs(info
, page
);
352 else if (!strcasecmp(name
, "konqueror"))
353 exec_man_konqueror(info
, page
);
355 exec_man_cmd(info
, page
);
357 warning("'%s': unknown man viewer.", name
);
360 static int show_man_page(const char *perf_cmd
)
362 struct man_viewer_list
*viewer
;
363 const char *page
= cmd_to_page(perf_cmd
);
364 const char *fallback
= getenv("PERF_MAN_VIEWER");
367 for (viewer
= man_viewer_list
; viewer
; viewer
= viewer
->next
)
368 exec_viewer(viewer
->name
, page
); /* will return when unable */
371 exec_viewer(fallback
, page
);
372 exec_viewer("man", page
);
374 pr_err("no man viewer handled the request");
378 static int show_info_page(const char *perf_cmd
)
380 const char *page
= cmd_to_page(perf_cmd
);
381 setenv("INFOPATH", system_path(PERF_INFO_PATH
), 1);
382 execlp("info", "info", "perfman", page
, NULL
);
386 static int get_html_page_path(struct strbuf
*page_path
, const char *page
)
389 const char *html_path
= system_path(PERF_HTML_PATH
);
391 /* Check that we have a perf documentation directory. */
392 if (stat(mkpath("%s/perf.html", html_path
), &st
)
393 || !S_ISREG(st
.st_mode
)) {
394 pr_err("'%s': not a documentation directory.", html_path
);
398 strbuf_init(page_path
, 0);
399 strbuf_addf(page_path
, "%s/%s.html", html_path
, page
);
405 * If open_html is not defined in a platform-specific way (see for
406 * example compat/mingw.h), we use the script web--browse to display
410 static void open_html(const char *path
)
412 execl_perf_cmd("web--browse", "-c", "help.browser", path
, NULL
);
416 static int show_html_page(const char *perf_cmd
)
418 const char *page
= cmd_to_page(perf_cmd
);
419 struct strbuf page_path
; /* it leaks but we exec bellow */
421 if (get_html_page_path(&page_path
, page
) != 0)
424 open_html(page_path
.buf
);
429 int cmd_help(int argc
, const char **argv
, const char *prefix __used
)
434 load_command_list("perf-", &main_cmds
, &other_cmds
);
436 perf_config(perf_help_config
, NULL
);
438 argc
= parse_options(argc
, argv
, builtin_help_options
,
439 builtin_help_usage
, 0);
442 printf("\n usage: %s\n\n", perf_usage_string
);
443 list_commands("perf commands", &main_cmds
, &other_cmds
);
444 printf(" %s\n\n", perf_more_info_string
);
449 printf("\n usage: %s\n\n", perf_usage_string
);
450 list_common_cmds_help();
451 printf("\n %s\n\n", perf_more_info_string
);
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
);
461 switch (help_format
) {
462 case HELP_FORMAT_MAN
:
463 rc
= show_man_page(argv
[0]);
465 case HELP_FORMAT_INFO
:
466 rc
= show_info_page(argv
[0]);
468 case HELP_FORMAT_WEB
:
469 rc
= show_html_page(argv
[0]);
471 case HELP_FORMAT_NONE
: