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