summaryrefslogtreecommitdiff
path: root/Master/bin/i386-freebsd/texi2dvi
blob: 18f6f59322b20335198c506db214548a46e250d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
#! /bin/sh
# texi2dvi --- produce DVI (or PDF) files from Texinfo (or LaTeX) sources.
# $Id: texi2dvi,v 1.42 2005/02/11 13:35:14 karl Exp $
#
# Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2001,
# 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, you can either send email to this
# program's maintainer or write to: The Free Software Foundation,
# Inc.; 59 Temple Place, Suite 330; Boston, MA 02111-1307, USA.
#
# Original author: Noah Friedman.
#
# Please send bug reports, etc. to bug-texinfo@gnu.org.
# If possible, please send a copy of the output of the script called with
# the `--debug' option when making a bug report.

test -f /bin/ksh && test -z "$RUNNING_KSH" \
  && { UNAMES=`uname -s`; test "x$UNAMES" = xULTRIX; } 2>/dev/null \
  && { RUNNING_KSH=true; export RUNNING_KSH; exec /bin/ksh $0 ${1+"$@"}; }
unset RUNNING_KSH

# This string is expanded by rcs automatically when this file is checked out.
rcs_revision='$Revision: 1.42 $'
rcs_version=`set - $rcs_revision; echo $2`
program=`echo $0 | sed -e 's!.*/!!'`
version="texi2dvi (GNU Texinfo 4.8) $rcs_version

Copyright (C) 2005 Free Software Foundation, Inc.
There is NO warranty.  You may redistribute this software
under the terms of the GNU General Public License.
For more information about these matters, see the files named COPYING."

usage="Usage: $program [OPTION]... FILE...

Run each Texinfo or LaTeX FILE through TeX in turn until all
cross-references are resolved, building all indices.  The directory
containing each FILE is searched for included files.  The suffix of FILE
is used to determine its language (LaTeX or Texinfo).

Makeinfo is used to perform Texinfo macro expansion before running TeX
when needed.

Operation modes:
  -b, --batch         no interaction
  -c, --clean         remove all auxiliary files
  -D, --debug         turn on shell debugging (set -x)
  -h, --help          display this help and exit successfully
  -o, --output=OFILE  leave output in OFILE (implies --clean);
                      Only one input FILE may be specified in this case
  -q, --quiet         no output unless errors (implies --batch)
  -s, --silent        same as --quiet
  -v, --version       display version information and exit successfully
  -V, --verbose       report on what is done

TeX tuning:
  -@                   use @input instead of \input; for preloaded Texinfo
  -e, -E, --expand     force macro expansion using makeinfo
  -I DIR               search DIR for Texinfo files
  -l, --language=LANG  specify the LANG of FILE (LaTeX or Texinfo)
  -p, --pdf            use pdftex or pdflatex for processing
  -r, --recode         call recode before TeX to translate input characters
  -t, --command=CMD    insert CMD in copy of input file
   or --texinfo=CMD    multiple values accumulate

The values of the BIBTEX, LATEX (or PDFLATEX), MAKEINDEX, MAKEINFO,
TEX (or PDFTEX), TEXINDEX, and THUMBPDF environment variables are used
to run those commands, if they are set.  Any CMD strings are added
after @setfilename for Texinfo input, in the first line for LaTeX input.

Email bug reports to <bug-texinfo@gnu.org>,
general questions and discussion to <help-texinfo@gnu.org>.
Texinfo home page: http://www.gnu.org/software/texinfo/"

# Initialize variables for option overriding and otherwise.
# Don't use `unset' since old bourne shells don't have this command.
# Instead, assign them an empty value.
batch=false     # eval for batch mode
clean=
debug=
escape='\'
expand=         # t for expansion via makeinfo
miincludes=     # makeinfo include path
oformat=dvi
oname=          # --output
quiet=          # by default let the tools' message be displayed
recode=false
set_language=
textra=         # Extra TeX commands to insert in the input file.
textra_cmd=     # sed command to insert TEXTRA where appropriate
tmpdir=${TMPDIR:-/tmp}/t2d$$  # avoid collisions on 8.3 filesystems.
txincludes=     # TEXINPUTS extensions, with trailing colon
txiprereq=19990129 # minimum texinfo.tex version with macro expansion
verb=false      # echo for verbose mode

