blob: cded6ebeea892a6476232a83d07d7a631af3d36f [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
6//config: bool "man"
7//config: default y
8//config: help
9//config: Format and display manual pages.
Denis Vlasenkoffa44992008-04-13 08:20:00 +000010
Pere Orga5bc8c002011-04-11 03:29:49 +020011//usage:#define man_trivial_usage
12//usage: "[-aw] [MANPAGE]..."
13//usage:#define man_full_usage "\n\n"
14//usage: "Format and display manual page\n"
Denys Vlasenko66426762011-06-05 03:58:28 +020015//usage: "\n -a Display all pages"
Pere Orga5bc8c002011-04-11 03:29:49 +020016//usage: "\n -w Show page locations"
Denys Vlasenko7c3c92c2016-10-31 01:52:18 +010017//usage: "\n"
18//usage: "\n$COLUMNS overrides output width"
Pere Orga5bc8c002011-04-11 03:29:49 +020019
Denis Vlasenkoffa44992008-04-13 08:20:00 +000020#include "libbb.h"
Denys Vlasenkoa92a7492016-10-30 22:31:30 +010021#include "common_bufsiz.h"
Denis Vlasenkoffa44992008-04-13 08:20:00 +000022
23enum {
24 OPT_a = 1, /* all */
25 OPT_w = 2, /* print path */
26};
27
Denis Vlasenkoce02b152008-06-27 22:05:21 +000028/* This is what I see on my desktop system being executed:
Denis Vlasenkoffa44992008-04-13 08:20:00 +000029(
30echo ".ll 12.4i"
31echo ".nr LL 12.4i"
32echo ".pl 1100i"
33gunzip -c '/usr/man/man1/bzip2.1.gz'
34echo ".\\\""
35echo ".pl \n(nlu+10"
36) | gtbl | nroff -Tlatin1 -mandoc | less
37
Denys Vlasenko2e6af542016-10-31 14:05:34 +010038Some systems use -Tascii.
39
Denys Vlasenkoa92a7492016-10-30 22:31:30 +010040On another system I see this:
41
42... | tbl | nroff -mandoc -rLL=<NNN>n -rLT=<NNN>n -Tutf8 | less
43
44where <NNN> is screen width minus 5.
45Replacing "DEFINE nroff nroff -mandoc" in /etc/man_db.conf
46changes "nroff -mandoc" part; -rLL=<NNN>n, -rLT=<NNN>n and -Tutf8 parts are
47appended to the user-specified command.
48
49Redirecting to a pipe or file sets GROFF_NO_SGR=1 to prevent color escapes,
50and uses "col -b -p -x" instead of pager, this filters out backspace
51and underscore tricks.
Denis Vlasenkoffa44992008-04-13 08:20:00 +000052*/
53
Denys Vlasenkoa92a7492016-10-30 22:31:30 +010054struct globals {
55 const char *col;
56 const char *tbl;
57 const char *nroff;
58 const char *pager;
59} FIX_ALIASING;
60#define G (*(struct globals*)bb_common_bufsiz1)
61#define INIT_G() do { \
62 setup_common_bufsiz(); \
63 G.col = "col"; \
64 G.tbl = "tbl"; \
Denys Vlasenko2e6af542016-10-31 14:05:34 +010065 /* Removed -Tlatin1. Assuming system nroff has suitable default */ \
66 G.nroff = "nroff -mandoc"; \
Denys Vlasenkoa92a7492016-10-30 22:31:30 +010067 G.pager = ENABLE_LESS ? "less" : "more"; \
68} while (0)
Denis Vlasenko540baf62008-08-05 13:16:18 +000069
Denys Vlasenkoa92a7492016-10-30 22:31:30 +010070static int show_manpage(char *man_filename, int man, int level);
71
72static int run_pipe(char *man_filename, int man, int level)
Denis Vlasenkoffa44992008-04-13 08:20:00 +000073{
74 char *cmd;
75
Denis Vlasenko540baf62008-08-05 13:16:18 +000076 /* Prevent man page link loops */
77 if (level > 10)
78 return 0;
79
Denis Vlasenkoffa44992008-04-13 08:20:00 +000080 if (access(man_filename, R_OK) != 0)
81 return 0;
82
83 if (option_mask32 & OPT_w) {
84 puts(man_filename);
85 return 1;
86 }
87
Denis Vlasenko540baf62008-08-05 13:16:18 +000088 if (man) { /* man page, not cat page */
Denis Vlasenkoe2a09de2008-08-05 14:15:19 +000089 /* Is this a link to another manpage? */
Denis Vlasenko540baf62008-08-05 13:16:18 +000090 /* The link has the following on the first line: */
91 /* ".so another_man_page" */
Denis Vlasenkoe2a09de2008-08-05 14:15:19 +000092
Denis Vlasenko540baf62008-08-05 13:16:18 +000093 struct stat sb;
94 char *line;
95 char *linkname, *p;
96
97 /* On my system:
98 * man1/genhostid.1.gz: 203 bytes - smallest real manpage
99 * man2/path_resolution.2.gz: 114 bytes - largest link
100 */
101 xstat(man_filename, &sb);
102 if (sb.st_size > 300) /* err on the safe side */
103 goto ordinary_manpage;
104
105 line = xmalloc_open_zipped_read_close(man_filename, NULL);
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100106 if (!line || !is_prefixed_with(line, ".so ")) {
Denis Vlasenko540baf62008-08-05 13:16:18 +0000107 free(line);
108 goto ordinary_manpage;
109 }
110 /* Example: man2/path_resolution.2.gz contains
111 * ".so man7/path_resolution.7\n<junk>"
112 */
113 *strchrnul(line, '\n') = '\0';
Denis Vlasenkoe2a09de2008-08-05 14:15:19 +0000114 linkname = skip_whitespace(&line[4]);
Denis Vlasenko540baf62008-08-05 13:16:18 +0000115
Denis Vlasenko22b0bd82008-08-05 13:45:22 +0000116 /* If link has no slashes, we just replace man page name.
117 * If link has slashes (however many), we go back *once*.
118 * ".so zzz/ggg/page.3" does NOT go back two levels. */
119 p = strrchr(man_filename, '/');
120 if (!p)
121 goto ordinary_manpage;
122 *p = '\0';
123 if (strchr(linkname, '/')) {
124 p = strrchr(man_filename, '/');
125 if (!p)
Denis Vlasenko540baf62008-08-05 13:16:18 +0000126 goto ordinary_manpage;
Denis Vlasenko22b0bd82008-08-05 13:45:22 +0000127 *p = '\0';
Denis Vlasenko540baf62008-08-05 13:16:18 +0000128 }
Denis Vlasenko22b0bd82008-08-05 13:45:22 +0000129
130 /* Links do not have .gz extensions, even if manpage
131 * is compressed */
Denys Vlasenko59655072012-03-06 16:23:50 +0100132 man_filename = xasprintf("%s/%s", man_filename, linkname);
Denis Vlasenko540baf62008-08-05 13:16:18 +0000133 free(line);
134 /* Note: we leak "new" man_filename string as well... */
Denys Vlasenkoa92a7492016-10-30 22:31:30 +0100135 if (show_manpage(man_filename, man, level + 1))
Denis Vlasenko540baf62008-08-05 13:16:18 +0000136 return 1;
Denis Vlasenko22b0bd82008-08-05 13:45:22 +0000137 /* else: show the link, it's better than nothing */
Denis Vlasenko540baf62008-08-05 13:16:18 +0000138 }
139
140 ordinary_manpage:
Denis Vlasenko0e7f28d2008-08-05 15:28:05 +0000141 close(STDIN_FILENO);
Denys Vlasenko640ce3d2014-02-02 02:06:38 +0100142 open_zipped(man_filename, /*fail_if_not_compressed:*/ 0); /* guaranteed to use fd 0 (STDIN_FILENO) */
Denys Vlasenkoa92a7492016-10-30 22:31:30 +0100143 if (man) {
Denys Vlasenko7c3c92c2016-10-31 01:52:18 +0100144 int w = get_terminal_width(-1);
Denys Vlasenkoa92a7492016-10-30 22:31:30 +0100145 if (w > 10)
146 w -= 2;
147 /* "2>&1" is added so that nroff errors are shown in pager too.
Denys Vlasenko7c3c92c2016-10-31 01:52:18 +0100148 * Otherwise it may show just empty screen.
149 */
Denys Vlasenkoa92a7492016-10-30 22:31:30 +0100150 cmd = xasprintf("%s | %s -rLL=%un -rLT=%un 2>&1 | %s",
151 G.tbl, G.nroff, w, w,
152 G.pager);
153 } else {
154 cmd = xstrdup(G.pager);
155 }
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000156 system(cmd);
157 free(cmd);
158 return 1;
159}
160
Denys Vlasenko59655072012-03-06 16:23:50 +0100161/* man_filename is of the form "/dir/dir/dir/name.s" */
Denys Vlasenkoa92a7492016-10-30 22:31:30 +0100162static int show_manpage(char *man_filename, int man, int level)
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000163{
Denys Vlasenko59655072012-03-06 16:23:50 +0100164#if SEAMLESS_COMPRESSION
165 /* We leak this allocation... */
166 char *filename_with_zext = xasprintf("%s.lzma", man_filename);
167 char *ext = strrchr(filename_with_zext, '.') + 1;
168#endif
169
Denis Vlasenko0e7f28d2008-08-05 15:28:05 +0000170#if ENABLE_FEATURE_SEAMLESS_LZMA
Denys Vlasenkoa92a7492016-10-30 22:31:30 +0100171 if (run_pipe(filename_with_zext, man, level))
Denys Vlasenko59655072012-03-06 16:23:50 +0100172 return 1;
173#endif
174#if ENABLE_FEATURE_SEAMLESS_XZ
175 strcpy(ext, "xz");
Denys Vlasenkoa92a7492016-10-30 22:31:30 +0100176 if (run_pipe(filename_with_zext, man, level))
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000177 return 1;
Denis Vlasenko0e7f28d2008-08-05 15:28:05 +0000178#endif
Denis Vlasenko0e7f28d2008-08-05 15:28:05 +0000179#if ENABLE_FEATURE_SEAMLESS_BZ2
Denys Vlasenko59655072012-03-06 16:23:50 +0100180 strcpy(ext, "bz2");
Denys Vlasenkoa92a7492016-10-30 22:31:30 +0100181 if (run_pipe(filename_with_zext, man, level))
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000182 return 1;
Denis Vlasenko0e7f28d2008-08-05 15:28:05 +0000183#endif
Denis Vlasenko0e7f28d2008-08-05 15:28:05 +0000184#if ENABLE_FEATURE_SEAMLESS_GZ
Denys Vlasenko59655072012-03-06 16:23:50 +0100185 strcpy(ext, "gz");
Denys Vlasenkoa92a7492016-10-30 22:31:30 +0100186 if (run_pipe(filename_with_zext, man, level))
Denis Vlasenko0e7f28d2008-08-05 15:28:05 +0000187 return 1;
188#endif
189
Denys Vlasenkoa92a7492016-10-30 22:31:30 +0100190 return run_pipe(man_filename, man, level);
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000191}
192
Denys Vlasenkoee410942014-11-27 00:40:08 +0100193static char **add_MANPATH(char **man_path_list, int *count_mp, char *path)
194{
195 if (path) while (*path) {
196 char *next_path;
197 char **path_element;
198
199 next_path = strchr(path, ':');
200 if (next_path) {
Denys Vlasenko476654c2014-11-30 19:39:58 +0100201 if (next_path == path) /* "::"? */
Denys Vlasenkoee410942014-11-27 00:40:08 +0100202 goto next;
Denys Vlasenko476654c2014-11-30 19:39:58 +0100203 *next_path = '\0';
Denys Vlasenkoee410942014-11-27 00:40:08 +0100204 }
205 /* Do we already have path? */
206 path_element = man_path_list;
207 if (path_element) while (*path_element) {
208 if (strcmp(*path_element, path) == 0)
209 goto skip;
210 path_element++;
211 }
212 man_path_list = xrealloc_vector(man_path_list, 4, *count_mp);
213 man_path_list[*count_mp] = xstrdup(path);
214 (*count_mp)++;
215 /* man_path_list is NULL terminated */
216 /* man_path_list[*count_mp] = NULL; - xrealloc_vector did it */
217 skip:
218 if (!next_path)
219 break;
Denys Vlasenko476654c2014-11-30 19:39:58 +0100220 /* "path" may be a result of getenv(), be nice and don't mangle it */
221 *next_path = ':';
Denys Vlasenkoee410942014-11-27 00:40:08 +0100222 next:
Denys Vlasenko476654c2014-11-30 19:39:58 +0100223 path = next_path + 1;
Denys Vlasenkoee410942014-11-27 00:40:08 +0100224 }
225 return man_path_list;
226}
227
Denys Vlasenkoa92a7492016-10-30 22:31:30 +0100228static const char *if_redefined(const char *var, const char *key, const char *line)
229{
230 if (!is_prefixed_with(line, key))
231 return var;
232 line += strlen(key);
233 if (!isspace(line[0]))
234 return var;
235 return xstrdup(skip_whitespace(line));
236}
237
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000238int man_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000239int man_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000240{
Denis Vlasenko09aaf782008-07-20 17:41:30 +0000241 parser_t *parser;
Denis Vlasenko50d068c2008-04-19 03:42:47 +0000242 char *sec_list;
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000243 char *cur_path, *cur_sect;
Denys Vlasenkoee410942014-11-27 00:40:08 +0100244 char **man_path_list;
245 int count_mp;
246 int cur_mp;
Denis Vlasenkoce02b152008-06-27 22:05:21 +0000247 int opt, not_found;
Denis Vlasenko084266e2008-07-26 23:08:31 +0000248 char *token[2];
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000249
Denys Vlasenkoa92a7492016-10-30 22:31:30 +0100250 INIT_G();
251
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000252 opt_complementary = "-1"; /* at least one argument */
253 opt = getopt32(argv, "+aw");
254 argv += optind;
255
John Spencerd6ae4fb2013-08-25 17:38:25 -0400256 sec_list = xstrdup("0p:1:1p:2:3:3p:4:5:6:7:8:9");
Denys Vlasenkoee410942014-11-27 00:40:08 +0100257
Denis Vlasenkoa1e16c92008-09-14 21:30:35 +0000258 count_mp = 0;
Denys Vlasenkoee410942014-11-27 00:40:08 +0100259 man_path_list = add_MANPATH(NULL, &count_mp,
260 getenv("MANDATORY_MANPATH"+10) /* "MANPATH" */
261 );
262 if (!man_path_list) {
263 /* default, may be overridden by /etc/man.conf */
264 man_path_list = xzalloc(2 * sizeof(man_path_list[0]));
Denis Vlasenkoe357d2a2008-09-14 21:26:55 +0000265 man_path_list[0] = (char*)"/usr/man";
Denys Vlasenkoee410942014-11-27 00:40:08 +0100266 /* count_mp stays 0.
267 * Thus, man.conf will overwrite man_path_list[0]
268 * if a path is defined there.
269 */
270 }
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000271
Denys Vlasenko9a1b2602010-10-04 14:19:59 +0200272 /* Parse man.conf[ig] or man_db.conf */
Denys Vlasenko0e1b6262009-07-24 02:28:12 +0200273 /* man version 1.6f uses man.config */
Denys Vlasenko9a1b2602010-10-04 14:19:59 +0200274 /* man-db implementation of man uses man_db.conf */
Denys Vlasenko0e1b6262009-07-24 02:28:12 +0200275 parser = config_open2("/etc/man.config", fopen_for_read);
276 if (!parser)
277 parser = config_open2("/etc/man.conf", fopen_for_read);
Denys Vlasenko9a1b2602010-10-04 14:19:59 +0200278 if (!parser)
279 parser = config_open2("/etc/man_db.conf", fopen_for_read);
Denys Vlasenko0e1b6262009-07-24 02:28:12 +0200280
Denis Vlasenko94d03f02008-07-26 23:16:33 +0000281 while (config_read(parser, token, 2, 0, "# \t", PARSE_NORMAL)) {
282 if (!token[1])
283 continue;
John Spencerff650612014-01-22 15:31:10 +0100284 if (strcmp("DEFINE", token[0]) == 0) {
Denys Vlasenkoa92a7492016-10-30 22:31:30 +0100285 G.col = if_redefined(G.tbl , "col", token[1]);
286 G.tbl = if_redefined(G.tbl , "tbl", token[1]);
287 G.nroff = if_redefined(G.nroff, "nroff", token[1]);
288 G.pager = if_redefined(G.pager, "pager", token[1]);
John Spencerff650612014-01-22 15:31:10 +0100289 } else
Denys Vlasenko9a1b2602010-10-04 14:19:59 +0200290 if (strcmp("MANDATORY_MANPATH"+10, token[0]) == 0 /* "MANPATH"? */
291 || strcmp("MANDATORY_MANPATH", token[0]) == 0
292 ) {
Denys Vlasenkoee410942014-11-27 00:40:08 +0100293 man_path_list = add_MANPATH(man_path_list, &count_mp, token[1]);
Denis Vlasenko94d03f02008-07-26 23:16:33 +0000294 }
295 if (strcmp("MANSECT", token[0]) == 0) {
296 free(sec_list);
297 sec_list = xstrdup(token[1]);
298 }
299 }
300 config_close(parser);
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000301
John Spencerff650612014-01-22 15:31:10 +0100302 {
303 /* environment overrides setting from man.config */
304 char *env_pager = getenv("MANPAGER");
305 if (!env_pager)
306 env_pager = getenv("PAGER");
307 if (env_pager)
Denys Vlasenkoa92a7492016-10-30 22:31:30 +0100308 G.pager = env_pager;
309 }
310
311 if (!isatty(STDOUT_FILENO)) {
312 putenv((char*)"GROFF_NO_SGR=1");
313 G.pager = xasprintf("%s -b -p -x", G.col);
John Spencerff650612014-01-22 15:31:10 +0100314 }
315
Denis Vlasenkoce02b152008-06-27 22:05:21 +0000316 not_found = 0;
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000317 do { /* for each argv[] */
Denis Vlasenkoce02b152008-06-27 22:05:21 +0000318 int found = 0;
Denis Vlasenko50d068c2008-04-19 03:42:47 +0000319 cur_mp = 0;
Denis Vlasenko0e7f28d2008-08-05 15:28:05 +0000320
321 if (strchr(*argv, '/')) {
Denys Vlasenkoa92a7492016-10-30 22:31:30 +0100322 found = show_manpage(*argv, /*man:*/ 1, 0);
Denis Vlasenko0e7f28d2008-08-05 15:28:05 +0000323 goto check_found;
324 }
Denis Vlasenko50d068c2008-04-19 03:42:47 +0000325 while ((cur_path = man_path_list[cur_mp++]) != NULL) {
326 /* for each MANPATH */
Denis Vlasenkoe88bd2d2008-11-16 04:05:13 +0000327 cur_sect = sec_list;
328 do { /* for each section */
329 char *next_sect = strchrnul(cur_sect, ':');
330 int sect_len = next_sect - cur_sect;
331 char *man_filename;
332 int cat0man1 = 0;
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000333
Denis Vlasenkoe88bd2d2008-11-16 04:05:13 +0000334 /* Search for cat, then man page */
335 while (cat0man1 < 2) {
336 int found_here;
Denys Vlasenko59655072012-03-06 16:23:50 +0100337 man_filename = xasprintf("%s/%s%.*s/%s.%.*s",
Denis Vlasenkoe88bd2d2008-11-16 04:05:13 +0000338 cur_path,
339 "cat\0man" + (cat0man1 * 4),
340 sect_len, cur_sect,
341 *argv,
342 sect_len, cur_sect);
Denys Vlasenkoa92a7492016-10-30 22:31:30 +0100343 found_here = show_manpage(man_filename, cat0man1, 0);
Denis Vlasenkoe88bd2d2008-11-16 04:05:13 +0000344 found |= found_here;
345 cat0man1 += found_here + 1;
346 free(man_filename);
347 }
Denis Vlasenko4cbffc02008-07-04 21:58:00 +0000348
Denis Vlasenkoe88bd2d2008-11-16 04:05:13 +0000349 if (found && !(opt & OPT_a))
350 goto next_arg;
351 cur_sect = next_sect;
352 while (*cur_sect == ':')
353 cur_sect++;
354 } while (*cur_sect);
Denis Vlasenko50d068c2008-04-19 03:42:47 +0000355 }
Denis Vlasenko0e7f28d2008-08-05 15:28:05 +0000356 check_found:
Denis Vlasenkoce02b152008-06-27 22:05:21 +0000357 if (!found) {
358 bb_error_msg("no manual entry for '%s'", *argv);
359 not_found = 1;
360 }
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000361 next_arg:
362 argv++;
363 } while (*argv);
364
Denis Vlasenkoce02b152008-06-27 22:05:21 +0000365 return not_found;
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000366}