Commit | Line | Data |
a0d0e21e |
1 | /* mg.c |
79072805 |
2 | * |
eb1102fc |
3 | * Copyright (c) 1991-2002, Larry Wall |
79072805 |
4 | * |
5 | * You may distribute under the terms of either the GNU General Public |
6 | * License or the Artistic License, as specified in the README file. |
7 | * |
a0d0e21e |
8 | */ |
9 | |
10 | /* |
11 | * "Sam sat on the ground and put his head in his hands. 'I wish I had never |
12 | * come here, and I don't want to see no more magic,' he said, and fell silent." |
79072805 |
13 | */ |
14 | |
ccfc67b7 |
15 | /* |
16 | =head1 Magical Functions |
17 | */ |
18 | |
79072805 |
19 | #include "EXTERN.h" |
864dbfa3 |
20 | #define PERL_IN_MG_C |
79072805 |
21 | #include "perl.h" |
22 | |
5cd24f17 |
23 | #if defined(HAS_GETGROUPS) || defined(HAS_SETGROUPS) |
188ea221 |
24 | # ifndef NGROUPS |
25 | # define NGROUPS 32 |
26 | # endif |
b7953727 |
27 | # ifdef I_GRP |
28 | # include <grp.h> |
29 | # endif |
188ea221 |
30 | #endif |
31 | |
17aa7f3d |
32 | #ifdef __hpux |
33 | # include <sys/pstat.h> |
34 | #endif |
35 | |
23ada85b |
36 | /* if you only have signal() and it resets on each signal, FAKE_PERSISTENT_SIGNAL_HANDLERS fixes */ |
85b332e2 |
37 | #if !defined(HAS_SIGACTION) && defined(VMS) |
23ada85b |
38 | # define FAKE_PERSISTENT_SIGNAL_HANDLERS |
85b332e2 |
39 | #endif |
2e34cc90 |
40 | /* if we're doing kill() with sys$sigprc on VMS, FAKE_DEFAULT_SIGNAL_HANDLERS */ |
7719e241 |
41 | #if defined(KILL_BY_SIGPRC) |
2e34cc90 |
42 | # define FAKE_DEFAULT_SIGNAL_HANDLERS |
43 | #endif |
85b332e2 |
44 | |
acfe0abc |
45 | static void restore_magic(pTHX_ void *p); |
46 | static void unwind_handler_stack(pTHX_ void *p); |
51371543 |
47 | |
c07a80fd |
48 | /* |
49 | * Use the "DESTRUCTOR" scope cleanup to reinstate magic. |
50 | */ |
51 | |
52 | struct magic_state { |
53 | SV* mgs_sv; |
54 | U32 mgs_flags; |
455ece5e |
55 | I32 mgs_ss_ix; |
c07a80fd |
56 | }; |
455ece5e |
57 | /* MGS is typedef'ed to struct magic_state in perl.h */ |
76e3520e |
58 | |
59 | STATIC void |
8fb26106 |
60 | S_save_magic(pTHX_ I32 mgs_ix, SV *sv) |
c07a80fd |
61 | { |
455ece5e |
62 | MGS* mgs; |
c07a80fd |
63 | assert(SvMAGICAL(sv)); |
765f542d |
64 | #ifdef PERL_COPY_ON_WRITE |
65 | /* Turning READONLY off for a copy-on-write scalar is a bad idea. */ |
66 | if (SvIsCOW(sv)) |
67 | sv_force_normal(sv); |
68 | #endif |
c07a80fd |
69 | |
685f876f |
70 | SAVEDESTRUCTOR_X(restore_magic, INT2PTR(void*, (IV)mgs_ix)); |
455ece5e |
71 | |
72 | mgs = SSPTR(mgs_ix, MGS*); |
c07a80fd |
73 | mgs->mgs_sv = sv; |
74 | mgs->mgs_flags = SvMAGICAL(sv) | SvREADONLY(sv); |
455ece5e |
75 | mgs->mgs_ss_ix = PL_savestack_ix; /* points after the saved destructor */ |
c07a80fd |
76 | |
77 | SvMAGICAL_off(sv); |
78 | SvREADONLY_off(sv); |
06759ea0 |
79 | SvFLAGS(sv) |= (SvFLAGS(sv) & (SVp_NOK|SVp_POK)) >> PRIVSHIFT; |
c07a80fd |
80 | } |
81 | |
954c1994 |
82 | /* |
83 | =for apidoc mg_magical |
84 | |
85 | Turns on the magical status of an SV. See C<sv_magic>. |
86 | |
87 | =cut |
88 | */ |
89 | |
8990e307 |
90 | void |
864dbfa3 |
91 | Perl_mg_magical(pTHX_ SV *sv) |
8990e307 |
92 | { |
93 | MAGIC* mg; |
94 | for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) { |
95 | MGVTBL* vtbl = mg->mg_virtual; |
96 | if (vtbl) { |
2b260de0 |
97 | if (vtbl->svt_get && !(mg->mg_flags & MGf_GSKIP)) |
8990e307 |
98 | SvGMAGICAL_on(sv); |
99 | if (vtbl->svt_set) |
100 | SvSMAGICAL_on(sv); |
2b260de0 |
101 | if (!(SvFLAGS(sv) & (SVs_GMG|SVs_SMG)) || vtbl->svt_clear) |
8990e307 |
102 | SvRMAGICAL_on(sv); |
103 | } |
104 | } |
105 | } |
106 | |
954c1994 |
107 | /* |
108 | =for apidoc mg_get |
109 | |
110 | Do magic after a value is retrieved from the SV. See C<sv_magic>. |
111 | |
112 | =cut |
113 | */ |
114 | |
79072805 |
115 | int |
864dbfa3 |
116 | Perl_mg_get(pTHX_ SV *sv) |
79072805 |
117 | { |
ff76feab |
118 | int new = 0; |
119 | MAGIC *newmg, *head, *cur, *mg; |
120 | I32 mgs_ix = SSNEW(sizeof(MGS)); |
463ee0b2 |
121 | |
455ece5e |
122 | save_magic(mgs_ix, sv); |
463ee0b2 |
123 | |
ff76feab |
124 | /* We must call svt_get(sv, mg) for each valid entry in the linked |
125 | list of magic. svt_get() may delete the current entry, add new |
126 | magic to the head of the list, or upgrade the SV. AMS 20010810 */ |
127 | |
128 | newmg = cur = head = mg = SvMAGIC(sv); |
129 | while (mg) { |
130 | MGVTBL *vtbl = mg->mg_virtual; |
131 | |
2b260de0 |
132 | if (!(mg->mg_flags & MGf_GSKIP) && vtbl && vtbl->svt_get) { |
316ad4fe |
133 | CALL_FPTR(vtbl->svt_get)(aTHX_ sv, mg); |
ff76feab |
134 | /* Don't restore the flags for this entry if it was deleted. */ |
135 | if (mg->mg_flags & MGf_GSKIP) |
136 | (SSPTR(mgs_ix, MGS *))->mgs_flags = 0; |
a0d0e21e |
137 | } |
ff76feab |
138 | |
139 | mg = mg->mg_moremagic; |
140 | |
141 | if (new) { |
142 | /* Have we finished with the new entries we saw? Start again |
143 | where we left off (unless there are more new entries). */ |
144 | if (mg == head) { |
145 | new = 0; |
146 | mg = cur; |
147 | head = newmg; |
148 | } |
149 | } |
150 | |
151 | /* Were any new entries added? */ |
152 | if (!new && (newmg = SvMAGIC(sv)) != head) { |
153 | new = 1; |
154 | cur = mg; |
155 | mg = newmg; |
760ac839 |
156 | } |
79072805 |
157 | } |
463ee0b2 |
158 | |
acfe0abc |
159 | restore_magic(aTHX_ INT2PTR(void *, (IV)mgs_ix)); |
79072805 |
160 | return 0; |
161 | } |
162 | |
954c1994 |
163 | /* |
164 | =for apidoc mg_set |
165 | |
166 | Do magic after a value is assigned to the SV. See C<sv_magic>. |
167 | |
168 | =cut |
169 | */ |
170 | |
79072805 |
171 | int |
864dbfa3 |
172 | Perl_mg_set(pTHX_ SV *sv) |
79072805 |
173 | { |
8fb26106 |
174 | I32 mgs_ix; |
79072805 |
175 | MAGIC* mg; |
463ee0b2 |
176 | MAGIC* nextmg; |
177 | |
455ece5e |
178 | mgs_ix = SSNEW(sizeof(MGS)); |
179 | save_magic(mgs_ix, sv); |
463ee0b2 |
180 | |
181 | for (mg = SvMAGIC(sv); mg; mg = nextmg) { |
79072805 |
182 | MGVTBL* vtbl = mg->mg_virtual; |
463ee0b2 |
183 | nextmg = mg->mg_moremagic; /* it may delete itself */ |
a0d0e21e |
184 | if (mg->mg_flags & MGf_GSKIP) { |
185 | mg->mg_flags &= ~MGf_GSKIP; /* setting requires another read */ |
455ece5e |
186 | (SSPTR(mgs_ix, MGS*))->mgs_flags = 0; |
a0d0e21e |
187 | } |
2b260de0 |
188 | if (vtbl && vtbl->svt_set) |
fc0dc3b3 |
189 | CALL_FPTR(vtbl->svt_set)(aTHX_ sv, mg); |
79072805 |
190 | } |
463ee0b2 |
191 | |
acfe0abc |
192 | restore_magic(aTHX_ INT2PTR(void*, (IV)mgs_ix)); |
79072805 |
193 | return 0; |
194 | } |
195 | |
954c1994 |
196 | /* |
197 | =for apidoc mg_length |
198 | |
199 | Report on the SV's length. See C<sv_magic>. |
200 | |
201 | =cut |
202 | */ |
203 | |
79072805 |
204 | U32 |
864dbfa3 |
205 | Perl_mg_length(pTHX_ SV *sv) |
79072805 |
206 | { |
207 | MAGIC* mg; |
463ee0b2 |
208 | STRLEN len; |
463ee0b2 |
209 | |
79072805 |
210 | for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) { |
211 | MGVTBL* vtbl = mg->mg_virtual; |
2b260de0 |
212 | if (vtbl && vtbl->svt_len) { |
8fb26106 |
213 | I32 mgs_ix; |
48e43a1c |
214 | |
455ece5e |
215 | mgs_ix = SSNEW(sizeof(MGS)); |
216 | save_magic(mgs_ix, sv); |
a0d0e21e |
217 | /* omit MGf_GSKIP -- not changed here */ |
fc0dc3b3 |
218 | len = CALL_FPTR(vtbl->svt_len)(aTHX_ sv, mg); |
acfe0abc |
219 | restore_magic(aTHX_ INT2PTR(void*, (IV)mgs_ix)); |
85e6fe83 |
220 | return len; |
221 | } |
222 | } |
223 | |
7719e241 |
224 | if (DO_UTF8(sv)) |
5636d518 |
225 | { |
226 | U8 *s = (U8*)SvPV(sv, len); |
227 | len = Perl_utf8_length(aTHX_ s, s + len); |
228 | } |
229 | else |
230 | (void)SvPV(sv, len); |
463ee0b2 |
231 | return len; |
79072805 |
232 | } |
233 | |
8fb26106 |
234 | I32 |
864dbfa3 |
235 | Perl_mg_size(pTHX_ SV *sv) |
93965878 |
236 | { |
237 | MAGIC* mg; |
238 | I32 len; |
ac27b0f5 |
239 | |
93965878 |
240 | for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) { |
241 | MGVTBL* vtbl = mg->mg_virtual; |
2b260de0 |
242 | if (vtbl && vtbl->svt_len) { |
8fb26106 |
243 | I32 mgs_ix; |
455ece5e |
244 | |
245 | mgs_ix = SSNEW(sizeof(MGS)); |
246 | save_magic(mgs_ix, sv); |
93965878 |
247 | /* omit MGf_GSKIP -- not changed here */ |
fc0dc3b3 |
248 | len = CALL_FPTR(vtbl->svt_len)(aTHX_ sv, mg); |
acfe0abc |
249 | restore_magic(aTHX_ INT2PTR(void*, (IV)mgs_ix)); |
93965878 |
250 | return len; |
251 | } |
252 | } |
253 | |
254 | switch(SvTYPE(sv)) { |
255 | case SVt_PVAV: |
256 | len = AvFILLp((AV *) sv); /* Fallback to non-tied array */ |
257 | return len; |
258 | case SVt_PVHV: |
259 | /* FIXME */ |
260 | default: |
cea2e8a9 |
261 | Perl_croak(aTHX_ "Size magic not implemented"); |
93965878 |
262 | break; |
263 | } |
264 | return 0; |
265 | } |
266 | |
954c1994 |
267 | /* |
268 | =for apidoc mg_clear |
269 | |
270 | Clear something magical that the SV represents. See C<sv_magic>. |
271 | |
272 | =cut |
273 | */ |
274 | |
79072805 |
275 | int |
864dbfa3 |
276 | Perl_mg_clear(pTHX_ SV *sv) |
79072805 |
277 | { |
8fb26106 |
278 | I32 mgs_ix; |
79072805 |
279 | MAGIC* mg; |
463ee0b2 |
280 | |
455ece5e |
281 | mgs_ix = SSNEW(sizeof(MGS)); |
282 | save_magic(mgs_ix, sv); |
463ee0b2 |
283 | |
79072805 |
284 | for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) { |
285 | MGVTBL* vtbl = mg->mg_virtual; |
a0d0e21e |
286 | /* omit GSKIP -- never set here */ |
727405f8 |
287 | |
2b260de0 |
288 | if (vtbl && vtbl->svt_clear) |
fc0dc3b3 |
289 | CALL_FPTR(vtbl->svt_clear)(aTHX_ sv, mg); |
79072805 |
290 | } |
463ee0b2 |
291 | |
acfe0abc |
292 | restore_magic(aTHX_ INT2PTR(void*, (IV)mgs_ix)); |
79072805 |
293 | return 0; |
294 | } |
295 | |
954c1994 |
296 | /* |
297 | =for apidoc mg_find |
298 | |
299 | Finds the magic pointer for type matching the SV. See C<sv_magic>. |
300 | |
301 | =cut |
302 | */ |
303 | |
93a17b20 |
304 | MAGIC* |
864dbfa3 |
305 | Perl_mg_find(pTHX_ SV *sv, int type) |
93a17b20 |
306 | { |
307 | MAGIC* mg; |
3f8f4626 |
308 | if (!sv) |
309 | return 0; |
93a17b20 |
310 | for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) { |
311 | if (mg->mg_type == type) |
312 | return mg; |
313 | } |
314 | return 0; |
315 | } |
316 | |
954c1994 |
317 | /* |
318 | =for apidoc mg_copy |
319 | |
320 | Copies the magic from one SV to another. See C<sv_magic>. |
321 | |
322 | =cut |
323 | */ |
324 | |
79072805 |
325 | int |
864dbfa3 |
326 | Perl_mg_copy(pTHX_ SV *sv, SV *nsv, const char *key, I32 klen) |
79072805 |
327 | { |
463ee0b2 |
328 | int count = 0; |
79072805 |
329 | MAGIC* mg; |
463ee0b2 |
330 | for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) { |
68795e93 |
331 | MGVTBL* vtbl = mg->mg_virtual; |
332 | if ((mg->mg_flags & MGf_COPY) && vtbl->svt_copy){ |
333 | count += CALL_FPTR(vtbl->svt_copy)(aTHX_ sv, mg, nsv, key, klen); |
334 | } |
335 | else if (isUPPER(mg->mg_type)) { |
33c27489 |
336 | sv_magic(nsv, |
14befaf4 |
337 | mg->mg_type == PERL_MAGIC_tied ? SvTIED_obj(sv, mg) : |
338 | (mg->mg_type == PERL_MAGIC_regdata && mg->mg_obj) |
339 | ? sv : mg->mg_obj, |
33c27489 |
340 | toLOWER(mg->mg_type), key, klen); |
463ee0b2 |
341 | count++; |
79072805 |
342 | } |
79072805 |
343 | } |
463ee0b2 |
344 | return count; |
79072805 |
345 | } |
346 | |
954c1994 |
347 | /* |
348 | =for apidoc mg_free |
349 | |
350 | Free any magic storage used by the SV. See C<sv_magic>. |
351 | |
352 | =cut |
353 | */ |
354 | |
79072805 |
355 | int |
864dbfa3 |
356 | Perl_mg_free(pTHX_ SV *sv) |
79072805 |
357 | { |
358 | MAGIC* mg; |
359 | MAGIC* moremagic; |
360 | for (mg = SvMAGIC(sv); mg; mg = moremagic) { |
361 | MGVTBL* vtbl = mg->mg_virtual; |
362 | moremagic = mg->mg_moremagic; |
2b260de0 |
363 | if (vtbl && vtbl->svt_free) |
fc0dc3b3 |
364 | CALL_FPTR(vtbl->svt_free)(aTHX_ sv, mg); |
14befaf4 |
365 | if (mg->mg_ptr && mg->mg_type != PERL_MAGIC_regex_global) { |
7719e241 |
366 | if (mg->mg_len > 0) |
88e89b8a |
367 | Safefree(mg->mg_ptr); |
565764a8 |
368 | else if (mg->mg_len == HEf_SVKEY) |
88e89b8a |
369 | SvREFCNT_dec((SV*)mg->mg_ptr); |
d460ef45 |
370 | } |
b881518d |
371 | if (mg->mg_flags & MGf_REFCOUNTED) |
372 | SvREFCNT_dec(mg->mg_obj); |
79072805 |
373 | Safefree(mg); |
374 | } |
375 | SvMAGIC(sv) = 0; |
376 | return 0; |
377 | } |
378 | |
d460ef45 |
379 | |
79072805 |
380 | #if !defined(NSIG) || defined(M_UNIX) || defined(M_XENIX) |
381 | #include <signal.h> |
382 | #endif |
383 | |
942e002e |
384 | U32 |
864dbfa3 |
385 | Perl_magic_regdata_cnt(pTHX_ SV *sv, MAGIC *mg) |
6cef1e77 |
386 | { |
6cef1e77 |
387 | register REGEXP *rx; |
6cef1e77 |
388 | |
aaa362c4 |
389 | if (PL_curpm && (rx = PM_GETRE(PL_curpm))) { |
8f580fb8 |
390 | if (mg->mg_obj) /* @+ */ |
391 | return rx->nparens; |
392 | else /* @- */ |
393 | return rx->lastparen; |
394 | } |
ac27b0f5 |
395 | |
942e002e |
396 | return (U32)-1; |
6cef1e77 |
397 | } |
398 | |
399 | int |
864dbfa3 |
400 | Perl_magic_regdatum_get(pTHX_ SV *sv, MAGIC *mg) |
6cef1e77 |
401 | { |
6cef1e77 |
402 | register I32 paren; |
cf93c79d |
403 | register I32 s; |
6cef1e77 |
404 | register I32 i; |
405 | register REGEXP *rx; |
cf93c79d |
406 | I32 t; |
6cef1e77 |
407 | |
aaa362c4 |
408 | if (PL_curpm && (rx = PM_GETRE(PL_curpm))) { |
6cef1e77 |
409 | paren = mg->mg_len; |
410 | if (paren < 0) |
411 | return 0; |
eb160463 |
412 | if (paren <= (I32)rx->nparens && |
cf93c79d |
413 | (s = rx->startp[paren]) != -1 && |
414 | (t = rx->endp[paren]) != -1) |
6cef1e77 |
415 | { |
416 | if (mg->mg_obj) /* @+ */ |
cf93c79d |
417 | i = t; |
6cef1e77 |
418 | else /* @- */ |
cf93c79d |
419 | i = s; |
727405f8 |
420 | |
53c4c00c |
421 | if (i > 0 && PL_reg_match_utf8) { |
1aa99e6b |
422 | char *b = rx->subbeg; |
0064a8a9 |
423 | if (b) |
424 | i = Perl_utf8_length(aTHX_ (U8*)b, (U8*)(b+i)); |
1aa99e6b |
425 | } |
0064a8a9 |
426 | |
427 | sv_setiv(sv, i); |
6cef1e77 |
428 | } |
429 | } |
430 | return 0; |
431 | } |
432 | |
e4b89193 |
433 | int |
a29d06ed |
434 | Perl_magic_regdatum_set(pTHX_ SV *sv, MAGIC *mg) |
435 | { |
a29d06ed |
436 | Perl_croak(aTHX_ PL_no_modify); |
e4b89193 |
437 | /* NOT REACHED */ |
438 | return 0; |
a29d06ed |
439 | } |
440 | |
93a17b20 |
441 | U32 |
864dbfa3 |
442 | Perl_magic_len(pTHX_ SV *sv, MAGIC *mg) |
93a17b20 |
443 | { |
444 | register I32 paren; |
93a17b20 |
445 | register I32 i; |
d9f97599 |
446 | register REGEXP *rx; |
a197cbdd |
447 | I32 s1, t1; |
93a17b20 |
448 | |
449 | switch (*mg->mg_ptr) { |
450 | case '1': case '2': case '3': case '4': |
451 | case '5': case '6': case '7': case '8': case '9': case '&': |
aaa362c4 |
452 | if (PL_curpm && (rx = PM_GETRE(PL_curpm))) { |
cf93c79d |
453 | |
ffc61ed2 |
454 | paren = atoi(mg->mg_ptr); /* $& is in [0] */ |
93a17b20 |
455 | getparen: |
eb160463 |
456 | if (paren <= (I32)rx->nparens && |
cf93c79d |
457 | (s1 = rx->startp[paren]) != -1 && |
458 | (t1 = rx->endp[paren]) != -1) |
bbce6d69 |
459 | { |
cf93c79d |
460 | i = t1 - s1; |
a197cbdd |
461 | getlen: |
53c4c00c |
462 | if (i > 0 && PL_reg_match_utf8) { |
ffc61ed2 |
463 | char *s = rx->subbeg + s1; |
a197cbdd |
464 | char *send = rx->subbeg + t1; |
ffc61ed2 |
465 | |
6d5fa195 |
466 | i = t1 - s1; |
60425c38 |
467 | if (is_utf8_string((U8*)s, i)) |
468 | i = Perl_utf8_length(aTHX_ (U8*)s, (U8*)send); |
a197cbdd |
469 | } |
ffc61ed2 |
470 | if (i < 0) |
0844c848 |
471 | Perl_croak(aTHX_ "panic: magic_len: %"IVdf, (IV)i); |
ffc61ed2 |
472 | return i; |
93a17b20 |
473 | } |
235bddc8 |
474 | else { |
475 | if (ckWARN(WARN_UNINITIALIZED)) |
476 | report_uninit(); |
477 | } |
478 | } |
479 | else { |
480 | if (ckWARN(WARN_UNINITIALIZED)) |
481 | report_uninit(); |
93a17b20 |
482 | } |
748a9306 |
483 | return 0; |
93a17b20 |
484 | case '+': |
aaa362c4 |
485 | if (PL_curpm && (rx = PM_GETRE(PL_curpm))) { |
d9f97599 |
486 | paren = rx->lastparen; |
13f57bf8 |
487 | if (paren) |
488 | goto getparen; |
93a17b20 |
489 | } |
748a9306 |
490 | return 0; |
a01268b5 |
491 | case '\016': /* ^N */ |
492 | if (PL_curpm && (rx = PM_GETRE(PL_curpm))) { |
493 | paren = rx->lastcloseparen; |
494 | if (paren) |
495 | goto getparen; |
496 | } |
497 | return 0; |
93a17b20 |
498 | case '`': |
aaa362c4 |
499 | if (PL_curpm && (rx = PM_GETRE(PL_curpm))) { |
cf93c79d |
500 | if (rx->startp[0] != -1) { |
501 | i = rx->startp[0]; |
a197cbdd |
502 | if (i > 0) { |
503 | s1 = 0; |
504 | t1 = i; |
505 | goto getlen; |
506 | } |
93a17b20 |
507 | } |
93a17b20 |
508 | } |
748a9306 |
509 | return 0; |
93a17b20 |
510 | case '\'': |
aaa362c4 |
511 | if (PL_curpm && (rx = PM_GETRE(PL_curpm))) { |
cf93c79d |
512 | if (rx->endp[0] != -1) { |
513 | i = rx->sublen - rx->endp[0]; |
a197cbdd |
514 | if (i > 0) { |
515 | s1 = rx->endp[0]; |
516 | t1 = rx->sublen; |
517 | goto getlen; |
518 | } |
93a17b20 |
519 | } |
93a17b20 |
520 | } |
748a9306 |
521 | return 0; |
93a17b20 |
522 | } |
523 | magic_get(sv,mg); |
2d8e6c8d |
524 | if (!SvPOK(sv) && SvNIOK(sv)) { |
525 | STRLEN n_a; |
526 | sv_2pv(sv, &n_a); |
527 | } |
93a17b20 |
528 | if (SvPOK(sv)) |
529 | return SvCUR(sv); |
530 | return 0; |
531 | } |
532 | |
79072805 |
533 | int |
864dbfa3 |
534 | Perl_magic_get(pTHX_ SV *sv, MAGIC *mg) |
79072805 |
535 | { |
536 | register I32 paren; |
537 | register char *s; |
538 | register I32 i; |
d9f97599 |
539 | register REGEXP *rx; |
79072805 |
540 | |
541 | switch (*mg->mg_ptr) { |
748a9306 |
542 | case '\001': /* ^A */ |
3280af22 |
543 | sv_setsv(sv, PL_bodytarget); |
748a9306 |
544 | break; |
49460fe6 |
545 | case '\003': /* ^C */ |
546 | sv_setiv(sv, (IV)PL_minus_c); |
547 | break; |
548 | |
79072805 |
549 | case '\004': /* ^D */ |
aea4f609 |
550 | sv_setiv(sv, (IV)(PL_debug & DEBUG_MASK)); |
d056ab3f |
551 | #if defined(YYDEBUG) && defined(DEBUGGING) |
aea4f609 |
552 | PL_yydebug = DEBUG_p_TEST; |
d056ab3f |
553 | #endif |
79072805 |
554 | break; |
28f23441 |
555 | case '\005': /* ^E */ |
0a378802 |
556 | if (*(mg->mg_ptr+1) == '\0') { |
cd39f2b6 |
557 | #ifdef MACOS_TRADITIONAL |
0a378802 |
558 | { |
559 | char msg[256]; |
727405f8 |
560 | |
0a378802 |
561 | sv_setnv(sv,(double)gMacPerl_OSErr); |
727405f8 |
562 | sv_setpv(sv, gMacPerl_OSErr ? GetSysErrText(gMacPerl_OSErr, msg) : ""); |
0a378802 |
563 | } |
727405f8 |
564 | #else |
28f23441 |
565 | #ifdef VMS |
0a378802 |
566 | { |
567 | # include <descrip.h> |
568 | # include <starlet.h> |
569 | char msg[255]; |
570 | $DESCRIPTOR(msgdsc,msg); |
571 | sv_setnv(sv,(NV) vaxc$errno); |
572 | if (sys$getmsg(vaxc$errno,&msgdsc.dsc$w_length,&msgdsc,0,0) & 1) |
573 | sv_setpvn(sv,msgdsc.dsc$a_pointer,msgdsc.dsc$w_length); |
574 | else |
575 | sv_setpv(sv,""); |
576 | } |
28f23441 |
577 | #else |
88e89b8a |
578 | #ifdef OS2 |
0a378802 |
579 | if (!(_emx_env & 0x200)) { /* Under DOS */ |
580 | sv_setnv(sv, (NV)errno); |
581 | sv_setpv(sv, errno ? Strerror(errno) : ""); |
582 | } else { |
583 | if (errno != errno_isOS2) { |
584 | int tmp = _syserrno(); |
585 | if (tmp) /* 2nd call to _syserrno() makes it 0 */ |
586 | Perl_rc = tmp; |
587 | } |
588 | sv_setnv(sv, (NV)Perl_rc); |
589 | sv_setpv(sv, os2error(Perl_rc)); |
590 | } |
88e89b8a |
591 | #else |
22fae026 |
592 | #ifdef WIN32 |
0a378802 |
593 | { |
594 | DWORD dwErr = GetLastError(); |
595 | sv_setnv(sv, (NV)dwErr); |
596 | if (dwErr) |
597 | { |
598 | PerlProc_GetOSError(sv, dwErr); |
599 | } |
600 | else |
601 | sv_setpv(sv, ""); |
602 | SetLastError(dwErr); |
603 | } |
22fae026 |
604 | #else |
0a378802 |
605 | sv_setnv(sv, (NV)errno); |
606 | sv_setpv(sv, errno ? Strerror(errno) : ""); |
28f23441 |
607 | #endif |
88e89b8a |
608 | #endif |
22fae026 |
609 | #endif |
cd39f2b6 |
610 | #endif |
0a378802 |
611 | SvNOK_on(sv); /* what a wonderful hack! */ |
612 | } |
613 | else if (strEQ(mg->mg_ptr+1, "NCODING")) |
614 | sv_setsv(sv, PL_encoding); |
615 | break; |
79072805 |
616 | case '\006': /* ^F */ |
3280af22 |
617 | sv_setiv(sv, (IV)PL_maxsysfd); |
79072805 |
618 | break; |
a0d0e21e |
619 | case '\010': /* ^H */ |
3280af22 |
620 | sv_setiv(sv, (IV)PL_hints); |
a0d0e21e |
621 | break; |
9d116dd7 |
622 | case '\011': /* ^I */ /* NOT \t in EBCDIC */ |
3280af22 |
623 | if (PL_inplace) |
624 | sv_setpv(sv, PL_inplace); |
79072805 |
625 | else |
3280af22 |
626 | sv_setsv(sv, &PL_sv_undef); |
79072805 |
627 | break; |
ac27b0f5 |
628 | case '\017': /* ^O & ^OPEN */ |
629 | if (*(mg->mg_ptr+1) == '\0') |
630 | sv_setpv(sv, PL_osname); |
631 | else if (strEQ(mg->mg_ptr, "\017PEN")) { |
632 | if (!PL_compiling.cop_io) |
633 | sv_setsv(sv, &PL_sv_undef); |
634 | else { |
635 | sv_setsv(sv, PL_compiling.cop_io); |
636 | } |
637 | } |
28f23441 |
638 | break; |
79072805 |
639 | case '\020': /* ^P */ |
3280af22 |
640 | sv_setiv(sv, (IV)PL_perldb); |
79072805 |
641 | break; |
fb73857a |
642 | case '\023': /* ^S */ |
d58bf5aa |
643 | { |
3280af22 |
644 | if (PL_lex_state != LEX_NOTPARSING) |
155aba94 |
645 | (void)SvOK_off(sv); |
3280af22 |
646 | else if (PL_in_eval) |
6dc8a9e4 |
647 | sv_setiv(sv, PL_in_eval & ~(EVAL_INREQUIRE)); |
a4268c0a |
648 | else |
649 | sv_setiv(sv, 0); |
d58bf5aa |
650 | } |
fb73857a |
651 | break; |
79072805 |
652 | case '\024': /* ^T */ |
7c36658b |
653 | if (*(mg->mg_ptr+1) == '\0') { |
88e89b8a |
654 | #ifdef BIG_TIME |
7c36658b |
655 | sv_setnv(sv, PL_basetime); |
88e89b8a |
656 | #else |
7c36658b |
657 | sv_setiv(sv, (IV)PL_basetime); |
88e89b8a |
658 | #endif |
7c36658b |
659 | } |
660 | else if (strEQ(mg->mg_ptr, "\024AINT")) |
661 | sv_setiv(sv, PL_tainting); |
662 | break; |
46487f74 |
663 | case '\027': /* ^W & $^WARNING_BITS & ^WIDE_SYSTEM_CALLS */ |
4438c4b7 |
664 | if (*(mg->mg_ptr+1) == '\0') |
665 | sv_setiv(sv, (IV)((PL_dowarn & G_WARN_ON) ? TRUE : FALSE)); |
0a378802 |
666 | else if (strEQ(mg->mg_ptr+1, "ARNING_BITS")) { |
d3a7d8c7 |
667 | if (PL_compiling.cop_warnings == pWARN_NONE || |
668 | PL_compiling.cop_warnings == pWARN_STD) |
4438c4b7 |
669 | { |
670 | sv_setpvn(sv, WARN_NONEstring, WARNsize) ; |
671 | } |
d3a7d8c7 |
672 | else if (PL_compiling.cop_warnings == pWARN_ALL) { |
4438c4b7 |
673 | sv_setpvn(sv, WARN_ALLstring, WARNsize) ; |
ac27b0f5 |
674 | } |
4438c4b7 |
675 | else { |
676 | sv_setsv(sv, PL_compiling.cop_warnings); |
ac27b0f5 |
677 | } |
d3a7d8c7 |
678 | SvPOK_only(sv); |
4438c4b7 |
679 | } |
0a378802 |
680 | else if (strEQ(mg->mg_ptr+1, "IDE_SYSTEM_CALLS")) |
46487f74 |
681 | sv_setiv(sv, (IV)PL_widesyscalls); |
79072805 |
682 | break; |
683 | case '1': case '2': case '3': case '4': |
684 | case '5': case '6': case '7': case '8': case '9': case '&': |
aaa362c4 |
685 | if (PL_curpm && (rx = PM_GETRE(PL_curpm))) { |
cf93c79d |
686 | I32 s1, t1; |
687 | |
a863c7d1 |
688 | /* |
689 | * Pre-threads, this was paren = atoi(GvENAME((GV*)mg->mg_obj)); |
690 | * XXX Does the new way break anything? |
691 | */ |
ffc61ed2 |
692 | paren = atoi(mg->mg_ptr); /* $& is in [0] */ |
79072805 |
693 | getparen: |
eb160463 |
694 | if (paren <= (I32)rx->nparens && |
cf93c79d |
695 | (s1 = rx->startp[paren]) != -1 && |
696 | (t1 = rx->endp[paren]) != -1) |
bbce6d69 |
697 | { |
cf93c79d |
698 | i = t1 - s1; |
699 | s = rx->subbeg + s1; |
01ec43d0 |
700 | if (!rx->subbeg) |
c2e66d9e |
701 | break; |
702 | |
13f57bf8 |
703 | getrx: |
748a9306 |
704 | if (i >= 0) { |
cf93c79d |
705 | sv_setpvn(sv, s, i); |
a8c7c11a |
706 | if (PL_reg_match_utf8 && is_utf8_string((U8*)s, i)) |
7e2040f0 |
707 | SvUTF8_on(sv); |
708 | else |
709 | SvUTF8_off(sv); |
e9814ee1 |
710 | if (PL_tainting) { |
711 | if (RX_MATCH_TAINTED(rx)) { |
712 | MAGIC* mg = SvMAGIC(sv); |
713 | MAGIC* mgt; |
714 | PL_tainted = 1; |
715 | SvMAGIC(sv) = mg->mg_moremagic; |
716 | SvTAINT(sv); |
717 | if ((mgt = SvMAGIC(sv))) { |
718 | mg->mg_moremagic = mgt; |
719 | SvMAGIC(sv) = mg; |
720 | } |
721 | } else |
722 | SvTAINTED_off(sv); |
723 | } |
748a9306 |
724 | break; |
725 | } |
79072805 |
726 | } |
79072805 |
727 | } |
3280af22 |
728 | sv_setsv(sv,&PL_sv_undef); |
79072805 |
729 | break; |
730 | case '+': |
aaa362c4 |
731 | if (PL_curpm && (rx = PM_GETRE(PL_curpm))) { |
d9f97599 |
732 | paren = rx->lastparen; |
a0d0e21e |
733 | if (paren) |
734 | goto getparen; |
79072805 |
735 | } |
3280af22 |
736 | sv_setsv(sv,&PL_sv_undef); |
79072805 |
737 | break; |
a01268b5 |
738 | case '\016': /* ^N */ |
739 | if (PL_curpm && (rx = PM_GETRE(PL_curpm))) { |
740 | paren = rx->lastcloseparen; |
741 | if (paren) |
742 | goto getparen; |
743 | } |
744 | sv_setsv(sv,&PL_sv_undef); |
745 | break; |
79072805 |
746 | case '`': |
aaa362c4 |
747 | if (PL_curpm && (rx = PM_GETRE(PL_curpm))) { |
cf93c79d |
748 | if ((s = rx->subbeg) && rx->startp[0] != -1) { |
749 | i = rx->startp[0]; |
13f57bf8 |
750 | goto getrx; |
79072805 |
751 | } |
79072805 |
752 | } |
3280af22 |
753 | sv_setsv(sv,&PL_sv_undef); |
79072805 |
754 | break; |
755 | case '\'': |
aaa362c4 |
756 | if (PL_curpm && (rx = PM_GETRE(PL_curpm))) { |
cf93c79d |
757 | if (rx->subbeg && rx->endp[0] != -1) { |
758 | s = rx->subbeg + rx->endp[0]; |
759 | i = rx->sublen - rx->endp[0]; |
13f57bf8 |
760 | goto getrx; |
79072805 |
761 | } |
79072805 |
762 | } |
3280af22 |
763 | sv_setsv(sv,&PL_sv_undef); |
79072805 |
764 | break; |
765 | case '.': |
766 | #ifndef lint |
3280af22 |
767 | if (GvIO(PL_last_in_gv)) { |
357c8808 |
768 | sv_setiv(sv, (IV)IoLINES(GvIOp(PL_last_in_gv))); |
79072805 |
769 | } |
770 | #endif |
771 | break; |
772 | case '?': |
809a5acc |
773 | { |
809a5acc |
774 | sv_setiv(sv, (IV)STATUS_CURRENT); |
ff0cee69 |
775 | #ifdef COMPLEX_STATUS |
6b88bc9c |
776 | LvTARGOFF(sv) = PL_statusvalue; |
777 | LvTARGLEN(sv) = PL_statusvalue_vms; |
ff0cee69 |
778 | #endif |
809a5acc |
779 | } |
79072805 |
780 | break; |
781 | case '^': |
3280af22 |
782 | s = IoTOP_NAME(GvIOp(PL_defoutgv)); |
79072805 |
783 | if (s) |
784 | sv_setpv(sv,s); |
785 | else { |
3280af22 |
786 | sv_setpv(sv,GvENAME(PL_defoutgv)); |
79072805 |
787 | sv_catpv(sv,"_TOP"); |
788 | } |
789 | break; |
790 | case '~': |
3280af22 |
791 | s = IoFMT_NAME(GvIOp(PL_defoutgv)); |
79072805 |
792 | if (!s) |
3280af22 |
793 | s = GvENAME(PL_defoutgv); |
79072805 |
794 | sv_setpv(sv,s); |
795 | break; |
796 | #ifndef lint |
797 | case '=': |
3280af22 |
798 | sv_setiv(sv, (IV)IoPAGE_LEN(GvIOp(PL_defoutgv))); |
79072805 |
799 | break; |
800 | case '-': |
3280af22 |
801 | sv_setiv(sv, (IV)IoLINES_LEFT(GvIOp(PL_defoutgv))); |
79072805 |
802 | break; |
803 | case '%': |
3280af22 |
804 | sv_setiv(sv, (IV)IoPAGE(GvIOp(PL_defoutgv))); |
79072805 |
805 | break; |
806 | #endif |
807 | case ':': |
808 | break; |
809 | case '/': |
810 | break; |
811 | case '[': |
3280af22 |
812 | WITH_THR(sv_setiv(sv, (IV)PL_curcop->cop_arybase)); |
79072805 |
813 | break; |
814 | case '|': |
3280af22 |
815 | sv_setiv(sv, (IV)(IoFLAGS(GvIOp(PL_defoutgv)) & IOf_FLUSH) != 0 ); |
79072805 |
816 | break; |
817 | case ',': |
79072805 |
818 | break; |
819 | case '\\': |
b2ce0fda |
820 | if (PL_ors_sv) |
f28098ff |
821 | sv_copypv(sv, PL_ors_sv); |
79072805 |
822 | break; |
823 | case '#': |
3280af22 |
824 | sv_setpv(sv,PL_ofmt); |
79072805 |
825 | break; |
826 | case '!': |
a5f75d66 |
827 | #ifdef VMS |
65202027 |
828 | sv_setnv(sv, (NV)((errno == EVMSERR) ? vaxc$errno : errno)); |
88e89b8a |
829 | sv_setpv(sv, errno ? Strerror(errno) : ""); |
a5f75d66 |
830 | #else |
88e89b8a |
831 | { |
832 | int saveerrno = errno; |
65202027 |
833 | sv_setnv(sv, (NV)errno); |
88e89b8a |
834 | #ifdef OS2 |
ed344e4f |
835 | if (errno == errno_isOS2 || errno == errno_isOS2_set) |
836 | sv_setpv(sv, os2error(Perl_rc)); |
88e89b8a |
837 | else |
a5f75d66 |
838 | #endif |
2304df62 |
839 | sv_setpv(sv, errno ? Strerror(errno) : ""); |
88e89b8a |
840 | errno = saveerrno; |
841 | } |
842 | #endif |
946ec16e |
843 | SvNOK_on(sv); /* what a wonderful hack! */ |
79072805 |
844 | break; |
845 | case '<': |
3280af22 |
846 | sv_setiv(sv, (IV)PL_uid); |
79072805 |
847 | break; |
848 | case '>': |
3280af22 |
849 | sv_setiv(sv, (IV)PL_euid); |
79072805 |
850 | break; |
851 | case '(': |
3280af22 |
852 | sv_setiv(sv, (IV)PL_gid); |
a52cb5f7 |
853 | #ifdef HAS_GETGROUPS |
785fb66b |
854 | Perl_sv_setpvf(aTHX_ sv, "%"Gid_t_f, PL_gid); |
a52cb5f7 |
855 | #endif |
79072805 |
856 | goto add_groups; |
857 | case ')': |
3280af22 |
858 | sv_setiv(sv, (IV)PL_egid); |
a52cb5f7 |
859 | #ifdef HAS_GETGROUPS |
785fb66b |
860 | Perl_sv_setpvf(aTHX_ sv, "%"Gid_t_f, PL_egid); |
a52cb5f7 |
861 | #endif |
79072805 |
862 | add_groups: |
79072805 |
863 | #ifdef HAS_GETGROUPS |
79072805 |
864 | { |
a0d0e21e |
865 | Groups_t gary[NGROUPS]; |
79072805 |
866 | i = getgroups(NGROUPS,gary); |
46fc3d4c |
867 | while (--i >= 0) |
785fb66b |
868 | Perl_sv_catpvf(aTHX_ sv, " %"Gid_t_f, gary[i]); |
79072805 |
869 | } |
870 | #endif |
155aba94 |
871 | (void)SvIOK_on(sv); /* what a wonderful hack! */ |
79072805 |
872 | break; |
873 | case '*': |
874 | break; |
cd39f2b6 |
875 | #ifndef MACOS_TRADITIONAL |
79072805 |
876 | case '0': |
877 | break; |
cd39f2b6 |
878 | #endif |
79072805 |
879 | } |
a0d0e21e |
880 | return 0; |
79072805 |
881 | } |
882 | |
883 | int |
864dbfa3 |
884 | Perl_magic_getuvar(pTHX_ SV *sv, MAGIC *mg) |
79072805 |
885 | { |
886 | struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr; |
887 | |
888 | if (uf && uf->uf_val) |
24f81a43 |
889 | (*uf->uf_val)(aTHX_ uf->uf_index, sv); |
79072805 |
890 | return 0; |
891 | } |
892 | |
893 | int |
864dbfa3 |
894 | Perl_magic_setenv(pTHX_ SV *sv, MAGIC *mg) |
79072805 |
895 | { |
896 | register char *s; |
88e89b8a |
897 | char *ptr; |
5aabfad6 |
898 | STRLEN len, klen; |
1e422769 |
899 | |
a0d0e21e |
900 | s = SvPV(sv,len); |
5aabfad6 |
901 | ptr = MgPV(mg,klen); |
88e89b8a |
902 | my_setenv(ptr, s); |
1e422769 |
903 | |
a0d0e21e |
904 | #ifdef DYNAMIC_ENV_FETCH |
905 | /* We just undefd an environment var. Is a replacement */ |
906 | /* waiting in the wings? */ |
907 | if (!len) { |
5aabfad6 |
908 | SV **valp; |
6b88bc9c |
909 | if ((valp = hv_fetch(GvHVn(PL_envgv), ptr, klen, FALSE))) |
5aabfad6 |
910 | s = SvPV(*valp, len); |
a0d0e21e |
911 | } |
912 | #endif |
1e422769 |
913 | |
39e571d4 |
914 | #if !defined(OS2) && !defined(AMIGAOS) && !defined(WIN32) && !defined(MSDOS) |
79072805 |
915 | /* And you'll never guess what the dog had */ |
916 | /* in its mouth... */ |
3280af22 |
917 | if (PL_tainting) { |
1e422769 |
918 | MgTAINTEDDIR_off(mg); |
919 | #ifdef VMS |
5aabfad6 |
920 | if (s && klen == 8 && strEQ(ptr, "DCL$PATH")) { |
1e422769 |
921 | char pathbuf[256], eltbuf[256], *cp, *elt = s; |
c623ac67 |
922 | Stat_t sbuf; |
1e422769 |
923 | int i = 0, j = 0; |
924 | |
925 | do { /* DCL$PATH may be a search list */ |
926 | while (1) { /* as may dev portion of any element */ |
927 | if ( ((cp = strchr(elt,'[')) || (cp = strchr(elt,'<'))) ) { |
928 | if ( *(cp+1) == '.' || *(cp+1) == '-' || |
929 | cando_by_name(S_IWUSR,0,elt) ) { |
930 | MgTAINTEDDIR_on(mg); |
931 | return 0; |
932 | } |
933 | } |
934 | if ((cp = strchr(elt, ':')) != Nullch) |
935 | *cp = '\0'; |
936 | if (my_trnlnm(elt, eltbuf, j++)) |
937 | elt = eltbuf; |
938 | else |
939 | break; |
940 | } |
941 | j = 0; |
942 | } while (my_trnlnm(s, pathbuf, i++) && (elt = pathbuf)); |
943 | } |
944 | #endif /* VMS */ |
5aabfad6 |
945 | if (s && klen == 4 && strEQ(ptr,"PATH")) { |
a0d0e21e |
946 | char *strend = s + len; |
463ee0b2 |
947 | |
948 | while (s < strend) { |
96827780 |
949 | char tmpbuf[256]; |
c623ac67 |
950 | Stat_t st; |
5f74f29c |
951 | I32 i; |
96827780 |
952 | s = delimcpy(tmpbuf, tmpbuf + sizeof tmpbuf, |
fc36a67e |
953 | s, strend, ':', &i); |
463ee0b2 |
954 | s++; |
96827780 |
955 | if (i >= sizeof tmpbuf /* too long -- assume the worst */ |
956 | || *tmpbuf != '/' |
c6ed36e1 |
957 | || (PerlLIO_stat(tmpbuf, &st) == 0 && (st.st_mode & 2)) ) { |
8990e307 |
958 | MgTAINTEDDIR_on(mg); |
1e422769 |
959 | return 0; |
960 | } |
463ee0b2 |
961 | } |
79072805 |
962 | } |
963 | } |
39e571d4 |
964 | #endif /* neither OS2 nor AMIGAOS nor WIN32 nor MSDOS */ |
1e422769 |
965 | |
79072805 |
966 | return 0; |
967 | } |
968 | |
969 | int |
864dbfa3 |
970 | Perl_magic_clearenv(pTHX_ SV *sv, MAGIC *mg) |
85e6fe83 |
971 | { |
2d8e6c8d |
972 | STRLEN n_a; |
973 | my_setenv(MgPV(mg,n_a),Nullch); |
85e6fe83 |
974 | return 0; |
975 | } |
976 | |
88e89b8a |
977 | int |
864dbfa3 |
978 | Perl_magic_set_all_env(pTHX_ SV *sv, MAGIC *mg) |
fb73857a |
979 | { |
980 | #if defined(VMS) |
cea2e8a9 |
981 | Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system"); |
fb73857a |
982 | #else |
3280af22 |
983 | if (PL_localizing) { |
fb73857a |
984 | HE* entry; |
2d8e6c8d |
985 | STRLEN n_a; |
fb73857a |
986 | magic_clear_all_env(sv,mg); |
987 | hv_iterinit((HV*)sv); |
155aba94 |
988 | while ((entry = hv_iternext((HV*)sv))) { |
fb73857a |
989 | I32 keylen; |
990 | my_setenv(hv_iterkey(entry, &keylen), |
2d8e6c8d |
991 | SvPV(hv_iterval((HV*)sv, entry), n_a)); |
fb73857a |
992 | } |
993 | } |
994 | #endif |
995 | return 0; |
996 | } |
997 | |
998 | int |
864dbfa3 |
999 | Perl_magic_clear_all_env(pTHX_ SV *sv, MAGIC *mg) |
66b1d557 |
1000 | { |
ed79a026 |
1001 | #if defined(VMS) || defined(EPOC) |
cea2e8a9 |
1002 | Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system"); |
3e3baf6d |
1003 | #else |
4efc5df6 |
1004 | # if defined(PERL_IMPLICIT_SYS) || defined(WIN32) |
7766f137 |
1005 | PerlEnv_clearenv(); |
4efc5df6 |
1006 | # else |
1007 | # ifdef USE_ENVIRON_ARRAY |
1008 | # if defined(USE_ITHREADS) |
1009 | /* only the parent thread can clobber the process environment */ |
1010 | if (PL_curinterp == aTHX) |
1011 | # endif |
1012 | { |
1013 | # ifndef PERL_USE_SAFE_PUTENV |
66b1d557 |
1014 | I32 i; |
1015 | |
3280af22 |
1016 | if (environ == PL_origenviron) |
f2517201 |
1017 | environ = (char**)safesysmalloc(sizeof(char*)); |
66b1d557 |
1018 | else |
1019 | for (i = 0; environ[i]; i++) |
f2517201 |
1020 | safesysfree(environ[i]); |
4efc5df6 |
1021 | # endif /* PERL_USE_SAFE_PUTENV */ |
f2517201 |
1022 | |
66b1d557 |
1023 | environ[0] = Nullch; |
4efc5df6 |
1024 | } |
1025 | # endif /* USE_ENVIRON_ARRAY */ |
052c2dc6 |
1026 | # endif /* PERL_IMPLICIT_SYS || WIN32 */ |
1027 | #endif /* VMS || EPC */ |
3e3baf6d |
1028 | return 0; |
66b1d557 |
1029 | } |
1030 | |
2e34cc90 |
1031 | #if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS)||defined(FAKE_DEFAULT_SIGNAL_HANDLERS) |
1032 | static int sig_handlers_initted = 0; |
1033 | #endif |
7719e241 |
1034 | #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS |
85b332e2 |
1035 | static int sig_ignoring[SIG_SIZE]; /* which signals we are ignoring */ |
1036 | #endif |
2e34cc90 |
1037 | #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS |
1038 | static int sig_defaulting[SIG_SIZE]; |
1039 | #endif |
85b332e2 |
1040 | |
64ca3a65 |
1041 | #ifndef PERL_MICRO |
66b1d557 |
1042 | int |
864dbfa3 |
1043 | Perl_magic_getsig(pTHX_ SV *sv, MAGIC *mg) |
88e89b8a |
1044 | { |
1045 | I32 i; |
2d8e6c8d |
1046 | STRLEN n_a; |
88e89b8a |
1047 | /* Are we fetching a signal entry? */ |
2d8e6c8d |
1048 | i = whichsig(MgPV(mg,n_a)); |
88e89b8a |
1049 | if (i) { |
22c35a8c |
1050 | if(PL_psig_ptr[i]) |
1051 | sv_setsv(sv,PL_psig_ptr[i]); |
88e89b8a |
1052 | else { |
85b332e2 |
1053 | Sighandler_t sigstate; |
2e34cc90 |
1054 | sigstate = rsignal_state(i); |
23ada85b |
1055 | #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS |
2e34cc90 |
1056 | if (sig_handlers_initted && sig_ignoring[i]) sigstate = SIG_IGN; |
1057 | #endif |
1058 | #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS |
1059 | if (sig_handlers_initted && sig_defaulting[i]) sigstate = SIG_DFL; |
85b332e2 |
1060 | #endif |
88e89b8a |
1061 | /* cache state so we don't fetch it again */ |
ff68c719 |
1062 | if(sigstate == SIG_IGN) |
88e89b8a |
1063 | sv_setpv(sv,"IGNORE"); |
1064 | else |
3280af22 |
1065 | sv_setsv(sv,&PL_sv_undef); |
22c35a8c |
1066 | PL_psig_ptr[i] = SvREFCNT_inc(sv); |
88e89b8a |
1067 | SvTEMP_off(sv); |
1068 | } |
1069 | } |
1070 | return 0; |
1071 | } |
1072 | int |
864dbfa3 |
1073 | Perl_magic_clearsig(pTHX_ SV *sv, MAGIC *mg) |
88e89b8a |
1074 | { |
1075 | I32 i; |
2d8e6c8d |
1076 | STRLEN n_a; |
88e89b8a |
1077 | /* Are we clearing a signal entry? */ |
2d8e6c8d |
1078 | i = whichsig(MgPV(mg,n_a)); |
88e89b8a |
1079 | if (i) { |
22c35a8c |
1080 | if(PL_psig_ptr[i]) { |
1081 | SvREFCNT_dec(PL_psig_ptr[i]); |
1082 | PL_psig_ptr[i]=0; |
88e89b8a |
1083 | } |
22c35a8c |
1084 | if(PL_psig_name[i]) { |
1085 | SvREFCNT_dec(PL_psig_name[i]); |
1086 | PL_psig_name[i]=0; |
88e89b8a |
1087 | } |
1088 | } |
1089 | return 0; |
1090 | } |
3d37d572 |
1091 | |
0a8e0eff |
1092 | void |
1093 | Perl_raise_signal(pTHX_ int sig) |
1094 | { |
1095 | /* Set a flag to say this signal is pending */ |
1096 | PL_psig_pend[sig]++; |
1097 | /* And one to say _a_ signal is pending */ |
1098 | PL_sig_pending = 1; |
1099 | } |
1100 | |
1101 | Signal_t |
1102 | Perl_csighandler(int sig) |
1103 | { |
1018e26f |
1104 | #ifdef PERL_GET_SIG_CONTEXT |
1105 | dTHXa(PERL_GET_SIG_CONTEXT); |
1106 | #else |
85b332e2 |
1107 | dTHX; |
1108 | #endif |
23ada85b |
1109 | #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS |
85b332e2 |
1110 | (void) rsignal(sig, &Perl_csighandler); |
1111 | if (sig_ignoring[sig]) return; |
1112 | #endif |
2e34cc90 |
1113 | #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS |
7719e241 |
1114 | if (sig_defaulting[sig]) |
2e34cc90 |
1115 | #ifdef KILL_BY_SIGPRC |
1116 | exit((Perl_sig_to_vmscondition(sig)&STS$M_COND_ID)|STS$K_SEVERE|STS$M_INHIB_MSG); |
1117 | #else |
1118 | exit(1); |
1119 | #endif |
1120 | #endif |
1121 | |
0a8e0eff |
1122 | #ifdef PERL_OLD_SIGNALS |
1123 | /* Call the perl level handler now with risk we may be in malloc() etc. */ |
1124 | (*PL_sighandlerp)(sig); |
1125 | #else |
0a8e0eff |
1126 | Perl_raise_signal(aTHX_ sig); |
1127 | #endif |
1128 | } |
1129 | |
2e34cc90 |
1130 | #if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS) || defined(FAKE_DEFAULT_SIGNAL_HANDLERS) |
1131 | void |
1132 | Perl_csighandler_init(void) |
1133 | { |
1134 | int sig; |
1135 | if (sig_handlers_initted) return; |
1136 | |
1137 | for (sig = 1; sig < SIG_SIZE; sig++) { |
1138 | #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS |
218fdd94 |
1139 | dTHX; |
2e34cc90 |
1140 | sig_defaulting[sig] = 1; |
1141 | (void) rsignal(sig, &Perl_csighandler); |
1142 | #endif |
1143 | #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS |
1144 | sig_ignoring[sig] = 0; |
1145 | #endif |
1146 | } |
1147 | sig_handlers_initted = 1; |
1148 | } |
1149 | #endif |
1150 | |
0a8e0eff |
1151 | void |
1152 | Perl_despatch_signals(pTHX) |
1153 | { |
1154 | int sig; |
1155 | PL_sig_pending = 0; |
1156 | for (sig = 1; sig < SIG_SIZE; sig++) { |
1157 | if (PL_psig_pend[sig]) { |
1158 | PL_psig_pend[sig] = 0; |
1159 | (*PL_sighandlerp)(sig); |
1160 | } |
1161 | } |
1162 | } |
1163 | |
85e6fe83 |
1164 | int |
864dbfa3 |
1165 | Perl_magic_setsig(pTHX_ SV *sv, MAGIC *mg) |
79072805 |
1166 | { |
1167 | register char *s; |
1168 | I32 i; |
b7953727 |
1169 | SV** svp = 0; |
e72dc28c |
1170 | STRLEN len; |
a0d0e21e |
1171 | |
e72dc28c |
1172 | s = MgPV(mg,len); |
748a9306 |
1173 | if (*s == '_') { |
1174 | if (strEQ(s,"__DIE__")) |
3280af22 |
1175 | svp = &PL_diehook; |
748a9306 |
1176 | else if (strEQ(s,"__WARN__")) |
3280af22 |
1177 | svp = &PL_warnhook; |
748a9306 |
1178 | else |
cea2e8a9 |
1179 | Perl_croak(aTHX_ "No such hook: %s", s); |
748a9306 |
1180 | i = 0; |
4633a7c4 |
1181 | if (*svp) { |
1182 | SvREFCNT_dec(*svp); |
1183 | *svp = 0; |
1184 | } |
748a9306 |
1185 | } |
1186 | else { |
1187 | i = whichsig(s); /* ...no, a brick */ |
1188 | if (!i) { |
e476b1b5 |
1189 | if (ckWARN(WARN_SIGNAL)) |
9014280d |
1190 | Perl_warner(aTHX_ packWARN(WARN_SIGNAL), "No such signal: SIG%s", s); |
748a9306 |
1191 | return 0; |
1192 | } |
2e34cc90 |
1193 | #if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS) || defined(FAKE_DEFAULT_SIGNAL_HANDLERS) |
1194 | if (!sig_handlers_initted) Perl_csighandler_init(); |
1195 | #endif |
23ada85b |
1196 | #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS |
85b332e2 |
1197 | sig_ignoring[i] = 0; |
1198 | #endif |
2e34cc90 |
1199 | #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS |
1200 | sig_defaulting[i] = 0; |
1201 | #endif |
22c35a8c |
1202 | SvREFCNT_dec(PL_psig_name[i]); |
1203 | SvREFCNT_dec(PL_psig_ptr[i]); |
1204 | PL_psig_ptr[i] = SvREFCNT_inc(sv); |
88e89b8a |
1205 | SvTEMP_off(sv); /* Make sure it doesn't go away on us */ |
e72dc28c |
1206 | PL_psig_name[i] = newSVpvn(s, len); |
22c35a8c |
1207 | SvREADONLY_on(PL_psig_name[i]); |
748a9306 |
1208 | } |
a0d0e21e |
1209 | if (SvTYPE(sv) == SVt_PVGV || SvROK(sv)) { |
748a9306 |
1210 | if (i) |
0a8e0eff |
1211 | (void)rsignal(i, &Perl_csighandler); |
748a9306 |
1212 | else |
1213 | *svp = SvREFCNT_inc(sv); |
a0d0e21e |
1214 | return 0; |
1215 | } |
e72dc28c |
1216 | s = SvPV_force(sv,len); |
748a9306 |
1217 | if (strEQ(s,"IGNORE")) { |
85b332e2 |
1218 | if (i) { |
23ada85b |
1219 | #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS |
85b332e2 |
1220 | sig_ignoring[i] = 1; |
1221 | (void)rsignal(i, &Perl_csighandler); |
1222 | #else |
ff68c719 |
1223 | (void)rsignal(i, SIG_IGN); |
85b332e2 |
1224 | #endif |
1225 | } else |
748a9306 |
1226 | *svp = 0; |
1227 | } |
1228 | else if (strEQ(s,"DEFAULT") || !*s) { |
1229 | if (i) |
2e34cc90 |
1230 | #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS |
1231 | { |
1232 | sig_defaulting[i] = 1; |
1233 | (void)rsignal(i, &Perl_csighandler); |
1234 | } |
1235 | #else |
ff68c719 |
1236 | (void)rsignal(i, SIG_DFL); |
2e34cc90 |
1237 | #endif |
748a9306 |
1238 | else |
1239 | *svp = 0; |
1240 | } |
79072805 |
1241 | else { |
5aabfad6 |
1242 | /* |
1243 | * We should warn if HINT_STRICT_REFS, but without |
1244 | * access to a known hint bit in a known OP, we can't |
1245 | * tell whether HINT_STRICT_REFS is in force or not. |
1246 | */ |
46fc3d4c |
1247 | if (!strchr(s,':') && !strchr(s,'\'')) |
e72dc28c |
1248 | sv_insert(sv, 0, 0, "main::", 6); |
748a9306 |
1249 | if (i) |
0a8e0eff |
1250 | (void)rsignal(i, &Perl_csighandler); |
748a9306 |
1251 | else |
1252 | *svp = SvREFCNT_inc(sv); |
79072805 |
1253 | } |
1254 | return 0; |
1255 | } |
64ca3a65 |
1256 | #endif /* !PERL_MICRO */ |
79072805 |
1257 | |
1258 | int |
864dbfa3 |
1259 | Perl_magic_setisa(pTHX_ SV *sv, MAGIC *mg) |
79072805 |
1260 | { |
3280af22 |
1261 | PL_sub_generation++; |
463ee0b2 |
1262 | return 0; |
1263 | } |
1264 | |
1265 | int |
864dbfa3 |
1266 | Perl_magic_setamagic(pTHX_ SV *sv, MAGIC *mg) |
463ee0b2 |
1267 | { |
a0d0e21e |
1268 | /* HV_badAMAGIC_on(Sv_STASH(sv)); */ |
3280af22 |
1269 | PL_amagic_generation++; |
463ee0b2 |
1270 | |
a0d0e21e |
1271 | return 0; |
1272 | } |
463ee0b2 |
1273 | |
946ec16e |
1274 | int |
864dbfa3 |
1275 | Perl_magic_getnkeys(pTHX_ SV *sv, MAGIC *mg) |
6ff81951 |
1276 | { |
1277 | HV *hv = (HV*)LvTARG(sv); |
6ff81951 |
1278 | I32 i = 0; |
7719e241 |
1279 | |
6ff81951 |
1280 | if (hv) { |
497b47a8 |
1281 | (void) hv_iterinit(hv); |
1282 | if (! SvTIED_mg((SV*)hv, PERL_MAGIC_tied)) |
1283 | i = HvKEYS(hv); |
1284 | else { |
1285 | while (hv_iternext(hv)) |
1286 | i++; |
1287 | } |
6ff81951 |
1288 | } |
1289 | |
1290 | sv_setiv(sv, (IV)i); |
1291 | return 0; |
1292 | } |
1293 | |
1294 | int |
864dbfa3 |
1295 | Perl_magic_setnkeys(pTHX_ SV *sv, MAGIC *mg) |
946ec16e |
1296 | { |
1297 | if (LvTARG(sv)) { |
1298 | hv_ksplit((HV*)LvTARG(sv), SvIV(sv)); |
946ec16e |
1299 | } |
1300 | return 0; |
ac27b0f5 |
1301 | } |
946ec16e |
1302 | |
e336de0d |
1303 | /* caller is responsible for stack switching/cleanup */ |
565764a8 |
1304 | STATIC int |
cea2e8a9 |
1305 | S_magic_methcall(pTHX_ SV *sv, MAGIC *mg, char *meth, I32 flags, int n, SV *val) |
a0d0e21e |
1306 | { |
1307 | dSP; |
463ee0b2 |
1308 | |
924508f0 |
1309 | PUSHMARK(SP); |
1310 | EXTEND(SP, n); |
33c27489 |
1311 | PUSHs(SvTIED_obj(sv, mg)); |
ac27b0f5 |
1312 | if (n > 1) { |
93965878 |
1313 | if (mg->mg_ptr) { |
565764a8 |
1314 | if (mg->mg_len >= 0) |
79cb57f6 |
1315 | PUSHs(sv_2mortal(newSVpvn(mg->mg_ptr, mg->mg_len))); |
565764a8 |
1316 | else if (mg->mg_len == HEf_SVKEY) |
93965878 |
1317 | PUSHs((SV*)mg->mg_ptr); |
1318 | } |
14befaf4 |
1319 | else if (mg->mg_type == PERL_MAGIC_tiedelem) { |
565764a8 |
1320 | PUSHs(sv_2mortal(newSViv(mg->mg_len))); |
93965878 |
1321 | } |
1322 | } |
1323 | if (n > 2) { |
1324 | PUSHs(val); |
88e89b8a |
1325 | } |
463ee0b2 |
1326 | PUTBACK; |
1327 | |
864dbfa3 |
1328 | return call_method(meth, flags); |
946ec16e |
1329 | } |
1330 | |
76e3520e |
1331 | STATIC int |
cea2e8a9 |
1332 | S_magic_methpack(pTHX_ SV *sv, MAGIC *mg, char *meth) |
a0d0e21e |
1333 | { |
1334 | dSP; |
463ee0b2 |
1335 | |
a0d0e21e |
1336 | ENTER; |
1337 | SAVETMPS; |
e788e7d3 |
1338 | PUSHSTACKi(PERLSI_MAGIC); |
463ee0b2 |
1339 | |
33c27489 |
1340 | if (magic_methcall(sv, mg, meth, G_SCALAR, 2, NULL)) { |
3280af22 |
1341 | sv_setsv(sv, *PL_stack_sp--); |
93965878 |
1342 | } |
463ee0b2 |
1343 | |
d3acc0f7 |
1344 | POPSTACK; |
a0d0e21e |
1345 | FREETMPS; |
1346 | LEAVE; |
1347 | return 0; |
1348 | } |
463ee0b2 |
1349 | |
a0d0e21e |
1350 | int |
864dbfa3 |
1351 | Perl_magic_getpack(pTHX_ SV *sv, MAGIC *mg) |
a0d0e21e |
1352 | { |
1353 | magic_methpack(sv,mg,"FETCH"); |
1354 | if (mg->mg_ptr) |
1355 | mg->mg_flags |= MGf_GSKIP; |
463ee0b2 |
1356 | return 0; |
1357 | } |
1358 | |
1359 | int |
864dbfa3 |
1360 | Perl_magic_setpack(pTHX_ SV *sv, MAGIC *mg) |
e336de0d |
1361 | { |
1362 | dSP; |
a60c0954 |
1363 | ENTER; |
e788e7d3 |
1364 | PUSHSTACKi(PERLSI_MAGIC); |
33c27489 |
1365 | magic_methcall(sv, mg, "STORE", G_SCALAR|G_DISCARD, 3, sv); |
d3acc0f7 |
1366 | POPSTACK; |
a60c0954 |
1367 | LEAVE; |
463ee0b2 |
1368 | return 0; |
1369 | } |
1370 | |
1371 | int |
864dbfa3 |
1372 | Perl_magic_clearpack(pTHX_ SV *sv, MAGIC *mg) |
463ee0b2 |
1373 | { |
a0d0e21e |
1374 | return magic_methpack(sv,mg,"DELETE"); |
1375 | } |
463ee0b2 |
1376 | |
93965878 |
1377 | |
1378 | U32 |
864dbfa3 |
1379 | Perl_magic_sizepack(pTHX_ SV *sv, MAGIC *mg) |
ac27b0f5 |
1380 | { |
e336de0d |
1381 | dSP; |
93965878 |
1382 | U32 retval = 0; |
1383 | |
1384 | ENTER; |
1385 | SAVETMPS; |
e788e7d3 |
1386 | PUSHSTACKi(PERLSI_MAGIC); |
33c27489 |
1387 | if (magic_methcall(sv, mg, "FETCHSIZE", G_SCALAR, 2, NULL)) { |
3280af22 |
1388 | sv = *PL_stack_sp--; |
a60c0954 |
1389 | retval = (U32) SvIV(sv)-1; |
93965878 |
1390 | } |
d3acc0f7 |
1391 | POPSTACK; |
93965878 |
1392 | FREETMPS; |
1393 | LEAVE; |
1394 | return retval; |
1395 | } |
1396 | |
cea2e8a9 |
1397 | int |
1398 | Perl_magic_wipepack(pTHX_ SV *sv, MAGIC *mg) |
a0d0e21e |
1399 | { |
1400 | dSP; |
463ee0b2 |
1401 | |
e336de0d |
1402 | ENTER; |
e788e7d3 |
1403 | PUSHSTACKi(PERLSI_MAGIC); |
924508f0 |
1404 | PUSHMARK(SP); |
33c27489 |
1405 | XPUSHs(SvTIED_obj(sv, mg)); |
463ee0b2 |
1406 | PUTBACK; |
864dbfa3 |
1407 | call_method("CLEAR", G_SCALAR|G_DISCARD); |
d3acc0f7 |
1408 | POPSTACK; |
a60c0954 |
1409 | LEAVE; |
463ee0b2 |
1410 | return 0; |
1411 | } |
1412 | |
1413 | int |
864dbfa3 |
1414 | Perl_magic_nextpack(pTHX_ SV *sv, MAGIC *mg, SV *key) |
463ee0b2 |
1415 | { |
463ee0b2 |
1416 | dSP; |
dff6d3cd |
1417 | const char *meth = SvOK(key) ? "NEXTKEY" : "FIRSTKEY"; |
463ee0b2 |
1418 | |
1419 | ENTER; |
a0d0e21e |
1420 | SAVETMPS; |
e788e7d3 |
1421 | PUSHSTACKi(PERLSI_MAGIC); |
924508f0 |
1422 | PUSHMARK(SP); |
1423 | EXTEND(SP, 2); |
33c27489 |
1424 | PUSHs(SvTIED_obj(sv, mg)); |
463ee0b2 |
1425 | if (SvOK(key)) |
1426 | PUSHs(key); |
1427 | PUTBACK; |
1428 | |
864dbfa3 |
1429 | if (call_method(meth, G_SCALAR)) |
3280af22 |
1430 | sv_setsv(key, *PL_stack_sp--); |
463ee0b2 |
1431 | |
d3acc0f7 |
1432 | POPSTACK; |
a0d0e21e |
1433 | FREETMPS; |
1434 | LEAVE; |
79072805 |
1435 | return 0; |
1436 | } |
1437 | |
1438 | int |
864dbfa3 |
1439 | Perl_magic_existspack(pTHX_ SV *sv, MAGIC *mg) |
a0d0e21e |
1440 | { |
1441 | return magic_methpack(sv,mg,"EXISTS"); |
ac27b0f5 |
1442 | } |
a0d0e21e |
1443 | |
1444 | int |
864dbfa3 |
1445 | Perl_magic_setdbline(pTHX_ SV *sv, MAGIC *mg) |
79072805 |
1446 | { |
1447 | OP *o; |
1448 | I32 i; |
1449 | GV* gv; |
1450 | SV** svp; |
2d8e6c8d |
1451 | STRLEN n_a; |
79072805 |
1452 | |
3280af22 |
1453 | gv = PL_DBline; |
79072805 |
1454 | i = SvTRUE(sv); |
188ea221 |
1455 | svp = av_fetch(GvAV(gv), |
2d8e6c8d |
1456 | atoi(MgPV(mg,n_a)), FALSE); |
57b2e452 |
1457 | if (svp && SvIOKp(*svp) && (o = INT2PTR(OP*,SvIVX(*svp)))) |
eb160463 |
1458 | o->op_private = (U8)i; |
79072805 |
1459 | return 0; |
1460 | } |
1461 | |
1462 | int |
864dbfa3 |
1463 | Perl_magic_getarylen(pTHX_ SV *sv, MAGIC *mg) |
79072805 |
1464 | { |
3280af22 |
1465 | sv_setiv(sv, AvFILL((AV*)mg->mg_obj) + PL_curcop->cop_arybase); |
79072805 |
1466 | return 0; |
1467 | } |
1468 | |
1469 | int |
864dbfa3 |
1470 | Perl_magic_setarylen(pTHX_ SV *sv, MAGIC *mg) |
79072805 |
1471 | { |
3280af22 |
1472 | av_fill((AV*)mg->mg_obj, SvIV(sv) - PL_curcop->cop_arybase); |
a0d0e21e |
1473 | return 0; |
1474 | } |
1475 | |
1476 | int |
864dbfa3 |
1477 | Perl_magic_getpos(pTHX_ SV *sv, MAGIC *mg) |
a0d0e21e |
1478 | { |
1479 | SV* lsv = LvTARG(sv); |
ac27b0f5 |
1480 | |
a0d0e21e |
1481 | if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv)) { |
14befaf4 |
1482 | mg = mg_find(lsv, PERL_MAGIC_regex_global); |
565764a8 |
1483 | if (mg && mg->mg_len >= 0) { |
a0ed51b3 |
1484 | I32 i = mg->mg_len; |
7e2040f0 |
1485 | if (DO_UTF8(lsv)) |
a0ed51b3 |
1486 | sv_pos_b2u(lsv, &i); |
1487 | sv_setiv(sv, i + PL_curcop->cop_arybase); |
a0d0e21e |
1488 | return 0; |
1489 | } |
1490 | } |
1491 | (void)SvOK_off(sv); |
1492 | return 0; |
1493 | } |
1494 | |
1495 | int |
864dbfa3 |
1496 | Perl_magic_setpos(pTHX_ SV *sv, MAGIC *mg) |
a0d0e21e |
1497 | { |
1498 | SV* lsv = LvTARG(sv); |
1499 | SSize_t pos; |
1500 | STRLEN len; |
c00206c8 |
1501 | STRLEN ulen = 0; |
a0d0e21e |
1502 | |
1503 | mg = 0; |
ac27b0f5 |
1504 | |
a0d0e21e |
1505 | if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv)) |
14befaf4 |
1506 | mg = mg_find(lsv, PERL_MAGIC_regex_global); |
a0d0e21e |
1507 | if (!mg) { |
1508 | if (!SvOK(sv)) |
1509 | return 0; |
14befaf4 |
1510 | sv_magic(lsv, (SV*)0, PERL_MAGIC_regex_global, Nullch, 0); |
1511 | mg = mg_find(lsv, PERL_MAGIC_regex_global); |
a0d0e21e |
1512 | } |
1513 | else if (!SvOK(sv)) { |
565764a8 |
1514 | mg->mg_len = -1; |
a0d0e21e |
1515 | return 0; |
1516 | } |
1517 | len = SvPOK(lsv) ? SvCUR(lsv) : sv_len(lsv); |
1518 | |
c485e607 |
1519 | pos = SvIV(sv) - PL_curcop->cop_arybase; |
a0ed51b3 |
1520 | |
7e2040f0 |
1521 | if (DO_UTF8(lsv)) { |
a0ed51b3 |
1522 | ulen = sv_len_utf8(lsv); |
1523 | if (ulen) |
1524 | len = ulen; |
a0ed51b3 |
1525 | } |
1526 | |
a0d0e21e |
1527 | if (pos < 0) { |
1528 | pos += len; |
1529 | if (pos < 0) |
1530 | pos = 0; |
1531 | } |
eb160463 |
1532 | else if (pos > (SSize_t)len) |
a0d0e21e |
1533 | pos = len; |
a0ed51b3 |
1534 | |
1535 | if (ulen) { |
1536 | I32 p = pos; |
1537 | sv_pos_u2b(lsv, &p, 0); |
1538 | pos = p; |
1539 | } |
727405f8 |
1540 | |
565764a8 |
1541 | mg->mg_len = pos; |
71be2cbc |
1542 | mg->mg_flags &= ~MGf_MINMATCH; |
a0d0e21e |
1543 | |
79072805 |
1544 | return 0; |
1545 | } |
1546 | |
1547 | int |
864dbfa3 |
1548 | Perl_magic_getglob(pTHX_ SV *sv, MAGIC *mg) |
79072805 |
1549 | { |
8646b087 |
1550 | if (SvFAKE(sv)) { /* FAKE globs can get coerced */ |
1551 | SvFAKE_off(sv); |
946ec16e |
1552 | gv_efullname3(sv,((GV*)sv), "*"); |
8646b087 |
1553 | SvFAKE_on(sv); |
1554 | } |
1555 | else |
946ec16e |
1556 | gv_efullname3(sv,((GV*)sv), "*"); /* a gv value, be nice */ |
79072805 |
1557 | return 0; |
1558 | } |
1559 | |
1560 | int |
864dbfa3 |
1561 | Perl_magic_setglob(pTHX_ SV *sv, MAGIC *mg) |
79072805 |
1562 | { |
1563 | register char *s; |
1564 | GV* gv; |
2d8e6c8d |
1565 | STRLEN n_a; |
79072805 |
1566 | |
1567 | if (!SvOK(sv)) |
1568 | return 0; |
2d8e6c8d |
1569 | s = SvPV(sv, n_a); |
79072805 |
1570 | if (*s == '*' && s[1]) |
1571 | s++; |
85e6fe83 |
1572 | gv = gv_fetchpv(s,TRUE, SVt_PVGV); |
79072805 |
1573 | if (sv == (SV*)gv) |
1574 | return 0; |
1575 | if (GvGP(sv)) |
88e89b8a |
1576 | gp_free((GV*)sv); |
79072805 |
1577 | GvGP(sv) = gp_ref(GvGP(gv)); |
79072805 |
1578 | return 0; |
1579 | } |
1580 | |
1581 | int |
864dbfa3 |
1582 | Perl_magic_getsubstr(pTHX_ SV *sv, MAGIC *mg) |
6ff81951 |
1583 | { |
1584 | STRLEN len; |
1585 | SV *lsv = LvTARG(sv); |
1586 | char *tmps = SvPV(lsv,len); |
1587 | I32 offs = LvTARGOFF(sv); |
1588 | I32 rem = LvTARGLEN(sv); |
1589 | |
9aa983d2 |
1590 | if (SvUTF8(lsv)) |
1591 | sv_pos_u2b(lsv, &offs, &rem); |
eb160463 |
1592 | if (offs > (I32)len) |
6ff81951 |
1593 | offs = len; |
eb160463 |
1594 | if (rem + offs > (I32)len) |
6ff81951 |
1595 | rem = len - offs; |
1596 | sv_setpvn(sv, tmps + offs, (STRLEN)rem); |
9aa983d2 |
1597 | if (SvUTF8(lsv)) |
2ef4b674 |
1598 | SvUTF8_on(sv); |
6ff81951 |
1599 | return 0; |
1600 | } |
1601 | |
1602 | int |
864dbfa3 |
1603 | Perl_magic_setsubstr(pTHX_ SV *sv, MAGIC *mg) |
79072805 |
1604 | { |
9aa983d2 |
1605 | STRLEN len; |
1606 | char *tmps = SvPV(sv, len); |
1607 | SV *lsv = LvTARG(sv); |
1608 | I32 lvoff = LvTARGOFF(sv); |
1609 | I32 lvlen = LvTARGLEN(sv); |
075a4a2b |
1610 | |
1aa99e6b |
1611 | if (DO_UTF8(sv)) { |
9aa983d2 |
1612 | sv_utf8_upgrade(lsv); |
1613 | sv_pos_u2b(lsv, &lvoff, &lvlen); |
1614 | sv_insert(lsv, lvoff, lvlen, tmps, len); |
1615 | SvUTF8_on(lsv); |
1616 | } |
9bf12eaf |
1617 | else if (lsv && SvUTF8(lsv)) { |
9aa983d2 |
1618 | sv_pos_u2b(lsv, &lvoff, &lvlen); |
e95af362 |
1619 | tmps = (char*)bytes_to_utf8((U8*)tmps, &len); |
9aa983d2 |
1620 | sv_insert(lsv, lvoff, lvlen, tmps, len); |
1621 | Safefree(tmps); |
1aa99e6b |
1622 | } |
1623 | else |
9aa983d2 |
1624 | sv_insert(lsv, lvoff, lvlen, tmps, len); |
1aa99e6b |
1625 | |
79072805 |
1626 | return 0; |
1627 | } |
1628 | |
1629 | int |
864dbfa3 |
1630 | Perl_magic_gettaint(pTHX_ SV *sv, MAGIC *mg) |
463ee0b2 |
1631 | { |
565764a8 |
1632 | TAINT_IF((mg->mg_len & 1) || |
155aba94 |
1633 | ((mg->mg_len & 2) && mg->mg_obj == sv)); /* kludge */ |
463ee0b2 |
1634 | return 0; |
1635 | } |
1636 | |
1637 | int |
864dbfa3 |
1638 | Perl_magic_settaint(pTHX_ SV *sv, MAGIC *mg) |
463ee0b2 |
1639 | { |
3280af22 |
1640 | if (PL_localizing) { |
1641 | if (PL_localizing == 1) |
565764a8 |
1642 | mg->mg_len <<= 1; |
748a9306 |
1643 | else |
565764a8 |
1644 | mg->mg_len >>= 1; |
a0d0e21e |
1645 | } |
3280af22 |
1646 | else if (PL_tainted) |
565764a8 |
1647 | mg->mg_len |= 1; |
748a9306 |
1648 | else |
565764a8 |
1649 | mg->mg_len &= ~1; |
463ee0b2 |
1650 | return 0; |
1651 | } |
1652 | |
1653 | int |
864dbfa3 |
1654 | Perl_magic_getvec(pTHX_ SV *sv, MAGIC *mg) |
6ff81951 |
1655 | { |
1656 | SV *lsv = LvTARG(sv); |
6ff81951 |
1657 | |
1658 | if (!lsv) { |
155aba94 |
1659 | (void)SvOK_off(sv); |
6ff81951 |
1660 | return 0; |
1661 | } |
6ff81951 |
1662 | |
81e118e0 |
1663 | sv_setuv(sv, do_vecget(lsv, LvTARGOFF(sv), LvTARGLEN(sv))); |
6ff81951 |
1664 | return 0; |
1665 | } |
1666 | |
1667 | int |
864dbfa3 |
1668 | Perl_magic_setvec(pTHX_ SV *sv, MAGIC *mg) |
79072805 |
1669 | { |
1670 | do_vecset(sv); /* XXX slurp this routine */ |
1671 | return 0; |
1672 | } |
1673 | |
1674 | int |
864dbfa3 |
1675 | Perl_magic_getdefelem(pTHX_ SV *sv, MAGIC *mg) |
5f05dabc |
1676 | { |
71be2cbc |
1677 | SV *targ = Nullsv; |
5f05dabc |
1678 | if (LvTARGLEN(sv)) { |
68dc0745 |
1679 | if (mg->mg_obj) { |
74e13ce4 |
1680 | SV *ahv = LvTARG(sv); |
6d822dc4 |
1681 | HE *he = hv_fetch_ent((HV*)ahv, mg->mg_obj, FALSE, 0); |
1682 | if (he) |
1683 | targ = HeVAL(he); |
68dc0745 |
1684 | } |
1685 | else { |
3c78fafa |
1686 | AV* av = (AV*)LvTARG(sv); |
68dc0745 |
1687 | if ((I32)LvTARGOFF(sv) <= AvFILL(av)) |
1688 | targ = AvARRAY(av)[LvTARGOFF(sv)]; |
1689 | } |
3280af22 |
1690 | if (targ && targ != &PL_sv_undef) { |
68dc0745 |
1691 | /* somebody else defined it for us */ |
1692 | SvREFCNT_dec(LvTARG(sv)); |
1693 | LvTARG(sv) = SvREFCNT_inc(targ); |
1694 | LvTARGLEN(sv) = 0; |
1695 | SvREFCNT_dec(mg->mg_obj); |
1696 | mg->mg_obj = Nullsv; |
1697 | mg->mg_flags &= ~MGf_REFCOUNTED; |
1698 | } |
5f05dabc |
1699 | } |
71be2cbc |
1700 | else |
1701 | targ = LvTARG(sv); |
3280af22 |
1702 | sv_setsv(sv, targ ? targ : &PL_sv_undef); |
71be2cbc |
1703 | return 0; |
1704 | } |
1705 | |
1706 | int |
864dbfa3 |
1707 | Perl_magic_setdefelem(pTHX_ SV *sv, MAGIC *mg) |
71be2cbc |
1708 | { |
1709 | if (LvTARGLEN(sv)) |
68dc0745 |
1710 | vivify_defelem(sv); |
1711 | if (LvTARG(sv)) { |
5f05dabc |
1712 | sv_setsv(LvTARG(sv), sv); |
68dc0745 |
1713 | SvSETMAGIC(LvTARG(sv)); |
1714 | } |
5f05dabc |
1715 | return 0; |
1716 | } |
1717 | |
71be2cbc |
1718 | void |
864dbfa3 |
1719 | Perl_vivify_defelem(pTHX_ SV *sv) |
71be2cbc |
1720 | { |
74e13ce4 |
1721 | MAGIC *mg; |
1722 | SV *value = Nullsv; |
71be2cbc |
1723 | |
14befaf4 |
1724 | if (!LvTARGLEN(sv) || !(mg = mg_find(sv, PERL_MAGIC_defelem))) |
71be2cbc |
1725 | return; |
68dc0745 |
1726 | if (mg->mg_obj) { |
74e13ce4 |
1727 | SV *ahv = LvTARG(sv); |
2d8e6c8d |
1728 | STRLEN n_a; |
6d822dc4 |
1729 | HE *he = hv_fetch_ent((HV*)ahv, mg->mg_obj, TRUE, 0); |
1730 | if (he) |
1731 | value = HeVAL(he); |
3280af22 |
1732 | if (!value || value == &PL_sv_undef) |
cea2e8a9 |
1733 | Perl_croak(aTHX_ PL_no_helem, SvPV(mg->mg_obj, n_a)); |
71be2cbc |
1734 | } |
68dc0745 |
1735 | else { |
1736 | AV* av = (AV*)LvTARG(sv); |
5aabfad6 |
1737 | if ((I32)LvTARGLEN(sv) < 0 && (I32)LvTARGOFF(sv) > AvFILL(av)) |
68dc0745 |
1738 | LvTARG(sv) = Nullsv; /* array can't be extended */ |
1739 | else { |
1740 | SV** svp = av_fetch(av, LvTARGOFF(sv), TRUE); |
3280af22 |
1741 | if (!svp || (value = *svp) == &PL_sv_undef) |
cea2e8a9 |
1742 | Perl_croak(aTHX_ PL_no_aelem, (I32)LvTARGOFF(sv)); |
68dc0745 |
1743 | } |
1744 | } |
3e3baf6d |
1745 | (void)SvREFCNT_inc(value); |
68dc0745 |
1746 | SvREFCNT_dec(LvTARG(sv)); |
1747 | LvTARG(sv) = value; |
71be2cbc |
1748 | LvTARGLEN(sv) = 0; |
68dc0745 |
1749 | SvREFCNT_dec(mg->mg_obj); |
1750 | mg->mg_obj = Nullsv; |
1751 | mg->mg_flags &= ~MGf_REFCOUNTED; |
5f05dabc |
1752 | } |
1753 | |
1754 | int |
864dbfa3 |
1755 | Perl_magic_killbackrefs(pTHX_ SV *sv, MAGIC *mg) |
810b8aa5 |
1756 | { |
1757 | AV *av = (AV*)mg->mg_obj; |
1758 | SV **svp = AvARRAY(av); |
1759 | I32 i = AvFILLp(av); |
1760 | while (i >= 0) { |
1761 | if (svp[i] && svp[i] != &PL_sv_undef) { |
1762 | if (!SvWEAKREF(svp[i])) |
cea2e8a9 |
1763 | Perl_croak(aTHX_ "panic: magic_killbackrefs"); |
810b8aa5 |
1764 | /* XXX Should we check that it hasn't changed? */ |
1765 | SvRV(svp[i]) = 0; |
155aba94 |
1766 | (void)SvOK_off(svp[i]); |
810b8aa5 |
1767 | SvWEAKREF_off(svp[i]); |
1768 | svp[i] = &PL_sv_undef; |
1769 | } |
1770 | i--; |
1771 | } |
1772 | return 0; |
1773 | } |
1774 | |
1775 | int |
864dbfa3 |
1776 | Perl_magic_setmglob(pTHX_ SV *sv, MAGIC *mg) |
93a17b20 |
1777 | { |
565764a8 |
1778 | mg->mg_len = -1; |
c6496cc7 |
1779 | SvSCREAM_off(sv); |
93a17b20 |
1780 | return 0; |
1781 | } |
1782 | |
1783 | int |
864dbfa3 |
1784 | Perl_magic_setbm(pTHX_ SV *sv, MAGIC *mg) |
79072805 |
1785 | { |
14befaf4 |
1786 | sv_unmagic(sv, PERL_MAGIC_bm); |
79072805 |
1787 | SvVALID_off(sv); |
1788 | return 0; |
1789 | } |
1790 | |
1791 | int |
864dbfa3 |
1792 | Perl_magic_setfm(pTHX_ SV *sv, MAGIC *mg) |
55497cff |
1793 | { |
14befaf4 |
1794 | sv_unmagic(sv, PERL_MAGIC_fm); |
55497cff |
1795 | SvCOMPILED_off(sv); |
1796 | return 0; |
1797 | } |
1798 | |
1799 | int |
864dbfa3 |
1800 | Perl_magic_setuvar(pTHX_ SV *sv, MAGIC *mg) |
79072805 |
1801 | { |
1802 | struct ufuncs *uf = (struct ufuncs *)mg->mg_ptr; |
1803 | |
1804 | if (uf && uf->uf_set) |
24f81a43 |
1805 | (*uf->uf_set)(aTHX_ uf->uf_index, sv); |
79072805 |
1806 | return 0; |
1807 | } |
1808 | |
c277df42 |
1809 | int |
864dbfa3 |
1810 | Perl_magic_freeregexp(pTHX_ SV *sv, MAGIC *mg) |
c277df42 |
1811 | { |
1812 | regexp *re = (regexp *)mg->mg_obj; |
1813 | ReREFCNT_dec(re); |
1814 | return 0; |
1815 | } |
1816 | |
7a4c00b4 |
1817 | #ifdef USE_LOCALE_COLLATE |
79072805 |
1818 | int |
864dbfa3 |
1819 | Perl_magic_setcollxfrm(pTHX_ SV *sv, MAGIC *mg) |
bbce6d69 |
1820 | { |
1821 | /* |
838b5b74 |
1822 | * RenE<eacute> Descartes said "I think not." |
bbce6d69 |
1823 | * and vanished with a faint plop. |
1824 | */ |
7a4c00b4 |
1825 | if (mg->mg_ptr) { |
1826 | Safefree(mg->mg_ptr); |
1827 | mg->mg_ptr = NULL; |
565764a8 |
1828 | mg->mg_len = -1; |
7a4c00b4 |
1829 | } |
bbce6d69 |
1830 | return 0; |
1831 | } |
7a4c00b4 |
1832 | #endif /* USE_LOCALE_COLLATE */ |
bbce6d69 |
1833 | |
1834 | int |
864dbfa3 |
1835 | Perl_magic_set(pTHX_ SV *sv, MAGIC *mg) |
79072805 |
1836 | { |
1837 | register char *s; |
1838 | I32 i; |
8990e307 |
1839 | STRLEN len; |
79072805 |
1840 | switch (*mg->mg_ptr) { |
748a9306 |
1841 | case '\001': /* ^A */ |
3280af22 |
1842 | sv_setsv(PL_bodytarget, sv); |
748a9306 |
1843 | break; |
49460fe6 |
1844 | case '\003': /* ^C */ |
eb160463 |
1845 | PL_minus_c = (bool)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)); |
49460fe6 |
1846 | break; |
1847 | |
79072805 |
1848 | case '\004': /* ^D */ |
aea4f609 |
1849 | PL_debug = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) | DEBUG_TOP_FLAG; |
79072805 |
1850 | DEBUG_x(dump_all()); |
1851 | break; |
28f23441 |
1852 | case '\005': /* ^E */ |
d0063567 |
1853 | if (*(mg->mg_ptr+1) == '\0') { |
cd39f2b6 |
1854 | #ifdef MACOS_TRADITIONAL |
d0063567 |
1855 | gMacPerl_OSErr = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv); |
28f23441 |
1856 | #else |
cd39f2b6 |
1857 | # ifdef VMS |
d0063567 |
1858 | set_vaxc_errno(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)); |
048c1ddf |
1859 | # else |
cd39f2b6 |
1860 | # ifdef WIN32 |
d0063567 |
1861 | SetLastError( SvIV(sv) ); |
cd39f2b6 |
1862 | # else |
9fed8b87 |
1863 | # ifdef OS2 |
d0063567 |
1864 | os2_setsyserrno(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)); |
9fed8b87 |
1865 | # else |
d0063567 |
1866 | /* will anyone ever use this? */ |
1867 | SETERRNO(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv), 4); |
cd39f2b6 |
1868 | # endif |
048c1ddf |
1869 | # endif |
1870 | # endif |
22fae026 |
1871 | #endif |
d0063567 |
1872 | } |
1873 | else if (strEQ(mg->mg_ptr+1, "NCODING")) { |
1874 | if (PL_encoding) |
1875 | SvREFCNT_dec(PL_encoding); |
1876 | if (SvOK(sv) || SvGMAGICAL(sv)) { |
1877 | PL_encoding = newSVsv(sv); |
1878 | } |
1879 | else { |
1880 | PL_encoding = Nullsv; |
1881 | } |
1882 | } |
1883 | break; |
79072805 |
1884 | case '\006': /* ^F */ |
3280af22 |
1885 | PL_maxsysfd = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv); |
79072805 |
1886 | break; |
a0d0e21e |
1887 | case '\010': /* ^H */ |
3280af22 |
1888 | PL_hints = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv); |
a0d0e21e |
1889 | break; |
9d116dd7 |
1890 | case '\011': /* ^I */ /* NOT \t in EBCDIC */ |
3280af22 |
1891 | if (PL_inplace) |
1892 | Safefree(PL_inplace); |
79072805 |
1893 | if (SvOK(sv)) |
2d8e6c8d |
1894 | PL_inplace = savepv(SvPV(sv,len)); |
79072805 |
1895 | else |
3280af22 |
1896 | PL_inplace = Nullch; |
79072805 |
1897 | break; |
28f23441 |
1898 | case '\017': /* ^O */ |
ac27b0f5 |
1899 | if (*(mg->mg_ptr+1) == '\0') { |
1900 | if (PL_osname) |
1901 | Safefree(PL_osname); |
1902 | if (SvOK(sv)) |
1903 | PL_osname = savepv(SvPV(sv,len)); |
1904 | else |
1905 | PL_osname = Nullch; |
1906 | } |
1907 | else if (strEQ(mg->mg_ptr, "\017PEN")) { |
1908 | if (!PL_compiling.cop_io) |
1909 | PL_compiling.cop_io = newSVsv(sv); |
1910 | else |
1911 | sv_setsv(PL_compiling.cop_io,sv); |
1912 | } |
28f23441 |
1913 | break; |
79072805 |
1914 | case '\020': /* ^P */ |
3280af22 |
1915 | PL_perldb = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv); |
1ee4443e |
1916 | if (PL_perldb && !PL_DBsingle) |
1917 | init_debugger(); |
79072805 |
1918 | break; |
1919 | case '\024': /* ^T */ |
88e89b8a |
1920 | #ifdef BIG_TIME |
6b88bc9c |
1921 | PL_basetime = (Time_t)(SvNOK(sv) ? SvNVX(sv) : sv_2nv(sv)); |
88e89b8a |
1922 | #else |
3280af22 |
1923 | PL_basetime = (Time_t)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)); |
88e89b8a |
1924 | #endif |
79072805 |
1925 | break; |
46487f74 |
1926 | case '\027': /* ^W & $^WARNING_BITS & ^WIDE_SYSTEM_CALLS */ |
4438c4b7 |
1927 | if (*(mg->mg_ptr+1) == '\0') { |
1928 | if ( ! (PL_dowarn & G_WARN_ALL_MASK)) { |
1929 | i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv); |
ac27b0f5 |
1930 | PL_dowarn = (PL_dowarn & ~G_WARN_ON) |
0453d815 |
1931 | | (i ? G_WARN_ON : G_WARN_OFF) ; |
4438c4b7 |
1932 | } |
599cee73 |
1933 | } |
0a378802 |
1934 | else if (strEQ(mg->mg_ptr+1, "ARNING_BITS")) { |
4438c4b7 |
1935 | if ( ! (PL_dowarn & G_WARN_ALL_MASK)) { |
d775746e |
1936 | if (!SvPOK(sv) && PL_localizing) { |
1937 | sv_setpvn(sv, WARN_NONEstring, WARNsize); |
d3a7d8c7 |
1938 | PL_compiling.cop_warnings = pWARN_NONE; |
d775746e |
1939 | break; |
1940 | } |
f4fc7782 |
1941 | { |
b5477537 |
1942 | STRLEN len, i; |
d3a7d8c7 |
1943 | int accumulate = 0 ; |
f4fc7782 |
1944 | int any_fatals = 0 ; |
d3a7d8c7 |
1945 | char * ptr = (char*)SvPV(sv, len) ; |
f4fc7782 |
1946 | for (i = 0 ; i < len ; ++i) { |
1947 | accumulate |= ptr[i] ; |
1948 | any_fatals |= (ptr[i] & 0xAA) ; |
1949 | } |
d3a7d8c7 |
1950 | if (!accumulate) |
1951 | PL_compiling.cop_warnings = pWARN_NONE; |
f4fc7782 |
1952 | else if (isWARN_on(sv, WARN_ALL) && !any_fatals) { |
1953 | PL_compiling.cop_warnings = pWARN_ALL; |
1954 | PL_dowarn |= G_WARN_ONCE ; |
727405f8 |
1955 | } |
d3a7d8c7 |
1956 | else { |
1957 | if (specialWARN(PL_compiling.cop_warnings)) |
1958 | PL_compiling.cop_warnings = newSVsv(sv) ; |
1959 | else |
1960 | sv_setsv(PL_compiling.cop_warnings, sv); |
1961 | if (isWARN_on(PL_compiling.cop_warnings, WARN_ONCE)) |
1962 | PL_dowarn |= G_WARN_ONCE ; |
1963 | } |
f4fc7782 |
1964 | |
d3a7d8c7 |
1965 | } |
4438c4b7 |
1966 | } |
971a9dd3 |
1967 | } |
0a378802 |
1968 | else if (strEQ(mg->mg_ptr+1, "IDE_SYSTEM_CALLS")) |
8a31060d |
1969 | PL_widesyscalls = (bool)SvTRUE(sv); |
79072805 |
1970 | break; |
1971 | case '.': |
3280af22 |
1972 | if (PL_localizing) { |
1973 | if (PL_localizing == 1) |
7766f137 |
1974 | SAVESPTR(PL_last_in_gv); |
748a9306 |
1975 | } |
3280af22 |
1976 | else if (SvOK(sv) && GvIO(PL_last_in_gv)) |
632db599 |
1977 | IoLINES(GvIOp(PL_last_in_gv)) = SvIV(sv); |
79072805 |
1978 | break; |
1979 | case '^': |
3280af22 |
1980 | Safefree(IoTOP_NAME(GvIOp(PL_defoutgv))); |
2d8e6c8d |
1981 | IoTOP_NAME(GvIOp(PL_defoutgv)) = s = savepv(SvPV(sv,len)); |
3280af22 |
1982 | IoTOP_GV(GvIOp(PL_defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO); |
79072805 |
1983 | break; |
1984 | case '~': |
3280af22 |
1985 | Safefree(IoFMT_NAME(GvIOp(PL_defoutgv))); |
2d8e6c8d |
1986 | IoFMT_NAME(GvIOp(PL_defoutgv)) = s = savepv(SvPV(sv,len)); |
3280af22 |
1987 | IoFMT_GV(GvIOp(PL_defoutgv)) = gv_fetchpv(s,TRUE, SVt_PVIO); |
79072805 |
1988 | break; |
1989 | case '=': |
632db599 |
1990 | IoPAGE_LEN(GvIOp(PL_defoutgv)) = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)); |
79072805 |
1991 | break; |
1992 | case '-': |
632db599 |
1993 | IoLINES_LEFT(GvIOp(PL_defoutgv)) = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)); |
3280af22 |
1994 | if (IoLINES_LEFT(GvIOp(PL_defoutgv)) < 0L) |
1995 | IoLINES_LEFT(GvIOp(PL_defoutgv)) = 0L; |
79072805 |
1996 | break; |
1997 | case '%': |
632db599 |
1998 | IoPAGE(GvIOp(PL_defoutgv)) = (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)); |
79072805 |
1999 | break; |
2000 | case '|': |
4b65379b |
2001 | { |
3280af22 |
2002 | IO *io = GvIOp(PL_defoutgv); |
720f287d |
2003 | if(!io) |
2004 | break; |
4b65379b |
2005 | if ((SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)) == 0) |
2006 | IoFLAGS(io) &= ~IOf_FLUSH; |
2007 | else { |
2008 | if (!(IoFLAGS(io) & IOf_FLUSH)) { |
2009 | PerlIO *ofp = IoOFP(io); |
2010 | if (ofp) |
2011 | (void)PerlIO_flush(ofp); |
2012 | IoFLAGS(io) |= IOf_FLUSH; |
2013 | } |
2014 | } |
79072805 |
2015 | } |
2016 | break; |
2017 | case '*': |
463ee0b2 |
2018 | i = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv); |
3280af22 |
2019 | PL_multiline = (i != 0); |
79072805 |
2020 | break; |
2021 | case '/': |
3280af22 |
2022 | SvREFCNT_dec(PL_rs); |
8bfdd7d9 |
2023 | PL_rs = newSVsv(sv); |
79072805 |
2024 | break; |
2025 | case '\\': |
7889fe52 |
2026 | if (PL_ors_sv) |
2027 | SvREFCNT_dec(PL_ors_sv); |
009c130f |
2028 | if (SvOK(sv) || SvGMAGICAL(sv)) { |
7889fe52 |
2029 | PL_ors_sv = newSVsv(sv); |
009c130f |
2030 | } |
e3c19b7b |
2031 | else { |
7889fe52 |
2032 | PL_ors_sv = Nullsv; |
e3c19b7b |
2033 | } |
79072805 |
2034 | break; |
2035 | case ',': |
7889fe52 |
2036 | if (PL_ofs_sv) |
2037 | SvREFCNT_dec(PL_ofs_sv); |
2038 | if (SvOK(sv) || SvGMAGICAL(sv)) { |
2039 | PL_ofs_sv = newSVsv(sv); |
2040 | } |
2041 | else { |
2042 | PL_ofs_sv = Nullsv; |
2043 | } |
79072805 |
2044 | break; |
2045 | case '#': |
3280af22 |
2046 | if (PL_ofmt) |
2047 | Safefree(PL_ofmt); |
2d8e6c8d |
2048 | PL_ofmt = savepv(SvPV(sv,len)); |
79072805 |
2049 | break; |
2050 | case '[': |
3280af22 |
2051 | PL_compiling.cop_arybase = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv); |
79072805 |
2052 | break; |
2053 | case '?': |
ff0cee69 |
2054 | #ifdef COMPLEX_STATUS |
6b88bc9c |
2055 | if (PL_localizing == 2) { |
2056 | PL_statusvalue = LvTARGOFF(sv); |
2057 | PL_statusvalue_vms = LvTARGLEN(sv); |
ff0cee69 |
2058 | } |
2059 | else |
2060 | #endif |
2061 | #ifdef VMSISH_STATUS |
2062 | if (VMSISH_STATUS) |
2063 | STATUS_NATIVE_SET((U32)(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv))); |
2064 | else |
2065 | #endif |
2066 | STATUS_POSIX_SET(SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv)); |
79072805 |
2067 | break; |
2068 | case '!': |
93189314 |
2069 | { |
2070 | #ifdef VMS |
2071 | # define PERL_VMS_BANG vaxc$errno |
2072 | #else |
2073 | # define PERL_VMS_BANG 0 |
2074 | #endif |
91487cfc |
2075 | SETERRNO(SvIOK(sv) ? SvIVX(sv) : SvOK(sv) ? sv_2iv(sv) : 0, |
93189314 |
2076 | (SvIV(sv) == EVMSERR) ? 4 : PERL_VMS_BANG); |
2077 | } |
79072805 |
2078 | break; |
2079 | case '<': |
3280af22 |
2080 | PL_uid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv); |
2081 | if (PL_delaymagic) { |
2082 | PL_delaymagic |= DM_RUID; |
79072805 |
2083 | break; /* don't do magic till later */ |
2084 | } |
2085 | #ifdef HAS_SETRUID |
b28d0864 |
2086 | (void)setruid((Uid_t)PL_uid); |
79072805 |
2087 | #else |
2088 | #ifdef HAS_SETREUID |
3280af22 |
2089 | (void)setreuid((Uid_t)PL_uid, (Uid_t)-1); |
748a9306 |
2090 | #else |
85e6fe83 |
2091 | #ifdef HAS_SETRESUID |
b28d0864 |
2092 | (void)setresuid((Uid_t)PL_uid, (Uid_t)-1, (Uid_t)-1); |
79072805 |
2093 | #else |
b28d0864 |
2094 | if (PL_uid == PL_euid) /* special case $< = $> */ |
2095 | (void)PerlProc_setuid(PL_uid); |
a0d0e21e |
2096 | else { |
d8eceb89 |
2097 | PL_uid = PerlProc_getuid(); |
cea2e8a9 |
2098 | Perl_croak(aTHX_ "setruid() not implemented"); |
a0d0e21e |
2099 | } |
79072805 |
2100 | #endif |
2101 | #endif |
85e6fe83 |
2102 | #endif |
d8eceb89 |
2103 | PL_uid = PerlProc_getuid(); |
3280af22 |
2104 | PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid)); |
79072805 |
2105 | break; |
2106 | case '>': |
3280af22 |
2107 | PL_euid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv); |
2108 | if (PL_delaymagic) { |
2109 | PL_delaymagic |= DM_EUID; |
79072805 |
2110 | break; /* don't do magic till later */ |
2111 | } |
2112 | #ifdef HAS_SETEUID |
3280af22 |
2113 | (void)seteuid((Uid_t)PL_euid); |
79072805 |
2114 | #else |
2115 | #ifdef HAS_SETREUID |
b28d0864 |
2116 | (void)setreuid((Uid_t)-1, (Uid_t)PL_euid); |
85e6fe83 |
2117 | #else |
2118 | #ifdef HAS_SETRESUID |
6b88bc9c |
2119 | (void)setresuid((Uid_t)-1, (Uid_t)PL_euid, (Uid_t)-1); |
79072805 |
2120 | #else |
b28d0864 |
2121 | if (PL_euid == PL_uid) /* special case $> = $< */ |
2122 | PerlProc_setuid(PL_euid); |
a0d0e21e |
2123 | else { |
e8ee3774 |
2124 | PL_euid = PerlProc_geteuid(); |
cea2e8a9 |
2125 | Perl_croak(aTHX_ "seteuid() not implemented"); |
a0d0e21e |
2126 | } |
79072805 |
2127 | #endif |
2128 | #endif |
85e6fe83 |
2129 | #endif |
d8eceb89 |
2130 | PL_euid = PerlProc_geteuid(); |
3280af22 |
2131 | PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid)); |
79072805 |
2132 | break; |
2133 | case '(': |
3280af22 |
2134 | PL_gid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv); |
2135 | if (PL_delaymagic) { |
2136 | PL_delaymagic |= DM_RGID; |
79072805 |
2137 | break; /* don't do magic till later */ |
2138 | } |
2139 | #ifdef HAS_SETRGID |
b28d0864 |
2140 | (void)setrgid((Gid_t)PL_gid); |
79072805 |
2141 | #else |
2142 | #ifdef HAS_SETREGID |
3280af22 |
2143 | (void)setregid((Gid_t)PL_gid, (Gid_t)-1); |
85e6fe83 |
2144 | #else |
2145 | #ifdef HAS_SETRESGID |
b28d0864 |
2146 | (void)setresgid((Gid_t)PL_gid, (Gid_t)-1, (Gid_t) 1); |
79072805 |
2147 | #else |
b28d0864 |
2148 | if (PL_gid == PL_egid) /* special case $( = $) */ |
2149 | (void)PerlProc_setgid(PL_gid); |
748a9306 |
2150 | else { |
d8eceb89 |
2151 | PL_gid = PerlProc_getgid(); |
cea2e8a9 |
2152 | Perl_croak(aTHX_ "setrgid() not implemented"); |
748a9306 |
2153 | } |
79072805 |
2154 | #endif |
2155 | #endif |
85e6fe83 |
2156 | #endif |
d8eceb89 |
2157 | PL_gid = PerlProc_getgid(); |
3280af22 |
2158 | PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid)); |
79072805 |
2159 | break; |
2160 | case ')': |
5cd24f17 |
2161 | #ifdef HAS_SETGROUPS |
2162 | { |
2d8e6c8d |
2163 | char *p = SvPV(sv, len); |
5cd24f17 |
2164 | Groups_t gary[NGROUPS]; |
2165 | |
5cd24f17 |
2166 | while (isSPACE(*p)) |
2167 | ++p; |
2d4389e4 |
2168 | PL_egid = Atol(p); |
5cd24f17 |
2169 | for (i = 0; i < NGROUPS; ++i) { |
2170 | while (*p && !isSPACE(*p)) |
2171 | ++p; |
2172 | while (isSPACE(*p)) |
2173 | ++p; |
2174 | if (!*p) |
2175 | break; |
2d4389e4 |
2176 | gary[i] = Atol(p); |
5cd24f17 |
2177 | } |
8cc95fdb |
2178 | if (i) |
2179 | (void)setgroups(i, gary); |
5cd24f17 |
2180 | } |
2181 | #else /* HAS_SETGROUPS */ |
b28d0864 |
2182 | PL_egid = SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv); |
5cd24f17 |
2183 | #endif /* HAS_SETGROUPS */ |
3280af22 |
2184 | if (PL_delaymagic) { |
2185 | PL_delaymagic |= DM_EGID; |
79072805 |
2186 | break; /* don't do magic till later */ |
2187 | } |
2188 | #ifdef HAS_SETEGID |
3280af22 |
2189 | (void)setegid((Gid_t)PL_egid); |
79072805 |
2190 | #else |
2191 | #ifdef HAS_SETREGID |
b28d0864 |
2192 | (void)setregid((Gid_t)-1, (Gid_t)PL_egid); |
85e6fe83 |
2193 | #else |
2194 | #ifdef HAS_SETRESGID |
b28d0864 |
2195 | (void)setresgid((Gid_t)-1, (Gid_t)PL_egid, (Gid_t)-1); |
79072805 |
2196 | #else |
b28d0864 |
2197 | if (PL_egid == PL_gid) /* special case $) = $( */ |
2198 | (void)PerlProc_setgid(PL_egid); |
748a9306 |
2199 | else { |
d8eceb89 |
2200 | PL_egid = PerlProc_getegid(); |
cea2e8a9 |
2201 | Perl_croak(aTHX_ "setegid() not implemented"); |
748a9306 |
2202 | } |
79072805 |
2203 | #endif |
2204 | #endif |
85e6fe83 |
2205 | #endif |
d8eceb89 |
2206 | PL_egid = PerlProc_getegid(); |
3280af22 |
2207 | PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid)); |
79072805 |
2208 | break; |
2209 | case ':': |
2d8e6c8d |
2210 | PL_chopset = SvPV_force(sv,len); |
79072805 |
2211 | break; |
cd39f2b6 |
2212 | #ifndef MACOS_TRADITIONAL |
79072805 |
2213 | case '0': |
e2975953 |
2214 | LOCK_DOLLARZERO_MUTEX; |
4bc88a62 |
2215 | #ifdef HAS_SETPROCTITLE |
2216 | /* The BSDs don't show the argv[] in ps(1) output, they |
2217 | * show a string from the process struct and provide |
2218 | * the setproctitle() routine to manipulate that. */ |
2219 | { |
2220 | s = SvPV(sv, len); |
98b76f99 |
2221 | # if __FreeBSD_version > 410001 |
9aad2c0e |
2222 | /* The leading "-" removes the "perl: " prefix, |
4bc88a62 |
2223 | * but not the "(perl) suffix from the ps(1) |
2224 | * output, because that's what ps(1) shows if the |
2225 | * argv[] is modified. */ |
6f2ad931 |
2226 | setproctitle("-%s", s); |
9aad2c0e |
2227 | # else /* old FreeBSDs, NetBSD, OpenBSD, anyBSD */ |
4bc88a62 |
2228 | /* This doesn't really work if you assume that |
2229 | * $0 = 'foobar'; will wipe out 'perl' from the $0 |
2230 | * because in ps(1) output the result will be like |
2231 | * sprintf("perl: %s (perl)", s) |
2232 | * I guess this is a security feature: |
2233 | * one (a user process) cannot get rid of the original name. |
2234 | * --jhi */ |
2235 | setproctitle("%s", s); |
2236 | # endif |
2237 | } |
2238 | #endif |
17aa7f3d |
2239 | #if defined(__hpux) && defined(PSTAT_SETCMD) |
2240 | { |
2241 | union pstun un; |
2242 | s = SvPV(sv, len); |
2243 | un.pst_command = s; |
2244 | pstat(PSTAT_SETCMD, un, len, 0, 0); |
2245 | } |
2246 | #endif |
3280af22 |
2247 | if (!PL_origalen) { |
2248 | s = PL_origargv[0]; |
79072805 |
2249 | s += strlen(s); |
2250 | /* See if all the arguments are contiguous in memory */ |
3280af22 |
2251 | for (i = 1; i < PL_origargc; i++) { |
2252 | if (PL_origargv[i] == s + 1 |
fb73857a |
2253 | #ifdef OS2 |
6b88bc9c |
2254 | || PL_origargv[i] == s + 2 |
ac27b0f5 |
2255 | #endif |
fb73857a |
2256 | ) |
379c4362 |
2257 | { |
2258 | ++s; |
2259 | s += strlen(s); /* this one is ok too */ |
2260 | } |
fb73857a |
2261 | else |
2262 | break; |
79072805 |
2263 | } |
bbce6d69 |
2264 | /* can grab env area too? */ |
4efc5df6 |
2265 | if (PL_origenviron |
2266 | #ifdef USE_ITHREADS |
2267 | && PL_curinterp == aTHX |
2268 | #endif |
2269 | && (PL_origenviron[0] == s + 1)) |
2270 | { |
66b1d557 |
2271 | my_setenv("NoNe SuCh", Nullch); |
79072805 |
2272 | /* force copy of environment */ |
3280af22 |
2273 | for (i = 0; PL_origenviron[i]; i++) |
379c4362 |
2274 | if (PL_origenviron[i] == s + 1) { |
2275 | ++s; |
2276 | s += strlen(s); |
2277 | } |
fb73857a |
2278 | else |
2279 | break; |
79072805 |
2280 | } |
3280af22 |
2281 | PL_origalen = s - PL_origargv[0]; |
79072805 |
2282 | } |
a0d0e21e |
2283 | s = SvPV_force(sv,len); |
8990e307 |
2284 | i = len; |
eb160463 |
2285 | if (i >= (I32)PL_origalen) { |
3280af22 |
2286 | i = PL_origalen; |
fb73857a |
2287 | /* don't allow system to limit $0 seen by script */ |
2288 | /* SvCUR_set(sv, i); *SvEND(sv) = '\0'; */ |
3280af22 |
2289 | Copy(s, PL_origargv[0], i, char); |
2290 | s = PL_origargv[0]+i; |
fb73857a |
2291 | *s = '\0'; |
79072805 |
2292 | } |
2293 | else { |
3280af22 |
2294 | Copy(s, PL_origargv[0], i, char); |
2295 | s = PL_origargv[0]+i; |
79072805 |
2296 | *s++ = '\0'; |
eb160463 |
2297 | while (++i < (I32)PL_origalen) |
f1c3b2ef |
2298 | *s++ = '\0'; |
3280af22 |
2299 | for (i = 1; i < PL_origargc; i++) |
2300 | PL_origargv[i] = Nullch; |
79072805 |
2301 | } |
e2975953 |
2302 | UNLOCK_DOLLARZERO_MUTEX; |
79072805 |
2303 | break; |
cd39f2b6 |
2304 | #endif |
79072805 |
2305 | } |
2306 | return 0; |
2307 | } |
2308 | |
2309 | I32 |
864dbfa3 |
2310 | Perl_whichsig(pTHX_ char *sig) |
79072805 |
2311 | { |
2312 | register char **sigv; |
2313 | |
22c35a8c |
2314 | for (sigv = PL_sig_name+1; *sigv; sigv++) |
79072805 |
2315 | if (strEQ(sig,*sigv)) |
22c35a8c |
2316 | return PL_sig_num[sigv - PL_sig_name]; |
79072805 |
2317 | #ifdef SIGCLD |
2318 | if (strEQ(sig,"CHLD")) |
2319 | return SIGCLD; |
2320 | #endif |
2321 | #ifdef SIGCHLD |
2322 | if (strEQ(sig,"CLD")) |
2323 | return SIGCHLD; |
2324 | #endif |
2325 | return 0; |
2326 | } |
2327 | |
df3728a2 |
2328 | #if !defined(PERL_IMPLICIT_CONTEXT) |
84902520 |
2329 | static SV* sig_sv; |
df3728a2 |
2330 | #endif |
84902520 |
2331 | |
ecfc5424 |
2332 | Signal_t |
cea2e8a9 |
2333 | Perl_sighandler(int sig) |
79072805 |
2334 | { |
1018e26f |
2335 | #ifdef PERL_GET_SIG_CONTEXT |
2336 | dTHXa(PERL_GET_SIG_CONTEXT); |
71d280e3 |
2337 | #else |
cea2e8a9 |
2338 | dTHX; |
71d280e3 |
2339 | #endif |
79072805 |
2340 | dSP; |
00d579c5 |
2341 | GV *gv = Nullgv; |
a0d0e21e |
2342 | HV *st; |
b7953727 |
2343 | SV *sv = Nullsv, *tSv = PL_Sv; |
00d579c5 |
2344 | CV *cv = Nullcv; |
533c011a |
2345 | OP *myop = PL_op; |
84902520 |
2346 | U32 flags = 0; |
3280af22 |
2347 | XPV *tXpv = PL_Xpv; |
71d280e3 |
2348 | |
3280af22 |
2349 | if (PL_savestack_ix + 15 <= PL_savestack_max) |
84902520 |
2350 | flags |= 1; |
3280af22 |
2351 | if (PL_markstack_ptr < PL_markstack_max - 2) |
84902520 |
2352 | flags |= 4; |
3280af22 |
2353 | if (PL_retstack_ix < PL_retstack_max - 2) |
84902520 |
2354 | flags |= 8; |
3280af22 |
2355 | if (PL_scopestack_ix < PL_scopestack_max - 3) |
84902520 |
2356 | flags |= 16; |
2357 | |
727405f8 |
2358 | if (!PL_psig_ptr[sig]) { |
99ef548b |
2359 | PerlIO_printf(Perl_error_log, "Signal SIG%s received, but no signal handler set.\n", |
727405f8 |
2360 | PL_sig_name[sig]); |
2361 | exit(sig); |
2362 | } |
ff0cee69 |
2363 | |
84902520 |
2364 | /* Max number of items pushed there is 3*n or 4. We cannot fix |
2365 | infinity, so we fix 4 (in fact 5): */ |
2366 | if (flags & 1) { |
3280af22 |
2367 | PL_savestack_ix += 5; /* Protect save in progress. */ |
c76ac1ee |
2368 | SAVEDESTRUCTOR_X(unwind_handler_stack, (void*)&flags); |
84902520 |
2369 | } |
ac27b0f5 |
2370 | if (flags & 4) |
3280af22 |
2371 | PL_markstack_ptr++; /* Protect mark. */ |
84902520 |
2372 | if (flags & 8) { |
3280af22 |
2373 | PL_retstack_ix++; |
2374 | PL_retstack[PL_retstack_ix] = NULL; |
84902520 |
2375 | } |
2376 | if (flags & 16) |
3280af22 |
2377 | PL_scopestack_ix += 1; |
84902520 |
2378 | /* sv_2cv is too complicated, try a simpler variant first: */ |
ac27b0f5 |
2379 | if (!SvROK(PL_psig_ptr[sig]) || !(cv = (CV*)SvRV(PL_psig_ptr[sig])) |
84902520 |
2380 | || SvTYPE(cv) != SVt_PVCV) |
22c35a8c |
2381 | cv = sv_2cv(PL_psig_ptr[sig],&st,&gv,TRUE); |
84902520 |
2382 | |
a0d0e21e |
2383 | if (!cv || !CvROOT(cv)) { |
599cee73 |
2384 | if (ckWARN(WARN_SIGNAL)) |
9014280d |
2385 | Perl_warner(aTHX_ packWARN(WARN_SIGNAL), "SIG%s handler \"%s\" not defined.\n", |
22c35a8c |
2386 | PL_sig_name[sig], (gv ? GvENAME(gv) |
00d579c5 |
2387 | : ((cv && CvGV(cv)) |
2388 | ? GvENAME(CvGV(cv)) |
2389 | : "__ANON__"))); |
2390 | goto cleanup; |
79072805 |
2391 | } |
2392 | |
22c35a8c |
2393 | if(PL_psig_name[sig]) { |
2394 | sv = SvREFCNT_inc(PL_psig_name[sig]); |
84902520 |
2395 | flags |= 64; |
df3728a2 |
2396 | #if !defined(PERL_IMPLICIT_CONTEXT) |
84902520 |
2397 | sig_sv = sv; |
df3728a2 |
2398 | #endif |
84902520 |
2399 | } else { |
ff0cee69 |
2400 | sv = sv_newmortal(); |
22c35a8c |
2401 | sv_setpv(sv,PL_sig_name[sig]); |
88e89b8a |
2402 | } |
e336de0d |
2403 | |
e788e7d3 |
2404 | PUSHSTACKi(PERLSI_SIGNAL); |
924508f0 |
2405 | PUSHMARK(SP); |
79072805 |
2406 | PUSHs(sv); |
79072805 |
2407 | PUTBACK; |
a0d0e21e |
2408 | |
1b266415 |
2409 | call_sv((SV*)cv, G_DISCARD|G_EVAL); |
79072805 |
2410 | |
d3acc0f7 |
2411 | POPSTACK; |
1b266415 |
2412 | if (SvTRUE(ERRSV)) { |
1d615522 |
2413 | #ifndef PERL_MICRO |
983dbef6 |
2414 | #ifdef HAS_SIGPROCMASK |
1b266415 |
2415 | /* Handler "died", for example to get out of a restart-able read(). |
2416 | * Before we re-do that on its behalf re-enable the signal which was |
2417 | * blocked by the system when we entered. |
2418 | */ |
2419 | sigset_t set; |
2420 | sigemptyset(&set); |
2421 | sigaddset(&set,sig); |
2422 | sigprocmask(SIG_UNBLOCK, &set, NULL); |
2423 | #else |
2424 | /* Not clear if this will work */ |
2425 | (void)rsignal(sig, SIG_IGN); |
0a8e0eff |
2426 | (void)rsignal(sig, &Perl_csighandler); |
1b266415 |
2427 | #endif |
1d615522 |
2428 | #endif /* !PERL_MICRO */ |
b3fe4827 |
2429 | Perl_die(aTHX_ Nullformat); |
1b266415 |
2430 | } |
00d579c5 |
2431 | cleanup: |
84902520 |
2432 | if (flags & 1) |
3280af22 |
2433 | PL_savestack_ix -= 8; /* Unprotect save in progress. */ |
ac27b0f5 |
2434 | if (flags & 4) |
3280af22 |
2435 | PL_markstack_ptr--; |
ac27b0f5 |
2436 | if (flags & 8) |
3280af22 |
2437 | PL_retstack_ix--; |
84902520 |
2438 | if (flags & 16) |
3280af22 |
2439 | PL_scopestack_ix -= 1; |
84902520 |
2440 | if (flags & 64) |
2441 | SvREFCNT_dec(sv); |
533c011a |
2442 | PL_op = myop; /* Apparently not needed... */ |
ac27b0f5 |
2443 | |
3280af22 |
2444 | PL_Sv = tSv; /* Restore global temporaries. */ |
2445 | PL_Xpv = tXpv; |
79072805 |
2446 | return; |
2447 | } |
4e35701f |
2448 | |
2449 | |
51371543 |
2450 | static void |
acfe0abc |
2451 | restore_magic(pTHX_ void *p) |
51371543 |
2452 | { |
48944bdf |
2453 | MGS* mgs = SSPTR(PTR2IV(p), MGS*); |
51371543 |
2454 | SV* sv = mgs->mgs_sv; |
2455 | |
2456 | if (!sv) |
2457 | return; |
2458 | |
2459 | if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) |
2460 | { |
2461 | if (mgs->mgs_flags) |
2462 | SvFLAGS(sv) |= mgs->mgs_flags; |
2463 | else |
2464 | mg_magical(sv); |
2465 | if (SvGMAGICAL(sv)) |
2466 | SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK); |
2467 | } |
2468 | |
2469 | mgs->mgs_sv = NULL; /* mark the MGS structure as restored */ |
2470 | |
2471 | /* If we're still on top of the stack, pop us off. (That condition |
2472 | * will be satisfied if restore_magic was called explicitly, but *not* |
2473 | * if it's being called via leave_scope.) |
2474 | * The reason for doing this is that otherwise, things like sv_2cv() |
2475 | * may leave alloc gunk on the savestack, and some code |
2476 | * (e.g. sighandler) doesn't expect that... |
2477 | */ |
2478 | if (PL_savestack_ix == mgs->mgs_ss_ix) |
2479 | { |
2480 | I32 popval = SSPOPINT; |
c76ac1ee |
2481 | assert(popval == SAVEt_DESTRUCTOR_X); |
51371543 |
2482 | PL_savestack_ix -= 2; |
2483 | popval = SSPOPINT; |
2484 | assert(popval == SAVEt_ALLOC); |
2485 | popval = SSPOPINT; |
2486 | PL_savestack_ix -= popval; |
2487 | } |
2488 | |
2489 | } |
2490 | |
2491 | static void |
acfe0abc |
2492 | unwind_handler_stack(pTHX_ void *p) |
51371543 |
2493 | { |
51371543 |
2494 | U32 flags = *(U32*)p; |
2495 | |
2496 | if (flags & 1) |
2497 | PL_savestack_ix -= 5; /* Unprotect save in progress. */ |
2498 | /* cxstack_ix-- Not needed, die already unwound it. */ |
df3728a2 |
2499 | #if !defined(PERL_IMPLICIT_CONTEXT) |
51371543 |
2500 | if (flags & 64) |
2501 | SvREFCNT_dec(sig_sv); |
df3728a2 |
2502 | #endif |
51371543 |
2503 | } |
1018e26f |
2504 | |
2505 | |