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