1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24#include "libbb.h"
25#include "common_bufsiz.h"
26
27enum {
28 OPT_a = 1,
29 OPT_w = 2,
30};
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58struct globals {
59 const char *col;
60 const char *tbl;
61 const char *nroff;
62 const char *pager;
63} FIX_ALIASING;
64#define G (*(struct globals*)bb_common_bufsiz1)
65#define INIT_G() do { \
66 setup_common_bufsiz(); \
67 G.col = "col"; \
68 G.tbl = "tbl"; \
69 \
70 G.nroff = "nroff -mandoc"; \
71 G.pager = ENABLE_LESS ? "less" : "more"; \
72} while (0)
73
74static int show_manpage(char *man_filename, int man, int level);
75
76static int run_pipe(char *man_filename, int man, int level)
77{
78 char *cmd;
79
80
81 if (level > 10)
82 return 0;
83
84 if (access(man_filename, R_OK) != 0)
85 return 0;
86
87 if (option_mask32 & OPT_w) {
88 puts(man_filename);
89 return 1;
90 }
91
92 if (man) {
93
94
95
96
97 struct stat sb;
98 char *line;
99 char *linkname, *p;
100
101
102
103
104
105 xstat(man_filename, &sb);
106 if (sb.st_size > 300)
107 goto ordinary_manpage;
108
109 line = xmalloc_open_zipped_read_close(man_filename, NULL);
110 if (!line || !is_prefixed_with(line, ".so ")) {
111 free(line);
112 goto ordinary_manpage;
113 }
114
115
116
117 *strchrnul(line, '\n') = '\0';
118 linkname = skip_whitespace(&line[4]);
119
120
121
122
123 p = strrchr(man_filename, '/');
124 if (!p)
125 goto ordinary_manpage;
126 *p = '\0';
127 if (strchr(linkname, '/')) {
128 p = strrchr(man_filename, '/');
129 if (!p)
130 goto ordinary_manpage;
131 *p = '\0';
132 }
133
134
135
136 man_filename = xasprintf("%s/%s", man_filename, linkname);
137 free(line);
138
139 if (show_manpage(man_filename, man, level + 1))
140 return 1;
141
142 }
143
144 ordinary_manpage:
145 close(STDIN_FILENO);
146 open_zipped(man_filename, 0);
147 if (man) {
148 int w = get_terminal_width(-1);
149 if (w > 10)
150 w -= 2;
151
152
153
154 cmd = xasprintf("%s | %s -rLL=%un -rLT=%un 2>&1 | %s",
155 G.tbl, G.nroff, w, w,
156 G.pager);
157 } else {
158 cmd = xstrdup(G.pager);
159 }
160 system(cmd);
161 free(cmd);
162 return 1;
163}
164
165
166static int show_manpage(char *man_filename, int man, int level)
167{
168#if SEAMLESS_COMPRESSION
169
170 char *filename_with_zext = xasprintf("%s.lzma", man_filename);
171 char *ext = strrchr(filename_with_zext, '.') + 1;
172#endif
173
174#if ENABLE_FEATURE_SEAMLESS_LZMA
175 if (run_pipe(filename_with_zext, man, level))
176 return 1;
177#endif
178#if ENABLE_FEATURE_SEAMLESS_XZ
179 strcpy(ext, "xz");
180 if (run_pipe(filename_with_zext, man, level))
181 return 1;
182#endif
183#if ENABLE_FEATURE_SEAMLESS_BZ2
184 strcpy(ext, "bz2");
185 if (run_pipe(filename_with_zext, man, level))
186 return 1;
187#endif
188#if ENABLE_FEATURE_SEAMLESS_GZ
189 strcpy(ext, "gz");
190 if (run_pipe(filename_with_zext, man, level))
191 return 1;
192#endif
193
194 return run_pipe(man_filename, man, level);
195}
196
197static char **add_MANPATH(char **man_path_list, int *count_mp, char *path)
198{
199 if (path) while (*path) {
200 char *next_path;
201 char **path_element;
202
203 next_path = strchr(path, ':');
204 if (next_path) {
205 if (next_path == path)
206 goto next;
207 *next_path = '\0';
208 }
209
210 path_element = man_path_list;
211 if (path_element) while (*path_element) {
212 if (strcmp(*path_element, path) == 0) {
213 goto skip;
214 }
215 path_element++;
216 }
217 man_path_list = xrealloc_vector(man_path_list, 4, *count_mp);
218 man_path_list[*count_mp] = xstrdup(path);
219 (*count_mp)++;
220
221
222 skip:
223 if (!next_path)
224 break;
225
226 *next_path = ':';
227 next:
228 path = next_path + 1;
229 }
230 return man_path_list;
231}
232
233static const char *if_redefined(const char *var, const char *key, const char *line)
234{
235 if (!is_prefixed_with(line, key))
236 return var;
237 line += strlen(key);
238 if (!isspace(line[0]))
239 return var;
240 return xstrdup(skip_whitespace(line));
241}
242
243int man_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
244int man_main(int argc UNUSED_PARAM, char **argv)
245{
246 parser_t *parser;
247 char *sec_list;
248 char **man_path_list;
249 int count_mp;
250 int opt, not_found;
251 char *token[2];
252
253 INIT_G();
254
255 opt = getopt32(argv, "^+" "aw" "\0" "-1");
256 argv += optind;
257
258 sec_list = xstrdup("0p:1:1p:2:3:3p:4:5:6:7:8:9");
259
260 count_mp = 0;
261 man_path_list = add_MANPATH(NULL, &count_mp,
262 getenv("MANDATORY_MANPATH"+10)
263 );
264
265
266
267 parser = config_open2("/etc/man.config", fopen_for_read);
268 if (!parser)
269 parser = config_open2("/etc/man.conf", fopen_for_read);
270 if (!parser)
271 parser = config_open2("/etc/man_db.conf", fopen_for_read);
272
273 while (config_read(parser, token, 2, 0, "# \t", PARSE_NORMAL)) {
274 if (!token[1])
275 continue;
276 if (strcmp("DEFINE", token[0]) == 0) {
277 G.col = if_redefined(G.col , "col", token[1]);
278 G.tbl = if_redefined(G.tbl , "tbl", token[1]);
279 G.nroff = if_redefined(G.nroff, "nroff", token[1]);
280 G.pager = if_redefined(G.pager, "pager", token[1]);
281 } else
282 if (strcmp("MANDATORY_MANPATH"+10, token[0]) == 0
283 || strcmp("MANDATORY_MANPATH", token[0]) == 0
284 ) {
285 man_path_list = add_MANPATH(man_path_list, &count_mp, token[1]);
286 }
287 if (strcmp("MANSECT", token[0]) == 0) {
288 free(sec_list);
289 sec_list = xstrdup(token[1]);
290 }
291 }
292 config_close(parser);
293
294 if (!man_path_list) {
295 static const char *const mpl[] = { "/usr/man", "/usr/share/man", NULL };
296 man_path_list = (char**)mpl;
297
298 }
299
300 {
301
302 char *env_pager = getenv("MANPAGER");
303 if (!env_pager)
304 env_pager = getenv("PAGER");
305 if (env_pager)
306 G.pager = env_pager;
307 }
308
309 if (!isatty(STDOUT_FILENO)) {
310 putenv((char*)"GROFF_NO_SGR=1");
311 G.pager = xasprintf("%s -b -p -x", G.col);
312 }
313
314 not_found = 0;
315 do {
316 const char *cur_path;
317 int cur_mp;
318 int found = 0;
319
320 if (strchr(*argv, '/')) {
321 found = show_manpage(*argv, 1, 0);
322 goto check_found;
323 }
324 cur_mp = 0;
325 while ((cur_path = man_path_list[cur_mp++]) != NULL) {
326
327 const char *cur_sect = sec_list;
328 do {
329 char *next_sect = strchrnul(cur_sect, ':');
330 int sect_len = next_sect - cur_sect;
331 char *man_filename;
332 int cat0man1 = 0;
333
334
335 while (cat0man1 < 2) {
336 int found_here;
337 man_filename = xasprintf("%s/%s%.*s/%s.%.*s",
338 cur_path,
339 "cat\0man" + (cat0man1 * 4),
340 sect_len, cur_sect,
341 *argv,
342 sect_len, cur_sect);
343 found_here = show_manpage(man_filename, cat0man1, 0);
344 found |= found_here;
345 cat0man1 += found_here + 1;
346 free(man_filename);
347 }
348
349 if (found && !(opt & OPT_a))
350 goto next_arg;
351 cur_sect = next_sect;
352 while (*cur_sect == ':')
353 cur_sect++;
354 } while (*cur_sect);
355 }
356 check_found:
357 if (!found) {
358 bb_error_msg("no manual entry for '%s'", *argv);
359 not_found = 1;
360 }
361 next_arg:
362 argv++;
363 } while (*argv);
364
365 return not_found;
366}
367