man: accept a list of dirs in $MANPATH
[oweals/busybox.git] / miscutils / man.c
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.
4  */
5
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"
12
13 #include "libbb.h"
14
15 enum {
16         OPT_a = 1, /* all */
17         OPT_w = 2, /* print path */
18 };
19
20 /* This is what I see on my desktop system being executed:
21
22 (
23 echo ".ll 12.4i"
24 echo ".nr LL 12.4i"
25 echo ".pl 1100i"
26 gunzip -c '/usr/man/man1/bzip2.1.gz'
27 echo ".\\\""
28 echo ".pl \n(nlu+10"
29 ) | gtbl | nroff -Tlatin1 -mandoc | less
30
31 */
32
33 static int show_manpage(const char *pager, char *man_filename, int man, int level);
34
35 static int run_pipe(const char *pager, char *man_filename, int man, int level)
36 {
37         char *cmd;
38
39         /* Prevent man page link loops */
40         if (level > 10)
41                 return 0;
42
43         if (access(man_filename, R_OK) != 0)
44                 return 0;
45
46         if (option_mask32 & OPT_w) {
47                 puts(man_filename);
48                 return 1;
49         }
50
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" */
55
56                 struct stat sb;
57                 char *line;
58                 char *linkname, *p;
59
60                 /* On my system:
61                  * man1/genhostid.1.gz: 203 bytes - smallest real manpage
62                  * man2/path_resolution.2.gz: 114 bytes - largest link
63                  */
64                 xstat(man_filename, &sb);
65                 if (sb.st_size > 300) /* err on the safe side */
66                         goto ordinary_manpage;
67
68                 line = xmalloc_open_zipped_read_close(man_filename, NULL);
69                 if (!line || strncmp(line, ".so ", 4) != 0) {
70                         free(line);
71                         goto ordinary_manpage;
72                 }
73                 /* Example: man2/path_resolution.2.gz contains
74                  * ".so man7/path_resolution.7\n<junk>"
75                  */
76                 *strchrnul(line, '\n') = '\0';
77                 linkname = skip_whitespace(&line[4]);
78
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, '/');
83                 if (!p)
84                         goto ordinary_manpage;
85                 *p = '\0';
86                 if (strchr(linkname, '/')) {
87                         p = strrchr(man_filename, '/');
88                         if (!p)
89                                 goto ordinary_manpage;
90                         *p = '\0';
91                 }
92
93                 /* Links do not have .gz extensions, even if manpage
94                  * is compressed */
95                 man_filename = xasprintf("%s/%s", man_filename, linkname);
96                 free(line);
97                 /* Note: we leak "new" man_filename string as well... */
98                 if (show_manpage(pager, man_filename, man, level + 1))
99                         return 1;
100                 /* else: show the link, it's better than nothing */
101         }
102
103  ordinary_manpage:
104         close(STDIN_FILENO);
105         open_zipped(man_filename, /*fail_if_not_compressed:*/ 0); /* 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 */
108         cmd = xasprintf(
109                 /* replaced -Tlatin1 with -Tascii for non-UTF8 displays */
110                 man ? "gtbl | nroff -Tascii -mandoc 2>&1 | %s"
111                     : "%s",
112                 pager);
113         system(cmd);
114         free(cmd);
115         return 1;
116 }
117
118 /* man_filename is of the form "/dir/dir/dir/name.s" */
119 static int show_manpage(const char *pager, char *man_filename, int man, int level)
120 {
121 #if SEAMLESS_COMPRESSION
122         /* We leak this allocation... */
123         char *filename_with_zext = xasprintf("%s.lzma", man_filename);
124         char *ext = strrchr(filename_with_zext, '.') + 1;
125 #endif
126
127 #if ENABLE_FEATURE_SEAMLESS_LZMA
128         if (run_pipe(pager, filename_with_zext, man, level))
129                 return 1;
130 #endif
131 #if ENABLE_FEATURE_SEAMLESS_XZ
132         strcpy(ext, "xz");
133         if (run_pipe(pager, filename_with_zext, man, level))
134                 return 1;
135 #endif
136 #if ENABLE_FEATURE_SEAMLESS_BZ2
137         strcpy(ext, "bz2");
138         if (run_pipe(pager, filename_with_zext, man, level))
139                 return 1;
140 #endif
141 #if ENABLE_FEATURE_SEAMLESS_GZ
142         strcpy(ext, "gz");
143         if (run_pipe(pager, filename_with_zext, man, level))
144                 return 1;
145 #endif
146
147         return run_pipe(pager, man_filename, man, level);
148 }
149
150 static char **add_MANPATH(char **man_path_list, int *count_mp, char *path)
151 {
152         if (path) while (*path) {
153                 char *next_path;
154                 char **path_element;
155
156                 next_path = strchr(path, ':');
157                 if (next_path) {
158                         *next_path = '\0';
159                         if (next_path++ == path) /* "::"? */
160                                 goto next;
161                 }
162                 /* Do we already have path? */
163                 path_element = man_path_list;
164                 if (path_element) while (*path_element) {
165                         if (strcmp(*path_element, path) == 0)
166                                 goto skip;
167                         path_element++;
168                 }
169                 man_path_list = xrealloc_vector(man_path_list, 4, *count_mp);
170                 man_path_list[*count_mp] = xstrdup(path);
171                 (*count_mp)++;
172                 /* man_path_list is NULL terminated */
173                 /* man_path_list[*count_mp] = NULL; - xrealloc_vector did it */
174  skip:
175                 if (!next_path)
176                         break;
177  next:
178                 path = next_path;
179         }
180         return man_path_list;
181 }
182
183 int man_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
184 int man_main(int argc UNUSED_PARAM, char **argv)
185 {
186         parser_t *parser;
187         const char *pager = ENABLE_LESS ? "less" : "more";
188         char *sec_list;
189         char *cur_path, *cur_sect;
190         char **man_path_list;
191         int count_mp;
192         int cur_mp;
193         int opt, not_found;
194         char *token[2];
195
196         opt_complementary = "-1"; /* at least one argument */
197         opt = getopt32(argv, "+aw");
198         argv += optind;
199
200         sec_list = xstrdup("0p:1:1p:2:3:3p:4:5:6:7:8:9");
201
202         count_mp = 0;
203         man_path_list = add_MANPATH(NULL, &count_mp,
204                         getenv("MANDATORY_MANPATH"+10) /* "MANPATH" */
205         );
206         if (!man_path_list) {
207                 /* default, may be overridden by /etc/man.conf */
208                 man_path_list = xzalloc(2 * sizeof(man_path_list[0]));
209                 man_path_list[0] = (char*)"/usr/man";
210                 /* count_mp stays 0.
211                  * Thus, man.conf will overwrite man_path_list[0]
212                  * if a path is defined there.
213                  */
214         }
215
216         /* Parse man.conf[ig] or man_db.conf */
217         /* man version 1.6f uses man.config */
218         /* man-db implementation of man uses man_db.conf */
219         parser = config_open2("/etc/man.config", fopen_for_read);
220         if (!parser)
221                 parser = config_open2("/etc/man.conf", fopen_for_read);
222         if (!parser)
223                 parser = config_open2("/etc/man_db.conf", fopen_for_read);
224
225         while (config_read(parser, token, 2, 0, "# \t", PARSE_NORMAL)) {
226                 if (!token[1])
227                         continue;
228                 if (strcmp("DEFINE", token[0]) == 0) {
229                         if (strncmp("pager", token[1], 5) == 0) {
230                                 pager = xstrdup(skip_whitespace(token[1]) + 5);
231                         }
232                 } else
233                 if (strcmp("MANDATORY_MANPATH"+10, token[0]) == 0 /* "MANPATH"? */
234                  || strcmp("MANDATORY_MANPATH", token[0]) == 0
235                 ) {
236                         man_path_list = add_MANPATH(man_path_list, &count_mp, token[1]);
237                 }
238                 if (strcmp("MANSECT", token[0]) == 0) {
239                         free(sec_list);
240                         sec_list = xstrdup(token[1]);
241                 }
242         }
243         config_close(parser);
244
245         {
246                 /* environment overrides setting from man.config */
247                 char *env_pager = getenv("MANPAGER");
248                 if (!env_pager)
249                         env_pager = getenv("PAGER");
250                 if (env_pager)
251                         pager = env_pager;
252         }
253
254         not_found = 0;
255         do { /* for each argv[] */
256                 int found = 0;
257                 cur_mp = 0;
258
259                 if (strchr(*argv, '/')) {
260                         found = show_manpage(pager, *argv, /*man:*/ 1, 0);
261                         goto check_found;
262                 }
263                 while ((cur_path = man_path_list[cur_mp++]) != NULL) {
264                         /* for each MANPATH */
265                         cur_sect = sec_list;
266                         do { /* for each section */
267                                 char *next_sect = strchrnul(cur_sect, ':');
268                                 int sect_len = next_sect - cur_sect;
269                                 char *man_filename;
270                                 int cat0man1 = 0;
271
272                                 /* Search for cat, then man page */
273                                 while (cat0man1 < 2) {
274                                         int found_here;
275                                         man_filename = xasprintf("%s/%s%.*s/%s.%.*s",
276                                                         cur_path,
277                                                         "cat\0man" + (cat0man1 * 4),
278                                                         sect_len, cur_sect,
279                                                         *argv,
280                                                         sect_len, cur_sect);
281                                         found_here = show_manpage(pager, man_filename, cat0man1, 0);
282                                         found |= found_here;
283                                         cat0man1 += found_here + 1;
284                                         free(man_filename);
285                                 }
286
287                                 if (found && !(opt & OPT_a))
288                                         goto next_arg;
289                                 cur_sect = next_sect;
290                                 while (*cur_sect == ':')
291                                         cur_sect++;
292                         } while (*cur_sect);
293                 }
294  check_found:
295                 if (!found) {
296                         bb_error_msg("no manual entry for '%s'", *argv);
297                         not_found = 1;
298                 }
299  next_arg:
300                 argv++;
301         } while (*argv);
302
303         return not_found;
304 }