-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrfcat
More file actions
executable file
·185 lines (150 loc) · 4.74 KB
/
rfcat
File metadata and controls
executable file
·185 lines (150 loc) · 4.74 KB
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
#!/usr/bin/env bash
:
##########################################################################
# Title : rfcat - print RFC text
# Version : 2.6
# Author : Heiner Steven <[email protected]>
# Date : 2001-04-25
# Requires : wget, gzip
# Category : WWW
# SCCS-Id. : @(#) rfcat 2.6 09/10/21
##########################################################################
# Description
# o Prints the text of an internet standard (RFC: "Request For
# Comments") to standard output. Tries to locate the RFC locally,
# and retrieves it otherwise direcly from the Web.
# o Each RFC text file can optionally be cached after first retrieval
##########################################################################
PN=`basename "$0"` # Program name
VER='2.6'
# A program to accept an URL as command line argument, retrieves it, and
# prints the results to standard output
: ${GETURL:=wget}
: ${GETURLFLAGS="-O- -q"} # may be empty
: ${COMPRESS:=gzip}
: ${COMPRESSFLAGS=-f} # may be empty
# The local directory all RFCs are contained within
: ${RFCDIR:=/usr/local/share/rfc}
# The base WWW URL for all RFCs
RFC_BASE_URL=http://www.ietf.org
# The URL of the "INDEX" page
RFC_INDEX_URL=$RFC_BASE_URL/download/rfc-index.txt
# A template for an RFC URL. The string '${no}' will be replaced by the
# RFC's number
RFC_BY_NUMBER_TEMPL="$RFC_BASE_URL/rfc/rfc\${no}.txt"
#########################################################################
Usage () {
echo >&2 "$PN - print RFC text, $VER (stv)
usage: $PN [-fc] rfcspec [...]
-c: create a local copy of the file, if it does not exist already
-f: force retrieval of the file from the Web
An rfcspec can be a RFC number (e.g. 2100), or the symbolic name
\"INDEX\". The RFC is searched locally (RFCDIR=$RFCDIR).
If this was not successful, $PN will retrieve it from $RFC_BASE_URL."
exit 1
}
Msg () {
for MsgLine
do echo "$PN: $MsgLine" >&2
done
}
Fatal () { Msg "$@"; exit 1; }
#########################################################################
# rfc2path - convert RFC specification to local path name of file
#########################################################################
rfc2path () {
[ $# -eq 1 ] || Fatal "INTERNAL ERROR: rfc2path needs one argument"
_rfcspec=$1; shift
case "$_rfcspec" in
INDEX)
_url=$RFC_INDEX_URL;;
*)
no=$_rfcspec
eval _url="$RFC_BY_NUMBER_TEMPL";
esac
echo $RFCDIR/rfc$_rfcspec
}
#########################################################################
# rfc2url - convert RFC specification to URL of file
#########################################################################
rfc2url () {
[ $# -eq 1 ] || Fatal "INTERNAL ERROR: rfc2url needs one argument"
_rfcspec=$1; shift
case "$_rfcspec" in
INDEX)
_url=$RFC_INDEX_URL;;
*)
no=$_rfcspec
eval _url="$RFC_BY_NUMBER_TEMPL";
esac
echo "$_url"
}
#########################################################################
set -- `getopt cfh "$@"` || Usage
[ $# -lt 1 ] && Usage # "getopt" detected an error
CacheFile=false
ForceRetrieval=false
while [ $# -gt 0 ]
do
case "$1" in
-c) CacheFile=true;;
-f) ForceRetrieval=true;;
--) shift; break;;
-h) Usage;;
-*) Usage;;
*) break;; # First file name
esac
shift
done
[ $# -lt 1 ] && Usage
errors=0
for rfc
do
rfc=`echo "$rfc" | sed 's/^00*//g'` # remove leading zeroes
#
# Search RFC locally
#
file=`rfc2path "$rfc"`
if [ -n "$file" ] && [ $ForceRetrieval = false ]
then
for ext in "" .gz .Z .z .txt.gz .txt.Z .txt.z
do
[ -r "$file$ext" ] || continue
case "$ext" in
"") cat "$file$ext" ;;
*.gz ) gunzip -c "$file$ext";;
*.Z ) zcat "$file$ext";;
*.z ) unpack "$file$ext";;
esac
[ $? -eq 0 ] || errors=`expr $errors + 1`
continue 2 # Next RFC
done
fi
#
# No local file. Try to retrieve the RFC via URL
#
url=`rfc2url "$rfc"`
if [ -n "$url" ]
then
# Get the RFC using the Web.
# XXX: This page could be cached locally using
# ... | tee $RFCDIR/...
if [ $CacheFile = true ]
then
# Retrieve file, and create a local copy.
# Ignore keyboard signals, because they could cause the local copy
# to be incomplete
trap '' 1 2 3 13 15
$GETURL $GETURLFLAGS "$url" | tee "$file"
trap 1 2 3 13 15
if [ -s "$file" ]
then "$COMPRESS" $COMPRESSFLAGS "$file"
else rm -f "$file" # remove empty file
fi
else
$GETURL $GETURLFLAGS "$url"
fi
[ $? -ne 0 ] && errors=`expr $errors + 1`
fi
done
exit $errors