orig_pwd=`pwd`

# Systems which define $COMSPEC or $ComSpec use semicolons to separate
# directories in TEXINPUTS -- except for Cygwin, where COMSPEC might be
# inherited, but : is used.
if test -n "$COMSPEC$ComSpec" && uname | grep -iv cygwin >/dev/null; then
  path_sep=";"
else
  path_sep=":"
fi

# Pacify verbose cds.
CDPATH=${ZSH_VERSION+.}$path_sep

# In case someone crazy insists on using grep -E.
: ${EGREP=egrep}

# return true if program $1 is somewhere in PATH, else false.
#
findprog ()
{
  foundprog=false

  saveIFS=$IFS
  IFS=$path_sep  # break path components at the path separator
  for dir in $PATH; do
    # use test -x rather than test -f for DJGPP, where test -x checks
    # for .exe.  But test -x will also return true for directories, so
    # explicitly ignore those.
    if test -x "$dir/$1" && test ! -d "$dir/$1"; then
      foundprog=true
      break

    # But on cygwin, test -x foo will not find foo.exe.  So also check
    # for that.
    elif test -x "$dir/$1.exe"; then
      foundprog=true
      break      
    fi
  done
  IFS=$saveIFS
  $foundprog
}

# Report some information.
report ()
{
  for i in "$@"
  do
    echo >&2 "$0: $i"
  done
}

# Report some verbose information.
verbose ()
{
  $verb >&2 "$0: $@"
}

# Report an error and exit with failure.
fatal ()
{
  s=$1
  shift
  report "$@"
  exit $s
}

# Save TEXINPUTS so we can construct a new TEXINPUTS path for each file.
# Likewise for bibtex and makeindex.
tex_envvars="BIBINPUTS BSTINPUTS INDEXSTYLE TEXINPUTS"
for var in $tex_envvars; do
  eval ${var}_orig=\$$var
  export $var
done


# Push a token among the arguments that will be used to notice when we
# ended options/arguments parsing.
# Use "set dummy ...; shift" rather than 'set - ..." because on
# Solaris set - turns off set -x (but keeps set -e).
# Use ${1+"$@"} rather than "$@" because Digital Unix and Ultrix 4.3
# still expand "$@" to a single argument (the empty string) rather
# than nothing at all.
arg_sep="$$--$$"
set dummy ${1+"$@"} "$arg_sep"; shift

