2 # texi2dvi -- smartly produce DVI files from texinfo sources
4 # Copyright (C) 1992, 1993 Free Software Foundation.
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2, or (at your option)
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, you can either send email to this
18 # program's author (see below) or write to:
20 # Free Software Foundation, Inc.
22 # Cambridge, MA 02139, USA.
24 # Please send bug reports, etc. to bug-texinfo@prep.ai.mit.edu
25 # If possible, please send a copy of the output of the script called with
26 # the `--debug' option when making a bug report.
29 # Last modified 26-Mar-93
32 # Please note that in the interest of general portability, some common
33 # bourne shell constructs were avoided because they weren't guaranteed to
34 # be available in some earlier implementations. I've tried to make this as
35 # portable as possible.
37 # Among the more interesting lossages I noticed with some bourne shells
39 # 1) Some don't have an `unset' builtin
40 # 2) In some implementations the `shift' builtin can't take a
43 progname=`basename $0`
45 usage="Usage: ${progname} {-D} {-h} [file1] {file2} {...}
48 Options in braces are optional. Those in brackets are required.
51 if test $# -eq 0 ; then
56 backup_extension=".bak"
59 bq="\`" # To prevent hairy quoting and escaping later.
63 if test "z${TEXINDEX}" != "z" ; then
64 texindex="${TEXINDEX}"
67 if test "z${TEX}" != "z" ; then
71 # Save this so we can construct a new TEXINPUTS path for each file to be
73 TEXINPUTS_orig="${TEXINPUTS}"
76 # Parse command line options
78 # "unset" option variables to make sure they weren't accidentally
82 # If you add new commands be sure to change the wildcards below to make
83 # sure they are unambiguous (i.e. only match one possible long option)
84 # Be sure to show at least one instance of the full long option name to
85 # document what the long option is canonically called.
86 while test $# -gt 0 ; do
88 z-D | z--debug | z--d* )
92 z-h | z--help | z--h* )
101 echo "${progname}: ${bq}${1}${eq} is not a valid option." 1>&2
112 # See if there are any command line args left (which will be interpreted as
113 # filename arguments)
114 if test $# -eq 0 ; then
115 echo "${progname}: at least one file name is required as an argument." 1>&2
121 test "z${debug}" = "zt" && set -x
124 for command_line_filename in ${1+"$@"} ; do
125 # Roughly equivalent to `dirname ...`, but more portable
126 directory="`echo ${command_line_filename} | sed 's/\/[^\/]*$//'`"
127 filename_texi="`basename ${command_line_filename}`"
128 # Strip off the last extension part (probably .texinfo or .texi)
129 filename_noext="`echo ${filename_texi} | sed 's/\.[^.]*$//'`"
131 # If directory and file are the same, then it's probably because there's
132 # no pathname component. Set dirname to `.', the current directory.
133 if test "z${directory}" = "z${command_line_filename}" ; then
137 # Source file might @include additional texinfo sources. Put `.' and
138 # directory where source file(s) reside in TEXINPUTS before anything
139 # else. `.' goes first to ensure that any old .aux, .cps, etc. files in
140 # ${directory} don't get used in preference to fresher files in `.'.
141 TEXINPUTS=".:${directory}:${TEXINPUTS_orig}"
143 # "Unset" variables that might have values from previous iterations and
144 # which won't be completely reset later.
145 definite_index_files=""
147 # See if file exists here. If it doesn't we're in trouble since, even
148 # though the user may be able to reenter a valid filename at the tex
149 # prompt (assuming they're attending the terminal), this script won't be
150 # able to find the right index files and so forth.
151 if test ! -r "${command_line_filename}" ; then
152 echo "${progname}: ${command_line_filename}: No such file or permission denied." 1>&2
156 # Find all files having root filename with a two-letter extension,
157 # determine whether they're really index files, and save them. Foo.aux
158 # is actually the cross-references file, but we need to keep track of
160 possible_index_files="`eval echo ${filename_noext}.?? ${filename_noext}.aux`"
161 for this_file in ${possible_index_files} ; do
162 # If file is empty, forget it.
163 if test ! -s "${this_file}" ; then
167 # Examine first character of file. If it's not a backslash or
168 # single quote, then it's definitely not an index or xref file.
169 first_character="`sed -n '1s/^\(.\).*$/\1/p;q' ${this_file}`"
170 if test "${first_character}" = "\\" -o "${first_character}" = "'" ; then
171 definite_index_files="${definite_index_files} ${this_file}"
174 orig_index_files="${definite_index_files}"
175 orig_index_files_sans_aux="`echo ${definite_index_files} \
176 | sed 's/'${filename_noext}'\.aux//;
177 s/^[ ]*//;s/[ ]*$//;'`"
179 # Now save copies of original index files so we have some means of
181 for index_file_to_save in ${orig_index_files} ; do
182 cp "${index_file_to_save}" "${index_file_to_save}${backup_extension}"
185 # Run texindex on current index files. If they already exist, and
186 # after running TeX a first time the index files don't change, then
187 # there's no reason to run TeX again. But we won't know that if the
188 # index files are out of date or nonexistent.
189 if test "${orig_index_files_sans_aux}" ; then
190 ${texindex} ${orig_index_files_sans_aux}
193 if ${tex} ${command_line_filename} ; then # TeX run first time
194 definite_index_files=""
195 # Get list of new index files
196 possible_index_files="`eval echo ${filename_noext}.?? ${filename_noext}.aux`"
197 for this_file in ${possible_index_files} ; do
198 # If file is empty, forget it.
199 if test ! -s ${this_file} ; then
203 # Examine first character of file. If it's not a backslash or
204 # single quote, then it's definitely not an index or xref file.
205 first_character="`sed -n '1s/^\(.\).*$/\1/p;q' ${this_file}`"
206 if test "${first_character}" = "\\" -o "${first_character}" = "'" ; then
207 definite_index_files="${definite_index_files} ${this_file}"
210 new_index_files="${definite_index_files}"
211 new_index_files_sans_aux="`echo ${definite_index_files} \
212 | sed 's/'${filename_noext}'\.aux//;
213 s/^[ ]*//;s/[ ]*$//;'`"
215 # If old and new list don't at least have the same file list, then one
216 # file or another has definitely changed.
217 if test "${orig_index_files}" != "${new_index_files}" ; then
218 index_files_changed_p=t
220 # File list is the same. We must compare each file until we find a
222 index_files_changed_p=""
223 for this_file in ${new_index_files} ; do
224 # cmp -s will return nonzero exit status if files differ.
225 cmp -s "${this_file}" "${this_file}${backup_extension}"
226 if test $? -ne 0 ; then
227 # We only need to keep comparing until we find *one* that
228 # differs, because we'll have to run texindex & tex no
230 index_files_changed_p=t
236 # If index files have changed since TeX has been run, or if the aux
237 # file wasn't present originally, run texindex and TeX again.
238 if test "${index_files_changed_p}" ; then
240 if test "${new_index_files_sans_aux}" ; then
241 ${texindex} ${new_index_files_sans_aux}
244 if test ${retval} -eq 0 ; then
245 ${tex} "${command_line_filename}"
250 # Generate list of files to delete, then call rm once with the entire
251 # list. This is significantly faster than multiple executions of rm.
253 for file in ${orig_index_files} ; do
254 file_list="${file_list} ${file}${backup_extension}"
256 if test "${file_list}" ; then