aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Ankarström <john@ankarstrom.se>2021-06-23 20:55:06 +0200
committerJohn Ankarström <john@ankarstrom.se>2021-06-23 20:55:06 +0200
commitbffbccd88aa57541bffca931c6a4c205773ad619 (patch)
tree113458e94b7f5439efa8b058e3a819827a42b342
parentdf9030831e294af0030d3c9df092a1ea910aa6df (diff)
downloadmk-bffbccd88aa57541bffca931c6a4c205773ad619.tar.gz
toc: Change toc script usage, skip unnecessary passes
-rw-r--r--Makefile2
-rw-r--r--toc/Makefile8
-rwxr-xr-xtoc/gtoc4
-rwxr-xr-xtoc/toc46
4 files changed, 37 insertions, 23 deletions
diff --git a/Makefile b/Makefile
index b3686c0..5ffed1e 100644
--- a/Makefile
+++ b/Makefile
@@ -5,4 +5,4 @@ hacking.pdf: hacking.t g.tmac
groff -Tps | ps2pdf - > hacking.pdf
README.pdf: README.t g.tmac
- toc/gtoc -Tps README.t | ps2pdf - > README.pdf
+ <README.t toc/toc groff -Tps | ps2pdf - > README.pdf
diff --git a/toc/Makefile b/toc/Makefile
index 6b6468f..d6d2a6e 100644
--- a/toc/Makefile
+++ b/toc/Makefile
@@ -1,5 +1,5 @@
-README.pdf: README.t toc.tmac toc gtoc
- ./gtoc -Tps README.t | ps2pdf - > README.pdf
+README.pdf: README.t toc.tmac toc
+ <README.t ./toc groff -Tps | ps2pdf - > README.pdf
-example.pdf: example.t toc.tmac toc gtoc
- ./gtoc -Tps example.t | ps2pdf - > example.pdf
+example.pdf: example.t toc.tmac toc
+ <example.t ./toc groff -Tps | ps2pdf - > example.pdf
diff --git a/toc/gtoc b/toc/gtoc
deleted file mode 100755
index f64ddbe..0000000
--- a/toc/gtoc
+++ /dev/null
@@ -1,4 +0,0 @@
-#!/bin/sh
-export TROFF=groff
-exec=${0%gtoc}toc
-exec "$exec" "$@"
diff --git a/toc/toc b/toc/toc
index d3de1a5..4e1f298 100755
--- a/toc/toc
+++ b/toc/toc
@@ -2,12 +2,17 @@
# toc -- run troff in three passes
-: ${TROFF:=troff}
-SED='sed -n /^(toc)/{s///;p}'
RM=rm
-
-trap '$RM; RM=; exit 130' INT
-trap '$RM' EXIT
+AWK='
+ /^\(toc\)/ { print substr($0, 6); next }
+ { print > "/dev/stderr" }
+'
+
+usage() {
+1>&2 echo "usage: $0 [cmd ... \|] <troff> [arg ...]"
+1>&2 echo "(input should be given on stdin)"
+ exit 1
+}
tmp() {
x=`mktemp` || exit $?
@@ -15,19 +20,32 @@ tmp() {
eval $1=\$x
}
-if [ ! -t 0 ]; then
- tmp f
- cat > $f
- set -- "$@" $f
-fi
+end() {
+ cat $1
+ exit $s
+}
+
+[ $# -eq 0 ] && usage
+trap '$RM; RM=; exit 130' INT
+trap '$RM' EXIT
+
+tmp f
+>$f cat
tmp g
tmp h
+tmp i
+tmp j
+
+echo $0: first pass 1>&2
+{ eval "$@" -rte=1 -dtf=; s=$?; } <$f 2>&1 1>$i | awk "$AWK" >$g
+[ -s $g ] || end $i
-$TROFF -rte=1 -dtf= "$@" 2>&1 1>/dev/null | $SED >$g
-$TROFF -rte=1 -dtf=$g "$@" 2>&1 1>/dev/null | $SED >$h
-$TROFF -rte=0 -dtf=$h "$@"
+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
-s=$?
+echo $0: third pass 1>&2
+{ eval "$@" -rte=0 -dtf=$h; s=$?; } <$f | grep -v '^(toc)'
exit $s