Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 1 | /* |
| 2 | * builtin-help.c |
| 3 | * |
| 4 | * Builtin help command |
| 5 | */ |
Ingo Molnar | f37a291 | 2009-07-01 12:37:06 +0200 | [diff] [blame] | 6 | #include "perf.h" |
Ingo Molnar | 148be2c | 2009-04-27 08:02:14 +0200 | [diff] [blame] | 7 | #include "util/cache.h" |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 8 | #include "builtin.h" |
Josh Poimboeuf | 4b6ab94 | 2015-12-15 09:39:39 -0600 | [diff] [blame] | 9 | #include <subcmd/exec-cmd.h> |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 10 | #include "common-cmds.h" |
Josh Poimboeuf | 4b6ab94 | 2015-12-15 09:39:39 -0600 | [diff] [blame] | 11 | #include <subcmd/parse-options.h> |
| 12 | #include <subcmd/run-command.h> |
| 13 | #include <subcmd/help.h> |
Jiri Olsa | 84f5d36 | 2014-07-14 23:46:48 +0200 | [diff] [blame] | 14 | #include "util/debug.h" |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 15 | |
| 16 | static struct man_viewer_list { |
| 17 | struct man_viewer_list *next; |
| 18 | char name[FLEX_ARRAY]; |
| 19 | } *man_viewer_list; |
| 20 | |
| 21 | static struct man_viewer_info_list { |
| 22 | struct man_viewer_info_list *next; |
| 23 | const char *info; |
| 24 | char name[FLEX_ARRAY]; |
| 25 | } *man_viewer_info_list; |
| 26 | |
| 27 | enum help_format { |
David Ahern | cc58482 | 2012-08-26 12:24:45 -0600 | [diff] [blame] | 28 | HELP_FORMAT_NONE, |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 29 | HELP_FORMAT_MAN, |
| 30 | HELP_FORMAT_INFO, |
| 31 | HELP_FORMAT_WEB, |
| 32 | }; |
| 33 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 34 | static enum help_format parse_help_format(const char *format) |
| 35 | { |
| 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; |
David Ahern | cc58482 | 2012-08-26 12:24:45 -0600 | [diff] [blame] | 42 | |
| 43 | pr_err("unrecognized help format '%s'", format); |
| 44 | return HELP_FORMAT_NONE; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | static const char *get_man_viewer_info(const char *name) |
| 48 | { |
| 49 | struct man_viewer_info_list *viewer; |
| 50 | |
Thiago Farina | c1e5301 | 2009-11-03 08:28:45 -0500 | [diff] [blame] | 51 | for (viewer = man_viewer_info_list; viewer; viewer = viewer->next) { |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 52 | if (!strcasecmp(name, viewer->name)) |
| 53 | return viewer->info; |
| 54 | } |
| 55 | return NULL; |
| 56 | } |
| 57 | |
| 58 | static int check_emacsclient_version(void) |
| 59 | { |
| 60 | struct strbuf buffer = STRBUF_INIT; |
| 61 | struct child_process ec_process; |
| 62 | const char *argv_ec[] = { "emacsclient", "--version", NULL }; |
| 63 | int version; |
| 64 | |
| 65 | /* emacsclient prints its version number on stderr */ |
| 66 | memset(&ec_process, 0, sizeof(ec_process)); |
| 67 | ec_process.argv = argv_ec; |
| 68 | ec_process.err = -1; |
| 69 | ec_process.stdout_to_stderr = 1; |
| 70 | if (start_command(&ec_process)) { |
| 71 | fprintf(stderr, "Failed to start emacsclient.\n"); |
| 72 | return -1; |
| 73 | } |
| 74 | strbuf_read(&buffer, ec_process.err, 20); |
| 75 | close(ec_process.err); |
| 76 | |
| 77 | /* |
| 78 | * Don't bother checking return value, because "emacsclient --version" |
| 79 | * seems to always exits with code 1. |
| 80 | */ |
| 81 | finish_command(&ec_process); |
| 82 | |
| 83 | if (prefixcmp(buffer.buf, "emacsclient")) { |
| 84 | fprintf(stderr, "Failed to parse emacsclient version.\n"); |
| 85 | strbuf_release(&buffer); |
| 86 | return -1; |
| 87 | } |
| 88 | |
| 89 | strbuf_remove(&buffer, 0, strlen("emacsclient")); |
| 90 | version = atoi(buffer.buf); |
| 91 | |
| 92 | if (version < 22) { |
| 93 | fprintf(stderr, |
| 94 | "emacsclient version '%d' too old (< 22).\n", |
| 95 | version); |
| 96 | strbuf_release(&buffer); |
| 97 | return -1; |
| 98 | } |
| 99 | |
| 100 | strbuf_release(&buffer); |
| 101 | return 0; |
| 102 | } |
| 103 | |
Thiago Farina | c1e5301 | 2009-11-03 08:28:45 -0500 | [diff] [blame] | 104 | static void exec_woman_emacs(const char *path, const char *page) |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 105 | { |
Masami Hiramatsu | ede395d | 2014-08-14 02:22:53 +0000 | [diff] [blame] | 106 | char sbuf[STRERR_BUFSIZE]; |
| 107 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 108 | if (!check_emacsclient_version()) { |
| 109 | /* This works only with emacsclient version >= 22. */ |
| 110 | struct strbuf man_page = STRBUF_INIT; |
| 111 | |
| 112 | if (!path) |
| 113 | path = "emacsclient"; |
| 114 | strbuf_addf(&man_page, "(woman \"%s\")", page); |
| 115 | execlp(path, "emacsclient", "-e", man_page.buf, NULL); |
Masami Hiramatsu | ede395d | 2014-08-14 02:22:53 +0000 | [diff] [blame] | 116 | warning("failed to exec '%s': %s", path, |
| 117 | strerror_r(errno, sbuf, sizeof(sbuf))); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | |
Thiago Farina | c1e5301 | 2009-11-03 08:28:45 -0500 | [diff] [blame] | 121 | static void exec_man_konqueror(const char *path, const char *page) |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 122 | { |
| 123 | const char *display = getenv("DISPLAY"); |
Masami Hiramatsu | ede395d | 2014-08-14 02:22:53 +0000 | [diff] [blame] | 124 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 125 | if (display && *display) { |
| 126 | struct strbuf man_page = STRBUF_INIT; |
| 127 | const char *filename = "kfmclient"; |
Masami Hiramatsu | ede395d | 2014-08-14 02:22:53 +0000 | [diff] [blame] | 128 | char sbuf[STRERR_BUFSIZE]; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 129 | |
| 130 | /* It's simpler to launch konqueror using kfmclient. */ |
| 131 | if (path) { |
| 132 | const char *file = strrchr(path, '/'); |
| 133 | if (file && !strcmp(file + 1, "konqueror")) { |
| 134 | char *new = strdup(path); |
| 135 | char *dest = strrchr(new, '/'); |
| 136 | |
| 137 | /* strlen("konqueror") == strlen("kfmclient") */ |
| 138 | strcpy(dest + 1, "kfmclient"); |
| 139 | path = new; |
| 140 | } |
| 141 | if (file) |
| 142 | filename = file; |
| 143 | } else |
| 144 | path = "kfmclient"; |
| 145 | strbuf_addf(&man_page, "man:%s(1)", page); |
| 146 | execlp(path, filename, "newTab", man_page.buf, NULL); |
Masami Hiramatsu | ede395d | 2014-08-14 02:22:53 +0000 | [diff] [blame] | 147 | warning("failed to exec '%s': %s", path, |
| 148 | strerror_r(errno, sbuf, sizeof(sbuf))); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | |
Thiago Farina | c1e5301 | 2009-11-03 08:28:45 -0500 | [diff] [blame] | 152 | static void exec_man_man(const char *path, const char *page) |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 153 | { |
Masami Hiramatsu | ede395d | 2014-08-14 02:22:53 +0000 | [diff] [blame] | 154 | char sbuf[STRERR_BUFSIZE]; |
| 155 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 156 | if (!path) |
| 157 | path = "man"; |
| 158 | execlp(path, "man", page, NULL); |
Masami Hiramatsu | ede395d | 2014-08-14 02:22:53 +0000 | [diff] [blame] | 159 | warning("failed to exec '%s': %s", path, |
| 160 | strerror_r(errno, sbuf, sizeof(sbuf))); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | static void exec_man_cmd(const char *cmd, const char *page) |
| 164 | { |
| 165 | struct strbuf shell_cmd = STRBUF_INIT; |
Masami Hiramatsu | ede395d | 2014-08-14 02:22:53 +0000 | [diff] [blame] | 166 | char sbuf[STRERR_BUFSIZE]; |
| 167 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 168 | strbuf_addf(&shell_cmd, "%s %s", cmd, page); |
| 169 | execl("/bin/sh", "sh", "-c", shell_cmd.buf, NULL); |
Masami Hiramatsu | ede395d | 2014-08-14 02:22:53 +0000 | [diff] [blame] | 170 | warning("failed to exec '%s': %s", cmd, |
| 171 | strerror_r(errno, sbuf, sizeof(sbuf))); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | static void add_man_viewer(const char *name) |
| 175 | { |
| 176 | struct man_viewer_list **p = &man_viewer_list; |
| 177 | size_t len = strlen(name); |
| 178 | |
| 179 | while (*p) |
| 180 | p = &((*p)->next); |
Arnaldo Carvalho de Melo | 3647948 | 2009-11-24 12:05:16 -0200 | [diff] [blame] | 181 | *p = zalloc(sizeof(**p) + len + 1); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 182 | strncpy((*p)->name, name, len); |
| 183 | } |
| 184 | |
| 185 | static int supported_man_viewer(const char *name, size_t len) |
| 186 | { |
| 187 | return (!strncasecmp("man", name, len) || |
| 188 | !strncasecmp("woman", name, len) || |
| 189 | !strncasecmp("konqueror", name, len)); |
| 190 | } |
| 191 | |
| 192 | static void do_add_man_viewer_info(const char *name, |
| 193 | size_t len, |
| 194 | const char *value) |
| 195 | { |
Arnaldo Carvalho de Melo | 3647948 | 2009-11-24 12:05:16 -0200 | [diff] [blame] | 196 | struct man_viewer_info_list *new = zalloc(sizeof(*new) + len + 1); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 197 | |
| 198 | strncpy(new->name, name, len); |
| 199 | new->info = strdup(value); |
| 200 | new->next = man_viewer_info_list; |
| 201 | man_viewer_info_list = new; |
| 202 | } |
| 203 | |
| 204 | static int add_man_viewer_path(const char *name, |
| 205 | size_t len, |
| 206 | const char *value) |
| 207 | { |
| 208 | if (supported_man_viewer(name, len)) |
| 209 | do_add_man_viewer_info(name, len, value); |
| 210 | else |
| 211 | warning("'%s': path for unsupported man viewer.\n" |
| 212 | "Please consider using 'man.<tool>.cmd' instead.", |
| 213 | name); |
| 214 | |
| 215 | return 0; |
| 216 | } |
| 217 | |
| 218 | static int add_man_viewer_cmd(const char *name, |
| 219 | size_t len, |
| 220 | const char *value) |
| 221 | { |
| 222 | if (supported_man_viewer(name, len)) |
| 223 | warning("'%s': cmd for supported man viewer.\n" |
| 224 | "Please consider using 'man.<tool>.path' instead.", |
| 225 | name); |
| 226 | else |
| 227 | do_add_man_viewer_info(name, len, value); |
| 228 | |
| 229 | return 0; |
| 230 | } |
| 231 | |
| 232 | static int add_man_viewer_info(const char *var, const char *value) |
| 233 | { |
| 234 | const char *name = var + 4; |
| 235 | const char *subkey = strrchr(name, '.'); |
| 236 | |
| 237 | if (!subkey) |
| 238 | return error("Config with no key for man viewer: %s", name); |
| 239 | |
| 240 | if (!strcmp(subkey, ".path")) { |
| 241 | if (!value) |
| 242 | return config_error_nonbool(var); |
| 243 | return add_man_viewer_path(name, subkey - name, value); |
| 244 | } |
| 245 | if (!strcmp(subkey, ".cmd")) { |
| 246 | if (!value) |
| 247 | return config_error_nonbool(var); |
| 248 | return add_man_viewer_cmd(name, subkey - name, value); |
| 249 | } |
| 250 | |
| 251 | warning("'%s': unsupported man viewer sub key.", subkey); |
| 252 | return 0; |
| 253 | } |
| 254 | |
| 255 | static int perf_help_config(const char *var, const char *value, void *cb) |
| 256 | { |
Arnaldo Carvalho de Melo | 2bae1d1 | 2012-10-01 15:20:58 -0300 | [diff] [blame] | 257 | enum help_format *help_formatp = cb; |
| 258 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 259 | if (!strcmp(var, "help.format")) { |
| 260 | if (!value) |
| 261 | return config_error_nonbool(var); |
Arnaldo Carvalho de Melo | 2bae1d1 | 2012-10-01 15:20:58 -0300 | [diff] [blame] | 262 | *help_formatp = parse_help_format(value); |
| 263 | if (*help_formatp == HELP_FORMAT_NONE) |
David Ahern | cc58482 | 2012-08-26 12:24:45 -0600 | [diff] [blame] | 264 | return -1; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 265 | return 0; |
| 266 | } |
| 267 | if (!strcmp(var, "man.viewer")) { |
| 268 | if (!value) |
| 269 | return config_error_nonbool(var); |
| 270 | add_man_viewer(value); |
| 271 | return 0; |
| 272 | } |
| 273 | if (!prefixcmp(var, "man.")) |
| 274 | return add_man_viewer_info(var, value); |
| 275 | |
| 276 | return perf_default_config(var, value, cb); |
| 277 | } |
| 278 | |
| 279 | static struct cmdnames main_cmds, other_cmds; |
| 280 | |
| 281 | void list_common_cmds_help(void) |
| 282 | { |
Ingo Molnar | f37a291 | 2009-07-01 12:37:06 +0200 | [diff] [blame] | 283 | unsigned int i, longest = 0; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 284 | |
| 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); |
| 288 | } |
| 289 | |
Ingo Molnar | 502fc5c | 2009-03-13 03:20:49 +0100 | [diff] [blame] | 290 | puts(" The most commonly used perf commands are:"); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 291 | for (i = 0; i < ARRAY_SIZE(common_cmds); i++) { |
Amerigo Wang | 3912f2a | 2009-12-14 03:23:56 -0500 | [diff] [blame] | 292 | printf(" %-*s ", longest, common_cmds[i].name); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 293 | puts(common_cmds[i].help); |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | static int is_perf_command(const char *s) |
| 298 | { |
| 299 | return is_in_cmdlist(&main_cmds, s) || |
| 300 | is_in_cmdlist(&other_cmds, s); |
| 301 | } |
| 302 | |
| 303 | static const char *prepend(const char *prefix, const char *cmd) |
| 304 | { |
| 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); |
| 310 | return p; |
| 311 | } |
| 312 | |
| 313 | static const char *cmd_to_page(const char *perf_cmd) |
| 314 | { |
| 315 | if (!perf_cmd) |
| 316 | return "perf"; |
| 317 | else if (!prefixcmp(perf_cmd, "perf")) |
| 318 | return perf_cmd; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 319 | else |
Ingo Molnar | c1c2365 | 2009-05-30 12:38:51 +0200 | [diff] [blame] | 320 | return prepend("perf-", perf_cmd); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | static void setup_man_path(void) |
| 324 | { |
| 325 | struct strbuf new_path = STRBUF_INIT; |
| 326 | const char *old_path = getenv("MANPATH"); |
| 327 | |
| 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, ':'); |
| 334 | if (old_path) |
| 335 | strbuf_addstr(&new_path, old_path); |
| 336 | |
| 337 | setenv("MANPATH", new_path.buf, 1); |
| 338 | |
| 339 | strbuf_release(&new_path); |
| 340 | } |
| 341 | |
| 342 | static void exec_viewer(const char *name, const char *page) |
| 343 | { |
| 344 | const char *info = get_man_viewer_info(name); |
| 345 | |
| 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); |
| 352 | else if (info) |
| 353 | exec_man_cmd(info, page); |
| 354 | else |
| 355 | warning("'%s': unknown man viewer.", name); |
| 356 | } |
| 357 | |
David Ahern | cc58482 | 2012-08-26 12:24:45 -0600 | [diff] [blame] | 358 | static int show_man_page(const char *perf_cmd) |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 359 | { |
| 360 | struct man_viewer_list *viewer; |
| 361 | const char *page = cmd_to_page(perf_cmd); |
| 362 | const char *fallback = getenv("PERF_MAN_VIEWER"); |
| 363 | |
| 364 | setup_man_path(); |
| 365 | for (viewer = man_viewer_list; viewer; viewer = viewer->next) |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 366 | exec_viewer(viewer->name, page); /* will return when unable */ |
Thiago Farina | c1e5301 | 2009-11-03 08:28:45 -0500 | [diff] [blame] | 367 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 368 | if (fallback) |
| 369 | exec_viewer(fallback, page); |
| 370 | exec_viewer("man", page); |
David Ahern | cc58482 | 2012-08-26 12:24:45 -0600 | [diff] [blame] | 371 | |
| 372 | pr_err("no man viewer handled the request"); |
| 373 | return -1; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 374 | } |
| 375 | |
David Ahern | cc58482 | 2012-08-26 12:24:45 -0600 | [diff] [blame] | 376 | static int show_info_page(const char *perf_cmd) |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 377 | { |
| 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); |
David Ahern | cc58482 | 2012-08-26 12:24:45 -0600 | [diff] [blame] | 381 | return -1; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 382 | } |
| 383 | |
David Ahern | cc58482 | 2012-08-26 12:24:45 -0600 | [diff] [blame] | 384 | static int get_html_page_path(struct strbuf *page_path, const char *page) |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 385 | { |
| 386 | struct stat st; |
| 387 | const char *html_path = system_path(PERF_HTML_PATH); |
| 388 | |
| 389 | /* Check that we have a perf documentation directory. */ |
| 390 | if (stat(mkpath("%s/perf.html", html_path), &st) |
David Ahern | cc58482 | 2012-08-26 12:24:45 -0600 | [diff] [blame] | 391 | || !S_ISREG(st.st_mode)) { |
| 392 | pr_err("'%s': not a documentation directory.", html_path); |
| 393 | return -1; |
| 394 | } |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 395 | |
| 396 | strbuf_init(page_path, 0); |
| 397 | strbuf_addf(page_path, "%s/%s.html", html_path, page); |
David Ahern | cc58482 | 2012-08-26 12:24:45 -0600 | [diff] [blame] | 398 | |
| 399 | return 0; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | /* |
| 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 |
| 405 | * HTML. |
| 406 | */ |
| 407 | #ifndef open_html |
Ingo Molnar | 16f762a | 2009-05-27 09:10:38 +0200 | [diff] [blame] | 408 | static void open_html(const char *path) |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 409 | { |
Josh Poimboeuf | 46113a5 | 2015-12-15 09:39:37 -0600 | [diff] [blame] | 410 | execl_cmd("web--browse", "-c", "help.browser", path, NULL); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 411 | } |
| 412 | #endif |
| 413 | |
David Ahern | cc58482 | 2012-08-26 12:24:45 -0600 | [diff] [blame] | 414 | static int show_html_page(const char *perf_cmd) |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 415 | { |
| 416 | const char *page = cmd_to_page(perf_cmd); |
| 417 | struct strbuf page_path; /* it leaks but we exec bellow */ |
| 418 | |
David Ahern | cc58482 | 2012-08-26 12:24:45 -0600 | [diff] [blame] | 419 | if (get_html_page_path(&page_path, page) != 0) |
| 420 | return -1; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 421 | |
| 422 | open_html(page_path.buf); |
David Ahern | cc58482 | 2012-08-26 12:24:45 -0600 | [diff] [blame] | 423 | |
| 424 | return 0; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 425 | } |
| 426 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 427 | int cmd_help(int argc, const char **argv, const char *prefix __maybe_unused) |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 428 | { |
Arnaldo Carvalho de Melo | 2bae1d1 | 2012-10-01 15:20:58 -0300 | [diff] [blame] | 429 | bool show_all = false; |
Namhyung Kim | 670ab5d | 2012-10-22 16:12:23 +0900 | [diff] [blame] | 430 | enum help_format help_format = HELP_FORMAT_MAN; |
Arnaldo Carvalho de Melo | 2bae1d1 | 2012-10-01 15:20:58 -0300 | [diff] [blame] | 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", |
| 435 | HELP_FORMAT_WEB), |
| 436 | OPT_SET_UINT('i', "info", &help_format, "show info page", |
| 437 | HELP_FORMAT_INFO), |
| 438 | OPT_END(), |
| 439 | }; |
Yunlong Song | e24a110 | 2015-03-18 21:35:53 +0800 | [diff] [blame] | 440 | const char * const builtin_help_subcommands[] = { |
| 441 | "buildid-cache", "buildid-list", "diff", "evlist", "help", "list", |
| 442 | "record", "report", "bench", "stat", "timechart", "top", "annotate", |
| 443 | "script", "sched", "kmem", "lock", "kvm", "test", "inject", "mem", "data", |
| 444 | #ifdef HAVE_LIBELF_SUPPORT |
| 445 | "probe", |
| 446 | #endif |
| 447 | #ifdef HAVE_LIBAUDIT_SUPPORT |
| 448 | "trace", |
| 449 | #endif |
| 450 | NULL }; |
| 451 | const char *builtin_help_usage[] = { |
Arnaldo Carvalho de Melo | 2bae1d1 | 2012-10-01 15:20:58 -0300 | [diff] [blame] | 452 | "perf help [--all] [--man|--web|--info] [command]", |
| 453 | NULL |
| 454 | }; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 455 | const char *alias; |
David Ahern | cc58482 | 2012-08-26 12:24:45 -0600 | [diff] [blame] | 456 | int rc = 0; |
Ingo Molnar | f37a291 | 2009-07-01 12:37:06 +0200 | [diff] [blame] | 457 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 458 | load_command_list("perf-", &main_cmds, &other_cmds); |
| 459 | |
Arnaldo Carvalho de Melo | 2bae1d1 | 2012-10-01 15:20:58 -0300 | [diff] [blame] | 460 | perf_config(perf_help_config, &help_format); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 461 | |
Yunlong Song | e24a110 | 2015-03-18 21:35:53 +0800 | [diff] [blame] | 462 | argc = parse_options_subcommand(argc, argv, builtin_help_options, |
| 463 | builtin_help_subcommands, builtin_help_usage, 0); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 464 | |
| 465 | if (show_all) { |
Yunlong Song | 3a134ae | 2015-10-15 15:39:51 +0800 | [diff] [blame] | 466 | printf("\n Usage: %s\n\n", perf_usage_string); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 467 | list_commands("perf commands", &main_cmds, &other_cmds); |
Ingo Molnar | 502fc5c | 2009-03-13 03:20:49 +0100 | [diff] [blame] | 468 | printf(" %s\n\n", perf_more_info_string); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 469 | return 0; |
| 470 | } |
| 471 | |
| 472 | if (!argv[0]) { |
Ingo Molnar | 502fc5c | 2009-03-13 03:20:49 +0100 | [diff] [blame] | 473 | printf("\n usage: %s\n\n", perf_usage_string); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 474 | list_common_cmds_help(); |
Ingo Molnar | 502fc5c | 2009-03-13 03:20:49 +0100 | [diff] [blame] | 475 | printf("\n %s\n\n", perf_more_info_string); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 476 | return 0; |
| 477 | } |
| 478 | |
| 479 | alias = alias_lookup(argv[0]); |
| 480 | if (alias && !is_perf_command(argv[0])) { |
| 481 | printf("`perf %s' is aliased to `%s'\n", argv[0], alias); |
| 482 | return 0; |
| 483 | } |
| 484 | |
| 485 | switch (help_format) { |
| 486 | case HELP_FORMAT_MAN: |
David Ahern | cc58482 | 2012-08-26 12:24:45 -0600 | [diff] [blame] | 487 | rc = show_man_page(argv[0]); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 488 | break; |
| 489 | case HELP_FORMAT_INFO: |
David Ahern | cc58482 | 2012-08-26 12:24:45 -0600 | [diff] [blame] | 490 | rc = show_info_page(argv[0]); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 491 | break; |
| 492 | case HELP_FORMAT_WEB: |
David Ahern | cc58482 | 2012-08-26 12:24:45 -0600 | [diff] [blame] | 493 | rc = show_html_page(argv[0]); |
| 494 | break; |
| 495 | case HELP_FORMAT_NONE: |
| 496 | /* fall-through */ |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 497 | default: |
David Ahern | cc58482 | 2012-08-26 12:24:45 -0600 | [diff] [blame] | 498 | rc = -1; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 499 | break; |
| 500 | } |
| 501 | |
David Ahern | cc58482 | 2012-08-26 12:24:45 -0600 | [diff] [blame] | 502 | return rc; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 503 | } |