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.
6 //config: bool "man (27 kb)"
9 //config: Format and display manual pages.
11 //applet:IF_MAN(APPLET(man, BB_DIR_USR_BIN, BB_SUID_DROP))
13 //kbuild:lib-$(CONFIG_MAN) += man.o
15 //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"
19 //usage: "\n -a Display all pages"
20 //usage: "\n -w Show page locations"
22 //usage: "\n$COLUMNS overrides output width"
25 #include "common_bufsiz.h"
29 OPT_w = 2, /* print path */
32 /* This is what I see on my desktop system being executed:
37 gunzip -c '/usr/man/man1/bzip2.1.gz'
40 ) | gtbl | nroff -Tlatin1 -mandoc | less
42 Some systems use -Tascii.
44 On another system I see this:
46 ... | tbl | nroff -mandoc -rLL=<NNN>n -rLT=<NNN>n -Tutf8 | less
48 where <NNN> is screen width minus 5.
49 Replacing "DEFINE nroff nroff -mandoc" in /etc/man_db.conf
50 changes "nroff -mandoc" part; -rLL=<NNN>n, -rLT=<NNN>n and -Tutf8 parts are
51 appended to the user-specified command.
53 Redirecting to a pipe or file sets GROFF_NO_SGR=1 to prevent color escapes,
54 and uses "col -b -p -x" instead of pager, this filters out backspace
55 and underscore tricks.
64 #define G (*(struct globals*)bb_common_bufsiz1)
65 #define INIT_G() do { \
66 setup_common_bufsiz(); \
69 /* Removed -Tlatin1. Assuming system nroff has suitable default */ \
70 G.nroff = "nroff -mandoc"; \
71 G.pager = ENABLE_LESS ? "less" : "more"; \
74 static int show_manpage(char *man_filename, int man, int level);
76 static int run_pipe(char *man_filename, int man, int level)
80 /* Prevent man page link loops */
84 if (access(man_filename, R_OK) != 0)
87 if (option_mask32 & OPT_w) {
92 if (man) { /* man page, not cat page */
93 /* Is this a link to another manpage? */
94 /* The link has the following on the first line: */
95 /* ".so another_man_page" */
102 * man1/genhostid.1.gz: 203 bytes - smallest real manpage
103 * man2/path_resolution.2.gz: 114 bytes - largest link
105 xstat(man_filename, &sb);
106 if (sb.st_size > 300) /* err on the safe side */
107 goto ordinary_manpage;
109 line = xmalloc_open_zipped_read_close(man_filename, NULL);
110 if (!line || !is_prefixed_with(line, ".so ")) {
112 goto ordinary_manpage;
114 /* Example: man2/path_resolution.2.gz contains
115 * ".so man7/path_resolution.7\n<junk>"
117 *strchrnul(line, '\n') = '\0';
118 linkname = skip_whitespace(&line[4]);
120 /* 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, '/');
125 goto ordinary_manpage;
127 if (strchr(linkname, '/')) {
128 p = strrchr(man_filename, '/');
130 goto ordinary_manpage;
134 /* Links do not have .gz extensions, even if manpage
136 man_filename = xasprintf("%s/%s", man_filename, linkname);
138 /* Note: we leak "new" man_filename string as well... */
139 if (show_manpage(man_filename, man, level + 1))
141 /* else: show the link, it's better than nothing */
146 open_zipped(man_filename, /*fail_if_not_compressed:*/ 0); /* guaranteed to use fd 0 (STDIN_FILENO) */
148 int w = get_terminal_width(-1);
151 /* "2>&1" is added so that nroff errors are shown in pager too.
152 * Otherwise it may show just empty screen.
154 cmd = xasprintf("%s | %s -rLL=%un -rLT=%un 2>&1 | %s",
155 G.tbl, G.nroff, w, w,
158 cmd = xstrdup(G.pager);
165 /* man_filename is of the form "/dir/dir/dir/name.s" */
166 static int show_manpage(char *man_filename, int man, int level)
168 #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;
174 #if ENABLE_FEATURE_SEAMLESS_LZMA
175 if (run_pipe(filename_with_zext, man, level))
178 #if ENABLE_FEATURE_SEAMLESS_XZ
180 if (run_pipe(filename_with_zext, man, level))
183 #if ENABLE_FEATURE_SEAMLESS_BZ2
185 if (run_pipe(filename_with_zext, man, level))
188 #if ENABLE_FEATURE_SEAMLESS_GZ
190 if (run_pipe(filename_with_zext, man, level))
194 return run_pipe(man_filename, man, level);
197 static char **add_MANPATH(char **man_path_list, int *count_mp, char *path)
199 if (path) while (*path) {
203 next_path = strchr(path, ':');
205 if (next_path == path) /* "::"? */
209 /* Do we already have path? */
210 path_element = man_path_list;
211 if (path_element) while (*path_element) {
212 if (strcmp(*path_element, path) == 0)
216 man_path_list = xrealloc_vector(man_path_list, 4, *count_mp);
217 man_path_list[*count_mp] = xstrdup(path);
219 /* man_path_list is NULL terminated */
220 /* man_path_list[*count_mp] = NULL; - xrealloc_vector did it */
224 /* "path" may be a result of getenv(), be nice and don't mangle it */
227 path = next_path + 1;
229 return man_path_list;
232 static const char *if_redefined(const char *var, const char *key, const char *line)
234 if (!is_prefixed_with(line, key))
237 if (!isspace(line[0]))
239 return xstrdup(skip_whitespace(line));
242 int man_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
243 int man_main(int argc UNUSED_PARAM, char **argv)
247 char *cur_path, *cur_sect;
248 char **man_path_list;
256 opt_complementary = "-1"; /* at least one argument */
257 opt = getopt32(argv, "+aw");
260 sec_list = xstrdup("0p:1:1p:2:3:3p:4:5:6:7:8:9");
263 man_path_list = add_MANPATH(NULL, &count_mp,
264 getenv("MANDATORY_MANPATH"+10) /* "MANPATH" */
266 if (!man_path_list) {
267 /* default, may be overridden by /etc/man.conf */
268 man_path_list = xzalloc(2 * sizeof(man_path_list[0]));
269 man_path_list[0] = (char*)"/usr/man";
271 * Thus, man.conf will overwrite man_path_list[0]
272 * if a path is defined there.
276 /* Parse man.conf[ig] or man_db.conf */
277 /* man version 1.6f uses man.config */
278 /* man-db implementation of man uses man_db.conf */
279 parser = config_open2("/etc/man.config", fopen_for_read);
281 parser = config_open2("/etc/man.conf", fopen_for_read);
283 parser = config_open2("/etc/man_db.conf", fopen_for_read);
285 while (config_read(parser, token, 2, 0, "# \t", PARSE_NORMAL)) {
288 if (strcmp("DEFINE", token[0]) == 0) {
289 G.col = if_redefined(G.tbl , "col", token[1]);
290 G.tbl = if_redefined(G.tbl , "tbl", token[1]);
291 G.nroff = if_redefined(G.nroff, "nroff", token[1]);
292 G.pager = if_redefined(G.pager, "pager", token[1]);
294 if (strcmp("MANDATORY_MANPATH"+10, token[0]) == 0 /* "MANPATH"? */
295 || strcmp("MANDATORY_MANPATH", token[0]) == 0
297 man_path_list = add_MANPATH(man_path_list, &count_mp, token[1]);
299 if (strcmp("MANSECT", token[0]) == 0) {
301 sec_list = xstrdup(token[1]);
304 config_close(parser);
307 /* environment overrides setting from man.config */
308 char *env_pager = getenv("MANPAGER");
310 env_pager = getenv("PAGER");
315 if (!isatty(STDOUT_FILENO)) {
316 putenv((char*)"GROFF_NO_SGR=1");
317 G.pager = xasprintf("%s -b -p -x", G.col);
321 do { /* for each argv[] */
325 if (strchr(*argv, '/')) {
326 found = show_manpage(*argv, /*man:*/ 1, 0);
329 while ((cur_path = man_path_list[cur_mp++]) != NULL) {
330 /* for each MANPATH */
332 do { /* for each section */
333 char *next_sect = strchrnul(cur_sect, ':');
334 int sect_len = next_sect - cur_sect;
338 /* Search for cat, then man page */
339 while (cat0man1 < 2) {
341 man_filename = xasprintf("%s/%s%.*s/%s.%.*s",
343 "cat\0man" + (cat0man1 * 4),
347 found_here = show_manpage(man_filename, cat0man1, 0);
349 cat0man1 += found_here + 1;
353 if (found && !(opt & OPT_a))
355 cur_sect = next_sect;
356 while (*cur_sect == ':')
362 bb_error_msg("no manual entry for '%s'", *argv);