man: code shrink
[platform/upstream/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 cat)
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(cat ? "%s '%s' | %s"
45                         : "%s '%s' | gtbl | nroff -Tlatin1 -mandoc 2>&1 | %s",
46                         unpacker, man_filename, pager);
47         system(cmd);
48         free(cmd);
49         return 1;
50 }
51
52 /* man_filename is of the form "/dir/dir/dir/name.s.bz2" */
53 static int show_manpage(const char *pager, char *man_filename, int cat)
54 {
55         int len;
56
57         if (run_pipe("bunzip2 -c", pager, man_filename, cat))
58                 return 1;
59
60         len = strlen(man_filename) - 1;
61
62         man_filename[len] = '\0'; /* ".bz2" -> ".gz" */
63         man_filename[len - 2] = 'g';
64         if (run_pipe("gunzip -c", pager, man_filename, cat))
65                 return 1;
66
67         man_filename[len - 3] = '\0'; /* ".gz" -> "" */
68         if (run_pipe(STR_cat, pager, man_filename, cat))
69                 return 1;
70
71         return 0;
72 }
73
74 int man_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
75 int man_main(int argc ATTRIBUTE_UNUSED, char **argv)
76 {
77         FILE *cf;
78         const char *pager;
79         char **man_path_list;
80         char *sec_list;
81         char *cur_path, *cur_sect;
82         char *line, *value;
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         cf = fopen_or_warn("/etc/man.conf", "r");
106         if (cf) {
107                 /* go through man configuration file and search relevant paths, sections */
108                 while ((line = xmalloc_fgetline(cf)) != NULL) {
109                         trim(line); /* remove whitespace at the beginning/end */
110                         if (isspace(line[7])) {
111                                 line[7] = '\0';
112                                 value = skip_whitespace(&line[8]);
113                                 *skip_non_whitespace(value) = '\0';
114                                 if (strcmp("MANPATH", line) == 0) {
115                                         man_path_list[count_mp] = xstrdup(value);
116                                         count_mp++;
117                                         /* man_path_list is NULL terminated */
118                                         man_path_list[count_mp] = NULL;
119                                         if (!(count_mp & 0xf)) { /* 0x10, 0x20 etc */
120                                                 /* so that last valid man_path_list[] is [count_mp + 0x10] */
121                                                 man_path_list = xrealloc(man_path_list,
122                                                         (count_mp + 0x11) * sizeof(man_path_list[0]));
123                                         }
124                                 }
125                                 if (strcmp("MANSECT", line) == 0) {
126                                         free(sec_list);
127                                         sec_list = xstrdup(value);
128                                 }
129                         }
130                         free(line);
131                 }
132                 fclose(cf);
133         }
134
135 // TODO: my man3/getpwuid.3.gz contains just one line:
136 // .so man3/getpwnam.3
137 // (and I _dont_ have man3/getpwnam.3, I have man3/getpwnam.3.gz)
138 // need to support this...
139
140         not_found = 0;
141         do { /* for each argv[] */
142                 int found = 0;
143                 cur_mp = 0;
144                 while ((cur_path = man_path_list[cur_mp++]) != NULL) {
145                         /* for each MANPATH */
146                         do { /* for each MANPATH item */
147                                 char *next_path = strchrnul(cur_path, ':');
148                                 int path_len = next_path - cur_path;
149                                 cur_sect = sec_list;
150                                 do { /* for each section */
151                                         char *next_sect = strchrnul(cur_sect, ':');
152                                         int sect_len = next_sect - cur_sect;
153                                         char *man_filename;
154                                         int cat0man1 = 0;
155
156                                         /* Search for cat, then man page */
157                                         while (cat0man1 < 2) {
158                                                 int found_here;
159                                                 man_filename = xasprintf("%.*s/%s%.*s/%s.%.*s" ".bz2",
160                                                                 path_len, cur_path,
161                                                                 STR_catNULmanNUL + cat0man1 * 4,
162                                                                 sect_len, cur_sect,
163                                                                 *argv,
164                                                                 sect_len, cur_sect);
165                                                 found_here = show_manpage(pager, man_filename, cat0man1);
166                                                 found |= found_here;
167                                                 cat0man1 += found_here + 1;
168                                                 free(man_filename);
169                                         }
170
171                                         if (found && !(opt & OPT_a))
172                                                 goto next_arg;
173                                         cur_sect = next_sect;
174                                         while (*cur_sect == ':')
175                                                 cur_sect++;
176                                 } while (*cur_sect);
177                                 cur_path = next_path;
178                                 while (*cur_path == ':')
179                                         cur_path++;
180                         } while (*cur_path);
181                 }
182                 if (!found) {
183                         bb_error_msg("no manual entry for '%s'", *argv);
184                         not_found = 1;
185                 }
186  next_arg:
187                 argv++;
188         } while (*argv);
189
190         return not_found;
191 }