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