builtin-help.c 11 KB

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