1 /* 2 * builtin-help.c 3 * 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" 14 15 static struct man_viewer_list { 16 struct man_viewer_list *next; 17 char name[FLEX_ARRAY]; 18 } *man_viewer_list; 19 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; 25 26 enum help_format { 27 HELP_FORMAT_NONE, 28 HELP_FORMAT_MAN, 29 HELP_FORMAT_INFO, 30 HELP_FORMAT_WEB, 31 }; 32 33 static bool show_all = false; 34 static enum help_format help_format = HELP_FORMAT_NONE; 35 static struct option builtin_help_options[] = { 36 OPT_BOOLEAN('a', "all", &show_all, "print all available commands"), 37 OPT_SET_UINT('m', "man", &help_format, "show man page", HELP_FORMAT_MAN), 38 OPT_SET_UINT('w', "web", &help_format, "show manual in web browser", 39 HELP_FORMAT_WEB), 40 OPT_SET_UINT('i', "info", &help_format, "show info page", 41 HELP_FORMAT_INFO), 42 OPT_END(), 43 }; 44 45 static const char * const builtin_help_usage[] = { 46 "perf help [--all] [--man|--web|--info] [command]", 47 NULL 48 }; 49 50 static enum help_format parse_help_format(const char *format) 51 { 52 if (!strcmp(format, "man")) 53 return HELP_FORMAT_MAN; 54 if (!strcmp(format, "info")) 55 return HELP_FORMAT_INFO; 56 if (!strcmp(format, "web") || !strcmp(format, "html")) 57 return HELP_FORMAT_WEB; 58 59 pr_err("unrecognized help format '%s'", format); 60 return HELP_FORMAT_NONE; 61 } 62 63 static const char *get_man_viewer_info(const char *name) 64 { 65 struct man_viewer_info_list *viewer; 66 67 for (viewer = man_viewer_info_list; viewer; viewer = viewer->next) { 68 if (!strcasecmp(name, viewer->name)) 69 return viewer->info; 70 } 71 return NULL; 72 } 73 74 static int check_emacsclient_version(void) 75 { 76 struct strbuf buffer = STRBUF_INIT; 77 struct child_process ec_process; 78 const char *argv_ec[] = { "emacsclient", "--version", NULL }; 79 int version; 80 81 /* emacsclient prints its version number on stderr */ 82 memset(&ec_process, 0, sizeof(ec_process)); 83 ec_process.argv = argv_ec; 84 ec_process.err = -1; 85 ec_process.stdout_to_stderr = 1; 86 if (start_command(&ec_process)) { 87 fprintf(stderr, "Failed to start emacsclient.\n"); 88 return -1; 89 } 90 strbuf_read(&buffer, ec_process.err, 20); 91 close(ec_process.err); 92 93 /* 94 * Don't bother checking return value, because "emacsclient --version" 95 * seems to always exits with code 1. 96 */ 97 finish_command(&ec_process); 98 99 if (prefixcmp(buffer.buf, "emacsclient")) { 100 fprintf(stderr, "Failed to parse emacsclient version.\n"); 101 strbuf_release(&buffer); 102 return -1; 103 } 104 105 strbuf_remove(&buffer, 0, strlen("emacsclient")); 106 version = atoi(buffer.buf); 107 108 if (version < 22) { 109 fprintf(stderr, 110 "emacsclient version '%d' too old (< 22).\n", 111 version); 112 strbuf_release(&buffer); 113 return -1; 114 } 115 116 strbuf_release(&buffer); 117 return 0; 118 } 119 120 static void exec_woman_emacs(const char *path, const char *page) 121 { 122 if (!check_emacsclient_version()) { 123 /* This works only with emacsclient version >= 22. */ 124 struct strbuf man_page = STRBUF_INIT; 125 126 if (!path) 127 path = "emacsclient"; 128 strbuf_addf(&man_page, "(woman \"%s\")", page); 129 execlp(path, "emacsclient", "-e", man_page.buf, NULL); 130 warning("failed to exec '%s': %s", path, strerror(errno)); 131 } 132 } 133 134 static void exec_man_konqueror(const char *path, const char *page) 135 { 136 const char *display = getenv("DISPLAY"); 137 if (display && *display) { 138 struct strbuf man_page = STRBUF_INIT; 139 const char *filename = "kfmclient"; 140 141 /* It's simpler to launch konqueror using kfmclient. */ 142 if (path) { 143 const char *file = strrchr(path, '/'); 144 if (file && !strcmp(file + 1, "konqueror")) { 145 char *new = strdup(path); 146 char *dest = strrchr(new, '/'); 147 148 /* strlen("konqueror") == strlen("kfmclient") */ 149 strcpy(dest + 1, "kfmclient"); 150 path = new; 151 } 152 if (file) 153 filename = file; 154 } else 155 path = "kfmclient"; 156 strbuf_addf(&man_page, "man:%s(1)", page); 157 execlp(path, filename, "newTab", man_page.buf, NULL); 158 warning("failed to exec '%s': %s", path, strerror(errno)); 159 } 160 } 161 162 static void exec_man_man(const char *path, const char *page) 163 { 164 if (!path) 165 path = "man"; 166 execlp(path, "man", page, NULL); 167 warning("failed to exec '%s': %s", path, strerror(errno)); 168 } 169 170 static void exec_man_cmd(const char *cmd, const char *page) 171 { 172 struct strbuf shell_cmd = STRBUF_INIT; 173 strbuf_addf(&shell_cmd, "%s %s", cmd, page); 174 execl("/bin/sh", "sh", "-c", shell_cmd.buf, NULL); 175 warning("failed to exec '%s': %s", cmd, strerror(errno)); 176 } 177 178 static void add_man_viewer(const char *name) 179 { 180 struct man_viewer_list **p = &man_viewer_list; 181 size_t len = strlen(name); 182 183 while (*p) 184 p = &((*p)->next); 185 *p = zalloc(sizeof(**p) + len + 1); 186 strncpy((*p)->name, name, len); 187 } 188 189 static int supported_man_viewer(const char *name, size_t len) 190 { 191 return (!strncasecmp("man", name, len) || 192 !strncasecmp("woman", name, len) || 193 !strncasecmp("konqueror", name, len)); 194 } 195 196 static void do_add_man_viewer_info(const char *name, 197 size_t len, 198 const char *value) 199 { 200 struct man_viewer_info_list *new = zalloc(sizeof(*new) + len + 1); 201 202 strncpy(new->name, name, len); 203 new->info = strdup(value); 204 new->next = man_viewer_info_list; 205 man_viewer_info_list = new; 206 } 207 208 static int add_man_viewer_path(const char *name, 209 size_t len, 210 const char *value) 211 { 212 if (supported_man_viewer(name, len)) 213 do_add_man_viewer_info(name, len, value); 214 else 215 warning("'%s': path for unsupported man viewer.\n" 216 "Please consider using 'man.<tool>.cmd' instead.", 217 name); 218 219 return 0; 220 } 221 222 static int add_man_viewer_cmd(const char *name, 223 size_t len, 224 const char *value) 225 { 226 if (supported_man_viewer(name, len)) 227 warning("'%s': cmd for supported man viewer.\n" 228 "Please consider using 'man.<tool>.path' instead.", 229 name); 230 else 231 do_add_man_viewer_info(name, len, value); 232 233 return 0; 234 } 235 236 static int add_man_viewer_info(const char *var, const char *value) 237 { 238 const char *name = var + 4; 239 const char *subkey = strrchr(name, '.'); 240 241 if (!subkey) 242 return error("Config with no key for man viewer: %s", name); 243 244 if (!strcmp(subkey, ".path")) { 245 if (!value) 246 return config_error_nonbool(var); 247 return add_man_viewer_path(name, subkey - name, value); 248 } 249 if (!strcmp(subkey, ".cmd")) { 250 if (!value) 251 return config_error_nonbool(var); 252 return add_man_viewer_cmd(name, subkey - name, value); 253 } 254 255 warning("'%s': unsupported man viewer sub key.", subkey); 256 return 0; 257 } 258 259 static int perf_help_config(const char *var, const char *value, void *cb) 260 { 261 if (!strcmp(var, "help.format")) { 262 if (!value) 263 return config_error_nonbool(var); 264 help_format = parse_help_format(value); 265 if (help_format == HELP_FORMAT_NONE) 266 return -1; 267 return 0; 268 } 269 if (!strcmp(var, "man.viewer")) { 270 if (!value) 271 return config_error_nonbool(var); 272 add_man_viewer(value); 273 return 0; 274 } 275 if (!prefixcmp(var, "man.")) 276 return add_man_viewer_info(var, value); 277 278 return perf_default_config(var, value, cb); 279 } 280 281 static struct cmdnames main_cmds, other_cmds; 282 283 void list_common_cmds_help(void) 284 { 285 unsigned int i, longest = 0; 286 287 for (i = 0; i < ARRAY_SIZE(common_cmds); i++) { 288 if (longest < strlen(common_cmds[i].name)) 289 longest = strlen(common_cmds[i].name); 290 } 291 292 puts(" The most commonly used perf commands are:"); 293 for (i = 0; i < ARRAY_SIZE(common_cmds); i++) { 294 printf(" %-*s ", longest, common_cmds[i].name); 295 puts(common_cmds[i].help); 296 } 297 } 298 299 static int is_perf_command(const char *s) 300 { 301 return is_in_cmdlist(&main_cmds, s) || 302 is_in_cmdlist(&other_cmds, s); 303 } 304 305 static const char *prepend(const char *prefix, const char *cmd) 306 { 307 size_t pre_len = strlen(prefix); 308 size_t cmd_len = strlen(cmd); 309 char *p = malloc(pre_len + cmd_len + 1); 310 memcpy(p, prefix, pre_len); 311 strcpy(p + pre_len, cmd); 312 return p; 313 } 314 315 static const char *cmd_to_page(const char *perf_cmd) 316 { 317 if (!perf_cmd) 318 return "perf"; 319 else if (!prefixcmp(perf_cmd, "perf")) 320 return perf_cmd; 321 else 322 return prepend("perf-", perf_cmd); 323 } 324 325 static void setup_man_path(void) 326 { 327 struct strbuf new_path = STRBUF_INIT; 328 const char *old_path = getenv("MANPATH"); 329 330 /* We should always put ':' after our path. If there is no 331 * old_path, the ':' at the end will let 'man' to try 332 * system-wide paths after ours to find the manual page. If 333 * there is old_path, we need ':' as delimiter. */ 334 strbuf_addstr(&new_path, system_path(PERF_MAN_PATH)); 335 strbuf_addch(&new_path, ':'); 336 if (old_path) 337 strbuf_addstr(&new_path, old_path); 338 339 setenv("MANPATH", new_path.buf, 1); 340 341 strbuf_release(&new_path); 342 } 343 344 static void exec_viewer(const char *name, const char *page) 345 { 346 const char *info = get_man_viewer_info(name); 347 348 if (!strcasecmp(name, "man")) 349 exec_man_man(info, page); 350 else if (!strcasecmp(name, "woman")) 351 exec_woman_emacs(info, page); 352 else if (!strcasecmp(name, "konqueror")) 353 exec_man_konqueror(info, page); 354 else if (info) 355 exec_man_cmd(info, page); 356 else 357 warning("'%s': unknown man viewer.", name); 358 } 359 360 static int show_man_page(const char *perf_cmd) 361 { 362 struct man_viewer_list *viewer; 363 const char *page = cmd_to_page(perf_cmd); 364 const char *fallback = getenv("PERF_MAN_VIEWER"); 365 366 setup_man_path(); 367 for (viewer = man_viewer_list; viewer; viewer = viewer->next) 368 exec_viewer(viewer->name, page); /* will return when unable */ 369 370 if (fallback) 371 exec_viewer(fallback, page); 372 exec_viewer("man", page); 373 374 pr_err("no man viewer handled the request"); 375 return -1; 376 } 377 378 static int show_info_page(const char *perf_cmd) 379 { 380 const char *page = cmd_to_page(perf_cmd); 381 setenv("INFOPATH", system_path(PERF_INFO_PATH), 1); 382 execlp("info", "info", "perfman", page, NULL); 383 return -1; 384 } 385 386 static int get_html_page_path(struct strbuf *page_path, const char *page) 387 { 388 struct stat st; 389 const char *html_path = system_path(PERF_HTML_PATH); 390 391 /* Check that we have a perf documentation directory. */ 392 if (stat(mkpath("%s/perf.html", html_path), &st) 393 || !S_ISREG(st.st_mode)) { 394 pr_err("'%s': not a documentation directory.", html_path); 395 return -1; 396 } 397 398 strbuf_init(page_path, 0); 399 strbuf_addf(page_path, "%s/%s.html", html_path, page); 400 401 return 0; 402 } 403 404 /* 405 * If open_html is not defined in a platform-specific way (see for 406 * example compat/mingw.h), we use the script web--browse to display 407 * HTML. 408 */ 409 #ifndef open_html 410 static void open_html(const char *path) 411 { 412 execl_perf_cmd("web--browse", "-c", "help.browser", path, NULL); 413 } 414 #endif 415 416 static int show_html_page(const char *perf_cmd) 417 { 418 const char *page = cmd_to_page(perf_cmd); 419 struct strbuf page_path; /* it leaks but we exec bellow */ 420 421 if (get_html_page_path(&page_path, page) != 0) 422 return -1; 423 424 open_html(page_path.buf); 425 426 return 0; 427 } 428 429 int cmd_help(int argc, const char **argv, const char *prefix __maybe_unused) 430 { 431 const char *alias; 432 int rc = 0; 433 434 load_command_list("perf-", &main_cmds, &other_cmds); 435 436 perf_config(perf_help_config, NULL); 437 438 argc = parse_options(argc, argv, builtin_help_options, 439 builtin_help_usage, 0); 440 441 if (show_all) { 442 printf("\n usage: %s\n\n", perf_usage_string); 443 list_commands("perf commands", &main_cmds, &other_cmds); 444 printf(" %s\n\n", perf_more_info_string); 445 return 0; 446 } 447 448 if (!argv[0]) { 449 printf("\n usage: %s\n\n", perf_usage_string); 450 list_common_cmds_help(); 451 printf("\n %s\n\n", perf_more_info_string); 452 return 0; 453 } 454 455 alias = alias_lookup(argv[0]); 456 if (alias && !is_perf_command(argv[0])) { 457 printf("`perf %s' is aliased to `%s'\n", argv[0], alias); 458 return 0; 459 } 460 461 switch (help_format) { 462 case HELP_FORMAT_MAN: 463 rc = show_man_page(argv[0]); 464 break; 465 case HELP_FORMAT_INFO: 466 rc = show_info_page(argv[0]); 467 break; 468 case HELP_FORMAT_WEB: 469 rc = show_html_page(argv[0]); 470 break; 471 case HELP_FORMAT_NONE: 472 /* fall-through */ 473 default: 474 rc = -1; 475 break; 476 } 477 478 return rc; 479 } 480