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" |
Ingo Molnar | 148be2c | 2009-04-27 08:02:14 +0200 | [diff] [blame] | 9 | #include "util/exec_cmd.h" |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 10 | #include "common-cmds.h" |
Ingo Molnar | 148be2c | 2009-04-27 08:02:14 +0200 | [diff] [blame] | 11 | #include "util/parse-options.h" |
| 12 | #include "util/run-command.h" |
| 13 | #include "util/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 | { |
| 106 | if (!check_emacsclient_version()) { |
| 107 | /* This works only with emacsclient version >= 22. */ |
| 108 | struct strbuf man_page = STRBUF_INIT; |
| 109 | |
| 110 | if (!path) |
| 111 | path = "emacsclient"; |
| 112 | strbuf_addf(&man_page, "(woman \"%s\")", page); |
| 113 | execlp(path, "emacsclient", "-e", man_page.buf, NULL); |
| 114 | warning("failed to exec '%s': %s", path, strerror(errno)); |
| 115 | } |
| 116 | } |
| 117 | |
Thiago Farina | c1e5301 | 2009-11-03 08:28:45 -0500 | [diff] [blame] | 118 | static void exec_man_konqueror(const char *path, const char *page) |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 119 | { |
| 120 | const char *display = getenv("DISPLAY"); |
| 121 | if (display && *display) { |
| 122 | struct strbuf man_page = STRBUF_INIT; |
| 123 | const char *filename = "kfmclient"; |
| 124 | |
| 125 | /* It's simpler to launch konqueror using kfmclient. */ |
| 126 | if (path) { |
| 127 | const char *file = strrchr(path, '/'); |
| 128 | if (file && !strcmp(file + 1, "konqueror")) { |
| 129 | char *new = strdup(path); |
| 130 | char *dest = strrchr(new, '/'); |
| 131 | |
| 132 | /* strlen("konqueror") == strlen("kfmclient") */ |
| 133 | strcpy(dest + 1, "kfmclient"); |
| 134 | path = new; |
| 135 | } |
| 136 | if (file) |
| 137 | filename = file; |
| 138 | } else |
| 139 | path = "kfmclient"; |
| 140 | strbuf_addf(&man_page, "man:%s(1)", page); |
| 141 | execlp(path, filename, "newTab", man_page.buf, NULL); |
| 142 | warning("failed to exec '%s': %s", path, strerror(errno)); |
| 143 | } |
| 144 | } |
| 145 | |
Thiago Farina | c1e5301 | 2009-11-03 08:28:45 -0500 | [diff] [blame] | 146 | static void exec_man_man(const char *path, const char *page) |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 147 | { |
| 148 | if (!path) |
| 149 | path = "man"; |
| 150 | execlp(path, "man", page, NULL); |
| 151 | warning("failed to exec '%s': %s", path, strerror(errno)); |
| 152 | } |
| 153 | |
| 154 | static void exec_man_cmd(const char *cmd, const char *page) |
| 155 | { |
| 156 | struct strbuf shell_cmd = STRBUF_INIT; |
| 157 | strbuf_addf(&shell_cmd, "%s %s", cmd, page); |
| 158 | execl("/bin/sh", "sh", "-c", shell_cmd.buf, NULL); |
| 159 | warning("failed to exec '%s': %s", cmd, strerror(errno)); |
| 160 | } |
| 161 | |
| 162 | static void add_man_viewer(const char *name) |
| 163 | { |
| 164 | struct man_viewer_list **p = &man_viewer_list; |
| 165 | size_t len = strlen(name); |
| 166 | |
| 167 | while (*p) |
| 168 | p = &((*p)->next); |
Arnaldo Carvalho de Melo | 3647948 | 2009-11-24 12:05:16 -0200 | [diff] [blame] | 169 | *p = zalloc(sizeof(**p) + len + 1); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 170 | strncpy((*p)->name, name, len); |
| 171 | } |
| 172 | |
| 173 | static int supported_man_viewer(const char *name, size_t len) |
| 174 | { |
| 175 | return (!strncasecmp("man", name, len) || |
| 176 | !strncasecmp("woman", name, len) || |
| 177 | !strncasecmp("konqueror", name, len)); |
| 178 | } |
| 179 | |
| 180 | static void do_add_man_viewer_info(const char *name, |
| 181 | size_t len, |
| 182 | const char *value) |
| 183 | { |
Arnaldo Carvalho de Melo | 3647948 | 2009-11-24 12:05:16 -0200 | [diff] [blame] | 184 | struct man_viewer_info_list *new = zalloc(sizeof(*new) + len + 1); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 185 | |
| 186 | strncpy(new->name, name, len); |
| 187 | new->info = strdup(value); |
| 188 | new->next = man_viewer_info_list; |
| 189 | man_viewer_info_list = new; |
| 190 | } |
| 191 | |
| 192 | static int add_man_viewer_path(const char *name, |
| 193 | size_t len, |
| 194 | const char *value) |
| 195 | { |
| 196 | if (supported_man_viewer(name, len)) |
| 197 | do_add_man_viewer_info(name, len, value); |
| 198 | else |
| 199 | warning("'%s': path for unsupported man viewer.\n" |
| 200 | "Please consider using 'man.<tool>.cmd' instead.", |
| 201 | name); |
| 202 | |
| 203 | return 0; |
| 204 | } |
| 205 | |
| 206 | static int add_man_viewer_cmd(const char *name, |
| 207 | size_t len, |
| 208 | const char *value) |
| 209 | { |
| 210 | if (supported_man_viewer(name, len)) |
| 211 | warning("'%s': cmd for supported man viewer.\n" |
| 212 | "Please consider using 'man.<tool>.path' instead.", |
| 213 | name); |
| 214 | else |
| 215 | do_add_man_viewer_info(name, len, value); |
| 216 | |
| 217 | return 0; |
| 218 | } |
| 219 | |
| 220 | static int add_man_viewer_info(const char *var, const char *value) |
| 221 | { |
| 222 | const char *name = var + 4; |
| 223 | const char *subkey = strrchr(name, '.'); |
| 224 | |
| 225 | if (!subkey) |
| 226 | return error("Config with no key for man viewer: %s", name); |
| 227 | |
| 228 | if (!strcmp(subkey, ".path")) { |
| 229 | if (!value) |
| 230 | return config_error_nonbool(var); |
| 231 | return add_man_viewer_path(name, subkey - name, value); |
| 232 | } |
| 233 | if (!strcmp(subkey, ".cmd")) { |
| 234 | if (!value) |
| 235 | return config_error_nonbool(var); |
| 236 | return add_man_viewer_cmd(name, subkey - name, value); |
| 237 | } |
| 238 | |
| 239 | warning("'%s': unsupported man viewer sub key.", subkey); |
| 240 | return 0; |
| 241 | } |
| 242 | |
| 243 | static int perf_help_config(const char *var, const char *value, void *cb) |
| 244 | { |
Arnaldo Carvalho de Melo | 2bae1d1 | 2012-10-01 15:20:58 -0300 | [diff] [blame] | 245 | enum help_format *help_formatp = cb; |
| 246 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 247 | if (!strcmp(var, "help.format")) { |
| 248 | if (!value) |
| 249 | return config_error_nonbool(var); |
Arnaldo Carvalho de Melo | 2bae1d1 | 2012-10-01 15:20:58 -0300 | [diff] [blame] | 250 | *help_formatp = parse_help_format(value); |
| 251 | if (*help_formatp == HELP_FORMAT_NONE) |
David Ahern | cc58482 | 2012-08-26 12:24:45 -0600 | [diff] [blame] | 252 | return -1; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 253 | return 0; |
| 254 | } |
| 255 | if (!strcmp(var, "man.viewer")) { |
| 256 | if (!value) |
| 257 | return config_error_nonbool(var); |
| 258 | add_man_viewer(value); |
| 259 | return 0; |
| 260 | } |
| 261 | if (!prefixcmp(var, "man.")) |
| 262 | return add_man_viewer_info(var, value); |
| 263 | |
| 264 | return perf_default_config(var, value, cb); |
| 265 | } |
| 266 | |
| 267 | static struct cmdnames main_cmds, other_cmds; |
| 268 | |
| 269 | void list_common_cmds_help(void) |
| 270 | { |
Ingo Molnar | f37a291 | 2009-07-01 12:37:06 +0200 | [diff] [blame] | 271 | unsigned int i, longest = 0; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 272 | |
| 273 | for (i = 0; i < ARRAY_SIZE(common_cmds); i++) { |
| 274 | if (longest < strlen(common_cmds[i].name)) |
| 275 | longest = strlen(common_cmds[i].name); |
| 276 | } |
| 277 | |
Ingo Molnar | 502fc5c | 2009-03-13 03:20:49 +0100 | [diff] [blame] | 278 | puts(" The most commonly used perf commands are:"); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 279 | for (i = 0; i < ARRAY_SIZE(common_cmds); i++) { |
Amerigo Wang | 3912f2a | 2009-12-14 03:23:56 -0500 | [diff] [blame] | 280 | printf(" %-*s ", longest, common_cmds[i].name); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 281 | puts(common_cmds[i].help); |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | static int is_perf_command(const char *s) |
| 286 | { |
| 287 | return is_in_cmdlist(&main_cmds, s) || |
| 288 | is_in_cmdlist(&other_cmds, s); |
| 289 | } |
| 290 | |
| 291 | static const char *prepend(const char *prefix, const char *cmd) |
| 292 | { |
| 293 | size_t pre_len = strlen(prefix); |
| 294 | size_t cmd_len = strlen(cmd); |
| 295 | char *p = malloc(pre_len + cmd_len + 1); |
| 296 | memcpy(p, prefix, pre_len); |
| 297 | strcpy(p + pre_len, cmd); |
| 298 | return p; |
| 299 | } |
| 300 | |
| 301 | static const char *cmd_to_page(const char *perf_cmd) |
| 302 | { |
| 303 | if (!perf_cmd) |
| 304 | return "perf"; |
| 305 | else if (!prefixcmp(perf_cmd, "perf")) |
| 306 | return perf_cmd; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 307 | else |
Ingo Molnar | c1c2365 | 2009-05-30 12:38:51 +0200 | [diff] [blame] | 308 | return prepend("perf-", perf_cmd); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | static void setup_man_path(void) |
| 312 | { |
| 313 | struct strbuf new_path = STRBUF_INIT; |
| 314 | const char *old_path = getenv("MANPATH"); |
| 315 | |
| 316 | /* We should always put ':' after our path. If there is no |
| 317 | * old_path, the ':' at the end will let 'man' to try |
| 318 | * system-wide paths after ours to find the manual page. If |
| 319 | * there is old_path, we need ':' as delimiter. */ |
| 320 | strbuf_addstr(&new_path, system_path(PERF_MAN_PATH)); |
| 321 | strbuf_addch(&new_path, ':'); |
| 322 | if (old_path) |
| 323 | strbuf_addstr(&new_path, old_path); |
| 324 | |
| 325 | setenv("MANPATH", new_path.buf, 1); |
| 326 | |
| 327 | strbuf_release(&new_path); |
| 328 | } |
| 329 | |
| 330 | static void exec_viewer(const char *name, const char *page) |
| 331 | { |
| 332 | const char *info = get_man_viewer_info(name); |
| 333 | |
| 334 | if (!strcasecmp(name, "man")) |
| 335 | exec_man_man(info, page); |
| 336 | else if (!strcasecmp(name, "woman")) |
| 337 | exec_woman_emacs(info, page); |
| 338 | else if (!strcasecmp(name, "konqueror")) |
| 339 | exec_man_konqueror(info, page); |
| 340 | else if (info) |
| 341 | exec_man_cmd(info, page); |
| 342 | else |
| 343 | warning("'%s': unknown man viewer.", name); |
| 344 | } |
| 345 | |
David Ahern | cc58482 | 2012-08-26 12:24:45 -0600 | [diff] [blame] | 346 | static int show_man_page(const char *perf_cmd) |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 347 | { |
| 348 | struct man_viewer_list *viewer; |
| 349 | const char *page = cmd_to_page(perf_cmd); |
| 350 | const char *fallback = getenv("PERF_MAN_VIEWER"); |
| 351 | |
| 352 | setup_man_path(); |
| 353 | for (viewer = man_viewer_list; viewer; viewer = viewer->next) |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 354 | exec_viewer(viewer->name, page); /* will return when unable */ |
Thiago Farina | c1e5301 | 2009-11-03 08:28:45 -0500 | [diff] [blame] | 355 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 356 | if (fallback) |
| 357 | exec_viewer(fallback, page); |
| 358 | exec_viewer("man", page); |
David Ahern | cc58482 | 2012-08-26 12:24:45 -0600 | [diff] [blame] | 359 | |
| 360 | pr_err("no man viewer handled the request"); |
| 361 | return -1; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 362 | } |
| 363 | |
David Ahern | cc58482 | 2012-08-26 12:24:45 -0600 | [diff] [blame] | 364 | static int show_info_page(const char *perf_cmd) |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 365 | { |
| 366 | const char *page = cmd_to_page(perf_cmd); |
| 367 | setenv("INFOPATH", system_path(PERF_INFO_PATH), 1); |
| 368 | execlp("info", "info", "perfman", page, NULL); |
David Ahern | cc58482 | 2012-08-26 12:24:45 -0600 | [diff] [blame] | 369 | return -1; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 370 | } |
| 371 | |
David Ahern | cc58482 | 2012-08-26 12:24:45 -0600 | [diff] [blame] | 372 | 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] | 373 | { |
| 374 | struct stat st; |
| 375 | const char *html_path = system_path(PERF_HTML_PATH); |
| 376 | |
| 377 | /* Check that we have a perf documentation directory. */ |
| 378 | if (stat(mkpath("%s/perf.html", html_path), &st) |
David Ahern | cc58482 | 2012-08-26 12:24:45 -0600 | [diff] [blame] | 379 | || !S_ISREG(st.st_mode)) { |
| 380 | pr_err("'%s': not a documentation directory.", html_path); |
| 381 | return -1; |
| 382 | } |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 383 | |
| 384 | strbuf_init(page_path, 0); |
| 385 | strbuf_addf(page_path, "%s/%s.html", html_path, page); |
David Ahern | cc58482 | 2012-08-26 12:24:45 -0600 | [diff] [blame] | 386 | |
| 387 | return 0; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | /* |
| 391 | * If open_html is not defined in a platform-specific way (see for |
| 392 | * example compat/mingw.h), we use the script web--browse to display |
| 393 | * HTML. |
| 394 | */ |
| 395 | #ifndef open_html |
Ingo Molnar | 16f762a | 2009-05-27 09:10:38 +0200 | [diff] [blame] | 396 | static void open_html(const char *path) |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 397 | { |
| 398 | execl_perf_cmd("web--browse", "-c", "help.browser", path, NULL); |
| 399 | } |
| 400 | #endif |
| 401 | |
David Ahern | cc58482 | 2012-08-26 12:24:45 -0600 | [diff] [blame] | 402 | static int show_html_page(const char *perf_cmd) |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 403 | { |
| 404 | const char *page = cmd_to_page(perf_cmd); |
| 405 | struct strbuf page_path; /* it leaks but we exec bellow */ |
| 406 | |
David Ahern | cc58482 | 2012-08-26 12:24:45 -0600 | [diff] [blame] | 407 | if (get_html_page_path(&page_path, page) != 0) |
| 408 | return -1; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 409 | |
| 410 | open_html(page_path.buf); |
David Ahern | cc58482 | 2012-08-26 12:24:45 -0600 | [diff] [blame] | 411 | |
| 412 | return 0; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 413 | } |
| 414 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 415 | 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] | 416 | { |
Arnaldo Carvalho de Melo | 2bae1d1 | 2012-10-01 15:20:58 -0300 | [diff] [blame] | 417 | bool show_all = false; |
Namhyung Kim | 670ab5d | 2012-10-22 16:12:23 +0900 | [diff] [blame] | 418 | enum help_format help_format = HELP_FORMAT_MAN; |
Arnaldo Carvalho de Melo | 2bae1d1 | 2012-10-01 15:20:58 -0300 | [diff] [blame] | 419 | struct option builtin_help_options[] = { |
| 420 | OPT_BOOLEAN('a', "all", &show_all, "print all available commands"), |
| 421 | OPT_SET_UINT('m', "man", &help_format, "show man page", HELP_FORMAT_MAN), |
| 422 | OPT_SET_UINT('w', "web", &help_format, "show manual in web browser", |
| 423 | HELP_FORMAT_WEB), |
| 424 | OPT_SET_UINT('i', "info", &help_format, "show info page", |
| 425 | HELP_FORMAT_INFO), |
| 426 | OPT_END(), |
| 427 | }; |
| 428 | const char * const builtin_help_usage[] = { |
| 429 | "perf help [--all] [--man|--web|--info] [command]", |
| 430 | NULL |
| 431 | }; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 432 | const char *alias; |
David Ahern | cc58482 | 2012-08-26 12:24:45 -0600 | [diff] [blame] | 433 | int rc = 0; |
Ingo Molnar | f37a291 | 2009-07-01 12:37:06 +0200 | [diff] [blame] | 434 | |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 435 | load_command_list("perf-", &main_cmds, &other_cmds); |
| 436 | |
Arnaldo Carvalho de Melo | 2bae1d1 | 2012-10-01 15:20:58 -0300 | [diff] [blame] | 437 | perf_config(perf_help_config, &help_format); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 438 | |
| 439 | argc = parse_options(argc, argv, builtin_help_options, |
| 440 | builtin_help_usage, 0); |
| 441 | |
| 442 | if (show_all) { |
Ingo Molnar | 502fc5c | 2009-03-13 03:20:49 +0100 | [diff] [blame] | 443 | printf("\n usage: %s\n\n", perf_usage_string); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 444 | list_commands("perf commands", &main_cmds, &other_cmds); |
Ingo Molnar | 502fc5c | 2009-03-13 03:20:49 +0100 | [diff] [blame] | 445 | printf(" %s\n\n", perf_more_info_string); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 446 | return 0; |
| 447 | } |
| 448 | |
| 449 | if (!argv[0]) { |
Ingo Molnar | 502fc5c | 2009-03-13 03:20:49 +0100 | [diff] [blame] | 450 | printf("\n usage: %s\n\n", perf_usage_string); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 451 | list_common_cmds_help(); |
Ingo Molnar | 502fc5c | 2009-03-13 03:20:49 +0100 | [diff] [blame] | 452 | printf("\n %s\n\n", perf_more_info_string); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 453 | return 0; |
| 454 | } |
| 455 | |
| 456 | alias = alias_lookup(argv[0]); |
| 457 | if (alias && !is_perf_command(argv[0])) { |
| 458 | printf("`perf %s' is aliased to `%s'\n", argv[0], alias); |
| 459 | return 0; |
| 460 | } |
| 461 | |
| 462 | switch (help_format) { |
| 463 | case HELP_FORMAT_MAN: |
David Ahern | cc58482 | 2012-08-26 12:24:45 -0600 | [diff] [blame] | 464 | rc = show_man_page(argv[0]); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 465 | break; |
| 466 | case HELP_FORMAT_INFO: |
David Ahern | cc58482 | 2012-08-26 12:24:45 -0600 | [diff] [blame] | 467 | rc = show_info_page(argv[0]); |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 468 | break; |
| 469 | case HELP_FORMAT_WEB: |
David Ahern | cc58482 | 2012-08-26 12:24:45 -0600 | [diff] [blame] | 470 | rc = show_html_page(argv[0]); |
| 471 | break; |
| 472 | case HELP_FORMAT_NONE: |
| 473 | /* fall-through */ |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 474 | default: |
David Ahern | cc58482 | 2012-08-26 12:24:45 -0600 | [diff] [blame] | 475 | rc = -1; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 476 | break; |
| 477 | } |
| 478 | |
David Ahern | cc58482 | 2012-08-26 12:24:45 -0600 | [diff] [blame] | 479 | return rc; |
Ingo Molnar | 0780060 | 2009-04-20 15:00:56 +0200 | [diff] [blame] | 480 | } |