This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Fix for [ID 20010124.001] POSIX::errno unreliable
[perl5.git] / sv.c
CommitLineData
a0d0e21e 1/* sv.c
79072805 2 *
bc89e66f 3 * Copyright (c) 1991-2001, Larry Wall
79072805
LW
4 *
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
7 *
a0d0e21e 8 * "I wonder what the Entish is for 'yes' and 'no'," he thought.
645c22ef
DM
9 *
10 *
11 * Manipulation of scalar values (SVs). This file contains the code that
12 * creates, manipulates and destroys SVs. (Opcode-level functions on SVs
13 * can be found in the various pp*.c files.) Note that the basic structure
14 * of an SV is also used to hold the other major Perl data types - AVs,
15 * HVs, GVs, IO etc. Low-level functions on these other types - such as
16 * memory allocation and destruction - are handled within this file, while
17 * higher-level stuff can be found in the individual files av.c, hv.c,
18 * etc.
79072805
LW
19 */
20
21#include "EXTERN.h"
864dbfa3 22#define PERL_IN_SV_C
79072805 23#include "perl.h"
79072805 24
51371543 25#define FCALL *f
6fc92669 26#define SV_CHECK_THINKFIRST(sv) if (SvTHINKFIRST(sv)) sv_force_normal(sv)
2c5424a7 27
645c22ef
DM
28
29/* ============================================================================
30
31=head1 Allocation and deallocation of SVs.
32
33An SV (or AV, HV etc) is in 2 parts: the head and the body. There is only
34one type of head, but around 13 body types. Head and body are each
35separately allocated. Normally, this allocation is done using arenas,
36which are approximately 1K chunks of memory parcelled up into N heads or
37bodies. The first slot in each arena is reserved, and is used to hold a
38link to the next arena. In the case of heads, the unused first slot
39also contains some flags and a note of the number of slots. Snaked through
40each arena chain is a linked list of free items; when this becomes empty,
41an extra arena is allocated and divided up into N items which are threaded
42into the free list.
43
44The following global variables are associated with arenas:
45
46 PL_sv_arenaroot pointer to list of SV arenas
47 PL_sv_root pointer to list of free SV structures
48
49 PL_foo_arenaroot pointer to list of foo arenas,
50 PL_foo_root pointer to list of free foo bodies
51 ... for foo in xiv, xnv, xrv, xpv etc.
52
53Note that some of the larger and more rarely used body types (eg xpvio)
54are not allocated using arenas, but are instead just malloc()/free()ed as
55required. Also, if PURIFY is defined, arenas are abandoned altogether,
56with all items individually malloc()ed. In addition, a few SV heads are
57not allocated from an arena, but are instead directly created as static
58or auto variables, eg PL_sv_undef.
59
60The SV arena serves the secondary purpose of allowing still-live SVs
61to be located and destroyed during final cleanup.
62
63At the lowest level, the macros new_SV() and del_SV() grab and free
64an SV head. (If debugging with -DD, del_SV() calls the function S_del_sv()
65to return the SV to the free list with error checking.) new_SV() calls
66more_sv() / sv_add_arena() to add an extra arena if the free list is empty.
67SVs in the free list have their SvTYPE field set to all ones.
68
69Similarly, there are macros new_XIV()/del_XIV(), new_XNV()/del_XNV() etc
70that allocate and return individual body types. Normally these are mapped
71to the arena-maniplulating functions new_xiv()/del_xiv() etc, but may be
72instead mapped directly to malloc()/free() if PURIFY is in effect. The
73new/del functions remove from, or add to, the appropriate PL_foo_root
74list, and call more_xiv() etc to add a new arena if the list is empty.
75
76It the time of very final cleanup, sv_free_arenas() is called from
77perl_destruct() to physically free all the arenas allocated since the
78start of the interpreter. Note that this also clears PL_he_arenaroot,
79which is otherwise dealt with in hv.c.
80
81Manipulation of any of the PL_*root pointers is protected by enclosing
82LOCK_SV_MUTEX; ... UNLOCK_SV_MUTEX calls which should Do the Right Thing
83if threads are enabled.
84
85The function visit() scans the SV arenas list, and calls a specified
86function for each SV it finds which is still live - ie which has an SvTYPE
87other than all 1's, and a non-zero SvREFCNT. visit() is used by the
88following functions (specified as [function that calls visit()] / [function
89called by visit() for each SV]):
90
91 sv_report_used() / do_report_used()
92 dump all remaining SVs (debugging aid)
93
94 sv_clean_objs() / do_clean_objs(),do_clean_named_objs()
95 Attempt to free all objects pointed to by RVs,
96 and, unless DISABLE_DESTRUCTOR_KLUDGE is defined,
97 try to do the same for all objects indirectly
98 referenced by typeglobs too. Called once from
99 perl_destruct(), prior to calling sv_clean_all()
100 below.
101
102 sv_clean_all() / do_clean_all()
103 SvREFCNT_dec(sv) each remaining SV, possibly
104 triggering an sv_free(). It also sets the
105 SVf_BREAK flag on the SV to indicate that the
106 refcnt has been artificially lowered, and thus
107 stopping sv_free() from giving spurious warnings
108 about SVs which unexpectedly have a refcnt
109 of zero. called repeatedly from perl_destruct()
110 until there are no SVs left.
111
112=head2 Summary
113
114Private API to rest of sv.c
115
116 new_SV(), del_SV(),
117
118 new_XIV(), del_XIV(),
119 new_XNV(), del_XNV(),
120 etc
121
122Public API:
123
124 sv_report_used(), sv_clean_objs(), sv_clean_all(), sv_free_arenas()
125
126
127=cut
128
129============================================================================ */
130
131
51371543 132
4561caa4
CS
133/*
134 * "A time to plant, and a time to uproot what was planted..."
135 */
136
053fc874
GS
137#define plant_SV(p) \
138 STMT_START { \
139 SvANY(p) = (void *)PL_sv_root; \
140 SvFLAGS(p) = SVTYPEMASK; \
141 PL_sv_root = (p); \
142 --PL_sv_count; \
143 } STMT_END
a0d0e21e 144
fba3b22e 145/* sv_mutex must be held while calling uproot_SV() */
053fc874
GS
146#define uproot_SV(p) \
147 STMT_START { \
148 (p) = PL_sv_root; \
149 PL_sv_root = (SV*)SvANY(p); \
150 ++PL_sv_count; \
151 } STMT_END
152
645c22ef
DM
153
154/* new_SV(): return a new, empty SV head */
155
053fc874
GS
156#define new_SV(p) \
157 STMT_START { \
158 LOCK_SV_MUTEX; \
159 if (PL_sv_root) \
160 uproot_SV(p); \
161 else \
162 (p) = more_sv(); \
163 UNLOCK_SV_MUTEX; \
164 SvANY(p) = 0; \
165 SvREFCNT(p) = 1; \
166 SvFLAGS(p) = 0; \
167 } STMT_END
463ee0b2 168
645c22ef
DM
169
170/* del_SV(): return an empty SV head to the free list */
171
a0d0e21e 172#ifdef DEBUGGING
4561caa4 173
053fc874
GS
174#define del_SV(p) \
175 STMT_START { \
176 LOCK_SV_MUTEX; \
aea4f609 177 if (DEBUG_D_TEST) \
053fc874
GS
178 del_sv(p); \
179 else \
180 plant_SV(p); \
181 UNLOCK_SV_MUTEX; \
182 } STMT_END
a0d0e21e 183
76e3520e 184STATIC void
cea2e8a9 185S_del_sv(pTHX_ SV *p)
463ee0b2 186{
aea4f609 187 if (DEBUG_D_TEST) {
4633a7c4 188 SV* sva;
a0d0e21e
LW
189 SV* sv;
190 SV* svend;
191 int ok = 0;
3280af22 192 for (sva = PL_sv_arenaroot; sva; sva = (SV *) SvANY(sva)) {
4633a7c4
LW
193 sv = sva + 1;
194 svend = &sva[SvREFCNT(sva)];
a0d0e21e
LW
195 if (p >= sv && p < svend)
196 ok = 1;
197 }
198 if (!ok) {
0453d815
PM
199 if (ckWARN_d(WARN_INTERNAL))
200 Perl_warner(aTHX_ WARN_INTERNAL,
1d7c1841
GS
201 "Attempt to free non-arena SV: 0x%"UVxf,
202 PTR2UV(p));
a0d0e21e
LW
203 return;
204 }
205 }
4561caa4 206 plant_SV(p);
463ee0b2 207}
a0d0e21e 208
4561caa4
CS
209#else /* ! DEBUGGING */
210
211#define del_SV(p) plant_SV(p)
212
213#endif /* DEBUGGING */
463ee0b2 214
645c22ef
DM
215
216/*
217=for apidoc sv_add_arena
218
219Given a chunk of memory, link it to the head of the list of arenas,
220and split it into a list of free SVs.
221
222=cut
223*/
224
4633a7c4 225void
864dbfa3 226Perl_sv_add_arena(pTHX_ char *ptr, U32 size, U32 flags)
463ee0b2 227{
4633a7c4 228 SV* sva = (SV*)ptr;
463ee0b2
LW
229 register SV* sv;
230 register SV* svend;
14dd3ad8 231 Zero(ptr, size, char);
4633a7c4
LW
232
233 /* The first SV in an arena isn't an SV. */
3280af22 234 SvANY(sva) = (void *) PL_sv_arenaroot; /* ptr to next arena */
4633a7c4
LW
235 SvREFCNT(sva) = size / sizeof(SV); /* number of SV slots */
236 SvFLAGS(sva) = flags; /* FAKE if not to be freed */
237
3280af22
NIS
238 PL_sv_arenaroot = sva;
239 PL_sv_root = sva + 1;
4633a7c4
LW
240
241 svend = &sva[SvREFCNT(sva) - 1];
242 sv = sva + 1;
463ee0b2 243 while (sv < svend) {
a0d0e21e 244 SvANY(sv) = (void *)(SV*)(sv + 1);
8990e307 245 SvFLAGS(sv) = SVTYPEMASK;
463ee0b2
LW
246 sv++;
247 }
248 SvANY(sv) = 0;
4633a7c4
LW
249 SvFLAGS(sv) = SVTYPEMASK;
250}
251
645c22ef
DM
252/* make some more SVs by adding another arena */
253
fba3b22e 254/* sv_mutex must be held while calling more_sv() */
76e3520e 255STATIC SV*
cea2e8a9 256S_more_sv(pTHX)
4633a7c4 257{
4561caa4
CS
258 register SV* sv;
259
3280af22
NIS
260 if (PL_nice_chunk) {
261 sv_add_arena(PL_nice_chunk, PL_nice_chunk_size, 0);
262 PL_nice_chunk = Nullch;
30ad99e7 263 PL_nice_chunk_size = 0;
c07a80fd 264 }
1edc1566 265 else {
266 char *chunk; /* must use New here to match call to */
267 New(704,chunk,1008,char); /* Safefree() in sv_free_arenas() */
268 sv_add_arena(chunk, 1008, 0);
269 }
4561caa4
CS
270 uproot_SV(sv);
271 return sv;
463ee0b2
LW
272}
273
645c22ef
DM
274/* visit(): call the named function for each non-free in SV the arenas. */
275
5226ed68 276STATIC I32
cea2e8a9 277S_visit(pTHX_ SVFUNC_t f)
8990e307 278{
4633a7c4 279 SV* sva;
8990e307
LW
280 SV* sv;
281 register SV* svend;
5226ed68 282 I32 visited = 0;
8990e307 283
3280af22 284 for (sva = PL_sv_arenaroot; sva; sva = (SV*)SvANY(sva)) {
4633a7c4 285 svend = &sva[SvREFCNT(sva)];
4561caa4 286 for (sv = sva + 1; sv < svend; ++sv) {
f25c30a3 287 if (SvTYPE(sv) != SVTYPEMASK && SvREFCNT(sv)) {
51371543 288 (FCALL)(aTHXo_ sv);
5226ed68
JH
289 ++visited;
290 }
8990e307
LW
291 }
292 }
5226ed68 293 return visited;
8990e307
LW
294}
295
645c22ef
DM
296/* called by sv_report_used() for each live SV */
297
298static void
299do_report_used(pTHXo_ SV *sv)
300{
301 if (SvTYPE(sv) != SVTYPEMASK) {
302 PerlIO_printf(Perl_debug_log, "****\n");
303 sv_dump(sv);
304 }
305}
306
307/*
308=for apidoc sv_report_used
309
310Dump the contents of all SVs not yet freed. (Debugging aid).
311
312=cut
313*/
314
8990e307 315void
864dbfa3 316Perl_sv_report_used(pTHX)
4561caa4 317{
0b94c7bb 318 visit(do_report_used);
4561caa4
CS
319}
320
645c22ef
DM
321/* called by sv_clean_objs() for each live SV */
322
323static void
324do_clean_objs(pTHXo_ SV *sv)
325{
326 SV* rv;
327
328 if (SvROK(sv) && SvOBJECT(rv = SvRV(sv))) {
329 DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning object ref:\n "), sv_dump(sv)));
330 if (SvWEAKREF(sv)) {
331 sv_del_backref(sv);
332 SvWEAKREF_off(sv);
333 SvRV(sv) = 0;
334 } else {
335 SvROK_off(sv);
336 SvRV(sv) = 0;
337 SvREFCNT_dec(rv);
338 }
339 }
340
341 /* XXX Might want to check arrays, etc. */
342}
343
344/* called by sv_clean_objs() for each live SV */
345
346#ifndef DISABLE_DESTRUCTOR_KLUDGE
347static void
348do_clean_named_objs(pTHXo_ SV *sv)
349{
350 if (SvTYPE(sv) == SVt_PVGV && GvGP(sv)) {
351 if ( SvOBJECT(GvSV(sv)) ||
352 (GvAV(sv) && SvOBJECT(GvAV(sv))) ||
353 (GvHV(sv) && SvOBJECT(GvHV(sv))) ||
354 (GvIO(sv) && SvOBJECT(GvIO(sv))) ||
355 (GvCV(sv) && SvOBJECT(GvCV(sv))) )
356 {
357 DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning named glob object:\n "), sv_dump(sv)));
358 SvREFCNT_dec(sv);
359 }
360 }
361}
362#endif
363
364/*
365=for apidoc sv_clean_objs
366
367Attempt to destroy all objects not yet freed
368
369=cut
370*/
371
4561caa4 372void
864dbfa3 373Perl_sv_clean_objs(pTHX)
4561caa4 374{
3280af22 375 PL_in_clean_objs = TRUE;
0b94c7bb 376 visit(do_clean_objs);
4561caa4 377#ifndef DISABLE_DESTRUCTOR_KLUDGE
2d0f3c12 378 /* some barnacles may yet remain, clinging to typeglobs */
0b94c7bb 379 visit(do_clean_named_objs);
4561caa4 380#endif
3280af22 381 PL_in_clean_objs = FALSE;
4561caa4
CS
382}
383
645c22ef
DM
384/* called by sv_clean_all() for each live SV */
385
386static void
387do_clean_all(pTHXo_ SV *sv)
388{
389 DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning loops: SV at 0x%"UVxf"\n", PTR2UV(sv)) ));
390 SvFLAGS(sv) |= SVf_BREAK;
391 SvREFCNT_dec(sv);
392}
393
394/*
395=for apidoc sv_clean_all
396
397Decrement the refcnt of each remaining SV, possibly triggering a
398cleanup. This function may have to be called multiple times to free
399SVs which are in complex self-referential heirarchies.
400
401=cut
402*/
403
5226ed68 404I32
864dbfa3 405Perl_sv_clean_all(pTHX)
8990e307 406{
5226ed68 407 I32 cleaned;
3280af22 408 PL_in_clean_all = TRUE;
5226ed68 409 cleaned = visit(do_clean_all);
3280af22 410 PL_in_clean_all = FALSE;
5226ed68 411 return cleaned;
8990e307 412}
463ee0b2 413
645c22ef
DM
414/*
415=for apidoc sv_free_arenas
416
417Deallocate the memory used by all arenas. Note that all the individual SV
418heads and bodies within the arenas must already have been freed.
419
420=cut
421*/
422
4633a7c4 423void
864dbfa3 424Perl_sv_free_arenas(pTHX)
4633a7c4
LW
425{
426 SV* sva;
427 SV* svanext;
612f20c3 428 XPV *arena, *arenanext;
4633a7c4
LW
429
430 /* Free arenas here, but be careful about fake ones. (We assume
431 contiguity of the fake ones with the corresponding real ones.) */
432
3280af22 433 for (sva = PL_sv_arenaroot; sva; sva = svanext) {
4633a7c4
LW
434 svanext = (SV*) SvANY(sva);
435 while (svanext && SvFAKE(svanext))
436 svanext = (SV*) SvANY(svanext);
437
438 if (!SvFAKE(sva))
1edc1566 439 Safefree((void *)sva);
4633a7c4 440 }
5f05dabc 441
612f20c3
GS
442 for (arena = PL_xiv_arenaroot; arena; arena = arenanext) {
443 arenanext = (XPV*)arena->xpv_pv;
444 Safefree(arena);
445 }
446 PL_xiv_arenaroot = 0;
447
448 for (arena = PL_xnv_arenaroot; arena; arena = arenanext) {
449 arenanext = (XPV*)arena->xpv_pv;
450 Safefree(arena);
451 }
452 PL_xnv_arenaroot = 0;
453
454 for (arena = PL_xrv_arenaroot; arena; arena = arenanext) {
455 arenanext = (XPV*)arena->xpv_pv;
456 Safefree(arena);
457 }
458 PL_xrv_arenaroot = 0;
459
460 for (arena = PL_xpv_arenaroot; arena; arena = arenanext) {
461 arenanext = (XPV*)arena->xpv_pv;
462 Safefree(arena);
463 }
464 PL_xpv_arenaroot = 0;
465
466 for (arena = (XPV*)PL_xpviv_arenaroot; arena; arena = arenanext) {
467 arenanext = (XPV*)arena->xpv_pv;
468 Safefree(arena);
469 }
470 PL_xpviv_arenaroot = 0;
471
472 for (arena = (XPV*)PL_xpvnv_arenaroot; arena; arena = arenanext) {
473 arenanext = (XPV*)arena->xpv_pv;
474 Safefree(arena);
475 }
476 PL_xpvnv_arenaroot = 0;
477
478 for (arena = (XPV*)PL_xpvcv_arenaroot; arena; arena = arenanext) {
479 arenanext = (XPV*)arena->xpv_pv;
480 Safefree(arena);
481 }
482 PL_xpvcv_arenaroot = 0;
483
484 for (arena = (XPV*)PL_xpvav_arenaroot; arena; arena = arenanext) {
485 arenanext = (XPV*)arena->xpv_pv;
486 Safefree(arena);
487 }
488 PL_xpvav_arenaroot = 0;
489
490 for (arena = (XPV*)PL_xpvhv_arenaroot; arena; arena = arenanext) {
491 arenanext = (XPV*)arena->xpv_pv;
492 Safefree(arena);
493 }
494 PL_xpvhv_arenaroot = 0;
495
496 for (arena = (XPV*)PL_xpvmg_arenaroot; arena; arena = arenanext) {
497 arenanext = (XPV*)arena->xpv_pv;
498 Safefree(arena);
499 }
500 PL_xpvmg_arenaroot = 0;
501
502 for (arena = (XPV*)PL_xpvlv_arenaroot; arena; arena = arenanext) {
503 arenanext = (XPV*)arena->xpv_pv;
504 Safefree(arena);
505 }
506 PL_xpvlv_arenaroot = 0;
507
508 for (arena = (XPV*)PL_xpvbm_arenaroot; arena; arena = arenanext) {
509 arenanext = (XPV*)arena->xpv_pv;
510 Safefree(arena);
511 }
512 PL_xpvbm_arenaroot = 0;
513
514 for (arena = (XPV*)PL_he_arenaroot; arena; arena = arenanext) {
515 arenanext = (XPV*)arena->xpv_pv;
516 Safefree(arena);
517 }
518 PL_he_arenaroot = 0;
519
3280af22
NIS
520 if (PL_nice_chunk)
521 Safefree(PL_nice_chunk);
522 PL_nice_chunk = Nullch;
523 PL_nice_chunk_size = 0;
524 PL_sv_arenaroot = 0;
525 PL_sv_root = 0;
4633a7c4
LW
526}
527
645c22ef
DM
528/*
529=for apidoc report_uninit
530
531Print appropriate "Use of uninitialized variable" warning
532
533=cut
534*/
535
1d7c1841
GS
536void
537Perl_report_uninit(pTHX)
538{
539 if (PL_op)
540 Perl_warner(aTHX_ WARN_UNINITIALIZED, PL_warn_uninit,
541 " in ", PL_op_desc[PL_op->op_type]);
542 else
543 Perl_warner(aTHX_ WARN_UNINITIALIZED, PL_warn_uninit, "", "");
544}
545
645c22ef
DM
546/* grab a new IV body from the free list, allocating more if necessary */
547
76e3520e 548STATIC XPVIV*
cea2e8a9 549S_new_xiv(pTHX)
463ee0b2 550{
ea7c11a3 551 IV* xiv;
cbe51380
GS
552 LOCK_SV_MUTEX;
553 if (!PL_xiv_root)
554 more_xiv();
555 xiv = PL_xiv_root;
556 /*
557 * See comment in more_xiv() -- RAM.
558 */
559 PL_xiv_root = *(IV**)xiv;
560 UNLOCK_SV_MUTEX;
561 return (XPVIV*)((char*)xiv - STRUCT_OFFSET(XPVIV, xiv_iv));
463ee0b2
LW
562}
563
645c22ef
DM
564/* return an IV body to the free list */
565
76e3520e 566STATIC void
cea2e8a9 567S_del_xiv(pTHX_ XPVIV *p)
463ee0b2 568{
23e6a22f 569 IV* xiv = (IV*)((char*)(p) + STRUCT_OFFSET(XPVIV, xiv_iv));
cbe51380 570 LOCK_SV_MUTEX;
3280af22
NIS
571 *(IV**)xiv = PL_xiv_root;
572 PL_xiv_root = xiv;
cbe51380 573 UNLOCK_SV_MUTEX;
463ee0b2
LW
574}
575
645c22ef
DM
576/* allocate another arena's worth of IV bodies */
577
cbe51380 578STATIC void
cea2e8a9 579S_more_xiv(pTHX)
463ee0b2 580{
ea7c11a3
SM
581 register IV* xiv;
582 register IV* xivend;
8c52afec
IZ
583 XPV* ptr;
584 New(705, ptr, 1008/sizeof(XPV), XPV);
645c22ef 585 ptr->xpv_pv = (char*)PL_xiv_arenaroot; /* linked list of xiv arenas */
3280af22 586 PL_xiv_arenaroot = ptr; /* to keep Purify happy */
a0d0e21e 587
ea7c11a3
SM
588 xiv = (IV*) ptr;
589 xivend = &xiv[1008 / sizeof(IV) - 1];
645c22ef 590 xiv += (sizeof(XPV) - 1) / sizeof(IV) + 1; /* fudge by size of XPV */
3280af22 591 PL_xiv_root = xiv;
463ee0b2 592 while (xiv < xivend) {
ea7c11a3 593 *(IV**)xiv = (IV *)(xiv + 1);
463ee0b2
LW
594 xiv++;
595 }
ea7c11a3 596 *(IV**)xiv = 0;
463ee0b2
LW
597}
598
645c22ef
DM
599/* grab a new NV body from the free list, allocating more if necessary */
600
76e3520e 601STATIC XPVNV*
cea2e8a9 602S_new_xnv(pTHX)
463ee0b2 603{
65202027 604 NV* xnv;
cbe51380
GS
605 LOCK_SV_MUTEX;
606 if (!PL_xnv_root)
607 more_xnv();
608 xnv = PL_xnv_root;
65202027 609 PL_xnv_root = *(NV**)xnv;
cbe51380
GS
610 UNLOCK_SV_MUTEX;
611 return (XPVNV*)((char*)xnv - STRUCT_OFFSET(XPVNV, xnv_nv));
463ee0b2
LW
612}
613
645c22ef
DM
614/* return an NV body to the free list */
615
76e3520e 616STATIC void
cea2e8a9 617S_del_xnv(pTHX_ XPVNV *p)
463ee0b2 618{
65202027 619 NV* xnv = (NV*)((char*)(p) + STRUCT_OFFSET(XPVNV, xnv_nv));
cbe51380 620 LOCK_SV_MUTEX;
65202027 621 *(NV**)xnv = PL_xnv_root;
3280af22 622 PL_xnv_root = xnv;
cbe51380 623 UNLOCK_SV_MUTEX;
463ee0b2
LW
624}
625
645c22ef
DM
626/* allocate another arena's worth of NV bodies */
627
cbe51380 628STATIC void
cea2e8a9 629S_more_xnv(pTHX)
463ee0b2 630{
65202027
DS
631 register NV* xnv;
632 register NV* xnvend;
612f20c3
GS
633 XPV *ptr;
634 New(711, ptr, 1008/sizeof(XPV), XPV);
635 ptr->xpv_pv = (char*)PL_xnv_arenaroot;
636 PL_xnv_arenaroot = ptr;
637
638 xnv = (NV*) ptr;
65202027
DS
639 xnvend = &xnv[1008 / sizeof(NV) - 1];
640 xnv += (sizeof(XPVIV) - 1) / sizeof(NV) + 1; /* fudge by sizeof XPVIV */
3280af22 641 PL_xnv_root = xnv;
463ee0b2 642 while (xnv < xnvend) {
65202027 643 *(NV**)xnv = (NV*)(xnv + 1);
463ee0b2
LW
644 xnv++;
645 }
65202027 646 *(NV**)xnv = 0;
463ee0b2
LW
647}
648
645c22ef
DM
649/* grab a new struct xrv from the free list, allocating more if necessary */
650
76e3520e 651STATIC XRV*
cea2e8a9 652S_new_xrv(pTHX)
ed6116ce
LW
653{
654 XRV* xrv;
cbe51380
GS
655 LOCK_SV_MUTEX;
656 if (!PL_xrv_root)
657 more_xrv();
658 xrv = PL_xrv_root;
659 PL_xrv_root = (XRV*)xrv->xrv_rv;
660 UNLOCK_SV_MUTEX;
661 return xrv;
ed6116ce
LW
662}
663
645c22ef
DM
664/* return a struct xrv to the free list */
665
76e3520e 666STATIC void
cea2e8a9 667S_del_xrv(pTHX_ XRV *p)
ed6116ce 668{
cbe51380 669 LOCK_SV_MUTEX;
3280af22
NIS
670 p->xrv_rv = (SV*)PL_xrv_root;
671 PL_xrv_root = p;
cbe51380 672 UNLOCK_SV_MUTEX;
ed6116ce
LW
673}
674
645c22ef
DM
675/* allocate another arena's worth of struct xrv */
676
cbe51380 677STATIC void
cea2e8a9 678S_more_xrv(pTHX)
ed6116ce 679{
ed6116ce
LW
680 register XRV* xrv;
681 register XRV* xrvend;
612f20c3
GS
682 XPV *ptr;
683 New(712, ptr, 1008/sizeof(XPV), XPV);
684 ptr->xpv_pv = (char*)PL_xrv_arenaroot;
685 PL_xrv_arenaroot = ptr;
686
687 xrv = (XRV*) ptr;
ed6116ce 688 xrvend = &xrv[1008 / sizeof(XRV) - 1];
612f20c3
GS
689 xrv += (sizeof(XPV) - 1) / sizeof(XRV) + 1;
690 PL_xrv_root = xrv;
ed6116ce
LW
691 while (xrv < xrvend) {
692 xrv->xrv_rv = (SV*)(xrv + 1);
693 xrv++;
694 }
695 xrv->xrv_rv = 0;
ed6116ce
LW
696}
697
645c22ef
DM
698/* grab a new struct xpv from the free list, allocating more if necessary */
699
76e3520e 700STATIC XPV*
cea2e8a9 701S_new_xpv(pTHX)
463ee0b2
LW
702{
703 XPV* xpv;
cbe51380
GS
704 LOCK_SV_MUTEX;
705 if (!PL_xpv_root)
706 more_xpv();
707 xpv = PL_xpv_root;
708 PL_xpv_root = (XPV*)xpv->xpv_pv;
709 UNLOCK_SV_MUTEX;
710 return xpv;
463ee0b2
LW
711}
712
645c22ef
DM
713/* return a struct xpv to the free list */
714
76e3520e 715STATIC void
cea2e8a9 716S_del_xpv(pTHX_ XPV *p)
463ee0b2 717{
cbe51380 718 LOCK_SV_MUTEX;
3280af22
NIS
719 p->xpv_pv = (char*)PL_xpv_root;
720 PL_xpv_root = p;
cbe51380 721 UNLOCK_SV_MUTEX;
463ee0b2
LW
722}
723
645c22ef
DM
724/* allocate another arena's worth of struct xpv */
725
cbe51380 726STATIC void
cea2e8a9 727S_more_xpv(pTHX)
463ee0b2 728{
463ee0b2
LW
729 register XPV* xpv;
730 register XPV* xpvend;
612f20c3
GS
731 New(713, xpv, 1008/sizeof(XPV), XPV);
732 xpv->xpv_pv = (char*)PL_xpv_arenaroot;
733 PL_xpv_arenaroot = xpv;
734
463ee0b2 735 xpvend = &xpv[1008 / sizeof(XPV) - 1];
612f20c3 736 PL_xpv_root = ++xpv;
463ee0b2
LW
737 while (xpv < xpvend) {
738 xpv->xpv_pv = (char*)(xpv + 1);
739 xpv++;
740 }
741 xpv->xpv_pv = 0;
463ee0b2
LW
742}
743
645c22ef
DM
744/* grab a new struct xpviv from the free list, allocating more if necessary */
745
932e9ff9
VB
746STATIC XPVIV*
747S_new_xpviv(pTHX)
748{
749 XPVIV* xpviv;
750 LOCK_SV_MUTEX;
751 if (!PL_xpviv_root)
752 more_xpviv();
753 xpviv = PL_xpviv_root;
754 PL_xpviv_root = (XPVIV*)xpviv->xpv_pv;
755 UNLOCK_SV_MUTEX;
756 return xpviv;
757}
758
645c22ef
DM
759/* return a struct xpviv to the free list */
760
932e9ff9
VB
761STATIC void
762S_del_xpviv(pTHX_ XPVIV *p)
763{
764 LOCK_SV_MUTEX;
765 p->xpv_pv = (char*)PL_xpviv_root;
766 PL_xpviv_root = p;
767 UNLOCK_SV_MUTEX;
768}
769
645c22ef
DM
770/* allocate another arena's worth of struct xpviv */
771
932e9ff9
VB
772STATIC void
773S_more_xpviv(pTHX)
774{
775 register XPVIV* xpviv;
776 register XPVIV* xpvivend;
612f20c3
GS
777 New(714, xpviv, 1008/sizeof(XPVIV), XPVIV);
778 xpviv->xpv_pv = (char*)PL_xpviv_arenaroot;
779 PL_xpviv_arenaroot = xpviv;
780
932e9ff9 781 xpvivend = &xpviv[1008 / sizeof(XPVIV) - 1];
612f20c3 782 PL_xpviv_root = ++xpviv;
932e9ff9
VB
783 while (xpviv < xpvivend) {
784 xpviv->xpv_pv = (char*)(xpviv + 1);
785 xpviv++;
786 }
787 xpviv->xpv_pv = 0;
788}
789
645c22ef
DM
790/* grab a new struct xpvnv from the free list, allocating more if necessary */
791
932e9ff9
VB
792STATIC XPVNV*
793S_new_xpvnv(pTHX)
794{
795 XPVNV* xpvnv;
796 LOCK_SV_MUTEX;
797 if (!PL_xpvnv_root)
798 more_xpvnv();
799 xpvnv = PL_xpvnv_root;
800 PL_xpvnv_root = (XPVNV*)xpvnv->xpv_pv;
801 UNLOCK_SV_MUTEX;
802 return xpvnv;
803}
804
645c22ef
DM
805/* return a struct xpvnv to the free list */
806
932e9ff9
VB
807STATIC void
808S_del_xpvnv(pTHX_ XPVNV *p)
809{
810 LOCK_SV_MUTEX;
811 p->xpv_pv = (char*)PL_xpvnv_root;
812 PL_xpvnv_root = p;
813 UNLOCK_SV_MUTEX;
814}
815
645c22ef
DM
816/* allocate another arena's worth of struct xpvnv */
817
932e9ff9
VB
818STATIC void
819S_more_xpvnv(pTHX)
820{
821 register XPVNV* xpvnv;
822 register XPVNV* xpvnvend;
612f20c3
GS
823 New(715, xpvnv, 1008/sizeof(XPVNV), XPVNV);
824 xpvnv->xpv_pv = (char*)PL_xpvnv_arenaroot;
825 PL_xpvnv_arenaroot = xpvnv;
826
932e9ff9 827 xpvnvend = &xpvnv[1008 / sizeof(XPVNV) - 1];
612f20c3 828 PL_xpvnv_root = ++xpvnv;
932e9ff9
VB
829 while (xpvnv < xpvnvend) {
830 xpvnv->xpv_pv = (char*)(xpvnv + 1);
831 xpvnv++;
832 }
833 xpvnv->xpv_pv = 0;
834}
835
645c22ef
DM
836/* grab a new struct xpvcv from the free list, allocating more if necessary */
837
932e9ff9
VB
838STATIC XPVCV*
839S_new_xpvcv(pTHX)
840{
841 XPVCV* xpvcv;
842 LOCK_SV_MUTEX;
843 if (!PL_xpvcv_root)
844 more_xpvcv();
845 xpvcv = PL_xpvcv_root;
846 PL_xpvcv_root = (XPVCV*)xpvcv->xpv_pv;
847 UNLOCK_SV_MUTEX;
848 return xpvcv;
849}
850
645c22ef
DM
851/* return a struct xpvcv to the free list */
852
932e9ff9
VB
853STATIC void
854S_del_xpvcv(pTHX_ XPVCV *p)
855{
856 LOCK_SV_MUTEX;
857 p->xpv_pv = (char*)PL_xpvcv_root;
858 PL_xpvcv_root = p;
859 UNLOCK_SV_MUTEX;
860}
861
645c22ef
DM
862/* allocate another arena's worth of struct xpvcv */
863
932e9ff9
VB
864STATIC void
865S_more_xpvcv(pTHX)
866{
867 register XPVCV* xpvcv;
868 register XPVCV* xpvcvend;
612f20c3
GS
869 New(716, xpvcv, 1008/sizeof(XPVCV), XPVCV);
870 xpvcv->xpv_pv = (char*)PL_xpvcv_arenaroot;
871 PL_xpvcv_arenaroot = xpvcv;
872
932e9ff9 873 xpvcvend = &xpvcv[1008 / sizeof(XPVCV) - 1];
612f20c3 874 PL_xpvcv_root = ++xpvcv;
932e9ff9
VB
875 while (xpvcv < xpvcvend) {
876 xpvcv->xpv_pv = (char*)(xpvcv + 1);
877 xpvcv++;
878 }
879 xpvcv->xpv_pv = 0;
880}
881
645c22ef
DM
882/* grab a new struct xpvav from the free list, allocating more if necessary */
883
932e9ff9
VB
884STATIC XPVAV*
885S_new_xpvav(pTHX)
886{
887 XPVAV* xpvav;
888 LOCK_SV_MUTEX;
889 if (!PL_xpvav_root)
890 more_xpvav();
891 xpvav = PL_xpvav_root;
892 PL_xpvav_root = (XPVAV*)xpvav->xav_array;
893 UNLOCK_SV_MUTEX;
894 return xpvav;
895}
896
645c22ef
DM
897/* return a struct xpvav to the free list */
898
932e9ff9
VB
899STATIC void
900S_del_xpvav(pTHX_ XPVAV *p)
901{
902 LOCK_SV_MUTEX;
903 p->xav_array = (char*)PL_xpvav_root;
904 PL_xpvav_root = p;
905 UNLOCK_SV_MUTEX;
906}
907
645c22ef
DM
908/* allocate another arena's worth of struct xpvav */
909
932e9ff9
VB
910STATIC void
911S_more_xpvav(pTHX)
912{
913 register XPVAV* xpvav;
914 register XPVAV* xpvavend;
612f20c3
GS
915 New(717, xpvav, 1008/sizeof(XPVAV), XPVAV);
916 xpvav->xav_array = (char*)PL_xpvav_arenaroot;
917 PL_xpvav_arenaroot = xpvav;
918
932e9ff9 919 xpvavend = &xpvav[1008 / sizeof(XPVAV) - 1];
612f20c3 920 PL_xpvav_root = ++xpvav;
932e9ff9
VB
921 while (xpvav < xpvavend) {
922 xpvav->xav_array = (char*)(xpvav + 1);
923 xpvav++;
924 }
925 xpvav->xav_array = 0;
926}
927
645c22ef
DM
928/* grab a new struct xpvhv from the free list, allocating more if necessary */
929
932e9ff9
VB
930STATIC XPVHV*
931S_new_xpvhv(pTHX)
932{
933 XPVHV* xpvhv;
934 LOCK_SV_MUTEX;
935 if (!PL_xpvhv_root)
936 more_xpvhv();
937 xpvhv = PL_xpvhv_root;
938 PL_xpvhv_root = (XPVHV*)xpvhv->xhv_array;
939 UNLOCK_SV_MUTEX;
940 return xpvhv;
941}
942
645c22ef
DM
943/* return a struct xpvhv to the free list */
944
932e9ff9
VB
945STATIC void
946S_del_xpvhv(pTHX_ XPVHV *p)
947{
948 LOCK_SV_MUTEX;
949 p->xhv_array = (char*)PL_xpvhv_root;
950 PL_xpvhv_root = p;
951 UNLOCK_SV_MUTEX;
952}
953
645c22ef
DM
954/* allocate another arena's worth of struct xpvhv */
955
932e9ff9
VB
956STATIC void
957S_more_xpvhv(pTHX)
958{
959 register XPVHV* xpvhv;
960 register XPVHV* xpvhvend;
612f20c3
GS
961 New(718, xpvhv, 1008/sizeof(XPVHV), XPVHV);
962 xpvhv->xhv_array = (char*)PL_xpvhv_arenaroot;
963 PL_xpvhv_arenaroot = xpvhv;
964
932e9ff9 965 xpvhvend = &xpvhv[1008 / sizeof(XPVHV) - 1];
612f20c3 966 PL_xpvhv_root = ++xpvhv;
932e9ff9
VB
967 while (xpvhv < xpvhvend) {
968 xpvhv->xhv_array = (char*)(xpvhv + 1);
969 xpvhv++;
970 }
971 xpvhv->xhv_array = 0;
972}
973
645c22ef
DM
974/* grab a new struct xpvmg from the free list, allocating more if necessary */
975
932e9ff9
VB
976STATIC XPVMG*
977S_new_xpvmg(pTHX)
978{
979 XPVMG* xpvmg;
980 LOCK_SV_MUTEX;
981 if (!PL_xpvmg_root)
982 more_xpvmg();
983 xpvmg = PL_xpvmg_root;
984 PL_xpvmg_root = (XPVMG*)xpvmg->xpv_pv;
985 UNLOCK_SV_MUTEX;
986 return xpvmg;
987}
988
645c22ef
DM
989/* return a struct xpvmg to the free list */
990
932e9ff9
VB
991STATIC void
992S_del_xpvmg(pTHX_ XPVMG *p)
993{
994 LOCK_SV_MUTEX;
995 p->xpv_pv = (char*)PL_xpvmg_root;
996 PL_xpvmg_root = p;
997 UNLOCK_SV_MUTEX;
998}
999
645c22ef
DM
1000/* allocate another arena's worth of struct xpvmg */
1001
932e9ff9
VB
1002STATIC void
1003S_more_xpvmg(pTHX)
1004{
1005 register XPVMG* xpvmg;
1006 register XPVMG* xpvmgend;
612f20c3
GS
1007 New(719, xpvmg, 1008/sizeof(XPVMG), XPVMG);
1008 xpvmg->xpv_pv = (char*)PL_xpvmg_arenaroot;
1009 PL_xpvmg_arenaroot = xpvmg;
1010
932e9ff9 1011 xpvmgend = &xpvmg[1008 / sizeof(XPVMG) - 1];
612f20c3 1012 PL_xpvmg_root = ++xpvmg;
932e9ff9
VB
1013 while (xpvmg < xpvmgend) {
1014 xpvmg->xpv_pv = (char*)(xpvmg + 1);
1015 xpvmg++;
1016 }
1017 xpvmg->xpv_pv = 0;
1018}
1019
645c22ef
DM
1020/* grab a new struct xpvlv from the free list, allocating more if necessary */
1021
932e9ff9
VB
1022STATIC XPVLV*
1023S_new_xpvlv(pTHX)
1024{
1025 XPVLV* xpvlv;
1026 LOCK_SV_MUTEX;
1027 if (!PL_xpvlv_root)
1028 more_xpvlv();
1029 xpvlv = PL_xpvlv_root;
1030 PL_xpvlv_root = (XPVLV*)xpvlv->xpv_pv;
1031 UNLOCK_SV_MUTEX;
1032 return xpvlv;
1033}
1034
645c22ef
DM
1035/* return a struct xpvlv to the free list */
1036
932e9ff9
VB
1037STATIC void
1038S_del_xpvlv(pTHX_ XPVLV *p)
1039{
1040 LOCK_SV_MUTEX;
1041 p->xpv_pv = (char*)PL_xpvlv_root;
1042 PL_xpvlv_root = p;
1043 UNLOCK_SV_MUTEX;
1044}
1045
645c22ef
DM
1046/* allocate another arena's worth of struct xpvlv */
1047
932e9ff9
VB
1048STATIC void
1049S_more_xpvlv(pTHX)
1050{
1051 register XPVLV* xpvlv;
1052 register XPVLV* xpvlvend;
612f20c3
GS
1053 New(720, xpvlv, 1008/sizeof(XPVLV), XPVLV);
1054 xpvlv->xpv_pv = (char*)PL_xpvlv_arenaroot;
1055 PL_xpvlv_arenaroot = xpvlv;
1056
932e9ff9 1057 xpvlvend = &xpvlv[1008 / sizeof(XPVLV) - 1];
612f20c3 1058 PL_xpvlv_root = ++xpvlv;
932e9ff9
VB
1059 while (xpvlv < xpvlvend) {
1060 xpvlv->xpv_pv = (char*)(xpvlv + 1);
1061 xpvlv++;
1062 }
1063 xpvlv->xpv_pv = 0;
1064}
1065
645c22ef
DM
1066/* grab a new struct xpvbm from the free list, allocating more if necessary */
1067
932e9ff9
VB
1068STATIC XPVBM*
1069S_new_xpvbm(pTHX)
1070{
1071 XPVBM* xpvbm;
1072 LOCK_SV_MUTEX;
1073 if (!PL_xpvbm_root)
1074 more_xpvbm();
1075 xpvbm = PL_xpvbm_root;
1076 PL_xpvbm_root = (XPVBM*)xpvbm->xpv_pv;
1077 UNLOCK_SV_MUTEX;
1078 return xpvbm;
1079}
1080
645c22ef
DM
1081/* return a struct xpvbm to the free list */
1082
932e9ff9
VB
1083STATIC void
1084S_del_xpvbm(pTHX_ XPVBM *p)
1085{
1086 LOCK_SV_MUTEX;
1087 p->xpv_pv = (char*)PL_xpvbm_root;
1088 PL_xpvbm_root = p;
1089 UNLOCK_SV_MUTEX;
1090}
1091
645c22ef
DM
1092/* allocate another arena's worth of struct xpvbm */
1093
932e9ff9
VB
1094STATIC void
1095S_more_xpvbm(pTHX)
1096{
1097 register XPVBM* xpvbm;
1098 register XPVBM* xpvbmend;
612f20c3
GS
1099 New(721, xpvbm, 1008/sizeof(XPVBM), XPVBM);
1100 xpvbm->xpv_pv = (char*)PL_xpvbm_arenaroot;
1101 PL_xpvbm_arenaroot = xpvbm;
1102
932e9ff9 1103 xpvbmend = &xpvbm[1008 / sizeof(XPVBM) - 1];
612f20c3 1104 PL_xpvbm_root = ++xpvbm;
932e9ff9
VB
1105 while (xpvbm < xpvbmend) {
1106 xpvbm->xpv_pv = (char*)(xpvbm + 1);
1107 xpvbm++;
1108 }
1109 xpvbm->xpv_pv = 0;
1110}
1111
d33b2eba
GS
1112#ifdef LEAKTEST
1113# define my_safemalloc(s) (void*)safexmalloc(717,s)
1114# define my_safefree(p) safexfree((char*)p)
1115#else
1116# define my_safemalloc(s) (void*)safemalloc(s)
1117# define my_safefree(p) safefree((char*)p)
1118#endif
463ee0b2 1119
d33b2eba 1120#ifdef PURIFY
463ee0b2 1121
d33b2eba
GS
1122#define new_XIV() my_safemalloc(sizeof(XPVIV))
1123#define del_XIV(p) my_safefree(p)
ed6116ce 1124
d33b2eba
GS
1125#define new_XNV() my_safemalloc(sizeof(XPVNV))
1126#define del_XNV(p) my_safefree(p)
463ee0b2 1127
d33b2eba
GS
1128#define new_XRV() my_safemalloc(sizeof(XRV))
1129#define del_XRV(p) my_safefree(p)
8c52afec 1130
d33b2eba
GS
1131#define new_XPV() my_safemalloc(sizeof(XPV))
1132#define del_XPV(p) my_safefree(p)
9b94d1dd 1133
d33b2eba
GS
1134#define new_XPVIV() my_safemalloc(sizeof(XPVIV))
1135#define del_XPVIV(p) my_safefree(p)
932e9ff9 1136
d33b2eba
GS
1137#define new_XPVNV() my_safemalloc(sizeof(XPVNV))
1138#define del_XPVNV(p) my_safefree(p)
932e9ff9 1139
d33b2eba
GS
1140#define new_XPVCV() my_safemalloc(sizeof(XPVCV))
1141#define del_XPVCV(p) my_safefree(p)
932e9ff9 1142
d33b2eba
GS
1143#define new_XPVAV() my_safemalloc(sizeof(XPVAV))
1144#define del_XPVAV(p) my_safefree(p)
1145
1146#define new_XPVHV() my_safemalloc(sizeof(XPVHV))
1147#define del_XPVHV(p) my_safefree(p)
1c846c1f 1148
d33b2eba
GS
1149#define new_XPVMG() my_safemalloc(sizeof(XPVMG))
1150#define del_XPVMG(p) my_safefree(p)
1151
1152#define new_XPVLV() my_safemalloc(sizeof(XPVLV))
1153#define del_XPVLV(p) my_safefree(p)
1154
1155#define new_XPVBM() my_safemalloc(sizeof(XPVBM))
1156#define del_XPVBM(p) my_safefree(p)
1157
1158#else /* !PURIFY */
1159
1160#define new_XIV() (void*)new_xiv()
1161#define del_XIV(p) del_xiv((XPVIV*) p)
1162
1163#define new_XNV() (void*)new_xnv()
1164#define del_XNV(p) del_xnv((XPVNV*) p)
9b94d1dd 1165
d33b2eba
GS
1166#define new_XRV() (void*)new_xrv()
1167#define del_XRV(p) del_xrv((XRV*) p)
9b94d1dd 1168
d33b2eba
GS
1169#define new_XPV() (void*)new_xpv()
1170#define del_XPV(p) del_xpv((XPV *)p)
1171
1172#define new_XPVIV() (void*)new_xpviv()
1173#define del_XPVIV(p) del_xpviv((XPVIV *)p)
1174
1175#define new_XPVNV() (void*)new_xpvnv()
1176#define del_XPVNV(p) del_xpvnv((XPVNV *)p)
1177
1178#define new_XPVCV() (void*)new_xpvcv()
1179#define del_XPVCV(p) del_xpvcv((XPVCV *)p)
1180
1181#define new_XPVAV() (void*)new_xpvav()
1182#define del_XPVAV(p) del_xpvav((XPVAV *)p)
1183
1184#define new_XPVHV() (void*)new_xpvhv()
1185#define del_XPVHV(p) del_xpvhv((XPVHV *)p)
1c846c1f 1186
d33b2eba
GS
1187#define new_XPVMG() (void*)new_xpvmg()
1188#define del_XPVMG(p) del_xpvmg((XPVMG *)p)
1189
1190#define new_XPVLV() (void*)new_xpvlv()
1191#define del_XPVLV(p) del_xpvlv((XPVLV *)p)
1192
1193#define new_XPVBM() (void*)new_xpvbm()
1194#define del_XPVBM(p) del_xpvbm((XPVBM *)p)
1195
1196#endif /* PURIFY */
9b94d1dd 1197
d33b2eba
GS
1198#define new_XPVGV() my_safemalloc(sizeof(XPVGV))
1199#define del_XPVGV(p) my_safefree(p)
1c846c1f 1200
d33b2eba
GS
1201#define new_XPVFM() my_safemalloc(sizeof(XPVFM))
1202#define del_XPVFM(p) my_safefree(p)
1c846c1f 1203
d33b2eba
GS
1204#define new_XPVIO() my_safemalloc(sizeof(XPVIO))
1205#define del_XPVIO(p) my_safefree(p)
8990e307 1206
954c1994
GS
1207/*
1208=for apidoc sv_upgrade
1209
645c22ef
DM
1210Upgrade an SV to a more complex form. Gnenerally adds a new body type to the
1211SV, then copies across as much information as possible from the old body.
1212You genrally want to use the C<SvUPGRADE> macro wrapper. See also C<svtype>.
954c1994
GS
1213
1214=cut
1215*/
1216
79072805 1217bool
864dbfa3 1218Perl_sv_upgrade(pTHX_ register SV *sv, U32 mt)
79072805
LW
1219{
1220 char* pv;
1221 U32 cur;
1222 U32 len;
a0d0e21e 1223 IV iv;
65202027 1224 NV nv;
79072805
LW
1225 MAGIC* magic;
1226 HV* stash;
1227
f130fd45
NIS
1228 if (mt != SVt_PV && SvREADONLY(sv) && SvFAKE(sv)) {
1229 sv_force_normal(sv);
1230 }
1231
79072805
LW
1232 if (SvTYPE(sv) == mt)
1233 return TRUE;
1234
a5f75d66
AD
1235 if (mt < SVt_PVIV)
1236 (void)SvOOK_off(sv);
1237
79072805
LW
1238 switch (SvTYPE(sv)) {
1239 case SVt_NULL:
1240 pv = 0;
1241 cur = 0;
1242 len = 0;
1243 iv = 0;
1244 nv = 0.0;
1245 magic = 0;
1246 stash = 0;
1247 break;
79072805
LW
1248 case SVt_IV:
1249 pv = 0;
1250 cur = 0;
1251 len = 0;
463ee0b2 1252 iv = SvIVX(sv);
65202027 1253 nv = (NV)SvIVX(sv);
79072805
LW
1254 del_XIV(SvANY(sv));
1255 magic = 0;
1256 stash = 0;
ed6116ce 1257 if (mt == SVt_NV)
463ee0b2 1258 mt = SVt_PVNV;
ed6116ce
LW
1259 else if (mt < SVt_PVIV)
1260 mt = SVt_PVIV;
79072805
LW
1261 break;
1262 case SVt_NV:
1263 pv = 0;
1264 cur = 0;
1265 len = 0;
463ee0b2 1266 nv = SvNVX(sv);
1bd302c3 1267 iv = I_V(nv);
79072805
LW
1268 magic = 0;
1269 stash = 0;
1270 del_XNV(SvANY(sv));
1271 SvANY(sv) = 0;
ed6116ce 1272 if (mt < SVt_PVNV)
79072805
LW
1273 mt = SVt_PVNV;
1274 break;
ed6116ce
LW
1275 case SVt_RV:
1276 pv = (char*)SvRV(sv);
1277 cur = 0;
1278 len = 0;
56431972
RB
1279 iv = PTR2IV(pv);
1280 nv = PTR2NV(pv);
ed6116ce
LW
1281 del_XRV(SvANY(sv));
1282 magic = 0;
1283 stash = 0;
1284 break;
79072805 1285 case SVt_PV:
463ee0b2 1286 pv = SvPVX(sv);
79072805
LW
1287 cur = SvCUR(sv);
1288 len = SvLEN(sv);
1289 iv = 0;
1290 nv = 0.0;
1291 magic = 0;
1292 stash = 0;
1293 del_XPV(SvANY(sv));
748a9306
LW
1294 if (mt <= SVt_IV)
1295 mt = SVt_PVIV;
1296 else if (mt == SVt_NV)
1297 mt = SVt_PVNV;
79072805
LW
1298 break;
1299 case SVt_PVIV:
463ee0b2 1300 pv = SvPVX(sv);
79072805
LW
1301 cur = SvCUR(sv);
1302 len = SvLEN(sv);
463ee0b2 1303 iv = SvIVX(sv);
79072805
LW
1304 nv = 0.0;
1305 magic = 0;
1306 stash = 0;
1307 del_XPVIV(SvANY(sv));
1308 break;
1309 case SVt_PVNV:
463ee0b2 1310 pv = SvPVX(sv);
79072805
LW
1311 cur = SvCUR(sv);
1312 len = SvLEN(sv);
463ee0b2
LW
1313 iv = SvIVX(sv);
1314 nv = SvNVX(sv);
79072805
LW
1315 magic = 0;
1316 stash = 0;
1317 del_XPVNV(SvANY(sv));
1318 break;
1319 case SVt_PVMG:
463ee0b2 1320 pv = SvPVX(sv);
79072805
LW
1321 cur = SvCUR(sv);
1322 len = SvLEN(sv);
463ee0b2
LW
1323 iv = SvIVX(sv);
1324 nv = SvNVX(sv);
79072805
LW
1325 magic = SvMAGIC(sv);
1326 stash = SvSTASH(sv);
1327 del_XPVMG(SvANY(sv));
1328 break;
1329 default:
cea2e8a9 1330 Perl_croak(aTHX_ "Can't upgrade that kind of scalar");
79072805
LW
1331 }
1332
1333 switch (mt) {
1334 case SVt_NULL:
cea2e8a9 1335 Perl_croak(aTHX_ "Can't upgrade to undef");
79072805
LW
1336 case SVt_IV:
1337 SvANY(sv) = new_XIV();
463ee0b2 1338 SvIVX(sv) = iv;
79072805
LW
1339 break;
1340 case SVt_NV:
1341 SvANY(sv) = new_XNV();
463ee0b2 1342 SvNVX(sv) = nv;
79072805 1343 break;
ed6116ce
LW
1344 case SVt_RV:
1345 SvANY(sv) = new_XRV();
1346 SvRV(sv) = (SV*)pv;
ed6116ce 1347 break;
79072805
LW
1348 case SVt_PV:
1349 SvANY(sv) = new_XPV();
463ee0b2 1350 SvPVX(sv) = pv;
79072805
LW
1351 SvCUR(sv) = cur;
1352 SvLEN(sv) = len;
1353 break;
1354 case SVt_PVIV:
1355 SvANY(sv) = new_XPVIV();
463ee0b2 1356 SvPVX(sv) = pv;
79072805
LW
1357 SvCUR(sv) = cur;
1358 SvLEN(sv) = len;
463ee0b2 1359 SvIVX(sv) = iv;
79072805 1360 if (SvNIOK(sv))
a0d0e21e 1361 (void)SvIOK_on(sv);
79072805
LW
1362 SvNOK_off(sv);
1363 break;
1364 case SVt_PVNV:
1365 SvANY(sv) = new_XPVNV();
463ee0b2 1366 SvPVX(sv) = pv;
79072805
LW
1367 SvCUR(sv) = cur;
1368 SvLEN(sv) = len;
463ee0b2
LW
1369 SvIVX(sv) = iv;
1370 SvNVX(sv) = nv;
79072805
LW
1371 break;
1372 case SVt_PVMG:
1373 SvANY(sv) = new_XPVMG();
463ee0b2 1374 SvPVX(sv) = pv;
79072805
LW
1375 SvCUR(sv) = cur;
1376 SvLEN(sv) = len;
463ee0b2
LW
1377 SvIVX(sv) = iv;
1378 SvNVX(sv) = nv;
79072805
LW
1379 SvMAGIC(sv) = magic;
1380 SvSTASH(sv) = stash;
1381 break;
1382 case SVt_PVLV:
1383 SvANY(sv) = new_XPVLV();
463ee0b2 1384 SvPVX(sv) = pv;
79072805
LW
1385 SvCUR(sv) = cur;
1386 SvLEN(sv) = len;
463ee0b2
LW
1387 SvIVX(sv) = iv;
1388 SvNVX(sv) = nv;
79072805
LW
1389 SvMAGIC(sv) = magic;
1390 SvSTASH(sv) = stash;
1391 LvTARGOFF(sv) = 0;
1392 LvTARGLEN(sv) = 0;
1393 LvTARG(sv) = 0;
1394 LvTYPE(sv) = 0;
1395 break;
1396 case SVt_PVAV:
1397 SvANY(sv) = new_XPVAV();
463ee0b2
LW
1398 if (pv)
1399 Safefree(pv);
2304df62 1400 SvPVX(sv) = 0;
d1bf51dd 1401 AvMAX(sv) = -1;
93965878 1402 AvFILLp(sv) = -1;
463ee0b2
LW
1403 SvIVX(sv) = 0;
1404 SvNVX(sv) = 0.0;
1405 SvMAGIC(sv) = magic;
1406 SvSTASH(sv) = stash;
1407 AvALLOC(sv) = 0;
79072805
LW
1408 AvARYLEN(sv) = 0;
1409 AvFLAGS(sv) = 0;
1410 break;
1411 case SVt_PVHV:
1412 SvANY(sv) = new_XPVHV();
463ee0b2
LW
1413 if (pv)
1414 Safefree(pv);
1415 SvPVX(sv) = 0;
1416 HvFILL(sv) = 0;
1417 HvMAX(sv) = 0;
1418 HvKEYS(sv) = 0;
1419 SvNVX(sv) = 0.0;
79072805
LW
1420 SvMAGIC(sv) = magic;
1421 SvSTASH(sv) = stash;
79072805
LW
1422 HvRITER(sv) = 0;
1423 HvEITER(sv) = 0;
1424 HvPMROOT(sv) = 0;
1425 HvNAME(sv) = 0;
79072805
LW
1426 break;
1427 case SVt_PVCV:
1428 SvANY(sv) = new_XPVCV();
748a9306 1429 Zero(SvANY(sv), 1, XPVCV);
463ee0b2 1430 SvPVX(sv) = pv;
79072805
LW
1431 SvCUR(sv) = cur;
1432 SvLEN(sv) = len;
463ee0b2
LW
1433 SvIVX(sv) = iv;
1434 SvNVX(sv) = nv;
79072805
LW
1435 SvMAGIC(sv) = magic;
1436 SvSTASH(sv) = stash;
79072805
LW
1437 break;
1438 case SVt_PVGV:
1439 SvANY(sv) = new_XPVGV();
463ee0b2 1440 SvPVX(sv) = pv;
79072805
LW
1441 SvCUR(sv) = cur;
1442 SvLEN(sv) = len;
463ee0b2
LW
1443 SvIVX(sv) = iv;
1444 SvNVX(sv) = nv;
79072805
LW
1445 SvMAGIC(sv) = magic;
1446 SvSTASH(sv) = stash;
93a17b20 1447 GvGP(sv) = 0;
79072805
LW
1448 GvNAME(sv) = 0;
1449 GvNAMELEN(sv) = 0;
1450 GvSTASH(sv) = 0;
a5f75d66 1451 GvFLAGS(sv) = 0;
79072805
LW
1452 break;
1453 case SVt_PVBM:
1454 SvANY(sv) = new_XPVBM();
463ee0b2 1455 SvPVX(sv) = pv;
79072805
LW
1456 SvCUR(sv) = cur;
1457 SvLEN(sv) = len;
463ee0b2
LW
1458 SvIVX(sv) = iv;
1459 SvNVX(sv) = nv;
79072805
LW
1460 SvMAGIC(sv) = magic;
1461 SvSTASH(sv) = stash;
1462 BmRARE(sv) = 0;
1463 BmUSEFUL(sv) = 0;
1464 BmPREVIOUS(sv) = 0;
1465 break;
1466 case SVt_PVFM:
1467 SvANY(sv) = new_XPVFM();
748a9306 1468 Zero(SvANY(sv), 1, XPVFM);
463ee0b2 1469 SvPVX(sv) = pv;
79072805
LW
1470 SvCUR(sv) = cur;
1471 SvLEN(sv) = len;
463ee0b2
LW
1472 SvIVX(sv) = iv;
1473 SvNVX(sv) = nv;
79072805
LW
1474 SvMAGIC(sv) = magic;
1475 SvSTASH(sv) = stash;
79072805 1476 break;
8990e307
LW
1477 case SVt_PVIO:
1478 SvANY(sv) = new_XPVIO();
748a9306 1479 Zero(SvANY(sv), 1, XPVIO);
8990e307
LW
1480 SvPVX(sv) = pv;
1481 SvCUR(sv) = cur;
1482 SvLEN(sv) = len;
1483 SvIVX(sv) = iv;
1484 SvNVX(sv) = nv;
1485 SvMAGIC(sv) = magic;
1486 SvSTASH(sv) = stash;
85e6fe83 1487 IoPAGE_LEN(sv) = 60;
8990e307
LW
1488 break;
1489 }
1490 SvFLAGS(sv) &= ~SVTYPEMASK;
1491 SvFLAGS(sv) |= mt;
79072805
LW
1492 return TRUE;
1493}
1494
645c22ef
DM
1495/*
1496=for apidoc sv_backoff
1497
1498Remove any string offset. You should normally use the C<SvOOK_off> macro
1499wrapper instead.
1500
1501=cut
1502*/
1503
79072805 1504int
864dbfa3 1505Perl_sv_backoff(pTHX_ register SV *sv)
79072805
LW
1506{
1507 assert(SvOOK(sv));
463ee0b2
LW
1508 if (SvIVX(sv)) {
1509 char *s = SvPVX(sv);
1510 SvLEN(sv) += SvIVX(sv);
1511 SvPVX(sv) -= SvIVX(sv);
79072805 1512 SvIV_set(sv, 0);
463ee0b2 1513 Move(s, SvPVX(sv), SvCUR(sv)+1, char);
79072805
LW
1514 }
1515 SvFLAGS(sv) &= ~SVf_OOK;
a0d0e21e 1516 return 0;
79072805
LW
1517}
1518
954c1994
GS
1519/*
1520=for apidoc sv_grow
1521
645c22ef
DM
1522Expands the character buffer in the SV. If necessary, uses C<sv_unref> and
1523upgrades the SV to C<SVt_PV>. Returns a pointer to the character buffer.
1524Use the C<SvGROW> wrapper instead.
954c1994
GS
1525
1526=cut
1527*/
1528
79072805 1529char *
864dbfa3 1530Perl_sv_grow(pTHX_ register SV *sv, register STRLEN newlen)
79072805
LW
1531{
1532 register char *s;
1533
55497cff 1534#ifdef HAS_64K_LIMIT
79072805 1535 if (newlen >= 0x10000) {
1d7c1841
GS
1536 PerlIO_printf(Perl_debug_log,
1537 "Allocation too large: %"UVxf"\n", (UV)newlen);
79072805
LW
1538 my_exit(1);
1539 }
55497cff 1540#endif /* HAS_64K_LIMIT */
a0d0e21e
LW
1541 if (SvROK(sv))
1542 sv_unref(sv);
79072805
LW
1543 if (SvTYPE(sv) < SVt_PV) {
1544 sv_upgrade(sv, SVt_PV);
463ee0b2 1545 s = SvPVX(sv);
79072805
LW
1546 }
1547 else if (SvOOK(sv)) { /* pv is offset? */
1548 sv_backoff(sv);
463ee0b2 1549 s = SvPVX(sv);
79072805
LW
1550 if (newlen > SvLEN(sv))
1551 newlen += 10 * (newlen - SvCUR(sv)); /* avoid copy each time */
c6f8c383
GA
1552#ifdef HAS_64K_LIMIT
1553 if (newlen >= 0x10000)
1554 newlen = 0xFFFF;
1555#endif
79072805
LW
1556 }
1557 else
463ee0b2 1558 s = SvPVX(sv);
79072805 1559 if (newlen > SvLEN(sv)) { /* need more room? */
8d6dde3e 1560 if (SvLEN(sv) && s) {
f5a32c7f 1561#if defined(MYMALLOC) && !defined(LEAKTEST)
8d6dde3e
IZ
1562 STRLEN l = malloced_size((void*)SvPVX(sv));
1563 if (newlen <= l) {
1564 SvLEN_set(sv, l);
1565 return s;
1566 } else
c70c8a0a 1567#endif
79072805 1568 Renew(s,newlen,char);
8d6dde3e 1569 }
79072805
LW
1570 else
1571 New(703,s,newlen,char);
1572 SvPV_set(sv, s);
1573 SvLEN_set(sv, newlen);
1574 }
1575 return s;
1576}
1577
954c1994
GS
1578/*
1579=for apidoc sv_setiv
1580
645c22ef
DM
1581Copies an integer into the given SV, upgrading first if necessary.
1582Does not handle 'set' magic. See also C<sv_setiv_mg>.
954c1994
GS
1583
1584=cut
1585*/
1586
79072805 1587void
864dbfa3 1588Perl_sv_setiv(pTHX_ register SV *sv, IV i)
79072805 1589{
2213622d 1590 SV_CHECK_THINKFIRST(sv);
463ee0b2
LW
1591 switch (SvTYPE(sv)) {
1592 case SVt_NULL:
79072805 1593 sv_upgrade(sv, SVt_IV);
463ee0b2
LW
1594 break;
1595 case SVt_NV:
1596 sv_upgrade(sv, SVt_PVNV);
1597 break;
ed6116ce 1598 case SVt_RV:
463ee0b2 1599 case SVt_PV:
79072805 1600 sv_upgrade(sv, SVt_PVIV);
463ee0b2 1601 break;
a0d0e21e
LW
1602
1603 case SVt_PVGV:
a0d0e21e
LW
1604 case SVt_PVAV:
1605 case SVt_PVHV:
1606 case SVt_PVCV:
1607 case SVt_PVFM:
1608 case SVt_PVIO:
411caa50
JH
1609 Perl_croak(aTHX_ "Can't coerce %s to integer in %s", sv_reftype(sv,0),
1610 PL_op_desc[PL_op->op_type]);
463ee0b2 1611 }
a0d0e21e 1612 (void)SvIOK_only(sv); /* validate number */
a5f75d66 1613 SvIVX(sv) = i;
463ee0b2 1614 SvTAINT(sv);
79072805
LW
1615}
1616
954c1994
GS
1617/*
1618=for apidoc sv_setiv_mg
1619
1620Like C<sv_setiv>, but also handles 'set' magic.
1621
1622=cut
1623*/
1624
79072805 1625void
864dbfa3 1626Perl_sv_setiv_mg(pTHX_ register SV *sv, IV i)
ef50df4b
GS
1627{
1628 sv_setiv(sv,i);
1629 SvSETMAGIC(sv);
1630}
1631
954c1994
GS
1632/*
1633=for apidoc sv_setuv
1634
645c22ef
DM
1635Copies an unsigned integer into the given SV, upgrading first if necessary.
1636Does not handle 'set' magic. See also C<sv_setuv_mg>.
954c1994
GS
1637
1638=cut
1639*/
1640
ef50df4b 1641void
864dbfa3 1642Perl_sv_setuv(pTHX_ register SV *sv, UV u)
55497cff 1643{
55ada374
NC
1644 /* With these two if statements:
1645 u=1.49 s=0.52 cu=72.49 cs=10.64 scripts=270 tests=20865
d460ef45 1646
55ada374
NC
1647 without
1648 u=1.35 s=0.47 cu=73.45 cs=11.43 scripts=270 tests=20865
d460ef45 1649
55ada374
NC
1650 If you wish to remove them, please benchmark to see what the effect is
1651 */
28e5dec8
JH
1652 if (u <= (UV)IV_MAX) {
1653 sv_setiv(sv, (IV)u);
1654 return;
1655 }
25da4f38
IZ
1656 sv_setiv(sv, 0);
1657 SvIsUV_on(sv);
1658 SvUVX(sv) = u;
55497cff 1659}
1660
954c1994
GS
1661/*
1662=for apidoc sv_setuv_mg
1663
1664Like C<sv_setuv>, but also handles 'set' magic.
1665
1666=cut
1667*/
1668
55497cff 1669void
864dbfa3 1670Perl_sv_setuv_mg(pTHX_ register SV *sv, UV u)
ef50df4b 1671{
55ada374
NC
1672 /* With these two if statements:
1673 u=1.49 s=0.52 cu=72.49 cs=10.64 scripts=270 tests=20865
d460ef45 1674
55ada374
NC
1675 without
1676 u=1.35 s=0.47 cu=73.45 cs=11.43 scripts=270 tests=20865
d460ef45 1677
55ada374
NC
1678 If you wish to remove them, please benchmark to see what the effect is
1679 */
28e5dec8
JH
1680 if (u <= (UV)IV_MAX) {
1681 sv_setiv(sv, (IV)u);
1682 } else {
1683 sv_setiv(sv, 0);
1684 SvIsUV_on(sv);
1685 sv_setuv(sv,u);
1686 }
ef50df4b
GS
1687 SvSETMAGIC(sv);
1688}
1689
954c1994
GS
1690/*
1691=for apidoc sv_setnv
1692
645c22ef
DM
1693Copies a double into the given SV, upgrading first if necessary.
1694Does not handle 'set' magic. See also C<sv_setnv_mg>.
954c1994
GS
1695
1696=cut
1697*/
1698
ef50df4b 1699void
65202027 1700Perl_sv_setnv(pTHX_ register SV *sv, NV num)
79072805 1701{
2213622d 1702 SV_CHECK_THINKFIRST(sv);
a0d0e21e
LW
1703 switch (SvTYPE(sv)) {
1704 case SVt_NULL:
1705 case SVt_IV:
79072805 1706 sv_upgrade(sv, SVt_NV);
a0d0e21e 1707 break;
a0d0e21e
LW
1708 case SVt_RV:
1709 case SVt_PV:
1710 case SVt_PVIV:
79072805 1711 sv_upgrade(sv, SVt_PVNV);
a0d0e21e 1712 break;
827b7e14 1713
a0d0e21e 1714 case SVt_PVGV:
a0d0e21e
LW
1715 case SVt_PVAV:
1716 case SVt_PVHV:
1717 case SVt_PVCV:
1718 case SVt_PVFM:
1719 case SVt_PVIO:
411caa50
JH
1720 Perl_croak(aTHX_ "Can't coerce %s to number in %s", sv_reftype(sv,0),
1721 PL_op_name[PL_op->op_type]);
79072805 1722 }
463ee0b2 1723 SvNVX(sv) = num;
a0d0e21e 1724 (void)SvNOK_only(sv); /* validate number */
463ee0b2 1725 SvTAINT(sv);
79072805
LW
1726}
1727
954c1994
GS
1728/*
1729=for apidoc sv_setnv_mg
1730
1731Like C<sv_setnv>, but also handles 'set' magic.
1732
1733=cut
1734*/
1735
ef50df4b 1736void
65202027 1737Perl_sv_setnv_mg(pTHX_ register SV *sv, NV num)
ef50df4b
GS
1738{
1739 sv_setnv(sv,num);
1740 SvSETMAGIC(sv);
1741}
1742
645c22ef
DM
1743/* Print an "isn't numeric" warning, using a cleaned-up,
1744 * printable version of the offending string
1745 */
1746
76e3520e 1747STATIC void
cea2e8a9 1748S_not_a_number(pTHX_ SV *sv)
a0d0e21e
LW
1749{
1750 char tmpbuf[64];
1751 char *d = tmpbuf;
dc28f22b
GA
1752 char *limit = tmpbuf + sizeof(tmpbuf) - 8;
1753 /* each *s can expand to 4 chars + "...\0",
1754 i.e. need room for 8 chars */
a0d0e21e 1755
59bb5845
RB
1756 char *s, *end;
1757 for (s = SvPVX(sv), end = s + SvCUR(sv); s < end && d < limit; s++) {
bbce6d69 1758 int ch = *s & 0xFF;
1759 if (ch & 128 && !isPRINT_LC(ch)) {
a0d0e21e
LW
1760 *d++ = 'M';
1761 *d++ = '-';
1762 ch &= 127;
1763 }
bbce6d69 1764 if (ch == '\n') {
1765 *d++ = '\\';
1766 *d++ = 'n';
1767 }
1768 else if (ch == '\r') {
1769 *d++ = '\\';
1770 *d++ = 'r';
1771 }
1772 else if (ch == '\f') {
1773 *d++ = '\\';
1774 *d++ = 'f';
1775 }
1776 else if (ch == '\\') {
1777 *d++ = '\\';
1778 *d++ = '\\';
1779 }
59bb5845
RB
1780 else if (ch == '\0') {
1781 *d++ = '\\';
1782 *d++ = '0';
1783 }
bbce6d69 1784 else if (isPRINT_LC(ch))
a0d0e21e
LW
1785 *d++ = ch;
1786 else {
1787 *d++ = '^';
bbce6d69 1788 *d++ = toCTRL(ch);
a0d0e21e
LW
1789 }
1790 }
e71c6625 1791 if (s < end) {
a0d0e21e
LW
1792 *d++ = '.';
1793 *d++ = '.';
1794 *d++ = '.';
1795 }
1796 *d = '\0';
1797
533c011a 1798 if (PL_op)
42d38218
MS
1799 Perl_warner(aTHX_ WARN_NUMERIC,
1800 "Argument \"%s\" isn't numeric in %s", tmpbuf,
1801 PL_op_desc[PL_op->op_type]);
a0d0e21e 1802 else
42d38218
MS
1803 Perl_warner(aTHX_ WARN_NUMERIC,
1804 "Argument \"%s\" isn't numeric", tmpbuf);
a0d0e21e
LW
1805}
1806
c2988b20
NC
1807/*
1808=for apidoc looks_like_number
1809
645c22ef
DM
1810Test if the content of an SV looks like a number (or is a number).
1811C<Inf> and C<Infinity> are treated as numbers (so will not issue a
1812non-numeric warning), even if your atof() doesn't grok them.
c2988b20
NC
1813
1814=cut
1815*/
1816
1817I32
1818Perl_looks_like_number(pTHX_ SV *sv)
1819{
1820 register char *sbegin;
1821 STRLEN len;
1822
1823 if (SvPOK(sv)) {
1824 sbegin = SvPVX(sv);
1825 len = SvCUR(sv);
1826 }
1827 else if (SvPOKp(sv))
1828 sbegin = SvPV(sv, len);
1829 else
1830 return 1; /* Historic. Wrong? */
1831 return grok_number(sbegin, len, NULL);
1832}
25da4f38
IZ
1833
1834/* Actually, ISO C leaves conversion of UV to IV undefined, but
1835 until proven guilty, assume that things are not that bad... */
1836
645c22ef
DM
1837/*
1838 NV_PRESERVES_UV:
1839
1840 As 64 bit platforms often have an NV that doesn't preserve all bits of
28e5dec8
JH
1841 an IV (an assumption perl has been based on to date) it becomes necessary
1842 to remove the assumption that the NV always carries enough precision to
1843 recreate the IV whenever needed, and that the NV is the canonical form.
1844 Instead, IV/UV and NV need to be given equal rights. So as to not lose
645c22ef 1845 precision as a side effect of conversion (which would lead to insanity
28e5dec8
JH
1846 and the dragon(s) in t/op/numconvert.t getting very angry) the intent is
1847 1) to distinguish between IV/UV/NV slots that have cached a valid
1848 conversion where precision was lost and IV/UV/NV slots that have a
1849 valid conversion which has lost no precision
645c22ef 1850 2) to ensure that if a numeric conversion to one form is requested that
28e5dec8
JH
1851 would lose precision, the precise conversion (or differently
1852 imprecise conversion) is also performed and cached, to prevent
1853 requests for different numeric formats on the same SV causing
1854 lossy conversion chains. (lossless conversion chains are perfectly
1855 acceptable (still))
1856
1857
1858 flags are used:
1859 SvIOKp is true if the IV slot contains a valid value
1860 SvIOK is true only if the IV value is accurate (UV if SvIOK_UV true)
1861 SvNOKp is true if the NV slot contains a valid value
1862 SvNOK is true only if the NV value is accurate
1863
1864 so
645c22ef 1865 while converting from PV to NV, check to see if converting that NV to an
28e5dec8
JH
1866 IV(or UV) would lose accuracy over a direct conversion from PV to
1867 IV(or UV). If it would, cache both conversions, return NV, but mark
1868 SV as IOK NOKp (ie not NOK).
1869
645c22ef 1870 While converting from PV to IV, check to see if converting that IV to an
28e5dec8
JH
1871 NV would lose accuracy over a direct conversion from PV to NV. If it
1872 would, cache both conversions, flag similarly.
1873
1874 Before, the SV value "3.2" could become NV=3.2 IV=3 NOK, IOK quite
1875 correctly because if IV & NV were set NV *always* overruled.
645c22ef
DM
1876 Now, "3.2" will become NV=3.2 IV=3 NOK, IOKp, because the flag's meaning
1877 changes - now IV and NV together means that the two are interchangeable:
28e5dec8 1878 SvIVX == (IV) SvNVX && SvNVX == (NV) SvIVX;
d460ef45 1879
645c22ef
DM
1880 The benefit of this is that operations such as pp_add know that if
1881 SvIOK is true for both left and right operands, then integer addition
1882 can be used instead of floating point (for cases where the result won't
1883 overflow). Before, floating point was always used, which could lead to
28e5dec8
JH
1884 loss of precision compared with integer addition.
1885
1886 * making IV and NV equal status should make maths accurate on 64 bit
1887 platforms
1888 * may speed up maths somewhat if pp_add and friends start to use
645c22ef 1889 integers when possible instead of fp. (Hopefully the overhead in
28e5dec8
JH
1890 looking for SvIOK and checking for overflow will not outweigh the
1891 fp to integer speedup)
1892 * will slow down integer operations (callers of SvIV) on "inaccurate"
1893 values, as the change from SvIOK to SvIOKp will cause a call into
1894 sv_2iv each time rather than a macro access direct to the IV slot
1895 * should speed up number->string conversion on integers as IV is
645c22ef 1896 favoured when IV and NV are equally accurate
28e5dec8
JH
1897
1898 ####################################################################
645c22ef
DM
1899 You had better be using SvIOK_notUV if you want an IV for arithmetic:
1900 SvIOK is true if (IV or UV), so you might be getting (IV)SvUV.
1901 On the other hand, SvUOK is true iff UV.
28e5dec8
JH
1902 ####################################################################
1903
645c22ef 1904 Your mileage will vary depending your CPU's relative fp to integer
28e5dec8
JH
1905 performance ratio.
1906*/
1907
1908#ifndef NV_PRESERVES_UV
645c22ef
DM
1909# define IS_NUMBER_UNDERFLOW_IV 1
1910# define IS_NUMBER_UNDERFLOW_UV 2
1911# define IS_NUMBER_IV_AND_UV 2
1912# define IS_NUMBER_OVERFLOW_IV 4
1913# define IS_NUMBER_OVERFLOW_UV 5
1914
1915/* sv_2iuv_non_preserve(): private routine for use by sv_2iv() and sv_2uv() */
28e5dec8
JH
1916
1917/* For sv_2nv these three cases are "SvNOK and don't bother casting" */
1918STATIC int
645c22ef 1919S_sv_2iuv_non_preserve(pTHX_ register SV *sv, I32 numtype)
28e5dec8 1920{
159fae86 1921 DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_2iuv_non '%s', IV=0x%"UVxf" NV=%g inttype=%"UVXf"\n", SvPVX(sv), SvIVX(sv), SvNVX(sv), (UV)numtype));
28e5dec8
JH
1922 if (SvNVX(sv) < (NV)IV_MIN) {
1923 (void)SvIOKp_on(sv);
1924 (void)SvNOK_on(sv);
1925 SvIVX(sv) = IV_MIN;
1926 return IS_NUMBER_UNDERFLOW_IV;
1927 }
1928 if (SvNVX(sv) > (NV)UV_MAX) {
1929 (void)SvIOKp_on(sv);
1930 (void)SvNOK_on(sv);
1931 SvIsUV_on(sv);
1932 SvUVX(sv) = UV_MAX;
1933 return IS_NUMBER_OVERFLOW_UV;
1934 }
c2988b20
NC
1935 (void)SvIOKp_on(sv);
1936 (void)SvNOK_on(sv);
1937 /* Can't use strtol etc to convert this string. (See truth table in
1938 sv_2iv */
1939 if (SvNVX(sv) <= (UV)IV_MAX) {
1940 SvIVX(sv) = I_V(SvNVX(sv));
1941 if ((NV)(SvIVX(sv)) == SvNVX(sv)) {
1942 SvIOK_on(sv); /* Integer is precise. NOK, IOK */
1943 } else {
1944 /* Integer is imprecise. NOK, IOKp */
1945 }
1946 return SvNVX(sv) < 0 ? IS_NUMBER_UNDERFLOW_UV : IS_NUMBER_IV_AND_UV;
1947 }
1948 SvIsUV_on(sv);
1949 SvUVX(sv) = U_V(SvNVX(sv));
1950 if ((NV)(SvUVX(sv)) == SvNVX(sv)) {
1951 if (SvUVX(sv) == UV_MAX) {
1952 /* As we know that NVs don't preserve UVs, UV_MAX cannot
1953 possibly be preserved by NV. Hence, it must be overflow.
1954 NOK, IOKp */
1955 return IS_NUMBER_OVERFLOW_UV;
1956 }
1957 SvIOK_on(sv); /* Integer is precise. NOK, UOK */
1958 } else {
1959 /* Integer is imprecise. NOK, IOKp */
28e5dec8 1960 }
c2988b20 1961 return IS_NUMBER_OVERFLOW_IV;
28e5dec8 1962}
645c22ef
DM
1963#endif /* !NV_PRESERVES_UV*/
1964
1965/*
1966=for apidoc sv_2iv
1967
1968Return the integer value of an SV, doing any necessary string conversion,
1969magic etc. Normally used via the C<SvIV(sv)> and C<SvIVx(sv)> macros.
1970
1971=cut
1972*/
28e5dec8 1973
a0d0e21e 1974IV
864dbfa3 1975Perl_sv_2iv(pTHX_ register SV *sv)
79072805
LW
1976{
1977 if (!sv)
1978 return 0;
8990e307 1979 if (SvGMAGICAL(sv)) {
463ee0b2
LW
1980 mg_get(sv);
1981 if (SvIOKp(sv))
1982 return SvIVX(sv);
748a9306 1983 if (SvNOKp(sv)) {
25da4f38 1984 return I_V(SvNVX(sv));
748a9306 1985 }
36477c24 1986 if (SvPOKp(sv) && SvLEN(sv))
1987 return asIV(sv);
3fe9a6f1 1988 if (!SvROK(sv)) {
d008e5eb 1989 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
d008e5eb 1990 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
1d7c1841 1991 report_uninit();
c6ee37c5 1992 }
36477c24 1993 return 0;
3fe9a6f1 1994 }
463ee0b2 1995 }
ed6116ce 1996 if (SvTHINKFIRST(sv)) {
a0d0e21e 1997 if (SvROK(sv)) {
a0d0e21e 1998 SV* tmpstr;
1554e226 1999 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)) &&
1dc13c17 2000 (SvTYPE(tmpstr) != SVt_RV || (SvRV(tmpstr) != SvRV(sv))))
9e7bc3e8 2001 return SvIV(tmpstr);
56431972 2002 return PTR2IV(SvRV(sv));
a0d0e21e 2003 }
47deb5e7
NIS
2004 if (SvREADONLY(sv) && SvFAKE(sv)) {
2005 sv_force_normal(sv);
2006 }
0336b60e 2007 if (SvREADONLY(sv) && !SvOK(sv)) {
0336b60e 2008 if (ckWARN(WARN_UNINITIALIZED))
1d7c1841 2009 report_uninit();
ed6116ce
LW
2010 return 0;
2011 }
79072805 2012 }
25da4f38
IZ
2013 if (SvIOKp(sv)) {
2014 if (SvIsUV(sv)) {
2015 return (IV)(SvUVX(sv));
2016 }
2017 else {
2018 return SvIVX(sv);
2019 }
463ee0b2 2020 }
748a9306 2021 if (SvNOKp(sv)) {
28e5dec8
JH
2022 /* erm. not sure. *should* never get NOKp (without NOK) from sv_2nv
2023 * without also getting a cached IV/UV from it at the same time
2024 * (ie PV->NV conversion should detect loss of accuracy and cache
2025 * IV or UV at same time to avoid this. NWC */
25da4f38
IZ
2026
2027 if (SvTYPE(sv) == SVt_NV)
2028 sv_upgrade(sv, SVt_PVNV);
2029
28e5dec8
JH
2030 (void)SvIOKp_on(sv); /* Must do this first, to clear any SvOOK */
2031 /* < not <= as for NV doesn't preserve UV, ((NV)IV_MAX+1) will almost
2032 certainly cast into the IV range at IV_MAX, whereas the correct
2033 answer is the UV IV_MAX +1. Hence < ensures that dodgy boundary
2034 cases go to UV */
2035 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
748a9306 2036 SvIVX(sv) = I_V(SvNVX(sv));
28e5dec8
JH
2037 if (SvNVX(sv) == (NV) SvIVX(sv)
2038#ifndef NV_PRESERVES_UV
2039 && (((UV)1 << NV_PRESERVES_UV_BITS) >
2040 (UV)(SvIVX(sv) > 0 ? SvIVX(sv) : -SvIVX(sv)))
2041 /* Don't flag it as "accurately an integer" if the number
2042 came from a (by definition imprecise) NV operation, and
2043 we're outside the range of NV integer precision */
2044#endif
2045 ) {
2046 SvIOK_on(sv); /* Can this go wrong with rounding? NWC */
2047 DEBUG_c(PerlIO_printf(Perl_debug_log,
2048 "0x%"UVxf" iv(%g => %"IVdf") (precise)\n",
2049 PTR2UV(sv),
2050 SvNVX(sv),
2051 SvIVX(sv)));
2052
2053 } else {
2054 /* IV not precise. No need to convert from PV, as NV
2055 conversion would already have cached IV if it detected
2056 that PV->IV would be better than PV->NV->IV
2057 flags already correct - don't set public IOK. */
2058 DEBUG_c(PerlIO_printf(Perl_debug_log,
2059 "0x%"UVxf" iv(%g => %"IVdf") (imprecise)\n",
2060 PTR2UV(sv),
2061 SvNVX(sv),
2062 SvIVX(sv)));
2063 }
2064 /* Can the above go wrong if SvIVX == IV_MIN and SvNVX < IV_MIN,
2065 but the cast (NV)IV_MIN rounds to a the value less (more
2066 negative) than IV_MIN which happens to be equal to SvNVX ??
2067 Analogous to 0xFFFFFFFFFFFFFFFF rounding up to NV (2**64) and
2068 NV rounding back to 0xFFFFFFFFFFFFFFFF, so UVX == UV(NVX) and
2069 (NV)UVX == NVX are both true, but the values differ. :-(
2070 Hopefully for 2s complement IV_MIN is something like
2071 0x8000000000000000 which will be exact. NWC */
d460ef45 2072 }
25da4f38 2073 else {
ff68c719 2074 SvUVX(sv) = U_V(SvNVX(sv));
28e5dec8
JH
2075 if (
2076 (SvNVX(sv) == (NV) SvUVX(sv))
2077#ifndef NV_PRESERVES_UV
2078 /* Make sure it's not 0xFFFFFFFFFFFFFFFF */
2079 /*&& (SvUVX(sv) != UV_MAX) irrelevant with code below */
2080 && (((UV)1 << NV_PRESERVES_UV_BITS) > SvUVX(sv))
2081 /* Don't flag it as "accurately an integer" if the number
2082 came from a (by definition imprecise) NV operation, and
2083 we're outside the range of NV integer precision */
2084#endif
2085 )
2086 SvIOK_on(sv);
25da4f38
IZ
2087 SvIsUV_on(sv);
2088 ret_iv_max:
1c846c1f 2089 DEBUG_c(PerlIO_printf(Perl_debug_log,
57def98f 2090 "0x%"UVxf" 2iv(%"UVuf" => %"IVdf") (as unsigned)\n",
56431972 2091 PTR2UV(sv),
57def98f
JH
2092 SvUVX(sv),
2093 SvUVX(sv)));
25da4f38
IZ
2094 return (IV)SvUVX(sv);
2095 }
748a9306
LW
2096 }
2097 else if (SvPOKp(sv) && SvLEN(sv)) {
c2988b20
NC
2098 UV value;
2099 int numtype = grok_number(SvPVX(sv), SvCUR(sv), &value);
25da4f38
IZ
2100 /* We want to avoid a possible problem when we cache an IV which
2101 may be later translated to an NV, and the resulting NV is not
c2988b20
NC
2102 the same as the direct translation of the initial string
2103 (eg 123.456 can shortcut to the IV 123 with atol(), but we must
2104 be careful to ensure that the value with the .456 is around if the
2105 NV value is requested in the future).
1c846c1f 2106
25da4f38
IZ
2107 This means that if we cache such an IV, we need to cache the
2108 NV as well. Moreover, we trade speed for space, and do not
28e5dec8 2109 cache the NV if we are sure it's not needed.
25da4f38 2110 */
16b7a9a4 2111
c2988b20
NC
2112 /* SVt_PVNV is one higher than SVt_PVIV, hence this order */
2113 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2114 == IS_NUMBER_IN_UV) {
2115 /* It's defintately an integer, only upgrade to PVIV */
28e5dec8
JH
2116 if (SvTYPE(sv) < SVt_PVIV)
2117 sv_upgrade(sv, SVt_PVIV);
f7bbb42a 2118 (void)SvIOK_on(sv);
c2988b20
NC
2119 } else if (SvTYPE(sv) < SVt_PVNV)
2120 sv_upgrade(sv, SVt_PVNV);
28e5dec8 2121
c2988b20
NC
2122 /* If NV preserves UV then we only use the UV value if we know that
2123 we aren't going to call atof() below. If NVs don't preserve UVs
2124 then the value returned may have more precision than atof() will
2125 return, even though value isn't perfectly accurate. */
2126 if ((numtype & (IS_NUMBER_IN_UV
2127#ifdef NV_PRESERVES_UV
2128 | IS_NUMBER_NOT_INT
2129#endif
2130 )) == IS_NUMBER_IN_UV) {
2131 /* This won't turn off the public IOK flag if it was set above */
2132 (void)SvIOKp_on(sv);
2133
2134 if (!(numtype & IS_NUMBER_NEG)) {
2135 /* positive */;
2136 if (value <= (UV)IV_MAX) {
2137 SvIVX(sv) = (IV)value;
2138 } else {
2139 SvUVX(sv) = value;
2140 SvIsUV_on(sv);
2141 }
2142 } else {
2143 /* 2s complement assumption */
2144 if (value <= (UV)IV_MIN) {
2145 SvIVX(sv) = -(IV)value;
2146 } else {
2147 /* Too negative for an IV. This is a double upgrade, but
2148 I'm assuming it will be be rare. */
2149 if (SvTYPE(sv) < SVt_PVNV)
2150 sv_upgrade(sv, SVt_PVNV);
2151 SvNOK_on(sv);
2152 SvIOK_off(sv);
2153 SvIOKp_on(sv);
2154 SvNVX(sv) = -(NV)value;
2155 SvIVX(sv) = IV_MIN;
2156 }
2157 }
2158 }
2159 /* For !NV_PRESERVES_UV and IS_NUMBER_IN_UV and IS_NUMBER_NOT_INT we
2160 will be in the previous block to set the IV slot, and the next
2161 block to set the NV slot. So no else here. */
2162
2163 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2164 != IS_NUMBER_IN_UV) {
2165 /* It wasn't an (integer that doesn't overflow the UV). */
2166 SvNVX(sv) = Atof(SvPVX(sv));
28e5dec8 2167
c2988b20
NC
2168 if (! numtype && ckWARN(WARN_NUMERIC))
2169 not_a_number(sv);
28e5dec8 2170
65202027 2171#if defined(USE_LONG_DOUBLE)
c2988b20
NC
2172 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%" PERL_PRIgldbl ")\n",
2173 PTR2UV(sv), SvNVX(sv)));
65202027 2174#else
c2988b20
NC
2175 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%g)\n",
2176 PTR2UV(sv), SvNVX(sv)));
65202027 2177#endif
28e5dec8
JH
2178
2179
2180#ifdef NV_PRESERVES_UV
c2988b20
NC
2181 (void)SvIOKp_on(sv);
2182 (void)SvNOK_on(sv);
2183 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
2184 SvIVX(sv) = I_V(SvNVX(sv));
2185 if ((NV)(SvIVX(sv)) == SvNVX(sv)) {
2186 SvIOK_on(sv);
28e5dec8 2187 } else {
c2988b20
NC
2188 /* Integer is imprecise. NOK, IOKp */
2189 }
2190 /* UV will not work better than IV */
2191 } else {
2192 if (SvNVX(sv) > (NV)UV_MAX) {
2193 SvIsUV_on(sv);
2194 /* Integer is inaccurate. NOK, IOKp, is UV */
2195 SvUVX(sv) = UV_MAX;
2196 SvIsUV_on(sv);
2197 } else {
2198 SvUVX(sv) = U_V(SvNVX(sv));
2199 /* 0xFFFFFFFFFFFFFFFF not an issue in here */
2200 if ((NV)(SvUVX(sv)) == SvNVX(sv)) {
2201 SvIOK_on(sv);
28e5dec8
JH
2202 SvIsUV_on(sv);
2203 } else {
c2988b20
NC
2204 /* Integer is imprecise. NOK, IOKp, is UV */
2205 SvIsUV_on(sv);
28e5dec8 2206 }
28e5dec8 2207 }
c2988b20
NC
2208 goto ret_iv_max;
2209 }
28e5dec8 2210#else /* NV_PRESERVES_UV */
c2988b20
NC
2211 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2212 == (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) {
2213 /* The IV slot will have been set from value returned by
2214 grok_number above. The NV slot has just been set using
2215 Atof. */
560b0c46 2216 SvNOK_on(sv);
c2988b20
NC
2217 assert (SvIOKp(sv));
2218 } else {
2219 if (((UV)1 << NV_PRESERVES_UV_BITS) >
2220 U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) {
2221 /* Small enough to preserve all bits. */
2222 (void)SvIOKp_on(sv);
2223 SvNOK_on(sv);
2224 SvIVX(sv) = I_V(SvNVX(sv));
2225 if ((NV)(SvIVX(sv)) == SvNVX(sv))
2226 SvIOK_on(sv);
2227 /* Assumption: first non-preserved integer is < IV_MAX,
2228 this NV is in the preserved range, therefore: */
2229 if (!(U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))
2230 < (UV)IV_MAX)) {
2231 Perl_croak(aTHX_ "sv_2iv assumed (U_V(fabs(SvNVX(sv))) < (UV)IV_MAX) but SvNVX(sv)=%g U_V is 0x%"UVxf", IV_MAX is 0x%"UVxf"\n", SvNVX(sv), U_V(SvNVX(sv)), (UV)IV_MAX);
2232 }
2233 } else {
2234 /* IN_UV NOT_INT
2235 0 0 already failed to read UV.
2236 0 1 already failed to read UV.
2237 1 0 you won't get here in this case. IV/UV
2238 slot set, public IOK, Atof() unneeded.
2239 1 1 already read UV.
2240 so there's no point in sv_2iuv_non_preserve() attempting
2241 to use atol, strtol, strtoul etc. */
2242 if (sv_2iuv_non_preserve (sv, numtype)
2243 >= IS_NUMBER_OVERFLOW_IV)
2244 goto ret_iv_max;
2245 }
2246 }
28e5dec8 2247#endif /* NV_PRESERVES_UV */
25da4f38 2248 }
28e5dec8 2249 } else {
599cee73 2250 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP))
1d7c1841 2251 report_uninit();
25da4f38
IZ
2252 if (SvTYPE(sv) < SVt_IV)
2253 /* Typically the caller expects that sv_any is not NULL now. */
2254 sv_upgrade(sv, SVt_IV);
a0d0e21e 2255 return 0;
79072805 2256 }
1d7c1841
GS
2257 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%"IVdf")\n",
2258 PTR2UV(sv),SvIVX(sv)));
25da4f38 2259 return SvIsUV(sv) ? (IV)SvUVX(sv) : SvIVX(sv);
79072805
LW
2260}
2261
645c22ef
DM
2262/*
2263=for apidoc sv_2uv
2264
2265Return the unsigned integer value of an SV, doing any necessary string
2266conversion, magic etc. Normally used via the C<SvUV(sv)> and C<SvUVx(sv)>
2267macros.
2268
2269=cut
2270*/
2271
ff68c719 2272UV
864dbfa3 2273Perl_sv_2uv(pTHX_ register SV *sv)
ff68c719 2274{
2275 if (!sv)
2276 return 0;
2277 if (SvGMAGICAL(sv)) {
2278 mg_get(sv);
2279 if (SvIOKp(sv))
2280 return SvUVX(sv);
2281 if (SvNOKp(sv))
2282 return U_V(SvNVX(sv));
36477c24 2283 if (SvPOKp(sv) && SvLEN(sv))
2284 return asUV(sv);
3fe9a6f1 2285 if (!SvROK(sv)) {
d008e5eb 2286 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
d008e5eb 2287 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
1d7c1841 2288 report_uninit();
c6ee37c5 2289 }
36477c24 2290 return 0;
3fe9a6f1 2291 }
ff68c719 2292 }
2293 if (SvTHINKFIRST(sv)) {
2294 if (SvROK(sv)) {
ff68c719 2295 SV* tmpstr;
1554e226 2296 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)) &&
1dc13c17 2297 (SvTYPE(tmpstr) != SVt_RV || (SvRV(tmpstr) != SvRV(sv))))
9e7bc3e8 2298 return SvUV(tmpstr);
56431972 2299 return PTR2UV(SvRV(sv));
ff68c719 2300 }
8a818333
NIS
2301 if (SvREADONLY(sv) && SvFAKE(sv)) {
2302 sv_force_normal(sv);
2303 }
0336b60e 2304 if (SvREADONLY(sv) && !SvOK(sv)) {
0336b60e 2305 if (ckWARN(WARN_UNINITIALIZED))
1d7c1841 2306 report_uninit();
ff68c719 2307 return 0;
2308 }
2309 }
25da4f38
IZ
2310 if (SvIOKp(sv)) {
2311 if (SvIsUV(sv)) {
2312 return SvUVX(sv);
2313 }
2314 else {
2315 return (UV)SvIVX(sv);
2316 }
ff68c719 2317 }
2318 if (SvNOKp(sv)) {
28e5dec8
JH
2319 /* erm. not sure. *should* never get NOKp (without NOK) from sv_2nv
2320 * without also getting a cached IV/UV from it at the same time
2321 * (ie PV->NV conversion should detect loss of accuracy and cache
2322 * IV or UV at same time to avoid this. */
2323 /* IV-over-UV optimisation - choose to cache IV if possible */
2324
25da4f38
IZ
2325 if (SvTYPE(sv) == SVt_NV)
2326 sv_upgrade(sv, SVt_PVNV);
28e5dec8
JH
2327
2328 (void)SvIOKp_on(sv); /* Must do this first, to clear any SvOOK */
2329 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
f7bbb42a 2330 SvIVX(sv) = I_V(SvNVX(sv));
28e5dec8
JH
2331 if (SvNVX(sv) == (NV) SvIVX(sv)
2332#ifndef NV_PRESERVES_UV
2333 && (((UV)1 << NV_PRESERVES_UV_BITS) >
2334 (UV)(SvIVX(sv) > 0 ? SvIVX(sv) : -SvIVX(sv)))
2335 /* Don't flag it as "accurately an integer" if the number
2336 came from a (by definition imprecise) NV operation, and
2337 we're outside the range of NV integer precision */
2338#endif
2339 ) {
2340 SvIOK_on(sv); /* Can this go wrong with rounding? NWC */
2341 DEBUG_c(PerlIO_printf(Perl_debug_log,
2342 "0x%"UVxf" uv(%g => %"IVdf") (precise)\n",
2343 PTR2UV(sv),
2344 SvNVX(sv),
2345 SvIVX(sv)));
2346
2347 } else {
2348 /* IV not precise. No need to convert from PV, as NV
2349 conversion would already have cached IV if it detected
2350 that PV->IV would be better than PV->NV->IV
2351 flags already correct - don't set public IOK. */
2352 DEBUG_c(PerlIO_printf(Perl_debug_log,
2353 "0x%"UVxf" uv(%g => %"IVdf") (imprecise)\n",
2354 PTR2UV(sv),
2355 SvNVX(sv),
2356 SvIVX(sv)));
2357 }
2358 /* Can the above go wrong if SvIVX == IV_MIN and SvNVX < IV_MIN,
2359 but the cast (NV)IV_MIN rounds to a the value less (more
2360 negative) than IV_MIN which happens to be equal to SvNVX ??
2361 Analogous to 0xFFFFFFFFFFFFFFFF rounding up to NV (2**64) and
2362 NV rounding back to 0xFFFFFFFFFFFFFFFF, so UVX == UV(NVX) and
2363 (NV)UVX == NVX are both true, but the values differ. :-(
2364 Hopefully for 2s complement IV_MIN is something like
2365 0x8000000000000000 which will be exact. NWC */
d460ef45 2366 }
28e5dec8
JH
2367 else {
2368 SvUVX(sv) = U_V(SvNVX(sv));
2369 if (
2370 (SvNVX(sv) == (NV) SvUVX(sv))
2371#ifndef NV_PRESERVES_UV
2372 /* Make sure it's not 0xFFFFFFFFFFFFFFFF */
2373 /*&& (SvUVX(sv) != UV_MAX) irrelevant with code below */
2374 && (((UV)1 << NV_PRESERVES_UV_BITS) > SvUVX(sv))
2375 /* Don't flag it as "accurately an integer" if the number
2376 came from a (by definition imprecise) NV operation, and
2377 we're outside the range of NV integer precision */
2378#endif
2379 )
2380 SvIOK_on(sv);
2381 SvIsUV_on(sv);
1c846c1f 2382 DEBUG_c(PerlIO_printf(Perl_debug_log,
28e5dec8 2383 "0x%"UVxf" 2uv(%"UVuf" => %"IVdf") (as unsigned)\n",
57def98f 2384 PTR2UV(sv),
28e5dec8
JH
2385 SvUVX(sv),
2386 SvUVX(sv)));
25da4f38 2387 }
ff68c719 2388 }
2389 else if (SvPOKp(sv) && SvLEN(sv)) {
c2988b20
NC
2390 UV value;
2391 int numtype = grok_number(SvPVX(sv), SvCUR(sv), &value);
25da4f38
IZ
2392
2393 /* We want to avoid a possible problem when we cache a UV which
2394 may be later translated to an NV, and the resulting NV is not
2395 the translation of the initial data.
1c846c1f 2396
25da4f38
IZ
2397 This means that if we cache such a UV, we need to cache the
2398 NV as well. Moreover, we trade speed for space, and do not
2399 cache the NV if not needed.
2400 */
16b7a9a4 2401
c2988b20
NC
2402 /* SVt_PVNV is one higher than SVt_PVIV, hence this order */
2403 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2404 == IS_NUMBER_IN_UV) {
2405 /* It's defintately an integer, only upgrade to PVIV */
28e5dec8 2406 if (SvTYPE(sv) < SVt_PVIV)
f7bbb42a
JH
2407 sv_upgrade(sv, SVt_PVIV);
2408 (void)SvIOK_on(sv);
c2988b20
NC
2409 } else if (SvTYPE(sv) < SVt_PVNV)
2410 sv_upgrade(sv, SVt_PVNV);
d460ef45 2411
c2988b20
NC
2412 /* If NV preserves UV then we only use the UV value if we know that
2413 we aren't going to call atof() below. If NVs don't preserve UVs
2414 then the value returned may have more precision than atof() will
2415 return, even though it isn't accurate. */
2416 if ((numtype & (IS_NUMBER_IN_UV
2417#ifdef NV_PRESERVES_UV
2418 | IS_NUMBER_NOT_INT
2419#endif
2420 )) == IS_NUMBER_IN_UV) {
2421 /* This won't turn off the public IOK flag if it was set above */
2422 (void)SvIOKp_on(sv);
2423
2424 if (!(numtype & IS_NUMBER_NEG)) {
2425 /* positive */;
2426 if (value <= (UV)IV_MAX) {
2427 SvIVX(sv) = (IV)value;
28e5dec8
JH
2428 } else {
2429 /* it didn't overflow, and it was positive. */
c2988b20 2430 SvUVX(sv) = value;
28e5dec8
JH
2431 SvIsUV_on(sv);
2432 }
c2988b20
NC
2433 } else {
2434 /* 2s complement assumption */
2435 if (value <= (UV)IV_MIN) {
2436 SvIVX(sv) = -(IV)value;
2437 } else {
2438 /* Too negative for an IV. This is a double upgrade, but
2439 I'm assuming it will be be rare. */
2440 if (SvTYPE(sv) < SVt_PVNV)
2441 sv_upgrade(sv, SVt_PVNV);
2442 SvNOK_on(sv);
2443 SvIOK_off(sv);
2444 SvIOKp_on(sv);
2445 SvNVX(sv) = -(NV)value;
2446 SvIVX(sv) = IV_MIN;
2447 }
2448 }
2449 }
2450
2451 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2452 != IS_NUMBER_IN_UV) {
2453 /* It wasn't an integer, or it overflowed the UV. */
2454 SvNVX(sv) = Atof(SvPVX(sv));
28e5dec8 2455
c2988b20 2456 if (! numtype && ckWARN(WARN_NUMERIC))
28e5dec8
JH
2457 not_a_number(sv);
2458
2459#if defined(USE_LONG_DOUBLE)
c2988b20
NC
2460 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%" PERL_PRIgldbl ")\n",
2461 PTR2UV(sv), SvNVX(sv)));
28e5dec8 2462#else
c2988b20
NC
2463 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%g)\n",
2464 PTR2UV(sv), SvNVX(sv)));
28e5dec8
JH
2465#endif
2466
2467#ifdef NV_PRESERVES_UV
c2988b20
NC
2468 (void)SvIOKp_on(sv);
2469 (void)SvNOK_on(sv);
2470 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
2471 SvIVX(sv) = I_V(SvNVX(sv));
2472 if ((NV)(SvIVX(sv)) == SvNVX(sv)) {
2473 SvIOK_on(sv);
2474 } else {
2475 /* Integer is imprecise. NOK, IOKp */
2476 }
2477 /* UV will not work better than IV */
2478 } else {
2479 if (SvNVX(sv) > (NV)UV_MAX) {
2480 SvIsUV_on(sv);
2481 /* Integer is inaccurate. NOK, IOKp, is UV */
2482 SvUVX(sv) = UV_MAX;
2483 SvIsUV_on(sv);
2484 } else {
2485 SvUVX(sv) = U_V(SvNVX(sv));
2486 /* 0xFFFFFFFFFFFFFFFF not an issue in here, NVs
2487 NV preservse UV so can do correct comparison. */
2488 if ((NV)(SvUVX(sv)) == SvNVX(sv)) {
2489 SvIOK_on(sv);
2490 SvIsUV_on(sv);
2491 } else {
2492 /* Integer is imprecise. NOK, IOKp, is UV */
2493 SvIsUV_on(sv);
2494 }
2495 }
2496 }
28e5dec8 2497#else /* NV_PRESERVES_UV */
c2988b20
NC
2498 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2499 == (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) {
2500 /* The UV slot will have been set from value returned by
2501 grok_number above. The NV slot has just been set using
2502 Atof. */
560b0c46 2503 SvNOK_on(sv);
c2988b20
NC
2504 assert (SvIOKp(sv));
2505 } else {
2506 if (((UV)1 << NV_PRESERVES_UV_BITS) >
2507 U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) {
2508 /* Small enough to preserve all bits. */
2509 (void)SvIOKp_on(sv);
2510 SvNOK_on(sv);
2511 SvIVX(sv) = I_V(SvNVX(sv));
2512 if ((NV)(SvIVX(sv)) == SvNVX(sv))
2513 SvIOK_on(sv);
2514 /* Assumption: first non-preserved integer is < IV_MAX,
2515 this NV is in the preserved range, therefore: */
2516 if (!(U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))
2517 < (UV)IV_MAX)) {
2518 Perl_croak(aTHX_ "sv_2uv assumed (U_V(fabs(SvNVX(sv))) < (UV)IV_MAX) but SvNVX(sv)=%g U_V is 0x%"UVxf", IV_MAX is 0x%"UVxf"\n", SvNVX(sv), U_V(SvNVX(sv)), (UV)IV_MAX);
2519 }
2520 } else
2521 sv_2iuv_non_preserve (sv, numtype);
2522 }
28e5dec8 2523#endif /* NV_PRESERVES_UV */
f7bbb42a 2524 }
ff68c719 2525 }
2526 else {
d008e5eb 2527 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
d008e5eb 2528 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
1d7c1841 2529 report_uninit();
c6ee37c5 2530 }
25da4f38
IZ
2531 if (SvTYPE(sv) < SVt_IV)
2532 /* Typically the caller expects that sv_any is not NULL now. */
2533 sv_upgrade(sv, SVt_IV);
ff68c719 2534 return 0;
2535 }
25da4f38 2536
1d7c1841
GS
2537 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%"UVuf")\n",
2538 PTR2UV(sv),SvUVX(sv)));
25da4f38 2539 return SvIsUV(sv) ? SvUVX(sv) : (UV)SvIVX(sv);
ff68c719 2540}
2541
645c22ef
DM
2542/*
2543=for apidoc sv_2nv
2544
2545Return the num value of an SV, doing any necessary string or integer
2546conversion, magic etc. Normally used via the C<SvNV(sv)> and C<SvNVx(sv)>
2547macros.
2548
2549=cut
2550*/
2551
65202027 2552NV
864dbfa3 2553Perl_sv_2nv(pTHX_ register SV *sv)
79072805
LW
2554{
2555 if (!sv)
2556 return 0.0;
8990e307 2557 if (SvGMAGICAL(sv)) {
463ee0b2
LW
2558 mg_get(sv);
2559 if (SvNOKp(sv))
2560 return SvNVX(sv);
a0d0e21e 2561 if (SvPOKp(sv) && SvLEN(sv)) {
c2988b20
NC
2562 if (ckWARN(WARN_NUMERIC) && !SvIOKp(sv) &&
2563 !grok_number(SvPVX(sv), SvCUR(sv), NULL))
a0d0e21e 2564 not_a_number(sv);
097ee67d 2565 return Atof(SvPVX(sv));
a0d0e21e 2566 }
25da4f38 2567 if (SvIOKp(sv)) {
1c846c1f 2568 if (SvIsUV(sv))
65202027 2569 return (NV)SvUVX(sv);
25da4f38 2570 else
65202027 2571 return (NV)SvIVX(sv);
25da4f38 2572 }
16d20bd9 2573 if (!SvROK(sv)) {
d008e5eb 2574 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
d008e5eb 2575 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
1d7c1841 2576 report_uninit();
c6ee37c5 2577 }
16d20bd9
AD
2578 return 0;
2579 }
463ee0b2 2580 }
ed6116ce 2581 if (SvTHINKFIRST(sv)) {
a0d0e21e 2582 if (SvROK(sv)) {
a0d0e21e 2583 SV* tmpstr;
1554e226 2584 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)) &&
1dc13c17 2585 (SvTYPE(tmpstr) != SVt_RV || (SvRV(tmpstr) != SvRV(sv))))
9e7bc3e8 2586 return SvNV(tmpstr);
56431972 2587 return PTR2NV(SvRV(sv));
a0d0e21e 2588 }
8a818333
NIS
2589 if (SvREADONLY(sv) && SvFAKE(sv)) {
2590 sv_force_normal(sv);
2591 }
0336b60e 2592 if (SvREADONLY(sv) && !SvOK(sv)) {
599cee73 2593 if (ckWARN(WARN_UNINITIALIZED))
1d7c1841 2594 report_uninit();
ed6116ce
LW
2595 return 0.0;
2596 }
79072805
LW
2597 }
2598 if (SvTYPE(sv) < SVt_NV) {
463ee0b2
LW
2599 if (SvTYPE(sv) == SVt_IV)
2600 sv_upgrade(sv, SVt_PVNV);
2601 else
2602 sv_upgrade(sv, SVt_NV);
906f284f 2603#ifdef USE_LONG_DOUBLE
097ee67d 2604 DEBUG_c({
f93f4e46 2605 STORE_NUMERIC_LOCAL_SET_STANDARD();
1d7c1841
GS
2606 PerlIO_printf(Perl_debug_log,
2607 "0x%"UVxf" num(%" PERL_PRIgldbl ")\n",
2608 PTR2UV(sv), SvNVX(sv));
572bbb43
GS
2609 RESTORE_NUMERIC_LOCAL();
2610 });
65202027 2611#else
572bbb43 2612 DEBUG_c({
f93f4e46 2613 STORE_NUMERIC_LOCAL_SET_STANDARD();
1d7c1841
GS
2614 PerlIO_printf(Perl_debug_log, "0x%"UVxf" num(%g)\n",
2615 PTR2UV(sv), SvNVX(sv));
097ee67d
JH
2616 RESTORE_NUMERIC_LOCAL();
2617 });
572bbb43 2618#endif
79072805
LW
2619 }
2620 else if (SvTYPE(sv) < SVt_PVNV)
2621 sv_upgrade(sv, SVt_PVNV);
61604483
HS
2622 if (SvNOKp(sv) && !(SvIOK(sv) || SvPOK(sv))) {
2623 SvNOK_on(sv);
2624 }
906f284f 2625 else if (SvIOKp(sv)) {
65202027 2626 SvNVX(sv) = SvIsUV(sv) ? (NV)SvUVX(sv) : (NV)SvIVX(sv);
28e5dec8
JH
2627#ifdef NV_PRESERVES_UV
2628 SvNOK_on(sv);
2629#else
2630 /* Only set the public NV OK flag if this NV preserves the IV */
2631 /* Check it's not 0xFFFFFFFFFFFFFFFF */
2632 if (SvIsUV(sv) ? ((SvUVX(sv) != UV_MAX)&&(SvUVX(sv) == U_V(SvNVX(sv))))
2633 : (SvIVX(sv) == I_V(SvNVX(sv))))
2634 SvNOK_on(sv);
2635 else
2636 SvNOKp_on(sv);
2637#endif
93a17b20 2638 }
748a9306 2639 else if (SvPOKp(sv) && SvLEN(sv)) {
c2988b20
NC
2640 UV value;
2641 int numtype = grok_number(SvPVX(sv), SvCUR(sv), &value);
2642 if (ckWARN(WARN_NUMERIC) && !SvIOKp(sv) && !numtype)
a0d0e21e 2643 not_a_number(sv);
28e5dec8 2644#ifdef NV_PRESERVES_UV
c2988b20
NC
2645 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2646 == IS_NUMBER_IN_UV) {
2647 /* It's defintately an integer */
2648 SvNVX(sv) = (numtype & IS_NUMBER_NEG) ? -(NV)value : (NV)value;
2649 } else
2650 SvNVX(sv) = Atof(SvPVX(sv));
28e5dec8
JH
2651 SvNOK_on(sv);
2652#else
c2988b20 2653 SvNVX(sv) = Atof(SvPVX(sv));
28e5dec8
JH
2654 /* Only set the public NV OK flag if this NV preserves the value in
2655 the PV at least as well as an IV/UV would.
2656 Not sure how to do this 100% reliably. */
2657 /* if that shift count is out of range then Configure's test is
2658 wonky. We shouldn't be in here with NV_PRESERVES_UV_BITS ==
2659 UV_BITS */
2660 if (((UV)1 << NV_PRESERVES_UV_BITS) >
c2988b20 2661 U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) {
28e5dec8 2662 SvNOK_on(sv); /* Definitely small enough to preserve all bits */
c2988b20
NC
2663 } else if (!(numtype & IS_NUMBER_IN_UV)) {
2664 /* Can't use strtol etc to convert this string, so don't try.
2665 sv_2iv and sv_2uv will use the NV to convert, not the PV. */
2666 SvNOK_on(sv);
2667 } else {
2668 /* value has been set. It may not be precise. */
2669 if ((numtype & IS_NUMBER_NEG) && (value > (UV)IV_MIN)) {
2670 /* 2s complement assumption for (UV)IV_MIN */
2671 SvNOK_on(sv); /* Integer is too negative. */
2672 } else {
2673 SvNOKp_on(sv);
2674 SvIOKp_on(sv);
6fa402ec 2675
c2988b20
NC
2676 if (numtype & IS_NUMBER_NEG) {
2677 SvIVX(sv) = -(IV)value;
2678 } else if (value <= (UV)IV_MAX) {
2679 SvIVX(sv) = (IV)value;
2680 } else {
2681 SvUVX(sv) = value;
2682 SvIsUV_on(sv);
2683 }
2684
2685 if (numtype & IS_NUMBER_NOT_INT) {
2686 /* I believe that even if the original PV had decimals,
2687 they are lost beyond the limit of the FP precision.
2688 However, neither is canonical, so both only get p
2689 flags. NWC, 2000/11/25 */
2690 /* Both already have p flags, so do nothing */
2691 } else {
2692 NV nv = SvNVX(sv);
2693 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
2694 if (SvIVX(sv) == I_V(nv)) {
2695 SvNOK_on(sv);
2696 SvIOK_on(sv);
2697 } else {
2698 SvIOK_on(sv);
2699 /* It had no "." so it must be integer. */
2700 }
2701 } else {
2702 /* between IV_MAX and NV(UV_MAX).
2703 Could be slightly > UV_MAX */
6fa402ec 2704
c2988b20
NC
2705 if (numtype & IS_NUMBER_NOT_INT) {
2706 /* UV and NV both imprecise. */
2707 } else {
2708 UV nv_as_uv = U_V(nv);
2709
2710 if (value == nv_as_uv && SvUVX(sv) != UV_MAX) {
2711 SvNOK_on(sv);
2712 SvIOK_on(sv);
2713 } else {
2714 SvIOK_on(sv);
2715 }
2716 }
2717 }
2718 }
2719 }
2720 }
28e5dec8 2721#endif /* NV_PRESERVES_UV */
93a17b20 2722 }
79072805 2723 else {
599cee73 2724 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP))
1d7c1841 2725 report_uninit();
25da4f38
IZ
2726 if (SvTYPE(sv) < SVt_NV)
2727 /* Typically the caller expects that sv_any is not NULL now. */
28e5dec8
JH
2728 /* XXX Ilya implies that this is a bug in callers that assume this
2729 and ideally should be fixed. */
25da4f38 2730 sv_upgrade(sv, SVt_NV);
a0d0e21e 2731 return 0.0;
79072805 2732 }
572bbb43 2733#if defined(USE_LONG_DOUBLE)
097ee67d 2734 DEBUG_c({
f93f4e46 2735 STORE_NUMERIC_LOCAL_SET_STANDARD();
1d7c1841
GS
2736 PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2nv(%" PERL_PRIgldbl ")\n",
2737 PTR2UV(sv), SvNVX(sv));
572bbb43
GS
2738 RESTORE_NUMERIC_LOCAL();
2739 });
65202027 2740#else
572bbb43 2741 DEBUG_c({
f93f4e46 2742 STORE_NUMERIC_LOCAL_SET_STANDARD();
1d7c1841
GS
2743 PerlIO_printf(Perl_debug_log, "0x%"UVxf" 1nv(%g)\n",
2744 PTR2UV(sv), SvNVX(sv));
097ee67d
JH
2745 RESTORE_NUMERIC_LOCAL();
2746 });
572bbb43 2747#endif
463ee0b2 2748 return SvNVX(sv);
79072805
LW
2749}
2750
645c22ef
DM
2751/* asIV(): extract an integer from the string value of an SV.
2752 * Caller must validate PVX */
2753
76e3520e 2754STATIC IV
cea2e8a9 2755S_asIV(pTHX_ SV *sv)
36477c24 2756{
c2988b20
NC
2757 UV value;
2758 int numtype = grok_number(SvPVX(sv), SvCUR(sv), &value);
2759
2760 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2761 == IS_NUMBER_IN_UV) {
645c22ef 2762 /* It's definitely an integer */
c2988b20
NC
2763 if (numtype & IS_NUMBER_NEG) {
2764 if (value < (UV)IV_MIN)
2765 return -(IV)value;
2766 } else {
2767 if (value < (UV)IV_MAX)
2768 return (IV)value;
2769 }
2770 }
d008e5eb 2771 if (!numtype) {
d008e5eb
GS
2772 if (ckWARN(WARN_NUMERIC))
2773 not_a_number(sv);
2774 }
c2988b20 2775 return I_V(Atof(SvPVX(sv)));
36477c24 2776}
2777
645c22ef
DM
2778/* asUV(): extract an unsigned integer from the string value of an SV
2779 * Caller must validate PVX */
2780
76e3520e 2781STATIC UV
cea2e8a9 2782S_asUV(pTHX_ SV *sv)
36477c24 2783{
c2988b20
NC
2784 UV value;
2785 int numtype = grok_number(SvPVX(sv), SvCUR(sv), &value);
36477c24 2786
c2988b20
NC
2787 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2788 == IS_NUMBER_IN_UV) {
645c22ef 2789 /* It's definitely an integer */
6fa402ec 2790 if (!(numtype & IS_NUMBER_NEG))
c2988b20
NC
2791 return value;
2792 }
d008e5eb 2793 if (!numtype) {
d008e5eb
GS
2794 if (ckWARN(WARN_NUMERIC))
2795 not_a_number(sv);
2796 }
097ee67d 2797 return U_V(Atof(SvPVX(sv)));
36477c24 2798}
2799
645c22ef
DM
2800/*
2801=for apidoc sv_2pv_nolen
2802
2803Like C<sv_2pv()>, but doesn't return the length too. You should usually
2804use the macro wrapper C<SvPV_nolen(sv)> instead.
2805=cut
2806*/
2807
79072805 2808char *
864dbfa3 2809Perl_sv_2pv_nolen(pTHX_ register SV *sv)
1fa8b10d
JD
2810{
2811 STRLEN n_a;
2812 return sv_2pv(sv, &n_a);
2813}
2814
645c22ef
DM
2815/* uiv_2buf(): private routine for use by sv_2pv_flags(): print an IV or
2816 * UV as a string towards the end of buf, and return pointers to start and
2817 * end of it.
2818 *
2819 * We assume that buf is at least TYPE_CHARS(UV) long.
2820 */
2821
864dbfa3 2822static char *
25da4f38
IZ
2823uiv_2buf(char *buf, IV iv, UV uv, int is_uv, char **peob)
2824{
25da4f38
IZ
2825 char *ptr = buf + TYPE_CHARS(UV);
2826 char *ebuf = ptr;
2827 int sign;
25da4f38
IZ
2828
2829 if (is_uv)
2830 sign = 0;
2831 else if (iv >= 0) {
2832 uv = iv;
2833 sign = 0;
2834 } else {
2835 uv = -iv;
2836 sign = 1;
2837 }
2838 do {
2839 *--ptr = '0' + (uv % 10);
2840 } while (uv /= 10);
2841 if (sign)
2842 *--ptr = '-';
2843 *peob = ebuf;
2844 return ptr;
2845}
2846
645c22ef
DM
2847/* For backwards-compatibility only. sv_2pv() is normally #def'ed to
2848 * C<sv_2pv_macro()>. See also C<sv_2pv_flags()>.
2849 */
2850
1fa8b10d 2851char *
864dbfa3 2852Perl_sv_2pv(pTHX_ register SV *sv, STRLEN *lp)
79072805 2853{
36f65ada 2854 return sv_2pv_flags(sv, lp, SV_GMAGIC);
8d6d96c1
HS
2855}
2856
645c22ef
DM
2857/*
2858=for apidoc sv_2pv_flags
2859
2860Returns pointer to the string value of an SV, and sets *lp to its length.
2861If flags includes SV_GMAGIC, does an mg_get() first. Coerces sv to a string
2862if necessary.
2863Normally invoked via the C<SvPV_flags> macro. C<sv_2pv()> and C<sv_2pv_nomg>
2864usually end up here too.
2865
2866=cut
2867*/
2868
8d6d96c1
HS
2869char *
2870Perl_sv_2pv_flags(pTHX_ register SV *sv, STRLEN *lp, I32 flags)
2871{
79072805
LW
2872 register char *s;
2873 int olderrno;
46fc3d4c 2874 SV *tsv;
25da4f38
IZ
2875 char tbuf[64]; /* Must fit sprintf/Gconvert of longest IV/NV */
2876 char *tmpbuf = tbuf;
79072805 2877
463ee0b2
LW
2878 if (!sv) {
2879 *lp = 0;
2880 return "";
2881 }
8990e307 2882 if (SvGMAGICAL(sv)) {
8d6d96c1
HS
2883 if (flags & SV_GMAGIC)
2884 mg_get(sv);
463ee0b2
LW
2885 if (SvPOKp(sv)) {
2886 *lp = SvCUR(sv);
2887 return SvPVX(sv);
2888 }
cf2093f6 2889 if (SvIOKp(sv)) {
1c846c1f 2890 if (SvIsUV(sv))
57def98f 2891 (void)sprintf(tmpbuf,"%"UVuf, (UV)SvUVX(sv));
cf2093f6 2892 else
57def98f 2893 (void)sprintf(tmpbuf,"%"IVdf, (IV)SvIVX(sv));
46fc3d4c 2894 tsv = Nullsv;
a0d0e21e 2895 goto tokensave;
463ee0b2
LW
2896 }
2897 if (SvNOKp(sv)) {
2d4389e4 2898 Gconvert(SvNVX(sv), NV_DIG, 0, tmpbuf);
46fc3d4c 2899 tsv = Nullsv;
a0d0e21e 2900 goto tokensave;
463ee0b2 2901 }
16d20bd9 2902 if (!SvROK(sv)) {
d008e5eb 2903 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
d008e5eb 2904 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
1d7c1841 2905 report_uninit();
c6ee37c5 2906 }
16d20bd9
AD
2907 *lp = 0;
2908 return "";
2909 }
463ee0b2 2910 }
ed6116ce
LW
2911 if (SvTHINKFIRST(sv)) {
2912 if (SvROK(sv)) {
a0d0e21e 2913 SV* tmpstr;
1554e226 2914 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,string)) &&
1dc13c17 2915 (SvTYPE(tmpstr) != SVt_RV || (SvRV(tmpstr) != SvRV(sv))))
9e7bc3e8 2916 return SvPV(tmpstr,*lp);
ed6116ce
LW
2917 sv = (SV*)SvRV(sv);
2918 if (!sv)
2919 s = "NULLREF";
2920 else {
f9277f47
IZ
2921 MAGIC *mg;
2922
ed6116ce 2923 switch (SvTYPE(sv)) {
f9277f47
IZ
2924 case SVt_PVMG:
2925 if ( ((SvFLAGS(sv) &
1c846c1f 2926 (SVs_OBJECT|SVf_OK|SVs_GMG|SVs_SMG|SVs_RMG))
3149a8e4 2927 == (SVs_OBJECT|SVs_RMG))
57668c4d 2928 && strEQ(s=HvNAME(SvSTASH(sv)), "Regexp")
14befaf4 2929 && (mg = mg_find(sv, PERL_MAGIC_qr))) {
2cd61cdb 2930 regexp *re = (regexp *)mg->mg_obj;
1bd3ad17 2931
2cd61cdb 2932 if (!mg->mg_ptr) {
8782bef2
GB
2933 char *fptr = "msix";
2934 char reflags[6];
2935 char ch;
2936 int left = 0;
2937 int right = 4;
2938 U16 reganch = (re->reganch & PMf_COMPILETIME) >> 12;
2939
155aba94 2940 while((ch = *fptr++)) {
8782bef2
GB
2941 if(reganch & 1) {
2942 reflags[left++] = ch;
2943 }
2944 else {
2945 reflags[right--] = ch;
2946 }
2947 reganch >>= 1;
2948 }
2949 if(left != 4) {
2950 reflags[left] = '-';
2951 left = 5;
2952 }
2953
2954 mg->mg_len = re->prelen + 4 + left;
2955 New(616, mg->mg_ptr, mg->mg_len + 1 + left, char);
2956 Copy("(?", mg->mg_ptr, 2, char);
2957 Copy(reflags, mg->mg_ptr+2, left, char);
2958 Copy(":", mg->mg_ptr+left+2, 1, char);
2959 Copy(re->precomp, mg->mg_ptr+3+left, re->prelen, char);
1bd3ad17
IZ
2960 mg->mg_ptr[mg->mg_len - 1] = ')';
2961 mg->mg_ptr[mg->mg_len] = 0;
2962 }
3280af22 2963 PL_reginterp_cnt += re->program[0].next_off;
1bd3ad17
IZ
2964 *lp = mg->mg_len;
2965 return mg->mg_ptr;
f9277f47
IZ
2966 }
2967 /* Fall through */
ed6116ce
LW
2968 case SVt_NULL:
2969 case SVt_IV:
2970 case SVt_NV:
2971 case SVt_RV:
2972 case SVt_PV:
2973 case SVt_PVIV:
2974 case SVt_PVNV:
81689caa
HS
2975 case SVt_PVBM: if (SvROK(sv))
2976 s = "REF";
2977 else
2978 s = "SCALAR"; break;
ed6116ce
LW
2979 case SVt_PVLV: s = "LVALUE"; break;
2980 case SVt_PVAV: s = "ARRAY"; break;
2981 case SVt_PVHV: s = "HASH"; break;
2982 case SVt_PVCV: s = "CODE"; break;
2983 case SVt_PVGV: s = "GLOB"; break;
1d2dff63 2984 case SVt_PVFM: s = "FORMAT"; break;
36477c24 2985 case SVt_PVIO: s = "IO"; break;
ed6116ce
LW
2986 default: s = "UNKNOWN"; break;
2987 }
46fc3d4c 2988 tsv = NEWSV(0,0);
ed6116ce 2989 if (SvOBJECT(sv))
cea2e8a9 2990 Perl_sv_setpvf(aTHX_ tsv, "%s=%s", HvNAME(SvSTASH(sv)), s);
ed6116ce 2991 else
46fc3d4c 2992 sv_setpv(tsv, s);
57def98f 2993 Perl_sv_catpvf(aTHX_ tsv, "(0x%"UVxf")", PTR2UV(sv));
a0d0e21e 2994 goto tokensaveref;
463ee0b2 2995 }
ed6116ce
LW
2996 *lp = strlen(s);
2997 return s;
79072805 2998 }
0336b60e 2999 if (SvREADONLY(sv) && !SvOK(sv)) {
0336b60e 3000 if (ckWARN(WARN_UNINITIALIZED))
1d7c1841 3001 report_uninit();
ed6116ce
LW
3002 *lp = 0;
3003 return "";
79072805 3004 }
79072805 3005 }
28e5dec8
JH
3006 if (SvIOK(sv) || ((SvIOKp(sv) && !SvNOKp(sv)))) {
3007 /* I'm assuming that if both IV and NV are equally valid then
3008 converting the IV is going to be more efficient */
3009 U32 isIOK = SvIOK(sv);
3010 U32 isUIOK = SvIsUV(sv);
3011 char buf[TYPE_CHARS(UV)];
3012 char *ebuf, *ptr;
3013
3014 if (SvTYPE(sv) < SVt_PVIV)
3015 sv_upgrade(sv, SVt_PVIV);
3016 if (isUIOK)
3017 ptr = uiv_2buf(buf, 0, SvUVX(sv), 1, &ebuf);
3018 else
3019 ptr = uiv_2buf(buf, SvIVX(sv), 0, 0, &ebuf);
3020 SvGROW(sv, ebuf - ptr + 1); /* inlined from sv_setpvn */
3021 Move(ptr,SvPVX(sv),ebuf - ptr,char);
3022 SvCUR_set(sv, ebuf - ptr);
3023 s = SvEND(sv);
3024 *s = '\0';
3025 if (isIOK)
3026 SvIOK_on(sv);
3027 else
3028 SvIOKp_on(sv);
3029 if (isUIOK)
3030 SvIsUV_on(sv);
3031 }
3032 else if (SvNOKp(sv)) {
79072805
LW
3033 if (SvTYPE(sv) < SVt_PVNV)
3034 sv_upgrade(sv, SVt_PVNV);
1c846c1f 3035 /* The +20 is pure guesswork. Configure test needed. --jhi */
59155cc0 3036 SvGROW(sv, NV_DIG + 20);
463ee0b2 3037 s = SvPVX(sv);
79072805 3038 olderrno = errno; /* some Xenix systems wipe out errno here */
79072805 3039#ifdef apollo
463ee0b2 3040 if (SvNVX(sv) == 0.0)
79072805
LW
3041 (void)strcpy(s,"0");
3042 else
3043#endif /*apollo*/
bbce6d69 3044 {
2d4389e4 3045 Gconvert(SvNVX(sv), NV_DIG, 0, s);
bbce6d69 3046 }
79072805 3047 errno = olderrno;
a0d0e21e
LW
3048#ifdef FIXNEGATIVEZERO
3049 if (*s == '-' && s[1] == '0' && !s[2])
3050 strcpy(s,"0");
3051#endif
79072805
LW
3052 while (*s) s++;
3053#ifdef hcx
3054 if (s[-1] == '.')
46fc3d4c 3055 *--s = '\0';
79072805
LW
3056#endif
3057 }
79072805 3058 else {
0336b60e
IZ
3059 if (ckWARN(WARN_UNINITIALIZED)
3060 && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP))
1d7c1841 3061 report_uninit();
a0d0e21e 3062 *lp = 0;
25da4f38
IZ
3063 if (SvTYPE(sv) < SVt_PV)
3064 /* Typically the caller expects that sv_any is not NULL now. */
3065 sv_upgrade(sv, SVt_PV);
a0d0e21e 3066 return "";
79072805 3067 }
463ee0b2
LW
3068 *lp = s - SvPVX(sv);
3069 SvCUR_set(sv, *lp);
79072805 3070 SvPOK_on(sv);
1d7c1841
GS
3071 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2pv(%s)\n",
3072 PTR2UV(sv),SvPVX(sv)));
463ee0b2 3073 return SvPVX(sv);
a0d0e21e
LW
3074
3075 tokensave:
3076 if (SvROK(sv)) { /* XXX Skip this when sv_pvn_force calls */
3077 /* Sneaky stuff here */
3078
3079 tokensaveref:
46fc3d4c 3080 if (!tsv)
96827780 3081 tsv = newSVpv(tmpbuf, 0);
46fc3d4c 3082 sv_2mortal(tsv);
3083 *lp = SvCUR(tsv);
3084 return SvPVX(tsv);
a0d0e21e
LW
3085 }
3086 else {
3087 STRLEN len;
46fc3d4c 3088 char *t;
3089
3090 if (tsv) {
3091 sv_2mortal(tsv);
3092 t = SvPVX(tsv);
3093 len = SvCUR(tsv);
3094 }
3095 else {
96827780
MB
3096 t = tmpbuf;
3097 len = strlen(tmpbuf);
46fc3d4c 3098 }
a0d0e21e 3099#ifdef FIXNEGATIVEZERO
46fc3d4c 3100 if (len == 2 && t[0] == '-' && t[1] == '0') {
3101 t = "0";
3102 len = 1;
3103 }
a0d0e21e
LW
3104#endif
3105 (void)SvUPGRADE(sv, SVt_PV);
46fc3d4c 3106 *lp = len;
a0d0e21e
LW
3107 s = SvGROW(sv, len + 1);
3108 SvCUR_set(sv, len);
46fc3d4c 3109 (void)strcpy(s, t);
6bf554b4 3110 SvPOKp_on(sv);
a0d0e21e
LW
3111 return s;
3112 }
463ee0b2
LW
3113}
3114
645c22ef
DM
3115/*
3116=for apidoc sv_2pvbyte_nolen
3117
3118Return a pointer to the byte-encoded representation of the SV.
3119May cause the SV to be downgraded from UTF8 as a side-effect.
3120
3121Usually accessed via the C<SvPVbyte_nolen> macro.
3122
3123=cut
3124*/
3125
7340a771
GS
3126char *
3127Perl_sv_2pvbyte_nolen(pTHX_ register SV *sv)
3128{
560a288e
GS
3129 STRLEN n_a;
3130 return sv_2pvbyte(sv, &n_a);
7340a771
GS
3131}
3132
645c22ef
DM
3133/*
3134=for apidoc sv_2pvbyte
3135
3136Return a pointer to the byte-encoded representation of the SV, and set *lp
3137to its length. May cause the SV to be downgraded from UTF8 as a
3138side-effect.
3139
3140Usually accessed via the C<SvPVbyte> macro.
3141
3142=cut
3143*/
3144
7340a771
GS
3145char *
3146Perl_sv_2pvbyte(pTHX_ register SV *sv, STRLEN *lp)
3147{
0875d2fe
NIS
3148 sv_utf8_downgrade(sv,0);
3149 return SvPV(sv,*lp);
7340a771
GS
3150}
3151
645c22ef
DM
3152/*
3153=for apidoc sv_2pvutf8_nolen
3154
3155Return a pointer to the UTF8-encoded representation of the SV.
3156May cause the SV to be upgraded to UTF8 as a side-effect.
3157
3158Usually accessed via the C<SvPVutf8_nolen> macro.
3159
3160=cut
3161*/
3162
7340a771
GS
3163char *
3164Perl_sv_2pvutf8_nolen(pTHX_ register SV *sv)
3165{
560a288e
GS
3166 STRLEN n_a;
3167 return sv_2pvutf8(sv, &n_a);
7340a771
GS
3168}
3169
645c22ef
DM
3170/*
3171=for apidoc sv_2pvutf8
3172
3173Return a pointer to the UTF8-encoded representation of the SV, and set *lp
3174to its length. May cause the SV to be upgraded to UTF8 as a side-effect.
3175
3176Usually accessed via the C<SvPVutf8> macro.
3177
3178=cut
3179*/
3180
7340a771
GS
3181char *
3182Perl_sv_2pvutf8(pTHX_ register SV *sv, STRLEN *lp)
3183{
560a288e 3184 sv_utf8_upgrade(sv);
7d59b7e4 3185 return SvPV(sv,*lp);
7340a771 3186}
1c846c1f 3187
645c22ef
DM
3188/*
3189=for apidoc sv_2bool
3190
3191This function is only called on magical items, and is only used by
3192sv_true() or its macro equivalent.
3193
3194=cut
3195*/
3196
463ee0b2 3197bool
864dbfa3 3198Perl_sv_2bool(pTHX_ register SV *sv)
463ee0b2 3199{
8990e307 3200 if (SvGMAGICAL(sv))
463ee0b2
LW
3201 mg_get(sv);
3202
a0d0e21e
LW
3203 if (!SvOK(sv))
3204 return 0;
3205 if (SvROK(sv)) {
a0d0e21e 3206 SV* tmpsv;
1554e226 3207 if (SvAMAGIC(sv) && (tmpsv=AMG_CALLun(sv,bool_)) &&
1dc13c17 3208 (SvTYPE(tmpsv) != SVt_RV || (SvRV(tmpsv) != SvRV(sv))))
9e7bc3e8 3209 return SvTRUE(tmpsv);
a0d0e21e
LW
3210 return SvRV(sv) != 0;
3211 }
463ee0b2 3212 if (SvPOKp(sv)) {
11343788
MB
3213 register XPV* Xpvtmp;
3214 if ((Xpvtmp = (XPV*)SvANY(sv)) &&
3215 (*Xpvtmp->xpv_pv > '0' ||
3216 Xpvtmp->xpv_cur > 1 ||
3217 (Xpvtmp->xpv_cur && *Xpvtmp->xpv_pv != '0')))
463ee0b2
LW
3218 return 1;
3219 else
3220 return 0;
3221 }
3222 else {
3223 if (SvIOKp(sv))
3224 return SvIVX(sv) != 0;
3225 else {
3226 if (SvNOKp(sv))
3227 return SvNVX(sv) != 0.0;
3228 else
3229 return FALSE;
3230 }
3231 }
79072805
LW
3232}
3233
c461cf8f
JH
3234/*
3235=for apidoc sv_utf8_upgrade
3236
3237Convert the PV of an SV to its UTF8-encoded form.
645c22ef 3238Forces the SV to string form if it is not already.
4411f3b6
NIS
3239Always sets the SvUTF8 flag to avoid future validity checks even
3240if all the bytes have hibit clear.
c461cf8f
JH
3241
3242=cut
3243*/
3244
4411f3b6 3245STRLEN
560a288e
GS
3246Perl_sv_utf8_upgrade(pTHX_ register SV *sv)
3247{
36f65ada 3248 return sv_utf8_upgrade_flags(sv, SV_GMAGIC);
8d6d96c1
HS
3249}
3250
3251/*
3252=for apidoc sv_utf8_upgrade_flags
3253
3254Convert the PV of an SV to its UTF8-encoded form.
645c22ef 3255Forces the SV to string form if it is not already.
8d6d96c1
HS
3256Always sets the SvUTF8 flag to avoid future validity checks even
3257if all the bytes have hibit clear. If C<flags> has C<SV_GMAGIC> bit set,
3258will C<mg_get> on C<sv> if appropriate, else not. C<sv_utf8_upgrade> and
3259C<sv_utf8_upgrade_nomg> are implemented in terms of this function.
3260
3261=cut
3262*/
3263
3264STRLEN
3265Perl_sv_utf8_upgrade_flags(pTHX_ register SV *sv, I32 flags)
3266{
db42d148 3267 U8 *s, *t, *e;
511c2ff0 3268 int hibit = 0;
560a288e 3269
4411f3b6
NIS
3270 if (!sv)
3271 return 0;
3272
e0e62c2a
NIS
3273 if (!SvPOK(sv)) {
3274 STRLEN len = 0;
8d6d96c1 3275 (void) sv_2pv_flags(sv,&len, flags);
e0e62c2a
NIS
3276 if (!SvPOK(sv))
3277 return len;
3278 }
4411f3b6
NIS
3279
3280 if (SvUTF8(sv))
3281 return SvCUR(sv);
560a288e 3282
db42d148
NIS
3283 if (SvREADONLY(sv) && SvFAKE(sv)) {
3284 sv_force_normal(sv);
3285 }
3286
40826f67
JH
3287 /* This function could be much more efficient if we had a FLAG in SVs
3288 * to signal if there are any hibit chars in the PV.
511c2ff0 3289 * Given that there isn't make loop fast as possible
560a288e 3290 */
db42d148
NIS
3291 s = (U8 *) SvPVX(sv);
3292 e = (U8 *) SvEND(sv);
511c2ff0
NIS
3293 t = s;
3294 while (t < e) {
c4d5f83a
NIS
3295 U8 ch = *t++;
3296 if ((hibit = !NATIVE_IS_INVARIANT(ch)))
8a818333 3297 break;
8a818333 3298 }
40826f67 3299 if (hibit) {
8a818333 3300 STRLEN len;
652088fc 3301
8a818333 3302 len = SvCUR(sv) + 1; /* Plus the \0 */
00df9076 3303 SvPVX(sv) = (char*)bytes_to_utf8((U8*)s, &len);
841d7a39 3304 SvCUR(sv) = len - 1;
511c2ff0
NIS
3305 if (SvLEN(sv) != 0)
3306 Safefree(s); /* No longer using what was there before. */
841d7a39 3307 SvLEN(sv) = len; /* No longer know the real size. */
560a288e 3308 }
4411f3b6
NIS
3309 /* Mark as UTF-8 even if no hibit - saves scanning loop */
3310 SvUTF8_on(sv);
3311 return SvCUR(sv);
560a288e
GS
3312}
3313
c461cf8f
JH
3314/*
3315=for apidoc sv_utf8_downgrade
3316
3317Attempt to convert the PV of an SV from UTF8-encoded to byte encoding.
3318This may not be possible if the PV contains non-byte encoding characters;
3319if this is the case, either returns false or, if C<fail_ok> is not
3320true, croaks.
3321
3322=cut
3323*/
3324
560a288e
GS
3325bool
3326Perl_sv_utf8_downgrade(pTHX_ register SV* sv, bool fail_ok)
3327{
3328 if (SvPOK(sv) && SvUTF8(sv)) {
fa301091 3329 if (SvCUR(sv)) {
03cfe0ae 3330 U8 *s;
652088fc 3331 STRLEN len;
fa301091 3332
652088fc
JH
3333 if (SvREADONLY(sv) && SvFAKE(sv))
3334 sv_force_normal(sv);
03cfe0ae
NIS
3335 s = (U8 *) SvPV(sv, len);
3336 if (!utf8_to_bytes(s, &len)) {
fa301091
JH
3337 if (fail_ok)
3338 return FALSE;
03cfe0ae 3339#ifdef USE_BYTES_DOWNGRADES
0064a8a9 3340 else if (IN_BYTES) {
03cfe0ae
NIS
3341 U8 *d = s;
3342 U8 *e = (U8 *) SvEND(sv);
3343 int first = 1;
3344 while (s < e) {
3345 UV ch = utf8n_to_uvchr(s,(e-s),&len,0);
3346 if (first && ch > 255) {
3347 if (PL_op)
3348 Perl_warner(aTHX_ WARN_UTF8, "Wide character in byte %s",
3349 PL_op_desc[PL_op->op_type]);
3350 else
3351 Perl_warner(aTHX_ WARN_UTF8, "Wide character in byte");
3352 first = 0;
3353 }
3354 *d++ = ch;
3355 s += len;
3356 }
3357 *d = '\0';
3358 len = (d - (U8 *) SvPVX(sv));
3359 }
3360#endif
fa301091
JH
3361 else {
3362 if (PL_op)
3363 Perl_croak(aTHX_ "Wide character in %s",
3364 PL_op_desc[PL_op->op_type]);
3365 else
3366 Perl_croak(aTHX_ "Wide character");
3367 }
4b3603a4 3368 }
fa301091 3369 SvCUR(sv) = len;
67e989fb 3370 }
560a288e 3371 }
ffebcc3e 3372 SvUTF8_off(sv);
560a288e
GS
3373 return TRUE;
3374}
3375
c461cf8f
JH
3376/*
3377=for apidoc sv_utf8_encode
3378
3379Convert the PV of an SV to UTF8-encoded, but then turn off the C<SvUTF8>
4411f3b6
NIS
3380flag so that it looks like octets again. Used as a building block
3381for encode_utf8 in Encode.xs
c461cf8f
JH
3382
3383=cut
3384*/
3385
560a288e
GS
3386void
3387Perl_sv_utf8_encode(pTHX_ register SV *sv)
3388{
4411f3b6 3389 (void) sv_utf8_upgrade(sv);
560a288e
GS
3390 SvUTF8_off(sv);
3391}
3392
4411f3b6
NIS
3393/*
3394=for apidoc sv_utf8_decode
3395
3396Convert the octets in the PV from UTF-8 to chars. Scan for validity and then
645c22ef 3397turn off SvUTF8 if needed so that we see characters. Used as a building block
4411f3b6
NIS
3398for decode_utf8 in Encode.xs
3399
3400=cut
3401*/
3402
560a288e
GS
3403bool
3404Perl_sv_utf8_decode(pTHX_ register SV *sv)
3405{
3406 if (SvPOK(sv)) {
63cd0674
NIS
3407 U8 *c;
3408 U8 *e;
9cbac4c7 3409
645c22ef
DM
3410 /* The octets may have got themselves encoded - get them back as
3411 * bytes
3412 */
3413 if (!sv_utf8_downgrade(sv, TRUE))
560a288e
GS
3414 return FALSE;
3415
3416 /* it is actually just a matter of turning the utf8 flag on, but
3417 * we want to make sure everything inside is valid utf8 first.
3418 */
63cd0674
NIS
3419 c = (U8 *) SvPVX(sv);
3420 if (!is_utf8_string(c, SvCUR(sv)+1))
67e989fb 3421 return FALSE;
63cd0674 3422 e = (U8 *) SvEND(sv);
511c2ff0 3423 while (c < e) {
c4d5f83a
NIS
3424 U8 ch = *c++;
3425 if (!UTF8_IS_INVARIANT(ch)) {
67e989fb
JH
3426 SvUTF8_on(sv);
3427 break;
3428 }
560a288e 3429 }
560a288e
GS
3430 }
3431 return TRUE;
3432}
3433
954c1994
GS
3434/*
3435=for apidoc sv_setsv
3436
645c22ef
DM
3437Copies the contents of the source SV C<ssv> into the destination SV
3438C<dsv>. The source SV may be destroyed if it is mortal, so don't use this
3439function if the source SV needs to be reused. Does not handle 'set' magic.
3440Loosely speaking, it performs a copy-by-value, obliterating any previous
3441content of the destination.
3442
3443You probably want to use one of the assortment of wrappers, such as
3444C<SvSetSV>, C<SvSetSV_nosteal>, C<SvSetMagicSV> and
3445C<SvSetMagicSV_nosteal>.
3446
954c1994
GS
3447
3448=cut
3449*/
3450
8d6d96c1
HS
3451/* sv_setsv() is aliased to Perl_sv_setsv_macro; this function provided
3452 for binary compatibility only
3453*/
79072805 3454void
864dbfa3 3455Perl_sv_setsv(pTHX_ SV *dstr, register SV *sstr)
79072805 3456{
8d6d96c1
HS
3457 sv_setsv_flags(dstr, sstr, SV_GMAGIC);
3458}
3459
3460/*
3461=for apidoc sv_setsv_flags
3462
645c22ef
DM
3463Copies the contents of the source SV C<ssv> into the destination SV
3464C<dsv>. The source SV may be destroyed if it is mortal, so don't use this
3465function if the source SV needs to be reused. Does not handle 'set' magic.
3466Loosely speaking, it performs a copy-by-value, obliterating any previous
3467content of the destination.
3468If the C<flags> parameter has the C<SV_GMAGIC> bit set, will C<mg_get> on
3469C<ssv> if appropriate, else not. C<sv_setsv> and C<sv_setsv_nomg> are
3470implemented in terms of this function.
3471
3472You probably want to use one of the assortment of wrappers, such as
3473C<SvSetSV>, C<SvSetSV_nosteal>, C<SvSetMagicSV> and
3474C<SvSetMagicSV_nosteal>.
3475
3476This is the primary function for copying scalars, and most other
3477copy-ish functions and macros use this underneath.
8d6d96c1
HS
3478
3479=cut
3480*/
3481
3482void
3483Perl_sv_setsv_flags(pTHX_ SV *dstr, register SV *sstr, I32 flags)
3484{
8990e307
LW
3485 register U32 sflags;
3486 register int dtype;
3487 register int stype;
463ee0b2 3488
79072805
LW
3489 if (sstr == dstr)
3490 return;
2213622d 3491 SV_CHECK_THINKFIRST(dstr);
79072805 3492 if (!sstr)
3280af22 3493 sstr = &PL_sv_undef;
8990e307
LW
3494 stype = SvTYPE(sstr);
3495 dtype = SvTYPE(dstr);
79072805 3496
a0d0e21e 3497 SvAMAGIC_off(dstr);
9e7bc3e8 3498
463ee0b2 3499 /* There's a lot of redundancy below but we're going for speed here */
79072805 3500
8990e307 3501 switch (stype) {
79072805 3502 case SVt_NULL:
aece5585 3503 undef_sstr:
20408e3c
GS
3504 if (dtype != SVt_PVGV) {
3505 (void)SvOK_off(dstr);
3506 return;
3507 }
3508 break;
463ee0b2 3509 case SVt_IV:
aece5585
GA
3510 if (SvIOK(sstr)) {
3511 switch (dtype) {
3512 case SVt_NULL:
8990e307 3513 sv_upgrade(dstr, SVt_IV);
aece5585
GA
3514 break;
3515 case SVt_NV:
8990e307 3516 sv_upgrade(dstr, SVt_PVNV);
aece5585
GA
3517 break;
3518 case SVt_RV:
3519 case SVt_PV:
a0d0e21e 3520 sv_upgrade(dstr, SVt_PVIV);
aece5585
GA
3521 break;
3522 }
3523 (void)SvIOK_only(dstr);
3524 SvIVX(dstr) = SvIVX(sstr);
25da4f38
IZ
3525 if (SvIsUV(sstr))
3526 SvIsUV_on(dstr);
27c9684d
AP
3527 if (SvTAINTED(sstr))
3528 SvTAINT(dstr);
aece5585 3529 return;
8990e307 3530 }
aece5585
GA
3531 goto undef_sstr;
3532
463ee0b2 3533 case SVt_NV:
aece5585
GA
3534 if (SvNOK(sstr)) {
3535 switch (dtype) {
3536 case SVt_NULL:
3537 case SVt_IV:
8990e307 3538 sv_upgrade(dstr, SVt_NV);
aece5585
GA
3539 break;
3540 case SVt_RV:
3541 case SVt_PV:
3542 case SVt_PVIV:
a0d0e21e 3543 sv_upgrade(dstr, SVt_PVNV);
aece5585
GA
3544 break;
3545 }
3546 SvNVX(dstr) = SvNVX(sstr);
3547 (void)SvNOK_only(dstr);
27c9684d
AP
3548 if (SvTAINTED(sstr))
3549 SvTAINT(dstr);
aece5585 3550 return;
8990e307 3551 }
aece5585
GA
3552 goto undef_sstr;
3553
ed6116ce 3554 case SVt_RV:
8990e307 3555 if (dtype < SVt_RV)
ed6116ce 3556 sv_upgrade(dstr, SVt_RV);
c07a80fd 3557 else if (dtype == SVt_PVGV &&
3558 SvTYPE(SvRV(sstr)) == SVt_PVGV) {
3559 sstr = SvRV(sstr);
a5f75d66 3560 if (sstr == dstr) {
1d7c1841
GS
3561 if (GvIMPORTED(dstr) != GVf_IMPORTED
3562 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
3563 {
a5f75d66 3564 GvIMPORTED_on(dstr);
1d7c1841 3565 }
a5f75d66
AD
3566 GvMULTI_on(dstr);
3567 return;
3568 }
c07a80fd 3569 goto glob_assign;
3570 }
ed6116ce 3571 break;
463ee0b2 3572 case SVt_PV:
fc36a67e 3573 case SVt_PVFM:
8990e307 3574 if (dtype < SVt_PV)
463ee0b2 3575 sv_upgrade(dstr, SVt_PV);
463ee0b2
LW
3576 break;
3577 case SVt_PVIV:
8990e307 3578 if (dtype < SVt_PVIV)
463ee0b2 3579 sv_upgrade(dstr, SVt_PVIV);
463ee0b2
LW
3580 break;
3581 case SVt_PVNV:
8990e307 3582 if (dtype < SVt_PVNV)
463ee0b2 3583 sv_upgrade(dstr, SVt_PVNV);
463ee0b2 3584 break;
4633a7c4
LW
3585 case SVt_PVAV:
3586 case SVt_PVHV:
3587 case SVt_PVCV:
4633a7c4 3588 case SVt_PVIO:
533c011a 3589 if (PL_op)
cea2e8a9 3590 Perl_croak(aTHX_ "Bizarre copy of %s in %s", sv_reftype(sstr, 0),
22c35a8c 3591 PL_op_name[PL_op->op_type]);
4633a7c4 3592 else
cea2e8a9 3593 Perl_croak(aTHX_ "Bizarre copy of %s", sv_reftype(sstr, 0));
4633a7c4
LW
3594 break;
3595
79072805 3596 case SVt_PVGV:
8990e307 3597 if (dtype <= SVt_PVGV) {
c07a80fd 3598 glob_assign:
a5f75d66 3599 if (dtype != SVt_PVGV) {
a0d0e21e
LW
3600 char *name = GvNAME(sstr);
3601 STRLEN len = GvNAMELEN(sstr);
463ee0b2 3602 sv_upgrade(dstr, SVt_PVGV);
14befaf4 3603 sv_magic(dstr, dstr, PERL_MAGIC_glob, Nullch, 0);
85aff577 3604 GvSTASH(dstr) = (HV*)SvREFCNT_inc(GvSTASH(sstr));
a0d0e21e
LW
3605 GvNAME(dstr) = savepvn(name, len);
3606 GvNAMELEN(dstr) = len;
3607 SvFAKE_on(dstr); /* can coerce to non-glob */
3608 }
7bac28a0 3609 /* ahem, death to those who redefine active sort subs */
3280af22
NIS
3610 else if (PL_curstackinfo->si_type == PERLSI_SORT
3611 && GvCV(dstr) && PL_sortcop == CvSTART(GvCV(dstr)))
cea2e8a9 3612 Perl_croak(aTHX_ "Can't redefine active sort subroutine %s",
7bac28a0 3613 GvNAME(dstr));
5bd07a3d
DM
3614
3615#ifdef GV_SHARED_CHECK
3616 if (GvSHARED((GV*)dstr)) {
3617 Perl_croak(aTHX_ PL_no_modify);
3618 }
3619#endif
3620
a0d0e21e 3621 (void)SvOK_off(dstr);
a5f75d66 3622 GvINTRO_off(dstr); /* one-shot flag */
1edc1566 3623 gp_free((GV*)dstr);
79072805 3624 GvGP(dstr) = gp_ref(GvGP(sstr));
27c9684d
AP
3625 if (SvTAINTED(sstr))
3626 SvTAINT(dstr);
1d7c1841
GS
3627 if (GvIMPORTED(dstr) != GVf_IMPORTED
3628 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
3629 {
a5f75d66 3630 GvIMPORTED_on(dstr);
1d7c1841 3631 }
a5f75d66 3632 GvMULTI_on(dstr);
79072805
LW
3633 return;
3634 }
3635 /* FALL THROUGH */
3636
3637 default:
8d6d96c1 3638 if (SvGMAGICAL(sstr) && (flags & SV_GMAGIC)) {
973f89ab
CS
3639 mg_get(sstr);
3640 if (SvTYPE(sstr) != stype) {
3641 stype = SvTYPE(sstr);
3642 if (stype == SVt_PVGV && dtype <= SVt_PVGV)
3643 goto glob_assign;
3644 }
3645 }
ded42b9f 3646 if (stype == SVt_PVLV)
6fc92669 3647 (void)SvUPGRADE(dstr, SVt_PVNV);
ded42b9f 3648 else
6fc92669 3649 (void)SvUPGRADE(dstr, stype);
79072805
LW
3650 }
3651
8990e307
LW
3652 sflags = SvFLAGS(sstr);
3653
3654 if (sflags & SVf_ROK) {
3655 if (dtype >= SVt_PV) {
3656 if (dtype == SVt_PVGV) {
3657 SV *sref = SvREFCNT_inc(SvRV(sstr));
3658 SV *dref = 0;
a5f75d66 3659 int intro = GvINTRO(dstr);
a0d0e21e 3660
5bd07a3d
DM
3661#ifdef GV_SHARED_CHECK
3662 if (GvSHARED((GV*)dstr)) {
3663 Perl_croak(aTHX_ PL_no_modify);
3664 }
3665#endif
3666
a0d0e21e 3667 if (intro) {
a5f75d66 3668 GvINTRO_off(dstr); /* one-shot flag */
1d7c1841 3669 GvLINE(dstr) = CopLINE(PL_curcop);
1edc1566 3670 GvEGV(dstr) = (GV*)dstr;
a0d0e21e 3671 }
a5f75d66 3672 GvMULTI_on(dstr);
8990e307
LW
3673 switch (SvTYPE(sref)) {
3674 case SVt_PVAV:
a0d0e21e
LW
3675 if (intro)
3676 SAVESPTR(GvAV(dstr));
3677 else
3678 dref = (SV*)GvAV(dstr);
8990e307 3679 GvAV(dstr) = (AV*)sref;
39bac7f7 3680 if (!GvIMPORTED_AV(dstr)
1d7c1841
GS
3681 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
3682 {
a5f75d66 3683 GvIMPORTED_AV_on(dstr);
1d7c1841 3684 }
8990e307
LW
3685 break;
3686 case SVt_PVHV:
a0d0e21e
LW
3687 if (intro)
3688 SAVESPTR(GvHV(dstr));
3689 else
3690 dref = (SV*)GvHV(dstr);
8990e307 3691 GvHV(dstr) = (HV*)sref;
39bac7f7 3692 if (!GvIMPORTED_HV(dstr)
1d7c1841
GS
3693 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
3694 {
a5f75d66 3695 GvIMPORTED_HV_on(dstr);
1d7c1841 3696 }
8990e307
LW
3697 break;
3698 case SVt_PVCV:
8ebc5c01 3699 if (intro) {
3700 if (GvCVGEN(dstr) && GvCV(dstr) != (CV*)sref) {
3701 SvREFCNT_dec(GvCV(dstr));
3702 GvCV(dstr) = Nullcv;
68dc0745 3703 GvCVGEN(dstr) = 0; /* Switch off cacheness. */
3280af22 3704 PL_sub_generation++;
8ebc5c01 3705 }
a0d0e21e 3706 SAVESPTR(GvCV(dstr));
8ebc5c01 3707 }
68dc0745 3708 else
3709 dref = (SV*)GvCV(dstr);
3710 if (GvCV(dstr) != (CV*)sref) {
748a9306 3711 CV* cv = GvCV(dstr);
4633a7c4 3712 if (cv) {
68dc0745 3713 if (!GvCVGEN((GV*)dstr) &&
3714 (CvROOT(cv) || CvXSUB(cv)))
3715 {
7bac28a0 3716 /* ahem, death to those who redefine
3717 * active sort subs */
3280af22
NIS
3718 if (PL_curstackinfo->si_type == PERLSI_SORT &&
3719 PL_sortcop == CvSTART(cv))
1c846c1f 3720 Perl_croak(aTHX_
7bac28a0 3721 "Can't redefine active sort subroutine %s",
3722 GvENAME((GV*)dstr));
beab0874
JT
3723 /* Redefining a sub - warning is mandatory if
3724 it was a const and its value changed. */
3725 if (ckWARN(WARN_REDEFINE)
3726 || (CvCONST(cv)
3727 && (!CvCONST((CV*)sref)
3728 || sv_cmp(cv_const_sv(cv),
3729 cv_const_sv((CV*)sref)))))
3730 {
3731 Perl_warner(aTHX_ WARN_REDEFINE,
3732 CvCONST(cv)
3733 ? "Constant subroutine %s redefined"
47deb5e7 3734 : "Subroutine %s redefined",
beab0874
JT
3735 GvENAME((GV*)dstr));
3736 }
9607fc9c 3737 }
3fe9a6f1 3738 cv_ckproto(cv, (GV*)dstr,
3739 SvPOK(sref) ? SvPVX(sref) : Nullch);
4633a7c4 3740 }
a5f75d66 3741 GvCV(dstr) = (CV*)sref;
7a4c00b4 3742 GvCVGEN(dstr) = 0; /* Switch off cacheness. */
a5f75d66 3743 GvASSUMECV_on(dstr);
3280af22 3744 PL_sub_generation++;
a5f75d66 3745 }
39bac7f7 3746 if (!GvIMPORTED_CV(dstr)
1d7c1841
GS
3747 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
3748 {
a5f75d66 3749 GvIMPORTED_CV_on(dstr);
1d7c1841 3750 }
8990e307 3751 break;
91bba347
LW
3752 case SVt_PVIO:
3753 if (intro)
3754 SAVESPTR(GvIOp(dstr));
3755 else
3756 dref = (SV*)GvIOp(dstr);
3757 GvIOp(dstr) = (IO*)sref;
3758 break;
f4d13ee9
JH
3759 case SVt_PVFM:
3760 if (intro)
3761 SAVESPTR(GvFORM(dstr));
3762 else
3763 dref = (SV*)GvFORM(dstr);
3764 GvFORM(dstr) = (CV*)sref;
3765 break;
8990e307 3766 default:
a0d0e21e
LW
3767 if (intro)
3768 SAVESPTR(GvSV(dstr));
3769 else
3770 dref = (SV*)GvSV(dstr);
8990e307 3771 GvSV(dstr) = sref;
39bac7f7 3772 if (!GvIMPORTED_SV(dstr)
1d7c1841
GS
3773 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
3774 {
a5f75d66 3775 GvIMPORTED_SV_on(dstr);
1d7c1841 3776 }
8990e307
LW
3777 break;
3778 }
3779 if (dref)
3780 SvREFCNT_dec(dref);
a0d0e21e
LW
3781 if (intro)
3782 SAVEFREESV(sref);
27c9684d
AP
3783 if (SvTAINTED(sstr))
3784 SvTAINT(dstr);
8990e307
LW
3785 return;
3786 }
a0d0e21e 3787 if (SvPVX(dstr)) {
760ac839 3788 (void)SvOOK_off(dstr); /* backoff */
50483b2c
JD
3789 if (SvLEN(dstr))
3790 Safefree(SvPVX(dstr));
a0d0e21e
LW
3791 SvLEN(dstr)=SvCUR(dstr)=0;
3792 }
8990e307 3793 }
a0d0e21e 3794 (void)SvOK_off(dstr);
8990e307 3795 SvRV(dstr) = SvREFCNT_inc(SvRV(sstr));
ed6116ce 3796 SvROK_on(dstr);
8990e307 3797 if (sflags & SVp_NOK) {
3332b3c1
JH
3798 SvNOKp_on(dstr);
3799 /* Only set the public OK flag if the source has public OK. */
3800 if (sflags & SVf_NOK)
3801 SvFLAGS(dstr) |= SVf_NOK;
ed6116ce
LW
3802 SvNVX(dstr) = SvNVX(sstr);
3803 }
8990e307 3804 if (sflags & SVp_IOK) {
3332b3c1
JH
3805 (void)SvIOKp_on(dstr);
3806 if (sflags & SVf_IOK)
3807 SvFLAGS(dstr) |= SVf_IOK;
2b1c7e3e 3808 if (sflags & SVf_IVisUV)
25da4f38 3809 SvIsUV_on(dstr);
3332b3c1 3810 SvIVX(dstr) = SvIVX(sstr);
ed6116ce 3811 }
a0d0e21e
LW
3812 if (SvAMAGIC(sstr)) {
3813 SvAMAGIC_on(dstr);
3814 }
ed6116ce 3815 }
8990e307 3816 else if (sflags & SVp_POK) {
79072805
LW
3817
3818 /*
3819 * Check to see if we can just swipe the string. If so, it's a
3820 * possible small lose on short strings, but a big win on long ones.
463ee0b2
LW
3821 * It might even be a win on short strings if SvPVX(dstr)
3822 * has to be allocated and SvPVX(sstr) has to be freed.
79072805
LW
3823 */
3824
ff68c719 3825 if (SvTEMP(sstr) && /* slated for free anyway? */
01b73108 3826 SvREFCNT(sstr) == 1 && /* and no other references to it? */
1c846c1f 3827 !(sflags & SVf_OOK) && /* and not involved in OOK hack? */
4c8f17b9 3828 SvLEN(sstr) && /* and really is a string */
645c22ef
DM
3829 /* and won't be needed again, potentially */
3830 !(PL_op && PL_op->op_type == OP_AASSIGN))
a5f75d66 3831 {
adbc6bb1 3832 if (SvPVX(dstr)) { /* we know that dtype >= SVt_PV */
a5f75d66
AD
3833 if (SvOOK(dstr)) {
3834 SvFLAGS(dstr) &= ~SVf_OOK;
3835 Safefree(SvPVX(dstr) - SvIVX(dstr));
3836 }
50483b2c 3837 else if (SvLEN(dstr))
a5f75d66 3838 Safefree(SvPVX(dstr));
79072805 3839 }
a5f75d66 3840 (void)SvPOK_only(dstr);
463ee0b2 3841 SvPV_set(dstr, SvPVX(sstr));
79072805
LW
3842 SvLEN_set(dstr, SvLEN(sstr));
3843 SvCUR_set(dstr, SvCUR(sstr));
f4e86e0f 3844
79072805 3845 SvTEMP_off(dstr);
645c22ef 3846 (void)SvOK_off(sstr); /* NOTE: nukes most SvFLAGS on sstr */
79072805
LW
3847 SvPV_set(sstr, Nullch);
3848 SvLEN_set(sstr, 0);
a5f75d66
AD
3849 SvCUR_set(sstr, 0);
3850 SvTEMP_off(sstr);
79072805 3851 }
645c22ef 3852 else { /* have to copy actual string */
8990e307
LW
3853 STRLEN len = SvCUR(sstr);
3854
645c22ef 3855 SvGROW(dstr, len + 1); /* inlined from sv_setpvn */
8990e307
LW
3856 Move(SvPVX(sstr),SvPVX(dstr),len,char);
3857 SvCUR_set(dstr, len);
3858 *SvEND(dstr) = '\0';
a0d0e21e 3859 (void)SvPOK_only(dstr);
79072805 3860 }
9aa983d2 3861 if (sflags & SVf_UTF8)
a7cb1f99 3862 SvUTF8_on(dstr);
79072805 3863 /*SUPPRESS 560*/
8990e307 3864 if (sflags & SVp_NOK) {
3332b3c1
JH
3865 SvNOKp_on(dstr);
3866 if (sflags & SVf_NOK)
3867 SvFLAGS(dstr) |= SVf_NOK;
463ee0b2 3868 SvNVX(dstr) = SvNVX(sstr);
79072805 3869 }
8990e307 3870 if (sflags & SVp_IOK) {
3332b3c1
JH
3871 (void)SvIOKp_on(dstr);
3872 if (sflags & SVf_IOK)
3873 SvFLAGS(dstr) |= SVf_IOK;
2b1c7e3e 3874 if (sflags & SVf_IVisUV)
25da4f38 3875 SvIsUV_on(dstr);
463ee0b2 3876 SvIVX(dstr) = SvIVX(sstr);
79072805
LW
3877 }
3878 }
8990e307 3879 else if (sflags & SVp_IOK) {
3332b3c1
JH
3880 if (sflags & SVf_IOK)
3881 (void)SvIOK_only(dstr);
3882 else {
9cbac4c7
DM
3883 (void)SvOK_off(dstr);
3884 (void)SvIOKp_on(dstr);
3332b3c1
JH
3885 }
3886 /* XXXX Do we want to set IsUV for IV(ROK)? Be extra safe... */
2b1c7e3e 3887 if (sflags & SVf_IVisUV)
25da4f38 3888 SvIsUV_on(dstr);
3332b3c1
JH
3889 SvIVX(dstr) = SvIVX(sstr);
3890 if (sflags & SVp_NOK) {
3891 if (sflags & SVf_NOK)
3892 (void)SvNOK_on(dstr);
3893 else
3894 (void)SvNOKp_on(dstr);
3895 SvNVX(dstr) = SvNVX(sstr);
3896 }
3897 }
3898 else if (sflags & SVp_NOK) {
3899 if (sflags & SVf_NOK)
3900 (void)SvNOK_only(dstr);
3901 else {
9cbac4c7 3902 (void)SvOK_off(dstr);
3332b3c1
JH
3903 SvNOKp_on(dstr);
3904 }
3905 SvNVX(dstr) = SvNVX(sstr);
79072805
LW
3906 }
3907 else {
20408e3c 3908 if (dtype == SVt_PVGV) {
e476b1b5
GS
3909 if (ckWARN(WARN_MISC))
3910 Perl_warner(aTHX_ WARN_MISC, "Undefined value assigned to typeglob");
20408e3c
GS
3911 }
3912 else
3913 (void)SvOK_off(dstr);
a0d0e21e 3914 }
27c9684d
AP
3915 if (SvTAINTED(sstr))
3916 SvTAINT(dstr);
79072805
LW
3917}
3918
954c1994
GS
3919/*
3920=for apidoc sv_setsv_mg
3921
3922Like C<sv_setsv>, but also handles 'set' magic.
3923
3924=cut
3925*/
3926
79072805 3927void
864dbfa3 3928Perl_sv_setsv_mg(pTHX_ SV *dstr, register SV *sstr)
ef50df4b
GS
3929{
3930 sv_setsv(dstr,sstr);
3931 SvSETMAGIC(dstr);
3932}
3933
954c1994
GS
3934/*
3935=for apidoc sv_setpvn
3936
3937Copies a string into an SV. The C<len> parameter indicates the number of
3938bytes to be copied. Does not handle 'set' magic. See C<sv_setpvn_mg>.
3939
3940=cut
3941*/
3942
ef50df4b 3943void
864dbfa3 3944Perl_sv_setpvn(pTHX_ register SV *sv, register const char *ptr, register STRLEN len)
79072805 3945{
c6f8c383 3946 register char *dptr;
22c522df 3947
2213622d 3948 SV_CHECK_THINKFIRST(sv);
463ee0b2 3949 if (!ptr) {
a0d0e21e 3950 (void)SvOK_off(sv);
463ee0b2
LW
3951 return;
3952 }
22c522df
JH
3953 else {
3954 /* len is STRLEN which is unsigned, need to copy to signed */
3955 IV iv = len;
9c5ffd7c
JH
3956 if (iv < 0)
3957 Perl_croak(aTHX_ "panic: sv_setpvn called with negative strlen");
22c522df 3958 }
6fc92669 3959 (void)SvUPGRADE(sv, SVt_PV);
c6f8c383 3960
79072805 3961 SvGROW(sv, len + 1);
c6f8c383
GA
3962 dptr = SvPVX(sv);
3963 Move(ptr,dptr,len,char);
3964 dptr[len] = '\0';
79072805 3965 SvCUR_set(sv, len);
1aa99e6b 3966 (void)SvPOK_only_UTF8(sv); /* validate pointer */
463ee0b2 3967 SvTAINT(sv);
79072805
LW
3968}
3969
954c1994
GS
3970/*
3971=for apidoc sv_setpvn_mg
3972
3973Like C<sv_setpvn>, but also handles 'set' magic.
3974
3975=cut
3976*/
3977
79072805 3978void
864dbfa3 3979Perl_sv_setpvn_mg(pTHX_ register SV *sv, register const char *ptr, register STRLEN len)
ef50df4b
GS
3980{
3981 sv_setpvn(sv,ptr,len);
3982 SvSETMAGIC(sv);
3983}
3984
954c1994
GS
3985/*
3986=for apidoc sv_setpv
3987
3988Copies a string into an SV. The string must be null-terminated. Does not
3989handle 'set' magic. See C<sv_setpv_mg>.
3990
3991=cut
3992*/
3993
ef50df4b 3994void
864dbfa3 3995Perl_sv_setpv(pTHX_ register SV *sv, register const char *ptr)
79072805
LW
3996{
3997 register STRLEN len;
3998
2213622d 3999 SV_CHECK_THINKFIRST(sv);
463ee0b2 4000 if (!ptr) {
a0d0e21e 4001 (void)SvOK_off(sv);
463ee0b2
LW
4002 return;
4003 }
79072805 4004 len = strlen(ptr);
6fc92669 4005 (void)SvUPGRADE(sv, SVt_PV);
c6f8c383 4006
79072805 4007 SvGROW(sv, len + 1);
463ee0b2 4008 Move(ptr,SvPVX(sv),len+1,char);
79072805 4009 SvCUR_set(sv, len);
1aa99e6b 4010 (void)SvPOK_only_UTF8(sv); /* validate pointer */
463ee0b2
LW
4011 SvTAINT(sv);
4012}
4013
954c1994
GS
4014/*
4015=for apidoc sv_setpv_mg
4016
4017Like C<sv_setpv>, but also handles 'set' magic.
4018
4019=cut
4020*/
4021
463ee0b2 4022void
864dbfa3 4023Perl_sv_setpv_mg(pTHX_ register SV *sv, register const char *ptr)
ef50df4b
GS
4024{
4025 sv_setpv(sv,ptr);
4026 SvSETMAGIC(sv);
4027}
4028
954c1994
GS
4029/*
4030=for apidoc sv_usepvn
4031
4032Tells an SV to use C<ptr> to find its string value. Normally the string is
1c846c1f 4033stored inside the SV but sv_usepvn allows the SV to use an outside string.
954c1994
GS
4034The C<ptr> should point to memory that was allocated by C<malloc>. The
4035string length, C<len>, must be supplied. This function will realloc the
4036memory pointed to by C<ptr>, so that pointer should not be freed or used by
4037the programmer after giving it to sv_usepvn. Does not handle 'set' magic.
4038See C<sv_usepvn_mg>.
4039
4040=cut
4041*/
4042
ef50df4b 4043void
864dbfa3 4044Perl_sv_usepvn(pTHX_ register SV *sv, register char *ptr, register STRLEN len)
463ee0b2 4045{
2213622d 4046 SV_CHECK_THINKFIRST(sv);
c6f8c383 4047 (void)SvUPGRADE(sv, SVt_PV);
463ee0b2 4048 if (!ptr) {
a0d0e21e 4049 (void)SvOK_off(sv);
463ee0b2
LW
4050 return;
4051 }
a0ed51b3 4052 (void)SvOOK_off(sv);
50483b2c 4053 if (SvPVX(sv) && SvLEN(sv))
463ee0b2
LW
4054 Safefree(SvPVX(sv));
4055 Renew(ptr, len+1, char);
4056 SvPVX(sv) = ptr;
4057 SvCUR_set(sv, len);
4058 SvLEN_set(sv, len+1);
4059 *SvEND(sv) = '\0';
1aa99e6b 4060 (void)SvPOK_only_UTF8(sv); /* validate pointer */
463ee0b2 4061 SvTAINT(sv);
79072805
LW
4062}
4063
954c1994
GS
4064/*
4065=for apidoc sv_usepvn_mg
4066
4067Like C<sv_usepvn>, but also handles 'set' magic.
4068
4069=cut
4070*/
4071
ef50df4b 4072void
864dbfa3 4073Perl_sv_usepvn_mg(pTHX_ register SV *sv, register char *ptr, register STRLEN len)
ef50df4b 4074{
51c1089b 4075 sv_usepvn(sv,ptr,len);
ef50df4b
GS
4076 SvSETMAGIC(sv);
4077}
4078
645c22ef
DM
4079/*
4080=for apidoc sv_force_normal_flags
4081
4082Undo various types of fakery on an SV: if the PV is a shared string, make
4083a private copy; if we're a ref, stop refing; if we're a glob, downgrade to
4084an xpvmg. The C<flags> parameter gets passed to C<sv_unref_flags()>
4085when unrefing. C<sv_force_normal> calls this function with flags set to 0.
4086
4087=cut
4088*/
4089
6fc92669 4090void
840a7b70 4091Perl_sv_force_normal_flags(pTHX_ register SV *sv, U32 flags)
0f15f207 4092{
2213622d 4093 if (SvREADONLY(sv)) {
1c846c1f
NIS
4094 if (SvFAKE(sv)) {
4095 char *pvx = SvPVX(sv);
4096 STRLEN len = SvCUR(sv);
4097 U32 hash = SvUVX(sv);
4098 SvGROW(sv, len + 1);
4099 Move(pvx,SvPVX(sv),len,char);
4100 *SvEND(sv) = '\0';
4101 SvFAKE_off(sv);
4102 SvREADONLY_off(sv);
c3654f1a 4103 unsharepvn(pvx,SvUTF8(sv)?-len:len,hash);
1c846c1f
NIS
4104 }
4105 else if (PL_curcop != &PL_compiling)
cea2e8a9 4106 Perl_croak(aTHX_ PL_no_modify);
0f15f207 4107 }
2213622d 4108 if (SvROK(sv))
840a7b70 4109 sv_unref_flags(sv, flags);
6fc92669
GS
4110 else if (SvFAKE(sv) && SvTYPE(sv) == SVt_PVGV)
4111 sv_unglob(sv);
0f15f207 4112}
1c846c1f 4113
645c22ef
DM
4114/*
4115=for apidoc sv_force_normal
4116
4117Undo various types of fakery on an SV: if the PV is a shared string, make
4118a private copy; if we're a ref, stop refing; if we're a glob, downgrade to
4119an xpvmg. See also C<sv_force_normal_flags>.
4120
4121=cut
4122*/
4123
840a7b70
IZ
4124void
4125Perl_sv_force_normal(pTHX_ register SV *sv)
4126{
4127 sv_force_normal_flags(sv, 0);
4128}
4129
954c1994
GS
4130/*
4131=for apidoc sv_chop
4132
1c846c1f 4133Efficient removal of characters from the beginning of the string buffer.
954c1994
GS
4134SvPOK(sv) must be true and the C<ptr> must be a pointer to somewhere inside
4135the string buffer. The C<ptr> becomes the first character of the adjusted
645c22ef 4136string. Uses the "OOK hack".
954c1994
GS
4137
4138=cut
4139*/
4140
79072805 4141void
645c22ef 4142Perl_sv_chop(pTHX_ register SV *sv, register char *ptr)
79072805
LW
4143{
4144 register STRLEN delta;
4145
a0d0e21e 4146 if (!ptr || !SvPOKp(sv))
79072805 4147 return;
2213622d 4148 SV_CHECK_THINKFIRST(sv);
79072805
LW
4149 if (SvTYPE(sv) < SVt_PVIV)
4150 sv_upgrade(sv,SVt_PVIV);
4151
4152 if (!SvOOK(sv)) {
50483b2c
JD
4153 if (!SvLEN(sv)) { /* make copy of shared string */
4154 char *pvx = SvPVX(sv);
4155 STRLEN len = SvCUR(sv);
4156 SvGROW(sv, len + 1);
4157 Move(pvx,SvPVX(sv),len,char);
4158 *SvEND(sv) = '\0';
4159 }
463ee0b2 4160 SvIVX(sv) = 0;
79072805
LW
4161 SvFLAGS(sv) |= SVf_OOK;
4162 }
25da4f38 4163 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVp_IOK|SVp_NOK|SVf_IVisUV);
463ee0b2 4164 delta = ptr - SvPVX(sv);
79072805
LW
4165 SvLEN(sv) -= delta;
4166 SvCUR(sv) -= delta;
463ee0b2
LW
4167 SvPVX(sv) += delta;
4168 SvIVX(sv) += delta;
79072805
LW
4169}
4170
954c1994
GS
4171/*
4172=for apidoc sv_catpvn
4173
4174Concatenates the string onto the end of the string which is in the SV. The
d5ce4a7c
GA
4175C<len> indicates number of bytes to copy. If the SV has the UTF8
4176status set, then the bytes appended should be valid UTF8.
4177Handles 'get' magic, but not 'set' magic. See C<sv_catpvn_mg>.
954c1994
GS
4178
4179=cut
4180*/
4181
8d6d96c1
HS
4182/* sv_catpvn() is aliased to Perl_sv_catpvn_macro; this function provided
4183 for binary compatibility only
4184*/
79072805 4185void
8d6d96c1 4186Perl_sv_catpvn(pTHX_ SV *dsv, const char* sstr, STRLEN slen)
79072805 4187{
8d6d96c1
HS
4188 sv_catpvn_flags(dsv, sstr, slen, SV_GMAGIC);
4189}
a0d0e21e 4190
8d6d96c1
HS
4191/*
4192=for apidoc sv_catpvn_flags
4193
4194Concatenates the string onto the end of the string which is in the SV. The
4195C<len> indicates number of bytes to copy. If the SV has the UTF8
4196status set, then the bytes appended should be valid UTF8.
4197If C<flags> has C<SV_GMAGIC> bit set, will C<mg_get> on C<dsv> if
4198appropriate, else not. C<sv_catpvn> and C<sv_catpvn_nomg> are implemented
4199in terms of this function.
4200
4201=cut
4202*/
4203
4204void
4205Perl_sv_catpvn_flags(pTHX_ register SV *dsv, register const char *sstr, register STRLEN slen, I32 flags)
4206{
4207 STRLEN dlen;
4208 char *dstr;
4209
4210 dstr = SvPV_force_flags(dsv, dlen, flags);
4211 SvGROW(dsv, dlen + slen + 1);
4212 if (sstr == dstr)
4213 sstr = SvPVX(dsv);
4214 Move(sstr, SvPVX(dsv) + dlen, slen, char);
4215 SvCUR(dsv) += slen;
4216 *SvEND(dsv) = '\0';
4217 (void)SvPOK_only_UTF8(dsv); /* validate pointer */
4218 SvTAINT(dsv);
79072805
LW
4219}
4220
954c1994
GS
4221/*
4222=for apidoc sv_catpvn_mg
4223
4224Like C<sv_catpvn>, but also handles 'set' magic.
4225
4226=cut
4227*/
4228
79072805 4229void
864dbfa3 4230Perl_sv_catpvn_mg(pTHX_ register SV *sv, register const char *ptr, register STRLEN len)
ef50df4b
GS
4231{
4232 sv_catpvn(sv,ptr,len);
4233 SvSETMAGIC(sv);
4234}
4235
954c1994
GS
4236/*
4237=for apidoc sv_catsv
4238
13e8c8e3
JH
4239Concatenates the string from SV C<ssv> onto the end of the string in
4240SV C<dsv>. Modifies C<dsv> but not C<ssv>. Handles 'get' magic, but
4241not 'set' magic. See C<sv_catsv_mg>.
954c1994 4242
13e8c8e3 4243=cut */
954c1994 4244
8d6d96c1
HS
4245/* sv_catsv() is aliased to Perl_sv_catsv_macro; this function provided
4246 for binary compatibility only
4247*/
4248void
4249Perl_sv_catsv(pTHX_ SV *dstr, register SV *sstr)
4250{
4251 sv_catsv_flags(dstr, sstr, SV_GMAGIC);
4252}
4253
4254/*
4255=for apidoc sv_catsv_flags
4256
4257Concatenates the string from SV C<ssv> onto the end of the string in
4258SV C<dsv>. Modifies C<dsv> but not C<ssv>. If C<flags> has C<SV_GMAGIC>
4259bit set, will C<mg_get> on the SVs if appropriate, else not. C<sv_catsv>
4260and C<sv_catsv_nomg> are implemented in terms of this function.
4261
4262=cut */
4263
ef50df4b 4264void
8d6d96c1 4265Perl_sv_catsv_flags(pTHX_ SV *dsv, register SV *ssv, I32 flags)
79072805 4266{
13e8c8e3
JH
4267 char *spv;
4268 STRLEN slen;
46199a12 4269 if (!ssv)
79072805 4270 return;
46199a12 4271 if ((spv = SvPV(ssv, slen))) {
46199a12 4272 bool sutf8 = DO_UTF8(ssv);
8d6d96c1 4273 bool dutf8;
13e8c8e3 4274
8d6d96c1
HS
4275 if (SvGMAGICAL(dsv) && (flags & SV_GMAGIC))
4276 mg_get(dsv);
4277 dutf8 = DO_UTF8(dsv);
4278
4279 if (dutf8 != sutf8) {
13e8c8e3 4280 if (dutf8) {
46199a12 4281 /* Not modifying source SV, so taking a temporary copy. */
8d6d96c1 4282 SV* csv = sv_2mortal(newSVpvn(spv, slen));
13e8c8e3 4283
46199a12 4284 sv_utf8_upgrade(csv);
8d6d96c1 4285 spv = SvPV(csv, slen);
13e8c8e3 4286 }
8d6d96c1
HS
4287 else
4288 sv_utf8_upgrade_nomg(dsv);
e84ff256 4289 }
8d6d96c1 4290 sv_catpvn_nomg(dsv, spv, slen);
560a288e 4291 }
79072805
LW
4292}
4293
954c1994
GS
4294/*
4295=for apidoc sv_catsv_mg
4296
4297Like C<sv_catsv>, but also handles 'set' magic.
4298
4299=cut
4300*/
4301
79072805 4302void
46199a12 4303Perl_sv_catsv_mg(pTHX_ SV *dsv, register SV *ssv)
ef50df4b 4304{
46199a12
JH
4305 sv_catsv(dsv,ssv);
4306 SvSETMAGIC(dsv);
ef50df4b
GS
4307}
4308
954c1994
GS
4309/*
4310=for apidoc sv_catpv
4311
4312Concatenates the string onto the end of the string which is in the SV.
d5ce4a7c
GA
4313If the SV has the UTF8 status set, then the bytes appended should be
4314valid UTF8. Handles 'get' magic, but not 'set' magic. See C<sv_catpv_mg>.
954c1994 4315
d5ce4a7c 4316=cut */
954c1994 4317
ef50df4b 4318void
0c981600 4319Perl_sv_catpv(pTHX_ register SV *sv, register const char *ptr)
79072805
LW
4320{
4321 register STRLEN len;
463ee0b2 4322 STRLEN tlen;
748a9306 4323 char *junk;
79072805 4324
0c981600 4325 if (!ptr)
79072805 4326 return;
748a9306 4327 junk = SvPV_force(sv, tlen);
0c981600 4328 len = strlen(ptr);
463ee0b2 4329 SvGROW(sv, tlen + len + 1);
0c981600
JH
4330 if (ptr == junk)
4331 ptr = SvPVX(sv);
4332 Move(ptr,SvPVX(sv)+tlen,len+1,char);
79072805 4333 SvCUR(sv) += len;
d41ff1b8 4334 (void)SvPOK_only_UTF8(sv); /* validate pointer */
463ee0b2 4335 SvTAINT(sv);
79072805
LW
4336}
4337
954c1994
GS
4338/*
4339=for apidoc sv_catpv_mg
4340
4341Like C<sv_catpv>, but also handles 'set' magic.
4342
4343=cut
4344*/
4345
ef50df4b 4346void
0c981600 4347Perl_sv_catpv_mg(pTHX_ register SV *sv, register const char *ptr)
ef50df4b 4348{
0c981600 4349 sv_catpv(sv,ptr);
ef50df4b
GS
4350 SvSETMAGIC(sv);
4351}
4352
645c22ef
DM
4353/*
4354=for apidoc newSV
4355
4356Create a new null SV, or if len > 0, create a new empty SVt_PV type SV
4357with an initial PV allocation of len+1. Normally accessed via the C<NEWSV>
4358macro.
4359
4360=cut
4361*/
4362
79072805 4363SV *
864dbfa3 4364Perl_newSV(pTHX_ STRLEN len)
79072805
LW
4365{
4366 register SV *sv;
1c846c1f 4367
4561caa4 4368 new_SV(sv);
79072805
LW
4369 if (len) {
4370 sv_upgrade(sv, SVt_PV);
4371 SvGROW(sv, len + 1);
4372 }
4373 return sv;
4374}
4375
954c1994
GS
4376/*
4377=for apidoc sv_magic
4378
645c22ef
DM
4379Adds magic to an SV. First upgrades C<sv> to type C<SVt_PVMG> if necessary,
4380then adds a new magic item of type C<how> to the head of the magic list.
4381
4382C<name> is assumed to contain an C<SV*> if C<(name && namelen == HEf_SVKEY)>
954c1994
GS
4383
4384=cut
4385*/
4386
79072805 4387void
864dbfa3 4388Perl_sv_magic(pTHX_ register SV *sv, SV *obj, int how, const char *name, I32 namlen)
79072805
LW
4389{
4390 MAGIC* mg;
1c846c1f 4391
0f15f207 4392 if (SvREADONLY(sv)) {
14befaf4 4393 if (PL_curcop != &PL_compiling
14befaf4
DM
4394 && how != PERL_MAGIC_regex_global
4395 && how != PERL_MAGIC_bm
4396 && how != PERL_MAGIC_fm
6fa402ec 4397 && how != PERL_MAGIC_sv
14befaf4
DM
4398 )
4399 {
cea2e8a9 4400 Perl_croak(aTHX_ PL_no_modify);
14befaf4 4401 }
0f15f207 4402 }
14befaf4 4403 if (SvMAGICAL(sv) || (how == PERL_MAGIC_taint && SvTYPE(sv) >= SVt_PVMG)) {
748a9306 4404 if (SvMAGIC(sv) && (mg = mg_find(sv, how))) {
14befaf4 4405 if (how == PERL_MAGIC_taint)
565764a8 4406 mg->mg_len |= 1;
463ee0b2 4407 return;
748a9306 4408 }
463ee0b2
LW
4409 }
4410 else {
c6f8c383 4411 (void)SvUPGRADE(sv, SVt_PVMG);
463ee0b2 4412 }
79072805
LW
4413 Newz(702,mg, 1, MAGIC);
4414 mg->mg_moremagic = SvMAGIC(sv);
79072805 4415 SvMAGIC(sv) = mg;
75f9d97a
JH
4416
4417 /* Some magic sontains a reference loop, where the sv and object refer to
4418 each other. To prevent a avoid a reference loop that would prevent such
4419 objects being freed, we look for such loops and if we find one we avoid
4420 incrementing the object refcount. */
14befaf4
DM
4421 if (!obj || obj == sv ||
4422 how == PERL_MAGIC_arylen ||
4423 how == PERL_MAGIC_qr ||
75f9d97a
JH
4424 (SvTYPE(obj) == SVt_PVGV &&
4425 (GvSV(obj) == sv || GvHV(obj) == (HV*)sv || GvAV(obj) == (AV*)sv ||
4426 GvCV(obj) == (CV*)sv || GvIOp(obj) == (IO*)sv ||
4427 GvFORM(obj) == (CV*)sv)))
4428 {
8990e307 4429 mg->mg_obj = obj;
75f9d97a 4430 }
85e6fe83 4431 else {
8990e307 4432 mg->mg_obj = SvREFCNT_inc(obj);
85e6fe83
LW
4433 mg->mg_flags |= MGf_REFCOUNTED;
4434 }
79072805 4435 mg->mg_type = how;
565764a8 4436 mg->mg_len = namlen;
9cbac4c7 4437 if (name) {
1edc1566 4438 if (namlen >= 0)
4439 mg->mg_ptr = savepvn(name, namlen);
c6ee37c5 4440 else if (namlen == HEf_SVKEY)
1edc1566 4441 mg->mg_ptr = (char*)SvREFCNT_inc((SV*)name);
9cbac4c7 4442 }
1c846c1f 4443
79072805 4444 switch (how) {
14befaf4 4445 case PERL_MAGIC_sv:
22c35a8c 4446 mg->mg_virtual = &PL_vtbl_sv;
79072805 4447 break;
14befaf4 4448 case PERL_MAGIC_overload:
22c35a8c 4449 mg->mg_virtual = &PL_vtbl_amagic;
a0d0e21e 4450 break;
14befaf4 4451 case PERL_MAGIC_overload_elem:
22c35a8c 4452 mg->mg_virtual = &PL_vtbl_amagicelem;
a0d0e21e 4453 break;
14befaf4 4454 case PERL_MAGIC_overload_table:
d460ef45 4455 mg->mg_virtual = &PL_vtbl_ovrld;
a0d0e21e 4456 break;
14befaf4 4457 case PERL_MAGIC_bm:
22c35a8c 4458 mg->mg_virtual = &PL_vtbl_bm;
79072805 4459 break;
14befaf4 4460 case PERL_MAGIC_regdata:
22c35a8c 4461 mg->mg_virtual = &PL_vtbl_regdata;
6cef1e77 4462 break;
14befaf4 4463 case PERL_MAGIC_regdatum:
22c35a8c 4464 mg->mg_virtual = &PL_vtbl_regdatum;
6cef1e77 4465 break;
14befaf4 4466 case PERL_MAGIC_env:
22c35a8c 4467 mg->mg_virtual = &PL_vtbl_env;
79072805 4468 break;
14befaf4 4469 case PERL_MAGIC_fm:
22c35a8c 4470 mg->mg_virtual = &PL_vtbl_fm;
55497cff 4471 break;
14befaf4 4472 case PERL_MAGIC_envelem:
22c35a8c 4473 mg->mg_virtual = &PL_vtbl_envelem;
79072805 4474 break;
14befaf4 4475 case PERL_MAGIC_regex_global:
22c35a8c 4476 mg->mg_virtual = &PL_vtbl_mglob;
93a17b20 4477 break;
14befaf4 4478 case PERL_MAGIC_isa:
22c35a8c 4479 mg->mg_virtual = &PL_vtbl_isa;
463ee0b2 4480 break;
14befaf4 4481 case PERL_MAGIC_isaelem:
22c35a8c 4482 mg->mg_virtual = &PL_vtbl_isaelem;
463ee0b2 4483 break;
14befaf4 4484 case PERL_MAGIC_nkeys:
22c35a8c 4485 mg->mg_virtual = &PL_vtbl_nkeys;
16660edb 4486 break;
14befaf4 4487 case PERL_MAGIC_dbfile:
a0d0e21e 4488 SvRMAGICAL_on(sv);
93a17b20
LW
4489 mg->mg_virtual = 0;
4490 break;
14befaf4 4491 case PERL_MAGIC_dbline:
22c35a8c 4492 mg->mg_virtual = &PL_vtbl_dbline;
79072805 4493 break;
f93b4edd 4494#ifdef USE_THREADS
14befaf4 4495 case PERL_MAGIC_mutex:
22c35a8c 4496 mg->mg_virtual = &PL_vtbl_mutex;
f93b4edd
MB
4497 break;
4498#endif /* USE_THREADS */
36477c24 4499#ifdef USE_LOCALE_COLLATE
14befaf4 4500 case PERL_MAGIC_collxfrm:
22c35a8c 4501 mg->mg_virtual = &PL_vtbl_collxfrm;
bbce6d69 4502 break;
36477c24 4503#endif /* USE_LOCALE_COLLATE */
14befaf4 4504 case PERL_MAGIC_tied:
22c35a8c 4505 mg->mg_virtual = &PL_vtbl_pack;
463ee0b2 4506 break;
14befaf4
DM
4507 case PERL_MAGIC_tiedelem:
4508 case PERL_MAGIC_tiedscalar:
22c35a8c 4509 mg->mg_virtual = &PL_vtbl_packelem;
463ee0b2 4510 break;
14befaf4 4511 case PERL_MAGIC_qr:
22c35a8c 4512 mg->mg_virtual = &PL_vtbl_regexp;
c277df42 4513 break;
14befaf4 4514 case PERL_MAGIC_sig:
22c35a8c 4515 mg->mg_virtual = &PL_vtbl_sig;
79072805 4516 break;
14befaf4 4517 case PERL_MAGIC_sigelem:
22c35a8c 4518 mg->mg_virtual = &PL_vtbl_sigelem;
79072805 4519 break;
14befaf4 4520 case PERL_MAGIC_taint:
22c35a8c 4521 mg->mg_virtual = &PL_vtbl_taint;
565764a8 4522 mg->mg_len = 1;
463ee0b2 4523 break;
14befaf4 4524 case PERL_MAGIC_uvar:
22c35a8c 4525 mg->mg_virtual = &PL_vtbl_uvar;
79072805 4526 break;
14befaf4 4527 case PERL_MAGIC_vec:
22c35a8c 4528 mg->mg_virtual = &PL_vtbl_vec;
79072805 4529 break;
14befaf4 4530 case PERL_MAGIC_substr:
22c35a8c 4531 mg->mg_virtual = &PL_vtbl_substr;
79072805 4532 break;
14befaf4 4533 case PERL_MAGIC_defelem:
22c35a8c 4534 mg->mg_virtual = &PL_vtbl_defelem;
5f05dabc 4535 break;
14befaf4 4536 case PERL_MAGIC_glob:
22c35a8c 4537 mg->mg_virtual = &PL_vtbl_glob;
79072805 4538 break;
14befaf4 4539 case PERL_MAGIC_arylen:
22c35a8c 4540 mg->mg_virtual = &PL_vtbl_arylen;
79072805 4541 break;
14befaf4 4542 case PERL_MAGIC_pos:
22c35a8c 4543 mg->mg_virtual = &PL_vtbl_pos;
a0d0e21e 4544 break;
14befaf4 4545 case PERL_MAGIC_backref:
810b8aa5
GS
4546 mg->mg_virtual = &PL_vtbl_backref;
4547 break;
14befaf4
DM
4548 case PERL_MAGIC_ext:
4549 /* Reserved for use by extensions not perl internals. */
4633a7c4
LW
4550 /* Useful for attaching extension internal data to perl vars. */
4551 /* Note that multiple extensions may clash if magical scalars */
4552 /* etc holding private data from one are passed to another. */
4553 SvRMAGICAL_on(sv);
a0d0e21e 4554 break;
79072805 4555 default:
14befaf4 4556 Perl_croak(aTHX_ "Don't know how to handle magic of type \\%o", how);
463ee0b2 4557 }
8990e307
LW
4558 mg_magical(sv);
4559 if (SvGMAGICAL(sv))
4560 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK);
463ee0b2
LW
4561}
4562
c461cf8f
JH
4563/*
4564=for apidoc sv_unmagic
4565
645c22ef 4566Removes all magic of type C<type> from an SV.
c461cf8f
JH
4567
4568=cut
4569*/
4570
463ee0b2 4571int
864dbfa3 4572Perl_sv_unmagic(pTHX_ SV *sv, int type)
463ee0b2
LW
4573{
4574 MAGIC* mg;
4575 MAGIC** mgp;
91bba347 4576 if (SvTYPE(sv) < SVt_PVMG || !SvMAGIC(sv))
463ee0b2
LW
4577 return 0;
4578 mgp = &SvMAGIC(sv);
4579 for (mg = *mgp; mg; mg = *mgp) {
4580 if (mg->mg_type == type) {
4581 MGVTBL* vtbl = mg->mg_virtual;
4582 *mgp = mg->mg_moremagic;
1d7c1841 4583 if (vtbl && vtbl->svt_free)
fc0dc3b3 4584 CALL_FPTR(vtbl->svt_free)(aTHX_ sv, mg);
14befaf4 4585 if (mg->mg_ptr && mg->mg_type != PERL_MAGIC_regex_global) {
565764a8 4586 if (mg->mg_len >= 0)
1edc1566 4587 Safefree(mg->mg_ptr);
565764a8 4588 else if (mg->mg_len == HEf_SVKEY)
1edc1566 4589 SvREFCNT_dec((SV*)mg->mg_ptr);
9cbac4c7 4590 }
a0d0e21e
LW
4591 if (mg->mg_flags & MGf_REFCOUNTED)
4592 SvREFCNT_dec(mg->mg_obj);
463ee0b2
LW
4593 Safefree(mg);
4594 }
4595 else
4596 mgp = &mg->mg_moremagic;
79072805 4597 }
91bba347 4598 if (!SvMAGIC(sv)) {
463ee0b2 4599 SvMAGICAL_off(sv);
06759ea0 4600 SvFLAGS(sv) |= (SvFLAGS(sv) & (SVp_NOK|SVp_POK)) >> PRIVSHIFT;
463ee0b2
LW
4601 }
4602
4603 return 0;
79072805
LW
4604}
4605
c461cf8f
JH
4606/*
4607=for apidoc sv_rvweaken
4608
645c22ef
DM
4609Weaken a reference: set the C<SvWEAKREF> flag on this RV; give the
4610referred-to SV C<PERL_MAGIC_backref> magic if it hasn't already; and
4611push a back-reference to this RV onto the array of backreferences
4612associated with that magic.
c461cf8f
JH
4613
4614=cut
4615*/
4616
810b8aa5 4617SV *
864dbfa3 4618Perl_sv_rvweaken(pTHX_ SV *sv)
810b8aa5
GS
4619{
4620 SV *tsv;
4621 if (!SvOK(sv)) /* let undefs pass */
4622 return sv;
4623 if (!SvROK(sv))
cea2e8a9 4624 Perl_croak(aTHX_ "Can't weaken a nonreference");
810b8aa5 4625 else if (SvWEAKREF(sv)) {
810b8aa5 4626 if (ckWARN(WARN_MISC))
cea2e8a9 4627 Perl_warner(aTHX_ WARN_MISC, "Reference is already weak");
810b8aa5
GS
4628 return sv;
4629 }
4630 tsv = SvRV(sv);
4631 sv_add_backref(tsv, sv);
4632 SvWEAKREF_on(sv);
1c846c1f 4633 SvREFCNT_dec(tsv);
810b8aa5
GS
4634 return sv;
4635}
4636
645c22ef
DM
4637/* Give tsv backref magic if it hasn't already got it, then push a
4638 * back-reference to sv onto the array associated with the backref magic.
4639 */
4640
810b8aa5 4641STATIC void
cea2e8a9 4642S_sv_add_backref(pTHX_ SV *tsv, SV *sv)
810b8aa5
GS
4643{
4644 AV *av;
4645 MAGIC *mg;
14befaf4 4646 if (SvMAGICAL(tsv) && (mg = mg_find(tsv, PERL_MAGIC_backref)))
810b8aa5
GS
4647 av = (AV*)mg->mg_obj;
4648 else {
4649 av = newAV();
14befaf4 4650 sv_magic(tsv, (SV*)av, PERL_MAGIC_backref, NULL, 0);
810b8aa5
GS
4651 SvREFCNT_dec(av); /* for sv_magic */
4652 }
4653 av_push(av,sv);
4654}
4655
645c22ef
DM
4656/* delete a back-reference to ourselves from the backref magic associated
4657 * with the SV we point to.
4658 */
4659
1c846c1f 4660STATIC void
cea2e8a9 4661S_sv_del_backref(pTHX_ SV *sv)
810b8aa5
GS
4662{
4663 AV *av;
4664 SV **svp;
4665 I32 i;
4666 SV *tsv = SvRV(sv);
4667 MAGIC *mg;
14befaf4 4668 if (!SvMAGICAL(tsv) || !(mg = mg_find(tsv, PERL_MAGIC_backref)))
cea2e8a9 4669 Perl_croak(aTHX_ "panic: del_backref");
810b8aa5
GS
4670 av = (AV *)mg->mg_obj;
4671 svp = AvARRAY(av);
4672 i = AvFILLp(av);
4673 while (i >= 0) {
4674 if (svp[i] == sv) {
4675 svp[i] = &PL_sv_undef; /* XXX */
4676 }
4677 i--;
4678 }
4679}
4680
954c1994
GS
4681/*
4682=for apidoc sv_insert
4683
4684Inserts a string at the specified offset/length within the SV. Similar to
4685the Perl substr() function.
4686
4687=cut
4688*/
4689
79072805 4690void
864dbfa3 4691Perl_sv_insert(pTHX_ SV *bigstr, STRLEN offset, STRLEN len, char *little, STRLEN littlelen)
79072805
LW
4692{
4693 register char *big;
4694 register char *mid;
4695 register char *midend;
4696 register char *bigend;
4697 register I32 i;
6ff81951 4698 STRLEN curlen;
1c846c1f 4699
79072805 4700
8990e307 4701 if (!bigstr)
cea2e8a9 4702 Perl_croak(aTHX_ "Can't modify non-existent substring");
6ff81951 4703 SvPV_force(bigstr, curlen);
60fa28ff 4704 (void)SvPOK_only_UTF8(bigstr);
6ff81951
GS
4705 if (offset + len > curlen) {
4706 SvGROW(bigstr, offset+len+1);
4707 Zero(SvPVX(bigstr)+curlen, offset+len-curlen, char);
4708 SvCUR_set(bigstr, offset+len);
4709 }
79072805 4710
69b47968 4711 SvTAINT(bigstr);
79072805
LW
4712 i = littlelen - len;
4713 if (i > 0) { /* string might grow */
a0d0e21e 4714 big = SvGROW(bigstr, SvCUR(bigstr) + i + 1);
79072805
LW
4715 mid = big + offset + len;
4716 midend = bigend = big + SvCUR(bigstr);
4717 bigend += i;
4718 *bigend = '\0';
4719 while (midend > mid) /* shove everything down */
4720 *--bigend = *--midend;
4721 Move(little,big+offset,littlelen,char);
4722 SvCUR(bigstr) += i;
4723 SvSETMAGIC(bigstr);
4724 return;
4725 }
4726 else if (i == 0) {
463ee0b2 4727 Move(little,SvPVX(bigstr)+offset,len,char);
79072805
LW
4728 SvSETMAGIC(bigstr);
4729 return;
4730 }
4731
463ee0b2 4732 big = SvPVX(bigstr);
79072805
LW
4733 mid = big + offset;
4734 midend = mid + len;
4735 bigend = big + SvCUR(bigstr);
4736
4737 if (midend > bigend)
cea2e8a9 4738 Perl_croak(aTHX_ "panic: sv_insert");
79072805
LW
4739
4740 if (mid - big > bigend - midend) { /* faster to shorten from end */
4741 if (littlelen) {
4742 Move(little, mid, littlelen,char);
4743 mid += littlelen;
4744 }
4745 i = bigend - midend;
4746 if (i > 0) {
4747 Move(midend, mid, i,char);
4748 mid += i;
4749 }
4750 *mid = '\0';
4751 SvCUR_set(bigstr, mid - big);
4752 }
4753 /*SUPPRESS 560*/
155aba94 4754 else if ((i = mid - big)) { /* faster from front */
79072805
LW
4755 midend -= littlelen;
4756 mid = midend;
4757 sv_chop(bigstr,midend-i);
4758 big += i;
4759 while (i--)
4760 *--midend = *--big;
4761 if (littlelen)
4762 Move(little, mid, littlelen,char);
4763 }
4764 else if (littlelen) {
4765 midend -= littlelen;
4766 sv_chop(bigstr,midend);
4767 Move(little,midend,littlelen,char);
4768 }
4769 else {
4770 sv_chop(bigstr,midend);
4771 }
4772 SvSETMAGIC(bigstr);
4773}
4774
c461cf8f
JH
4775/*
4776=for apidoc sv_replace
4777
4778Make the first argument a copy of the second, then delete the original.
645c22ef
DM
4779The target SV physically takes over ownership of the body of the source SV
4780and inherits its flags; however, the target keeps any magic it owns,
4781and any magic in the source is discarded.
4782Note that this a rather specialist SV copying operation; most of the
4783time you'll want to use C<sv_setsv> or one of its many macro front-ends.
c461cf8f
JH
4784
4785=cut
4786*/
79072805
LW
4787
4788void
864dbfa3 4789Perl_sv_replace(pTHX_ register SV *sv, register SV *nsv)
79072805
LW
4790{
4791 U32 refcnt = SvREFCNT(sv);
2213622d 4792 SV_CHECK_THINKFIRST(sv);
0453d815
PM
4793 if (SvREFCNT(nsv) != 1 && ckWARN_d(WARN_INTERNAL))
4794 Perl_warner(aTHX_ WARN_INTERNAL, "Reference miscount in sv_replace()");
93a17b20 4795 if (SvMAGICAL(sv)) {
a0d0e21e
LW
4796 if (SvMAGICAL(nsv))
4797 mg_free(nsv);
4798 else
4799 sv_upgrade(nsv, SVt_PVMG);
93a17b20 4800 SvMAGIC(nsv) = SvMAGIC(sv);
a0d0e21e 4801 SvFLAGS(nsv) |= SvMAGICAL(sv);
93a17b20
LW
4802 SvMAGICAL_off(sv);
4803 SvMAGIC(sv) = 0;
4804 }
79072805
LW
4805 SvREFCNT(sv) = 0;
4806 sv_clear(sv);
477f5d66 4807 assert(!SvREFCNT(sv));
79072805
LW
4808 StructCopy(nsv,sv,SV);
4809 SvREFCNT(sv) = refcnt;
1edc1566 4810 SvFLAGS(nsv) |= SVTYPEMASK; /* Mark as freed */
463ee0b2 4811 del_SV(nsv);
79072805
LW
4812}
4813
c461cf8f
JH
4814/*
4815=for apidoc sv_clear
4816
645c22ef
DM
4817Clear an SV: call any destructors, free up any memory used by the body,
4818and free the body itself. The SV's head is I<not> freed, although
4819its type is set to all 1's so that it won't inadvertently be assumed
4820to be live during global destruction etc.
4821This function should only be called when REFCNT is zero. Most of the time
4822you'll want to call C<sv_free()> (or its macro wrapper C<SvREFCNT_dec>)
4823instead.
c461cf8f
JH
4824
4825=cut
4826*/
4827
79072805 4828void
864dbfa3 4829Perl_sv_clear(pTHX_ register SV *sv)
79072805 4830{
ec12f114 4831 HV* stash;
79072805
LW
4832 assert(sv);
4833 assert(SvREFCNT(sv) == 0);
4834
ed6116ce 4835 if (SvOBJECT(sv)) {
3280af22 4836 if (PL_defstash) { /* Still have a symbol table? */
39644a26 4837 dSP;
32251b26 4838 CV* destructor;
837485b6 4839 SV tmpref;
a0d0e21e 4840
837485b6
GS
4841 Zero(&tmpref, 1, SV);
4842 sv_upgrade(&tmpref, SVt_RV);
4843 SvROK_on(&tmpref);
4844 SvREADONLY_on(&tmpref); /* DESTROY() could be naughty */
4845 SvREFCNT(&tmpref) = 1;
8ebc5c01 4846
d460ef45 4847 do {
4e8e7886 4848 stash = SvSTASH(sv);
32251b26 4849 destructor = StashHANDLER(stash,DESTROY);
4e8e7886
GS
4850 if (destructor) {
4851 ENTER;
e788e7d3 4852 PUSHSTACKi(PERLSI_DESTROY);
837485b6 4853 SvRV(&tmpref) = SvREFCNT_inc(sv);
4e8e7886
GS
4854 EXTEND(SP, 2);
4855 PUSHMARK(SP);
837485b6 4856 PUSHs(&tmpref);
4e8e7886 4857 PUTBACK;
32251b26 4858 call_sv((SV*)destructor, G_DISCARD|G_EVAL|G_KEEPERR);
4e8e7886 4859 SvREFCNT(sv)--;
d3acc0f7 4860 POPSTACK;
3095d977 4861 SPAGAIN;
4e8e7886
GS
4862 LEAVE;
4863 }
4864 } while (SvOBJECT(sv) && SvSTASH(sv) != stash);
8ebc5c01 4865
837485b6 4866 del_XRV(SvANY(&tmpref));
6f44e0a4
JP
4867
4868 if (SvREFCNT(sv)) {
4869 if (PL_in_clean_objs)
cea2e8a9 4870 Perl_croak(aTHX_ "DESTROY created new reference to dead object '%s'",
6f44e0a4
JP
4871 HvNAME(stash));
4872 /* DESTROY gave object new lease on life */
4873 return;
4874 }
a0d0e21e 4875 }
4e8e7886 4876
a0d0e21e 4877 if (SvOBJECT(sv)) {
4e8e7886 4878 SvREFCNT_dec(SvSTASH(sv)); /* possibly of changed persuasion */
a0d0e21e
LW
4879 SvOBJECT_off(sv); /* Curse the object. */
4880 if (SvTYPE(sv) != SVt_PVIO)
3280af22 4881 --PL_sv_objcount; /* XXX Might want something more general */
a0d0e21e 4882 }
463ee0b2 4883 }
524189f1
JH
4884 if (SvTYPE(sv) >= SVt_PVMG) {
4885 if (SvMAGIC(sv))
4886 mg_free(sv);
4887 if (SvFLAGS(sv) & SVpad_TYPED)
4888 SvREFCNT_dec(SvSTASH(sv));
4889 }
ec12f114 4890 stash = NULL;
79072805 4891 switch (SvTYPE(sv)) {
8990e307 4892 case SVt_PVIO:
df0bd2f4
GS
4893 if (IoIFP(sv) &&
4894 IoIFP(sv) != PerlIO_stdin() &&
5f05dabc 4895 IoIFP(sv) != PerlIO_stdout() &&
4896 IoIFP(sv) != PerlIO_stderr())
93578b34 4897 {
f2b5be74 4898 io_close((IO*)sv, FALSE);
93578b34 4899 }
1d7c1841 4900 if (IoDIRP(sv) && !(IoFLAGS(sv) & IOf_FAKE_DIRP))
1236053a 4901 PerlDir_close(IoDIRP(sv));
1d7c1841 4902 IoDIRP(sv) = (DIR*)NULL;
8990e307
LW
4903 Safefree(IoTOP_NAME(sv));
4904 Safefree(IoFMT_NAME(sv));
4905 Safefree(IoBOTTOM_NAME(sv));
a0d0e21e 4906 /* FALL THROUGH */
79072805 4907 case SVt_PVBM:
a0d0e21e 4908 goto freescalar;
79072805 4909 case SVt_PVCV:
748a9306 4910 case SVt_PVFM:
85e6fe83 4911 cv_undef((CV*)sv);
a0d0e21e 4912 goto freescalar;
79072805 4913 case SVt_PVHV:
85e6fe83 4914 hv_undef((HV*)sv);
a0d0e21e 4915 break;
79072805 4916 case SVt_PVAV:
85e6fe83 4917 av_undef((AV*)sv);
a0d0e21e 4918 break;
02270b4e
GS
4919 case SVt_PVLV:
4920 SvREFCNT_dec(LvTARG(sv));
4921 goto freescalar;
a0d0e21e 4922 case SVt_PVGV:
1edc1566 4923 gp_free((GV*)sv);
a0d0e21e 4924 Safefree(GvNAME(sv));
ec12f114
JPC
4925 /* cannot decrease stash refcount yet, as we might recursively delete
4926 ourselves when the refcnt drops to zero. Delay SvREFCNT_dec
4927 of stash until current sv is completely gone.
4928 -- JohnPC, 27 Mar 1998 */
4929 stash = GvSTASH(sv);
a0d0e21e 4930 /* FALL THROUGH */
79072805 4931 case SVt_PVMG:
79072805
LW
4932 case SVt_PVNV:
4933 case SVt_PVIV:
a0d0e21e
LW
4934 freescalar:
4935 (void)SvOOK_off(sv);
79072805
LW
4936 /* FALL THROUGH */
4937 case SVt_PV:
a0d0e21e 4938 case SVt_RV:
810b8aa5
GS
4939 if (SvROK(sv)) {
4940 if (SvWEAKREF(sv))
4941 sv_del_backref(sv);
4942 else
4943 SvREFCNT_dec(SvRV(sv));
4944 }
1edc1566 4945 else if (SvPVX(sv) && SvLEN(sv))
463ee0b2 4946 Safefree(SvPVX(sv));
1c846c1f 4947 else if (SvPVX(sv) && SvREADONLY(sv) && SvFAKE(sv)) {
c3654f1a 4948 unsharepvn(SvPVX(sv),SvUTF8(sv)?-SvCUR(sv):SvCUR(sv),SvUVX(sv));
1c846c1f
NIS
4949 SvFAKE_off(sv);
4950 }
79072805 4951 break;
a0d0e21e 4952/*
79072805 4953 case SVt_NV:
79072805 4954 case SVt_IV:
79072805
LW
4955 case SVt_NULL:
4956 break;
a0d0e21e 4957*/
79072805
LW
4958 }
4959
4960 switch (SvTYPE(sv)) {
4961 case SVt_NULL:
4962 break;
79072805
LW
4963 case SVt_IV:
4964 del_XIV(SvANY(sv));
4965 break;
4966 case SVt_NV:
4967 del_XNV(SvANY(sv));
4968 break;
ed6116ce
LW
4969 case SVt_RV:
4970 del_XRV(SvANY(sv));
4971 break;
79072805
LW
4972 case SVt_PV:
4973 del_XPV(SvANY(sv));
4974 break;
4975 case SVt_PVIV:
4976 del_XPVIV(SvANY(sv));
4977 break;
4978 case SVt_PVNV:
4979 del_XPVNV(SvANY(sv));
4980 break;
4981 case SVt_PVMG:
4982 del_XPVMG(SvANY(sv));
4983 break;
4984 case SVt_PVLV:
4985 del_XPVLV(SvANY(sv));
4986 break;
4987 case SVt_PVAV:
4988 del_XPVAV(SvANY(sv));
4989 break;
4990 case SVt_PVHV:
4991 del_XPVHV(SvANY(sv));
4992 break;
4993 case SVt_PVCV:
4994 del_XPVCV(SvANY(sv));
4995 break;
4996 case SVt_PVGV:
4997 del_XPVGV(SvANY(sv));
ec12f114
JPC
4998 /* code duplication for increased performance. */
4999 SvFLAGS(sv) &= SVf_BREAK;
5000 SvFLAGS(sv) |= SVTYPEMASK;
5001 /* decrease refcount of the stash that owns this GV, if any */
5002 if (stash)
5003 SvREFCNT_dec(stash);
5004 return; /* not break, SvFLAGS reset already happened */
79072805
LW
5005 case SVt_PVBM:
5006 del_XPVBM(SvANY(sv));
5007 break;
5008 case SVt_PVFM:
5009 del_XPVFM(SvANY(sv));
5010 break;
8990e307
LW
5011 case SVt_PVIO:
5012 del_XPVIO(SvANY(sv));
5013 break;
79072805 5014 }
a0d0e21e 5015 SvFLAGS(sv) &= SVf_BREAK;
8990e307 5016 SvFLAGS(sv) |= SVTYPEMASK;
79072805
LW
5017}
5018
645c22ef
DM
5019/*
5020=for apidoc sv_newref
5021
5022Increment an SV's reference count. Use the C<SvREFCNT_inc()> wrapper
5023instead.
5024
5025=cut
5026*/
5027
79072805 5028SV *
864dbfa3 5029Perl_sv_newref(pTHX_ SV *sv)
79072805 5030{
463ee0b2 5031 if (sv)
dce16143 5032 ATOMIC_INC(SvREFCNT(sv));
79072805
LW
5033 return sv;
5034}
5035
c461cf8f
JH
5036/*
5037=for apidoc sv_free
5038
645c22ef
DM
5039Decrement an SV's reference count, and if it drops to zero, call
5040C<sv_clear> to invoke destructors and free up any memory used by
5041the body; finally, deallocate the SV's head itself.
5042Normally called via a wrapper macro C<SvREFCNT_dec>.
c461cf8f
JH
5043
5044=cut
5045*/
5046
79072805 5047void
864dbfa3 5048Perl_sv_free(pTHX_ SV *sv)
79072805 5049{
dce16143
MB
5050 int refcount_is_zero;
5051
79072805
LW
5052 if (!sv)
5053 return;
a0d0e21e
LW
5054 if (SvREFCNT(sv) == 0) {
5055 if (SvFLAGS(sv) & SVf_BREAK)
645c22ef
DM
5056 /* this SV's refcnt has been artificially decremented to
5057 * trigger cleanup */
a0d0e21e 5058 return;
3280af22 5059 if (PL_in_clean_all) /* All is fair */
1edc1566 5060 return;
d689ffdd
JP
5061 if (SvREADONLY(sv) && SvIMMORTAL(sv)) {
5062 /* make sure SvREFCNT(sv)==0 happens very seldom */
5063 SvREFCNT(sv) = (~(U32)0)/2;
5064 return;
5065 }
0453d815
PM
5066 if (ckWARN_d(WARN_INTERNAL))
5067 Perl_warner(aTHX_ WARN_INTERNAL, "Attempt to free unreferenced scalar");
79072805
LW
5068 return;
5069 }
dce16143
MB
5070 ATOMIC_DEC_AND_TEST(refcount_is_zero, SvREFCNT(sv));
5071 if (!refcount_is_zero)
8990e307 5072 return;
463ee0b2
LW
5073#ifdef DEBUGGING
5074 if (SvTEMP(sv)) {
0453d815 5075 if (ckWARN_d(WARN_DEBUGGING))
f248d071 5076 Perl_warner(aTHX_ WARN_DEBUGGING,
1d7c1841
GS
5077 "Attempt to free temp prematurely: SV 0x%"UVxf,
5078 PTR2UV(sv));
79072805 5079 return;
79072805 5080 }
463ee0b2 5081#endif
d689ffdd
JP
5082 if (SvREADONLY(sv) && SvIMMORTAL(sv)) {
5083 /* make sure SvREFCNT(sv)==0 happens very seldom */
5084 SvREFCNT(sv) = (~(U32)0)/2;
5085 return;
5086 }
79072805 5087 sv_clear(sv);
477f5d66
CS
5088 if (! SvREFCNT(sv))
5089 del_SV(sv);
79072805
LW
5090}
5091
954c1994
GS
5092/*
5093=for apidoc sv_len
5094
645c22ef
DM
5095Returns the length of the string in the SV. Handles magic and type
5096coercion. See also C<SvCUR>, which gives raw access to the xpv_cur slot.
954c1994
GS
5097
5098=cut
5099*/
5100
79072805 5101STRLEN
864dbfa3 5102Perl_sv_len(pTHX_ register SV *sv)
79072805 5103{
748a9306 5104 char *junk;
463ee0b2 5105 STRLEN len;
79072805
LW
5106
5107 if (!sv)
5108 return 0;
5109
8990e307 5110 if (SvGMAGICAL(sv))
565764a8 5111 len = mg_length(sv);
8990e307 5112 else
748a9306 5113 junk = SvPV(sv, len);
463ee0b2 5114 return len;
79072805
LW
5115}
5116
c461cf8f
JH
5117/*
5118=for apidoc sv_len_utf8
5119
5120Returns the number of characters in the string in an SV, counting wide
645c22ef 5121UTF8 bytes as a single character. Handles magic and type coercion.
c461cf8f
JH
5122
5123=cut
5124*/
5125
a0ed51b3 5126STRLEN
864dbfa3 5127Perl_sv_len_utf8(pTHX_ register SV *sv)
a0ed51b3 5128{
a0ed51b3
LW
5129 if (!sv)
5130 return 0;
5131
a0ed51b3 5132 if (SvGMAGICAL(sv))
b76347f2 5133 return mg_length(sv);
a0ed51b3 5134 else
b76347f2
JH
5135 {
5136 STRLEN len;
5137 U8 *s = (U8*)SvPV(sv, len);
5138
d6efbbad 5139 return Perl_utf8_length(aTHX_ s, s + len);
a0ed51b3 5140 }
a0ed51b3
LW
5141}
5142
645c22ef
DM
5143/*
5144=for apidoc sv_pos_u2b
5145
5146Converts the value pointed to by offsetp from a count of UTF8 chars from
5147the start of the string, to a count of the equivalent number of bytes; if
5148lenp is non-zero, it does the same to lenp, but this time starting from
5149the offset, rather than from the start of the string. Handles magic and
5150type coercion.
5151
5152=cut
5153*/
5154
a0ed51b3 5155void
864dbfa3 5156Perl_sv_pos_u2b(pTHX_ register SV *sv, I32* offsetp, I32* lenp)
a0ed51b3 5157{
dfe13c55
GS
5158 U8 *start;
5159 U8 *s;
5160 U8 *send;
a0ed51b3
LW
5161 I32 uoffset = *offsetp;
5162 STRLEN len;
5163
5164 if (!sv)
5165 return;
5166
dfe13c55 5167 start = s = (U8*)SvPV(sv, len);
a0ed51b3
LW
5168 send = s + len;
5169 while (s < send && uoffset--)
5170 s += UTF8SKIP(s);
bb40f870
GA
5171 if (s >= send)
5172 s = send;
a0ed51b3
LW
5173 *offsetp = s - start;
5174 if (lenp) {
5175 I32 ulen = *lenp;
5176 start = s;
5177 while (s < send && ulen--)
5178 s += UTF8SKIP(s);
bb40f870
GA
5179 if (s >= send)
5180 s = send;
a0ed51b3
LW
5181 *lenp = s - start;
5182 }
5183 return;
5184}
5185
645c22ef
DM
5186/*
5187=for apidoc sv_pos_b2u
5188
5189Converts the value pointed to by offsetp from a count of bytes from the
5190start of the string, to a count of the equivalent number of UTF8 chars.
5191Handles magic and type coercion.
5192
5193=cut
5194*/
5195
a0ed51b3 5196void
864dbfa3 5197Perl_sv_pos_b2u(pTHX_ register SV *sv, I32* offsetp)
a0ed51b3 5198{
dfe13c55
GS
5199 U8 *s;
5200 U8 *send;
a0ed51b3
LW
5201 STRLEN len;
5202
5203 if (!sv)
5204 return;
5205
dfe13c55 5206 s = (U8*)SvPV(sv, len);
a0ed51b3 5207 if (len < *offsetp)
a0dbb045 5208 Perl_croak(aTHX_ "panic: sv_pos_b2u: bad byte offset");
a0ed51b3
LW
5209 send = s + *offsetp;
5210 len = 0;
5211 while (s < send) {
a0dbb045 5212 STRLEN n;
2b9d42f0
NIS
5213 /* Call utf8n_to_uvchr() to validate the sequence */
5214 utf8n_to_uvchr(s, UTF8SKIP(s), &n, 0);
5215 if (n > 0) {
a0dbb045
JH
5216 s += n;
5217 len++;
5218 }
5219 else
5220 break;
a0ed51b3
LW
5221 }
5222 *offsetp = len;
5223 return;
5224}
5225
954c1994
GS
5226/*
5227=for apidoc sv_eq
5228
5229Returns a boolean indicating whether the strings in the two SVs are
645c22ef
DM
5230identical. Is UTF-8 and 'use bytes' aware, handles get magic, and will
5231coerce its args to strings if necessary.
954c1994
GS
5232
5233=cut
5234*/
5235
79072805 5236I32
e01b9e88 5237Perl_sv_eq(pTHX_ register SV *sv1, register SV *sv2)
79072805
LW
5238{
5239 char *pv1;
463ee0b2 5240 STRLEN cur1;
79072805 5241 char *pv2;
463ee0b2 5242 STRLEN cur2;
e01b9e88 5243 I32 eq = 0;
db42d148 5244 char *tpv = Nullch;
79072805 5245
e01b9e88 5246 if (!sv1) {
79072805
LW
5247 pv1 = "";
5248 cur1 = 0;
5249 }
463ee0b2 5250 else
e01b9e88 5251 pv1 = SvPV(sv1, cur1);
79072805 5252
e01b9e88
SC
5253 if (!sv2){
5254 pv2 = "";
5255 cur2 = 0;
92d29cee 5256 }
e01b9e88
SC
5257 else
5258 pv2 = SvPV(sv2, cur2);
79072805 5259
e01b9e88 5260 /* do not utf8ize the comparands as a side-effect */
0064a8a9 5261 if (cur1 && cur2 && SvUTF8(sv1) != SvUTF8(sv2) && !IN_BYTES) {
f9a63242 5262 bool is_utf8 = TRUE;
db42d148 5263 /* UTF-8ness differs */
1aa99e6b
IH
5264 if (PL_hints & HINT_UTF8_DISTINCT)
5265 return FALSE;
5266
e01b9e88 5267 if (SvUTF8(sv1)) {
db42d148 5268 /* sv1 is the UTF-8 one , If is equal it must be downgrade-able */
f34ff0a8 5269 char *pv = (char*)bytes_from_utf8((U8*)pv1, &cur1, &is_utf8);
db42d148
NIS
5270 if (pv != pv1)
5271 pv1 = tpv = pv;
e01b9e88
SC
5272 }
5273 else {
db42d148 5274 /* sv2 is the UTF-8 one , If is equal it must be downgrade-able */
f34ff0a8 5275 char *pv = (char *)bytes_from_utf8((U8*)pv2, &cur2, &is_utf8);
db42d148
NIS
5276 if (pv != pv2)
5277 pv2 = tpv = pv;
5278 }
5279 if (is_utf8) {
5280 /* Downgrade not possible - cannot be eq */
5281 return FALSE;
e01b9e88
SC
5282 }
5283 }
79072805 5284
e01b9e88
SC
5285 if (cur1 == cur2)
5286 eq = memEQ(pv1, pv2, cur1);
5287
db42d148
NIS
5288 if (tpv != Nullch)
5289 Safefree(tpv);
e01b9e88
SC
5290
5291 return eq;
79072805
LW
5292}
5293
954c1994
GS
5294/*
5295=for apidoc sv_cmp
5296
5297Compares the strings in two SVs. Returns -1, 0, or 1 indicating whether the
5298string in C<sv1> is less than, equal to, or greater than the string in
645c22ef
DM
5299C<sv2>. Is UTF-8 and 'use bytes' aware, handles get magic, and will
5300coerce its args to strings if necessary. See also C<sv_cmp_locale>.
954c1994
GS
5301
5302=cut
5303*/
5304
79072805 5305I32
e01b9e88 5306Perl_sv_cmp(pTHX_ register SV *sv1, register SV *sv2)
79072805 5307{
560a288e
GS
5308 STRLEN cur1, cur2;
5309 char *pv1, *pv2;
1c846c1f 5310 I32 cmp;
e01b9e88
SC
5311 bool pv1tmp = FALSE;
5312 bool pv2tmp = FALSE;
560a288e 5313
e01b9e88
SC
5314 if (!sv1) {
5315 pv1 = "";
560a288e
GS
5316 cur1 = 0;
5317 }
e01b9e88
SC
5318 else
5319 pv1 = SvPV(sv1, cur1);
560a288e 5320
e01b9e88
SC
5321 if (!sv2){
5322 pv2 = "";
560a288e
GS
5323 cur2 = 0;
5324 }
e01b9e88
SC
5325 else
5326 pv2 = SvPV(sv2, cur2);
79072805 5327
e01b9e88 5328 /* do not utf8ize the comparands as a side-effect */
0064a8a9 5329 if (cur1 && cur2 && SvUTF8(sv1) != SvUTF8(sv2) && !IN_BYTES) {
1aa99e6b
IH
5330 if (PL_hints & HINT_UTF8_DISTINCT)
5331 return SvUTF8(sv1) ? 1 : -1;
5332
e01b9e88
SC
5333 if (SvUTF8(sv1)) {
5334 pv2 = (char*)bytes_to_utf8((U8*)pv2, &cur2);
5335 pv2tmp = TRUE;
5336 }
5337 else {
5338 pv1 = (char*)bytes_to_utf8((U8*)pv1, &cur1);
5339 pv1tmp = TRUE;
5340 }
5341 }
79072805 5342
e01b9e88
SC
5343 if (!cur1) {
5344 cmp = cur2 ? -1 : 0;
5345 } else if (!cur2) {
5346 cmp = 1;
5347 } else {
5348 I32 retval = memcmp((void*)pv1, (void*)pv2, cur1 < cur2 ? cur1 : cur2);
5349
5350 if (retval) {
5351 cmp = retval < 0 ? -1 : 1;
5352 } else if (cur1 == cur2) {
5353 cmp = 0;
5354 } else {
5355 cmp = cur1 < cur2 ? -1 : 1;
5356 }
5357 }
16660edb 5358
e01b9e88
SC
5359 if (pv1tmp)
5360 Safefree(pv1);
5361 if (pv2tmp)
5362 Safefree(pv2);
16660edb 5363
e01b9e88 5364 return cmp;
bbce6d69 5365}
16660edb 5366
c461cf8f
JH
5367/*
5368=for apidoc sv_cmp_locale
5369
645c22ef
DM
5370Compares the strings in two SVs in a locale-aware manner. Is UTF-8 and
5371'use bytes' aware, handles get magic, and will coerce its args to strings
5372if necessary. See also C<sv_cmp_locale>. See also C<sv_cmp>.
c461cf8f
JH
5373
5374=cut
5375*/
5376
bbce6d69 5377I32
864dbfa3 5378Perl_sv_cmp_locale(pTHX_ register SV *sv1, register SV *sv2)
bbce6d69 5379{
36477c24 5380#ifdef USE_LOCALE_COLLATE
16660edb 5381
bbce6d69 5382 char *pv1, *pv2;
5383 STRLEN len1, len2;
5384 I32 retval;
16660edb 5385
3280af22 5386 if (PL_collation_standard)
bbce6d69 5387 goto raw_compare;
16660edb 5388
bbce6d69 5389 len1 = 0;
8ac85365 5390 pv1 = sv1 ? sv_collxfrm(sv1, &len1) : (char *) NULL;
bbce6d69 5391 len2 = 0;
8ac85365 5392 pv2 = sv2 ? sv_collxfrm(sv2, &len2) : (char *) NULL;
16660edb 5393
bbce6d69 5394 if (!pv1 || !len1) {
5395 if (pv2 && len2)
5396 return -1;
5397 else
5398 goto raw_compare;
5399 }
5400 else {
5401 if (!pv2 || !len2)
5402 return 1;
5403 }
16660edb 5404
bbce6d69 5405 retval = memcmp((void*)pv1, (void*)pv2, len1 < len2 ? len1 : len2);
16660edb 5406
bbce6d69 5407 if (retval)
16660edb 5408 return retval < 0 ? -1 : 1;
5409
bbce6d69 5410 /*
5411 * When the result of collation is equality, that doesn't mean
5412 * that there are no differences -- some locales exclude some
5413 * characters from consideration. So to avoid false equalities,
5414 * we use the raw string as a tiebreaker.
5415 */
16660edb 5416
bbce6d69 5417 raw_compare:
5418 /* FALL THROUGH */
16660edb 5419
36477c24 5420#endif /* USE_LOCALE_COLLATE */
16660edb 5421
bbce6d69 5422 return sv_cmp(sv1, sv2);
5423}
79072805 5424
645c22ef 5425
36477c24 5426#ifdef USE_LOCALE_COLLATE
645c22ef 5427
7a4c00b4 5428/*
645c22ef
DM
5429=for apidoc sv_collxfrm
5430
5431Add Collate Transform magic to an SV if it doesn't already have it.
5432
5433Any scalar variable may carry PERL_MAGIC_collxfrm magic that contains the
5434scalar data of the variable, but transformed to such a format that a normal
5435memory comparison can be used to compare the data according to the locale
5436settings.
5437
5438=cut
5439*/
5440
bbce6d69 5441char *
864dbfa3 5442Perl_sv_collxfrm(pTHX_ SV *sv, STRLEN *nxp)
bbce6d69 5443{
7a4c00b4 5444 MAGIC *mg;
16660edb 5445
14befaf4 5446 mg = SvMAGICAL(sv) ? mg_find(sv, PERL_MAGIC_collxfrm) : (MAGIC *) NULL;
3280af22 5447 if (!mg || !mg->mg_ptr || *(U32*)mg->mg_ptr != PL_collation_ix) {
bbce6d69 5448 char *s, *xf;
5449 STRLEN len, xlen;
5450
7a4c00b4 5451 if (mg)
5452 Safefree(mg->mg_ptr);
bbce6d69 5453 s = SvPV(sv, len);
5454 if ((xf = mem_collxfrm(s, len, &xlen))) {
ff0cee69 5455 if (SvREADONLY(sv)) {
5456 SAVEFREEPV(xf);
5457 *nxp = xlen;
3280af22 5458 return xf + sizeof(PL_collation_ix);
ff0cee69 5459 }
7a4c00b4 5460 if (! mg) {
14befaf4
DM
5461 sv_magic(sv, 0, PERL_MAGIC_collxfrm, 0, 0);
5462 mg = mg_find(sv, PERL_MAGIC_collxfrm);
7a4c00b4 5463 assert(mg);
bbce6d69 5464 }
7a4c00b4 5465 mg->mg_ptr = xf;
565764a8 5466 mg->mg_len = xlen;
7a4c00b4 5467 }
5468 else {
ff0cee69 5469 if (mg) {
5470 mg->mg_ptr = NULL;
565764a8 5471 mg->mg_len = -1;
ff0cee69 5472 }
bbce6d69 5473 }
5474 }
7a4c00b4 5475 if (mg && mg->mg_ptr) {
565764a8 5476 *nxp = mg->mg_len;
3280af22 5477 return mg->mg_ptr + sizeof(PL_collation_ix);
bbce6d69 5478 }
5479 else {
5480 *nxp = 0;
5481 return NULL;
16660edb 5482 }
79072805
LW
5483}
5484
36477c24 5485#endif /* USE_LOCALE_COLLATE */
bbce6d69 5486
c461cf8f
JH
5487/*
5488=for apidoc sv_gets
5489
5490Get a line from the filehandle and store it into the SV, optionally
5491appending to the currently-stored string.
5492
5493=cut
5494*/
5495
79072805 5496char *
864dbfa3 5497Perl_sv_gets(pTHX_ register SV *sv, register PerlIO *fp, I32 append)
79072805 5498{
c07a80fd 5499 char *rsptr;
5500 STRLEN rslen;
5501 register STDCHAR rslast;
5502 register STDCHAR *bp;
5503 register I32 cnt;
9c5ffd7c 5504 I32 i = 0;
c07a80fd 5505
2213622d 5506 SV_CHECK_THINKFIRST(sv);
6fc92669 5507 (void)SvUPGRADE(sv, SVt_PV);
99491443 5508
ff68c719 5509 SvSCREAM_off(sv);
c07a80fd 5510
3280af22 5511 if (RsSNARF(PL_rs)) {
c07a80fd 5512 rsptr = NULL;
5513 rslen = 0;
5514 }
3280af22 5515 else if (RsRECORD(PL_rs)) {
5b2b9c68
HM
5516 I32 recsize, bytesread;
5517 char *buffer;
5518
5519 /* Grab the size of the record we're getting */
3280af22 5520 recsize = SvIV(SvRV(PL_rs));
5b2b9c68 5521 (void)SvPOK_only(sv); /* Validate pointer */
e670df4e 5522 buffer = SvGROW(sv, recsize + 1);
5b2b9c68
HM
5523 /* Go yank in */
5524#ifdef VMS
5525 /* VMS wants read instead of fread, because fread doesn't respect */
5526 /* RMS record boundaries. This is not necessarily a good thing to be */
5527 /* doing, but we've got no other real choice */
5528 bytesread = PerlLIO_read(PerlIO_fileno(fp), buffer, recsize);
5529#else
5530 bytesread = PerlIO_read(fp, buffer, recsize);
5531#endif
5532 SvCUR_set(sv, bytesread);
e670df4e 5533 buffer[bytesread] = '\0';
7d59b7e4
NIS
5534 if (PerlIO_isutf8(fp))
5535 SvUTF8_on(sv);
5536 else
5537 SvUTF8_off(sv);
5b2b9c68
HM
5538 return(SvCUR(sv) ? SvPVX(sv) : Nullch);
5539 }
3280af22 5540 else if (RsPARA(PL_rs)) {
c07a80fd 5541 rsptr = "\n\n";
5542 rslen = 2;
5543 }
7d59b7e4
NIS
5544 else {
5545 /* Get $/ i.e. PL_rs into same encoding as stream wants */
5546 if (PerlIO_isutf8(fp)) {
5547 rsptr = SvPVutf8(PL_rs, rslen);
5548 }
5549 else {
5550 if (SvUTF8(PL_rs)) {
5551 if (!sv_utf8_downgrade(PL_rs, TRUE)) {
5552 Perl_croak(aTHX_ "Wide character in $/");
5553 }
5554 }
5555 rsptr = SvPV(PL_rs, rslen);
5556 }
5557 }
5558
c07a80fd 5559 rslast = rslen ? rsptr[rslen - 1] : '\0';
5560
3280af22 5561 if (RsPARA(PL_rs)) { /* have to do this both before and after */
79072805 5562 do { /* to make sure file boundaries work right */
760ac839 5563 if (PerlIO_eof(fp))
a0d0e21e 5564 return 0;
760ac839 5565 i = PerlIO_getc(fp);
79072805 5566 if (i != '\n') {
a0d0e21e
LW
5567 if (i == -1)
5568 return 0;
760ac839 5569 PerlIO_ungetc(fp,i);
79072805
LW
5570 break;
5571 }
5572 } while (i != EOF);
5573 }
c07a80fd 5574
760ac839
LW
5575 /* See if we know enough about I/O mechanism to cheat it ! */
5576
5577 /* This used to be #ifdef test - it is made run-time test for ease
1c846c1f 5578 of abstracting out stdio interface. One call should be cheap
760ac839
LW
5579 enough here - and may even be a macro allowing compile
5580 time optimization.
5581 */
5582
5583 if (PerlIO_fast_gets(fp)) {
5584
5585 /*
5586 * We're going to steal some values from the stdio struct
5587 * and put EVERYTHING in the innermost loop into registers.
5588 */
5589 register STDCHAR *ptr;
5590 STRLEN bpx;
5591 I32 shortbuffered;
5592
16660edb 5593#if defined(VMS) && defined(PERLIO_IS_STDIO)
5594 /* An ungetc()d char is handled separately from the regular
5595 * buffer, so we getc() it back out and stuff it in the buffer.
5596 */
5597 i = PerlIO_getc(fp);
5598 if (i == EOF) return 0;
5599 *(--((*fp)->_ptr)) = (unsigned char) i;
5600 (*fp)->_cnt++;
5601#endif
c07a80fd 5602
c2960299 5603 /* Here is some breathtakingly efficient cheating */
c07a80fd 5604
a20bf0c3 5605 cnt = PerlIO_get_cnt(fp); /* get count into register */
a0d0e21e 5606 (void)SvPOK_only(sv); /* validate pointer */
79072805
LW
5607 if (SvLEN(sv) - append <= cnt + 1) { /* make sure we have the room */
5608 if (cnt > 80 && SvLEN(sv) > append) {
5609 shortbuffered = cnt - SvLEN(sv) + append + 1;
5610 cnt -= shortbuffered;
5611 }
5612 else {
5613 shortbuffered = 0;
bbce6d69 5614 /* remember that cnt can be negative */
5615 SvGROW(sv, append + (cnt <= 0 ? 2 : (cnt + 1)));
79072805
LW
5616 }
5617 }
5618 else
5619 shortbuffered = 0;
c07a80fd 5620 bp = (STDCHAR*)SvPVX(sv) + append; /* move these two too to registers */
a20bf0c3 5621 ptr = (STDCHAR*)PerlIO_get_ptr(fp);
16660edb 5622 DEBUG_P(PerlIO_printf(Perl_debug_log,
1d7c1841 5623 "Screamer: entering, ptr=%"UVuf", cnt=%ld\n",PTR2UV(ptr),(long)cnt));
16660edb 5624 DEBUG_P(PerlIO_printf(Perl_debug_log,
1d7c1841 5625 "Screamer: entering: FILE * thinks ptr=%"UVuf", cnt=%ld, base=%"UVuf"\n",
1c846c1f 5626 PTR2UV(PerlIO_get_ptr(fp)), (long)PerlIO_get_cnt(fp),
1d7c1841 5627 PTR2UV(PerlIO_has_base(fp) ? PerlIO_get_base(fp) : 0)));
79072805
LW
5628 for (;;) {
5629 screamer:
93a17b20 5630 if (cnt > 0) {
c07a80fd 5631 if (rslen) {
760ac839
LW
5632 while (cnt > 0) { /* this | eat */
5633 cnt--;
c07a80fd 5634 if ((*bp++ = *ptr++) == rslast) /* really | dust */
5635 goto thats_all_folks; /* screams | sed :-) */
5636 }
5637 }
5638 else {
1c846c1f
NIS
5639 Copy(ptr, bp, cnt, char); /* this | eat */
5640 bp += cnt; /* screams | dust */
c07a80fd 5641 ptr += cnt; /* louder | sed :-) */
a5f75d66 5642 cnt = 0;
93a17b20 5643 }
79072805
LW
5644 }
5645
748a9306 5646 if (shortbuffered) { /* oh well, must extend */
79072805
LW
5647 cnt = shortbuffered;
5648 shortbuffered = 0;
c07a80fd 5649 bpx = bp - (STDCHAR*)SvPVX(sv); /* box up before relocation */
79072805
LW
5650 SvCUR_set(sv, bpx);
5651 SvGROW(sv, SvLEN(sv) + append + cnt + 2);
c07a80fd 5652 bp = (STDCHAR*)SvPVX(sv) + bpx; /* unbox after relocation */
79072805
LW
5653 continue;
5654 }
5655
16660edb 5656 DEBUG_P(PerlIO_printf(Perl_debug_log,
1d7c1841
GS
5657 "Screamer: going to getc, ptr=%"UVuf", cnt=%ld\n",
5658 PTR2UV(ptr),(long)cnt));
a20bf0c3 5659 PerlIO_set_ptrcnt(fp, ptr, cnt); /* deregisterize cnt and ptr */
16660edb 5660 DEBUG_P(PerlIO_printf(Perl_debug_log,
1d7c1841 5661 "Screamer: pre: FILE * thinks ptr=%"UVuf", cnt=%ld, base=%"UVuf"\n",
1c846c1f 5662 PTR2UV(PerlIO_get_ptr(fp)), (long)PerlIO_get_cnt(fp),
1d7c1841 5663 PTR2UV(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0)));
1c846c1f 5664 /* This used to call 'filbuf' in stdio form, but as that behaves like
774d564b 5665 getc when cnt <= 0 we use PerlIO_getc here to avoid introducing
5666 another abstraction. */
760ac839 5667 i = PerlIO_getc(fp); /* get more characters */
16660edb 5668 DEBUG_P(PerlIO_printf(Perl_debug_log,
1d7c1841 5669 "Screamer: post: FILE * thinks ptr=%"UVuf", cnt=%ld, base=%"UVuf"\n",
1c846c1f 5670 PTR2UV(PerlIO_get_ptr(fp)), (long)PerlIO_get_cnt(fp),
1d7c1841 5671 PTR2UV(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0)));
a20bf0c3
JH
5672 cnt = PerlIO_get_cnt(fp);
5673 ptr = (STDCHAR*)PerlIO_get_ptr(fp); /* reregisterize cnt and ptr */
16660edb 5674 DEBUG_P(PerlIO_printf(Perl_debug_log,
1d7c1841 5675 "Screamer: after getc, ptr=%"UVuf", cnt=%ld\n",PTR2UV(ptr),(long)cnt));
79072805 5676
748a9306
LW
5677 if (i == EOF) /* all done for ever? */
5678 goto thats_really_all_folks;
5679
c07a80fd 5680 bpx = bp - (STDCHAR*)SvPVX(sv); /* box up before relocation */
79072805
LW
5681 SvCUR_set(sv, bpx);
5682 SvGROW(sv, bpx + cnt + 2);
c07a80fd 5683 bp = (STDCHAR*)SvPVX(sv) + bpx; /* unbox after relocation */
5684
760ac839 5685 *bp++ = i; /* store character from PerlIO_getc */
79072805 5686
c07a80fd 5687 if (rslen && (STDCHAR)i == rslast) /* all done for now? */
79072805 5688 goto thats_all_folks;
79072805
LW
5689 }
5690
5691thats_all_folks:
c07a80fd 5692 if ((rslen > 1 && (bp - (STDCHAR*)SvPVX(sv) < rslen)) ||
36477c24 5693 memNE((char*)bp - rslen, rsptr, rslen))
760ac839 5694 goto screamer; /* go back to the fray */
79072805
LW
5695thats_really_all_folks:
5696 if (shortbuffered)
5697 cnt += shortbuffered;
16660edb 5698 DEBUG_P(PerlIO_printf(Perl_debug_log,
1d7c1841 5699 "Screamer: quitting, ptr=%"UVuf", cnt=%ld\n",PTR2UV(ptr),(long)cnt));
a20bf0c3 5700 PerlIO_set_ptrcnt(fp, ptr, cnt); /* put these back or we're in trouble */
16660edb 5701 DEBUG_P(PerlIO_printf(Perl_debug_log,
1d7c1841 5702 "Screamer: end: FILE * thinks ptr=%"UVuf", cnt=%ld, base=%"UVuf"\n",
1c846c1f 5703 PTR2UV(PerlIO_get_ptr(fp)), (long)PerlIO_get_cnt(fp),
1d7c1841 5704 PTR2UV(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0)));
79072805 5705 *bp = '\0';
760ac839 5706 SvCUR_set(sv, bp - (STDCHAR*)SvPVX(sv)); /* set length */
16660edb 5707 DEBUG_P(PerlIO_printf(Perl_debug_log,
fb73857a 5708 "Screamer: done, len=%ld, string=|%.*s|\n",
5709 (long)SvCUR(sv),(int)SvCUR(sv),SvPVX(sv)));
760ac839
LW
5710 }
5711 else
79072805 5712 {
4d2c4e07 5713#ifndef EPOC
760ac839 5714 /*The big, slow, and stupid way */
c07a80fd 5715 STDCHAR buf[8192];
4d2c4e07
OF
5716#else
5717 /* Need to work around EPOC SDK features */
5718 /* On WINS: MS VC5 generates calls to _chkstk, */
5719 /* if a `large' stack frame is allocated */
5720 /* gcc on MARM does not generate calls like these */
5721 STDCHAR buf[1024];
5722#endif
79072805 5723
760ac839 5724screamer2:
c07a80fd 5725 if (rslen) {
760ac839
LW
5726 register STDCHAR *bpe = buf + sizeof(buf);
5727 bp = buf;
5728 while ((i = PerlIO_getc(fp)) != EOF && (*bp++ = i) != rslast && bp < bpe)
5729 ; /* keep reading */
5730 cnt = bp - buf;
c07a80fd 5731 }
5732 else {
760ac839 5733 cnt = PerlIO_read(fp,(char*)buf, sizeof(buf));
16660edb 5734 /* Accomodate broken VAXC compiler, which applies U8 cast to
5735 * both args of ?: operator, causing EOF to change into 255
5736 */
5737 if (cnt) { i = (U8)buf[cnt - 1]; } else { i = EOF; }
c07a80fd 5738 }
79072805
LW
5739
5740 if (append)
760ac839 5741 sv_catpvn(sv, (char *) buf, cnt);
79072805 5742 else
760ac839 5743 sv_setpvn(sv, (char *) buf, cnt);
c07a80fd 5744
5745 if (i != EOF && /* joy */
5746 (!rslen ||
5747 SvCUR(sv) < rslen ||
36477c24 5748 memNE(SvPVX(sv) + SvCUR(sv) - rslen, rsptr, rslen)))
79072805
LW
5749 {
5750 append = -1;
63e4d877
CS
5751 /*
5752 * If we're reading from a TTY and we get a short read,
5753 * indicating that the user hit his EOF character, we need
5754 * to notice it now, because if we try to read from the TTY
5755 * again, the EOF condition will disappear.
5756 *
5757 * The comparison of cnt to sizeof(buf) is an optimization
5758 * that prevents unnecessary calls to feof().
5759 *
5760 * - jik 9/25/96
5761 */
5762 if (!(cnt < sizeof(buf) && PerlIO_eof(fp)))
5763 goto screamer2;
79072805
LW
5764 }
5765 }
5766
1c846c1f 5767 if (RsPARA(PL_rs)) { /* have to do this both before and after */
c07a80fd 5768 while (i != EOF) { /* to make sure file boundaries work right */
760ac839 5769 i = PerlIO_getc(fp);
79072805 5770 if (i != '\n') {
760ac839 5771 PerlIO_ungetc(fp,i);
79072805
LW
5772 break;
5773 }
5774 }
5775 }
c07a80fd 5776
7d59b7e4
NIS
5777 if (PerlIO_isutf8(fp))
5778 SvUTF8_on(sv);
5779 else
5780 SvUTF8_off(sv);
5781
c07a80fd 5782 return (SvCUR(sv) - append) ? SvPVX(sv) : Nullch;
79072805
LW
5783}
5784
954c1994
GS
5785/*
5786=for apidoc sv_inc
5787
645c22ef
DM
5788Auto-increment of the value in the SV, doing string to numeric conversion
5789if necessary. Handles 'get' magic.
954c1994
GS
5790
5791=cut
5792*/
5793
79072805 5794void
864dbfa3 5795Perl_sv_inc(pTHX_ register SV *sv)
79072805
LW
5796{
5797 register char *d;
463ee0b2 5798 int flags;
79072805
LW
5799
5800 if (!sv)
5801 return;
b23a5f78
GB
5802 if (SvGMAGICAL(sv))
5803 mg_get(sv);
ed6116ce 5804 if (SvTHINKFIRST(sv)) {
0f15f207 5805 if (SvREADONLY(sv)) {
3280af22 5806 if (PL_curcop != &PL_compiling)
cea2e8a9 5807 Perl_croak(aTHX_ PL_no_modify);
0f15f207 5808 }
a0d0e21e 5809 if (SvROK(sv)) {
b5be31e9 5810 IV i;
9e7bc3e8
JD
5811 if (SvAMAGIC(sv) && AMG_CALLun(sv,inc))
5812 return;
56431972 5813 i = PTR2IV(SvRV(sv));
b5be31e9
SM
5814 sv_unref(sv);
5815 sv_setiv(sv, i);
a0d0e21e 5816 }
ed6116ce 5817 }
8990e307 5818 flags = SvFLAGS(sv);
28e5dec8
JH
5819 if ((flags & (SVp_NOK|SVp_IOK)) == SVp_NOK) {
5820 /* It's (privately or publicly) a float, but not tested as an
5821 integer, so test it to see. */
d460ef45 5822 (void) SvIV(sv);
28e5dec8
JH
5823 flags = SvFLAGS(sv);
5824 }
5825 if ((flags & SVf_IOK) || ((flags & (SVp_IOK | SVp_NOK)) == SVp_IOK)) {
5826 /* It's publicly an integer, or privately an integer-not-float */
5827 oops_its_int:
25da4f38
IZ
5828 if (SvIsUV(sv)) {
5829 if (SvUVX(sv) == UV_MAX)
65202027 5830 sv_setnv(sv, (NV)UV_MAX + 1.0);
25da4f38
IZ
5831 else
5832 (void)SvIOK_only_UV(sv);
5833 ++SvUVX(sv);
5834 } else {
5835 if (SvIVX(sv) == IV_MAX)
28e5dec8 5836 sv_setuv(sv, (UV)IV_MAX + 1);
25da4f38
IZ
5837 else {
5838 (void)SvIOK_only(sv);
5839 ++SvIVX(sv);
1c846c1f 5840 }
55497cff 5841 }
79072805
LW
5842 return;
5843 }
28e5dec8
JH
5844 if (flags & SVp_NOK) {
5845 (void)SvNOK_only(sv);
5846 SvNVX(sv) += 1.0;
5847 return;
5848 }
5849
8990e307 5850 if (!(flags & SVp_POK) || !*SvPVX(sv)) {
28e5dec8
JH
5851 if ((flags & SVTYPEMASK) < SVt_PVIV)
5852 sv_upgrade(sv, SVt_IV);
5853 (void)SvIOK_only(sv);
5854 SvIVX(sv) = 1;
79072805
LW
5855 return;
5856 }
463ee0b2 5857 d = SvPVX(sv);
79072805
LW
5858 while (isALPHA(*d)) d++;
5859 while (isDIGIT(*d)) d++;
5860 if (*d) {
28e5dec8
JH
5861#ifdef PERL_PRESERVE_IVUV
5862 /* Got to punt this an an integer if needs be, but we don't issue
5863 warnings. Probably ought to make the sv_iv_please() that does
5864 the conversion if possible, and silently. */
c2988b20 5865 int numtype = grok_number(SvPVX(sv), SvCUR(sv), NULL);
28e5dec8
JH
5866 if (numtype && !(numtype & IS_NUMBER_INFINITY)) {
5867 /* Need to try really hard to see if it's an integer.
5868 9.22337203685478e+18 is an integer.
5869 but "9.22337203685478e+18" + 0 is UV=9223372036854779904
5870 so $a="9.22337203685478e+18"; $a+0; $a++
5871 needs to be the same as $a="9.22337203685478e+18"; $a++
5872 or we go insane. */
d460ef45 5873
28e5dec8
JH
5874 (void) sv_2iv(sv);
5875 if (SvIOK(sv))
5876 goto oops_its_int;
5877
5878 /* sv_2iv *should* have made this an NV */
5879 if (flags & SVp_NOK) {
5880 (void)SvNOK_only(sv);
5881 SvNVX(sv) += 1.0;
5882 return;
5883 }
5884 /* I don't think we can get here. Maybe I should assert this
5885 And if we do get here I suspect that sv_setnv will croak. NWC
5886 Fall through. */
5887#if defined(USE_LONG_DOUBLE)
5888 DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_inc punt failed to convert '%s' to IOK or NOKp, UV=0x%"UVxf" NV=%"PERL_PRIgldbl"\n",
5889 SvPVX(sv), SvIVX(sv), SvNVX(sv)));
5890#else
5891 DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_inc punt failed to convert '%s' to IOK or NOKp, UV=0x%"UVxf" NV=%g\n",
5892 SvPVX(sv), SvIVX(sv), SvNVX(sv)));
5893#endif
5894 }
5895#endif /* PERL_PRESERVE_IVUV */
5896 sv_setnv(sv,Atof(SvPVX(sv)) + 1.0);
79072805
LW
5897 return;
5898 }
5899 d--;
463ee0b2 5900 while (d >= SvPVX(sv)) {
79072805
LW
5901 if (isDIGIT(*d)) {
5902 if (++*d <= '9')
5903 return;
5904 *(d--) = '0';
5905 }
5906 else {
9d116dd7
JH
5907#ifdef EBCDIC
5908 /* MKS: The original code here died if letters weren't consecutive.
5909 * at least it didn't have to worry about non-C locales. The
5910 * new code assumes that ('z'-'a')==('Z'-'A'), letters are
1c846c1f 5911 * arranged in order (although not consecutively) and that only
9d116dd7
JH
5912 * [A-Za-z] are accepted by isALPHA in the C locale.
5913 */
5914 if (*d != 'z' && *d != 'Z') {
5915 do { ++*d; } while (!isALPHA(*d));
5916 return;
5917 }
5918 *(d--) -= 'z' - 'a';
5919#else
79072805
LW
5920 ++*d;
5921 if (isALPHA(*d))
5922 return;
5923 *(d--) -= 'z' - 'a' + 1;
9d116dd7 5924#endif
79072805
LW
5925 }
5926 }
5927 /* oh,oh, the number grew */
5928 SvGROW(sv, SvCUR(sv) + 2);
5929 SvCUR(sv)++;
463ee0b2 5930 for (d = SvPVX(sv) + SvCUR(sv); d > SvPVX(sv); d--)
79072805
LW
5931 *d = d[-1];
5932 if (isDIGIT(d[1]))
5933 *d = '1';
5934 else
5935 *d = d[1];
5936}
5937
954c1994
GS
5938/*
5939=for apidoc sv_dec
5940
645c22ef
DM
5941Auto-decrement of the value in the SV, doing string to numeric conversion
5942if necessary. Handles 'get' magic.
954c1994
GS
5943
5944=cut
5945*/
5946
79072805 5947void
864dbfa3 5948Perl_sv_dec(pTHX_ register SV *sv)
79072805 5949{
463ee0b2
LW
5950 int flags;
5951
79072805
LW
5952 if (!sv)
5953 return;
b23a5f78
GB
5954 if (SvGMAGICAL(sv))
5955 mg_get(sv);
ed6116ce 5956 if (SvTHINKFIRST(sv)) {
0f15f207 5957 if (SvREADONLY(sv)) {
3280af22 5958 if (PL_curcop != &PL_compiling)
cea2e8a9 5959 Perl_croak(aTHX_ PL_no_modify);
0f15f207 5960 }
a0d0e21e 5961 if (SvROK(sv)) {
b5be31e9 5962 IV i;
9e7bc3e8
JD
5963 if (SvAMAGIC(sv) && AMG_CALLun(sv,dec))
5964 return;
56431972 5965 i = PTR2IV(SvRV(sv));
b5be31e9
SM
5966 sv_unref(sv);
5967 sv_setiv(sv, i);
a0d0e21e 5968 }
ed6116ce 5969 }
28e5dec8
JH
5970 /* Unlike sv_inc we don't have to worry about string-never-numbers
5971 and keeping them magic. But we mustn't warn on punting */
8990e307 5972 flags = SvFLAGS(sv);
28e5dec8
JH
5973 if ((flags & SVf_IOK) || ((flags & (SVp_IOK | SVp_NOK)) == SVp_IOK)) {
5974 /* It's publicly an integer, or privately an integer-not-float */
5975 oops_its_int:
25da4f38
IZ
5976 if (SvIsUV(sv)) {
5977 if (SvUVX(sv) == 0) {
5978 (void)SvIOK_only(sv);
5979 SvIVX(sv) = -1;
5980 }
5981 else {
5982 (void)SvIOK_only_UV(sv);
5983 --SvUVX(sv);
1c846c1f 5984 }
25da4f38
IZ
5985 } else {
5986 if (SvIVX(sv) == IV_MIN)
65202027 5987 sv_setnv(sv, (NV)IV_MIN - 1.0);
25da4f38
IZ
5988 else {
5989 (void)SvIOK_only(sv);
5990 --SvIVX(sv);
1c846c1f 5991 }
55497cff 5992 }
5993 return;
5994 }
28e5dec8
JH
5995 if (flags & SVp_NOK) {
5996 SvNVX(sv) -= 1.0;
5997 (void)SvNOK_only(sv);
5998 return;
5999 }
8990e307 6000 if (!(flags & SVp_POK)) {
4633a7c4
LW
6001 if ((flags & SVTYPEMASK) < SVt_PVNV)
6002 sv_upgrade(sv, SVt_NV);
463ee0b2 6003 SvNVX(sv) = -1.0;
a0d0e21e 6004 (void)SvNOK_only(sv);
79072805
LW
6005 return;
6006 }
28e5dec8
JH
6007#ifdef PERL_PRESERVE_IVUV
6008 {
c2988b20 6009 int numtype = grok_number(SvPVX(sv), SvCUR(sv), NULL);
28e5dec8
JH
6010 if (numtype && !(numtype & IS_NUMBER_INFINITY)) {
6011 /* Need to try really hard to see if it's an integer.
6012 9.22337203685478e+18 is an integer.
6013 but "9.22337203685478e+18" + 0 is UV=9223372036854779904
6014 so $a="9.22337203685478e+18"; $a+0; $a--
6015 needs to be the same as $a="9.22337203685478e+18"; $a--
6016 or we go insane. */
d460ef45 6017
28e5dec8
JH
6018 (void) sv_2iv(sv);
6019 if (SvIOK(sv))
6020 goto oops_its_int;
6021
6022 /* sv_2iv *should* have made this an NV */
6023 if (flags & SVp_NOK) {
6024 (void)SvNOK_only(sv);
6025 SvNVX(sv) -= 1.0;
6026 return;
6027 }
6028 /* I don't think we can get here. Maybe I should assert this
6029 And if we do get here I suspect that sv_setnv will croak. NWC
6030 Fall through. */
6031#if defined(USE_LONG_DOUBLE)
6032 DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_dec punt failed to convert '%s' to IOK or NOKp, UV=0x%"UVxf" NV=%"PERL_PRIgldbl"\n",
6033 SvPVX(sv), SvIVX(sv), SvNVX(sv)));
6034#else
6035 DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_dec punt failed to convert '%s' to IOK or NOKp, UV=0x%"UVxf" NV=%g\n",
6036 SvPVX(sv), SvIVX(sv), SvNVX(sv)));
6037#endif
6038 }
6039 }
6040#endif /* PERL_PRESERVE_IVUV */
097ee67d 6041 sv_setnv(sv,Atof(SvPVX(sv)) - 1.0); /* punt */
79072805
LW
6042}
6043
954c1994
GS
6044/*
6045=for apidoc sv_mortalcopy
6046
645c22ef
DM
6047Creates a new SV which is a copy of the original SV (using C<sv_setsv>).
6048The new SV is marked as mortal. It will be destroyed when the current
6049context ends. See also C<sv_newmortal> and C<sv_2mortal>.
954c1994
GS
6050
6051=cut
6052*/
6053
79072805
LW
6054/* Make a string that will exist for the duration of the expression
6055 * evaluation. Actually, it may have to last longer than that, but
6056 * hopefully we won't free it until it has been assigned to a
6057 * permanent location. */
6058
6059SV *
864dbfa3 6060Perl_sv_mortalcopy(pTHX_ SV *oldstr)
79072805 6061{
463ee0b2 6062 register SV *sv;
79072805 6063
4561caa4 6064 new_SV(sv);
79072805 6065 sv_setsv(sv,oldstr);
677b06e3
GS
6066 EXTEND_MORTAL(1);
6067 PL_tmps_stack[++PL_tmps_ix] = sv;
8990e307
LW
6068 SvTEMP_on(sv);
6069 return sv;
6070}
6071
954c1994
GS
6072/*
6073=for apidoc sv_newmortal
6074
645c22ef
DM
6075Creates a new null SV which is mortal. The reference count of the SV is
6076set to 1. It will be destroyed when the current context ends. See
6077also C<sv_mortalcopy> and C<sv_2mortal>.
954c1994
GS
6078
6079=cut
6080*/
6081
8990e307 6082SV *
864dbfa3 6083Perl_sv_newmortal(pTHX)
8990e307
LW
6084{
6085 register SV *sv;
6086
4561caa4 6087 new_SV(sv);
8990e307 6088 SvFLAGS(sv) = SVs_TEMP;
677b06e3
GS
6089 EXTEND_MORTAL(1);
6090 PL_tmps_stack[++PL_tmps_ix] = sv;
79072805
LW
6091 return sv;
6092}
6093
954c1994
GS
6094/*
6095=for apidoc sv_2mortal
6096
645c22ef
DM
6097Marks an existing SV as mortal. The SV will be destroyed when the current
6098context ends. See also C<sv_newmortal> and C<sv_mortalcopy>.
954c1994
GS
6099
6100=cut
6101*/
6102
79072805 6103SV *
864dbfa3 6104Perl_sv_2mortal(pTHX_ register SV *sv)
79072805
LW
6105{
6106 if (!sv)
6107 return sv;
d689ffdd 6108 if (SvREADONLY(sv) && SvIMMORTAL(sv))
11162842 6109 return sv;
677b06e3
GS
6110 EXTEND_MORTAL(1);
6111 PL_tmps_stack[++PL_tmps_ix] = sv;
8990e307 6112 SvTEMP_on(sv);
79072805
LW
6113 return sv;
6114}
6115
954c1994
GS
6116/*
6117=for apidoc newSVpv
6118
6119Creates a new SV and copies a string into it. The reference count for the
6120SV is set to 1. If C<len> is zero, Perl will compute the length using
6121strlen(). For efficiency, consider using C<newSVpvn> instead.
6122
6123=cut
6124*/
6125
79072805 6126SV *
864dbfa3 6127Perl_newSVpv(pTHX_ const char *s, STRLEN len)
79072805 6128{
463ee0b2 6129 register SV *sv;
79072805 6130
4561caa4 6131 new_SV(sv);
79072805
LW
6132 if (!len)
6133 len = strlen(s);
6134 sv_setpvn(sv,s,len);
6135 return sv;
6136}
6137
954c1994
GS
6138/*
6139=for apidoc newSVpvn
6140
6141Creates a new SV and copies a string into it. The reference count for the
1c846c1f 6142SV is set to 1. Note that if C<len> is zero, Perl will create a zero length
954c1994
GS
6143string. You are responsible for ensuring that the source string is at least
6144C<len> bytes long.
6145
6146=cut
6147*/
6148
9da1e3b5 6149SV *
864dbfa3 6150Perl_newSVpvn(pTHX_ const char *s, STRLEN len)
9da1e3b5
MUN
6151{
6152 register SV *sv;
6153
6154 new_SV(sv);
9da1e3b5
MUN
6155 sv_setpvn(sv,s,len);
6156 return sv;
6157}
6158
1c846c1f
NIS
6159/*
6160=for apidoc newSVpvn_share
6161
645c22ef
DM
6162Creates a new SV with its SvPVX pointing to a shared string in the string
6163table. If the string does not already exist in the table, it is created
6164first. Turns on READONLY and FAKE. The string's hash is stored in the UV
6165slot of the SV; if the C<hash> parameter is non-zero, that value is used;
6166otherwise the hash is computed. The idea here is that as the string table
6167is used for shared hash keys these strings will have SvPVX == HeKEY and
6168hash lookup will avoid string compare.
1c846c1f
NIS
6169
6170=cut
6171*/
6172
6173SV *
c3654f1a 6174Perl_newSVpvn_share(pTHX_ const char *src, I32 len, U32 hash)
1c846c1f
NIS
6175{
6176 register SV *sv;
c3654f1a
IH
6177 bool is_utf8 = FALSE;
6178 if (len < 0) {
6179 len = -len;
6180 is_utf8 = TRUE;
6181 }
75a54232
JH
6182 if (is_utf8 && !(PL_hints & HINT_UTF8_DISTINCT)) {
6183 STRLEN tmplen = len;
6184 /* See the note in hv.c:hv_fetch() --jhi */
6185 src = (char*)bytes_from_utf8((U8*)src, &tmplen, &is_utf8);
6186 len = tmplen;
6187 }
1c846c1f
NIS
6188 if (!hash)
6189 PERL_HASH(hash, src, len);
6190 new_SV(sv);
6191 sv_upgrade(sv, SVt_PVIV);
c3654f1a 6192 SvPVX(sv) = sharepvn(src, is_utf8?-len:len, hash);
1c846c1f
NIS
6193 SvCUR(sv) = len;
6194 SvUVX(sv) = hash;
6195 SvLEN(sv) = 0;
6196 SvREADONLY_on(sv);
6197 SvFAKE_on(sv);
6198 SvPOK_on(sv);
c3654f1a
IH
6199 if (is_utf8)
6200 SvUTF8_on(sv);
1c846c1f
NIS
6201 return sv;
6202}
6203
645c22ef 6204
cea2e8a9 6205#if defined(PERL_IMPLICIT_CONTEXT)
645c22ef
DM
6206
6207/* pTHX_ magic can't cope with varargs, so this is a no-context
6208 * version of the main function, (which may itself be aliased to us).
6209 * Don't access this version directly.
6210 */
6211
46fc3d4c 6212SV *
cea2e8a9 6213Perl_newSVpvf_nocontext(const char* pat, ...)
46fc3d4c 6214{
cea2e8a9 6215 dTHX;
46fc3d4c 6216 register SV *sv;
6217 va_list args;
46fc3d4c 6218 va_start(args, pat);
c5be433b 6219 sv = vnewSVpvf(pat, &args);
46fc3d4c 6220 va_end(args);
6221 return sv;
6222}
cea2e8a9 6223#endif
46fc3d4c 6224
954c1994
GS
6225/*
6226=for apidoc newSVpvf
6227
645c22ef 6228Creates a new SV and initializes it with the string formatted like
954c1994
GS
6229C<sprintf>.
6230
6231=cut
6232*/
6233
cea2e8a9
GS
6234SV *
6235Perl_newSVpvf(pTHX_ const char* pat, ...)
6236{
6237 register SV *sv;
6238 va_list args;
cea2e8a9 6239 va_start(args, pat);
c5be433b 6240 sv = vnewSVpvf(pat, &args);
cea2e8a9
GS
6241 va_end(args);
6242 return sv;
6243}
46fc3d4c 6244
645c22ef
DM
6245/* backend for newSVpvf() and newSVpvf_nocontext() */
6246
79072805 6247SV *
c5be433b
GS
6248Perl_vnewSVpvf(pTHX_ const char* pat, va_list* args)
6249{
6250 register SV *sv;
6251 new_SV(sv);
6252 sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*));
6253 return sv;
6254}
6255
954c1994
GS
6256/*
6257=for apidoc newSVnv
6258
6259Creates a new SV and copies a floating point value into it.
6260The reference count for the SV is set to 1.
6261
6262=cut
6263*/
6264
c5be433b 6265SV *
65202027 6266Perl_newSVnv(pTHX_ NV n)
79072805 6267{
463ee0b2 6268 register SV *sv;
79072805 6269
4561caa4 6270 new_SV(sv);
79072805
LW
6271 sv_setnv(sv,n);
6272 return sv;
6273}
6274
954c1994
GS
6275/*
6276=for apidoc newSViv
6277
6278Creates a new SV and copies an integer into it. The reference count for the
6279SV is set to 1.
6280
6281=cut
6282*/
6283
79072805 6284SV *
864dbfa3 6285Perl_newSViv(pTHX_ IV i)
79072805 6286{
463ee0b2 6287 register SV *sv;
79072805 6288
4561caa4 6289 new_SV(sv);
79072805
LW
6290 sv_setiv(sv,i);
6291 return sv;
6292}
6293
954c1994 6294/*
1a3327fb
JH
6295=for apidoc newSVuv
6296
6297Creates a new SV and copies an unsigned integer into it.
6298The reference count for the SV is set to 1.
6299
6300=cut
6301*/
6302
6303SV *
6304Perl_newSVuv(pTHX_ UV u)
6305{
6306 register SV *sv;
6307
6308 new_SV(sv);
6309 sv_setuv(sv,u);
6310 return sv;
6311}
6312
6313/*
954c1994
GS
6314=for apidoc newRV_noinc
6315
6316Creates an RV wrapper for an SV. The reference count for the original
6317SV is B<not> incremented.
6318
6319=cut
6320*/
6321
2304df62 6322SV *
864dbfa3 6323Perl_newRV_noinc(pTHX_ SV *tmpRef)
2304df62
AD
6324{
6325 register SV *sv;
6326
4561caa4 6327 new_SV(sv);
2304df62 6328 sv_upgrade(sv, SVt_RV);
76e3520e 6329 SvTEMP_off(tmpRef);
d689ffdd 6330 SvRV(sv) = tmpRef;
2304df62 6331 SvROK_on(sv);
2304df62
AD
6332 return sv;
6333}
6334
645c22ef
DM
6335/* newRV_inc is the offical function name to use now.
6336 * newRV_inc is in fact #defined to newRV in sv.h
6337 */
6338
5f05dabc 6339SV *
864dbfa3 6340Perl_newRV(pTHX_ SV *tmpRef)
5f05dabc 6341{
5f6447b6 6342 return newRV_noinc(SvREFCNT_inc(tmpRef));
5f05dabc 6343}
5f05dabc 6344
954c1994
GS
6345/*
6346=for apidoc newSVsv
6347
6348Creates a new SV which is an exact duplicate of the original SV.
645c22ef 6349(Uses C<sv_setsv>).
954c1994
GS
6350
6351=cut
6352*/
6353
79072805 6354SV *
864dbfa3 6355Perl_newSVsv(pTHX_ register SV *old)
79072805 6356{
463ee0b2 6357 register SV *sv;
79072805
LW
6358
6359 if (!old)
6360 return Nullsv;
8990e307 6361 if (SvTYPE(old) == SVTYPEMASK) {
0453d815
PM
6362 if (ckWARN_d(WARN_INTERNAL))
6363 Perl_warner(aTHX_ WARN_INTERNAL, "semi-panic: attempt to dup freed string");
79072805
LW
6364 return Nullsv;
6365 }
4561caa4 6366 new_SV(sv);
ff68c719 6367 if (SvTEMP(old)) {
6368 SvTEMP_off(old);
463ee0b2 6369 sv_setsv(sv,old);
ff68c719 6370 SvTEMP_on(old);
79072805
LW
6371 }
6372 else
463ee0b2
LW
6373 sv_setsv(sv,old);
6374 return sv;
79072805
LW
6375}
6376
645c22ef
DM
6377/*
6378=for apidoc sv_reset
6379
6380Underlying implementation for the C<reset> Perl function.
6381Note that the perl-level function is vaguely deprecated.
6382
6383=cut
6384*/
6385
79072805 6386void
864dbfa3 6387Perl_sv_reset(pTHX_ register char *s, HV *stash)
79072805
LW
6388{
6389 register HE *entry;
6390 register GV *gv;
6391 register SV *sv;
6392 register I32 i;
6393 register PMOP *pm;
6394 register I32 max;
4802d5d7 6395 char todo[PERL_UCHAR_MAX+1];
79072805 6396
49d8d3a1
MB
6397 if (!stash)
6398 return;
6399
79072805
LW
6400 if (!*s) { /* reset ?? searches */
6401 for (pm = HvPMROOT(stash); pm; pm = pm->op_pmnext) {
48c036b1 6402 pm->op_pmdynflags &= ~PMdf_USED;
79072805
LW
6403 }
6404 return;
6405 }
6406
6407 /* reset variables */
6408
6409 if (!HvARRAY(stash))
6410 return;
463ee0b2
LW
6411
6412 Zero(todo, 256, char);
79072805 6413 while (*s) {
4802d5d7 6414 i = (unsigned char)*s;
79072805
LW
6415 if (s[1] == '-') {
6416 s += 2;
6417 }
4802d5d7 6418 max = (unsigned char)*s++;
79072805 6419 for ( ; i <= max; i++) {
463ee0b2
LW
6420 todo[i] = 1;
6421 }
a0d0e21e 6422 for (i = 0; i <= (I32) HvMAX(stash); i++) {
79072805 6423 for (entry = HvARRAY(stash)[i];
9e35f4b3
GS
6424 entry;
6425 entry = HeNEXT(entry))
6426 {
1edc1566 6427 if (!todo[(U8)*HeKEY(entry)])
463ee0b2 6428 continue;
1edc1566 6429 gv = (GV*)HeVAL(entry);
79072805 6430 sv = GvSV(gv);
9e35f4b3
GS
6431 if (SvTHINKFIRST(sv)) {
6432 if (!SvREADONLY(sv) && SvROK(sv))
6433 sv_unref(sv);
6434 continue;
6435 }
a0d0e21e 6436 (void)SvOK_off(sv);
79072805
LW
6437 if (SvTYPE(sv) >= SVt_PV) {
6438 SvCUR_set(sv, 0);
463ee0b2
LW
6439 if (SvPVX(sv) != Nullch)
6440 *SvPVX(sv) = '\0';
44a8e56a 6441 SvTAINT(sv);
79072805
LW
6442 }
6443 if (GvAV(gv)) {
6444 av_clear(GvAV(gv));
6445 }
44a8e56a 6446 if (GvHV(gv) && !HvNAME(GvHV(gv))) {
463ee0b2 6447 hv_clear(GvHV(gv));
fa6a1c44 6448#ifdef USE_ENVIRON_ARRAY
3280af22 6449 if (gv == PL_envgv)
79072805 6450 environ[0] = Nullch;
a0d0e21e 6451#endif
79072805
LW
6452 }
6453 }
6454 }
6455 }
6456}
6457
645c22ef
DM
6458/*
6459=for apidoc sv_2io
6460
6461Using various gambits, try to get an IO from an SV: the IO slot if its a
6462GV; or the recursive result if we're an RV; or the IO slot of the symbol
6463named after the PV if we're a string.
6464
6465=cut
6466*/
6467
46fc3d4c 6468IO*
864dbfa3 6469Perl_sv_2io(pTHX_ SV *sv)
46fc3d4c 6470{
6471 IO* io;
6472 GV* gv;
2d8e6c8d 6473 STRLEN n_a;
46fc3d4c 6474
6475 switch (SvTYPE(sv)) {
6476 case SVt_PVIO:
6477 io = (IO*)sv;
6478 break;
6479 case SVt_PVGV:
6480 gv = (GV*)sv;
6481 io = GvIO(gv);
6482 if (!io)
cea2e8a9 6483 Perl_croak(aTHX_ "Bad filehandle: %s", GvNAME(gv));
46fc3d4c 6484 break;
6485 default:
6486 if (!SvOK(sv))
cea2e8a9 6487 Perl_croak(aTHX_ PL_no_usym, "filehandle");
46fc3d4c 6488 if (SvROK(sv))
6489 return sv_2io(SvRV(sv));
2d8e6c8d 6490 gv = gv_fetchpv(SvPV(sv,n_a), FALSE, SVt_PVIO);
46fc3d4c 6491 if (gv)
6492 io = GvIO(gv);
6493 else
6494 io = 0;
6495 if (!io)
cea2e8a9 6496 Perl_croak(aTHX_ "Bad filehandle: %s", SvPV(sv,n_a));
46fc3d4c 6497 break;
6498 }
6499 return io;
6500}
6501
645c22ef
DM
6502/*
6503=for apidoc sv_2cv
6504
6505Using various gambits, try to get a CV from an SV; in addition, try if
6506possible to set C<*st> and C<*gvp> to the stash and GV associated with it.
6507
6508=cut
6509*/
6510
79072805 6511CV *
864dbfa3 6512Perl_sv_2cv(pTHX_ SV *sv, HV **st, GV **gvp, I32 lref)
79072805
LW
6513{
6514 GV *gv;
6515 CV *cv;
2d8e6c8d 6516 STRLEN n_a;
79072805
LW
6517
6518 if (!sv)
93a17b20 6519 return *gvp = Nullgv, Nullcv;
79072805 6520 switch (SvTYPE(sv)) {
79072805
LW
6521 case SVt_PVCV:
6522 *st = CvSTASH(sv);
6523 *gvp = Nullgv;
6524 return (CV*)sv;
6525 case SVt_PVHV:
6526 case SVt_PVAV:
6527 *gvp = Nullgv;
6528 return Nullcv;
8990e307
LW
6529 case SVt_PVGV:
6530 gv = (GV*)sv;
a0d0e21e 6531 *gvp = gv;
8990e307
LW
6532 *st = GvESTASH(gv);
6533 goto fix_gv;
6534
79072805 6535 default:
a0d0e21e
LW
6536 if (SvGMAGICAL(sv))
6537 mg_get(sv);
6538 if (SvROK(sv)) {
f5284f61
IZ
6539 SV **sp = &sv; /* Used in tryAMAGICunDEREF macro. */
6540 tryAMAGICunDEREF(to_cv);
6541
62f274bf
GS
6542 sv = SvRV(sv);
6543 if (SvTYPE(sv) == SVt_PVCV) {
6544 cv = (CV*)sv;
6545 *gvp = Nullgv;
6546 *st = CvSTASH(cv);
6547 return cv;
6548 }
6549 else if(isGV(sv))
6550 gv = (GV*)sv;
6551 else
cea2e8a9 6552 Perl_croak(aTHX_ "Not a subroutine reference");
a0d0e21e 6553 }
62f274bf 6554 else if (isGV(sv))
79072805
LW
6555 gv = (GV*)sv;
6556 else
2d8e6c8d 6557 gv = gv_fetchpv(SvPV(sv, n_a), lref, SVt_PVCV);
79072805
LW
6558 *gvp = gv;
6559 if (!gv)
6560 return Nullcv;
6561 *st = GvESTASH(gv);
8990e307 6562 fix_gv:
8ebc5c01 6563 if (lref && !GvCVu(gv)) {
4633a7c4 6564 SV *tmpsv;
748a9306 6565 ENTER;
4633a7c4 6566 tmpsv = NEWSV(704,0);
16660edb 6567 gv_efullname3(tmpsv, gv, Nullch);
f6ec51f7
GS
6568 /* XXX this is probably not what they think they're getting.
6569 * It has the same effect as "sub name;", i.e. just a forward
6570 * declaration! */
774d564b 6571 newSUB(start_subparse(FALSE, 0),
4633a7c4
LW
6572 newSVOP(OP_CONST, 0, tmpsv),
6573 Nullop,
8990e307 6574 Nullop);
748a9306 6575 LEAVE;
8ebc5c01 6576 if (!GvCVu(gv))
cea2e8a9 6577 Perl_croak(aTHX_ "Unable to create sub named \"%s\"", SvPV(sv,n_a));
8990e307 6578 }
8ebc5c01 6579 return GvCVu(gv);
79072805
LW
6580 }
6581}
6582
c461cf8f
JH
6583/*
6584=for apidoc sv_true
6585
6586Returns true if the SV has a true value by Perl's rules.
645c22ef
DM
6587Use the C<SvTRUE> macro instead, which may call C<sv_true()> or may
6588instead use an in-line version.
c461cf8f
JH
6589
6590=cut
6591*/
6592
79072805 6593I32
864dbfa3 6594Perl_sv_true(pTHX_ register SV *sv)
79072805 6595{
8990e307
LW
6596 if (!sv)
6597 return 0;
79072805 6598 if (SvPOK(sv)) {
4e35701f
NIS
6599 register XPV* tXpv;
6600 if ((tXpv = (XPV*)SvANY(sv)) &&
c2f1de04 6601 (tXpv->xpv_cur > 1 ||
4e35701f 6602 (tXpv->xpv_cur && *tXpv->xpv_pv != '0')))
79072805
LW
6603 return 1;
6604 else
6605 return 0;
6606 }
6607 else {
6608 if (SvIOK(sv))
463ee0b2 6609 return SvIVX(sv) != 0;
79072805
LW
6610 else {
6611 if (SvNOK(sv))
463ee0b2 6612 return SvNVX(sv) != 0.0;
79072805 6613 else
463ee0b2 6614 return sv_2bool(sv);
79072805
LW
6615 }
6616 }
6617}
79072805 6618
645c22ef
DM
6619/*
6620=for apidoc sv_iv
6621
6622A private implementation of the C<SvIVx> macro for compilers which can't
6623cope with complex macro expressions. Always use the macro instead.
6624
6625=cut
6626*/
6627
ff68c719 6628IV
864dbfa3 6629Perl_sv_iv(pTHX_ register SV *sv)
85e6fe83 6630{
25da4f38
IZ
6631 if (SvIOK(sv)) {
6632 if (SvIsUV(sv))
6633 return (IV)SvUVX(sv);
ff68c719 6634 return SvIVX(sv);
25da4f38 6635 }
ff68c719 6636 return sv_2iv(sv);
85e6fe83 6637}
85e6fe83 6638
645c22ef
DM
6639/*
6640=for apidoc sv_uv
6641
6642A private implementation of the C<SvUVx> macro for compilers which can't
6643cope with complex macro expressions. Always use the macro instead.
6644
6645=cut
6646*/
6647
ff68c719 6648UV
864dbfa3 6649Perl_sv_uv(pTHX_ register SV *sv)
ff68c719 6650{
25da4f38
IZ
6651 if (SvIOK(sv)) {
6652 if (SvIsUV(sv))
6653 return SvUVX(sv);
6654 return (UV)SvIVX(sv);
6655 }
ff68c719 6656 return sv_2uv(sv);
6657}
85e6fe83 6658
645c22ef
DM
6659/*
6660=for apidoc sv_nv
6661
6662A private implementation of the C<SvNVx> macro for compilers which can't
6663cope with complex macro expressions. Always use the macro instead.
6664
6665=cut
6666*/
6667
65202027 6668NV
864dbfa3 6669Perl_sv_nv(pTHX_ register SV *sv)
79072805 6670{
ff68c719 6671 if (SvNOK(sv))
6672 return SvNVX(sv);
6673 return sv_2nv(sv);
79072805 6674}
79072805 6675
645c22ef
DM
6676/*
6677=for apidoc sv_pv
6678
6679A private implementation of the C<SvPV_nolen> macro for compilers which can't
6680cope with complex macro expressions. Always use the macro instead.
6681
6682=cut
6683*/
6684
79072805 6685char *
864dbfa3 6686Perl_sv_pv(pTHX_ SV *sv)
1fa8b10d
JD
6687{
6688 STRLEN n_a;
6689
6690 if (SvPOK(sv))
6691 return SvPVX(sv);
6692
6693 return sv_2pv(sv, &n_a);
6694}
6695
645c22ef
DM
6696/*
6697=for apidoc sv_pvn
6698
6699A private implementation of the C<SvPV> macro for compilers which can't
6700cope with complex macro expressions. Always use the macro instead.
6701
6702=cut
6703*/
6704
1fa8b10d 6705char *
864dbfa3 6706Perl_sv_pvn(pTHX_ SV *sv, STRLEN *lp)
79072805 6707{
85e6fe83
LW
6708 if (SvPOK(sv)) {
6709 *lp = SvCUR(sv);
a0d0e21e 6710 return SvPVX(sv);
85e6fe83 6711 }
463ee0b2 6712 return sv_2pv(sv, lp);
79072805 6713}
79072805 6714
c461cf8f
JH
6715/*
6716=for apidoc sv_pvn_force
6717
6718Get a sensible string out of the SV somehow.
645c22ef
DM
6719A private implementation of the C<SvPV_force> macro for compilers which
6720can't cope with complex macro expressions. Always use the macro instead.
c461cf8f
JH
6721
6722=cut
6723*/
6724
a0d0e21e 6725char *
864dbfa3 6726Perl_sv_pvn_force(pTHX_ SV *sv, STRLEN *lp)
a0d0e21e 6727{
36f65ada 6728 return sv_pvn_force_flags(sv, lp, SV_GMAGIC);
8d6d96c1
HS
6729}
6730
6731/*
6732=for apidoc sv_pvn_force_flags
6733
6734Get a sensible string out of the SV somehow.
6735If C<flags> has C<SV_GMAGIC> bit set, will C<mg_get> on C<sv> if
6736appropriate, else not. C<sv_pvn_force> and C<sv_pvn_force_nomg> are
6737implemented in terms of this function.
645c22ef
DM
6738You normally want to use the various wrapper macros instead: see
6739C<SvPV_force> and C<SvPV_force_nomg>
8d6d96c1
HS
6740
6741=cut
6742*/
6743
6744char *
6745Perl_sv_pvn_force_flags(pTHX_ SV *sv, STRLEN *lp, I32 flags)
6746{
a0d0e21e
LW
6747 char *s;
6748
6fc92669
GS
6749 if (SvTHINKFIRST(sv) && !SvROK(sv))
6750 sv_force_normal(sv);
1c846c1f 6751
a0d0e21e
LW
6752 if (SvPOK(sv)) {
6753 *lp = SvCUR(sv);
6754 }
6755 else {
748a9306 6756 if (SvTYPE(sv) > SVt_PVLV && SvTYPE(sv) != SVt_PVFM) {
cea2e8a9 6757 Perl_croak(aTHX_ "Can't coerce %s to string in %s", sv_reftype(sv,0),
6fc92669 6758 PL_op_name[PL_op->op_type]);
a0d0e21e 6759 }
4633a7c4 6760 else
8d6d96c1 6761 s = sv_2pv_flags(sv, lp, flags);
a0d0e21e
LW
6762 if (s != SvPVX(sv)) { /* Almost, but not quite, sv_setpvn() */
6763 STRLEN len = *lp;
1c846c1f 6764
a0d0e21e
LW
6765 if (SvROK(sv))
6766 sv_unref(sv);
6767 (void)SvUPGRADE(sv, SVt_PV); /* Never FALSE */
6768 SvGROW(sv, len + 1);
6769 Move(s,SvPVX(sv),len,char);
6770 SvCUR_set(sv, len);
6771 *SvEND(sv) = '\0';
6772 }
6773 if (!SvPOK(sv)) {
6774 SvPOK_on(sv); /* validate pointer */
6775 SvTAINT(sv);
1d7c1841
GS
6776 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2pv(%s)\n",
6777 PTR2UV(sv),SvPVX(sv)));
a0d0e21e
LW
6778 }
6779 }
6780 return SvPVX(sv);
6781}
6782
645c22ef
DM
6783/*
6784=for apidoc sv_pvbyte
6785
6786A private implementation of the C<SvPVbyte_nolen> macro for compilers
6787which can't cope with complex macro expressions. Always use the macro
6788instead.
6789
6790=cut
6791*/
6792
a0d0e21e 6793char *
7340a771
GS
6794Perl_sv_pvbyte(pTHX_ SV *sv)
6795{
ffebcc3e 6796 sv_utf8_downgrade(sv,0);
7340a771
GS
6797 return sv_pv(sv);
6798}
6799
645c22ef
DM
6800/*
6801=for apidoc sv_pvbyten
6802
6803A private implementation of the C<SvPVbyte> macro for compilers
6804which can't cope with complex macro expressions. Always use the macro
6805instead.
6806
6807=cut
6808*/
6809
7340a771
GS
6810char *
6811Perl_sv_pvbyten(pTHX_ SV *sv, STRLEN *lp)
6812{
ffebcc3e 6813 sv_utf8_downgrade(sv,0);
7340a771
GS
6814 return sv_pvn(sv,lp);
6815}
6816
645c22ef
DM
6817/*
6818=for apidoc sv_pvbyten_force
6819
6820A private implementation of the C<SvPVbytex_force> macro for compilers
6821which can't cope with complex macro expressions. Always use the macro
6822instead.
6823
6824=cut
6825*/
6826
7340a771
GS
6827char *
6828Perl_sv_pvbyten_force(pTHX_ SV *sv, STRLEN *lp)
6829{
ffebcc3e 6830 sv_utf8_downgrade(sv,0);
7340a771
GS
6831 return sv_pvn_force(sv,lp);
6832}
6833
645c22ef
DM
6834/*
6835=for apidoc sv_pvutf8
6836
6837A private implementation of the C<SvPVutf8_nolen> macro for compilers
6838which can't cope with complex macro expressions. Always use the macro
6839instead.
6840
6841=cut
6842*/
6843
7340a771
GS
6844char *
6845Perl_sv_pvutf8(pTHX_ SV *sv)
6846{
560a288e 6847 sv_utf8_upgrade(sv);
7340a771
GS
6848 return sv_pv(sv);
6849}
6850
645c22ef
DM
6851/*
6852=for apidoc sv_pvutf8n
6853
6854A private implementation of the C<SvPVutf8> macro for compilers
6855which can't cope with complex macro expressions. Always use the macro
6856instead.
6857
6858=cut
6859*/
6860
7340a771
GS
6861char *
6862Perl_sv_pvutf8n(pTHX_ SV *sv, STRLEN *lp)
6863{
560a288e 6864 sv_utf8_upgrade(sv);
7340a771
GS
6865 return sv_pvn(sv,lp);
6866}
6867
c461cf8f
JH
6868/*
6869=for apidoc sv_pvutf8n_force
6870
645c22ef
DM
6871A private implementation of the C<SvPVutf8_force> macro for compilers
6872which can't cope with complex macro expressions. Always use the macro
6873instead.
c461cf8f
JH
6874
6875=cut
6876*/
6877
7340a771
GS
6878char *
6879Perl_sv_pvutf8n_force(pTHX_ SV *sv, STRLEN *lp)
6880{
560a288e 6881 sv_utf8_upgrade(sv);
7340a771
GS
6882 return sv_pvn_force(sv,lp);
6883}
6884
c461cf8f
JH
6885/*
6886=for apidoc sv_reftype
6887
6888Returns a string describing what the SV is a reference to.
6889
6890=cut
6891*/
6892
7340a771 6893char *
864dbfa3 6894Perl_sv_reftype(pTHX_ SV *sv, int ob)
a0d0e21e
LW
6895{
6896 if (ob && SvOBJECT(sv))
6897 return HvNAME(SvSTASH(sv));
6898 else {
6899 switch (SvTYPE(sv)) {
6900 case SVt_NULL:
6901 case SVt_IV:
6902 case SVt_NV:
6903 case SVt_RV:
6904 case SVt_PV:
6905 case SVt_PVIV:
6906 case SVt_PVNV:
6907 case SVt_PVMG:
6908 case SVt_PVBM:
6909 if (SvROK(sv))
6910 return "REF";
6911 else
6912 return "SCALAR";
6913 case SVt_PVLV: return "LVALUE";
6914 case SVt_PVAV: return "ARRAY";
6915 case SVt_PVHV: return "HASH";
6916 case SVt_PVCV: return "CODE";
6917 case SVt_PVGV: return "GLOB";
1d2dff63 6918 case SVt_PVFM: return "FORMAT";
27f9d8f3 6919 case SVt_PVIO: return "IO";
a0d0e21e
LW
6920 default: return "UNKNOWN";
6921 }
6922 }
6923}
6924
954c1994
GS
6925/*
6926=for apidoc sv_isobject
6927
6928Returns a boolean indicating whether the SV is an RV pointing to a blessed
6929object. If the SV is not an RV, or if the object is not blessed, then this
6930will return false.
6931
6932=cut
6933*/
6934
463ee0b2 6935int
864dbfa3 6936Perl_sv_isobject(pTHX_ SV *sv)
85e6fe83 6937{
68dc0745 6938 if (!sv)
6939 return 0;
6940 if (SvGMAGICAL(sv))
6941 mg_get(sv);
85e6fe83
LW
6942 if (!SvROK(sv))
6943 return 0;
6944 sv = (SV*)SvRV(sv);
6945 if (!SvOBJECT(sv))
6946 return 0;
6947 return 1;
6948}
6949
954c1994
GS
6950/*
6951=for apidoc sv_isa
6952
6953Returns a boolean indicating whether the SV is blessed into the specified
6954class. This does not check for subtypes; use C<sv_derived_from> to verify
6955an inheritance relationship.
6956
6957=cut
6958*/
6959
85e6fe83 6960int
864dbfa3 6961Perl_sv_isa(pTHX_ SV *sv, const char *name)
463ee0b2 6962{
68dc0745 6963 if (!sv)
6964 return 0;
6965 if (SvGMAGICAL(sv))
6966 mg_get(sv);
ed6116ce 6967 if (!SvROK(sv))
463ee0b2 6968 return 0;
ed6116ce
LW
6969 sv = (SV*)SvRV(sv);
6970 if (!SvOBJECT(sv))
463ee0b2
LW
6971 return 0;
6972
6973 return strEQ(HvNAME(SvSTASH(sv)), name);
6974}
6975
954c1994
GS
6976/*
6977=for apidoc newSVrv
6978
6979Creates a new SV for the RV, C<rv>, to point to. If C<rv> is not an RV then
6980it will be upgraded to one. If C<classname> is non-null then the new SV will
6981be blessed in the specified package. The new SV is returned and its
6982reference count is 1.
6983
6984=cut
6985*/
6986
463ee0b2 6987SV*
864dbfa3 6988Perl_newSVrv(pTHX_ SV *rv, const char *classname)
463ee0b2 6989{
463ee0b2
LW
6990 SV *sv;
6991
4561caa4 6992 new_SV(sv);
51cf62d8 6993
2213622d 6994 SV_CHECK_THINKFIRST(rv);
51cf62d8 6995 SvAMAGIC_off(rv);
51cf62d8 6996
0199fce9
JD
6997 if (SvTYPE(rv) >= SVt_PVMG) {
6998 U32 refcnt = SvREFCNT(rv);
6999 SvREFCNT(rv) = 0;
7000 sv_clear(rv);
7001 SvFLAGS(rv) = 0;
7002 SvREFCNT(rv) = refcnt;
7003 }
7004
51cf62d8 7005 if (SvTYPE(rv) < SVt_RV)
0199fce9
JD
7006 sv_upgrade(rv, SVt_RV);
7007 else if (SvTYPE(rv) > SVt_RV) {
7008 (void)SvOOK_off(rv);
7009 if (SvPVX(rv) && SvLEN(rv))
7010 Safefree(SvPVX(rv));
7011 SvCUR_set(rv, 0);
7012 SvLEN_set(rv, 0);
7013 }
51cf62d8
OT
7014
7015 (void)SvOK_off(rv);
053fc874 7016 SvRV(rv) = sv;
ed6116ce 7017 SvROK_on(rv);
463ee0b2 7018
a0d0e21e
LW
7019 if (classname) {
7020 HV* stash = gv_stashpv(classname, TRUE);
7021 (void)sv_bless(rv, stash);
7022 }
7023 return sv;
7024}
7025
954c1994
GS
7026/*
7027=for apidoc sv_setref_pv
7028
7029Copies a pointer into a new SV, optionally blessing the SV. The C<rv>
7030argument will be upgraded to an RV. That RV will be modified to point to
7031the new SV. If the C<pv> argument is NULL then C<PL_sv_undef> will be placed
7032into the SV. The C<classname> argument indicates the package for the
7033blessing. Set C<classname> to C<Nullch> to avoid the blessing. The new SV
7034will be returned and will have a reference count of 1.
7035
7036Do not use with other Perl types such as HV, AV, SV, CV, because those
7037objects will become corrupted by the pointer copy process.
7038
7039Note that C<sv_setref_pvn> copies the string while this copies the pointer.
7040
7041=cut
7042*/
7043
a0d0e21e 7044SV*
864dbfa3 7045Perl_sv_setref_pv(pTHX_ SV *rv, const char *classname, void *pv)
a0d0e21e 7046{
189b2af5 7047 if (!pv) {
3280af22 7048 sv_setsv(rv, &PL_sv_undef);
189b2af5
GS
7049 SvSETMAGIC(rv);
7050 }
a0d0e21e 7051 else
56431972 7052 sv_setiv(newSVrv(rv,classname), PTR2IV(pv));
a0d0e21e
LW
7053 return rv;
7054}
7055
954c1994
GS
7056/*
7057=for apidoc sv_setref_iv
7058
7059Copies an integer into a new SV, optionally blessing the SV. The C<rv>
7060argument will be upgraded to an RV. That RV will be modified to point to
7061the new SV. The C<classname> argument indicates the package for the
7062blessing. Set C<classname> to C<Nullch> to avoid the blessing. The new SV
7063will be returned and will have a reference count of 1.
7064
7065=cut
7066*/
7067
a0d0e21e 7068SV*
864dbfa3 7069Perl_sv_setref_iv(pTHX_ SV *rv, const char *classname, IV iv)
a0d0e21e
LW
7070{
7071 sv_setiv(newSVrv(rv,classname), iv);
7072 return rv;
7073}
7074
954c1994 7075/*
e1c57cef
JH
7076=for apidoc sv_setref_uv
7077
7078Copies an unsigned integer into a new SV, optionally blessing the SV. The C<rv>
7079argument will be upgraded to an RV. That RV will be modified to point to
7080the new SV. The C<classname> argument indicates the package for the
7081blessing. Set C<classname> to C<Nullch> to avoid the blessing. The new SV
7082will be returned and will have a reference count of 1.
7083
7084=cut
7085*/
7086
7087SV*
7088Perl_sv_setref_uv(pTHX_ SV *rv, const char *classname, UV uv)
7089{
7090 sv_setuv(newSVrv(rv,classname), uv);
7091 return rv;
7092}
7093
7094/*
954c1994
GS
7095=for apidoc sv_setref_nv
7096
7097Copies a double into a new SV, optionally blessing the SV. The C<rv>
7098argument will be upgraded to an RV. That RV will be modified to point to
7099the new SV. The C<classname> argument indicates the package for the
7100blessing. Set C<classname> to C<Nullch> to avoid the blessing. The new SV
7101will be returned and will have a reference count of 1.
7102
7103=cut
7104*/
7105
a0d0e21e 7106SV*
65202027 7107Perl_sv_setref_nv(pTHX_ SV *rv, const char *classname, NV nv)
a0d0e21e
LW
7108{
7109 sv_setnv(newSVrv(rv,classname), nv);
7110 return rv;
7111}
463ee0b2 7112
954c1994
GS
7113/*
7114=for apidoc sv_setref_pvn
7115
7116Copies a string into a new SV, optionally blessing the SV. The length of the
7117string must be specified with C<n>. The C<rv> argument will be upgraded to
7118an RV. That RV will be modified to point to the new SV. The C<classname>
7119argument indicates the package for the blessing. Set C<classname> to
7120C<Nullch> to avoid the blessing. The new SV will be returned and will have
7121a reference count of 1.
7122
7123Note that C<sv_setref_pv> copies the pointer while this copies the string.
7124
7125=cut
7126*/
7127
a0d0e21e 7128SV*
864dbfa3 7129Perl_sv_setref_pvn(pTHX_ SV *rv, const char *classname, char *pv, STRLEN n)
a0d0e21e
LW
7130{
7131 sv_setpvn(newSVrv(rv,classname), pv, n);
463ee0b2
LW
7132 return rv;
7133}
7134
954c1994
GS
7135/*
7136=for apidoc sv_bless
7137
7138Blesses an SV into a specified package. The SV must be an RV. The package
7139must be designated by its stash (see C<gv_stashpv()>). The reference count
7140of the SV is unaffected.
7141
7142=cut
7143*/
7144
a0d0e21e 7145SV*
864dbfa3 7146Perl_sv_bless(pTHX_ SV *sv, HV *stash)
a0d0e21e 7147{
76e3520e 7148 SV *tmpRef;
a0d0e21e 7149 if (!SvROK(sv))
cea2e8a9 7150 Perl_croak(aTHX_ "Can't bless non-reference value");
76e3520e
GS
7151 tmpRef = SvRV(sv);
7152 if (SvFLAGS(tmpRef) & (SVs_OBJECT|SVf_READONLY)) {
7153 if (SvREADONLY(tmpRef))
cea2e8a9 7154 Perl_croak(aTHX_ PL_no_modify);
76e3520e
GS
7155 if (SvOBJECT(tmpRef)) {
7156 if (SvTYPE(tmpRef) != SVt_PVIO)
3280af22 7157 --PL_sv_objcount;
76e3520e 7158 SvREFCNT_dec(SvSTASH(tmpRef));
2e3febc6 7159 }
a0d0e21e 7160 }
76e3520e
GS
7161 SvOBJECT_on(tmpRef);
7162 if (SvTYPE(tmpRef) != SVt_PVIO)
3280af22 7163 ++PL_sv_objcount;
76e3520e
GS
7164 (void)SvUPGRADE(tmpRef, SVt_PVMG);
7165 SvSTASH(tmpRef) = (HV*)SvREFCNT_inc(stash);
a0d0e21e 7166
2e3febc6
CS
7167 if (Gv_AMG(stash))
7168 SvAMAGIC_on(sv);
7169 else
7170 SvAMAGIC_off(sv);
a0d0e21e
LW
7171
7172 return sv;
7173}
7174
645c22ef
DM
7175/* Downgrades a PVGV to a PVMG.
7176 *
7177 * XXX This function doesn't actually appear to be used anywhere
7178 * DAPM 15-Jun-01
7179 */
7180
76e3520e 7181STATIC void
cea2e8a9 7182S_sv_unglob(pTHX_ SV *sv)
a0d0e21e 7183{
850fabdf
GS
7184 void *xpvmg;
7185
a0d0e21e
LW
7186 assert(SvTYPE(sv) == SVt_PVGV);
7187 SvFAKE_off(sv);
7188 if (GvGP(sv))
1edc1566 7189 gp_free((GV*)sv);
e826b3c7
GS
7190 if (GvSTASH(sv)) {
7191 SvREFCNT_dec(GvSTASH(sv));
7192 GvSTASH(sv) = Nullhv;
7193 }
14befaf4 7194 sv_unmagic(sv, PERL_MAGIC_glob);
a0d0e21e 7195 Safefree(GvNAME(sv));
a5f75d66 7196 GvMULTI_off(sv);
850fabdf
GS
7197
7198 /* need to keep SvANY(sv) in the right arena */
7199 xpvmg = new_XPVMG();
7200 StructCopy(SvANY(sv), xpvmg, XPVMG);
7201 del_XPVGV(SvANY(sv));
7202 SvANY(sv) = xpvmg;
7203
a0d0e21e
LW
7204 SvFLAGS(sv) &= ~SVTYPEMASK;
7205 SvFLAGS(sv) |= SVt_PVMG;
7206}
7207
954c1994 7208/*
840a7b70 7209=for apidoc sv_unref_flags
954c1994
GS
7210
7211Unsets the RV status of the SV, and decrements the reference count of
7212whatever was being referenced by the RV. This can almost be thought of
840a7b70
IZ
7213as a reversal of C<newSVrv>. The C<cflags> argument can contain
7214C<SV_IMMEDIATE_UNREF> to force the reference count to be decremented
7215(otherwise the decrementing is conditional on the reference count being
7216different from one or the reference being a readonly SV).
7889fe52 7217See C<SvROK_off>.
954c1994
GS
7218
7219=cut
7220*/
7221
ed6116ce 7222void
840a7b70 7223Perl_sv_unref_flags(pTHX_ SV *sv, U32 flags)
ed6116ce 7224{
a0d0e21e 7225 SV* rv = SvRV(sv);
810b8aa5
GS
7226
7227 if (SvWEAKREF(sv)) {
7228 sv_del_backref(sv);
7229 SvWEAKREF_off(sv);
7230 SvRV(sv) = 0;
7231 return;
7232 }
ed6116ce
LW
7233 SvRV(sv) = 0;
7234 SvROK_off(sv);
840a7b70 7235 if (SvREFCNT(rv) != 1 || SvREADONLY(rv) || flags) /* SV_IMMEDIATE_UNREF */
4633a7c4 7236 SvREFCNT_dec(rv);
840a7b70 7237 else /* XXX Hack, but hard to make $a=$a->[1] work otherwise */
4633a7c4 7238 sv_2mortal(rv); /* Schedule for freeing later */
ed6116ce 7239}
8990e307 7240
840a7b70
IZ
7241/*
7242=for apidoc sv_unref
7243
7244Unsets the RV status of the SV, and decrements the reference count of
7245whatever was being referenced by the RV. This can almost be thought of
7246as a reversal of C<newSVrv>. This is C<sv_unref_flags> with the C<flag>
7889fe52 7247being zero. See C<SvROK_off>.
840a7b70
IZ
7248
7249=cut
7250*/
7251
7252void
7253Perl_sv_unref(pTHX_ SV *sv)
7254{
7255 sv_unref_flags(sv, 0);
7256}
7257
645c22ef
DM
7258/*
7259=for apidoc sv_taint
7260
7261Taint an SV. Use C<SvTAINTED_on> instead.
7262=cut
7263*/
7264
bbce6d69 7265void
864dbfa3 7266Perl_sv_taint(pTHX_ SV *sv)
bbce6d69 7267{
14befaf4 7268 sv_magic((sv), Nullsv, PERL_MAGIC_taint, Nullch, 0);
bbce6d69 7269}
7270
645c22ef
DM
7271/*
7272=for apidoc sv_untaint
7273
7274Untaint an SV. Use C<SvTAINTED_off> instead.
7275=cut
7276*/
7277
bbce6d69 7278void
864dbfa3 7279Perl_sv_untaint(pTHX_ SV *sv)
bbce6d69 7280{
13f57bf8 7281 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
14befaf4 7282 MAGIC *mg = mg_find(sv, PERL_MAGIC_taint);
36477c24 7283 if (mg)
565764a8 7284 mg->mg_len &= ~1;
36477c24 7285 }
bbce6d69 7286}
7287
645c22ef
DM
7288/*
7289=for apidoc sv_tainted
7290
7291Test an SV for taintedness. Use C<SvTAINTED> instead.
7292=cut
7293*/
7294
bbce6d69 7295bool
864dbfa3 7296Perl_sv_tainted(pTHX_ SV *sv)
bbce6d69 7297{
13f57bf8 7298 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
14befaf4 7299 MAGIC *mg = mg_find(sv, PERL_MAGIC_taint);
155aba94 7300 if (mg && ((mg->mg_len & 1) || ((mg->mg_len & 2) && mg->mg_obj == sv)))
36477c24 7301 return TRUE;
7302 }
7303 return FALSE;
bbce6d69 7304}
7305
954c1994
GS
7306/*
7307=for apidoc sv_setpviv
7308
7309Copies an integer into the given SV, also updating its string value.
7310Does not handle 'set' magic. See C<sv_setpviv_mg>.
7311
7312=cut
7313*/
7314
84902520 7315void
864dbfa3 7316Perl_sv_setpviv(pTHX_ SV *sv, IV iv)
84902520 7317{
25da4f38
IZ
7318 char buf[TYPE_CHARS(UV)];
7319 char *ebuf;
7320 char *ptr = uiv_2buf(buf, iv, 0, 0, &ebuf);
84902520 7321
25da4f38 7322 sv_setpvn(sv, ptr, ebuf - ptr);
84902520
TB
7323}
7324
954c1994
GS
7325/*
7326=for apidoc sv_setpviv_mg
7327
7328Like C<sv_setpviv>, but also handles 'set' magic.
7329
7330=cut
7331*/
7332
ef50df4b 7333void
864dbfa3 7334Perl_sv_setpviv_mg(pTHX_ SV *sv, IV iv)
ef50df4b 7335{
25da4f38
IZ
7336 char buf[TYPE_CHARS(UV)];
7337 char *ebuf;
7338 char *ptr = uiv_2buf(buf, iv, 0, 0, &ebuf);
7339
7340 sv_setpvn(sv, ptr, ebuf - ptr);
ef50df4b
GS
7341 SvSETMAGIC(sv);
7342}
7343
cea2e8a9 7344#if defined(PERL_IMPLICIT_CONTEXT)
645c22ef
DM
7345
7346/* pTHX_ magic can't cope with varargs, so this is a no-context
7347 * version of the main function, (which may itself be aliased to us).
7348 * Don't access this version directly.
7349 */
7350
cea2e8a9
GS
7351void
7352Perl_sv_setpvf_nocontext(SV *sv, const char* pat, ...)
7353{
7354 dTHX;
7355 va_list args;
7356 va_start(args, pat);
c5be433b 7357 sv_vsetpvf(sv, pat, &args);
cea2e8a9
GS
7358 va_end(args);
7359}
7360
645c22ef
DM
7361/* pTHX_ magic can't cope with varargs, so this is a no-context
7362 * version of the main function, (which may itself be aliased to us).
7363 * Don't access this version directly.
7364 */
cea2e8a9
GS
7365
7366void
7367Perl_sv_setpvf_mg_nocontext(SV *sv, const char* pat, ...)
7368{
7369 dTHX;
7370 va_list args;
7371 va_start(args, pat);
c5be433b 7372 sv_vsetpvf_mg(sv, pat, &args);
cea2e8a9 7373 va_end(args);
cea2e8a9
GS
7374}
7375#endif
7376
954c1994
GS
7377/*
7378=for apidoc sv_setpvf
7379
7380Processes its arguments like C<sprintf> and sets an SV to the formatted
7381output. Does not handle 'set' magic. See C<sv_setpvf_mg>.
7382
7383=cut
7384*/
7385
46fc3d4c 7386void
864dbfa3 7387Perl_sv_setpvf(pTHX_ SV *sv, const char* pat, ...)
46fc3d4c 7388{
7389 va_list args;
46fc3d4c 7390 va_start(args, pat);
c5be433b 7391 sv_vsetpvf(sv, pat, &args);
46fc3d4c 7392 va_end(args);
7393}
7394
645c22ef
DM
7395/* backend for C<sv_setpvf> and C<sv_setpvf_nocontext> */
7396
c5be433b
GS
7397void
7398Perl_sv_vsetpvf(pTHX_ SV *sv, const char* pat, va_list* args)
7399{
7400 sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*));
7401}
ef50df4b 7402
954c1994
GS
7403/*
7404=for apidoc sv_setpvf_mg
7405
7406Like C<sv_setpvf>, but also handles 'set' magic.
7407
7408=cut
7409*/
7410
ef50df4b 7411void
864dbfa3 7412Perl_sv_setpvf_mg(pTHX_ SV *sv, const char* pat, ...)
ef50df4b
GS
7413{
7414 va_list args;
ef50df4b 7415 va_start(args, pat);
c5be433b 7416 sv_vsetpvf_mg(sv, pat, &args);
ef50df4b 7417 va_end(args);
c5be433b
GS
7418}
7419
645c22ef
DM
7420/* backend for C<sv_setpvf_mg> C<setpvf_mg_nocontext> */
7421
c5be433b
GS
7422void
7423Perl_sv_vsetpvf_mg(pTHX_ SV *sv, const char* pat, va_list* args)
7424{
7425 sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*));
ef50df4b
GS
7426 SvSETMAGIC(sv);
7427}
7428
cea2e8a9 7429#if defined(PERL_IMPLICIT_CONTEXT)
645c22ef
DM
7430
7431/* pTHX_ magic can't cope with varargs, so this is a no-context
7432 * version of the main function, (which may itself be aliased to us).
7433 * Don't access this version directly.
7434 */
7435
cea2e8a9
GS
7436void
7437Perl_sv_catpvf_nocontext(SV *sv, const char* pat, ...)
7438{
7439 dTHX;
7440 va_list args;
7441 va_start(args, pat);
c5be433b 7442 sv_vcatpvf(sv, pat, &args);
cea2e8a9
GS
7443 va_end(args);
7444}
7445
645c22ef
DM
7446/* pTHX_ magic can't cope with varargs, so this is a no-context
7447 * version of the main function, (which may itself be aliased to us).
7448 * Don't access this version directly.
7449 */
7450
cea2e8a9
GS
7451void
7452Perl_sv_catpvf_mg_nocontext(SV *sv, const char* pat, ...)
7453{
7454 dTHX;
7455 va_list args;
7456 va_start(args, pat);
c5be433b 7457 sv_vcatpvf_mg(sv, pat, &args);
cea2e8a9 7458 va_end(args);
cea2e8a9
GS
7459}
7460#endif
7461
954c1994
GS
7462/*
7463=for apidoc sv_catpvf
7464
d5ce4a7c
GA
7465Processes its arguments like C<sprintf> and appends the formatted
7466output to an SV. If the appended data contains "wide" characters
7467(including, but not limited to, SVs with a UTF-8 PV formatted with %s,
7468and characters >255 formatted with %c), the original SV might get
7469upgraded to UTF-8. Handles 'get' magic, but not 'set' magic.
7470C<SvSETMAGIC()> must typically be called after calling this function
7471to handle 'set' magic.
954c1994 7472
d5ce4a7c 7473=cut */
954c1994 7474
46fc3d4c 7475void
864dbfa3 7476Perl_sv_catpvf(pTHX_ SV *sv, const char* pat, ...)
46fc3d4c 7477{
7478 va_list args;
46fc3d4c 7479 va_start(args, pat);
c5be433b 7480 sv_vcatpvf(sv, pat, &args);
46fc3d4c 7481 va_end(args);
7482}
7483
645c22ef
DM
7484/* backend for C<sv_catpvf> and C<catpvf_mg_nocontext> */
7485
ef50df4b 7486void
c5be433b
GS
7487Perl_sv_vcatpvf(pTHX_ SV *sv, const char* pat, va_list* args)
7488{
7489 sv_vcatpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*));
7490}
7491
954c1994
GS
7492/*
7493=for apidoc sv_catpvf_mg
7494
7495Like C<sv_catpvf>, but also handles 'set' magic.
7496
7497=cut
7498*/
7499
c5be433b 7500void
864dbfa3 7501Perl_sv_catpvf_mg(pTHX_ SV *sv, const char* pat, ...)
ef50df4b
GS
7502{
7503 va_list args;
ef50df4b 7504 va_start(args, pat);
c5be433b 7505 sv_vcatpvf_mg(sv, pat, &args);
ef50df4b 7506 va_end(args);
c5be433b
GS
7507}
7508
645c22ef
DM
7509/* backend for C<catpvf_mg> and C<catpvf_mg_nocontext> */
7510
c5be433b
GS
7511void
7512Perl_sv_vcatpvf_mg(pTHX_ SV *sv, const char* pat, va_list* args)
7513{
7514 sv_vcatpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*));
ef50df4b
GS
7515 SvSETMAGIC(sv);
7516}
7517
954c1994
GS
7518/*
7519=for apidoc sv_vsetpvfn
7520
7521Works like C<vcatpvfn> but copies the text into the SV instead of
7522appending it.
7523
645c22ef
DM
7524Usually used via one of its frontends C<sv_setpvf> and C<sv_setpvf_mg>.
7525
954c1994
GS
7526=cut
7527*/
7528
46fc3d4c 7529void
7d5ea4e7 7530Perl_sv_vsetpvfn(pTHX_ SV *sv, const char *pat, STRLEN patlen, va_list *args, SV **svargs, I32 svmax, bool *maybe_tainted)
46fc3d4c 7531{
7532 sv_setpvn(sv, "", 0);
7d5ea4e7 7533 sv_vcatpvfn(sv, pat, patlen, args, svargs, svmax, maybe_tainted);
46fc3d4c 7534}
7535
645c22ef
DM
7536/* private function for use in sv_vcatpvfn via the EXPECT_NUMBER macro */
7537
2d00ba3b 7538STATIC I32
9dd79c3f 7539S_expect_number(pTHX_ char** pattern)
211dfcf1
HS
7540{
7541 I32 var = 0;
7542 switch (**pattern) {
7543 case '1': case '2': case '3':
7544 case '4': case '5': case '6':
7545 case '7': case '8': case '9':
7546 while (isDIGIT(**pattern))
7547 var = var * 10 + (*(*pattern)++ - '0');
7548 }
7549 return var;
7550}
9dd79c3f 7551#define EXPECT_NUMBER(pattern, var) (var = S_expect_number(aTHX_ &pattern))
211dfcf1 7552
954c1994
GS
7553/*
7554=for apidoc sv_vcatpvfn
7555
7556Processes its arguments like C<vsprintf> and appends the formatted output
7557to an SV. Uses an array of SVs if the C style variable argument list is
7558missing (NULL). When running with taint checks enabled, indicates via
7559C<maybe_tainted> if results are untrustworthy (often due to the use of
7560locales).
7561
645c22ef
DM
7562Usually used via one of its frontends C<sv_catpvf> and C<sv_catpvf_mg>.
7563
954c1994
GS
7564=cut
7565*/
7566
46fc3d4c 7567void
7d5ea4e7 7568Perl_sv_vcatpvfn(pTHX_ SV *sv, const char *pat, STRLEN patlen, va_list *args, SV **svargs, I32 svmax, bool *maybe_tainted)
46fc3d4c 7569{
7570 char *p;
7571 char *q;
7572 char *patend;
fc36a67e 7573 STRLEN origlen;
46fc3d4c 7574 I32 svix = 0;
c635e13b 7575 static char nullstr[] = "(null)";
9c5ffd7c 7576 SV *argsv = Nullsv;
46fc3d4c 7577
7578 /* no matter what, this is a string now */
fc36a67e 7579 (void)SvPV_force(sv, origlen);
46fc3d4c 7580
fc36a67e 7581 /* special-case "", "%s", and "%_" */
46fc3d4c 7582 if (patlen == 0)
7583 return;
fc36a67e 7584 if (patlen == 2 && pat[0] == '%') {
7585 switch (pat[1]) {
7586 case 's':
c635e13b 7587 if (args) {
7588 char *s = va_arg(*args, char*);
7589 sv_catpv(sv, s ? s : nullstr);
7590 }
7e2040f0 7591 else if (svix < svmax) {
fc36a67e 7592 sv_catsv(sv, *svargs);
7e2040f0
GS
7593 if (DO_UTF8(*svargs))
7594 SvUTF8_on(sv);
7595 }
fc36a67e 7596 return;
7597 case '_':
7598 if (args) {
7e2040f0
GS
7599 argsv = va_arg(*args, SV*);
7600 sv_catsv(sv, argsv);
7601 if (DO_UTF8(argsv))
7602 SvUTF8_on(sv);
fc36a67e 7603 return;
7604 }
7605 /* See comment on '_' below */
7606 break;
7607 }
46fc3d4c 7608 }
7609
7610 patend = (char*)pat + patlen;
7611 for (p = (char*)pat; p < patend; p = q) {
7612 bool alt = FALSE;
7613 bool left = FALSE;
b22c7a20 7614 bool vectorize = FALSE;
211dfcf1 7615 bool vectorarg = FALSE;
b2e23cf9 7616 bool vec_utf = FALSE;
46fc3d4c 7617 char fill = ' ';
7618 char plus = 0;
7619 char intsize = 0;
7620 STRLEN width = 0;
fc36a67e 7621 STRLEN zeros = 0;
46fc3d4c 7622 bool has_precis = FALSE;
7623 STRLEN precis = 0;
7e2040f0 7624 bool is_utf = FALSE;
eb3fce90 7625
46fc3d4c 7626 char esignbuf[4];
ad391ad9 7627 U8 utf8buf[UTF8_MAXLEN+1];
46fc3d4c 7628 STRLEN esignlen = 0;
7629
7630 char *eptr = Nullch;
fc36a67e 7631 STRLEN elen = 0;
089c015b
JH
7632 /* Times 4: a decimal digit takes more than 3 binary digits.
7633 * NV_DIG: mantissa takes than many decimal digits.
7634 * Plus 32: Playing safe. */
7635 char ebuf[IV_DIG * 4 + NV_DIG + 32];
2d4389e4
JH
7636 /* large enough for "%#.#f" --chip */
7637 /* what about long double NVs? --jhi */
b22c7a20
GS
7638
7639 SV *vecsv;
a05b299f 7640 U8 *vecstr = Null(U8*);
b22c7a20 7641 STRLEN veclen = 0;
46fc3d4c 7642 char c;
7643 int i;
9c5ffd7c 7644 unsigned base = 0;
46fc3d4c 7645 IV iv;
7646 UV uv;
65202027 7647 NV nv;
46fc3d4c 7648 STRLEN have;
7649 STRLEN need;
7650 STRLEN gap;
b22c7a20
GS
7651 char *dotstr = ".";
7652 STRLEN dotstrlen = 1;
211dfcf1 7653 I32 efix = 0; /* explicit format parameter index */
eb3fce90 7654 I32 ewix = 0; /* explicit width index */
211dfcf1
HS
7655 I32 epix = 0; /* explicit precision index */
7656 I32 evix = 0; /* explicit vector index */
eb3fce90 7657 bool asterisk = FALSE;
46fc3d4c 7658
211dfcf1 7659 /* echo everything up to the next format specification */
46fc3d4c 7660 for (q = p; q < patend && *q != '%'; ++q) ;
7661 if (q > p) {
7662 sv_catpvn(sv, p, q - p);
7663 p = q;
7664 }
7665 if (q++ >= patend)
7666 break;
7667
211dfcf1
HS
7668/*
7669 We allow format specification elements in this order:
7670 \d+\$ explicit format parameter index
7671 [-+ 0#]+ flags
7672 \*?(\d+\$)?v vector with optional (optionally specified) arg
7673 \d+|\*(\d+\$)? width using optional (optionally specified) arg
7674 \.(\d*|\*(\d+\$)?) precision using optional (optionally specified) arg
7675 [hlqLV] size
7676 [%bcdefginopsux_DFOUX] format (mandatory)
7677*/
7678 if (EXPECT_NUMBER(q, width)) {
7679 if (*q == '$') {
7680 ++q;
7681 efix = width;
7682 } else {
7683 goto gotwidth;
7684 }
7685 }
7686
fc36a67e 7687 /* FLAGS */
7688
46fc3d4c 7689 while (*q) {
7690 switch (*q) {
7691 case ' ':
7692 case '+':
7693 plus = *q++;
7694 continue;
7695
7696 case '-':
7697 left = TRUE;
7698 q++;
7699 continue;
7700
7701 case '0':
7702 fill = *q++;
7703 continue;
7704
7705 case '#':
7706 alt = TRUE;
7707 q++;
7708 continue;
7709
fc36a67e 7710 default:
7711 break;
7712 }
7713 break;
7714 }
46fc3d4c 7715
211dfcf1 7716 tryasterisk:
eb3fce90 7717 if (*q == '*') {
211dfcf1
HS
7718 q++;
7719 if (EXPECT_NUMBER(q, ewix))
7720 if (*q++ != '$')
7721 goto unknown;
eb3fce90 7722 asterisk = TRUE;
211dfcf1
HS
7723 }
7724 if (*q == 'v') {
eb3fce90 7725 q++;
211dfcf1
HS
7726 if (vectorize)
7727 goto unknown;
9cbac4c7 7728 if ((vectorarg = asterisk)) {
211dfcf1
HS
7729 evix = ewix;
7730 ewix = 0;
7731 asterisk = FALSE;
7732 }
7733 vectorize = TRUE;
7734 goto tryasterisk;
eb3fce90
JH
7735 }
7736
211dfcf1
HS
7737 if (!asterisk)
7738 EXPECT_NUMBER(q, width);
7739
7740 if (vectorize) {
7741 if (vectorarg) {
7742 if (args)
7743 vecsv = va_arg(*args, SV*);
7744 else
7745 vecsv = (evix ? evix <= svmax : svix < svmax) ?
7746 svargs[ewix ? ewix-1 : svix++] : &PL_sv_undef;
4459522c 7747 dotstr = SvPVx(vecsv, dotstrlen);
211dfcf1
HS
7748 if (DO_UTF8(vecsv))
7749 is_utf = TRUE;
7750 }
7751 if (args) {
7752 vecsv = va_arg(*args, SV*);
7753 vecstr = (U8*)SvPVx(vecsv,veclen);
b2e23cf9 7754 vec_utf = DO_UTF8(vecsv);
eb3fce90 7755 }
211dfcf1
HS
7756 else if (efix ? efix <= svmax : svix < svmax) {
7757 vecsv = svargs[efix ? efix-1 : svix++];
7758 vecstr = (U8*)SvPVx(vecsv,veclen);
b2e23cf9 7759 vec_utf = DO_UTF8(vecsv);
211dfcf1
HS
7760 }
7761 else {
7762 vecstr = (U8*)"";
7763 veclen = 0;
7764 }
eb3fce90 7765 }
fc36a67e 7766
eb3fce90 7767 if (asterisk) {
fc36a67e 7768 if (args)
7769 i = va_arg(*args, int);
7770 else
eb3fce90
JH
7771 i = (ewix ? ewix <= svmax : svix < svmax) ?
7772 SvIVx(svargs[ewix ? ewix-1 : svix++]) : 0;
fc36a67e 7773 left |= (i < 0);
7774 width = (i < 0) ? -i : i;
fc36a67e 7775 }
211dfcf1 7776 gotwidth:
fc36a67e 7777
7778 /* PRECISION */
46fc3d4c 7779
fc36a67e 7780 if (*q == '.') {
7781 q++;
7782 if (*q == '*') {
211dfcf1
HS
7783 q++;
7784 if (EXPECT_NUMBER(q, epix) && *q++ != '$')
7785 goto unknown;
46fc3d4c 7786 if (args)
7787 i = va_arg(*args, int);
7788 else
eb3fce90
JH
7789 i = (ewix ? ewix <= svmax : svix < svmax)
7790 ? SvIVx(svargs[ewix ? ewix-1 : svix++]) : 0;
fc36a67e 7791 precis = (i < 0) ? 0 : i;
fc36a67e 7792 }
7793 else {
7794 precis = 0;
7795 while (isDIGIT(*q))
7796 precis = precis * 10 + (*q++ - '0');
7797 }
7798 has_precis = TRUE;
7799 }
46fc3d4c 7800
fc36a67e 7801 /* SIZE */
46fc3d4c 7802
fc36a67e 7803 switch (*q) {
e5c81feb 7804#if defined(HAS_QUAD) || (defined(HAS_LONG_DOUBLE) && defined(USE_LONG_DOUBLE))
6f9bb7fd 7805 case 'L': /* Ld */
e5c81feb
JH
7806 /* FALL THROUGH */
7807#endif
7808#ifdef HAS_QUAD
6f9bb7fd
GS
7809 case 'q': /* qd */
7810 intsize = 'q';
7811 q++;
7812 break;
7813#endif
fc36a67e 7814 case 'l':
e5c81feb
JH
7815#if defined(HAS_QUAD) || (defined(HAS_LONG_DOUBLE) && defined(USE_LONG_DOUBLE))
7816 if (*(q + 1) == 'l') { /* lld, llf */
fc36a67e 7817 intsize = 'q';
7818 q += 2;
46fc3d4c 7819 break;
cf2093f6 7820 }
fc36a67e 7821#endif
6f9bb7fd 7822 /* FALL THROUGH */
fc36a67e 7823 case 'h':
cf2093f6 7824 /* FALL THROUGH */
fc36a67e 7825 case 'V':
7826 intsize = *q++;
46fc3d4c 7827 break;
7828 }
7829
fc36a67e 7830 /* CONVERSION */
7831
211dfcf1
HS
7832 if (*q == '%') {
7833 eptr = q++;
7834 elen = 1;
7835 goto string;
7836 }
7837
7838 if (!args)
7839 argsv = (efix ? efix <= svmax : svix < svmax) ?
7840 svargs[efix ? efix-1 : svix++] : &PL_sv_undef;
7841
46fc3d4c 7842 switch (c = *q++) {
7843
7844 /* STRINGS */
7845
46fc3d4c 7846 case 'c':
211dfcf1 7847 uv = args ? va_arg(*args, int) : SvIVx(argsv);
1bd104fb
JH
7848 if ((uv > 255 ||
7849 (!UNI_IS_INVARIANT(uv) && SvUTF8(sv)))
0064a8a9 7850 && !IN_BYTES) {
dfe13c55 7851 eptr = (char*)utf8buf;
9041c2e3 7852 elen = uvchr_to_utf8((U8*)eptr, uv) - utf8buf;
7e2040f0
GS
7853 is_utf = TRUE;
7854 }
7855 else {
7856 c = (char)uv;
7857 eptr = &c;
7858 elen = 1;
a0ed51b3 7859 }
46fc3d4c 7860 goto string;
7861
46fc3d4c 7862 case 's':
7863 if (args) {
fc36a67e 7864 eptr = va_arg(*args, char*);
c635e13b 7865 if (eptr)
1d7c1841
GS
7866#ifdef MACOS_TRADITIONAL
7867 /* On MacOS, %#s format is used for Pascal strings */
7868 if (alt)
7869 elen = *eptr++;
7870 else
7871#endif
c635e13b 7872 elen = strlen(eptr);
7873 else {
7874 eptr = nullstr;
7875 elen = sizeof nullstr - 1;
7876 }
46fc3d4c 7877 }
211dfcf1 7878 else {
7e2040f0
GS
7879 eptr = SvPVx(argsv, elen);
7880 if (DO_UTF8(argsv)) {
a0ed51b3
LW
7881 if (has_precis && precis < elen) {
7882 I32 p = precis;
7e2040f0 7883 sv_pos_u2b(argsv, &p, 0); /* sticks at end */
a0ed51b3
LW
7884 precis = p;
7885 }
7886 if (width) { /* fudge width (can't fudge elen) */
7e2040f0 7887 width += elen - sv_len_utf8(argsv);
a0ed51b3 7888 }
7e2040f0 7889 is_utf = TRUE;
a0ed51b3
LW
7890 }
7891 }
46fc3d4c 7892 goto string;
7893
fc36a67e 7894 case '_':
7895 /*
7896 * The "%_" hack might have to be changed someday,
7897 * if ISO or ANSI decide to use '_' for something.
7898 * So we keep it hidden from users' code.
7899 */
7900 if (!args)
7901 goto unknown;
211dfcf1 7902 argsv = va_arg(*args, SV*);
7e2040f0
GS
7903 eptr = SvPVx(argsv, elen);
7904 if (DO_UTF8(argsv))
7905 is_utf = TRUE;
fc36a67e 7906
46fc3d4c 7907 string:
b22c7a20 7908 vectorize = FALSE;
46fc3d4c 7909 if (has_precis && elen > precis)
7910 elen = precis;
7911 break;
7912
7913 /* INTEGERS */
7914
fc36a67e 7915 case 'p':
c2e66d9e
GS
7916 if (alt)
7917 goto unknown;
211dfcf1 7918 uv = PTR2UV(args ? va_arg(*args, void*) : argsv);
fc36a67e 7919 base = 16;
7920 goto integer;
7921
46fc3d4c 7922 case 'D':
29fe7a80 7923#ifdef IV_IS_QUAD
22f3ae8c 7924 intsize = 'q';
29fe7a80 7925#else
46fc3d4c 7926 intsize = 'l';
29fe7a80 7927#endif
46fc3d4c 7928 /* FALL THROUGH */
7929 case 'd':
7930 case 'i':
b22c7a20 7931 if (vectorize) {
ba210ebe 7932 STRLEN ulen;
211dfcf1
HS
7933 if (!veclen)
7934 continue;
b2e23cf9 7935 if (vec_utf)
9041c2e3 7936 iv = (IV)utf8n_to_uvchr(vecstr, veclen, &ulen, 0);
b22c7a20 7937 else {
a05b299f 7938 iv = *vecstr;
b22c7a20
GS
7939 ulen = 1;
7940 }
7941 vecstr += ulen;
7942 veclen -= ulen;
7943 }
7944 else if (args) {
46fc3d4c 7945 switch (intsize) {
7946 case 'h': iv = (short)va_arg(*args, int); break;
7947 default: iv = va_arg(*args, int); break;
7948 case 'l': iv = va_arg(*args, long); break;
fc36a67e 7949 case 'V': iv = va_arg(*args, IV); break;
cf2093f6
JH
7950#ifdef HAS_QUAD
7951 case 'q': iv = va_arg(*args, Quad_t); break;
7952#endif
46fc3d4c 7953 }
7954 }
7955 else {
211dfcf1 7956 iv = SvIVx(argsv);
46fc3d4c 7957 switch (intsize) {
7958 case 'h': iv = (short)iv; break;
be28567c 7959 default: break;
46fc3d4c 7960 case 'l': iv = (long)iv; break;
fc36a67e 7961 case 'V': break;
cf2093f6
JH
7962#ifdef HAS_QUAD
7963 case 'q': iv = (Quad_t)iv; break;
7964#endif
46fc3d4c 7965 }
7966 }
7967 if (iv >= 0) {
7968 uv = iv;
7969 if (plus)
7970 esignbuf[esignlen++] = plus;
7971 }
7972 else {
7973 uv = -iv;
7974 esignbuf[esignlen++] = '-';
7975 }
7976 base = 10;
7977 goto integer;
7978
fc36a67e 7979 case 'U':
29fe7a80 7980#ifdef IV_IS_QUAD
22f3ae8c 7981 intsize = 'q';
29fe7a80 7982#else
fc36a67e 7983 intsize = 'l';
29fe7a80 7984#endif
fc36a67e 7985 /* FALL THROUGH */
7986 case 'u':
7987 base = 10;
7988 goto uns_integer;
7989
4f19785b
WSI
7990 case 'b':
7991 base = 2;
7992 goto uns_integer;
7993
46fc3d4c 7994 case 'O':
29fe7a80 7995#ifdef IV_IS_QUAD
22f3ae8c 7996 intsize = 'q';
29fe7a80 7997#else
46fc3d4c 7998 intsize = 'l';
29fe7a80 7999#endif
46fc3d4c 8000 /* FALL THROUGH */
8001 case 'o':
8002 base = 8;
8003 goto uns_integer;
8004
8005 case 'X':
46fc3d4c 8006 case 'x':
8007 base = 16;
46fc3d4c 8008
8009 uns_integer:
b22c7a20 8010 if (vectorize) {
ba210ebe 8011 STRLEN ulen;
b22c7a20 8012 vector:
211dfcf1
HS
8013 if (!veclen)
8014 continue;
b2e23cf9 8015 if (vec_utf)
9041c2e3 8016 uv = utf8n_to_uvchr(vecstr, veclen, &ulen, 0);
b22c7a20 8017 else {
a05b299f 8018 uv = *vecstr;
b22c7a20
GS
8019 ulen = 1;
8020 }
8021 vecstr += ulen;
8022 veclen -= ulen;
8023 }
8024 else if (args) {
46fc3d4c 8025 switch (intsize) {
8026 case 'h': uv = (unsigned short)va_arg(*args, unsigned); break;
8027 default: uv = va_arg(*args, unsigned); break;
8028 case 'l': uv = va_arg(*args, unsigned long); break;
fc36a67e 8029 case 'V': uv = va_arg(*args, UV); break;
cf2093f6
JH
8030#ifdef HAS_QUAD
8031 case 'q': uv = va_arg(*args, Quad_t); break;
8032#endif
46fc3d4c 8033 }
8034 }
8035 else {
211dfcf1 8036 uv = SvUVx(argsv);
46fc3d4c 8037 switch (intsize) {
8038 case 'h': uv = (unsigned short)uv; break;
be28567c 8039 default: break;
46fc3d4c 8040 case 'l': uv = (unsigned long)uv; break;
fc36a67e 8041 case 'V': break;
cf2093f6
JH
8042#ifdef HAS_QUAD
8043 case 'q': uv = (Quad_t)uv; break;
8044#endif
46fc3d4c 8045 }
8046 }
8047
8048 integer:
46fc3d4c 8049 eptr = ebuf + sizeof ebuf;
fc36a67e 8050 switch (base) {
8051 unsigned dig;
8052 case 16:
c10ed8b9
HS
8053 if (!uv)
8054 alt = FALSE;
1d7c1841
GS
8055 p = (char*)((c == 'X')
8056 ? "0123456789ABCDEF" : "0123456789abcdef");
fc36a67e 8057 do {
8058 dig = uv & 15;
8059 *--eptr = p[dig];
8060 } while (uv >>= 4);
8061 if (alt) {
46fc3d4c 8062 esignbuf[esignlen++] = '0';
fc36a67e 8063 esignbuf[esignlen++] = c; /* 'x' or 'X' */
46fc3d4c 8064 }
fc36a67e 8065 break;
8066 case 8:
8067 do {
8068 dig = uv & 7;
8069 *--eptr = '0' + dig;
8070 } while (uv >>= 3);
8071 if (alt && *eptr != '0')
8072 *--eptr = '0';
8073 break;
4f19785b
WSI
8074 case 2:
8075 do {
8076 dig = uv & 1;
8077 *--eptr = '0' + dig;
8078 } while (uv >>= 1);
eda88b6d
JH
8079 if (alt) {
8080 esignbuf[esignlen++] = '0';
7481bb52 8081 esignbuf[esignlen++] = 'b';
eda88b6d 8082 }
4f19785b 8083 break;
fc36a67e 8084 default: /* it had better be ten or less */
6bc102ca 8085#if defined(PERL_Y2KWARN)
e476b1b5 8086 if (ckWARN(WARN_Y2K)) {
6bc102ca
GS
8087 STRLEN n;
8088 char *s = SvPV(sv,n);
8089 if (n >= 2 && s[n-2] == '1' && s[n-1] == '9'
8090 && (n == 2 || !isDIGIT(s[n-3])))
8091 {
e476b1b5 8092 Perl_warner(aTHX_ WARN_Y2K,
6bc102ca
GS
8093 "Possible Y2K bug: %%%c %s",
8094 c, "format string following '19'");
8095 }
8096 }
8097#endif
fc36a67e 8098 do {
8099 dig = uv % base;
8100 *--eptr = '0' + dig;
8101 } while (uv /= base);
8102 break;
46fc3d4c 8103 }
8104 elen = (ebuf + sizeof ebuf) - eptr;
c10ed8b9
HS
8105 if (has_precis) {
8106 if (precis > elen)
8107 zeros = precis - elen;
8108 else if (precis == 0 && elen == 1 && *eptr == '0')
8109 elen = 0;
8110 }
46fc3d4c 8111 break;
8112
8113 /* FLOATING POINT */
8114
fc36a67e 8115 case 'F':
8116 c = 'f'; /* maybe %F isn't supported here */
8117 /* FALL THROUGH */
46fc3d4c 8118 case 'e': case 'E':
fc36a67e 8119 case 'f':
46fc3d4c 8120 case 'g': case 'G':
8121
8122 /* This is evil, but floating point is even more evil */
8123
b22c7a20 8124 vectorize = FALSE;
211dfcf1 8125 nv = args ? va_arg(*args, NV) : SvNVx(argsv);
fc36a67e 8126
8127 need = 0;
8128 if (c != 'e' && c != 'E') {
8129 i = PERL_INT_MIN;
73b309ea 8130 (void)Perl_frexp(nv, &i);
fc36a67e 8131 if (i == PERL_INT_MIN)
cea2e8a9 8132 Perl_die(aTHX_ "panic: frexp");
c635e13b 8133 if (i > 0)
fc36a67e 8134 need = BIT_DIGITS(i);
8135 }
8136 need += has_precis ? precis : 6; /* known default */
8137 if (need < width)
8138 need = width;
8139
46fc3d4c 8140 need += 20; /* fudge factor */
80252599
GS
8141 if (PL_efloatsize < need) {
8142 Safefree(PL_efloatbuf);
8143 PL_efloatsize = need + 20; /* more fudge */
8144 New(906, PL_efloatbuf, PL_efloatsize, char);
7d5ea4e7 8145 PL_efloatbuf[0] = '\0';
46fc3d4c 8146 }
8147
8148 eptr = ebuf + sizeof ebuf;
8149 *--eptr = '\0';
8150 *--eptr = c;
e5c81feb 8151#if defined(USE_LONG_DOUBLE) && defined(PERL_PRIfldbl)
cf2093f6 8152 {
e5c81feb
JH
8153 /* Copy the one or more characters in a long double
8154 * format before the 'base' ([efgEFG]) character to
8155 * the format string. */
8156 static char const prifldbl[] = PERL_PRIfldbl;
8157 char const *p = prifldbl + sizeof(prifldbl) - 3;
8158 while (p >= prifldbl) { *--eptr = *p--; }
cf2093f6 8159 }
65202027 8160#endif
46fc3d4c 8161 if (has_precis) {
8162 base = precis;
8163 do { *--eptr = '0' + (base % 10); } while (base /= 10);
8164 *--eptr = '.';
8165 }
8166 if (width) {
8167 base = width;
8168 do { *--eptr = '0' + (base % 10); } while (base /= 10);
8169 }
8170 if (fill == '0')
8171 *--eptr = fill;
84902520
TB
8172 if (left)
8173 *--eptr = '-';
46fc3d4c 8174 if (plus)
8175 *--eptr = plus;
8176 if (alt)
8177 *--eptr = '#';
8178 *--eptr = '%';
8179
ff9121f8
JH
8180 /* No taint. Otherwise we are in the strange situation
8181 * where printf() taints but print($float) doesn't.
bda0f7a5 8182 * --jhi */
dd8482fc 8183 (void)sprintf(PL_efloatbuf, eptr, nv);
8af02333 8184
80252599
GS
8185 eptr = PL_efloatbuf;
8186 elen = strlen(PL_efloatbuf);
46fc3d4c 8187 break;
8188
fc36a67e 8189 /* SPECIAL */
8190
8191 case 'n':
b22c7a20 8192 vectorize = FALSE;
fc36a67e 8193 i = SvCUR(sv) - origlen;
8194 if (args) {
c635e13b 8195 switch (intsize) {
8196 case 'h': *(va_arg(*args, short*)) = i; break;
8197 default: *(va_arg(*args, int*)) = i; break;
8198 case 'l': *(va_arg(*args, long*)) = i; break;
8199 case 'V': *(va_arg(*args, IV*)) = i; break;
cf2093f6
JH
8200#ifdef HAS_QUAD
8201 case 'q': *(va_arg(*args, Quad_t*)) = i; break;
8202#endif
c635e13b 8203 }
fc36a67e 8204 }
9dd79c3f 8205 else
211dfcf1 8206 sv_setuv_mg(argsv, (UV)i);
fc36a67e 8207 continue; /* not "break" */
8208
8209 /* UNKNOWN */
8210
46fc3d4c 8211 default:
fc36a67e 8212 unknown:
b22c7a20 8213 vectorize = FALSE;
599cee73 8214 if (!args && ckWARN(WARN_PRINTF) &&
533c011a 8215 (PL_op->op_type == OP_PRTF || PL_op->op_type == OP_SPRINTF)) {
c635e13b 8216 SV *msg = sv_newmortal();
cea2e8a9 8217 Perl_sv_setpvf(aTHX_ msg, "Invalid conversion in %s: ",
533c011a 8218 (PL_op->op_type == OP_PRTF) ? "printf" : "sprintf");
0f4b6630 8219 if (c) {
0f4b6630 8220 if (isPRINT(c))
1c846c1f 8221 Perl_sv_catpvf(aTHX_ msg,
0f4b6630
JH
8222 "\"%%%c\"", c & 0xFF);
8223 else
8224 Perl_sv_catpvf(aTHX_ msg,
57def98f 8225 "\"%%\\%03"UVof"\"",
0f4b6630 8226 (UV)c & 0xFF);
0f4b6630 8227 } else
c635e13b 8228 sv_catpv(msg, "end of string");
894356b3 8229 Perl_warner(aTHX_ WARN_PRINTF, "%"SVf, msg); /* yes, this is reentrant */
c635e13b 8230 }
fb73857a 8231
8232 /* output mangled stuff ... */
8233 if (c == '\0')
8234 --q;
46fc3d4c 8235 eptr = p;
8236 elen = q - p;
fb73857a 8237
8238 /* ... right here, because formatting flags should not apply */
8239 SvGROW(sv, SvCUR(sv) + elen + 1);
8240 p = SvEND(sv);
4459522c 8241 Copy(eptr, p, elen, char);
fb73857a 8242 p += elen;
8243 *p = '\0';
8244 SvCUR(sv) = p - SvPVX(sv);
8245 continue; /* not "break" */
46fc3d4c 8246 }
8247
fc36a67e 8248 have = esignlen + zeros + elen;
46fc3d4c 8249 need = (have > width ? have : width);
8250 gap = need - have;
8251
b22c7a20 8252 SvGROW(sv, SvCUR(sv) + need + dotstrlen + 1);
46fc3d4c 8253 p = SvEND(sv);
8254 if (esignlen && fill == '0') {
8255 for (i = 0; i < esignlen; i++)
8256 *p++ = esignbuf[i];
8257 }
8258 if (gap && !left) {
8259 memset(p, fill, gap);
8260 p += gap;
8261 }
8262 if (esignlen && fill != '0') {
8263 for (i = 0; i < esignlen; i++)
8264 *p++ = esignbuf[i];
8265 }
fc36a67e 8266 if (zeros) {
8267 for (i = zeros; i; i--)
8268 *p++ = '0';
8269 }
46fc3d4c 8270 if (elen) {
4459522c 8271 Copy(eptr, p, elen, char);
46fc3d4c 8272 p += elen;
8273 }
8274 if (gap && left) {
8275 memset(p, ' ', gap);
8276 p += gap;
8277 }
b22c7a20
GS
8278 if (vectorize) {
8279 if (veclen) {
4459522c 8280 Copy(dotstr, p, dotstrlen, char);
b22c7a20
GS
8281 p += dotstrlen;
8282 }
8283 else
8284 vectorize = FALSE; /* done iterating over vecstr */
8285 }
7e2040f0
GS
8286 if (is_utf)
8287 SvUTF8_on(sv);
46fc3d4c 8288 *p = '\0';
8289 SvCUR(sv) = p - SvPVX(sv);
b22c7a20
GS
8290 if (vectorize) {
8291 esignlen = 0;
8292 goto vector;
8293 }
46fc3d4c 8294 }
8295}
51371543 8296
645c22ef
DM
8297/* =========================================================================
8298
8299=head1 Cloning an interpreter
8300
8301All the macros and functions in this section are for the private use of
8302the main function, perl_clone().
8303
8304The foo_dup() functions make an exact copy of an existing foo thinngy.
8305During the course of a cloning, a hash table is used to map old addresses
8306to new addresses. The table is created and manipulated with the
8307ptr_table_* functions.
8308
8309=cut
8310
8311============================================================================*/
8312
8313
1d7c1841
GS
8314#if defined(USE_ITHREADS)
8315
8316#if defined(USE_THREADS)
8317# include "error: USE_THREADS and USE_ITHREADS are incompatible"
8318#endif
8319
1d7c1841
GS
8320#ifndef GpREFCNT_inc
8321# define GpREFCNT_inc(gp) ((gp) ? (++(gp)->gp_refcnt, (gp)) : (GP*)NULL)
8322#endif
8323
8324
8325#define sv_dup_inc(s) SvREFCNT_inc(sv_dup(s))
8326#define av_dup(s) (AV*)sv_dup((SV*)s)
8327#define av_dup_inc(s) (AV*)SvREFCNT_inc(sv_dup((SV*)s))
8328#define hv_dup(s) (HV*)sv_dup((SV*)s)
8329#define hv_dup_inc(s) (HV*)SvREFCNT_inc(sv_dup((SV*)s))
8330#define cv_dup(s) (CV*)sv_dup((SV*)s)
8331#define cv_dup_inc(s) (CV*)SvREFCNT_inc(sv_dup((SV*)s))
8332#define io_dup(s) (IO*)sv_dup((SV*)s)
8333#define io_dup_inc(s) (IO*)SvREFCNT_inc(sv_dup((SV*)s))
8334#define gv_dup(s) (GV*)sv_dup((SV*)s)
8335#define gv_dup_inc(s) (GV*)SvREFCNT_inc(sv_dup((SV*)s))
8336#define SAVEPV(p) (p ? savepv(p) : Nullch)
8337#define SAVEPVN(p,n) (p ? savepvn(p,n) : Nullch)
8338
645c22ef
DM
8339/* duplicate a regexp */
8340
1d7c1841
GS
8341REGEXP *
8342Perl_re_dup(pTHX_ REGEXP *r)
8343{
8344 /* XXX fix when pmop->op_pmregexp becomes shared */
8345 return ReREFCNT_inc(r);
8346}
8347
645c22ef
DM
8348/* duplicate a filke handle */
8349
1d7c1841
GS
8350PerlIO *
8351Perl_fp_dup(pTHX_ PerlIO *fp, char type)
8352{
8353 PerlIO *ret;
8354 if (!fp)
8355 return (PerlIO*)NULL;
8356
8357 /* look for it in the table first */
8358 ret = (PerlIO*)ptr_table_fetch(PL_ptr_table, fp);
8359 if (ret)
8360 return ret;
8361
8362 /* create anew and remember what it is */
5f1a76d0 8363 ret = PerlIO_fdupopen(aTHX_ fp);
1d7c1841
GS
8364 ptr_table_store(PL_ptr_table, fp, ret);
8365 return ret;
8366}
8367
645c22ef
DM
8368/* duplicate a directory handle */
8369
1d7c1841
GS
8370DIR *
8371Perl_dirp_dup(pTHX_ DIR *dp)
8372{
8373 if (!dp)
8374 return (DIR*)NULL;
8375 /* XXX TODO */
8376 return dp;
8377}
8378
645c22ef
DM
8379/* duplictate a typeglob */
8380
1d7c1841
GS
8381GP *
8382Perl_gp_dup(pTHX_ GP *gp)
8383{
8384 GP *ret;
8385 if (!gp)
8386 return (GP*)NULL;
8387 /* look for it in the table first */
8388 ret = (GP*)ptr_table_fetch(PL_ptr_table, gp);
8389 if (ret)
8390 return ret;
8391
8392 /* create anew and remember what it is */
8393 Newz(0, ret, 1, GP);
8394 ptr_table_store(PL_ptr_table, gp, ret);
8395
8396 /* clone */
8397 ret->gp_refcnt = 0; /* must be before any other dups! */
8398 ret->gp_sv = sv_dup_inc(gp->gp_sv);
8399 ret->gp_io = io_dup_inc(gp->gp_io);
8400 ret->gp_form = cv_dup_inc(gp->gp_form);
8401 ret->gp_av = av_dup_inc(gp->gp_av);
8402 ret->gp_hv = hv_dup_inc(gp->gp_hv);
8403 ret->gp_egv = gv_dup(gp->gp_egv); /* GvEGV is not refcounted */
8404 ret->gp_cv = cv_dup_inc(gp->gp_cv);
8405 ret->gp_cvgen = gp->gp_cvgen;
8406 ret->gp_flags = gp->gp_flags;
8407 ret->gp_line = gp->gp_line;
8408 ret->gp_file = gp->gp_file; /* points to COP.cop_file */
8409 return ret;
8410}
8411
645c22ef
DM
8412/* duplicate a chain of magic */
8413
1d7c1841
GS
8414MAGIC *
8415Perl_mg_dup(pTHX_ MAGIC *mg)
8416{
cb359b41
JH
8417 MAGIC *mgprev = (MAGIC*)NULL;
8418 MAGIC *mgret;
1d7c1841
GS
8419 if (!mg)
8420 return (MAGIC*)NULL;
8421 /* look for it in the table first */
8422 mgret = (MAGIC*)ptr_table_fetch(PL_ptr_table, mg);
8423 if (mgret)
8424 return mgret;
8425
8426 for (; mg; mg = mg->mg_moremagic) {
8427 MAGIC *nmg;
8428 Newz(0, nmg, 1, MAGIC);
cb359b41 8429 if (mgprev)
1d7c1841 8430 mgprev->mg_moremagic = nmg;
cb359b41
JH
8431 else
8432 mgret = nmg;
1d7c1841
GS
8433 nmg->mg_virtual = mg->mg_virtual; /* XXX copy dynamic vtable? */
8434 nmg->mg_private = mg->mg_private;
8435 nmg->mg_type = mg->mg_type;
8436 nmg->mg_flags = mg->mg_flags;
14befaf4 8437 if (mg->mg_type == PERL_MAGIC_qr) {
1d7c1841
GS
8438 nmg->mg_obj = (SV*)re_dup((REGEXP*)mg->mg_obj);
8439 }
8440 else {
8441 nmg->mg_obj = (mg->mg_flags & MGf_REFCOUNTED)
8442 ? sv_dup_inc(mg->mg_obj)
8443 : sv_dup(mg->mg_obj);
8444 }
8445 nmg->mg_len = mg->mg_len;
8446 nmg->mg_ptr = mg->mg_ptr; /* XXX random ptr? */
14befaf4 8447 if (mg->mg_ptr && mg->mg_type != PERL_MAGIC_regex_global) {
1d7c1841
GS
8448 if (mg->mg_len >= 0) {
8449 nmg->mg_ptr = SAVEPVN(mg->mg_ptr, mg->mg_len);
14befaf4
DM
8450 if (mg->mg_type == PERL_MAGIC_overload_table &&
8451 AMT_AMAGIC((AMT*)mg->mg_ptr))
8452 {
1d7c1841
GS
8453 AMT *amtp = (AMT*)mg->mg_ptr;
8454 AMT *namtp = (AMT*)nmg->mg_ptr;
8455 I32 i;
8456 for (i = 1; i < NofAMmeth; i++) {
8457 namtp->table[i] = cv_dup_inc(amtp->table[i]);
8458 }
8459 }
8460 }
8461 else if (mg->mg_len == HEf_SVKEY)
8462 nmg->mg_ptr = (char*)sv_dup_inc((SV*)mg->mg_ptr);
8463 }
8464 mgprev = nmg;
8465 }
8466 return mgret;
8467}
8468
645c22ef
DM
8469/* create a new pointer-mapping table */
8470
1d7c1841
GS
8471PTR_TBL_t *
8472Perl_ptr_table_new(pTHX)
8473{
8474 PTR_TBL_t *tbl;
8475 Newz(0, tbl, 1, PTR_TBL_t);
8476 tbl->tbl_max = 511;
8477 tbl->tbl_items = 0;
8478 Newz(0, tbl->tbl_ary, tbl->tbl_max + 1, PTR_TBL_ENT_t*);
8479 return tbl;
8480}
8481
645c22ef
DM
8482/* map an existing pointer using a table */
8483
1d7c1841
GS
8484void *
8485Perl_ptr_table_fetch(pTHX_ PTR_TBL_t *tbl, void *sv)
8486{
8487 PTR_TBL_ENT_t *tblent;
d2a79402 8488 UV hash = PTR2UV(sv);
1d7c1841
GS
8489 assert(tbl);
8490 tblent = tbl->tbl_ary[hash & tbl->tbl_max];
8491 for (; tblent; tblent = tblent->next) {
8492 if (tblent->oldval == sv)
8493 return tblent->newval;
8494 }
8495 return (void*)NULL;
8496}
8497
645c22ef
DM
8498/* add a new entry to a pointer-mapping table */
8499
1d7c1841
GS
8500void
8501Perl_ptr_table_store(pTHX_ PTR_TBL_t *tbl, void *oldv, void *newv)
8502{
8503 PTR_TBL_ENT_t *tblent, **otblent;
8504 /* XXX this may be pessimal on platforms where pointers aren't good
8505 * hash values e.g. if they grow faster in the most significant
8506 * bits */
d2a79402 8507 UV hash = PTR2UV(oldv);
1d7c1841
GS
8508 bool i = 1;
8509
8510 assert(tbl);
8511 otblent = &tbl->tbl_ary[hash & tbl->tbl_max];
8512 for (tblent = *otblent; tblent; i=0, tblent = tblent->next) {
8513 if (tblent->oldval == oldv) {
8514 tblent->newval = newv;
8515 tbl->tbl_items++;
8516 return;
8517 }
8518 }
8519 Newz(0, tblent, 1, PTR_TBL_ENT_t);
8520 tblent->oldval = oldv;
8521 tblent->newval = newv;
8522 tblent->next = *otblent;
8523 *otblent = tblent;
8524 tbl->tbl_items++;
8525 if (i && tbl->tbl_items > tbl->tbl_max)
8526 ptr_table_split(tbl);
8527}
8528
645c22ef
DM
8529/* double the hash bucket size of an existing ptr table */
8530
1d7c1841
GS
8531void
8532Perl_ptr_table_split(pTHX_ PTR_TBL_t *tbl)
8533{
8534 PTR_TBL_ENT_t **ary = tbl->tbl_ary;
8535 UV oldsize = tbl->tbl_max + 1;
8536 UV newsize = oldsize * 2;
8537 UV i;
8538
8539 Renew(ary, newsize, PTR_TBL_ENT_t*);
8540 Zero(&ary[oldsize], newsize-oldsize, PTR_TBL_ENT_t*);
8541 tbl->tbl_max = --newsize;
8542 tbl->tbl_ary = ary;
8543 for (i=0; i < oldsize; i++, ary++) {
8544 PTR_TBL_ENT_t **curentp, **entp, *ent;
8545 if (!*ary)
8546 continue;
8547 curentp = ary + oldsize;
8548 for (entp = ary, ent = *ary; ent; ent = *entp) {
d2a79402 8549 if ((newsize & PTR2UV(ent->oldval)) != i) {
1d7c1841
GS
8550 *entp = ent->next;
8551 ent->next = *curentp;
8552 *curentp = ent;
8553 continue;
8554 }
8555 else
8556 entp = &ent->next;
8557 }
8558 }
8559}
8560
645c22ef
DM
8561/* remove all the entries from a ptr table */
8562
a0739874
DM
8563void
8564Perl_ptr_table_clear(pTHX_ PTR_TBL_t *tbl)
8565{
8566 register PTR_TBL_ENT_t **array;
8567 register PTR_TBL_ENT_t *entry;
8568 register PTR_TBL_ENT_t *oentry = Null(PTR_TBL_ENT_t*);
8569 UV riter = 0;
8570 UV max;
8571
8572 if (!tbl || !tbl->tbl_items) {
8573 return;
8574 }
8575
8576 array = tbl->tbl_ary;
8577 entry = array[0];
8578 max = tbl->tbl_max;
8579
8580 for (;;) {
8581 if (entry) {
8582 oentry = entry;
8583 entry = entry->next;
8584 Safefree(oentry);
8585 }
8586 if (!entry) {
8587 if (++riter > max) {
8588 break;
8589 }
8590 entry = array[riter];
8591 }
8592 }
8593
8594 tbl->tbl_items = 0;
8595}
8596
645c22ef
DM
8597/* clear and free a ptr table */
8598
a0739874
DM
8599void
8600Perl_ptr_table_free(pTHX_ PTR_TBL_t *tbl)
8601{
8602 if (!tbl) {
8603 return;
8604 }
8605 ptr_table_clear(tbl);
8606 Safefree(tbl->tbl_ary);
8607 Safefree(tbl);
8608}
8609
1d7c1841
GS
8610#ifdef DEBUGGING
8611char *PL_watch_pvx;
8612#endif
8613
645c22ef
DM
8614/* attempt to make everything in the typeglob readonly */
8615
5bd07a3d
DM
8616STATIC SV *
8617S_gv_share(pTHX_ SV *sstr)
8618{
8619 GV *gv = (GV*)sstr;
8620 SV *sv = &PL_sv_no; /* just need SvREADONLY-ness */
8621
8622 if (GvIO(gv) || GvFORM(gv)) {
8623 GvSHARED_off(gv); /* GvIOs cannot be shared. nor can GvFORMs */
8624 }
8625 else if (!GvCV(gv)) {
8626 GvCV(gv) = (CV*)sv;
8627 }
8628 else {
8629 /* CvPADLISTs cannot be shared */
8630 if (!CvXSUB(GvCV(gv))) {
8631 GvSHARED_off(gv);
8632 }
8633 }
8634
8635 if (!GvSHARED(gv)) {
8636#if 0
8637 PerlIO_printf(Perl_debug_log, "gv_share: unable to share %s::%s\n",
8638 HvNAME(GvSTASH(gv)), GvNAME(gv));
8639#endif
8640 return Nullsv;
8641 }
8642
4411f3b6 8643 /*
5bd07a3d
DM
8644 * write attempts will die with
8645 * "Modification of a read-only value attempted"
8646 */
8647 if (!GvSV(gv)) {
8648 GvSV(gv) = sv;
8649 }
8650 else {
8651 SvREADONLY_on(GvSV(gv));
8652 }
8653
8654 if (!GvAV(gv)) {
8655 GvAV(gv) = (AV*)sv;
8656 }
8657 else {
8658 SvREADONLY_on(GvAV(gv));
8659 }
8660
8661 if (!GvHV(gv)) {
8662 GvHV(gv) = (HV*)sv;
8663 }
8664 else {
8665 SvREADONLY_on(GvAV(gv));
8666 }
8667
8668 return sstr; /* he_dup() will SvREFCNT_inc() */
8669}
8670
645c22ef
DM
8671/* duplicate an SV of any type (including AV, HV etc) */
8672
1d7c1841
GS
8673SV *
8674Perl_sv_dup(pTHX_ SV *sstr)
8675{
1d7c1841
GS
8676 SV *dstr;
8677
8678 if (!sstr || SvTYPE(sstr) == SVTYPEMASK)
8679 return Nullsv;
8680 /* look for it in the table first */
8681 dstr = (SV*)ptr_table_fetch(PL_ptr_table, sstr);
8682 if (dstr)
8683 return dstr;
8684
8685 /* create anew and remember what it is */
8686 new_SV(dstr);
8687 ptr_table_store(PL_ptr_table, sstr, dstr);
8688
8689 /* clone */
8690 SvFLAGS(dstr) = SvFLAGS(sstr);
8691 SvFLAGS(dstr) &= ~SVf_OOK; /* don't propagate OOK hack */
8692 SvREFCNT(dstr) = 0; /* must be before any other dups! */
8693
8694#ifdef DEBUGGING
8695 if (SvANY(sstr) && PL_watch_pvx && SvPVX(sstr) == PL_watch_pvx)
8696 PerlIO_printf(Perl_debug_log, "watch at %p hit, found string \"%s\"\n",
8697 PL_watch_pvx, SvPVX(sstr));
8698#endif
8699
8700 switch (SvTYPE(sstr)) {
8701 case SVt_NULL:
8702 SvANY(dstr) = NULL;
8703 break;
8704 case SVt_IV:
8705 SvANY(dstr) = new_XIV();
8706 SvIVX(dstr) = SvIVX(sstr);
8707 break;
8708 case SVt_NV:
8709 SvANY(dstr) = new_XNV();
8710 SvNVX(dstr) = SvNVX(sstr);
8711 break;
8712 case SVt_RV:
8713 SvANY(dstr) = new_XRV();
89cd1aa3
DM
8714 SvRV(dstr) = SvRV(sstr) && SvWEAKREF(SvRV(sstr))
8715 ? sv_dup(SvRV(sstr))
8716 : sv_dup_inc(SvRV(sstr));
1d7c1841
GS
8717 break;
8718 case SVt_PV:
8719 SvANY(dstr) = new_XPV();
8720 SvCUR(dstr) = SvCUR(sstr);
8721 SvLEN(dstr) = SvLEN(sstr);
8722 if (SvROK(sstr))
ce4ad881 8723 SvRV(dstr) = SvWEAKREF(SvRV(sstr))
89cd1aa3
DM
8724 ? sv_dup(SvRV(sstr))
8725 : sv_dup_inc(SvRV(sstr));
1d7c1841
GS
8726 else if (SvPVX(sstr) && SvLEN(sstr))
8727 SvPVX(dstr) = SAVEPVN(SvPVX(sstr), SvLEN(sstr)-1);
8728 else
8729 SvPVX(dstr) = SvPVX(sstr); /* XXX shared string/random ptr? */
8730 break;
8731 case SVt_PVIV:
8732 SvANY(dstr) = new_XPVIV();
8733 SvCUR(dstr) = SvCUR(sstr);
8734 SvLEN(dstr) = SvLEN(sstr);
8735 SvIVX(dstr) = SvIVX(sstr);
8736 if (SvROK(sstr))
ce4ad881 8737 SvRV(dstr) = SvWEAKREF(SvRV(sstr))
89cd1aa3
DM
8738 ? sv_dup(SvRV(sstr))
8739 : sv_dup_inc(SvRV(sstr));
1d7c1841
GS
8740 else if (SvPVX(sstr) && SvLEN(sstr))
8741 SvPVX(dstr) = SAVEPVN(SvPVX(sstr), SvLEN(sstr)-1);
8742 else
8743 SvPVX(dstr) = SvPVX(sstr); /* XXX shared string/random ptr? */
8744 break;
8745 case SVt_PVNV:
8746 SvANY(dstr) = new_XPVNV();
8747 SvCUR(dstr) = SvCUR(sstr);
8748 SvLEN(dstr) = SvLEN(sstr);
8749 SvIVX(dstr) = SvIVX(sstr);
8750 SvNVX(dstr) = SvNVX(sstr);
8751 if (SvROK(sstr))
ce4ad881 8752 SvRV(dstr) = SvWEAKREF(SvRV(sstr))
89cd1aa3
DM
8753 ? sv_dup(SvRV(sstr))
8754 : sv_dup_inc(SvRV(sstr));
1d7c1841
GS
8755 else if (SvPVX(sstr) && SvLEN(sstr))
8756 SvPVX(dstr) = SAVEPVN(SvPVX(sstr), SvLEN(sstr)-1);
8757 else
8758 SvPVX(dstr) = SvPVX(sstr); /* XXX shared string/random ptr? */
8759 break;
8760 case SVt_PVMG:
8761 SvANY(dstr) = new_XPVMG();
8762 SvCUR(dstr) = SvCUR(sstr);
8763 SvLEN(dstr) = SvLEN(sstr);
8764 SvIVX(dstr) = SvIVX(sstr);
8765 SvNVX(dstr) = SvNVX(sstr);
8766 SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr));
8767 SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr));
8768 if (SvROK(sstr))
ce4ad881 8769 SvRV(dstr) = SvWEAKREF(SvRV(sstr))
89cd1aa3
DM
8770 ? sv_dup(SvRV(sstr))
8771 : sv_dup_inc(SvRV(sstr));
1d7c1841
GS
8772 else if (SvPVX(sstr) && SvLEN(sstr))
8773 SvPVX(dstr) = SAVEPVN(SvPVX(sstr), SvLEN(sstr)-1);
8774 else
8775 SvPVX(dstr) = SvPVX(sstr); /* XXX shared string/random ptr? */
8776 break;
8777 case SVt_PVBM:
8778 SvANY(dstr) = new_XPVBM();
8779 SvCUR(dstr) = SvCUR(sstr);
8780 SvLEN(dstr) = SvLEN(sstr);
8781 SvIVX(dstr) = SvIVX(sstr);
8782 SvNVX(dstr) = SvNVX(sstr);
8783 SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr));
8784 SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr));
8785 if (SvROK(sstr))
ce4ad881 8786 SvRV(dstr) = SvWEAKREF(SvRV(sstr))
89cd1aa3
DM
8787 ? sv_dup(SvRV(sstr))
8788 : sv_dup_inc(SvRV(sstr));
1d7c1841
GS
8789 else if (SvPVX(sstr) && SvLEN(sstr))
8790 SvPVX(dstr) = SAVEPVN(SvPVX(sstr), SvLEN(sstr)-1);
8791 else
8792 SvPVX(dstr) = SvPVX(sstr); /* XXX shared string/random ptr? */
8793 BmRARE(dstr) = BmRARE(sstr);
8794 BmUSEFUL(dstr) = BmUSEFUL(sstr);
8795 BmPREVIOUS(dstr)= BmPREVIOUS(sstr);
8796 break;
8797 case SVt_PVLV:
8798 SvANY(dstr) = new_XPVLV();
8799 SvCUR(dstr) = SvCUR(sstr);
8800 SvLEN(dstr) = SvLEN(sstr);
8801 SvIVX(dstr) = SvIVX(sstr);
8802 SvNVX(dstr) = SvNVX(sstr);
8803 SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr));
8804 SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr));
8805 if (SvROK(sstr))
ce4ad881 8806 SvRV(dstr) = SvWEAKREF(SvRV(sstr))
89cd1aa3
DM
8807 ? sv_dup(SvRV(sstr))
8808 : sv_dup_inc(SvRV(sstr));
1d7c1841
GS
8809 else if (SvPVX(sstr) && SvLEN(sstr))
8810 SvPVX(dstr) = SAVEPVN(SvPVX(sstr), SvLEN(sstr)-1);
8811 else
8812 SvPVX(dstr) = SvPVX(sstr); /* XXX shared string/random ptr? */
8813 LvTARGOFF(dstr) = LvTARGOFF(sstr); /* XXX sometimes holds PMOP* when DEBUGGING */
8814 LvTARGLEN(dstr) = LvTARGLEN(sstr);
8815 LvTARG(dstr) = sv_dup_inc(LvTARG(sstr));
8816 LvTYPE(dstr) = LvTYPE(sstr);
8817 break;
8818 case SVt_PVGV:
5bd07a3d
DM
8819 if (GvSHARED((GV*)sstr)) {
8820 SV *share;
8821 if ((share = gv_share(sstr))) {
8822 del_SV(dstr);
8823 dstr = share;
8824#if 0
8825 PerlIO_printf(Perl_debug_log, "sv_dup: sharing %s::%s\n",
8826 HvNAME(GvSTASH(share)), GvNAME(share));
8827#endif
8828 break;
8829 }
8830 }
1d7c1841
GS
8831 SvANY(dstr) = new_XPVGV();
8832 SvCUR(dstr) = SvCUR(sstr);
8833 SvLEN(dstr) = SvLEN(sstr);
8834 SvIVX(dstr) = SvIVX(sstr);
8835 SvNVX(dstr) = SvNVX(sstr);
8836 SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr));
8837 SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr));
8838 if (SvROK(sstr))
ce4ad881 8839 SvRV(dstr) = SvWEAKREF(SvRV(sstr))
89cd1aa3
DM
8840 ? sv_dup(SvRV(sstr))
8841 : sv_dup_inc(SvRV(sstr));
1d7c1841
GS
8842 else if (SvPVX(sstr) && SvLEN(sstr))
8843 SvPVX(dstr) = SAVEPVN(SvPVX(sstr), SvLEN(sstr)-1);
8844 else
8845 SvPVX(dstr) = SvPVX(sstr); /* XXX shared string/random ptr? */
8846 GvNAMELEN(dstr) = GvNAMELEN(sstr);
8847 GvNAME(dstr) = SAVEPVN(GvNAME(sstr), GvNAMELEN(sstr));
8848 GvSTASH(dstr) = hv_dup_inc(GvSTASH(sstr));
8849 GvFLAGS(dstr) = GvFLAGS(sstr);
8850 GvGP(dstr) = gp_dup(GvGP(sstr));
8851 (void)GpREFCNT_inc(GvGP(dstr));
8852 break;
8853 case SVt_PVIO:
8854 SvANY(dstr) = new_XPVIO();
8855 SvCUR(dstr) = SvCUR(sstr);
8856 SvLEN(dstr) = SvLEN(sstr);
8857 SvIVX(dstr) = SvIVX(sstr);
8858 SvNVX(dstr) = SvNVX(sstr);
8859 SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr));
8860 SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr));
8861 if (SvROK(sstr))
ce4ad881 8862 SvRV(dstr) = SvWEAKREF(SvRV(sstr))
89cd1aa3
DM
8863 ? sv_dup(SvRV(sstr))
8864 : sv_dup_inc(SvRV(sstr));
1d7c1841
GS
8865 else if (SvPVX(sstr) && SvLEN(sstr))
8866 SvPVX(dstr) = SAVEPVN(SvPVX(sstr), SvLEN(sstr)-1);
8867 else
8868 SvPVX(dstr) = SvPVX(sstr); /* XXX shared string/random ptr? */
8869 IoIFP(dstr) = fp_dup(IoIFP(sstr), IoTYPE(sstr));
8870 if (IoOFP(sstr) == IoIFP(sstr))
8871 IoOFP(dstr) = IoIFP(dstr);
8872 else
8873 IoOFP(dstr) = fp_dup(IoOFP(sstr), IoTYPE(sstr));
8874 /* PL_rsfp_filters entries have fake IoDIRP() */
8875 if (IoDIRP(sstr) && !(IoFLAGS(sstr) & IOf_FAKE_DIRP))
8876 IoDIRP(dstr) = dirp_dup(IoDIRP(sstr));
8877 else
8878 IoDIRP(dstr) = IoDIRP(sstr);
8879 IoLINES(dstr) = IoLINES(sstr);
8880 IoPAGE(dstr) = IoPAGE(sstr);
8881 IoPAGE_LEN(dstr) = IoPAGE_LEN(sstr);
8882 IoLINES_LEFT(dstr) = IoLINES_LEFT(sstr);
8883 IoTOP_NAME(dstr) = SAVEPV(IoTOP_NAME(sstr));
8884 IoTOP_GV(dstr) = gv_dup(IoTOP_GV(sstr));
8885 IoFMT_NAME(dstr) = SAVEPV(IoFMT_NAME(sstr));
8886 IoFMT_GV(dstr) = gv_dup(IoFMT_GV(sstr));
8887 IoBOTTOM_NAME(dstr) = SAVEPV(IoBOTTOM_NAME(sstr));
8888 IoBOTTOM_GV(dstr) = gv_dup(IoBOTTOM_GV(sstr));
8889 IoSUBPROCESS(dstr) = IoSUBPROCESS(sstr);
8890 IoTYPE(dstr) = IoTYPE(sstr);
8891 IoFLAGS(dstr) = IoFLAGS(sstr);
8892 break;
8893 case SVt_PVAV:
8894 SvANY(dstr) = new_XPVAV();
8895 SvCUR(dstr) = SvCUR(sstr);
8896 SvLEN(dstr) = SvLEN(sstr);
8897 SvIVX(dstr) = SvIVX(sstr);
8898 SvNVX(dstr) = SvNVX(sstr);
8899 SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr));
8900 SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr));
8901 AvARYLEN((AV*)dstr) = sv_dup_inc(AvARYLEN((AV*)sstr));
8902 AvFLAGS((AV*)dstr) = AvFLAGS((AV*)sstr);
8903 if (AvARRAY((AV*)sstr)) {
8904 SV **dst_ary, **src_ary;
8905 SSize_t items = AvFILLp((AV*)sstr) + 1;
8906
8907 src_ary = AvARRAY((AV*)sstr);
8908 Newz(0, dst_ary, AvMAX((AV*)sstr)+1, SV*);
8909 ptr_table_store(PL_ptr_table, src_ary, dst_ary);
8910 SvPVX(dstr) = (char*)dst_ary;
8911 AvALLOC((AV*)dstr) = dst_ary;
8912 if (AvREAL((AV*)sstr)) {
8913 while (items-- > 0)
8914 *dst_ary++ = sv_dup_inc(*src_ary++);
8915 }
8916 else {
8917 while (items-- > 0)
8918 *dst_ary++ = sv_dup(*src_ary++);
8919 }
8920 items = AvMAX((AV*)sstr) - AvFILLp((AV*)sstr);
8921 while (items-- > 0) {
8922 *dst_ary++ = &PL_sv_undef;
8923 }
8924 }
8925 else {
8926 SvPVX(dstr) = Nullch;
8927 AvALLOC((AV*)dstr) = (SV**)NULL;
8928 }
8929 break;
8930 case SVt_PVHV:
8931 SvANY(dstr) = new_XPVHV();
8932 SvCUR(dstr) = SvCUR(sstr);
8933 SvLEN(dstr) = SvLEN(sstr);
8934 SvIVX(dstr) = SvIVX(sstr);
8935 SvNVX(dstr) = SvNVX(sstr);
8936 SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr));
8937 SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr));
8938 HvRITER((HV*)dstr) = HvRITER((HV*)sstr);
8939 if (HvARRAY((HV*)sstr)) {
1d7c1841
GS
8940 STRLEN i = 0;
8941 XPVHV *dxhv = (XPVHV*)SvANY(dstr);
8942 XPVHV *sxhv = (XPVHV*)SvANY(sstr);
8943 Newz(0, dxhv->xhv_array,
8944 PERL_HV_ARRAY_ALLOC_BYTES(dxhv->xhv_max+1), char);
8945 while (i <= sxhv->xhv_max) {
8946 ((HE**)dxhv->xhv_array)[i] = he_dup(((HE**)sxhv->xhv_array)[i],
8947 !!HvSHAREKEYS(sstr));
8948 ++i;
8949 }
8950 dxhv->xhv_eiter = he_dup(sxhv->xhv_eiter, !!HvSHAREKEYS(sstr));
8951 }
8952 else {
8953 SvPVX(dstr) = Nullch;
8954 HvEITER((HV*)dstr) = (HE*)NULL;
8955 }
8956 HvPMROOT((HV*)dstr) = HvPMROOT((HV*)sstr); /* XXX */
8957 HvNAME((HV*)dstr) = SAVEPV(HvNAME((HV*)sstr));
f284b03f 8958 /* Record stashes for possible cloning in Perl_clone_using(). */
6676db26
AMS
8959 if(HvNAME((HV*)dstr))
8960 av_push(PL_clone_callbacks, dstr);
1d7c1841
GS
8961 break;
8962 case SVt_PVFM:
8963 SvANY(dstr) = new_XPVFM();
8964 FmLINES(dstr) = FmLINES(sstr);
8965 goto dup_pvcv;
8966 /* NOTREACHED */
8967 case SVt_PVCV:
8968 SvANY(dstr) = new_XPVCV();
8969dup_pvcv:
8970 SvCUR(dstr) = SvCUR(sstr);
8971 SvLEN(dstr) = SvLEN(sstr);
8972 SvIVX(dstr) = SvIVX(sstr);
8973 SvNVX(dstr) = SvNVX(sstr);
8974 SvMAGIC(dstr) = mg_dup(SvMAGIC(sstr));
8975 SvSTASH(dstr) = hv_dup_inc(SvSTASH(sstr));
8976 if (SvPVX(sstr) && SvLEN(sstr))
8977 SvPVX(dstr) = SAVEPVN(SvPVX(sstr), SvLEN(sstr)-1);
8978 else
8979 SvPVX(dstr) = SvPVX(sstr); /* XXX shared string/random ptr? */
8980 CvSTASH(dstr) = hv_dup(CvSTASH(sstr));/* NOTE: not refcounted */
8981 CvSTART(dstr) = CvSTART(sstr);
8982 CvROOT(dstr) = OpREFCNT_inc(CvROOT(sstr));
8983 CvXSUB(dstr) = CvXSUB(sstr);
8984 CvXSUBANY(dstr) = CvXSUBANY(sstr);
f25c30a3 8985 CvGV(dstr) = gv_dup(CvGV(sstr));
1d7c1841
GS
8986 CvDEPTH(dstr) = CvDEPTH(sstr);
8987 if (CvPADLIST(sstr) && !AvREAL(CvPADLIST(sstr))) {
8988 /* XXX padlists are real, but pretend to be not */
8989 AvREAL_on(CvPADLIST(sstr));
8990 CvPADLIST(dstr) = av_dup_inc(CvPADLIST(sstr));
8991 AvREAL_off(CvPADLIST(sstr));
8992 AvREAL_off(CvPADLIST(dstr));
8993 }
8994 else
8995 CvPADLIST(dstr) = av_dup_inc(CvPADLIST(sstr));
282f25c9
JH
8996 if (!CvANON(sstr) || CvCLONED(sstr))
8997 CvOUTSIDE(dstr) = cv_dup_inc(CvOUTSIDE(sstr));
8998 else
8999 CvOUTSIDE(dstr) = cv_dup(CvOUTSIDE(sstr));
1d7c1841
GS
9000 CvFLAGS(dstr) = CvFLAGS(sstr);
9001 break;
9002 default:
9003 Perl_croak(aTHX_ "Bizarre SvTYPE [%d]", SvTYPE(sstr));
9004 break;
9005 }
9006
9007 if (SvOBJECT(dstr) && SvTYPE(dstr) != SVt_PVIO)
9008 ++PL_sv_objcount;
9009
9010 return dstr;
9011}
9012
645c22ef
DM
9013/* duplicate a context */
9014
1d7c1841
GS
9015PERL_CONTEXT *
9016Perl_cx_dup(pTHX_ PERL_CONTEXT *cxs, I32 ix, I32 max)
9017{
9018 PERL_CONTEXT *ncxs;
9019
9020 if (!cxs)
9021 return (PERL_CONTEXT*)NULL;
9022
9023 /* look for it in the table first */
9024 ncxs = (PERL_CONTEXT*)ptr_table_fetch(PL_ptr_table, cxs);
9025 if (ncxs)
9026 return ncxs;
9027
9028 /* create anew and remember what it is */
9029 Newz(56, ncxs, max + 1, PERL_CONTEXT);
9030 ptr_table_store(PL_ptr_table, cxs, ncxs);
9031
9032 while (ix >= 0) {
9033 PERL_CONTEXT *cx = &cxs[ix];
9034 PERL_CONTEXT *ncx = &ncxs[ix];
9035 ncx->cx_type = cx->cx_type;
9036 if (CxTYPE(cx) == CXt_SUBST) {
9037 Perl_croak(aTHX_ "Cloning substitution context is unimplemented");
9038 }
9039 else {
9040 ncx->blk_oldsp = cx->blk_oldsp;
9041 ncx->blk_oldcop = cx->blk_oldcop;
9042 ncx->blk_oldretsp = cx->blk_oldretsp;
9043 ncx->blk_oldmarksp = cx->blk_oldmarksp;
9044 ncx->blk_oldscopesp = cx->blk_oldscopesp;
9045 ncx->blk_oldpm = cx->blk_oldpm;
9046 ncx->blk_gimme = cx->blk_gimme;
9047 switch (CxTYPE(cx)) {
9048 case CXt_SUB:
9049 ncx->blk_sub.cv = (cx->blk_sub.olddepth == 0
9050 ? cv_dup_inc(cx->blk_sub.cv)
9051 : cv_dup(cx->blk_sub.cv));
9052 ncx->blk_sub.argarray = (cx->blk_sub.hasargs
9053 ? av_dup_inc(cx->blk_sub.argarray)
9054 : Nullav);
f25c30a3 9055 ncx->blk_sub.savearray = av_dup_inc(cx->blk_sub.savearray);
1d7c1841
GS
9056 ncx->blk_sub.olddepth = cx->blk_sub.olddepth;
9057 ncx->blk_sub.hasargs = cx->blk_sub.hasargs;
9058 ncx->blk_sub.lval = cx->blk_sub.lval;
9059 break;
9060 case CXt_EVAL:
9061 ncx->blk_eval.old_in_eval = cx->blk_eval.old_in_eval;
9062 ncx->blk_eval.old_op_type = cx->blk_eval.old_op_type;
0f79a09d 9063 ncx->blk_eval.old_namesv = sv_dup_inc(cx->blk_eval.old_namesv);
1d7c1841
GS
9064 ncx->blk_eval.old_eval_root = cx->blk_eval.old_eval_root;
9065 ncx->blk_eval.cur_text = sv_dup(cx->blk_eval.cur_text);
9066 break;
9067 case CXt_LOOP:
9068 ncx->blk_loop.label = cx->blk_loop.label;
9069 ncx->blk_loop.resetsp = cx->blk_loop.resetsp;
9070 ncx->blk_loop.redo_op = cx->blk_loop.redo_op;
9071 ncx->blk_loop.next_op = cx->blk_loop.next_op;
9072 ncx->blk_loop.last_op = cx->blk_loop.last_op;
9073 ncx->blk_loop.iterdata = (CxPADLOOP(cx)
9074 ? cx->blk_loop.iterdata
9075 : gv_dup((GV*)cx->blk_loop.iterdata));
a4b82a6f
GS
9076 ncx->blk_loop.oldcurpad
9077 = (SV**)ptr_table_fetch(PL_ptr_table,
9078 cx->blk_loop.oldcurpad);
1d7c1841
GS
9079 ncx->blk_loop.itersave = sv_dup_inc(cx->blk_loop.itersave);
9080 ncx->blk_loop.iterlval = sv_dup_inc(cx->blk_loop.iterlval);
9081 ncx->blk_loop.iterary = av_dup_inc(cx->blk_loop.iterary);
9082 ncx->blk_loop.iterix = cx->blk_loop.iterix;
9083 ncx->blk_loop.itermax = cx->blk_loop.itermax;
9084 break;
9085 case CXt_FORMAT:
9086 ncx->blk_sub.cv = cv_dup(cx->blk_sub.cv);
9087 ncx->blk_sub.gv = gv_dup(cx->blk_sub.gv);
9088 ncx->blk_sub.dfoutgv = gv_dup_inc(cx->blk_sub.dfoutgv);
9089 ncx->blk_sub.hasargs = cx->blk_sub.hasargs;
9090 break;
9091 case CXt_BLOCK:
9092 case CXt_NULL:
9093 break;
9094 }
9095 }
9096 --ix;
9097 }
9098 return ncxs;
9099}
9100
645c22ef
DM
9101/* duplicate a stack info structure */
9102
1d7c1841
GS
9103PERL_SI *
9104Perl_si_dup(pTHX_ PERL_SI *si)
9105{
9106 PERL_SI *nsi;
9107
9108 if (!si)
9109 return (PERL_SI*)NULL;
9110
9111 /* look for it in the table first */
9112 nsi = (PERL_SI*)ptr_table_fetch(PL_ptr_table, si);
9113 if (nsi)
9114 return nsi;
9115
9116 /* create anew and remember what it is */
9117 Newz(56, nsi, 1, PERL_SI);
9118 ptr_table_store(PL_ptr_table, si, nsi);
9119
9120 nsi->si_stack = av_dup_inc(si->si_stack);
9121 nsi->si_cxix = si->si_cxix;
9122 nsi->si_cxmax = si->si_cxmax;
9123 nsi->si_cxstack = cx_dup(si->si_cxstack, si->si_cxix, si->si_cxmax);
9124 nsi->si_type = si->si_type;
9125 nsi->si_prev = si_dup(si->si_prev);
9126 nsi->si_next = si_dup(si->si_next);
9127 nsi->si_markoff = si->si_markoff;
9128
9129 return nsi;
9130}
9131
9132#define POPINT(ss,ix) ((ss)[--(ix)].any_i32)
9133#define TOPINT(ss,ix) ((ss)[ix].any_i32)
9134#define POPLONG(ss,ix) ((ss)[--(ix)].any_long)
9135#define TOPLONG(ss,ix) ((ss)[ix].any_long)
9136#define POPIV(ss,ix) ((ss)[--(ix)].any_iv)
9137#define TOPIV(ss,ix) ((ss)[ix].any_iv)
9138#define POPPTR(ss,ix) ((ss)[--(ix)].any_ptr)
9139#define TOPPTR(ss,ix) ((ss)[ix].any_ptr)
9140#define POPDPTR(ss,ix) ((ss)[--(ix)].any_dptr)
9141#define TOPDPTR(ss,ix) ((ss)[ix].any_dptr)
9142#define POPDXPTR(ss,ix) ((ss)[--(ix)].any_dxptr)
9143#define TOPDXPTR(ss,ix) ((ss)[ix].any_dxptr)
9144
9145/* XXXXX todo */
9146#define pv_dup_inc(p) SAVEPV(p)
9147#define pv_dup(p) SAVEPV(p)
9148#define svp_dup_inc(p,pp) any_dup(p,pp)
9149
645c22ef
DM
9150/* map any object to the new equivent - either something in the
9151 * ptr table, or something in the interpreter structure
9152 */
9153
1d7c1841
GS
9154void *
9155Perl_any_dup(pTHX_ void *v, PerlInterpreter *proto_perl)
9156{
9157 void *ret;
9158
9159 if (!v)
9160 return (void*)NULL;
9161
9162 /* look for it in the table first */
9163 ret = ptr_table_fetch(PL_ptr_table, v);
9164 if (ret)
9165 return ret;
9166
9167 /* see if it is part of the interpreter structure */
9168 if (v >= (void*)proto_perl && v < (void*)(proto_perl+1))
9169 ret = (void*)(((char*)aTHXo) + (((char*)v) - (char*)proto_perl));
9170 else
9171 ret = v;
9172
9173 return ret;
9174}
9175
645c22ef
DM
9176/* duplicate the save stack */
9177
1d7c1841
GS
9178ANY *
9179Perl_ss_dup(pTHX_ PerlInterpreter *proto_perl)
9180{
9181 ANY *ss = proto_perl->Tsavestack;
9182 I32 ix = proto_perl->Tsavestack_ix;
9183 I32 max = proto_perl->Tsavestack_max;
9184 ANY *nss;
9185 SV *sv;
9186 GV *gv;
9187 AV *av;
9188 HV *hv;
9189 void* ptr;
9190 int intval;
9191 long longval;
9192 GP *gp;
9193 IV iv;
9194 I32 i;
9195 char *c;
9196 void (*dptr) (void*);
9197 void (*dxptr) (pTHXo_ void*);
e977893f 9198 OP *o;
1d7c1841
GS
9199
9200 Newz(54, nss, max, ANY);
9201
9202 while (ix > 0) {
9203 i = POPINT(ss,ix);
9204 TOPINT(nss,ix) = i;
9205 switch (i) {
9206 case SAVEt_ITEM: /* normal string */
9207 sv = (SV*)POPPTR(ss,ix);
9208 TOPPTR(nss,ix) = sv_dup_inc(sv);
9209 sv = (SV*)POPPTR(ss,ix);
9210 TOPPTR(nss,ix) = sv_dup_inc(sv);
9211 break;
9212 case SAVEt_SV: /* scalar reference */
9213 sv = (SV*)POPPTR(ss,ix);
9214 TOPPTR(nss,ix) = sv_dup_inc(sv);
9215 gv = (GV*)POPPTR(ss,ix);
9216 TOPPTR(nss,ix) = gv_dup_inc(gv);
9217 break;
f4dd75d9
GS
9218 case SAVEt_GENERIC_PVREF: /* generic char* */
9219 c = (char*)POPPTR(ss,ix);
9220 TOPPTR(nss,ix) = pv_dup(c);
9221 ptr = POPPTR(ss,ix);
9222 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
9223 break;
1d7c1841
GS
9224 case SAVEt_GENERIC_SVREF: /* generic sv */
9225 case SAVEt_SVREF: /* scalar reference */
9226 sv = (SV*)POPPTR(ss,ix);
9227 TOPPTR(nss,ix) = sv_dup_inc(sv);
9228 ptr = POPPTR(ss,ix);
9229 TOPPTR(nss,ix) = svp_dup_inc((SV**)ptr, proto_perl);/* XXXXX */
9230 break;
9231 case SAVEt_AV: /* array reference */
9232 av = (AV*)POPPTR(ss,ix);
9233 TOPPTR(nss,ix) = av_dup_inc(av);
9234 gv = (GV*)POPPTR(ss,ix);
9235 TOPPTR(nss,ix) = gv_dup(gv);
9236 break;
9237 case SAVEt_HV: /* hash reference */
9238 hv = (HV*)POPPTR(ss,ix);
9239 TOPPTR(nss,ix) = hv_dup_inc(hv);
9240 gv = (GV*)POPPTR(ss,ix);
9241 TOPPTR(nss,ix) = gv_dup(gv);
9242 break;
9243 case SAVEt_INT: /* int reference */
9244 ptr = POPPTR(ss,ix);
9245 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
9246 intval = (int)POPINT(ss,ix);
9247 TOPINT(nss,ix) = intval;
9248 break;
9249 case SAVEt_LONG: /* long reference */
9250 ptr = POPPTR(ss,ix);
9251 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
9252 longval = (long)POPLONG(ss,ix);
9253 TOPLONG(nss,ix) = longval;
9254 break;
9255 case SAVEt_I32: /* I32 reference */
9256 case SAVEt_I16: /* I16 reference */
9257 case SAVEt_I8: /* I8 reference */
9258 ptr = POPPTR(ss,ix);
9259 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
9260 i = POPINT(ss,ix);
9261 TOPINT(nss,ix) = i;
9262 break;
9263 case SAVEt_IV: /* IV reference */
9264 ptr = POPPTR(ss,ix);
9265 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
9266 iv = POPIV(ss,ix);
9267 TOPIV(nss,ix) = iv;
9268 break;
9269 case SAVEt_SPTR: /* SV* reference */
9270 ptr = POPPTR(ss,ix);
9271 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
9272 sv = (SV*)POPPTR(ss,ix);
9273 TOPPTR(nss,ix) = sv_dup(sv);
9274 break;
9275 case SAVEt_VPTR: /* random* reference */
9276 ptr = POPPTR(ss,ix);
9277 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
9278 ptr = POPPTR(ss,ix);
9279 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
9280 break;
9281 case SAVEt_PPTR: /* char* reference */
9282 ptr = POPPTR(ss,ix);
9283 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
9284 c = (char*)POPPTR(ss,ix);
9285 TOPPTR(nss,ix) = pv_dup(c);
9286 break;
9287 case SAVEt_HPTR: /* HV* reference */
9288 ptr = POPPTR(ss,ix);
9289 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
9290 hv = (HV*)POPPTR(ss,ix);
9291 TOPPTR(nss,ix) = hv_dup(hv);
9292 break;
9293 case SAVEt_APTR: /* AV* reference */
9294 ptr = POPPTR(ss,ix);
9295 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
9296 av = (AV*)POPPTR(ss,ix);
9297 TOPPTR(nss,ix) = av_dup(av);
9298 break;
9299 case SAVEt_NSTAB:
9300 gv = (GV*)POPPTR(ss,ix);
9301 TOPPTR(nss,ix) = gv_dup(gv);
9302 break;
9303 case SAVEt_GP: /* scalar reference */
9304 gp = (GP*)POPPTR(ss,ix);
9305 TOPPTR(nss,ix) = gp = gp_dup(gp);
9306 (void)GpREFCNT_inc(gp);
9307 gv = (GV*)POPPTR(ss,ix);
9308 TOPPTR(nss,ix) = gv_dup_inc(c);
9309 c = (char*)POPPTR(ss,ix);
9310 TOPPTR(nss,ix) = pv_dup(c);
9311 iv = POPIV(ss,ix);
9312 TOPIV(nss,ix) = iv;
9313 iv = POPIV(ss,ix);
9314 TOPIV(nss,ix) = iv;
9315 break;
9316 case SAVEt_FREESV:
26d9b02f 9317 case SAVEt_MORTALIZESV:
1d7c1841
GS
9318 sv = (SV*)POPPTR(ss,ix);
9319 TOPPTR(nss,ix) = sv_dup_inc(sv);
9320 break;
9321 case SAVEt_FREEOP:
9322 ptr = POPPTR(ss,ix);
9323 if (ptr && (((OP*)ptr)->op_private & OPpREFCOUNTED)) {
9324 /* these are assumed to be refcounted properly */
9325 switch (((OP*)ptr)->op_type) {
9326 case OP_LEAVESUB:
9327 case OP_LEAVESUBLV:
9328 case OP_LEAVEEVAL:
9329 case OP_LEAVE:
9330 case OP_SCOPE:
9331 case OP_LEAVEWRITE:
e977893f
GS
9332 TOPPTR(nss,ix) = ptr;
9333 o = (OP*)ptr;
9334 OpREFCNT_inc(o);
1d7c1841
GS
9335 break;
9336 default:
9337 TOPPTR(nss,ix) = Nullop;
9338 break;
9339 }
9340 }
9341 else
9342 TOPPTR(nss,ix) = Nullop;
9343 break;
9344 case SAVEt_FREEPV:
9345 c = (char*)POPPTR(ss,ix);
9346 TOPPTR(nss,ix) = pv_dup_inc(c);
9347 break;
9348 case SAVEt_CLEARSV:
9349 longval = POPLONG(ss,ix);
9350 TOPLONG(nss,ix) = longval;
9351 break;
9352 case SAVEt_DELETE:
9353 hv = (HV*)POPPTR(ss,ix);
9354 TOPPTR(nss,ix) = hv_dup_inc(hv);
9355 c = (char*)POPPTR(ss,ix);
9356 TOPPTR(nss,ix) = pv_dup_inc(c);
9357 i = POPINT(ss,ix);
9358 TOPINT(nss,ix) = i;
9359 break;
9360 case SAVEt_DESTRUCTOR:
9361 ptr = POPPTR(ss,ix);
9362 TOPPTR(nss,ix) = any_dup(ptr, proto_perl); /* XXX quite arbitrary */
9363 dptr = POPDPTR(ss,ix);
ef75a179 9364 TOPDPTR(nss,ix) = (void (*)(void*))any_dup((void *)dptr, proto_perl);
1d7c1841
GS
9365 break;
9366 case SAVEt_DESTRUCTOR_X:
9367 ptr = POPPTR(ss,ix);
9368 TOPPTR(nss,ix) = any_dup(ptr, proto_perl); /* XXX quite arbitrary */
9369 dxptr = POPDXPTR(ss,ix);
ef75a179 9370 TOPDXPTR(nss,ix) = (void (*)(pTHXo_ void*))any_dup((void *)dxptr, proto_perl);
1d7c1841
GS
9371 break;
9372 case SAVEt_REGCONTEXT:
9373 case SAVEt_ALLOC:
9374 i = POPINT(ss,ix);
9375 TOPINT(nss,ix) = i;
9376 ix -= i;
9377 break;
9378 case SAVEt_STACK_POS: /* Position on Perl stack */
9379 i = POPINT(ss,ix);
9380 TOPINT(nss,ix) = i;
9381 break;
9382 case SAVEt_AELEM: /* array element */
9383 sv = (SV*)POPPTR(ss,ix);
9384 TOPPTR(nss,ix) = sv_dup_inc(sv);
9385 i = POPINT(ss,ix);
9386 TOPINT(nss,ix) = i;
9387 av = (AV*)POPPTR(ss,ix);
9388 TOPPTR(nss,ix) = av_dup_inc(av);
9389 break;
9390 case SAVEt_HELEM: /* hash element */
9391 sv = (SV*)POPPTR(ss,ix);
9392 TOPPTR(nss,ix) = sv_dup_inc(sv);
9393 sv = (SV*)POPPTR(ss,ix);
9394 TOPPTR(nss,ix) = sv_dup_inc(sv);
9395 hv = (HV*)POPPTR(ss,ix);
9396 TOPPTR(nss,ix) = hv_dup_inc(hv);
9397 break;
9398 case SAVEt_OP:
9399 ptr = POPPTR(ss,ix);
9400 TOPPTR(nss,ix) = ptr;
9401 break;
9402 case SAVEt_HINTS:
9403 i = POPINT(ss,ix);
9404 TOPINT(nss,ix) = i;
9405 break;
c4410b1b
GS
9406 case SAVEt_COMPPAD:
9407 av = (AV*)POPPTR(ss,ix);
9408 TOPPTR(nss,ix) = av_dup(av);
9409 break;
c3564e5c
GS
9410 case SAVEt_PADSV:
9411 longval = (long)POPLONG(ss,ix);
9412 TOPLONG(nss,ix) = longval;
9413 ptr = POPPTR(ss,ix);
9414 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
9415 sv = (SV*)POPPTR(ss,ix);
9416 TOPPTR(nss,ix) = sv_dup(sv);
9417 break;
1d7c1841
GS
9418 default:
9419 Perl_croak(aTHX_ "panic: ss_dup inconsistency");
9420 }
9421 }
9422
9423 return nss;
9424}
9425
9426#ifdef PERL_OBJECT
9427#include "XSUB.h"
9428#endif
9429
645c22ef
DM
9430/*
9431=for apidoc perl_clone
9432
9433Create and return a new interpreter by cloning the current one.
9434
9435=cut
9436*/
9437
9438/* XXX the above needs expanding by someone who actually understands it ! */
9439
1d7c1841
GS
9440PerlInterpreter *
9441perl_clone(PerlInterpreter *proto_perl, UV flags)
9442{
9443#ifdef PERL_OBJECT
9444 CPerlObj *pPerl = (CPerlObj*)proto_perl;
9445#endif
9446
9447#ifdef PERL_IMPLICIT_SYS
9448 return perl_clone_using(proto_perl, flags,
9449 proto_perl->IMem,
9450 proto_perl->IMemShared,
9451 proto_perl->IMemParse,
9452 proto_perl->IEnv,
9453 proto_perl->IStdIO,
9454 proto_perl->ILIO,
9455 proto_perl->IDir,
9456 proto_perl->ISock,
9457 proto_perl->IProc);
9458}
9459
9460PerlInterpreter *
9461perl_clone_using(PerlInterpreter *proto_perl, UV flags,
9462 struct IPerlMem* ipM, struct IPerlMem* ipMS,
9463 struct IPerlMem* ipMP, struct IPerlEnv* ipE,
9464 struct IPerlStdIO* ipStd, struct IPerlLIO* ipLIO,
9465 struct IPerlDir* ipD, struct IPerlSock* ipS,
9466 struct IPerlProc* ipP)
9467{
9468 /* XXX many of the string copies here can be optimized if they're
9469 * constants; they need to be allocated as common memory and just
9470 * their pointers copied. */
9471
9472 IV i;
1d7c1841
GS
9473# ifdef PERL_OBJECT
9474 CPerlObj *pPerl = new(ipM) CPerlObj(ipM, ipMS, ipMP, ipE, ipStd, ipLIO,
9475 ipD, ipS, ipP);
ba869deb 9476 PERL_SET_THX(pPerl);
1d7c1841
GS
9477# else /* !PERL_OBJECT */
9478 PerlInterpreter *my_perl = (PerlInterpreter*)(*ipM->pMalloc)(ipM, sizeof(PerlInterpreter));
ba869deb 9479 PERL_SET_THX(my_perl);
1d7c1841
GS
9480
9481# ifdef DEBUGGING
9482 memset(my_perl, 0xab, sizeof(PerlInterpreter));
9483 PL_markstack = 0;
9484 PL_scopestack = 0;
9485 PL_savestack = 0;
9486 PL_retstack = 0;
66fe0623 9487 PL_sig_pending = 0;
1d7c1841
GS
9488# else /* !DEBUGGING */
9489 Zero(my_perl, 1, PerlInterpreter);
9490# endif /* DEBUGGING */
9491
9492 /* host pointers */
9493 PL_Mem = ipM;
9494 PL_MemShared = ipMS;
9495 PL_MemParse = ipMP;
9496 PL_Env = ipE;
9497 PL_StdIO = ipStd;
9498 PL_LIO = ipLIO;
9499 PL_Dir = ipD;
9500 PL_Sock = ipS;
9501 PL_Proc = ipP;
9502# endif /* PERL_OBJECT */
9503#else /* !PERL_IMPLICIT_SYS */
9504 IV i;
1d7c1841 9505 PerlInterpreter *my_perl = (PerlInterpreter*)PerlMem_malloc(sizeof(PerlInterpreter));
ba869deb 9506 PERL_SET_THX(my_perl);
1d7c1841
GS
9507
9508# ifdef DEBUGGING
9509 memset(my_perl, 0xab, sizeof(PerlInterpreter));
9510 PL_markstack = 0;
9511 PL_scopestack = 0;
9512 PL_savestack = 0;
9513 PL_retstack = 0;
66fe0623 9514 PL_sig_pending = 0;
1d7c1841
GS
9515# else /* !DEBUGGING */
9516 Zero(my_perl, 1, PerlInterpreter);
9517# endif /* DEBUGGING */
9518#endif /* PERL_IMPLICIT_SYS */
9519
9520 /* arena roots */
9521 PL_xiv_arenaroot = NULL;
9522 PL_xiv_root = NULL;
612f20c3 9523 PL_xnv_arenaroot = NULL;
1d7c1841 9524 PL_xnv_root = NULL;
612f20c3 9525 PL_xrv_arenaroot = NULL;
1d7c1841 9526 PL_xrv_root = NULL;
612f20c3 9527 PL_xpv_arenaroot = NULL;
1d7c1841 9528 PL_xpv_root = NULL;
612f20c3 9529 PL_xpviv_arenaroot = NULL;
1d7c1841 9530 PL_xpviv_root = NULL;
612f20c3 9531 PL_xpvnv_arenaroot = NULL;
1d7c1841 9532 PL_xpvnv_root = NULL;
612f20c3 9533 PL_xpvcv_arenaroot = NULL;
1d7c1841 9534 PL_xpvcv_root = NULL;
612f20c3 9535 PL_xpvav_arenaroot = NULL;
1d7c1841 9536 PL_xpvav_root = NULL;
612f20c3 9537 PL_xpvhv_arenaroot = NULL;
1d7c1841 9538 PL_xpvhv_root = NULL;
612f20c3 9539 PL_xpvmg_arenaroot = NULL;
1d7c1841 9540 PL_xpvmg_root = NULL;
612f20c3 9541 PL_xpvlv_arenaroot = NULL;
1d7c1841 9542 PL_xpvlv_root = NULL;
612f20c3 9543 PL_xpvbm_arenaroot = NULL;
1d7c1841 9544 PL_xpvbm_root = NULL;
612f20c3 9545 PL_he_arenaroot = NULL;
1d7c1841
GS
9546 PL_he_root = NULL;
9547 PL_nice_chunk = NULL;
9548 PL_nice_chunk_size = 0;
9549 PL_sv_count = 0;
9550 PL_sv_objcount = 0;
9551 PL_sv_root = Nullsv;
9552 PL_sv_arenaroot = Nullsv;
9553
9554 PL_debug = proto_perl->Idebug;
9555
9556 /* create SV map for pointer relocation */
9557 PL_ptr_table = ptr_table_new();
9558
9559 /* initialize these special pointers as early as possible */
9560 SvANY(&PL_sv_undef) = NULL;
9561 SvREFCNT(&PL_sv_undef) = (~(U32)0)/2;
9562 SvFLAGS(&PL_sv_undef) = SVf_READONLY|SVt_NULL;
9563 ptr_table_store(PL_ptr_table, &proto_perl->Isv_undef, &PL_sv_undef);
9564
9565#ifdef PERL_OBJECT
9566 SvUPGRADE(&PL_sv_no, SVt_PVNV);
9567#else
9568 SvANY(&PL_sv_no) = new_XPVNV();
9569#endif
9570 SvREFCNT(&PL_sv_no) = (~(U32)0)/2;
9571 SvFLAGS(&PL_sv_no) = SVp_NOK|SVf_NOK|SVp_POK|SVf_POK|SVf_READONLY|SVt_PVNV;
9572 SvPVX(&PL_sv_no) = SAVEPVN(PL_No, 0);
9573 SvCUR(&PL_sv_no) = 0;
9574 SvLEN(&PL_sv_no) = 1;
9575 SvNVX(&PL_sv_no) = 0;
9576 ptr_table_store(PL_ptr_table, &proto_perl->Isv_no, &PL_sv_no);
9577
9578#ifdef PERL_OBJECT
9579 SvUPGRADE(&PL_sv_yes, SVt_PVNV);
9580#else
9581 SvANY(&PL_sv_yes) = new_XPVNV();
9582#endif
9583 SvREFCNT(&PL_sv_yes) = (~(U32)0)/2;
9584 SvFLAGS(&PL_sv_yes) = SVp_NOK|SVf_NOK|SVp_POK|SVf_POK|SVf_READONLY|SVt_PVNV;
9585 SvPVX(&PL_sv_yes) = SAVEPVN(PL_Yes, 1);
9586 SvCUR(&PL_sv_yes) = 1;
9587 SvLEN(&PL_sv_yes) = 2;
9588 SvNVX(&PL_sv_yes) = 1;
9589 ptr_table_store(PL_ptr_table, &proto_perl->Isv_yes, &PL_sv_yes);
9590
9591 /* create shared string table */
9592 PL_strtab = newHV();
9593 HvSHAREKEYS_off(PL_strtab);
9594 hv_ksplit(PL_strtab, 512);
9595 ptr_table_store(PL_ptr_table, proto_perl->Istrtab, PL_strtab);
9596
9597 PL_compiling = proto_perl->Icompiling;
9598 PL_compiling.cop_stashpv = SAVEPV(PL_compiling.cop_stashpv);
9599 PL_compiling.cop_file = SAVEPV(PL_compiling.cop_file);
9600 ptr_table_store(PL_ptr_table, &proto_perl->Icompiling, &PL_compiling);
9601 if (!specialWARN(PL_compiling.cop_warnings))
9602 PL_compiling.cop_warnings = sv_dup_inc(PL_compiling.cop_warnings);
ac27b0f5
NIS
9603 if (!specialCopIO(PL_compiling.cop_io))
9604 PL_compiling.cop_io = sv_dup_inc(PL_compiling.cop_io);
1d7c1841
GS
9605 PL_curcop = (COP*)any_dup(proto_perl->Tcurcop, proto_perl);
9606
9607 /* pseudo environmental stuff */
9608 PL_origargc = proto_perl->Iorigargc;
9609 i = PL_origargc;
9610 New(0, PL_origargv, i+1, char*);
9611 PL_origargv[i] = '\0';
9612 while (i-- > 0) {
9613 PL_origargv[i] = SAVEPV(proto_perl->Iorigargv[i]);
9614 }
4a09accc 9615 PL_clone_callbacks = newAV(); /* Setup array of objects to callbackon */
1d7c1841
GS
9616 PL_envgv = gv_dup(proto_perl->Ienvgv);
9617 PL_incgv = gv_dup(proto_perl->Iincgv);
9618 PL_hintgv = gv_dup(proto_perl->Ihintgv);
9619 PL_origfilename = SAVEPV(proto_perl->Iorigfilename);
9620 PL_diehook = sv_dup_inc(proto_perl->Idiehook);
9621 PL_warnhook = sv_dup_inc(proto_perl->Iwarnhook);
9622
9623 /* switches */
9624 PL_minus_c = proto_perl->Iminus_c;
a7cb1f99 9625 PL_patchlevel = sv_dup_inc(proto_perl->Ipatchlevel);
1d7c1841
GS
9626 PL_localpatches = proto_perl->Ilocalpatches;
9627 PL_splitstr = proto_perl->Isplitstr;
9628 PL_preprocess = proto_perl->Ipreprocess;
9629 PL_minus_n = proto_perl->Iminus_n;
9630 PL_minus_p = proto_perl->Iminus_p;
9631 PL_minus_l = proto_perl->Iminus_l;
9632 PL_minus_a = proto_perl->Iminus_a;
9633 PL_minus_F = proto_perl->Iminus_F;
9634 PL_doswitches = proto_perl->Idoswitches;
9635 PL_dowarn = proto_perl->Idowarn;
9636 PL_doextract = proto_perl->Idoextract;
9637 PL_sawampersand = proto_perl->Isawampersand;
9638 PL_unsafe = proto_perl->Iunsafe;
9639 PL_inplace = SAVEPV(proto_perl->Iinplace);
9640 PL_e_script = sv_dup_inc(proto_perl->Ie_script);
9641 PL_perldb = proto_perl->Iperldb;
9642 PL_perl_destruct_level = proto_perl->Iperl_destruct_level;
9643
9644 /* magical thingies */
9645 /* XXX time(&PL_basetime) when asked for? */
9646 PL_basetime = proto_perl->Ibasetime;
9647 PL_formfeed = sv_dup(proto_perl->Iformfeed);
9648
9649 PL_maxsysfd = proto_perl->Imaxsysfd;
9650 PL_multiline = proto_perl->Imultiline;
9651 PL_statusvalue = proto_perl->Istatusvalue;
9652#ifdef VMS
9653 PL_statusvalue_vms = proto_perl->Istatusvalue_vms;
9654#endif
9655
9656 /* shortcuts to various I/O objects */
9657 PL_stdingv = gv_dup(proto_perl->Istdingv);
9658 PL_stderrgv = gv_dup(proto_perl->Istderrgv);
9659 PL_defgv = gv_dup(proto_perl->Idefgv);
9660 PL_argvgv = gv_dup(proto_perl->Iargvgv);
9661 PL_argvoutgv = gv_dup(proto_perl->Iargvoutgv);
f25c30a3 9662 PL_argvout_stack = av_dup_inc(proto_perl->Iargvout_stack);
1d7c1841
GS
9663
9664 /* shortcuts to regexp stuff */
9665 PL_replgv = gv_dup(proto_perl->Ireplgv);
9666
9667 /* shortcuts to misc objects */
9668 PL_errgv = gv_dup(proto_perl->Ierrgv);
9669
9670 /* shortcuts to debugging objects */
9671 PL_DBgv = gv_dup(proto_perl->IDBgv);
9672 PL_DBline = gv_dup(proto_perl->IDBline);
9673 PL_DBsub = gv_dup(proto_perl->IDBsub);
9674 PL_DBsingle = sv_dup(proto_perl->IDBsingle);
9675 PL_DBtrace = sv_dup(proto_perl->IDBtrace);
9676 PL_DBsignal = sv_dup(proto_perl->IDBsignal);
9677 PL_lineary = av_dup(proto_perl->Ilineary);
9678 PL_dbargs = av_dup(proto_perl->Idbargs);
9679
9680 /* symbol tables */
9681 PL_defstash = hv_dup_inc(proto_perl->Tdefstash);
9682 PL_curstash = hv_dup(proto_perl->Tcurstash);
771208af 9683 PL_nullstash = hv_dup(proto_perl->Inullstash);
1d7c1841
GS
9684 PL_debstash = hv_dup(proto_perl->Idebstash);
9685 PL_globalstash = hv_dup(proto_perl->Iglobalstash);
9686 PL_curstname = sv_dup_inc(proto_perl->Icurstname);
9687
9688 PL_beginav = av_dup_inc(proto_perl->Ibeginav);
9689 PL_endav = av_dup_inc(proto_perl->Iendav);
7d30b5c4 9690 PL_checkav = av_dup_inc(proto_perl->Icheckav);
1d7c1841
GS
9691 PL_initav = av_dup_inc(proto_perl->Iinitav);
9692
9693 PL_sub_generation = proto_perl->Isub_generation;
9694
9695 /* funky return mechanisms */
9696 PL_forkprocess = proto_perl->Iforkprocess;
9697
9698 /* subprocess state */
9699 PL_fdpid = av_dup_inc(proto_perl->Ifdpid);
9700
9701 /* internal state */
9702 PL_tainting = proto_perl->Itainting;
9703 PL_maxo = proto_perl->Imaxo;
9704 if (proto_perl->Iop_mask)
9705 PL_op_mask = SAVEPVN(proto_perl->Iop_mask, PL_maxo);
9706 else
9707 PL_op_mask = Nullch;
9708
9709 /* current interpreter roots */
9710 PL_main_cv = cv_dup_inc(proto_perl->Imain_cv);
9711 PL_main_root = OpREFCNT_inc(proto_perl->Imain_root);
9712 PL_main_start = proto_perl->Imain_start;
e977893f 9713 PL_eval_root = proto_perl->Ieval_root;
1d7c1841
GS
9714 PL_eval_start = proto_perl->Ieval_start;
9715
9716 /* runtime control stuff */
9717 PL_curcopdb = (COP*)any_dup(proto_perl->Icurcopdb, proto_perl);
9718 PL_copline = proto_perl->Icopline;
9719
9720 PL_filemode = proto_perl->Ifilemode;
9721 PL_lastfd = proto_perl->Ilastfd;
9722 PL_oldname = proto_perl->Ioldname; /* XXX not quite right */
9723 PL_Argv = NULL;
9724 PL_Cmd = Nullch;
9725 PL_gensym = proto_perl->Igensym;
9726 PL_preambled = proto_perl->Ipreambled;
9727 PL_preambleav = av_dup_inc(proto_perl->Ipreambleav);
9728 PL_laststatval = proto_perl->Ilaststatval;
9729 PL_laststype = proto_perl->Ilaststype;
9730 PL_mess_sv = Nullsv;
9731
7889fe52 9732 PL_ors_sv = sv_dup_inc(proto_perl->Iors_sv);
1d7c1841
GS
9733 PL_ofmt = SAVEPV(proto_perl->Iofmt);
9734
9735 /* interpreter atexit processing */
9736 PL_exitlistlen = proto_perl->Iexitlistlen;
9737 if (PL_exitlistlen) {
9738 New(0, PL_exitlist, PL_exitlistlen, PerlExitListEntry);
9739 Copy(proto_perl->Iexitlist, PL_exitlist, PL_exitlistlen, PerlExitListEntry);
9740 }
9741 else
9742 PL_exitlist = (PerlExitListEntry*)NULL;
9743 PL_modglobal = hv_dup_inc(proto_perl->Imodglobal);
9744
9745 PL_profiledata = NULL;
9746 PL_rsfp = fp_dup(proto_perl->Irsfp, '<');
9747 /* PL_rsfp_filters entries have fake IoDIRP() */
9748 PL_rsfp_filters = av_dup_inc(proto_perl->Irsfp_filters);
9749
9750 PL_compcv = cv_dup(proto_perl->Icompcv);
9751 PL_comppad = av_dup(proto_perl->Icomppad);
9752 PL_comppad_name = av_dup(proto_perl->Icomppad_name);
9753 PL_comppad_name_fill = proto_perl->Icomppad_name_fill;
9754 PL_comppad_name_floor = proto_perl->Icomppad_name_floor;
9755 PL_curpad = (SV**)ptr_table_fetch(PL_ptr_table,
9756 proto_perl->Tcurpad);
9757
9758#ifdef HAVE_INTERP_INTERN
9759 sys_intern_dup(&proto_perl->Isys_intern, &PL_sys_intern);
9760#endif
9761
9762 /* more statics moved here */
9763 PL_generation = proto_perl->Igeneration;
9764 PL_DBcv = cv_dup(proto_perl->IDBcv);
1d7c1841
GS
9765
9766 PL_in_clean_objs = proto_perl->Iin_clean_objs;
9767 PL_in_clean_all = proto_perl->Iin_clean_all;
9768
9769 PL_uid = proto_perl->Iuid;
9770 PL_euid = proto_perl->Ieuid;
9771 PL_gid = proto_perl->Igid;
9772 PL_egid = proto_perl->Iegid;
9773 PL_nomemok = proto_perl->Inomemok;
9774 PL_an = proto_perl->Ian;
9775 PL_cop_seqmax = proto_perl->Icop_seqmax;
9776 PL_op_seqmax = proto_perl->Iop_seqmax;
9777 PL_evalseq = proto_perl->Ievalseq;
9778 PL_origenviron = proto_perl->Iorigenviron; /* XXX not quite right */
9779 PL_origalen = proto_perl->Iorigalen;
9780 PL_pidstatus = newHV(); /* XXX flag for cloning? */
9781 PL_osname = SAVEPV(proto_perl->Iosname);
9782 PL_sh_path = SAVEPV(proto_perl->Ish_path);
9783 PL_sighandlerp = proto_perl->Isighandlerp;
9784
9785
9786 PL_runops = proto_perl->Irunops;
9787
9788 Copy(proto_perl->Itokenbuf, PL_tokenbuf, 256, char);
9789
9790#ifdef CSH
9791 PL_cshlen = proto_perl->Icshlen;
9792 PL_cshname = SAVEPVN(proto_perl->Icshname, PL_cshlen);
9793#endif
9794
9795 PL_lex_state = proto_perl->Ilex_state;
9796 PL_lex_defer = proto_perl->Ilex_defer;
9797 PL_lex_expect = proto_perl->Ilex_expect;
9798 PL_lex_formbrack = proto_perl->Ilex_formbrack;
9799 PL_lex_dojoin = proto_perl->Ilex_dojoin;
9800 PL_lex_starts = proto_perl->Ilex_starts;
9801 PL_lex_stuff = sv_dup_inc(proto_perl->Ilex_stuff);
9802 PL_lex_repl = sv_dup_inc(proto_perl->Ilex_repl);
9803 PL_lex_op = proto_perl->Ilex_op;
9804 PL_lex_inpat = proto_perl->Ilex_inpat;
9805 PL_lex_inwhat = proto_perl->Ilex_inwhat;
9806 PL_lex_brackets = proto_perl->Ilex_brackets;
9807 i = (PL_lex_brackets < 120 ? 120 : PL_lex_brackets);
9808 PL_lex_brackstack = SAVEPVN(proto_perl->Ilex_brackstack,i);
9809 PL_lex_casemods = proto_perl->Ilex_casemods;
9810 i = (PL_lex_casemods < 12 ? 12 : PL_lex_casemods);
9811 PL_lex_casestack = SAVEPVN(proto_perl->Ilex_casestack,i);
9812
9813 Copy(proto_perl->Inextval, PL_nextval, 5, YYSTYPE);
9814 Copy(proto_perl->Inexttype, PL_nexttype, 5, I32);
9815 PL_nexttoke = proto_perl->Inexttoke;
9816
9817 PL_linestr = sv_dup_inc(proto_perl->Ilinestr);
9818 i = proto_perl->Ibufptr - SvPVX(proto_perl->Ilinestr);
9819 PL_bufptr = SvPVX(PL_linestr) + (i < 0 ? 0 : i);
9820 i = proto_perl->Ioldbufptr - SvPVX(proto_perl->Ilinestr);
9821 PL_oldbufptr = SvPVX(PL_linestr) + (i < 0 ? 0 : i);
9822 i = proto_perl->Ioldoldbufptr - SvPVX(proto_perl->Ilinestr);
9823 PL_oldoldbufptr = SvPVX(PL_linestr) + (i < 0 ? 0 : i);
9824 PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr);
9825 i = proto_perl->Ilinestart - SvPVX(proto_perl->Ilinestr);
9826 PL_linestart = SvPVX(PL_linestr) + (i < 0 ? 0 : i);
9827 PL_pending_ident = proto_perl->Ipending_ident;
9828 PL_sublex_info = proto_perl->Isublex_info; /* XXX not quite right */
9829
9830 PL_expect = proto_perl->Iexpect;
9831
9832 PL_multi_start = proto_perl->Imulti_start;
9833 PL_multi_end = proto_perl->Imulti_end;
9834 PL_multi_open = proto_perl->Imulti_open;
9835 PL_multi_close = proto_perl->Imulti_close;
9836
9837 PL_error_count = proto_perl->Ierror_count;
9838 PL_subline = proto_perl->Isubline;
9839 PL_subname = sv_dup_inc(proto_perl->Isubname);
9840
9841 PL_min_intro_pending = proto_perl->Imin_intro_pending;
9842 PL_max_intro_pending = proto_perl->Imax_intro_pending;
9843 PL_padix = proto_perl->Ipadix;
9844 PL_padix_floor = proto_perl->Ipadix_floor;
9845 PL_pad_reset_pending = proto_perl->Ipad_reset_pending;
9846
9847 i = proto_perl->Ilast_uni - SvPVX(proto_perl->Ilinestr);
9848 PL_last_uni = SvPVX(PL_linestr) + (i < 0 ? 0 : i);
9849 i = proto_perl->Ilast_lop - SvPVX(proto_perl->Ilinestr);
9850 PL_last_lop = SvPVX(PL_linestr) + (i < 0 ? 0 : i);
9851 PL_last_lop_op = proto_perl->Ilast_lop_op;
9852 PL_in_my = proto_perl->Iin_my;
9853 PL_in_my_stash = hv_dup(proto_perl->Iin_my_stash);
9854#ifdef FCRYPT
9855 PL_cryptseen = proto_perl->Icryptseen;
9856#endif
9857
9858 PL_hints = proto_perl->Ihints;
9859
9860 PL_amagic_generation = proto_perl->Iamagic_generation;
9861
9862#ifdef USE_LOCALE_COLLATE
9863 PL_collation_ix = proto_perl->Icollation_ix;
9864 PL_collation_name = SAVEPV(proto_perl->Icollation_name);
9865 PL_collation_standard = proto_perl->Icollation_standard;
9866 PL_collxfrm_base = proto_perl->Icollxfrm_base;
9867 PL_collxfrm_mult = proto_perl->Icollxfrm_mult;
9868#endif /* USE_LOCALE_COLLATE */
9869
9870#ifdef USE_LOCALE_NUMERIC
9871 PL_numeric_name = SAVEPV(proto_perl->Inumeric_name);
9872 PL_numeric_standard = proto_perl->Inumeric_standard;
9873 PL_numeric_local = proto_perl->Inumeric_local;
a453c169 9874 PL_numeric_radix_sv = sv_dup_inc(proto_perl->Inumeric_radix_sv);
1d7c1841
GS
9875#endif /* !USE_LOCALE_NUMERIC */
9876
9877 /* utf8 character classes */
9878 PL_utf8_alnum = sv_dup_inc(proto_perl->Iutf8_alnum);
9879 PL_utf8_alnumc = sv_dup_inc(proto_perl->Iutf8_alnumc);
9880 PL_utf8_ascii = sv_dup_inc(proto_perl->Iutf8_ascii);
9881 PL_utf8_alpha = sv_dup_inc(proto_perl->Iutf8_alpha);
9882 PL_utf8_space = sv_dup_inc(proto_perl->Iutf8_space);
9883 PL_utf8_cntrl = sv_dup_inc(proto_perl->Iutf8_cntrl);
9884 PL_utf8_graph = sv_dup_inc(proto_perl->Iutf8_graph);
9885 PL_utf8_digit = sv_dup_inc(proto_perl->Iutf8_digit);
9886 PL_utf8_upper = sv_dup_inc(proto_perl->Iutf8_upper);
9887 PL_utf8_lower = sv_dup_inc(proto_perl->Iutf8_lower);
9888 PL_utf8_print = sv_dup_inc(proto_perl->Iutf8_print);
9889 PL_utf8_punct = sv_dup_inc(proto_perl->Iutf8_punct);
9890 PL_utf8_xdigit = sv_dup_inc(proto_perl->Iutf8_xdigit);
9891 PL_utf8_mark = sv_dup_inc(proto_perl->Iutf8_mark);
9892 PL_utf8_toupper = sv_dup_inc(proto_perl->Iutf8_toupper);
9893 PL_utf8_totitle = sv_dup_inc(proto_perl->Iutf8_totitle);
9894 PL_utf8_tolower = sv_dup_inc(proto_perl->Iutf8_tolower);
9895
9896 /* swatch cache */
9897 PL_last_swash_hv = Nullhv; /* reinits on demand */
9898 PL_last_swash_klen = 0;
9899 PL_last_swash_key[0]= '\0';
9900 PL_last_swash_tmps = (U8*)NULL;
9901 PL_last_swash_slen = 0;
9902
9903 /* perly.c globals */
9904 PL_yydebug = proto_perl->Iyydebug;
9905 PL_yynerrs = proto_perl->Iyynerrs;
9906 PL_yyerrflag = proto_perl->Iyyerrflag;
9907 PL_yychar = proto_perl->Iyychar;
9908 PL_yyval = proto_perl->Iyyval;
9909 PL_yylval = proto_perl->Iyylval;
9910
9911 PL_glob_index = proto_perl->Iglob_index;
9912 PL_srand_called = proto_perl->Isrand_called;
9913 PL_uudmap['M'] = 0; /* reinits on demand */
9914 PL_bitcount = Nullch; /* reinits on demand */
9915
66fe0623
NIS
9916 if (proto_perl->Ipsig_pend) {
9917 Newz(0, PL_psig_pend, SIG_SIZE, int);
9dd79c3f 9918 }
66fe0623
NIS
9919 else {
9920 PL_psig_pend = (int*)NULL;
9921 }
9922
1d7c1841 9923 if (proto_perl->Ipsig_ptr) {
76d3c696
JH
9924 Newz(0, PL_psig_ptr, SIG_SIZE, SV*);
9925 Newz(0, PL_psig_name, SIG_SIZE, SV*);
76d3c696
JH
9926 for (i = 1; i < SIG_SIZE; i++) {
9927 PL_psig_ptr[i] = sv_dup_inc(proto_perl->Ipsig_ptr[i]);
1d7c1841
GS
9928 PL_psig_name[i] = sv_dup_inc(proto_perl->Ipsig_name[i]);
9929 }
9930 }
9931 else {
9932 PL_psig_ptr = (SV**)NULL;
9933 PL_psig_name = (SV**)NULL;
9934 }
9935
9936 /* thrdvar.h stuff */
9937
a0739874 9938 if (flags & CLONEf_COPY_STACKS) {
1d7c1841
GS
9939 /* next allocation will be PL_tmps_stack[PL_tmps_ix+1] */
9940 PL_tmps_ix = proto_perl->Ttmps_ix;
9941 PL_tmps_max = proto_perl->Ttmps_max;
9942 PL_tmps_floor = proto_perl->Ttmps_floor;
9943 Newz(50, PL_tmps_stack, PL_tmps_max, SV*);
9944 i = 0;
9945 while (i <= PL_tmps_ix) {
9946 PL_tmps_stack[i] = sv_dup_inc(proto_perl->Ttmps_stack[i]);
9947 ++i;
9948 }
9949
9950 /* next PUSHMARK() sets *(PL_markstack_ptr+1) */
9951 i = proto_perl->Tmarkstack_max - proto_perl->Tmarkstack;
9952 Newz(54, PL_markstack, i, I32);
9953 PL_markstack_max = PL_markstack + (proto_perl->Tmarkstack_max
9954 - proto_perl->Tmarkstack);
9955 PL_markstack_ptr = PL_markstack + (proto_perl->Tmarkstack_ptr
9956 - proto_perl->Tmarkstack);
9957 Copy(proto_perl->Tmarkstack, PL_markstack,
9958 PL_markstack_ptr - PL_markstack + 1, I32);
9959
9960 /* next push_scope()/ENTER sets PL_scopestack[PL_scopestack_ix]
9961 * NOTE: unlike the others! */
9962 PL_scopestack_ix = proto_perl->Tscopestack_ix;
9963 PL_scopestack_max = proto_perl->Tscopestack_max;
9964 Newz(54, PL_scopestack, PL_scopestack_max, I32);
9965 Copy(proto_perl->Tscopestack, PL_scopestack, PL_scopestack_ix, I32);
9966
9967 /* next push_return() sets PL_retstack[PL_retstack_ix]
9968 * NOTE: unlike the others! */
9969 PL_retstack_ix = proto_perl->Tretstack_ix;
9970 PL_retstack_max = proto_perl->Tretstack_max;
9971 Newz(54, PL_retstack, PL_retstack_max, OP*);
9972 Copy(proto_perl->Tretstack, PL_retstack, PL_retstack_ix, I32);
9973
9974 /* NOTE: si_dup() looks at PL_markstack */
9975 PL_curstackinfo = si_dup(proto_perl->Tcurstackinfo);
9976
9977 /* PL_curstack = PL_curstackinfo->si_stack; */
9978 PL_curstack = av_dup(proto_perl->Tcurstack);
9979 PL_mainstack = av_dup(proto_perl->Tmainstack);
9980
9981 /* next PUSHs() etc. set *(PL_stack_sp+1) */
9982 PL_stack_base = AvARRAY(PL_curstack);
9983 PL_stack_sp = PL_stack_base + (proto_perl->Tstack_sp
9984 - proto_perl->Tstack_base);
9985 PL_stack_max = PL_stack_base + AvMAX(PL_curstack);
9986
9987 /* next SSPUSHFOO() sets PL_savestack[PL_savestack_ix]
9988 * NOTE: unlike the others! */
9989 PL_savestack_ix = proto_perl->Tsavestack_ix;
9990 PL_savestack_max = proto_perl->Tsavestack_max;
9991 /*Newz(54, PL_savestack, PL_savestack_max, ANY);*/
9992 PL_savestack = ss_dup(proto_perl);
9993 }
9994 else {
9995 init_stacks();
985e7056 9996 ENTER; /* perl_destruct() wants to LEAVE; */
1d7c1841
GS
9997 }
9998
9999 PL_start_env = proto_perl->Tstart_env; /* XXXXXX */
10000 PL_top_env = &PL_start_env;
10001
10002 PL_op = proto_perl->Top;
10003
10004 PL_Sv = Nullsv;
10005 PL_Xpv = (XPV*)NULL;
10006 PL_na = proto_perl->Tna;
10007
10008 PL_statbuf = proto_perl->Tstatbuf;
10009 PL_statcache = proto_perl->Tstatcache;
10010 PL_statgv = gv_dup(proto_perl->Tstatgv);
10011 PL_statname = sv_dup_inc(proto_perl->Tstatname);
10012#ifdef HAS_TIMES
10013 PL_timesbuf = proto_perl->Ttimesbuf;
10014#endif
10015
10016 PL_tainted = proto_perl->Ttainted;
10017 PL_curpm = proto_perl->Tcurpm; /* XXX No PMOP ref count */
10018 PL_nrs = sv_dup_inc(proto_perl->Tnrs);
10019 PL_rs = sv_dup_inc(proto_perl->Trs);
10020 PL_last_in_gv = gv_dup(proto_perl->Tlast_in_gv);
7889fe52 10021 PL_ofs_sv = sv_dup_inc(proto_perl->Tofs_sv);
1d7c1841
GS
10022 PL_defoutgv = gv_dup_inc(proto_perl->Tdefoutgv);
10023 PL_chopset = proto_perl->Tchopset; /* XXX never deallocated */
10024 PL_toptarget = sv_dup_inc(proto_perl->Ttoptarget);
10025 PL_bodytarget = sv_dup_inc(proto_perl->Tbodytarget);
10026 PL_formtarget = sv_dup(proto_perl->Tformtarget);
10027
10028 PL_restartop = proto_perl->Trestartop;
10029 PL_in_eval = proto_perl->Tin_eval;
10030 PL_delaymagic = proto_perl->Tdelaymagic;
10031 PL_dirty = proto_perl->Tdirty;
10032 PL_localizing = proto_perl->Tlocalizing;
10033
14dd3ad8 10034#ifdef PERL_FLEXIBLE_EXCEPTIONS
1d7c1841 10035 PL_protect = proto_perl->Tprotect;
14dd3ad8 10036#endif
1d7c1841
GS
10037 PL_errors = sv_dup_inc(proto_perl->Terrors);
10038 PL_av_fetch_sv = Nullsv;
10039 PL_hv_fetch_sv = Nullsv;
10040 Zero(&PL_hv_fetch_ent_mh, 1, HE); /* XXX */
10041 PL_modcount = proto_perl->Tmodcount;
10042 PL_lastgotoprobe = Nullop;
10043 PL_dumpindent = proto_perl->Tdumpindent;
10044
10045 PL_sortcop = (OP*)any_dup(proto_perl->Tsortcop, proto_perl);
10046 PL_sortstash = hv_dup(proto_perl->Tsortstash);
10047 PL_firstgv = gv_dup(proto_perl->Tfirstgv);
10048 PL_secondgv = gv_dup(proto_perl->Tsecondgv);
10049 PL_sortcxix = proto_perl->Tsortcxix;
10050 PL_efloatbuf = Nullch; /* reinits on demand */
10051 PL_efloatsize = 0; /* reinits on demand */
10052
10053 /* regex stuff */
10054
10055 PL_screamfirst = NULL;
10056 PL_screamnext = NULL;
10057 PL_maxscream = -1; /* reinits on demand */
10058 PL_lastscream = Nullsv;
10059
10060 PL_watchaddr = NULL;
10061 PL_watchok = Nullch;
10062
10063 PL_regdummy = proto_perl->Tregdummy;
10064 PL_regcomp_parse = Nullch;
10065 PL_regxend = Nullch;
10066 PL_regcode = (regnode*)NULL;
10067 PL_regnaughty = 0;
10068 PL_regsawback = 0;
10069 PL_regprecomp = Nullch;
10070 PL_regnpar = 0;
10071 PL_regsize = 0;
10072 PL_regflags = 0;
10073 PL_regseen = 0;
10074 PL_seen_zerolen = 0;
10075 PL_seen_evals = 0;
10076 PL_regcomp_rx = (regexp*)NULL;
10077 PL_extralen = 0;
10078 PL_colorset = 0; /* reinits PL_colors[] */
10079 /*PL_colors[6] = {0,0,0,0,0,0};*/
10080 PL_reg_whilem_seen = 0;
10081 PL_reginput = Nullch;
10082 PL_regbol = Nullch;
10083 PL_regeol = Nullch;
10084 PL_regstartp = (I32*)NULL;
10085 PL_regendp = (I32*)NULL;
10086 PL_reglastparen = (U32*)NULL;
10087 PL_regtill = Nullch;
1d7c1841
GS
10088 PL_reg_start_tmp = (char**)NULL;
10089 PL_reg_start_tmpl = 0;
10090 PL_regdata = (struct reg_data*)NULL;
10091 PL_bostr = Nullch;
10092 PL_reg_flags = 0;
10093 PL_reg_eval_set = 0;
10094 PL_regnarrate = 0;
10095 PL_regprogram = (regnode*)NULL;
10096 PL_regindent = 0;
10097 PL_regcc = (CURCUR*)NULL;
10098 PL_reg_call_cc = (struct re_cc_state*)NULL;
10099 PL_reg_re = (regexp*)NULL;
10100 PL_reg_ganch = Nullch;
10101 PL_reg_sv = Nullsv;
10102 PL_reg_magic = (MAGIC*)NULL;
10103 PL_reg_oldpos = 0;
10104 PL_reg_oldcurpm = (PMOP*)NULL;
10105 PL_reg_curpm = (PMOP*)NULL;
10106 PL_reg_oldsaved = Nullch;
10107 PL_reg_oldsavedlen = 0;
10108 PL_reg_maxiter = 0;
10109 PL_reg_leftiter = 0;
10110 PL_reg_poscache = Nullch;
10111 PL_reg_poscache_size= 0;
10112
10113 /* RE engine - function pointers */
10114 PL_regcompp = proto_perl->Tregcompp;
10115 PL_regexecp = proto_perl->Tregexecp;
10116 PL_regint_start = proto_perl->Tregint_start;
10117 PL_regint_string = proto_perl->Tregint_string;
10118 PL_regfree = proto_perl->Tregfree;
10119
10120 PL_reginterp_cnt = 0;
10121 PL_reg_starttry = 0;
10122
a0739874
DM
10123 if (!(flags & CLONEf_KEEP_PTR_TABLE)) {
10124 ptr_table_free(PL_ptr_table);
10125 PL_ptr_table = NULL;
10126 }
4a09accc 10127
f284b03f
AMS
10128 /* Call the ->CLONE method, if it exists, for each of the stashes
10129 identified by sv_dup() above.
10130 */
4a09accc
AB
10131 while(av_len(PL_clone_callbacks) != -1) {
10132 HV* stash = (HV*) av_shift(PL_clone_callbacks);
f284b03f
AMS
10133 GV* cloner = gv_fetchmethod_autoload(stash, "CLONE", 0);
10134 if (cloner && GvCV(cloner)) {
10135 dSP;
10136 ENTER;
10137 SAVETMPS;
10138 PUSHMARK(SP);
10139 XPUSHs(newSVpv(HvNAME(stash), 0));
10140 PUTBACK;
10141 call_sv((SV*)GvCV(cloner), G_DISCARD);
10142 FREETMPS;
10143 LEAVE;
10144 }
4a09accc 10145 }
a0739874 10146
1d7c1841
GS
10147#ifdef PERL_OBJECT
10148 return (PerlInterpreter*)pPerl;
10149#else
10150 return my_perl;
10151#endif
10152}
10153
10154#else /* !USE_ITHREADS */
51371543
GS
10155
10156#ifdef PERL_OBJECT
51371543
GS
10157#include "XSUB.h"
10158#endif
10159
1d7c1841
GS
10160#endif /* USE_ITHREADS */
10161