man.c 7.3 KB

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