summaryrefslogtreecommitdiff
path: root/perly.fixer
blob: 1c07724e630740250544e59fbf0683ba7933dfaf (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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#!/bin/sh

# Fix up yacc output to allow dynamic allocation.  Since perly.c
# is now provided with the perl source, this should not be necessary.
#
# However, if the user wishes to use byacc, or wishes to try another 
# compiler compiler (e.g. bison or yacc), this script will get run.
# See makefile run_byacc target for more details.
#
# Currently, only byacc version 1.8 is fully supported.
#
#  Hacks to make it work with Interactive's SysVr3 Version 2.2
#   doughera@lafvax.lafayette.edu (Andy Dougherty)   3/23/91
#
# Additional information to make the BSD section work with SunOS 4.0.2
#   tdinger@East.Sun.COM (Tom Dinger)	4/15/1991
#
# Also edit some practices gcc -Wall finds questionable.
#  

gnupatch=patch
. config.sh

input=$1
output=$2
tmp=/tmp/f$$

inputh=`echo $input|sed 's:\.c$:.h:'`
if grep '^#ifdef PERL_CORE' $inputh; then
	: never mind
else
	echo "#ifdef PERL_CORE"	> $tmp
	sed -e 's:^typedef union {:#endif /* PERL_CORE */\
\
typedef union {:' $inputh		>> $tmp
	mv -f $tmp $inputh
fi

if grep 'yaccpar 1.8 (Berkeley)' $input >/dev/null 2>&1; then
    cp $input $output
    # Don't expect the diff to do everything -- do some by hand
    if test -f perly_c.diff; then
	$gnupatch -F3 $output <perly_c.diff
	sed -e '/^[ 	]*printf("yydebug:/s/printf(/PerlIO_printf(Perl_debug_log, /' \
	    -e '/^#line /s/"y[.]tab[.]c"/"perly.c"/' \
	    -e '/\[\] *= *[{]/s/^/static /' \
	    -e '/^static static/s/^static //' \
	    -e '/^#define.WORD/,/^#define.ARROW/d' \
	    -e '/^int.yydebug/,/^#define.yystacksize/d' \
	    -e 's/^yyerrlab:$//' \
	    -e 's/^    goto yyerrlab;//' \
	    -e 's/^yynewerror:$//' \
	    -e 's/^    goto yynewerror;//' \
	    -e 's|^static char yysccsid\(.*\)|/* static char yysccsid\1 */|' \
	    < $output > $tmp && mv -f $tmp $output || exit 1
	rm -rf $input
	echo "If you need to debug perly.c, you need to fix up the #line"
	echo "directives yourself."
    fi
    exit
elif grep 'yaccpar	1.9 (Berkeley)' $input >/dev/null 2>&1; then
    if test -f perly.c.dif9; then
	$gnupatch -F3 $output <perly.c.dif9
	sed -e '/^[ 	]*printf("yydebug:/s/printf(/PerlIO_printf(Perl_debug_log, /' \
	    -e '/^#line /s/"y[.]tab[.]c"/"perly.c"/' \
	    -e '/\[\] *= *[{]/s/^/static /' \
	    -e '/^static static/s/^static //' \
	    -e '/^#define.WORD/,/^#define.ARROW/d' \
	    -e '/^int.yydebug/,/^#define.yystacksize/d' \
	    -e 's/^yyerrlab:$//' \
	    -e 's/^    goto yyerrlab;//' \
	    -e 's/^yynewerror:$//' \
	    -e 's/^    goto yynewerror;//' \
	    -e 's|^static char yysccsid\(.*\)|/* static char yysccsid\1 */|' \
	    < $output > $tmp && mv -f $tmp $output || exit 1
	rm -rf $input
	echo "If you need to debug perly.c, you need to fix up the #line"
	echo "directives yourself."
	exit 0
    else
	echo "Diffs from byacc-1.9 are not available."
	echo "If you wish to proceed anyway, do"
	echo "cp $input $output"
	echo "cp y.tab.h perly.h"
	echo "and re-run make. Otherwise, I will use the old perly.c"
	touch perly.c
	# Exit with error status to stop make.
	exit 1
    fi
fi

plan="unknown"

echo ""
echo "Warning: the yacc you have used is not directly supported by perl."
echo "The perly.fixer script will attempt to make some changes to the generated"
echo "file. The changes may be incomplete and that might lead to problems later"
echo "(especially with complex scripts). You may need to apply the changes"
echo "embedded in perl.fixer (and/or perly_c.dif*) by hand."
echo ""

# Below, we check for various characteristic yaccpar outputs.

#  Test for BSD 4.3 version.
#  Also tests for the SunOS 4.0.2 version
egrep 'YYSTYPE[ 	]*yyv\[ *YYMAXDEPTH *\];
short[  ]*yys\[ *YYMAXDEPTH *\] *;
yyps *= *&yys\[ *-1 *\];
yypv *= *&yyv\[ *-1 *\];
if *\( *\+\+yyps *>=* *&yys\[ *YYMAXDEPTH *\] *\)' $input >$tmp 2>/dev/null

set `wc -l $tmp`
if test "$1" = "5"; then
      plan="bsd43"
fi

if test "$plan" = "unknown"; then
    #   Test for ISC 2.2 version (probably generic SysVr3).
egrep 'YYSTYPE[ 	]*yyv\[ *YYMAXDEPTH *\];
int[    ]*yys\[ *YYMAXDEPTH *\] *;
yyps *= *&yys\[ *-1 *\];
yypv *= *&yyv\[ *-1 *\];
if *\( *\+\+yy_ps *>= *&yys\[ *YYMAXDEPTH *\] *\)' $input >$tmp 2>/dev/null

    set `wc -l $tmp`
    if test "$1" = "5"; then
	plan="isc"
    fi
fi

# ------

case "$plan" in
    ##################################################################
    # The SunOS 4.0.2 version has the comparison fixed already.
    # Also added are out of memory checks (makes porting the generated
    # code easier) For most systems, it can't hurt. -- TD
    "bsd43")
	echo "Attempting to patch perly.c to allow dynamic yacc stack allocation"
	echo "Assuming bsd4.3 yaccpar"
	cat >$tmp <<'END'
