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.
9 //config: Format and display manual pages.
11 //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"
15 //usage: "\n -a Display all pages"
16 //usage: "\n -w Show page locations"
18 //usage: "\n$COLUMNS overrides output width"
21 #include "common_bufsiz.h"
25 OPT_w = 2, /* print path */
28 /* This is what I see on my desktop system being executed:
33 gunzip -c '/usr/man/man1/bzip2.1.gz'
36 ) | gtbl | nroff -Tlatin1 -mandoc | less
38 Some systems use -Tascii.
40 On another system I see this:
42 ... | tbl | nroff -mandoc -rLL=<NNN>n -rLT=<NNN>n -Tutf8 | less
44 where <NNN> is screen width minus 5.
45 Replacing "DEFINE nroff nroff -mandoc" in /etc/man_db.conf
46 changes "nroff -mandoc" part; -rLL=<NNN>n, -rLT=<NNN>n and -Tutf8 parts are
47 appended to the user-specified command.
49 Redirecting to a pipe or file sets GROFF_NO_SGR=1 to prevent color escapes,
50 and uses "col -b -p -x" instead of pager, this filters out backspace
51 and underscore tricks.
60 #define G (*(struct globals*)bb_common_bufsiz1)
61 #define INIT_G() do { \
62 setup_common_bufsiz(); \
65 /* Removed -Tlatin1. Assuming system nroff has suitable default */ \
66 G.nroff = "nroff -mandoc"; \
67 G.pager = ENABLE_LESS ? "less" : "more"; \
70 static int show_manpage(char *man_filename, int man, int level);
72 static int run_pipe(char *man_filename, int man, int level)
76 /* Prevent man page link loops */
80 if (access(man_filename, R_OK) != 0)
83 if (option_mask32 & OPT_w) {
88 if (man) { /* man page, not cat page */
89 /* Is this a link to another manpage? */
90 /* The link has the following on the first line: */
91 /* ".so another_man_page" */
98 * man1/genhostid.1.gz: 203 bytes - smallest real manpage
99 * man2/path_resolution.2.gz: 114 bytes - largest link
101 xstat(man_filename, &sb);
102 if (sb.st_size > 300) /* err on the safe side */
103 goto ordinary_manpage;
105 line = xmalloc_open_zipped_read_close(man_filename, NULL);
106 if (!line || !is_prefixed_with(line, ".so ")) {
108 goto ordinary_manpage;
110 /* Example: man2/path_resolution.2.gz contains
111 * ".so man7/path_resolution.7\n<junk>"
113 *strchrnul(line, '\n') = '\0';
114 linkname = skip_whitespace(&line[4]);
116 /* 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, '/');
121 goto ordinary_manpage;
123 if (strchr(linkname, '/')) {
124 p = strrchr(man_filename, '/');
126 goto ordinary_manpage;
130 /* Links do not have .gz extensions, even if manpage
132 man_filename = xasprintf("%s/%s", man_filename, linkname);
134 /* Note: we leak "new" man_filename string as well... */
135 if (show_manpage(man_filename, man, level + 1))
137 /* else: show the link, it's better than nothing */
142 open_zipped(man_filename, /*fail_if_not_compressed:*/ 0); /* guaranteed to use fd 0 (STDIN_FILENO) */
144 int w = get_terminal_width(-1);
147 /* "2>&1" is added so that nroff errors are shown in pager too.
148 * Otherwise it may show just empty screen.
150 cmd = xasprintf("%s | %s -rLL=%un -rLT=%un 2>&1 | %s",
151 G.tbl, G.nroff, w, w,
154 cmd = xstrdup(G.pager);
161 /* man_filename is of the form "/dir/dir/dir/name.s" */
162 static int show_manpage(char *man_filename, int man, int level)
164 #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;
170 #if ENABLE_FEATURE_SEAMLESS_LZMA
171 if (run_pipe(filename_with_zext, man, level))
174 #if ENABLE_FEATURE_SEAMLESS_XZ
176 if (run_pipe(filename_with_zext, man, level))
179 #if ENABLE_FEATURE_SEAMLESS_BZ2
181 if (run_pipe(filename_with_zext, man, level))
184 #if ENABLE_FEATURE_SEAMLESS_GZ
186 if (run_pipe(filename_with_zext, man, level))
190 return run_pipe(man_filename, man, level);
193 static char **add_MANPATH(char **man_path_list, int *count_mp, char *path)
195 if (path) while (*path) {
199 next_path = strchr(path, ':');
201 if (next_path == path) /* "::"? */
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)
212 man_path_list = xrealloc_vector(man_path_list, 4, *count_mp);
213 man_path_list[*count_mp] = xstrdup(path);
215 /* man_path_list is NULL terminated */
216 /* man_path_list[*count_mp] = NULL; - xrealloc_vector did it */
220 /* "path" may be a result of getenv(), be nice and don't mangle it */
223 path = next_path + 1;
225 return man_path_list;
228 static const char *if_redefined(const char *var, const char *key, const char *line)
230 if (!is_prefixed_with(line, key))
233 if (!isspace(line[0]))
235 return xstrdup(skip_whitespace(line));
238 int man_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
239 int man_main(int argc UNUSED_PARAM, char **argv)
243 char *cur_path, *cur_sect;
244 char **man_path_list;
252 opt_complementary = "-1"; /* at least one argument */
253 opt = getopt32(argv, "+aw");
256 sec_list = xstrdup("0p:1:1p:2:3:3p:4:5:6:7:8:9");
259 man_path_list = add_MANPATH(NULL, &count_mp,
260 getenv("MANDATORY_MANPATH"+10) /* "MANPATH" */
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]));
265 man_path_list[0] = (char*)"/usr/man";
267 * Thus, man.conf will overwrite man_path_list[0]
268 * if a path is defined there.
272 /* Parse man.conf[ig] or man_db.conf */
273 /* man version 1.6f uses man.config */
274 /* man-db implementation of man uses man_db.conf */
275 parser = config_open2("/etc/man.config", fopen_for_read);
277 parser = config_open2("/etc/man.conf", fopen_for_read);
279 parser = config_open2("/etc/man_db.conf", fopen_for_read);
281 while (config_read(parser, token, 2, 0, "# \t", PARSE_NORMAL)) {
284 if (strcmp("DEFINE", token[0]) == 0) {
285 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]);
290 if (strcmp("MANDATORY_MANPATH"+10, token[0]) == 0 /* "MANPATH"? */
291 || strcmp("MANDATORY_MANPATH", token[0]) == 0
293 man_path_list = add_MANPATH(man_path_list, &count_mp, token[1]);
295 if (strcmp("MANSECT", token[0]) == 0) {
297 sec_list = xstrdup(token[1]);
300 config_close(parser);
303 /* environment overrides setting from man.config */
304 char *env_pager = getenv("MANPAGER");
306 env_pager = getenv("PAGER");
311 if (!isatty(STDOUT_FILENO)) {
312 putenv((char*)"GROFF_NO_SGR=1");
313 G.pager = xasprintf("%s -b -p -x", G.col);
317 do { /* for each argv[] */
321 if (strchr(*argv, '/')) {
322 found = show_manpage(*argv, /*man:*/ 1, 0);
325 while ((cur_path = man_path_list[cur_mp++]) != NULL) {
326 /* for each MANPATH */
328 do { /* for each section */
329 char *next_sect = strchrnul(cur_sect, ':');
330 int sect_len = next_sect - cur_sect;
334 /* Search for cat, then man page */
335 while (cat0man1 < 2) {
337 man_filename = xasprintf("%s/%s%.*s/%s.%.*s",
339 "cat\0man" + (cat0man1 * 4),
343 found_here = show_manpage(man_filename, cat0man1, 0);
345 cat0man1 += found_here + 1;
349 if (found && !(opt & OPT_a))
351 cur_sect = next_sect;
352 while (*cur_sect == ':')
358 bb_error_msg("no manual entry for '%s'", *argv);