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"
14 #include "common_bufsiz.h"
18 OPT_w = 2, /* print path */
21 /* This is what I see on my desktop system being executed:
26 gunzip -c '/usr/man/man1/bzip2.1.gz'
29 ) | gtbl | nroff -Tlatin1 -mandoc | less
31 On another system I see this:
33 ... | tbl | nroff -mandoc -rLL=<NNN>n -rLT=<NNN>n -Tutf8 | less
35 where <NNN> is screen width minus 5.
36 Replacing "DEFINE nroff nroff -mandoc" in /etc/man_db.conf
37 changes "nroff -mandoc" part; -rLL=<NNN>n, -rLT=<NNN>n and -Tutf8 parts are
38 appended to the user-specified command.
40 Redirecting to a pipe or file sets GROFF_NO_SGR=1 to prevent color escapes,
41 and uses "col -b -p -x" instead of pager, this filters out backspace
42 and underscore tricks.
51 #define G (*(struct globals*)bb_common_bufsiz1)
52 #define INIT_G() do { \
53 setup_common_bufsiz(); \
56 /* replaced -Tlatin1 with -Tascii for non-UTF8 displays */; \
57 G.nroff = "nroff -mandoc -Tascii"; \
58 G.pager = ENABLE_LESS ? "less" : "more"; \
61 static int show_manpage(char *man_filename, int man, int level);
63 static int run_pipe(char *man_filename, int man, int level)
67 /* Prevent man page link loops */
71 if (access(man_filename, R_OK) != 0)
74 if (option_mask32 & OPT_w) {
79 if (man) { /* man page, not cat page */
80 /* Is this a link to another manpage? */
81 /* The link has the following on the first line: */
82 /* ".so another_man_page" */
89 * man1/genhostid.1.gz: 203 bytes - smallest real manpage
90 * man2/path_resolution.2.gz: 114 bytes - largest link
92 xstat(man_filename, &sb);
93 if (sb.st_size > 300) /* err on the safe side */
94 goto ordinary_manpage;
96 line = xmalloc_open_zipped_read_close(man_filename, NULL);
97 if (!line || !is_prefixed_with(line, ".so ")) {
99 goto ordinary_manpage;
101 /* Example: man2/path_resolution.2.gz contains
102 * ".so man7/path_resolution.7\n<junk>"
104 *strchrnul(line, '\n') = '\0';
105 linkname = skip_whitespace(&line[4]);
107 /* If link has no slashes, we just replace man page name.
108 * If link has slashes (however many), we go back *once*.
109 * ".so zzz/ggg/page.3" does NOT go back two levels. */
110 p = strrchr(man_filename, '/');
112 goto ordinary_manpage;
114 if (strchr(linkname, '/')) {
115 p = strrchr(man_filename, '/');
117 goto ordinary_manpage;
121 /* Links do not have .gz extensions, even if manpage
123 man_filename = xasprintf("%s/%s", man_filename, linkname);
125 /* Note: we leak "new" man_filename string as well... */
126 if (show_manpage(man_filename, man, level + 1))
128 /* else: show the link, it's better than nothing */
133 open_zipped(man_filename, /*fail_if_not_compressed:*/ 0); /* guaranteed to use fd 0 (STDIN_FILENO) */
135 /* "man man" formats to screen width.
136 * "man man >file" formats to default 80 columns.
137 * "man man | cat" formats to default 80 columns.
139 int w = get_terminal_width(STDOUT_FILENO);
142 /* "2>&1" is added so that nroff errors are shown in pager too.
143 * Otherwise it may show just empty screen */
144 cmd = xasprintf("%s | %s -rLL=%un -rLT=%un 2>&1 | %s",
145 G.tbl, G.nroff, w, w,
148 cmd = xstrdup(G.pager);
155 /* man_filename is of the form "/dir/dir/dir/name.s" */
156 static int show_manpage(char *man_filename, int man, int level)
158 #if SEAMLESS_COMPRESSION
159 /* We leak this allocation... */
160 char *filename_with_zext = xasprintf("%s.lzma", man_filename);
161 char *ext = strrchr(filename_with_zext, '.') + 1;
164 #if ENABLE_FEATURE_SEAMLESS_LZMA
165 if (run_pipe(filename_with_zext, man, level))
168 #if ENABLE_FEATURE_SEAMLESS_XZ
170 if (run_pipe(filename_with_zext, man, level))
173 #if ENABLE_FEATURE_SEAMLESS_BZ2
175 if (run_pipe(filename_with_zext, man, level))
178 #if ENABLE_FEATURE_SEAMLESS_GZ
180 if (run_pipe(filename_with_zext, man, level))
184 return run_pipe(man_filename, man, level);
187 static char **add_MANPATH(char **man_path_list, int *count_mp, char *path)
189 if (path) while (*path) {
193 next_path = strchr(path, ':');
195 if (next_path == path) /* "::"? */
199 /* Do we already have path? */
200 path_element = man_path_list;
201 if (path_element) while (*path_element) {
202 if (strcmp(*path_element, path) == 0)
206 man_path_list = xrealloc_vector(man_path_list, 4, *count_mp);
207 man_path_list[*count_mp] = xstrdup(path);
209 /* man_path_list is NULL terminated */
210 /* man_path_list[*count_mp] = NULL; - xrealloc_vector did it */
214 /* "path" may be a result of getenv(), be nice and don't mangle it */
217 path = next_path + 1;
219 return man_path_list;
222 static const char *if_redefined(const char *var, const char *key, const char *line)
224 if (!is_prefixed_with(line, key))
227 if (!isspace(line[0]))
229 return xstrdup(skip_whitespace(line));
232 int man_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
233 int man_main(int argc UNUSED_PARAM, char **argv)
237 char *cur_path, *cur_sect;
238 char **man_path_list;
246 opt_complementary = "-1"; /* at least one argument */
247 opt = getopt32(argv, "+aw");
250 sec_list = xstrdup("0p:1:1p:2:3:3p:4:5:6:7:8:9");
253 man_path_list = add_MANPATH(NULL, &count_mp,
254 getenv("MANDATORY_MANPATH"+10) /* "MANPATH" */
256 if (!man_path_list) {
257 /* default, may be overridden by /etc/man.conf */
258 man_path_list = xzalloc(2 * sizeof(man_path_list[0]));
259 man_path_list[0] = (char*)"/usr/man";
261 * Thus, man.conf will overwrite man_path_list[0]
262 * if a path is defined there.
266 /* Parse man.conf[ig] or man_db.conf */
267 /* man version 1.6f uses man.config */
268 /* man-db implementation of man uses man_db.conf */
269 parser = config_open2("/etc/man.config", fopen_for_read);
271 parser = config_open2("/etc/man.conf", fopen_for_read);
273 parser = config_open2("/etc/man_db.conf", fopen_for_read);
275 while (config_read(parser, token, 2, 0, "# \t", PARSE_NORMAL)) {
278 if (strcmp("DEFINE", token[0]) == 0) {
279 G.col = if_redefined(G.tbl , "col", token[1]);
280 G.tbl = if_redefined(G.tbl , "tbl", token[1]);
281 G.nroff = if_redefined(G.nroff, "nroff", token[1]);
282 G.pager = if_redefined(G.pager, "pager", token[1]);
284 if (strcmp("MANDATORY_MANPATH"+10, token[0]) == 0 /* "MANPATH"? */
285 || strcmp("MANDATORY_MANPATH", token[0]) == 0
287 man_path_list = add_MANPATH(man_path_list, &count_mp, token[1]);
289 if (strcmp("MANSECT", token[0]) == 0) {
291 sec_list = xstrdup(token[1]);
294 config_close(parser);
297 /* environment overrides setting from man.config */
298 char *env_pager = getenv("MANPAGER");
300 env_pager = getenv("PAGER");
305 if (!isatty(STDOUT_FILENO)) {
306 putenv((char*)"GROFF_NO_SGR=1");
307 G.pager = xasprintf("%s -b -p -x", G.col);
311 do { /* for each argv[] */
315 if (strchr(*argv, '/')) {
316 found = show_manpage(*argv, /*man:*/ 1, 0);
319 while ((cur_path = man_path_list[cur_mp++]) != NULL) {
320 /* for each MANPATH */
322 do { /* for each section */
323 char *next_sect = strchrnul(cur_sect, ':');
324 int sect_len = next_sect - cur_sect;
328 /* Search for cat, then man page */
329 while (cat0man1 < 2) {
331 man_filename = xasprintf("%s/%s%.*s/%s.%.*s",
333 "cat\0man" + (cat0man1 * 4),
337 found_here = show_manpage(man_filename, cat0man1, 0);
339 cat0man1 += found_here + 1;
343 if (found && !(opt & OPT_a))
345 cur_sect = next_sect;
346 while (*cur_sect == ':')
352 bb_error_msg("no manual entry for '%s'", *argv);