man.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /* mini man implementation for busybox
  2. * Copyright (C) 2008 Denys Vlasenko <vda.linux@googlemail.com>
  3. * Licensed under GPLv2, see file LICENSE in this source tree.
  4. */
  5. //config:config MAN
  6. //config: bool "man (26 kb)"
  7. //config: default y
  8. //config: help
  9. //config: Format and display manual pages.
  10. //applet:IF_MAN(APPLET(man, BB_DIR_USR_BIN, BB_SUID_DROP))
  11. //kbuild:lib-$(CONFIG_MAN) += man.o
  12. //usage:#define man_trivial_usage
  13. //usage: "[-aw] [SECTION] MANPAGE[.SECTION]..."
  14. //usage:#define man_full_usage "\n\n"
  15. //usage: "Display manual page\n"
  16. //usage: "\n -a Display all pages"
  17. //usage: "\n -w Show page locations"
  18. //usage: "\n"
  19. //usage: "\n$COLUMNS overrides output width"
  20. #include "libbb.h"
  21. #include "common_bufsiz.h"
  22. enum {
  23. OPT_a = 1, /* all */
  24. OPT_w = 2, /* print path */
  25. };
  26. /* This is what I see on my desktop system being executed:
  27. (
  28. echo ".ll 12.4i"
  29. echo ".nr LL 12.4i"
  30. echo ".pl 1100i"
  31. gunzip -c '/usr/man/man1/bzip2.1.gz'
  32. echo ".\\\""
  33. echo ".pl \n(nlu+10"
  34. ) | gtbl | nroff -Tlatin1 -mandoc | less
  35. Some systems use -Tascii.
  36. On another system I see this:
  37. ... | tbl | nroff -mandoc -rLL=<NNN>n -rLT=<NNN>n -Tutf8 | less
  38. where <NNN> is screen width minus 5.
  39. Replacing "DEFINE nroff nroff -mandoc" in /etc/man_db.conf
  40. changes "nroff -mandoc" part; -rLL=<NNN>n, -rLT=<NNN>n and -Tutf8 parts are
  41. appended to the user-specified command.
  42. Redirecting to a pipe or file sets GROFF_NO_SGR=1 to prevent color escapes,
  43. and uses "col -b -p -x" instead of pager, this filters out backspace
  44. and underscore tricks.
  45. */
  46. struct globals {
  47. const char *col;
  48. const char *tbl;
  49. const char *nroff;
  50. const char *pager;
  51. } FIX_ALIASING;
  52. #define G (*(struct globals*)bb_common_bufsiz1)
  53. #define INIT_G() do { \
  54. setup_common_bufsiz(); \
  55. G.col = "col"; \
  56. G.tbl = "tbl"; \
  57. /* Removed -Tlatin1. Assuming system nroff has suitable default */ \
  58. G.nroff = "nroff -mandoc"; \
  59. G.pager = ENABLE_LESS ? "less" : "more"; \
  60. } while (0)
  61. static int show_manpage(char *man_filename, int man, int level);
  62. static int run_pipe(char *man_filename, int man, int level)
  63. {
  64. char *cmd;
  65. /* Prevent man page link loops */
  66. if (level > 10)
  67. return 0;
  68. if (access(man_filename, R_OK) != 0)
  69. return 0;
  70. if (option_mask32 & OPT_w) {
  71. puts(man_filename);
  72. return 1;
  73. }
  74. if (man) { /* man page, not cat page */
  75. /* Is this a link to another manpage? */
  76. /* The link has the following on the first line: */
  77. /* ".so another_man_page" */
  78. struct stat sb;
  79. char *line;
  80. char *linkname, *p;
  81. /* On my system:
  82. * man1/genhostid.1.gz: 203 bytes - smallest real manpage
  83. * man2/path_resolution.2.gz: 114 bytes - largest link
  84. */
  85. xstat(man_filename, &sb);
  86. if (sb.st_size > 300) /* err on the safe side */
  87. goto ordinary_manpage;
  88. line = xmalloc_open_zipped_read_close(man_filename, NULL);
  89. if (!line || !is_prefixed_with(line, ".so ")) {
  90. free(line);
  91. goto ordinary_manpage;
  92. }
  93. /* Example: man2/path_resolution.2.gz contains
  94. * ".so man7/path_resolution.7\n<junk>"
  95. */
  96. *strchrnul(line, '\n') = '\0';
  97. linkname = skip_whitespace(&line[4]);
  98. /* If link has no slashes, we just replace man page name.
  99. * If link has slashes (however many), we go back *once*.
  100. * ".so zzz/ggg/page.3" does NOT go back two levels. */
  101. p = strrchr(man_filename, '/');
  102. if (!p)
  103. goto ordinary_manpage;
  104. *p = '\0';
  105. if (strchr(linkname, '/')) {
  106. p = strrchr(man_filename, '/');
  107. if (!p)
  108. goto ordinary_manpage;
  109. *p = '\0';
  110. }
  111. /* Links do not have .gz extensions, even if manpage
  112. * is compressed */
  113. man_filename = xasprintf("%s/%s", man_filename, linkname);
  114. free(line);
  115. /* Note: we leak "new" man_filename string as well... */
  116. if (show_manpage(man_filename, man, level + 1))
  117. return 1;
  118. /* else: show the link, it's better than nothing */
  119. }
  120. ordinary_manpage:
  121. close(STDIN_FILENO);
  122. open_zipped(man_filename, /*fail_if_not_compressed:*/ 0); /* guaranteed to use fd 0 (STDIN_FILENO) */
  123. if (man) {
  124. int w = get_terminal_width(-1);
  125. if (w > 10)
  126. w -= 2;
  127. /* "2>&1" is added so that nroff errors are shown in pager too.
  128. * Otherwise it may show just empty screen.
  129. */
  130. cmd = xasprintf("%s | %s -rLL=%un -rLT=%un 2>&1 | %s",
  131. G.tbl, G.nroff, w, w,
  132. G.pager);
  133. } else {
  134. cmd = xstrdup(G.pager);
  135. }
  136. system(cmd);
  137. free(cmd);
  138. return 1;
  139. }
  140. /* man_filename is of the form "/dir/dir/dir/name.s" */
  141. static int show_manpage(char *man_filename, int man, int level)
  142. {
  143. #if SEAMLESS_COMPRESSION
  144. /* We leak this allocation... */
  145. char *filename_with_zext = xasprintf("%s.lzma", man_filename);
  146. char *ext = strrchr(filename_with_zext, '.') + 1;
  147. #endif
  148. #if ENABLE_FEATURE_SEAMLESS_LZMA
  149. if (run_pipe(filename_with_zext, man, level))
  150. return 1;
  151. #endif
  152. #if ENABLE_FEATURE_SEAMLESS_XZ
  153. strcpy(ext, "xz");
  154. if (run_pipe(filename_with_zext, man, level))
  155. return 1;
  156. #endif
  157. #if ENABLE_FEATURE_SEAMLESS_BZ2
  158. strcpy(ext, "bz2");
  159. if (run_pipe(filename_with_zext, man, level))
  160. return 1;
  161. #endif
  162. #if ENABLE_FEATURE_SEAMLESS_GZ
  163. strcpy(ext, "gz");
  164. if (run_pipe(filename_with_zext, man, level))
  165. return 1;
  166. #endif
  167. return run_pipe(man_filename, man, level);
  168. }
  169. static char **add_MANPATH(char **man_path_list, int *count_mp, char *path)
  170. {
  171. if (path) while (*path) {
  172. char *next_path;
  173. char **path_element;
  174. next_path = strchr(path, ':');
  175. if (next_path) {
  176. if (next_path == path) /* "::"? */
  177. goto next;
  178. *next_path = '\0';
  179. }
  180. /* Do we already have path? */
  181. path_element = man_path_list;
  182. if (path_element) while (*path_element) {
  183. if (strcmp(*path_element, path) == 0) {
  184. goto skip;
  185. }
  186. path_element++;
  187. }
  188. man_path_list = xrealloc_vector(man_path_list, 4, *count_mp);
  189. man_path_list[*count_mp] = xstrdup(path);
  190. (*count_mp)++;
  191. /* man_path_list is NULL terminated */
  192. /* man_path_list[*count_mp] = NULL; - xrealloc_vector did it */
  193. skip:
  194. if (!next_path)
  195. break;
  196. /* "path" may be a result of getenv(), be nice and don't mangle it */
  197. *next_path = ':';
  198. next:
  199. path = next_path + 1;
  200. }
  201. return man_path_list;
  202. }
  203. static const char *if_redefined(const char *var, const char *key, const char *line)
  204. {
  205. if (!is_prefixed_with(line, key))
  206. return var;
  207. line += strlen(key);
  208. if (!isspace(line[0]))
  209. return var;
  210. return xstrdup(skip_whitespace(line));
  211. }
  212. static int is_section_name(const char *sections, const char *str)
  213. {
  214. const char *s = strstr(sections, str);
  215. if (s) {
  216. int l = strlen(str);
  217. return (s[l] == ':' || s[l] == '\0');
  218. }
  219. return 0;
  220. }
  221. int man_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  222. int man_main(int argc UNUSED_PARAM, char **argv)
  223. {
  224. parser_t *parser;
  225. char *conf_sec_list;
  226. char *sec_list;
  227. char **man_path_list;
  228. int count_mp;
  229. int opt, not_found;
  230. char *token[2];
  231. INIT_G();
  232. opt = getopt32(argv, "^+" "aw" "\0" "-1"/*at least one arg*/);
  233. argv += optind;
  234. conf_sec_list = xstrdup("0p:1:1p:2:3:3p:4:5:6:7:8:9");
  235. count_mp = 0;
  236. man_path_list = add_MANPATH(NULL, &count_mp,
  237. getenv("MANDATORY_MANPATH"+10) /* "MANPATH" */
  238. );
  239. /* Parse man.conf[ig] or man_db.conf */
  240. /* man version 1.6f uses man.config */
  241. /* man-db implementation of man uses man_db.conf */
  242. parser = config_open2("/etc/man.config", fopen_for_read);
  243. if (!parser)
  244. parser = config_open2("/etc/man.conf", fopen_for_read);
  245. if (!parser)
  246. parser = config_open2("/etc/man_db.conf", fopen_for_read);
  247. while (config_read(parser, token, 2, 0, "# \t", PARSE_NORMAL)) {
  248. if (!token[1])
  249. continue;
  250. if (strcmp("DEFINE", token[0]) == 0) {
  251. G.col = if_redefined(G.col , "col", token[1]);
  252. G.tbl = if_redefined(G.tbl , "tbl", token[1]);
  253. G.nroff = if_redefined(G.nroff, "nroff", token[1]);
  254. G.pager = if_redefined(G.pager, "pager", token[1]);
  255. } else
  256. if (strcmp("MANDATORY_MANPATH"+10, token[0]) == 0 /* "MANPATH"? */
  257. || strcmp("MANDATORY_MANPATH", token[0]) == 0
  258. ) {
  259. man_path_list = add_MANPATH(man_path_list, &count_mp, token[1]);
  260. }
  261. if (strcmp("MANSECT", token[0]) == 0) {
  262. free(conf_sec_list);
  263. conf_sec_list = xstrdup(token[1]);
  264. }
  265. }
  266. config_close(parser);
  267. if (!man_path_list) {
  268. static const char *const mpl[] = { "/usr/man", "/usr/share/man", NULL };
  269. man_path_list = (char**)mpl;
  270. /*count_mp = 2; - not used below anyway */
  271. }
  272. {
  273. /* environment overrides setting from man.config */
  274. char *env_pager = getenv("MANPAGER");
  275. if (!env_pager)
  276. env_pager = getenv("PAGER");
  277. if (env_pager)
  278. G.pager = env_pager;
  279. }
  280. if (!isatty(STDOUT_FILENO)) {
  281. putenv((char*)"GROFF_NO_SGR=1");
  282. G.pager = xasprintf("%s -b -p -x", G.col);
  283. }
  284. /* is 1st ARG a SECTION? */
  285. sec_list = conf_sec_list;
  286. if (is_section_name(conf_sec_list, *argv)) {
  287. /* yes */
  288. sec_list = *argv++;
  289. }
  290. not_found = 0;
  291. do { /* for each argv[] */
  292. const char *cur_path;
  293. int cur_mp;
  294. int found = 0;
  295. if (strchr(*argv, '/')) {
  296. found = show_manpage(*argv, /*man:*/ 1, 0);
  297. goto check_found;
  298. }
  299. /* for each MANPATH */
  300. cur_mp = 0;
  301. while ((cur_path = man_path_list[cur_mp++]) != NULL) {
  302. const char *cur_sect = sec_list;
  303. /* is MANPAGE of the form NAME.SECTION? */
  304. char *sect_ext = strrchr(*argv, '.');
  305. if (sect_ext && is_section_name(conf_sec_list, sect_ext + 1)) {
  306. *sect_ext = '\0';
  307. cur_sect = sect_ext + 1;
  308. }
  309. do { /* for each SECTION in cur_sect */
  310. char *next_sect = strchrnul(cur_sect, ':');
  311. int sect_len = next_sect - cur_sect;
  312. char *man_filename;
  313. int cat0man1 = 0;
  314. /* Search for cat, then man page */
  315. while (cat0man1 < 2) {
  316. int found_here;
  317. man_filename = xasprintf("%s/%s%.*s/%s.%.*s",
  318. cur_path,
  319. "cat\0man" + (cat0man1 * 4),
  320. sect_len, cur_sect,
  321. *argv,
  322. sect_len, cur_sect);
  323. found_here = show_manpage(man_filename, cat0man1, 0);
  324. found |= found_here;
  325. cat0man1 += found_here + 1;
  326. free(man_filename);
  327. }
  328. if (found && !(opt & OPT_a))
  329. goto next_arg;
  330. cur_sect = next_sect;
  331. while (*cur_sect == ':')
  332. cur_sect++;
  333. } while (*cur_sect);
  334. if (sect_ext) *sect_ext = '.';
  335. }
  336. check_found:
  337. if (!found) {
  338. bb_error_msg("no manual entry for '%s'", *argv);
  339. not_found = 1;
  340. }
  341. next_arg:
  342. argv++;
  343. } while (*argv);
  344. return not_found;
  345. }