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