man.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. man_path_list = xzalloc(0x11 * sizeof(man_path_list[0]));
  146. count_mp = 0;
  147. man_path_list[0] = xstrdup(getenv("MANPATH"));
  148. if (man_path_list[0])
  149. count_mp++;
  150. pager = getenv("MANPAGER");
  151. if (!pager) {
  152. pager = getenv("PAGER");
  153. if (!pager)
  154. pager = "more";
  155. }
  156. /* Parse man.conf */
  157. parser = config_open("/etc/man.conf");
  158. while (config_read(parser, token, 2, 0, "# \t", PARSE_NORMAL)) {
  159. if (!token[1])
  160. continue;
  161. if (strcmp("MANPATH", token[0]) == 0) {
  162. man_path_list = xrealloc_vector(man_path_list, 4, count_mp);
  163. man_path_list[count_mp] = xstrdup(token[1]);
  164. count_mp++;
  165. /* man_path_list is NULL terminated */
  166. /*man_path_list[count_mp] = NULL; - xrealloc_vector did it */
  167. }
  168. if (strcmp("MANSECT", token[0]) == 0) {
  169. free(sec_list);
  170. sec_list = xstrdup(token[1]);
  171. }
  172. }
  173. config_close(parser);
  174. not_found = 0;
  175. do { /* for each argv[] */
  176. int found = 0;
  177. cur_mp = 0;
  178. if (strchr(*argv, '/')) {
  179. found = show_manpage(pager, *argv, /*man:*/ 1, 0);
  180. goto check_found;
  181. }
  182. while ((cur_path = man_path_list[cur_mp++]) != NULL) {
  183. /* for each MANPATH */
  184. do { /* for each MANPATH item */
  185. char *next_path = strchrnul(cur_path, ':');
  186. int path_len = next_path - cur_path;
  187. cur_sect = sec_list;
  188. do { /* for each section */
  189. char *next_sect = strchrnul(cur_sect, ':');
  190. int sect_len = next_sect - cur_sect;
  191. char *man_filename;
  192. int cat0man1 = 0;
  193. /* Search for cat, then man page */
  194. while (cat0man1 < 2) {
  195. int found_here;
  196. man_filename = xasprintf("%.*s/%s%.*s/%s.%.*s" Z_SUFFIX,
  197. path_len, cur_path,
  198. "cat\0man" + (cat0man1 * 4),
  199. sect_len, cur_sect,
  200. *argv,
  201. sect_len, cur_sect);
  202. found_here = show_manpage(pager, man_filename, cat0man1, 0);
  203. found |= found_here;
  204. cat0man1 += found_here + 1;
  205. free(man_filename);
  206. }
  207. if (found && !(opt & OPT_a))
  208. goto next_arg;
  209. cur_sect = next_sect;
  210. while (*cur_sect == ':')
  211. cur_sect++;
  212. } while (*cur_sect);
  213. cur_path = next_path;
  214. while (*cur_path == ':')
  215. cur_path++;
  216. } while (*cur_path);
  217. }
  218. check_found:
  219. if (!found) {
  220. bb_error_msg("no manual entry for '%s'", *argv);
  221. not_found = 1;
  222. }
  223. next_arg:
  224. argv++;
  225. } while (*argv);
  226. return not_found;
  227. }