Merge branch 'sfi-release' of git://git.kernel.org/pub/scm/linux/kernel/git/lenb...
[linux-2.6/x86.git] / tools / perf / builtin-help.c
blob4fb8734a796e992513cbae3f0cb2a5d8851ec113
1 /*
2 * builtin-help.c
4 * Builtin help command
5 */
6 #include "perf.h"
7 #include "util/cache.h"
8 #include "builtin.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];
18 } *man_viewer_list;
20 static 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;
26 enum help_format {
27 HELP_FORMAT_MAN,
28 HELP_FORMAT_INFO,
29 HELP_FORMAT_WEB,
32 static int show_all = 0;
33 static enum help_format help_format = HELP_FORMAT_MAN;
34 static struct option builtin_help_options[] = {
35 OPT_BOOLEAN('a', "all", &show_all, "print all available commands"),
36 OPT_SET_INT('m', "man", &help_format, "show man page", HELP_FORMAT_MAN),
37 OPT_SET_INT('w', "web", &help_format, "show manual in web browser",
38 HELP_FORMAT_WEB),
39 OPT_SET_INT('i', "info", &help_format, "show info page",
40 HELP_FORMAT_INFO),
41 OPT_END(),
44 static const char * const builtin_help_usage[] = {
45 "perf help [--all] [--man|--web|--info] [command]",
46 NULL
49 static enum help_format parse_help_format(const char *format)
51 if (!strcmp(format, "man"))
52 return HELP_FORMAT_MAN;
53 if (!strcmp(format, "info"))
54 return HELP_FORMAT_INFO;
55 if (!strcmp(format, "web") || !strcmp(format, "html"))
56 return HELP_FORMAT_WEB;
57 die("unrecognized help format '%s'", format);
60 static const char *get_man_viewer_info(const char *name)
62 struct man_viewer_info_list *viewer;
64 for (viewer = man_viewer_info_list; viewer; viewer = viewer->next)
66 if (!strcasecmp(name, viewer->name))
67 return viewer->info;
69 return NULL;
72 static int check_emacsclient_version(void)
74 struct strbuf buffer = STRBUF_INIT;
75 struct child_process ec_process;
76 const char *argv_ec[] = { "emacsclient", "--version", NULL };
77 int version;
79 /* emacsclient prints its version number on stderr */
80 memset(&ec_process, 0, sizeof(ec_process));
81 ec_process.argv = argv_ec;
82 ec_process.err = -1;
83 ec_process.stdout_to_stderr = 1;
84 if (start_command(&ec_process)) {
85 fprintf(stderr, "Failed to start emacsclient.\n");
86 return -1;
88 strbuf_read(&buffer, ec_process.err, 20);
89 close(ec_process.err);
92 * Don't bother checking return value, because "emacsclient --version"
93 * seems to always exits with code 1.
95 finish_command(&ec_process);
97 if (prefixcmp(buffer.buf, "emacsclient")) {
98 fprintf(stderr, "Failed to parse emacsclient version.\n");
99 strbuf_release(&buffer);
100 return -1;
103 strbuf_remove(&buffer, 0, strlen("emacsclient"));
104 version = atoi(buffer.buf);
106 if (version < 22) {
107 fprintf(stderr,
108 "emacsclient version '%d' too old (< 22).\n",
109 version);
110 strbuf_release(&buffer);
111 return -1;
114 strbuf_release(&buffer);
115 return 0;
118 static void exec_woman_emacs(const char* path, const char *page)
120 if (!check_emacsclient_version()) {
121 /* This works only with emacsclient version >= 22. */
122 struct strbuf man_page = STRBUF_INIT;
124 if (!path)
125 path = "emacsclient";
126 strbuf_addf(&man_page, "(woman \"%s\")", page);
127 execlp(path, "emacsclient", "-e", man_page.buf, NULL);
128 warning("failed to exec '%s': %s", path, strerror(errno));
132 static void exec_man_konqueror(const char* path, const char *page)
134 const char *display = getenv("DISPLAY");
135 if (display && *display) {
136 struct strbuf man_page = STRBUF_INIT;
137 const char *filename = "kfmclient";
139 /* It's simpler to launch konqueror using kfmclient. */
140 if (path) {
141 const char *file = strrchr(path, '/');
142 if (file && !strcmp(file + 1, "konqueror")) {
143 char *new = strdup(path);
144 char *dest = strrchr(new, '/');
146 /* strlen("konqueror") == strlen("kfmclient") */
147 strcpy(dest + 1, "kfmclient");
148 path = new;
150 if (file)
151 filename = file;
152 } else
153 path = "kfmclient";
154 strbuf_addf(&man_page, "man:%s(1)", page);
155 execlp(path, filename, "newTab", man_page.buf, NULL);
156 warning("failed to exec '%s': %s", path, strerror(errno));
160 static void exec_man_man(const char* path, const char *page)
162 if (!path)
163 path = "man";
164 execlp(path, "man", page, NULL);
165 warning("failed to exec '%s': %s", path, strerror(errno));
168 static void exec_man_cmd(const char *cmd, const char *page)
170 struct strbuf shell_cmd = STRBUF_INIT;
171 strbuf_addf(&shell_cmd, "%s %s", cmd, page);
172 execl("/bin/sh", "sh", "-c", shell_cmd.buf, NULL);
173 warning("failed to exec '%s': %s", cmd, strerror(errno));
176 static void add_man_viewer(const char *name)
178 struct man_viewer_list **p = &man_viewer_list;
179 size_t len = strlen(name);
181 while (*p)
182 p = &((*p)->next);
183 *p = calloc(1, (sizeof(**p) + len + 1));
184 strncpy((*p)->name, name, len);
187 static int supported_man_viewer(const char *name, size_t len)
189 return (!strncasecmp("man", name, len) ||
190 !strncasecmp("woman", name, len) ||
191 !strncasecmp("konqueror", name, len));
194 static void do_add_man_viewer_info(const char *name,
195 size_t len,
196 const char *value)
198 struct man_viewer_info_list *new = calloc(1, sizeof(*new) + len + 1);
200 strncpy(new->name, name, len);
201 new->info = strdup(value);
202 new->next = man_viewer_info_list;
203 man_viewer_info_list = new;
206 static int add_man_viewer_path(const char *name,
207 size_t len,
208 const char *value)
210 if (supported_man_viewer(name, len))
211 do_add_man_viewer_info(name, len, value);
212 else
213 warning("'%s': path for unsupported man viewer.\n"
214 "Please consider using 'man.<tool>.cmd' instead.",
215 name);
217 return 0;
220 static int add_man_viewer_cmd(const char *name,
221 size_t len,
222 const char *value)
224 if (supported_man_viewer(name, len))
225 warning("'%s': cmd for supported man viewer.\n"
226 "Please consider using 'man.<tool>.path' instead.",
227 name);
228 else
229 do_add_man_viewer_info(name, len, value);
231 return 0;
234 static int add_man_viewer_info(const char *var, const char *value)
236 const char *name = var + 4;
237 const char *subkey = strrchr(name, '.');
239 if (!subkey)
240 return error("Config with no key for man viewer: %s", name);
242 if (!strcmp(subkey, ".path")) {
243 if (!value)
244 return config_error_nonbool(var);
245 return add_man_viewer_path(name, subkey - name, value);
247 if (!strcmp(subkey, ".cmd")) {
248 if (!value)
249 return config_error_nonbool(var);
250 return add_man_viewer_cmd(name, subkey - name, value);
253 warning("'%s': unsupported man viewer sub key.", subkey);
254 return 0;
257 static int perf_help_config(const char *var, const char *value, void *cb)
259 if (!strcmp(var, "help.format")) {
260 if (!value)
261 return config_error_nonbool(var);
262 help_format = parse_help_format(value);
263 return 0;
265 if (!strcmp(var, "man.viewer")) {
266 if (!value)
267 return config_error_nonbool(var);
268 add_man_viewer(value);
269 return 0;
271 if (!prefixcmp(var, "man."))
272 return add_man_viewer_info(var, value);
274 return perf_default_config(var, value, cb);
277 static struct cmdnames main_cmds, other_cmds;
279 void list_common_cmds_help(void)
281 unsigned int i, longest = 0;
283 for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
284 if (longest < strlen(common_cmds[i].name))
285 longest = strlen(common_cmds[i].name);
288 puts(" The most commonly used perf commands are:");
289 for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
290 printf(" %s ", common_cmds[i].name);
291 mput_char(' ', longest - strlen(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);
309 return p;
312 static const char *cmd_to_page(const char *perf_cmd)
314 if (!perf_cmd)
315 return "perf";
316 else if (!prefixcmp(perf_cmd, "perf"))
317 return perf_cmd;
318 else if (is_perf_command(perf_cmd))
319 return prepend("perf-", perf_cmd);
320 else
321 return prepend("perf-", perf_cmd);
324 static void setup_man_path(void)
326 struct strbuf new_path = STRBUF_INIT;
327 const char *old_path = getenv("MANPATH");
329 /* We should always put ':' after our path. If there is no
330 * old_path, the ':' at the end will let 'man' to try
331 * system-wide paths after ours to find the manual page. If
332 * there is old_path, we need ':' as delimiter. */
333 strbuf_addstr(&new_path, system_path(PERF_MAN_PATH));
334 strbuf_addch(&new_path, ':');
335 if (old_path)
336 strbuf_addstr(&new_path, old_path);
338 setenv("MANPATH", new_path.buf, 1);
340 strbuf_release(&new_path);
343 static void exec_viewer(const char *name, const char *page)
345 const char *info = get_man_viewer_info(name);
347 if (!strcasecmp(name, "man"))
348 exec_man_man(info, page);
349 else if (!strcasecmp(name, "woman"))
350 exec_woman_emacs(info, page);
351 else if (!strcasecmp(name, "konqueror"))
352 exec_man_konqueror(info, page);
353 else if (info)
354 exec_man_cmd(info, page);
355 else
356 warning("'%s': unknown man viewer.", name);
359 static void show_man_page(const char *perf_cmd)
361 struct man_viewer_list *viewer;
362 const char *page = cmd_to_page(perf_cmd);
363 const char *fallback = getenv("PERF_MAN_VIEWER");
365 setup_man_path();
366 for (viewer = man_viewer_list; viewer; viewer = viewer->next)
368 exec_viewer(viewer->name, page); /* will return when unable */
370 if (fallback)
371 exec_viewer(fallback, page);
372 exec_viewer("man", page);
373 die("no man viewer handled the request");
376 static void 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);
383 static void get_html_page_path(struct strbuf *page_path, const char *page)
385 struct stat st;
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 die("'%s': not a documentation directory.", html_path);
393 strbuf_init(page_path, 0);
394 strbuf_addf(page_path, "%s/%s.html", html_path, page);
398 * If open_html is not defined in a platform-specific way (see for
399 * example compat/mingw.h), we use the script web--browse to display
400 * HTML.
402 #ifndef open_html
403 static void open_html(const char *path)
405 execl_perf_cmd("web--browse", "-c", "help.browser", path, NULL);
407 #endif
409 static void show_html_page(const char *perf_cmd)
411 const char *page = cmd_to_page(perf_cmd);
412 struct strbuf page_path; /* it leaks but we exec bellow */
414 get_html_page_path(&page_path, page);
416 open_html(page_path.buf);
419 int cmd_help(int argc, const char **argv, const char *prefix __used)
421 const char *alias;
423 load_command_list("perf-", &main_cmds, &other_cmds);
425 perf_config(perf_help_config, NULL);
427 argc = parse_options(argc, argv, builtin_help_options,
428 builtin_help_usage, 0);
430 if (show_all) {
431 printf("\n usage: %s\n\n", perf_usage_string);
432 list_commands("perf commands", &main_cmds, &other_cmds);
433 printf(" %s\n\n", perf_more_info_string);
434 return 0;
437 if (!argv[0]) {
438 printf("\n usage: %s\n\n", perf_usage_string);
439 list_common_cmds_help();
440 printf("\n %s\n\n", perf_more_info_string);
441 return 0;
444 alias = alias_lookup(argv[0]);
445 if (alias && !is_perf_command(argv[0])) {
446 printf("`perf %s' is aliased to `%s'\n", argv[0], alias);
447 return 0;
450 switch (help_format) {
451 case HELP_FORMAT_MAN:
452 show_man_page(argv[0]);
453 break;
454 case HELP_FORMAT_INFO:
455 show_info_page(argv[0]);
456 break;
457 case HELP_FORMAT_WEB:
458 show_html_page(argv[0]);
459 default:
460 break;
463 return 0;