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"
11 //usage: "\n -a Display all pages"
12 //usage: "\n -w Show page locations"
18 OPT_w = 2, /* print path */
21 /* This is what I see on my desktop system being executed:
27 gunzip -c '/usr/man/man1/bzip2.1.gz'
30 ) | gtbl | nroff -Tlatin1 -mandoc | less
34 #if ENABLE_FEATURE_SEAMLESS_LZMA
35 #define Z_SUFFIX ".lzma"
36 #elif ENABLE_FEATURE_SEAMLESS_BZ2
37 #define Z_SUFFIX ".bz2"
38 #elif ENABLE_FEATURE_SEAMLESS_GZ
39 #define Z_SUFFIX ".gz"
44 static int show_manpage(const char *pager, char *man_filename, int man, int level);
46 static int run_pipe(const char *pager, char *man_filename, int man, int level)
50 /* Prevent man page link loops */
54 if (access(man_filename, R_OK) != 0)
57 if (option_mask32 & OPT_w) {
62 if (man) { /* man page, not cat page */
63 /* Is this a link to another manpage? */
64 /* The link has the following on the first line: */
65 /* ".so another_man_page" */
72 * man1/genhostid.1.gz: 203 bytes - smallest real manpage
73 * man2/path_resolution.2.gz: 114 bytes - largest link
75 xstat(man_filename, &sb);
76 if (sb.st_size > 300) /* err on the safe side */
77 goto ordinary_manpage;
79 line = xmalloc_open_zipped_read_close(man_filename, NULL);
80 if (!line || strncmp(line, ".so ", 4) != 0) {
82 goto ordinary_manpage;
84 /* Example: man2/path_resolution.2.gz contains
85 * ".so man7/path_resolution.7\n<junk>"
87 *strchrnul(line, '\n') = '\0';
88 linkname = skip_whitespace(&line[4]);
90 /* If link has no slashes, we just replace man page name.
91 * If link has slashes (however many), we go back *once*.
92 * ".so zzz/ggg/page.3" does NOT go back two levels. */
93 p = strrchr(man_filename, '/');
95 goto ordinary_manpage;
97 if (strchr(linkname, '/')) {
98 p = strrchr(man_filename, '/');
100 goto ordinary_manpage;
104 /* Links do not have .gz extensions, even if manpage
106 man_filename = xasprintf("%s/%s" Z_SUFFIX, man_filename, linkname);
108 /* Note: we leak "new" man_filename string as well... */
109 if (show_manpage(pager, man_filename, man, level + 1))
111 /* else: show the link, it's better than nothing */
116 open_zipped(man_filename); /* guaranteed to use fd 0 (STDIN_FILENO) */
117 /* "2>&1" is added so that nroff errors are shown in pager too.
118 * Otherwise it may show just empty screen */
120 man ? "gtbl | nroff -Tlatin1 -mandoc 2>&1 | %s"
128 /* man_filename is of the form "/dir/dir/dir/name.s" Z_SUFFIX */
129 static int show_manpage(const char *pager, char *man_filename, int man, int level)
131 #if ENABLE_FEATURE_SEAMLESS_LZMA
132 if (run_pipe(pager, man_filename, man, level))
136 #if ENABLE_FEATURE_SEAMLESS_BZ2
137 #if ENABLE_FEATURE_SEAMLESS_LZMA
138 strcpy(strrchr(man_filename, '.') + 1, "bz2");
140 if (run_pipe(pager, man_filename, man, level))
144 #if ENABLE_FEATURE_SEAMLESS_GZ
145 #if ENABLE_FEATURE_SEAMLESS_LZMA || ENABLE_FEATURE_SEAMLESS_BZ2
146 strcpy(strrchr(man_filename, '.') + 1, "gz");
148 if (run_pipe(pager, man_filename, man, level))
152 #if ENABLE_FEATURE_SEAMLESS_LZMA || ENABLE_FEATURE_SEAMLESS_BZ2 || ENABLE_FEATURE_SEAMLESS_GZ
153 *strrchr(man_filename, '.') = '\0';
155 if (run_pipe(pager, man_filename, man, level))
161 int man_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
162 int man_main(int argc UNUSED_PARAM, char **argv)
166 char **man_path_list;
168 char *cur_path, *cur_sect;
169 int count_mp, cur_mp;
173 opt_complementary = "-1"; /* at least one argument */
174 opt = getopt32(argv, "+aw");
177 sec_list = xstrdup("1:2:3:4:5:6:7:8:9");
178 /* Last valid man_path_list[] is [0x10] */
180 man_path_list = xzalloc(0x11 * sizeof(man_path_list[0]));
181 man_path_list[0] = getenv("MANPATH");
182 if (!man_path_list[0]) /* default, may be overridden by /etc/man.conf */
183 man_path_list[0] = (char*)"/usr/man";
186 pager = getenv("MANPAGER");
188 pager = getenv("PAGER");
193 /* Parse man.conf[ig] or man_db.conf */
194 /* man version 1.6f uses man.config */
195 /* man-db implementation of man uses man_db.conf */
196 parser = config_open2("/etc/man.config", fopen_for_read);
198 parser = config_open2("/etc/man.conf", fopen_for_read);
200 parser = config_open2("/etc/man_db.conf", fopen_for_read);
202 while (config_read(parser, token, 2, 0, "# \t", PARSE_NORMAL)) {
205 if (strcmp("MANDATORY_MANPATH"+10, token[0]) == 0 /* "MANPATH"? */
206 || strcmp("MANDATORY_MANPATH", token[0]) == 0
208 char *path = token[1];
213 next_path = strchr(path, ':');
216 if (next_path++ == path) /* "::"? */
219 /* Do we already have path? */
220 path_element = man_path_list;
221 while (*path_element) {
222 if (strcmp(*path_element, path) == 0)
226 man_path_list = xrealloc_vector(man_path_list, 4, count_mp);
227 man_path_list[count_mp] = xstrdup(path);
229 /* man_path_list is NULL terminated */
230 /*man_path_list[count_mp] = NULL; - xrealloc_vector did it */
238 if (strcmp("MANSECT", token[0]) == 0) {
240 sec_list = xstrdup(token[1]);
243 config_close(parser);
246 do { /* for each argv[] */
250 if (strchr(*argv, '/')) {
251 found = show_manpage(pager, *argv, /*man:*/ 1, 0);
254 while ((cur_path = man_path_list[cur_mp++]) != NULL) {
255 /* for each MANPATH */
257 do { /* for each section */
258 char *next_sect = strchrnul(cur_sect, ':');
259 int sect_len = next_sect - cur_sect;
263 /* Search for cat, then man page */
264 while (cat0man1 < 2) {
266 man_filename = xasprintf("%s/%s%.*s/%s.%.*s" Z_SUFFIX,
268 "cat\0man" + (cat0man1 * 4),
272 found_here = show_manpage(pager, man_filename, cat0man1, 0);
274 cat0man1 += found_here + 1;
278 if (found && !(opt & OPT_a))
280 cur_sect = next_sect;
281 while (*cur_sect == ':')
287 bb_error_msg("no manual entry for '%s'", *argv);