aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Ankarström <john@ankarstrom.se>2021-07-21 22:00:33 +0200
committerJohn Ankarström <john@ankarstrom.se>2021-07-21 22:00:33 +0200
commita73e7d163f2a2463143a261c995355505f55e9b6 (patch)
tree1aecef76d91ebf05d4a81e207adc0641b61e2925
parent03731c1074b0da55e0c0be76cf16e4e58e9f0f69 (diff)
downloadmk-a73e7d163f2a2463143a261c995355505f55e9b6.tar.gz
toc/toc: Clean up scriptHEADmaster
-rwxr-xr-xtoc/toc53
1 files changed, 27 insertions, 26 deletions
diff --git a/toc/toc b/toc/toc
index 4518cf6..201b6cc 100755
--- a/toc/toc
+++ b/toc/toc
@@ -1,51 +1,52 @@
#!/bin/sh
-
+#
# toc -- run troff in three passes
-
+#
# Permission to use, copy, modify and/or
# distribute this software for any purpose
# with or without fee is hereby granted.
+#
-usage() {
+if [ $# -eq 0 ]; then
echo "usage: $0 [cmd ... \|] <troff> [arg ...]" 1>&2
echo "(input should be given on stdin)" 1>&2
exit 1
-}
+fi
-tmp() {
+# Create temporary files.
+FILES=
+for t in f g h i j; do
x=`mktemp` || exit $?
- RM=$RM\ $x
- eval $1=\$x
-}
-
-end() {
- cat $1
- exit $s
-}
-
-RM=rm
+ FILES="$FILES $x"
+ eval $t=\$x
+done
+trap 'rm $FILES; trap -' INT QUIT EXIT
+
+#
+# An awk script separates toc output from troff output.
+# The former is printed on standard out, while the latter
+# is printer on standard error.
+#
AWK='
/^\(toc\)/ { print substr($0, 6); next }
{ print > "/dev/stderr" }
'
-[ $# -eq 0 ] && usage
-trap '$RM; RM=; exit 130' INT
-trap '$RM' EXIT
-for t in f g h i j; do tmp $t; done
-
+#
+# Troff is run as many times as needed, at most three.
+# Toc output is saved in a temporary file. After each pass, an
+# analysis of the output determines whether more passes are needed.
+#
+# The script always exits with the status code of the last troff pass.
+#
echo $0: first pass 1>&2
-
tee $f | { eval "$@" -rte=1 -dtf=; s=$?; } 2>&1 1>$i | awk "$AWK" >$g
-[ -s $g ] || end $i
+[ -s $g ] || { cat $j; exit $s; }
echo $0: second pass 1>&2
-
{ eval "$@" -rte=1 -dtf=$g; s=$?; } <$f 2>&1 1>$j | awk "$AWK" >$h
-diff -q $g $h >/dev/null && end $j
+diff -q $g $h >/dev/null && { cat $j; exit $s; }
echo $0: third pass 1>&2
-
{ eval "$@" -rte=0 -dtf=$h; s=$?; } <$f | grep -v '^(toc)'
-
exit $s