blob: d3e832bd7e43dfb8bfb7be197c54cbad10faa742 [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>
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02003 * Licensed under GPLv2, see file LICENSE in this source tree.
Denis Vlasenkoffa44992008-04-13 08:20:00 +00004 */
5
Pere Orga5bc8c002011-04-11 03:29:49 +02006//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"
Denys Vlasenko66426762011-06-05 03:58:28 +020010//usage: "\n -a Display all pages"
Pere Orga5bc8c002011-04-11 03:29:49 +020011//usage: "\n -w Show page locations"
12
Denis Vlasenkoffa44992008-04-13 08:20:00 +000013#include "libbb.h"
14
15enum {
16 OPT_a = 1, /* all */
17 OPT_w = 2, /* print path */
18};
19
Denis Vlasenkoce02b152008-06-27 22:05:21 +000020/* This is what I see on my desktop system being executed:
Denis Vlasenkoffa44992008-04-13 08:20:00 +000021
22(
23echo ".ll 12.4i"
24echo ".nr LL 12.4i"
25echo ".pl 1100i"
26gunzip -c '/usr/man/man1/bzip2.1.gz'
27echo ".\\\""
28echo ".pl \n(nlu+10"
29) | gtbl | nroff -Tlatin1 -mandoc | less
30
31*/
32
Denis Vlasenko540baf62008-08-05 13:16:18 +000033static int show_manpage(const char *pager, char *man_filename, int man, int level);
34
Denis Vlasenko0e7f28d2008-08-05 15:28:05 +000035static int run_pipe(const char *pager, char *man_filename, int man, int level)
Denis Vlasenkoffa44992008-04-13 08:20:00 +000036{
37 char *cmd;
38
Denis Vlasenko540baf62008-08-05 13:16:18 +000039 /* Prevent man page link loops */
40 if (level > 10)
41 return 0;
42
Denis Vlasenkoffa44992008-04-13 08:20:00 +000043 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
Denis Vlasenko540baf62008-08-05 13:16:18 +000051 if (man) { /* man page, not cat page */
Denis Vlasenkoe2a09de2008-08-05 14:15:19 +000052 /* Is this a link to another manpage? */
Denis Vlasenko540baf62008-08-05 13:16:18 +000053 /* The link has the following on the first line: */
54 /* ".so another_man_page" */
Denis Vlasenkoe2a09de2008-08-05 14:15:19 +000055
Denis Vlasenko540baf62008-08-05 13:16:18 +000056 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';
Denis Vlasenkoe2a09de2008-08-05 14:15:19 +000077 linkname = skip_whitespace(&line[4]);
Denis Vlasenko540baf62008-08-05 13:16:18 +000078
Denis Vlasenko22b0bd82008-08-05 13:45:22 +000079 /* 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)
Denis Vlasenko540baf62008-08-05 13:16:18 +000089 goto ordinary_manpage;
Denis Vlasenko22b0bd82008-08-05 13:45:22 +000090 *p = '\0';
Denis Vlasenko540baf62008-08-05 13:16:18 +000091 }
Denis Vlasenko22b0bd82008-08-05 13:45:22 +000092
93 /* Links do not have .gz extensions, even if manpage
94 * is compressed */
Denys Vlasenko59655072012-03-06 16:23:50 +010095 man_filename = xasprintf("%s/%s", man_filename, linkname);
Denis Vlasenko540baf62008-08-05 13:16:18 +000096 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;
Denis Vlasenko22b0bd82008-08-05 13:45:22 +0000100 /* else: show the link, it's better than nothing */
Denis Vlasenko540baf62008-08-05 13:16:18 +0000101 }
102
103 ordinary_manpage:
Denis Vlasenko0e7f28d2008-08-05 15:28:05 +0000104 close(STDIN_FILENO);
105 open_zipped(man_filename); /* guaranteed to use fd 0 (STDIN_FILENO) */
Denis Vlasenko4cbffc02008-07-04 21:58:00 +0000106 /* "2>&1" is added so that nroff errors are shown in pager too.
Denis Vlasenkob75fe792008-06-27 22:31:07 +0000107 * Otherwise it may show just empty screen */
Denis Vlasenkof6efccc2008-07-05 08:50:08 +0000108 cmd = xasprintf(
Denis Vlasenko0e7f28d2008-08-05 15:28:05 +0000109 man ? "gtbl | nroff -Tlatin1 -mandoc 2>&1 | %s"
110 : "%s",
111 pager);
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000112 system(cmd);
113 free(cmd);
114 return 1;
115}
116
Denys Vlasenko59655072012-03-06 16:23:50 +0100117/* man_filename is of the form "/dir/dir/dir/name.s" */
Denis Vlasenko540baf62008-08-05 13:16:18 +0000118static int show_manpage(const char *pager, char *man_filename, int man, int level)
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000119{
Denys Vlasenko59655072012-03-06 16:23:50 +0100120#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
Denis Vlasenko0e7f28d2008-08-05 15:28:05 +0000126#if ENABLE_FEATURE_SEAMLESS_LZMA
Denys Vlasenko59655072012-03-06 16:23:50 +0100127 if (run_pipe(pager, filename_with_zext, man, level))
128 return 1;
129#endif
130#if ENABLE_FEATURE_SEAMLESS_XZ
131 strcpy(ext, "xz");
Denys Vlasenkoba1d5612012-06-08 10:22:05 +0200132 if (run_pipe(pager, filename_with_zext, man, level))
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000133 return 1;
Denis Vlasenko0e7f28d2008-08-05 15:28:05 +0000134#endif
Denis Vlasenko0e7f28d2008-08-05 15:28:05 +0000135#if ENABLE_FEATURE_SEAMLESS_BZ2
Denys Vlasenko59655072012-03-06 16:23:50 +0100136 strcpy(ext, "bz2");
Denys Vlasenkoba1d5612012-06-08 10:22:05 +0200137 if (run_pipe(pager, filename_with_zext, man, level))
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000138 return 1;
Denis Vlasenko0e7f28d2008-08-05 15:28:05 +0000139#endif
Denis Vlasenko0e7f28d2008-08-05 15:28:05 +0000140#if ENABLE_FEATURE_SEAMLESS_GZ
Denys Vlasenko59655072012-03-06 16:23:50 +0100141 strcpy(ext, "gz");
Denys Vlasenkoba1d5612012-06-08 10:22:05 +0200142 if (run_pipe(pager, filename_with_zext, man, level))
Denis Vlasenko0e7f28d2008-08-05 15:28:05 +0000143 return 1;
144#endif
145
Denys Vlasenkoba1d5612012-06-08 10:22:05 +0200146 return run_pipe(pager, man_filename, man, level);
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000147}
148
149int man_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000150int man_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000151{
Denis Vlasenko09aaf782008-07-20 17:41:30 +0000152 parser_t *parser;
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000153 const char *pager;
Denis Vlasenko50d068c2008-04-19 03:42:47 +0000154 char **man_path_list;
155 char *sec_list;
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000156 char *cur_path, *cur_sect;
Denis Vlasenko9835d472008-07-04 21:57:11 +0000157 int count_mp, cur_mp;
Denis Vlasenkoce02b152008-06-27 22:05:21 +0000158 int opt, not_found;
Denis Vlasenko084266e2008-07-26 23:08:31 +0000159 char *token[2];
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000160
161 opt_complementary = "-1"; /* at least one argument */
162 opt = getopt32(argv, "+aw");
163 argv += optind;
164
John Spencerd6ae4fb2013-08-25 17:38:25 -0400165 sec_list = xstrdup("0p:1:1p:2:3:3p:4:5:6:7:8:9");
Denis Vlasenko9835d472008-07-04 21:57:11 +0000166 /* Last valid man_path_list[] is [0x10] */
Denis Vlasenkoa1e16c92008-09-14 21:30:35 +0000167 count_mp = 0;
Denis Vlasenko9835d472008-07-04 21:57:11 +0000168 man_path_list = xzalloc(0x11 * sizeof(man_path_list[0]));
Denis Vlasenkoe357d2a2008-09-14 21:26:55 +0000169 man_path_list[0] = getenv("MANPATH");
Denis Vlasenkoa1e16c92008-09-14 21:30:35 +0000170 if (!man_path_list[0]) /* default, may be overridden by /etc/man.conf */
Denis Vlasenkoe357d2a2008-09-14 21:26:55 +0000171 man_path_list[0] = (char*)"/usr/man";
Denis Vlasenkoa1e16c92008-09-14 21:30:35 +0000172 else
173 count_mp++;
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000174 pager = getenv("MANPAGER");
175 if (!pager) {
176 pager = getenv("PAGER");
177 if (!pager)
178 pager = "more";
179 }
180
Denys Vlasenko9a1b2602010-10-04 14:19:59 +0200181 /* Parse man.conf[ig] or man_db.conf */
Denys Vlasenko0e1b6262009-07-24 02:28:12 +0200182 /* man version 1.6f uses man.config */
Denys Vlasenko9a1b2602010-10-04 14:19:59 +0200183 /* man-db implementation of man uses man_db.conf */
Denys Vlasenko0e1b6262009-07-24 02:28:12 +0200184 parser = config_open2("/etc/man.config", fopen_for_read);
185 if (!parser)
186 parser = config_open2("/etc/man.conf", fopen_for_read);
Denys Vlasenko9a1b2602010-10-04 14:19:59 +0200187 if (!parser)
188 parser = config_open2("/etc/man_db.conf", fopen_for_read);
Denys Vlasenko0e1b6262009-07-24 02:28:12 +0200189
Denis Vlasenko94d03f02008-07-26 23:16:33 +0000190 while (config_read(parser, token, 2, 0, "# \t", PARSE_NORMAL)) {
191 if (!token[1])
192 continue;
Denys Vlasenko9a1b2602010-10-04 14:19:59 +0200193 if (strcmp("MANDATORY_MANPATH"+10, token[0]) == 0 /* "MANPATH"? */
194 || strcmp("MANDATORY_MANPATH", token[0]) == 0
195 ) {
Denis Vlasenkoe88bd2d2008-11-16 04:05:13 +0000196 char *path = token[1];
197 while (*path) {
198 char *next_path;
199 char **path_element;
200
201 next_path = strchr(path, ':');
202 if (next_path) {
203 *next_path = '\0';
204 if (next_path++ == path) /* "::"? */
205 goto next;
206 }
207 /* Do we already have path? */
208 path_element = man_path_list;
209 while (*path_element) {
210 if (strcmp(*path_element, path) == 0)
211 goto skip;
212 path_element++;
213 }
214 man_path_list = xrealloc_vector(man_path_list, 4, count_mp);
215 man_path_list[count_mp] = xstrdup(path);
216 count_mp++;
217 /* man_path_list is NULL terminated */
218 /*man_path_list[count_mp] = NULL; - xrealloc_vector did it */
219 skip:
220 if (!next_path)
221 break;
222 next:
223 path = next_path;
Denis Vlasenkoe357d2a2008-09-14 21:26:55 +0000224 }
Denis Vlasenko94d03f02008-07-26 23:16:33 +0000225 }
226 if (strcmp("MANSECT", token[0]) == 0) {
227 free(sec_list);
228 sec_list = xstrdup(token[1]);
229 }
230 }
231 config_close(parser);
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000232
Denis Vlasenkoce02b152008-06-27 22:05:21 +0000233 not_found = 0;
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000234 do { /* for each argv[] */
Denis Vlasenkoce02b152008-06-27 22:05:21 +0000235 int found = 0;
Denis Vlasenko50d068c2008-04-19 03:42:47 +0000236 cur_mp = 0;
Denis Vlasenko0e7f28d2008-08-05 15:28:05 +0000237
238 if (strchr(*argv, '/')) {
239 found = show_manpage(pager, *argv, /*man:*/ 1, 0);
240 goto check_found;
241 }
Denis Vlasenko50d068c2008-04-19 03:42:47 +0000242 while ((cur_path = man_path_list[cur_mp++]) != NULL) {
243 /* for each MANPATH */
Denis Vlasenkoe88bd2d2008-11-16 04:05:13 +0000244 cur_sect = sec_list;
245 do { /* for each section */
246 char *next_sect = strchrnul(cur_sect, ':');
247 int sect_len = next_sect - cur_sect;
248 char *man_filename;
249 int cat0man1 = 0;
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000250
Denis Vlasenkoe88bd2d2008-11-16 04:05:13 +0000251 /* Search for cat, then man page */
252 while (cat0man1 < 2) {
253 int found_here;
Denys Vlasenko59655072012-03-06 16:23:50 +0100254 man_filename = xasprintf("%s/%s%.*s/%s.%.*s",
Denis Vlasenkoe88bd2d2008-11-16 04:05:13 +0000255 cur_path,
256 "cat\0man" + (cat0man1 * 4),
257 sect_len, cur_sect,
258 *argv,
259 sect_len, cur_sect);
260 found_here = show_manpage(pager, man_filename, cat0man1, 0);
261 found |= found_here;
262 cat0man1 += found_here + 1;
263 free(man_filename);
264 }
Denis Vlasenko4cbffc02008-07-04 21:58:00 +0000265
Denis Vlasenkoe88bd2d2008-11-16 04:05:13 +0000266 if (found && !(opt & OPT_a))
267 goto next_arg;
268 cur_sect = next_sect;
269 while (*cur_sect == ':')
270 cur_sect++;
271 } while (*cur_sect);
Denis Vlasenko50d068c2008-04-19 03:42:47 +0000272 }
Denis Vlasenko0e7f28d2008-08-05 15:28:05 +0000273 check_found:
Denis Vlasenkoce02b152008-06-27 22:05:21 +0000274 if (!found) {
275 bb_error_msg("no manual entry for '%s'", *argv);
276 not_found = 1;
277 }
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000278 next_arg:
279 argv++;
280 } while (*argv);
281
Denis Vlasenkoce02b152008-06-27 22:05:21 +0000282 return not_found;
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000283}