Commit | Line | Data |
---|---|---|
a0d0e21e | 1 | /* mg.c |
79072805 | 2 | * |
1129b882 NC |
3 | * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, |
4 | * 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 by Larry Wall and others | |
79072805 LW |
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 LW |
9 | */ |
10 | ||
11 | /* | |
4ac71550 TC |
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. | |
14 | * | |
15 | * [p.363 of _The Lord of the Rings_, II/vii: "The Mirror of Galadriel"] | |
79072805 LW |
16 | */ |
17 | ||
ccfc67b7 JH |
18 | /* |
19 | =head1 Magical Functions | |
166f8a29 DM |
20 | |
21 | "Magic" is special data attached to SV structures in order to give them | |
22 | "magical" properties. When any Perl code tries to read from, or assign to, | |
23 | an SV marked as magical, it calls the 'get' or 'set' function associated | |
24 | with that SV's magic. A get is called prior to reading an SV, in order to | |
ddfa107c | 25 | give it a chance to update its internal value (get on $. writes the line |
166f8a29 DM |
26 | number of the last read filehandle into to the SV's IV slot), while |
27 | set is called after an SV has been written to, in order to allow it to make | |
ddfa107c | 28 | use of its changed value (set on $/ copies the SV's new value to the |
166f8a29 DM |
29 | PL_rs global variable). |
30 | ||
31 | Magic is implemented as a linked list of MAGIC structures attached to the | |
32 | SV. Each MAGIC struct holds the type of the magic, a pointer to an array | |
33 | of functions that implement the get(), set(), length() etc functions, | |
34 | plus space for some flags and pointers. For example, a tied variable has | |
35 | a MAGIC structure that contains a pointer to the object associated with the | |
36 | tie. | |
37 | ||
ccfc67b7 JH |
38 | */ |
39 | ||
79072805 | 40 | #include "EXTERN.h" |
864dbfa3 | 41 | #define PERL_IN_MG_C |
79072805 LW |
42 | #include "perl.h" |
43 | ||
5cd24f17 | 44 | #if defined(HAS_GETGROUPS) || defined(HAS_SETGROUPS) |
b7953727 JH |
45 | # ifdef I_GRP |
46 | # include <grp.h> | |
47 | # endif | |
188ea221 CS |
48 | #endif |
49 | ||
757f63d8 SP |
50 | #if defined(HAS_SETGROUPS) |
51 | # ifndef NGROUPS | |
52 | # define NGROUPS 32 | |
53 | # endif | |
54 | #endif | |
55 | ||
17aa7f3d JH |
56 | #ifdef __hpux |
57 | # include <sys/pstat.h> | |
58 | #endif | |
59 | ||
7636ea95 AB |
60 | #ifdef HAS_PRCTL_SET_NAME |
61 | # include <sys/prctl.h> | |
62 | #endif | |
63 | ||
8aad04aa | 64 | #if defined(HAS_SIGACTION) && defined(SA_SIGINFO) |
b6455c53 | 65 | Signal_t Perl_csighandler(int sig, siginfo_t *, void *); |
8aad04aa | 66 | #else |
e69880a5 | 67 | Signal_t Perl_csighandler(int sig); |
8aad04aa | 68 | #endif |
e69880a5 | 69 | |
9cffb111 OS |
70 | #ifdef __Lynx__ |
71 | /* Missing protos on LynxOS */ | |
72 | void setruid(uid_t id); | |
73 | void seteuid(uid_t id); | |
74 | void setrgid(uid_t id); | |
75 | void setegid(uid_t id); | |
76 | #endif | |
77 | ||
c07a80fd | 78 | /* |
79 | * Use the "DESTRUCTOR" scope cleanup to reinstate magic. | |
80 | */ | |
81 | ||
82 | struct magic_state { | |
83 | SV* mgs_sv; | |
455ece5e | 84 | I32 mgs_ss_ix; |
f9c6fee5 CS |
85 | U32 mgs_magical; |
86 | bool mgs_readonly; | |
c07a80fd | 87 | }; |
455ece5e | 88 | /* MGS is typedef'ed to struct magic_state in perl.h */ |
76e3520e GS |
89 | |
90 | STATIC void | |
8fb26106 | 91 | S_save_magic(pTHX_ I32 mgs_ix, SV *sv) |
c07a80fd | 92 | { |
97aff369 | 93 | dVAR; |
455ece5e | 94 | MGS* mgs; |
7918f24d NC |
95 | |
96 | PERL_ARGS_ASSERT_SAVE_MAGIC; | |
97 | ||
8985fe98 DM |
98 | /* guard against sv having being freed midway by holding a private |
99 | reference. */ | |
100 | SvREFCNT_inc_simple_void_NN(sv); | |
101 | ||
c07a80fd | 102 | assert(SvMAGICAL(sv)); |
d8b2590f NC |
103 | /* Turning READONLY off for a copy-on-write scalar (including shared |
104 | hash keys) is a bad idea. */ | |
765f542d | 105 | if (SvIsCOW(sv)) |
9a265e59 | 106 | sv_force_normal_flags(sv, 0); |
c07a80fd | 107 | |
8772537c | 108 | SAVEDESTRUCTOR_X(S_restore_magic, INT2PTR(void*, (IV)mgs_ix)); |
455ece5e AD |
109 | |
110 | mgs = SSPTR(mgs_ix, MGS*); | |
c07a80fd | 111 | mgs->mgs_sv = sv; |
f9c6fee5 CS |
112 | mgs->mgs_magical = SvMAGICAL(sv); |
113 | mgs->mgs_readonly = SvREADONLY(sv) != 0; | |
455ece5e | 114 | mgs->mgs_ss_ix = PL_savestack_ix; /* points after the saved destructor */ |
c07a80fd | 115 | |
116 | SvMAGICAL_off(sv); | |
117 | SvREADONLY_off(sv); | |
085bde85 NC |
118 | if (!(SvFLAGS(sv) & (SVf_IOK|SVf_NOK|SVf_POK))) { |
119 | /* No public flags are set, so promote any private flags to public. */ | |
120 | SvFLAGS(sv) |= (SvFLAGS(sv) & (SVp_IOK|SVp_NOK|SVp_POK)) >> PRIVSHIFT; | |
121 | } | |
c07a80fd | 122 | } |
123 | ||
954c1994 GS |
124 | /* |
125 | =for apidoc mg_magical | |
126 | ||
127 | Turns on the magical status of an SV. See C<sv_magic>. | |
128 | ||
129 | =cut | |
130 | */ | |
131 | ||
8990e307 | 132 | void |
864dbfa3 | 133 | Perl_mg_magical(pTHX_ SV *sv) |
8990e307 | 134 | { |
e1ec3a88 | 135 | const MAGIC* mg; |
7918f24d | 136 | PERL_ARGS_ASSERT_MG_MAGICAL; |
96a5add6 | 137 | PERL_UNUSED_CONTEXT; |
f9c6fee5 CS |
138 | |
139 | SvMAGICAL_off(sv); | |
218787bd | 140 | if ((mg = SvMAGIC(sv))) { |
218787bd VP |
141 | do { |
142 | const MGVTBL* const vtbl = mg->mg_virtual; | |
143 | if (vtbl) { | |
144 | if (vtbl->svt_get && !(mg->mg_flags & MGf_GSKIP)) | |
145 | SvGMAGICAL_on(sv); | |
146 | if (vtbl->svt_set) | |
147 | SvSMAGICAL_on(sv); | |
148 | if (vtbl->svt_clear) | |
149 | SvRMAGICAL_on(sv); | |
150 | } | |
151 | } while ((mg = mg->mg_moremagic)); | |
152 | if (!(SvFLAGS(sv) & (SVs_GMG|SVs_SMG))) | |
153 | SvRMAGICAL_on(sv); | |
8990e307 LW |
154 | } |
155 | } | |
156 | ||
2767dea0 DM |
157 | |
158 | /* is this container magic (%ENV, $1 etc), or value magic (pos, taint etc)? */ | |
159 | ||
160 | STATIC bool | |
161 | S_is_container_magic(const MAGIC *mg) | |
162 | { | |
7918f24d | 163 | assert(mg); |
2767dea0 DM |
164 | switch (mg->mg_type) { |
165 | case PERL_MAGIC_bm: | |
166 | case PERL_MAGIC_fm: | |
167 | case PERL_MAGIC_regex_global: | |
168 | case PERL_MAGIC_nkeys: | |
169 | #ifdef USE_LOCALE_COLLATE | |
170 | case PERL_MAGIC_collxfrm: | |
171 | #endif | |
172 | case PERL_MAGIC_qr: | |
173 | case PERL_MAGIC_taint: | |
174 | case PERL_MAGIC_vec: | |
175 | case PERL_MAGIC_vstring: | |
176 | case PERL_MAGIC_utf8: | |
177 | case PERL_MAGIC_substr: | |
178 | case PERL_MAGIC_defelem: | |
179 | case PERL_MAGIC_arylen: | |
180 | case PERL_MAGIC_pos: | |
181 | case PERL_MAGIC_backref: | |
182 | case PERL_MAGIC_arylen_p: | |
183 | case PERL_MAGIC_rhash: | |
184 | case PERL_MAGIC_symtab: | |
5afa72af | 185 | case PERL_MAGIC_tied: /* treat as value, so 'local @tied' isn't tied */ |
d9088386 | 186 | case PERL_MAGIC_checkcall: |
2767dea0 DM |
187 | return 0; |
188 | default: | |
189 | return 1; | |
190 | } | |
191 | } | |
192 | ||
954c1994 GS |
193 | /* |
194 | =for apidoc mg_get | |
195 | ||
196 | Do magic after a value is retrieved from the SV. See C<sv_magic>. | |
197 | ||
198 | =cut | |
199 | */ | |
200 | ||
79072805 | 201 | int |
864dbfa3 | 202 | Perl_mg_get(pTHX_ SV *sv) |
79072805 | 203 | { |
97aff369 | 204 | dVAR; |
35a4481c | 205 | const I32 mgs_ix = SSNEW(sizeof(MGS)); |
f9c6fee5 | 206 | bool have_new = 0; |
ff76feab | 207 | MAGIC *newmg, *head, *cur, *mg; |
6683b158 | 208 | |
7918f24d NC |
209 | PERL_ARGS_ASSERT_MG_GET; |
210 | ||
455ece5e | 211 | save_magic(mgs_ix, sv); |
463ee0b2 | 212 | |
ff76feab AMS |
213 | /* We must call svt_get(sv, mg) for each valid entry in the linked |
214 | list of magic. svt_get() may delete the current entry, add new | |
215 | magic to the head of the list, or upgrade the SV. AMS 20010810 */ | |
216 | ||
217 | newmg = cur = head = mg = SvMAGIC(sv); | |
218 | while (mg) { | |
35a4481c | 219 | const MGVTBL * const vtbl = mg->mg_virtual; |
f9c6fee5 | 220 | MAGIC * const nextmg = mg->mg_moremagic; /* it may delete itself */ |
ff76feab | 221 | |
2b260de0 | 222 | if (!(mg->mg_flags & MGf_GSKIP) && vtbl && vtbl->svt_get) { |
16c91539 | 223 | vtbl->svt_get(aTHX_ sv, mg); |
b77f7d40 | 224 | |
58f82c5c DM |
225 | /* guard against magic having been deleted - eg FETCH calling |
226 | * untie */ | |
f9c6fee5 CS |
227 | if (!SvMAGIC(sv)) { |
228 | (SSPTR(mgs_ix, MGS *))->mgs_magical = 0; /* recalculate flags */ | |
58f82c5c | 229 | break; |
f9c6fee5 | 230 | } |
b77f7d40 | 231 | |
f9c6fee5 | 232 | /* recalculate flags if this entry was deleted. */ |
ff76feab | 233 | if (mg->mg_flags & MGf_GSKIP) |
f9c6fee5 | 234 | (SSPTR(mgs_ix, MGS *))->mgs_magical = 0; |
a0d0e21e | 235 | } |
ff76feab | 236 | |
f9c6fee5 | 237 | mg = nextmg; |
ff76feab | 238 | |
0723351e | 239 | if (have_new) { |
ff76feab AMS |
240 | /* Have we finished with the new entries we saw? Start again |
241 | where we left off (unless there are more new entries). */ | |
242 | if (mg == head) { | |
0723351e | 243 | have_new = 0; |
ff76feab AMS |
244 | mg = cur; |
245 | head = newmg; | |
246 | } | |
247 | } | |
248 | ||
249 | /* Were any new entries added? */ | |
0723351e NC |
250 | if (!have_new && (newmg = SvMAGIC(sv)) != head) { |
251 | have_new = 1; | |
ff76feab AMS |
252 | cur = mg; |
253 | mg = newmg; | |
f9c6fee5 | 254 | (SSPTR(mgs_ix, MGS *))->mgs_magical = 0; /* recalculate flags */ |
760ac839 | 255 | } |
79072805 | 256 | } |
463ee0b2 | 257 | |
8772537c | 258 | restore_magic(INT2PTR(void *, (IV)mgs_ix)); |
79072805 LW |
259 | return 0; |
260 | } | |
261 | ||
954c1994 GS |
262 | /* |
263 | =for apidoc mg_set | |
264 | ||
265 | Do magic after a value is assigned to the SV. See C<sv_magic>. | |
266 | ||
267 | =cut | |
268 | */ | |
269 | ||
79072805 | 270 | int |
864dbfa3 | 271 | Perl_mg_set(pTHX_ SV *sv) |
79072805 | 272 | { |
97aff369 | 273 | dVAR; |
35a4481c | 274 | const I32 mgs_ix = SSNEW(sizeof(MGS)); |
79072805 | 275 | MAGIC* mg; |
463ee0b2 LW |
276 | MAGIC* nextmg; |
277 | ||
7918f24d NC |
278 | PERL_ARGS_ASSERT_MG_SET; |
279 | ||
455ece5e | 280 | save_magic(mgs_ix, sv); |
463ee0b2 LW |
281 | |
282 | for (mg = SvMAGIC(sv); mg; mg = nextmg) { | |
e1ec3a88 | 283 | const MGVTBL* vtbl = mg->mg_virtual; |
463ee0b2 | 284 | nextmg = mg->mg_moremagic; /* it may delete itself */ |
a0d0e21e LW |
285 | if (mg->mg_flags & MGf_GSKIP) { |
286 | mg->mg_flags &= ~MGf_GSKIP; /* setting requires another read */ | |
f9c6fee5 | 287 | (SSPTR(mgs_ix, MGS*))->mgs_magical = 0; |
a0d0e21e | 288 | } |
e7cbf6c6 DM |
289 | if (PL_localizing == 2 && !S_is_container_magic(mg)) |
290 | continue; | |
2b260de0 | 291 | if (vtbl && vtbl->svt_set) |
16c91539 | 292 | vtbl->svt_set(aTHX_ sv, mg); |
79072805 | 293 | } |
463ee0b2 | 294 | |
8772537c | 295 | restore_magic(INT2PTR(void*, (IV)mgs_ix)); |
79072805 LW |
296 | return 0; |
297 | } | |
298 | ||
954c1994 GS |
299 | /* |
300 | =for apidoc mg_length | |
301 | ||
302 | Report on the SV's length. See C<sv_magic>. | |
303 | ||
304 | =cut | |
305 | */ | |
306 | ||
79072805 | 307 | U32 |
864dbfa3 | 308 | Perl_mg_length(pTHX_ SV *sv) |
79072805 | 309 | { |
97aff369 | 310 | dVAR; |
79072805 | 311 | MAGIC* mg; |
463ee0b2 | 312 | STRLEN len; |
463ee0b2 | 313 | |
7918f24d NC |
314 | PERL_ARGS_ASSERT_MG_LENGTH; |
315 | ||
79072805 | 316 | for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) { |
35a4481c | 317 | const MGVTBL * const vtbl = mg->mg_virtual; |
2b260de0 | 318 | if (vtbl && vtbl->svt_len) { |
35a4481c | 319 | const I32 mgs_ix = SSNEW(sizeof(MGS)); |
455ece5e | 320 | save_magic(mgs_ix, sv); |
a0d0e21e | 321 | /* omit MGf_GSKIP -- not changed here */ |
16c91539 | 322 | len = vtbl->svt_len(aTHX_ sv, mg); |
8772537c | 323 | restore_magic(INT2PTR(void*, (IV)mgs_ix)); |
85e6fe83 LW |
324 | return len; |
325 | } | |
326 | } | |
327 | ||
d0644529 NC |
328 | { |
329 | /* You can't know whether it's UTF-8 until you get the string again... | |
330 | */ | |
10516c54 | 331 | const U8 *s = (U8*)SvPV_const(sv, len); |
d0644529 NC |
332 | |
333 | if (DO_UTF8(sv)) { | |
334 | len = utf8_length(s, s + len); | |
335 | } | |
5636d518 | 336 | } |
463ee0b2 | 337 | return len; |
79072805 LW |
338 | } |
339 | ||
8fb26106 | 340 | I32 |
864dbfa3 | 341 | Perl_mg_size(pTHX_ SV *sv) |
93965878 NIS |
342 | { |
343 | MAGIC* mg; | |
ac27b0f5 | 344 | |
7918f24d NC |
345 | PERL_ARGS_ASSERT_MG_SIZE; |
346 | ||
93965878 | 347 | for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) { |
35a4481c | 348 | const MGVTBL* const vtbl = mg->mg_virtual; |
2b260de0 | 349 | if (vtbl && vtbl->svt_len) { |
35a4481c AL |
350 | const I32 mgs_ix = SSNEW(sizeof(MGS)); |
351 | I32 len; | |
455ece5e | 352 | save_magic(mgs_ix, sv); |
93965878 | 353 | /* omit MGf_GSKIP -- not changed here */ |
16c91539 | 354 | len = vtbl->svt_len(aTHX_ sv, mg); |
8772537c | 355 | restore_magic(INT2PTR(void*, (IV)mgs_ix)); |
93965878 NIS |
356 | return len; |
357 | } | |
358 | } | |
359 | ||
360 | switch(SvTYPE(sv)) { | |
361 | case SVt_PVAV: | |
502c6561 | 362 | return AvFILLp((const AV *) sv); /* Fallback to non-tied array */ |
93965878 NIS |
363 | case SVt_PVHV: |
364 | /* FIXME */ | |
365 | default: | |
cea2e8a9 | 366 | Perl_croak(aTHX_ "Size magic not implemented"); |
93965878 NIS |
367 | break; |
368 | } | |
369 | return 0; | |
370 | } | |
371 | ||
954c1994 GS |
372 | /* |
373 | =for apidoc mg_clear | |
374 | ||
375 | Clear something magical that the SV represents. See C<sv_magic>. | |
376 | ||
377 | =cut | |
378 | */ | |
379 | ||
79072805 | 380 | int |
864dbfa3 | 381 | Perl_mg_clear(pTHX_ SV *sv) |
79072805 | 382 | { |
35a4481c | 383 | const I32 mgs_ix = SSNEW(sizeof(MGS)); |
79072805 | 384 | MAGIC* mg; |
8ac77ac9 | 385 | MAGIC *nextmg; |
463ee0b2 | 386 | |
7918f24d NC |
387 | PERL_ARGS_ASSERT_MG_CLEAR; |
388 | ||
455ece5e | 389 | save_magic(mgs_ix, sv); |
463ee0b2 | 390 | |
8ac77ac9 | 391 | for (mg = SvMAGIC(sv); mg; mg = nextmg) { |
35a4481c | 392 | const MGVTBL* const vtbl = mg->mg_virtual; |
a0d0e21e | 393 | /* omit GSKIP -- never set here */ |
727405f8 | 394 | |
8ac77ac9 NC |
395 | nextmg = mg->mg_moremagic; /* it may delete itself */ |
396 | ||
2b260de0 | 397 | if (vtbl && vtbl->svt_clear) |
16c91539 | 398 | vtbl->svt_clear(aTHX_ sv, mg); |
79072805 | 399 | } |
463ee0b2 | 400 | |
8772537c | 401 | restore_magic(INT2PTR(void*, (IV)mgs_ix)); |
79072805 LW |
402 | return 0; |
403 | } | |
404 | ||
39de7f53 FR |
405 | MAGIC* |
406 | S_mg_findext_flags(pTHX_ const SV *sv, int type, const MGVTBL *vtbl, U32 flags) | |
407 | { | |
408 | PERL_UNUSED_CONTEXT; | |
409 | ||
410 | assert(flags <= 1); | |
411 | ||
412 | if (sv) { | |
413 | MAGIC *mg; | |
414 | ||
415 | for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) { | |
416 | if (mg->mg_type == type && (!flags || mg->mg_virtual == vtbl)) { | |
417 | return mg; | |
418 | } | |
419 | } | |
420 | } | |
421 | ||
422 | return NULL; | |
423 | } | |
424 | ||
954c1994 GS |
425 | /* |
426 | =for apidoc mg_find | |
427 | ||
428 | Finds the magic pointer for type matching the SV. See C<sv_magic>. | |
429 | ||
430 | =cut | |
431 | */ | |
432 | ||
93a17b20 | 433 | MAGIC* |
35a4481c | 434 | Perl_mg_find(pTHX_ const SV *sv, int type) |
93a17b20 | 435 | { |
39de7f53 FR |
436 | return S_mg_findext_flags(aTHX_ sv, type, NULL, 0); |
437 | } | |
438 | ||
439 | /* | |
440 | =for apidoc mg_findext | |
441 | ||
442 | Finds the magic pointer of C<type> with the given C<vtbl> for the C<SV>. See | |
443 | C<sv_magicext>. | |
444 | ||
445 | =cut | |
446 | */ | |
447 | ||
448 | MAGIC* | |
449 | Perl_mg_findext(pTHX_ const SV *sv, int type, const MGVTBL *vtbl) | |
450 | { | |
451 | return S_mg_findext_flags(aTHX_ sv, type, vtbl, 1); | |
93a17b20 LW |
452 | } |
453 | ||
954c1994 GS |
454 | /* |
455 | =for apidoc mg_copy | |
456 | ||
457 | Copies the magic from one SV to another. See C<sv_magic>. | |
458 | ||
459 | =cut | |
460 | */ | |
461 | ||
79072805 | 462 | int |
864dbfa3 | 463 | Perl_mg_copy(pTHX_ SV *sv, SV *nsv, const char *key, I32 klen) |
79072805 | 464 | { |
463ee0b2 | 465 | int count = 0; |
79072805 | 466 | MAGIC* mg; |
7918f24d NC |
467 | |
468 | PERL_ARGS_ASSERT_MG_COPY; | |
469 | ||
463ee0b2 | 470 | for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) { |
35a4481c | 471 | const MGVTBL* const vtbl = mg->mg_virtual; |
68795e93 | 472 | if ((mg->mg_flags & MGf_COPY) && vtbl->svt_copy){ |
16c91539 | 473 | count += vtbl->svt_copy(aTHX_ sv, mg, nsv, key, klen); |
68795e93 | 474 | } |
823a54a3 AL |
475 | else { |
476 | const char type = mg->mg_type; | |
1e73acc8 | 477 | if (isUPPER(type) && type != PERL_MAGIC_uvar) { |
823a54a3 AL |
478 | sv_magic(nsv, |
479 | (type == PERL_MAGIC_tied) | |
480 | ? SvTIED_obj(sv, mg) | |
481 | : (type == PERL_MAGIC_regdata && mg->mg_obj) | |
482 | ? sv | |
483 | : mg->mg_obj, | |
484 | toLOWER(type), key, klen); | |
485 | count++; | |
486 | } | |
79072805 | 487 | } |
79072805 | 488 | } |
463ee0b2 | 489 | return count; |
79072805 LW |
490 | } |
491 | ||
954c1994 | 492 | /* |
0cbee0a4 DM |
493 | =for apidoc mg_localize |
494 | ||
9711599e CS |
495 | Copy some of the magic from an existing SV to new localized version of that |
496 | SV. Container magic (eg %ENV, $1, tie) gets copied, value magic doesn't (eg | |
497 | taint, pos). | |
498 | ||
af7df257 | 499 | If setmagic is false then no set magic will be called on the new (empty) SV. |
9711599e CS |
500 | This typically means that assignment will soon follow (e.g. 'local $x = $y'), |
501 | and that will handle the magic. | |
0cbee0a4 DM |
502 | |
503 | =cut | |
504 | */ | |
505 | ||
506 | void | |
af7df257 | 507 | Perl_mg_localize(pTHX_ SV *sv, SV *nsv, bool setmagic) |
0cbee0a4 | 508 | { |
97aff369 | 509 | dVAR; |
0cbee0a4 | 510 | MAGIC *mg; |
7918f24d NC |
511 | |
512 | PERL_ARGS_ASSERT_MG_LOCALIZE; | |
513 | ||
0cbee0a4 | 514 | for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) { |
2b1b43ea | 515 | const MGVTBL* const vtbl = mg->mg_virtual; |
2767dea0 | 516 | if (!S_is_container_magic(mg)) |
0cbee0a4 | 517 | continue; |
0cbee0a4 | 518 | |
a5063e7c | 519 | if ((mg->mg_flags & MGf_LOCAL) && vtbl->svt_local) |
16c91539 | 520 | (void)vtbl->svt_local(aTHX_ nsv, mg); |
a5063e7c | 521 | else |
0cbee0a4 DM |
522 | sv_magicext(nsv, mg->mg_obj, mg->mg_type, vtbl, |
523 | mg->mg_ptr, mg->mg_len); | |
a5063e7c | 524 | |
0cbee0a4 DM |
525 | /* container types should remain read-only across localization */ |
526 | SvFLAGS(nsv) |= SvREADONLY(sv); | |
527 | } | |
528 | ||
529 | if (SvTYPE(nsv) >= SVt_PVMG && SvMAGIC(nsv)) { | |
530 | SvFLAGS(nsv) |= SvMAGICAL(sv); | |
af7df257 | 531 | if (setmagic) { |
9711599e CS |
532 | PL_localizing = 1; |
533 | SvSETMAGIC(nsv); | |
534 | PL_localizing = 0; | |
535 | } | |
0cbee0a4 DM |
536 | } |
537 | } | |
538 | ||
d9088386 Z |
539 | #define mg_free_struct(sv, mg) S_mg_free_struct(aTHX_ sv, mg) |
540 | static void | |
541 | S_mg_free_struct(pTHX_ SV *sv, MAGIC *mg) | |
542 | { | |
543 | const MGVTBL* const vtbl = mg->mg_virtual; | |
544 | if (vtbl && vtbl->svt_free) | |
545 | vtbl->svt_free(aTHX_ sv, mg); | |
546 | if (mg->mg_ptr && mg->mg_type != PERL_MAGIC_regex_global) { | |
547 | if (mg->mg_len > 0 || mg->mg_type == PERL_MAGIC_utf8) | |
548 | Safefree(mg->mg_ptr); | |
549 | else if (mg->mg_len == HEf_SVKEY) | |
550 | SvREFCNT_dec(MUTABLE_SV(mg->mg_ptr)); | |
551 | } | |
552 | if (mg->mg_flags & MGf_REFCOUNTED) | |
553 | SvREFCNT_dec(mg->mg_obj); | |
554 | Safefree(mg); | |
555 | } | |
556 | ||
0cbee0a4 | 557 | /* |
954c1994 GS |
558 | =for apidoc mg_free |
559 | ||
560 | Free any magic storage used by the SV. See C<sv_magic>. | |
561 | ||
562 | =cut | |
563 | */ | |
564 | ||
79072805 | 565 | int |
864dbfa3 | 566 | Perl_mg_free(pTHX_ SV *sv) |
79072805 LW |
567 | { |
568 | MAGIC* mg; | |
569 | MAGIC* moremagic; | |
7918f24d NC |
570 | |
571 | PERL_ARGS_ASSERT_MG_FREE; | |
572 | ||
79072805 | 573 | for (mg = SvMAGIC(sv); mg; mg = moremagic) { |
79072805 | 574 | moremagic = mg->mg_moremagic; |
d9088386 | 575 | mg_free_struct(sv, mg); |
c826f41b | 576 | SvMAGIC_set(sv, moremagic); |
79072805 | 577 | } |
b162af07 | 578 | SvMAGIC_set(sv, NULL); |
68f8932e | 579 | SvMAGICAL_off(sv); |
79072805 LW |
580 | return 0; |
581 | } | |
582 | ||
d9088386 Z |
583 | /* |
584 | =for apidoc Am|void|mg_free_type|SV *sv|int how | |
585 | ||
586 | Remove any magic of type I<how> from the SV I<sv>. See L</sv_magic>. | |
587 | ||
588 | =cut | |
589 | */ | |
590 | ||
591 | void | |
592 | Perl_mg_free_type(pTHX_ SV *sv, int how) | |
593 | { | |
594 | MAGIC *mg, *prevmg, *moremg; | |
595 | PERL_ARGS_ASSERT_MG_FREE_TYPE; | |
596 | for (prevmg = NULL, mg = SvMAGIC(sv); mg; prevmg = mg, mg = moremg) { | |
597 | MAGIC *newhead; | |
598 | moremg = mg->mg_moremagic; | |
599 | if (mg->mg_type == how) { | |
600 | /* temporarily move to the head of the magic chain, in case | |
601 | custom free code relies on this historical aspect of mg_free */ | |
602 | if (prevmg) { | |
603 | prevmg->mg_moremagic = moremg; | |
604 | mg->mg_moremagic = SvMAGIC(sv); | |
605 | SvMAGIC_set(sv, mg); | |
606 | } | |
607 | newhead = mg->mg_moremagic; | |
608 | mg_free_struct(sv, mg); | |
609 | SvMAGIC_set(sv, newhead); | |
610 | mg = prevmg; | |
611 | } | |
612 | } | |
613 | mg_magical(sv); | |
614 | } | |
615 | ||
79072805 | 616 | #include <signal.h> |
79072805 | 617 | |
942e002e | 618 | U32 |
864dbfa3 | 619 | Perl_magic_regdata_cnt(pTHX_ SV *sv, MAGIC *mg) |
6cef1e77 | 620 | { |
97aff369 | 621 | dVAR; |
8772537c | 622 | PERL_UNUSED_ARG(sv); |
6cef1e77 | 623 | |
7918f24d NC |
624 | PERL_ARGS_ASSERT_MAGIC_REGDATA_CNT; |
625 | ||
0bd48802 AL |
626 | if (PL_curpm) { |
627 | register const REGEXP * const rx = PM_GETRE(PL_curpm); | |
628 | if (rx) { | |
a678b0e4 YO |
629 | if (mg->mg_obj) { /* @+ */ |
630 | /* return the number possible */ | |
07bc277f | 631 | return RX_NPARENS(rx); |
a678b0e4 | 632 | } else { /* @- */ |
07bc277f | 633 | I32 paren = RX_LASTPAREN(rx); |
a678b0e4 YO |
634 | |
635 | /* return the last filled */ | |
f46c4081 | 636 | while ( paren >= 0 |
07bc277f NC |
637 | && (RX_OFFS(rx)[paren].start == -1 |
638 | || RX_OFFS(rx)[paren].end == -1) ) | |
f46c4081 | 639 | paren--; |
a678b0e4 YO |
640 | return (U32)paren; |
641 | } | |
0bd48802 | 642 | } |
8f580fb8 | 643 | } |
ac27b0f5 | 644 | |
942e002e | 645 | return (U32)-1; |
6cef1e77 IZ |
646 | } |
647 | ||
648 | int | |
864dbfa3 | 649 | Perl_magic_regdatum_get(pTHX_ SV *sv, MAGIC *mg) |
6cef1e77 | 650 | { |
97aff369 | 651 | dVAR; |
7918f24d NC |
652 | |
653 | PERL_ARGS_ASSERT_MAGIC_REGDATUM_GET; | |
654 | ||
0bd48802 AL |
655 | if (PL_curpm) { |
656 | register const REGEXP * const rx = PM_GETRE(PL_curpm); | |
657 | if (rx) { | |
658 | register const I32 paren = mg->mg_len; | |
659 | register I32 s; | |
660 | register I32 t; | |
661 | if (paren < 0) | |
662 | return 0; | |
07bc277f NC |
663 | if (paren <= (I32)RX_NPARENS(rx) && |
664 | (s = RX_OFFS(rx)[paren].start) != -1 && | |
665 | (t = RX_OFFS(rx)[paren].end) != -1) | |
0bd48802 AL |
666 | { |
667 | register I32 i; | |
668 | if (mg->mg_obj) /* @+ */ | |
669 | i = t; | |
670 | else /* @- */ | |
671 | i = s; | |
672 | ||
673 | if (i > 0 && RX_MATCH_UTF8(rx)) { | |
07bc277f | 674 | const char * const b = RX_SUBBEG(rx); |
0bd48802 | 675 | if (b) |
f5a63d97 | 676 | i = utf8_length((U8*)b, (U8*)(b+i)); |
0bd48802 | 677 | } |
727405f8 | 678 | |
0bd48802 | 679 | sv_setiv(sv, i); |
1aa99e6b | 680 | } |
0bd48802 | 681 | } |
6cef1e77 IZ |
682 | } |
683 | return 0; | |
684 | } | |
685 | ||
e4b89193 | 686 | int |
a29d06ed MG |
687 | Perl_magic_regdatum_set(pTHX_ SV *sv, MAGIC *mg) |
688 | { | |
7918f24d | 689 | PERL_ARGS_ASSERT_MAGIC_REGDATUM_SET; |
d4c19fe8 AL |
690 | PERL_UNUSED_ARG(sv); |
691 | PERL_UNUSED_ARG(mg); | |
6ad8f254 | 692 | Perl_croak_no_modify(aTHX); |
0dbb1585 | 693 | NORETURN_FUNCTION_END; |
a29d06ed MG |
694 | } |
695 | ||
93a17b20 | 696 | U32 |
864dbfa3 | 697 | Perl_magic_len(pTHX_ SV *sv, MAGIC *mg) |
93a17b20 | 698 | { |
97aff369 | 699 | dVAR; |
93a17b20 | 700 | register I32 paren; |
93a17b20 | 701 | register I32 i; |
2fdbfb4d AB |
702 | register const REGEXP * rx; |
703 | const char * const remaining = mg->mg_ptr + 1; | |
93a17b20 | 704 | |
7918f24d NC |
705 | PERL_ARGS_ASSERT_MAGIC_LEN; |
706 | ||
93a17b20 | 707 | switch (*mg->mg_ptr) { |
2fdbfb4d AB |
708 | case '\020': |
709 | if (*remaining == '\0') { /* ^P */ | |
710 | break; | |
711 | } else if (strEQ(remaining, "REMATCH")) { /* $^PREMATCH */ | |
712 | goto do_prematch; | |
713 | } else if (strEQ(remaining, "OSTMATCH")) { /* $^POSTMATCH */ | |
714 | goto do_postmatch; | |
715 | } | |
716 | break; | |
717 | case '\015': /* $^MATCH */ | |
718 | if (strEQ(remaining, "ATCH")) { | |
719 | goto do_match; | |
720 | } else { | |
721 | break; | |
722 | } | |
723 | case '`': | |
724 | do_prematch: | |
f1b875a0 | 725 | paren = RX_BUFF_IDX_PREMATCH; |
2fdbfb4d AB |
726 | goto maybegetparen; |
727 | case '\'': | |
728 | do_postmatch: | |
f1b875a0 | 729 | paren = RX_BUFF_IDX_POSTMATCH; |
2fdbfb4d AB |
730 | goto maybegetparen; |
731 | case '&': | |
732 | do_match: | |
f1b875a0 | 733 | paren = RX_BUFF_IDX_FULLMATCH; |
2fdbfb4d | 734 | goto maybegetparen; |
93a17b20 | 735 | case '1': case '2': case '3': case '4': |
2fdbfb4d AB |
736 | case '5': case '6': case '7': case '8': case '9': |
737 | paren = atoi(mg->mg_ptr); | |
738 | maybegetparen: | |
aaa362c4 | 739 | if (PL_curpm && (rx = PM_GETRE(PL_curpm))) { |
2fdbfb4d AB |
740 | getparen: |
741 | i = CALLREG_NUMBUF_LENGTH((REGEXP * const)rx, sv, paren); | |
cf93c79d | 742 | |
ffc61ed2 | 743 | if (i < 0) |
0844c848 | 744 | Perl_croak(aTHX_ "panic: magic_len: %"IVdf, (IV)i); |
ffc61ed2 | 745 | return i; |
2fdbfb4d | 746 | } else { |
235bddc8 | 747 | if (ckWARN(WARN_UNINITIALIZED)) |
29489e7c | 748 | report_uninit(sv); |
2fdbfb4d | 749 | return 0; |
93a17b20 | 750 | } |
93a17b20 | 751 | case '+': |
aaa362c4 | 752 | if (PL_curpm && (rx = PM_GETRE(PL_curpm))) { |
07bc277f | 753 | paren = RX_LASTPAREN(rx); |
13f57bf8 CS |
754 | if (paren) |
755 | goto getparen; | |
93a17b20 | 756 | } |
748a9306 | 757 | return 0; |
a01268b5 JH |
758 | case '\016': /* ^N */ |
759 | if (PL_curpm && (rx = PM_GETRE(PL_curpm))) { | |
07bc277f | 760 | paren = RX_LASTCLOSEPAREN(rx); |
a01268b5 JH |
761 | if (paren) |
762 | goto getparen; | |
763 | } | |
764 | return 0; | |
93a17b20 LW |
765 | } |
766 | magic_get(sv,mg); | |
2d8e6c8d | 767 | if (!SvPOK(sv) && SvNIOK(sv)) { |
8b6b16e7 | 768 | sv_2pv(sv, 0); |
2d8e6c8d | 769 | } |
93a17b20 LW |
770 | if (SvPOK(sv)) |
771 | return SvCUR(sv); | |
772 | return 0; | |
773 | } | |
774 | ||
ad3296c6 | 775 | #define SvRTRIM(sv) STMT_START { \ |
eae92ea0 GA |
776 | if (SvPOK(sv)) { \ |
777 | STRLEN len = SvCUR(sv); \ | |
778 | char * const p = SvPVX(sv); \ | |
8e6b4db6 PC |
779 | while (len > 0 && isSPACE(p[len-1])) \ |
780 | --len; \ | |
781 | SvCUR_set(sv, len); \ | |
782 | p[len] = '\0'; \ | |
783 | } \ | |
ad3296c6 SH |
784 | } STMT_END |
785 | ||
8b850bd5 NC |
786 | void |
787 | Perl_emulate_cop_io(pTHX_ const COP *const c, SV *const sv) | |
788 | { | |
7918f24d NC |
789 | PERL_ARGS_ASSERT_EMULATE_COP_IO; |
790 | ||
8b850bd5 NC |
791 | if (!(CopHINTS_get(c) & (HINT_LEXICAL_IO_IN|HINT_LEXICAL_IO_OUT))) |
792 | sv_setsv(sv, &PL_sv_undef); | |
793 | else { | |
794 | sv_setpvs(sv, ""); | |
795 | SvUTF8_off(sv); | |
796 | if ((CopHINTS_get(c) & HINT_LEXICAL_IO_IN)) { | |
20439bc7 | 797 | SV *const value = cop_hints_fetch_pvs(c, "open<", 0); |
8b850bd5 NC |
798 | assert(value); |
799 | sv_catsv(sv, value); | |
800 | } | |
801 | sv_catpvs(sv, "\0"); | |
802 | if ((CopHINTS_get(c) & HINT_LEXICAL_IO_OUT)) { | |
20439bc7 | 803 | SV *const value = cop_hints_fetch_pvs(c, "open>", 0); |
8b850bd5 NC |
804 | assert(value); |
805 | sv_catsv(sv, value); | |
806 | } | |
807 | } | |
808 | } | |
809 | ||
79072805 | 810 | int |
864dbfa3 | 811 | Perl_magic_get(pTHX_ SV *sv, MAGIC *mg) |
79072805 | 812 | { |
27da23d5 | 813 | dVAR; |
79072805 | 814 | register I32 paren; |
db7198b5 | 815 | register const char *s = NULL; |
d9f97599 | 816 | register REGEXP *rx; |
823a54a3 AL |
817 | const char * const remaining = mg->mg_ptr + 1; |
818 | const char nextchar = *remaining; | |
79072805 | 819 | |
7918f24d NC |
820 | PERL_ARGS_ASSERT_MAGIC_GET; |
821 | ||
79072805 | 822 | switch (*mg->mg_ptr) { |
748a9306 | 823 | case '\001': /* ^A */ |
3280af22 | 824 | sv_setsv(sv, PL_bodytarget); |
125b9982 NT |
825 | if (SvTAINTED(PL_bodytarget)) |
826 | SvTAINTED_on(sv); | |
748a9306 | 827 | break; |
e5218da5 | 828 | case '\003': /* ^C, ^CHILD_ERROR_NATIVE */ |
823a54a3 | 829 | if (nextchar == '\0') { |
e5218da5 GA |
830 | sv_setiv(sv, (IV)PL_minus_c); |
831 | } | |
823a54a3 | 832 | else if (strEQ(remaining, "HILD_ERROR_NATIVE")) { |
e5218da5 GA |
833 | sv_setiv(sv, (IV)STATUS_NATIVE); |
834 | } | |
49460fe6 NIS |
835 | break; |
836 | ||
79072805 | 837 | case '\004': /* ^D */ |
aea4f609 | 838 | sv_setiv(sv, (IV)(PL_debug & DEBUG_MASK)); |
79072805 | 839 | break; |
28f23441 | 840 | case '\005': /* ^E */ |
823a54a3 | 841 | if (nextchar == '\0') { |
e37778c2 | 842 | #if defined(VMS) |
0a378802 JH |
843 | { |
844 | # include <descrip.h> | |
845 | # include <starlet.h> | |
846 | char msg[255]; | |
847 | $DESCRIPTOR(msgdsc,msg); | |
848 | sv_setnv(sv,(NV) vaxc$errno); | |
849 | if (sys$getmsg(vaxc$errno,&msgdsc.dsc$w_length,&msgdsc,0,0) & 1) | |
850 | sv_setpvn(sv,msgdsc.dsc$a_pointer,msgdsc.dsc$w_length); | |
851 | else | |
76f68e9b | 852 | sv_setpvs(sv,""); |
0a378802 | 853 | } |
4b645107 | 854 | #elif defined(OS2) |
0a378802 JH |
855 | if (!(_emx_env & 0x200)) { /* Under DOS */ |
856 | sv_setnv(sv, (NV)errno); | |
857 | sv_setpv(sv, errno ? Strerror(errno) : ""); | |
858 | } else { | |
859 | if (errno != errno_isOS2) { | |
823a54a3 | 860 | const int tmp = _syserrno(); |
0a378802 JH |
861 | if (tmp) /* 2nd call to _syserrno() makes it 0 */ |
862 | Perl_rc = tmp; | |
863 | } | |
864 | sv_setnv(sv, (NV)Perl_rc); | |
865 | sv_setpv(sv, os2error(Perl_rc)); | |
866 | } | |
4b645107 | 867 | #elif defined(WIN32) |
0a378802 | 868 | { |
d4c19fe8 | 869 | const DWORD dwErr = GetLastError(); |
0a378802 | 870 | sv_setnv(sv, (NV)dwErr); |
823a54a3 | 871 | if (dwErr) { |
0a378802 JH |
872 | PerlProc_GetOSError(sv, dwErr); |
873 | } | |
874 | else | |
76f68e9b | 875 | sv_setpvs(sv, ""); |
0a378802 JH |
876 | SetLastError(dwErr); |
877 | } | |
22fae026 | 878 | #else |
f6c8f21d | 879 | { |
4ee39169 | 880 | dSAVE_ERRNO; |
f6c8f21d | 881 | sv_setnv(sv, (NV)errno); |
666ea192 | 882 | sv_setpv(sv, errno ? Strerror(errno) : ""); |
4ee39169 | 883 | RESTORE_ERRNO; |
f6c8f21d | 884 | } |
28f23441 | 885 | #endif |
ad3296c6 | 886 | SvRTRIM(sv); |
0a378802 JH |
887 | SvNOK_on(sv); /* what a wonderful hack! */ |
888 | } | |
823a54a3 | 889 | else if (strEQ(remaining, "NCODING")) |
0a378802 JH |
890 | sv_setsv(sv, PL_encoding); |
891 | break; | |
79072805 | 892 | case '\006': /* ^F */ |
3280af22 | 893 | sv_setiv(sv, (IV)PL_maxsysfd); |
79072805 | 894 | break; |
9ebf26ad FR |
895 | case '\007': /* ^GLOBAL_PHASE */ |
896 | if (strEQ(remaining, "LOBAL_PHASE")) { | |
897 | sv_setpvn(sv, PL_phase_names[PL_phase], | |
898 | strlen(PL_phase_names[PL_phase])); | |
899 | } | |
900 | break; | |
a0d0e21e | 901 | case '\010': /* ^H */ |
3280af22 | 902 | sv_setiv(sv, (IV)PL_hints); |
a0d0e21e | 903 | break; |
9d116dd7 | 904 | case '\011': /* ^I */ /* NOT \t in EBCDIC */ |
120f7abe | 905 | sv_setpv(sv, PL_inplace); /* Will undefine sv if PL_inplace is NULL */ |
79072805 | 906 | break; |
ac27b0f5 | 907 | case '\017': /* ^O & ^OPEN */ |
823a54a3 | 908 | if (nextchar == '\0') { |
ac27b0f5 | 909 | sv_setpv(sv, PL_osname); |
3511154c DM |
910 | SvTAINTED_off(sv); |
911 | } | |
823a54a3 | 912 | else if (strEQ(remaining, "PEN")) { |
8b850bd5 | 913 | Perl_emulate_cop_io(aTHX_ &PL_compiling, sv); |
ac27b0f5 | 914 | } |
28f23441 | 915 | break; |
9ebf26ad | 916 | case '\020': |
cde0cee5 YO |
917 | if (nextchar == '\0') { /* ^P */ |
918 | sv_setiv(sv, (IV)PL_perldb); | |
919 | } else if (strEQ(remaining, "REMATCH")) { /* $^PREMATCH */ | |
920 | goto do_prematch_fetch; | |
921 | } else if (strEQ(remaining, "OSTMATCH")) { /* $^POSTMATCH */ | |
922 | goto do_postmatch_fetch; | |
923 | } | |
79072805 | 924 | break; |
fb73857a | 925 | case '\023': /* ^S */ |
823a54a3 | 926 | if (nextchar == '\0') { |
bc177e6b | 927 | if (PL_parser && PL_parser->lex_state != LEX_NOTPARSING) |
0c34ef67 | 928 | SvOK_off(sv); |
3280af22 | 929 | else if (PL_in_eval) |
6dc8a9e4 | 930 | sv_setiv(sv, PL_in_eval & ~(EVAL_INREQUIRE)); |
a4268c0a AMS |
931 | else |
932 | sv_setiv(sv, 0); | |
d58bf5aa | 933 | } |
fb73857a | 934 | break; |
79072805 | 935 | case '\024': /* ^T */ |
823a54a3 | 936 | if (nextchar == '\0') { |
88e89b8a | 937 | #ifdef BIG_TIME |
7c36658b | 938 | sv_setnv(sv, PL_basetime); |
88e89b8a | 939 | #else |
7c36658b | 940 | sv_setiv(sv, (IV)PL_basetime); |
88e89b8a | 941 | #endif |
7c36658b | 942 | } |
823a54a3 | 943 | else if (strEQ(remaining, "AINT")) |
9aa05f58 RGS |
944 | sv_setiv(sv, PL_tainting |
945 | ? (PL_taint_warn || PL_unsafe ? -1 : 1) | |
946 | : 0); | |
7c36658b | 947 | break; |
e07ea26a | 948 | case '\025': /* $^UNICODE, $^UTF8LOCALE, $^UTF8CACHE */ |
823a54a3 | 949 | if (strEQ(remaining, "NICODE")) |
a05d7ebb | 950 | sv_setuv(sv, (UV) PL_unicode); |
823a54a3 | 951 | else if (strEQ(remaining, "TF8LOCALE")) |
7cebcbc0 | 952 | sv_setuv(sv, (UV) PL_utf8locale); |
e07ea26a NC |
953 | else if (strEQ(remaining, "TF8CACHE")) |
954 | sv_setiv(sv, (IV) PL_utf8cache); | |
fde18df1 JH |
955 | break; |
956 | case '\027': /* ^W & $^WARNING_BITS */ | |
823a54a3 | 957 | if (nextchar == '\0') |
4438c4b7 | 958 | sv_setiv(sv, (IV)((PL_dowarn & G_WARN_ON) ? TRUE : FALSE)); |
823a54a3 | 959 | else if (strEQ(remaining, "ARNING_BITS")) { |
013b78e8 | 960 | if (PL_compiling.cop_warnings == pWARN_NONE) { |
4438c4b7 | 961 | sv_setpvn(sv, WARN_NONEstring, WARNsize) ; |
013b78e8 RGS |
962 | } |
963 | else if (PL_compiling.cop_warnings == pWARN_STD) { | |
666ea192 JH |
964 | sv_setpvn( |
965 | sv, | |
966 | (PL_dowarn & G_WARN_ON) ? WARN_ALLstring : WARN_NONEstring, | |
967 | WARNsize | |
968 | ); | |
013b78e8 | 969 | } |
d3a7d8c7 | 970 | else if (PL_compiling.cop_warnings == pWARN_ALL) { |
75b6c4ca RGS |
971 | /* Get the bit mask for $warnings::Bits{all}, because |
972 | * it could have been extended by warnings::register */ | |
6673a63c | 973 | HV * const bits=get_hv("warnings::Bits", 0); |
f5a63d97 AL |
974 | if (bits) { |
975 | SV ** const bits_all = hv_fetchs(bits, "all", FALSE); | |
976 | if (bits_all) | |
977 | sv_setsv(sv, *bits_all); | |
75b6c4ca RGS |
978 | } |
979 | else { | |
980 | sv_setpvn(sv, WARN_ALLstring, WARNsize) ; | |
981 | } | |
ac27b0f5 | 982 | } |
4438c4b7 | 983 | else { |
72dc9ed5 NC |
984 | sv_setpvn(sv, (char *) (PL_compiling.cop_warnings + 1), |
985 | *PL_compiling.cop_warnings); | |
ac27b0f5 | 986 | } |
d3a7d8c7 | 987 | SvPOK_only(sv); |
4438c4b7 | 988 | } |
79072805 | 989 | break; |
cde0cee5 YO |
990 | case '\015': /* $^MATCH */ |
991 | if (strEQ(remaining, "ATCH")) { | |
79072805 LW |
992 | case '1': case '2': case '3': case '4': |
993 | case '5': case '6': case '7': case '8': case '9': case '&': | |
cde0cee5 YO |
994 | if (PL_curpm && (rx = PM_GETRE(PL_curpm))) { |
995 | /* | |
159b6efe | 996 | * Pre-threads, this was paren = atoi(GvENAME((const GV *)mg->mg_obj)); |
cde0cee5 YO |
997 | * XXX Does the new way break anything? |
998 | */ | |
999 | paren = atoi(mg->mg_ptr); /* $& is in [0] */ | |
2fdbfb4d | 1000 | CALLREG_NUMBUF_FETCH(rx,paren,sv); |
cde0cee5 YO |
1001 | break; |
1002 | } | |
1003 | sv_setsv(sv,&PL_sv_undef); | |
79072805 LW |
1004 | } |
1005 | break; | |
1006 | case '+': | |
aaa362c4 | 1007 | if (PL_curpm && (rx = PM_GETRE(PL_curpm))) { |
07bc277f NC |
1008 | if (RX_LASTPAREN(rx)) { |
1009 | CALLREG_NUMBUF_FETCH(rx,RX_LASTPAREN(rx),sv); | |
44a2ac75 YO |
1010 | break; |
1011 | } | |
79072805 | 1012 | } |
3280af22 | 1013 | sv_setsv(sv,&PL_sv_undef); |
79072805 | 1014 | break; |
a01268b5 JH |
1015 | case '\016': /* ^N */ |
1016 | if (PL_curpm && (rx = PM_GETRE(PL_curpm))) { | |
07bc277f NC |
1017 | if (RX_LASTCLOSEPAREN(rx)) { |
1018 | CALLREG_NUMBUF_FETCH(rx,RX_LASTCLOSEPAREN(rx),sv); | |
44a2ac75 YO |
1019 | break; |
1020 | } | |
1021 | ||
a01268b5 JH |
1022 | } |
1023 | sv_setsv(sv,&PL_sv_undef); | |
1024 | break; | |
79072805 | 1025 | case '`': |
cde0cee5 | 1026 | do_prematch_fetch: |
aaa362c4 | 1027 | if (PL_curpm && (rx = PM_GETRE(PL_curpm))) { |
2fdbfb4d | 1028 | CALLREG_NUMBUF_FETCH(rx,-2,sv); |
93b32b6d | 1029 | break; |
79072805 | 1030 | } |
3280af22 | 1031 | sv_setsv(sv,&PL_sv_undef); |
79072805 LW |
1032 | break; |
1033 | case '\'': | |
cde0cee5 | 1034 | do_postmatch_fetch: |
aaa362c4 | 1035 | if (PL_curpm && (rx = PM_GETRE(PL_curpm))) { |
2fdbfb4d | 1036 | CALLREG_NUMBUF_FETCH(rx,-1,sv); |
93b32b6d | 1037 | break; |
79072805 | 1038 | } |
3280af22 | 1039 | sv_setsv(sv,&PL_sv_undef); |
79072805 LW |
1040 | break; |
1041 | case '.': | |
3280af22 | 1042 | if (GvIO(PL_last_in_gv)) { |
357c8808 | 1043 | sv_setiv(sv, (IV)IoLINES(GvIOp(PL_last_in_gv))); |
79072805 | 1044 | } |
79072805 LW |
1045 | break; |
1046 | case '?': | |
809a5acc | 1047 | { |
809a5acc | 1048 | sv_setiv(sv, (IV)STATUS_CURRENT); |
ff0cee69 | 1049 | #ifdef COMPLEX_STATUS |
41cb7b2b | 1050 | SvUPGRADE(sv, SVt_PVLV); |
6b88bc9c GS |
1051 | LvTARGOFF(sv) = PL_statusvalue; |
1052 | LvTARGLEN(sv) = PL_statusvalue_vms; | |
ff0cee69 | 1053 | #endif |
809a5acc | 1054 | } |
79072805 LW |
1055 | break; |
1056 | case '^': | |
099be4f1 DM |
1057 | if (!isGV_with_GP(PL_defoutgv)) |
1058 | s = ""; | |
1059 | else if (GvIOp(PL_defoutgv)) | |
1060 | s = IoTOP_NAME(GvIOp(PL_defoutgv)); | |
79072805 LW |
1061 | if (s) |
1062 | sv_setpv(sv,s); | |
1063 | else { | |
3280af22 | 1064 | sv_setpv(sv,GvENAME(PL_defoutgv)); |
cb421d5e | 1065 | sv_catpvs(sv,"_TOP"); |
79072805 LW |
1066 | } |
1067 | break; | |
1068 | case '~': | |
099be4f1 DM |
1069 | if (!isGV_with_GP(PL_defoutgv)) |
1070 | s = ""; | |
1071 | else if (GvIOp(PL_defoutgv)) | |
0daa599b | 1072 | s = IoFMT_NAME(GvIOp(PL_defoutgv)); |
79072805 | 1073 | if (!s) |
3280af22 | 1074 | s = GvENAME(PL_defoutgv); |
79072805 LW |
1075 | sv_setpv(sv,s); |
1076 | break; | |
79072805 | 1077 | case '=': |
099be4f1 | 1078 | if (GvIO(PL_defoutgv)) |
0daa599b | 1079 | sv_setiv(sv, (IV)IoPAGE_LEN(GvIOp(PL_defoutgv))); |
79072805 LW |
1080 | break; |
1081 | case '-': | |
099be4f1 | 1082 | if (GvIO(PL_defoutgv)) |
0daa599b | 1083 | sv_setiv(sv, (IV)IoLINES_LEFT(GvIOp(PL_defoutgv))); |
79072805 LW |
1084 | break; |
1085 | case '%': | |
099be4f1 | 1086 | if (GvIO(PL_defoutgv)) |
0daa599b | 1087 | sv_setiv(sv, (IV)IoPAGE(GvIOp(PL_defoutgv))); |
79072805 | 1088 | break; |
79072805 LW |
1089 | case ':': |
1090 | break; | |
1091 | case '/': | |
1092 | break; | |
1093 | case '[': | |
11206fdd | 1094 | sv_setiv(sv, (IV)CopARYBASE_get(PL_curcop)); |
79072805 LW |
1095 | break; |
1096 | case '|': | |
099be4f1 | 1097 | if (GvIO(PL_defoutgv)) |
0daa599b | 1098 | sv_setiv(sv, (IV)(IoFLAGS(GvIOp(PL_defoutgv)) & IOf_FLUSH) != 0 ); |
79072805 | 1099 | break; |
79072805 | 1100 | case '\\': |
b2ce0fda | 1101 | if (PL_ors_sv) |
f28098ff | 1102 | sv_copypv(sv, PL_ors_sv); |
79072805 | 1103 | break; |
79072805 | 1104 | case '!': |
666d8aa2 CB |
1105 | { |
1106 | dSAVE_ERRNO; | |
a5f75d66 | 1107 | #ifdef VMS |
65202027 | 1108 | sv_setnv(sv, (NV)((errno == EVMSERR) ? vaxc$errno : errno)); |
a5f75d66 | 1109 | #else |
65202027 | 1110 | sv_setnv(sv, (NV)errno); |
666d8aa2 | 1111 | #endif |
88e89b8a | 1112 | #ifdef OS2 |
ed344e4f IZ |
1113 | if (errno == errno_isOS2 || errno == errno_isOS2_set) |
1114 | sv_setpv(sv, os2error(Perl_rc)); | |
88e89b8a | 1115 | else |
a5f75d66 | 1116 | #endif |
666ea192 | 1117 | sv_setpv(sv, errno ? Strerror(errno) : ""); |
be1cf43c NC |
1118 | if (SvPOKp(sv)) |
1119 | SvPOK_on(sv); /* may have got removed during taint processing */ | |
4ee39169 | 1120 | RESTORE_ERRNO; |
88e89b8a | 1121 | } |
666d8aa2 | 1122 | |
ad3296c6 | 1123 | SvRTRIM(sv); |
946ec16e | 1124 | SvNOK_on(sv); /* what a wonderful hack! */ |
79072805 LW |
1125 | break; |
1126 | case '<': | |
3280af22 | 1127 | sv_setiv(sv, (IV)PL_uid); |
79072805 LW |
1128 | break; |
1129 | case '>': | |
3280af22 | 1130 | sv_setiv(sv, (IV)PL_euid); |
79072805 LW |
1131 | break; |
1132 | case '(': | |
3280af22 | 1133 | sv_setiv(sv, (IV)PL_gid); |
79072805 LW |
1134 | goto add_groups; |
1135 | case ')': | |
3280af22 | 1136 | sv_setiv(sv, (IV)PL_egid); |
79072805 | 1137 | add_groups: |
79072805 | 1138 | #ifdef HAS_GETGROUPS |
79072805 | 1139 | { |
57d7c65e | 1140 | Groups_t *gary = NULL; |
fb45abb2 | 1141 | I32 i, num_groups = getgroups(0, gary); |
57d7c65e JC |
1142 | Newx(gary, num_groups, Groups_t); |
1143 | num_groups = getgroups(num_groups, gary); | |
fb45abb2 GA |
1144 | for (i = 0; i < num_groups; i++) |
1145 | Perl_sv_catpvf(aTHX_ sv, " %"IVdf, (IV)gary[i]); | |
57d7c65e | 1146 | Safefree(gary); |
79072805 | 1147 | } |
155aba94 | 1148 | (void)SvIOK_on(sv); /* what a wonderful hack! */ |
cd70abae | 1149 | #endif |
79072805 | 1150 | break; |
79072805 LW |
1151 | case '0': |
1152 | break; | |
1153 | } | |
a0d0e21e | 1154 | return 0; |
79072805 LW |
1155 | } |
1156 | ||
1157 | int | |
864dbfa3 | 1158 | Perl_magic_getuvar(pTHX_ SV *sv, MAGIC *mg) |
79072805 | 1159 | { |
8772537c | 1160 | struct ufuncs * const uf = (struct ufuncs *)mg->mg_ptr; |
79072805 | 1161 | |
7918f24d NC |
1162 | PERL_ARGS_ASSERT_MAGIC_GETUVAR; |
1163 | ||
79072805 | 1164 | if (uf && uf->uf_val) |
24f81a43 | 1165 | (*uf->uf_val)(aTHX_ uf->uf_index, sv); |
79072805 LW |
1166 | return 0; |
1167 | } | |
1168 | ||
1169 | int | |
864dbfa3 | 1170 | Perl_magic_setenv(pTHX_ SV *sv, MAGIC *mg) |
79072805 | 1171 | { |
27da23d5 | 1172 | dVAR; |
9ae3433d | 1173 | STRLEN len = 0, klen; |
666ea192 | 1174 | const char *s = SvOK(sv) ? SvPV_const(sv,len) : ""; |
fabdb6c0 | 1175 | const char * const ptr = MgPV_const(mg,klen); |
88e89b8a | 1176 | my_setenv(ptr, s); |
1e422769 | 1177 | |
7918f24d NC |
1178 | PERL_ARGS_ASSERT_MAGIC_SETENV; |
1179 | ||
a0d0e21e LW |
1180 | #ifdef DYNAMIC_ENV_FETCH |
1181 | /* We just undefd an environment var. Is a replacement */ | |
1182 | /* waiting in the wings? */ | |
1183 | if (!len) { | |
fabdb6c0 AL |
1184 | SV ** const valp = hv_fetch(GvHVn(PL_envgv), ptr, klen, FALSE); |
1185 | if (valp) | |
4ab59fcc | 1186 | s = SvOK(*valp) ? SvPV_const(*valp, len) : ""; |
a0d0e21e LW |
1187 | } |
1188 | #endif | |
1e422769 | 1189 | |
39e571d4 | 1190 | #if !defined(OS2) && !defined(AMIGAOS) && !defined(WIN32) && !defined(MSDOS) |
79072805 LW |
1191 | /* And you'll never guess what the dog had */ |
1192 | /* in its mouth... */ | |
3280af22 | 1193 | if (PL_tainting) { |
1e422769 | 1194 | MgTAINTEDDIR_off(mg); |
1195 | #ifdef VMS | |
5aabfad6 | 1196 | if (s && klen == 8 && strEQ(ptr, "DCL$PATH")) { |
b8ffc8df | 1197 | char pathbuf[256], eltbuf[256], *cp, *elt; |
1e422769 | 1198 | int i = 0, j = 0; |
1199 | ||
6fca0082 | 1200 | my_strlcpy(eltbuf, s, sizeof(eltbuf)); |
b8ffc8df | 1201 | elt = eltbuf; |
1e422769 | 1202 | do { /* DCL$PATH may be a search list */ |
1203 | while (1) { /* as may dev portion of any element */ | |
1204 | if ( ((cp = strchr(elt,'[')) || (cp = strchr(elt,'<'))) ) { | |
1205 | if ( *(cp+1) == '.' || *(cp+1) == '-' || | |
1206 | cando_by_name(S_IWUSR,0,elt) ) { | |
1207 | MgTAINTEDDIR_on(mg); | |
1208 | return 0; | |
1209 | } | |
1210 | } | |
bd61b366 | 1211 | if ((cp = strchr(elt, ':')) != NULL) |
1e422769 | 1212 | *cp = '\0'; |
1213 | if (my_trnlnm(elt, eltbuf, j++)) | |
1214 | elt = eltbuf; | |
1215 | else | |
1216 | break; | |
1217 | } | |
1218 | j = 0; | |
1219 | } while (my_trnlnm(s, pathbuf, i++) && (elt = pathbuf)); | |
1220 | } | |
1221 | #endif /* VMS */ | |
5aabfad6 | 1222 | if (s && klen == 4 && strEQ(ptr,"PATH")) { |
8772537c | 1223 | const char * const strend = s + len; |
463ee0b2 LW |
1224 | |
1225 | while (s < strend) { | |
96827780 | 1226 | char tmpbuf[256]; |
c623ac67 | 1227 | Stat_t st; |
5f74f29c | 1228 | I32 i; |
f5a63d97 AL |
1229 | #ifdef VMS /* Hmm. How do we get $Config{path_sep} from C? */ |
1230 | const char path_sep = '|'; | |
1231 | #else | |
1232 | const char path_sep = ':'; | |
1233 | #endif | |
96827780 | 1234 | s = delimcpy(tmpbuf, tmpbuf + sizeof tmpbuf, |
427eaa01 | 1235 | s, strend, path_sep, &i); |
463ee0b2 | 1236 | s++; |
bb7a0f54 | 1237 | if (i >= (I32)sizeof tmpbuf /* too long -- assume the worst */ |
326b5008 CB |
1238 | #ifdef VMS |
1239 | || !strchr(tmpbuf, ':') /* no colon thus no device name -- assume relative path */ | |
1240 | #else | |
1241 | || *tmpbuf != '/' /* no starting slash -- assume relative path */ | |
1242 | #endif | |
c6ed36e1 | 1243 | || (PerlLIO_stat(tmpbuf, &st) == 0 && (st.st_mode & 2)) ) { |
8990e307 | 1244 | MgTAINTEDDIR_on(mg); |
1e422769 | 1245 | return 0; |
1246 | } | |
463ee0b2 | 1247 | } |
79072805 LW |
1248 | } |
1249 | } | |
39e571d4 | 1250 | #endif /* neither OS2 nor AMIGAOS nor WIN32 nor MSDOS */ |
1e422769 | 1251 | |
79072805 LW |
1252 | return 0; |
1253 | } | |
1254 | ||
1255 | int | |
864dbfa3 | 1256 | Perl_magic_clearenv(pTHX_ SV *sv, MAGIC *mg) |
85e6fe83 | 1257 | { |
7918f24d | 1258 | PERL_ARGS_ASSERT_MAGIC_CLEARENV; |
8772537c | 1259 | PERL_UNUSED_ARG(sv); |
bd61b366 | 1260 | my_setenv(MgPV_nolen_const(mg),NULL); |
85e6fe83 LW |
1261 | return 0; |
1262 | } | |
1263 | ||
88e89b8a | 1264 | int |
864dbfa3 | 1265 | Perl_magic_set_all_env(pTHX_ SV *sv, MAGIC *mg) |
fb73857a | 1266 | { |
97aff369 | 1267 | dVAR; |
7918f24d | 1268 | PERL_ARGS_ASSERT_MAGIC_SET_ALL_ENV; |
65e66c80 | 1269 | PERL_UNUSED_ARG(mg); |
b0269e46 | 1270 | #if defined(VMS) |
cea2e8a9 | 1271 | Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system"); |
fb73857a | 1272 | #else |
3280af22 | 1273 | if (PL_localizing) { |
fb73857a | 1274 | HE* entry; |
b0269e46 | 1275 | my_clearenv(); |
85fbaab2 NC |
1276 | hv_iterinit(MUTABLE_HV(sv)); |
1277 | while ((entry = hv_iternext(MUTABLE_HV(sv)))) { | |
fb73857a | 1278 | I32 keylen; |
1279 | my_setenv(hv_iterkey(entry, &keylen), | |
85fbaab2 | 1280 | SvPV_nolen_const(hv_iterval(MUTABLE_HV(sv), entry))); |
fb73857a | 1281 | } |
1282 | } | |
1283 | #endif | |
1284 | return 0; | |
1285 | } | |
1286 | ||
1287 | int | |
864dbfa3 | 1288 | Perl_magic_clear_all_env(pTHX_ SV *sv, MAGIC *mg) |
66b1d557 | 1289 | { |
27da23d5 | 1290 | dVAR; |
7918f24d | 1291 | PERL_ARGS_ASSERT_MAGIC_CLEAR_ALL_ENV; |
8772537c AL |
1292 | PERL_UNUSED_ARG(sv); |
1293 | PERL_UNUSED_ARG(mg); | |
b0269e46 AB |
1294 | #if defined(VMS) |
1295 | Perl_die(aTHX_ "Can't make list assignment to %%ENV on this system"); | |
1296 | #else | |
1297 | my_clearenv(); | |
1298 | #endif | |
3e3baf6d | 1299 | return 0; |
66b1d557 HM |
1300 | } |
1301 | ||
64ca3a65 | 1302 | #ifndef PERL_MICRO |
2d4fcd5e AJ |
1303 | #ifdef HAS_SIGPROCMASK |
1304 | static void | |
1305 | restore_sigmask(pTHX_ SV *save_sv) | |
1306 | { | |
0bd48802 | 1307 | const sigset_t * const ossetp = (const sigset_t *) SvPV_nolen_const( save_sv ); |
f5a63d97 | 1308 | (void)sigprocmask(SIG_SETMASK, ossetp, NULL); |
2d4fcd5e AJ |
1309 | } |
1310 | #endif | |
66b1d557 | 1311 | int |
864dbfa3 | 1312 | Perl_magic_getsig(pTHX_ SV *sv, MAGIC *mg) |
88e89b8a | 1313 | { |
97aff369 | 1314 | dVAR; |
88e89b8a | 1315 | /* Are we fetching a signal entry? */ |
708854f2 | 1316 | int i = (I16)mg->mg_private; |
7918f24d NC |
1317 | |
1318 | PERL_ARGS_ASSERT_MAGIC_GETSIG; | |
1319 | ||
708854f2 NC |
1320 | if (!i) { |
1321 | mg->mg_private = i = whichsig(MgPV_nolen_const(mg)); | |
1322 | } | |
1323 | ||
e02bfb16 | 1324 | if (i > 0) { |
22c35a8c GS |
1325 | if(PL_psig_ptr[i]) |
1326 | sv_setsv(sv,PL_psig_ptr[i]); | |
88e89b8a | 1327 | else { |
46da273f | 1328 | Sighandler_t sigstate = rsignal_state(i); |
23ada85b | 1329 | #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS |
46da273f AL |
1330 | if (PL_sig_handlers_initted && PL_sig_ignoring[i]) |
1331 | sigstate = SIG_IGN; | |
2e34cc90 CL |
1332 | #endif |
1333 | #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS | |
46da273f AL |
1334 | if (PL_sig_handlers_initted && PL_sig_defaulting[i]) |
1335 | sigstate = SIG_DFL; | |
85b332e2 | 1336 | #endif |
88e89b8a | 1337 | /* cache state so we don't fetch it again */ |
8aad04aa | 1338 | if(sigstate == (Sighandler_t) SIG_IGN) |
6502358f | 1339 | sv_setpvs(sv,"IGNORE"); |
88e89b8a | 1340 | else |
3280af22 | 1341 | sv_setsv(sv,&PL_sv_undef); |
46da273f | 1342 | PL_psig_ptr[i] = SvREFCNT_inc_simple_NN(sv); |
88e89b8a | 1343 | SvTEMP_off(sv); |
1344 | } | |
1345 | } | |
1346 | return 0; | |
1347 | } | |
1348 | int | |
864dbfa3 | 1349 | Perl_magic_clearsig(pTHX_ SV *sv, MAGIC *mg) |
88e89b8a | 1350 | { |
7918f24d | 1351 | PERL_ARGS_ASSERT_MAGIC_CLEARSIG; |
8772537c | 1352 | PERL_UNUSED_ARG(sv); |
179c85a2 | 1353 | |
38a124f0 | 1354 | magic_setsig(NULL, mg); |
179c85a2 | 1355 | return sv_unmagic(sv, mg->mg_type); |
88e89b8a | 1356 | } |
3d37d572 | 1357 | |
0a8e0eff | 1358 | Signal_t |
8aad04aa | 1359 | #if defined(HAS_SIGACTION) && defined(SA_SIGINFO) |
b6455c53 | 1360 | Perl_csighandler(int sig, siginfo_t *sip PERL_UNUSED_DECL, void *uap PERL_UNUSED_DECL) |
8aad04aa | 1361 | #else |
0a8e0eff | 1362 | Perl_csighandler(int sig) |
8aad04aa | 1363 | #endif |
0a8e0eff | 1364 | { |
1018e26f NIS |
1365 | #ifdef PERL_GET_SIG_CONTEXT |
1366 | dTHXa(PERL_GET_SIG_CONTEXT); | |
1367 | #else | |
85b332e2 CL |
1368 | dTHX; |
1369 | #endif | |
23ada85b | 1370 | #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS |
5c1546dc | 1371 | (void) rsignal(sig, PL_csighandlerp); |
27da23d5 | 1372 | if (PL_sig_ignoring[sig]) return; |
85b332e2 | 1373 | #endif |
2e34cc90 | 1374 | #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS |
27da23d5 | 1375 | if (PL_sig_defaulting[sig]) |
2e34cc90 CL |
1376 | #ifdef KILL_BY_SIGPRC |
1377 | exit((Perl_sig_to_vmscondition(sig)&STS$M_COND_ID)|STS$K_SEVERE|STS$M_INHIB_MSG); | |
1378 | #else | |
1379 | exit(1); | |
1380 | #endif | |
1381 | #endif | |
406878dd | 1382 | if ( |
853d2c32 RGS |
1383 | #ifdef SIGILL |
1384 | sig == SIGILL || | |
1385 | #endif | |
1386 | #ifdef SIGBUS | |
1387 | sig == SIGBUS || | |
1388 | #endif | |
1389 | #ifdef SIGSEGV | |
1390 | sig == SIGSEGV || | |
1391 | #endif | |
1392 | (PL_signals & PERL_SIGNALS_UNSAFE_FLAG)) | |
4ffa73a3 | 1393 | /* Call the perl level handler now-- |
31c91b43 | 1394 | * with risk we may be in malloc() or being destructed etc. */ |
6e324d09 | 1395 | #if defined(HAS_SIGACTION) && defined(SA_SIGINFO) |
80626cf1 | 1396 | (*PL_sighandlerp)(sig, NULL, NULL); |
6e324d09 CB |
1397 | #else |
1398 | (*PL_sighandlerp)(sig); | |
92807b6d | 1399 | #endif |
406878dd | 1400 | else { |
31c91b43 | 1401 | if (!PL_psig_pend) return; |
406878dd GA |
1402 | /* Set a flag to say this signal is pending, that is awaiting delivery after |
1403 | * the current Perl opcode completes */ | |
1404 | PL_psig_pend[sig]++; | |
1405 | ||
1406 | #ifndef SIG_PENDING_DIE_COUNT | |
1407 | # define SIG_PENDING_DIE_COUNT 120 | |
1408 | #endif | |
fe13d51d | 1409 | /* Add one to say _a_ signal is pending */ |
406878dd GA |
1410 | if (++PL_sig_pending >= SIG_PENDING_DIE_COUNT) |
1411 | Perl_croak(aTHX_ "Maximal count of pending signals (%lu) exceeded", | |
1412 | (unsigned long)SIG_PENDING_DIE_COUNT); | |
1413 | } | |
0a8e0eff NIS |
1414 | } |
1415 | ||
2e34cc90 CL |
1416 | #if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS) || defined(FAKE_DEFAULT_SIGNAL_HANDLERS) |
1417 | void | |
1418 | Perl_csighandler_init(void) | |
1419 | { | |
1420 | int sig; | |
27da23d5 | 1421 | if (PL_sig_handlers_initted) return; |
2e34cc90 CL |
1422 | |
1423 | for (sig = 1; sig < SIG_SIZE; sig++) { | |
1424 | #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS | |
218fdd94 | 1425 | dTHX; |
27da23d5 | 1426 | PL_sig_defaulting[sig] = 1; |
5c1546dc | 1427 | (void) rsignal(sig, PL_csighandlerp); |
2e34cc90 CL |
1428 | #endif |
1429 | #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS | |
27da23d5 | 1430 | PL_sig_ignoring[sig] = 0; |
2e34cc90 CL |
1431 | #endif |
1432 | } | |
27da23d5 | 1433 | PL_sig_handlers_initted = 1; |
2e34cc90 CL |
1434 | } |
1435 | #endif | |
1436 | ||
0a8e0eff NIS |
1437 | void |
1438 | Perl_despatch_signals(pTHX) | |
1439 | { | |
97aff369 | 1440 | dVAR; |
0a8e0eff NIS |
1441 | int sig; |
1442 | PL_sig_pending = 0; | |
1443 | for (sig = 1; sig < SIG_SIZE; sig++) { | |
1444 | if (PL_psig_pend[sig]) { | |
d0166017 | 1445 | dSAVE_ERRNO; |
55534442 LT |
1446 | #if defined(HAS_SIGPROCMASK) |
1447 | /* From sigaction(2) (FreeBSD man page): | |
1448 | * | Signal routines normally execute with the signal that | |
1449 | * | caused their invocation blocked, but other signals may | |
1450 | * | yet occur. | |
1451 | * Emulation of this behavior (from within Perl) is enabled | |
1452 | * using sigprocmask | |
1453 | */ | |
1454 | int was_blocked; | |
1455 | sigset_t newset, oldset; | |
1456 | ||
1457 | sigemptyset(&newset); | |
1458 | sigaddset(&newset, sig); | |
1459 | sigprocmask(SIG_BLOCK, &newset, &oldset); | |
1460 | was_blocked = sigismember(&oldset, sig); | |
1461 | #endif | |
25da4428 | 1462 | PL_psig_pend[sig] = 0; |
6e324d09 | 1463 | #if defined(HAS_SIGACTION) && defined(SA_SIGINFO) |
80626cf1 | 1464 | (*PL_sighandlerp)(sig, NULL, NULL); |
6e324d09 CB |
1465 | #else |
1466 | (*PL_sighandlerp)(sig); | |
92807b6d | 1467 | #endif |
55534442 LT |
1468 | #if defined(HAS_SIGPROCMASK) |
1469 | if (!was_blocked) | |
1470 | sigprocmask(SIG_UNBLOCK, &newset, NULL); | |
1471 | #endif | |
d0166017 | 1472 | RESTORE_ERRNO; |
0a8e0eff NIS |
1473 | } |
1474 | } | |
1475 | } | |
1476 | ||
38a124f0 | 1477 | /* sv of NULL signifies that we're acting as magic_clearsig. */ |
85e6fe83 | 1478 | int |
864dbfa3 | 1479 | Perl_magic_setsig(pTHX_ SV *sv, MAGIC *mg) |
79072805 | 1480 | { |
27da23d5 | 1481 | dVAR; |
79072805 | 1482 | I32 i; |
cbbf8932 | 1483 | SV** svp = NULL; |
2d4fcd5e AJ |
1484 | /* Need to be careful with SvREFCNT_dec(), because that can have side |
1485 | * effects (due to closures). We must make sure that the new disposition | |
1486 | * is in place before it is called. | |
1487 | */ | |
cbbf8932 | 1488 | SV* to_dec = NULL; |
e72dc28c | 1489 | STRLEN len; |
2d4fcd5e AJ |
1490 | #ifdef HAS_SIGPROCMASK |
1491 | sigset_t set, save; | |
1492 | SV* save_sv; | |
1493 | #endif | |
d5263905 | 1494 | register const char *s = MgPV_const(mg,len); |
7918f24d NC |
1495 | |
1496 | PERL_ARGS_ASSERT_MAGIC_SETSIG; | |
1497 | ||
748a9306 LW |
1498 | if (*s == '_') { |
1499 | if (strEQ(s,"__DIE__")) | |
3280af22 | 1500 | svp = &PL_diehook; |
38a124f0 NC |
1501 | else if (strEQ(s,"__WARN__") |
1502 | && (sv ? 1 : PL_warnhook != PERL_WARNHOOK_FATAL)) { | |
1503 | /* Merge the existing behaviours, which are as follows: | |
1504 | magic_setsig, we always set svp to &PL_warnhook | |
1505 | (hence we always change the warnings handler) | |
1506 | For magic_clearsig, we don't change the warnings handler if it's | |
1507 | set to the &PL_warnhook. */ | |
3280af22 | 1508 | svp = &PL_warnhook; |
38a124f0 | 1509 | } else if (sv) |
cea2e8a9 | 1510 | Perl_croak(aTHX_ "No such hook: %s", s); |
748a9306 | 1511 | i = 0; |
38a124f0 | 1512 | if (svp && *svp) { |
9289f461 RGS |
1513 | if (*svp != PERL_WARNHOOK_FATAL) |
1514 | to_dec = *svp; | |
cbbf8932 | 1515 | *svp = NULL; |
4633a7c4 | 1516 | } |
748a9306 LW |
1517 | } |
1518 | else { | |
708854f2 NC |
1519 | i = (I16)mg->mg_private; |
1520 | if (!i) { | |
58a26b12 NC |
1521 | i = whichsig(s); /* ...no, a brick */ |
1522 | mg->mg_private = (U16)i; | |
708854f2 | 1523 | } |
86d86cad | 1524 | if (i <= 0) { |
a2a5de95 NC |
1525 | if (sv) |
1526 | Perl_ck_warner(aTHX_ packWARN(WARN_SIGNAL), "No such signal: SIG%s", s); | |
748a9306 LW |
1527 | return 0; |
1528 | } | |
2d4fcd5e AJ |
1529 | #ifdef HAS_SIGPROCMASK |
1530 | /* Avoid having the signal arrive at a bad time, if possible. */ | |
1531 | sigemptyset(&set); | |
1532 | sigaddset(&set,i); | |
1533 | sigprocmask(SIG_BLOCK, &set, &save); | |
1534 | ENTER; | |
9ff8e806 | 1535 | save_sv = newSVpvn((char *)(&save), sizeof(sigset_t)); |
2d4fcd5e AJ |
1536 | SAVEFREESV(save_sv); |
1537 | SAVEDESTRUCTOR_X(restore_sigmask, save_sv); | |
1538 | #endif | |
1539 | PERL_ASYNC_CHECK(); | |
2e34cc90 | 1540 | #if defined(FAKE_PERSISTENT_SIGNAL_HANDLERS) || defined(FAKE_DEFAULT_SIGNAL_HANDLERS) |
27da23d5 | 1541 | if (!PL_sig_handlers_initted) Perl_csighandler_init(); |
2e34cc90 | 1542 | #endif |
23ada85b | 1543 | #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS |
27da23d5 | 1544 | PL_sig_ignoring[i] = 0; |
85b332e2 | 1545 | #endif |
2e34cc90 | 1546 | #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS |
27da23d5 | 1547 | PL_sig_defaulting[i] = 0; |
2e34cc90 | 1548 | #endif |
2d4fcd5e | 1549 | to_dec = PL_psig_ptr[i]; |
38a124f0 NC |
1550 | if (sv) { |
1551 | PL_psig_ptr[i] = SvREFCNT_inc_simple_NN(sv); | |
1552 | SvTEMP_off(sv); /* Make sure it doesn't go away on us */ | |
79fd3822 NC |
1553 | |
1554 | /* Signals don't change name during the program's execution, so once | |
1555 | they're cached in the appropriate slot of PL_psig_name, they can | |
1556 | stay there. | |
1557 | ||
1558 | Ideally we'd find some way of making SVs at (C) compile time, or | |
1559 | at least, doing most of the work. */ | |
1560 | if (!PL_psig_name[i]) { | |
1561 | PL_psig_name[i] = newSVpvn(s, len); | |
1562 | SvREADONLY_on(PL_psig_name[i]); | |
1563 | } | |
38a124f0 | 1564 | } else { |
79fd3822 | 1565 | SvREFCNT_dec(PL_psig_name[i]); |
38a124f0 NC |
1566 | PL_psig_name[i] = NULL; |
1567 | PL_psig_ptr[i] = NULL; | |
1568 | } | |
748a9306 | 1569 | } |
38a124f0 | 1570 | if (sv && (isGV_with_GP(sv) || SvROK(sv))) { |
2d4fcd5e | 1571 | if (i) { |
5c1546dc | 1572 | (void)rsignal(i, PL_csighandlerp); |
2d4fcd5e | 1573 | } |
748a9306 | 1574 | else |
b37c2d43 | 1575 | *svp = SvREFCNT_inc_simple_NN(sv); |
38a124f0 | 1576 | } else { |
9dfa190b NC |
1577 | if (sv && SvOK(sv)) { |
1578 | s = SvPV_force(sv, len); | |
1579 | } else { | |
1580 | sv = NULL; | |
1581 | } | |
1582 | if (sv && strEQ(s,"IGNORE")) { | |
1583 | if (i) { | |
23ada85b | 1584 | #ifdef FAKE_PERSISTENT_SIGNAL_HANDLERS |
9dfa190b NC |
1585 | PL_sig_ignoring[i] = 1; |
1586 | (void)rsignal(i, PL_csighandlerp); | |
85b332e2 | 1587 | #else |
9dfa190b | 1588 | (void)rsignal(i, (Sighandler_t) SIG_IGN); |
85b332e2 | 1589 | #endif |
9dfa190b | 1590 | } |
2d4fcd5e | 1591 | } |
9dfa190b NC |
1592 | else if (!sv || strEQ(s,"DEFAULT") || !len) { |
1593 | if (i) { | |
2e34cc90 | 1594 | #ifdef FAKE_DEFAULT_SIGNAL_HANDLERS |
9dfa190b NC |
1595 | PL_sig_defaulting[i] = 1; |
1596 | (void)rsignal(i, PL_csighandlerp); | |
2e34cc90 | 1597 | #else |
9dfa190b | 1598 | (void)rsignal(i, (Sighandler_t) SIG_DFL); |
2e34cc90 | 1599 | #endif |
9dfa190b NC |
1600 | } |
1601 | } | |
1602 | else { | |
1603 | /* | |
1604 | * We should warn if HINT_STRICT_REFS, but without | |
1605 | * access to a known hint bit in a known OP, we can't | |
1606 | * tell whether HINT_STRICT_REFS is in force or not. | |
1607 | */ | |
1608 | if (!strchr(s,':') && !strchr(s,'\'')) | |
1609 | Perl_sv_insert_flags(aTHX_ sv, 0, 0, STR_WITH_LEN("main::"), | |
1610 | SV_GMAGIC); | |
1611 | if (i) | |
1612 | (void)rsignal(i, PL_csighandlerp); | |
1613 | else | |
1614 | *svp = SvREFCNT_inc_simple_NN(sv); | |
136e0459 | 1615 | } |
748a9306 | 1616 | } |
9dfa190b | 1617 | |
2d4fcd5e AJ |
1618 | #ifdef HAS_SIGPROCMASK |
1619 | if(i) | |
1620 | LEAVE; | |
1621 | #endif | |
ef8d46e8 | 1622 | SvREFCNT_dec(to_dec); |
79072805 LW |
1623 | return 0; |
1624 | } | |
64ca3a65 | 1625 | #endif /* !PERL_MICRO */ |
79072805 LW |
1626 | |
1627 | int | |
864dbfa3 | 1628 | Perl_magic_setisa(pTHX_ SV *sv, MAGIC *mg) |
79072805 | 1629 | { |
97aff369 | 1630 | dVAR; |
7918f24d | 1631 | PERL_ARGS_ASSERT_MAGIC_SETISA; |
8772537c | 1632 | PERL_UNUSED_ARG(sv); |
e1a479c5 | 1633 | |
89c14e2e | 1634 | /* Skip _isaelem because _isa will handle it shortly */ |
354b0578 | 1635 | if (PL_delaymagic & DM_ARRAY_ISA && mg->mg_type == PERL_MAGIC_isaelem) |
89c14e2e BB |
1636 | return 0; |
1637 | ||
0e446081 | 1638 | return magic_clearisa(NULL, mg); |
463ee0b2 LW |
1639 | } |
1640 | ||
0e446081 | 1641 | /* sv of NULL signifies that we're acting as magic_setisa. */ |
463ee0b2 | 1642 | int |
52b45067 RD |
1643 | Perl_magic_clearisa(pTHX_ SV *sv, MAGIC *mg) |
1644 | { | |
1645 | dVAR; | |
1646 | HV* stash; | |
1647 | ||
7918f24d NC |
1648 | PERL_ARGS_ASSERT_MAGIC_CLEARISA; |
1649 | ||
52b45067 | 1650 | /* Bail out if destruction is going on */ |
627364f1 | 1651 | if(PL_phase == PERL_PHASE_DESTRUCT) return 0; |
52b45067 | 1652 | |
0e446081 NC |
1653 | if (sv) |
1654 | av_clear(MUTABLE_AV(sv)); | |
52b45067 | 1655 | |
6624142a FC |
1656 | if (SvTYPE(mg->mg_obj) != SVt_PVGV && SvSMAGICAL(mg->mg_obj)) |
1657 | /* This occurs with setisa_elem magic, which calls this | |
1658 | same function. */ | |
1659 | mg = mg_find(mg->mg_obj, PERL_MAGIC_isa); | |
1660 | ||
1661 | if (SvTYPE(mg->mg_obj) == SVt_PVAV) { /* multiple stashes */ | |
1662 | SV **svp = AvARRAY((AV *)mg->mg_obj); | |
1663 | I32 items = AvFILLp((AV *)mg->mg_obj) + 1; | |
1664 | while (items--) { | |
1665 | stash = GvSTASH((GV *)*svp++); | |
1666 | if (stash && HvENAME(stash)) mro_isa_changed_in(stash); | |
1667 | } | |
1668 | ||
1669 | return 0; | |
1670 | } | |
1671 | ||
52b45067 | 1672 | stash = GvSTASH( |
6624142a | 1673 | (const GV *)mg->mg_obj |
52b45067 RD |
1674 | ); |
1675 | ||
00169e2c FC |
1676 | /* The stash may have been detached from the symbol table, so check its |
1677 | name before doing anything. */ | |
1678 | if (stash && HvENAME_get(stash)) | |
5562fa71 | 1679 | mro_isa_changed_in(stash); |
52b45067 RD |
1680 | |
1681 | return 0; | |
1682 | } | |
1683 | ||
1684 | int | |
864dbfa3 | 1685 | Perl_magic_setamagic(pTHX_ SV *sv, MAGIC *mg) |
463ee0b2 | 1686 | { |
97aff369 | 1687 | dVAR; |
7918f24d | 1688 | PERL_ARGS_ASSERT_MAGIC_SETAMAGIC; |
8772537c AL |
1689 | PERL_UNUSED_ARG(sv); |
1690 | PERL_UNUSED_ARG(mg); | |
3280af22 | 1691 | PL_amagic_generation++; |
463ee0b2 | 1692 | |
a0d0e21e LW |
1693 | return 0; |
1694 | } | |
463ee0b2 | 1695 | |
946ec16e | 1696 | int |
864dbfa3 | 1697 | Perl_magic_getnkeys(pTHX_ SV *sv, MAGIC *mg) |
6ff81951 | 1698 | { |
85fbaab2 | 1699 | HV * const hv = MUTABLE_HV(LvTARG(sv)); |
6ff81951 | 1700 | I32 i = 0; |
7918f24d NC |
1701 | |
1702 | PERL_ARGS_ASSERT_MAGIC_GETNKEYS; | |
8772537c | 1703 | PERL_UNUSED_ARG(mg); |
7719e241 | 1704 | |
6ff81951 | 1705 | if (hv) { |
497b47a8 | 1706 | (void) hv_iterinit(hv); |
ad64d0ec | 1707 | if (! SvTIED_mg((const SV *)hv, PERL_MAGIC_tied)) |
497b47a8 JH |
1708 | i = HvKEYS(hv); |
1709 | else { | |
1710 | while (hv_iternext(hv)) | |
1711 | i++; | |
1712 | } | |
6ff81951 GS |
1713 | } |
1714 | ||
1715 | sv_setiv(sv, (IV)i); | |
1716 | return 0; | |
1717 | } | |
1718 | ||
1719 | int | |
864dbfa3 | 1720 | Perl_magic_setnkeys(pTHX_ SV *sv, MAGIC *mg) |
946ec16e | 1721 | { |
7918f24d | 1722 | PERL_ARGS_ASSERT_MAGIC_SETNKEYS; |
8772537c | 1723 | PERL_UNUSED_ARG(mg); |
946ec16e | 1724 | if (LvTARG(sv)) { |
85fbaab2 | 1725 | hv_ksplit(MUTABLE_HV(LvTARG(sv)), SvIV(sv)); |
946ec16e | 1726 | } |
1727 | return 0; | |
ac27b0f5 | 1728 | } |
946ec16e | 1729 | |
efaf3674 DM |
1730 | /* |
1731 | =for apidoc magic_methcall | |
1732 | ||
1733 | Invoke a magic method (like FETCH). | |
1734 | ||
1735 | * sv and mg are the tied thinggy and the tie magic; | |
1736 | * meth is the name of the method to call; | |
1a1a5af7 DM |
1737 | * argc is the number of args (in addition to $self) to pass to the method; |
1738 | the args themselves are any values following the argc argument. | |
efaf3674 DM |
1739 | * flags: |
1740 | G_DISCARD: invoke method with G_DISCARD flag and don't return a value | |
1a1a5af7 | 1741 | G_UNDEF_FILL: fill the stack with argc pointers to PL_sv_undef. |
efaf3674 DM |
1742 | |
1743 | Returns the SV (if any) returned by the method, or NULL on failure. | |
1744 | ||
1745 | ||
1746 | =cut | |
1747 | */ | |
1748 | ||
1749 | SV* | |
c7a0c747 | 1750 | Perl_magic_methcall(pTHX_ SV *sv, const MAGIC *mg, const char *meth, U32 flags, |
046b0c7d | 1751 | U32 argc, ...) |
a0d0e21e | 1752 | { |
97aff369 | 1753 | dVAR; |
a0d0e21e | 1754 | dSP; |
efaf3674 | 1755 | SV* ret = NULL; |
463ee0b2 | 1756 | |
7918f24d NC |
1757 | PERL_ARGS_ASSERT_MAGIC_METHCALL; |
1758 | ||
efaf3674 | 1759 | ENTER; |
d1d7a15d NC |
1760 | |
1761 | if (flags & G_WRITING_TO_STDERR) { | |
1762 | SAVETMPS; | |
1763 | ||
1764 | save_re_context(); | |
1765 | SAVESPTR(PL_stderrgv); | |
1766 | PL_stderrgv = NULL; | |
1767 | } | |
1768 | ||
efaf3674 | 1769 | PUSHSTACKi(PERLSI_MAGIC); |
924508f0 | 1770 | PUSHMARK(SP); |
efaf3674 | 1771 | |
67549bd2 NC |
1772 | EXTEND(SP, argc+1); |
1773 | PUSHs(SvTIED_obj(sv, mg)); | |
1774 | if (flags & G_UNDEF_FILL) { | |
1775 | while (argc--) { | |
efaf3674 | 1776 | PUSHs(&PL_sv_undef); |
93965878 | 1777 | } |
67549bd2 | 1778 | } else if (argc > 0) { |
046b0c7d NC |
1779 | va_list args; |
1780 | va_start(args, argc); | |
1781 | ||
1782 | do { | |
1783 | SV *const sv = va_arg(args, SV *); | |
1784 | PUSHs(sv); | |
1785 | } while (--argc); | |
1786 | ||
1787 | va_end(args); | |
88e89b8a | 1788 | } |
463ee0b2 | 1789 | PUTBACK; |
efaf3674 DM |
1790 | if (flags & G_DISCARD) { |
1791 | call_method(meth, G_SCALAR|G_DISCARD); | |
1792 | } | |
1793 | else { | |
1794 | if (call_method(meth, G_SCALAR)) | |
1795 | ret = *PL_stack_sp--; | |
1796 | } | |
1797 | POPSTACK; | |
d1d7a15d NC |
1798 | if (flags & G_WRITING_TO_STDERR) |
1799 | FREETMPS; | |
efaf3674 DM |
1800 | LEAVE; |
1801 | return ret; | |
1802 | } | |
1803 | ||
1804 | ||
1805 | /* wrapper for magic_methcall that creates the first arg */ | |
463ee0b2 | 1806 | |
efaf3674 | 1807 | STATIC SV* |
c7a0c747 | 1808 | S_magic_methcall1(pTHX_ SV *sv, const MAGIC *mg, const char *meth, U32 flags, |
efaf3674 DM |
1809 | int n, SV *val) |
1810 | { | |
1811 | dVAR; | |
1812 | SV* arg1 = NULL; | |
1813 | ||
1814 | PERL_ARGS_ASSERT_MAGIC_METHCALL1; | |
1815 | ||
1816 | if (mg->mg_ptr) { | |
1817 | if (mg->mg_len >= 0) { | |
db4b3a1d | 1818 | arg1 = newSVpvn_flags(mg->mg_ptr, mg->mg_len, SVs_TEMP); |
efaf3674 DM |
1819 | } |
1820 | else if (mg->mg_len == HEf_SVKEY) | |
1821 | arg1 = MUTABLE_SV(mg->mg_ptr); | |
1822 | } | |
1823 | else if (mg->mg_type == PERL_MAGIC_tiedelem) { | |
db4b3a1d | 1824 | arg1 = newSViv((IV)(mg->mg_len)); |
efaf3674 DM |
1825 | sv_2mortal(arg1); |
1826 | } | |
1827 | if (!arg1) { | |
046b0c7d | 1828 | return Perl_magic_methcall(aTHX_ sv, mg, meth, flags, n - 1, val); |
efaf3674 | 1829 | } |
046b0c7d | 1830 | return Perl_magic_methcall(aTHX_ sv, mg, meth, flags, n, arg1, val); |
946ec16e | 1831 | } |
1832 | ||
76e3520e | 1833 | STATIC int |
e1ec3a88 | 1834 | S_magic_methpack(pTHX_ SV *sv, const MAGIC *mg, const char *meth) |
a0d0e21e | 1835 | { |
efaf3674 DM |
1836 | dVAR; |
1837 | SV* ret; | |
463ee0b2 | 1838 | |
7918f24d NC |
1839 | PERL_ARGS_ASSERT_MAGIC_METHPACK; |
1840 | ||
efaf3674 DM |
1841 | ret = magic_methcall1(sv, mg, meth, 0, 1, NULL); |
1842 | if (ret) | |
1843 | sv_setsv(sv, ret); | |
a0d0e21e LW |
1844 | return 0; |
1845 | } | |
463ee0b2 | 1846 | |
a0d0e21e | 1847 | int |
864dbfa3 | 1848 | Perl_magic_getpack(pTHX_ SV *sv, MAGIC *mg) |
a0d0e21e | 1849 | { |
7918f24d NC |
1850 | PERL_ARGS_ASSERT_MAGIC_GETPACK; |
1851 | ||
fd69380d | 1852 | if (mg->mg_type == PERL_MAGIC_tiedelem) |
a0d0e21e | 1853 | mg->mg_flags |= MGf_GSKIP; |
58f82c5c | 1854 | magic_methpack(sv,mg,"FETCH"); |
463ee0b2 LW |
1855 | return 0; |
1856 | } | |
1857 | ||
1858 | int | |
864dbfa3 | 1859 | Perl_magic_setpack(pTHX_ SV *sv, MAGIC *mg) |
e336de0d | 1860 | { |
efaf3674 | 1861 | dVAR; |
b112cff9 DM |
1862 | MAGIC *tmg; |
1863 | SV *val; | |
7918f24d NC |
1864 | |
1865 | PERL_ARGS_ASSERT_MAGIC_SETPACK; | |
1866 | ||
b112cff9 DM |
1867 | /* in the code C<$tied{foo} = $val>, the "thing" that gets passed to |
1868 | * STORE() is not $val, but rather a PVLV (the sv in this call), whose | |
1869 | * public flags indicate its value based on copying from $val. Doing | |
1870 | * mg_set() on the PVLV temporarily does SvMAGICAL_off(), then calls us. | |
1871 | * So STORE()'s $_[2] arg is a temporarily disarmed PVLV. This goes | |
1872 | * wrong if $val happened to be tainted, as sv hasn't got magic | |
1873 | * enabled, even though taint magic is in the chain. In which case, | |
1874 | * fake up a temporary tainted value (this is easier than temporarily | |
1875 | * re-enabling magic on sv). */ | |
1876 | ||
1877 | if (PL_tainting && (tmg = mg_find(sv, PERL_MAGIC_taint)) | |
1878 | && (tmg->mg_len & 1)) | |
1879 | { | |
1880 | val = sv_mortalcopy(sv); | |
1881 | SvTAINTED_on(val); | |
1882 | } | |
1883 | else | |
1884 | val = sv; | |
1885 | ||
efaf3674 | 1886 | magic_methcall1(sv, mg, "STORE", G_DISCARD, 2, val); |
463ee0b2 LW |
1887 | return 0; |
1888 | } | |
1889 | ||
1890 | int | |
864dbfa3 | 1891 | Perl_magic_clearpack(pTHX_ SV *sv, MAGIC *mg) |
463ee0b2 | 1892 | { |
7918f24d NC |
1893 | PERL_ARGS_ASSERT_MAGIC_CLEARPACK; |
1894 | ||
a0d0e21e LW |
1895 | return magic_methpack(sv,mg,"DELETE"); |
1896 | } | |
463ee0b2 | 1897 | |
93965878 NIS |
1898 | |
1899 | U32 | |
864dbfa3 | 1900 | Perl_magic_sizepack(pTHX_ SV *sv, MAGIC *mg) |
ac27b0f5 | 1901 | { |
efaf3674 | 1902 | dVAR; |
22846ab4 | 1903 | I32 retval = 0; |
efaf3674 | 1904 | SV* retsv; |
93965878 | 1905 | |
7918f24d NC |
1906 | PERL_ARGS_ASSERT_MAGIC_SIZEPACK; |
1907 | ||
efaf3674 DM |
1908 | retsv = magic_methcall1(sv, mg, "FETCHSIZE", 0, 1, NULL); |
1909 | if (retsv) { | |
1910 | retval = SvIV(retsv)-1; | |
22846ab4 AB |
1911 | if (retval < -1) |
1912 | Perl_croak(aTHX_ "FETCHSIZE returned a negative value"); | |
93965878 | 1913 | } |
22846ab4 | 1914 | return (U32) retval; |
93965878 NIS |
1915 | } |
1916 | ||
cea2e8a9 GS |
1917 | int |
1918 | Perl_magic_wipepack(pTHX_ SV *sv, MAGIC *mg) | |
a0d0e21e | 1919 | { |
efaf3674 | 1920 | dVAR; |
463ee0b2 | 1921 | |
7918f24d NC |
1922 | PERL_ARGS_ASSERT_MAGIC_WIPEPACK; |
1923 | ||
046b0c7d | 1924 | Perl_magic_methcall(aTHX_ sv, mg, "CLEAR", G_DISCARD, 0); |
463ee0b2 LW |
1925 | return 0; |
1926 | } | |
1927 | ||
1928 | int | |
864dbfa3 | 1929 | Perl_magic_nextpack(pTHX_ SV *sv, MAGIC *mg, SV *key) |
463ee0b2 | 1930 | { |
efaf3674 DM |
1931 | dVAR; |
1932 | SV* ret; | |
463ee0b2 | 1933 | |
7918f24d NC |
1934 | PERL_ARGS_ASSERT_MAGIC_NEXTPACK; |
1935 | ||
046b0c7d NC |
1936 | ret = SvOK(key) ? Perl_magic_methcall(aTHX_ sv, mg, "NEXTKEY", 0, 1, key) |
1937 | : Perl_magic_methcall(aTHX_ sv, mg, "FIRSTKEY", 0, 0); | |
efaf3674 DM |
1938 | if (ret) |
1939 | sv_setsv(key,ret); | |
79072805 LW |
1940 | return 0; |
1941 | } | |
1942 | ||
1943 | int | |
1146e912 | 1944 | Perl_magic_existspack(pTHX_ SV *sv, const MAGIC *mg) |
a0d0e21e | 1945 | { |
7918f24d NC |
1946 | PERL_ARGS_ASSERT_MAGIC_EXISTSPACK; |
1947 | ||
a0d0e21e | 1948 | return magic_methpack(sv,mg,"EXISTS"); |
ac27b0f5 | 1949 | } |
a0d0e21e | 1950 | |
a3bcc51e TP |
1951 | SV * |
1952 | Perl_magic_scalarpack(pTHX_ HV *hv, MAGIC *mg) | |
1953 | { | |
efaf3674 | 1954 | dVAR; |
5fcbf73d | 1955 | SV *retval; |
ad64d0ec NC |
1956 | SV * const tied = SvTIED_obj(MUTABLE_SV(hv), mg); |
1957 | HV * const pkg = SvSTASH((const SV *)SvRV(tied)); | |
a3bcc51e | 1958 | |
7918f24d NC |
1959 | PERL_ARGS_ASSERT_MAGIC_SCALARPACK; |
1960 | ||
a3bcc51e TP |
1961 | if (!gv_fetchmethod_autoload(pkg, "SCALAR", FALSE)) { |
1962 | SV *key; | |
bfcb3514 | 1963 | if (HvEITER_get(hv)) |
a3bcc51e TP |
1964 | /* we are in an iteration so the hash cannot be empty */ |
1965 | return &PL_sv_yes; | |
1966 | /* no xhv_eiter so now use FIRSTKEY */ | |
1967 | key = sv_newmortal(); | |
ad64d0ec | 1968 | magic_nextpack(MUTABLE_SV(hv), mg, key); |
bfcb3514 | 1969 | HvEITER_set(hv, NULL); /* need to reset iterator */ |
a3bcc51e TP |
1970 | return SvOK(key) ? &PL_sv_yes : &PL_sv_no; |
1971 | } | |
1972 | ||
1973 | /* there is a SCALAR method that we can call */ | |
046b0c7d | 1974 | retval = Perl_magic_methcall(aTHX_ MUTABLE_SV(hv), mg, "SCALAR", 0, 0); |
efaf3674 | 1975 | if (!retval) |
5fcbf73d | 1976 | retval = &PL_sv_undef; |
a3bcc51e TP |
1977 | return retval; |
1978 | } | |
1979 | ||
a0d0e21e | 1980 | int |
864dbfa3 | 1981 | Perl_magic_setdbline(pTHX_ SV *sv, MAGIC *mg) |
79072805 | 1982 | { |
97aff369 | 1983 | dVAR; |
8772537c AL |
1984 | GV * const gv = PL_DBline; |
1985 | const I32 i = SvTRUE(sv); | |
1986 | SV ** const svp = av_fetch(GvAV(gv), | |
01b8bcb7 | 1987 | atoi(MgPV_nolen_const(mg)), FALSE); |
7918f24d NC |
1988 | |
1989 | PERL_ARGS_ASSERT_MAGIC_SETDBLINE; | |
1990 | ||
8772537c AL |
1991 | if (svp && SvIOKp(*svp)) { |
1992 | OP * const o = INT2PTR(OP*,SvIVX(*svp)); | |
1993 | if (o) { | |
1994 | /* set or clear breakpoint in the relevant control op */ | |
1995 | if (i) | |
1996 | o->op_flags |= OPf_SPECIAL; | |
1997 | else | |
1998 | o->op_flags &= ~OPf_SPECIAL; | |
1999 | } | |
5df8de69 | 2000 | } |
79072805 LW |
2001 | return 0; |
2002 | } | |
2003 | ||
2004 | int | |
8772537c | 2005 | Perl_magic_getarylen(pTHX_ SV *sv, const MAGIC *mg) |
79072805 | 2006 | { |
97aff369 | 2007 | dVAR; |
502c6561 | 2008 | AV * const obj = MUTABLE_AV(mg->mg_obj); |
7918f24d NC |
2009 | |
2010 | PERL_ARGS_ASSERT_MAGIC_GETARYLEN; | |
2011 | ||
83bf042f | 2012 | if (obj) { |
fc15ae8f | 2013 | sv_setiv(sv, AvFILL(obj) + CopARYBASE_get(PL_curcop)); |
83bf042f NC |
2014 | } else { |
2015 | SvOK_off(sv); | |
2016 | } | |
79072805 LW |
2017 | return 0; |
2018 | } | |
2019 | ||
2020 | int | |
864dbfa3 | 2021 | Perl_magic_setarylen(pTHX_ SV *sv, MAGIC *mg) |
79072805 | 2022 | { |
97aff369 | 2023 | dVAR; |
502c6561 | 2024 | AV * const obj = MUTABLE_AV(mg->mg_obj); |
7918f24d NC |
2025 | |
2026 | PERL_ARGS_ASSERT_MAGIC_SETARYLEN; | |
2027 | ||
83bf042f | 2028 | if (obj) { |
fc15ae8f | 2029 | av_fill(obj, SvIV(sv) - CopARYBASE_get(PL_curcop)); |
83bf042f | 2030 | } else { |
a2a5de95 NC |
2031 | Perl_ck_warner(aTHX_ packWARN(WARN_MISC), |
2032 | "Attempt to set length of freed array"); | |
83bf042f NC |
2033 | } |
2034 | return 0; | |
2035 | } | |
2036 | ||
2037 | int | |
2038 | Perl_magic_freearylen_p(pTHX_ SV *sv, MAGIC *mg) | |
2039 | { | |
97aff369 | 2040 | dVAR; |
7918f24d NC |
2041 | |
2042 | PERL_ARGS_ASSERT_MAGIC_FREEARYLEN_P; | |
53c1dcc0 | 2043 | PERL_UNUSED_ARG(sv); |
7918f24d | 2044 | |
94f3782b DM |
2045 | /* during global destruction, mg_obj may already have been freed */ |
2046 | if (PL_in_clean_all) | |
1ea47f64 | 2047 | return 0; |
94f3782b | 2048 | |
83bf042f NC |
2049 | mg = mg_find (mg->mg_obj, PERL_MAGIC_arylen); |
2050 | ||
2051 | if (mg) { | |
2052 | /* arylen scalar holds a pointer back to the array, but doesn't own a | |
2053 | reference. Hence the we (the array) are about to go away with it | |
2054 | still pointing at us. Clear its pointer, else it would be pointing | |
2055 | at free memory. See the comment in sv_magic about reference loops, | |
2056 | and why it can't own a reference to us. */ | |
2057 | mg->mg_obj = 0; | |
2058 | } | |
a0d0e21e LW |
2059 | return 0; |
2060 | } | |
2061 | ||
2062 | int | |
864dbfa3 | 2063 | Perl_magic_getpos(pTHX_ SV *sv, MAGIC *mg) |
a0d0e21e | 2064 | { |
97aff369 | 2065 | dVAR; |
8772537c | 2066 | SV* const lsv = LvTARG(sv); |
7918f24d NC |
2067 | |
2068 | PERL_ARGS_ASSERT_MAGIC_GETPOS; | |
3881461a | 2069 | PERL_UNUSED_ARG(mg); |
ac27b0f5 | 2070 | |
a0d0e21e | 2071 | if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv)) { |
3881461a AL |
2072 | MAGIC * const found = mg_find(lsv, PERL_MAGIC_regex_global); |
2073 | if (found && found->mg_len >= 0) { | |
2074 | I32 i = found->mg_len; | |
7e2040f0 | 2075 | if (DO_UTF8(lsv)) |
a0ed51b3 | 2076 | sv_pos_b2u(lsv, &i); |
fc15ae8f | 2077 | sv_setiv(sv, i + CopARYBASE_get(PL_curcop)); |
a0d0e21e LW |
2078 | return 0; |
2079 | } | |
2080 | } | |
0c34ef67 | 2081 | SvOK_off(sv); |
a0d0e21e LW |
2082 | return 0; |
2083 | } | |
2084 | ||
2085 | int | |
864dbfa3 | 2086 | Perl_magic_setpos(pTHX_ SV *sv, MAGIC *mg) |
a0d0e21e | 2087 | { |
97aff369 | 2088 | dVAR; |
8772537c | 2089 | SV* const lsv = LvTARG(sv); |
a0d0e21e LW |
2090 | SSize_t pos; |
2091 | STRLEN len; | |
c00206c8 | 2092 | STRLEN ulen = 0; |
53d44271 | 2093 | MAGIC* found; |
a0d0e21e | 2094 | |
7918f24d | 2095 | PERL_ARGS_ASSERT_MAGIC_SETPOS; |
3881461a | 2096 | PERL_UNUSED_ARG(mg); |
ac27b0f5 | 2097 | |
a0d0e21e | 2098 | if (SvTYPE(lsv) >= SVt_PVMG && SvMAGIC(lsv)) |
3881461a AL |
2099 | found = mg_find(lsv, PERL_MAGIC_regex_global); |
2100 | else | |
2101 | found = NULL; | |
2102 | if (!found) { | |
a0d0e21e LW |
2103 | if (!SvOK(sv)) |
2104 | return 0; | |
d83f0a82 NC |
2105 | #ifdef PERL_OLD_COPY_ON_WRITE |
2106 | if (SvIsCOW(lsv)) | |
2107 | sv_force_normal_flags(lsv, 0); | |
2108 | #endif | |
3881461a | 2109 | found = sv_magicext(lsv, NULL, PERL_MAGIC_regex_global, &PL_vtbl_mglob, |
53d44271 | 2110 | NULL, 0); |
a0d0e21e LW |
2111 | } |
2112 | else if (!SvOK(sv)) { | |
3881461a | 2113 | found->mg_len = -1; |
a0d0e21e LW |
2114 | return 0; |
2115 | } | |
2116 | len = SvPOK(lsv) ? SvCUR(lsv) : sv_len(lsv); | |
2117 | ||
fc15ae8f | 2118 | pos = SvIV(sv) - CopARYBASE_get(PL_curcop); |
a0ed51b3 | 2119 | |
7e2040f0 | 2120 | if (DO_UTF8(lsv)) { |
a0ed51b3 LW |
2121 | ulen = sv_len_utf8(lsv); |
2122 | if (ulen) | |
2123 | len = ulen; | |
a0ed51b3 LW |
2124 | } |
2125 | ||
a0d0e21e LW |
2126 | if (pos < 0) { |
2127 | pos += len; | |
2128 | if (pos < 0) | |
2129 | pos = 0; | |
2130 | } | |
eb160463 | 2131 | else if (pos > (SSize_t)len) |
a0d0e21e | 2132 | pos = len; |
a0ed51b3 LW |
2133 | |
2134 | if (ulen) { | |
2135 | I32 p = pos; | |
2136 | sv_pos_u2b(lsv, &p, 0); | |
2137 | pos = p; | |
2138 | } | |
727405f8 | 2139 | |
3881461a AL |
2140 | found->mg_len = pos; |
2141 | found->mg_flags &= ~MGf_MINMATCH; | |
a0d0e21e | 2142 | |
79072805 LW |
2143 | return 0; |
2144 | } | |
2145 | ||
2146 | int | |
864dbfa3 | 2147 | Perl_magic_getsubstr(pTHX_ SV *sv, MAGIC *mg) |
6ff81951 GS |
2148 | { |
2149 | STRLEN len; | |
35a4481c | 2150 | SV * const lsv = LvTARG(sv); |
b83604b4 | 2151 | const char * const tmps = SvPV_const(lsv,len); |
777f7c56 EB |
2152 | STRLEN offs = LvTARGOFF(sv); |
2153 | STRLEN rem = LvTARGLEN(sv); | |
7918f24d NC |
2154 | |
2155 | PERL_ARGS_ASSERT_MAGIC_GETSUBSTR; | |
8772537c | 2156 | PERL_UNUSED_ARG(mg); |
6ff81951 | 2157 | |
9aa983d2 | 2158 | if (SvUTF8(lsv)) |
d931b1be | 2159 | offs = sv_pos_u2b_flags(lsv, offs, &rem, SV_CONST_RETURN); |
777f7c56 | 2160 | if (offs > len) |
6ff81951 | 2161 | offs = len; |
777f7c56 | 2162 | if (rem > len - offs) |
6ff81951 | 2163 | rem = len - offs; |
1c900557 | 2164 | sv_setpvn(sv, tmps + offs, rem); |
9aa983d2 | 2165 | if (SvUTF8(lsv)) |
2ef4b674 | 2166 | SvUTF8_on(sv); |
6ff81951 GS |
2167 | return 0; |
2168 | } | |
2169 | ||
2170 | int | |
864dbfa3 | 2171 | Perl_magic_setsubstr(pTHX_ SV *sv, MAGIC *mg) |
79072805 | 2172 | { |
97aff369 | 2173 | dVAR; |
9aa983d2 | 2174 | STRLEN len; |
5fcbf73d | 2175 | const char * const tmps = SvPV_const(sv, len); |
dd374669 | 2176 | SV * const lsv = LvTARG(sv); |
777f7c56 EB |
2177 | STRLEN lvoff = LvTARGOFF(sv); |
2178 | STRLEN lvlen = LvTARGLEN(sv); | |
7918f24d NC |
2179 | |
2180 | PERL_ARGS_ASSERT_MAGIC_SETSUBSTR; | |
8772537c | 2181 | PERL_UNUSED_ARG(mg); |
075a4a2b | 2182 | |
1aa99e6b | 2183 | if (DO_UTF8(sv)) { |
9aa983d2 | 2184 | sv_utf8_upgrade(lsv); |
d931b1be | 2185 | lvoff = sv_pos_u2b_flags(lsv, lvoff, &lvlen, SV_CONST_RETURN); |
9aa983d2 | 2186 | sv_insert(lsv, lvoff, lvlen, tmps, len); |
b76f3ce2 | 2187 | LvTARGLEN(sv) = sv_len_utf8(sv); |
9aa983d2 JH |
2188 | SvUTF8_on(lsv); |
2189 | } | |
9bf12eaf | 2190 | else if (lsv && SvUTF8(lsv)) { |
5fcbf73d | 2191 | const char *utf8; |
d931b1be | 2192 | lvoff = sv_pos_u2b_flags(lsv, lvoff, &lvlen, SV_CONST_RETURN); |
b76f3ce2 | 2193 | LvTARGLEN(sv) = len; |
5fcbf73d AL |
2194 | utf8 = (char*)bytes_to_utf8((U8*)tmps, &len); |
2195 | sv_insert(lsv, lvoff, lvlen, utf8, len); | |
2196 | Safefree(utf8); | |
1aa99e6b | 2197 | } |
b76f3ce2 GB |
2198 | else { |
2199 | sv_insert(lsv, lvoff, lvlen, tmps, len); | |
2200 | LvTARGLEN(sv) = len; | |
2201 | } | |
2202 | ||
79072805 LW |
2203 | return 0; |
2204 | } | |
2205 | ||
2206 | int | |
864dbfa3 | 2207 | Perl_magic_gettaint(pTHX_ SV *sv, MAGIC *mg) |
463ee0b2 | 2208 | { |
97aff369 | 2209 | dVAR; |
7918f24d NC |
2210 | |
2211 | PERL_ARGS_ASSERT_MAGIC_GETTAINT; | |
8772537c | 2212 | PERL_UNUSED_ARG(sv); |
7918f24d | 2213 | |
27cc343c | 2214 | TAINT_IF((PL_localizing != 1) && (mg->mg_len & 1)); |
463ee0b2 LW |
2215 | return 0; |
2216 | } | |
2217 | ||
2218 | int | |
864dbfa3 | 2219 | Perl_magic_settaint(pTHX_ SV *sv, MAGIC *mg) |
463ee0b2 | 2220 | { |
97aff369 | 2221 | dVAR; |
7918f24d NC |
2222 | |
2223 | PERL_ARGS_ASSERT_MAGIC_SETTAINT; | |
8772537c | 2224 | PERL_UNUSED_ARG(sv); |
7918f24d | 2225 | |
b01e650a DM |
2226 | /* update taint status */ |
2227 | if (PL_tainted) | |
2228 | mg->mg_len |= 1; | |
2229 | else | |
2230 | mg->mg_len &= ~1; | |
463ee0b2 LW |
2231 | return 0; |
2232 | } | |
2233 | ||
2234 | int | |
864dbfa3 | 2235 | Perl_magic_getvec(pTHX_ SV *sv, MAGIC *mg) |
6ff81951 | 2236 | { |
35a4481c | 2237 | SV * const lsv = LvTARG(sv); |
7918f24d NC |
2238 | |
2239 | PERL_ARGS_ASSERT_MAGIC_GETVEC; | |
8772537c | 2240 | PERL_UNUSED_ARG(mg); |
6ff81951 | 2241 | |
6136c704 AL |
2242 | if (lsv) |
2243 | sv_setuv(sv, do_vecget(lsv, LvTARGOFF(sv), LvTARGLEN(sv))); | |
2244 | else | |
0c34ef67 | 2245 | SvOK_off(sv); |
6ff81951 | 2246 | |
6ff81951 GS |
2247 | return 0; |
2248 | } | |
2249 | ||
2250 | int | |
864dbfa3 | 2251 | Perl_magic_setvec(pTHX_ SV *sv, MAGIC *mg) |
79072805 | 2252 | { |
7918f24d | 2253 | PERL_ARGS_ASSERT_MAGIC_SETVEC; |
8772537c | 2254 | PERL_UNUSED_ARG(mg); |
79072805 LW |
2255 | do_vecset(sv); /* XXX slurp this routine */ |
2256 | return 0; | |
2257 | } | |
2258 | ||
2259 | int | |
864dbfa3 | 2260 | Perl_magic_getdefelem(pTHX_ SV *sv, MAGIC *mg) |
5f05dabc | 2261 | { |
97aff369 | 2262 | dVAR; |
a0714e2c | 2263 | SV *targ = NULL; |
7918f24d NC |
2264 | |
2265 | PERL_ARGS_ASSERT_MAGIC_GETDEFELEM; | |
2266 | ||
5f05dabc | 2267 | if (LvTARGLEN(sv)) { |
68dc0745 | 2268 | if (mg->mg_obj) { |
8772537c | 2269 | SV * const ahv = LvTARG(sv); |
85fbaab2 | 2270 | HE * const he = hv_fetch_ent(MUTABLE_HV(ahv), mg->mg_obj, FALSE, 0); |
6d822dc4 MS |
2271 | if (he) |
2272 | targ = HeVAL(he); | |
68dc0745 | 2273 | } |
2274 | else { | |
502c6561 | 2275 | AV *const av = MUTABLE_AV(LvTARG(sv)); |
68dc0745 | 2276 | if ((I32)LvTARGOFF(sv) <= AvFILL(av)) |
2277 | targ = AvARRAY(av)[LvTARGOFF(sv)]; | |
2278 | } | |
46da273f | 2279 | if (targ && (targ != &PL_sv_undef)) { |
68dc0745 | 2280 | /* somebody else defined it for us */ |
2281 | SvREFCNT_dec(LvTARG(sv)); | |
b37c2d43 | 2282 | LvTARG(sv) = SvREFCNT_inc_simple_NN(targ); |
68dc0745 | 2283 | LvTARGLEN(sv) = 0; |
2284 | SvREFCNT_dec(mg->mg_obj); | |
a0714e2c | 2285 | mg->mg_obj = NULL; |
68dc0745 | 2286 | mg->mg_flags &= ~MGf_REFCOUNTED; |
2287 | } | |
5f05dabc | 2288 | } |
71be2cbc | 2289 | else |
2290 | targ = LvTARG(sv); | |
3280af22 | 2291 | sv_setsv(sv, targ ? targ : &PL_sv_undef); |
71be2cbc | 2292 | return 0; |
2293 | } | |
2294 | ||
2295 | int | |
864dbfa3 | 2296 | Perl_magic_setdefelem(pTHX_ SV *sv, MAGIC *mg) |
71be2cbc | 2297 | { |
7918f24d | 2298 | PERL_ARGS_ASSERT_MAGIC_SETDEFELEM; |
8772537c | 2299 | PERL_UNUSED_ARG(mg); |
71be2cbc | 2300 | if (LvTARGLEN(sv)) |
68dc0745 | 2301 | vivify_defelem(sv); |
2302 | if (LvTARG(sv)) { | |
5f05dabc | 2303 | sv_setsv(LvTARG(sv), sv); |
68dc0745 | 2304 | SvSETMAGIC(LvTARG(sv)); |
2305 | } | |
5f05dabc | 2306 | return 0; |
2307 | } | |
2308 | ||
71be2cbc | 2309 | void |
864dbfa3 | 2310 | Perl_vivify_defelem(pTHX_ SV *sv) |
71be2cbc | 2311 | { |
97aff369 | 2312 | dVAR; |
74e13ce4 | 2313 | MAGIC *mg; |
a0714e2c | 2314 | SV *value = NULL; |
71be2cbc | 2315 | |
7918f24d NC |
2316 | PERL_ARGS_ASSERT_VIVIFY_DEFELEM; |
2317 | ||
14befaf4 | 2318 | if (!LvTARGLEN(sv) || !(mg = mg_find(sv, PERL_MAGIC_defelem))) |
71be2cbc | 2319 | return; |
68dc0745 | 2320 | if (mg->mg_obj) { |
8772537c | 2321 | SV * const ahv = LvTARG(sv); |
85fbaab2 | 2322 | HE * const he = hv_fetch_ent(MUTABLE_HV(ahv), mg->mg_obj, TRUE, 0); |
6d822dc4 MS |
2323 | if (he) |
2324 | value = HeVAL(he); | |
3280af22 | 2325 | if (!value || value == &PL_sv_undef) |
be2597df | 2326 | Perl_croak(aTHX_ PL_no_helem_sv, SVfARG(mg->mg_obj)); |
71be2cbc | 2327 | } |
68dc0745 | 2328 | else { |
502c6561 | 2329 | AV *const av = MUTABLE_AV(LvTARG(sv)); |
5aabfad6 | 2330 | if ((I32)LvTARGLEN(sv) < 0 && (I32)LvTARGOFF(sv) > AvFILL(av)) |
a0714e2c | 2331 | LvTARG(sv) = NULL; /* array can't be extended */ |
68dc0745 | 2332 | else { |
d4c19fe8 | 2333 | SV* const * const svp = av_fetch(av, LvTARGOFF(sv), TRUE); |
3280af22 | 2334 | if (!svp || (value = *svp) == &PL_sv_undef) |
cea2e8a9 | 2335 | Perl_croak(aTHX_ PL_no_aelem, (I32)LvTARGOFF(sv)); |
68dc0745 | 2336 | } |
2337 | } | |
b37c2d43 | 2338 | SvREFCNT_inc_simple_void(value); |
68dc0745 | 2339 | SvREFCNT_dec(LvTARG(sv)); |
2340 | LvTARG(sv) = value; | |
71be2cbc | 2341 | LvTARGLEN(sv) = 0; |
68dc0745 | 2342 | SvREFCNT_dec(mg->mg_obj); |
a0714e2c | 2343 | mg->mg_obj = NULL; |
68dc0745 | 2344 | mg->mg_flags &= ~MGf_REFCOUNTED; |
5f05dabc | 2345 | } |
2346 | ||
2347 | int | |
864dbfa3 | 2348 | Perl_magic_killbackrefs(pTHX_ SV *sv, MAGIC *mg) |
810b8aa5 | 2349 | { |
7918f24d | 2350 | PERL_ARGS_ASSERT_MAGIC_KILLBACKREFS; |
5648c0ae DM |
2351 | Perl_sv_kill_backrefs(aTHX_ sv, MUTABLE_AV(mg->mg_obj)); |
2352 | return 0; | |
810b8aa5 GS |
2353 | } |
2354 | ||
2355 | int | |
864dbfa3 | 2356 | Perl_magic_setmglob(pTHX_ SV *sv, MAGIC *mg) |
93a17b20 | 2357 | { |
7918f24d | 2358 | PERL_ARGS_ASSERT_MAGIC_SETMGLOB; |
96a5add6 | 2359 | PERL_UNUSED_CONTEXT; |
565764a8 | 2360 | mg->mg_len = -1; |
1f730e6c FC |
2361 | if (!isGV_with_GP(sv)) |
2362 | SvSCREAM_off(sv); | |
93a17b20 LW |
2363 | return 0; |
2364 | } | |
2365 | ||
2366 | int | |
864dbfa3 | 2367 | Perl_magic_setuvar(pTHX_ SV *sv, MAGIC *mg) |
79072805 | 2368 | { |
35a4481c | 2369 | const struct ufuncs * const uf = (struct ufuncs *)mg->mg_ptr; |
79072805 | 2370 | |
7918f24d NC |
2371 | PERL_ARGS_ASSERT_MAGIC_SETUVAR; |
2372 | ||
79072805 | 2373 | if (uf && uf->uf_set) |
24f81a43 | 2374 | (*uf->uf_set)(aTHX_ uf->uf_index, sv); |
79072805 LW |
2375 | return 0; |
2376 | } | |
2377 | ||
c277df42 | 2378 | int |
faf82a0b AE |
2379 | Perl_magic_setregexp(pTHX_ SV *sv, MAGIC *mg) |
2380 | { | |
488344d2 | 2381 | const char type = mg->mg_type; |
7918f24d NC |
2382 | |
2383 | PERL_ARGS_ASSERT_MAGIC_SETREGEXP; | |
2384 | ||
488344d2 NC |
2385 | if (type == PERL_MAGIC_qr) { |
2386 | } else if (type == PERL_MAGIC_bm) { | |
2387 | SvTAIL_off(sv); | |
2388 | SvVALID_off(sv); | |
2389 | } else { | |
2390 | assert(type == PERL_MAGIC_fm); | |
2391 | SvCOMPILED_off(sv); | |
2392 | } | |
2393 | return sv_unmagic(sv, type); | |
faf82a0b AE |
2394 | } |
2395 | ||
7a4c00b4 | 2396 | #ifdef USE_LOCALE_COLLATE |
79072805 | 2397 | int |
864dbfa3 | 2398 | Perl_magic_setcollxfrm(pTHX_ SV *sv, MAGIC *mg) |
bbce6d69 | 2399 | { |
7918f24d NC |
2400 | PERL_ARGS_ASSERT_MAGIC_SETCOLLXFRM; |
2401 | ||
bbce6d69 | 2402 | /* |
838b5b74 | 2403 | * RenE<eacute> Descartes said "I think not." |
bbce6d69 | 2404 | * and vanished with a faint plop. |
2405 | */ | |
96a5add6 | 2406 | PERL_UNUSED_CONTEXT; |
8772537c | 2407 | PERL_UNUSED_ARG(sv); |
7a4c00b4 | 2408 | if (mg->mg_ptr) { |
2409 | Safefree(mg->mg_ptr); | |
2410 | mg->mg_ptr = NULL; | |
565764a8 | 2411 | mg->mg_len = -1; |
7a4c00b4 | 2412 | } |
bbce6d69 | 2413 | return 0; |
2414 | } | |
7a4c00b4 | 2415 | #endif /* USE_LOCALE_COLLATE */ |
bbce6d69 | 2416 | |
7e8c5dac HS |
2417 | /* Just clear the UTF-8 cache data. */ |
2418 | int | |
2419 | Perl_magic_setutf8(pTHX_ SV *sv, MAGIC *mg) | |
2420 | { | |
7918f24d | 2421 | PERL_ARGS_ASSERT_MAGIC_SETUTF8; |
96a5add6 | 2422 | PERL_UNUSED_CONTEXT; |
8772537c | 2423 | PERL_UNUSED_ARG(sv); |
7e8c5dac | 2424 | Safefree(mg->mg_ptr); /* The mg_ptr holds the pos cache. */ |
3881461a | 2425 | mg->mg_ptr = NULL; |
7e8c5dac HS |
2426 | mg->mg_len = -1; /* The mg_len holds the len cache. */ |
2427 | return 0; | |
2428 | } | |
2429 | ||
bbce6d69 | 2430 | int |
864dbfa3 | 2431 | Perl_magic_set(pTHX_ SV *sv, MAGIC *mg) |
79072805 | 2432 | { |
97aff369 | 2433 | dVAR; |
e1ec3a88 | 2434 | register const char *s; |
2fdbfb4d AB |
2435 | register I32 paren; |
2436 | register const REGEXP * rx; | |
2437 | const char * const remaining = mg->mg_ptr + 1; | |
79072805 | 2438 | I32 i; |
8990e307 | 2439 | STRLEN len; |
125b9982 | 2440 | MAGIC *tmg; |
2fdbfb4d | 2441 | |
7918f24d NC |
2442 | PERL_ARGS_ASSERT_MAGIC_SET; |
2443 | ||
79072805 | 2444 | switch (*mg->mg_ptr) { |
2fdbfb4d AB |
2445 | case '\015': /* $^MATCH */ |
2446 | if (strEQ(remaining, "ATCH")) | |
2447 | goto do_match; | |
2448 | case '`': /* ${^PREMATCH} caught below */ | |
2449 | do_prematch: | |
f1b875a0 | 2450 | paren = RX_BUFF_IDX_PREMATCH; |
2fdbfb4d AB |
2451 | goto setparen; |
2452 | case '\'': /* ${^POSTMATCH} caught below */ | |
2453 | do_postmatch: | |
f1b875a0 | 2454 | paren = RX_BUFF_IDX_POSTMATCH; |
2fdbfb4d AB |
2455 | goto setparen; |
2456 | case '&': | |
2457 | do_match: | |
f1b875a0 | 2458 | paren = RX_BUFF_IDX_FULLMATCH; |
2fdbfb4d AB |
2459 | goto setparen; |
2460 | case '1': case '2': case '3': case '4': | |
2461 | case '5': case '6': case '7': case '8': case '9': | |
104a8018 | 2462 | paren = atoi(mg->mg_ptr); |
2fdbfb4d | 2463 | setparen: |
1e05feb3 | 2464 | if (PL_curpm && (rx = PM_GETRE(PL_curpm))) { |
2fdbfb4d | 2465 | CALLREG_NUMBUF_STORE((REGEXP * const)rx,paren,sv); |
1e05feb3 | 2466 | } else { |
2fdbfb4d AB |
2467 | /* Croak with a READONLY error when a numbered match var is |
2468 | * set without a previous pattern match. Unless it's C<local $1> | |
2469 | */ | |
2470 | if (!PL_localizing) { | |
6ad8f254 | 2471 | Perl_croak_no_modify(aTHX); |
2fdbfb4d AB |
2472 | } |
2473 | } | |
9b9e0be4 | 2474 | break; |
748a9306 | 2475 | case '\001': /* ^A */ |
3280af22 | 2476 | sv_setsv(PL_bodytarget, sv); |
125b9982 NT |
2477 | /* mg_set() has temporarily made sv non-magical */ |
2478 | if (PL_tainting) { | |
2479 | if ((tmg = mg_find(sv,PERL_MAGIC_taint)) && tmg->mg_len & 1) | |
2480 | SvTAINTED_on(PL_bodytarget); | |
2481 | else | |
2482 | SvTAINTED_off(PL_bodytarget); | |
2483 | } | |
748a9306 | 2484 | break; |
49460fe6 | 2485 | case '\003': /* ^C */ |
f2338a2e | 2486 | PL_minus_c = cBOOL(SvIV(sv)); |
49460fe6 NIS |
2487 | break; |
2488 | ||
79072805 | 2489 | case '\004': /* ^D */ |
b4ab917c | 2490 | #ifdef DEBUGGING |
b83604b4 | 2491 | s = SvPV_nolen_const(sv); |
ddcf8bc1 | 2492 | PL_debug = get_debug_opts(&s, 0) | DEBUG_TOP_FLAG; |
a58fb6f9 CS |
2493 | if (DEBUG_x_TEST || DEBUG_B_TEST) |
2494 | dump_all_perl(!DEBUG_B_TEST); | |
b4ab917c | 2495 | #else |
38ab35f8 | 2496 | PL_debug = (SvIV(sv)) | DEBUG_TOP_FLAG; |
b4ab917c | 2497 | #endif |
79072805 | 2498 | break; |
28f23441 | 2499 | case '\005': /* ^E */ |
d0063567 | 2500 | if (*(mg->mg_ptr+1) == '\0') { |
e37778c2 | 2501 | #ifdef VMS |
38ab35f8 | 2502 | set_vaxc_errno(SvIV(sv)); |
e37778c2 NC |
2503 | #else |
2504 | # ifdef WIN32 | |
d0063567 | 2505 | SetLastError( SvIV(sv) ); |
e37778c2 NC |
2506 | # else |
2507 | # ifdef OS2 | |
38ab35f8 | 2508 | os2_setsyserrno(SvIV(sv)); |
e37778c2 | 2509 | # else |
d0063567 | 2510 | /* will anyone ever use this? */ |
38ab35f8 | 2511 | SETERRNO(SvIV(sv), 4); |
048c1ddf IZ |
2512 | # endif |
2513 | # endif | |
22fae026 | 2514 | #endif |
d0063567 DK |
2515 | } |
2516 | else if (strEQ(mg->mg_ptr+1, "NCODING")) { | |
ef8d46e8 | 2517 | SvREFCNT_dec(PL_encoding); |
d0063567 DK |
2518 | if (SvOK(sv) || SvGMAGICAL(sv)) { |
2519 | PL_encoding = newSVsv(sv); | |
2520 | } | |
2521 | else { | |
a0714e2c | 2522 | PL_encoding = NULL; |
d0063567 DK |
2523 | } |
2524 | } | |
2525 | break; | |
79072805 | 2526 | case '\006': /* ^F */ |
38ab35f8 | 2527 | PL_maxsysfd = SvIV(sv); |
79072805 | 2528 | break; |
a0d0e21e | 2529 | case '\010': /* ^H */ |
38ab35f8 | 2530 | PL_hints = SvIV(sv); |
a0d0e21e | 2531 | break; |
9d116dd7 | 2532 | case '\011': /* ^I */ /* NOT \t in EBCDIC */ |
43c5f42d | 2533 | Safefree(PL_inplace); |
bd61b366 | 2534 | PL_inplace = SvOK(sv) ? savesvpv(sv) : NULL; |
da78da6e | 2535 | break; |
28f23441 | 2536 | case '\017': /* ^O */ |
ac27b0f5 | 2537 | if (*(mg->mg_ptr+1) == '\0') { |
43c5f42d | 2538 | Safefree(PL_osname); |
bd61b366 | 2539 | PL_osname = NULL; |
3511154c DM |
2540 | if (SvOK(sv)) { |
2541 | TAINT_PROPER("assigning to $^O"); | |
2e0de35c | 2542 | PL_osname = savesvpv(sv); |
3511154c | 2543 | } |
ac27b0f5 NIS |
2544 | } |
2545 | else if (strEQ(mg->mg_ptr, "\017PEN")) { | |
8b850bd5 NC |
2546 | STRLEN len; |
2547 | const char *const start = SvPV(sv, len); | |
b54fc2b6 | 2548 | const char *out = (const char*)memchr(start, '\0', len); |
8b850bd5 | 2549 | SV *tmp; |
8b850bd5 NC |
2550 | |
2551 | ||
2552 | PL_compiling.cop_hints |= HINT_LEXICAL_IO_IN | HINT_LEXICAL_IO_OUT; | |
f747ebd6 | 2553 | PL_hints |= HINT_LEXICAL_IO_IN | HINT_LEXICAL_IO_OUT; |
8b850bd5 NC |
2554 | |
2555 | /* Opening for input is more common than opening for output, so | |
2556 | ensure that hints for input are sooner on linked list. */ | |
59cd0e26 | 2557 | tmp = out ? newSVpvn_flags(out + 1, start + len - out - 1, |
f747ebd6 Z |
2558 | SvUTF8(sv)) |
2559 | : newSVpvs_flags("", SvUTF8(sv)); | |
2560 | (void)hv_stores(GvHV(PL_hintgv), "open>", tmp); | |
2561 | mg_set(tmp); | |
8b850bd5 | 2562 | |
f747ebd6 Z |
2563 | tmp = newSVpvn_flags(start, out ? (STRLEN)(out - start) : len, |
2564 | SvUTF8(sv)); | |
2565 | (void)hv_stores(GvHV(PL_hintgv), "open<", tmp); | |
2566 | mg_set(tmp); | |
ac27b0f5 | 2567 | } |
28f23441 | 2568 | break; |
79072805 | 2569 | case '\020': /* ^P */ |
2fdbfb4d AB |
2570 | if (*remaining == '\0') { /* ^P */ |
2571 | PL_perldb = SvIV(sv); | |
2572 | if (PL_perldb && !PL_DBsingle) | |
2573 | init_debugger(); | |
2574 | break; | |
2575 | } else if (strEQ(remaining, "REMATCH")) { /* $^PREMATCH */ | |
2576 | goto do_prematch; | |
2577 | } else if (strEQ(remaining, "OSTMATCH")) { /* $^POSTMATCH */ | |
2578 | goto do_postmatch; | |
2579 | } | |
9b9e0be4 | 2580 | break; |
79072805 | 2581 | case '\024': /* ^T */ |
88e89b8a | 2582 | #ifdef BIG_TIME |
6b88bc9c | 2583 | PL_basetime = (Time_t)(SvNOK(sv) ? SvNVX(sv) : sv_2nv(sv)); |
88e89b8a | 2584 | #else |
38ab35f8 | 2585 | PL_basetime = (Time_t)SvIV(sv); |
88e89b8a | 2586 | #endif |
79072805 | 2587 | break; |
e07ea26a NC |
2588 | case '\025': /* ^UTF8CACHE */ |
2589 | if (strEQ(mg->mg_ptr+1, "TF8CACHE")) { | |
2590 | PL_utf8cache = (signed char) sv_2iv(sv); | |
2591 | } | |
2592 | break; | |
fde18df1 | 2593 | case '\027': /* ^W & $^WARNING_BITS */ |
4438c4b7 JH |
2594 | if (*(mg->mg_ptr+1) == '\0') { |
2595 | if ( ! (PL_dowarn & G_WARN_ALL_MASK)) { | |
38ab35f8 | 2596 | i = SvIV(sv); |
ac27b0f5 | 2597 | PL_dowarn = (PL_dowarn & ~G_WARN_ON) |
0453d815 | 2598 | | (i ? G_WARN_ON : G_WARN_OFF) ; |
4438c4b7 | 2599 | } |
599cee73 | 2600 | } |
0a378802 | 2601 | else if (strEQ(mg->mg_ptr+1, "ARNING_BITS")) { |
4438c4b7 | 2602 | if ( ! (PL_dowarn & G_WARN_ALL_MASK)) { |
d775746e GS |
2603 | if (!SvPOK(sv) && PL_localizing) { |
2604 | sv_setpvn(sv, WARN_NONEstring, WARNsize); | |
d3a7d8c7 | 2605 | PL_compiling.cop_warnings = pWARN_NONE; |
d775746e GS |
2606 | break; |
2607 | } | |
f4fc7782 | 2608 | { |
b5477537 | 2609 | STRLEN len, i; |
d3a7d8c7 | 2610 | int accumulate = 0 ; |
f4fc7782 | 2611 | int any_fatals = 0 ; |
b83604b4 | 2612 | const char * const ptr = SvPV_const(sv, len) ; |
f4fc7782 JH |
2613 | for (i = 0 ; i < len ; ++i) { |
2614 | accumulate |= ptr[i] ; | |
2615 | any_fatals |= (ptr[i] & 0xAA) ; | |
2616 | } | |
4243c432 NC |
2617 | if (!accumulate) { |
2618 | if (!specialWARN(PL_compiling.cop_warnings)) | |
2619 | PerlMemShared_free(PL_compiling.cop_warnings); | |
2620 | PL_compiling.cop_warnings = pWARN_NONE; | |
2621 | } | |
72dc9ed5 NC |
2622 | /* Yuck. I can't see how to abstract this: */ |
2623 | else if (isWARN_on(((STRLEN *)SvPV_nolen_const(sv)) - 1, | |
2624 | WARN_ALL) && !any_fatals) { | |
4243c432 NC |
2625 | if (!specialWARN(PL_compiling.cop_warnings)) |
2626 | PerlMemShared_free(PL_compiling.cop_warnings); | |
f4fc7782 JH |
2627 | PL_compiling.cop_warnings = pWARN_ALL; |
2628 | PL_dowarn |= G_WARN_ONCE ; | |
727405f8 | 2629 | } |
d3a7d8c7 | 2630 | else { |
72dc9ed5 NC |
2631 | STRLEN len; |
2632 | const char *const p = SvPV_const(sv, len); | |
2633 | ||
2634 | PL_compiling.cop_warnings | |
8ee4cf24 | 2635 | = Perl_new_warnings_bitfield(aTHX_ PL_compiling.cop_warnings, |
72dc9ed5 NC |
2636 | p, len); |
2637 | ||
d3a7d8c7 GS |
2638 | if (isWARN_on(PL_compiling.cop_warnings, WARN_ONCE)) |
2639 | PL_dowarn |= G_WARN_ONCE ; | |
2640 | } | |
f4fc7782 | 2641 | |
d3a7d8c7 | 2642 | } |
4438c4b7 | 2643 | } |
971a9dd3 | 2644 | } |
79072805 LW |
2645 | break; |
2646 | case '.': | |
3280af22 NIS |
2647 | if (PL_localizing) { |
2648 | if (PL_localizing == 1) | |
7766f137 | 2649 | SAVESPTR(PL_last_in_gv); |
748a9306 | 2650 | } |
3280af22 | 2651 | else if (SvOK(sv) && GvIO(PL_last_in_gv)) |
632db599 | 2652 | IoLINES(GvIOp(PL_last_in_gv)) = SvIV(sv); |
79072805 LW |
2653 | break; |
2654 | case '^': | |
099be4f1 DM |
2655 | if (isGV_with_GP(PL_defoutgv)) { |
2656 | Safefree(IoTOP_NAME(GvIOp(PL_defoutgv))); | |
2657 | s = IoTOP_NAME(GvIOp(PL_defoutgv)) = savesvpv(sv); | |
2658 | IoTOP_GV(GvIOp(PL_defoutgv)) = gv_fetchsv(sv, GV_ADD, SVt_PVIO); | |
2659 | } | |
79072805 LW |
2660 | break; |
2661 | case '~': | |
099be4f1 DM |
2662 | if (isGV_with_GP(PL_defoutgv)) { |
2663 | Safefree(IoFMT_NAME(GvIOp(PL_defoutgv))); | |
2664 | s = IoFMT_NAME(GvIOp(PL_defoutgv)) = savesvpv(sv); | |
2665 | IoFMT_GV(GvIOp(PL_defoutgv)) = gv_fetchsv(sv, GV_ADD, SVt_PVIO); | |
2666 | } | |
79072805 LW |
2667 | break; |
2668 | case '=': | |
099be4f1 DM |
2669 | if (isGV_with_GP(PL_defoutgv)) |
2670 | IoPAGE_LEN(GvIOp(PL_defoutgv)) = (SvIV(sv)); | |
79072805 LW |
2671 | break; |
2672 | case '-': | |
099be4f1 DM |
2673 | if (isGV_with_GP(PL_defoutgv)) { |
2674 | IoLINES_LEFT(GvIOp(PL_defoutgv)) = (SvIV(sv)); | |
2675 | if (IoLINES_LEFT(GvIOp(PL_defoutgv)) < 0L) | |
2676 | IoLINES_LEFT(GvIOp(PL_defoutgv)) = 0L; | |
2677 | } | |
79072805 LW |
2678 | break; |
2679 | case '%': | |
099be4f1 DM |
2680 | if (isGV_with_GP(PL_defoutgv)) |
2681 | IoPAGE(GvIOp(PL_defoutgv)) = (SvIV(sv)); | |
79072805 LW |
2682 | break; |
2683 | case '|': | |
4b65379b | 2684 | { |
099be4f1 | 2685 | IO * const io = GvIO(PL_defoutgv); |
720f287d AB |
2686 | if(!io) |
2687 | break; | |
38ab35f8 | 2688 | if ((SvIV(sv)) == 0) |
4b65379b CS |
2689 | IoFLAGS(io) &= ~IOf_FLUSH; |
2690 | else { | |
2691 | if (!(IoFLAGS(io) & IOf_FLUSH)) { | |
2692 | PerlIO *ofp = IoOFP(io); | |
2693 | if (ofp) | |
2694 | (void)PerlIO_flush(ofp); | |
2695 | IoFLAGS(io) |= IOf_FLUSH; | |
2696 | } | |
2697 | } | |
79072805 LW |
2698 | } |
2699 | break; | |
79072805 | 2700 | case '/': |
3280af22 | 2701 | SvREFCNT_dec(PL_rs); |
8bfdd7d9 | 2702 | PL_rs = newSVsv(sv); |
79072805 LW |
2703 | break; |
2704 | case '\\': | |
ef8d46e8 | 2705 | SvREFCNT_dec(PL_ors_sv); |
009c130f | 2706 | if (SvOK(sv) || SvGMAGICAL(sv)) { |
7889fe52 | 2707 | PL_ors_sv = newSVsv(sv); |
009c130f | 2708 | } |
e3c19b7b | 2709 | else { |
a0714e2c | 2710 | PL_ors_sv = NULL; |
e3c19b7b | 2711 | } |
79072805 | 2712 | break; |
79072805 | 2713 | case '[': |
38ab35f8 | 2714 | CopARYBASE_set(&PL_compiling, SvIV(sv)); |
79072805 LW |
2715 | break; |
2716 | case '?': | |
ff0cee69 | 2717 | #ifdef COMPLEX_STATUS |
6b88bc9c | 2718 | if (PL_localizing == 2) { |
41cb7b2b | 2719 | SvUPGRADE(sv, SVt_PVLV); |
6b88bc9c GS |
2720 | PL_statusvalue = LvTARGOFF(sv); |
2721 | PL_statusvalue_vms = LvTARGLEN(sv); | |
ff0cee69 | 2722 | } |
2723 | else | |
2724 | #endif | |
2725 | #ifdef VMSISH_STATUS | |
2726 | if (VMSISH_STATUS) | |
fb38d079 | 2727 | STATUS_NATIVE_CHILD_SET((U32)SvIV(sv)); |
ff0cee69 | 2728 | else |
2729 | #endif | |
38ab35f8 | 2730 | STATUS_UNIX_EXIT_SET(SvIV(sv)); |
79072805 LW |
2731 | break; |
2732 | case '!': | |
93189314 JH |
2733 | { |
2734 | #ifdef VMS | |
2735 | # define PERL_VMS_BANG vaxc$errno | |
2736 | #else | |
2737 | # define PERL_VMS_BANG 0 | |
2738 | #endif | |
91487cfc | 2739 | SETERRNO(SvIOK(sv) ? SvIVX(sv) : SvOK(sv) ? sv_2iv(sv) : 0, |
93189314 JH |
2740 | (SvIV(sv) == EVMSERR) ? 4 : PERL_VMS_BANG); |
2741 | } | |
79072805 LW |
2742 | break; |
2743 | case '<': | |
38ab35f8 | 2744 | PL_uid = SvIV(sv); |
3280af22 NIS |
2745 | if (PL_delaymagic) { |
2746 | PL_delaymagic |= DM_RUID; | |
79072805 LW |
2747 | break; /* don't do magic till later */ |
2748 | } | |
2749 | #ifdef HAS_SETRUID | |
b28d0864 | 2750 | (void)setruid((Uid_t)PL_uid); |
79072805 LW |
2751 | #else |
2752 | #ifdef HAS_SETREUID | |
3280af22 | 2753 | (void)setreuid((Uid_t)PL_uid, (Uid_t)-1); |
748a9306 | 2754 | #else |
85e6fe83 | 2755 | #ifdef HAS_SETRESUID |
b28d0864 | 2756 | (void)setresuid((Uid_t)PL_uid, (Uid_t)-1, (Uid_t)-1); |
79072805 | 2757 | #else |
75870ed3 | 2758 | if (PL_uid == PL_euid) { /* special case $< = $> */ |
2759 | #ifdef PERL_DARWIN | |
2760 | /* workaround for Darwin's setuid peculiarity, cf [perl #24122] */ | |
2761 | if (PL_uid != 0 && PerlProc_getuid() == 0) | |
2762 | (void)PerlProc_setuid(0); | |
2763 | #endif | |
b28d0864 | 2764 | (void)PerlProc_setuid(PL_uid); |
75870ed3 | 2765 | } else { |
d8eceb89 | 2766 | PL_uid = PerlProc_getuid(); |
cea2e8a9 | 2767 | Perl_croak(aTHX_ "setruid() not implemented"); |
a0d0e21e | 2768 | } |
79072805 LW |
2769 | #endif |
2770 | #endif | |
85e6fe83 | 2771 | #endif |
d8eceb89 | 2772 | PL_uid = PerlProc_getuid(); |
79072805 LW |
2773 | break; |
2774 | case '>': | |
38ab35f8 | 2775 | PL_euid = SvIV(sv); |
3280af22 NIS |
2776 | if (PL_delaymagic) { |
2777 | PL_delaymagic |= DM_EUID; | |
79072805 LW |
2778 | break; /* don't do magic till later */ |
2779 | } | |
2780 | #ifdef HAS_SETEUID | |
3280af22 | 2781 | (void)seteuid((Uid_t)PL_euid); |
79072805 LW |
2782 | #else |
2783 | #ifdef HAS_SETREUID | |
b28d0864 | 2784 | (void)setreuid((Uid_t)-1, (Uid_t)PL_euid); |
85e6fe83 LW |
2785 | #else |
2786 | #ifdef HAS_SETRESUID | |
6b88bc9c | 2787 | (void)setresuid((Uid_t)-1, (Uid_t)PL_euid, (Uid_t)-1); |
79072805 | 2788 | #else |
b28d0864 NIS |
2789 | if (PL_euid == PL_uid) /* special case $> = $< */ |
2790 | PerlProc_setuid(PL_euid); | |
a0d0e21e | 2791 | else { |
e8ee3774 | 2792 | PL_euid = PerlProc_geteuid(); |
cea2e8a9 | 2793 | Perl_croak(aTHX_ "seteuid() not implemented"); |
a0d0e21e | 2794 | } |
79072805 LW |
2795 | #endif |
2796 | #endif | |
85e6fe83 | 2797 | #endif |
d8eceb89 | 2798 | PL_euid = PerlProc_geteuid(); |
79072805 LW |
2799 | break; |
2800 | case '(': | |
38ab35f8 | 2801 | PL_gid = SvIV(sv); |
3280af22 NIS |
2802 | if (PL_delaymagic) { |
2803 | PL_delaymagic |= DM_RGID; | |
79072805 LW |
2804 | break; /* don't do magic till later */ |
2805 | } | |
2806 | #ifdef HAS_SETRGID | |
b28d0864 | 2807 | (void)setrgid((Gid_t)PL_gid); |
79072805 LW |
2808 | #else |
2809 | #ifdef HAS_SETREGID | |
3280af22 | 2810 | (void)setregid((Gid_t)PL_gid, (Gid_t)-1); |
85e6fe83 LW |
2811 | #else |
2812 | #ifdef HAS_SETRESGID | |
b28d0864 | 2813 | (void)setresgid((Gid_t)PL_gid, (Gid_t)-1, (Gid_t) 1); |
79072805 | 2814 | #else |
b28d0864 NIS |
2815 | if (PL_gid == PL_egid) /* special case $( = $) */ |
2816 | (void)PerlProc_setgid(PL_gid); | |
748a9306 | 2817 | else { |
d8eceb89 | 2818 | PL_gid = PerlProc_getgid(); |
cea2e8a9 | 2819 | Perl_croak(aTHX_ "setrgid() not implemented"); |
748a9306 | 2820 | } |
79072805 LW |
2821 | #endif |
2822 | #endif | |
85e6fe83 | 2823 | #endif |
d8eceb89 | 2824 | PL_gid = PerlProc_getgid(); |
79072805 LW |
2825 | break; |
2826 | case ')': | |
5cd24f17 | 2827 | #ifdef HAS_SETGROUPS |
2828 | { | |
b83604b4 | 2829 | const char *p = SvPV_const(sv, len); |
757f63d8 | 2830 | Groups_t *gary = NULL; |
fb4089e0 | 2831 | #ifdef _SC_NGROUPS_MAX |
2832 | int maxgrp = sysconf(_SC_NGROUPS_MAX); | |
2833 | ||
2834 | if (maxgrp < 0) | |
2835 | maxgrp = NGROUPS; | |
2836 | #else | |
2837 | int maxgrp = NGROUPS; | |
2838 | #endif | |
757f63d8 SP |
2839 | |
2840 | while (isSPACE(*p)) | |
2841 | ++p; | |
2842 | PL_egid = Atol(p); | |
fb4089e0 | 2843 | for (i = 0; i < maxgrp; ++i) { |
757f63d8 SP |
2844 | while (*p && !isSPACE(*p)) |
2845 | ++p; | |
2846 | while (isSPACE(*p)) | |
2847 | ++p; | |
2848 | if (!*p) | |
2849 | break; | |
2850 | if(!gary) | |
2851 | Newx(gary, i + 1, Groups_t); | |
2852 | else | |
2853 | Renew(gary, i + 1, Groups_t); | |
2854 | gary[i] = Atol(p); | |
2855 | } | |
2856 | if (i) | |
2857 | (void)setgroups(i, gary); | |
f5a63d97 | 2858 | Safefree(gary); |
5cd24f17 | 2859 | } |
2860 | #else /* HAS_SETGROUPS */ | |
38ab35f8 | 2861 | PL_egid = SvIV(sv); |
5cd24f17 | 2862 | #endif /* HAS_SETGROUPS */ |
3280af22 NIS |
2863 | if (PL_delaymagic) { |
2864 | PL_delaymagic |= DM_EGID; | |
79072805 LW |
2865 | break; /* don't do magic till later */ |
2866 | } | |
2867 | #ifdef HAS_SETEGID | |
3280af22 | 2868 | (void)setegid((Gid_t)PL_egid); |
79072805 LW |
2869 | #else |
2870 | #ifdef HAS_SETREGID | |
b28d0864 | 2871 | (void)setregid((Gid_t)-1, (Gid_t)PL_egid); |
85e6fe83 LW |
2872 | #else |
2873 | #ifdef HAS_SETRESGID | |
b28d0864 | 2874 | (void)setresgid((Gid_t)-1, (Gid_t)PL_egid, (Gid_t)-1); |
79072805 | 2875 | #else |
b28d0864 NIS |
2876 | if (PL_egid == PL_gid) /* special case $) = $( */ |
2877 | (void)PerlProc_setgid(PL_egid); | |
748a9306 | 2878 | else { |
d8eceb89 | 2879 | PL_egid = PerlProc_getegid(); |
cea2e8a9 | 2880 | Perl_croak(aTHX_ "setegid() not implemented"); |
748a9306 | 2881 | } |
79072805 LW |
2882 | #endif |
2883 | #endif | |
85e6fe83 | 2884 | #endif |
d8eceb89 | 2885 | PL_egid = PerlProc_getegid(); |
79072805 LW |
2886 | break; |
2887 | case ':': | |
2d8e6c8d | 2888 | PL_chopset = SvPV_force(sv,len); |
79072805 LW |
2889 | break; |
2890 | case '0': | |
e2975953 | 2891 | LOCK_DOLLARZERO_MUTEX; |
4bc88a62 PS |
2892 | #ifdef HAS_SETPROCTITLE |
2893 | /* The BSDs don't show the argv[] in ps(1) output, they | |
2894 | * show a string from the process struct and provide | |
2895 | * the setproctitle() routine to manipulate that. */ | |
a2722ac9 | 2896 | if (PL_origalen != 1) { |
b83604b4 | 2897 | s = SvPV_const(sv, len); |
98b76f99 | 2898 | # if __FreeBSD_version > 410001 |
9aad2c0e | 2899 | /* The leading "-" removes the "perl: " prefix, |
4bc88a62 PS |
2900 | * but not the "(perl) suffix from the ps(1) |
2901 | * output, because that's what ps(1) shows if the | |
2902 | * argv[] is modified. */ | |
6f2ad931 | 2903 | setproctitle("-%s", s); |
9aad2c0e | 2904 | # else /* old FreeBSDs, NetBSD, OpenBSD, anyBSD */ |
4bc88a62 PS |
2905 | /* This doesn't really work if you assume that |
2906 | * $0 = 'foobar'; will wipe out 'perl' from the $0 | |
2907 | * because in ps(1) output the result will be like | |
2908 | * sprintf("perl: %s (perl)", s) | |
2909 | * I guess this is a security feature: | |
2910 | * one (a user process) cannot get rid of the original name. | |
2911 | * --jhi */ | |
2912 | setproctitle("%s", s); | |
2913 | # endif | |
2914 | } | |
9d3968b2 | 2915 | #elif defined(__hpux) && defined(PSTAT_SETCMD) |
a2722ac9 | 2916 | if (PL_origalen != 1) { |
17aa7f3d | 2917 | union pstun un; |
b83604b4 | 2918 | s = SvPV_const(sv, len); |
6867be6d | 2919 | un.pst_command = (char *)s; |
17aa7f3d JH |
2920 | pstat(PSTAT_SETCMD, un, len, 0, 0); |
2921 | } | |
9d3968b2 | 2922 | #else |
2d2af554 GA |
2923 | if (PL_origalen > 1) { |
2924 | /* PL_origalen is set in perl_parse(). */ | |
2925 | s = SvPV_force(sv,len); | |
2926 | if (len >= (STRLEN)PL_origalen-1) { | |
2927 | /* Longer than original, will be truncated. We assume that | |
2928 | * PL_origalen bytes are available. */ | |
2929 | Copy(s, PL_origargv[0], PL_origalen-1, char); | |
2930 | } | |
2931 | else { | |
2932 | /* Shorter than original, will be padded. */ | |
235ac35d | 2933 | #ifdef PERL_DARWIN |
60777a0d JH |
2934 | /* Special case for Mac OS X: see [perl #38868] */ |
2935 | const int pad = 0; | |
235ac35d | 2936 | #else |
8a89a4f1 MB |
2937 | /* Is the space counterintuitive? Yes. |
2938 | * (You were expecting \0?) | |
2939 | * Does it work? Seems to. (In Linux 2.4.20 at least.) | |
2940 | * --jhi */ | |
60777a0d | 2941 | const int pad = ' '; |
235ac35d | 2942 | #endif |
60777a0d JH |
2943 | Copy(s, PL_origargv[0], len, char); |
2944 | PL_origargv[0][len] = 0; | |
2945 | memset(PL_origargv[0] + len + 1, | |
2946 | pad, PL_origalen - len - 1); | |
2d2af554 GA |
2947 | } |
2948 | PL_origargv[0][PL_origalen-1] = 0; | |
2949 | for (i = 1; i < PL_origargc; i++) | |
2950 | PL_origargv[i] = 0; | |
7636ea95 AB |
2951 | #ifdef HAS_PRCTL_SET_NAME |
2952 | /* Set the legacy process name in addition to the POSIX name on Linux */ | |
2953 | if (prctl(PR_SET_NAME, (unsigned long)s, 0, 0, 0) != 0) { | |
2954 | /* diag_listed_as: SKIPME */ | |
2955 | Perl_croak(aTHX_ "Can't set $0 with prctl(): %s", Strerror(errno)); | |
2956 | } | |
2957 | #endif | |
79072805 | 2958 | } |
9d3968b2 | 2959 | #endif |
e2975953 | 2960 | UNLOCK_DOLLARZERO_MUTEX; |
79072805 LW |
2961 | break; |
2962 | } | |
2963 | return 0; | |
2964 | } | |
2965 | ||
2966 | I32 | |
35a4481c | 2967 | Perl_whichsig(pTHX_ const char *sig) |
79072805 | 2968 | { |
aadb217d | 2969 | register char* const* sigv; |
7918f24d NC |
2970 | |
2971 | PERL_ARGS_ASSERT_WHICHSIG; | |
96a5add6 | 2972 | PERL_UNUSED_CONTEXT; |
79072805 | 2973 | |
aadb217d | 2974 | for (sigv = (char* const*)PL_sig_name; *sigv; sigv++) |
79072805 | 2975 | if (strEQ(sig,*sigv)) |
aadb217d | 2976 | return PL_sig_num[sigv - (char* const*)PL_sig_name]; |
79072805 LW |
2977 | #ifdef SIGCLD |
2978 | if (strEQ(sig,"CHLD")) | |
2979 | return SIGCLD; | |
2980 | #endif | |
2981 | #ifdef SIGCHLD | |
2982 | if (strEQ(sig,"CLD")) | |
2983 | return SIGCHLD; | |
2984 | #endif | |
7f1236c0 | 2985 | return -1; |
79072805 LW |
2986 | } |
2987 | ||
ecfc5424 | 2988 | Signal_t |
1e82f5a6 | 2989 | #if defined(HAS_SIGACTION) && defined(SA_SIGINFO) |
b6455c53 | 2990 | Perl_sighandler(int sig, siginfo_t *sip, void *uap PERL_UNUSED_DECL) |
1e82f5a6 SH |
2991 | #else |
2992 | Perl_sighandler(int sig) | |
2993 | #endif | |
79072805 | 2994 | { |
1018e26f NIS |
2995 | #ifdef PERL_GET_SIG_CONTEXT |
2996 | dTHXa(PERL_GET_SIG_CONTEXT); | |
71d280e3 | 2997 | #else |
cea2e8a9 | 2998 | dTHX; |
71d280e3 | 2999 | #endif |
79072805 | 3000 | dSP; |
a0714e2c SS |
3001 | GV *gv = NULL; |
3002 | SV *sv = NULL; | |
8772537c | 3003 | SV * const tSv = PL_Sv; |
601f1833 | 3004 | CV *cv = NULL; |
533c011a | 3005 | OP *myop = PL_op; |
84902520 | 3006 | U32 flags = 0; |
8772537c | 3007 | XPV * const tXpv = PL_Xpv; |
0c4d3b5e | 3008 | I32 old_ss_ix = PL_savestack_ix; |
71d280e3 | 3009 | |
84902520 | 3010 | |
727405f8 | 3011 | if (!PL_psig_ptr[sig]) { |
99ef548b | 3012 | PerlIO_printf(Perl_error_log, "Signal SIG%s received, but no signal handler set.\n", |
727405f8 NIS |
3013 | PL_sig_name[sig]); |
3014 | exit(sig); | |
3015 | } | |
ff0cee69 | 3016 | |
84902520 TB |
3017 | /* Max number of items pushed there is 3*n or 4. We cannot fix |
3018 | infinity, so we fix 4 (in fact 5): */ | |
72048cfe DM |
3019 | if (PL_savestack_ix + 15 <= PL_savestack_max) { |
3020 | flags |= 1; | |
3280af22 | 3021 | PL_savestack_ix += 5; /* Protect save in progress. */ |
0c4d3b5e | 3022 | SAVEDESTRUCTOR_X(S_unwind_handler_stack, NULL); |
84902520 | 3023 | } |
72048cfe DM |
3024 | if (PL_markstack_ptr < PL_markstack_max - 2) { |
3025 | flags |= 2; | |
3280af22 | 3026 | PL_markstack_ptr++; /* Protect mark. */ |
72048cfe DM |
3027 | } |
3028 | if (PL_scopestack_ix < PL_scopestack_max - 3) { | |
3029 | flags |= 4; | |
3030 | PL_scopestack_ix++; | |
3031 | } | |
84902520 | 3032 | /* sv_2cv is too complicated, try a simpler variant first: */ |
ea726b52 | 3033 | if (!SvROK(PL_psig_ptr[sig]) || !(cv = MUTABLE_CV(SvRV(PL_psig_ptr[sig]))) |
8772537c AL |
3034 | || SvTYPE(cv) != SVt_PVCV) { |
3035 | HV *st; | |
f2c0649b | 3036 | cv = sv_2cv(PL_psig_ptr[sig], &st, &gv, GV_ADD); |
8772537c | 3037 | } |
84902520 | 3038 | |
a0d0e21e | 3039 | if (!cv || !CvROOT(cv)) { |
a2a5de95 NC |
3040 | Perl_ck_warner(aTHX_ packWARN(WARN_SIGNAL), "SIG%s handler \"%s\" not defined.\n", |
3041 | PL_sig_name[sig], (gv ? GvENAME(gv) | |
3042 | : ((cv && CvGV(cv)) | |
3043 | ? GvENAME(CvGV(cv)) | |
3044 | : "__ANON__"))); | |
00d579c5 | 3045 | goto cleanup; |
79072805 LW |
3046 | } |
3047 | ||
0c4d3b5e DM |
3048 | sv = PL_psig_name[sig] |
3049 | ? SvREFCNT_inc_NN(PL_psig_name[sig]) | |
3050 | : newSVpv(PL_sig_name[sig],0); | |
72048cfe | 3051 | flags |= 8; |
0c4d3b5e DM |
3052 | SAVEFREESV(sv); |
3053 | ||
3054 | /* make sure our assumption about the size of the SAVEs are correct: | |
3055 | * 3 for SAVEDESTRUCTOR_X, 2 for SAVEFREESV */ | |
3056 | assert(old_ss_ix + 2 + ((flags & 1) ? 3+5 : 0) == PL_savestack_ix); | |
e336de0d | 3057 | |
e788e7d3 | 3058 | PUSHSTACKi(PERLSI_SIGNAL); |
924508f0 | 3059 | PUSHMARK(SP); |
79072805 | 3060 | PUSHs(sv); |
8aad04aa JH |
3061 | #if defined(HAS_SIGACTION) && defined(SA_SIGINFO) |
3062 | { | |
3063 | struct sigaction oact; | |
3064 | ||
3065 | if (sigaction(sig, 0, &oact) == 0 && oact.sa_flags & SA_SIGINFO) { | |
8aad04aa JH |
3066 | if (sip) { |
3067 | HV *sih = newHV(); | |
ad64d0ec | 3068 | SV *rv = newRV_noinc(MUTABLE_SV(sih)); |
8aad04aa JH |
3069 | /* The siginfo fields signo, code, errno, pid, uid, |
3070 | * addr, status, and band are defined by POSIX/SUSv3. */ | |
85771703 NC |
3071 | (void)hv_stores(sih, "signo", newSViv(sip->si_signo)); |
3072 | (void)hv_stores(sih, "code", newSViv(sip->si_code)); | |
79dec0f4 | 3073 | #if 0 /* XXX TODO: Configure scan for the existence of these, but even that does not help if the SA_SIGINFO is not implemented according to the spec. */ |
85771703 NC |
3074 | hv_stores(sih, "errno", newSViv(sip->si_errno)); |
3075 | hv_stores(sih, "status", newSViv(sip->si_status)); | |
3076 | hv_stores(sih, "uid", newSViv(sip->si_uid)); | |
3077 | hv_stores(sih, "pid", newSViv(sip->si_pid)); | |
3078 | hv_stores(sih, "addr", newSVuv(PTR2UV(sip->si_addr))); | |
3079 | hv_stores(sih, "band", newSViv(sip->si_band)); | |
79dec0f4 | 3080 | #endif |
8aad04aa | 3081 | EXTEND(SP, 2); |
ad64d0ec | 3082 | PUSHs(rv); |
22f1178f | 3083 | mPUSHp((char *)sip, sizeof(*sip)); |
8aad04aa | 3084 | } |
b4552a27 | 3085 | |
8aad04aa JH |
3086 | } |
3087 | } | |
3088 | #endif | |
79072805 | 3089 | PUTBACK; |
a0d0e21e | 3090 | |
ad64d0ec | 3091 | call_sv(MUTABLE_SV(cv), G_DISCARD|G_EVAL); |
79072805 | 3092 | |
d3acc0f7 | 3093 | POPSTACK; |
1b266415 | 3094 | if (SvTRUE(ERRSV)) { |
1d615522 | 3095 | #ifndef PERL_MICRO |
983dbef6 | 3096 | #ifdef HAS_SIGPROCMASK |
1b266415 NIS |
3097 | /* Handler "died", for example to get out of a restart-able read(). |
3098 | * Before we re-do that on its behalf re-enable the signal which was | |
3099 | * blocked by the system when we entered. | |
3100 | */ | |
3101 | sigset_t set; | |
3102 | sigemptyset(&set); | |
3103 | sigaddset(&set,sig); | |
3104 | sigprocmask(SIG_UNBLOCK, &set, NULL); | |
3105 | #else | |
3106 | /* Not clear if this will work */ | |
3107 | (void)rsignal(sig, SIG_IGN); | |
5c1546dc | 3108 | (void)rsignal(sig, PL_csighandlerp); |
1b266415 | 3109 | #endif |
1d615522 | 3110 | #endif /* !PERL_MICRO */ |
c5df3096 | 3111 | die_sv(ERRSV); |
1b266415 | 3112 | } |
00d579c5 | 3113 | cleanup: |
0c4d3b5e DM |
3114 | /* pop any of SAVEFREESV, SAVEDESTRUCTOR_X and "save in progress" */ |
3115 | PL_savestack_ix = old_ss_ix; | |
72048cfe | 3116 | if (flags & 2) |
3280af22 | 3117 | PL_markstack_ptr--; |
72048cfe | 3118 | if (flags & 4) |
3280af22 | 3119 | PL_scopestack_ix -= 1; |
72048cfe | 3120 | if (flags & 8) |
84902520 | 3121 | SvREFCNT_dec(sv); |
533c011a | 3122 | PL_op = myop; /* Apparently not needed... */ |
ac27b0f5 | 3123 | |
3280af22 NIS |
3124 | PL_Sv = tSv; /* Restore global temporaries. */ |
3125 | PL_Xpv = tXpv; | |
53bb94e2 | 3126 | return; |
79072805 | 3127 | } |
4e35701f NIS |
3128 | |
3129 | ||
51371543 | 3130 | static void |
8772537c | 3131 | S_restore_magic(pTHX_ const void *p) |
51371543 | 3132 | { |
97aff369 | 3133 | dVAR; |
8772537c AL |
3134 | MGS* const mgs = SSPTR(PTR2IV(p), MGS*); |
3135 | SV* const sv = mgs->mgs_sv; | |
51371543 GS |
3136 | |
3137 | if (!sv) | |
3138 | return; | |
3139 | ||
3140 | if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) | |
3141 | { | |
f8c7b90f | 3142 | #ifdef PERL_OLD_COPY_ON_WRITE |
f9701176 NC |
3143 | /* While magic was saved (and off) sv_setsv may well have seen |
3144 | this SV as a prime candidate for COW. */ | |
3145 | if (SvIsCOW(sv)) | |
e424a81e | 3146 | sv_force_normal_flags(sv, 0); |
f9701176 NC |
3147 | #endif |
3148 | ||
f9c6fee5 CS |
3149 | if (mgs->mgs_readonly) |
3150 | SvREADONLY_on(sv); | |
3151 | if (mgs->mgs_magical) | |
3152 | SvFLAGS(sv) |= mgs->mgs_magical; | |
51371543 GS |
3153 | else |
3154 | mg_magical(sv); | |
2b77b520 YST |
3155 | if (SvGMAGICAL(sv)) { |
3156 | /* downgrade public flags to private, | |
3157 | and discard any other private flags */ | |
3158 | ||
10edeb5d JH |
3159 | const U32 pubflags = SvFLAGS(sv) & (SVf_IOK|SVf_NOK|SVf_POK); |
3160 | if (pubflags) { | |
3161 | SvFLAGS(sv) &= ~( pubflags | (SVp_IOK|SVp_NOK|SVp_POK) ); | |
3162 | SvFLAGS(sv) |= ( pubflags << PRIVSHIFT ); | |
2b77b520 YST |
3163 | } |
3164 | } | |
51371543 GS |
3165 | } |
3166 | ||
3167 | mgs->mgs_sv = NULL; /* mark the MGS structure as restored */ | |
3168 | ||
3169 | /* If we're still on top of the stack, pop us off. (That condition | |
3170 | * will be satisfied if restore_magic was called explicitly, but *not* | |
3171 | * if it's being called via leave_scope.) | |
3172 | * The reason for doing this is that otherwise, things like sv_2cv() | |
3173 | * may leave alloc gunk on the savestack, and some code | |
3174 | * (e.g. sighandler) doesn't expect that... | |
3175 | */ | |
3176 | if (PL_savestack_ix == mgs->mgs_ss_ix) | |
3177 | { | |
1be36ce0 NC |
3178 | UV popval = SSPOPUV; |
3179 | assert(popval == SAVEt_DESTRUCTOR_X); | |
51371543 | 3180 | PL_savestack_ix -= 2; |
1be36ce0 NC |
3181 | popval = SSPOPUV; |
3182 | assert((popval & SAVE_MASK) == SAVEt_ALLOC); | |
3183 | PL_savestack_ix -= popval >> SAVE_TIGHT_SHIFT; | |
51371543 | 3184 | } |
8985fe98 DM |
3185 | if (SvREFCNT(sv) == 1) { |
3186 | /* We hold the last reference to this SV, which implies that the | |
3187 | SV was deleted as a side effect of the routines we called. | |
3188 | So artificially keep it alive a bit longer. | |
3189 | We avoid turning on the TEMP flag, which can cause the SV's | |
3190 | buffer to get stolen (and maybe other stuff). */ | |
3191 | int was_temp = SvTEMP(sv); | |
3192 | sv_2mortal(sv); | |
3193 | if (!was_temp) { | |
3194 | SvTEMP_off(sv); | |
3195 | } | |
3196 | SvOK_off(sv); | |
3197 | } | |
3198 | else | |
3199 | SvREFCNT_dec(sv); /* undo the inc in S_save_magic() */ | |
51371543 GS |
3200 | } |
3201 | ||
0c4d3b5e DM |
3202 | /* clean up the mess created by Perl_sighandler(). |
3203 | * Note that this is only called during an exit in a signal handler; | |
3204 | * a die is trapped by the call_sv() and the SAVEDESTRUCTOR_X manually | |
3205 | * skipped over. This is why we don't need to fix up the markstack and | |
3206 | * scopestack - they're going to be set to 0 anyway */ | |
3207 | ||
51371543 | 3208 | static void |
8772537c | 3209 | S_unwind_handler_stack(pTHX_ const void *p) |
51371543 | 3210 | { |
27da23d5 | 3211 | dVAR; |
0c4d3b5e | 3212 | PERL_UNUSED_ARG(p); |
7918f24d | 3213 | |
0c4d3b5e | 3214 | PL_savestack_ix -= 5; /* Unprotect save in progress. */ |
51371543 | 3215 | } |
1018e26f | 3216 | |
66610fdd | 3217 | /* |
b3ca2e83 NC |
3218 | =for apidoc magic_sethint |
3219 | ||
3220 | Triggered by a store to %^H, records the key/value pair to | |
c28fe1ec NC |
3221 | C<PL_compiling.cop_hints_hash>. It is assumed that hints aren't storing |
3222 | anything that would need a deep copy. Maybe we should warn if we find a | |
3223 | reference. | |
b3ca2e83 NC |
3224 | |
3225 | =cut | |
3226 | */ | |
3227 | int | |
3228 | Perl_magic_sethint(pTHX_ SV *sv, MAGIC *mg) | |
3229 | { | |
3230 | dVAR; | |
ad64d0ec | 3231 | SV *key = (mg->mg_len == HEf_SVKEY) ? MUTABLE_SV(mg->mg_ptr) |
59cd0e26 | 3232 | : newSVpvn_flags(mg->mg_ptr, mg->mg_len, SVs_TEMP); |
b3ca2e83 | 3233 | |
7918f24d NC |
3234 | PERL_ARGS_ASSERT_MAGIC_SETHINT; |
3235 | ||
e6e3e454 NC |
3236 | /* mg->mg_obj isn't being used. If needed, it would be possible to store |
3237 | an alternative leaf in there, with PL_compiling.cop_hints being used if | |
3238 | it's NULL. If needed for threads, the alternative could lock a mutex, | |
3239 | or take other more complex action. */ | |
3240 | ||
5b9c0671 NC |
3241 | /* Something changed in %^H, so it will need to be restored on scope exit. |
3242 | Doing this here saves a lot of doing it manually in perl code (and | |
3243 | forgetting to do it, and consequent subtle errors. */ | |
3244 | PL_hints |= HINT_LOCALIZE_HH; | |
20439bc7 Z |
3245 | CopHINTHASH_set(&PL_compiling, |
3246 | cophh_store_sv(CopHINTHASH_get(&PL_compiling), key, 0, sv, 0)); | |
b3ca2e83 NC |
3247 | return 0; |
3248 | } | |
3249 | ||
3250 | /* | |
f175cff5 | 3251 | =for apidoc magic_clearhint |
b3ca2e83 | 3252 | |
c28fe1ec NC |
3253 | Triggered by a delete from %^H, records the key to |
3254 | C<PL_compiling.cop_hints_hash>. | |
b3ca2e83 NC |
3255 | |
3256 | =cut | |
3257 | */ | |
3258 | int | |
3259 | Perl_magic_clearhint(pTHX_ SV *sv, MAGIC *mg) | |
3260 | { | |
3261 | dVAR; | |
7918f24d NC |
3262 | |
3263 | PERL_ARGS_ASSERT_MAGIC_CLEARHINT; | |
f5a63d97 AL |
3264 | PERL_UNUSED_ARG(sv); |
3265 | ||
b3ca2e83 NC |
3266 | assert(mg->mg_len == HEf_SVKEY); |
3267 | ||
b3f24c00 MHM |
3268 | PERL_UNUSED_ARG(sv); |
3269 | ||
5b9c0671 | 3270 | PL_hints |= HINT_LOCALIZE_HH; |
20439bc7 Z |
3271 | CopHINTHASH_set(&PL_compiling, |
3272 | cophh_delete_sv(CopHINTHASH_get(&PL_compiling), | |
3273 | MUTABLE_SV(mg->mg_ptr), 0, 0)); | |
b3ca2e83 NC |
3274 | return 0; |
3275 | } | |
3276 | ||
3277 | /* | |
f747ebd6 Z |
3278 | =for apidoc magic_clearhints |
3279 | ||
3280 | Triggered by clearing %^H, resets C<PL_compiling.cop_hints_hash>. | |
3281 | ||
3282 | =cut | |
3283 | */ | |
3284 | int | |
3285 | Perl_magic_clearhints(pTHX_ SV *sv, MAGIC *mg) | |
3286 | { | |
3287 | PERL_ARGS_ASSERT_MAGIC_CLEARHINTS; | |
3288 | PERL_UNUSED_ARG(sv); | |
3289 | PERL_UNUSED_ARG(mg); | |
20439bc7 Z |
3290 | cophh_free(CopHINTHASH_get(&PL_compiling)); |
3291 | CopHINTHASH_set(&PL_compiling, cophh_new_empty()); | |
f747ebd6 Z |
3292 | return 0; |
3293 | } | |
3294 | ||
3295 | /* | |
66610fdd RGS |
3296 | * Local variables: |
3297 | * c-indentation-style: bsd | |
3298 | * c-basic-offset: 4 | |
3299 | * indent-tabs-mode: t | |
3300 | * End: | |
3301 | * | |
37442d52 RGS |
3302 | * ex: set ts=8 sts=4 sw=4 noet: |
3303 | */ |