stat: pointers are not 0L, they are NULL
[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, alloc_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         alloc_mp = 10;
87         man_path_list = xmalloc(10 * 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                                         if (alloc_mp == count_mp) {
113                                                 alloc_mp += 10;
114                                                 man_path_list = xrealloc(man_path_list, alloc_mp * sizeof(man_path_list[0]));
115                                         }
116                                         /* thus man_path_list is always NULL terminated */
117                                 }
118                                 if (strcmp("MANSECT", line) == 0) {
119                                         free(sec_list);
120                                         sec_list = xstrdup(value);
121                                 }
122                         }
123                         free(line);
124                 }
125                 fclose(cf);
126         }
127
128 // TODO: my man3/getpwuid.3.gz contains just one line:
129 // .so man3/getpwnam.3
130 // (and I _dont_ have man3/getpwnam.3, I have man3/getpwnam.3.gz)
131 // need to support this...
132
133         not_found = 0;
134         do { /* for each argv[] */
135                 int found = 0;
136                 cur_mp = 0;
137                 while ((cur_path = man_path_list[cur_mp++]) != NULL) {
138                         /* for each MANPATH */
139                         do { /* for each MANPATH item */
140                                 char *next_path = strchrnul(cur_path, ':');
141                                 int path_len = next_path - cur_path;
142                                 cur_sect = sec_list;
143                                 do { /* for each section */
144                                         char *next_sect = strchrnul(cur_sect, ':');
145                                         int sect_len = next_sect - cur_sect;
146
147                                         char *man_filename = xasprintf("%.*s/man%.*s/%s.%.*s" ".bz2",
148                                                                 path_len, cur_path,
149                                                                 sect_len, cur_sect,
150                                                                 *argv,
151                                                                 sect_len, cur_sect);
152                                         found |= show_manpage(pager, man_filename);
153                                         free(man_filename);
154                                         if (found && !(opt & OPT_a))
155                                                 goto next_arg;
156                                         cur_sect = next_sect;
157                                         while (*cur_sect == ':')
158                                                 cur_sect++;
159                                 } while (*cur_sect);
160                                 cur_path = next_path;
161                                 while (*cur_path == ':')
162                                         cur_path++;
163                         } while (*cur_path);
164                 }
165                 if (!found) {
166                         bb_error_msg("no manual entry for '%s'", *argv);
167                         not_found = 1;
168                 }
169  next_arg:
170                 argv++;
171         } while (*argv);
172
173         return not_found;
174 }