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