aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2008-04-13 08:20:00 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2008-04-13 08:20:00 +0000
commitffa4499c9f55e652e71acd0c4e5e9412ede37050 (patch)
tree04379edd4447d751c08c5104ba45d5f21fd8ec2c
parent867bd37c7f9f87d622047a2076c9a78c08f71b30 (diff)
downloadbusybox-w32-ffa4499c9f55e652e71acd0c4e5e9412ede37050.tar.gz
busybox-w32-ffa4499c9f55e652e71acd0c4e5e9412ede37050.tar.bz2
busybox-w32-ffa4499c9f55e652e71acd0c4e5e9412ede37050.zip
man: new applet. ~600 bytes.
-rw-r--r--include/applets.h1
-rw-r--r--include/usage.h8
-rw-r--r--miscutils/Config.in6
-rw-r--r--miscutils/Kbuild1
-rw-r--r--miscutils/man.c151
5 files changed, 167 insertions, 0 deletions
diff --git a/include/applets.h b/include/applets.h
index 2e661aca0..2bd70f281 100644
--- a/include/applets.h
+++ b/include/applets.h
@@ -237,6 +237,7 @@ USE_LSATTR(APPLET(lsattr, _BB_DIR_BIN, _BB_SUID_NEVER))
237USE_LSMOD(APPLET(lsmod, _BB_DIR_SBIN, _BB_SUID_NEVER)) 237USE_LSMOD(APPLET(lsmod, _BB_DIR_SBIN, _BB_SUID_NEVER))
238USE_UNLZMA(APPLET_ODDNAME(lzmacat, unlzma, _BB_DIR_USR_BIN, _BB_SUID_NEVER, lzmacat)) 238USE_UNLZMA(APPLET_ODDNAME(lzmacat, unlzma, _BB_DIR_USR_BIN, _BB_SUID_NEVER, lzmacat))
239USE_MAKEDEVS(APPLET(makedevs, _BB_DIR_SBIN, _BB_SUID_NEVER)) 239USE_MAKEDEVS(APPLET(makedevs, _BB_DIR_SBIN, _BB_SUID_NEVER))
240USE_MAN(APPLET(man, _BB_DIR_SBIN, _BB_SUID_NEVER))
240USE_MATCHPATHCON(APPLET(matchpathcon, _BB_DIR_USR_SBIN, _BB_SUID_NEVER)) 241USE_MATCHPATHCON(APPLET(matchpathcon, _BB_DIR_USR_SBIN, _BB_SUID_NEVER))
241USE_MD5SUM(APPLET_ODDNAME(md5sum, md5_sha1_sum, _BB_DIR_USR_BIN, _BB_SUID_NEVER, md5sum)) 242USE_MD5SUM(APPLET_ODDNAME(md5sum, md5_sha1_sum, _BB_DIR_USR_BIN, _BB_SUID_NEVER, md5sum))
242USE_MDEV(APPLET(mdev, _BB_DIR_SBIN, _BB_SUID_NEVER)) 243USE_MDEV(APPLET(mdev, _BB_DIR_SBIN, _BB_SUID_NEVER))
diff --git a/include/usage.h b/include/usage.h
index dbe403d13..d1847020b 100644
--- a/include/usage.h
+++ b/include/usage.h
@@ -2300,6 +2300,14 @@
2300 "/dev/hda[0-15]\n" 2300 "/dev/hda[0-15]\n"
2301#endif 2301#endif
2302 2302
2303#define man_trivial_usage \
2304 "[OPTION]... [MANPAGE]..."
2305#define man_full_usage \
2306 "Format and display manual page\n" \
2307 "\nOptions:" \
2308 "\n -a Display all pages" \
2309 "\n -w Show page locations" \
2310
2303#define matchpathcon_trivial_usage \ 2311#define matchpathcon_trivial_usage \
2304 "[-n] [-N] [-f file_contexts_file] [-p prefix] [-V]" 2312 "[-n] [-N] [-f file_contexts_file] [-p prefix] [-V]"
2305#define matchpathcon_full_usage \ 2313#define matchpathcon_full_usage \
diff --git a/miscutils/Config.in b/miscutils/Config.in
index c6c1490df..06cac14ab 100644
--- a/miscutils/Config.in
+++ b/miscutils/Config.in
@@ -372,6 +372,12 @@ config FEATURE_MAKEDEVS_TABLE
372 372
373endchoice 373endchoice
374 374
375config MAN
376 bool "man"
377 default n
378 help
379 Format and display manual pages.
380
375config MICROCOM 381config MICROCOM
376 bool "microcom" 382 bool "microcom"
377 default n 383 default n
diff --git a/miscutils/Kbuild b/miscutils/Kbuild
index 513c03882..ba2a0dc14 100644
--- a/miscutils/Kbuild
+++ b/miscutils/Kbuild
@@ -19,6 +19,7 @@ lib-$(CONFIG_HDPARM) += hdparm.o
19lib-$(CONFIG_LAST) += last.o 19lib-$(CONFIG_LAST) += last.o
20lib-$(CONFIG_LESS) += less.o 20lib-$(CONFIG_LESS) += less.o
21lib-$(CONFIG_MAKEDEVS) += makedevs.o 21lib-$(CONFIG_MAKEDEVS) += makedevs.o
22lib-$(CONFIG_MAN) += man.o
22lib-$(CONFIG_MICROCOM) += microcom.o 23lib-$(CONFIG_MICROCOM) += microcom.o
23lib-$(CONFIG_MOUNTPOINT) += mountpoint.o 24lib-$(CONFIG_MOUNTPOINT) += mountpoint.o
24lib-$(CONFIG_MT) += mt.o 25lib-$(CONFIG_MT) += mt.o
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
8enum {
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(
16echo ".ll 12.4i"
17echo ".nr LL 12.4i"
18echo ".pl 1100i"
19gunzip -c '/usr/man/man1/bzip2.1.gz'
20echo ".\\\""
21echo ".pl \n(nlu+10"
22) | gtbl | nroff -Tlatin1 -mandoc | less
23
24*/
25
26static 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" */
46static 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
67int man_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
68int 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}