Home | History | Annotate | Line # | Download | only in util
      1 #! /bin/sh
      2 # texi2dvi --- produce DVI (or PDF) files from Texinfo (or LaTeX) sources.
      3 # Id: texi2dvi,v 1.34 2004/12/01 18:35:36 karl Exp 
      4 #
      5 # Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2001,
      6 # 2002, 2003, 2004 Free Software Foundation, Inc.
      7 #
      8 # This program is free software; you can redistribute it and/or modify
      9 # it under the terms of the GNU General Public License as published by
     10 # the Free Software Foundation; either version 2, or (at your option)
     11 # any later version.
     12 #
     13 # This program is distributed in the hope that it will be useful,
     14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
     15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     16 # GNU General Public License for more details.
     17 #
     18 # You should have received a copy of the GNU General Public License
     19 # along with this program; if not, you can either send email to this
     20 # program's maintainer or write to: The Free Software Foundation,
     21 # Inc.; 59 Temple Place, Suite 330; Boston, MA 02111-1307, USA.
     22 #
     23 # Original author: Noah Friedman.
     24 #
     25 # Please send bug reports, etc. to bug-texinfo (at] gnu.org.
     26 # If possible, please send a copy of the output of the script called with
     27 # the `--debug' option when making a bug report.
     28 
     29 # This string is expanded by rcs automatically when this file is checked out.
     30 rcs_revision='Revision: 1.34 '
     31 rcs_version=`set - $rcs_revision; echo $2`
     32 program=`echo $0 | sed -e 's!.*/!!'`
     33 version="texi2dvi (GNU Texinfo 4.8) $rcs_version
     34 
     35 Copyright (C) 2004 Free Software Foundation, Inc.
     36 There is NO warranty.  You may redistribute this software
     37 under the terms of the GNU General Public License.
     38 For more information about these matters, see the files named COPYING."
     39 
     40 usage="Usage: $program [OPTION]... FILE...
     41 
     42 Run each Texinfo or LaTeX FILE through TeX in turn until all
     43 cross-references are resolved, building all indices.  The directory
     44 containing each FILE is searched for included files.  The suffix of FILE
     45 is used to determine its language (LaTeX or Texinfo).
     46 
     47 Makeinfo is used to perform Texinfo macro expansion before running TeX
     48 when needed.
     49 
     50 Operation modes:
     51   -b, --batch         no interaction
     52   -c, --clean         remove all auxiliary files
     53   -D, --debug         turn on shell debugging (set -x)
     54   -h, --help          display this help and exit successfully
     55   -o, --output=OFILE  leave output in OFILE (implies --clean);
     56                       Only one input FILE may be specified in this case
     57   -q, --quiet         no output unless errors (implies --batch)
     58   -s, --silent        same as --quiet
     59   -v, --version       display version information and exit successfully
     60   -V, --verbose       report on what is done
     61 
     62 TeX tuning:
     63   -@                   use @input instead of \input; for preloaded Texinfo
     64   -e, -E, --expand     force macro expansion using makeinfo
     65   -I DIR               search DIR for Texinfo files
     66   -l, --language=LANG  specify the LANG of FILE (LaTeX or Texinfo)
     67   -p, --pdf            use pdftex or pdflatex for processing
     68   -r, --recode         call recode before TeX to translate input characters
     69   -t, --command=CMD    insert CMD in copy of input file
     70    or --texinfo=CMD    multiple values accumulate
     71 
     72 The values of the BIBTEX, LATEX (or PDFLATEX), MAKEINDEX, MAKEINFO,
     73 TEX (or PDFTEX), TEXINDEX, and THUMBPDF environment variables are used
     74 to run those commands, if they are set.  Any CMD strings are added
     75 after @setfilename for Texinfo input, in the first line for LaTeX input.
     76 
     77 Email bug reports to <bug-texinfo (at] gnu.org>,
     78 general questions and discussion to <help-texinfo (at] gnu.org>.
     79 Texinfo home page: http://www.gnu.org/software/texinfo/"
     80 
     81 # Initialize variables for option overriding and otherwise.
     82 # Don't use `unset' since old bourne shells don't have this command.
     83 # Instead, assign them an empty value.
     84 batch=false     # eval for batch mode
     85 clean=
     86 debug=
     87 escape='\'
     88 expand=         # t for expansion via makeinfo
     89 miincludes=     # makeinfo include path
     90 oformat=dvi
     91 oname=          # --output
     92 quiet=          # by default let the tools' message be displayed
     93 recode=false
     94 set_language=
     95 textra=         # Extra TeX commands to insert in the input file.
     96 textra_cmd=     # sed command to insert TEXTRA where appropriate
     97 tmpdir=${TMPDIR:-/tmp}/t2d$$  # avoid collisions on 8.3 filesystems.
     98 txincludes=     # TEXINPUTS extensions, with trailing colon
     99 txiprereq=19990129 # minimum texinfo.tex version with macro expansion
    100 verbose=false   # echo for verbose mode
    101 
    102 orig_pwd=`pwd`
    103 
    104 # Systems which define $COMSPEC or $ComSpec use semicolons to separate
    105 # directories in TEXINPUTS.
    106 if test -n "$COMSPEC$ComSpec"; then
    107   path_sep=";"
    108 else
    109   path_sep=":"
    110 fi
    111 
    112 # Pacify verbose cds.
    113 CDPATH=${ZSH_VERSION+.}$path_sep
    114 
    115 # In case someone crazy insists on using grep -E.
    116 : ${EGREP=egrep}
    117 
    118 # return true if program $1 is somewhere in PATH, else false.
    119 #
    120 findprog () {
    121   foundprog=false
    122   for dir in `echo $PATH | tr "$path_sep" " "`; do
    123     if test -x "$dir/$1"; then  # does anyone still need test -f?
    124       foundprog=true
    125       break
    126     fi
    127   done
    128   $foundprog
    129 }
    130 
    131 # Report an error and exit with failure.
    132 fatal () {
    133   echo "$0: $*" >&2
    134   exit 1
    135 }
    136 
    137 # Save TEXINPUTS so we can construct a new TEXINPUTS path for each file.
    138 # Likewise for bibtex and makeindex.
    139 tex_envvars="BIBINPUTS BSTINPUTS INDEXSTYLE TEXINPUTS"
    140 for var in $tex_envvars; do
    141   eval ${var}_orig=\$$var
    142   export $var
    143 done
    144 
    145 
    146 # Push a token among the arguments that will be used to notice when we
    147 # ended options/arguments parsing.
    148 # Use "set dummy ...; shift" rather than 'set - ..." because on
    149 # Solaris set - turns off set -x (but keeps set -e).
    150 # Use ${1+"$@"} rather than "$@" because Digital Unix and Ultrix 4.3
    151 # still expand "$@" to a single argument (the empty string) rather
    152 # than nothing at all.
    153 arg_sep="$$--$$"
    154 set dummy ${1+"$@"} "$arg_sep"; shift
    155 
    156 # 
    158 # Parse command line arguments.
    159 while test x"$1" != x"$arg_sep"; do
    160 
    161   # Handle --option=value by splitting apart and putting back on argv.
    162   case "$1" in
    163     --*=*)
    164       opt=`echo "$1" | sed -e 's/=.*//'`
    165       val=`echo "$1" | sed -e 's/[^=]*=//'`
    166       shift
    167       set dummy "$opt" "$val" ${1+"$@"}; shift
    168       ;;
    169   esac
    170 
    171   # This recognizes --quark as --quiet.  So what.
    172   case "$1" in
    173     -@ ) escape=@;;
    174     # Silently and without documentation accept -b and --b[atch] as synonyms.
    175     -b | --b*) batch=true;;
    176     -c | --c*) clean=t;;
    177     -D | --d*) debug=t;;
    178     -e | -E | --e*) expand=t;;
    179     -h | --h*) echo "$usage"; exit 0;;
    180     -I | --I*)
    181       shift
    182       miincludes="$miincludes -I $1"
    183       txincludes="$txincludes$1$path_sep"
    184       ;;
    185     -l | --l*) shift; set_language=$1;;
    186     -o | --o*)
    187       shift
    188       clean=t
    189       case "$1" in
    190         /* | ?:/*) oname=$1;;
    191                 *) oname="$orig_pwd/$1";;
    192       esac;;
    193     -p | --p*) oformat=pdf;;
    194     -q | -s | --q* | --s*) quiet=t; batch=true;;
    195     -r | --r*) recode=true;;
    196     -t | --tex* | --com* ) shift; textra="$textra\\
    197 "`echo "$1" | sed 's/\\\\/\\\\\\\\/g'`;;
    198     -v | --vers*) echo "$version"; exit 0;;
    199     -V | --verb*) verbose=echo;;
    200     --) # What remains are not options.
    201       shift
    202       while test x"$1" != x"$arg_sep"; do
    203         set dummy ${1+"$@"} "$1"; shift
    204         shift
    205       done
    206       break;;
    207     -*)
    208       echo "$0: Unknown or ambiguous option \`$1'." >&2
    209       echo "$0: Try \`--help' for more information." >&2
    210       exit 1;;
    211     *) set dummy ${1+"$@"} "$1"; shift;;
    212    esac
    213    shift
    214 done
    215 # Pop the token
    216 shift
    217 
    218 # Interpret remaining command line args as filenames.
    219 case $# in
    220  0)
    221   echo "$0: Missing file arguments." >&2
    222   echo "$0: Try \`--help' for more information." >&2
    223   exit 2
    224   ;;
    225  1) ;;
    226  *)
    227   if test -n "$oname"; then
    228     echo "$0: Can't use option \`--output' with more than one argument." >&2
    229     exit 2
    230   fi
    231   ;;
    232 esac
    233 
    234 
    235 # We can't do much without tex.
    236 #
    237 if findprog ${TEX:-tex}; then :; else cat <<EOM
    238 You don't have a working TeX binary (${TEX:-tex}) installed anywhere in
    239 your PATH, and texi2dvi cannot proceed without one.  If you want to use
    240 this script, you'll need to install TeX (if you don't have it) or change
    241 your PATH or TEX environment variable (if you do).  See the --help
    242 output for more details.
    243 
    244 For information about obtaining TeX, please see http://www.tug.org.  If
    245 you happen to be using Debian, you can get it with this command:
    246   apt-get install tetex-bin
    247 EOM
    248   exit 1
    249 fi
    250 
    251 
    252 # We want to use etex (or pdftex) if they are available, and the user
    253 # didn't explicitly specify.  We don't check for elatex and pdfelatex
    254 # because (as of 2003), the LaTeX team has asked that new distributions
    255 # use etex by default anyway.
    256 #
    257 # End up with the TEX and PDFTEX variables set to what we are going to use.
    258 if test -z "$TEX"; then
    259   if findprog etex; then TEX=etex; else TEX=tex; fi
    260 fi
    261 #
    262 if test -z "$PDFTEX"; then
    263   if findprog pdfetex; then PDFTEX=pdfetex; else PDFTEX=pdftex; fi
    264 fi
    265 
    266 
    267 # Prepare the temporary directory.  Remove it at exit, unless debugging.
    268 if test -z "$debug"; then
    269   trap "cd / && rm -rf $tmpdir" 0 1 2 15
    270 fi
    271 
    272 # Create the temporary directory with strict rights
    273 (umask 077 && mkdir $tmpdir) || exit 1
    274 
    275 # Prepare the tools we might need.  This may be extra work in some
    276 # cases, but improves the readability of the script.
    277 utildir=$tmpdir/utils
    278 mkdir $utildir || exit 1
    279 
    280 # A sed script that preprocesses Texinfo sources in order to keep the
    281 # iftex sections only.  We want to remove non TeX sections, and comment
    282 # (with `@c texi2dvi') TeX sections so that makeinfo does not try to
    283 # parse them.  Nevertheless, while commenting TeX sections, don't
    284 # comment @macro/@end macro so that makeinfo does propagate them.
    285 # Unfortunately makeinfo --iftex --no-ifinfo doesn't work well enough
    286 # (yet), makeinfo can't parse the TeX commands, so work around with sed.
    287 #
    288 comment_iftex_sed=$utildir/comment.sed
    289 cat <<EOF >$comment_iftex_sed
    290 /^@tex/,/^@end tex/{
    291   s/^/@c texi2dvi/
    292 }
    293 /^@iftex/,/^@end iftex/{
    294   s/^/@c texi2dvi/
    295   /^@c texi2dvi@macro/,/^@c texi2dvi@end macro/{
    296     s/^@c texi2dvi//
    297   }
    298 }
    299 /^@ifnottex/,/^@end ifnottex/{
    300   s/^/@c (texi2dvi)/
    301 }
    302 /^@ifinfo/,/^@end ifinfo/{
    303   /^@node/p
    304   /^@menu/,/^@end menu/p
    305   t
    306   s/^/@c (texi2dvi)/
    307 }
    308 s/^@ifnotinfo/@c texi2dvi@ifnotinfo/
    309 s/^@end ifnotinfo/@c texi2dvi@end ifnotinfo/
    310 EOF
    311 # Uncommenting is simple: Remove any leading `@c texi2dvi'.
    312 uncomment_iftex_sed=$utildir/uncomment.sed
    313 cat <<EOF >$uncomment_iftex_sed
    314 s/^@c texi2dvi//
    315 EOF
    316 
    317 # Compute the list of xref files.
    318 # Takes the filename (without extension) of which we look for xref
    319 # files as argument.  The index files must be reported last.
    320 get_xref_files ()
    321 {
    322   # Get list of xref files (indexes, tables and lists).
    323   # Find all files having root filename with a two-letter extension,
    324   # saves the ones that are really Texinfo-related files.  .?o? catches
    325   # many files: .toc, .log, LaTeX tables and lists, FiXme's .lox, maybe more.
    326   for this_file in "$1".?o? "$1".aux "$1".?? "$1".idx; do
    327     # If file is empty, skip it.
    328     test -s "$this_file" || continue
    329     # If the file is not suitable to be an index or xref file, don't
    330     # process it.  It's suitable if the first character is a
    331     # backslash or right quote or at, as long as the first line isn't
    332     # \input texinfo.
    333     first_character=`sed -n '1s/^\(.\).*$/\1/p;q' $this_file`
    334     if (test "x$first_character" = "x\\" \
    335         && sed 1q $this_file | grep -v '^\\input *texinfo' >/dev/null) \
    336        || test "x$first_character" = "x'" \
    337        || test "x$first_character" = "x@"; then
    338       xref_files="$xref_files ./$this_file"
    339     fi
    340   done
    341   echo "$xref_files"
    342 }
    343 
    344 # File descriptor usage:
    345 # 0 standard input
    346 # 1 standard output (--verbose messages)
    347 # 2 standard error
    348 # 3 some systems may open it to /dev/tty
    349 # 4 used on the Kubota Titan
    350 # 5 tools output (turned off by --quiet)
    351 
    352 # Tools' output.  If quiet, discard, else redirect to the message flow.
    353 if test "$quiet" = t; then
    354   exec 5>/dev/null
    355 else
    356   exec 5>&1
    357 fi
    358 
    359 # Enable tracing
    360 if test "$debug" = t; then
    361   exec 6>&1
    362   set -x
    363 else
    364   exec 6>/dev/null
    365 fi
    366 
    367 # 
    369 # TeXify files.
    370 
    371 for command_line_filename in ${1+"$@"}; do
    372   $verbose "Processing $command_line_filename ..."
    373 
    374   # If the COMMAND_LINE_FILENAME is not absolute (e.g., --debug.tex),
    375   # prepend `./' in order to avoid that the tools take it as an option.
    376   echo "$command_line_filename" | $EGREP '^(/|[A-z]:/)' >&6 \
    377   || command_line_filename="./$command_line_filename"
    378 
    379   # See if the file exists.  If it doesn't we're in trouble since, even
    380   # though the user may be able to reenter a valid filename at the tex
    381   # prompt (assuming they're attending the terminal), this script won't
    382   # be able to find the right xref files and so forth.
    383   if test ! -r "$command_line_filename"; then
    384     echo "$0: Could not read $command_line_filename, skipping." >&2
    385     continue
    386   fi
    387 
    388   # Get the name of the current directory.  We want the full path
    389   # because in clean mode we are in tmp, in which case a relative
    390   # path has no meaning.
    391   filename_dir=`echo $command_line_filename | sed 's!/[^/]*$!!;s!^$!.!'`
    392   filename_dir=`cd "$filename_dir" >/dev/null && pwd`
    393 
    394   # Strip directory part but leave extension.
    395   filename_ext=`basename "$command_line_filename"`
    396   # Strip extension.
    397   filename_noext=`echo "$filename_ext" | sed 's/\.[^.]*$//'`
    398   ext=`echo "$filename_ext" | sed 's/^.*\.//'`
    399 
    400   # _src.  Use same basename since we want to generate aux files with
    401   # the same basename as the manual.  If --expand, then output the
    402   # macro-expanded file to here, else copy the original file.
    403   tmpdir_src=$tmpdir/src
    404   filename_src=$tmpdir_src/$filename_noext.$ext
    405 
    406   # _xtr.  The file with the user's extra commands.
    407   tmpdir_xtr=$tmpdir/xtr
    408   filename_xtr=$tmpdir_xtr/$filename_noext.$ext
    409 
    410   # _rcd.  The Texinfo file recoded in 7bit.
    411   tmpdir_rcd=$tmpdir/rcd
    412   filename_rcd=$tmpdir_rcd/$filename_noext.$ext
    413 
    414   # _bak.  Copies of the previous xref files (another round is run if
    415   # they differ from the new one).
    416   tmpdir_bak=$tmpdir/bak
    417 
    418   # Make all those directories and give up if we can't succeed.
    419   mkdir $tmpdir_src $tmpdir_xtr $tmpdir_rcd $tmpdir_bak || exit 1
    420 
    421   # Source file might include additional sources.
    422   # We want `.:$orig_pwd' before anything else.  (We'll add `.:' later
    423   # after all other directories have been turned into absolute paths.)
    424   # `.' goes first to ensure that any old .aux, .cps,
    425   # etc. files in ${directory} don't get used in preference to fresher
    426   # files in `.'.  Include orig_pwd in case we are in clean mode, where
    427   # we've cd'd to a temp directory.
    428   common="$orig_pwd$path_sep$filename_dir$path_sep$txincludes"
    429   for var in $tex_envvars; do
    430     eval ${var}="\$common\$${var}_orig"
    431     export $var
    432   done
    433 
    434   # Convert relative paths to absolute paths, so we can run in another
    435   # directory (e.g., in --clean mode, or during the macro-support detection.)
    436   #
    437   # Empty path components are meaningful to tex.  We rewrite them
    438   # as `EMPTY' so they don't get lost when we split on $path_sep.
    439   # Hopefully no one will have an actual directory named EMPTY.
    440   replace_empty="-e 's/^$path_sep/EMPTY$path_sep/g' \
    441                  -e 's/$path_sep\$/${path_sep}EMPTY/g' \
    442                  -e 's/$path_sep$path_sep/${path_sep}EMPTY:/g'"
    443    TEXINPUTS=`echo $TEXINPUTS  | eval sed $replace_empty`
    444   INDEXSTYLE=`echo $INDEXSTYLE | eval sed $replace_empty`
    445   save_IFS=$IFS
    446   IFS=$path_sep
    447   set x $TEXINPUTS; shift
    448   TEXINPUTS=.
    449   for dir
    450   do
    451     case $dir in
    452       EMPTY)
    453         TEXINPUTS=$TEXINPUTS$path_sep
    454         ;;
    455       [\\/]* | ?:[\\/]*)        # Absolute paths don't need to be expanded.
    456         TEXINPUTS=$TEXINPUTS$path_sep$dir
    457         ;;
    458       *)
    459         abs=`cd "$dir" && pwd` && TEXINPUTS=$TEXINPUTS$path_sep$abs
    460         ;;
    461     esac
    462   done
    463   set x $INDEXSTYLE; shift
    464   INDEXSTYLE=.
    465   for dir
    466   do
    467     case $dir in
    468       EMPTY)
    469         INDEXSTYLE=$INDEXSTYLE$path_sep
    470         ;;
    471       [\\/]* | ?:[\\/]*)        # Absolute paths don't need to be expansed.
    472         INDEXSTYLE=$INDEXSTYLE$path_sep$dir
    473         ;;
    474       *)
    475         abs=`cd "$dir" && pwd` && INDEXSTYLE=$INDEXSTYLE$path_sep$abs
    476         ;;
    477     esac
    478   done
    479   IFS=$save_IFS
    480 
    481   # If the user explicitly specified the language, use that.
    482   # Otherwise, if the first line is \input texinfo, assume it's texinfo.
    483   # Otherwise, guess from the file extension.
    484   if test -n "$set_language"; then
    485     language=$set_language
    486   elif sed 1q "$command_line_filename" | grep 'input texinfo' >&6; then
    487     language=texinfo
    488   else
    489     language=
    490   fi
    491 
    492   # Get the type of the file (latex or texinfo) from the given language
    493   # we just guessed, or from the file extension if not set yet.
    494   case ${language:-$filename_ext} in
    495     [lL]a[tT]e[xX] | *.ltx | *.tex)
    496       # Assume a LaTeX file.  LaTeX needs bibtex and uses latex for
    497       # compilation.  No makeinfo.
    498       language=latex
    499       bibtex=${BIBTEX:-bibtex}
    500       makeinfo= # no point in running makeinfo on latex source.
    501       texindex=${MAKEINDEX:-makeindex}
    502       textra_cmd=1i
    503       if test $oformat = dvi; then
    504         tex=${LATEX:-latex}
    505       else
    506         tex=${PDFLATEX:-pdflatex}
    507       fi
    508       thumbpdf=${THUMBPDF:-thumbpdf}
    509       ;;
    510 
    511     *)
    512       # Assume a Texinfo file.  Texinfo files need makeinfo, texindex and tex.
    513       language=texinfo
    514       bibtex=
    515       texindex=${TEXINDEX:-texindex}
    516       textra_cmd='/^@setfilename/a'
    517       if test $oformat = dvi; then
    518         # MetaPost also uses the TEX environment variable.  If the user
    519         # has set TEX=latex for that reason, don't bomb out.
    520         if echo $TEX | grep 'latex$' >/dev/null; then
    521           tex=tex  # don't bother trying to find etex
    522         else
    523           tex=$TEX
    524         fi
    525       else
    526         tex=$PDFTEX
    527       fi
    528       # Unless required by the user, makeinfo expansion is wanted only
    529       # if texinfo.tex is too old.
    530       if test "$expand" = t; then
    531         makeinfo=${MAKEINFO:-makeinfo}
    532       else
    533         # Check if texinfo.tex performs macro expansion by looking for
    534         # its version.  The version is a date of the form YEAR-MO-DA.
    535         # We don't need to use [0-9] to match the digits since anyway
    536         # the comparison with $txiprereq, a number, will fail with non
    537         # digits.
    538         # Run in the tmpdir to avoid leaving files.
    539 	(
    540            cd $tmpdir
    541 	   echo '\input texinfo.tex @bye' >txiversion.tex
    542 	   # Be sure that if tex wants to fail, it is not interactive:
    543 	   # close stdin.
    544            $tex txiversion.tex </dev/null
    545 	) >$tmpdir/txiversion.out 2>$tmpdir/txiversion.err
    546 	if test $? != 0; then
    547 	  cat $tmpdir/txiversion.out
    548 	  cat $tmpdir/txiversion.err >&2
    549 	  fatal "texinfo.tex appears to be broken, quitting."
    550         fi
    551 	eval `sed -n 's/^.*\[\(.*\)version \(....\)-\(..\)-\(..\).*$/txiformat=\1 txiversion="\2\3\4"/p' $tmpdir/txiversion.out`
    552         $verbose "texinfo.tex preloaded as \`$txiformat', version is \`$txiversion' ..."
    553         if test "$txiprereq" -le "$txiversion" >/dev/null 2>&1; then
    554           makeinfo=
    555         else
    556           makeinfo=${MAKEINFO:-makeinfo}
    557         fi
    558         # As long as we had to run TeX, offer the user this convenience:
    559         test "$txiformat" = Texinfo && escape=@
    560       fi
    561       thumbpdf=${THUMBPDF:-thumbpdf}
    562       ;;
    563   esac
    564 
    565   # Go to $tmpdir to try --help, since old versions that don't accept
    566   # --help will generate a texput.log.
    567   tex_help=`cd $tmpdir >/dev/null && $tex --help </dev/null 2>&1`
    568 
    569   # If possible, make TeX report error locations in GNU format.
    570   tex_args=
    571   case $tex_help in
    572     *file-line-error*) tex_args="$tex_args --file-line-error";;
    573   esac
    574 
    575   # Tell TeX to be batch if requested.  (\batchmode does not show
    576   # terminal output at all, so we don't want that.)
    577   $batch && tex_args="$tex_args ${escape}nonstopmode ${escape}input"
    578 
    579   # Expand macro commands in the original source file using Makeinfo.
    580   # Always use `end' footnote style, since the `separate' style
    581   #   generates different output (arguably this is a bug in -E).
    582   # Discard main info output, the user asked to run TeX, not makeinfo.
    583   if test -n "$makeinfo"; then
    584     $verbose "Macro-expanding $command_line_filename to $filename_src ..."
    585     sed -f $comment_iftex_sed "$command_line_filename" \
    586       | $makeinfo --footnote-style=end -I "$filename_dir" $miincludes \
    587         -o /dev/null --macro-expand=- \
    588       | sed -f $uncomment_iftex_sed >"$filename_src"
    589     filename_input=$filename_src
    590   fi
    591 
    592   # If makeinfo failed (or was not even run), use the original file as input.
    593   if test $? -ne 0 \
    594      || test ! -r "$filename_src"; then
    595     $verbose "Reverting to $command_line_filename ..."
    596     filename_input=$filename_dir/$filename_ext
    597   fi
    598 
    599   # Used most commonly for @finalout, @smallbook, etc.
    600   if test -n "$textra"; then
    601     $verbose "Inserting extra commands: $textra"
    602     sed "$textra_cmd\\
    603 $textra" "$filename_input" >"$filename_xtr"
    604     filename_input=$filename_xtr
    605   fi
    606 
    607   # If this is a Texinfo file with a specified input encoding, and
    608   # recode is available, then recode to plain 7 bit Texinfo.
    609   if test $language = texinfo; then
    610     pgm='s/\(^\|.* \)@documentencoding  *\([^ ][^ ]*\)\( .*\|$\)/\2/
    611 	t found
    612 	d
    613 	:found
    614 	q'
    615     encoding=`sed -e "$pgm" "$filename_input"`
    616     if $recode && test -n "$encoding" && findprog recode; then
    617       $verbose "Recoding from $encoding to Texinfo."
    618       if recode "$encoding"..texinfo <"$filename_input" >"$filename_rcd" \
    619          && test -s "$filename_rcd"; then
    620         filename_input=$filename_rcd
    621       else
    622         $verbose "Recoding failed, using original input."
    623       fi
    624     fi
    625   fi
    626 
    627   # If clean mode was specified, then move to the temporary directory.
    628   if test "$clean" = t; then
    629     $verbose "cd $tmpdir_src"
    630     cd "$tmpdir_src" || exit 1
    631   fi
    632 
    633   while :; do # will break out of loop below
    634     orig_xref_files=`get_xref_files "$filename_noext"`
    635 
    636     # Save copies of originals for later comparison.
    637     if test -n "$orig_xref_files"; then
    638       $verbose "Backing up xref files: `echo $orig_xref_files | sed 's|\./||g'`"
    639       cp $orig_xref_files $tmpdir_bak
    640     fi
    641 
    642     # Run bibtex on current file.
    643     # - If its input (AUX) exists.
    644     # - If AUX contains both `\bibdata' and `\bibstyle'.
    645     # - If some citations are missing (LOG contains `Citation').
    646     #   or the LOG complains of a missing .bbl
    647     #
    648     # We run bibtex first, because I can see reasons for the indexes
    649     # to change after bibtex is run, but I see no reason for the
    650     # converse.
    651     #
    652     # Don't try to be too smart.  Running bibtex only if the bbl file
    653     # exists and is older than the LaTeX file is wrong, since the
    654     # document might include files that have changed.  Because there
    655     # can be several AUX (if there are \include's), but a single LOG,
    656     # looking for missing citations in LOG is easier, though we take
    657     # the risk to match false messages.
    658     if test -n "$bibtex" \
    659        && test -r "$filename_noext.aux" \
    660        && test -r "$filename_noext.log" \
    661        && (grep '^\\bibdata[{]'  "$filename_noext.aux" \
    662            && grep '^\\bibstyle[{]' "$filename_noext.aux" \
    663            && (grep 'Warning:.*Citation.*undefined' "$filename_noext.log" \
    664                || grep 'No file .*\.bbl\.' "$filename_noext.log")) \
    665           >&6 2>&1; \
    666     then
    667       $verbose "Running $bibtex $filename_noext ..."
    668       $bibtex "$filename_noext" >&5 ||
    669         fatal "$bibtex exited with bad status, quitting."
    670     fi
    671 
    672     # What we'll run texindex on -- exclude non-index files.
    673     # Since we know index files are last, it is correct to remove everything
    674     # before .aux and .?o?.  But don't really do <anything>o<anything>
    675     # -- don't match whitespace as <anything>.
    676     # Otherwise, if orig_xref_files contains something like
    677     #   foo.xo foo.whatever
    678     # the space after the o will get matched.
    679     index_files=`echo "$orig_xref_files" \
    680                  | sed "s!.*\.aux!!g;
    681                         s!./$filename_noext\.[^ ]o[^ ]!!g;
    682                         s/^[ ]*//;s/[ ]*$//"`
    683     # Run texindex (or makeindex) on current index files.  If they
    684     # already exist, and after running TeX a first time the index
    685     # files don't change, then there's no reason to run TeX again.
    686     # But we won't know that if the index files are out of date or
    687     # nonexistent.
    688     if test -n "$texindex" && test -n "$index_files"; then
    689       $verbose "Running $texindex $index_files ..."
    690       $texindex $index_files 2>&5 1>&2 ||
    691          fatal "$texindex exited with bad status, quitting."
    692     fi
    693 
    694     # Finally, run TeX.
    695     cmd="$tex $tex_args"
    696     $verbose "Running $cmd ..."
    697     if $cmd "$filename_input" >&5; then :; else
    698       echo "$0: TeX failed. If the above said 'tex: not found', " >&2
    699       echo "$0: you may need to install TeX;" >&2
    700       echo "$0: it is available from the pkgsrc system in print/teTeX." >&2
    701       echo "$0: If TeX is installed, make sure it is in your $PATH, or" >&2
    702       echo "$0: set the environment variable $TEX to its location." >&2
    703       echo "$0: $tex exited with bad status, quitting." >&2
    704       echo "$0: see $filename_noext.log for errors." >&2
    705       test "$clean" = t \
    706         && cp "$filename_noext.log" "$orig_pwd"
    707       exit 1
    708     fi
    709 
    710 
    711     # Decide if looping again is needed.
    712     finished=t
    713 
    714     # LaTeX (and the package changebar) report in the LOG file if it
    715     # should be rerun.  This is needed for files included from
    716     # subdirs, since texi2dvi does not try to compare xref files in
    717     # subdirs.  Performing xref files test is still good since LaTeX
    718     # does not report changes in xref files.
    719     if grep "Rerun to get" "$filename_noext.log" >&6 2>&1; then
    720       finished=
    721     fi
    722 
    723     # Check if xref files changed.
    724     new_xref_files=`get_xref_files "$filename_noext"`
    725     $verbose "Original xref files = `echo $orig_xref_files | sed 's|\./||g'`"
    726     $verbose "New xref files      = `echo $new_xref_files | sed 's|\./||g'`"
    727 
    728     # If old and new lists don't at least have the same file list,
    729     # then one file or another has definitely changed.
    730     test "x$orig_xref_files" != "x$new_xref_files" && finished=
    731 
    732     # File list is the same.  We must compare each file until we find
    733     # a difference.
    734     if test -n "$finished"; then
    735       for this_file in $new_xref_files; do
    736         $verbose "Comparing xref file `echo $this_file | sed 's|\./||g'` ..."
    737         # cmp -s returns nonzero exit status if files differ.
    738         if cmp -s "$this_file" "$tmpdir_bak/$this_file"; then :; else
    739           # We only need to keep comparing until we find one that
    740           # differs, because we'll have to run texindex & tex again no
    741           # matter how many more there might be.
    742           finished=
    743           $verbose "xref file `echo $this_file | sed 's|\./||g'` differed ..."
    744           test "$debug" = t && diff -c "$tmpdir_bak/$this_file" "$this_file"
    745           break
    746         fi
    747       done
    748     fi
    749 
    750     # If finished, exit the loop, else rerun the loop.
    751     test -n "$finished" && break
    752   done # while :;
    753 
    754   # If we were using thumbpdf and producing PDF, then run thumbpdf
    755   # and TeX one last time.
    756   if test $oformat = pdf \
    757      && test -r "$filename_noext.log" \
    758      && grep 'thumbpdf\.sty'  "$filename_noext.log" >&6 2>&1; \
    759   then
    760     $verbose "Running $thumbpdf $filename_noext ..."
    761     $thumbpdf "$filename_noext" >&5 ||
    762       fatal "$thumbpdf exited with bad status, quitting."
    763 
    764     $verbose "Running $cmd $filename_input..."
    765     if $cmd "$filename_input" >&5; then :; else
    766       echo "$0: $tex exited with bad status, quitting." >&2
    767       echo "$0: see $filename_noext.log for errors." >&2
    768       test "$clean" = t \
    769 	&& cp "$filename_noext.log" "$orig_pwd"
    770       exit 1
    771     fi
    772   fi
    773 
    774 
    775   # If we were in clean mode, compilation was in a tmp directory.
    776   # Copy the DVI (or PDF) file into the directory where the compilation
    777   # has been done.  (The temp dir is about to get removed anyway.)
    778   # We also return to the original directory so that
    779   # - the next file is processed in correct conditions
    780   # - the temporary file can be removed
    781   if test -n "$clean"; then
    782     if test -n "$oname"; then
    783        dest=$oname
    784     else
    785        dest=$orig_pwd
    786     fi
    787     $verbose "Copying $oformat file from `pwd` to $dest"
    788     cp -p "./$filename_noext.$oformat" "$dest"
    789     cd / # in case $orig_pwd is on a different drive (for DOS)
    790     cd $orig_pwd || exit 1
    791   fi
    792 
    793   # Remove temporary files.
    794   if test "x$debug" = "x"; then
    795     $verbose "Removing $tmpdir_src $tmpdir_xtr $tmpdir_bak ..."
    796     cd /
    797     rm -rf $tmpdir_src $tmpdir_xtr $tmpdir_bak
    798   fi
    799 done
    800 
    801 $verbose "$0: done."
    802 exit 0 # exit successfully, not however we ended the loop.
    803