builtin-help.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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 int show_all = 0;
  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_INT('m', "man", &help_format, "show man page", HELP_FORMAT_MAN),
  33. OPT_SET_INT('w', "web", &help_format, "show manual in web browser",
  34. HELP_FORMAT_WEB),
  35. OPT_SET_INT('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. {
  58. if (!strcasecmp(name, viewer->name))
  59. return viewer->info;
  60. }
  61. return NULL;
  62. }
  63. static int check_emacsclient_version(void)
  64. {
  65. struct strbuf buffer = STRBUF_INIT;
  66. struct child_process ec_process;
  67. const char *argv_ec[] = { "emacsclient", "--version", NULL };
  68. int version;
  69. /* emacsclient prints its version number on stderr */
  70. memset(&ec_process, 0, sizeof(ec_process));
  71. ec_process.argv = argv_ec;
  72. ec_process.err = -1;
  73. ec_process.stdout_to_stderr = 1;
  74. if (start_command(&ec_process)) {
  75. fprintf(stderr, "Failed to start emacsclient.\n");
  76. return -1;
  77. }
  78. strbuf_read(&buffer, ec_process.err, 20);
  79. close(ec_process.err);
  80. /*
  81. * Don't bother checking return value, because "emacsclient --version"
  82. * seems to always exits with code 1.
  83. */
  84. finish_command(&ec_process);
  85. if (prefixcmp(buffer.buf, "emacsclient")) {
  86. fprintf(stderr, "Failed to parse emacsclient version.\n");
  87. strbuf_release(&buffer);
  88. return -1;
  89. }
  90. strbuf_remove(&buffer, 0, strlen("emacsclient"));
  91. version = atoi(buffer.buf);
  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. strbuf_release(&buffer);
  100. return 0;
  101. }
  102. static void exec_woman_emacs(const char* path, const char *page)
  103. {
  104. if (!check_emacsclient_version()) {
  105. /* This works only with emacsclient version >= 22. */
  106. struct strbuf man_page = STRBUF_INIT;
  107. if (!path)
  108. path = "emacsclient";
  109. strbuf_addf(&man_page, "(woman \"%s\")", page);
  110. execlp(path, "emacsclient", "-e", man_page.buf, NULL);
  111. warning("failed to exec '%s': %s", path, strerror(errno));
  112. }
  113. }
  114. static void exec_man_konqueror(const char* path, const char *page)
  115. {
  116. const char *display = getenv("DISPLAY");
  117. if (display && *display) {
  118. struct strbuf man_page = STRBUF_INIT;
  119. const char *filename = "kfmclient";
  120. /* It's simpler to launch konqueror using kfmclient. */
  121. if (path) {
  122. const char *file = strrchr(path, '/');
  123. if (file && !strcmp(file + 1, "konqueror")) {
  124. char *new = strdup(path);
  125. char *dest = strrchr(new, '/');
  126. /* strlen("konqueror") == strlen("kfmclient") */
  127. strcpy(dest + 1, "kfmclient");
  128. path = new;
  129. }
  130. if (file)
  131. filename = file;
  132. } else
  133. path = "kfmclient";
  134. strbuf_addf(&man_page, "man:%s(1)", page);
  135. execlp(path, filename, "newTab", man_page.buf, NULL);
  136. warning("failed to exec '%s': %s", path, strerror(errno));
  137. }
  138. }
  139. static void exec_man_man(const char* path, const char *page)
  140. {
  141. if (!path)
  142. path = "man";
  143. execlp(path, "man", page, NULL);
  144. warning("failed to exec '%s': %s", path, strerror(errno));
  145. }
  146. static void exec_man_cmd(const char *cmd, const char *page)
  147. {
  148. struct strbuf shell_cmd = STRBUF_INIT;
  149. strbuf_addf(&shell_cmd, "%s %s", cmd, page);
  150. execl("/bin/sh", "sh", "-c", shell_cmd.buf, NULL);
  151. warning("failed to exec '%s': %s", cmd, strerror(errno));
  152. }
  153. static void add_man_viewer(const char *name)
  154. {
  155. struct man_viewer_list **p = &man_viewer_list;
  156. size_t len = strlen(name);
  157. while (*p)
  158. p = &((*p)->next);
  159. *p = calloc(1, (sizeof(**p) + len + 1));
  160. strncpy((*p)->name, name, len);
  161. }
  162. static int supported_man_viewer(const char *name, size_t len)
  163. {
  164. return (!strncasecmp("man", name, len) ||
  165. !strncasecmp("woman", name, len) ||
  166. !strncasecmp("konqueror", name, len));
  167. }
  168. static void do_add_man_viewer_info(const char *name,
  169. size_t len,
  170. const char *value)
  171. {
  172. struct man_viewer_info_list *new = calloc(1, sizeof(*new) + len + 1);
  173. strncpy(new->name, name, len);
  174. new->info = strdup(value);
  175. new->next = man_viewer_info_list;
  176. man_viewer_info_list = new;
  177. }
  178. static int add_man_viewer_path(const char *name,
  179. size_t len,
  180. const char *value)
  181. {
  182. if (supported_man_viewer(name, len))
  183. do_add_man_viewer_info(name, len, value);
  184. else
  185. warning("'%s': path for unsupported man viewer.\n"
  186. "Please consider using 'man.<tool>.cmd' instead.",
  187. name);
  188. return 0;
  189. }
  190. static int add_man_viewer_cmd(const char *name,
  191. size_t len,
  192. const char *value)
  193. {
  194. if (supported_man_viewer(name, len))
  195. warning("'%s': cmd for supported man viewer.\n"
  196. "Please consider using 'man.<tool>.path' instead.",
  197. name);
  198. else
  199. do_add_man_viewer_info(name, len, value);
  200. return 0;
  201. }
  202. static int add_man_viewer_info(const char *var, const char *value)
  203. {
  204. const char *name = var + 4;
  205. const char *subkey = strrchr(name, '.');
  206. if (!subkey)
  207. return error("Config with no key for man viewer: %s", name);
  208. if (!strcmp(subkey, ".path")) {
  209. if (!value)
  210. return config_error_nonbool(var);
  211. return add_man_viewer_path(name, subkey - name, value);
  212. }
  213. if (!strcmp(subkey, ".cmd")) {
  214. if (!value)
  215. return config_error_nonbool(var);
  216. return add_man_viewer_cmd(name, subkey - name, value);
  217. }
  218. warning("'%s': unsupported man viewer sub key.", subkey);
  219. return 0;
  220. }
  221. static int perf_help_config(const char *var, const char *value, void *cb)
  222. {
  223. if (!strcmp(var, "help.format")) {
  224. if (!value)
  225. return config_error_nonbool(var);
  226. help_format = parse_help_format(value);
  227. return 0;
  228. }
  229. if (!strcmp(var, "man.viewer")) {
  230. if (!value)
  231. return config_error_nonbool(var);
  232. add_man_viewer(value);
  233. return 0;
  234. }
  235. if (!prefixcmp(var, "man."))
  236. return add_man_viewer_info(var, value);
  237. return perf_default_config(var, value, cb);
  238. }
  239. static struct cmdnames main_cmds, other_cmds;
  240. void list_common_cmds_help(void)
  241. {
  242. unsigned int i, longest = 0;
  243. for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
  244. if (longest < strlen(common_cmds[i].name))
  245. longest = strlen(common_cmds[i].name);
  246. }
  247. puts(" The most commonly used perf commands are:");
  248. for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
  249. printf(" %s ", common_cmds[i].name);
  250. mput_char(' ', longest - strlen(common_cmds[i].name));
  251. puts(common_cmds[i].help);
  252. }
  253. }
  254. static int is_perf_command(const char *s)
  255. {
  256. return is_in_cmdlist(&main_cmds, s) ||
  257. is_in_cmdlist(&other_cmds, s);
  258. }
  259. static const char *prepend(const char *prefix, const char *cmd)
  260. {
  261. size_t pre_len = strlen(prefix);
  262. size_t cmd_len = strlen(cmd);
  263. char *p = malloc(pre_len + cmd_len + 1);
  264. memcpy(p, prefix, pre_len);
  265. strcpy(p + pre_len, cmd);
  266. return p;
  267. }
  268. static const char *cmd_to_page(const char *perf_cmd)
  269. {
  270. if (!perf_cmd)
  271. return "perf";
  272. else if (!prefixcmp(perf_cmd, "perf"))
  273. return perf_cmd;
  274. else if (is_perf_command(perf_cmd))
  275. return prepend("perf-", 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 void 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. {
  316. exec_viewer(viewer->name, page); /* will return when unable */
  317. }
  318. if (fallback)
  319. exec_viewer(fallback, page);
  320. exec_viewer("man", page);
  321. die("no man viewer handled the request");
  322. }
  323. static void show_info_page(const char *perf_cmd)
  324. {
  325. const char *page = cmd_to_page(perf_cmd);
  326. setenv("INFOPATH", system_path(PERF_INFO_PATH), 1);
  327. execlp("info", "info", "perfman", page, NULL);
  328. }
  329. static void 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. die("'%s': not a documentation directory.", html_path);
  337. strbuf_init(page_path, 0);
  338. strbuf_addf(page_path, "%s/%s.html", html_path, page);
  339. }
  340. /*
  341. * If open_html is not defined in a platform-specific way (see for
  342. * example compat/mingw.h), we use the script web--browse to display
  343. * HTML.
  344. */
  345. #ifndef open_html
  346. static void open_html(const char *path)
  347. {
  348. execl_perf_cmd("web--browse", "-c", "help.browser", path, NULL);
  349. }
  350. #endif
  351. static void show_html_page(const char *perf_cmd)
  352. {
  353. const char *page = cmd_to_page(perf_cmd);
  354. struct strbuf page_path; /* it leaks but we exec bellow */
  355. get_html_page_path(&page_path, page);
  356. open_html(page_path.buf);
  357. }
  358. int cmd_help(int argc, const char **argv, const char *prefix __used)
  359. {
  360. const char *alias;
  361. load_command_list("perf-", &main_cmds, &other_cmds);
  362. perf_config(perf_help_config, NULL);
  363. argc = parse_options(argc, argv, builtin_help_options,
  364. builtin_help_usage, 0);
  365. if (show_all) {
  366. printf("\n usage: %s\n\n", perf_usage_string);
  367. list_commands("perf commands", &main_cmds, &other_cmds);
  368. printf(" %s\n\n", perf_more_info_string);
  369. return 0;
  370. }
  371. if (!argv[0]) {
  372. printf("\n usage: %s\n\n", perf_usage_string);
  373. list_common_cmds_help();
  374. printf("\n %s\n\n", perf_more_info_string);
  375. return 0;
  376. }
  377. alias = alias_lookup(argv[0]);
  378. if (alias && !is_perf_command(argv[0])) {
  379. printf("`perf %s' is aliased to `%s'\n", argv[0], alias);
  380. return 0;
  381. }
  382. switch (help_format) {
  383. case HELP_FORMAT_MAN:
  384. show_man_page(argv[0]);
  385. break;
  386. case HELP_FORMAT_INFO:
  387. show_info_page(argv[0]);
  388. break;
  389. case HELP_FORMAT_WEB:
  390. show_html_page(argv[0]);
  391. break;
  392. }
  393. return 0;
  394. }