man: add handling of "man links", by Ivana Varekova <varekova AT redhat.com>
[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                 /* Test whether the man page is not a link to another one. */
53                 /* The link has the following on the first line: */
54                 /* ".so another_man_page" */
55                 struct stat sb;
56                 char *line;
57                 char *linkname, *p;
58
59                 /* On my system:
60                  * man1/genhostid.1.gz: 203 bytes - smallest real manpage
61                  * man2/path_resolution.2.gz: 114 bytes - largest link
62                  */
63                 xstat(man_filename, &sb);
64                 if (sb.st_size > 300) /* err on the safe side */
65                         goto ordinary_manpage;
66
67                 line = xmalloc_open_zipped_read_close(man_filename, NULL);
68                 if (!line || strncmp(line, ".so ", 4) != 0) {
69                         free(line);
70                         goto ordinary_manpage;
71                 }
72                 /* Example: man2/path_resolution.2.gz contains
73                  * ".so man7/path_resolution.7\n<junk>"
74                  */
75                 *strchrnul(line, '\n') = '\0';
76                 linkname = p = skip_whitespace(&line[4]);
77                 while (1) {
78                         char *newname_slash, *oldname_slash;
79
80                         oldname_slash = strrchr(man_filename, '/');
81                         if (!oldname_slash)
82                                 goto ordinary_manpage;
83                         *oldname_slash = '\0';
84                         newname_slash = strchr(p, '/');
85                         if (!newname_slash)
86                                 break;
87                         p = newname_slash + 1;
88                 }
89                 man_filename = xasprintf("%s/%s" ".bz2", man_filename, linkname);
90                 free(line);
91                 /* Note: we leak "new" man_filename string as well... */
92                 if (show_manpage(pager, man_filename, man, level + 1))
93                         return 1;
94                 /* else: show the link, it's better than nothing */
95         }
96
97  ordinary_manpage:
98         /* "2>&1" is added so that nroff errors are shown in pager too.
99          * Otherwise it may show just empty screen */
100         cmd = xasprintf(
101                 man ? "%s '%s' | gtbl | nroff -Tlatin1 -mandoc 2>&1 | %s"
102                     : "%s '%s' | %s",
103                 unpacker, man_filename, pager);
104         system(cmd);
105         free(cmd);
106         return 1;
107 }
108
109 /* man_filename is of the form "/dir/dir/dir/name.s.bz2" */
110 static int show_manpage(const char *pager, char *man_filename, int man, int level)
111 {
112         int len;
113
114         if (run_pipe("bunzip2 -c", pager, man_filename, man, level))
115                 return 1;
116
117         len = strlen(man_filename) - 1;
118
119         man_filename[len] = '\0'; /* ".bz2" -> ".gz" */
120         man_filename[len - 2] = 'g';
121         if (run_pipe("gunzip -c", pager, man_filename, man, level))
122                 return 1;
123
124         man_filename[len - 3] = '\0'; /* ".gz" -> "" */
125         if (run_pipe(STR_cat, pager, man_filename, man, level))
126                 return 1;
127
128         return 0;
129 }
130
131 int man_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
132 int man_main(int argc UNUSED_PARAM, char **argv)
133 {
134         parser_t *parser;
135         const char *pager;
136         char **man_path_list;
137         char *sec_list;
138         char *cur_path, *cur_sect;
139         int count_mp, cur_mp;
140         int opt, not_found;
141         char *token[2];
142
143         opt_complementary = "-1"; /* at least one argument */
144         opt = getopt32(argv, "+aw");
145         argv += optind;
146
147         sec_list = xstrdup("1:2:3:4:5:6:7:8:9");
148         /* Last valid man_path_list[] is [0x10] */
149         man_path_list = xzalloc(0x11 * sizeof(man_path_list[0]));
150         count_mp = 0;
151         man_path_list[0] = xstrdup(getenv("MANPATH"));
152         if (man_path_list[0])
153                 count_mp++;
154         pager = getenv("MANPAGER");
155         if (!pager) {
156                 pager = getenv("PAGER");
157                 if (!pager)
158                         pager = "more";
159         }
160
161         /* Parse man.conf */
162         parser = config_open("/etc/man.conf");
163         while (config_read(parser, token, 2, 0, "# \t", PARSE_NORMAL)) {
164                 if (!token[1])
165                         continue;
166                 if (strcmp("MANPATH", token[0]) == 0) {
167                         man_path_list = xrealloc_vector(man_path_list, 4, count_mp);
168                         man_path_list[count_mp] = xstrdup(token[1]);
169                         count_mp++;
170                         /* man_path_list is NULL terminated */
171                         /*man_path_list[count_mp] = NULL; - xrealloc_vector did it */
172                 }
173                 if (strcmp("MANSECT", token[0]) == 0) {
174                         free(sec_list);
175                         sec_list = xstrdup(token[1]);
176                 }
177         }
178         config_close(parser);
179
180         not_found = 0;
181         do { /* for each argv[] */
182                 int found = 0;
183                 cur_mp = 0;
184                 while ((cur_path = man_path_list[cur_mp++]) != NULL) {
185                         /* for each MANPATH */
186                         do { /* for each MANPATH item */
187                                 char *next_path = strchrnul(cur_path, ':');
188                                 int path_len = next_path - cur_path;
189                                 cur_sect = sec_list;
190                                 do { /* for each section */
191                                         char *next_sect = strchrnul(cur_sect, ':');
192                                         int sect_len = next_sect - cur_sect;
193                                         char *man_filename;
194                                         int cat0man1 = 0;
195
196                                         /* Search for cat, then man page */
197                                         while (cat0man1 < 2) {
198                                                 int found_here;
199                                                 man_filename = xasprintf("%.*s/%s%.*s/%s.%.*s" ".bz2",
200                                                                 path_len, cur_path,
201                                                                 STR_catNULmanNUL + cat0man1 * 4,
202                                                                 sect_len, cur_sect,
203                                                                 *argv,
204                                                                 sect_len, cur_sect);
205                                                 found_here = show_manpage(pager, man_filename, cat0man1, 0);
206                                                 found |= found_here;
207                                                 cat0man1 += found_here + 1;
208                                                 free(man_filename);
209                                         }
210
211                                         if (found && !(opt & OPT_a))
212                                                 goto next_arg;
213                                         cur_sect = next_sect;
214                                         while (*cur_sect == ':')
215                                                 cur_sect++;
216                                 } while (*cur_sect);
217                                 cur_path = next_path;
218                                 while (*cur_path == ':')
219                                         cur_path++;
220                         } while (*cur_path);
221                 }
222                 if (!found) {
223                         bb_error_msg("no manual entry for '%s'", *argv);
224                         not_found = 1;
225                 }
226  next_arg:
227                 argv++;
228         } while (*argv);
229
230         return not_found;
231 }