home *** CD-ROM | disk | FTP | other *** search
/ Dream 52 / Amiga_Dream_52.iso / OS2 / gnuinfo.zip / util / texi2dvi < prev    next >
Text File  |  1997-07-14  |  13KB  |  332 lines

  1. #! /bin/sh
  2. # texi2dvi --- smartly produce DVI files from texinfo sources
  3.  
  4. # Copyright (C) 1992, 93, 94, 95, 96 Free Software Foundation, Inc.
  5.  
  6. # $Id: texi2dvi,v 0.6 1997/07/14 19:23:18 karl 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.6 $'
  51. version=`set - $rcs_revision; echo $2`
  52.  
  53. # To prevent hairy quoting and escaping later.
  54. bq='`'
  55. eq="'"
  56.  
  57. usage="Usage: $0 [OPTION]... FILE...
  58. Run a Texinfo document through TeX.
  59.  
  60. Options:
  61. -D, --debug          Turn on shell debugging ($bq${bq}set -x$eq$eq).
  62. -t, --texinfo CMD    Insert CMD after @setfilename before running TeX.
  63. --verbose            Report on what is done.
  64. -h, --help           Display this help and exit.
  65. -v, --version        Display version information and exit.
  66.  
  67. The values of the TEX, TEXINDEX, and MAKEINFO environment variables are
  68. used to run those commands, if they are set.
  69.  
  70. Email bug reports to bug-texinfo@prep.ai.mit.edu.
  71. "
  72.  
  73. # Initialize variables.
  74. # Don't use `unset' since old bourne shells don't have this command.
  75. # Instead, assign them an empty value.
  76. # Some of these, like TEX and TEXINDEX, may be inherited from the environment.
  77. backup_extension=.bak # these files get deleted if all goes well.
  78. debug=
  79. orig_pwd="`pwd`"
  80. textra=
  81. verbose=false
  82. makeinfo="${MAKEINFO-makeinfo}"
  83. texindex="${TEXINDEX-texindex}"
  84. tex="${TEX-tex}"
  85.  
  86. # Save this so we can construct a new TEXINPUTS path for each file.
  87. TEXINPUTS_orig="$TEXINPUTS"
  88. export TEXINPUTS
  89.  
  90. # Parse command line arguments.
  91. # Make sure that all wildcarded options are long enough to be unambiguous.
  92. # It's a good idea to document the full long option name in each case.
  93. # Long options which take arguments will need a `*' appended to the
  94. # canonical name to match the value appended after the `=' character.
  95. while : ; do
  96.   case $# in 0) break ;; esac
  97.   case "$1" in
  98.     -D | --debug | --d* ) debug=t; shift ;;
  99.     -h | --help | --h* )   echo "$usage"; exit 0 ;;
  100.     # OK, we should do real option parsing here, but be lazy for now.
  101.     -t | --texinfo | --t*) shift; textra="$textra $1"; shift ;; 
  102.     -v | --vers* )
  103.       echo "$progname (Texinfo 3.9) $version"
  104.       echo "Copyright (C) 1996 Free Software Foundation, Inc.
  105. There is NO warranty.  You may redistribute this software
  106. under the terms of the GNU General Public License.
  107. For more information about these matters, see the files named COPYING."
  108.       exit 0 ;;
  109.     --verb* )            verbose=echo; shift ;;
  110.     -- )     # Stop option processing
  111.       shift
  112.       break
  113.      ;;
  114.     -* )
  115.       case "$1" in
  116.         --*=* ) arg=`echo "$1" | sed -e 's/=.*//'` ;;
  117.         * )     arg="$1" ;;
  118.       esac
  119.       exec 1>&2
  120.       echo "$progname: Unknown or ambiguous option $bq$arg$eq."
  121.       echo "$progname: Try $bq--help$eq for more information."
  122.       exit 1
  123.      ;;
  124.     * )
  125.       break
  126.      ;;
  127.    esac
  128. done
  129.  
  130. # See if there are any command line args left (which will be interpreted as
  131. # filename arguments).
  132. if test $# -eq 0; then
  133.   exec 1>&2
  134.   echo "$progname: At least one file name is required as an argument."
  135.   echo "$progname: Try $bq--help$eq for more information."
  136.   exit 2
  137. fi
  138.  
  139. test "$debug" = t && set -x
  140.  
  141. # Texify files
  142. for command_line_filename in ${1+"$@"} ; do
  143.   $verbose "Processing $command_line_filename ..."
  144.  
  145.   # See if file exists.  If it doesn't we're in trouble since, even
  146.   # though the user may be able to reenter a valid filename at the tex
  147.   # prompt (assuming they're attending the terminal), this script won't
  148.   # be able to find the right index files and so forth.
  149.   if test ! -r "${command_line_filename}" ; then
  150.     echo "$0: Could not read ${command_line_filename}." >&2
  151.     continue
  152.   fi
  153.  
  154.   # Roughly equivalent to `dirname ...`, but more portable
  155.   directory="`echo ${command_line_filename} | sed 's/\/[^\/]*$//'`"
  156.   filename_texi="`basename ${command_line_filename}`"
  157.   # Strip off the last extension part (probably .texinfo or .texi)
  158.   filename_noext="`echo ${filename_texi} | sed 's/\.[^.]*$//'`"
  159.  
  160.   # Use same basename since we want to generate aux files with the same
  161.   # basename as the manual.  Use extension .texi for the temp file so
  162.   # that TeX will ignore it.  Thus, we must use a subdirectory.
  163.   #
  164.   # Output the macro-expanded file to here.
  165.   tmp_dir=${TMPDIR-/tmp}/$$
  166.   filename_tmp=$tmp_dir/$filename_noext.texi
  167.   # Output the file with the user's extra commands to here.
  168.   filename_tmp2=$tmp_dir.2/$filename_noext.texi
  169.   mkdir $tmp_dir $tmp_dir.2
  170.  
  171.   # If directory and file are the same, then it's probably because there's
  172.   # no pathname component.  Set dirname to `.', the current directory.
  173.   if test "z${directory}" = "z${command_line_filename}" ; then
  174.      directory=.
  175.   fi
  176.  
  177.   # Source file might @include additional texinfo sources.  Put `.' and
  178.   # directory where source file(s) reside in TEXINPUTS before anything
  179.   # else.  `.' goes first to ensure that any old .aux, .cps, etc. files in
  180.   # ${directory} don't get used in preference to fresher files in `.'.
  181.   TEXINPUTS=".:${directory}:${TEXINPUTS_orig}"
  182.  
  183.   # Expand macro commands in the original source file using Makeinfo;
  184.   #   the macro syntax bfox implemented is impossible to implement in TeX.
  185.   # Always use `end' footnote style, since the `separate' style
  186.   #   generates different output (arguably this is a bug in -E).
  187.   # Discard main info output, the user asked to run TeX, not makeinfo.
  188.   # Redirect output to /dev/null to throw away `Making info file...' msg.
  189.   $verbose "Macro-expanding $command_line_filename to $filename_tmp ..."
  190.   $makeinfo --footnote-style=end -E $filename_tmp -o /dev/null \
  191.     $command_line_filename >/dev/null
  192.  
  193.   # But if there were no macros, or makeinfo failed for some reason,
  194.   # just use the original file.  (It shouldn't make any difference, but
  195.   # let's be safe.)
  196.   if test $? -ne 0 || cmp -s $filename_tmp $command_line_filename; then
  197.     $verbose "Reverting to $command_line_filename ..."
  198.     filename_input=$command_line_filename
  199.   else
  200.     filename_input=$filename_tmp
  201.   fi
  202.  
  203.   # Used most commonly for @finalout, @smallbook, etc.
  204.   if test -n "$textra"; then
  205.     $verbose "Inserting extra commands: $textra."
  206.     sed '/^@setfilename/a\
  207. '"$textra" $filename_input >$filename_tmp2
  208.    filename_input=$filename_tmp2
  209.   fi
  210.  
  211.   while true; do # will break out of loop below
  212.     # "Unset" variables that might have values from previous iterations and
  213.     # which won't be completely reset later.
  214.     definite_index_files=
  215.  
  216.     # Find all files having root filename with a two-letter extension,
  217.     # determine whether they're really index files, and save them.  Foo.aux
  218.     # is actually the cross-references file, but we need to keep track of
  219.     # that too.
  220.     possible_index_files="`eval echo ${filename_noext}.?? ${filename_noext}.aux`"
  221.     for this_file in ${possible_index_files} ; do
  222.        # If file is empty, forget it.
  223.        test -s "${this_file}" || continue
  224.  
  225.        # Examine first character of file.  If it's not suitable to be an
  226.        # index or xref file, don't process it.
  227.        first_character="`sed -n '1s/^\(.\).*$/\1/p;q' ${this_file}`"
  228.        if test "x${first_character}" = "x\\" \
  229.           || test "x${first_character}" = "x'"; then
  230.          definite_index_files="${definite_index_files} ${this_file}"
  231.        fi
  232.     done
  233.     orig_index_files="${definite_index_files}"
  234.     orig_index_files_sans_aux="`echo ${definite_index_files} \
  235.                                  | sed 's/'${filename_noext}'\.aux//;
  236.                                         s/^[ ]*//;s/[ ]*$//;'`"
  237.  
  238.     # Now save copies of original index files so we have some means of
  239.     # comparison later.
  240.     $verbose "Backing up current index files: $orig_index_files ..."  
  241.     for index_file_to_save in ${orig_index_files} ; do
  242.       cp "${index_file_to_save}" "${index_file_to_save}${backup_extension}"
  243.     done
  244.  
  245.     # Run texindex on current index files.  If they already exist, and
  246.     # after running TeX a first time the index files don't change, then
  247.     # there's no reason to run TeX again.  But we won't know that if the
  248.     # index files are out of date or nonexistent.
  249.     if test -n "${orig_index_files_sans_aux}" ; then
  250.        $verbose "Running $texindex $orig_index_files_sans_aux ..."
  251.        ${texindex} ${orig_index_files_sans_aux}
  252.     fi
  253.  
  254.     # Finally, run TeX.
  255.     $verbose "Running $tex $filename_input ..."
  256.     ${tex} "$filename_input"
  257.  
  258.     # Check if index files changed.
  259.     # 
  260.     definite_index_files=
  261.     # Get list of new index files.
  262.     possible_index_files="`eval echo ${filename_noext}.?? ${filename_noext}.aux`"
  263.     for this_file in ${possible_index_files} ; do
  264.        # If file is empty, forget it.
  265.        test -s "${this_file}" || continue
  266.  
  267.        # Examine first character of file.  If it's not a backslash or
  268.        # single quote, then it's definitely not an index or xref file.
  269.        # (Will have to check for @ when we switch to Texinfo syntax in
  270.        # all these files...)
  271.        first_character="`sed -n '1s/^\(.\).*$/\1/p;q' ${this_file}`"
  272.        if test "x${first_character}" = "x\\" \
  273.           || test "x${first_character}" = "x'"; then
  274.          definite_index_files="${definite_index_files} ${this_file}"
  275.        fi
  276.     done
  277.     new_index_files="${definite_index_files}"
  278.     new_index_files_sans_aux="`echo ${definite_index_files} \
  279.                                | sed 's/'${filename_noext}'\.aux//;
  280.                                       s/^[ ]*//;s/[ ]*$//;'`"
  281.  
  282.     # If old and new list don't at least have the same file list, then one
  283.     # file or another has definitely changed.
  284.     $verbose "Original index files =$orig_index_files"
  285.     $verbose "New index files =$new_index_files"
  286.     if test "z${orig_index_files}" != "z${new_index_files}" ; then
  287.       index_files_changed_p=t
  288.     else
  289.       # File list is the same.  We must compare each file until we find a
  290.       # difference.
  291.       index_files_changed_p=
  292.       for this_file in ${new_index_files} ; do
  293.         $verbose "Comparing index file $this_file ..."
  294.         # cmp -s will return nonzero exit status if files differ.
  295.         cmp -s "${this_file}" "${this_file}${backup_extension}"
  296.         if test $? -ne 0 ; then
  297.           # We only need to keep comparing until we find *one* that
  298.           # differs, because we'll have to run texindex & tex no
  299.           # matter what.
  300.           index_files_changed_p=t
  301.           $verbose "Index file $this_file differed:"
  302.           test $verbose = echo \
  303.           && diff -c "${this_file}${backup_extension}" "${this_file}"
  304.           break
  305.         fi
  306.       done
  307.     fi
  308.  
  309.     # If index files have changed since TeX has been run, or if the aux
  310.     # file wasn't present originally, run texindex and TeX again.
  311.     if test "${index_files_changed_p}" ; then :; else
  312.       # Nothing changed.  We're done with TeX.
  313.       break
  314.     fi
  315.   done
  316.  
  317.   # Generate list of files to delete, then call rm once with the entire
  318.   # list.  This is significantly faster than multiple executions of rm.
  319.   file_list=
  320.   for file in ${orig_index_files} ; do
  321.     file_list="${file_list} ${file}${backup_extension}"
  322.   done
  323.   if test -n "${file_list}" ; then
  324.     $verbose "Removing $file_list $tmp_dir $tmp_dir.2 ..."
  325.     rm -f ${file_list}
  326.     rm -rf $tmp_dir $tmp_dir.2
  327.   fi
  328. done
  329.  
  330. $verbose "$0 done."
  331. true # exit successfully.
  332.