man.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. //usage:#define man_trivial_usage
  6. //usage: "[-aw] [MANPAGE]..."
  7. //usage:#define man_full_usage "\n\n"
  8. //usage: "Format and display manual page\n"
  9. //usage: "\n -a Display all pages"
  10. //usage: "\n -w Show page locations"
  11. #include "libbb.h"
  12. enum {
  13. OPT_a = 1, /* all */
  14. OPT_w = 2, /* print path */
  15. };
  16. /* This is what I see on my desktop system being executed:
  17. (
  18. echo ".ll 12.4i"
  19. echo ".nr LL 12.4i"
  20. echo ".pl 1100i"
  21. gunzip -c '/usr/man/man1/bzip2.1.gz'
  22. echo ".\\\""
  23. echo ".pl \n(nlu+10"
  24. ) | gtbl | nroff -Tlatin1 -mandoc | less
  25. */
  26. static int show_manpage(const char *pager, char *man_filename, int man, int level);
  27. static int run_pipe(const char *pager, char *man_filename, int man, int level)
  28. {
  29. char *cmd;
  30. /* Prevent man page link loops */
  31. if (level > 10)
  32. return 0;
  33. if (access(man_filename, R_OK) != 0)
  34. return 0;
  35. if (option_mask32 & OPT_w) {
  36. puts(man_filename);
  37. return 1;
  38. }
  39. if (man) { /* man page, not cat page */
  40. /* Is this a link to another manpage? */
  41. /* The link has the following on the first line: */
  42. /* ".so another_man_page" */
  43. struct stat sb;
  44. char *line;
  45. char *linkname, *p;
  46. /* On my system:
  47. * man1/genhostid.1.gz: 203 bytes - smallest real manpage
  48. * man2/path_resolution.2.gz: 114 bytes - largest link
  49. */
  50. xstat(man_filename, &sb);
  51. if (sb.st_size > 300) /* err on the safe side */
  52. goto ordinary_manpage;
  53. line = xmalloc_open_zipped_read_close(man_filename, NULL);
  54. if (!line || !is_prefixed_with(line, ".so ")) {
  55. free(line);
  56. goto ordinary_manpage;
  57. }
  58. /* Example: man2/path_resolution.2.gz contains
  59. * ".so man7/path_resolution.7\n<junk>"
  60. */
  61. *strchrnul(line, '\n') = '\0';
  62. linkname = skip_whitespace(&line[4]);
  63. /* If link has no slashes, we just replace man page name.
  64. * If link has slashes (however many), we go back *once*.
  65. * ".so zzz/ggg/page.3" does NOT go back two levels. */
  66. p = strrchr(man_filename, '/');
  67. if (!p)
  68. goto ordinary_manpage;
  69. *p = '\0';
  70. if (strchr(linkname, '/')) {
  71. p = strrchr(man_filename, '/');
  72. if (!p)
  73. goto ordinary_manpage;
  74. *p = '\0';
  75. }
  76. /* Links do not have .gz extensions, even if manpage
  77. * is compressed */
  78. man_filename = xasprintf("%s/%s", man_filename, linkname);
  79. free(line);
  80. /* Note: we leak "new" man_filename string as well... */
  81. if (show_manpage(pager, man_filename, man, level + 1))
  82. return 1;
  83. /* else: show the link, it's better than nothing */
  84. }
  85. ordinary_manpage:
  86. close(STDIN_FILENO);
  87. open_zipped(man_filename, /*fail_if_not_compressed:*/ 0); /* guaranteed to use fd 0 (STDIN_FILENO) */
  88. /* "2>&1" is added so that nroff errors are shown in pager too.
  89. * Otherwise it may show just empty screen */
  90. cmd = xasprintf(
  91. /* replaced -Tlatin1 with -Tascii for non-UTF8 displays */
  92. man ? "gtbl | nroff -Tascii -mandoc 2>&1 | %s"
  93. : "%s",
  94. pager);
  95. system(cmd);
  96. free(cmd);
  97. return 1;
  98. }
  99. /* man_filename is of the form "/dir/dir/dir/name.s" */
  100. static int show_manpage(const char *pager, char *man_filename, int man, int level)
  101. {
  102. #if SEAMLESS_COMPRESSION
  103. /* We leak this allocation... */
  104. char *filename_with_zext = xasprintf("%s.lzma", man_filename);
  105. char *ext = strrchr(filename_with_zext, '.') + 1;
  106. #endif
  107. #if ENABLE_FEATURE_SEAMLESS_LZMA
  108. if (run_pipe(pager, filename_with_zext, man, level))
  109. return 1;
  110. #endif
  111. #if ENABLE_FEATURE_SEAMLESS_XZ
  112. strcpy(ext, "xz");
  113. if (run_pipe(pager, filename_with_zext, man, level))
  114. return 1;
  115. #endif
  116. #if ENABLE_FEATURE_SEAMLESS_BZ2
  117. strcpy(ext, "bz2");
  118. if (run_pipe(pager, filename_with_zext, man, level))
  119. return 1;
  120. #endif
  121. #if ENABLE_FEATURE_SEAMLESS_GZ
  122. strcpy(ext, "gz");
  123. if (run_pipe(pager, filename_with_zext, man, level))
  124. return 1;
  125. #endif
  126. return run_pipe(pager, man_filename, man, level);
  127. }
  128. static char **add_MANPATH(char **man_path_list, int *count_mp, char *path)
  129. {
  130. if (path) while (*path) {
  131. char *next_path;
  132. char **path_element;
  133. next_path = strchr(path, ':');
  134. if (next_path) {
  135. if (next_path == path) /* "::"? */
  136. goto next;
  137. *next_path = '\0';
  138. }
  139. /* Do we already have path? */
  140. path_element = man_path_list;
  141. if (path_element) while (*path_element) {
  142. if (strcmp(*path_element, path) == 0)
  143. goto skip;
  144. path_element++;
  145. }
  146. man_path_list = xrealloc_vector(man_path_list, 4, *count_mp);
  147. man_path_list[*count_mp] = xstrdup(path);
  148. (*count_mp)++;
  149. /* man_path_list is NULL terminated */
  150. /* man_path_list[*count_mp] = NULL; - xrealloc_vector did it */
  151. skip:
  152. if (!next_path)
  153. break;
  154. /* "path" may be a result of getenv(), be nice and don't mangle it */
  155. *next_path = ':';
  156. next:
  157. path = next_path + 1;
  158. }
  159. return man_path_list;
  160. }
  161. int man_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  162. int man_main(int argc UNUSED_PARAM, char **argv)
  163. {
  164. parser_t *parser;
  165. const char *pager = ENABLE_LESS ? "less" : "more";
  166. char *sec_list;
  167. char *cur_path, *cur_sect;
  168. char **man_path_list;
  169. int count_mp;
  170. int cur_mp;
  171. int opt, not_found;
  172. char *token[2];
  173. opt_complementary = "-1"; /* at least one argument */
  174. opt = getopt32(argv, "+aw");
  175. argv += optind;
  176. sec_list = xstrdup("0p:1:1p:2:3:3p:4:5:6:7:8:9");
  177. count_mp = 0;
  178. man_path_list = add_MANPATH(NULL, &count_mp,
  179. getenv("MANDATORY_MANPATH"+10) /* "MANPATH" */
  180. );
  181. if (!man_path_list) {
  182. /* default, may be overridden by /etc/man.conf */
  183. man_path_list = xzalloc(2 * sizeof(man_path_list[0]));
  184. man_path_list[0] = (char*)"/usr/man";
  185. /* count_mp stays 0.
  186. * Thus, man.conf will overwrite man_path_list[0]
  187. * if a path is defined there.
  188. */
  189. }
  190. /* Parse man.conf[ig] or man_db.conf */
  191. /* man version 1.6f uses man.config */
  192. /* man-db implementation of man uses man_db.conf */
  193. parser = config_open2("/etc/man.config", fopen_for_read);
  194. if (!parser)
  195. parser = config_open2("/etc/man.conf", fopen_for_read);
  196. if (!parser)
  197. parser = config_open2("/etc/man_db.conf", fopen_for_read);
  198. while (config_read(parser, token, 2, 0, "# \t", PARSE_NORMAL)) {
  199. if (!token[1])
  200. continue;
  201. if (strcmp("DEFINE", token[0]) == 0) {
  202. if (is_prefixed_with("pager", token[1])) {
  203. pager = xstrdup(skip_whitespace(token[1]) + 5);
  204. }
  205. } else
  206. if (strcmp("MANDATORY_MANPATH"+10, token[0]) == 0 /* "MANPATH"? */
  207. || strcmp("MANDATORY_MANPATH", token[0]) == 0
  208. ) {
  209. man_path_list = add_MANPATH(man_path_list, &count_mp, token[1]);
  210. }
  211. if (strcmp("MANSECT", token[0]) == 0) {
  212. free(sec_list);
  213. sec_list = xstrdup(token[1]);
  214. }
  215. }
  216. config_close(parser);
  217. {
  218. /* environment overrides setting from man.config */
  219. char *env_pager = getenv("MANPAGER");
  220. if (!env_pager)
  221. env_pager = getenv("PAGER");
  222. if (env_pager)
  223. pager = env_pager;
  224. }
  225. not_found = 0;
  226. do { /* for each argv[] */
  227. int found = 0;
  228. cur_mp = 0;
  229. if (strchr(*argv, '/')) {
  230. found = show_manpage(pager, *argv, /*man:*/ 1, 0);
  231. goto check_found;
  232. }
  233. while ((cur_path = man_path_list[cur_mp++]) != NULL) {
  234. /* for each MANPATH */
  235. cur_sect = sec_list;
  236. do { /* for each section */
  237. char *next_sect = strchrnul(cur_sect, ':');
  238. int sect_len = next_sect - cur_sect;
  239. char *man_filename;
  240. int cat0man1 = 0;
  241. /* Search for cat, then man page */
  242. while (cat0man1 < 2) {
  243. int found_here;
  244. man_filename = xasprintf("%s/%s%.*s/%s.%.*s",
  245. cur_path,
  246. "cat\0man" + (cat0man1 * 4),
  247. sect_len, cur_sect,
  248. *argv,
  249. sect_len, cur_sect);
  250. found_here = show_manpage(pager, man_filename, cat0man1, 0);
  251. found |= found_here;
  252. cat0man1 += found_here + 1;
  253. free(man_filename);
  254. }
  255. if (found && !(opt & OPT_a))
  256. goto next_arg;
  257. cur_sect = next_sect;
  258. while (*cur_sect == ':')
  259. cur_sect++;
  260. } while (*cur_sect);
  261. }
  262. check_found:
  263. if (!found) {
  264. bb_error_msg("no manual entry for '%s'", *argv);
  265. not_found = 1;
  266. }
  267. next_arg:
  268. argv++;
  269. } while (*argv);
  270. return not_found;
  271. }