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 //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: "\n -a Display all pages"
11 //usage: "\n -w Show page locations"
13 //usage: "\n$COLUMNS overrides output width"
16 #include "common_bufsiz.h"
20 OPT_w = 2, /* print path */
23 /* This is what I see on my desktop system being executed:
28 gunzip -c '/usr/man/man1/bzip2.1.gz'
31 ) | gtbl | nroff -Tlatin1 -mandoc | less
33 Some systems use -Tascii.
35 On another system I see this:
37 ... | tbl | nroff -mandoc -rLL=<NNN>n -rLT=<NNN>n -Tutf8 | less
39 where <NNN> is screen width minus 5.
40 Replacing "DEFINE nroff nroff -mandoc" in /etc/man_db.conf
41 changes "nroff -mandoc" part; -rLL=<NNN>n, -rLT=<NNN>n and -Tutf8 parts are
42 appended to the user-specified command.
44 Redirecting to a pipe or file sets GROFF_NO_SGR=1 to prevent color escapes,
45 and uses "col -b -p -x" instead of pager, this filters out backspace
46 and underscore tricks.
55 #define G (*(struct globals*)bb_common_bufsiz1)
56 #define INIT_G() do { \
57 setup_common_bufsiz(); \
60 /* Removed -Tlatin1. Assuming system nroff has suitable default */ \
61 G.nroff = "nroff -mandoc"; \
62 G.pager = ENABLE_LESS ? "less" : "more"; \
65 static int show_manpage(char *man_filename, int man, int level);
67 static int run_pipe(char *man_filename, int man, int level)
71 /* Prevent man page link loops */
75 if (access(man_filename, R_OK) != 0)
78 if (option_mask32 & OPT_w) {
83 if (man) { /* man page, not cat page */
84 /* Is this a link to another manpage? */
85 /* The link has the following on the first line: */
86 /* ".so another_man_page" */
93 * man1/genhostid.1.gz: 203 bytes - smallest real manpage
94 * man2/path_resolution.2.gz: 114 bytes - largest link
96 xstat(man_filename, &sb);
97 if (sb.st_size > 300) /* err on the safe side */
98 goto ordinary_manpage;
100 line = xmalloc_open_zipped_read_close(man_filename, NULL);
101 if (!line || !is_prefixed_with(line, ".so ")) {
103 goto ordinary_manpage;
105 /* Example: man2/path_resolution.2.gz contains
106 * ".so man7/path_resolution.7\n<junk>"
108 *strchrnul(line, '\n') = '\0';
109 linkname = skip_whitespace(&line[4]);
111 /* If link has no slashes, we just replace man page name.
112 * If link has slashes (however many), we go back *once*.
113 * ".so zzz/ggg/page.3" does NOT go back two levels. */
114 p = strrchr(man_filename, '/');
116 goto ordinary_manpage;
118 if (strchr(linkname, '/')) {
119 p = strrchr(man_filename, '/');
121 goto ordinary_manpage;
125 /* Links do not have .gz extensions, even if manpage
127 man_filename = xasprintf("%s/%s", man_filename, linkname);
129 /* Note: we leak "new" man_filename string as well... */
130 if (show_manpage(man_filename, man, level + 1))
132 /* else: show the link, it's better than nothing */
137 open_zipped(man_filename, /*fail_if_not_compressed:*/ 0); /* guaranteed to use fd 0 (STDIN_FILENO) */
139 int w = get_terminal_width(-1);
142 /* "2>&1" is added so that nroff errors are shown in pager too.
143 * Otherwise it may show just empty screen.
145 cmd = xasprintf("%s | %s -rLL=%un -rLT=%un 2>&1 | %s",
146 G.tbl, G.nroff, w, w,
149 cmd = xstrdup(G.pager);
156 /* man_filename is of the form "/dir/dir/dir/name.s" */
157 static int show_manpage(char *man_filename, int man, int level)
159 #if SEAMLESS_COMPRESSION
160 /* We leak this allocation... */
161 char *filename_with_zext = xasprintf("%s.lzma", man_filename);
162 char *ext = strrchr(filename_with_zext, '.') + 1;
165 #if ENABLE_FEATURE_SEAMLESS_LZMA
166 if (run_pipe(filename_with_zext, man, level))
169 #if ENABLE_FEATURE_SEAMLESS_XZ
171 if (run_pipe(filename_with_zext, man, level))
174 #if ENABLE_FEATURE_SEAMLESS_BZ2
176 if (run_pipe(filename_with_zext, man, level))
179 #if ENABLE_FEATURE_SEAMLESS_GZ
181 if (run_pipe(filename_with_zext, man, level))
185 return run_pipe(man_filename, man, level);
188 static char **add_MANPATH(char **man_path_list, int *count_mp, char *path)
190 if (path) while (*path) {
194 next_path = strchr(path, ':');
196 if (next_path == path) /* "::"? */
200 /* Do we already have path? */
201 path_element = man_path_list;
202 if (path_element) while (*path_element) {
203 if (strcmp(*path_element, path) == 0)
207 man_path_list = xrealloc_vector(man_path_list, 4, *count_mp);
208 man_path_list[*count_mp] = xstrdup(path);
210 /* man_path_list is NULL terminated */
211 /* man_path_list[*count_mp] = NULL; - xrealloc_vector did it */
215 /* "path" may be a result of getenv(), be nice and don't mangle it */
218 path = next_path + 1;
220 return man_path_list;
223 static const char *if_redefined(const char *var, const char *key, const char *line)
225 if (!is_prefixed_with(line, key))
228 if (!isspace(line[0]))
230 return xstrdup(skip_whitespace(line));
233 int man_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
234 int man_main(int argc UNUSED_PARAM, char **argv)
238 char *cur_path, *cur_sect;
239 char **man_path_list;
247 opt_complementary = "-1"; /* at least one argument */
248 opt = getopt32(argv, "+aw");
251 sec_list = xstrdup("0p:1:1p:2:3:3p:4:5:6:7:8:9");
254 man_path_list = add_MANPATH(NULL, &count_mp,
255 getenv("MANDATORY_MANPATH"+10) /* "MANPATH" */
257 if (!man_path_list) {
258 /* default, may be overridden by /etc/man.conf */
259 man_path_list = xzalloc(2 * sizeof(man_path_list[0]));
260 man_path_list[0] = (char*)"/usr/man";
262 * Thus, man.conf will overwrite man_path_list[0]
263 * if a path is defined there.
267 /* Parse man.conf[ig] or man_db.conf */
268 /* man version 1.6f uses man.config */
269 /* man-db implementation of man uses man_db.conf */
270 parser = config_open2("/etc/man.config", fopen_for_read);
272 parser = config_open2("/etc/man.conf", fopen_for_read);
274 parser = config_open2("/etc/man_db.conf", fopen_for_read);
276 while (config_read(parser, token, 2, 0, "# \t", PARSE_NORMAL)) {
279 if (strcmp("DEFINE", token[0]) == 0) {
280 G.col = if_redefined(G.tbl , "col", token[1]);
281 G.tbl = if_redefined(G.tbl , "tbl", token[1]);
282 G.nroff = if_redefined(G.nroff, "nroff", token[1]);
283 G.pager = if_redefined(G.pager, "pager", token[1]);
285 if (strcmp("MANDATORY_MANPATH"+10, token[0]) == 0 /* "MANPATH"? */
286 || strcmp("MANDATORY_MANPATH", token[0]) == 0
288 man_path_list = add_MANPATH(man_path_list, &count_mp, token[1]);
290 if (strcmp("MANSECT", token[0]) == 0) {
292 sec_list = xstrdup(token[1]);
295 config_close(parser);
298 /* environment overrides setting from man.config */
299 char *env_pager = getenv("MANPAGER");
301 env_pager = getenv("PAGER");
306 if (!isatty(STDOUT_FILENO)) {
307 putenv((char*)"GROFF_NO_SGR=1");
308 G.pager = xasprintf("%s -b -p -x", G.col);
312 do { /* for each argv[] */
316 if (strchr(*argv, '/')) {
317 found = show_manpage(*argv, /*man:*/ 1, 0);
320 while ((cur_path = man_path_list[cur_mp++]) != NULL) {
321 /* for each MANPATH */
323 do { /* for each section */
324 char *next_sect = strchrnul(cur_sect, ':');
325 int sect_len = next_sect - cur_sect;
329 /* Search for cat, then man page */
330 while (cat0man1 < 2) {
332 man_filename = xasprintf("%s/%s%.*s/%s.%.*s",
334 "cat\0man" + (cat0man1 * 4),
338 found_here = show_manpage(man_filename, cat0man1, 0);
340 cat0man1 += found_here + 1;
344 if (found && !(opt & OPT_a))
346 cur_sect = next_sect;
347 while (*cur_sect == ':')
353 bb_error_msg("no manual entry for '%s'", *argv);