summaryrefslogtreecommitdiff
path: root/eval
blob: 21cebafe50683a2993328b1e87af8080a7122186 (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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318

void
save_lines(array, sv)
AV *array;
SV *sv;
{
    register char *s = sv->sv_ptr;
    register char *send = sv->sv_ptr + sv->sv_cur;
    register char *t;
    register int line = 1;

    while (s && s < send) {
	SV *tmpstr = NEWSV(85,0);

	t = index(s, '\n');
	if (t)
	    t++;
	else
	    t = send;

	sv_setpvn(tmpstr, s, t - s);
	av_store(array, line++, tmpstr);
	s = t;
    }
}

int
do_eval(sv,optype,stash,savecmd,gimme,arglast)
SV *sv;
int optype;
HV *stash;
int savecmd;
int gimme;
int *arglast;
{
    SV **st = stack->av_array;
    int retval;
    COP *myroot = Nullcop;
    AV *ar;
    int i;
    COP * VOL oldcurcmd = curcmd;
    VOL int oldtmps_floor = tmps_floor;
    VOL int oldsave = savestack->av_fill;
    VOL int oldperldb = perldb;
    PM * VOL oldspat = curspat;
    PM * VOL oldlspat = lastspat;

    VOL int sp = arglast[0];
    char *specfilename;
    char *tmpfilename;
    int parsing = 1;

    tmps_floor = tmps_ix;
    if (curstash != stash) {
	(void)save_hptr(&curstash);
	curstash = stash;
    }
    sv_setpv(GvSV(gv_fetchpv("@",TRUE)),"");
    if (curcmd->cop_line == 0)		/* don't debug debugger... */
	perldb = FALSE;
    curcmd = &compiling;
    if (optype == OP_EVAL) {		/* normal oldeval */
	curcmd->cop_filestab = gv_fetchfile("(oldeval)");
	curcmd->cop_line = 1;
	sv_setsv(linestr,sv);
	sv_catpv(linestr,";\n;\n");	/* be kind to them */
	if (perldb)
	    save_lines(GvAV(curcmd->cop_filestab), linestr);
    }
    else {
	if (last_root && !in_eval) {
	    Safefree(last_eval);
	    last_eval = Nullch;
	    cop_free(last_root);
	    last_root = Nullcop;
	}
	specfilename = SvPV(sv);
	sv_setpv(linestr,"");
	if (optype == OP_REQUIRE && &sv_undef !=
	  hv_fetch(GvHVn(incstab), specfilename, strlen(specfilename), 0)) {
	    curcmd = oldcurcmd;
	    tmps_floor = oldtmps_floor;
	    st[++sp] = &sv_yes;
	    perldb = oldperldb;
	    return sp;
	}
	tmpfilename = savestr(specfilename);
	if (*tmpfilename == '/' ||
	    (*tmpfilename == '.' && 
		(tmpfilename[1] == '/' ||
		 (tmpfilename[1] == '.' && tmpfilename[2] == '/'))))
	{
	    rsfp = fopen(tmpfilename,"r");
	}
	else {
	    ar = GvAVn(incstab);
	    for (i = 0; i <= ar->av_fill; i++) {
		(void)sprintf(buf, "%s/%s",
		  SvPV(av_fetch(ar,i,TRUE)), specfilename);
		rsfp = fopen(buf,"r");
		if (rsfp) {
		    char *s = buf;

		    if (*s == '.' && s[1] == '/')
			s += 2;
		    Safefree(tmpfilename);
		    tmpfilename = savestr(s);
		    break;
		}
	    }
	}
	curcmd->cop_filestab = gv_fetchfile(tmpfilename);
	Safefree(tmpfilename);
	tmpfilename = Nullch;
	if (!rsfp) {
	    curcmd = oldcurcmd;
	    tmps_floor = oldtmps_floor;
	    if (optype == OP_REQUIRE) {
		sprintf(tokenbuf,"Can't locate %s in @INC", specfilename);
		if (instr(tokenbuf,".h "))
		    strcat(tokenbuf," (change .h to .ph maybe?)");
		if (instr(tokenbuf,".ph "))
		    strcat(tokenbuf," (did you run h2ph?)");
		fatal("%s",tokenbuf);
	    }
	    if (gimme != G_ARRAY)
		st[++sp] = &sv_undef;
	    perldb = oldperldb;
	    return sp;
	}
	curcmd->cop_line = 0;
    }
    in_eval++;
    oldoldbufptr = oldbufptr = bufptr = SvPV(linestr);
    bufend = bufptr + linestr->sv_cur;
    if (++cxstack_ix >= block_max) {
	block_max += 128;
	Renew(block_stack, block_max, struct loop);
    }
    block_stack[cxstack_ix].block_label = "_EVAL_";
    block_stack[cxstack_ix].block_sp = sp;
#ifdef DEBUGGING
    if (debug & 4) {
	deb("(Pushing label #%d _EVAL_)\n", cxstack_ix);
    }
#endif
    eval_root = Nullcop;
    if (setjmp(block_stack[cxstack_ix].block_env)) {
	retval = 1;
    }
    else {
	error_count = 0;
	if (rsfp) {
	    retval = yyparse();
	    retval |= error_count;
	}
	else if (last_root && last_elen == bufend - bufptr
	  && *bufptr == *last_eval && !bcmp(bufptr,last_eval,last_elen)){
	    retval = 0;
	    eval_root = last_root;	/* no point in reparsing */
	}
	else if (in_eval == 1 && !savecmd) {
	    if (last_root) {
		Safefree(last_eval);
		last_eval = Nullch;
		cop_free(last_root);
	    }
	    last_root = Nullcop;
	    last_elen = bufend - bufptr;
	    last_eval = nsavestr(bufptr, last_elen);
	    retval = yyparse();
	    retval |= error_count;
	    if (!retval)
		last_root = eval_root;
	    if (!last_root) {
		Safefree(last_eval);
		last_eval = Nullch;
	    }
	}
	else
	    retval = yyparse();
    }
    myroot = eval_root;		/* in case cop_exec does another oldeval! */

    if (retval || error_count) {
	st = stack->av_array;
	sp = arglast[0];
	if (gimme != G_ARRAY)
	    st[++sp] = &sv_undef;
	if (parsing) {
#ifndef MANGLEDPARSE
#ifdef DEBUGGING
	    if (debug & 128)
		fprintf(stderr,"Freeing eval_root %lx\n",(long)eval_root);
#endif
	    cop_free(eval_root);
#endif
	    /*SUPPRESS 29*/ /*SUPPRESS 30*/
	    if ((COP*)eval_root == last_root)
		last_root = Nullcop;
	    eval_root = myroot = Nullcop;
	}
	if (rsfp) {
	    fclose(rsfp);
	    rsfp = 0;
	}
    }
    else {
	parsing = 0;
	sp = cop_exec(eval_root,gimme,sp);
	st = stack->av_array;
	for (i = arglast[0] + 1; i <= sp; i++)
	    st[i] = sv_mortalcopy(st[i]);
				/* if we don't save result, free zaps it */
	if (savecmd)
	    eval_root = myroot;
	else if (in_eval != 1 && myroot != last_root)
	    cop_free(myroot);
    }

    perldb = oldperldb;
    in_eval--;
#ifdef DEBUGGING
    if (debug & 4) {
	char *tmps = block_stack[cxstack_ix].block_label;
	deb("(Popping label #%d %s)\n",cxstack_ix,
	    tmps ? tmps : "" );
    }
#endif
    cxstack_ix--;
    tmps_floor = oldtmps_floor;
    curspat = oldspat;
    lastspat = oldlspat;
    if (savestack->av_fill > oldsave)	/* let them use local() */
	leave_scope(oldsave);

    if (optype != OP_EVAL) {
	if (retval) {
	    if (optype == OP_REQUIRE)
		fatal("%s", SvPV(GvSV(gv_fetchpv("@",TRUE))));
	}
	else {
	    curcmd = oldcurcmd;
	    if (gimme == G_SCALAR ? SvTRUE(st[sp]) : sp > arglast[0]) {
		(void)hv_store(GvHVn(incstab), specfilename,
		  strlen(specfilename), newSVsv(GvSV(curcmd->cop_filestab)),
		      0 );
	    }
	    else if (optype == OP_REQUIRE)
		fatal("%s did not return a true value", specfilename);
	}
    }
    curcmd = oldcurcmd;
    return sp;
}

int
do_try(cmd,gimme,arglast)
COP *cmd;
int gimme;
int *arglast;
{
    SV **st = stack->av_array;

    COP * VOL oldcurcmd = curcmd;
    VOL int oldtmps_floor = tmps_floor;
    VOL int oldsave = savestack->av_fill;
    PM * VOL oldspat = curspat;
    PM * VOL oldlspat = lastspat;
    VOL int sp = arglast[0];

    tmps_floor = tmps_ix;
    sv_setpv(GvSV(gv_fetchpv("@",TRUE)),"");
    in_eval++;
    if (++cxstack_ix >= block_max) {
	block_max += 128;
	Renew(block_stack, block_max, struct loop);
    }
    block_stack[cxstack_ix].block_label = "_EVAL_";
    block_stack[cxstack_ix].block_sp = sp;
#ifdef DEBUGGING
    if (debug & 4) {
	deb("(Pushing label #%d _EVAL_)\n", cxstack_ix);
    }
#endif
    if (setjmp(block_stack[cxstack_ix].block_env)) {
	st = stack->av_array;
	sp = arglast[0];
	if (gimme != G_ARRAY)
	    st[++sp] = &sv_undef;
    }
    else {
	sp = cop_exec(cmd,gimme,sp);
	st = stack->av_array;
/*	for (i = arglast[0] + 1; i <= sp; i++)
	    st[i] = sv_mortalcopy(st[i]);  not needed, I think */
				/* if we don't save result, free zaps it */
    }

    in_eval--;
#ifdef DEBUGGING
    if (debug & 4) {
	char *tmps = block_stack[cxstack_ix].block_label;
	deb("(Popping label #%d %s)\n",cxstack_ix,
	    tmps ? tmps : "" );
    }
#endif
    cxstack_ix--;
    tmps_floor = oldtmps_floor;
    curspat = oldspat;
    lastspat = oldlspat;
    curcmd = oldcurcmd;
    if (savestack->av_fill > oldsave)	/* let them use local() */
	leave_scope(oldsave);

    return sp;
}