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