man.c 7.1 KB

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