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 #include "util/debug.h" 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 { 28 HELP_FORMAT_NONE, 29 HELP_FORMAT_MAN, 30 HELP_FORMAT_INFO, 31 HELP_FORMAT_WEB, 32 }; 33 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; 42 43 pr_err("unrecognized help format '%s'", format); 44 return HELP_FORMAT_NONE; 45 } 46 47 static const char *get_man_viewer_info(const char *name) 48 { 49 struct man_viewer_info_list *viewer; 50 51 for (viewer = man_viewer_info_list; viewer; viewer = viewer->next) { 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 104 static void exec_woman_emacs(const char *path, const char *page) 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 118 static void exec_man_konqueror(const char *path, const char *page) 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 146 static void exec_man_man(const char *path, const char *page) 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); 169 *p = zalloc(sizeof(**p) + len + 1); 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 { 184 struct man_viewer_info_list *new = zalloc(sizeof(*new) + len + 1); 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 { 245 enum help_format *help_formatp = cb; 246 247 if (!strcmp(var, "help.format")) { 248 if (!value) 249 return config_error_nonbool(var); 250 *help_formatp = parse_help_format(value); 251 if (*help_formatp == HELP_FORMAT_NONE) 252 return -1; 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 { 271 unsigned int i, longest = 0; 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 278 puts(" The most commonly used perf commands are:"); 279 for (i = 0; i < ARRAY_SIZE(common_cmds); i++) { 280 printf(" %-*s ", longest, common_cmds[i].name); 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; 307 else 308 return prepend("perf-", perf_cmd); 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 346 static int show_man_page(const char *perf_cmd) 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) 354 exec_viewer(viewer->name, page); /* will return when unable */ 355 356 if (fallback) 357 exec_viewer(fallback, page); 358 exec_viewer("man", page); 359 360 pr_err("no man viewer handled the request"); 361 return -1; 362 } 363 364 static int show_info_page(const char *perf_cmd) 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); 369 return -1; 370 } 371 372 static int get_html_page_path(struct strbuf *page_path, const char *page) 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) 379 || !S_ISREG(st.st_mode)) { 380 pr_err("'%s': not a documentation directory.", html_path); 381 return -1; 382 } 383 384 strbuf_init(page_path, 0); 385 strbuf_addf(page_path, "%s/%s.html", html_path, page); 386 387 return 0; 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 396 static void open_html(const char *path) 397 { 398 execl_perf_cmd("web--browse", "-c", "help.browser", path, NULL); 399 } 400 #endif 401 402 static int show_html_page(const char *perf_cmd) 403 { 404 const char *page = cmd_to_page(perf_cmd); 405 struct strbuf page_path; /* it leaks but we exec bellow */ 406 407 if (get_html_page_path(&page_path, page) != 0) 408 return -1; 409 410 open_html(page_path.buf); 411 412 return 0; 413 } 414 415 int cmd_help(int argc, const char **argv, const char *prefix __maybe_unused) 416 { 417 bool show_all = false; 418 enum help_format help_format = HELP_FORMAT_MAN; 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 }; 432 const char *alias; 433 int rc = 0; 434 435 load_command_list("perf-", &main_cmds, &other_cmds); 436 437 perf_config(perf_help_config, &help_format); 438 439 argc = parse_options(argc, argv, builtin_help_options, 440 builtin_help_usage, 0); 441 442 if (show_all) { 443 printf("\n usage: %s\n\n", perf_usage_string); 444 list_commands("perf commands", &main_cmds, &other_cmds); 445 printf(" %s\n\n", perf_more_info_string); 446 return 0; 447 } 448 449 if (!argv[0]) { 450 printf("\n usage: %s\n\n", perf_usage_string); 451 list_common_cmds_help(); 452 printf("\n %s\n\n", perf_more_info_string); 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: 464 rc = show_man_page(argv[0]); 465 break; 466 case HELP_FORMAT_INFO: 467 rc = show_info_page(argv[0]); 468 break; 469 case HELP_FORMAT_WEB: 470 rc = show_html_page(argv[0]); 471 break; 472 case HELP_FORMAT_NONE: 473 /* fall-through */ 474 default: 475 rc = -1; 476 break; 477 } 478 479 return rc; 480 } 481