man: fix breakage: must not die on lines with < 2 tokens
[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 static int run_pipe(const char *unpacker, const char *pager, char *man_filename, int man)
31 {
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
42         /* "2>&1" is added so that nroff errors are shown in pager too.
43          * Otherwise it may show just empty screen */
44         cmd = xasprintf(
45                 man ? "%s '%s' | gtbl | nroff -Tlatin1 -mandoc 2>&1 | %s"
46                     : "%s '%s' | %s",
47                 unpacker, man_filename, pager);
48         system(cmd);
49         free(cmd);
50         return 1;
51 }
52
53 /* man_filename is of the form "/dir/dir/dir/name.s.bz2" */
54 static int show_manpage(const char *pager, char *man_filename, int man)
55 {
56         int len;
57
58         if (run_pipe("bunzip2 -c", pager, man_filename, man))
59                 return 1;
60
61         len = strlen(man_filename) - 1;
62
63         man_filename[len] = '\0'; /* ".bz2" -> ".gz" */
64         man_filename[len - 2] = 'g';
65         if (run_pipe("gunzip -c", pager, man_filename, man))
66                 return 1;
67
68         man_filename[len - 3] = '\0'; /* ".gz" -> "" */
69         if (run_pipe(STR_cat, pager, man_filename, man))
70                 return 1;
71
72         return 0;
73 }
74
75 int man_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
76 int man_main(int argc UNUSED_PARAM, char **argv)
77 {
78         parser_t *parser;
79         const char *pager;
80         char **man_path_list;
81         char *sec_list;
82         char *cur_path, *cur_sect;
83         int count_mp, cur_mp;
84         int opt, not_found;
85
86         opt_complementary = "-1"; /* at least one argument */
87         opt = getopt32(argv, "+aw");
88         argv += optind;
89
90         sec_list = xstrdup("1:2:3:4:5:6:7:8:9");
91         /* Last valid man_path_list[] is [0x10] */
92         man_path_list = xzalloc(0x11 * sizeof(man_path_list[0]));
93         count_mp = 0;
94         man_path_list[0] = xstrdup(getenv("MANPATH"));
95         if (man_path_list[0])
96                 count_mp++;
97         pager = getenv("MANPAGER");
98         if (!pager) {
99                 pager = getenv("PAGER");
100                 if (!pager)
101                         pager = "more";
102         }
103
104         /* Parse man.conf */
105         parser = config_open("/etc/man.conf");
106         if (parser) {
107                 /* go through man configuration file and search relevant paths, sections */
108                 char *token[2];
109                 while (config_read(parser, token, 2, 0, "# \t", PARSE_LAST_IS_GREEDY)) {
110                         if (!token[1])
111                                 continue;
112                         if (strcmp("MANPATH", token[0]) == 0) {
113                                 man_path_list[count_mp] = xstrdup(token[1]);
114                                 count_mp++;
115                                 /* man_path_list is NULL terminated */
116                                 man_path_list[count_mp] = NULL;
117                                 man_path_list = xrealloc_vector(man_path_list, 4, count_mp);
118                         }
119                         if (strcmp("MANSECT", token[0]) == 0) {
120                                 free(sec_list);
121                                 sec_list = xstrdup(token[1]);
122                         }
123                 }
124                 config_close(parser);
125         }
126
127 // TODO: my man3/getpwuid.3.gz contains just one line:
128 // .so man3/getpwnam.3
129 // (and I _dont_ have man3/getpwnam.3, I have man3/getpwnam.3.gz)
130 // need to support this...
131
132         not_found = 0;
133         do { /* for each argv[] */
134                 int found = 0;
135                 cur_mp = 0;
136                 while ((cur_path = man_path_list[cur_mp++]) != NULL) {
137                         /* for each MANPATH */
138                         do { /* for each MANPATH item */
139                                 char *next_path = strchrnul(cur_path, ':');
140                                 int path_len = next_path - cur_path;
141                                 cur_sect = sec_list;
142                                 do { /* for each section */
143                                         char *next_sect = strchrnul(cur_sect, ':');
144                                         int sect_len = next_sect - cur_sect;
145                                         char *man_filename;
146                                         int cat0man1 = 0;
147
148                                         /* Search for cat, then man page */
149                                         while (cat0man1 < 2) {
150                                                 int found_here;
151                                                 man_filename = xasprintf("%.*s/%s%.*s/%s.%.*s" ".bz2",
152                                                                 path_len, cur_path,
153                                                                 STR_catNULmanNUL + cat0man1 * 4,
154                                                                 sect_len, cur_sect,
155                                                                 *argv,
156                                                                 sect_len, cur_sect);
157                                                 found_here = show_manpage(pager, man_filename, cat0man1);
158                                                 found |= found_here;
159                                                 cat0man1 += found_here + 1;
160                                                 free(man_filename);
161                                         }
162
163                                         if (found && !(opt & OPT_a))
164                                                 goto next_arg;
165                                         cur_sect = next_sect;
166                                         while (*cur_sect == ':')
167                                                 cur_sect++;
168                                 } while (*cur_sect);
169                                 cur_path = next_path;
170                                 while (*cur_path == ':')
171                                         cur_path++;
172                         } while (*cur_path);
173                 }
174                 if (!found) {
175                         bb_error_msg("no manual entry for '%s'", *argv);
176                         not_found = 1;
177                 }
178  next_arg:
179                 argv++;
180         } while (*argv);
181
182         return not_found;
183 }