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.
10 OPT_w = 2, /* print path */
13 /* This is what I see on my desktop system being executed:
19 gunzip -c '/usr/man/man1/bzip2.1.gz'
22 ) | gtbl | nroff -Tlatin1 -mandoc | less
26 /* Trick gcc to reuse "cat" string. */
27 #define STR_catNULmanNUL "cat\0man"
28 #define STR_cat "cat\0man"
30 static int run_pipe(const char *unpacker, const char *pager, char *man_filename, int man)
34 if (access(man_filename, R_OK) != 0)
37 if (option_mask32 & OPT_w) {
42 /* "2>&1" is added so that nroff errors are shown in pager too.
43 * Otherwise it may show just empty screen */
45 man ? "%s '%s' | gtbl | nroff -Tlatin1 -mandoc 2>&1 | %s"
47 unpacker, man_filename, pager);
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)
58 if (run_pipe("bunzip2 -c", pager, man_filename, man))
61 len = strlen(man_filename) - 1;
63 man_filename[len] = '\0'; /* ".bz2" -> ".gz" */
64 man_filename[len - 2] = 'g';
65 if (run_pipe("gunzip -c", pager, man_filename, man))
68 man_filename[len - 3] = '\0'; /* ".gz" -> "" */
69 if (run_pipe(STR_cat, pager, man_filename, man))
75 int man_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
76 int man_main(int argc UNUSED_PARAM, char **argv)
82 char *cur_path, *cur_sect;
87 opt_complementary = "-1"; /* at least one argument */
88 opt = getopt32(argv, "+aw");
91 sec_list = xstrdup("1:2:3:4:5:6:7:8:9");
92 /* Last valid man_path_list[] is [0x10] */
93 man_path_list = xzalloc(0x11 * sizeof(man_path_list[0]));
95 man_path_list[0] = xstrdup(getenv("MANPATH"));
98 pager = getenv("MANPAGER");
100 pager = getenv("PAGER");
106 parser = config_open("/etc/man.conf");
107 while (config_read(parser, token, 2, 0, "# \t", PARSE_NORMAL)) {
110 if (strcmp("MANPATH", token[0]) == 0) {
111 man_path_list = xrealloc_vector(man_path_list, 4, count_mp);
112 man_path_list[count_mp] = xstrdup(token[1]);
114 /* man_path_list is NULL terminated */
115 /*man_path_list[count_mp] = NULL; - xrealloc_vector did it */
117 if (strcmp("MANSECT", token[0]) == 0) {
119 sec_list = xstrdup(token[1]);
122 config_close(parser);
124 // TODO: my man3/getpwuid.3.gz contains just one line:
125 // .so man3/getpwnam.3
126 // (and I _dont_ have man3/getpwnam.3, I have man3/getpwnam.3.gz)
127 // need to support this...
130 do { /* for each argv[] */
133 while ((cur_path = man_path_list[cur_mp++]) != NULL) {
134 /* for each MANPATH */
135 do { /* for each MANPATH item */
136 char *next_path = strchrnul(cur_path, ':');
137 int path_len = next_path - cur_path;
139 do { /* for each section */
140 char *next_sect = strchrnul(cur_sect, ':');
141 int sect_len = next_sect - cur_sect;
145 /* Search for cat, then man page */
146 while (cat0man1 < 2) {
148 man_filename = xasprintf("%.*s/%s%.*s/%s.%.*s" ".bz2",
150 STR_catNULmanNUL + cat0man1 * 4,
154 found_here = show_manpage(pager, man_filename, cat0man1);
156 cat0man1 += found_here + 1;
160 if (found && !(opt & OPT_a))
162 cur_sect = next_sect;
163 while (*cur_sect == ':')
166 cur_path = next_path;
167 while (*cur_path == ':')
172 bb_error_msg("no manual entry for '%s'", *argv);