3
0

man.c 6.7 KB

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