3
0

man.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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"
  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] [MANPAGE]..."
  14. //usage:#define man_full_usage "\n\n"
  15. //usage: "Format and 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. path_element++;
  186. }
  187. man_path_list = xrealloc_vector(man_path_list, 4, *count_mp);
  188. man_path_list[*count_mp] = xstrdup(path);
  189. (*count_mp)++;
  190. /* man_path_list is NULL terminated */
  191. /* man_path_list[*count_mp] = NULL; - xrealloc_vector did it */
  192. skip:
  193. if (!next_path)
  194. break;
  195. /* "path" may be a result of getenv(), be nice and don't mangle it */
  196. *next_path = ':';
  197. next:
  198. path = next_path + 1;
  199. }
  200. return man_path_list;
  201. }
  202. static const char *if_redefined(const char *var, const char *key, const char *line)
  203. {
  204. if (!is_prefixed_with(line, key))
  205. return var;
  206. line += strlen(key);
  207. if (!isspace(line[0]))
  208. return var;
  209. return xstrdup(skip_whitespace(line));
  210. }
  211. int man_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  212. int man_main(int argc UNUSED_PARAM, char **argv)
  213. {
  214. parser_t *parser;
  215. char *sec_list;
  216. char *cur_path, *cur_sect;
  217. char **man_path_list;
  218. int count_mp;
  219. int cur_mp;
  220. int opt, not_found;
  221. char *token[2];
  222. INIT_G();
  223. opt_complementary = "-1"; /* at least one argument */
  224. opt = getopt32(argv, "+aw");
  225. argv += optind;
  226. sec_list = xstrdup("0p:1:1p:2:3:3p:4:5:6:7:8:9");
  227. count_mp = 0;
  228. man_path_list = add_MANPATH(NULL, &count_mp,
  229. getenv("MANDATORY_MANPATH"+10) /* "MANPATH" */
  230. );
  231. if (!man_path_list) {
  232. /* default, may be overridden by /etc/man.conf */
  233. man_path_list = xzalloc(2 * sizeof(man_path_list[0]));
  234. man_path_list[0] = (char*)"/usr/man";
  235. /* count_mp stays 0.
  236. * Thus, man.conf will overwrite man_path_list[0]
  237. * if a path is defined there.
  238. */
  239. }
  240. /* Parse man.conf[ig] or man_db.conf */
  241. /* man version 1.6f uses man.config */
  242. /* man-db implementation of man uses man_db.conf */
  243. parser = config_open2("/etc/man.config", fopen_for_read);
  244. if (!parser)
  245. parser = config_open2("/etc/man.conf", fopen_for_read);
  246. if (!parser)
  247. parser = config_open2("/etc/man_db.conf", fopen_for_read);
  248. while (config_read(parser, token, 2, 0, "# \t", PARSE_NORMAL)) {
  249. if (!token[1])
  250. continue;
  251. if (strcmp("DEFINE", token[0]) == 0) {
  252. G.col = if_redefined(G.tbl , "col", token[1]);
  253. G.tbl = if_redefined(G.tbl , "tbl", token[1]);
  254. G.nroff = if_redefined(G.nroff, "nroff", token[1]);
  255. G.pager = if_redefined(G.pager, "pager", token[1]);
  256. } else
  257. if (strcmp("MANDATORY_MANPATH"+10, token[0]) == 0 /* "MANPATH"? */
  258. || strcmp("MANDATORY_MANPATH", token[0]) == 0
  259. ) {
  260. man_path_list = add_MANPATH(man_path_list, &count_mp, token[1]);
  261. }
  262. if (strcmp("MANSECT", token[0]) == 0) {
  263. free(sec_list);
  264. sec_list = xstrdup(token[1]);
  265. }
  266. }
  267. config_close(parser);
  268. {
  269. /* environment overrides setting from man.config */
  270. char *env_pager = getenv("MANPAGER");
  271. if (!env_pager)
  272. env_pager = getenv("PAGER");
  273. if (env_pager)
  274. G.pager = env_pager;
  275. }
  276. if (!isatty(STDOUT_FILENO)) {
  277. putenv((char*)"GROFF_NO_SGR=1");
  278. G.pager = xasprintf("%s -b -p -x", G.col);
  279. }
  280. not_found = 0;
  281. do { /* for each argv[] */
  282. int found = 0;
  283. cur_mp = 0;
  284. if (strchr(*argv, '/')) {
  285. found = show_manpage(*argv, /*man:*/ 1, 0);
  286. goto check_found;
  287. }
  288. while ((cur_path = man_path_list[cur_mp++]) != NULL) {
  289. /* for each MANPATH */
  290. cur_sect = sec_list;
  291. do { /* for each section */
  292. char *next_sect = strchrnul(cur_sect, ':');
  293. int sect_len = next_sect - cur_sect;
  294. char *man_filename;
  295. int cat0man1 = 0;
  296. /* Search for cat, then man page */
  297. while (cat0man1 < 2) {
  298. int found_here;
  299. man_filename = xasprintf("%s/%s%.*s/%s.%.*s",
  300. cur_path,
  301. "cat\0man" + (cat0man1 * 4),
  302. sect_len, cur_sect,
  303. *argv,
  304. sect_len, cur_sect);
  305. found_here = show_manpage(man_filename, cat0man1, 0);
  306. found |= found_here;
  307. cat0man1 += found_here + 1;
  308. free(man_filename);
  309. }
  310. if (found && !(opt & OPT_a))
  311. goto next_arg;
  312. cur_sect = next_sect;
  313. while (*cur_sect == ':')
  314. cur_sect++;
  315. } while (*cur_sect);
  316. }
  317. check_found:
  318. if (!found) {
  319. bb_error_msg("no manual entry for '%s'", *argv);
  320. not_found = 1;
  321. }
  322. next_arg:
  323. argv++;
  324. } while (*argv);
  325. return not_found;
  326. }