man: allow nroff and tbl commands be overridden; unmangle writing to files
[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 #include "common_bufsiz.h"
15
16 enum {
17         OPT_a = 1, /* all */
18         OPT_w = 2, /* print path */
19 };
20
21 /* This is what I see on my desktop system being executed:
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 On another system I see this:
32
33 ... | tbl | nroff -mandoc -rLL=<NNN>n -rLT=<NNN>n -Tutf8 | less
34
35 where <NNN> is screen width minus 5.
36 Replacing "DEFINE nroff nroff -mandoc" in /etc/man_db.conf
37 changes "nroff -mandoc" part; -rLL=<NNN>n, -rLT=<NNN>n and -Tutf8 parts are
38 appended to the user-specified command.
39
40 Redirecting to a pipe or file sets GROFF_NO_SGR=1 to prevent color escapes,
41 and uses "col -b -p -x" instead of pager, this filters out backspace
42 and underscore tricks.
43 */
44
45 struct globals {
46         const char *col;
47         const char *tbl;
48         const char *nroff;
49         const char *pager;
50 } FIX_ALIASING;
51 #define G (*(struct globals*)bb_common_bufsiz1)
52 #define INIT_G() do { \
53         setup_common_bufsiz(); \
54         G.col = "col"; \
55         G.tbl = "tbl"; \
56         /* replaced -Tlatin1 with -Tascii for non-UTF8 displays */; \
57         G.nroff = "nroff -mandoc -Tascii"; \
58         G.pager = ENABLE_LESS ? "less" : "more"; \
59 } while (0)
60
61 static int show_manpage(char *man_filename, int man, int level);
62
63 static int run_pipe(char *man_filename, int man, int level)
64 {
65         char *cmd;
66
67         /* Prevent man page link loops */
68         if (level > 10)
69                 return 0;
70
71         if (access(man_filename, R_OK) != 0)
72                 return 0;
73
74         if (option_mask32 & OPT_w) {
75                 puts(man_filename);
76                 return 1;
77         }
78
79         if (man) { /* man page, not cat page */
80                 /* Is this a link to another manpage? */
81                 /* The link has the following on the first line: */
82                 /* ".so another_man_page" */
83
84                 struct stat sb;
85                 char *line;
86                 char *linkname, *p;
87
88                 /* On my system:
89                  * man1/genhostid.1.gz: 203 bytes - smallest real manpage
90                  * man2/path_resolution.2.gz: 114 bytes - largest link
91                  */
92                 xstat(man_filename, &sb);
93                 if (sb.st_size > 300) /* err on the safe side */
94                         goto ordinary_manpage;
95
96                 line = xmalloc_open_zipped_read_close(man_filename, NULL);
97                 if (!line || !is_prefixed_with(line, ".so ")) {
98                         free(line);
99                         goto ordinary_manpage;
100                 }
101                 /* Example: man2/path_resolution.2.gz contains
102                  * ".so man7/path_resolution.7\n<junk>"
103                  */
104                 *strchrnul(line, '\n') = '\0';
105                 linkname = skip_whitespace(&line[4]);
106
107                 /* If link has no slashes, we just replace man page name.
108                  * If link has slashes (however many), we go back *once*.
109                  * ".so zzz/ggg/page.3" does NOT go back two levels. */
110                 p = strrchr(man_filename, '/');
111                 if (!p)
112                         goto ordinary_manpage;
113                 *p = '\0';
114                 if (strchr(linkname, '/')) {
115                         p = strrchr(man_filename, '/');
116                         if (!p)
117                                 goto ordinary_manpage;
118                         *p = '\0';
119                 }
120
121                 /* Links do not have .gz extensions, even if manpage
122                  * is compressed */
123                 man_filename = xasprintf("%s/%s", man_filename, linkname);
124                 free(line);
125                 /* Note: we leak "new" man_filename string as well... */
126                 if (show_manpage(man_filename, man, level + 1))
127                         return 1;
128                 /* else: show the link, it's better than nothing */
129         }
130
131  ordinary_manpage:
132         close(STDIN_FILENO);
133         open_zipped(man_filename, /*fail_if_not_compressed:*/ 0); /* guaranteed to use fd 0 (STDIN_FILENO) */
134         if (man) {
135                 /* "man man" formats to screen width.
136                  * "man man >file" formats to default 80 columns.
137                  * "man man | cat" formats to default 80 columns.
138                  */
139                 int w = get_terminal_width(STDOUT_FILENO);
140                 if (w > 10)
141                         w -= 2;
142                 /* "2>&1" is added so that nroff errors are shown in pager too.
143                  * Otherwise it may show just empty screen */
144                 cmd = xasprintf("%s | %s -rLL=%un -rLT=%un 2>&1 | %s",
145                                 G.tbl, G.nroff, w, w,
146                                 G.pager);
147         } else {
148                 cmd = xstrdup(G.pager);
149         }
150         system(cmd);
151         free(cmd);
152         return 1;
153 }
154
155 /* man_filename is of the form "/dir/dir/dir/name.s" */
156 static int show_manpage(char *man_filename, int man, int level)
157 {
158 #if SEAMLESS_COMPRESSION
159         /* We leak this allocation... */
160         char *filename_with_zext = xasprintf("%s.lzma", man_filename);
161         char *ext = strrchr(filename_with_zext, '.') + 1;
162 #endif
163
164 #if ENABLE_FEATURE_SEAMLESS_LZMA
165         if (run_pipe(filename_with_zext, man, level))
166                 return 1;
167 #endif
168 #if ENABLE_FEATURE_SEAMLESS_XZ
169         strcpy(ext, "xz");
170         if (run_pipe(filename_with_zext, man, level))
171                 return 1;
172 #endif
173 #if ENABLE_FEATURE_SEAMLESS_BZ2
174         strcpy(ext, "bz2");
175         if (run_pipe(filename_with_zext, man, level))
176                 return 1;
177 #endif
178 #if ENABLE_FEATURE_SEAMLESS_GZ
179         strcpy(ext, "gz");
180         if (run_pipe(filename_with_zext, man, level))
181                 return 1;
182 #endif
183
184         return run_pipe(man_filename, man, level);
185 }
186
187 static char **add_MANPATH(char **man_path_list, int *count_mp, char *path)
188 {
189         if (path) while (*path) {
190                 char *next_path;
191                 char **path_element;
192
193                 next_path = strchr(path, ':');
194                 if (next_path) {
195                         if (next_path == path) /* "::"? */
196                                 goto next;
197                         *next_path = '\0';
198                 }
199                 /* Do we already have path? */
200                 path_element = man_path_list;
201                 if (path_element) while (*path_element) {
202                         if (strcmp(*path_element, path) == 0)
203                                 goto skip;
204                         path_element++;
205                 }
206                 man_path_list = xrealloc_vector(man_path_list, 4, *count_mp);
207                 man_path_list[*count_mp] = xstrdup(path);
208                 (*count_mp)++;
209                 /* man_path_list is NULL terminated */
210                 /* man_path_list[*count_mp] = NULL; - xrealloc_vector did it */
211  skip:
212                 if (!next_path)
213                         break;
214                 /* "path" may be a result of getenv(), be nice and don't mangle it */
215                 *next_path = ':';
216  next:
217                 path = next_path + 1;
218         }
219         return man_path_list;
220 }
221
222 static const char *if_redefined(const char *var, const char *key, const char *line)
223 {
224         if (!is_prefixed_with(line, key))
225                 return var;
226         line += strlen(key);
227         if (!isspace(line[0]))
228                 return var;
229         return xstrdup(skip_whitespace(line));
230 }
231
232 int man_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
233 int man_main(int argc UNUSED_PARAM, char **argv)
234 {
235         parser_t *parser;
236         char *sec_list;
237         char *cur_path, *cur_sect;
238         char **man_path_list;
239         int count_mp;
240         int cur_mp;
241         int opt, not_found;
242         char *token[2];
243
244         INIT_G();
245
246         opt_complementary = "-1"; /* at least one argument */
247         opt = getopt32(argv, "+aw");
248         argv += optind;
249
250         sec_list = xstrdup("0p:1:1p:2:3:3p:4:5:6:7:8:9");
251
252         count_mp = 0;
253         man_path_list = add_MANPATH(NULL, &count_mp,
254                         getenv("MANDATORY_MANPATH"+10) /* "MANPATH" */
255         );
256         if (!man_path_list) {
257                 /* default, may be overridden by /etc/man.conf */
258                 man_path_list = xzalloc(2 * sizeof(man_path_list[0]));
259                 man_path_list[0] = (char*)"/usr/man";
260                 /* count_mp stays 0.
261                  * Thus, man.conf will overwrite man_path_list[0]
262                  * if a path is defined there.
263                  */
264         }
265
266         /* Parse man.conf[ig] or man_db.conf */
267         /* man version 1.6f uses man.config */
268         /* man-db implementation of man uses man_db.conf */
269         parser = config_open2("/etc/man.config", fopen_for_read);
270         if (!parser)
271                 parser = config_open2("/etc/man.conf", fopen_for_read);
272         if (!parser)
273                 parser = config_open2("/etc/man_db.conf", fopen_for_read);
274
275         while (config_read(parser, token, 2, 0, "# \t", PARSE_NORMAL)) {
276                 if (!token[1])
277                         continue;
278                 if (strcmp("DEFINE", token[0]) == 0) {
279                         G.col   = if_redefined(G.tbl  , "col",   token[1]);
280                         G.tbl   = if_redefined(G.tbl  , "tbl",   token[1]);
281                         G.nroff = if_redefined(G.nroff, "nroff", token[1]);
282                         G.pager = if_redefined(G.pager, "pager", token[1]);
283                 } else
284                 if (strcmp("MANDATORY_MANPATH"+10, token[0]) == 0 /* "MANPATH"? */
285                  || strcmp("MANDATORY_MANPATH", token[0]) == 0
286                 ) {
287                         man_path_list = add_MANPATH(man_path_list, &count_mp, token[1]);
288                 }
289                 if (strcmp("MANSECT", token[0]) == 0) {
290                         free(sec_list);
291                         sec_list = xstrdup(token[1]);
292                 }
293         }
294         config_close(parser);
295
296         {
297                 /* environment overrides setting from man.config */
298                 char *env_pager = getenv("MANPAGER");
299                 if (!env_pager)
300                         env_pager = getenv("PAGER");
301                 if (env_pager)
302                         G.pager = env_pager;
303         }
304
305         if (!isatty(STDOUT_FILENO)) {
306                 putenv((char*)"GROFF_NO_SGR=1");
307                 G.pager = xasprintf("%s -b -p -x", G.col);
308         }
309
310         not_found = 0;
311         do { /* for each argv[] */
312                 int found = 0;
313                 cur_mp = 0;
314
315                 if (strchr(*argv, '/')) {
316                         found = show_manpage(*argv, /*man:*/ 1, 0);
317                         goto check_found;
318                 }
319                 while ((cur_path = man_path_list[cur_mp++]) != NULL) {
320                         /* for each MANPATH */
321                         cur_sect = sec_list;
322                         do { /* for each section */
323                                 char *next_sect = strchrnul(cur_sect, ':');
324                                 int sect_len = next_sect - cur_sect;
325                                 char *man_filename;
326                                 int cat0man1 = 0;
327
328                                 /* Search for cat, then man page */
329                                 while (cat0man1 < 2) {
330                                         int found_here;
331                                         man_filename = xasprintf("%s/%s%.*s/%s.%.*s",
332                                                         cur_path,
333                                                         "cat\0man" + (cat0man1 * 4),
334                                                         sect_len, cur_sect,
335                                                         *argv,
336                                                         sect_len, cur_sect);
337                                         found_here = show_manpage(man_filename, cat0man1, 0);
338                                         found |= found_here;
339                                         cat0man1 += found_here + 1;
340                                         free(man_filename);
341                                 }
342
343                                 if (found && !(opt & OPT_a))
344                                         goto next_arg;
345                                 cur_sect = next_sect;
346                                 while (*cur_sect == ':')
347                                         cur_sect++;
348                         } while (*cur_sect);
349                 }
350  check_found:
351                 if (!found) {
352                         bb_error_msg("no manual entry for '%s'", *argv);
353                         not_found = 1;
354                 }
355  next_arg:
356                 argv++;
357         } while (*argv);
358
359         return not_found;
360 }