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 static int run_pipe(const char *unpacker, const char *pager, char *man_filename)
30 if (access(man_filename, R_OK) != 0)
33 if (option_mask32 & OPT_w) {
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);
47 /* man_filename is of the form "/dir/dir/dir/name.s.bz2" */
48 static int show_manpage(const char *pager, char *man_filename)
52 if (run_pipe("bunzip2 -c", pager, man_filename))
55 len = strlen(man_filename) - 1;
57 man_filename[len] = '\0'; /* ".bz2" -> ".gz" */
58 man_filename[len - 2] = 'g';
59 if (run_pipe("gunzip -c", pager, man_filename))
62 man_filename[len - 3] = '\0'; /* ".gz" -> "" */
63 if (run_pipe("cat", pager, man_filename))
69 int man_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
70 int man_main(int argc ATTRIBUTE_UNUSED, char **argv)
76 char *cur_path, *cur_sect;
81 opt_complementary = "-1"; /* at least one argument */
82 opt = getopt32(argv, "+aw");
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]));
89 man_path_list[0] = xstrdup(getenv("MANPATH"));
92 pager = getenv("MANPAGER");
94 pager = getenv("PAGER");
100 cf = fopen_or_warn("/etc/man.conf", "r");
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])) {
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);
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]));
120 if (strcmp("MANSECT", line) == 0) {
122 sec_list = xstrdup(value);
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...
136 do { /* for each argv[] */
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;
145 do { /* for each section */
146 char *next_sect = strchrnul(cur_sect, ':');
147 int sect_len = next_sect - cur_sect;
149 char *man_filename = xasprintf("%.*s/man%.*s/%s.%.*s" ".bz2",
154 found |= show_manpage(pager, man_filename);
156 if (found && !(opt & OPT_a))
158 cur_sect = next_sect;
159 while (*cur_sect == ':')
162 cur_path = next_path;
163 while (*cur_path == ':')
168 bb_error_msg("no manual entry for '%s'", *argv);