Imported from ../bash-1.14.7.tar.gz.
[platform/upstream/bash.git] / support / texi2dvi
1 #!/bin/sh
2 # texi2dvi -- smartly produce DVI files from texinfo sources
3 #
4 # Copyright (C) 1992, 1993 Free Software Foundation.
5 #
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)
9 # any later version.
10 #
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.
15 #
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:
19 #
20 #              Free Software Foundation, Inc.
21 #              675 Mass Ave.
22 #              Cambridge, MA 02139, USA. 
23 #
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. 
27 #
28 # Version 0.4
29 # Last modified 26-Mar-93
30 #
31
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. 
36 #
37 # Among the more interesting lossages I noticed with some bourne shells
38 # are:
39 #       1) Some don't have an `unset' builtin 
40 #       2) In some implementations the `shift' builtin can't take a
41 #          numerical argument. 
42
43 progname=`basename $0`
44
45 usage="Usage: ${progname} {-D} {-h} [file1] {file2} {...}
46        {--debug} {--help}
47
48    Options in braces are optional.  Those in brackets are required. 
49 "
50
51 if test $# -eq 0 ; then
52    echo "${usage}" 1>&2;
53    exit 1
54 fi
55
56 backup_extension=".bak"
57 texindex="texindex"
58 tex="tex"
59 bq="\`"  # To prevent hairy quoting and escaping later.
60 eq="'"
61 orig_pwd="`pwd`"
62
63 if test "z${TEXINDEX}" != "z" ; then
64    texindex="${TEXINDEX}"
65 fi
66
67 if test "z${TEX}" != "z" ; then
68    tex="${TEX}"
69 fi
70
71 # Save this so we can construct a new TEXINPUTS path for each file to be
72 # processed.
73 TEXINPUTS_orig="${TEXINPUTS}"
74 export TEXINPUTS
75
76 # Parse command line options
77
78 # "unset" option variables to make sure they weren't accidentally
79 # exported
80 debug=""
81
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
87    case z$1 in
88       z-D | z--debug | z--d* )
89          debug="t"
90          shift
91         ;;
92       z-h | z--help | z--h* )
93          echo "${usage}" 1>&2
94          exit 1
95         ;;
96       z-- )
97          shift
98          break 
99         ;;
100       z-* )
101          echo "${progname}: ${bq}${1}${eq} is not a valid option." 1>&2
102          echo "" 1>&2
103          echo "${usage}" 1>&2
104          exit 1
105         ;;
106       * )
107          break 
108         ;;
109    esac
110 done
111
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
116    echo "" 1>&2
117    echo "${usage}" 1>&2
118    exit 1
119 fi
120
121 test "z${debug}" = "zt" && set -x
122
123 # Texify files
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/\.[^.]*$//'`"
130
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
134       directory="."
135    fi
136
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}"
142
143    # "Unset" variables that might have values from previous iterations and
144    # which won't be completely reset later.
145    definite_index_files=""
146
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
153       continue; 
154    fi
155
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
159    # that too.
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
164          continue;
165       fi
166
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}"
172       fi
173    done
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/[ ]*$//;'`"
178
179    # Now save copies of original index files so we have some means of
180    # comparison later. 
181    for index_file_to_save in ${orig_index_files} ; do
182        cp "${index_file_to_save}" "${index_file_to_save}${backup_extension}"
183    done
184
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}
191    fi
192
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
200             continue;
201          fi
202
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}"
208          fi
209       done
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/[ ]*$//;'`"
214
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
219       else
220          # File list is the same.  We must compare each file until we find a
221          # difference.  
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
229                # matter what. 
230                index_files_changed_p=t
231                break
232             fi
233          done
234       fi
235
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
239          retval=0
240          if test "${new_index_files_sans_aux}" ; then
241             ${texindex} ${new_index_files_sans_aux} 
242             retval=$?
243          fi
244          if test ${retval} -eq 0 ; then
245             ${tex} "${command_line_filename}"
246          fi
247       fi
248    fi
249
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. 
252    file_list=""
253    for file in ${orig_index_files} ; do
254        file_list="${file_list} ${file}${backup_extension}"
255    done
256    if test "${file_list}" ; then
257       rm -f ${file_list}
258    fi
259 done
260
261 #
262 # eof
263 #