aboutsummaryrefslogtreecommitdiff
path: root/toc/toc
blob: 4518cf61f0c274731352a08608cdcc5767e99500 (plain)
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
#!/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() {
	echo "usage: $0 [cmd ... \|] <troff> [arg ...]" 1>&2
	echo "(input should be given on stdin)" 1>&2
	exit 1
}

tmp() {
	x=`mktemp` || exit $?
	RM=$RM\ $x
	eval $1=\$x
}

end() {
	cat $1
	exit $s
}

RM=rm
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

echo $0: first pass 1>&2

tee $f | { eval "$@" -rte=1 -dtf=; s=$?; } 2>&1 1>$i | awk "$AWK" >$g
[ -s $g ] || end $i

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

echo $0: third pass 1>&2

{ eval "$@" -rte=0 -dtf=$h; s=$?; } <$f | grep -v '^(toc)'

exit $s