who,less: tweaked help text
[platform/upstream/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); /* 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                 man ? "gtbl | nroff -Tlatin1 -mandoc 2>&1 | %s"
110                     : "%s",
111                 pager);
112         system(cmd);
113         free(cmd);
114         return 1;
115 }
116
117 /* man_filename is of the form "/dir/dir/dir/name.s" */
118 static int show_manpage(const char *pager, char *man_filename, int man, int level)
119 {
120 #if SEAMLESS_COMPRESSION
121         /* We leak this allocation... */
122         char *filename_with_zext = xasprintf("%s.lzma", man_filename);
123         char *ext = strrchr(filename_with_zext, '.') + 1;
124 #endif
125
126 #if ENABLE_FEATURE_SEAMLESS_LZMA
127         if (run_pipe(pager, filename_with_zext, man, level))
128                 return 1;
129 #endif
130 #if ENABLE_FEATURE_SEAMLESS_XZ
131         strcpy(ext, "xz");
132         if (run_pipe(pager, man_filename, man, level))
133                 return 1;
134 #endif
135 #if ENABLE_FEATURE_SEAMLESS_BZ2
136         strcpy(ext, "bz2");
137         if (run_pipe(pager, man_filename, man, level))
138                 return 1;
139 #endif
140 #if ENABLE_FEATURE_SEAMLESS_GZ
141         strcpy(ext, "gz");
142         if (run_pipe(pager, man_filename, man, level))
143                 return 1;
144 #endif
145
146 #if SEAMLESS_COMPRESSION
147         ext[-1] = '\0';
148 #endif
149         if (run_pipe(pager, man_filename, man, level))
150                 return 1;
151
152         return 0;
153 }
154
155 int man_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
156 int man_main(int argc UNUSED_PARAM, char **argv)
157 {
158         parser_t *parser;
159         const char *pager;
160         char **man_path_list;
161         char *sec_list;
162         char *cur_path, *cur_sect;
163         int count_mp, cur_mp;
164         int opt, not_found;
165         char *token[2];
166
167         opt_complementary = "-1"; /* at least one argument */
168         opt = getopt32(argv, "+aw");
169         argv += optind;
170
171         sec_list = xstrdup("1:2:3:4:5:6:7:8:9");
172         /* Last valid man_path_list[] is [0x10] */
173         count_mp = 0;
174         man_path_list = xzalloc(0x11 * sizeof(man_path_list[0]));
175         man_path_list[0] = getenv("MANPATH");
176         if (!man_path_list[0]) /* default, may be overridden by /etc/man.conf */
177                 man_path_list[0] = (char*)"/usr/man";
178         else
179                 count_mp++;
180         pager = getenv("MANPAGER");
181         if (!pager) {
182                 pager = getenv("PAGER");
183                 if (!pager)
184                         pager = "more";
185         }
186
187         /* Parse man.conf[ig] or man_db.conf */
188         /* man version 1.6f uses man.config */
189         /* man-db implementation of man uses man_db.conf */
190         parser = config_open2("/etc/man.config", fopen_for_read);
191         if (!parser)
192                 parser = config_open2("/etc/man.conf", fopen_for_read);
193         if (!parser)
194                 parser = config_open2("/etc/man_db.conf", fopen_for_read);
195
196         while (config_read(parser, token, 2, 0, "# \t", PARSE_NORMAL)) {
197                 if (!token[1])
198                         continue;
199                 if (strcmp("MANDATORY_MANPATH"+10, token[0]) == 0 /* "MANPATH"? */
200                  || strcmp("MANDATORY_MANPATH", token[0]) == 0
201                 ) {
202                         char *path = token[1];
203                         while (*path) {
204                                 char *next_path;
205                                 char **path_element;
206
207                                 next_path = strchr(path, ':');
208                                 if (next_path) {
209                                         *next_path = '\0';
210                                         if (next_path++ == path) /* "::"? */
211                                                 goto next;
212                                 }
213                                 /* Do we already have path? */
214                                 path_element = man_path_list;
215                                 while (*path_element) {
216                                         if (strcmp(*path_element, path) == 0)
217                                                 goto skip;
218                                         path_element++;
219                                 }
220                                 man_path_list = xrealloc_vector(man_path_list, 4, count_mp);
221                                 man_path_list[count_mp] = xstrdup(path);
222                                 count_mp++;
223                                 /* man_path_list is NULL terminated */
224                                 /*man_path_list[count_mp] = NULL; - xrealloc_vector did it */
225  skip:
226                                 if (!next_path)
227                                         break;
228  next:
229                                 path = next_path;
230                         }
231                 }
232                 if (strcmp("MANSECT", token[0]) == 0) {
233                         free(sec_list);
234                         sec_list = xstrdup(token[1]);
235                 }
236         }
237         config_close(parser);
238
239         not_found = 0;
240         do { /* for each argv[] */
241                 int found = 0;
242                 cur_mp = 0;
243
244                 if (strchr(*argv, '/')) {
245                         found = show_manpage(pager, *argv, /*man:*/ 1, 0);
246                         goto check_found;
247                 }
248                 while ((cur_path = man_path_list[cur_mp++]) != NULL) {
249                         /* for each MANPATH */
250                         cur_sect = sec_list;
251                         do { /* for each section */
252                                 char *next_sect = strchrnul(cur_sect, ':');
253                                 int sect_len = next_sect - cur_sect;
254                                 char *man_filename;
255                                 int cat0man1 = 0;
256
257                                 /* Search for cat, then man page */
258                                 while (cat0man1 < 2) {
259                                         int found_here;
260                                         man_filename = xasprintf("%s/%s%.*s/%s.%.*s",
261                                                         cur_path,
262                                                         "cat\0man" + (cat0man1 * 4),
263                                                         sect_len, cur_sect,
264                                                         *argv,
265                                                         sect_len, cur_sect);
266                                         found_here = show_manpage(pager, man_filename, cat0man1, 0);
267                                         found |= found_here;
268                                         cat0man1 += found_here + 1;
269                                         free(man_filename);
270                                 }
271
272                                 if (found && !(opt & OPT_a))
273                                         goto next_arg;
274                                 cur_sect = next_sect;
275                                 while (*cur_sect == ':')
276                                         cur_sect++;
277                         } while (*cur_sect);
278                 }
279  check_found:
280                 if (!found) {
281                         bb_error_msg("no manual entry for '%s'", *argv);
282                         not_found = 1;
283                 }
284  next_arg:
285                 argv++;
286         } while (*argv);
287
288         return not_found;
289 }