blob: 5c8e2a4e5b32ca804bcb817f29864232a446b382 [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 Vlasenkofec8b422008-07-05 08:38:41 +000026/* Trick gcc to reuse "cat" string. */
27#define STR_catNULmanNUL "cat\0man"
28#define STR_cat "cat\0man"
29
Denis Vlasenkof6efccc2008-07-05 08:50:08 +000030static int run_pipe(const char *unpacker, const char *pager, char *man_filename, int man)
Denis Vlasenkoffa44992008-04-13 08:20:00 +000031{
32 char *cmd;
33
34 if (access(man_filename, R_OK) != 0)
35 return 0;
36
37 if (option_mask32 & OPT_w) {
38 puts(man_filename);
39 return 1;
40 }
41
Denis Vlasenko4cbffc02008-07-04 21:58:00 +000042 /* "2>&1" is added so that nroff errors are shown in pager too.
Denis Vlasenkob75fe792008-06-27 22:31:07 +000043 * Otherwise it may show just empty screen */
Denis Vlasenkof6efccc2008-07-05 08:50:08 +000044 cmd = xasprintf(
45 man ? "%s '%s' | gtbl | nroff -Tlatin1 -mandoc 2>&1 | %s"
46 : "%s '%s' | %s",
47 unpacker, man_filename, pager);
Denis Vlasenkoffa44992008-04-13 08:20:00 +000048 system(cmd);
49 free(cmd);
50 return 1;
51}
52
53/* man_filename is of the form "/dir/dir/dir/name.s.bz2" */
Denis Vlasenkof6efccc2008-07-05 08:50:08 +000054static int show_manpage(const char *pager, char *man_filename, int man)
Denis Vlasenkoffa44992008-04-13 08:20:00 +000055{
56 int len;
57
Denis Vlasenkof6efccc2008-07-05 08:50:08 +000058 if (run_pipe("bunzip2 -c", pager, man_filename, man))
Denis Vlasenkoffa44992008-04-13 08:20:00 +000059 return 1;
60
61 len = strlen(man_filename) - 1;
62
63 man_filename[len] = '\0'; /* ".bz2" -> ".gz" */
64 man_filename[len - 2] = 'g';
Denis Vlasenkof6efccc2008-07-05 08:50:08 +000065 if (run_pipe("gunzip -c", pager, man_filename, man))
Denis Vlasenkoffa44992008-04-13 08:20:00 +000066 return 1;
67
68 man_filename[len - 3] = '\0'; /* ".gz" -> "" */
Denis Vlasenkof6efccc2008-07-05 08:50:08 +000069 if (run_pipe(STR_cat, pager, man_filename, man))
Denis Vlasenkoffa44992008-04-13 08:20:00 +000070 return 1;
71
72 return 0;
73}
74
75int man_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000076int man_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenkoffa44992008-04-13 08:20:00 +000077{
Denis Vlasenko09aaf782008-07-20 17:41:30 +000078 parser_t *parser;
Denis Vlasenkoffa44992008-04-13 08:20:00 +000079 const char *pager;
Denis Vlasenko50d068c2008-04-19 03:42:47 +000080 char **man_path_list;
81 char *sec_list;
Denis Vlasenkoffa44992008-04-13 08:20:00 +000082 char *cur_path, *cur_sect;
Denis Vlasenko9835d472008-07-04 21:57:11 +000083 int count_mp, cur_mp;
Denis Vlasenkoce02b152008-06-27 22:05:21 +000084 int opt, not_found;
Denis Vlasenko084266e2008-07-26 23:08:31 +000085 char *token[2];
Denis Vlasenkoffa44992008-04-13 08:20:00 +000086
87 opt_complementary = "-1"; /* at least one argument */
88 opt = getopt32(argv, "+aw");
89 argv += optind;
90
91 sec_list = xstrdup("1:2:3:4:5:6:7:8:9");
Denis Vlasenko9835d472008-07-04 21:57:11 +000092 /* Last valid man_path_list[] is [0x10] */
93 man_path_list = xzalloc(0x11 * sizeof(man_path_list[0]));
Denis Vlasenko50d068c2008-04-19 03:42:47 +000094 count_mp = 0;
95 man_path_list[0] = xstrdup(getenv("MANPATH"));
96 if (man_path_list[0])
97 count_mp++;
Denis Vlasenkoffa44992008-04-13 08:20:00 +000098 pager = getenv("MANPAGER");
99 if (!pager) {
100 pager = getenv("PAGER");
101 if (!pager)
102 pager = "more";
103 }
104
105 /* Parse man.conf */
Denis Vlasenko09aaf782008-07-20 17:41:30 +0000106 parser = config_open("/etc/man.conf");
Denis Vlasenkoc24033e2008-07-26 23:14:23 +0000107 while (config_read(parser, token, 2, 0, "# \t", PARSE_NORMAL)) {
108 if (!token[1])
109 continue;
110 if (strcmp("MANPATH", token[0]) == 0) {
111 man_path_list = xrealloc_vector(man_path_list, 4, count_mp);
112 man_path_list[count_mp] = xstrdup(token[1]);
113 count_mp++;
114 /* man_path_list is NULL terminated */
115 man_path_list[count_mp] = NULL;
116 }
117 if (strcmp("MANSECT", token[0]) == 0) {
118 free(sec_list);
119 sec_list = xstrdup(token[1]);
120 }
121 }
122 config_close(parser);
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000123
Denis Vlasenkob75fe792008-06-27 22:31:07 +0000124// TODO: my man3/getpwuid.3.gz contains just one line:
125// .so man3/getpwnam.3
126// (and I _dont_ have man3/getpwnam.3, I have man3/getpwnam.3.gz)
127// need to support this...
128
Denis Vlasenkoce02b152008-06-27 22:05:21 +0000129 not_found = 0;
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000130 do { /* for each argv[] */
Denis Vlasenkoce02b152008-06-27 22:05:21 +0000131 int found = 0;
Denis Vlasenko50d068c2008-04-19 03:42:47 +0000132 cur_mp = 0;
133 while ((cur_path = man_path_list[cur_mp++]) != NULL) {
134 /* for each MANPATH */
135 do { /* for each MANPATH item */
136 char *next_path = strchrnul(cur_path, ':');
137 int path_len = next_path - cur_path;
138 cur_sect = sec_list;
139 do { /* for each section */
140 char *next_sect = strchrnul(cur_sect, ':');
141 int sect_len = next_sect - cur_sect;
Denis Vlasenko4cbffc02008-07-04 21:58:00 +0000142 char *man_filename;
Denis Vlasenkofec8b422008-07-05 08:38:41 +0000143 int cat0man1 = 0;
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000144
Denis Vlasenkofec8b422008-07-05 08:38:41 +0000145 /* Search for cat, then man page */
146 while (cat0man1 < 2) {
147 int found_here;
Denis Vlasenko4cbffc02008-07-04 21:58:00 +0000148 man_filename = xasprintf("%.*s/%s%.*s/%s.%.*s" ".bz2",
Denis Vlasenkofec8b422008-07-05 08:38:41 +0000149 path_len, cur_path,
150 STR_catNULmanNUL + cat0man1 * 4,
151 sect_len, cur_sect,
152 *argv,
153 sect_len, cur_sect);
154 found_here = show_manpage(pager, man_filename, cat0man1);
155 found |= found_here;
156 cat0man1 += found_here + 1;
Denis Vlasenko4cbffc02008-07-04 21:58:00 +0000157 free(man_filename);
158 }
Denis Vlasenko4cbffc02008-07-04 21:58:00 +0000159
Denis Vlasenko50d068c2008-04-19 03:42:47 +0000160 if (found && !(opt & OPT_a))
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000161 goto next_arg;
Denis Vlasenko50d068c2008-04-19 03:42:47 +0000162 cur_sect = next_sect;
163 while (*cur_sect == ':')
164 cur_sect++;
165 } while (*cur_sect);
166 cur_path = next_path;
167 while (*cur_path == ':')
168 cur_path++;
169 } while (*cur_path);
170 }
Denis Vlasenkoce02b152008-06-27 22:05:21 +0000171 if (!found) {
172 bb_error_msg("no manual entry for '%s'", *argv);
173 not_found = 1;
174 }
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000175 next_arg:
176 argv++;
177 } while (*argv);
178
Denis Vlasenkoce02b152008-06-27 22:05:21 +0000179 return not_found;
Denis Vlasenkoffa44992008-04-13 08:20:00 +0000180}