2 # texi2dvi --- produce DVI (or PDF) files from Texinfo (or (La)TeX) sources.
3 # $Id: texi2dvi,v 1.135 2008/09/18 18:46:01 karl Exp $
5 # Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2001,
6 # 2002, 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
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 3 of the License,
11 # or (at your option) any later version.
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.
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21 # Original author: Noah Friedman.
23 # Please send bug reports, etc. to bug-texinfo@gnu.org.
24 # If possible, please send a copy of the output of the script called with
25 # the `--debug' option when making a bug report.
27 test -f /bin/ksh && test -z "$RUNNING_KSH" \
28 && { UNAMES=`uname -s`; test "x$UNAMES" = xULTRIX; } 2>/dev/null \
29 && { RUNNING_KSH=true; export RUNNING_KSH; exec /bin/ksh $0 ${1+"$@"}; }
32 # No failure shall remain unpunished.
35 # This string is expanded by rcs automatically when this file is checked out.
36 rcs_revision='$Revision: 1.135 $'
37 rcs_version=`set - $rcs_revision; echo $2`
38 program=`echo $0 | sed -e 's!.*/!!'`
40 build_mode=${TEXI2DVI_BUILD_MODE:-local}
41 build_dir=${TEXI2DVI_BUILD_DIRECTORY:-.}
43 # Initialize variables for option overriding and otherwise.
44 # Don't use `unset' since old bourne shells don't have this command.
45 # Instead, assign them an empty value.
47 batch=false # true for batch mode
50 expand= # t for expansion via makeinfo
52 line_error=true # Pass --file-line-error to TeX.
53 no_line_error=false # absolutely do not pass --file-line-error to TeX
56 quiet=false # by default let the tools' message be displayed
60 textra= # Extra TeX commands to insert in the input file.
61 txiprereq=19990129 # minimum texinfo.tex version with macro expansion
62 verb=false # true for verbose mode
63 translate_file= # name of charset translation file
64 recode_from= # if not empty, recode from this encoding to @documentencoding
68 # We have to initialize IFS to space tab newline since we save and
69 # restore IFS and apparently POSIX allows stupid/broken behavior with
71 # http://lists.gnu.org/archive/html/automake-patches/2006-05/msg00008.html
72 # We need space, tab and new line, in precisely that order. And don't leave
78 IFS="$space$tab$newline"
80 # In case someone pedantic insists on using grep -E.
83 # Systems which define $COMSPEC or $ComSpec use semicolons to separate
84 # directories in TEXINPUTS -- except for Cygwin et al., where COMSPEC
85 # might be inherited, but : is used.
86 if test -n "$COMSPEC$ComSpec" \
87 && uname | $EGREP -iv 'cygwin|mingw|djgpp' >/dev/null; then
94 CDPATH=${ZSH_VERSION+.}$path_sep
96 # If $TEX is set to a directory, don't use it.
97 test -n "$TEX" && test -d "$TEX" && unset TEX
100 ## --------------------- ##
101 ## Auxiliary functions. ##
102 ## --------------------- ##
104 # In case `local' is not supported by the shell, provide a function
105 # that simulates it by simply performing the assignments. This means
106 # that we must not expect `local' to work, i.e., we must not (i) rely
107 # on it during recursion, and (ii) have two local declarations of the
108 # same variable. (ii) is easy to check statically, and our test suite
109 # does make sure there is never twice a static local declaration of a
110 # variable. (i) cannot be checked easily, so just be careful.
112 # Note that since we might use a function simulating `local', we can
113 # no longer rely on the fact that no IFS-splitting is performed. So,
118 # is fine (no IFS-splitting), never write
141 # Return to the original directory.
144 # In case $orig_pwd is on a different drive (for DOS).
147 # Return to the original directory so that
148 # - the next file is processed in correct conditions
149 # - the temporary file can be removed
150 cd "$orig_pwd" || exit 1
155 # Return the directory part of FILE.
158 dirname "$1" 2>/dev/null \
159 || { echo "$1" | sed 's!/[^/]*$!!;s!^$!.!'; }
163 # absolute NAME -> ABS-NAME
164 # -------------------------
165 # Return an absolute path to NAME.
170 # Absolute paths don't need to be expanded.
174 slashes=`echo "$1" | sed -n 's,.*[^/]\(/*\)$,\1,p'`
176 rel=$orig_pwd/`func_dirname "$1"`
177 if test -d "$rel"; then
178 (cd "$rel" 2>/dev/null &&
180 n=`pwd`/`basename "$1"`"$slashes"
183 error 1 "not a directory: $rel"
190 # ensure_dir DIR1 DIR2...
191 # -----------------------
192 # Make sure the directories exist.
199 || error 1 "cannot create directory: $dir"
204 # error EXIT_STATUS LINE1 LINE2...
205 # --------------------------------
206 # Report an error and exit with failure if EXIT_STATUS is non null.
212 if test "$s" != 0; then
220 # Return true if PROG is somewhere in PATH, else false.
224 IFS=$path_sep # break path components at the path separator
227 # The basic test for an executable is `test -f $f && test -x $f'.
228 # (`test -x' is not enough, because it can also be true for directories.)
229 # We have to try this both for $1 and $1.exe.
231 # Note: On Cygwin and DJGPP, `test -x' also looks for .exe. On Cygwin,
232 # also `test -f' has this enhancement, bot not on DJGPP. (Both are
233 # design decisions, so there is little chance to make them consistent.)
234 # Thusly, it seems to be difficult to make use of these enhancements.
236 if { test -f "$dir/$1" && test -x "$dir/$1"; } ||
237 { test -f "$dir/$1.exe" && test -x "$dir/$1.exe"; }; then
244 # report LINE1 LINE2...
245 # ---------------------
246 # Report some information on stderr.
258 # Run the COMMAND-LINE verbosely, and catching errors as failures.
269 # Display usage and exit successfully.
272 # We used to simply have `echo "$usage"', but coping with the
273 # changing behavior of `echo' is much harder than simply using a
276 # echo '\noto' echo '\\noto' echo -e '\\noto'
277 # bash 3.1 \noto \\noto \noto
278 # bash 3.2 %oto \noto -e \noto
280 # where % denotes the eol character.
282 Usage: $program [OPTION]... FILE...
284 Run each Texinfo or (La)TeX FILE through TeX in turn until all
285 cross-references are resolved, building all indices. The directory
286 containing each FILE is searched for included files. The suffix of FILE
287 is used to determine its language ((La)TeX or Texinfo). To process
288 (e)plain TeX files, set the environment variable LATEX=tex.
290 In order to make texi2dvi a drop-in replacement of TeX/LaTeX in AUC-TeX,
291 the FILE may also be composed of the following simple TeX commands.
292 \`\\input{FILE}' the actual file to compile
293 \`\\nonstopmode' same as --batch
295 Makeinfo is used to perform Texinfo macro expansion before running TeX
299 -b, --batch no interaction
300 -D, --debug turn on shell debugging (set -x)
301 -h, --help display this help and exit successfully
302 -o, --output=OFILE leave output in OFILE (implies --clean);
303 only one input FILE may be specified in this case
304 -q, --quiet no output unless errors (implies --batch)
305 -s, --silent same as --quiet
306 -v, --version display version information and exit successfully
307 -V, --verbose report on what is done
310 -@ use @input instead of \input for preloaded Texinfo
311 --dvi output a DVI file [default]
312 --dvipdf output a PDF file via DVI (using dvipdf)
313 -e, -E, --expand force macro expansion using makeinfo
314 -I DIR search DIR for Texinfo files
315 -l, --language=LANG specify LANG for FILE, either latex or texinfo
316 --no-line-error do not pass --file-line-error to TeX
317 -p, --pdf use pdftex or pdflatex for processing
318 -r, --recode call recode before TeX to translate input
319 --recode-from=ENC recode from ENC to the @documentencoding
320 --src-specials pass --src-specials to TeX
321 -t, --command=CMD insert CMD in copy of input file
322 or --texinfo=CMD multiple values accumulate
323 --translate-file=FILE use given charset translation file for TeX
326 --build=MODE specify the treatment of auxiliary files [$build_mode]
327 --tidy same as --build=tidy
328 -c, --clean same as --build=clean
329 --build-dir=DIR specify where the tidy compilation is performed;
331 defaults to TEXI2DVI_BUILD_DIRECTORY [$build_dir]
332 --mostly-clean remove the auxiliary files and directories
335 The MODE specifies where the TeX compilation takes place, and, as a
336 consequence, how auxiliary files are treated. The build mode
337 can also be set using the environment variable TEXI2DVI_BUILD_MODE.
340 \`local' compile in the current directory, leaving all the auxiliary
341 files around. This is the traditional TeX use.
342 \`tidy' compile in a local *.t2d directory, where the auxiliary files
343 are left. Output files are copied back to the original file.
344 \`clean' same as \`tidy', but remove the auxiliary directory afterwards.
345 Every compilation therefore requires the full cycle.
347 Using the \`tidy' mode brings several advantages:
348 - the current directory is not cluttered with plethora of temporary files.
349 - clutter can be even reduced using --build-dir=dir: all the *.t2d
350 directories are stored there.
351 - clutter can be reduced to zero using, e.g., --build-dir=/tmp/\$USER.t2d
352 or --build-dir=\$HOME/.t2d.
353 - the output file is updated after every succesful TeX run, for
354 sake of concurrent visualization of the output. In a \`local' build
355 the viewer stops during the whole TeX run.
356 - if the compilation fails, the previous state of the output file
358 - PDF and DVI compilation are kept in separate subdirectories
359 preventing any possibility of auxiliary file incompatibility.
361 On the other hand, because \`tidy' compilation takes place in another
362 directory, occasionally TeX won't be able to find some files (e.g., when
363 using \\graphicspath): in that case use -I to specify the additional
364 directories to consider.
366 The values of the BIBTEX, LATEX (or PDFLATEX), MAKEINDEX, MAKEINFO,
367 TEX (or PDFTEX), TEXINDEX, and THUMBPDF environment variables are used
368 to run those commands, if they are set. Any CMD strings are added
369 after @setfilename for Texinfo input, in the first line for LaTeX input.
371 Email bug reports to <bug-texinfo@gnu.org>,
372 general questions and discussion to <help-texinfo@gnu.org>.
373 Texinfo home page: http://www.gnu.org/software/texinfo/
379 # verbose WORD1 WORD2
380 # -------------------
381 # Report some verbose information.
392 # Display version info and exit succesfully.
396 texi2dvi (GNU Texinfo 4.13) $rcs_version
398 Copyright (C) 2008 Free Software Foundation, Inc.
399 License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
400 This is free software: you are free to change and redistribute it.
401 There is NO WARRANTY, to the extent permitted by law.
407 ## ---------------- ##
408 ## Handling lists. ##
409 ## ---------------- ##
412 # list_append LIST-NAME ELEM
413 # --------------------------
414 # Set LIST-NAME to its former contents, with ELEM appended.
419 eval set X \$$la_l "$@"
425 # list_concat_dirs LIST-NAME DIR-LIST
426 # -----------------------------------
427 # Append to LIST-NAME all the components (included empty) from
428 # the $path_sep separated list DIR-LIST. Make the paths absolute.
432 # Empty path components are meaningful to tex. We rewrite them as
433 # `EMPTY' so they don't get lost when we split on $path_sep.
434 # Hopefully no one will have an actual directory named EMPTY.
435 local replace_EMPTY="-e 's/^$path_sep/EMPTY$path_sep/g' \
436 -e 's/$path_sep\$/${path_sep}EMPTY/g' \
437 -e 's/$path_sep$path_sep/${path_sep}EMPTY:/g'"
440 set x `echo "$2" | eval sed $replace_EMPTY`; shift
447 list_append $lcd_list ""
450 if test -d $dir; then
451 dir=`absolute "$dir"`
452 list_append $lcd_list "$dir"
460 # list_prefix LIST-NAME SEP -> STRING
461 # -----------------------------------
462 # Return a string that is composed of the LIST-NAME with each item
472 lp_res="$lp_res \"$lp_p\" \"$i\""
477 # list_infix LIST-NAME SEP -> STRING
478 # ----------------------------------
479 # Same as list_prefix, but a separator.
490 # list_dir_to_abs LIST-NAME
491 # -------------------------
492 # Convert the list to using only absolute dir names.
493 # Currently unused, but should replace absolute_filenames some day.
502 dir=`absolute "$dir"`
503 test -d "$dir" || continue
504 ld_res="$ld_res \"$dir\""
511 ## ------------------------------ ##
512 ## Language auxiliary functions. ##
513 ## ------------------------------ ##
517 # Return the tex output language (DVI or PDF) for $OUT_LANG.
521 dvi | ps | dvipdf ) echo dvi;;
522 pdf ) echo $out_lang;;
523 html | info | text ) echo $out_lang;;
524 *) error 1 "$0: invalid out_lang: $1";;
531 # Return the extension for $OUT_LANG.
536 dvi | html | info | pdf | ps | text ) echo $out_lang;;
537 *) error 1 "$0: invalid out_lang: $1";;
542 ## ------------------------- ##
543 ## TeX auxiliary functions. ##
544 ## ------------------------- ##
546 # Save TEXINPUTS so we can construct a new TEXINPUTS path for each file.
547 # Likewise for bibtex and makeindex.
548 tex_envvars="BIBINPUTS BSTINPUTS DVIPSHEADERS INDEXSTYLE MFINPUTS MPINPUTS \
550 for var in $tex_envvars; do
551 eval ${var}_orig=\$$var
556 # absolute_filenames TEX-PATH -> TEX-PATH
557 # ---------------------------------------
558 # Convert relative paths to absolute paths, so we can run in another
559 # directory (e.g., in tidy build mode, or during the macro-support
560 # detection). Prepend ".".
561 absolute_filenames ()
563 # Empty path components are meaningful to tex. We rewrite them as
564 # `EMPTY' so they don't get lost when we split on $path_sep.
565 # Hopefully no one will have an actual directory named EMPTY.
566 local replace_empty="-e 's/^$path_sep/EMPTY$path_sep/g' \
567 -e 's/$path_sep\$/${path_sep}EMPTY/g' \
568 -e 's/$path_sep$path_sep/${path_sep}EMPTY:/g'"
570 res=`echo "$1" | eval sed $replace_empty`
582 if test -d "$dir"; then
583 res=$res$path_sep`absolute "$dir"`
585 # Even if $dir is not a directory, preserve it in the path.
586 # It might contain metacharacters that TeX will expand in
587 # turn, e.g., /some/path/{a,b,c}. This will not get the
588 # implicit absolutification of the path, but we can't help that.
589 res=$res$path_sep$dir
598 # output_base_name FILE
599 # ---------------------
600 # The name of FILE, possibly renamed to satisfy --output.
606 out_noext=`echo "$oname" | sed 's/\.[^.]*$//'`
608 file_ext=`echo "$1" | sed 's/^.*\.//'`
609 echo "$out_noext.$file_ext"
615 # move_to_dest FILE...
616 # --------------------
617 # Move FILE to the place where the user expects it. Truly move it, that
618 # is, it must not remain in its build location unless that is also the
619 # output location. (Otherwise it might appear as an extra file in make
622 # FILE can be the principal output (in which case -o directly applies), or
623 # an auxiliary file with the same base name.
636 true:) dest=$orig_pwd;;
638 *:*) dest=`output_base_name "$file"`;;
640 if test ! -f "$file"; then
641 error 1 "no such file or directory: $file"
643 if test -n "$dest"; then
644 # We need to know whether $dest is a directory.
645 if test -d "$dest"; then
649 destdir="`dirname $dest`"
652 # We want to compare the source location and the output location,
653 # and if they are different, do the move. But if they are the
654 # same, we must preserve the source. Since we can't assume
655 # stat(1) or test -ef is available, resort to comparing the
656 # directory names, canonicalized with pwd. We can't use cmp -s
657 # since the output file might not actually change from run to run;
658 # e.g., TeX DVI output is timestamped to only the nearest minute.
659 destdir=`cd $destdir && pwd`
660 destbase=`basename $destfile`
662 sourcedir=`dirname $file`
663 sourcedir=`cd $sourcedir && pwd`
664 sourcebase=`basename $file`
666 if test "$sourcedir/$sourcebase" != "$destdir/$destbase"; then
667 verbose "Moving $file to $destfile"
669 mv "$file" "$destfile"
676 ## --------------------- ##
677 ## Managing xref files. ##
678 ## --------------------- ##
682 # Return with success with FILE is an aux file.
685 test -f "$1" || return 1
694 # Return with success with FILE is an aux file containing citation
698 test -s "$1" || return 1
699 if (grep '^\\bibstyle[{]' "$1" \
700 && grep '^\\bibdata[{]' "$1" \
701 ## The following line is suspicious: fails when there
702 ## are citations in sub aux files. We need to be
703 ## smarter in this case.
704 ## && grep '^\\citation[{]' "$f"
714 # Return with success with FILE is an index file.
715 # When index.sty is used, there is a space before the brace.
718 test -f "$1" || return 1
719 case `sed '1q' "$1"` in
720 "\\entry{"*|"\\indexentry{"*|"\\indexentry {"*) return 0;;
727 # Return with success if FILE is an xref file (indexes, tables and lists).
730 test -f "$1" || return 1
731 # If the file is not suitable to be an index or xref file, don't
732 # process it. It's suitable if the first character is a
733 # backslash or right quote or at, as long as the first line isn't
735 case `sed '1q' "$1"` in
736 "\\input texinfo"*) return 1;;
743 # generated_files_get FILENAME-NOEXT [PREDICATE-FILTER]
744 # -----------------------------------------------------
745 # Return the list of files generated by the TeX compilation of FILENAME-NOEXT.
746 generated_files_get ()
749 if test -n "$2"; then
753 # Gather the files created by TeX.
755 if test -f "$1.log"; then
756 sed -n -e "s,^\\\\openout.* = \`\\(.*\\)'\\.,\\1,p" "$1.log"
760 # Depending on these files, infer outputs from other tools.
765 # texindex: texinfo.cp -> texinfo.cps
766 if index_file_p $file; then
771 if aux_file_p $file; then
772 # bibtex: *.aux -> *.bbl and *.blg.
773 echo $file | sed 's/^\(.*\)\.aux$/\1.bbl/'
774 echo $file | sed 's/^\(.*\)\.aux$/\1.blg/'
776 echo $file | sed 's/^\(.*\)\.aux$/\1.fls/'
781 # Filter existing files matching the criterion.
783 # With an input file name containing a space, this produces a
784 # "command not found" message (and filtering is ineffective).
785 # The situation with a newline is presumably even worse.
787 if $filter "$file"; then
792 # Some files are opened several times, e.g., listings.sty's *.vrb.
799 # Save the xref files.
802 # Save copies of auxiliary files for later comparison.
803 xref_files_orig=`generated_files_get "$in_noext" xref_file_p`
804 if test -n "$xref_files_orig"; then
805 verbose "Backing up xref files: $xref_files_orig"
806 # The following line improves `cp $xref_files_orig "$work_bak"'
807 # by preserving the directory parts. Think of
808 # cp chap1/main.aux chap2/main.aux $work_bak.
810 # Users may have, e.g., --keep-old-files. Don't let this interfere.
811 # (Don't use unset for the sake of ancient shells.)
812 TAR_OPTIONS=; export TAR_OPTIONS
813 tar cf - $xref_files_orig | (cd "$work_bak" && tar xf -)
820 # Whether the xref files were changed since the previous run.
821 xref_files_changed ()
823 # LaTeX (and the package changebar) report in the LOG file if it
824 # should be rerun. This is needed for files included from
825 # subdirs, since texi2dvi does not try to compare xref files in
826 # subdirs. Performing xref files test is still good since LaTeX
827 # does not report changes in xref files.
828 if grep "Rerun to get" "$in_noext.log" >&6 2>&1; then
832 # If old and new lists don't at least have the same file list,
833 # then one file or another has definitely changed.
834 xref_files_new=`generated_files_get "$in_noext" xref_file_p`
835 verbose "Original xref files = $xref_files_orig"
836 verbose "New xref files = $xref_files_new"
837 if test "x$xref_files_orig" != "x$xref_files_new"; then
841 # Compare each file until we find a difference.
842 for this_file in $xref_files_new; do
843 verbose "Comparing xref file `echo $this_file | sed 's|\./||g'` ..."
844 # cmp -s returns nonzero exit status if files differ.
845 if cmp -s "$this_file" "$work_bak/$this_file"; then :; else
846 verbose "xref file `echo $this_file | sed 's|\./||g'` differed ..."
848 diff -u "$work_bak/$this_file" "$this_file"
860 ## ----------------------- ##
861 ## Running the TeX suite. ##
862 ## ----------------------- ##
868 # Run TeX as "$tex $in_input", taking care of errors and logs.
871 case $in_lang:`out_lang_tex` in
872 latex:dvi) tex=${LATEX:-latex};;
873 latex:pdf) tex=${PDFLATEX:-pdflatex};;
875 # MetaPost also uses the TEX environment variable. If the user
876 # has set TEX=latex for that reason, don't bomb out.
878 *latex) tex=tex;; # don't bother trying to find etex
881 texinfo:pdf) tex=$PDFTEX;;
883 *) error 1 "$0: $out_lang not supported for $in_lang";;
886 # Beware of aux files in subdirectories that require the
887 # subdirectory to exist.
888 case $in_lang:$tidy in
890 sed -n 's|^[ ]*\\include{\(.*\)/.*}.*|\1|p' "$in_input" |
894 ensure_dir "$work_build/$d"
899 # Note that this will be used via an eval: quote properly.
902 # If possible, make TeX report error locations in GNU format.
903 if test "${tex_help:+set}" != set; then
904 # Go to a temporary directory to try --help, since old versions that
905 # don't accept --help will generate a texput.log.
906 tex_help_dir=$t2ddir/tex_help
907 ensure_dir "$tex_help_dir"
908 tex_help=`cd "$tex_help_dir" >&6 && $tex --help </dev/null 2>&1`
910 if $no_line_error; then :; else
911 # The mk program and perhaps others want to parse TeX's
912 # original error messages.
913 case $line_error:$tex_help in
914 true:*file-line-error*) cmd="$cmd --file-line-error";;
918 # Tell TeX about TCX file, if specified.
919 test -n "$translate_file" && cmd="$cmd --translate-file=$translate_file"
921 # Tell TeX to make source specials (for backtracking from output to
922 # source, given a sufficiently smart editor), if specifed.
923 test -n "$src_specials" && cmd="$cmd $src_specials"
925 # Tell TeX to be batch if requested.
927 # \batchmode does not show terminal output at all, so we don't
928 # want that. And even in batch mode, TeX insists on having input
929 # from the user. Close its stdin to make it impossible.
930 cmd="$cmd </dev/null '${escape}nonstopmode' '${escape}input'"
933 # we'd like to handle arbitrary input file names, such as a~b.tex.
934 # This isn't a general way to do it :), though it does work, kind of.
935 # cmd="$cmd '${escape}catcode126=12 \input '"
937 # TeX's \input does not (easily or reliably) support whitespace
938 # characters or other special characters in file names. Our intensive
939 # use of absolute file names makes this worse: the enclosing directory
940 # names may include white spaces. Improve the situation using a
941 # symbolic link to the filename in the current directory, in tidy mode
942 # only. Do not alter in_input.
944 # The filename is almost always tokenized using plain TeX conventions
945 # (the exception would be if the user made a texinfo.fmt file). Not
946 # all the plain TeX special characters cause trouble, but there's no
947 # harm in making the link.
949 case $tidy:`func_dirname "$in_input"` in
950 true:*["$space$tab$newline\"#\$%\\^_{}"]*)
951 _run_tex_file_name=`basename "$in_input"`
952 if test ! -f "$_run_tex_file_name"; then
953 # It might not be a file, clear it.
954 run rm -f "$_run_tex_file_name"
955 run ln -s "$in_input"
957 cmd="$cmd '$_run_tex_file_name'"
961 cmd="$cmd '$in_input'"
965 verbose "$0: Running $cmd ..."
966 if eval "$cmd" >&5; then
968 dvi | pdf ) move_to_dest "$in_noext.$out_lang";;
971 error 1 "$tex exited with bad status, quitting."
977 # Run bibtex on current file.
978 # - If its input (AUX) exists.
979 # - If some citations are missing (LOG contains `Citation').
980 # or the LOG complains of a missing .bbl
982 # Don't try to be too smart:
984 # 1. Running bibtex only if the bbl file exists and is older than
985 # the LaTeX file is wrong, since the document might include files
988 # 3. Because there can be several AUX (if there are \include's),
989 # but a single LOG, looking for missing citations in LOG is
990 # easier, though we take the risk to match false messages.
994 latex) bibtex=${BIBTEX:-bibtex};;
998 # "Citation undefined" is for LaTeX, "Undefined citation" for btxmac.tex.
999 # The no .aux && \bibdata test is also for btxmac, in case it was the
1000 # first run of a bibtex-using document. Otherwise, it's possible that
1001 # bibtex would never be run.
1002 if test -r "$in_noext.aux" \
1003 && test -r "$in_noext.log" \
1004 && (grep 'Warning:.*Citation.*undefined' "$in_noext.log" \
1005 || grep '.*Undefined citation' "$in_noext.log" \
1006 || grep 'No file .*\.bbl\.' "$in_noext.log") \
1007 || (grep 'No \.aux file' "$in_noext.log" \
1008 && grep '^\\bibdata' "$in_noext.aux") \
1011 for f in `generated_files_get "$in_noext" bibaux_file_p`
1020 # Run texindex (or makeindex) on current index files. If they already
1021 # exist, and after running TeX a first time the index files don't
1022 # change, then there's no reason to run TeX again. But we won't know
1023 # that if the index files are out of date or nonexistent.
1027 latex) texindex=${MAKEINDEX:-makeindex};;
1028 texinfo) texindex=${TEXINDEX:-texindex};;
1030 index_files=`generated_files_get $in_noext index_file_p`
1031 if test -n "$texindex" && test -n "$index_files"; then
1032 run $texindex $index_files
1041 if test `out_lang_tex` = pdf \
1042 && test -r "$in_noext.log" \
1043 && grep 'thumbpdf\.sty' "$in_noext.log" >&6 2>&1; \
1045 thumbpdf=${THUMBPDF:-thumbpdf}
1046 thumbcmd="$thumbpdf $in_dir/$in_noext"
1047 verbose "Running $thumbcmd ..."
1048 if $thumbcmd >&5; then
1051 report "$thumbpdf exited with bad status." \
1052 "Ignoring its output."
1058 # run_dvipdf FILE.dvi
1059 # -------------------
1060 # Convert FILE.dvi to FILE.pdf.
1063 # Find which dvi->pdf program is available.
1064 if test -z "$dvipdf"; then
1065 for i in "$DVIPDF" dvipdfmx dvipdfm dvipdf dvi2pdf dvitopdf;
1067 if findprog $i; then
1072 # These tools have varying interfaces, some 'input output', others
1073 # 'input -o output'. They all seem to accept 'input' only,
1074 # outputting using the expected file name.
1076 if test ! -f `echo "$1" | sed -e 's/\.dvi$/.pdf/'`; then
1077 error 1 "$0: cannot find output file"
1083 # Run the TeX tools until a fix point is reached.
1086 # Move to the working directory.
1088 verbose "cd $work_build"
1089 cd "$work_build" || exit 1
1092 # Count the number of cycles.
1096 cycle=`expr $cycle + 1`
1097 verbose "Cycle $cycle for $command_line_filename"
1101 # We run bibtex first, because I can see reasons for the indexes
1102 # to change after bibtex is run, but I see no reason for the
1108 xref_files_changed || break
1111 # If we were using thumbpdf and producing PDF, then run thumbpdf
1112 # and TeX one last time.
1115 # Install the result if we didn't already (i.e., if the output is
1119 run_dvipdf "$in_noext.`out_lang_tex`"
1120 move_to_dest "$in_noext.`out_lang_ext`"
1123 dvips -o "$in_noext.`out_lang_ext`" "$in_noext.`out_lang_tex`"
1124 move_to_dest "$in_noext.`out_lang_ext`"
1131 ## -------------------------------- ##
1132 ## TeX processing auxiliary tools. ##
1133 ## -------------------------------- ##
1136 # A sed script that preprocesses Texinfo sources in order to keep the
1137 # iftex sections only. We want to remove non TeX sections, and comment
1138 # (with `@c texi2dvi') TeX sections so that makeinfo does not try to
1139 # parse them. Nevertheless, while commenting TeX sections, don't
1140 # comment @macro/@end macro so that makeinfo does propagate them.
1141 # Unfortunately makeinfo --iftex --no-ifinfo doesn't work well enough
1142 # (yet), makeinfo can't parse the TeX commands, so work around with sed.
1145 '/^@tex/,/^@end tex/{
1148 /^@iftex/,/^@end iftex/{
1150 /^@c texi2dvi@macro/,/^@c texi2dvi@end macro/{
1154 /^@ifnottex/,/^@end ifnottex/{
1157 /^@ifinfo/,/^@end ifinfo/{
1159 /^@menu/,/^@end menu/p
1163 s/^@ifnotinfo/@c texi2dvi@ifnotinfo/
1164 s/^@end ifnotinfo/@c texi2dvi@end ifnotinfo/'
1166 # Uncommenting is simple: Remove any leading `@c texi2dvi'.
1167 uncomment_iftex='s/^@c texi2dvi//'
1172 # Expand macro commands in the original source file using Makeinfo.
1173 # Always use `end' footnote style, since the `separate' style
1174 # generates different output (arguably this is a bug in -E). Discard
1175 # main info output, the user asked to run TeX, not makeinfo.
1178 test $in_lang = texinfo \
1181 # Unless required by the user, makeinfo expansion is wanted only
1182 # if texinfo.tex is too old.
1183 if test "$expand" = t; then
1184 makeinfo=${MAKEINFO:-makeinfo}
1186 # Check if texinfo.tex performs macro expansion by looking for
1187 # its version. The version is a date of the form YEAR-MO-DA.
1188 # We don't need to use [0-9] to match the digits since anyway
1189 # the comparison with $txiprereq, a number, will fail with non
1191 # Run in a temporary directory to avoid leaving files.
1192 version_test_dir=$t2ddir/version_test
1193 ensure_dir "$version_test_dir"
1195 cd "$version_test_dir"
1196 echo '\input texinfo.tex @bye' >txiversion.tex
1197 # Be sure that if tex wants to fail, it is not interactive:
1199 $TEX txiversion.tex </dev/null >txiversion.out 2>txiversion.err
1201 if test $? != 0; then
1202 cat "$version_test_dir/txiversion.out"
1203 cat "$version_test_dir/txiversion.err" >&2
1204 error 1 "texinfo.tex appears to be broken, quitting."
1206 eval `sed -n 's/^.*\[\(.*\)version \(....\)-\(..\)-\(..\).*$/txiformat=\1 txiversion="\2\3\4"/p' "$version_test_dir/txiversion.out"`
1207 verbose "texinfo.tex preloaded as \`$txiformat', version is \`$txiversion' ..."
1208 if test "$txiprereq" -le "$txiversion" >&6 2>&1; then
1211 makeinfo=${MAKEINFO:-makeinfo}
1213 # As long as we had to run TeX, offer the user this convenience:
1214 if test "$txiformat" = Texinfo; then
1219 if test -n "$makeinfo"; then
1220 # in_src: the file with macros expanded.
1221 # Use the same basename to generate the same aux file names.
1222 work_src=$workdir/src
1223 ensure_dir "$work_src"
1224 in_src=$work_src/$in_base
1226 miincludes=`list_prefix includes -I`
1227 verbose "Macro-expanding $command_line_filename to $in_src ..."
1228 # eval $makeinfo because it might be defined as something complex
1229 # (running missing) and then we end up with things like '"-I"',
1230 # and "-I" (including the quotes) is not an option name. This
1231 # happens with gettext 0.14.5, at least.
1232 sed "$comment_iftex" "$command_line_filename" \
1233 | eval $makeinfo --footnote-style=end -I "$in_dir" $miincludes \
1234 -o /dev/null --macro-expand=- \
1235 | sed "$uncomment_iftex" >"$in_src"
1236 # Continue only if everything succeeded.
1238 || test ! -r "$in_src"; then
1239 verbose "Expansion failed, ignored...";
1246 # insert_commands ()
1247 # ------------------
1248 # Used most commonly for @finalout, @smallbook, etc.
1253 latex) textra_cmd=1i;;
1254 texinfo) textra_cmd='/^@setfilename/a';;
1255 *) error 1 "internal error, unknown language: $in_lang";;
1258 if test -n "$textra"; then
1259 # _xtr. The file with the user's extra commands.
1260 work_xtr=$workdir/xtr
1261 in_xtr=$work_xtr/$in_base
1262 ensure_dir "$work_xtr"
1263 verbose "Inserting extra commands: $textra"
1265 $textra" "$in_input" >"$in_xtr"
1272 # If this is a Texinfo file with a specified input encoding, and
1273 # recode is available, then recode to plain 7 bit Texinfo.
1279 if test $in_lang = texinfo; then
1280 pgm='s/^ *@documentencoding *\([^ ][^ ]*\) *$/\1/
1285 encoding=`sed -e "$pgm" "$in_input"`
1286 if $recode && test -n "$encoding" && findprog recode; then
1287 if test -n "$recode_from"; then
1294 verbose "Recoding from $from to $to."
1295 # _rcd. The Texinfo file recoded in 7bit.
1296 work_rcd=$workdir/recode
1297 in_rcd=$work_rcd/$in_base
1298 ensure_dir "$work_rcd"
1299 if recode "$encoding..$to" <"$in_input" >"$in_rcd" \
1300 && test -s "$in_rcd"; then
1303 verbose "Recoding failed, using original input."
1309 # compute_language FILENAME
1310 # -------------------------
1311 # Return the short string describing the language in which FILENAME
1312 # is written: `texinfo' or `latex'.
1315 # If the user explicitly specified the language, use that.
1316 # Otherwise, if the first line is \input texinfo, assume it's texinfo.
1317 # Otherwise, guess from the file extension.
1318 if test -n "$set_language"; then
1320 elif sed 1q "$1" | grep 'input texinfo' >&6; then
1323 # Get the type of the file (latex or texinfo) from the given language
1324 # we just guessed, or from the file extension if not set yet.
1326 *.ltx | *.tex | *.drv | *.dtx) echo latex;;
1335 # Convert to HTML/INFO/TEXT.
1337 # Don't pass `-noiso' to hevea: it's useless in HTML since anyway the
1338 # charset is set to latin1, and troublesome in other modes since
1339 # accented characters loose their accents.
1341 # Don't pass `-o DEST' to hevea because in that case it leaves all its
1342 # auxiliary files there too... Too bad, because it means we will need
1343 # to handle images some day.
1346 local hevea="${HEVEA:-hevea}"
1347 local run_hevea="$hevea"
1351 text|info) run_hevea="$run_hevea -$1";;
1352 *) error 1 "run_hevea: invalid argument: $1";;
1355 # Compiling to the tmp directory enables to preserve a previous
1356 # successful compilation.
1357 run_hevea="$run_hevea -fix -O -o '$out_base'"
1358 run_hevea="$run_hevea `list_prefix includes -I` -I '$orig_pwd' "
1359 run_hevea="$run_hevea '$in_input'"
1362 run_hevea="$run_hevea -v -v"
1365 verbose "running $run_hevea"
1366 if eval "$run_hevea" >&5; then
1367 # hevea leaves trailing white spaces, this is annoying.
1368 case $1 in text|info)
1369 perl -pi -e 's/[ \t]+$//g' "$out_base"*;;
1372 html|text) move_to_dest "$out_base";;
1373 info) # There can be foo.info-1, foo.info-2 etc.
1374 move_to_dest "$out_base"*;;
1377 error 1 "$hevea exited with bad status, quitting."
1382 # run_core_conversion ()
1383 # ----------------------
1384 # Run the TeX (or HeVeA).
1385 run_core_conversion ()
1387 case $in_lang:`out_lang_tex` in
1390 latex:html|latex:text|latex:info)
1391 run_hevea $out_lang;;
1393 error 1 "invalid input/output combination: $in_lang/$out_lang";;
1400 # Run the full compilation chain, from pre-processing to installation
1401 # of the output at its expected location.
1404 # Source file might include additional sources.
1405 # We want `.:$orig_pwd' before anything else. (We'll add `.:' later
1406 # after all other directories have been turned into absolute paths.)
1407 # `.' goes first to ensure that any old .aux, .cps,
1408 # etc. files in ${directory} don't get used in preference to fresher
1409 # files in `.'. Include orig_pwd in case we are in clean build mode, where
1410 # we've cd'd to a temp directory.
1411 txincludes=`list_infix includes $path_sep`
1412 common="$orig_pwd$path_sep$in_dir$path_sep$txincludes$path_sep"
1413 for var in $tex_envvars; do
1414 eval val="\$common\$${var}_orig"
1415 # Convert relative paths to absolute paths, so we can run in another
1416 # directory (e.g., in clean build mode, or during the macro-support
1417 # detection). ".:" is added here.
1418 val=`absolute_filenames "$val"`
1419 eval $var="\"$val\""
1421 eval verbose \"$var=\'\$${var}\'\"
1427 # --command, --texinfo
1433 # Run until a fix point is reached.
1442 verbose "Removing" "$@"
1449 # Remove auxiliary files and directories. Changes the current directory.
1456 local log="$work_build/$in_noext.log"
1457 set X ${1+"$@"} "$log" `generated_files_get "$work_build/$in_noext"`
1466 # Remove what should be removed according to options.
1467 # Called at the end of each compilation cycle, and at the end of
1468 # the script. Changes the current directory.
1472 local) cd_orig; remove "$t2ddir";;
1473 clean) mostly_clean;;
1480 ## ---------------------- ##
1481 ## Command line parsing. ##
1482 ## ---------------------- ##
1484 # Push a token among the arguments that will be used to notice when we
1485 # ended options/arguments parsing.
1486 # Use "set dummy ...; shift" rather than 'set - ..." because on
1487 # Solaris set - turns off set -x (but keeps set -e).
1488 # Use ${1+"$@"} rather than "$@" because Digital Unix and Ultrix 4.3
1489 # still expand "$@" to a single argument (the empty string) rather
1490 # than nothing at all.
1492 set dummy ${1+"$@"} "$arg_sep"; shift
1495 # Parse command line arguments.
1496 while test x"$1" != x"$arg_sep"; do
1498 # Handle --option=value by splitting apart and putting back on argv.
1501 opt=`echo "$1" | sed -e 's/=.*//'`
1502 val=`echo "$1" | sed -e 's/[^=]*=//'`
1504 set dummy "$opt" "$val" ${1+"$@"}; shift
1508 # This recognizes --quark as --quiet. So what.
1511 # Silently and without documentation accept -b and --b[atch] as synonyms.
1512 -b | --batch) batch=true;;
1513 --build) shift; build_mode=$1;;
1514 --build-dir) shift; build_dir=$1; build_mode=tidy;;
1515 -c | --clean) build_mode=clean;;
1516 -D | --debug) debug=true;;
1517 --dvi) out_lang=dvi;;
1518 --dvipdf) out_lang=dvipdf;;
1519 -e | -E | --expand) expand=t;;
1520 -h | --help) usage;;
1521 --html) out_lang=html;;
1522 -I) shift; list_concat_dirs includes "$1";;
1523 --info) out_lang=info;;
1524 -l | --lang | --language) shift; set_language=$1;;
1525 --mostly-clean) action=mostly-clean;;
1526 --no-line-error) no_line_error=true;;
1527 -o | --out | --output)
1529 # Make it absolute, just in case we also have --clean, or whatever.
1530 oname=`absolute "$1"`;;
1531 -p | --pdf) out_lang=pdf;;
1533 -q | -s | --quiet | --silent) quiet=true; batch=true;;
1534 -r | --recode) recode=true;;
1535 --recode-from) shift; recode=true; recode_from="$1";;
1536 --src-specials) src_specials=--src-specials;;
1537 -t | --texinfo | --command ) shift; textra="$textra\\
1538 "`echo "$1" | sed 's/\\\\/\\\\\\\\/g'`;;
1539 --text) out_lang=text;;
1540 --translate-file ) shift; translate_file="$1";;
1541 --tidy) build_mode=tidy;;
1542 -v | --vers*) version;;
1543 -V | --verb*) verb=true;;
1544 --) # What remains are not options.
1546 while test x"$1" != x"$arg_sep"; do
1547 set dummy ${1+"$@"} "$1"; shift
1552 error 1 "Unknown or ambiguous option \`$1'." \
1553 "Try \`--help' for more information."
1555 *) set dummy ${1+"$@"} "$1"; shift;;
1562 # $tidy: compile in a t2d directory.
1563 # $clean: remove all the aux files.
1565 local) clean=false; tidy=false;;
1566 tidy) clean=false; tidy=true;;
1567 clean) clean=true; tidy=true;;
1568 *) error 1 "invalid build mode: $build_mode";;
1571 # Interpret remaining command line args as filenames.
1574 error 2 "Missing file arguments." "Try \`--help' for more information."
1578 if test -n "$oname"; then
1579 error 2 "Can't use option \`--output' with more than one argument."
1585 # We can't do much without tex.
1587 if findprog ${TEX:-tex}; then :; else cat <<EOM
1588 You don't have a working TeX binary (${TEX:-tex}) installed anywhere in
1589 your PATH, and texi2dvi cannot proceed without one. If you want to use
1590 this script, you'll need to install TeX (if you don't have it) or change
1591 your PATH or TEX environment variable (if you do). See the --help
1592 output for more details.
1594 For information about obtaining TeX, please see http://www.tug.org. If
1595 you happen to be using Debian, you can get it with this command:
1596 apt-get install tetex-bin
1602 # We want to use etex (or pdftex) if they are available, and the user
1603 # didn't explicitly specify. We don't check for elatex and pdfelatex
1604 # because (as of 2003), the LaTeX team has asked that new distributions
1605 # use etex by default anyway.
1607 # End up with the TEX and PDFTEX variables set to what we are going to use.
1608 if test -z "$TEX"; then
1609 if findprog etex; then TEX=etex; else TEX=tex; fi
1612 if test -z "$PDFTEX"; then
1613 if findprog pdfetex; then PDFTEX=pdfetex; else PDFTEX=pdftex; fi
1617 # File descriptor usage:
1619 # 1 standard output (--verbose messages)
1621 # 3 some systems may open it to /dev/tty
1622 # 4 used on the Kubota Titan
1623 # 5 tools output (turned off by --quiet)
1624 # 6 tracing/debugging (set -x output, etc.)
1627 # Main tools' output (TeX, etc.) that TeX users are used to seeing.
1629 # If quiet, discard, else redirect to the message flow.
1637 # Enable tracing, and auxiliary tools output.
1639 # Should be used where you'd typically use /dev/null to throw output
1640 # away. But sometimes it is convenient to see that output (e.g., from
1641 # a grep) to aid debugging. Especially debugging at distance, via the
1652 # input_file_name_decode
1653 # ----------------------
1654 # Decode COMMAND_LINE_FILENAME, and compute:
1655 # - COMMAND_LINE_FILENAME clean of TeX commands
1657 # The directory to the input file, possibly absolute if needed.
1659 # The absolute directory of the input file.
1661 # The input file base name (no directory part).
1663 # The input file name without extensions (nor directory part).
1665 # Defaults to COMMAND_LINE_FILENAME, but might change if the
1666 # input is preprocessed (recode etc.). With directory, possibly absolute.
1667 input_file_name_decode ()
1669 # See if we are run from within AUC-Tex, in which case we are
1670 # passed `\input{FOO.tex}' or even `\nonstopmode\input{FOO.tex}'.
1671 case $command_line_filename in
1675 case $command_line_filename in
1677 # Let AUC-TeX error parser deal with line numbers.
1679 command_line_filename=`\
1680 expr X"$command_line_filename" : X'.*input{\([^}]*\)}'`
1684 # If the COMMAND_LINE_FILENAME is not absolute (e.g., --debug.tex),
1685 # prepend `./' in order to avoid that the tools take it as an option.
1686 echo "$command_line_filename" | $EGREP '^(/|[A-z]:/)' >&6 \
1687 || command_line_filename="./$command_line_filename"
1689 # See if the file exists. If it doesn't we're in trouble since, even
1690 # though the user may be able to reenter a valid filename at the tex
1691 # prompt (assuming they're attending the terminal), this script won't
1692 # be able to find the right xref files and so forth.
1693 test -r "$command_line_filename" ||
1694 error 1 "cannot read $command_line_filename, skipping."
1696 # Get the name of the current directory.
1697 in_dir=`func_dirname "$command_line_filename"`
1698 in_dir_abs=`absolute "$in_dir"`
1699 # In a clean build, we `cd', so get an absolute file name.
1704 # Strip directory part but leave extension.
1705 in_base=`basename "$command_line_filename"`
1707 in_noext=`echo "$in_base" | sed 's/\.[^.]*$//'`
1709 # The normalized file name to compile. Must always point to the
1710 # file to actually compile (in case of recoding, macro-expansion etc.).
1711 in_input=$in_dir/$in_base
1714 # Compute the output file name.
1715 if test x"$oname" != x; then
1718 out_name=$in_noext.`out_lang_ext`
1720 out_dir=`func_dirname "$out_name"`
1721 out_dir_abs=`absolute "$out_dir"`
1722 out_base=`basename "$out_name"`
1723 out_noext=`echo "$out_base" | sed 's/\.[^.]*$//'`
1727 ## -------------- ##
1729 ## -------------- ##
1731 for command_line_filename
1733 verbose "Processing $command_line_filename ..."
1735 input_file_name_decode
1737 # `texinfo' or `latex'?
1738 in_lang=`compute_language "$command_line_filename"`
1740 # An auxiliary directory used for all the auxiliary tasks involved
1741 # in compiling this document.
1743 '' | . ) t2ddir=$out_noext.t2d ;;
1744 *) # Avoid collisions between multiple occurrences of the same
1745 # file. The sed expression is fragile if the cwd has
1746 # active characters.
1747 t2ddir=$build_dir/`echo "$out_dir_abs/$out_noext.t2d" |
1748 sed "s,^$orig_pwd/,," |
1751 # Remove it at exit if clean mode.
1752 trap "cleanup" 0 1 2 15
1754 ensure_dir "$build_dir" "$t2ddir"
1756 # We will change directory, better work with an absolute path...
1757 t2ddir=`absolute "$t2ddir"`
1758 # Sometimes there are incompatibilities between auxiliary files for
1759 # DVI and PDF. The contents can also change whether we work on PDF
1760 # and/or DVI. So keep separate spaces for each.
1761 workdir=$t2ddir/`out_lang_tex`
1762 ensure_dir "$workdir"
1764 # _build. In a tidy build, where the auxiliary files are output.
1766 work_build=$workdir/build
1771 # _bak. Copies of the previous auxiliary files (another round is
1772 # run if they differ from the new ones).
1773 work_bak=$workdir/bak
1775 # Make those directories.
1776 ensure_dir "$work_build" "$work_bak"
1780 # Compile the document.
1792 exit 0 # exit successfully, not however we ended the loop.