blob: 3bf7e84b6fa56e6253098e5afbbebd69e3190db8 [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"
Denys Vlasenko66426762011-06-05 03:58:28 +020010//usage: "\n -a Display all pages"
Pere Orga5bc8c002011-04-11 03:29:49 +020011//usage: "\n -w Show page locations"
12
Denis Vlasenkoffa44992008-04-13 08:20:00 +000013#include "libbb.h"
14
15enum {
16 OPT_a = 1, /* all */
17 OPT_w = 2, /* print path */
18};
19
Denis Vlasenkoce02b152008-06-27 22:05:21 +000020/* This is what I see on my desktop system being executed:
Denis Vlasenkoffa44992008-04-13 08:20:00 +000021
22(
23echo ".ll 12.4i"
24echo ".nr LL 12.4i"
25echo ".pl 1100i"
26gunzip -c '/usr/man/man1/bzip2.1.gz'
27echo ".\\\""
28echo ".pl \n(nlu+10"
29) | gtbl | nroff -Tlatin1 -mandoc | less
30
31*/
32
Denis Vlasenko0e7f28d2008-08-05 15:28:05 +000033#if ENABLE_FEATURE_SEAMLESS_LZMA
34#define Z_SUFFIX ".lzma"
35#elif ENABLE_FEATURE_SEAMLESS_BZ2
36#define Z_SUFFIX ".bz2"
37#elif ENABLE_FEATURE_SEAMLESS_GZ
38#define Z_SUFFIX ".gz"
39#else
40#define Z_SUFFIX ""
41#endif
Denis Vlasenko540baf62008-08-05 13:16:18 +000042
43static int show_manpage(const char *pager, char *man_filename, int man, int level);
44
Denis Vlasenko0e7f28d2008-08-05 15:28:05 +000045static int run_pipe(const char *pager, char *man_filename, int man, int level)
Denis Vlasenkoffa44992008-04-13 08:20:00 +000046{
47 char *cmd;
48
Denis Vlasenko540baf62008-08-05 13:16:18 +000049 /* Prevent man page link loops */
50 if (level > 10)
51 return 0;
52
Denis Vlasenkoffa44992008-04-13 08:20:00 +000053 if (access(man_filename, R_OK) != 0)
54 return 0;
55
56 if (option_mask32 & OPT_w) {
57 puts(man_filename);
58 return 1;
59 }
60
Denis Vlasenko540baf62008-08-05 13:16:18 +000061 if (man) { /* man page, not cat page */
Denis Vlasenkoe2a09de2008-08-05 14:15:19 +000062 /* Is this a link to another manpage? */
Denis Vlasenko540baf62008-08-05 13:16:18 +000063 /* The link has the following on the first line: */
64 /* ".so another_man_page" */
Denis Vlasenkoe2a09de2008-08-05 14:15:19 +000065
Denis Vlasenko540baf62008-08-05 13:16:18 +000066 struct stat sb;
67 char *line;
68 char *linkname, *p;
69
70 /* On my system:
71 * man1/genhostid.1.gz: 203 bytes - smallest real manpage
72 * man2/path_resolution.2.gz: 114 bytes - largest link
73 */
74 xstat(man_filename, &sb);
75 if (sb.st_size > 300) /* err on the safe side */
76 goto ordinary_manpage;
77
78 line = xmalloc_open_zipped_read_close(man_filename, NULL);
79 if (!line || strncmp(line, ".so ", 4) != 0) {
80 free(line);
81 goto ordinary_manpage;
82 }
83 /* Example: man2/path_resolution.2.gz contains
84 * ".so man7/path_resolution.7\n<junk>"
85 */
86 *strchrnul(line, '\n') = '\0';
Denis Vlasenkoe2a09de2008-08-05 14:15:19 +000087 linkname = skip_whitespace(&line[4]);
Denis Vlasenko540baf62008-08-05 13:16:18 +000088
Denis Vlasenko22b0bd82008-08-05 13:45:22 +000089 /* If link has no slashes, we just replace man page name.
90 * If link has slashes (however many), we go back *once*.
91 * ".so zzz/ggg/page.3" does NOT go back two levels. */
92 p = strrchr(man_filename, '/');
93 if (!p)
94 goto ordinary_manpage;
95 *p = '\0';
96 if (strchr(linkname, '/')) {
97 p = strrchr(man_filename, '/');
98 if (!p)
Denis Vlasenko540baf62008-08-05 13:16:18 +000099 goto ordinary_manpage;
Denis Vlasenko22b0bd82008-08-05 13:45:22 +0000100 *p = '\0';
Denis Vlasenko540baf62008-08-05 13:16:18 +0000101 }
Denis Vlasenko22b0bd82008-08-05 13:45:22 +0000102
103 /* Links do not have .gz extensions, even if manpage
104 * is compressed */
Denis Vlasenko0e7f28d2008-08-05 15:28:05 +0000105 man_filename = xasprintf("%s/%s" Z_SUFFIX, man_filename, linkname);
Denis Vlasenko540baf62008-08-05 13:16:18 +0000106 free(line);
107 /* Note: we leak "new" man_filename string as well... */
108 if (show_manpage(pager, man_filename, man, level + 1))
109 return 1;
Denis Vlasenko22b0bd82008-08-05 13:45:22 +0000110 /* else: show the link, it's better than nothing */
Denis Vlasenko540baf62008-08-05 13:16:18 +0000111 }
112
113 ordinary_manpage:
Denis Vlasenko0e7f28d2008-08-05 15:28:05 +0000114 close(STDIN_FILENO);
115 open_zipped(man_filename); /* guaranteed to use fd 0 (STDIN_FILENO) */
Denis Vlasenko4cbffc02008-07-04 21:58:00 +0000116 /* "2>&1" is added so that nroff errors are shown in pager too.
Denis Vlasenkob75fe792008-06-27 22:31:07 +0000117 * Otherwise it may show just empty screen */
Denis Vlasenkof6efccc2008-07-05 08:50:08 +0000118 cmd = xasprintf(
Denis Vlasenko0e7f28d2008-08-05 15:28:05 +0000119 man ? "gtbl | nroff -Tlatin1 -mandoc 2>&1 | %s"
120 : "%s",
121 pager);
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000122 system(cmd);
123 free(cmd);
124 return 1;
125}
126
Denis Vlasenko0e7f28d2008-08-05 15:28:05 +0000127/* man_filename is of the form "/dir/dir/dir/name.s" Z_SUFFIX */
Denis Vlasenko540baf62008-08-05 13:16:18 +0000128static int show_manpage(const char *pager, char *man_filename, int man, int level)
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000129{
Denis Vlasenko0e7f28d2008-08-05 15:28:05 +0000130#if ENABLE_FEATURE_SEAMLESS_LZMA
131 if (run_pipe(pager, man_filename, man, level))
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000132 return 1;
Denis Vlasenko0e7f28d2008-08-05 15:28:05 +0000133#endif
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000134
Denis Vlasenko0e7f28d2008-08-05 15:28:05 +0000135#if ENABLE_FEATURE_SEAMLESS_BZ2
136#if ENABLE_FEATURE_SEAMLESS_LZMA
137 strcpy(strrchr(man_filename, '.') + 1, "bz2");
138#endif
139 if (run_pipe(pager, man_filename, man, level))
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000140 return 1;
Denis Vlasenko0e7f28d2008-08-05 15:28:05 +0000141#endif
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000142
Denis Vlasenko0e7f28d2008-08-05 15:28:05 +0000143#if ENABLE_FEATURE_SEAMLESS_GZ
144#if ENABLE_FEATURE_SEAMLESS_LZMA || ENABLE_FEATURE_SEAMLESS_BZ2
145 strcpy(strrchr(man_filename, '.') + 1, "gz");
146#endif
147 if (run_pipe(pager, man_filename, man, level))
148 return 1;
149#endif
150
151#if ENABLE_FEATURE_SEAMLESS_LZMA || ENABLE_FEATURE_SEAMLESS_BZ2 || ENABLE_FEATURE_SEAMLESS_GZ
152 *strrchr(man_filename, '.') = '\0';
153#endif
154 if (run_pipe(pager, man_filename, man, level))
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000155 return 1;
156
157 return 0;
158}
159
160int man_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000161int man_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000162{
Denis Vlasenko09aaf782008-07-20 17:41:30 +0000163 parser_t *parser;
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000164 const char *pager;
Denis Vlasenko50d068c2008-04-19 03:42:47 +0000165 char **man_path_list;
166 char *sec_list;
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000167 char *cur_path, *cur_sect;
Denis Vlasenko9835d472008-07-04 21:57:11 +0000168 int count_mp, cur_mp;
Denis Vlasenkoce02b152008-06-27 22:05:21 +0000169 int opt, not_found;
Denis Vlasenko084266e2008-07-26 23:08:31 +0000170 char *token[2];
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000171
172 opt_complementary = "-1"; /* at least one argument */
173 opt = getopt32(argv, "+aw");
174 argv += optind;
175
176 sec_list = xstrdup("1:2:3:4:5:6:7:8:9");
Denis Vlasenko9835d472008-07-04 21:57:11 +0000177 /* Last valid man_path_list[] is [0x10] */
Denis Vlasenkoa1e16c92008-09-14 21:30:35 +0000178 count_mp = 0;
Denis Vlasenko9835d472008-07-04 21:57:11 +0000179 man_path_list = xzalloc(0x11 * sizeof(man_path_list[0]));
Denis Vlasenkoe357d2a2008-09-14 21:26:55 +0000180 man_path_list[0] = getenv("MANPATH");
Denis Vlasenkoa1e16c92008-09-14 21:30:35 +0000181 if (!man_path_list[0]) /* default, may be overridden by /etc/man.conf */
Denis Vlasenkoe357d2a2008-09-14 21:26:55 +0000182 man_path_list[0] = (char*)"/usr/man";
Denis Vlasenkoa1e16c92008-09-14 21:30:35 +0000183 else
184 count_mp++;
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000185 pager = getenv("MANPAGER");
186 if (!pager) {
187 pager = getenv("PAGER");
188 if (!pager)
189 pager = "more";
190 }
191
Denys Vlasenko9a1b2602010-10-04 14:19:59 +0200192 /* Parse man.conf[ig] or man_db.conf */
Denys Vlasenko0e1b6262009-07-24 02:28:12 +0200193 /* man version 1.6f uses man.config */
Denys Vlasenko9a1b2602010-10-04 14:19:59 +0200194 /* man-db implementation of man uses man_db.conf */
Denys Vlasenko0e1b6262009-07-24 02:28:12 +0200195 parser = config_open2("/etc/man.config", fopen_for_read);
196 if (!parser)
197 parser = config_open2("/etc/man.conf", fopen_for_read);
Denys Vlasenko9a1b2602010-10-04 14:19:59 +0200198 if (!parser)
199 parser = config_open2("/etc/man_db.conf", fopen_for_read);
Denys Vlasenko0e1b6262009-07-24 02:28:12 +0200200
Denis Vlasenko94d03f02008-07-26 23:16:33 +0000201 while (config_read(parser, token, 2, 0, "# \t", PARSE_NORMAL)) {
202 if (!token[1])
203 continue;
Denys Vlasenko9a1b2602010-10-04 14:19:59 +0200204 if (strcmp("MANDATORY_MANPATH"+10, token[0]) == 0 /* "MANPATH"? */
205 || strcmp("MANDATORY_MANPATH", token[0]) == 0
206 ) {
Denis Vlasenkoe88bd2d2008-11-16 04:05:13 +0000207 char *path = token[1];
208 while (*path) {
209 char *next_path;
210 char **path_element;
211
212 next_path = strchr(path, ':');
213 if (next_path) {
214 *next_path = '\0';
215 if (next_path++ == path) /* "::"? */
216 goto next;
217 }
218 /* Do we already have path? */
219 path_element = man_path_list;
220 while (*path_element) {
221 if (strcmp(*path_element, path) == 0)
222 goto skip;
223 path_element++;
224 }
225 man_path_list = xrealloc_vector(man_path_list, 4, count_mp);
226 man_path_list[count_mp] = xstrdup(path);
227 count_mp++;
228 /* man_path_list is NULL terminated */
229 /*man_path_list[count_mp] = NULL; - xrealloc_vector did it */
230 skip:
231 if (!next_path)
232 break;
233 next:
234 path = next_path;
Denis Vlasenkoe357d2a2008-09-14 21:26:55 +0000235 }
Denis Vlasenko94d03f02008-07-26 23:16:33 +0000236 }
237 if (strcmp("MANSECT", token[0]) == 0) {
238 free(sec_list);
239 sec_list = xstrdup(token[1]);
240 }
241 }
242 config_close(parser);
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000243
Denis Vlasenkoce02b152008-06-27 22:05:21 +0000244 not_found = 0;
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000245 do { /* for each argv[] */
Denis Vlasenkoce02b152008-06-27 22:05:21 +0000246 int found = 0;
Denis Vlasenko50d068c2008-04-19 03:42:47 +0000247 cur_mp = 0;
Denis Vlasenko0e7f28d2008-08-05 15:28:05 +0000248
249 if (strchr(*argv, '/')) {
250 found = show_manpage(pager, *argv, /*man:*/ 1, 0);
251 goto check_found;
252 }
Denis Vlasenko50d068c2008-04-19 03:42:47 +0000253 while ((cur_path = man_path_list[cur_mp++]) != NULL) {
254 /* for each MANPATH */
Denis Vlasenkoe88bd2d2008-11-16 04:05:13 +0000255 cur_sect = sec_list;
256 do { /* for each section */
257 char *next_sect = strchrnul(cur_sect, ':');
258 int sect_len = next_sect - cur_sect;
259 char *man_filename;
260 int cat0man1 = 0;
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000261
Denis Vlasenkoe88bd2d2008-11-16 04:05:13 +0000262 /* Search for cat, then man page */
263 while (cat0man1 < 2) {
264 int found_here;
265 man_filename = xasprintf("%s/%s%.*s/%s.%.*s" Z_SUFFIX,
266 cur_path,
267 "cat\0man" + (cat0man1 * 4),
268 sect_len, cur_sect,
269 *argv,
270 sect_len, cur_sect);
271 found_here = show_manpage(pager, man_filename, cat0man1, 0);
272 found |= found_here;
273 cat0man1 += found_here + 1;
274 free(man_filename);
275 }
Denis Vlasenko4cbffc02008-07-04 21:58:00 +0000276
Denis Vlasenkoe88bd2d2008-11-16 04:05:13 +0000277 if (found && !(opt & OPT_a))
278 goto next_arg;
279 cur_sect = next_sect;
280 while (*cur_sect == ':')
281 cur_sect++;
282 } while (*cur_sect);
Denis Vlasenko50d068c2008-04-19 03:42:47 +0000283 }
Denis Vlasenko0e7f28d2008-08-05 15:28:05 +0000284 check_found:
Denis Vlasenkoce02b152008-06-27 22:05:21 +0000285 if (!found) {
286 bb_error_msg("no manual entry for '%s'", *argv);
287 not_found = 1;
288 }
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000289 next_arg:
290 argv++;
291 } while (*argv);
292
Denis Vlasenkoce02b152008-06-27 22:05:21 +0000293 return not_found;
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000294}