summaryrefslogtreecommitdiff
path: root/Master/bin/powerpc-darwin/makempx
blob: 8aaa9bac1dc096d08bcd4f61c65366168f68f5b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/bin/sh
# $Id: makempx.in,v 1.18 2005/06/22 17:33:26 olaf Exp $
# Make an MPX file from the labels in a MetaPost source file,
# using mpto and either dvitomp (TeX) or dmp (troff).
# From John Hobby's original (though there's not much of it left by now).
# Public domain.
#
#
version=0.9

: ${DMP=dmp}
: ${DVITOMP=dvitomp}
: ${MAKEMPX_BINDIR=`echo $0 | sed 's%/[^/]*$%%'`}
: ${MPTEXPRE=mptexpre.tex}
: ${MPTOTEX='mpto -tex'}
: ${MPTOTR='mpto -troff'}
: ${NEWER=newer}
# This is oriented towards GNU roff: old value was
# 'eqn -Tpost -d\$\$ | troff -Tpost'
: ${TROFF='eqn -Tps -d\$\$ | troff -Tps'}

# convert relative path to absolute in $MAKEMPX_BINDIR:
case "$MAKEMPX_BINDIR" in
  /*) ;;
  *)   MAKEMPX_BINDIR=`pwd`/$MAKEMPX_BINDIR;;
esac

PATH=$MAKEMPX_BINDIR:/bin:/usr/bin:$PATH

# These names are documented in the MetaPost manual, so it's
# unwise to change them.
ERRLOG=mpxerr.log		# file for an error log if necessary
TEXERR=mpxerr.tex		# file for erroneous TeX if any
DVIERR=mpxerr.dvi		# troublesome dvi file if any
TROFF_INERR=mpxerr		# file for erroneous troff input, if any
TROFF_OUTERR=mpxerr.t		# file for troublesome troff output, if any


usage="Usage: $0 [-tex|-tex=<program>|-troff] MPFILE MPXFILE.
  If MPXFILE is older than MPFILE, translate the labels from the MetaPost
  input file MPFIle to low-level commands in MPXFILE, by running
    $MPTOTEX, $TEX, and $DVITOMP
  by default; or, if -troff is specified,
    $MPTOTR, $TROFF, and $DMP.

  The current directory is used for writing temporary files.  Errors are
  left in mpxerr.{tex,log,dvi}.

  If the file named in \$MPTEXPRE (mptexpre.tex by default) exists, it is
  prepended to the output in tex mode.

Email bug reports to metapost@tug.org."


mode=tex

while test $# -gt 0; do
  case "$1" in
    -help|--help) 
      echo "$usage"; exit 0;;
    -version|--version)
      echo "`basename $0` $version"
      echo "There is NO warranty.  This script is public domain.
Primary author: John Hobby."
      exit 0;;
    -troff|--troff) mode=troff;;
    -tex|--tex) mode=tex;;
    -tex=*|--tex=*) mode=tex; TEX=`echo "$1" | sed 's/^-*tex=//'`;;
    -*) 
      echo "$0: Invalid option: $1." >&2
      echo "Try \``basename $0` --help' for more information." >&2 
      exit 1;;
    *) 
      if test -z "$MPFILE"; then
        MPFILE=$1                   # input file
      elif test -z "$MPXFILE"; then
        MPXFILE=$1                  # output file
      else
        echo "$0: Extra argument $1." >&2 
        echo "Try \``basename $0` --help' for more information." >&2
        exit 1
      fi;;
  esac
  shift
done

if test -z "$MPFILE" || test -z "$MPXFILE"; then
  echo "$0: Need exactly two file arguments." >&2
  echo "Try \``basename $0` --help' for more information." >&2
  exit 1
fi

trap "rm -f mpx$$.* \"$ERRLOG\"; exit 4" 1 2 3 15

# If MPX file is up-to-date, do nothing.
if $NEWER "$MPFILE" "$MPXFILE"; then

  # Have to remake.
  # Step 0: Check typesetter mode for consistency.
  case "$mode" in
      tex) MPTO="$MPTOTEX";;
    troff) MPTO="$MPTOTR";;
        *) echo "$0: Unknown typesetter mode: $mode" >&2
           exit 1;;
  esac
  
  # Step 1: Extract typesetter source from MetaPost source.
  if $MPTO "$MPFILE" >mpx$$.tex 2>"$ERRLOG"; then :;
    # success
  else
    # failure
    echo "$0: Command failed: $MPTO $MPFILE" >&2
    cat "$ERRLOG" >&2
    rm -f mpx$$.tex
    exit 1
  fi
  if test "$mode" = troff; then
    mv -f mpx$$.tex mpx$$.i
  fi
  
  # Step 2: Run typesetter.
  if test "$mode" = tex; then
    if test -r "$MPTEXPRE"; then
      # Prepend user file.
      cat "$MPTEXPRE" mpx$$.tex >mpx$$.tmp
      mv mpx$$.tmp mpx$$.tex
    fi

    test -z "$TEX" && \
      TEX=`sed -n '1s/%\&[ 	]*\([^ 	
]*\).*$/\1 --parse-first-line/p;q' mpx$$.tex`
    test -z "$TEX" && TEX='tex --parse-first-line'

    if $TEX --interaction=batchmode mpx$$.tex </dev/null >/dev/null; then
      WHATEVER_TO_MPX="$DVITOMP"
      INFILE=mpx$$.dvi
      INERROR=$DVIERR
    else
      # failure
      mv -f mpx$$.tex "$TEXERR"
      mv -f mpx$$.log "$ERRLOG"
      echo "$0: Command failed: $TEX $TEXERR; see $ERRLOG" >&2
      exit 2
    fi
  elif test "$mode" = troff; then
    if cat mpx$$.i | eval $TROFF >mpx$$.t; then
      # success, prepare for step 3.
      WHATEVER_TO_MPX="$DMP"
      INFILE=mpx$$.t
      INERROR=$TROFF_OUTERR
    else
      # failure
      mv -f mpx$$.i "$TROFF_INERR"
      echo "$0: Command failed: cat $TROFF_INERR | $TROFF" >&2
      rm -f mpx$$.t
      exit 2
    fi
  else
    echo "$0: Unknown typesetter mode: $mode; how did this happen?" >&2
    exit 2
  fi

  # Step 3: Translate typesetter output to a MetaPost MPX.
  if $WHATEVER_TO_MPX "$INFILE" "$MPXFILE" >"$ERRLOG"; then
    : # success
  else 
    # failure
    mv -f "$INFILE" "$INERROR"
    test $mode = troff && mv -f mpx$$.i "$TROFF_INERR"
    echo "$0: Command failed: $WHATEVER_TO_MPX $INERROR $MPXFILE" >&2
    # Better to remove $MPXFILE if something went wrong rather than
    # leaving behind an unfinished or unusable version since $NEWER
    # might think that all is fine if $MPXFILE exists.
    rm -f "$MPXFILE"
    cat "$ERRLOG" >&2
    exit 3
  fi

  rm -f "$ERRLOG" mpx$$.*
fi

exit 0