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"
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 strbuf_remove(&buffer
, 0, strlen("emacsclient"));
90 version
= atoi(buffer
.buf
);
94 "emacsclient version '%d' too old (< 22).\n",
96 strbuf_release(&buffer
);
100 strbuf_release(&buffer
);
104 static void exec_woman_emacs(const char *path
, const char *page
)
106 char sbuf
[STRERR_BUFSIZE
];
108 if (!check_emacsclient_version()) {
109 /* This works only with emacsclient version >= 22. */
110 struct strbuf man_page
= STRBUF_INIT
;
113 path
= "emacsclient";
114 strbuf_addf(&man_page
, "(woman \"%s\")", page
);
115 execlp(path
, "emacsclient", "-e", man_page
.buf
, NULL
);
116 warning("failed to exec '%s': %s", path
,
117 strerror_r(errno
, sbuf
, sizeof(sbuf
)));
121 static void exec_man_konqueror(const char *path
, const char *page
)
123 const char *display
= getenv("DISPLAY");
125 if (display
&& *display
) {
126 struct strbuf man_page
= STRBUF_INIT
;
127 const char *filename
= "kfmclient";
128 char sbuf
[STRERR_BUFSIZE
];
130 /* It's simpler to launch konqueror using kfmclient. */
132 const char *file
= strrchr(path
, '/');
133 if (file
&& !strcmp(file
+ 1, "konqueror")) {
134 char *new = strdup(path
);
135 char *dest
= strrchr(new, '/');
137 /* strlen("konqueror") == strlen("kfmclient") */
138 strcpy(dest
+ 1, "kfmclient");
145 strbuf_addf(&man_page
, "man:%s(1)", page
);
146 execlp(path
, filename
, "newTab", man_page
.buf
, NULL
);
147 warning("failed to exec '%s': %s", path
,
148 strerror_r(errno
, sbuf
, sizeof(sbuf
)));
152 static void exec_man_man(const char *path
, const char *page
)
154 char sbuf
[STRERR_BUFSIZE
];
158 execlp(path
, "man", page
, NULL
);
159 warning("failed to exec '%s': %s", path
,
160 strerror_r(errno
, sbuf
, sizeof(sbuf
)));
163 static void exec_man_cmd(const char *cmd
, const char *page
)
165 struct strbuf shell_cmd
= STRBUF_INIT
;
166 char sbuf
[STRERR_BUFSIZE
];
168 strbuf_addf(&shell_cmd
, "%s %s", cmd
, page
);
169 execl("/bin/sh", "sh", "-c", shell_cmd
.buf
, NULL
);
170 warning("failed to exec '%s': %s", cmd
,
171 strerror_r(errno
, sbuf
, sizeof(sbuf
)));
174 static void add_man_viewer(const char *name
)
176 struct man_viewer_list
**p
= &man_viewer_list
;
177 size_t len
= strlen(name
);
181 *p
= zalloc(sizeof(**p
) + len
+ 1);
182 strncpy((*p
)->name
, name
, len
);
185 static int supported_man_viewer(const char *name
, size_t len
)
187 return (!strncasecmp("man", name
, len
) ||
188 !strncasecmp("woman", name
, len
) ||
189 !strncasecmp("konqueror", name
, len
));
192 static void do_add_man_viewer_info(const char *name
,
196 struct man_viewer_info_list
*new = zalloc(sizeof(*new) + len
+ 1);
198 strncpy(new->name
, name
, len
);
199 new->info
= strdup(value
);
200 new->next
= man_viewer_info_list
;
201 man_viewer_info_list
= new;
204 static int add_man_viewer_path(const char *name
,
208 if (supported_man_viewer(name
, len
))
209 do_add_man_viewer_info(name
, len
, value
);
211 warning("'%s': path for unsupported man viewer.\n"
212 "Please consider using 'man.<tool>.cmd' instead.",
218 static int add_man_viewer_cmd(const char *name
,
222 if (supported_man_viewer(name
, len
))
223 warning("'%s': cmd for supported man viewer.\n"
224 "Please consider using 'man.<tool>.path' instead.",
227 do_add_man_viewer_info(name
, len
, value
);
232 static int add_man_viewer_info(const char *var
, const char *value
)
234 const char *name
= var
+ 4;
235 const char *subkey
= strrchr(name
, '.');
238 return error("Config with no key for man viewer: %s", name
);
240 if (!strcmp(subkey
, ".path")) {
242 return config_error_nonbool(var
);
243 return add_man_viewer_path(name
, subkey
- name
, value
);
245 if (!strcmp(subkey
, ".cmd")) {
247 return config_error_nonbool(var
);
248 return add_man_viewer_cmd(name
, subkey
- name
, value
);
251 warning("'%s': unsupported man viewer sub key.", subkey
);
255 static int perf_help_config(const char *var
, const char *value
, void *cb
)
257 enum help_format
*help_formatp
= cb
;
259 if (!strcmp(var
, "help.format")) {
261 return config_error_nonbool(var
);
262 *help_formatp
= parse_help_format(value
);
263 if (*help_formatp
== HELP_FORMAT_NONE
)
267 if (!strcmp(var
, "man.viewer")) {
269 return config_error_nonbool(var
);
270 add_man_viewer(value
);
273 if (!prefixcmp(var
, "man."))
274 return add_man_viewer_info(var
, value
);
276 return perf_default_config(var
, value
, cb
);
279 static struct cmdnames main_cmds
, other_cmds
;
281 void list_common_cmds_help(void)
283 unsigned int i
, longest
= 0;
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
);
290 puts(" The most commonly used perf commands are:");
291 for (i
= 0; i
< ARRAY_SIZE(common_cmds
); i
++) {
292 printf(" %-*s ", longest
, common_cmds
[i
].name
);
293 puts(common_cmds
[i
].help
);
297 static int is_perf_command(const char *s
)
299 return is_in_cmdlist(&main_cmds
, s
) ||
300 is_in_cmdlist(&other_cmds
, s
);
303 static const char *prepend(const char *prefix
, const char *cmd
)
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
);
313 static const char *cmd_to_page(const char *perf_cmd
)
317 else if (!prefixcmp(perf_cmd
, "perf"))
320 return prepend("perf-", perf_cmd
);
323 static void setup_man_path(void)
325 struct strbuf new_path
= STRBUF_INIT
;
326 const char *old_path
= getenv("MANPATH");
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
, ':');
335 strbuf_addstr(&new_path
, old_path
);
337 setenv("MANPATH", new_path
.buf
, 1);
339 strbuf_release(&new_path
);
342 static void exec_viewer(const char *name
, const char *page
)
344 const char *info
= get_man_viewer_info(name
);
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
);
353 exec_man_cmd(info
, page
);
355 warning("'%s': unknown man viewer.", name
);
358 static int show_man_page(const char *perf_cmd
)
360 struct man_viewer_list
*viewer
;
361 const char *page
= cmd_to_page(perf_cmd
);
362 const char *fallback
= getenv("PERF_MAN_VIEWER");
365 for (viewer
= man_viewer_list
; viewer
; viewer
= viewer
->next
)
366 exec_viewer(viewer
->name
, page
); /* will return when unable */
369 exec_viewer(fallback
, page
);
370 exec_viewer("man", page
);
372 pr_err("no man viewer handled the request");
376 static int show_info_page(const char *perf_cmd
)
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
);
384 static int get_html_page_path(struct strbuf
*page_path
, const char *page
)
387 const char *html_path
= system_path(PERF_HTML_PATH
);
389 /* Check that we have a perf documentation directory. */
390 if (stat(mkpath("%s/perf.html", html_path
), &st
)
391 || !S_ISREG(st
.st_mode
)) {
392 pr_err("'%s': not a documentation directory.", html_path
);
396 strbuf_init(page_path
, 0);
397 strbuf_addf(page_path
, "%s/%s.html", html_path
, page
);
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
408 static void open_html(const char *path
)
410 execl_perf_cmd("web--browse", "-c", "help.browser", path
, NULL
);
414 static int show_html_page(const char *perf_cmd
)
416 const char *page
= cmd_to_page(perf_cmd
);
417 struct strbuf page_path
; /* it leaks but we exec bellow */
419 if (get_html_page_path(&page_path
, page
) != 0)
422 open_html(page_path
.buf
);
427 int cmd_help(int argc
, const char **argv
, const char *prefix __maybe_unused
)
429 bool show_all
= false;
430 enum help_format help_format
= HELP_FORMAT_MAN
;
431 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",
436 OPT_SET_UINT('i', "info", &help_format
, "show info page",
440 const char * const builtin_help_usage
[] = {
441 "perf help [--all] [--man|--web|--info] [command]",
447 load_command_list("perf-", &main_cmds
, &other_cmds
);
449 perf_config(perf_help_config
, &help_format
);
451 argc
= parse_options(argc
, argv
, builtin_help_options
,
452 builtin_help_usage
, 0);
455 printf("\n usage: %s\n\n", perf_usage_string
);
456 list_commands("perf commands", &main_cmds
, &other_cmds
);
457 printf(" %s\n\n", perf_more_info_string
);
462 printf("\n usage: %s\n\n", perf_usage_string
);
463 list_common_cmds_help();
464 printf("\n %s\n\n", perf_more_info_string
);
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
);
474 switch (help_format
) {
475 case HELP_FORMAT_MAN
:
476 rc
= show_man_page(argv
[0]);
478 case HELP_FORMAT_INFO
:
479 rc
= show_info_page(argv
[0]);
481 case HELP_FORMAT_WEB
:
482 rc
= show_html_page(argv
[0]);
484 case HELP_FORMAT_NONE
: