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> |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 3 | * Licensed under GPLv2, see file LICENSE in this source tree. |
Denis Vlasenko | ffa4499 | 2008-04-13 08:20:00 +0000 | [diff] [blame] | 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 | 0e7f28d | 2008-08-05 15:28:05 +0000 | [diff] [blame] | 26 | #if ENABLE_FEATURE_SEAMLESS_LZMA |
| 27 | #define Z_SUFFIX ".lzma" |
| 28 | #elif ENABLE_FEATURE_SEAMLESS_BZ2 |
| 29 | #define Z_SUFFIX ".bz2" |
| 30 | #elif ENABLE_FEATURE_SEAMLESS_GZ |
| 31 | #define Z_SUFFIX ".gz" |
| 32 | #else |
| 33 | #define Z_SUFFIX "" |
| 34 | #endif |
Denis Vlasenko | 540baf6 | 2008-08-05 13:16:18 +0000 | [diff] [blame] | 35 | |
| 36 | static int show_manpage(const char *pager, char *man_filename, int man, int level); |
| 37 | |
Denis Vlasenko | 0e7f28d | 2008-08-05 15:28:05 +0000 | [diff] [blame] | 38 | static int run_pipe(const char *pager, char *man_filename, int man, int level) |
Denis Vlasenko | ffa4499 | 2008-04-13 08:20:00 +0000 | [diff] [blame] | 39 | { |
| 40 | char *cmd; |
| 41 | |
Denis Vlasenko | 540baf6 | 2008-08-05 13:16:18 +0000 | [diff] [blame] | 42 | /* Prevent man page link loops */ |
| 43 | if (level > 10) |
| 44 | return 0; |
| 45 | |
Denis Vlasenko | ffa4499 | 2008-04-13 08:20:00 +0000 | [diff] [blame] | 46 | if (access(man_filename, R_OK) != 0) |
| 47 | return 0; |
| 48 | |
| 49 | if (option_mask32 & OPT_w) { |
| 50 | puts(man_filename); |
| 51 | return 1; |
| 52 | } |
| 53 | |
Denis Vlasenko | 540baf6 | 2008-08-05 13:16:18 +0000 | [diff] [blame] | 54 | if (man) { /* man page, not cat page */ |
Denis Vlasenko | e2a09de | 2008-08-05 14:15:19 +0000 | [diff] [blame] | 55 | /* Is this a link to another manpage? */ |
Denis Vlasenko | 540baf6 | 2008-08-05 13:16:18 +0000 | [diff] [blame] | 56 | /* The link has the following on the first line: */ |
| 57 | /* ".so another_man_page" */ |
Denis Vlasenko | e2a09de | 2008-08-05 14:15:19 +0000 | [diff] [blame] | 58 | |
Denis Vlasenko | 540baf6 | 2008-08-05 13:16:18 +0000 | [diff] [blame] | 59 | struct stat sb; |
| 60 | char *line; |
| 61 | char *linkname, *p; |
| 62 | |
| 63 | /* On my system: |
| 64 | * man1/genhostid.1.gz: 203 bytes - smallest real manpage |
| 65 | * man2/path_resolution.2.gz: 114 bytes - largest link |
| 66 | */ |
| 67 | xstat(man_filename, &sb); |
| 68 | if (sb.st_size > 300) /* err on the safe side */ |
| 69 | goto ordinary_manpage; |
| 70 | |
| 71 | line = xmalloc_open_zipped_read_close(man_filename, NULL); |
| 72 | if (!line || strncmp(line, ".so ", 4) != 0) { |
| 73 | free(line); |
| 74 | goto ordinary_manpage; |
| 75 | } |
| 76 | /* Example: man2/path_resolution.2.gz contains |
| 77 | * ".so man7/path_resolution.7\n<junk>" |
| 78 | */ |
| 79 | *strchrnul(line, '\n') = '\0'; |
Denis Vlasenko | e2a09de | 2008-08-05 14:15:19 +0000 | [diff] [blame] | 80 | linkname = skip_whitespace(&line[4]); |
Denis Vlasenko | 540baf6 | 2008-08-05 13:16:18 +0000 | [diff] [blame] | 81 | |
Denis Vlasenko | 22b0bd8 | 2008-08-05 13:45:22 +0000 | [diff] [blame] | 82 | /* If link has no slashes, we just replace man page name. |
| 83 | * If link has slashes (however many), we go back *once*. |
| 84 | * ".so zzz/ggg/page.3" does NOT go back two levels. */ |
| 85 | p = strrchr(man_filename, '/'); |
| 86 | if (!p) |
| 87 | goto ordinary_manpage; |
| 88 | *p = '\0'; |
| 89 | if (strchr(linkname, '/')) { |
| 90 | p = strrchr(man_filename, '/'); |
| 91 | if (!p) |
Denis Vlasenko | 540baf6 | 2008-08-05 13:16:18 +0000 | [diff] [blame] | 92 | goto ordinary_manpage; |
Denis Vlasenko | 22b0bd8 | 2008-08-05 13:45:22 +0000 | [diff] [blame] | 93 | *p = '\0'; |
Denis Vlasenko | 540baf6 | 2008-08-05 13:16:18 +0000 | [diff] [blame] | 94 | } |
Denis Vlasenko | 22b0bd8 | 2008-08-05 13:45:22 +0000 | [diff] [blame] | 95 | |
| 96 | /* Links do not have .gz extensions, even if manpage |
| 97 | * is compressed */ |
Denis Vlasenko | 0e7f28d | 2008-08-05 15:28:05 +0000 | [diff] [blame] | 98 | man_filename = xasprintf("%s/%s" Z_SUFFIX, man_filename, linkname); |
Denis Vlasenko | 540baf6 | 2008-08-05 13:16:18 +0000 | [diff] [blame] | 99 | free(line); |
| 100 | /* Note: we leak "new" man_filename string as well... */ |
| 101 | if (show_manpage(pager, man_filename, man, level + 1)) |
| 102 | return 1; |
Denis Vlasenko | 22b0bd8 | 2008-08-05 13:45:22 +0000 | [diff] [blame] | 103 | /* else: show the link, it's better than nothing */ |
Denis Vlasenko | 540baf6 | 2008-08-05 13:16:18 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | ordinary_manpage: |
Denis Vlasenko | 0e7f28d | 2008-08-05 15:28:05 +0000 | [diff] [blame] | 107 | close(STDIN_FILENO); |
| 108 | open_zipped(man_filename); /* guaranteed to use fd 0 (STDIN_FILENO) */ |
Denis Vlasenko | 4cbffc0 | 2008-07-04 21:58:00 +0000 | [diff] [blame] | 109 | /* "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] | 110 | * Otherwise it may show just empty screen */ |
Denis Vlasenko | f6efccc | 2008-07-05 08:50:08 +0000 | [diff] [blame] | 111 | cmd = xasprintf( |
Denis Vlasenko | 0e7f28d | 2008-08-05 15:28:05 +0000 | [diff] [blame] | 112 | man ? "gtbl | nroff -Tlatin1 -mandoc 2>&1 | %s" |
| 113 | : "%s", |
| 114 | pager); |
Denis Vlasenko | ffa4499 | 2008-04-13 08:20:00 +0000 | [diff] [blame] | 115 | system(cmd); |
| 116 | free(cmd); |
| 117 | return 1; |
| 118 | } |
| 119 | |
Denis Vlasenko | 0e7f28d | 2008-08-05 15:28:05 +0000 | [diff] [blame] | 120 | /* man_filename is of the form "/dir/dir/dir/name.s" Z_SUFFIX */ |
Denis Vlasenko | 540baf6 | 2008-08-05 13:16:18 +0000 | [diff] [blame] | 121 | 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] | 122 | { |
Denis Vlasenko | 0e7f28d | 2008-08-05 15:28:05 +0000 | [diff] [blame] | 123 | #if ENABLE_FEATURE_SEAMLESS_LZMA |
| 124 | if (run_pipe(pager, man_filename, man, level)) |
Denis Vlasenko | ffa4499 | 2008-04-13 08:20:00 +0000 | [diff] [blame] | 125 | return 1; |
Denis Vlasenko | 0e7f28d | 2008-08-05 15:28:05 +0000 | [diff] [blame] | 126 | #endif |
Denis Vlasenko | ffa4499 | 2008-04-13 08:20:00 +0000 | [diff] [blame] | 127 | |
Denis Vlasenko | 0e7f28d | 2008-08-05 15:28:05 +0000 | [diff] [blame] | 128 | #if ENABLE_FEATURE_SEAMLESS_BZ2 |
| 129 | #if ENABLE_FEATURE_SEAMLESS_LZMA |
| 130 | strcpy(strrchr(man_filename, '.') + 1, "bz2"); |
| 131 | #endif |
| 132 | if (run_pipe(pager, man_filename, man, level)) |
Denis Vlasenko | ffa4499 | 2008-04-13 08:20:00 +0000 | [diff] [blame] | 133 | return 1; |
Denis Vlasenko | 0e7f28d | 2008-08-05 15:28:05 +0000 | [diff] [blame] | 134 | #endif |
Denis Vlasenko | ffa4499 | 2008-04-13 08:20:00 +0000 | [diff] [blame] | 135 | |
Denis Vlasenko | 0e7f28d | 2008-08-05 15:28:05 +0000 | [diff] [blame] | 136 | #if ENABLE_FEATURE_SEAMLESS_GZ |
| 137 | #if ENABLE_FEATURE_SEAMLESS_LZMA || ENABLE_FEATURE_SEAMLESS_BZ2 |
| 138 | strcpy(strrchr(man_filename, '.') + 1, "gz"); |
| 139 | #endif |
| 140 | if (run_pipe(pager, man_filename, man, level)) |
| 141 | return 1; |
| 142 | #endif |
| 143 | |
| 144 | #if ENABLE_FEATURE_SEAMLESS_LZMA || ENABLE_FEATURE_SEAMLESS_BZ2 || ENABLE_FEATURE_SEAMLESS_GZ |
| 145 | *strrchr(man_filename, '.') = '\0'; |
| 146 | #endif |
| 147 | if (run_pipe(pager, man_filename, man, level)) |
Denis Vlasenko | ffa4499 | 2008-04-13 08:20:00 +0000 | [diff] [blame] | 148 | return 1; |
| 149 | |
| 150 | return 0; |
| 151 | } |
| 152 | |
| 153 | int man_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 154 | int man_main(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | ffa4499 | 2008-04-13 08:20:00 +0000 | [diff] [blame] | 155 | { |
Denis Vlasenko | 09aaf78 | 2008-07-20 17:41:30 +0000 | [diff] [blame] | 156 | parser_t *parser; |
Denis Vlasenko | ffa4499 | 2008-04-13 08:20:00 +0000 | [diff] [blame] | 157 | const char *pager; |
Denis Vlasenko | 50d068c | 2008-04-19 03:42:47 +0000 | [diff] [blame] | 158 | char **man_path_list; |
| 159 | char *sec_list; |
Denis Vlasenko | ffa4499 | 2008-04-13 08:20:00 +0000 | [diff] [blame] | 160 | char *cur_path, *cur_sect; |
Denis Vlasenko | 9835d47 | 2008-07-04 21:57:11 +0000 | [diff] [blame] | 161 | int count_mp, cur_mp; |
Denis Vlasenko | ce02b15 | 2008-06-27 22:05:21 +0000 | [diff] [blame] | 162 | int opt, not_found; |
Denis Vlasenko | 084266e | 2008-07-26 23:08:31 +0000 | [diff] [blame] | 163 | char *token[2]; |
Denis Vlasenko | ffa4499 | 2008-04-13 08:20:00 +0000 | [diff] [blame] | 164 | |
| 165 | opt_complementary = "-1"; /* at least one argument */ |
| 166 | opt = getopt32(argv, "+aw"); |
| 167 | argv += optind; |
| 168 | |
| 169 | sec_list = xstrdup("1:2:3:4:5:6:7:8:9"); |
Denis Vlasenko | 9835d47 | 2008-07-04 21:57:11 +0000 | [diff] [blame] | 170 | /* Last valid man_path_list[] is [0x10] */ |
Denis Vlasenko | a1e16c9 | 2008-09-14 21:30:35 +0000 | [diff] [blame] | 171 | count_mp = 0; |
Denis Vlasenko | 9835d47 | 2008-07-04 21:57:11 +0000 | [diff] [blame] | 172 | man_path_list = xzalloc(0x11 * sizeof(man_path_list[0])); |
Denis Vlasenko | e357d2a | 2008-09-14 21:26:55 +0000 | [diff] [blame] | 173 | man_path_list[0] = getenv("MANPATH"); |
Denis Vlasenko | a1e16c9 | 2008-09-14 21:30:35 +0000 | [diff] [blame] | 174 | if (!man_path_list[0]) /* default, may be overridden by /etc/man.conf */ |
Denis Vlasenko | e357d2a | 2008-09-14 21:26:55 +0000 | [diff] [blame] | 175 | man_path_list[0] = (char*)"/usr/man"; |
Denis Vlasenko | a1e16c9 | 2008-09-14 21:30:35 +0000 | [diff] [blame] | 176 | else |
| 177 | count_mp++; |
Denis Vlasenko | ffa4499 | 2008-04-13 08:20:00 +0000 | [diff] [blame] | 178 | pager = getenv("MANPAGER"); |
| 179 | if (!pager) { |
| 180 | pager = getenv("PAGER"); |
| 181 | if (!pager) |
| 182 | pager = "more"; |
| 183 | } |
| 184 | |
Denys Vlasenko | 9a1b260 | 2010-10-04 14:19:59 +0200 | [diff] [blame^] | 185 | /* Parse man.conf[ig] or man_db.conf */ |
Denys Vlasenko | 0e1b626 | 2009-07-24 02:28:12 +0200 | [diff] [blame] | 186 | /* man version 1.6f uses man.config */ |
Denys Vlasenko | 9a1b260 | 2010-10-04 14:19:59 +0200 | [diff] [blame^] | 187 | /* man-db implementation of man uses man_db.conf */ |
Denys Vlasenko | 0e1b626 | 2009-07-24 02:28:12 +0200 | [diff] [blame] | 188 | parser = config_open2("/etc/man.config", fopen_for_read); |
| 189 | if (!parser) |
| 190 | parser = config_open2("/etc/man.conf", fopen_for_read); |
Denys Vlasenko | 9a1b260 | 2010-10-04 14:19:59 +0200 | [diff] [blame^] | 191 | if (!parser) |
| 192 | parser = config_open2("/etc/man_db.conf", fopen_for_read); |
Denys Vlasenko | 0e1b626 | 2009-07-24 02:28:12 +0200 | [diff] [blame] | 193 | |
Denis Vlasenko | 94d03f0 | 2008-07-26 23:16:33 +0000 | [diff] [blame] | 194 | while (config_read(parser, token, 2, 0, "# \t", PARSE_NORMAL)) { |
| 195 | if (!token[1]) |
| 196 | continue; |
Denys Vlasenko | 9a1b260 | 2010-10-04 14:19:59 +0200 | [diff] [blame^] | 197 | if (strcmp("MANDATORY_MANPATH"+10, token[0]) == 0 /* "MANPATH"? */ |
| 198 | || strcmp("MANDATORY_MANPATH", token[0]) == 0 |
| 199 | ) { |
Denis Vlasenko | e88bd2d | 2008-11-16 04:05:13 +0000 | [diff] [blame] | 200 | char *path = token[1]; |
| 201 | while (*path) { |
| 202 | char *next_path; |
| 203 | char **path_element; |
| 204 | |
| 205 | next_path = strchr(path, ':'); |
| 206 | if (next_path) { |
| 207 | *next_path = '\0'; |
| 208 | if (next_path++ == path) /* "::"? */ |
| 209 | goto next; |
| 210 | } |
| 211 | /* Do we already have path? */ |
| 212 | path_element = man_path_list; |
| 213 | while (*path_element) { |
| 214 | if (strcmp(*path_element, path) == 0) |
| 215 | goto skip; |
| 216 | path_element++; |
| 217 | } |
| 218 | man_path_list = xrealloc_vector(man_path_list, 4, count_mp); |
| 219 | man_path_list[count_mp] = xstrdup(path); |
| 220 | count_mp++; |
| 221 | /* man_path_list is NULL terminated */ |
| 222 | /*man_path_list[count_mp] = NULL; - xrealloc_vector did it */ |
| 223 | skip: |
| 224 | if (!next_path) |
| 225 | break; |
| 226 | next: |
| 227 | path = next_path; |
Denis Vlasenko | e357d2a | 2008-09-14 21:26:55 +0000 | [diff] [blame] | 228 | } |
Denis Vlasenko | 94d03f0 | 2008-07-26 23:16:33 +0000 | [diff] [blame] | 229 | } |
| 230 | if (strcmp("MANSECT", token[0]) == 0) { |
| 231 | free(sec_list); |
| 232 | sec_list = xstrdup(token[1]); |
| 233 | } |
| 234 | } |
| 235 | config_close(parser); |
Denis Vlasenko | ffa4499 | 2008-04-13 08:20:00 +0000 | [diff] [blame] | 236 | |
Denis Vlasenko | ce02b15 | 2008-06-27 22:05:21 +0000 | [diff] [blame] | 237 | not_found = 0; |
Denis Vlasenko | ffa4499 | 2008-04-13 08:20:00 +0000 | [diff] [blame] | 238 | do { /* for each argv[] */ |
Denis Vlasenko | ce02b15 | 2008-06-27 22:05:21 +0000 | [diff] [blame] | 239 | int found = 0; |
Denis Vlasenko | 50d068c | 2008-04-19 03:42:47 +0000 | [diff] [blame] | 240 | cur_mp = 0; |
Denis Vlasenko | 0e7f28d | 2008-08-05 15:28:05 +0000 | [diff] [blame] | 241 | |
| 242 | if (strchr(*argv, '/')) { |
| 243 | found = show_manpage(pager, *argv, /*man:*/ 1, 0); |
| 244 | goto check_found; |
| 245 | } |
Denis Vlasenko | 50d068c | 2008-04-19 03:42:47 +0000 | [diff] [blame] | 246 | while ((cur_path = man_path_list[cur_mp++]) != NULL) { |
| 247 | /* for each MANPATH */ |
Denis Vlasenko | e88bd2d | 2008-11-16 04:05:13 +0000 | [diff] [blame] | 248 | cur_sect = sec_list; |
| 249 | do { /* for each section */ |
| 250 | char *next_sect = strchrnul(cur_sect, ':'); |
| 251 | int sect_len = next_sect - cur_sect; |
| 252 | char *man_filename; |
| 253 | int cat0man1 = 0; |
Denis Vlasenko | ffa4499 | 2008-04-13 08:20:00 +0000 | [diff] [blame] | 254 | |
Denis Vlasenko | e88bd2d | 2008-11-16 04:05:13 +0000 | [diff] [blame] | 255 | /* Search for cat, then man page */ |
| 256 | while (cat0man1 < 2) { |
| 257 | int found_here; |
| 258 | man_filename = xasprintf("%s/%s%.*s/%s.%.*s" Z_SUFFIX, |
| 259 | cur_path, |
| 260 | "cat\0man" + (cat0man1 * 4), |
| 261 | sect_len, cur_sect, |
| 262 | *argv, |
| 263 | sect_len, cur_sect); |
| 264 | found_here = show_manpage(pager, man_filename, cat0man1, 0); |
| 265 | found |= found_here; |
| 266 | cat0man1 += found_here + 1; |
| 267 | free(man_filename); |
| 268 | } |
Denis Vlasenko | 4cbffc0 | 2008-07-04 21:58:00 +0000 | [diff] [blame] | 269 | |
Denis Vlasenko | e88bd2d | 2008-11-16 04:05:13 +0000 | [diff] [blame] | 270 | if (found && !(opt & OPT_a)) |
| 271 | goto next_arg; |
| 272 | cur_sect = next_sect; |
| 273 | while (*cur_sect == ':') |
| 274 | cur_sect++; |
| 275 | } while (*cur_sect); |
Denis Vlasenko | 50d068c | 2008-04-19 03:42:47 +0000 | [diff] [blame] | 276 | } |
Denis Vlasenko | 0e7f28d | 2008-08-05 15:28:05 +0000 | [diff] [blame] | 277 | check_found: |
Denis Vlasenko | ce02b15 | 2008-06-27 22:05:21 +0000 | [diff] [blame] | 278 | if (!found) { |
| 279 | bb_error_msg("no manual entry for '%s'", *argv); |
| 280 | not_found = 1; |
| 281 | } |
Denis Vlasenko | ffa4499 | 2008-04-13 08:20:00 +0000 | [diff] [blame] | 282 | next_arg: |
| 283 | argv++; |
| 284 | } while (*argv); |
| 285 | |
Denis Vlasenko | ce02b15 | 2008-06-27 22:05:21 +0000 | [diff] [blame] | 286 | return not_found; |
Denis Vlasenko | ffa4499 | 2008-04-13 08:20:00 +0000 | [diff] [blame] | 287 | } |