man.c 7.1 KB

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