2 # texi2dvi --- smartly produce DVI files from texinfo sources
4 # Copyright (C) 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2, or (at your option)
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, 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.
25 # Author: Noah Friedman <friedman@prep.ai.mit.edu>
27 # Please send bug reports, etc. to bug-texinfo@prep.ai.mit.edu
28 # If possible, please send a copy of the output of the script called with
29 # the `--debug' option when making a bug report.
31 # In the interest of general portability, some common bourne shell
32 # constructs were avoided because they weren't guaranteed to be available
33 # in some earlier implementations. I've tried to make this program as
34 # portable as possible. Welcome to unix, where the lowest common
35 # denominator is rapidly diminishing.
37 # Among the more interesting lossages I noticed with some bourne shells
39 # * No shell functions.
40 # * No `unset' builtin.
41 # * `shift' cannot take a numeric argument, and signals an error if
42 # there are no arguments to shift.
46 # Name by which this script was invoked.
47 progname
=`echo "$0" | sed -e 's/[^\/]*\///g'`
49 # This string is expanded by rcs automatically when this file is checked out.
50 rcs_revision
='$Revision$'
51 version
=`set - $rcs_revision; echo $2`
53 # To prevent hairy quoting and escaping later.
57 usage
="Usage: $progname {options} [file1] {file2 {...}}
61 -D, --debug Turn on shell debugging ($bq${bq}set -x$eq$eq).
62 -h, --help You're looking at it.
63 -v, --version Print version number.
65 Arguments in brackets are required. Those in braces are optional.
68 # Initialize variables.
69 # Don't use `unset' since old bourne shells don't have this command.
70 # Instead, assign them an empty value.
71 # Some of these, like TEX and TEXINDEX, may be inherited from the environment
76 texindex
="${TEXINDEX-texindex}"
79 # Save this so we can construct a new TEXINPUTS path for each file to be
81 TEXINPUTS_orig
="$TEXINPUTS"
84 # Parse command line arguments.
85 # Make sure that all wildcarded options are long enough to be unambiguous.
86 # It's a good idea to document the full long option name in each case.
87 # Long options which take arguments will need a `*' appended to the
88 # canonical name to match the value appended after the `=' character.
90 case $# in 0) break ;; esac
100 -v |
--version |
--v* )
101 echo "texi2dvi version $version" 1>&2
104 -- ) # Stop option processing
110 --*=* ) arg
=`echo "$1" | sed -e 's/=.*//'` ;;
114 echo "$progname: unknown or ambiguous option $bq$arg$eq"
115 echo "$progname: Use $bq--help$eq for a list of options."
124 # See if there are any command line args left (which will be interpreted as
125 # filename arguments)
129 echo "$progname: at least one file name is required as an argument."
130 echo "$progname: Use $bq--help$eq for a description of command syntax."
135 case "$debug" in t
) set -x ;; esac
138 for command_line_filename
in ${1+"$@"} ; do
139 # Roughly equivalent to `dirname ...`, but more portable
140 directory
="`echo ${command_line_filename} | sed 's/\/[^\/]*$//'`"
141 filename_texi
="`basename ${command_line_filename}`"
142 # Strip off the last extension part (probably .texinfo or .texi)
143 filename_noext
="`echo ${filename_texi} | sed 's/\.[^.]*$//'`"
145 # If directory and file are the same, then it's probably because there's
146 # no pathname component. Set dirname to `.', the current directory.
147 if test "z${directory}" = "z${command_line_filename}" ; then
151 # Source file might @include additional texinfo sources. Put `.' and
152 # directory where source file(s) reside in TEXINPUTS before anything
153 # else. `.' goes first to ensure that any old .aux, .cps, etc. files in
154 # ${directory} don't get used in preference to fresher files in `.'.
155 TEXINPUTS
=".:${directory}:${TEXINPUTS_orig}"
157 # "Unset" variables that might have values from previous iterations and
158 # which won't be completely reset later.
159 definite_index_files
=""
161 # See if file exists here. If it doesn't we're in trouble since, even
162 # though the user may be able to reenter a valid filename at the tex
163 # prompt (assuming they're attending the terminal), this script won't be
164 # able to find the right index files and so forth.
165 if test ! -r "${command_line_filename}" ; then
166 echo "${progname}: ${command_line_filename}: No such file or permission denied." 1>&2
170 # Find all files having root filename with a two-letter extension,
171 # determine whether they're really index files, and save them. Foo.aux
172 # is actually the cross-references file, but we need to keep track of
174 possible_index_files
="`eval echo ${filename_noext}.?? ${filename_noext}.aux`"
175 for this_file
in ${possible_index_files} ; do
176 # If file is empty, forget it.
177 if test ! -s "${this_file}" ; then
181 # Examine first character of file. If it's not a backslash or
182 # single quote, then it's definitely not an index or xref file.
183 first_character
="`sed -n '1s/^\(.\).*$/\1/p;q' ${this_file}`"
184 if test "${first_character}" = "\\" -o "${first_character}" = "'" ; then
185 definite_index_files
="${definite_index_files} ${this_file}"
188 orig_index_files
="${definite_index_files}"
189 orig_index_files_sans_aux
="`echo ${definite_index_files} \
190 | sed 's/'${filename_noext}'\.aux//;
191 s/^[ ]*//;s/[ ]*$//;'`"
193 # Now save copies of original index files so we have some means of
195 for index_file_to_save
in ${orig_index_files} ; do
196 cp "${index_file_to_save}" "${index_file_to_save}${backup_extension}"
199 # Run texindex on current index files. If they already exist, and
200 # after running TeX a first time the index files don't change, then
201 # there's no reason to run TeX again. But we won't know that if the
202 # index files are out of date or nonexistent.
203 if test "${orig_index_files_sans_aux}" ; then
204 ${texindex} ${orig_index_files_sans_aux}
207 if ${tex} ${command_line_filename} ; then # TeX run first time
208 definite_index_files
=""
209 # Get list of new index files
210 possible_index_files
="`eval echo ${filename_noext}.?? ${filename_noext}.aux`"
211 for this_file
in ${possible_index_files} ; do
212 # If file is empty, forget it.
213 if test ! -s ${this_file} ; then
217 # Examine first character of file. If it's not a backslash or
218 # single quote, then it's definitely not an index or xref file.
219 first_character
="`sed -n '1s/^\(.\).*$/\1/p;q' ${this_file}`"
220 if test "${first_character}" = "\\" -o "${first_character}" = "'" ; then
221 definite_index_files
="${definite_index_files} ${this_file}"
224 new_index_files
="${definite_index_files}"
225 new_index_files_sans_aux
="`echo ${definite_index_files} \
226 | sed 's/'${filename_noext}'\.aux//;
227 s/^[ ]*//;s/[ ]*$//;'`"
229 # If old and new list don't at least have the same file list, then one
230 # file or another has definitely changed.
231 if test "${orig_index_files}" != "${new_index_files}" ; then
232 index_files_changed_p
=t
234 # File list is the same. We must compare each file until we find a
236 index_files_changed_p
=""
237 for this_file
in ${new_index_files} ; do
238 # cmp -s will return nonzero exit status if files differ.
239 cmp -s "${this_file}" "${this_file}${backup_extension}"
240 if test $?
-ne 0 ; then
241 # We only need to keep comparing until we find *one* that
242 # differs, because we'll have to run texindex & tex no
244 index_files_changed_p
=t
250 # If index files have changed since TeX has been run, or if the aux
251 # file wasn't present originally, run texindex and TeX again.
252 if test "${index_files_changed_p}" ; then
254 if test "${new_index_files_sans_aux}" ; then
255 ${texindex} ${new_index_files_sans_aux}
258 if test ${retval} -eq 0 ; then
259 ${tex} "${command_line_filename}"
264 # Generate list of files to delete, then call rm once with the entire
265 # list. This is significantly faster than multiple executions of rm.
267 for file in ${orig_index_files} ; do
268 file_list
="${file_list} ${file}${backup_extension}"
270 if test "${file_list}" ; then