man: yet another fixlet to "manpage link" code
[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 tarball for details.
4  */
5
6 #include "libbb.h"
7
8 enum {
9         OPT_a = 1, /* all */
10         OPT_w = 2, /* print path */
11 };
12
13 /* This is what I see on my desktop system being executed:
14
15 (
16 echo ".ll 12.4i"
17 echo ".nr LL 12.4i"
18 echo ".pl 1100i"
19 gunzip -c '/usr/man/man1/bzip2.1.gz'
20 echo ".\\\""
21 echo ".pl \n(nlu+10"
22 ) | gtbl | nroff -Tlatin1 -mandoc | less
23
24 */
25
26 /* Trick gcc to reuse "cat" string. */
27 #define STR_catNULmanNUL "cat\0man"
28 #define STR_cat          "cat\0man"
29
30 //TODO: make gz/bz2 support conditional on FEATURE_SEAMLESS_GZ/BZ2,
31 // add SEAMLESS_LZMA support
32
33 static int show_manpage(const char *pager, char *man_filename, int man, int level);
34
35 static int run_pipe(const char *unpacker, 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" ".bz2", 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         /* "2>&1" is added so that nroff errors are shown in pager too.
105          * Otherwise it may show just empty screen */
106         cmd = xasprintf(
107                 man ? "%s '%s' 2>&1 | gtbl | nroff -Tlatin1 -mandoc 2>&1 | %s"
108                     : "%s '%s' 2>&1 | %s",
109                 unpacker, man_filename, pager);
110         system(cmd);
111         free(cmd);
112         return 1;
113 }
114
115 /* man_filename is of the form "/dir/dir/dir/name.s.bz2" */
116 static int show_manpage(const char *pager, char *man_filename, int man, int level)
117 {
118         int len;
119
120         if (run_pipe("bunzip2 -c", pager, man_filename, man, level))
121                 return 1;
122
123         len = strlen(man_filename) - 1;
124
125         man_filename[len] = '\0'; /* ".bz2" -> ".gz" */
126         man_filename[len - 2] = 'g';
127         if (run_pipe("gunzip -c", pager, man_filename, man, level))
128                 return 1;
129
130         man_filename[len - 3] = '\0'; /* ".gz" -> "" */
131         if (run_pipe(STR_cat, pager, man_filename, man, level))
132                 return 1;
133
134         return 0;
135 }
136
137 int man_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
138 int man_main(int argc UNUSED_PARAM, char **argv)
139 {
140         parser_t *parser;
141         const char *pager;
142         char **man_path_list;
143         char *sec_list;
144         char *cur_path, *cur_sect;
145         int count_mp, cur_mp;
146         int opt, not_found;
147         char *token[2];
148
149         opt_complementary = "-1"; /* at least one argument */
150         opt = getopt32(argv, "+aw");
151         argv += optind;
152
153         sec_list = xstrdup("1:2:3:4:5:6:7:8:9");
154         /* Last valid man_path_list[] is [0x10] */
155         man_path_list = xzalloc(0x11 * sizeof(man_path_list[0]));
156         count_mp = 0;
157         man_path_list[0] = xstrdup(getenv("MANPATH"));
158         if (man_path_list[0])
159                 count_mp++;
160         pager = getenv("MANPAGER");
161         if (!pager) {
162                 pager = getenv("PAGER");
163                 if (!pager)
164                         pager = "more";
165         }
166
167         /* Parse man.conf */
168         parser = config_open("/etc/man.conf");
169         while (config_read(parser, token, 2, 0, "# \t", PARSE_NORMAL)) {
170                 if (!token[1])
171                         continue;
172                 if (strcmp("MANPATH", token[0]) == 0) {
173                         man_path_list = xrealloc_vector(man_path_list, 4, count_mp);
174                         man_path_list[count_mp] = xstrdup(token[1]);
175                         count_mp++;
176                         /* man_path_list is NULL terminated */
177                         /*man_path_list[count_mp] = NULL; - xrealloc_vector did it */
178                 }
179                 if (strcmp("MANSECT", token[0]) == 0) {
180                         free(sec_list);
181                         sec_list = xstrdup(token[1]);
182                 }
183         }
184         config_close(parser);
185
186         not_found = 0;
187         do { /* for each argv[] */
188                 int found = 0;
189                 cur_mp = 0;
190                 while ((cur_path = man_path_list[cur_mp++]) != NULL) {
191                         /* for each MANPATH */
192                         do { /* for each MANPATH item */
193                                 char *next_path = strchrnul(cur_path, ':');
194                                 int path_len = next_path - cur_path;
195                                 cur_sect = sec_list;
196                                 do { /* for each section */
197                                         char *next_sect = strchrnul(cur_sect, ':');
198                                         int sect_len = next_sect - cur_sect;
199                                         char *man_filename;
200                                         int cat0man1 = 0;
201
202                                         /* Search for cat, then man page */
203                                         while (cat0man1 < 2) {
204                                                 int found_here;
205                                                 man_filename = xasprintf("%.*s/%s%.*s/%s.%.*s" ".bz2",
206                                                                 path_len, cur_path,
207                                                                 STR_catNULmanNUL + cat0man1 * 4,
208                                                                 sect_len, cur_sect,
209                                                                 *argv,
210                                                                 sect_len, cur_sect);
211                                                 found_here = show_manpage(pager, man_filename, cat0man1, 0);
212                                                 found |= found_here;
213                                                 cat0man1 += found_here + 1;
214                                                 free(man_filename);
215                                         }
216
217                                         if (found && !(opt & OPT_a))
218                                                 goto next_arg;
219                                         cur_sect = next_sect;
220                                         while (*cur_sect == ':')
221                                                 cur_sect++;
222                                 } while (*cur_sect);
223                                 cur_path = next_path;
224                                 while (*cur_path == ':')
225                                         cur_path++;
226                         } while (*cur_path);
227                 }
228                 if (!found) {
229                         bb_error_msg("no manual entry for '%s'", *argv);
230                         not_found = 1;
231                 }
232  next_arg:
233                 argv++;
234         } while (*argv);
235
236         return not_found;
237 }