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"
17 OPT_w = 2, /* print path */
20 /* 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
33 static int show_manpage(const char *pager, char *man_filename, int man, int level);
35 static int run_pipe(const char *pager, char *man_filename, int man, int level)
39 /* Prevent man page link loops */
43 if (access(man_filename, R_OK) != 0)
46 if (option_mask32 & OPT_w) {
51 if (man) { /* man page, not cat page */
52 /* Is this a link to another manpage? */
53 /* The link has the following on the first line: */
54 /* ".so another_man_page" */
61 * man1/genhostid.1.gz: 203 bytes - smallest real manpage
62 * man2/path_resolution.2.gz: 114 bytes - largest link
64 xstat(man_filename, &sb);
65 if (sb.st_size > 300) /* err on the safe side */
66 goto ordinary_manpage;
68 line = xmalloc_open_zipped_read_close(man_filename, NULL);
69 if (!line || strncmp(line, ".so ", 4) != 0) {
71 goto ordinary_manpage;
73 /* Example: man2/path_resolution.2.gz contains
74 * ".so man7/path_resolution.7\n<junk>"
76 *strchrnul(line, '\n') = '\0';
77 linkname = skip_whitespace(&line[4]);
79 /* If link has no slashes, we just replace man page name.
80 * If link has slashes (however many), we go back *once*.
81 * ".so zzz/ggg/page.3" does NOT go back two levels. */
82 p = strrchr(man_filename, '/');
84 goto ordinary_manpage;
86 if (strchr(linkname, '/')) {
87 p = strrchr(man_filename, '/');
89 goto ordinary_manpage;
93 /* Links do not have .gz extensions, even if manpage
95 man_filename = xasprintf("%s/%s", man_filename, linkname);
97 /* Note: we leak "new" man_filename string as well... */
98 if (show_manpage(pager, man_filename, man, level + 1))
100 /* else: show the link, it's better than nothing */
105 open_zipped(man_filename); /* guaranteed to use fd 0 (STDIN_FILENO) */
106 /* "2>&1" is added so that nroff errors are shown in pager too.
107 * Otherwise it may show just empty screen */
109 man ? "gtbl | nroff -Tlatin1 -mandoc 2>&1 | %s"
117 /* man_filename is of the form "/dir/dir/dir/name.s" */
118 static int show_manpage(const char *pager, char *man_filename, int man, int level)
120 #if SEAMLESS_COMPRESSION
121 /* We leak this allocation... */
122 char *filename_with_zext = xasprintf("%s.lzma", man_filename);
123 char *ext = strrchr(filename_with_zext, '.') + 1;
126 #if ENABLE_FEATURE_SEAMLESS_LZMA
127 if (run_pipe(pager, filename_with_zext, man, level))
130 #if ENABLE_FEATURE_SEAMLESS_XZ
132 if (run_pipe(pager, filename_with_zext, man, level))
135 #if ENABLE_FEATURE_SEAMLESS_BZ2
137 if (run_pipe(pager, filename_with_zext, man, level))
140 #if ENABLE_FEATURE_SEAMLESS_GZ
142 if (run_pipe(pager, filename_with_zext, man, level))
146 return run_pipe(pager, man_filename, man, level);
149 int man_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
150 int man_main(int argc UNUSED_PARAM, char **argv)
154 char **man_path_list;
156 char *cur_path, *cur_sect;
157 int count_mp, cur_mp;
161 opt_complementary = "-1"; /* at least one argument */
162 opt = getopt32(argv, "+aw");
165 sec_list = xstrdup("1:2:3:4:5:6:7:8:9");
166 /* Last valid man_path_list[] is [0x10] */
168 man_path_list = xzalloc(0x11 * sizeof(man_path_list[0]));
169 man_path_list[0] = getenv("MANPATH");
170 if (!man_path_list[0]) /* default, may be overridden by /etc/man.conf */
171 man_path_list[0] = (char*)"/usr/man";
174 pager = getenv("MANPAGER");
176 pager = getenv("PAGER");
181 /* Parse man.conf[ig] or man_db.conf */
182 /* man version 1.6f uses man.config */
183 /* man-db implementation of man uses man_db.conf */
184 parser = config_open2("/etc/man.config", fopen_for_read);
186 parser = config_open2("/etc/man.conf", fopen_for_read);
188 parser = config_open2("/etc/man_db.conf", fopen_for_read);
190 while (config_read(parser, token, 2, 0, "# \t", PARSE_NORMAL)) {
193 if (strcmp("MANDATORY_MANPATH"+10, token[0]) == 0 /* "MANPATH"? */
194 || strcmp("MANDATORY_MANPATH", token[0]) == 0
196 char *path = token[1];
201 next_path = strchr(path, ':');
204 if (next_path++ == path) /* "::"? */
207 /* Do we already have path? */
208 path_element = man_path_list;
209 while (*path_element) {
210 if (strcmp(*path_element, path) == 0)
214 man_path_list = xrealloc_vector(man_path_list, 4, count_mp);
215 man_path_list[count_mp] = xstrdup(path);
217 /* man_path_list is NULL terminated */
218 /*man_path_list[count_mp] = NULL; - xrealloc_vector did it */
226 if (strcmp("MANSECT", token[0]) == 0) {
228 sec_list = xstrdup(token[1]);
231 config_close(parser);
234 do { /* for each argv[] */
238 if (strchr(*argv, '/')) {
239 found = show_manpage(pager, *argv, /*man:*/ 1, 0);
242 while ((cur_path = man_path_list[cur_mp++]) != NULL) {
243 /* for each MANPATH */
245 do { /* for each section */
246 char *next_sect = strchrnul(cur_sect, ':');
247 int sect_len = next_sect - cur_sect;
251 /* Search for cat, then man page */
252 while (cat0man1 < 2) {
254 man_filename = xasprintf("%s/%s%.*s/%s.%.*s",
256 "cat\0man" + (cat0man1 * 4),
260 found_here = show_manpage(pager, man_filename, cat0man1, 0);
262 cat0man1 += found_here + 1;
266 if (found && !(opt & OPT_a))
268 cur_sect = next_sect;
269 while (*cur_sect == ':')
275 bb_error_msg("no manual entry for '%s'", *argv);