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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
|
/* $RCSfile: gv.c,v $$Revision: 4.1 $$Date: 92/08/07 18:26:39 $
*
* Copyright (c) 1991, Larry Wall
*
* You may distribute under the terms of either the GNU General Public
* License or the Artistic License, as specified in the README file.
*
* $Log: gv.c,v $
* Revision 4.1 92/08/07 18:26:39 lwall
*
* Revision 4.0.1.4 92/06/08 15:32:19 lwall
* patch20: fixed confusion between a *var's real name and its effective name
* patch20: the debugger now warns you on lines that can't set a breakpoint
* patch20: the debugger made perl forget the last pattern used by //
* patch20: paragraph mode now skips extra newlines automatically
* patch20: ($<,$>) = ... didn't work on some architectures
*
* Revision 4.0.1.3 91/11/05 18:35:33 lwall
* patch11: length($x) was sometimes wrong for numeric $x
* patch11: perl now issues warning if $SIG{'ALARM'} is referenced
* patch11: *foo = undef coredumped
* patch11: solitary subroutine references no longer trigger typo warnings
* patch11: local(*FILEHANDLE) had a memory leak
*
* Revision 4.0.1.2 91/06/07 11:55:53 lwall
* patch4: new copyright notice
* patch4: added $^P variable to control calling of perldb routines
* patch4: added $^F variable to specify maximum system fd, default 2
* patch4: $` was busted inside s///
* patch4: default top-of-form run_format is now FILEHANDLE_TOP
* patch4: length($`), length($&), length($') now optimized to avoid string copy
* patch4: $^D |= 1024 now does syntax tree dump at run-time
*
* Revision 4.0.1.1 91/04/12 09:10:24 lwall
* patch1: Configure now differentiates getgroups() type from getgid() type
* patch1: you may now use "die" and "caller" in a signal handler
*
* Revision 4.0 91/03/20 01:39:41 lwall
* 4.0 baseline.
*
*/
#include "EXTERN.h"
#include "perl.h"
GV *
gv_AVadd(gv)
register GV *gv;
{
if (!GvAV(gv))
GvAV(gv) = newAV();
return gv;
}
GV *
gv_HVadd(gv)
register GV *gv;
{
if (!GvHV(gv))
GvHV(gv) = newHV(COEFFSIZE);
return gv;
}
GV *
gv_fetchfile(name)
char *name;
{
char tmpbuf[1200];
GV *gv;
sprintf(tmpbuf,"'_<%s", name);
gv = gv_fetchpv(tmpbuf, TRUE);
sv_setpv(GvSV(gv), name);
if (perldb)
(void)gv_HVadd(gv_AVadd(gv));
return gv;
}
GV *
gv_fetchmethod(stash, name)
HV* stash;
char* name;
{
AV* av;
GV* gv;
GV** gvp = (GV**)hv_fetch(stash,name,strlen(name),FALSE);
if (gvp && (gv = *gvp) != (GV*)&sv_undef && GvCV(gv))
return gv;
gvp = (GV**)hv_fetch(stash,"ISA",3,FALSE);
if (gvp && (gv = *gvp) != (GV*)&sv_undef && (av = GvAV(gv))) {
SV** svp = AvARRAY(av);
I32 items = AvFILL(av) + 1;
while (items--) {
char tmpbuf[512];
SV* sv = *svp++;
*tmpbuf = '_';
SvUPGRADE(sv, SVt_PV);
strcpy(tmpbuf+1,SvPVn(sv));
gv = gv_fetchpv(tmpbuf,FALSE);
if (!gv || !(stash = GvHV(gv))) {
if (dowarn)
warn("Can't locate package %s for @%s'ISA",
SvPV(sv), HvNAME(stash));
continue;
}
gv = gv_fetchmethod(stash, name);
if (gv)
return gv;
}
}
return 0;
}
GV *
gv_fetchpv(name,add)
register char *name;
I32 add;
{
register GV *gv;
GV**gvp;
register GP *gp;
I32 len;
register char *namend;
HV *stash;
char *sawquote = Nullch;
char *prevquote = Nullch;
bool global = FALSE;
if (isUPPER(*name)) {
if (*name > 'I') {
if (*name == 'S' && (
strEQ(name, "SIG") ||
strEQ(name, "STDIN") ||
strEQ(name, "STDOUT") ||
strEQ(name, "STDERR") ))
global = TRUE;
}
else if (*name > 'E') {
if (*name == 'I' && strEQ(name, "INC"))
global = TRUE;
}
else if (*name > 'A') {
if (*name == 'E' && strEQ(name, "ENV"))
global = TRUE;
}
else if (*name == 'A' && (
strEQ(name, "ARGV") ||
strEQ(name, "ARGVOUT") ))
global = TRUE;
}
for (namend = name; *namend; namend++) {
if (*namend == '\'' && namend[1])
prevquote = sawquote, sawquote = namend;
}
if (sawquote == name && name[1]) {
stash = defstash;
sawquote = Nullch;
name++;
}
else if (!isALPHA(*name) || global)
stash = defstash;
else if ((COP*)curcop == &compiling)
stash = curstash;
else
stash = curcop->cop_stash;
if (sawquote) {
char tmpbuf[256];
char *s, *d;
*sawquote = '\0';
/*SUPPRESS 560*/
if (s = prevquote) {
strncpy(tmpbuf,name,s-name+1);
d = tmpbuf+(s-name+1);
*d++ = '_';
strcpy(d,s+1);
}
else {
*tmpbuf = '_';
strcpy(tmpbuf+1,name);
}
gv = gv_fetchpv(tmpbuf,TRUE);
if (!(stash = GvHV(gv)))
stash = GvHV(gv) = newHV(0);
if (!HvNAME(stash))
HvNAME(stash) = savestr(name);
name = sawquote+1;
*sawquote = '\'';
}
len = namend - name;
gvp = (GV**)hv_fetch(stash,name,len,add);
if (!gvp || *gvp == (GV*)&sv_undef)
return Nullgv;
gv = *gvp;
if (SvTYPE(gv) == SVt_PVGV) {
SvMULTI_on(gv);
return gv;
}
else {
sv_upgrade(gv, SVt_PVGV);
if (SvLEN(gv))
Safefree(SvPV(gv));
Newz(602,gp, 1, GP);
GvGP(gv) = gp;
GvREFCNT(gv) = 1;
GvSV(gv) = NEWSV(72,0);
GvLINE(gv) = curcop->cop_line;
GvEGV(gv) = gv;
sv_magic((SV*)gv, (SV*)gv, '*', name, len);
GvSTASH(gv) = stash;
GvNAME(gv) = nsavestr(name, len);
GvNAMELEN(gv) = len;
if (isDIGIT(*name) && *name != '0')
sv_magic(GvSV(gv), (SV*)gv, 0, name, len);
if (add & 2)
SvMULTI_on(gv);
return gv;
}
}
void
gv_fullname(sv,gv)
SV *sv;
GV *gv;
{
HV *hv = GvSTASH(gv);
if (!hv)
return;
sv_setpv(sv, sv == (SV*)gv ? "*" : "");
sv_catpv(sv,HvNAME(hv));
sv_catpvn(sv,"'", 1);
sv_catpvn(sv,GvNAME(gv),GvNAMELEN(gv));
}
void
gv_efullname(sv,gv)
SV *sv;
GV *gv;
{
GV* egv = GvEGV(gv);
HV *hv = GvSTASH(egv);
if (!hv)
return;
sv_setpv(sv, sv == (SV*)gv ? "*" : "");
sv_catpv(sv,HvNAME(hv));
sv_catpvn(sv,"'", 1);
sv_catpvn(sv,GvNAME(egv),GvNAMELEN(egv));
}
IO *
newIO()
{
IO *io;
Newz(603,io,1,IO);
io->page_len = 60;
return io;
}
void
gv_check(min,max)
I32 min;
register I32 max;
{
register HE *entry;
register I32 i;
register GV *gv;
for (i = min; i <= max; i++) {
for (entry = HvARRAY(defstash)[i]; entry; entry = entry->hent_next) {
gv = (GV*)entry->hent_val;
if (SvMULTI(gv))
continue;
curcop->cop_line = GvLINE(gv);
warn("Possible typo: \"%s\"", GvNAME(gv));
}
}
}
GV *
newGVgen()
{
(void)sprintf(tokenbuf,"_GEN_%d",gensym++);
return gv_fetchpv(tokenbuf,TRUE);
}
/* hopefully this is only called on local symbol table entries */
GP*
gp_ref(gp)
GP* gp;
{
gp->gp_refcnt++;
return gp;
}
void
gp_free(gv)
GV* gv;
{
IO *io;
CV *cv;
GP* gp;
if (!gv || !(gp = GvGP(gv)))
return;
if (gp->gp_refcnt == 0) {
warn("Attempt to free unreferenced glob pointers");
return;
}
if (--gp->gp_refcnt > 0)
return;
sv_free(gp->gp_sv);
sv_free(gp->gp_av);
sv_free(gp->gp_hv);
if (io = gp->gp_io) {
do_close(gv,FALSE);
Safefree(io->top_name);
Safefree(io->fmt_name);
Safefree(io);
}
if (cv = gp->gp_cv)
sv_free(cv);
Safefree(gp);
GvGP(gv) = 0;
}
#if defined(CRIPPLED_CC) && (defined(iAPX286) || defined(M_I286) || defined(I80286))
#define MICROPORT
#endif
#ifdef MICROPORT /* Microport 2.4 hack */
AV *GvAVn(gv)
register GV *gv;
{
if (GvGP(gv)->gp_av)
return GvGP(gv)->gp_av;
else
return GvGP(gv_AVadd(gv))->gp_av;
}
HV *GvHVn(gv)
register GV *gv;
{
if (GvGP(gv)->gp_hv)
return GvGP(gv)->gp_hv;
else
return GvGP(gv_HVadd(gv))->gp_hv;
}
#endif /* Microport 2.4 hack */
GV *
fetch_gv(op,num)
OP *op;
I32 num;
{
if (op->op_private < num)
return 0;
if (op->op_flags & OPf_STACKED)
return gv_fetchpv(SvPVnx(*(stack_sp--)),TRUE);
else
return cGVOP->op_gv;
}
IO *
fetch_io(op,num)
OP *op;
I32 num;
{
GV *gv;
if (op->op_private < num)
return 0;
if (op->op_flags & OPf_STACKED)
gv = gv_fetchpv(SvPVnx(*(stack_sp--)),TRUE);
else
gv = cGVOP->op_gv;
if (!gv)
return 0;
return GvIOn(gv);
}
|