builtin-help.c 11 KB

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