summaryrefslogtreecommitdiff
path: root/Build/source/texk/web2c/web2c/convert
blob: 0b56a9202efe8861a239b9757882e015f5251905 (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
#!/bin/sh
# Convert WEB programs not needing special treatment to C.
# Public domain.  Originally written ca.1985 by Tom Rokicki,
# Tim Morgan, Karl Berry, et al.

: ${srcdir=.}

usage="Usage: $0 BASE

BASE is the root part of the file to be converted; it is extended with
.p to make the name of the (input) Pascal file, and extended with .c and
.h to make the name of the main (output) C files.  Additional files
BASEini.c, BASEd.h, BASEcoerce.h are created for the larger programs.
"

basefile=
while test $# -gt 0; do
  case $1 in
    -*) echo "$1?" >&2; echo "$usage" >&2; exit 1;;
     *) if test -n "$basefile"; then
          echo "$1?" >&2; echo "$usage" >&2; exit 1; fi
        basefile=$1;;
  esac
  shift
done
if test -z "$basefile"; then
  echo "$0: missing base (Pascal) file argument." >&2
  echo "$usage" >&2
  exit 1
fi

pascalfile=$basefile.p
cfile=$basefile.c

# This is for tangleboot if the build and source directories are different.
test -r $pascalfile || pascalfile=$srcdir/$pascalfile

# We use cpascal.h by default instead of config.h because straight C
# routines can only be confused by the definitions for `chr', etc.
hfile=cpascal.h
more_defines=
web2c_options=-c$basefile
precmd=
midcmd=
fixwrites_options=
# This used to have various values to control the number of *tex[0-9].c files.
# We now avoid to split the C code for MF and all TeX-like engines.
splitup_options="-i -l 65000"
postcmd=
output="> $cfile"
output_files="$cfile $basefile.h"

case $basefile in
  pbibtex|pdvitype|ppltotf|ptftopl)
    more_defines="$srcdir/ptexdir/ptex.defines"
    hfile=ptexdir/kanji.h
    ;;
  upbibtex|updvitype|uppltotf|uptftopl)
    more_defines="$srcdir/uptexdir/uptex.defines"
    hfile=uptexdir/kanji.h
    ;;
esac

case $basefile in
  bibtex|pbibtex|upbibtex)
    midcmd="| sed -f $srcdir/web2c/cvtbib.sed"
    ;;

  mf|mflua|mfluajit|tex|aleph|etex|pdftex|ptex|eptex|euptex|uptex|xetex)
    case $basefile in
      mf*)
        more_defines="$srcdir/web2c/texmf.defines $srcdir/web2c/mfmp.defines"
        precmd="| sed -f $srcdir/web2c/cvtmf1.sed"
        web2c_options="-m -c${basefile}coerce"
        midcmd="| sed -f $srcdir/web2c/cvtmf2.sed"
        ;;

      *)
        more_defines="$srcdir/web2c/texmf.defines $srcdir/synctexdir/synctex.defines"
        web2c_options="-t -c${basefile}coerce"
        fixwrites_options=-t
        ;;
    esac
    prog_defines="$srcdir/${basefile}dir/$basefile.defines"
    if test -f $prog_defines; then
      more_defines="$more_defines $prog_defines"
    fi
    hfile=texmfmp.h
    postcmd="| ./web2c/splitup $splitup_options $basefile"
    cfile=${basefile}0.c # last output file, or thereabouts
    output=
    output_files="$basefile[0-9].c ${basefile}ini.c ${basefile}d.h \
                  ${basefile}coerce.h"
    ;;
esac

# Do it.
#set -x
eval "cat $srcdir/web2c/common.defines $more_defines $pascalfile \
  $precmd \
  | ./web2c/web2c -h$hfile $web2c_options \
  $midcmd \
  | ./web2c/fixwrites $fixwrites_options $basefile \
  $postcmd \
  $output"

# Using the above pipeline as the condition of an if does no good, since
# typical sh's use the status of the first command as the pipeline result.
# So check for an empty output file, or one with the error marker we put in.
if test ! -s "$cfile" || grep @error@ $output_files >/dev/null; then
  : ${TMPDIR=/tmp}
  # Don't just delete it, since it may be useful for debugging.
  if test ! -s "$pascalfile"; then
    echo "$0: $pascalfile empty, removing it." >&2
    rm -f $pascalfile
  fi
  echo "$0: conversion of $pascalfile failed, moving dregs:" >&2
  cmd="mv $output_files $TMPDIR"
  (cd $TMPDIR && rm -f $output_files)
  echo "$0:   $cmd" >&2
  $cmd
  exit 1
fi

case $basefile in
  bibtex|pbibtex|upbibtex)
    sed -e 's/(buftype)//g' -e 's/(pdstype)//g' <$basefile.h >x$basefile.h
    mv x$basefile.h $basefile.h
    ;;
  mf|mflua|mfluajit|tex|aleph|etex|pdftex|ptex|eptex|euptex|uptex|xetex)
    sleep 2 # so timestamps are definitely later, to avoid make weirdness
    cat $srcdir/web2c/coerce.h >>${basefile}coerce.h
    touch ${basefile}d.h
    ;;
esac

exit 0