blob: 24551c0e06d79ae640a333f5e96a7f2ae6b33f2f [file] [log] [blame]
Denis Vlasenkoffa44992008-04-13 08:20:00 +00001/* 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
8enum {
9 OPT_a = 1, /* all */
10 OPT_w = 2, /* print path */
11};
12
Denis Vlasenkoce02b152008-06-27 22:05:21 +000013/* This is what I see on my desktop system being executed:
Denis Vlasenkoffa44992008-04-13 08:20:00 +000014
15(
16echo ".ll 12.4i"
17echo ".nr LL 12.4i"
18echo ".pl 1100i"
19gunzip -c '/usr/man/man1/bzip2.1.gz'
20echo ".\\\""
21echo ".pl \n(nlu+10"
22) | gtbl | nroff -Tlatin1 -mandoc | less
23
24*/
25
Denis Vlasenko0e7f28d2008-08-05 15:28:05 +000026#if ENABLE_FEATURE_SEAMLESS_LZMA
27#define Z_SUFFIX ".lzma"
28#elif ENABLE_FEATURE_SEAMLESS_BZ2
29#define Z_SUFFIX ".bz2"
30#elif ENABLE_FEATURE_SEAMLESS_GZ
31#define Z_SUFFIX ".gz"
32#else
33#define Z_SUFFIX ""
34#endif
Denis Vlasenko540baf62008-08-05 13:16:18 +000035
36static int show_manpage(const char *pager, char *man_filename, int man, int level);
37
Denis Vlasenko0e7f28d2008-08-05 15:28:05 +000038static int run_pipe(const char *pager, char *man_filename, int man, int level)
Denis Vlasenkoffa44992008-04-13 08:20:00 +000039{
40 char *cmd;
41
Denis Vlasenko540baf62008-08-05 13:16:18 +000042 /* Prevent man page link loops */
43 if (level > 10)
44 return 0;
45
Denis Vlasenkoffa44992008-04-13 08:20:00 +000046 if (access(man_filename, R_OK) != 0)
47 return 0;
48
49 if (option_mask32 & OPT_w) {
50 puts(man_filename);
51 return 1;
52 }
53
Denis Vlasenko540baf62008-08-05 13:16:18 +000054 if (man) { /* man page, not cat page */
Denis Vlasenkoe2a09de2008-08-05 14:15:19 +000055 /* Is this a link to another manpage? */
Denis Vlasenko540baf62008-08-05 13:16:18 +000056 /* The link has the following on the first line: */
57 /* ".so another_man_page" */
Denis Vlasenkoe2a09de2008-08-05 14:15:19 +000058
Denis Vlasenko540baf62008-08-05 13:16:18 +000059 struct stat sb;
60 char *line;
61 char *linkname, *p;
62
63 /* On my system:
64 * man1/genhostid.1.gz: 203 bytes - smallest real manpage
65 * man2/path_resolution.2.gz: 114 bytes - largest link
66 */
67 xstat(man_filename, &sb);
68 if (sb.st_size > 300) /* err on the safe side */
69 goto ordinary_manpage;
70
71 line = xmalloc_open_zipped_read_close(man_filename, NULL);
72 if (!line || strncmp(line, ".so ", 4) != 0) {
73 free(line);
74 goto ordinary_manpage;
75 }
76 /* Example: man2/path_resolution.2.gz contains
77 * ".so man7/path_resolution.7\n<junk>"
78 */
79 *strchrnul(line, '\n') = '\0';
Denis Vlasenkoe2a09de2008-08-05 14:15:19 +000080 linkname = skip_whitespace(&line[4]);
Denis Vlasenko540baf62008-08-05 13:16:18 +000081
Denis Vlasenko22b0bd82008-08-05 13:45:22 +000082 /* If link has no slashes, we just replace man page name.
83 * If link has slashes (however many), we go back *once*.
84 * ".so zzz/ggg/page.3" does NOT go back two levels. */
85 p = strrchr(man_filename, '/');
86 if (!p)
87 goto ordinary_manpage;
88 *p = '\0';
89 if (strchr(linkname, '/')) {
90 p = strrchr(man_filename, '/');
91 if (!p)
Denis Vlasenko540baf62008-08-05 13:16:18 +000092 goto ordinary_manpage;
Denis Vlasenko22b0bd82008-08-05 13:45:22 +000093 *p = '\0';
Denis Vlasenko540baf62008-08-05 13:16:18 +000094 }
Denis Vlasenko22b0bd82008-08-05 13:45:22 +000095
96 /* Links do not have .gz extensions, even if manpage
97 * is compressed */
Denis Vlasenko0e7f28d2008-08-05 15:28:05 +000098 man_filename = xasprintf("%s/%s" Z_SUFFIX, man_filename, linkname);
Denis Vlasenko540baf62008-08-05 13:16:18 +000099 free(line);
100 /* Note: we leak "new" man_filename string as well... */
101 if (show_manpage(pager, man_filename, man, level + 1))
102 return 1;
Denis Vlasenko22b0bd82008-08-05 13:45:22 +0000103 /* else: show the link, it's better than nothing */
Denis Vlasenko540baf62008-08-05 13:16:18 +0000104 }
105
106 ordinary_manpage:
Denis Vlasenko0e7f28d2008-08-05 15:28:05 +0000107 close(STDIN_FILENO);
108 open_zipped(man_filename); /* guaranteed to use fd 0 (STDIN_FILENO) */
Denis Vlasenko4cbffc02008-07-04 21:58:00 +0000109 /* "2>&1" is added so that nroff errors are shown in pager too.
Denis Vlasenkob75fe792008-06-27 22:31:07 +0000110 * Otherwise it may show just empty screen */
Denis Vlasenkof6efccc2008-07-05 08:50:08 +0000111 cmd = xasprintf(
Denis Vlasenko0e7f28d2008-08-05 15:28:05 +0000112 man ? "gtbl | nroff -Tlatin1 -mandoc 2>&1 | %s"
113 : "%s",
114 pager);
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000115 system(cmd);
116 free(cmd);
117 return 1;
118}
119
Denis Vlasenko0e7f28d2008-08-05 15:28:05 +0000120/* man_filename is of the form "/dir/dir/dir/name.s" Z_SUFFIX */
Denis Vlasenko540baf62008-08-05 13:16:18 +0000121static int show_manpage(const char *pager, char *man_filename, int man, int level)
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000122{
Denis Vlasenko0e7f28d2008-08-05 15:28:05 +0000123#if ENABLE_FEATURE_SEAMLESS_LZMA
124 if (run_pipe(pager, man_filename, man, level))
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000125 return 1;
Denis Vlasenko0e7f28d2008-08-05 15:28:05 +0000126#endif
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000127
Denis Vlasenko0e7f28d2008-08-05 15:28:05 +0000128#if ENABLE_FEATURE_SEAMLESS_BZ2
129#if ENABLE_FEATURE_SEAMLESS_LZMA
130 strcpy(strrchr(man_filename, '.') + 1, "bz2");
131#endif
132 if (run_pipe(pager, man_filename, man, level))
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000133 return 1;
Denis Vlasenko0e7f28d2008-08-05 15:28:05 +0000134#endif
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000135
Denis Vlasenko0e7f28d2008-08-05 15:28:05 +0000136#if ENABLE_FEATURE_SEAMLESS_GZ
137#if ENABLE_FEATURE_SEAMLESS_LZMA || ENABLE_FEATURE_SEAMLESS_BZ2
138 strcpy(strrchr(man_filename, '.') + 1, "gz");
139#endif
140 if (run_pipe(pager, man_filename, man, level))
141 return 1;
142#endif
143
144#if ENABLE_FEATURE_SEAMLESS_LZMA || ENABLE_FEATURE_SEAMLESS_BZ2 || ENABLE_FEATURE_SEAMLESS_GZ
145 *strrchr(man_filename, '.') = '\0';
146#endif
147 if (run_pipe(pager, man_filename, man, level))
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000148 return 1;
149
150 return 0;
151}
152
153int man_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000154int man_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000155{
Denis Vlasenko09aaf782008-07-20 17:41:30 +0000156 parser_t *parser;
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000157 const char *pager;
Denis Vlasenko50d068c2008-04-19 03:42:47 +0000158 char **man_path_list;
159 char *sec_list;
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000160 char *cur_path, *cur_sect;
Denis Vlasenko9835d472008-07-04 21:57:11 +0000161 int count_mp, cur_mp;
Denis Vlasenkoce02b152008-06-27 22:05:21 +0000162 int opt, not_found;
Denis Vlasenko084266e2008-07-26 23:08:31 +0000163 char *token[2];
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000164
165 opt_complementary = "-1"; /* at least one argument */
166 opt = getopt32(argv, "+aw");
167 argv += optind;
168
169 sec_list = xstrdup("1:2:3:4:5:6:7:8:9");
Denis Vlasenko9835d472008-07-04 21:57:11 +0000170 /* Last valid man_path_list[] is [0x10] */
Denis Vlasenkoa1e16c92008-09-14 21:30:35 +0000171 count_mp = 0;
Denis Vlasenko9835d472008-07-04 21:57:11 +0000172 man_path_list = xzalloc(0x11 * sizeof(man_path_list[0]));
Denis Vlasenkoe357d2a2008-09-14 21:26:55 +0000173 man_path_list[0] = getenv("MANPATH");
Denis Vlasenkoa1e16c92008-09-14 21:30:35 +0000174 if (!man_path_list[0]) /* default, may be overridden by /etc/man.conf */
Denis Vlasenkoe357d2a2008-09-14 21:26:55 +0000175 man_path_list[0] = (char*)"/usr/man";
Denis Vlasenkoa1e16c92008-09-14 21:30:35 +0000176 else
177 count_mp++;
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000178 pager = getenv("MANPAGER");
179 if (!pager) {
180 pager = getenv("PAGER");
181 if (!pager)
182 pager = "more";
183 }
184
185 /* Parse man.conf */
Denis Vlasenkoa1e16c92008-09-14 21:30:35 +0000186 parser = config_open2("/etc/man.conf", fopen_for_read);
Denis Vlasenko94d03f02008-07-26 23:16:33 +0000187 while (config_read(parser, token, 2, 0, "# \t", PARSE_NORMAL)) {
188 if (!token[1])
189 continue;
190 if (strcmp("MANPATH", token[0]) == 0) {
Denis Vlasenkoe357d2a2008-09-14 21:26:55 +0000191 /* Do we already have it? */
192 char **path_element = man_path_list;
193 while (*path_element) {
194 if (strcmp(*path_element, token[1]) == 0)
195 goto skip;
196 path_element++;
197 }
Denis Vlasenko94d03f02008-07-26 23:16:33 +0000198 man_path_list = xrealloc_vector(man_path_list, 4, count_mp);
199 man_path_list[count_mp] = xstrdup(token[1]);
200 count_mp++;
201 /* man_path_list is NULL terminated */
Denis Vlasenko27842282008-08-04 13:20:36 +0000202 /*man_path_list[count_mp] = NULL; - xrealloc_vector did it */
Denis Vlasenko94d03f02008-07-26 23:16:33 +0000203 }
204 if (strcmp("MANSECT", token[0]) == 0) {
205 free(sec_list);
206 sec_list = xstrdup(token[1]);
207 }
Denis Vlasenkoe357d2a2008-09-14 21:26:55 +0000208 skip: ;
Denis Vlasenko94d03f02008-07-26 23:16:33 +0000209 }
210 config_close(parser);
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000211
Denis Vlasenkoce02b152008-06-27 22:05:21 +0000212 not_found = 0;
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000213 do { /* for each argv[] */
Denis Vlasenkoce02b152008-06-27 22:05:21 +0000214 int found = 0;
Denis Vlasenko50d068c2008-04-19 03:42:47 +0000215 cur_mp = 0;
Denis Vlasenko0e7f28d2008-08-05 15:28:05 +0000216
217 if (strchr(*argv, '/')) {
218 found = show_manpage(pager, *argv, /*man:*/ 1, 0);
219 goto check_found;
220 }
Denis Vlasenko50d068c2008-04-19 03:42:47 +0000221 while ((cur_path = man_path_list[cur_mp++]) != NULL) {
222 /* for each MANPATH */
223 do { /* for each MANPATH item */
224 char *next_path = strchrnul(cur_path, ':');
225 int path_len = next_path - cur_path;
226 cur_sect = sec_list;
227 do { /* for each section */
228 char *next_sect = strchrnul(cur_sect, ':');
229 int sect_len = next_sect - cur_sect;
Denis Vlasenko4cbffc02008-07-04 21:58:00 +0000230 char *man_filename;
Denis Vlasenkofec8b422008-07-05 08:38:41 +0000231 int cat0man1 = 0;
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000232
Denis Vlasenkofec8b422008-07-05 08:38:41 +0000233 /* Search for cat, then man page */
234 while (cat0man1 < 2) {
235 int found_here;
Denis Vlasenko0e7f28d2008-08-05 15:28:05 +0000236 man_filename = xasprintf("%.*s/%s%.*s/%s.%.*s" Z_SUFFIX,
Denis Vlasenkofec8b422008-07-05 08:38:41 +0000237 path_len, cur_path,
Denis Vlasenko0e7f28d2008-08-05 15:28:05 +0000238 "cat\0man" + (cat0man1 * 4),
Denis Vlasenkofec8b422008-07-05 08:38:41 +0000239 sect_len, cur_sect,
240 *argv,
241 sect_len, cur_sect);
Denis Vlasenko540baf62008-08-05 13:16:18 +0000242 found_here = show_manpage(pager, man_filename, cat0man1, 0);
Denis Vlasenkofec8b422008-07-05 08:38:41 +0000243 found |= found_here;
244 cat0man1 += found_here + 1;
Denis Vlasenko4cbffc02008-07-04 21:58:00 +0000245 free(man_filename);
246 }
Denis Vlasenko4cbffc02008-07-04 21:58:00 +0000247
Denis Vlasenko50d068c2008-04-19 03:42:47 +0000248 if (found && !(opt & OPT_a))
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000249 goto next_arg;
Denis Vlasenko50d068c2008-04-19 03:42:47 +0000250 cur_sect = next_sect;
251 while (*cur_sect == ':')
252 cur_sect++;
253 } while (*cur_sect);
254 cur_path = next_path;
255 while (*cur_path == ':')
256 cur_path++;
257 } while (*cur_path);
258 }
Denis Vlasenko0e7f28d2008-08-05 15:28:05 +0000259 check_found:
Denis Vlasenkoce02b152008-06-27 22:05:21 +0000260 if (!found) {
261 bb_error_msg("no manual entry for '%s'", *argv);
262 not_found = 1;
263 }
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000264 next_arg:
265 argv++;
266 } while (*argv);
267
Denis Vlasenkoce02b152008-06-27 22:05:21 +0000268 return not_found;
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000269}