blob: 2c6b25cebc3b297c7c17bc0a813d5fb57af5dd96 [file] [log] [blame]
Denis Vlasenkoffa44992008-04-13 08:20:00 +00001/* mini man implementation for busybox
2 * Copyright (C) 2008 Denys Vlasenko <vda.linux@googlemail.com>
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02003 * Licensed under GPLv2, see file LICENSE in this source tree.
Denis Vlasenkoffa44992008-04-13 08:20:00 +00004 */
5
Pere Orga5bc8c002011-04-11 03:29:49 +02006//usage:#define man_trivial_usage
7//usage: "[-aw] [MANPAGE]..."
8//usage:#define man_full_usage "\n\n"
9//usage: "Format and display manual page\n"
10//usage: "\nOptions:"
11//usage: "\n -a Display all pages"
12//usage: "\n -w Show page locations"
13
Denis Vlasenkoffa44992008-04-13 08:20:00 +000014#include "libbb.h"
15
16enum {
17 OPT_a = 1, /* all */
18 OPT_w = 2, /* print path */
19};
20
Denis Vlasenkoce02b152008-06-27 22:05:21 +000021/* This is what I see on my desktop system being executed:
Denis Vlasenkoffa44992008-04-13 08:20:00 +000022
23(
24echo ".ll 12.4i"
25echo ".nr LL 12.4i"
26echo ".pl 1100i"
27gunzip -c '/usr/man/man1/bzip2.1.gz'
28echo ".\\\""
29echo ".pl \n(nlu+10"
30) | gtbl | nroff -Tlatin1 -mandoc | less
31
32*/
33
Denis Vlasenko0e7f28d2008-08-05 15:28:05 +000034#if ENABLE_FEATURE_SEAMLESS_LZMA
35#define Z_SUFFIX ".lzma"
36#elif ENABLE_FEATURE_SEAMLESS_BZ2
37#define Z_SUFFIX ".bz2"
38#elif ENABLE_FEATURE_SEAMLESS_GZ
39#define Z_SUFFIX ".gz"
40#else
41#define Z_SUFFIX ""
42#endif
Denis Vlasenko540baf62008-08-05 13:16:18 +000043
44static int show_manpage(const char *pager, char *man_filename, int man, int level);
45
Denis Vlasenko0e7f28d2008-08-05 15:28:05 +000046static int run_pipe(const char *pager, char *man_filename, int man, int level)
Denis Vlasenkoffa44992008-04-13 08:20:00 +000047{
48 char *cmd;
49
Denis Vlasenko540baf62008-08-05 13:16:18 +000050 /* Prevent man page link loops */
51 if (level > 10)
52 return 0;
53
Denis Vlasenkoffa44992008-04-13 08:20:00 +000054 if (access(man_filename, R_OK) != 0)
55 return 0;
56
57 if (option_mask32 & OPT_w) {
58 puts(man_filename);
59 return 1;
60 }
61
Denis Vlasenko540baf62008-08-05 13:16:18 +000062 if (man) { /* man page, not cat page */
Denis Vlasenkoe2a09de2008-08-05 14:15:19 +000063 /* Is this a link to another manpage? */
Denis Vlasenko540baf62008-08-05 13:16:18 +000064 /* The link has the following on the first line: */
65 /* ".so another_man_page" */
Denis Vlasenkoe2a09de2008-08-05 14:15:19 +000066
Denis Vlasenko540baf62008-08-05 13:16:18 +000067 struct stat sb;
68 char *line;
69 char *linkname, *p;
70
71 /* On my system:
72 * man1/genhostid.1.gz: 203 bytes - smallest real manpage
73 * man2/path_resolution.2.gz: 114 bytes - largest link
74 */
75 xstat(man_filename, &sb);
76 if (sb.st_size > 300) /* err on the safe side */
77 goto ordinary_manpage;
78
79 line = xmalloc_open_zipped_read_close(man_filename, NULL);
80 if (!line || strncmp(line, ".so ", 4) != 0) {
81 free(line);
82 goto ordinary_manpage;
83 }
84 /* Example: man2/path_resolution.2.gz contains
85 * ".so man7/path_resolution.7\n<junk>"
86 */
87 *strchrnul(line, '\n') = '\0';
Denis Vlasenkoe2a09de2008-08-05 14:15:19 +000088 linkname = skip_whitespace(&line[4]);
Denis Vlasenko540baf62008-08-05 13:16:18 +000089
Denis Vlasenko22b0bd82008-08-05 13:45:22 +000090 /* If link has no slashes, we just replace man page name.
91 * If link has slashes (however many), we go back *once*.
92 * ".so zzz/ggg/page.3" does NOT go back two levels. */
93 p = strrchr(man_filename, '/');
94 if (!p)
95 goto ordinary_manpage;
96 *p = '\0';
97 if (strchr(linkname, '/')) {
98 p = strrchr(man_filename, '/');
99 if (!p)
Denis Vlasenko540baf62008-08-05 13:16:18 +0000100 goto ordinary_manpage;
Denis Vlasenko22b0bd82008-08-05 13:45:22 +0000101 *p = '\0';
Denis Vlasenko540baf62008-08-05 13:16:18 +0000102 }
Denis Vlasenko22b0bd82008-08-05 13:45:22 +0000103
104 /* Links do not have .gz extensions, even if manpage
105 * is compressed */
Denis Vlasenko0e7f28d2008-08-05 15:28:05 +0000106 man_filename = xasprintf("%s/%s" Z_SUFFIX, man_filename, linkname);
Denis Vlasenko540baf62008-08-05 13:16:18 +0000107 free(line);
108 /* Note: we leak "new" man_filename string as well... */
109 if (show_manpage(pager, man_filename, man, level + 1))
110 return 1;
Denis Vlasenko22b0bd82008-08-05 13:45:22 +0000111 /* else: show the link, it's better than nothing */
Denis Vlasenko540baf62008-08-05 13:16:18 +0000112 }
113
114 ordinary_manpage:
Denis Vlasenko0e7f28d2008-08-05 15:28:05 +0000115 close(STDIN_FILENO);
116 open_zipped(man_filename); /* guaranteed to use fd 0 (STDIN_FILENO) */
Denis Vlasenko4cbffc02008-07-04 21:58:00 +0000117 /* "2>&1" is added so that nroff errors are shown in pager too.
Denis Vlasenkob75fe792008-06-27 22:31:07 +0000118 * Otherwise it may show just empty screen */
Denis Vlasenkof6efccc2008-07-05 08:50:08 +0000119 cmd = xasprintf(
Denis Vlasenko0e7f28d2008-08-05 15:28:05 +0000120 man ? "gtbl | nroff -Tlatin1 -mandoc 2>&1 | %s"
121 : "%s",
122 pager);
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000123 system(cmd);
124 free(cmd);
125 return 1;
126}
127
Denis Vlasenko0e7f28d2008-08-05 15:28:05 +0000128/* man_filename is of the form "/dir/dir/dir/name.s" Z_SUFFIX */
Denis Vlasenko540baf62008-08-05 13:16:18 +0000129static int show_manpage(const char *pager, char *man_filename, int man, int level)
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000130{
Denis Vlasenko0e7f28d2008-08-05 15:28:05 +0000131#if ENABLE_FEATURE_SEAMLESS_LZMA
132 if (run_pipe(pager, man_filename, man, level))
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000133 return 1;
Denis Vlasenko0e7f28d2008-08-05 15:28:05 +0000134#endif
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000135
Denis Vlasenko0e7f28d2008-08-05 15:28:05 +0000136#if ENABLE_FEATURE_SEAMLESS_BZ2
137#if ENABLE_FEATURE_SEAMLESS_LZMA
138 strcpy(strrchr(man_filename, '.') + 1, "bz2");
139#endif
140 if (run_pipe(pager, man_filename, man, level))
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000141 return 1;
Denis Vlasenko0e7f28d2008-08-05 15:28:05 +0000142#endif
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000143
Denis Vlasenko0e7f28d2008-08-05 15:28:05 +0000144#if ENABLE_FEATURE_SEAMLESS_GZ
145#if ENABLE_FEATURE_SEAMLESS_LZMA || ENABLE_FEATURE_SEAMLESS_BZ2
146 strcpy(strrchr(man_filename, '.') + 1, "gz");
147#endif
148 if (run_pipe(pager, man_filename, man, level))
149 return 1;
150#endif
151
152#if ENABLE_FEATURE_SEAMLESS_LZMA || ENABLE_FEATURE_SEAMLESS_BZ2 || ENABLE_FEATURE_SEAMLESS_GZ
153 *strrchr(man_filename, '.') = '\0';
154#endif
155 if (run_pipe(pager, man_filename, man, level))
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000156 return 1;
157
158 return 0;
159}
160
161int man_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000162int man_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000163{
Denis Vlasenko09aaf782008-07-20 17:41:30 +0000164 parser_t *parser;
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000165 const char *pager;
Denis Vlasenko50d068c2008-04-19 03:42:47 +0000166 char **man_path_list;
167 char *sec_list;
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000168 char *cur_path, *cur_sect;
Denis Vlasenko9835d472008-07-04 21:57:11 +0000169 int count_mp, cur_mp;
Denis Vlasenkoce02b152008-06-27 22:05:21 +0000170 int opt, not_found;
Denis Vlasenko084266e2008-07-26 23:08:31 +0000171 char *token[2];
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000172
173 opt_complementary = "-1"; /* at least one argument */
174 opt = getopt32(argv, "+aw");
175 argv += optind;
176
177 sec_list = xstrdup("1:2:3:4:5:6:7:8:9");
Denis Vlasenko9835d472008-07-04 21:57:11 +0000178 /* Last valid man_path_list[] is [0x10] */
Denis Vlasenkoa1e16c92008-09-14 21:30:35 +0000179 count_mp = 0;
Denis Vlasenko9835d472008-07-04 21:57:11 +0000180 man_path_list = xzalloc(0x11 * sizeof(man_path_list[0]));
Denis Vlasenkoe357d2a2008-09-14 21:26:55 +0000181 man_path_list[0] = getenv("MANPATH");
Denis Vlasenkoa1e16c92008-09-14 21:30:35 +0000182 if (!man_path_list[0]) /* default, may be overridden by /etc/man.conf */
Denis Vlasenkoe357d2a2008-09-14 21:26:55 +0000183 man_path_list[0] = (char*)"/usr/man";
Denis Vlasenkoa1e16c92008-09-14 21:30:35 +0000184 else
185 count_mp++;
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000186 pager = getenv("MANPAGER");
187 if (!pager) {
188 pager = getenv("PAGER");
189 if (!pager)
190 pager = "more";
191 }
192
Denys Vlasenko9a1b2602010-10-04 14:19:59 +0200193 /* Parse man.conf[ig] or man_db.conf */
Denys Vlasenko0e1b6262009-07-24 02:28:12 +0200194 /* man version 1.6f uses man.config */
Denys Vlasenko9a1b2602010-10-04 14:19:59 +0200195 /* man-db implementation of man uses man_db.conf */
Denys Vlasenko0e1b6262009-07-24 02:28:12 +0200196 parser = config_open2("/etc/man.config", fopen_for_read);
197 if (!parser)
198 parser = config_open2("/etc/man.conf", fopen_for_read);
Denys Vlasenko9a1b2602010-10-04 14:19:59 +0200199 if (!parser)
200 parser = config_open2("/etc/man_db.conf", fopen_for_read);
Denys Vlasenko0e1b6262009-07-24 02:28:12 +0200201
Denis Vlasenko94d03f02008-07-26 23:16:33 +0000202 while (config_read(parser, token, 2, 0, "# \t", PARSE_NORMAL)) {
203 if (!token[1])
204 continue;
Denys Vlasenko9a1b2602010-10-04 14:19:59 +0200205 if (strcmp("MANDATORY_MANPATH"+10, token[0]) == 0 /* "MANPATH"? */
206 || strcmp("MANDATORY_MANPATH", token[0]) == 0
207 ) {
Denis Vlasenkoe88bd2d2008-11-16 04:05:13 +0000208 char *path = token[1];
209 while (*path) {
210 char *next_path;
211 char **path_element;
212
213 next_path = strchr(path, ':');
214 if (next_path) {
215 *next_path = '\0';
216 if (next_path++ == path) /* "::"? */
217 goto next;
218 }
219 /* Do we already have path? */
220 path_element = man_path_list;
221 while (*path_element) {
222 if (strcmp(*path_element, path) == 0)
223 goto skip;
224 path_element++;
225 }
226 man_path_list = xrealloc_vector(man_path_list, 4, count_mp);
227 man_path_list[count_mp] = xstrdup(path);
228 count_mp++;
229 /* man_path_list is NULL terminated */
230 /*man_path_list[count_mp] = NULL; - xrealloc_vector did it */
231 skip:
232 if (!next_path)
233 break;
234 next:
235 path = next_path;
Denis Vlasenkoe357d2a2008-09-14 21:26:55 +0000236 }
Denis Vlasenko94d03f02008-07-26 23:16:33 +0000237 }
238 if (strcmp("MANSECT", token[0]) == 0) {
239 free(sec_list);
240 sec_list = xstrdup(token[1]);
241 }
242 }
243 config_close(parser);
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000244
Denis Vlasenkoce02b152008-06-27 22:05:21 +0000245 not_found = 0;
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000246 do { /* for each argv[] */
Denis Vlasenkoce02b152008-06-27 22:05:21 +0000247 int found = 0;
Denis Vlasenko50d068c2008-04-19 03:42:47 +0000248 cur_mp = 0;
Denis Vlasenko0e7f28d2008-08-05 15:28:05 +0000249
250 if (strchr(*argv, '/')) {
251 found = show_manpage(pager, *argv, /*man:*/ 1, 0);
252 goto check_found;
253 }
Denis Vlasenko50d068c2008-04-19 03:42:47 +0000254 while ((cur_path = man_path_list[cur_mp++]) != NULL) {
255 /* for each MANPATH */
Denis Vlasenkoe88bd2d2008-11-16 04:05:13 +0000256 cur_sect = sec_list;
257 do { /* for each section */
258 char *next_sect = strchrnul(cur_sect, ':');
259 int sect_len = next_sect - cur_sect;
260 char *man_filename;
261 int cat0man1 = 0;
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000262
Denis Vlasenkoe88bd2d2008-11-16 04:05:13 +0000263 /* Search for cat, then man page */
264 while (cat0man1 < 2) {
265 int found_here;
266 man_filename = xasprintf("%s/%s%.*s/%s.%.*s" Z_SUFFIX,
267 cur_path,
268 "cat\0man" + (cat0man1 * 4),
269 sect_len, cur_sect,
270 *argv,
271 sect_len, cur_sect);
272 found_here = show_manpage(pager, man_filename, cat0man1, 0);
273 found |= found_here;
274 cat0man1 += found_here + 1;
275 free(man_filename);
276 }
Denis Vlasenko4cbffc02008-07-04 21:58:00 +0000277
Denis Vlasenkoe88bd2d2008-11-16 04:05:13 +0000278 if (found && !(opt & OPT_a))
279 goto next_arg;
280 cur_sect = next_sect;
281 while (*cur_sect == ':')
282 cur_sect++;
283 } while (*cur_sect);
Denis Vlasenko50d068c2008-04-19 03:42:47 +0000284 }
Denis Vlasenko0e7f28d2008-08-05 15:28:05 +0000285 check_found:
Denis Vlasenkoce02b152008-06-27 22:05:21 +0000286 if (!found) {
287 bb_error_msg("no manual entry for '%s'", *argv);
288 not_found = 1;
289 }
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000290 next_arg:
291 argv++;
292 } while (*argv);
293
Denis Vlasenkoce02b152008-06-27 22:05:21 +0000294 return not_found;
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000295}