# 
# Parse command line arguments.
while test x"$1" != x"$arg_sep"; do

  # Handle --option=value by splitting apart and putting back on argv.
  case "$1" in
    --*=*)
      opt=`echo "$1" | sed -e 's/=.*//'`
      val=`echo "$1" | sed -e 's/[^=]*=//'`
      shift
      set dummy "$opt" "$val" ${1+"$@"}; shift
      ;;
  esac

  # This recognizes --quark as --quiet.  So what.
  case "$1" in
    -@ ) escape=@;;
    # Silently and without documentation accept -b and --b[atch] as synonyms.
    -b | --b*) batch=true;;
    -c | --c*) clean=t;;
    -D | --d*) debug=t;;
    -e | -E | --e*) expand=t;;
    -h | --h*) echo "$usage"; exit 0;;
    -I | --I*)
      shift
      miincludes="$miincludes -I $1"
      txincludes="$txincludes$1$path_sep"
      ;;
    -l | --l*) shift; set_language=$1;;
    -o | --o*)
      shift
      clean=t
      case "$1" in
        /* | ?:/*) oname=$1;;
                *) oname="$orig_pwd/$1";;
      esac;;
    -p | --p*) oformat=pdf;;
    -q | -s | --q* | --s*) quiet=t; batch=true;;
    -r | --r*) recode=true;;
    -t | --tex* | --com* ) shift; textra="$textra\\
"`echo "$1" | sed 's/\\\\/\\\\\\\\/g'`;;
    -v | --vers*) echo "$version"; exit 0;;
    -V | --verb*) verb=echo;;
    --) # What remains are not options.
      shift
      while test x"$1" != x"$arg_sep"; do
        set dummy ${1+"$@"} "$1"; shift
        shift
      done
      break;;
    -*)
      fatal 1 "Unknown or ambiguous option \`$1'." \
              "Try \`--help' for more information."
      ;;
    *) set dummy ${1+"$@"} "$1"; shift;;
   esac
   shift
done
# Pop the token
shift

# Interpret remaining command line args as filenames.
case $# in
 0)
  fatal 2 "Missing file arguments." "Try \`--help' for more information."
  ;;
 1) ;;
 *)
  if test -n "$oname"; then
    fatal 2 "Can't use option \`--output' with more than one argument."
  fi
  ;;
esac


# We can't do much without tex.
#
if findprog ${TEX:-tex}; then :; else cat <<EOM
You don't have a working TeX binary (${TEX:-tex}) installed anywhere in
your PATH, and texi2dvi cannot proceed without one.  If you want to use
this script, you'll need to install TeX (if you don't have it) or change
your PATH or TEX environment variable (if you do).  See the --help
output for more details.

For information about obtaining TeX, please see http://www.tug.org.  If
you happen to be using Debian, you can get it with this command:
  apt-get install tetex-bin
EOM
  exit 1
fi


# We want to use etex (or pdftex) if they are available, and the user
# didn't explicitly specify.  We don't check for elatex and pdfelatex
# because (as of 2003), the LaTeX team has asked that new distributions
# use etex by default anyway.
#
# End up with the TEX and PDFTEX variables set to what we are going to use.
if test -z "$TEX"; then
  if findprog etex; then TEX=etex; else TEX=tex; fi
fi
#
if test -z "$PDFTEX"; then
  if findprog pdfetex; then PDFTEX=pdfetex; else PDFTEX=pdftex; fi
fi


# Prepare the temporary directory.  Remove it at exit, unless debugging.
if test -z "$debug"; then
  trap "cd / && rm -rf $tmpdir" 0 1 2 15
fi

# Create the temporary directory with strict rights
(umask 077 && mkdir $tmpdir) || exit 1

# Prepare the tools we might need.  This may be extra work in some
# cases, but improves the readability of the script.
utildir=$tmpdir/utils
mkdir $utildir || exit 1

# A sed script that preprocesses Texinfo sources in order to keep the
# iftex sections only.  We want to remove non TeX sections, and comment
# (with `@c texi2dvi') TeX sections so that makeinfo does not try to
# parse them.  Nevertheless, while commenting TeX sections, don't
# comment @macro/@end macro so that makeinfo does propagate them.
# Unfortunately makeinfo --iftex --no-ifinfo doesn't work well enough
# (yet), makeinfo can't parse the TeX commands, so work around with sed.
#
comment_iftex_sed=$utildir/comment.sed
cat <<EOF >$comment_iftex_sed
/^@tex/,/^@end tex/{
  s/^/@c texi2dvi/
}
/^@iftex/,/^@end iftex/{
  s/^/@c texi2dvi/
  /^@c texi2dvi@macro/,/^@c texi2dvi@end macro/{
    s/^@c texi2dvi//
  }
}
/^@ifnottex/,/^@end ifnottex/{
  s/^/@c (texi2dvi)/
}
/^@ifinfo/,/^@end ifinfo/{
  /^@node/p
  /^@menu/,/^@end menu/p
  t
  s/^/@c (texi2dvi)/
}
s/^@ifnotinfo/@c texi2dvi@ifnotinfo/
s/^@end ifnotinfo/@c texi2dvi@end ifnotinfo/
EOF
# Uncommenting is simple: Remove any leading `@c texi2dvi'.
uncomment_iftex_sed=$utildir/uncomment.sed
cat <<EOF >$uncomment_iftex_sed
s/^@c texi2dvi//
EOF

# Compute the list of xref files.
# Takes the filename (without extension) of which we look for xref
# files as argument.  The index files must be reported last.
get_xref_files ()
{
  # Get list of xref files (indexes, tables and lists).
  # Find all files having root filename with a two-letter extension,
  # saves the ones that are really Texinfo-related files.  .?o? catches
  # many files: .toc, .log, LaTeX tables and lists, FiXme's .lox, maybe more.
  for this_file in "$1".?o? "$1".aux "$1".?? "$1".idx; do
    # If file is empty, skip it.
    test -s "$this_file" || continue
    # If the file is not suitable to be an index or xref file, don't
    # process it.  It's suitable if the first character is a
    # backslash or right quote or at, as long as the first line isn't
    # \input texinfo.
    first_character=`sed -n '1s/^\(.\).*$/\1/p;q' $this_file`
    if (test "x$first_character" = "x\\" \
        && sed 1q $this_file | grep -v '^\\input *texinfo' >/dev/null) \
       || test "x$first_character" = "x'" \
       || test "x$first_character" = "x@"; then
      xref_files="$xref_files ./$this_file"
    fi
  done
  echo "$xref_files"
}

# Convert relative paths to absolute paths, so we can run in another
# directory (e.g., in --clean mode, or during the macro-support detection.)
#
# Empty path components are meaningful to tex.  We rewrite them
# as `EMPTY' so they don't get lost when we split on $path_sep.
# Hopefully no one will have an actual directory named EMPTY.
absolute_filenames ()
{
  replace_empty="-e 's/^$path_sep/EMPTY$path_sep/g' \
                 -e 's/$path_sep\$/${path_sep}EMPTY/g' \
                 -e 's/$path_sep$path_sep/${path_sep}EMPTY:/g'"
  _res=`echo "$1" | eval sed $replace_empty`
  save_IFS=$IFS
  IFS=$path_sep
  set x $_res; shift
  _res=.
  for dir
  do
    case $dir in
      EMPTY)
        _res=$_res$path_sep
        ;;
      [\\/]* | ?:[\\/]*)        # Absolute paths don't need to be expanded.
        _res=$_res$path_sep$dir
        ;;
      *)
        abs=`cd "$dir" && pwd` && _res=$_res$path_sep$abs
        ;;
    esac
  done
  echo "$_res"
}


# run_tex ()
# ----------
# Run TeX as "$tex $tex_args $filename_input", taking care of errors
# and logs.
run_tex ()
{
  # Note that this will be used via an eval: quote properly.
  cmd=$tex

  # If possible, make TeX report error locations in GNU format.
  tex_args=
  case $tex_help in
    *file-line-error*) cmd="$cmd --file-line-error";;
  esac

  # Tell TeX to be batch if requested.
  if $batch; then
    # \batchmode does not show terminal output at all, so we don't
    # want that.  And even in batch mode, TeX insists on having input
    # from the user.  Close its stdin to make it impossible.
    cmd="$cmd </dev/null '${escape}nonstopmode' '${escape}input'"
  fi

  cmd="$cmd '$filename_input'"

  verbose "Running $cmd ..."
  if eval "$cmd" >&5; then :; else
    test "$clean" = t \
        && cp "$filename_noext.log" "$orig_pwd"
    fatal 1 "$tex exited with bad status, quitting." \
            "see $filename_noext.log for errors."
  fi
}

# File descriptor usage:
# 0 standard input
# 1 standard output (--verbose messages)
# 2 standard error
# 3 some systems may open it to /dev/tty
# 4 used on the Kubota Titan
# 5 tools output (turned off by --quiet)
# 6 tracing/debugging (set -x output, etc.)

# Tools' output.  If quiet, discard, else redirect to the message flow.
if test "$quiet" = t; then
  exec 5>/dev/null
else
  exec 5>&1
fi

# Enable tracing
if test "$debug" = t; then
  exec 6>&1
  set -x
else
  exec 6>/dev/null
fi

# 
# TeXify files.

for command_line_filename in ${1+"$@"}; do
  verbose "Processing $command_line_filename ..."

  # If the COMMAND_LINE_FILENAME is not absolute (e.g., --debug.tex),
  # prepend `./' in order to avoid that the tools take it as an option.
  echo "$command_line_filename" | $EGREP '^(/|[A-z]:/)' >&6 \
  || command_line_filename="./$command_line_filename"

  # See if the file exists.  If it doesn't we're in trouble since, even
  # though the user may be able to reenter a valid filename at the tex
  # prompt (assuming they're attending the terminal), this script won't
  # be able to find the right xref files and so forth.
  if test ! -r "$command_line_filename"; then
    report "Could not read $command_line_filename, skipping."
    continue
  fi

  # Get the name of the current directory.  We want the full path
  # because in clean mode we are in tmp, in which case a relative
  # path has no meaning.
  filename_dir=`echo $command_line_filename | sed 's!/[^/]*$!!;s!^$!.!'`
  filename_dir=`cd "$filename_dir" >/dev/null && pwd`

  # Strip directory part but leave extension.
  filename_ext=`basename "$command_line_filename"`
  # Strip extension.
  filename_noext=`echo "$filename_ext" | sed 's/\.[^.]*$//'`
  ext=`echo "$filename_ext" | sed 's/^.*\.//'`

  # _src.  Use same basename since we want to generate aux files with
  # the same basename as the manual.  If --expand, then output the
  # macro-expanded file to here, else copy the original file.
  tmpdir_src=$tmpdir/src
  filename_src=$tmpdir_src/$filename_noext.$ext

  # _xtr.  The file with the user's extra commands.
  tmpdir_xtr=$tmpdir/xtr
  filename_xtr=$tmpdir_xtr/$filename_noext.$ext

  # _rcd.  The Texinfo file recoded in 7bit.
  tmpdir_rcd=$tmpdir/rcd
  filename_rcd=$tmpdir_rcd/$filename_noext.$ext

  # _bak.  Copies of the previous xref files (another round is run if
  # they differ from the new one).
  tmpdir_bak=$tmpdir/bak

  # Make all those directories and give up if we can't succeed.
  mkdir $tmpdir_src $tmpdir_xtr $tmpdir_rcd $tmpdir_bak || exit 1

  # Source file might include additional sources.
  # We want `.:$orig_pwd' before anything else.  (We'll add `.:' later
  # after all other directories have been turned into absolute paths.)
  # `.' goes first to ensure that any old .aux, .cps,
  # etc. files in ${directory} don't get used in preference to fresher
  # files in `.'.  Include orig_pwd in case we are in clean mode, where
  # we've cd'd to a temp directory.
  common="$orig_pwd$path_sep$filename_dir$path_sep$txincludes"
  for var in $tex_envvars; do
    eval ${var}="\$common\$${var}_orig"
    export $var
  done

  # Convert relative paths to absolute paths, so we can run in another
  # directory (e.g., in --clean mode, or during the macro-support detection.)
  TEXINPUTS=`absolute_filenames "$TEXINPUTS"`
  verbose "TEXINPUTS: $TEXINPUTS"
  BIBINPUTS=`absolute_filenames "$BIBINPUTS"`
  verbose "BIBINPUTS: $BIBINPUTS"
  BSTINPUTS=`absolute_filenames "$BSTINPUTS"`
  verbose "BSTINPUTS: $BSTINPUTS"
  INDEXSTYLE=`absolute_filenames "$INDEXSTYLE"`
  verbose "INDEXSTYLE: $INDEXSTYLE"

  # If the user explicitly specified the language, use that.
  # Otherwise, if the first line is \input texinfo, assume it's texinfo.
  # Otherwise, guess from the file extension.
  if test -n "$set_language"; then
    language=$set_language
  elif sed 1q "$command_line_filename" | grep 'input texinfo' >&6; then
    language=texinfo
  else
    language=
  fi

  # Get the type of the file (latex or texinfo) from the given language
  # we just guessed, or from the file extension if not set yet.
  case ${language:-$filename_ext} in
    [lL]a[tT]e[xX] | *.ltx | *.tex)
      # Assume a LaTeX file.  LaTeX needs bibtex and uses latex for
      # compilation.  No makeinfo.
      language=latex
      bibtex=${BIBTEX:-bibtex}
      makeinfo= # no point in running makeinfo on latex source.
      texindex=${MAKEINDEX:-makeindex}
      textra_cmd=1i
      if test $oformat = dvi; then
        tex=${LATEX:-latex}
      else
        tex=${PDFLATEX:-pdflatex}
      fi
      thumbpdf=${THUMBPDF:-thumbpdf}
      ;;

    *)
      # Assume a Texinfo file.  Texinfo files need makeinfo, texindex and tex.
      language=texinfo
      bibtex=
      texindex=${TEXINDEX:-texindex}
      textra_cmd='/^@setfilename/a'
      if test $oformat = dvi; then
        # MetaPost also uses the TEX environment variable.  If the user
        # has set TEX=latex for that reason, don't bomb out.
        if echo $TEX | grep 'latex$' >/dev/null; then
          tex=tex  # don't bother trying to find etex
        else
          tex=$TEX
        fi
      else
        tex=$PDFTEX
      fi
      # Unless required by the user, makeinfo expansion is wanted only
      # if texinfo.tex is too old.
      if test "$expand" = t; then
        makeinfo=${MAKEINFO:-makeinfo}
      else
        # Check if texinfo.tex performs macro expansion by looking for
        # its version.  The version is a date of the form YEAR-MO-DA.
        # We don't need to use [0-9] to match the digits since anyway
        # the comparison with $txiprereq, a number, will fail with non
        # digits.
        # Run in the tmpdir to avoid leaving files.
	(
           cd $tmpdir
	   echo '\input texinfo.tex @bye' >txiversion.tex
	   # Be sure that if tex wants to fail, it is not interactive:
	   # close stdin.
           $tex txiversion.tex </dev/null
	) >$tmpdir/txiversion.out 2>$tmpdir/txiversion.err
	if test $? != 0; then
	  cat $tmpdir/txiversion.out
	  cat $tmpdir/txiversion.err >&2
	  fatal 1 "texinfo.tex appears to be broken, quitting."
        fi
	eval `sed -n 's/^.*\[\(.*\)version \(....\)-\(..\)-\(..\).*$/txiformat=\1 txiversion="\2\3\4"/p' $tmpdir/txiversion.out`
        verbose "texinfo.tex preloaded as \`$txiformat', version is \`$txiversion' ..."
        if test "$txiprereq" -le "$txiversion" >/dev/null 2>&1; then
          makeinfo=
        else
          makeinfo=${MAKEINFO:-makeinfo}
        fi
        # As long as we had to run TeX, offer the user this convenience:
        test "$txiformat" = Texinfo && escape=@
      fi
      thumbpdf=${THUMBPDF:-thumbpdf}
      ;;
  esac

  # Go to $tmpdir to try --help, since old versions that don't accept
  # --help will generate a texput.log.
  tex_help=`cd $tmpdir >/dev/null && $tex --help </dev/null 2>&1`

  # Expand macro commands in the original source file using Makeinfo.
  # Always use `end' footnote style, since the `separate' style
  #   generates different output (arguably this is a bug in -E).
  # Discard main info output, the user asked to run TeX, not makeinfo.
  if test -n "$makeinfo"; then
    verbose "Macro-expanding $command_line_filename to $filename_src ..."
    sed -f $comment_iftex_sed "$command_line_filename" \
      | $makeinfo --footnote-style=end -I "$filename_dir" $miincludes \
        -o /dev/null --macro-expand=- \
      | sed -f $uncomment_iftex_sed >"$filename_src"
    filename_input=$filename_src
  fi

  # If makeinfo failed (or was not even run), use the original file as input.
  if test $? -ne 0 \
     || test ! -r "$filename_src"; then
    verbose "Reverting to $command_line_filename ..."
    filename_input=$filename_dir/$filename_ext
  fi

  # Used most commonly for @finalout, @smallbook, etc.
  if test -n "$textra"; then
    verbose "Inserting extra commands: $textra"
    sed "$textra_cmd\\
$textra" "$filename_input" >"$filename_xtr"
    filename_input=$filename_xtr
  fi

  # If this is a Texinfo file with a specified input encoding, and
  # recode is available, then recode to plain 7 bit Texinfo.
  if test $language = texinfo; then
    pgm='s/^ *@documentencoding  *\([^ ][^ ]*\) *$/\1/
	t found
	d
	:found
	q'
    encoding=`sed -e "$pgm" "$filename_input"`
    if $recode && test -n "$encoding" && findprog recode; then
      verbose "Recoding from $encoding to Texinfo."
      if recode "$encoding"..texinfo <"$filename_input" >"$filename_rcd" \
         && test -s "$filename_rcd"; then
        filename_input=$filename_rcd
      else
        verbose "Recoding failed, using original input."
      fi
    fi
  fi

  # If clean mode was specified, then move to the temporary directory.
  if test "$clean" = t; then
    verbose "cd $tmpdir_src"
    cd "$tmpdir_src" || exit 1
  fi

  while :; do # will break out of loop below
    orig_xref_files=`get_xref_files "$filename_noext"`

    # Save copies of originals for later comparison.
    if test -n "$orig_xref_files"; then
      verbose "Backing up xref files: `echo $orig_xref_files | sed 's|\./||g'`"
      cp $orig_xref_files $tmpdir_bak
    fi

    # Run bibtex on current file.
    # - If its input (AUX) exists.
    # - If AUX contains both `\bibdata' and `\bibstyle'.
    # - If some citations are missing (LOG contains `Citation').
    #   or the LOG complains of a missing .bbl
    #
    # We run bibtex first, because I can see reasons for the indexes
    # to change after bibtex is run, but I see no reason for the
    # converse.
    #
    # Don't try to be too smart.  Running bibtex only if the bbl file
    # exists and is older than the LaTeX file is wrong, since the
    # document might include files that have changed.  Because there
    # can be several AUX (if there are \include's), but a single LOG,
    # looking for missing citations in LOG is easier, though we take
    # the risk to match false messages.
    if test -n "$bibtex" \
       && test -r "$filename_noext.aux" \
       && test -r "$filename_noext.log" \
       && (grep '^\\bibdata[{]'  "$filename_noext.aux" \
           && grep '^\\bibstyle[{]' "$filename_noext.aux" \
           && (grep 'Warning:.*Citation.*undefined' "$filename_noext.log" \
               || grep 'No file .*\.bbl\.' "$filename_noext.log")) \
          >&6 2>&1; \
    then
      verbose "Running $bibtex $filename_noext ..."
      $bibtex "$filename_noext" >&5 ||
        fatal 1 "$bibtex exited with bad status, quitting."
    fi

    # What we'll run texindex on -- exclude non-index files.
    # Since we know index files are last, it is correct to remove everything
    # before .aux and .?o?.  But don't really do <anything>o<anything>
    # -- don't match whitespace as <anything>.
    # Otherwise, if orig_xref_files contains something like
    #   foo.xo foo.whatever
    # the space after the o will get matched.
    index_files=`echo "$orig_xref_files" \
                 | sed "s!.*\.aux!!g;
                        s!./$filename_noext\.[^ ]o[^ ]!!g;
                        s/^[ ]*//;s/[ ]*$//"`
    # Run texindex (or makeindex) on current index files.  If they
    # already exist, and after running TeX a first time the index
    # files don't change, then there's no reason to run TeX again.
    # But we won't know that if the index files are out of date or
    # nonexistent.
    if test -n "$texindex" && test -n "$index_files"; then
      verbose "Running $texindex $index_files ..."
      $texindex $index_files 2>&5 1>&2 ||
         fatal 1 "$texindex exited with bad status, quitting."
    fi

    # Finally, run TeX.
    run_tex

    # Decide if looping again is needed.
    finished=t

    # LaTeX (and the package changebar) report in the LOG file if it
    # should be rerun.  This is needed for files included from
    # subdirs, since texi2dvi does not try to compare xref files in
    # subdirs.  Performing xref files test is still good since LaTeX
    # does not report changes in xref files.
    if grep "Rerun to get" "$filename_noext.log" >&6 2>&1; then
      finished=
    fi

    # Check if xref files changed.
    new_xref_files=`get_xref_files "$filename_noext"`
    verbose "Original xref files = `echo $orig_xref_files | sed 's|\./||g'`"
    verbose "New xref files      = `echo $new_xref_files | sed 's|\./||g'`"

    # If old and new lists don't at least have the same file list,
    # then one file or another has definitely changed.
    test "x$orig_xref_files" != "x$new_xref_files" && finished=

    # File list is the same.  We must compare each file until we find
    # a difference.
    if test -n "$finished"; then
      for this_file in $new_xref_files; do
        verbose "Comparing xref file `echo $this_file | sed 's|\./||g'` ..."
        # cmp -s returns nonzero exit status if files differ.
        if cmp -s "$this_file" "$tmpdir_bak/$this_file"; then :; else
          # We only need to keep comparing until we find one that
          # differs, because we'll have to run texindex & tex again no
          # matter how many more there might be.
          finished=
          verbose "xref file `echo $this_file | sed 's|\./||g'` differed ..."
          test "$debug" = t && diff -c "$tmpdir_bak/$this_file" "$this_file"
          break
        fi
      done
    fi

    # If finished, exit the loop, else rerun the loop.
    test -n "$finished" && break
  done # while :;

  # If we were using thumbpdf and producing PDF, then run thumbpdf
  # and TeX one last time.
  if test $oformat = pdf \
     && test -r "$filename_noext.log" \
     && grep 'thumbpdf\.sty'  "$filename_noext.log" >&6 2>&1; \
  then
    verbose "Running $thumbpdf $filename_noext ..."
    $thumbpdf "$filename_noext" >&5 ||
      fatal 1 "$thumbpdf exited with bad status, quitting."
    run_tex
  fi


  # If we were in clean mode, compilation was in a tmp directory.
  # Copy the DVI (or PDF) file into the directory where the compilation
  # has been done.  (The temp dir is about to get removed anyway.)
  # We also return to the original directory so that
  # - the next file is processed in correct conditions
  # - the temporary file can be removed
  if test -n "$clean"; then
    if test -n "$oname"; then
       dest=$oname
    else
       dest=$orig_pwd
    fi
    verbose "Copying $oformat file from `pwd` to $dest"
    cp -p "./$filename_noext.$oformat" "$dest"
    cd / # in case $orig_pwd is on a different drive (for DOS)
    cd $orig_pwd || exit 1
  fi

  # Remove temporary files.
  if test "x$debug" = "x"; then
    verbose "Removing $tmpdir_src $tmpdir_xtr $tmpdir_bak ..."
    cd /
    rm -rf $tmpdir_src $tmpdir_xtr $tmpdir_bak
  fi
done

verbose "done."
exit 0 # exit successfully, not however we ended the loop.