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