-
Notifications
You must be signed in to change notification settings - Fork 0
/
rfc-reader
executable file
·483 lines (457 loc) · 17.5 KB
/
rfc-reader
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
#! /bin/bash
# Copyright (C) 2011-2024 Erik Auerswald <auerswal@unix-ag.uni-kl.de>
# Copying and distribution of this file, with or without modification,
# are permitted in any medium without royalty provided the copyright
# notice and this notice are preserved. This file is offered as-is,
# without any warranty.
# BUGS:
# - draft-rep-wg-topic-00.txt is not formatted well by rfc-reader
# - the text version lacks the '\f' characters needed for filling the pages
# - RFC 603 is not formatted well by rfc-reader
# - first and second line are too long (79 characters)
# - 7th line is too long (76 characters)
# - 10th line is too long (75 characters)
# -> workaround: env C=81 rfc-reader 602
# - RFC 698 is not formatted well by rfc-reader
# - first line is too long (79 characters)
# -> workaround: env C=81 rfc-reader 698
# - RFC 713 is not formatted well by rfc-reader
# - first two lines are too long (81 characters)
# - pages of the RFC are too long
# -> workaround: env L=61 C=81 rfc-reader 713
# - RFC 734 is not formatted well by rfc-reader
# - lines are too long and thus truncated
# -> workaround: env C=84 rfc-reader 734
# - RFC 827 is not formatted well by rfc-reader
# - some lines are too long
# -> workaround: env C=82 rfc-reader 827
# - RFC 888 is not formatted well by rfc-reader
# - some lines are too long
# -> workaround: env C=79 rfc-reader 888
# - RFC 911 is not formatted well by rfc-reader
# - lines are too long and thus truncated
# -> workaround: env C=83 rfc-reader 911
# - RFC 994 is not formatted well by rfc-reader
# - some lines are too long
# -> workaround: env C=84 rfc-reader 994
# - RFC 995 is not formatted well by rfc-reader
# - lines of the RFC are too long
# -> workaround: env C=79 rfc-reader 995
# - RFC 1142 is not paginated, but contains lots of stray Form Feed characters,
# thus rfc-reader cannot format it well
# - RFC 1347 is not formatted well by rfc-reader
# - lines of the RFC are too long
# - pages of the RFC are too long
# -> workaround: env L=64 C=81 XT=xterm XTFS=4 rfc-reader 1347
# - RFC 1454 is not formatted well by rfc-reader
# - dome lines are too long
# -> workaround: env C=80 rfc-reader 1454
# - RFC 2329 is not formatted well by rfc-reader
# - too long lines
# -> workaround: env C=82 rfc-reader 2329
# - XTerm / GNOME Terminal font size heuristic breaks on high DPI displays
# - XTerm and GNOME Terminal (and/or the window manager) do not honor size
# requests if the size does not fit inside the $DISPLAY
set -u
B=$(basename "$0")
# e.g. openSUSE and Ubuntu provide RFCs as a package
RFCDIRS='/usr/share/doc/rfc /usr/share/doc/RFC/links'
# use a local cache if available
# a relative XDG_CACHE_HOME must be ignored
RFCCACHE=''
xdg_cache="${XDG_CACHE_HOME:-$HOME/.cache}/$B"
test "${xdg_cache:0:1}" = '/' || xdg_cache="$HOME/.cache/$B"
for DIR in "$HOME/.$B/cache" "$xdg_cache"; do
test \( -d "$DIR" \) -a \( -r "$DIR" \) &&
RFCDIRS="$DIR $RFCDIRS" &&
test -w "$DIR" && RFCCACHE="$DIR"
done
copyright()
{
echo 'Copyright (C) 2011-2024 Erik Auerswald <auerswal@unix-ag.uni-kl.de>'
}
version()
{
local V="0.67"
printf -- "%s\n" "$B version $V"
}
license()
{
cat <<EOL
Copying and distribution of this file, with or without modification,
are permitted in any medium without royalty provided the copyright
notice and this notice are preserved. This file is offered as-is,
without any warranty.
EOL
}
usage()
{
printf -- '%s\n' "Usage: $B [OPTS] [{rfc|bcp|fyi|ien|std}][{-|.| }]NUMBER[.txt]"
printf -- '%s\n' " $B [OPTS] {I-D.|draft-}DRAFT_NAME[-NUMBER][.txt]"
printf -- '%s\n' " $B [OPTS]"
echo
echo 'Download specified RFC or I-D (unless found locally), and show it'
echo 'in a pager inside a new terminal window of appropriate size.'
# shellcheck disable=SC2016
echo 'Display in current terminal if $DISPLAY is empty or unset.'
echo
echo 'Options:'
printf -- ' -h print this help and exit\n'
printf -- ' -V print version information and exit\n'
printf -- ' -L print license and exit\n'
# shellcheck disable=SC2016
printf -- ' -t display in current terminal (irrespective of $DISPLAY)\n'
echo
echo 'Examples:'
printf -- '%s\n' " $B # print version, copyright, help"
printf -- '%s\n' " $B 1 # downloads and displays RFC 1"
printf -- '%s\n' " $B rfc1 # downloads and displays RFC 1"
printf -- '%s\n' " $B rfc 1 # downloads and displays RFC 1"
printf -- '%s\n' " $B rfc-1 # downloads and displays RFC 1"
printf -- '%s\n' " $B rfc.1 # downloads and displays RFC 1"
printf -- '%s\n' " $B rfc1.txt # downloads and displays RFC 1"
printf -- '%s\n' " $B rfc0001.txt # downloads and displays RFC 1"
printf -- '%s\n' " $B bcp78 # downloads and displays BCP 78"
printf -- '%s\n' " $B fyi3 # downloads and displays FYI 3"
printf -- '%s\n' " $B ien137 # downloads and displays IEN 137"
printf -- '%s\n' " $B std1 # downloads and displays STD 1"
printf -- '%s\n' " $B draft-rep-wg-topic-00 # downloads & displays REP I-D"
printf -- '%s\n' " $B I-D.rep-wg-topic-00 # downloads & displays REP I-D"
printf -- '%s\n' " $B draft-rep-wg-topic # downloads & displays REP I-D"
printf -- '%s\n' " $B I-D.rep-wg-topic # downloads & displays REP I-D"
echo
echo 'Looking for RFCs in the following local directories:'
for DIR in $RFCDIRS; do
printf -- ' %s\n' "$DIR"
done
test -n "$RFCCACHE" &&
printf -- '\nUsing "%s" as download cache.\n' "$RFCCACHE" &&
echo '(BCP and STD files in the download cache are refreshed if possible.)'
}
# parse options
while getopts 'hVLt' OPT; do
case "$OPT" in
'h') version; copyright; license; echo; usage; exit 0;;
'V') version; copyright; exit 0;;
'L') version; copyright; license; exit 0;;
't') unset DISPLAY;;
'?') exit 1;;
*) printf -- '%s: ERROR: getopts() failure\n' "$B"; exit 1;;
esac
done
shift $((OPTIND - 1))
# exactly one or two arguments must be given
test \( $# -eq 1 \) -o \( $# -eq 2 \) ||
{ version; copyright; license; echo; usage; exit 1; }
# check some prerequisites:
# wget is needed to download files
HAVE_WGET=0
command -v wget >/dev/null 2>&1 && HAVE_WGET=1
# local directories with RFC (and similar) files can obviate donwloads
HAVE_LOCAL_DIRS=0
for DIR in $RFCDIRS; do
test -d "$DIR" && test -r "$DIR" && test -x "$DIR" && HAVE_LOCAL_DIRS=1 &&
break
done
# no wget is worth a warning
test "$HAVE_WGET" -eq 0 &&
printf -- '%s: WARN: no wget found, cannot download files\n' "$B"
# no local RFC directory is rather common and not worth a warning
# neither wget nor local RFCs means rfc-reader cannot work
test "$HAVE_WGET" -eq 0 && "$HAVE_LOCAL_DIRS" -eq 0 && {
printf -- '%s: ERROR: neither wget nor local file repo available\n' "$B"
exit 1
}
# parse arguments into type and number of document and build file name
if test $# -eq 2; then
DOC="$1$2"
else
DOC="$1"
fi
# remove optional extension .txt
DOC="$(printf -- '%s' "$DOC" | sed s/\.[Tt][Xx][Tt]$//)"
# shellcheck disable=SC2003
expr " $DOC" : ' *[0-9][0-9]* *$' > /dev/null && DOC="rfc$DOC"
# determine document type
# shellcheck disable=SC2003
DT="$(expr " $DOC" : ' \([Dd][Rr][Aa][Ff][Tt]\)' \
| tr '[:upper:]' '[:lower:]')"
# shellcheck disable=SC2003
test -z "$DT" &&
DT="$(expr " $DOC" : ' \([Ii]-[Dd]\)\.' \
| tr '[:lower:]' '[:upper:]')"
# shellcheck disable=SC2003
test -z "$DT" &&
DT="$(expr " $DOC" : ' \([A-Za-z][A-Za-z]*\)' \
| tr '[:upper:]' '[:lower:]')"
# determine document number (or name for I-Ds)
# shellcheck disable=SC2003
case "$DT" in
draft|I-D)
DOC="$(printf -- '%s' "$DOC" | sed 's/^[Ii]-[Dd]\./draft-/')"
DN="$(printf -- '%s' "$DOC" | tr '[:upper:]' '[:lower:]' \
| sed 's/^[[:space:]]\+//;s/\.txt[[:space:]]*$//;s/^i-d\./draft-/')"
expr " $DN" : ' .*-[0-9][0-9][0-9]*$' > /dev/null || DN='';;
*) DN="$(expr " $DOC" : ' [-A-Za-z .]*0*\([1-9][0-9]*\)')";;
esac
# determine latest I-D version number if I-D name w/o number was provided
IDLST='all_id.txt'
IDLSTBS='https://www.ietf.org/id'
test -z "$DN" && { test "$DT" = 'draft' || test "$DT" = 'I-D'; } &&
printf -- '%s: determining latest version of %s ... ' "$B" "$DOC" &&
if test \( -n "$RFCCACHE" \) -a \( -d "$RFCCACHE" \) -a \( -w "$RFCCACHE" \)
then
printf -- 'refreshing I-D list %s/%s ... ' "$RFCCACHE" "$IDLST"
if wget -q -o/dev/null -N -P "$RFCCACHE" -- "${IDLSTBS}/${IDLST}"; then
echo 'done'
else
echo 'failed, using cached I-D list if available'
fi
test -r "${RFCCACHE}/${IDLST}" ||
{ printf -- '%s: ERROR: no I-D list available\n' "$B"; exit 1; }
DN="$(grep -- "^${DOC}-[0-9][0-9]" "${RFCCACHE}/${IDLST}"|awk '{print $1}')"
else
DN="$(wget -q -o/dev/null -O- -- "${IDLSTBS}/${IDLST}" | grep -- "^${DOC}" \
| awk '{print $1}')"
fi
# determine download base URL for document
test -z "$DT" && {
printf -- '%s: ERROR: could not determine document type\n' "$B"
usage
exit 1
}
test -z "$DN" && {
printf -- '%s: ERROR: could not determine document number or name\n' "$B"
usage
exit 1
}
R="${DT}${DN}.txt"
case "$DT" in
rfc) DB='https://www.rfc-editor.org/rfc/';;
bcp|fyi|ien|std) DB="https://www.rfc-editor.org/rfc/${DT}/";;
draft|I-D) DB='https://www.ietf.org/archive/id/'; R="${DN}.txt";;
*) printf -- '%s: ERROR: unknown document type "%s"\n' "$B" "$DT"
usage
exit 1
;;
esac
# create temporary files for original and formatted RFC(-like) text
F=$(mktemp -t rfc-reader.$$.XXXXXXXXXX)
test -e "$F" || {
printf -- '%s: ERROR: could not create temp. file for unformatted RFC\n' "$B"
exit 1
}
trap 'rm -rf "$F"' 0
FMT=$(mktemp -t rfc-reader.$$.XXXXXXXXXX)
test -e "$F" || {
printf -- '%s: ERROR: could not create temp. file for formatted RFC\n' "$B"
exit 1
}
trap 'rm -rf "$F" "$FMT"' 0
# obtain RFC(-like) text
printf -- '%s: looking for %s ... ' "$B" "$R"
# shellcheck disable=SC2086
LR=$(find $RFCDIRS -name "${R}*" 2>/dev/null | head -n1)
LRZ=''
# search local directories (a local copy may be compressed)
test -n "$LR" && printf -- '%s' "found $LR ... " &&
LRZ=$(file -L "$LR" | sed -n 's/^.* \([^ ]*\) compressed .*$/\1/p')
# BCP contents change with time, so refresh a cached copy if possible
test "$DT" = 'bcp' && test -n "$LR" && test -z "$LRZ" && test -n "$RFCCACHE" &&
test "$(dirname "$LR")" = "$RFCCACHE" && {
printf -- 'refreshing BCP ... '
wget -q -o/dev/null -N -P"$RFCCACHE" -- "${DB}${R}" ||
printf -- 'ERROR, using cached BCP version ... '
}
# STD contents change with time, so refresh a cached copy if possible
test "$DT" = 'std' && test -n "$LR" && test -z "$LRZ" && test -n "$RFCCACHE" &&
test "$(dirname "$LR")" = "$RFCCACHE" && {
printf -- 'refreshing STD ... '
wget -q -o/dev/null -N -P"$RFCCACHE" -- "${DB}${R}" ||
printf -- 'ERROR, using cached STD version ... '
}
# decompress a compressed local copy
test -n "$LR" &&
case "$LRZ" in
'') cp "$LR" "$F";;
'gzip') printf -- '%s' 'decompressing ... '; zcat "$LR" > "$F";;
'bzip2') printf -- '%s' 'decompressing ... '; bzcat "$LR" > "$F";;
'XZ') printf -- '%s' 'decompressing ... '; xzcat "$LR" > "$F";;
*) printf -- 'WARN: ignoring "%s" with unknown compression "%s" ... ' \
"$LR" "$LRZ";;
esac
# if no usable local copy was found, then download document
test -s "$F" ||
{ printf -- 'downloading "%s" from "%s" ... ' "$R" "${DB}${R}" &&
wget -q -o/dev/null -O"$F" -- "${DB}${R}";
# optionally cache downloaded document
test \( -s "$F" \) -a \( -n "$RFCCACHE" \) -a \( -d "$RFCCACHE" \) -a \
\( -w "$RFCCACHE" \) &&
{ printf -- 'copying %s to cache %s ... ' "$R" "$RFCCACHE";
cp -- "$F" "${RFCCACHE}/$R"; }; }
test -s "$F" || { printf -- "ERROR\n"; exit 1; }
printf -- 'done\n'
# format RFC(-like) text for viewing in a terminal window
C=${C:=76} # 72 chars max. per line (RFC 1 has line length <= 77) + 4 blanks
L=${L:=59} # 58 lines max. per page + separator line in, e.g., STD 1
# use $PAGER or less
P=${PAGER:=less}
test -x "$(command -v "$P")" || P='less' # de-facto GNU/Linux pager
test -x "$(command -v "$P")" || P='most' # nice alternative pager
test -x "$(command -v "$P")" || P='pager' # pager from Debian's alternatives
test -x "$(command -v "$P")" || P='more' # POSIX and BSD compatibility
test -x "$(command -v "$P")" || P='page' # alternate name for 'more -p'
test -x "$(command -v "$P")" || P='pg' # System V compatibility
test -x "$(command -v "$P")" || {
printf -- '%s: ERROR: no pager found\n' "$B"
exit 1
}
PO=${PO:=''}
# rfc-reader has default options for less and most only
test "$P" = "less" && test -z "$PO" && PO="-M -S"
test "$P" = "most" && test -z "$PO" && PO="+s"
# most uses two status lines instead of one
PSL='1'
PA='0'
test "$P" = 'most' && PSL='2' PA='1'
# check if RFC / I-D is paginated (i.e., contains Form Feeds)
PAGINATED=1
awk '/\014/{exit 1}' "$F" && {
# do not warn about current RFCs w/o pagination
test "$DT" = 'rfc' && test "$DN" -ge 8650 || \
printf -- '%s: WARN: no Form Feed characters in RFC/I-D "%s"\n' "$B" "$R"
PAGINATED=0
# adjust pager options to unpaginated text
case "$P" in
more|less) PO="$PO -s -c";;
most) PO="$PO -s";;
esac
}
# find a usable terminal emulator program or disable X Window System use
DISPLAY=${DISPLAY:=}
test -n "$DISPLAY" && {
# determine window sizes
Y=$(xrandr | sed -n '/\*/s/^[^x]*x *\([0-9]*\)[^0-9].*$/\1/p' | head -n1)
test -z "$Y" && Y=0
if test "$Y" -ge 1100; then
XTFS=${XTFS:=5} #XTerm font size: 5 is 'large'
GTZ='1.0'
else
XTFS=${XTFS:=4} #XTerm font size: 4 is 'medium'
GTZ='0.9'
fi
# find a terminal program to use
XT=${XT:=''}
test -z "$XT" && test -n "${WINDOWID:-}" &&
XT=$(xprop -id "$WINDOWID" WM_CLASS | sed -n 's/^[^"]*"\([^"]*\)".*$/\1/p')
test -x "$(command -v "$XT")" || XT=""
test -z "$XT" && { env | grep -F gnome-terminal >/dev/null 2>&1; } &&
XT='gnome-terminal'
test -x "$(command -v "$XT")" || XT=""
test -z "$XT" && XT='xterm'
found=''
while test -z "$found"; do
case "$XT" in
xterm|uxterm)
# XTerm
# (tested on Debian/Lenny, Debian/Sid, Ubuntu 12.04 LTS, openSUSE 12.3)
XO='+sb -xrm'
XF="xterm*vt100.initialFont: ${XTFS}"
G="-g ${C}x$((L+PSL))"
T='-T '
E="-e $P $PO"
found='xterm'
;;
gnome-terminal)
# GNOME Terminal (tested on Debian/Lenny and Ubuntu 12.04 LTS)
XO="--hide-menubar --disable-factory --zoom ${GTZ}"
XF=''
G="--geometry ${C}x$((L+PSL))"
T='-t '
E="-x $P $PO"
found='gnome-terminal'
;;
konsole)
# Konsole (tested on openSUSE 12.3 with KDE 4.11.3)
# XO="--hide-menubar --hide-tabbar -p ScrollBarPosition=2 --nofork"
# XF=''
# G="-p TerminalColumns=${C} -p TerminalRows=${R}"
# T="-p tabtitle="
# E="-e $P $PO"
printf -- \
'%s: WARN: found "%s", but cannot set geometry, trying "xterm"\n' \
"$B" "$XT"
# found='konsole'
XT='xterm'
;;
*)
printf -- \
'%s: WARN: found unsupported terminal "%s", trying "xterm"\n' \
"$B" "$XT"
XT='xterm'
;;
esac
done
test -x "$(command -v "$XT")" || {
printf -- \
'%s: WARN: no usable terminal program found, using current terminal\n' \
"$B"
DISPLAY=''
}
}
# use current terminal if X Window System is not usable
test -z "$DISPLAY" && test "$PAGINATED" -eq '1' && {
# check terminal size
TL="${TL:=${LINES:=}}"
test -z "$TL" && TL="$(stty size 2>/dev/null | cut -d' ' -f1)"
test -z "$TL" && TL="$(tput lines 2>/dev/null)"
test -z "$TL" && TL="$(resize -u 2>/dev/null \
| sed -n '/LINES/s/^.*=\([0-9]\+\).*$/\1/p')"
test -z "$TL" &&
{ printf -- '%s: ERROR: cannot determine terminal height\n' "$B"; exit 1; }
test "$TL" -lt "$((L+PSL))" && {
printf -- '%s: ERROR: terminal too short (%d lines, but %d needed)\n' \
"$B" "$TL" "$((L+PSL))"
exit 1
}
TC="${TC:=${COLUMNS:=}}"
test -z "$TC" && TC="$(stty size 2>/dev/null | cut -d' ' -f2)"
test -z "$TC" && TC="$(tput cols 2>/dev/null)"
test -z "$TC" && TC="$(resize -u 2>/dev/null \
| sed -n '/COLUMNS/s/^.*=\([0-9]\+\).*$/\1/p')"
test -z "$TC" &&
{ printf -- '%s: ERROR: cannot determine terminal width\n' "$B"; exit 1; }
test "$TC" -lt "$C" && {
printf -- '%s: ERROR: terminal too narrow (%d columns, but %d needed)\n' \
"$B" "$TC" "$C"
exit 1
}
# format RFC(-like) text for current terminal's height
L="$((TL-PSL))"
}
# reformat RFC(-like) text for chosen terminal which has L lines for viewing
# text content (including SL status line(s) used by the pager)
# shellcheck disable=SC2026
if test "$PAGINATED" -eq 1; then
awk -vn_lines="$L" -vpg_adds="$PA" \
'!/\f/{line++; print};
/\f/{for (i=line; i<n_lines; i++) print ""; line=0}
END {if (line || pg_adds){for (i=line; i<n_lines-pg_adds; i++) print ""}}'\
"$F" | sed '1s/^'$'\uFEFF''//;/^$/!s/^/ /' > "$FMT"
else
sed '1s/^'$'\uFEFF''//;/^$/!s/^/ /' "$F" \
| awk '!text && /^[[:space:]]*$/{next}; !text{text=1}; {print}' > "$FMT"
fi
# open suitable terminal window if X-Windows is available
test -n "$DISPLAY" && {
# open terminal window with a pager displaying the RFC(-like) text
# shellcheck disable=SC2086
"$XT" $XO "$XF" $G $T"$B - $(basename "$R")" $E "$FMT"
# gnome-terminal cannot run in the foreground
wait
exit
}
# otherwise open pager in current terminal
# shellcheck disable=SC2086
"$P" $PO "$FMT"
# vim:expandtab:sw=2: