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 | |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 6 | //usage:#define man_trivial_usage |
| 7 | //usage: "[-aw] [MANPAGE]..." |
| 8 | //usage:#define man_full_usage "\n\n" |
| 9 | //usage: "Format and display manual page\n" |
Denys Vlasenko | 6642676 | 2011-06-05 03:58:28 +0200 | [diff] [blame] | 10 | //usage: "\n -a Display all pages" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 11 | //usage: "\n -w Show page locations" |
Denys Vlasenko | 7c3c92c | 2016-10-31 01:52:18 +0100 | [diff] [blame^] | 12 | //usage: "\n" |
| 13 | //usage: "\n$COLUMNS overrides output width" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 14 | |
Denis Vlasenko | ffa4499 | 2008-04-13 08:20:00 +0000 | [diff] [blame] | 15 | #include "libbb.h" |
Denys Vlasenko | a92a749 | 2016-10-30 22:31:30 +0100 | [diff] [blame] | 16 | #include "common_bufsiz.h" |
Denis Vlasenko | ffa4499 | 2008-04-13 08:20:00 +0000 | [diff] [blame] | 17 | |
| 18 | enum { |
| 19 | OPT_a = 1, /* all */ |
| 20 | OPT_w = 2, /* print path */ |
| 21 | }; |
| 22 | |
Denis Vlasenko | ce02b15 | 2008-06-27 22:05:21 +0000 | [diff] [blame] | 23 | /* This is what I see on my desktop system being executed: |
Denis Vlasenko | ffa4499 | 2008-04-13 08:20:00 +0000 | [diff] [blame] | 24 | ( |
| 25 | echo ".ll 12.4i" |
| 26 | echo ".nr LL 12.4i" |
| 27 | echo ".pl 1100i" |
| 28 | gunzip -c '/usr/man/man1/bzip2.1.gz' |
| 29 | echo ".\\\"" |
| 30 | echo ".pl \n(nlu+10" |
| 31 | ) | gtbl | nroff -Tlatin1 -mandoc | less |
| 32 | |
Denys Vlasenko | a92a749 | 2016-10-30 22:31:30 +0100 | [diff] [blame] | 33 | On another system I see this: |
| 34 | |
| 35 | ... | tbl | nroff -mandoc -rLL=<NNN>n -rLT=<NNN>n -Tutf8 | less |
| 36 | |
| 37 | where <NNN> is screen width minus 5. |
| 38 | Replacing "DEFINE nroff nroff -mandoc" in /etc/man_db.conf |
| 39 | changes "nroff -mandoc" part; -rLL=<NNN>n, -rLT=<NNN>n and -Tutf8 parts are |
| 40 | appended to the user-specified command. |
| 41 | |
| 42 | Redirecting to a pipe or file sets GROFF_NO_SGR=1 to prevent color escapes, |
| 43 | and uses "col -b -p -x" instead of pager, this filters out backspace |
| 44 | and underscore tricks. |
Denis Vlasenko | ffa4499 | 2008-04-13 08:20:00 +0000 | [diff] [blame] | 45 | */ |
| 46 | |
Denys Vlasenko | a92a749 | 2016-10-30 22:31:30 +0100 | [diff] [blame] | 47 | struct globals { |
| 48 | const char *col; |
| 49 | const char *tbl; |
| 50 | const char *nroff; |
| 51 | const char *pager; |
| 52 | } FIX_ALIASING; |
| 53 | #define G (*(struct globals*)bb_common_bufsiz1) |
| 54 | #define INIT_G() do { \ |
| 55 | setup_common_bufsiz(); \ |
| 56 | G.col = "col"; \ |
| 57 | G.tbl = "tbl"; \ |
Denys Vlasenko | 7c3c92c | 2016-10-31 01:52:18 +0100 | [diff] [blame^] | 58 | /* replaced -Tlatin1 with -Tascii for non-UTF8 displays */ \ |
Denys Vlasenko | a92a749 | 2016-10-30 22:31:30 +0100 | [diff] [blame] | 59 | G.nroff = "nroff -mandoc -Tascii"; \ |
| 60 | G.pager = ENABLE_LESS ? "less" : "more"; \ |
| 61 | } while (0) |
Denis Vlasenko | 540baf6 | 2008-08-05 13:16:18 +0000 | [diff] [blame] | 62 | |
Denys Vlasenko | a92a749 | 2016-10-30 22:31:30 +0100 | [diff] [blame] | 63 | static int show_manpage(char *man_filename, int man, int level); |
| 64 | |
| 65 | static int run_pipe(char *man_filename, int man, int level) |
Denis Vlasenko | ffa4499 | 2008-04-13 08:20:00 +0000 | [diff] [blame] | 66 | { |
| 67 | char *cmd; |
| 68 | |
Denis Vlasenko | 540baf6 | 2008-08-05 13:16:18 +0000 | [diff] [blame] | 69 | /* Prevent man page link loops */ |
| 70 | if (level > 10) |
| 71 | return 0; |
| 72 | |
Denis Vlasenko | ffa4499 | 2008-04-13 08:20:00 +0000 | [diff] [blame] | 73 | if (access(man_filename, R_OK) != 0) |
| 74 | return 0; |
| 75 | |
| 76 | if (option_mask32 & OPT_w) { |
| 77 | puts(man_filename); |
| 78 | return 1; |
| 79 | } |
| 80 | |
Denis Vlasenko | 540baf6 | 2008-08-05 13:16:18 +0000 | [diff] [blame] | 81 | if (man) { /* man page, not cat page */ |
Denis Vlasenko | e2a09de | 2008-08-05 14:15:19 +0000 | [diff] [blame] | 82 | /* Is this a link to another manpage? */ |
Denis Vlasenko | 540baf6 | 2008-08-05 13:16:18 +0000 | [diff] [blame] | 83 | /* The link has the following on the first line: */ |
| 84 | /* ".so another_man_page" */ |
Denis Vlasenko | e2a09de | 2008-08-05 14:15:19 +0000 | [diff] [blame] | 85 | |
Denis Vlasenko | 540baf6 | 2008-08-05 13:16:18 +0000 | [diff] [blame] | 86 | struct stat sb; |
| 87 | char *line; |
| 88 | char *linkname, *p; |
| 89 | |
| 90 | /* On my system: |
| 91 | * man1/genhostid.1.gz: 203 bytes - smallest real manpage |
| 92 | * man2/path_resolution.2.gz: 114 bytes - largest link |
| 93 | */ |
| 94 | xstat(man_filename, &sb); |
| 95 | if (sb.st_size > 300) /* err on the safe side */ |
| 96 | goto ordinary_manpage; |
| 97 | |
| 98 | line = xmalloc_open_zipped_read_close(man_filename, NULL); |
Denys Vlasenko | 8dff01d | 2015-03-12 17:48:34 +0100 | [diff] [blame] | 99 | if (!line || !is_prefixed_with(line, ".so ")) { |
Denis Vlasenko | 540baf6 | 2008-08-05 13:16:18 +0000 | [diff] [blame] | 100 | free(line); |
| 101 | goto ordinary_manpage; |
| 102 | } |
| 103 | /* Example: man2/path_resolution.2.gz contains |
| 104 | * ".so man7/path_resolution.7\n<junk>" |
| 105 | */ |
| 106 | *strchrnul(line, '\n') = '\0'; |
Denis Vlasenko | e2a09de | 2008-08-05 14:15:19 +0000 | [diff] [blame] | 107 | linkname = skip_whitespace(&line[4]); |
Denis Vlasenko | 540baf6 | 2008-08-05 13:16:18 +0000 | [diff] [blame] | 108 | |
Denis Vlasenko | 22b0bd8 | 2008-08-05 13:45:22 +0000 | [diff] [blame] | 109 | /* If link has no slashes, we just replace man page name. |
| 110 | * If link has slashes (however many), we go back *once*. |
| 111 | * ".so zzz/ggg/page.3" does NOT go back two levels. */ |
| 112 | p = strrchr(man_filename, '/'); |
| 113 | if (!p) |
| 114 | goto ordinary_manpage; |
| 115 | *p = '\0'; |
| 116 | if (strchr(linkname, '/')) { |
| 117 | p = strrchr(man_filename, '/'); |
| 118 | if (!p) |
Denis Vlasenko | 540baf6 | 2008-08-05 13:16:18 +0000 | [diff] [blame] | 119 | goto ordinary_manpage; |
Denis Vlasenko | 22b0bd8 | 2008-08-05 13:45:22 +0000 | [diff] [blame] | 120 | *p = '\0'; |
Denis Vlasenko | 540baf6 | 2008-08-05 13:16:18 +0000 | [diff] [blame] | 121 | } |
Denis Vlasenko | 22b0bd8 | 2008-08-05 13:45:22 +0000 | [diff] [blame] | 122 | |
| 123 | /* Links do not have .gz extensions, even if manpage |
| 124 | * is compressed */ |
Denys Vlasenko | 5965507 | 2012-03-06 16:23:50 +0100 | [diff] [blame] | 125 | man_filename = xasprintf("%s/%s", man_filename, linkname); |
Denis Vlasenko | 540baf6 | 2008-08-05 13:16:18 +0000 | [diff] [blame] | 126 | free(line); |
| 127 | /* Note: we leak "new" man_filename string as well... */ |
Denys Vlasenko | a92a749 | 2016-10-30 22:31:30 +0100 | [diff] [blame] | 128 | if (show_manpage(man_filename, man, level + 1)) |
Denis Vlasenko | 540baf6 | 2008-08-05 13:16:18 +0000 | [diff] [blame] | 129 | return 1; |
Denis Vlasenko | 22b0bd8 | 2008-08-05 13:45:22 +0000 | [diff] [blame] | 130 | /* else: show the link, it's better than nothing */ |
Denis Vlasenko | 540baf6 | 2008-08-05 13:16:18 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | ordinary_manpage: |
Denis Vlasenko | 0e7f28d | 2008-08-05 15:28:05 +0000 | [diff] [blame] | 134 | close(STDIN_FILENO); |
Denys Vlasenko | 640ce3d | 2014-02-02 02:06:38 +0100 | [diff] [blame] | 135 | open_zipped(man_filename, /*fail_if_not_compressed:*/ 0); /* guaranteed to use fd 0 (STDIN_FILENO) */ |
Denys Vlasenko | a92a749 | 2016-10-30 22:31:30 +0100 | [diff] [blame] | 136 | if (man) { |
Denys Vlasenko | 7c3c92c | 2016-10-31 01:52:18 +0100 | [diff] [blame^] | 137 | int w = get_terminal_width(-1); |
Denys Vlasenko | a92a749 | 2016-10-30 22:31:30 +0100 | [diff] [blame] | 138 | if (w > 10) |
| 139 | w -= 2; |
| 140 | /* "2>&1" is added so that nroff errors are shown in pager too. |
Denys Vlasenko | 7c3c92c | 2016-10-31 01:52:18 +0100 | [diff] [blame^] | 141 | * Otherwise it may show just empty screen. |
| 142 | */ |
Denys Vlasenko | a92a749 | 2016-10-30 22:31:30 +0100 | [diff] [blame] | 143 | cmd = xasprintf("%s | %s -rLL=%un -rLT=%un 2>&1 | %s", |
| 144 | G.tbl, G.nroff, w, w, |
| 145 | G.pager); |
| 146 | } else { |
| 147 | cmd = xstrdup(G.pager); |
| 148 | } |
Denis Vlasenko | ffa4499 | 2008-04-13 08:20:00 +0000 | [diff] [blame] | 149 | system(cmd); |
| 150 | free(cmd); |
| 151 | return 1; |
| 152 | } |
| 153 | |
Denys Vlasenko | 5965507 | 2012-03-06 16:23:50 +0100 | [diff] [blame] | 154 | /* man_filename is of the form "/dir/dir/dir/name.s" */ |
Denys Vlasenko | a92a749 | 2016-10-30 22:31:30 +0100 | [diff] [blame] | 155 | static int show_manpage(char *man_filename, int man, int level) |
Denis Vlasenko | ffa4499 | 2008-04-13 08:20:00 +0000 | [diff] [blame] | 156 | { |
Denys Vlasenko | 5965507 | 2012-03-06 16:23:50 +0100 | [diff] [blame] | 157 | #if SEAMLESS_COMPRESSION |
| 158 | /* We leak this allocation... */ |
| 159 | char *filename_with_zext = xasprintf("%s.lzma", man_filename); |
| 160 | char *ext = strrchr(filename_with_zext, '.') + 1; |
| 161 | #endif |
| 162 | |
Denis Vlasenko | 0e7f28d | 2008-08-05 15:28:05 +0000 | [diff] [blame] | 163 | #if ENABLE_FEATURE_SEAMLESS_LZMA |
Denys Vlasenko | a92a749 | 2016-10-30 22:31:30 +0100 | [diff] [blame] | 164 | if (run_pipe(filename_with_zext, man, level)) |
Denys Vlasenko | 5965507 | 2012-03-06 16:23:50 +0100 | [diff] [blame] | 165 | return 1; |
| 166 | #endif |
| 167 | #if ENABLE_FEATURE_SEAMLESS_XZ |
| 168 | strcpy(ext, "xz"); |
Denys Vlasenko | a92a749 | 2016-10-30 22:31:30 +0100 | [diff] [blame] | 169 | if (run_pipe(filename_with_zext, man, level)) |
Denis Vlasenko | ffa4499 | 2008-04-13 08:20:00 +0000 | [diff] [blame] | 170 | return 1; |
Denis Vlasenko | 0e7f28d | 2008-08-05 15:28:05 +0000 | [diff] [blame] | 171 | #endif |
Denis Vlasenko | 0e7f28d | 2008-08-05 15:28:05 +0000 | [diff] [blame] | 172 | #if ENABLE_FEATURE_SEAMLESS_BZ2 |
Denys Vlasenko | 5965507 | 2012-03-06 16:23:50 +0100 | [diff] [blame] | 173 | strcpy(ext, "bz2"); |
Denys Vlasenko | a92a749 | 2016-10-30 22:31:30 +0100 | [diff] [blame] | 174 | if (run_pipe(filename_with_zext, man, level)) |
Denis Vlasenko | ffa4499 | 2008-04-13 08:20:00 +0000 | [diff] [blame] | 175 | return 1; |
Denis Vlasenko | 0e7f28d | 2008-08-05 15:28:05 +0000 | [diff] [blame] | 176 | #endif |
Denis Vlasenko | 0e7f28d | 2008-08-05 15:28:05 +0000 | [diff] [blame] | 177 | #if ENABLE_FEATURE_SEAMLESS_GZ |
Denys Vlasenko | 5965507 | 2012-03-06 16:23:50 +0100 | [diff] [blame] | 178 | strcpy(ext, "gz"); |
Denys Vlasenko | a92a749 | 2016-10-30 22:31:30 +0100 | [diff] [blame] | 179 | if (run_pipe(filename_with_zext, man, level)) |
Denis Vlasenko | 0e7f28d | 2008-08-05 15:28:05 +0000 | [diff] [blame] | 180 | return 1; |
| 181 | #endif |
| 182 | |
Denys Vlasenko | a92a749 | 2016-10-30 22:31:30 +0100 | [diff] [blame] | 183 | return run_pipe(man_filename, man, level); |
Denis Vlasenko | ffa4499 | 2008-04-13 08:20:00 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Denys Vlasenko | ee41094 | 2014-11-27 00:40:08 +0100 | [diff] [blame] | 186 | static char **add_MANPATH(char **man_path_list, int *count_mp, char *path) |
| 187 | { |
| 188 | if (path) while (*path) { |
| 189 | char *next_path; |
| 190 | char **path_element; |
| 191 | |
| 192 | next_path = strchr(path, ':'); |
| 193 | if (next_path) { |
Denys Vlasenko | 476654c | 2014-11-30 19:39:58 +0100 | [diff] [blame] | 194 | if (next_path == path) /* "::"? */ |
Denys Vlasenko | ee41094 | 2014-11-27 00:40:08 +0100 | [diff] [blame] | 195 | goto next; |
Denys Vlasenko | 476654c | 2014-11-30 19:39:58 +0100 | [diff] [blame] | 196 | *next_path = '\0'; |
Denys Vlasenko | ee41094 | 2014-11-27 00:40:08 +0100 | [diff] [blame] | 197 | } |
| 198 | /* Do we already have path? */ |
| 199 | path_element = man_path_list; |
| 200 | if (path_element) while (*path_element) { |
| 201 | if (strcmp(*path_element, path) == 0) |
| 202 | goto skip; |
| 203 | path_element++; |
| 204 | } |
| 205 | man_path_list = xrealloc_vector(man_path_list, 4, *count_mp); |
| 206 | man_path_list[*count_mp] = xstrdup(path); |
| 207 | (*count_mp)++; |
| 208 | /* man_path_list is NULL terminated */ |
| 209 | /* man_path_list[*count_mp] = NULL; - xrealloc_vector did it */ |
| 210 | skip: |
| 211 | if (!next_path) |
| 212 | break; |
Denys Vlasenko | 476654c | 2014-11-30 19:39:58 +0100 | [diff] [blame] | 213 | /* "path" may be a result of getenv(), be nice and don't mangle it */ |
| 214 | *next_path = ':'; |
Denys Vlasenko | ee41094 | 2014-11-27 00:40:08 +0100 | [diff] [blame] | 215 | next: |
Denys Vlasenko | 476654c | 2014-11-30 19:39:58 +0100 | [diff] [blame] | 216 | path = next_path + 1; |
Denys Vlasenko | ee41094 | 2014-11-27 00:40:08 +0100 | [diff] [blame] | 217 | } |
| 218 | return man_path_list; |
| 219 | } |
| 220 | |
Denys Vlasenko | a92a749 | 2016-10-30 22:31:30 +0100 | [diff] [blame] | 221 | static const char *if_redefined(const char *var, const char *key, const char *line) |
| 222 | { |
| 223 | if (!is_prefixed_with(line, key)) |
| 224 | return var; |
| 225 | line += strlen(key); |
| 226 | if (!isspace(line[0])) |
| 227 | return var; |
| 228 | return xstrdup(skip_whitespace(line)); |
| 229 | } |
| 230 | |
Denis Vlasenko | ffa4499 | 2008-04-13 08:20:00 +0000 | [diff] [blame] | 231 | int man_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 232 | int man_main(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | ffa4499 | 2008-04-13 08:20:00 +0000 | [diff] [blame] | 233 | { |
Denis Vlasenko | 09aaf78 | 2008-07-20 17:41:30 +0000 | [diff] [blame] | 234 | parser_t *parser; |
Denis Vlasenko | 50d068c | 2008-04-19 03:42:47 +0000 | [diff] [blame] | 235 | char *sec_list; |
Denis Vlasenko | ffa4499 | 2008-04-13 08:20:00 +0000 | [diff] [blame] | 236 | char *cur_path, *cur_sect; |
Denys Vlasenko | ee41094 | 2014-11-27 00:40:08 +0100 | [diff] [blame] | 237 | char **man_path_list; |
| 238 | int count_mp; |
| 239 | int cur_mp; |
Denis Vlasenko | ce02b15 | 2008-06-27 22:05:21 +0000 | [diff] [blame] | 240 | int opt, not_found; |
Denis Vlasenko | 084266e | 2008-07-26 23:08:31 +0000 | [diff] [blame] | 241 | char *token[2]; |
Denis Vlasenko | ffa4499 | 2008-04-13 08:20:00 +0000 | [diff] [blame] | 242 | |
Denys Vlasenko | a92a749 | 2016-10-30 22:31:30 +0100 | [diff] [blame] | 243 | INIT_G(); |
| 244 | |
Denis Vlasenko | ffa4499 | 2008-04-13 08:20:00 +0000 | [diff] [blame] | 245 | opt_complementary = "-1"; /* at least one argument */ |
| 246 | opt = getopt32(argv, "+aw"); |
| 247 | argv += optind; |
| 248 | |
John Spencer | d6ae4fb | 2013-08-25 17:38:25 -0400 | [diff] [blame] | 249 | sec_list = xstrdup("0p:1:1p:2:3:3p:4:5:6:7:8:9"); |
Denys Vlasenko | ee41094 | 2014-11-27 00:40:08 +0100 | [diff] [blame] | 250 | |
Denis Vlasenko | a1e16c9 | 2008-09-14 21:30:35 +0000 | [diff] [blame] | 251 | count_mp = 0; |
Denys Vlasenko | ee41094 | 2014-11-27 00:40:08 +0100 | [diff] [blame] | 252 | man_path_list = add_MANPATH(NULL, &count_mp, |
| 253 | getenv("MANDATORY_MANPATH"+10) /* "MANPATH" */ |
| 254 | ); |
| 255 | if (!man_path_list) { |
| 256 | /* default, may be overridden by /etc/man.conf */ |
| 257 | man_path_list = xzalloc(2 * sizeof(man_path_list[0])); |
Denis Vlasenko | e357d2a | 2008-09-14 21:26:55 +0000 | [diff] [blame] | 258 | man_path_list[0] = (char*)"/usr/man"; |
Denys Vlasenko | ee41094 | 2014-11-27 00:40:08 +0100 | [diff] [blame] | 259 | /* count_mp stays 0. |
| 260 | * Thus, man.conf will overwrite man_path_list[0] |
| 261 | * if a path is defined there. |
| 262 | */ |
| 263 | } |
Denis Vlasenko | ffa4499 | 2008-04-13 08:20:00 +0000 | [diff] [blame] | 264 | |
Denys Vlasenko | 9a1b260 | 2010-10-04 14:19:59 +0200 | [diff] [blame] | 265 | /* Parse man.conf[ig] or man_db.conf */ |
Denys Vlasenko | 0e1b626 | 2009-07-24 02:28:12 +0200 | [diff] [blame] | 266 | /* man version 1.6f uses man.config */ |
Denys Vlasenko | 9a1b260 | 2010-10-04 14:19:59 +0200 | [diff] [blame] | 267 | /* man-db implementation of man uses man_db.conf */ |
Denys Vlasenko | 0e1b626 | 2009-07-24 02:28:12 +0200 | [diff] [blame] | 268 | parser = config_open2("/etc/man.config", fopen_for_read); |
| 269 | if (!parser) |
| 270 | parser = config_open2("/etc/man.conf", fopen_for_read); |
Denys Vlasenko | 9a1b260 | 2010-10-04 14:19:59 +0200 | [diff] [blame] | 271 | if (!parser) |
| 272 | parser = config_open2("/etc/man_db.conf", fopen_for_read); |
Denys Vlasenko | 0e1b626 | 2009-07-24 02:28:12 +0200 | [diff] [blame] | 273 | |
Denis Vlasenko | 94d03f0 | 2008-07-26 23:16:33 +0000 | [diff] [blame] | 274 | while (config_read(parser, token, 2, 0, "# \t", PARSE_NORMAL)) { |
| 275 | if (!token[1]) |
| 276 | continue; |
John Spencer | ff65061 | 2014-01-22 15:31:10 +0100 | [diff] [blame] | 277 | if (strcmp("DEFINE", token[0]) == 0) { |
Denys Vlasenko | a92a749 | 2016-10-30 22:31:30 +0100 | [diff] [blame] | 278 | G.col = if_redefined(G.tbl , "col", token[1]); |
| 279 | G.tbl = if_redefined(G.tbl , "tbl", token[1]); |
| 280 | G.nroff = if_redefined(G.nroff, "nroff", token[1]); |
| 281 | G.pager = if_redefined(G.pager, "pager", token[1]); |
John Spencer | ff65061 | 2014-01-22 15:31:10 +0100 | [diff] [blame] | 282 | } else |
Denys Vlasenko | 9a1b260 | 2010-10-04 14:19:59 +0200 | [diff] [blame] | 283 | if (strcmp("MANDATORY_MANPATH"+10, token[0]) == 0 /* "MANPATH"? */ |
| 284 | || strcmp("MANDATORY_MANPATH", token[0]) == 0 |
| 285 | ) { |
Denys Vlasenko | ee41094 | 2014-11-27 00:40:08 +0100 | [diff] [blame] | 286 | man_path_list = add_MANPATH(man_path_list, &count_mp, token[1]); |
Denis Vlasenko | 94d03f0 | 2008-07-26 23:16:33 +0000 | [diff] [blame] | 287 | } |
| 288 | if (strcmp("MANSECT", token[0]) == 0) { |
| 289 | free(sec_list); |
| 290 | sec_list = xstrdup(token[1]); |
| 291 | } |
| 292 | } |
| 293 | config_close(parser); |
Denis Vlasenko | ffa4499 | 2008-04-13 08:20:00 +0000 | [diff] [blame] | 294 | |
John Spencer | ff65061 | 2014-01-22 15:31:10 +0100 | [diff] [blame] | 295 | { |
| 296 | /* environment overrides setting from man.config */ |
| 297 | char *env_pager = getenv("MANPAGER"); |
| 298 | if (!env_pager) |
| 299 | env_pager = getenv("PAGER"); |
| 300 | if (env_pager) |
Denys Vlasenko | a92a749 | 2016-10-30 22:31:30 +0100 | [diff] [blame] | 301 | G.pager = env_pager; |
| 302 | } |
| 303 | |
| 304 | if (!isatty(STDOUT_FILENO)) { |
| 305 | putenv((char*)"GROFF_NO_SGR=1"); |
| 306 | G.pager = xasprintf("%s -b -p -x", G.col); |
John Spencer | ff65061 | 2014-01-22 15:31:10 +0100 | [diff] [blame] | 307 | } |
| 308 | |
Denis Vlasenko | ce02b15 | 2008-06-27 22:05:21 +0000 | [diff] [blame] | 309 | not_found = 0; |
Denis Vlasenko | ffa4499 | 2008-04-13 08:20:00 +0000 | [diff] [blame] | 310 | do { /* for each argv[] */ |
Denis Vlasenko | ce02b15 | 2008-06-27 22:05:21 +0000 | [diff] [blame] | 311 | int found = 0; |
Denis Vlasenko | 50d068c | 2008-04-19 03:42:47 +0000 | [diff] [blame] | 312 | cur_mp = 0; |
Denis Vlasenko | 0e7f28d | 2008-08-05 15:28:05 +0000 | [diff] [blame] | 313 | |
| 314 | if (strchr(*argv, '/')) { |
Denys Vlasenko | a92a749 | 2016-10-30 22:31:30 +0100 | [diff] [blame] | 315 | found = show_manpage(*argv, /*man:*/ 1, 0); |
Denis Vlasenko | 0e7f28d | 2008-08-05 15:28:05 +0000 | [diff] [blame] | 316 | goto check_found; |
| 317 | } |
Denis Vlasenko | 50d068c | 2008-04-19 03:42:47 +0000 | [diff] [blame] | 318 | while ((cur_path = man_path_list[cur_mp++]) != NULL) { |
| 319 | /* for each MANPATH */ |
Denis Vlasenko | e88bd2d | 2008-11-16 04:05:13 +0000 | [diff] [blame] | 320 | cur_sect = sec_list; |
| 321 | do { /* for each section */ |
| 322 | char *next_sect = strchrnul(cur_sect, ':'); |
| 323 | int sect_len = next_sect - cur_sect; |
| 324 | char *man_filename; |
| 325 | int cat0man1 = 0; |
Denis Vlasenko | ffa4499 | 2008-04-13 08:20:00 +0000 | [diff] [blame] | 326 | |
Denis Vlasenko | e88bd2d | 2008-11-16 04:05:13 +0000 | [diff] [blame] | 327 | /* Search for cat, then man page */ |
| 328 | while (cat0man1 < 2) { |
| 329 | int found_here; |
Denys Vlasenko | 5965507 | 2012-03-06 16:23:50 +0100 | [diff] [blame] | 330 | man_filename = xasprintf("%s/%s%.*s/%s.%.*s", |
Denis Vlasenko | e88bd2d | 2008-11-16 04:05:13 +0000 | [diff] [blame] | 331 | cur_path, |
| 332 | "cat\0man" + (cat0man1 * 4), |
| 333 | sect_len, cur_sect, |
| 334 | *argv, |
| 335 | sect_len, cur_sect); |
Denys Vlasenko | a92a749 | 2016-10-30 22:31:30 +0100 | [diff] [blame] | 336 | found_here = show_manpage(man_filename, cat0man1, 0); |
Denis Vlasenko | e88bd2d | 2008-11-16 04:05:13 +0000 | [diff] [blame] | 337 | found |= found_here; |
| 338 | cat0man1 += found_here + 1; |
| 339 | free(man_filename); |
| 340 | } |
Denis Vlasenko | 4cbffc0 | 2008-07-04 21:58:00 +0000 | [diff] [blame] | 341 | |
Denis Vlasenko | e88bd2d | 2008-11-16 04:05:13 +0000 | [diff] [blame] | 342 | if (found && !(opt & OPT_a)) |
| 343 | goto next_arg; |
| 344 | cur_sect = next_sect; |
| 345 | while (*cur_sect == ':') |
| 346 | cur_sect++; |
| 347 | } while (*cur_sect); |
Denis Vlasenko | 50d068c | 2008-04-19 03:42:47 +0000 | [diff] [blame] | 348 | } |
Denis Vlasenko | 0e7f28d | 2008-08-05 15:28:05 +0000 | [diff] [blame] | 349 | check_found: |
Denis Vlasenko | ce02b15 | 2008-06-27 22:05:21 +0000 | [diff] [blame] | 350 | if (!found) { |
| 351 | bb_error_msg("no manual entry for '%s'", *argv); |
| 352 | not_found = 1; |
| 353 | } |
Denis Vlasenko | ffa4499 | 2008-04-13 08:20:00 +0000 | [diff] [blame] | 354 | next_arg: |
| 355 | argv++; |
| 356 | } while (*argv); |
| 357 | |
Denis Vlasenko | ce02b15 | 2008-06-27 22:05:21 +0000 | [diff] [blame] | 358 | return not_found; |
Denis Vlasenko | ffa4499 | 2008-04-13 08:20:00 +0000 | [diff] [blame] | 359 | } |