man.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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] MANPAGE..."
  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. int man_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  213. int man_main(int argc UNUSED_PARAM, char **argv)
  214. {
  215. parser_t *parser;
  216. char *sec_list;
  217. char **man_path_list;
  218. int count_mp;
  219. int opt, not_found;
  220. char *token[2];
  221. INIT_G();
  222. opt = getopt32(argv, "^+" "aw" "\0" "-1"/*at least one arg*/);
  223. argv += optind;
  224. sec_list = xstrdup("0p:1:1p:2:3:3p:4:5:6:7:8:9");
  225. count_mp = 0;
  226. man_path_list = add_MANPATH(NULL, &count_mp,
  227. getenv("MANDATORY_MANPATH"+10) /* "MANPATH" */
  228. );
  229. /* Parse man.conf[ig] or man_db.conf */
  230. /* man version 1.6f uses man.config */
  231. /* man-db implementation of man uses man_db.conf */
  232. parser = config_open2("/etc/man.config", fopen_for_read);
  233. if (!parser)
  234. parser = config_open2("/etc/man.conf", fopen_for_read);
  235. if (!parser)
  236. parser = config_open2("/etc/man_db.conf", fopen_for_read);
  237. while (config_read(parser, token, 2, 0, "# \t", PARSE_NORMAL)) {
  238. if (!token[1])
  239. continue;
  240. if (strcmp("DEFINE", token[0]) == 0) {
  241. G.col = if_redefined(G.col , "col", token[1]);
  242. G.tbl = if_redefined(G.tbl , "tbl", token[1]);
  243. G.nroff = if_redefined(G.nroff, "nroff", token[1]);
  244. G.pager = if_redefined(G.pager, "pager", token[1]);
  245. } else
  246. if (strcmp("MANDATORY_MANPATH"+10, token[0]) == 0 /* "MANPATH"? */
  247. || strcmp("MANDATORY_MANPATH", token[0]) == 0
  248. ) {
  249. man_path_list = add_MANPATH(man_path_list, &count_mp, token[1]);
  250. }
  251. if (strcmp("MANSECT", token[0]) == 0) {
  252. free(sec_list);
  253. sec_list = xstrdup(token[1]);
  254. }
  255. }
  256. config_close(parser);
  257. if (!man_path_list) {
  258. static const char *const mpl[] = { "/usr/man", "/usr/share/man", NULL };
  259. man_path_list = (char**)mpl;
  260. /*count_mp = 2; - not used below anyway */
  261. }
  262. {
  263. /* environment overrides setting from man.config */
  264. char *env_pager = getenv("MANPAGER");
  265. if (!env_pager)
  266. env_pager = getenv("PAGER");
  267. if (env_pager)
  268. G.pager = env_pager;
  269. }
  270. if (!isatty(STDOUT_FILENO)) {
  271. putenv((char*)"GROFF_NO_SGR=1");
  272. G.pager = xasprintf("%s -b -p -x", G.col);
  273. }
  274. not_found = 0;
  275. do { /* for each argv[] */
  276. const char *cur_path;
  277. int cur_mp;
  278. int found = 0;
  279. if (strchr(*argv, '/')) {
  280. found = show_manpage(*argv, /*man:*/ 1, 0);
  281. goto check_found;
  282. }
  283. cur_mp = 0;
  284. while ((cur_path = man_path_list[cur_mp++]) != NULL) {
  285. /* for each MANPATH */
  286. const char *cur_sect = sec_list;
  287. do { /* for each section */
  288. char *next_sect = strchrnul(cur_sect, ':');
  289. int sect_len = next_sect - cur_sect;
  290. char *man_filename;
  291. int cat0man1 = 0;
  292. /* Search for cat, then man page */
  293. while (cat0man1 < 2) {
  294. int found_here;
  295. man_filename = xasprintf("%s/%s%.*s/%s.%.*s",
  296. cur_path,
  297. "cat\0man" + (cat0man1 * 4),
  298. sect_len, cur_sect,
  299. *argv,
  300. sect_len, cur_sect);
  301. found_here = show_manpage(man_filename, cat0man1, 0);
  302. found |= found_here;
  303. cat0man1 += found_here + 1;
  304. free(man_filename);
  305. }
  306. if (found && !(opt & OPT_a))
  307. goto next_arg;
  308. cur_sect = next_sect;
  309. while (*cur_sect == ':')
  310. cur_sect++;
  311. } while (*cur_sect);
  312. }
  313. check_found:
  314. if (!found) {
  315. bb_error_msg("no manual entry for '%s'", *argv);
  316. not_found = 1;
  317. }
  318. next_arg:
  319. argv++;
  320. } while (*argv);
  321. return not_found;
  322. }