blob: 9884325b7b3eae713ddb0d4f69e562369b06ae98 [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 */
Denys Vlasenkofb4da162016-11-22 23:14:24 +01005//config:config MAN
Denys Vlasenkob097a842018-12-28 03:20:17 +01006//config: bool "man (26 kb)"
Denys Vlasenkofb4da162016-11-22 23:14:24 +01007//config: default y
8//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +02009//config: Format and display manual pages.
Denis Vlasenkoffa44992008-04-13 08:20:00 +000010
Denys Vlasenkof88e3bf2016-11-22 23:54:17 +010011//applet:IF_MAN(APPLET(man, BB_DIR_USR_BIN, BB_SUID_DROP))
12
13//kbuild:lib-$(CONFIG_MAN) += man.o
14
Pere Orga5bc8c002011-04-11 03:29:49 +020015//usage:#define man_trivial_usage
16//usage: "[-aw] [MANPAGE]..."
17//usage:#define man_full_usage "\n\n"
18//usage: "Format and display manual page\n"
Denys Vlasenko66426762011-06-05 03:58:28 +020019//usage: "\n -a Display all pages"
Pere Orga5bc8c002011-04-11 03:29:49 +020020//usage: "\n -w Show page locations"
Denys Vlasenko7c3c92c2016-10-31 01:52:18 +010021//usage: "\n"
22//usage: "\n$COLUMNS overrides output width"
Pere Orga5bc8c002011-04-11 03:29:49 +020023
Denis Vlasenkoffa44992008-04-13 08:20:00 +000024#include "libbb.h"
Denys Vlasenkoa92a7492016-10-30 22:31:30 +010025#include "common_bufsiz.h"
Denis Vlasenkoffa44992008-04-13 08:20:00 +000026
27enum {
28 OPT_a = 1, /* all */
29 OPT_w = 2, /* print path */
30};
31
Denis Vlasenkoce02b152008-06-27 22:05:21 +000032/* This is what I see on my desktop system being executed:
Denis Vlasenkoffa44992008-04-13 08:20:00 +000033(
34echo ".ll 12.4i"
35echo ".nr LL 12.4i"
36echo ".pl 1100i"
37gunzip -c '/usr/man/man1/bzip2.1.gz'
38echo ".\\\""
39echo ".pl \n(nlu+10"
40) | gtbl | nroff -Tlatin1 -mandoc | less
41
Denys Vlasenko2e6af542016-10-31 14:05:34 +010042Some systems use -Tascii.
43
Denys Vlasenkoa92a7492016-10-30 22:31:30 +010044On another system I see this:
45
46... | tbl | nroff -mandoc -rLL=<NNN>n -rLT=<NNN>n -Tutf8 | less
47
48where <NNN> is screen width minus 5.
49Replacing "DEFINE nroff nroff -mandoc" in /etc/man_db.conf
50changes "nroff -mandoc" part; -rLL=<NNN>n, -rLT=<NNN>n and -Tutf8 parts are
51appended to the user-specified command.
52
53Redirecting to a pipe or file sets GROFF_NO_SGR=1 to prevent color escapes,
54and uses "col -b -p -x" instead of pager, this filters out backspace
55and underscore tricks.
Denis Vlasenkoffa44992008-04-13 08:20:00 +000056*/
57
Denys Vlasenkoa92a7492016-10-30 22:31:30 +010058struct globals {
59 const char *col;
60 const char *tbl;
61 const char *nroff;
62 const char *pager;
63} FIX_ALIASING;
64#define G (*(struct globals*)bb_common_bufsiz1)
65#define INIT_G() do { \
66 setup_common_bufsiz(); \
67 G.col = "col"; \
68 G.tbl = "tbl"; \
Denys Vlasenko2e6af542016-10-31 14:05:34 +010069 /* Removed -Tlatin1. Assuming system nroff has suitable default */ \
70 G.nroff = "nroff -mandoc"; \
Denys Vlasenkoa92a7492016-10-30 22:31:30 +010071 G.pager = ENABLE_LESS ? "less" : "more"; \
72} while (0)
Denis Vlasenko540baf62008-08-05 13:16:18 +000073
Denys Vlasenkoa92a7492016-10-30 22:31:30 +010074static int show_manpage(char *man_filename, int man, int level);
75
76static int run_pipe(char *man_filename, int man, int level)
Denis Vlasenkoffa44992008-04-13 08:20:00 +000077{
78 char *cmd;
79
Denis Vlasenko540baf62008-08-05 13:16:18 +000080 /* Prevent man page link loops */
81 if (level > 10)
82 return 0;
83
Denis Vlasenkoffa44992008-04-13 08:20:00 +000084 if (access(man_filename, R_OK) != 0)
85 return 0;
86
87 if (option_mask32 & OPT_w) {
88 puts(man_filename);
89 return 1;
90 }
91
Denis Vlasenko540baf62008-08-05 13:16:18 +000092 if (man) { /* man page, not cat page */
Denis Vlasenkoe2a09de2008-08-05 14:15:19 +000093 /* Is this a link to another manpage? */
Denis Vlasenko540baf62008-08-05 13:16:18 +000094 /* The link has the following on the first line: */
95 /* ".so another_man_page" */
Denis Vlasenkoe2a09de2008-08-05 14:15:19 +000096
Denis Vlasenko540baf62008-08-05 13:16:18 +000097 struct stat sb;
98 char *line;
99 char *linkname, *p;
100
101 /* On my system:
102 * man1/genhostid.1.gz: 203 bytes - smallest real manpage
103 * man2/path_resolution.2.gz: 114 bytes - largest link
104 */
105 xstat(man_filename, &sb);
106 if (sb.st_size > 300) /* err on the safe side */
107 goto ordinary_manpage;
108
109 line = xmalloc_open_zipped_read_close(man_filename, NULL);
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100110 if (!line || !is_prefixed_with(line, ".so ")) {
Denis Vlasenko540baf62008-08-05 13:16:18 +0000111 free(line);
112 goto ordinary_manpage;
113 }
114 /* Example: man2/path_resolution.2.gz contains
115 * ".so man7/path_resolution.7\n<junk>"
116 */
117 *strchrnul(line, '\n') = '\0';
Denis Vlasenkoe2a09de2008-08-05 14:15:19 +0000118 linkname = skip_whitespace(&line[4]);
Denis Vlasenko540baf62008-08-05 13:16:18 +0000119
Denis Vlasenko22b0bd82008-08-05 13:45:22 +0000120 /* If link has no slashes, we just replace man page name.
121 * If link has slashes (however many), we go back *once*.
122 * ".so zzz/ggg/page.3" does NOT go back two levels. */
123 p = strrchr(man_filename, '/');
124 if (!p)
125 goto ordinary_manpage;
126 *p = '\0';
127 if (strchr(linkname, '/')) {
128 p = strrchr(man_filename, '/');
129 if (!p)
Denis Vlasenko540baf62008-08-05 13:16:18 +0000130 goto ordinary_manpage;
Denis Vlasenko22b0bd82008-08-05 13:45:22 +0000131 *p = '\0';
Denis Vlasenko540baf62008-08-05 13:16:18 +0000132 }
Denis Vlasenko22b0bd82008-08-05 13:45:22 +0000133
134 /* Links do not have .gz extensions, even if manpage
135 * is compressed */
Denys Vlasenko59655072012-03-06 16:23:50 +0100136 man_filename = xasprintf("%s/%s", man_filename, linkname);
Denis Vlasenko540baf62008-08-05 13:16:18 +0000137 free(line);
138 /* Note: we leak "new" man_filename string as well... */
Denys Vlasenkoa92a7492016-10-30 22:31:30 +0100139 if (show_manpage(man_filename, man, level + 1))
Denis Vlasenko540baf62008-08-05 13:16:18 +0000140 return 1;
Denis Vlasenko22b0bd82008-08-05 13:45:22 +0000141 /* else: show the link, it's better than nothing */
Denis Vlasenko540baf62008-08-05 13:16:18 +0000142 }
143
144 ordinary_manpage:
Denis Vlasenko0e7f28d2008-08-05 15:28:05 +0000145 close(STDIN_FILENO);
Denys Vlasenko640ce3d2014-02-02 02:06:38 +0100146 open_zipped(man_filename, /*fail_if_not_compressed:*/ 0); /* guaranteed to use fd 0 (STDIN_FILENO) */
Denys Vlasenkoa92a7492016-10-30 22:31:30 +0100147 if (man) {
Denys Vlasenko7c3c92c2016-10-31 01:52:18 +0100148 int w = get_terminal_width(-1);
Denys Vlasenkoa92a7492016-10-30 22:31:30 +0100149 if (w > 10)
150 w -= 2;
151 /* "2>&1" is added so that nroff errors are shown in pager too.
Denys Vlasenko7c3c92c2016-10-31 01:52:18 +0100152 * Otherwise it may show just empty screen.
153 */
Denys Vlasenkoa92a7492016-10-30 22:31:30 +0100154 cmd = xasprintf("%s | %s -rLL=%un -rLT=%un 2>&1 | %s",
155 G.tbl, G.nroff, w, w,
156 G.pager);
157 } else {
158 cmd = xstrdup(G.pager);
159 }
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000160 system(cmd);
161 free(cmd);
162 return 1;
163}
164
Denys Vlasenko59655072012-03-06 16:23:50 +0100165/* man_filename is of the form "/dir/dir/dir/name.s" */
Denys Vlasenkoa92a7492016-10-30 22:31:30 +0100166static int show_manpage(char *man_filename, int man, int level)
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000167{
Denys Vlasenko59655072012-03-06 16:23:50 +0100168#if SEAMLESS_COMPRESSION
169 /* We leak this allocation... */
170 char *filename_with_zext = xasprintf("%s.lzma", man_filename);
171 char *ext = strrchr(filename_with_zext, '.') + 1;
172#endif
173
Denis Vlasenko0e7f28d2008-08-05 15:28:05 +0000174#if ENABLE_FEATURE_SEAMLESS_LZMA
Denys Vlasenkoa92a7492016-10-30 22:31:30 +0100175 if (run_pipe(filename_with_zext, man, level))
Denys Vlasenko59655072012-03-06 16:23:50 +0100176 return 1;
177#endif
178#if ENABLE_FEATURE_SEAMLESS_XZ
179 strcpy(ext, "xz");
Denys Vlasenkoa92a7492016-10-30 22:31:30 +0100180 if (run_pipe(filename_with_zext, man, level))
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000181 return 1;
Denis Vlasenko0e7f28d2008-08-05 15:28:05 +0000182#endif
Denis Vlasenko0e7f28d2008-08-05 15:28:05 +0000183#if ENABLE_FEATURE_SEAMLESS_BZ2
Denys Vlasenko59655072012-03-06 16:23:50 +0100184 strcpy(ext, "bz2");
Denys Vlasenkoa92a7492016-10-30 22:31:30 +0100185 if (run_pipe(filename_with_zext, man, level))
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000186 return 1;
Denis Vlasenko0e7f28d2008-08-05 15:28:05 +0000187#endif
Denis Vlasenko0e7f28d2008-08-05 15:28:05 +0000188#if ENABLE_FEATURE_SEAMLESS_GZ
Denys Vlasenko59655072012-03-06 16:23:50 +0100189 strcpy(ext, "gz");
Denys Vlasenkoa92a7492016-10-30 22:31:30 +0100190 if (run_pipe(filename_with_zext, man, level))
Denis Vlasenko0e7f28d2008-08-05 15:28:05 +0000191 return 1;
192#endif
193
Denys Vlasenkoa92a7492016-10-30 22:31:30 +0100194 return run_pipe(man_filename, man, level);
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000195}
196
Denys Vlasenkoee410942014-11-27 00:40:08 +0100197static char **add_MANPATH(char **man_path_list, int *count_mp, char *path)
198{
199 if (path) while (*path) {
200 char *next_path;
201 char **path_element;
202
203 next_path = strchr(path, ':');
204 if (next_path) {
Denys Vlasenko476654c2014-11-30 19:39:58 +0100205 if (next_path == path) /* "::"? */
Denys Vlasenkoee410942014-11-27 00:40:08 +0100206 goto next;
Denys Vlasenko476654c2014-11-30 19:39:58 +0100207 *next_path = '\0';
Denys Vlasenkoee410942014-11-27 00:40:08 +0100208 }
209 /* Do we already have path? */
210 path_element = man_path_list;
211 if (path_element) while (*path_element) {
Ron Yorston3c6f3332019-03-22 16:25:32 +0000212 if (strcmp(*path_element, path) == 0) {
213 /* Have path but haven't counted it, must be default */
214 if (*count_mp == 0)
215 break;
Denys Vlasenkoee410942014-11-27 00:40:08 +0100216 goto skip;
Ron Yorston3c6f3332019-03-22 16:25:32 +0000217 }
Denys Vlasenkoee410942014-11-27 00:40:08 +0100218 path_element++;
219 }
220 man_path_list = xrealloc_vector(man_path_list, 4, *count_mp);
221 man_path_list[*count_mp] = xstrdup(path);
222 (*count_mp)++;
223 /* man_path_list is NULL terminated */
224 /* man_path_list[*count_mp] = NULL; - xrealloc_vector did it */
225 skip:
226 if (!next_path)
227 break;
Denys Vlasenko476654c2014-11-30 19:39:58 +0100228 /* "path" may be a result of getenv(), be nice and don't mangle it */
229 *next_path = ':';
Denys Vlasenkoee410942014-11-27 00:40:08 +0100230 next:
Denys Vlasenko476654c2014-11-30 19:39:58 +0100231 path = next_path + 1;
Denys Vlasenkoee410942014-11-27 00:40:08 +0100232 }
233 return man_path_list;
234}
235
Denys Vlasenkoa92a7492016-10-30 22:31:30 +0100236static const char *if_redefined(const char *var, const char *key, const char *line)
237{
238 if (!is_prefixed_with(line, key))
239 return var;
240 line += strlen(key);
241 if (!isspace(line[0]))
242 return var;
243 return xstrdup(skip_whitespace(line));
244}
245
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000246int man_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000247int man_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000248{
Denis Vlasenko09aaf782008-07-20 17:41:30 +0000249 parser_t *parser;
Denis Vlasenko50d068c2008-04-19 03:42:47 +0000250 char *sec_list;
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000251 char *cur_path, *cur_sect;
Denys Vlasenkoee410942014-11-27 00:40:08 +0100252 char **man_path_list;
253 int count_mp;
254 int cur_mp;
Denis Vlasenkoce02b152008-06-27 22:05:21 +0000255 int opt, not_found;
Denis Vlasenko084266e2008-07-26 23:08:31 +0000256 char *token[2];
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000257
Denys Vlasenkoa92a7492016-10-30 22:31:30 +0100258 INIT_G();
259
Denys Vlasenko22542ec2017-08-08 21:55:02 +0200260 opt = getopt32(argv, "^+" "aw" "\0" "-1"/*at least one arg*/);
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000261 argv += optind;
262
John Spencerd6ae4fb2013-08-25 17:38:25 -0400263 sec_list = xstrdup("0p:1:1p:2:3:3p:4:5:6:7:8:9");
Denys Vlasenkoee410942014-11-27 00:40:08 +0100264
Denis Vlasenkoa1e16c92008-09-14 21:30:35 +0000265 count_mp = 0;
Denys Vlasenkoee410942014-11-27 00:40:08 +0100266 man_path_list = add_MANPATH(NULL, &count_mp,
267 getenv("MANDATORY_MANPATH"+10) /* "MANPATH" */
268 );
269 if (!man_path_list) {
270 /* default, may be overridden by /etc/man.conf */
271 man_path_list = xzalloc(2 * sizeof(man_path_list[0]));
Denis Vlasenkoe357d2a2008-09-14 21:26:55 +0000272 man_path_list[0] = (char*)"/usr/man";
Denys Vlasenkoee410942014-11-27 00:40:08 +0100273 /* count_mp stays 0.
274 * Thus, man.conf will overwrite man_path_list[0]
275 * if a path is defined there.
276 */
277 }
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000278
Denys Vlasenko9a1b2602010-10-04 14:19:59 +0200279 /* Parse man.conf[ig] or man_db.conf */
Denys Vlasenko0e1b6262009-07-24 02:28:12 +0200280 /* man version 1.6f uses man.config */
Denys Vlasenko9a1b2602010-10-04 14:19:59 +0200281 /* man-db implementation of man uses man_db.conf */
Denys Vlasenko0e1b6262009-07-24 02:28:12 +0200282 parser = config_open2("/etc/man.config", fopen_for_read);
283 if (!parser)
284 parser = config_open2("/etc/man.conf", fopen_for_read);
Denys Vlasenko9a1b2602010-10-04 14:19:59 +0200285 if (!parser)
286 parser = config_open2("/etc/man_db.conf", fopen_for_read);
Denys Vlasenko0e1b6262009-07-24 02:28:12 +0200287
Denis Vlasenko94d03f02008-07-26 23:16:33 +0000288 while (config_read(parser, token, 2, 0, "# \t", PARSE_NORMAL)) {
289 if (!token[1])
290 continue;
John Spencerff650612014-01-22 15:31:10 +0100291 if (strcmp("DEFINE", token[0]) == 0) {
Denys Vlasenkoa92a7492016-10-30 22:31:30 +0100292 G.col = if_redefined(G.tbl , "col", token[1]);
293 G.tbl = if_redefined(G.tbl , "tbl", token[1]);
294 G.nroff = if_redefined(G.nroff, "nroff", token[1]);
295 G.pager = if_redefined(G.pager, "pager", token[1]);
John Spencerff650612014-01-22 15:31:10 +0100296 } else
Denys Vlasenko9a1b2602010-10-04 14:19:59 +0200297 if (strcmp("MANDATORY_MANPATH"+10, token[0]) == 0 /* "MANPATH"? */
298 || strcmp("MANDATORY_MANPATH", token[0]) == 0
299 ) {
Denys Vlasenkoee410942014-11-27 00:40:08 +0100300 man_path_list = add_MANPATH(man_path_list, &count_mp, token[1]);
Denis Vlasenko94d03f02008-07-26 23:16:33 +0000301 }
302 if (strcmp("MANSECT", token[0]) == 0) {
303 free(sec_list);
304 sec_list = xstrdup(token[1]);
305 }
306 }
307 config_close(parser);
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000308
John Spencerff650612014-01-22 15:31:10 +0100309 {
310 /* environment overrides setting from man.config */
311 char *env_pager = getenv("MANPAGER");
312 if (!env_pager)
313 env_pager = getenv("PAGER");
314 if (env_pager)
Denys Vlasenkoa92a7492016-10-30 22:31:30 +0100315 G.pager = env_pager;
316 }
317
318 if (!isatty(STDOUT_FILENO)) {
319 putenv((char*)"GROFF_NO_SGR=1");
320 G.pager = xasprintf("%s -b -p -x", G.col);
John Spencerff650612014-01-22 15:31:10 +0100321 }
322
Denis Vlasenkoce02b152008-06-27 22:05:21 +0000323 not_found = 0;
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000324 do { /* for each argv[] */
Denis Vlasenkoce02b152008-06-27 22:05:21 +0000325 int found = 0;
Denis Vlasenko50d068c2008-04-19 03:42:47 +0000326 cur_mp = 0;
Denis Vlasenko0e7f28d2008-08-05 15:28:05 +0000327
328 if (strchr(*argv, '/')) {
Denys Vlasenkoa92a7492016-10-30 22:31:30 +0100329 found = show_manpage(*argv, /*man:*/ 1, 0);
Denis Vlasenko0e7f28d2008-08-05 15:28:05 +0000330 goto check_found;
331 }
Denis Vlasenko50d068c2008-04-19 03:42:47 +0000332 while ((cur_path = man_path_list[cur_mp++]) != NULL) {
333 /* for each MANPATH */
Denis Vlasenkoe88bd2d2008-11-16 04:05:13 +0000334 cur_sect = sec_list;
335 do { /* for each section */
336 char *next_sect = strchrnul(cur_sect, ':');
337 int sect_len = next_sect - cur_sect;
338 char *man_filename;
339 int cat0man1 = 0;
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000340
Denis Vlasenkoe88bd2d2008-11-16 04:05:13 +0000341 /* Search for cat, then man page */
342 while (cat0man1 < 2) {
343 int found_here;
Denys Vlasenko59655072012-03-06 16:23:50 +0100344 man_filename = xasprintf("%s/%s%.*s/%s.%.*s",
Denis Vlasenkoe88bd2d2008-11-16 04:05:13 +0000345 cur_path,
346 "cat\0man" + (cat0man1 * 4),
347 sect_len, cur_sect,
348 *argv,
349 sect_len, cur_sect);
Denys Vlasenkoa92a7492016-10-30 22:31:30 +0100350 found_here = show_manpage(man_filename, cat0man1, 0);
Denis Vlasenkoe88bd2d2008-11-16 04:05:13 +0000351 found |= found_here;
352 cat0man1 += found_here + 1;
353 free(man_filename);
354 }
Denis Vlasenko4cbffc02008-07-04 21:58:00 +0000355
Denis Vlasenkoe88bd2d2008-11-16 04:05:13 +0000356 if (found && !(opt & OPT_a))
357 goto next_arg;
358 cur_sect = next_sect;
359 while (*cur_sect == ':')
360 cur_sect++;
361 } while (*cur_sect);
Denis Vlasenko50d068c2008-04-19 03:42:47 +0000362 }
Denis Vlasenko0e7f28d2008-08-05 15:28:05 +0000363 check_found:
Denis Vlasenkoce02b152008-06-27 22:05:21 +0000364 if (!found) {
365 bb_error_msg("no manual entry for '%s'", *argv);
366 not_found = 1;
367 }
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000368 next_arg:
369 argv++;
370 } while (*argv);
371
Denis Vlasenkoce02b152008-06-27 22:05:21 +0000372 return not_found;
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000373}