33d1c5cd1a1bf87e87c1154664fb605337b16727
[p5sagit/p5-mst-13.2.git] / perly.fixer
1 #!/bin/sh
2
3 #  Hacks to make it work with Interactive's SysVr3 Version 2.2
4 #   doughera@lafvax.lafayette.edu (Andy Dougherty)   3/23/91
5
6 input=$1
7 output=$2
8 tmp=/tmp/f$$
9
10 plan="unknown"
11
12 #  Test for BSD 4.3 version.
13 egrep 'YYSTYPE[         ]*yyv\[ *YYMAXDEPTH *\];
14 short[  ]*yys\[ *YYMAXDEPTH *\] *;
15 yyps *= *&yys\[ *-1 *\];
16 yypv *= *&yyv\[ *-1 *\];
17 if *\( *\+\+yyps *> *&yys\[ *YYMAXDEPTH *\] *\)' $input >$tmp
18
19 set `wc -l $tmp`
20 if test "$1" = "5"; then
21       plan="bsd43"
22 fi
23
24 if test "$plan" = "unknown"; then
25     #   Test for ISC 2.2 version.
26 egrep 'YYSTYPE[         ]*yyv\[ *YYMAXDEPTH *\];
27 int[    ]*yys\[ *YYMAXDEPTH *\] *;
28 yyps *= *&yys\[ *-1 *\];
29 yypv *= *&yyv\[ *-1 *\];
30 if *\( *\+\+yy_ps *>= *&yys\[ *YYMAXDEPTH *\] *\)' $input >$tmp
31
32     set `wc -l $tmp`
33     if test "$1" = "5"; then
34         plan="isc"
35     fi
36 fi
37
38 case "$plan" in
39     #######################################################
40     "bsd43")
41         echo "Patching perly.c to allow dynamic yacc stack allocation"
42         echo "Assuming bsd4.3 yaccpar"
43         cat >$tmp <<'END'
44 /YYSTYPE[       ]*yyv\[ *YYMAXDEPTH *\];/c\
45 int yymaxdepth = YYMAXDEPTH;\
46 YYSTYPE *yyv; /* where the values are stored */\
47 short *yys;\
48 short *maxyyps;
49
50 /short[         ]*yys\[ *YYMAXDEPTH *\] *;/d
51
52 /yyps *= *&yys\[ *-1 *\];/d
53
54 /yypv *= *&yyv\[ *-1 *\];/c\
55 \       if (!yyv) {\
56 \           yyv = (YYSTYPE*) malloc(yymaxdepth * sizeof(YYSTYPE));\
57 \           yys = (short*) malloc(yymaxdepth * sizeof(short));\
58 \           maxyyps = &yys[yymaxdepth];\
59 \       }\
60 \       yyps = &yys[-1];\
61 \       yypv = &yyv[-1];
62
63
64 /if *( *\+\+yyps *> *&yys\[ *YYMAXDEPTH *\] *)/c\
65 \               if( ++yyps >= maxyyps ) {\
66 \                   int tv = yypv - yyv;\
67 \                   int ts = yyps - yys;\
68 \
69 \                   yymaxdepth *= 2;\
70 \                   yyv = (YYSTYPE*)realloc((char*)yyv,\
71 \                     yymaxdepth*sizeof(YYSTYPE));\
72 \                   yys = (short*)realloc((char*)yys,\
73 \                     yymaxdepth*sizeof(short));\
74 \                   yyps = yys + ts;\
75 \                   yypv = yyv + tv;\
76 \                   maxyyps = &yys[yymaxdepth];\
77 \               }
78
79 /yacc stack overflow.*}/d
80 /yacc stack overflow/,/}/d
81 END
82         sed -f $tmp <$input >$output ;;
83
84     #######################################################
85     "isc") # Interactive Systems 2.2  version
86         echo "Patching perly.c to allow dynamic yacc stack allocation"
87         echo "Assuming Interactive SysVr3 2.2 yaccpar"
88         # Easier to simply put whole script here than to modify the
89         # bsd script with sed.
90         # Main changes:  yaccpar sometimes uses yy_ps and yy_pv
91         # which are local register variables.
92         #  if(++yyps > YYMAXDEPTH) had opening brace on next line.
93         # I've kept that brace in along with a call to yyerror if
94         # realloc fails. (Actually, I just don't know how to do
95         # multi-line matches in sed.)
96         cat > $tmp << 'END'
97 /YYSTYPE[       ]*yyv\[ *YYMAXDEPTH *\];/c\
98 int yymaxdepth = YYMAXDEPTH;\
99 YYSTYPE *yyv; /* where the values are stored */\
100 int *yys;\
101 int *maxyyps;
102
103 /int[   ]*yys\[ *YYMAXDEPTH *\] *;/d
104
105 /yyps *= *&yys\[ *-1 *\];/d
106
107 /yypv *= *&yyv\[ *-1 *\];/c\
108 \       if (!yyv) {\
109 \           yyv = (YYSTYPE*) malloc(yymaxdepth * sizeof(YYSTYPE));\
110 \           yys = (int*) malloc(yymaxdepth * sizeof(int));\
111 \           maxyyps = &yys[yymaxdepth];\
112 \       }\
113 \       yyps = &yys[-1];\
114 \       yypv = &yyv[-1];
115
116 /if *( *\+\+yy_ps *>= *&yys\[ *YYMAXDEPTH *\] *)/c\
117 \               if( ++yy_ps >= maxyyps ) {\
118 \                   int tv = yy_pv - yyv;\
119 \                   int ts = yy_ps - yys;\
120 \
121 \                   yymaxdepth *= 2;\
122 \                   yyv = (YYSTYPE*)realloc((char*)yyv,\
123 \                     yymaxdepth*sizeof(YYSTYPE));\
124 \                   yys = (int*)realloc((char*)yys,\
125 \                     yymaxdepth*sizeof(int));\
126 \                   yy_ps = yyps = yys + ts;\
127 \                   yy_pv = yypv = yyv + tv;\
128 \                   maxyyps = &yys[yymaxdepth];\
129 \               }\
130 \               if (yyv == NULL || yys == NULL)
131 END
132         sed -f $tmp < $input > $output ;;
133
134     ######################################################
135     # Plan still unknown
136     *) mv $input $output;
137 esac
138
139 rm -rf $tmp $input