home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / texlatex.zip / web2c72.zip / bin / makempx < prev    next >
Text File  |  1999-09-14  |  5KB  |  170 lines

  1. #!sh
  2. # '$Id: makempx.in,v 1.7 1997/12/01 19:21:12 olaf Exp $'
  3. # Make an MPX file from the labels in a MetaPost source file,
  4. # using mpto and either dvitomp (TeX) or dmp (troff).
  5. # From John Hobby's original (though there's not much of it left by now).
  6. # Public domain.
  7.  
  8. rcs_revision='$Revision: 1.7 $'
  9. version=`set - $rcs_revision; echo $2`
  10.  
  11. : ${DMP=dmp}
  12. : ${DVITOMP=dvitomp}
  13. : ${MAKEMPX_BINDIR=/usr/local/bin}
  14. : ${MPTEXPRE=mptexpre.tex}
  15. : ${MPTOTEX="mpto -tex"}
  16. : ${MPTOTR="mpto -troff"}
  17. : ${NEWER=newer}
  18. : ${TEX=tex}
  19. : ${TROFF='eqn -d\$\$ | troff -Tpost'}
  20.  
  21. PATH=$MAKEMPX_BINDIR:/bin:/usr/bin:$PATH
  22.  
  23. # These names are documented in the MetaPost manual, so it's
  24. # unwise to change them.
  25. ERRLOG=mpxerr.log        # file for an error log if necessary
  26. TEXERR=mpxerr.tex        # file for erroneous TeX if any
  27. DVIERR=mpxerr.dvi        # troublesome dvi file if any
  28. TROFF_INERR=mpxerr        # file for erroneous troff input, if any
  29. TROFF_OUTERR=mpxerr.t        # file for troublesome troff output, if any
  30.  
  31.  
  32. usage="Usage: $0 [-tex|-troff] MPFILE MPXFILE.
  33.   If MPXFILE is older than MPFILE, translate the labels from the MetaPost
  34.   input file MPFIle to low-level commands in MPXFILE, by running
  35.     $MPTOTEX, $TEX, and $DVITOMP
  36.   by default; or, if -troff is specified,
  37.     $MPTOTR, $TROFF, and $DMP.
  38.  
  39.   The current directory is used for writing temporary files.  Errors are
  40.   left in mpxerr.{tex,log,dvi}.
  41.  
  42.   If the file named in \$MPTEXPRE (mptexpre.tex by default) exists, it is
  43.   prepended to the output in tex mode.
  44.  
  45. Email bug reports to tex-k@mail.tug.org."
  46.  
  47.  
  48. mode=tex
  49.  
  50. while test $# -gt 0; do
  51.   case "$1" in
  52.     -help|--help) 
  53.       echo "$usage"; exit 0;;
  54.     -version|--version)
  55.       echo "`basename $0` (Web2c 7.2) $version"
  56.       echo "There is NO warranty.  This script is public domain.
  57. Primary author: John Hobby."
  58.       exit 0;;
  59.     -troff|--troff) mode=troff;;
  60.     -tex|--tex) mode=tex;;
  61.     -*) 
  62.       echo "$0: Invalid option: $1." >&2
  63.       echo "Try \``basename $0` --help' for more information." >&2 
  64.       exit 1;;
  65.     *) 
  66.       if test -z "$MPFILE"; then
  67.         MPFILE=$1                   # input file
  68.       elif test -z "$MPXFILE"; then
  69.         MPXFILE=$1                  # output file
  70.       else
  71.         echo "$0: Extra argument $1." >&2 
  72.         echo "Try \``basename $0` --help' for more information." >&2
  73.         exit 1
  74.       fi;;
  75.   esac
  76.   shift
  77. done
  78.  
  79. if test -z "$MPFILE" || test -z "$MPXFILE"; then
  80.   echo "$0: Need exactly two file arguments." >&2
  81.   echo "Try \``basename $0` --help' for more information." >&2
  82.   exit 1
  83. fi
  84.  
  85. trap "rm -f mpx$$.* $ERRLOG; exit 4" 1 2 3 15
  86.  
  87. # If MPX file is up-to-date, do nothing.
  88. if $NEWER $MPFILE $MPXFILE; then
  89.  
  90.   # Have to remake.
  91.   # Step 0: Check typesetter mode for consistency.
  92.   case "$mode" in
  93.       tex) MPTO="$MPTOTEX";;
  94.     troff) MPTO="$MPTOTR";;
  95.         *) echo "$0: Unknown typesetter mode: $mode" >&2
  96.            exit 1;;
  97.   esac
  98.   
  99.   # Step 1: Extract typesetter source from MetaPost source.
  100.   if $MPTO $MPFILE >mpx$$.tex 2>$ERRLOG; then :;
  101.     # success
  102.   else
  103.     # failure
  104.     echo "$0: Command failed: $MPTO $MPFILE" >&2
  105.     cat $ERRLOG >&2
  106.     rm -f mpx$$.tex
  107.     exit 1
  108.   fi
  109.   if test "$mode" = troff; then
  110.     mv -f mpx$$.tex mpx$$.i
  111.   fi
  112.   
  113.   # Step 2: Run typesetter.
  114.   if test "$mode" = tex; then
  115.     if test -r $MPTEXPRE; then
  116.       # Prepend user file.
  117.       cat $MPTEXPRE mpx$$.tex >mpx$$.tmp
  118.       mv mpx$$.tmp mpx$$.tex
  119.     fi
  120.  
  121.     if $TEX --interaction=batchmode mpx$$.tex </dev/null >/dev/null; then
  122.       WHATEVER_TO_MPX="$DVITOMP"
  123.       INFILE=mpx$$.dvi
  124.       INERROR=$DVIERR
  125.     else
  126.       # failure
  127.       mv -f mpx$$.tex $TEXERR
  128.       mv -f mpx$$.log $ERRLOG
  129.       echo "$0: Command failed: $TEX $TEXERR; see $ERRLOG" >&2
  130.       exit 2
  131.     fi
  132.   elif test "$mode" = troff; then
  133.     if cat mpx$$.i | eval $TROFF >mpx$$.t; then
  134.       # success, prepare for step 3.
  135.       WHATEVER_TO_MPX="$DMP"
  136.       INFILE=mpx$$.t
  137.       INERROR=$TROFF_OUTERR
  138.     else
  139.       # failure
  140.       mv -f mpx$$.i $TROFF_INERR
  141.       echo "$0: Command failed: cat $TROFF_INERR | $TROFF" >&2
  142.       rm -f mpx$$.t
  143.       exit 2
  144.     fi
  145.   else
  146.     echo "$0: Unknown typesetter mode: $mode; how did this happen?" >&2
  147.     exit 2
  148.   fi
  149.  
  150.   # Step 3: Translate typesetter output to a MetaPost MPX.
  151.   if $WHATEVER_TO_MPX $INFILE $MPXFILE >$ERRLOG; then
  152.     : # success
  153.   else 
  154.     # failure
  155.     mv -f $INFILE $INERROR
  156.     test $mode = troff && mv -f mpx$$.i $TROFF_INERR
  157.     echo "$0: Command failed: $WHATEVER_TO_MPX $INERROR $MPXFILE" >&2
  158.     # Better to remove $MPXFILE if something went wrong rather than
  159.     # leaving behind an unfinished or unusable version since $NEWER
  160.     # might think that all is fine if $MPXFILE exists.
  161.     rm -f $MPXFILE
  162.     cat $ERRLOG >&2
  163.     exit 3
  164.   fi
  165.  
  166.   rm -f $ERRLOG mpx$$.*
  167. fi
  168.  
  169. exit 0
  170.