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 tarball for details.
10 OPT_w
= 2, /* print path */
13 /* This is what I see on my desktop system being executed:
19 gunzip -c '/usr/man/man1/bzip2.1.gz'
22 ) | gtbl | nroff -Tlatin1 -mandoc | less
26 #if ENABLE_FEATURE_SEAMLESS_LZMA
27 #define Z_SUFFIX ".lzma"
28 #elif ENABLE_FEATURE_SEAMLESS_BZ2
29 #define Z_SUFFIX ".bz2"
30 #elif ENABLE_FEATURE_SEAMLESS_GZ
31 #define Z_SUFFIX ".gz"
36 static int show_manpage(const char *pager
, char *man_filename
, int man
, int level
);
38 static int run_pipe(const char *pager
, char *man_filename
, int man
, int level
)
42 /* Prevent man page link loops */
46 if (access(man_filename
, R_OK
) != 0)
49 if (option_mask32
& OPT_w
) {
54 if (man
) { /* man page, not cat page */
55 /* Is this a link to another manpage? */
56 /* The link has the following on the first line: */
57 /* ".so another_man_page" */
64 * man1/genhostid.1.gz: 203 bytes - smallest real manpage
65 * man2/path_resolution.2.gz: 114 bytes - largest link
67 xstat(man_filename
, &sb
);
68 if (sb
.st_size
> 300) /* err on the safe side */
69 goto ordinary_manpage
;
71 line
= xmalloc_open_zipped_read_close(man_filename
, NULL
);
72 if (!line
|| strncmp(line
, ".so ", 4) != 0) {
74 goto ordinary_manpage
;
76 /* Example: man2/path_resolution.2.gz contains
77 * ".so man7/path_resolution.7\n<junk>"
79 *strchrnul(line
, '\n') = '\0';
80 linkname
= skip_whitespace(&line
[4]);
82 /* If link has no slashes, we just replace man page name.
83 * If link has slashes (however many), we go back *once*.
84 * ".so zzz/ggg/page.3" does NOT go back two levels. */
85 p
= strrchr(man_filename
, '/');
87 goto ordinary_manpage
;
89 if (strchr(linkname
, '/')) {
90 p
= strrchr(man_filename
, '/');
92 goto ordinary_manpage
;
96 /* Links do not have .gz extensions, even if manpage
98 man_filename
= xasprintf("%s/%s" Z_SUFFIX
, man_filename
, linkname
);
100 /* Note: we leak "new" man_filename string as well... */
101 if (show_manpage(pager
, man_filename
, man
, level
+ 1))
103 /* else: show the link, it's better than nothing */
108 open_zipped(man_filename
); /* guaranteed to use fd 0 (STDIN_FILENO) */
109 /* "2>&1" is added so that nroff errors are shown in pager too.
110 * Otherwise it may show just empty screen */
112 man
? "gtbl | nroff -Tlatin1 -mandoc 2>&1 | %s"
120 /* man_filename is of the form "/dir/dir/dir/name.s" Z_SUFFIX */
121 static int show_manpage(const char *pager
, char *man_filename
, int man
, int level
)
123 #if ENABLE_FEATURE_SEAMLESS_LZMA
124 if (run_pipe(pager
, man_filename
, man
, level
))
128 #if ENABLE_FEATURE_SEAMLESS_BZ2
129 #if ENABLE_FEATURE_SEAMLESS_LZMA
130 strcpy(strrchr(man_filename
, '.') + 1, "bz2");
132 if (run_pipe(pager
, man_filename
, man
, level
))
136 #if ENABLE_FEATURE_SEAMLESS_GZ
137 #if ENABLE_FEATURE_SEAMLESS_LZMA || ENABLE_FEATURE_SEAMLESS_BZ2
138 strcpy(strrchr(man_filename
, '.') + 1, "gz");
140 if (run_pipe(pager
, man_filename
, man
, level
))
144 #if ENABLE_FEATURE_SEAMLESS_LZMA || ENABLE_FEATURE_SEAMLESS_BZ2 || ENABLE_FEATURE_SEAMLESS_GZ
145 *strrchr(man_filename
, '.') = '\0';
147 if (run_pipe(pager
, man_filename
, man
, level
))
153 int man_main(int argc
, char **argv
) MAIN_EXTERNALLY_VISIBLE
;
154 int man_main(int argc UNUSED_PARAM
, char **argv
)
158 char **man_path_list
;
160 char *cur_path
, *cur_sect
;
161 int count_mp
, cur_mp
;
165 opt_complementary
= "-1"; /* at least one argument */
166 opt
= getopt32(argv
, "+aw");
169 sec_list
= xstrdup("1:2:3:4:5:6:7:8:9");
170 /* Last valid man_path_list[] is [0x10] */
172 man_path_list
= xzalloc(0x11 * sizeof(man_path_list
[0]));
173 man_path_list
[0] = getenv("MANPATH");
174 if (!man_path_list
[0]) /* default, may be overridden by /etc/man.conf */
175 man_path_list
[0] = (char*)"/usr/man";
178 pager
= getenv("MANPAGER");
180 pager
= getenv("PAGER");
186 parser
= config_open2("/etc/man.conf", fopen_for_read
);
187 while (config_read(parser
, token
, 2, 0, "# \t", PARSE_NORMAL
)) {
190 if (strcmp("MANPATH", token
[0]) == 0) {
191 char *path
= token
[1];
196 next_path
= strchr(path
, ':');
199 if (next_path
++ == path
) /* "::"? */
202 /* Do we already have path? */
203 path_element
= man_path_list
;
204 while (*path_element
) {
205 if (strcmp(*path_element
, path
) == 0)
209 man_path_list
= xrealloc_vector(man_path_list
, 4, count_mp
);
210 man_path_list
[count_mp
] = xstrdup(path
);
212 /* man_path_list is NULL terminated */
213 /*man_path_list[count_mp] = NULL; - xrealloc_vector did it */
221 if (strcmp("MANSECT", token
[0]) == 0) {
223 sec_list
= xstrdup(token
[1]);
226 config_close(parser
);
229 do { /* for each argv[] */
233 if (strchr(*argv
, '/')) {
234 found
= show_manpage(pager
, *argv
, /*man:*/ 1, 0);
237 while ((cur_path
= man_path_list
[cur_mp
++]) != NULL
) {
238 /* for each MANPATH */
240 do { /* for each section */
241 char *next_sect
= strchrnul(cur_sect
, ':');
242 int sect_len
= next_sect
- cur_sect
;
246 /* Search for cat, then man page */
247 while (cat0man1
< 2) {
249 man_filename
= xasprintf("%s/%s%.*s/%s.%.*s" Z_SUFFIX
,
251 "cat\0man" + (cat0man1
* 4),
255 found_here
= show_manpage(pager
, man_filename
, cat0man1
, 0);
257 cat0man1
+= found_here
+ 1;
261 if (found
&& !(opt
& OPT_a
))
263 cur_sect
= next_sect
;
264 while (*cur_sect
== ':')
270 bb_error_msg("no manual entry for '%s'", *argv
);