diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2008-04-13 08:20:00 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2008-04-13 08:20:00 +0000 |
commit | ffa4499c9f55e652e71acd0c4e5e9412ede37050 (patch) | |
tree | 04379edd4447d751c08c5104ba45d5f21fd8ec2c /miscutils/man.c | |
parent | 867bd37c7f9f87d622047a2076c9a78c08f71b30 (diff) | |
download | busybox-w32-ffa4499c9f55e652e71acd0c4e5e9412ede37050.tar.gz busybox-w32-ffa4499c9f55e652e71acd0c4e5e9412ede37050.tar.bz2 busybox-w32-ffa4499c9f55e652e71acd0c4e5e9412ede37050.zip |
man: new applet. ~600 bytes.
Diffstat (limited to 'miscutils/man.c')
-rw-r--r-- | miscutils/man.c | 151 |
1 files changed, 151 insertions, 0 deletions
diff --git a/miscutils/man.c b/miscutils/man.c new file mode 100644 index 000000000..ed31b66c7 --- /dev/null +++ b/miscutils/man.c | |||
@@ -0,0 +1,151 @@ | |||
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 | |||
13 | /* This is what I see on my desktop system deing executed: | ||
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 | |||
26 | static int run_pipe(const char *unpacker, const char *pager, char *man_filename) | ||
27 | { | ||
28 | char *cmd; | ||
29 | |||
30 | if (access(man_filename, R_OK) != 0) | ||
31 | return 0; | ||
32 | |||
33 | if (option_mask32 & OPT_w) { | ||
34 | puts(man_filename); | ||
35 | return 1; | ||
36 | } | ||
37 | |||
38 | cmd = xasprintf("%s '%s' | gtbl | nroff -Tlatin1 -mandoc | %s", | ||
39 | unpacker, man_filename, pager); | ||
40 | system(cmd); | ||
41 | free(cmd); | ||
42 | return 1; | ||
43 | } | ||
44 | |||
45 | /* man_filename is of the form "/dir/dir/dir/name.s.bz2" */ | ||
46 | static int show_manpage(const char *pager, char *man_filename) | ||
47 | { | ||
48 | int len; | ||
49 | |||
50 | if (run_pipe("bunzip2 -c", pager, man_filename)) | ||
51 | return 1; | ||
52 | |||
53 | len = strlen(man_filename) - 1; | ||
54 | |||
55 | man_filename[len] = '\0'; /* ".bz2" -> ".gz" */ | ||
56 | man_filename[len - 2] = 'g'; | ||
57 | if (run_pipe("gunzip -c", pager, man_filename)) | ||
58 | return 1; | ||
59 | |||
60 | man_filename[len - 3] = '\0'; /* ".gz" -> "" */ | ||
61 | if (run_pipe("cat", pager, man_filename)) | ||
62 | return 1; | ||
63 | |||
64 | return 0; | ||
65 | } | ||
66 | |||
67 | int man_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | ||
68 | int man_main(int argc ATTRIBUTE_UNUSED, char **argv) | ||
69 | { | ||
70 | FILE *cf; | ||
71 | const char *pager; | ||
72 | char *man_path, *sec_list; | ||
73 | char *cur_path, *cur_sect; | ||
74 | char *line, *value; | ||
75 | int opt; | ||
76 | |||
77 | opt_complementary = "-1"; /* at least one argument */ | ||
78 | opt = getopt32(argv, "+aw"); | ||
79 | argv += optind; | ||
80 | |||
81 | sec_list = xstrdup("1:2:3:4:5:6:7:8:9"); | ||
82 | man_path = xstrdup(getenv("MANPATH")); | ||
83 | pager = getenv("MANPAGER"); | ||
84 | if (!pager) { | ||
85 | pager = getenv("PAGER"); | ||
86 | if (!pager) | ||
87 | pager = "more"; | ||
88 | } | ||
89 | |||
90 | /* Parse man.conf */ | ||
91 | cf = fopen_or_warn("/etc/man.conf", "r"); | ||
92 | if (cf) { | ||
93 | /* go through man configuration file and search relevant paths, sections */ | ||
94 | while ((line = xmalloc_fgetline(cf)) != NULL) { | ||
95 | trim(line); /* remove whitespace at the beginning/end */ | ||
96 | if (isspace(line[7])) { | ||
97 | line[7] = '\0'; | ||
98 | value = skip_whitespace(&line[8]); | ||
99 | *skip_non_whitespace(value) = '\0'; | ||
100 | if (strcmp("MANPATH", line) == 0) { | ||
101 | free(man_path); | ||
102 | man_path = xstrdup(value); | ||
103 | } | ||
104 | if (strcmp("MANSECT", line) == 0) { | ||
105 | free(sec_list); | ||
106 | sec_list = xstrdup(value); | ||
107 | } | ||
108 | } | ||
109 | free(line); | ||
110 | } | ||
111 | fclose(cf); | ||
112 | } | ||
113 | |||
114 | do { /* for each argv[] */ | ||
115 | cur_path = man_path; | ||
116 | do { /* for each MANPATH item */ | ||
117 | char *next_path = strchrnul(cur_path, ':'); | ||
118 | int path_len = next_path - cur_path; | ||
119 | |||
120 | cur_sect = sec_list; | ||
121 | do { /* for each section */ | ||
122 | char *next_sect = strchrnul(cur_sect, ':'); | ||
123 | int sect_len = next_sect - cur_sect; | ||
124 | |||
125 | char *man_filename = xasprintf("%.*s/man%.*s/%s.%.*s" ".bz2", | ||
126 | path_len, cur_path, | ||
127 | sect_len, cur_sect, | ||
128 | *argv, | ||
129 | sect_len, cur_sect); | ||
130 | if (show_manpage(pager, man_filename)) { | ||
131 | if (!(opt & OPT_a)) { | ||
132 | goto next_arg; | ||
133 | } | ||
134 | } | ||
135 | free(man_filename); | ||
136 | |||
137 | cur_sect = next_sect; | ||
138 | while (*cur_sect == ':') | ||
139 | cur_sect++; | ||
140 | } while (*cur_sect); | ||
141 | |||
142 | cur_path = next_path; | ||
143 | while (*cur_path == ':') | ||
144 | cur_path++; | ||
145 | } while (*cur_path); | ||
146 | next_arg: | ||
147 | argv++; | ||
148 | } while (*argv); | ||
149 | |||
150 | return EXIT_SUCCESS; | ||
151 | } | ||