Denis Vlasenko | ffa4499 | 2008-04-13 08:20:00 +0000 | [diff] [blame] | 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 | |
Denis Vlasenko | ce02b15 | 2008-06-27 22:05:21 +0000 | [diff] [blame] | 13 | /* This is what I see on my desktop system being executed: |
Denis Vlasenko | ffa4499 | 2008-04-13 08:20:00 +0000 | [diff] [blame] | 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 | |
Denis Vlasenko | fec8b42 | 2008-07-05 08:38:41 +0000 | [diff] [blame] | 26 | /* Trick gcc to reuse "cat" string. */ |
| 27 | #define STR_catNULmanNUL "cat\0man" |
| 28 | #define STR_cat "cat\0man" |
| 29 | |
Denis Vlasenko | 540baf6 | 2008-08-05 13:16:18 +0000 | [diff] [blame] | 30 | //TODO: make gz/bz2 support conditional on FEATURE_SEAMLESS_GZ/BZ2, |
| 31 | // add SEAMLESS_LZMA support |
| 32 | |
| 33 | static int show_manpage(const char *pager, char *man_filename, int man, int level); |
| 34 | |
| 35 | static int run_pipe(const char *unpacker, const char *pager, char *man_filename, int man, int level) |
Denis Vlasenko | ffa4499 | 2008-04-13 08:20:00 +0000 | [diff] [blame] | 36 | { |
| 37 | char *cmd; |
| 38 | |
Denis Vlasenko | 540baf6 | 2008-08-05 13:16:18 +0000 | [diff] [blame] | 39 | /* Prevent man page link loops */ |
| 40 | if (level > 10) |
| 41 | return 0; |
| 42 | |
Denis Vlasenko | ffa4499 | 2008-04-13 08:20:00 +0000 | [diff] [blame] | 43 | if (access(man_filename, R_OK) != 0) |
| 44 | return 0; |
| 45 | |
| 46 | if (option_mask32 & OPT_w) { |
| 47 | puts(man_filename); |
| 48 | return 1; |
| 49 | } |
| 50 | |
Denis Vlasenko | 540baf6 | 2008-08-05 13:16:18 +0000 | [diff] [blame] | 51 | if (man) { /* man page, not cat page */ |
| 52 | /* Test whether the man page is not a link to another one. */ |
| 53 | /* The link has the following on the first line: */ |
| 54 | /* ".so another_man_page" */ |
| 55 | struct stat sb; |
| 56 | char *line; |
| 57 | char *linkname, *p; |
| 58 | |
| 59 | /* On my system: |
| 60 | * man1/genhostid.1.gz: 203 bytes - smallest real manpage |
| 61 | * man2/path_resolution.2.gz: 114 bytes - largest link |
| 62 | */ |
| 63 | xstat(man_filename, &sb); |
| 64 | if (sb.st_size > 300) /* err on the safe side */ |
| 65 | goto ordinary_manpage; |
| 66 | |
| 67 | line = xmalloc_open_zipped_read_close(man_filename, NULL); |
| 68 | if (!line || strncmp(line, ".so ", 4) != 0) { |
| 69 | free(line); |
| 70 | goto ordinary_manpage; |
| 71 | } |
| 72 | /* Example: man2/path_resolution.2.gz contains |
| 73 | * ".so man7/path_resolution.7\n<junk>" |
| 74 | */ |
| 75 | *strchrnul(line, '\n') = '\0'; |
| 76 | linkname = p = skip_whitespace(&line[4]); |
Denis Vlasenko | 540baf6 | 2008-08-05 13:16:18 +0000 | [diff] [blame] | 77 | |
Denis Vlasenko | 22b0bd8 | 2008-08-05 13:45:22 +0000 | [diff] [blame^] | 78 | /* If link has no slashes, we just replace man page name. |
| 79 | * If link has slashes (however many), we go back *once*. |
| 80 | * ".so zzz/ggg/page.3" does NOT go back two levels. */ |
| 81 | p = strrchr(man_filename, '/'); |
| 82 | if (!p) |
| 83 | goto ordinary_manpage; |
| 84 | *p = '\0'; |
| 85 | if (strchr(linkname, '/')) { |
| 86 | p = strrchr(man_filename, '/'); |
| 87 | if (!p) |
Denis Vlasenko | 540baf6 | 2008-08-05 13:16:18 +0000 | [diff] [blame] | 88 | goto ordinary_manpage; |
Denis Vlasenko | 22b0bd8 | 2008-08-05 13:45:22 +0000 | [diff] [blame^] | 89 | *p = '\0'; |
Denis Vlasenko | 540baf6 | 2008-08-05 13:16:18 +0000 | [diff] [blame] | 90 | } |
Denis Vlasenko | 22b0bd8 | 2008-08-05 13:45:22 +0000 | [diff] [blame^] | 91 | |
| 92 | /* Links do not have .gz extensions, even if manpage |
| 93 | * is compressed */ |
Denis Vlasenko | 540baf6 | 2008-08-05 13:16:18 +0000 | [diff] [blame] | 94 | man_filename = xasprintf("%s/%s" ".bz2", man_filename, linkname); |
| 95 | free(line); |
| 96 | /* Note: we leak "new" man_filename string as well... */ |
| 97 | if (show_manpage(pager, man_filename, man, level + 1)) |
| 98 | return 1; |
Denis Vlasenko | 22b0bd8 | 2008-08-05 13:45:22 +0000 | [diff] [blame^] | 99 | /* else: show the link, it's better than nothing */ |
Denis Vlasenko | 540baf6 | 2008-08-05 13:16:18 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | ordinary_manpage: |
Denis Vlasenko | 4cbffc0 | 2008-07-04 21:58:00 +0000 | [diff] [blame] | 103 | /* "2>&1" is added so that nroff errors are shown in pager too. |
Denis Vlasenko | b75fe79 | 2008-06-27 22:31:07 +0000 | [diff] [blame] | 104 | * Otherwise it may show just empty screen */ |
Denis Vlasenko | f6efccc | 2008-07-05 08:50:08 +0000 | [diff] [blame] | 105 | cmd = xasprintf( |
Denis Vlasenko | 22b0bd8 | 2008-08-05 13:45:22 +0000 | [diff] [blame^] | 106 | man ? "%s '%s' 2>&1 | gtbl | nroff -Tlatin1 -mandoc 2>&1 | %s" |
| 107 | : "%s '%s' 2>&1 | %s", |
Denis Vlasenko | f6efccc | 2008-07-05 08:50:08 +0000 | [diff] [blame] | 108 | unpacker, man_filename, pager); |
Denis Vlasenko | ffa4499 | 2008-04-13 08:20:00 +0000 | [diff] [blame] | 109 | system(cmd); |
| 110 | free(cmd); |
| 111 | return 1; |
| 112 | } |
| 113 | |
| 114 | /* man_filename is of the form "/dir/dir/dir/name.s.bz2" */ |
Denis Vlasenko | 540baf6 | 2008-08-05 13:16:18 +0000 | [diff] [blame] | 115 | static int show_manpage(const char *pager, char *man_filename, int man, int level) |
Denis Vlasenko | ffa4499 | 2008-04-13 08:20:00 +0000 | [diff] [blame] | 116 | { |
| 117 | int len; |
| 118 | |
Denis Vlasenko | 540baf6 | 2008-08-05 13:16:18 +0000 | [diff] [blame] | 119 | if (run_pipe("bunzip2 -c", pager, man_filename, man, level)) |
Denis Vlasenko | ffa4499 | 2008-04-13 08:20:00 +0000 | [diff] [blame] | 120 | return 1; |
| 121 | |
| 122 | len = strlen(man_filename) - 1; |
| 123 | |
| 124 | man_filename[len] = '\0'; /* ".bz2" -> ".gz" */ |
| 125 | man_filename[len - 2] = 'g'; |
Denis Vlasenko | 540baf6 | 2008-08-05 13:16:18 +0000 | [diff] [blame] | 126 | if (run_pipe("gunzip -c", pager, man_filename, man, level)) |
Denis Vlasenko | ffa4499 | 2008-04-13 08:20:00 +0000 | [diff] [blame] | 127 | return 1; |
| 128 | |
| 129 | man_filename[len - 3] = '\0'; /* ".gz" -> "" */ |
Denis Vlasenko | 540baf6 | 2008-08-05 13:16:18 +0000 | [diff] [blame] | 130 | if (run_pipe(STR_cat, pager, man_filename, man, level)) |
Denis Vlasenko | ffa4499 | 2008-04-13 08:20:00 +0000 | [diff] [blame] | 131 | return 1; |
| 132 | |
| 133 | return 0; |
| 134 | } |
| 135 | |
| 136 | int man_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 137 | int man_main(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | ffa4499 | 2008-04-13 08:20:00 +0000 | [diff] [blame] | 138 | { |
Denis Vlasenko | 09aaf78 | 2008-07-20 17:41:30 +0000 | [diff] [blame] | 139 | parser_t *parser; |
Denis Vlasenko | ffa4499 | 2008-04-13 08:20:00 +0000 | [diff] [blame] | 140 | const char *pager; |
Denis Vlasenko | 50d068c | 2008-04-19 03:42:47 +0000 | [diff] [blame] | 141 | char **man_path_list; |
| 142 | char *sec_list; |
Denis Vlasenko | ffa4499 | 2008-04-13 08:20:00 +0000 | [diff] [blame] | 143 | char *cur_path, *cur_sect; |
Denis Vlasenko | 9835d47 | 2008-07-04 21:57:11 +0000 | [diff] [blame] | 144 | int count_mp, cur_mp; |
Denis Vlasenko | ce02b15 | 2008-06-27 22:05:21 +0000 | [diff] [blame] | 145 | int opt, not_found; |
Denis Vlasenko | 084266e | 2008-07-26 23:08:31 +0000 | [diff] [blame] | 146 | char *token[2]; |
Denis Vlasenko | ffa4499 | 2008-04-13 08:20:00 +0000 | [diff] [blame] | 147 | |
| 148 | opt_complementary = "-1"; /* at least one argument */ |
| 149 | opt = getopt32(argv, "+aw"); |
| 150 | argv += optind; |
| 151 | |
| 152 | sec_list = xstrdup("1:2:3:4:5:6:7:8:9"); |
Denis Vlasenko | 9835d47 | 2008-07-04 21:57:11 +0000 | [diff] [blame] | 153 | /* Last valid man_path_list[] is [0x10] */ |
| 154 | man_path_list = xzalloc(0x11 * sizeof(man_path_list[0])); |
Denis Vlasenko | 50d068c | 2008-04-19 03:42:47 +0000 | [diff] [blame] | 155 | count_mp = 0; |
| 156 | man_path_list[0] = xstrdup(getenv("MANPATH")); |
| 157 | if (man_path_list[0]) |
| 158 | count_mp++; |
Denis Vlasenko | ffa4499 | 2008-04-13 08:20:00 +0000 | [diff] [blame] | 159 | pager = getenv("MANPAGER"); |
| 160 | if (!pager) { |
| 161 | pager = getenv("PAGER"); |
| 162 | if (!pager) |
| 163 | pager = "more"; |
| 164 | } |
| 165 | |
| 166 | /* Parse man.conf */ |
Denis Vlasenko | 09aaf78 | 2008-07-20 17:41:30 +0000 | [diff] [blame] | 167 | parser = config_open("/etc/man.conf"); |
Denis Vlasenko | 94d03f0 | 2008-07-26 23:16:33 +0000 | [diff] [blame] | 168 | while (config_read(parser, token, 2, 0, "# \t", PARSE_NORMAL)) { |
| 169 | if (!token[1]) |
| 170 | continue; |
| 171 | if (strcmp("MANPATH", token[0]) == 0) { |
| 172 | man_path_list = xrealloc_vector(man_path_list, 4, count_mp); |
| 173 | man_path_list[count_mp] = xstrdup(token[1]); |
| 174 | count_mp++; |
| 175 | /* man_path_list is NULL terminated */ |
Denis Vlasenko | 2784228 | 2008-08-04 13:20:36 +0000 | [diff] [blame] | 176 | /*man_path_list[count_mp] = NULL; - xrealloc_vector did it */ |
Denis Vlasenko | 94d03f0 | 2008-07-26 23:16:33 +0000 | [diff] [blame] | 177 | } |
| 178 | if (strcmp("MANSECT", token[0]) == 0) { |
| 179 | free(sec_list); |
| 180 | sec_list = xstrdup(token[1]); |
| 181 | } |
| 182 | } |
| 183 | config_close(parser); |
Denis Vlasenko | ffa4499 | 2008-04-13 08:20:00 +0000 | [diff] [blame] | 184 | |
Denis Vlasenko | ce02b15 | 2008-06-27 22:05:21 +0000 | [diff] [blame] | 185 | not_found = 0; |
Denis Vlasenko | ffa4499 | 2008-04-13 08:20:00 +0000 | [diff] [blame] | 186 | do { /* for each argv[] */ |
Denis Vlasenko | ce02b15 | 2008-06-27 22:05:21 +0000 | [diff] [blame] | 187 | int found = 0; |
Denis Vlasenko | 50d068c | 2008-04-19 03:42:47 +0000 | [diff] [blame] | 188 | cur_mp = 0; |
| 189 | while ((cur_path = man_path_list[cur_mp++]) != NULL) { |
| 190 | /* for each MANPATH */ |
| 191 | do { /* for each MANPATH item */ |
| 192 | char *next_path = strchrnul(cur_path, ':'); |
| 193 | int path_len = next_path - cur_path; |
| 194 | cur_sect = sec_list; |
| 195 | do { /* for each section */ |
| 196 | char *next_sect = strchrnul(cur_sect, ':'); |
| 197 | int sect_len = next_sect - cur_sect; |
Denis Vlasenko | 4cbffc0 | 2008-07-04 21:58:00 +0000 | [diff] [blame] | 198 | char *man_filename; |
Denis Vlasenko | fec8b42 | 2008-07-05 08:38:41 +0000 | [diff] [blame] | 199 | int cat0man1 = 0; |
Denis Vlasenko | ffa4499 | 2008-04-13 08:20:00 +0000 | [diff] [blame] | 200 | |
Denis Vlasenko | fec8b42 | 2008-07-05 08:38:41 +0000 | [diff] [blame] | 201 | /* Search for cat, then man page */ |
| 202 | while (cat0man1 < 2) { |
| 203 | int found_here; |
Denis Vlasenko | 4cbffc0 | 2008-07-04 21:58:00 +0000 | [diff] [blame] | 204 | man_filename = xasprintf("%.*s/%s%.*s/%s.%.*s" ".bz2", |
Denis Vlasenko | fec8b42 | 2008-07-05 08:38:41 +0000 | [diff] [blame] | 205 | path_len, cur_path, |
| 206 | STR_catNULmanNUL + cat0man1 * 4, |
| 207 | sect_len, cur_sect, |
| 208 | *argv, |
| 209 | sect_len, cur_sect); |
Denis Vlasenko | 540baf6 | 2008-08-05 13:16:18 +0000 | [diff] [blame] | 210 | found_here = show_manpage(pager, man_filename, cat0man1, 0); |
Denis Vlasenko | fec8b42 | 2008-07-05 08:38:41 +0000 | [diff] [blame] | 211 | found |= found_here; |
| 212 | cat0man1 += found_here + 1; |
Denis Vlasenko | 4cbffc0 | 2008-07-04 21:58:00 +0000 | [diff] [blame] | 213 | free(man_filename); |
| 214 | } |
Denis Vlasenko | 4cbffc0 | 2008-07-04 21:58:00 +0000 | [diff] [blame] | 215 | |
Denis Vlasenko | 50d068c | 2008-04-19 03:42:47 +0000 | [diff] [blame] | 216 | if (found && !(opt & OPT_a)) |
Denis Vlasenko | ffa4499 | 2008-04-13 08:20:00 +0000 | [diff] [blame] | 217 | goto next_arg; |
Denis Vlasenko | 50d068c | 2008-04-19 03:42:47 +0000 | [diff] [blame] | 218 | cur_sect = next_sect; |
| 219 | while (*cur_sect == ':') |
| 220 | cur_sect++; |
| 221 | } while (*cur_sect); |
| 222 | cur_path = next_path; |
| 223 | while (*cur_path == ':') |
| 224 | cur_path++; |
| 225 | } while (*cur_path); |
| 226 | } |
Denis Vlasenko | ce02b15 | 2008-06-27 22:05:21 +0000 | [diff] [blame] | 227 | if (!found) { |
| 228 | bb_error_msg("no manual entry for '%s'", *argv); |
| 229 | not_found = 1; |
| 230 | } |
Denis Vlasenko | ffa4499 | 2008-04-13 08:20:00 +0000 | [diff] [blame] | 231 | next_arg: |
| 232 | argv++; |
| 233 | } while (*argv); |
| 234 | |
Denis Vlasenko | ce02b15 | 2008-06-27 22:05:21 +0000 | [diff] [blame] | 235 | return not_found; |
Denis Vlasenko | ffa4499 | 2008-04-13 08:20:00 +0000 | [diff] [blame] | 236 | } |