8fb2f908771cf376acf9c892171bd70c6b507a8c
[platform/upstream/bash.git] / support / texi2dvi
1 #! /bin/sh
2 # texi2dvi --- smartly produce DVI files from texinfo sources
3
4 # Copyright (C) 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
5
6 # $Id: texi2dvi,v 0.5 1995/06/20 02:21:36 friedman Exp $
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 # Commentary:
24
25 # Author: Noah Friedman <friedman@prep.ai.mit.edu>
26
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.
30
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.
36 #
37 # Among the more interesting lossages I noticed with some bourne shells
38 # are:
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.
43
44 # Code:
45
46 # Name by which this script was invoked.
47 progname=`echo "$0" | sed -e 's/[^\/]*\///g'`
48
49 # This string is expanded by rcs automatically when this file is checked out.
50 rcs_revision='$Revision: 0.5 $'
51 version=`set - $rcs_revision; echo $2`
52
53 # To prevent hairy quoting and escaping later.
54 bq='`'
55 eq="'"
56
57 usage="Usage: $progname {options} [file1] {file2 {...}}
58 (version $version)
59
60 Options are:
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.
64
65 Arguments in brackets are required.  Those in braces are optional.
66 "
67
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
72 backup_extension=.bak
73 debug=
74 orig_pwd="`pwd`"
75 verbose=
76 texindex="${TEXINDEX-texindex}"
77 tex="${TEX-tex}"
78
79 # Save this so we can construct a new TEXINPUTS path for each file to be
80 # processed.
81 TEXINPUTS_orig="$TEXINPUTS"
82 export TEXINPUTS
83
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.
89 while : ; do
90   case $# in 0) break ;; esac
91   case "$1" in
92     -D | --debug | --d* )
93       debug=t
94       shift
95      ;;
96     -h | --help | --h* )
97       echo "$usage" 1>&2
98       exit 0
99      ;;
100     -v | --version | --v* )
101       echo "texi2dvi version $version" 1>&2
102       exit 0
103      ;;
104     -- )     # Stop option processing
105       shift
106       break
107      ;;
108     -* )
109       case "$1" in
110         --*=* ) arg=`echo "$1" | sed -e 's/=.*//'` ;;
111         * )     arg="$1" ;;
112       esac
113       exec 1>&2
114       echo "$progname: unknown or ambiguous option $bq$arg$eq"
115       echo "$progname: Use $bq--help$eq for a list of options."
116       exit 1
117      ;;
118     * )
119       break
120      ;;
121    esac
122 done
123
124 # See if there are any command line args left (which will be interpreted as
125 # filename arguments)
126 case $# in
127   0 )
128     exec 1>&2
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."
131     exit 2
132    ;;
133 esac
134
135 case "$debug" in t ) set -x ;; esac
136
137 # Texify files
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/\.[^.]*$//'`"
144
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
148       directory="."
149    fi
150
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}"
156
157    # "Unset" variables that might have values from previous iterations and
158    # which won't be completely reset later.
159    definite_index_files=""
160
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
167       continue;
168    fi
169
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
173    # that too.
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
178          continue;
179       fi
180
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}"
186       fi
187    done
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/[ ]*$//;'`"
192
193    # Now save copies of original index files so we have some means of
194    # comparison later.
195    for index_file_to_save in ${orig_index_files} ; do
196        cp "${index_file_to_save}" "${index_file_to_save}${backup_extension}"
197    done
198
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}
205    fi
206
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
214             continue;
215          fi
216
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}"
222          fi
223       done
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/[ ]*$//;'`"
228
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
233       else
234          # File list is the same.  We must compare each file until we find a
235          # difference.
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
243                # matter what.
244                index_files_changed_p=t
245                break
246             fi
247          done
248       fi
249
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
253          retval=0
254          if test "${new_index_files_sans_aux}" ; then
255             ${texindex} ${new_index_files_sans_aux}
256             retval=$?
257          fi
258          if test ${retval} -eq 0 ; then
259             ${tex} "${command_line_filename}"
260          fi
261       fi
262    fi
263
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.
266    file_list=""
267    for file in ${orig_index_files} ; do
268        file_list="${file_list} ${file}${backup_extension}"
269    done
270    if test "${file_list}" ; then
271       rm -f ${file_list}
272    fi
273 done
274
275 # texi2dvi ends here