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