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 | f6efccc | 2008-07-05 08:50:08 +0000 | [diff] [blame] | 30 | static int run_pipe(const char *unpacker, const char *pager, char *man_filename, int man) |
Denis Vlasenko | ffa4499 | 2008-04-13 08:20:00 +0000 | [diff] [blame] | 31 | { |
| 32 | char *cmd; |
| 33 | |
| 34 | if (access(man_filename, R_OK) != 0) |
| 35 | return 0; |
| 36 | |
| 37 | if (option_mask32 & OPT_w) { |
| 38 | puts(man_filename); |
| 39 | return 1; |
| 40 | } |
| 41 | |
Denis Vlasenko | 4cbffc0 | 2008-07-04 21:58:00 +0000 | [diff] [blame] | 42 | /* "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] | 43 | * Otherwise it may show just empty screen */ |
Denis Vlasenko | f6efccc | 2008-07-05 08:50:08 +0000 | [diff] [blame] | 44 | cmd = xasprintf( |
| 45 | man ? "%s '%s' | gtbl | nroff -Tlatin1 -mandoc 2>&1 | %s" |
| 46 | : "%s '%s' | %s", |
| 47 | unpacker, man_filename, pager); |
Denis Vlasenko | ffa4499 | 2008-04-13 08:20:00 +0000 | [diff] [blame] | 48 | system(cmd); |
| 49 | free(cmd); |
| 50 | return 1; |
| 51 | } |
| 52 | |
| 53 | /* man_filename is of the form "/dir/dir/dir/name.s.bz2" */ |
Denis Vlasenko | f6efccc | 2008-07-05 08:50:08 +0000 | [diff] [blame] | 54 | static int show_manpage(const char *pager, char *man_filename, int man) |
Denis Vlasenko | ffa4499 | 2008-04-13 08:20:00 +0000 | [diff] [blame] | 55 | { |
| 56 | int len; |
| 57 | |
Denis Vlasenko | f6efccc | 2008-07-05 08:50:08 +0000 | [diff] [blame] | 58 | if (run_pipe("bunzip2 -c", pager, man_filename, man)) |
Denis Vlasenko | ffa4499 | 2008-04-13 08:20:00 +0000 | [diff] [blame] | 59 | return 1; |
| 60 | |
| 61 | len = strlen(man_filename) - 1; |
| 62 | |
| 63 | man_filename[len] = '\0'; /* ".bz2" -> ".gz" */ |
| 64 | man_filename[len - 2] = 'g'; |
Denis Vlasenko | f6efccc | 2008-07-05 08:50:08 +0000 | [diff] [blame] | 65 | if (run_pipe("gunzip -c", pager, man_filename, man)) |
Denis Vlasenko | ffa4499 | 2008-04-13 08:20:00 +0000 | [diff] [blame] | 66 | return 1; |
| 67 | |
| 68 | man_filename[len - 3] = '\0'; /* ".gz" -> "" */ |
Denis Vlasenko | f6efccc | 2008-07-05 08:50:08 +0000 | [diff] [blame] | 69 | if (run_pipe(STR_cat, pager, man_filename, man)) |
Denis Vlasenko | ffa4499 | 2008-04-13 08:20:00 +0000 | [diff] [blame] | 70 | return 1; |
| 71 | |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | int man_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 76 | int man_main(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | ffa4499 | 2008-04-13 08:20:00 +0000 | [diff] [blame] | 77 | { |
| 78 | FILE *cf; |
| 79 | const char *pager; |
Denis Vlasenko | 50d068c | 2008-04-19 03:42:47 +0000 | [diff] [blame] | 80 | char **man_path_list; |
| 81 | char *sec_list; |
Denis Vlasenko | ffa4499 | 2008-04-13 08:20:00 +0000 | [diff] [blame] | 82 | char *cur_path, *cur_sect; |
| 83 | char *line, *value; |
Denis Vlasenko | 9835d47 | 2008-07-04 21:57:11 +0000 | [diff] [blame] | 84 | int count_mp, cur_mp; |
Denis Vlasenko | ce02b15 | 2008-06-27 22:05:21 +0000 | [diff] [blame] | 85 | int opt, not_found; |
Denis Vlasenko | ffa4499 | 2008-04-13 08:20:00 +0000 | [diff] [blame] | 86 | |
| 87 | opt_complementary = "-1"; /* at least one argument */ |
| 88 | opt = getopt32(argv, "+aw"); |
| 89 | argv += optind; |
| 90 | |
| 91 | sec_list = xstrdup("1:2:3:4:5:6:7:8:9"); |
Denis Vlasenko | 9835d47 | 2008-07-04 21:57:11 +0000 | [diff] [blame] | 92 | /* Last valid man_path_list[] is [0x10] */ |
| 93 | man_path_list = xzalloc(0x11 * sizeof(man_path_list[0])); |
Denis Vlasenko | 50d068c | 2008-04-19 03:42:47 +0000 | [diff] [blame] | 94 | count_mp = 0; |
| 95 | man_path_list[0] = xstrdup(getenv("MANPATH")); |
| 96 | if (man_path_list[0]) |
| 97 | count_mp++; |
Denis Vlasenko | ffa4499 | 2008-04-13 08:20:00 +0000 | [diff] [blame] | 98 | pager = getenv("MANPAGER"); |
| 99 | if (!pager) { |
| 100 | pager = getenv("PAGER"); |
| 101 | if (!pager) |
| 102 | pager = "more"; |
| 103 | } |
| 104 | |
| 105 | /* Parse man.conf */ |
| 106 | cf = fopen_or_warn("/etc/man.conf", "r"); |
| 107 | if (cf) { |
| 108 | /* go through man configuration file and search relevant paths, sections */ |
| 109 | while ((line = xmalloc_fgetline(cf)) != NULL) { |
| 110 | trim(line); /* remove whitespace at the beginning/end */ |
| 111 | if (isspace(line[7])) { |
| 112 | line[7] = '\0'; |
| 113 | value = skip_whitespace(&line[8]); |
| 114 | *skip_non_whitespace(value) = '\0'; |
| 115 | if (strcmp("MANPATH", line) == 0) { |
Denis Vlasenko | 50d068c | 2008-04-19 03:42:47 +0000 | [diff] [blame] | 116 | man_path_list[count_mp] = xstrdup(value); |
| 117 | count_mp++; |
Denis Vlasenko | 9835d47 | 2008-07-04 21:57:11 +0000 | [diff] [blame] | 118 | /* man_path_list is NULL terminated */ |
| 119 | man_path_list[count_mp] = NULL; |
Denis Vlasenko | deeed59 | 2008-07-08 05:14:36 +0000 | [diff] [blame^] | 120 | man_path_list = xrealloc_vector(man_path_list, 4, count_mp); |
Denis Vlasenko | ffa4499 | 2008-04-13 08:20:00 +0000 | [diff] [blame] | 121 | } |
| 122 | if (strcmp("MANSECT", line) == 0) { |
| 123 | free(sec_list); |
| 124 | sec_list = xstrdup(value); |
| 125 | } |
| 126 | } |
| 127 | free(line); |
| 128 | } |
| 129 | fclose(cf); |
| 130 | } |
| 131 | |
Denis Vlasenko | b75fe79 | 2008-06-27 22:31:07 +0000 | [diff] [blame] | 132 | // TODO: my man3/getpwuid.3.gz contains just one line: |
| 133 | // .so man3/getpwnam.3 |
| 134 | // (and I _dont_ have man3/getpwnam.3, I have man3/getpwnam.3.gz) |
| 135 | // need to support this... |
| 136 | |
Denis Vlasenko | ce02b15 | 2008-06-27 22:05:21 +0000 | [diff] [blame] | 137 | not_found = 0; |
Denis Vlasenko | ffa4499 | 2008-04-13 08:20:00 +0000 | [diff] [blame] | 138 | do { /* for each argv[] */ |
Denis Vlasenko | ce02b15 | 2008-06-27 22:05:21 +0000 | [diff] [blame] | 139 | int found = 0; |
Denis Vlasenko | 50d068c | 2008-04-19 03:42:47 +0000 | [diff] [blame] | 140 | cur_mp = 0; |
| 141 | while ((cur_path = man_path_list[cur_mp++]) != NULL) { |
| 142 | /* for each MANPATH */ |
| 143 | do { /* for each MANPATH item */ |
| 144 | char *next_path = strchrnul(cur_path, ':'); |
| 145 | int path_len = next_path - cur_path; |
| 146 | cur_sect = sec_list; |
| 147 | do { /* for each section */ |
| 148 | char *next_sect = strchrnul(cur_sect, ':'); |
| 149 | int sect_len = next_sect - cur_sect; |
Denis Vlasenko | 4cbffc0 | 2008-07-04 21:58:00 +0000 | [diff] [blame] | 150 | char *man_filename; |
Denis Vlasenko | fec8b42 | 2008-07-05 08:38:41 +0000 | [diff] [blame] | 151 | int cat0man1 = 0; |
Denis Vlasenko | ffa4499 | 2008-04-13 08:20:00 +0000 | [diff] [blame] | 152 | |
Denis Vlasenko | fec8b42 | 2008-07-05 08:38:41 +0000 | [diff] [blame] | 153 | /* Search for cat, then man page */ |
| 154 | while (cat0man1 < 2) { |
| 155 | int found_here; |
Denis Vlasenko | 4cbffc0 | 2008-07-04 21:58:00 +0000 | [diff] [blame] | 156 | man_filename = xasprintf("%.*s/%s%.*s/%s.%.*s" ".bz2", |
Denis Vlasenko | fec8b42 | 2008-07-05 08:38:41 +0000 | [diff] [blame] | 157 | path_len, cur_path, |
| 158 | STR_catNULmanNUL + cat0man1 * 4, |
| 159 | sect_len, cur_sect, |
| 160 | *argv, |
| 161 | sect_len, cur_sect); |
| 162 | found_here = show_manpage(pager, man_filename, cat0man1); |
| 163 | found |= found_here; |
| 164 | cat0man1 += found_here + 1; |
Denis Vlasenko | 4cbffc0 | 2008-07-04 21:58:00 +0000 | [diff] [blame] | 165 | free(man_filename); |
| 166 | } |
Denis Vlasenko | 4cbffc0 | 2008-07-04 21:58:00 +0000 | [diff] [blame] | 167 | |
Denis Vlasenko | 50d068c | 2008-04-19 03:42:47 +0000 | [diff] [blame] | 168 | if (found && !(opt & OPT_a)) |
Denis Vlasenko | ffa4499 | 2008-04-13 08:20:00 +0000 | [diff] [blame] | 169 | goto next_arg; |
Denis Vlasenko | 50d068c | 2008-04-19 03:42:47 +0000 | [diff] [blame] | 170 | cur_sect = next_sect; |
| 171 | while (*cur_sect == ':') |
| 172 | cur_sect++; |
| 173 | } while (*cur_sect); |
| 174 | cur_path = next_path; |
| 175 | while (*cur_path == ':') |
| 176 | cur_path++; |
| 177 | } while (*cur_path); |
| 178 | } |
Denis Vlasenko | ce02b15 | 2008-06-27 22:05:21 +0000 | [diff] [blame] | 179 | if (!found) { |
| 180 | bb_error_msg("no manual entry for '%s'", *argv); |
| 181 | not_found = 1; |
| 182 | } |
Denis Vlasenko | ffa4499 | 2008-04-13 08:20:00 +0000 | [diff] [blame] | 183 | next_arg: |
| 184 | argv++; |
| 185 | } while (*argv); |
| 186 | |
Denis Vlasenko | ce02b15 | 2008-06-27 22:05:21 +0000 | [diff] [blame] | 187 | return not_found; |
Denis Vlasenko | ffa4499 | 2008-04-13 08:20:00 +0000 | [diff] [blame] | 188 | } |