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