1#! /bin/sh
2# texi2dvi --- produce DVI (or PDF) files from Texinfo (or LaTeX) sources.
3# $Id: texi2dvi,v 0.43 1999/09/28 19:36:53 karl Exp $
4#
5# Copyright (C) 1992, 93, 94, 95, 96, 97, 98, 99 Free Software Foundation, Inc.
6#
7# This program is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation; either version 2, or (at your option)
10# any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program; if not, you can either send email to this
19# program's maintainer or write to: The Free Software Foundation,
20# Inc.; 59 Temple Place, Suite 330; Boston, MA 02111-1307, USA.
21#
22# Original author: Noah Friedman <friedman@gnu.org>.
23#
24# Please send bug reports, etc. to bug-texinfo@gnu.org.
25# If possible, please send a copy of the output of the script called with
26# the `--debug' option when making a bug report.
27
28# This string is expanded by rcs automatically when this file is checked out.
29rcs_revision='$Revision: 0.43 $'
30rcs_version=`set - $rcs_revision; echo $2`
31program=`echo $0 | sed -e 's!.*/!!'`
32version="texi2dvi (GNU Texinfo 4.0) $rcs_version
33
34Copyright (C) 1999 Free Software Foundation, Inc.
35There is NO warranty.  You may redistribute this software
36under the terms of the GNU General Public License.
37For more information about these matters, see the files named COPYING."
38
39usage="\
40Usage: $program [OPTION]... FILE...
41
42Run each Texinfo or LaTeX FILE through TeX in turn until all
43cross-references are resolved, building all indices.  The directory
44containing each FILE is searched for included files.  The suffix of FILE
45is used to determine its language (LaTeX or Texinfo).
46
47Makeinfo is used to perform Texinfo macro expansion before running TeX
48when needed.
49
50Operation modes:
51  -h, --help          display this help and exit successfully
52  -v, --version       display version information and exit successfully
53  -V, --verbose       report on what is done
54  -q, --quiet         no output unless errors (implies --batch)
55  -s, --silent        same as --quiet
56  -D, --debug         turn on shell debugging (set -x)
57  -b, --batch         no interaction
58  -c, --clean         remove all auxiliary files
59  -o, --output=FILE   leave output in FILE (implies --clean)
60
61Only one FILE may be specified if \`--output' is used.
62
63
64TeX tuning:
65  -@                   use @input instead of \input; for preloaded Texinfo
66  -e, --expand         force macro expansion using makeinfo
67  -I DIR               search DIR for Texinfo files
68  -l, --language=LANG  specify the LANG of FILE (LaTeX or Texinfo)
69  -p, --pdf            use pdftex or pdflatex for processing
70  -t, --texinfo=CMD    insert CMD after @setfilename in copy of input file
71                       multiple values accumulate
72
73The values of the BIBTEX, LATEX (or PDFLATEX), MAKEINDEX, MAKEINFO,
74TEX (or PDFTEX), and TEXINDEX environment variables are used to run
75those commands, if they are set.
76
77Email bug reports to <bug-texinfo@gnu.org>,
78general questions and discussion to <help-texinfo@gnu.org>."
79
80# Initialize variables for option overriding and otherwise.
81# Don't use `unset' since old bourne shells don't have this command.
82# Instead, assign them an empty value.
83batch=false     # eval for batch mode
84clean=
85debug=
86escape='\'
87expand=         # t for expansion via makeinfo
88miincludes=     # makeinfo include path
89oformat=dvi
90oname=		# --output
91quiet=          # by default let the tools' message be displayed
92set_language=
93textra=
94tmpdir=$(mktemp -d -t ${program}) || exit 1
95txincludes=     # TEXINPUTS extensions
96txiprereq=19990129 # minimum texinfo.tex version to have macro expansion
97verbose=false   # echo for verbose mode
98
99orig_pwd=`pwd`
100
101# Systems which define $COMSPEC or $ComSpec use semicolons to separate
102# directories in TEXINPUTS.
103if test -n "$COMSPEC$ComSpec"; then
104  path_sep=";"
105else
106  path_sep=":"
107fi
108
109# Save this so we can construct a new TEXINPUTS path for each file.
110TEXINPUTS_orig="$TEXINPUTS"
111# Unfortunately makeindex does not read TEXINPUTS.
112INDEXSTYLE_orig="$INDEXSTYLE"
113export TEXINPUTS INDEXSTYLE
114
115# Push a token among the arguments that will be used to notice when we
116# ended options/arguments parsing.
117# Use "set dummy ...; shift" rather than 'set - ..." because on
118# Solaris set - turns off set -x (but keeps set -e).
119# Use ${1+"$@"} rather than "$@" because Digital Unix and Ultrix 4.3
120# still expand "$@" to a single argument (the empty string) rather
121# than nothing at all.
122arg_sep="$$--$$"
123set dummy ${1+"$@"} "$arg_sep"; shift
124
125#
126# Parse command line arguments.
127while test x"$1" != x"$arg_sep"; do
128
129  # Handle --option=value by splitting apart and putting back on argv.
130  case "$1" in
131    --*=*)
132      opt=`echo "$1" | sed -e 's/=.*//'`
133      val=`echo "$1" | sed -e 's/[^=]*=//'`
134      shift
135      set dummy "$opt" "$val" ${1+"$@"}; shift
136      ;;
137  esac
138
139  # This recognizes --quark as --quiet.  So what.
140  case "$1" in
141    -@ ) escape=@;;
142    # Silently and without documentation accept -b and --b[atch] as synonyms.
143    -b | --b*) batch=eval;;
144    -q | -s | --q* | --s*) quiet=t; batch=eval;;
145    -c | --c*) clean=t;;
146    -D | --d*) debug=t;;
147    -e | --e*) expand=t;;
148    -h | --h*) echo "$usage"; exit 0;;
149    -I | --I*)
150      shift
151      miincludes="$miincludes -I $1"
152      txincludes="$txincludes$path_sep$1"
153      ;;
154    -l | --l*) shift; set_language=$1;;
155    -o | --o*)
156      shift
157      clean=t
158      case "$1" in
159        /* | ?:/*) oname=$1;;
160                *) oname="$orig_pwd/$1";;
161      esac;;
162    -p | --p*) oformat=pdf;;
163    -t | --t*) shift; textra="$textra\\
164$1";;
165    -v | --vers*) echo "$version"; exit 0;;
166    -V | --verb*) verbose=echo;;
167    --) # What remains are not options.
168      shift
169      while test x"$1" != x"$arg_sep"; do
170        set dummy ${1+"$@"} "$1"; shift
171        shift
172      done
173      break;;
174    -*)
175      echo "$0: Unknown or ambiguous option \`$1'." >&2
176      echo "$0: Try \`--help' for more information." >&2
177      exit 1;;
178    *) set dummy ${1+"$@"} "$1"; shift;;
179   esac
180   shift
181done
182# Pop the token
183shift
184
185# Interpret remaining command line args as filenames.
186case $# in
187 0)
188  echo "$0: Missing file arguments." >&2
189  echo "$0: Try \`--help' for more information." >&2
190  exit 2
191  ;;
192 1) ;;
193 *)
194  if test -n "$oname"; then
195    echo "$0: Can't use option \`--output' with more than one argument." >&2
196    exit 2
197  fi
198  ;;
199esac
200
201# Prepare the temporary directory.  Remove it at exit, unless debugging.
202if test -z "$debug"; then
203  trap "cd / && rm -rf $tmpdir" 0 1 2 15
204fi
205
206# Prepare the tools we might need.  This may be extra work in some
207# cases, but improves the readibility of the script.
208utildir=$tmpdir/utils
209mkdir $utildir || exit 1
210
211# A sed script that preprocesses Texinfo sources in order to keep the
212# iftex sections only.  We want to remove non TeX sections, and
213# comment (with `@c texi2dvi') TeX sections so that makeinfo does not
214# try to parse them.  Nevertheless, while commenting TeX sections,
215# don't comment @macro/@end macro so that makeinfo does propagate
216# them.  Unfortunately makeinfo --iftex --no-ifhtml --no-ifinfo
217# doesn't work well enough (yet) to use that, so work around with sed.
218comment_iftex_sed=$utildir/comment.sed
219cat <<EOF >$comment_iftex_sed
220/^@tex/,/^@end tex/{
221  s/^/@c texi2dvi/
222}
223/^@iftex/,/^@end iftex/{
224  s/^/@c texi2dvi/
225  /^@c texi2dvi@macro/,/^@c texi2dvi@end macro/{
226    s/^@c texi2dvi//
227  }
228}
229/^@html/,/^@end html/d
230/^@ifhtml/,/^@end ifhtml/d
231/^@ifnottex/,/^@end ifnottex/d
232/^@ifinfo/,/^@end ifinfo/{
233  /^@node/p
234  /^@menu/,/^@end menu/p
235  d
236}
237EOF
238# Uncommenting is simple: Remove any leading `@c texi2dvi'.
239uncomment_iftex_sed=$utildir/uncomment.sed
240cat <<EOF >$uncomment_iftex_sed
241s/^@c texi2dvi//
242EOF
243
244# A shell script that computes the list of xref files.
245# Takes the filename (without extension) of which we look for xref
246# files as argument.  The index files must be reported last.
247get_xref_files=$utildir/get_xref.sh
248cat <<\EOF >$get_xref_files
249#! /bin/sh
250
251# Get list of xref files (indexes, tables and lists).
252# Find all files having root filename with a two-letter extension,
253# saves the ones that are really Texinfo-related files.  .?o? catches
254# LaTeX tables and lists.
255for this_file in "$1".?o? "$1".aux "$1".?? "$1".idx; do
256  # If file is empty, skip it.
257  test -s "$this_file" || continue
258  # If the file is not suitable to be an index or xref file, don't
259  # process it.  The file can't be if its first character is not a
260  # backslash or single quote.
261  first_character=`sed -n '1s/^\(.\).*$/\1/p;q' $this_file`
262  if test "x$first_character" = "x\\" \
263     || test "x$first_character" = "x'"; then
264    xref_files="$xref_files ./$this_file"
265  fi
266done
267echo "$xref_files"
268EOF
269chmod 500 $get_xref_files
270
271# File descriptor usage:
272# 0 standard input
273# 1 standard output (--verbose messages)
274# 2 standard error
275# 3 some systems may open it to /dev/tty
276# 4 used on the Kubota Titan
277# 5 tools output (turned off by --quiet)
278
279# Tools' output.  If quiet, discard, else redirect to the message flow.
280if test "$quiet" = t; then
281  exec 5>/dev/null
282else
283  exec 5>&1
284fi
285
286# Enable tracing
287test "$debug" = t && set -x
288
289#
290# TeXify files.
291
292for command_line_filename in ${1+"$@"}; do
293  $verbose "Processing $command_line_filename ..."
294
295  # If the COMMAND_LINE_FILENAME is not absolute (e.g., --debug.tex),
296  # prepend `./' in order to avoid that the tools take it as an option.
297  echo "$command_line_filename" | egrep '^(/|[A-z]:/)' >/dev/null \
298  || command_line_filename="./$command_line_filename"
299
300  # See if the file exists.  If it doesn't we're in trouble since, even
301  # though the user may be able to reenter a valid filename at the tex
302  # prompt (assuming they're attending the terminal), this script won't
303  # be able to find the right xref files and so forth.
304  if test ! -r "$command_line_filename"; then
305    echo "$0: Could not read $command_line_filename, skipping." >&2
306    continue
307  fi
308
309  # Get the name of the current directory.  We want the full path
310  # because in clean mode we are in tmp, in which case a relative
311  # path has no meaning.
312  filename_dir=`echo $command_line_filename | sed 's!/[^/]*$!!;s!^$!.!'`
313  filename_dir=`cd "$filename_dir" >/dev/null && pwd`
314
315  # Strip directory part but leave extension.
316  filename_ext=`basename "$command_line_filename"`
317  # Strip extension.
318  filename_noext=`echo "$filename_ext" | sed 's/\.[^.]*$//'`
319  ext=`echo "$filename_ext" | sed 's/^.*\.//'`
320
321  # _src.  Use same basename since we want to generate aux files with
322  # the same basename as the manual.  If --expand, then output the
323  # macro-expanded file to here, else copy the original file.
324  tmpdir_src=$tmpdir/src
325  filename_src=$tmpdir_src/$filename_noext.$ext
326
327  # _xtr.  The file with the user's extra commands.
328  tmpdir_xtr=$tmpdir/xtr
329  filename_xtr=$tmpdir_xtr/$filename_noext.$ext
330
331  # _bak.  Copies of the previous xref files (another round is run if
332  # they differ from the new one).
333  tmpdir_bak=$tmpdir/bak
334
335  # Make all those directories and give up if we can't succeed.
336  mkdir $tmpdir_src $tmpdir_xtr $tmpdir_bak || exit 1
337
338  # Source file might include additional sources.  Put `.' and
339  # directory where source file(s) reside in TEXINPUTS before anything
340  # else.  `.' goes first to ensure that any old .aux, .cps,
341  # etc. files in ${directory} don't get used in preference to fresher
342  # files in `.'.  Include orig_pwd in case we are in clean mode, where
343  # we've cd'd to a temp directory.
344  common=".$path_sep$orig_pwd$path_sep$filename_dir$path_sep$txincludes$path_sep"
345   TEXINPUTS="$common$TEXINPUTS_orig"
346  INDEXSTYLE="$common$INDEXSTYLE_orig"
347
348  # If the user explicitly specified the language, use that.
349  # Otherwise, if the first line is \input texinfo, assume it's texinfo.
350  # Otherwise, guess from the file extension.
351  if test -n "$set_language"; then
352    language=$set_language
353  elif sed 1q "$command_line_filename" | fgrep 'input texinfo' >/dev/null; then
354    language=texinfo
355  else
356    language=
357  fi
358
359  # Get the type of the file (latex or texinfo) from the given language
360  # we just guessed, or from the file extension if not set yet.
361  case ${language:-$filename_ext} in
362    [lL]a[tT]e[xX] | *.ltx | *.tex)
363      # Assume a LaTeX file.  LaTeX needs bibtex and uses latex for
364      # compilation.  No makeinfo.
365      bibtex=${BIBTEX:-bibtex}
366      makeinfo= # no point in running makeinfo on latex source.
367      texindex=${MAKEINDEX:-makeindex}
368      if test $oformat = dvi; then
369        tex=${LATEX:-latex}
370      else
371        tex=${PDFLATEX:-pdflatex}
372      fi
373      ;;
374
375    *)
376      # Assume a Texinfo file.  Texinfo files need makeinfo, texindex and tex.
377      bibtex=
378      texindex=${TEXINDEX:-texindex}
379      if test $oformat = dvi; then
380        tex=${TEX:-tex}
381      else
382        tex=${PDFTEX:-pdftex}
383      fi
384      # Unless required by the user, makeinfo expansion is wanted only
385      # if texinfo.tex is too old.
386      if test "$expand" = t; then
387        makeinfo=${MAKEINFO:-makeinfo}
388      else
389        # Check if texinfo.tex performs macro expansion by looking for
390        # its version.  The version is a date of the form YEAR-MO-DA.
391        # We don't need to use [0-9] to match the digits since anyway
392        # the comparison with $txiprereq, a number, will fail with non
393        # digits.
394        txiversion_tex=txiversion.tex
395        echo '\input texinfo.tex @bye' >$tmpdir/$txiversion_tex
396        # Run in the tmpdir to avoid leaving files.
397        eval `cd $tmpdir >/dev/null &&
398              $tex $txiversion_tex 2>/dev/null |
399              sed -n 's/^.*\[\(.*\)version \(....\)-\(..\)-\(..\).*$/txiformat=\1 txiversion="\2\3\4"/p'`
400        $verbose "texinfo.tex preloaded as \`$txiformat', version is \`$txiversion' ..."
401        if test "$txiprereq" -le "$txiversion" >/dev/null 2>&1; then
402          makeinfo=
403        else
404          makeinfo=${MAKEINFO:-makeinfo}
405        fi
406        # As long as we had to run TeX, offer the user this convenience
407        if test "$txiformat" = Texinfo; then
408          escape=@
409        fi
410      fi
411      ;;
412  esac
413
414  # Expand macro commands in the original source file using Makeinfo.
415  # Always use `end' footnote style, since the `separate' style
416  #   generates different output (arguably this is a bug in -E).
417  # Discard main info output, the user asked to run TeX, not makeinfo.
418  if test -n "$makeinfo"; then
419    $verbose "Macro-expanding $command_line_filename to $filename_src ..."
420    sed -f $comment_iftex_sed "$command_line_filename" \
421      | $makeinfo --footnote-style=end -I "$filename_dir" $miincludes \
422        -o /dev/null --macro-expand=- \
423      | sed -f $uncomment_iftex_sed >"$filename_src"
424    filename_input=$filename_src
425  fi
426
427  # If makeinfo failed (or was not even run), use the original file as input.
428  if test $? -ne 0 \
429     || test ! -r "$filename_src"; then
430    $verbose "Reverting to $command_line_filename ..."
431    filename_input=$filename_dir/$filename_ext
432  fi
433
434  # Used most commonly for @finalout, @smallbook, etc.
435  if test -n "$textra"; then
436    $verbose "Inserting extra commands: $textra"
437    sed '/^@setfilename/a\
438'"$textra" "$filename_input" >$filename_xtr
439    filename_input=$filename_xtr
440  fi
441
442  # If clean mode was specified, then move to the temporary directory.
443  if test "$clean" = t; then
444    $verbose "cd $tmpdir_src"
445    cd "$tmpdir_src" || exit 1
446  fi
447
448  while :; do # will break out of loop below
449    orig_xref_files=`$get_xref_files "$filename_noext"`
450
451    # Save copies of originals for later comparison.
452    if test -n "$orig_xref_files"; then
453      $verbose "Backing up xref files: `echo $orig_xref_files | sed 's|\./||g'`"
454      cp $orig_xref_files $tmpdir_bak
455    fi
456
457    # Run bibtex on current file.
458    # - If its input (AUX) exists.
459    # - If AUX contains both `\bibdata' and `\bibstyle'.
460    # - If some citations are missing (LOG contains `Citation').
461    #   or the LOG complains of a missing .bbl
462    #
463    # We run bibtex first, because I can see reasons for the indexes
464    # to change after bibtex is run, but I see no reason for the
465    # converse.
466    #
467    # Don't try to be too smart.  Running bibtex only if the bbl file
468    # exists and is older than the LaTeX file is wrong, since the
469    # document might include files that have changed.  Because there
470    # can be several AUX (if there are \include's), but a single LOG,
471    # looking for missing citations in LOG is easier, though we take
472    # the risk to match false messages.
473    if test -n "$bibtex" \
474       && test -r "$filename_noext.aux" \
475       && test -r "$filename_noext.log" \
476       && (grep '^\\bibdata[{]'  "$filename_noext.aux" \
477           && grep '^\\bibstyle[{]' "$filename_noext.aux" \
478           && (grep 'Warning:.*Citation.*undefined' "$filename_noext.log" \
479               || grep 'No file .*\.bbl\.' "$filename_noext.log")) \
480          >/dev/null 2>&1; \
481    then
482      $verbose "Running $bibtex $filename_noext ..."
483      if $bibtex "$filename_noext" >&5; then :; else
484        echo "$0: $bibtex exited with bad status, quitting." >&2
485        exit 1
486      fi
487    fi
488
489    # What we'll run texindex on -- exclude non-index files.
490    # Since we know index files are last, it is correct to remove everything
491    # before .aux and .?o?.
492    index_files=`echo "$orig_xref_files" \
493                 | sed "s!.*\.aux!!g;
494                        s!./$filename_noext\..o.!!g;
495                        s/^[ ]*//;s/[ ]*$//"`
496    # Run texindex (or makeindex) on current index files.  If they
497    # already exist, and after running TeX a first time the index
498    # files don't change, then there's no reason to run TeX again.
499    # But we won't know that if the index files are out of date or
500    # nonexistent.
501    if test -n "$texindex" && test -n "$index_files"; then
502      $verbose "Running $texindex $index_files ..."
503      if $texindex $index_files 2>&5 1>&2; then :; else
504         echo "$0: $texindex exited with bad status, quitting." >&2
505         exit 1
506      fi
507    fi
508
509    # Finally, run TeX.
510    # Prevent $ESCAPE from being interpreted by the shell if it happens
511    # to be `/'.
512    $batch tex_args="\\${escape}nonstopmode\ \\${escape}input"
513    $verbose "Running $cmd ..."
514    cmd="$tex $tex_args $filename_input"
515    if $cmd >&5; then :; else
516      echo "$0: $tex exited with bad status, quitting." >&2
517      echo "$0: see $filename_noext.log for errors." >&2
518      test "$clean" = t \
519        && cp "$filename_noext.log" "$orig_pwd"
520      exit 1
521    fi
522
523
524    # Decide if looping again is needed.
525    finished=t
526
527    # LaTeX (and the package changebar) report in the LOG file if it
528    # should be rerun.  This is needed for files included from
529    # subdirs, since texi2dvi does not try to compare xref files in
530    # subdirs.  Performing xref files test is still good since LaTeX
531    # does not report changes in xref files.
532    if fgrep "Rerun to get" "$filename_noext.log" >/dev/null 2>&1; then
533      finished=
534    fi
535
536    # Check if xref files changed.
537    new_xref_files=`$get_xref_files "$filename_noext"`
538    $verbose "Original xref files = `echo $orig_xref_files | sed 's|\./||g'`"
539    $verbose "New xref files      = `echo $new_xref_files | sed 's|\./||g'`"
540
541    # If old and new lists don't at least have the same file list,
542    # then one file or another has definitely changed.
543    test "x$orig_xref_files" != "x$new_xref_files" && finished=
544
545    # File list is the same.  We must compare each file until we find
546    # a difference.
547    if test -n "$finished"; then
548      for this_file in $new_xref_files; do
549        $verbose "Comparing xref file `echo $this_file | sed 's|\./||g'` ..."
550        # cmp -s returns nonzero exit status if files differ.
551        if cmp -s "$this_file" "$tmpdir_bak/$this_file"; then :; else
552          # We only need to keep comparing until we find one that
553          # differs, because we'll have to run texindex & tex again no
554          # matter how many more there might be.
555          finished=
556          $verbose "xref file `echo $this_file | sed 's|\./||g'` differed ..."
557          test "$debug" = t && diff -c "$tmpdir_bak/$this_file" "$this_file"
558          break
559        fi
560      done
561    fi
562
563    # If finished, exit the loop, else rerun the loop.
564    test -n "$finished" && break
565  done
566
567  # If we were in clean mode, compilation was in a tmp directory.
568  # Copy the DVI (or PDF) file into the directory where the compilation
569  # has been done.  (The temp dir is about to get removed anyway.)
570  # We also return to the original directory so that
571  # - the next file is processed in correct conditions
572  # - the temporary file can be removed
573  if test -n "$clean"; then
574    if test -n "$oname"; then
575       dest=$oname
576    else
577       dest=$orig_pwd
578    fi
579    $verbose "Copying $oformat file from `pwd` to $dest"
580    cp -p "./$filename_noext.$oformat" "$dest"
581    cd / # in case $orig_pwd is on a different drive (for DOS)
582    cd $orig_pwd || exit 1
583  fi
584
585  # Remove temporary files.
586  if test "x$debug" = "x"; then
587    $verbose "Removing $tmpdir_src $tmpdir_xtr $tmpdir_bak ..."
588    cd /
589    rm -rf $tmpdir_src $tmpdir_xtr $tmpdir_bak
590  fi
591done
592
593$verbose "$0 done."
594exit 0 # exit successfully, not however we ended the loop.
595