The R Project SVN R

Rev

Details | Last modification | View Log | RSS feed

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