#! /bin/sh
NULLDEV="/dev/null"
test -c $NULLDEV || NULLDEV="NUL"
CMD=`exec 2>$NULLDEV; basename $0` || CMD=$0
searchpath(){
IFS="${PATH_SEPARATOR-":"}" prog=':'
for dir in $2
do
for ext in '' '.exe'
do
try="$dir/$1$ext"
test -f "$try" && test -x "$try" && prog="$try" && break
done
test "$prog" = ":" || break
done
echo "$prog"
}
CAT=`searchpath cat "$PATH"`
GREP=`searchpath grep "$PATH"`
SED=`searchpath sed "$PATH"`
if test -n "$GROFF_BIN_DIR"
then
GPATH=GROFF_BIN_DIR
GROFF=`searchpath groff "$GROFF_BIN_DIR"`
elif test -n "$GROFF_BIN_PATH"
then
GPATH=GROFF_BIN_PATH
GROFF=`searchpath groff "$GROFF_BIN_PATH"`
else
GPATH=PATH
GROFF=`searchpath groff "$PATH"`
fi
NO='' NOPROG="$CMD: installation problem: cannot find program"
test "$CAT" = ":" && echo >&2 "$NOPROG 'cat' in PATH" && NO="$NO 'cat'"
test "$GREP" = ":" && echo >&2 "$NOPROG 'grep' in PATH" && NO="$NO 'grep'"
test "$GROFF" = ":" && echo >&2 "$NOPROG 'groff' in $GPATH" && NO="$NO 'groff'"
test "$SED" = ":" && echo >&2 "$NOPROG 'sed' in PATH" && NO="$NO 'sed'"
if test -n "$NO"
then
set $NO
test $ while test $ do
test $ test $ test $ shift
done
$CAT >&2 <<-ETX
*** FATAL INSTALLATION ERROR ***
The program$NO $IS required by '$CMD',
but cannot be found; '$CMD' is unable to continue.
ETX
exit 1
fi
WRKFILE=${GROFF_TMPDIR=${TMPDIR-${TMP-${TEMP-"."}}}}/pdf$$.tmp
REFCOPY=${GROFF_TMPDIR}/pdf$$.cmp
REFFILE=${GROFF_TMPDIR}/pdf$$.ref
CS_DATA=""
TC_DATA=${GROFF_TMPDIR}/pdf$$.tc
BD_DATA=${GROFF_TMPDIR}/pdf$$.ps
trap "rm -f ${GROFF_TMPDIR}/pdf$$.*" 0
TOC_FORMAT="-rPHASE=1"
BODY_FORMAT="-rPHASE=2"
LONGOPTS="
help reference-dictionary no-reference-dictionary
stylesheet pdf-output no-pdf-output
version report-progress no-toc-relocation
"
DIFF="" STREAM="" INPUT_FILES=""
SHOW_VERSION="" GROFF_STYLE="$GROFF -Tps"
while test $ do
case "$1" in
--*)
MATCH="" OPTNAME=`IFS==; set dummy $1; echo $2`
for OPT in $LONGOPTS
do
MATCH="$MATCH"`echo --$OPT | $GREP "^$OPTNAME"`
done
OPTARG=`echo $1 | $SED -n s?"^${OPTNAME}="??p`
case "$MATCH" in
--help)
$CAT >&2 <<-ETX
Usage: $CMD [-option ...] [--long-option ...] [file ...]
Options:
-h
--help
Display this usage summary, and exit.
-v
--version
Display a version identification message and exit.
--report-progress
Enable console messages, indicating the progress of the
PDF document formatting process.
--pdf-output=name
Write the PDF output stream to file 'name'; if this option
is unspecified, standard output is used for PDF output.
--no-pdf-output
Suppress the generation of PDF output entirely; use this
with the --reference-dictionary option, if processing a
document stream to produce only a reference dictionary.
--no-reference-dictionary
Suppress the generation of a '$CMD' reference dictionary
for the PDF document. Normally '$CMD' will create a
reference dictionary, at the start of document processing;
this option can accelerate processing, if it is known in
advance, that no reference dictionary is required.
--reference-dictionary=name
Save the document reference dictionary in file 'name'.
If 'name' already exists, when processing commences, it
will be used as the base case, from which the updated
dictionary will be derived. If this option is not used,
then the reference dictionary, created during the normal
execution of '$CMD', will be deleted on completion of
document processing.
--stylesheet=name
Use the file 'name' as a 'groff' style sheet, to control
the appearance of the document's front cover section. If
this option is not specified, then no special formatting
is applied, to create a front cover section.
--no-toc-relocation
Suppress the multiple pass 'groff' processing, which is
normally required to position the table of contents at the
start of a PDF document.
ETX
exit 0
;;
--version)
GROFF_STYLE="$GROFF_STYLE \"$1\""
SHOW_VERSION="GNU pdfroff (groff) version @VERSION@"
;;
--report-progress)
SHOW_PROGRESS=echo
;;
--pdf-output)
PDF_OUTPUT="$OPTARG"
;;
--no-pdf-output)
PDF_OUTPUT="$NULLDEV"
;;
--reference-dictionary)
REFFILE="$OPTARG"
;;
--no-reference-dictionary)
AWK=":" DIFF=":" REFFILE="$NULLDEV" REFCOPY="$NULLDEV"
;;
--stylesheet)
STYLESHEET="$OPTARG" CS_DATA=${GROFF_TMPDIR}/pdf$$.cs
;;
--no-toc-relocation)
TC_DATA="" TOC_FORMAT="" BODY_FORMAT=""
;;
--*)
echo >&2 "$CMD: ambiguous abbreviation in option '$1'"
exit 1
;;
*)
echo >&2 "$CMD: unknown option '$1'"
exit 1
;;
esac
;;
- | -i*)
STREAM="$CAT ${GROFF_TMPDIR}/pdf$$.in |"
test "$1" = "-" && INPUT_FILES="$INPUT_FILES $1" \
|| GROFF_STYLE="$GROFF_STYLE $1"
;;
-[dfFILmMnoPrTwW])
OPTNAME="$1"
shift; set reparse "$OPTNAME$@"
;;
-Tps) ;;
-T*) echo >&2 "$CMD: option '$1' is incompatible with PDF output"
exit 1
;;
-h*) set redirect --help
;;
-v*) shift; set redirect --version "$@"
;;
-*) GROFF_STYLE="$GROFF_STYLE \"$1\""
;;
*) INPUT_FILES="$INPUT_FILES \"$1\""
;;
esac
shift
done
if test -n "$SHOW_VERSION"
then
echo >&2 "$SHOW_VERSION"
echo >&2; eval $GROFF_STYLE $INPUT_FILES
exit $?
fi
case `echo "testing\c"; echo 1,2,3`,`echo -n testing; echo 1,2,3` in
*c*,*-n*) n='' c='' ;;
*c*) n='-n' c='' ;;
*) n='' c='\c' ;;
esac
test -z "$INPUT_FILES" && STREAM="$CAT ${GROFF_TMPDIR}/pdf$$.in |"
test -n "$STREAM" && $CAT > ${GROFF_TMPDIR}/pdf$$.in
SAY=":"
if test -z "$DIFF"
then
>> $REFFILE
echo kickstart > $REFCOPY
test "${SHOW_PROGRESS+"set"}" = "set" && SAY=echo
NO=''
if test -n "$GROFF_AWK_INTERPRETER"
then
AWK="$GROFF_AWK_INTERPRETER"
test -f "$AWK" && test -x "$AWK" || AWK=":"
else
for prog in @GROFF_AWK_INTERPRETERS@
do
AWK=`searchpath $prog "$PATH"`
test "$AWK" = ":" || break
done
fi
DIFF=`searchpath diff "$PATH"`
test "$AWK" = ":" && echo >&2 "$NOPROG 'awk' in PATH" && NO="$NO 'awk'"
test "$DIFF" = ":" && echo >&2 "$NOPROG 'diff' in PATH" && NO="$NO 'diff'"
if test -n "$NO"
then
set $NO
SAY=":" AWK=":" DIFF=":"
test $ $CAT >&2 <<-ETX
*** WARNING ***
The program$NO required, but cannot be found;
consequently, '$CMD' is unable to resolve 'pdfmark' references.
Document processing will continue, but no 'pdfmark' reference dictionary
will be compiled; if any 'pdfmark' reference appears in the resulting PDF
document, the formatting may not be correct.
ETX
fi
fi
$SAY >&2 $n Resolving references ..$c
until $DIFF $REFCOPY $REFFILE 1>$NULLDEV 2>&1
do
$SAY >&2 $n .$c
PASS_INDICATOR="${PASS_INDICATOR}."
if test "$PASS_INDICATOR" = "...."
then
$SAY >&2 " failed"
$CAT >&2 <<-ETX
$CMD: unable to resolve references consistently after three passes
$CMD: the source document may exhibit instability about the reference(s) ...
ETX
DIFFOPT=''
$DIFF -c0 $NULLDEV $NULLDEV 1>$NULLDEV 2>&1 && DIFFOPT='-c0'
$DIFF -u0 $NULLDEV $NULLDEV 1>$NULLDEV 2>&1 && DIFFOPT='-u0'
$DIFF >&2 $DIFFOPT $REFCOPY $REFFILE
exit 1
fi
rm -f $REFCOPY
mv $REFFILE $REFCOPY
eval $STREAM $GROFF_STYLE -Z 1>$NULLDEV 2>$WRKFILE $REFCOPY $INPUT_FILES
$AWK '/^gropdf-info:href/ {$1 = ".pdfhref D -N"; print}' $WRKFILE > $REFFILE
done
$SAY >&2 " done"
test "$PDF_OUTPUT" = "$NULLDEV" && exit 0
if test -n "$GROFF_GHOSTSCRIPT_INTERPRETER"
then
GS="$GROFF_GHOSTSCRIPT_INTERPRETER"
test -f "$GS" && test -x "$GS" || GS=":"
else
for prog in @GROFF_GHOSTSCRIPT_INTERPRETERS@
do
GS=`searchpath $prog "$PATH"`
test "$GS" = ":" || break
done
fi
if test "$GS" = ":"
then
echo >&2 "$CMD: installation problem: cannot find GhostScript interpreter"
$CAT >&2 <<-ETX
*** FATAL INSTALLATION ERROR ***
'$CMD' requires a GhostScript interpreter to convert PostScript to PDF.
Since you do not appear to have one installed, '$CMD' connot continue.
ETX
exit 1
fi
$AWK '/^grohtml-info/ {print ".pdfhref Z", $2, $3, $4}' $WRKFILE >> $REFCOPY
test "${SHOW_PROGRESS+"set"}" = "set" && SAY=echo
if test -n "$STYLESHEET"
then
DOT='^\.[ ]*'
CS_MACRO=${CS_MACRO-"CS"} CE_MACRO=${CE_MACRO-"CE"}
$SAY >&2 $n "Formatting document ... front cover section ..$c"
CS_FILTER="$STREAM $SED -n '/${DOT}${CS_MACRO}/,/${DOT}${CE_MACRO}/p'"
eval $CS_FILTER $INPUT_FILES | eval $GROFF_STYLE $STYLESHEET - > $CS_DATA
$SAY >&2 ". done"
fi
if test -n "$TC_DATA"
then
$SAY >&2 $n "Formatting document ... table of contents ..$c"
eval $STREAM $GROFF_STYLE $TOC_FORMAT $REFCOPY $INPUT_FILES > $TC_DATA
$SAY >&2 ". done"
fi
$SAY >&2 $n "Formatting document ... body section ..$c"
eval $STREAM $GROFF_STYLE $BODY_FORMAT $REFCOPY $INPUT_FILES > $BD_DATA
$SAY >&2 ". done"
$SAY >&2 $n "Writing PDF output ..$c"
PDFWRITE="$GS -dQUIET -dBATCH -dNOPAUSE -sDEVICE=pdfwrite"
$SED '
:again
/%%EndPageSetup/b finish
/%%Page:/{
N
b again
}
b
:finish
N
/^%%Page:.*0 *Cg *EP/d
' $TC_DATA $BD_DATA | $PDFWRITE -sOutputFile=${PDF_OUTPUT-"-"} $CS_DATA -
$SAY >&2 ". done"