-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathwman
executable file
·371 lines (318 loc) · 7.94 KB
/
wman
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#!/bin/sh
#
# Fetch man pages for selected OS from a man page mirror.
# Copyright © 2004 Emre Sevinc <[email protected]>
# Tweaked by KIVILCIM "Sundance" Hindistan <[email protected]>
# Lots of changes by Recai Oktaþ <[email protected]>
# Fixes from Wei Dai <[email protected]>
#
# Licensed under the GNU General Public License, version 2.
# See the file `http://www.gnu.org/copyleft/gpl.txt'.
# ------------------------------------------------------------------------------
# Script constants
# ------------------------------------------------------------------------------
NAME=wman
THIS=${0##*/}
SYSTEM_CONF=/etc/${NAME}rc
USER_CONF=$HOME/.${NAME}rc
# ------------------------------------------------------------------------------
# Default setting for target OSes
# ------------------------------------------------------------------------------
DEFAULT_OPENBSD='
OSNAME=OpenBSD
HOST=http://www.openbsd.org
CGI=cgi-bin/man.cgi?query=$page&sektion=$sect&format=troff
FORMAT=raw
FAILURE=[Nn]o [Mm]anual [Ee]ntry
'
DEFAULT_FREEBSD='
OSNAME=FreeBSD
HOST=http://www.freebsd.org
CGI=cgi/man.cgi?query=$page&sektion=$sect&format=ascii
FORMAT=raw
FAILURE=^[Ss]orry
'
DEFAULT_NETBSD='
OSNAME=NetBSD
HOST=http://netbsd.gw.com
CGI=cgi-bin/man-cgi?$page+$sect
FORMAT=html
FILTER=DEFAULT_NETBSD_FILTER
FAILURE=[Mm]anual [Pp]age was not found
'
DEFAULT_NETBSD_FILTER() {
sed '/^-----/,$d'
}
DEFAULT_DRAGONFLY='
OSNAME=Dragonfly BSD
HOST=http://leaf.dragonflybsd.org
CGI=cgi/web-man?command=$page§ion=$sect
FORMAT=html
FAILURE=[Mm]anual [Pp]age could not be found
'
DEFAULT_LINUX='
OSNAME=GNU/Linux
HOST=http://www.freebsd.org
CGI=cgi/man.cgi?query=$page&sektion=$sect&manpath=Red+Hat+Linux%2Fi386+9&format=ascii
FORMAT=raw
FAILURE=^[Ss]orry
'
# ------------------------------------------------------------------------------
# Helpers
# ------------------------------------------------------------------------------
# Portable which(1).
pathfind() {
ifs_save="$IFS"
IFS=:
for _p in $PATH; do
if [ -x "$_p/$*" ] && [ -f "$_p/$*" ]; then
IFS="$OLDIFS"
return 0
fi
done
IFS="$ifs_save"
return 1
}
dumper_raw() {
for p in wget lynx w3m curl links w3c; do
if pathfind $p; then
dumper=$p
break
fi
done
# Setup proper options.
case "$dumper" in
wget) opt="-O-" ;;
lynx) opt="-source" ;;
w3m) opt="-dump_source" ;;
curl) opt="" ;;
links) opt="-source" ;;
w3c) opt="-n -get" ;;
"") printf "Couldn't locate a program to fetch files from net " >&2
printf "(e.g. wget, w3m, lynx, w3c, or curl).\n" >&2
exit 1 ;;
esac
$dumper $opt "$@"
}
dumper_html() {
# TODO w3c
for p in w3m links elinks lynx; do
if pathfind $p; then
dumper=$p
break
fi
done
# Setup proper options.
case "$dumper" in
w3m) opt="-dump" ;;
links) opt="-dump" ;;
elinks) opt="-dump" ;;
lynx) opt="-dump" ;;
"") printf "Couldn't locate a program to fetch files from net " >&2
printf "(e.g. lynx, w3m, lynx or w3c).\n" >&2
exit 1 ;;
esac
$dumper $opt "$@"
}
is_cache_valid() {
cache="$1"
failstamp="$2"
if [ $(sed '/^[[:blank:]]*$/d' $cache | wc -l) -lt 20 ]; then
[ -z "$DEBUG" ] || echo >&2 "Fetched file is too small!"
return 1
fi
if sed '/^[[:blank:]]*$/d;{3q}' $cache | grep -q "$failstamp"; then
[ -z "$DEBUG" ] || echo >&2 "Failure stamp '$failstamp' seen!"
return 1
fi
return 0
}
# ------------------------------------------------------------------------------
# Init
# ------------------------------------------------------------------------------
LC_ALL=C
export LC_ALL
for conf in $SYSTEM_CONF $USER_CONF; do
# Read user conf file.
if [ -r $conf ]; then
case $- in
*e*) ;;
*) set -e; noexitonerr=yes ;;
esac
trap "Error in configuration file $conf; aborting." QUIT INT
. $conf
trap - QUIT INT
if [ -n "$noexitonerr" ]; then
set +e
fi
fi
done
OS=
DEBUG=${DEBUG-}
CACHE_RENEW=
while getopts o:dhr opt; do
case $opt in
o) OS="$OPTARG" ;;
d) DEBUG=yes ;;
r) CACHE_RENEW=yes ;;
h) cat >&2 <<- EOF
Usage: $THIS [options] [section] page
Options:
-o os_name operating system (e.g. openbsd, netbsd)
-d print debug info
-n renew cache
-h this screen
EOF
exit 2 ;;
esac
done
shift $(($OPTIND - 1))
if [ -z "$OS" ] && ! [ "$THIS" = "$NAME" ]; then
OS=$(echo "$THIS" | sed 's/^man[-_+]//')
else
DEFAULT_OS=${DEFAULT_OS:-$(uname)}
OS=${OS:-$DEFAULT_OS}
fi
if [ -z "$OS" ]; then
echo "Couldn't determine OS; aborting"
exit 1
fi
OS=$(echo $OS | tr 'a-z' 'A-Z')
# Defaults for some global settings.
PAGER=${PAGER-less}
DEBUG=${DEBUG:-}
CACHEROOT=${CACHEROOT-$HOME/.$THIS}
if [ $# -eq 0 ]; then
echo >&2 "What manual page do you want?"
exit 1
fi
sect=
case "$1" in
[1-9])
sect=$1
shift
if [ -z "$1" ]; then
echo >&2 "What manual page do you want from section ${sect}?"
exit 1
fi
;;
esac
page="$1"
NEWLINE='
'
OLDIFS="$IFS"
_expand_variable() {
condensed=${1:?'variable empty!'}
eval "this=\$$condensed"
# Expand page and sect.
eval "this=\"$this\""
IFS="$NEWLINE"
for line in $this; do
IFS="$OLDIFS"
var="${line%%=*}"
val="${line#[!=]*=}"
case "$var" in
'OSNAME'|'HOST'|'CGI'|'FORMAT'|'FILTER'|'FAILURE')
var="man_$(echo $var | tr 'A-Z' 'a-z')"
eval "$var=\${$var:-$val}" ;;
*)
continue ;;
esac
done
}
# User settings.
_expand_variable "$OS"
# Builtin default settings.
_expand_variable "DEFAULT_$OS"
man_url="$man_host/$man_cgi"
man_dumper="dumper_$man_format"
man_osname="${man_osname:-$(echo $OS | tr 'A-Z' 'a-z')}"
[ -z "$DEBUG" ] || cat >&2 <<- EOF
Fetch parameters:
OS: $man_osname
Page: $page
Section: $sect
Host: $man_host
CGI: $man_cgi
Format: $man_format
Dumper: $man_dumper
Filter: $man_filter
Failure: '$man_failure'
Resulting url: $man_url
EOF
( eval $man_dumper </dev/null >/dev/null 2>&1 )
if [ $? -eq 127 ]; then
echo >&2 "No such '$man_format' dumper for '$OS'; aborting"
exit 1
fi
if [ -n "$man_filter" ]; then
( eval $man_filter </dev/null >/dev/null 2>&1 )
if [ $? -eq 127 ]; then
echo >&2 "No such '$man_filter' for '$OS'; aborting"
exit 1
fi
else
man_filter=cat
fi
# ... now cache variables
cachedir=$CACHEROOT/$(echo $OS | tr 'A-Z' 'a-z')
man_cache=$cachedir/$page.${sect:-0} # use 0 to catch the default man page
# ------------------------------------------------------------------------------
# Main
# ------------------------------------------------------------------------------
if [ -n "$CACHE_RENEW" ] || {
[ -f $man_cache ] && ! is_cache_valid $man_cache "$man_failure"
}; then
rm -f $man_cache
fi
what="$man_osname manual entry for $page"
if [ -n "$sect" ]; then
what="$what in section $sect"
fi
if ! [ -f $man_cache ]; then
# Initiate cache if it doesn't exist.
(umask 0077 && mkdir -p $cachedir)
tmpfile="$(mktemp $cachedir/$THIS.XXXXXXXX)" || exit 1
# Remove partial files.
trap 'exitcode=$?
[ -z "$tmpfile" ] || rm -rf "$tmpfile"
exit $exitcode' 0 1 2 3 13 15
printf "Querying $what... " >&2
if $man_dumper "$man_url" 2>/dev/null | sed '/./,$!d' >$tmpfile; then
if is_cache_valid $tmpfile "$man_failure"; then
echo "success."
mv -f $tmpfile $man_cache && tmpfile=
else
echo "failed."
fi >&2
else
echo >&2 "fetch failed."
fi
fi
if ! [ -f $man_cache ]; then
echo >&2 "No $what."
exit 1
fi
# Attempt to reduce cache space whenever a section is explicitly given.
# Note that we avoid to extract the section info from cache file, since
# this might be unreliable for some formats and also it would increase
# the code complexity.
if [ "$sect" != "0" ]; then
man_fallback=$cachedir/$page.0
if [ -f $man_fallback ] && ! [ -L $man_fallback ]; then
(
cd $cachedir
# Search for an idantic file.
found=
for f in $page.[1-9]; do
if cmp -s $f $man_fallback; then
found=$f
break
fi
done
# If found, symlink it to sectioned file to reduce space.
[ -z "$found" ] || ln -sf $found $man_fallback
)
fi
fi
$man_filter <$man_cache | $PAGER