man.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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 tarball for details.
  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] */
  159. /* man version 1.6f uses man.config */
  160. parser = config_open2("/etc/man.config", fopen_for_read);
  161. if (!parser)
  162. parser = config_open2("/etc/man.conf", fopen_for_read);
  163. while (config_read(parser, token, 2, 0, "# \t", PARSE_NORMAL)) {
  164. if (!token[1])
  165. continue;
  166. if (strcmp("MANPATH", token[0]) == 0) {
  167. char *path = token[1];
  168. while (*path) {
  169. char *next_path;
  170. char **path_element;
  171. next_path = strchr(path, ':');
  172. if (next_path) {
  173. *next_path = '\0';
  174. if (next_path++ == path) /* "::"? */
  175. goto next;
  176. }
  177. /* Do we already have path? */
  178. path_element = man_path_list;
  179. while (*path_element) {
  180. if (strcmp(*path_element, path) == 0)
  181. goto skip;
  182. path_element++;
  183. }
  184. man_path_list = xrealloc_vector(man_path_list, 4, count_mp);
  185. man_path_list[count_mp] = xstrdup(path);
  186. count_mp++;
  187. /* man_path_list is NULL terminated */
  188. /*man_path_list[count_mp] = NULL; - xrealloc_vector did it */
  189. skip:
  190. if (!next_path)
  191. break;
  192. next:
  193. path = next_path;
  194. }
  195. }
  196. if (strcmp("MANSECT", token[0]) == 0) {
  197. free(sec_list);
  198. sec_list = xstrdup(token[1]);
  199. }
  200. }
  201. config_close(parser);
  202. not_found = 0;
  203. do { /* for each argv[] */
  204. int found = 0;
  205. cur_mp = 0;
  206. if (strchr(*argv, '/')) {
  207. found = show_manpage(pager, *argv, /*man:*/ 1, 0);
  208. goto check_found;
  209. }
  210. while ((cur_path = man_path_list[cur_mp++]) != NULL) {
  211. /* for each MANPATH */
  212. cur_sect = sec_list;
  213. do { /* for each section */
  214. char *next_sect = strchrnul(cur_sect, ':');
  215. int sect_len = next_sect - cur_sect;
  216. char *man_filename;
  217. int cat0man1 = 0;
  218. /* Search for cat, then man page */
  219. while (cat0man1 < 2) {
  220. int found_here;
  221. man_filename = xasprintf("%s/%s%.*s/%s.%.*s" Z_SUFFIX,
  222. cur_path,
  223. "cat\0man" + (cat0man1 * 4),
  224. sect_len, cur_sect,
  225. *argv,
  226. sect_len, cur_sect);
  227. found_here = show_manpage(pager, man_filename, cat0man1, 0);
  228. found |= found_here;
  229. cat0man1 += found_here + 1;
  230. free(man_filename);
  231. }
  232. if (found && !(opt & OPT_a))
  233. goto next_arg;
  234. cur_sect = next_sect;
  235. while (*cur_sect == ':')
  236. cur_sect++;
  237. } while (*cur_sect);
  238. }
  239. check_found:
  240. if (!found) {
  241. bb_error_msg("no manual entry for '%s'", *argv);
  242. not_found = 1;
  243. }
  244. next_arg:
  245. argv++;
  246. } while (*argv);
  247. return not_found;
  248. }