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