From bffbccd88aa57541bffca931c6a4c205773ad619 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= Date: Wed, 23 Jun 2021 20:55:06 +0200 Subject: toc: Change toc script usage, skip unnecessary passes --- Makefile | 2 +- toc/Makefile | 8 ++++---- toc/gtoc | 4 ---- toc/toc | 46 ++++++++++++++++++++++++++++++++-------------- 4 files changed, 37 insertions(+), 23 deletions(-) delete mode 100755 toc/gtoc 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.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.pdf -example.pdf: example.t toc.tmac toc gtoc - ./gtoc -Tps example.t | ps2pdf - > example.pdf +example.pdf: example.t toc.tmac toc + 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 ... \|] [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 -- cgit v1.2.3