/YYSTYPE[ 	]*yyv\[ *YYMAXDEPTH *\];/c\
int yymaxdepth = YYMAXDEPTH;\
YYSTYPE *yyv; /* where the values are stored */\
short *yys;\
short *maxyyps;

/short[ 	]*yys\[ *YYMAXDEPTH *\] *;/d

/yyps *= *&yys\[ *-1 *\];/d

/yypv *= *&yyv\[ *-1 *\];/c\
\	if (!yyv) {\
\	    New(73, yyv, yymaxdepth, YYSTYPE);\
\	    New(73, yys, yymaxdepth, short);\
\	    if ( !yyv || !yys ) {\
\		yyerror( "out of memory" );\
\		return(1);\
\	    }\
\	    maxyyps = &yys[yymaxdepth];\
\	}\
\	yyps = &yys[-1];\
\	yypv = &yyv[-1];


/if *( *\+\+yyps *>=* *&yys\[ *YYMAXDEPTH *\] *)/c\
\		if( ++yyps >= maxyyps ) {\
\		    int tv = yypv - yyv;\
\		    int ts = yyps - yys;\
\
\		    yymaxdepth *= 2;\
\		    Renew(yyv, yymaxdepth, YYSTYPE);\
\		    Renew(yys, yymaxdepth, short);\
\		    if ( !yyv || !yys ) {\
\			yyerror( "yacc stack overflow" );\
\			return(1);\
\		    }\
\		    yyps = yys + ts;\
\		    yypv = yyv + tv;\
\		    maxyyps = &yys[yymaxdepth];\
\		}

/yacc stack overflow.*}/d
/yacc stack overflow/,/}/d
END
	if sed -f $tmp <$input >$output
	then echo "The edit seems to have been applied okay."
	else echo "The edit seems to have failed!"
	fi
	;;

    #######################################################
    "isc") # Interactive Systems 2.2  version
	echo "Attempting to patch perly.c to allow dynamic yacc stack allocation"
	echo "Assuming Interactive SysVr3 2.2 yaccpar"
	# Easier to simply put whole script here than to modify the
	# bsd script with sed.
	# Main changes:  yaccpar sometimes uses yy_ps and yy_pv
	# which are local register variables.
	#  if(++yyps > YYMAXDEPTH) had opening brace on next line.
	# I've kept that brace in along with a call to yyerror if
	# realloc fails. (Actually, I just don't know how to do
	# multi-line matches in sed.)
	cat > $tmp << 'END'
/YYSTYPE[ 	]*yyv\[ *YYMAXDEPTH *\];/c\
int yymaxdepth = YYMAXDEPTH;\
YYSTYPE *yyv; /* where the values are stored */\
int *yys;\
int *maxyyps;

/int[ 	]*yys\[ *YYMAXDEPTH *\] *;/d

/yyps *= *&yys\[ *-1 *\];/d

/yypv *= *&yyv\[ *-1 *\];/c\
\	if (!yyv) {\
\	    New(73, yyv, yymaxdepth, YYSTYPE);\
\	    New(73, yys, yymaxdepth, int);\
\	    maxyyps = &yys[yymaxdepth];\
\	}\
\	yyps = &yys[-1];\
\	yypv = &yyv[-1];

/if *( *\+\+yy_ps *>= *&yys\[ *YYMAXDEPTH *\] *)/c\
\		if( ++yy_ps >= maxyyps ) {\
\		    int tv = yy_pv - yyv;\
\		    int ts = yy_ps - yys;\
\
\		    yymaxdepth *= 2;\
\		    Renew(yyv, yymaxdepth, YYSTYPE);\
\		    Renew(yys, yymaxdepth, int);\
\		    yy_ps = yyps = yys + ts;\
\		    yy_pv = yypv = yyv + tv;\
\		    maxyyps = &yys[yymaxdepth];\
\		}\
\		if (yyv == NULL || yys == NULL)
END
	if sed -f $tmp < $input > $output
	then echo "The edit seems to have been applied okay."
	else echo "The edit seems to have failed!"
	fi
	;;

    ######################################################
    # Plan still unknown
    *)
	echo "Unable to patch perly.c to allow dynamic yacc stack allocation (plan=$plan)"
	# just do minimal change to write $output from $input
	sed -e 's/Received token/ *** Received token/' $input >$output
	;;
esac

echo ""
rm -rf $tmp $input