This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
This is my patch patch.1n for perl5.001.
[perl5.git] / sv.c
CommitLineData
a0d0e21e 1/* sv.c
79072805 2 *
a0d0e21e 3 * Copyright (c) 1991-1994, 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
LW
8 */
9
10/*
11 * "I wonder what the Entish is for 'yes' and 'no'," he thought.
79072805
LW
12 */
13
14#include "EXTERN.h"
15#include "perl.h"
79072805 16
a0d0e21e
LW
17/* The following is all to get DBL_DIG, in order to pick a nice
18 default value for printing floating point numbers in Gconvert.
19 (see config.h)
20*/
21#ifdef I_LIMITS
22#include <limits.h>
23#endif
24#ifdef I_FLOAT
25#include <float.h>
26#endif
27#ifndef HAS_DBL_DIG
28#define DBL_DIG 15 /* A guess that works lots of places */
29#endif
30
31static SV *more_sv _((void));
32static XPVIV *more_xiv _((void));
33static XPVNV *more_xnv _((void));
34static XPV *more_xpv _((void));
35static XRV *more_xrv _((void));
36static SV *new_sv _((void));
37static XPVIV *new_xiv _((void));
38static XPVNV *new_xnv _((void));
39static XPV *new_xpv _((void));
40static XRV *new_xrv _((void));
41static void del_xiv _((XPVIV* p));
42static void del_xnv _((XPVNV* p));
43static void del_xpv _((XPV* p));
44static void del_xrv _((XRV* p));
45static void sv_mortalgrow _((void));
46
47static void sv_unglob _((SV* sv));
48
49#ifdef PURIFY
79072805 50
a0d0e21e
LW
51#define new_SV() sv = (SV*)safemalloc(sizeof(SV))
52#define del_SV(p) free((char*)p)
53
54#else
55
56#define new_SV() \
57 if (sv_root) { \
58 sv = sv_root; \
59 sv_root = (SV*)SvANY(sv); \
60 ++sv_count; \
61 } \
62 else \
63 sv = more_sv();
463ee0b2
LW
64
65static SV*
66new_sv()
67{
68 SV* sv;
69 if (sv_root) {
70 sv = sv_root;
71 sv_root = (SV*)SvANY(sv);
8990e307 72 ++sv_count;
463ee0b2
LW
73 return sv;
74 }
75 return more_sv();
76}
77
a0d0e21e
LW
78#ifdef DEBUGGING
79#define del_SV(p) \
80 if (debug & 32768) \
81 del_sv(p); \
82 else { \
83 SvANY(p) = (void *)sv_root; \
84 sv_root = p; \
85 --sv_count; \
86 }
87
463ee0b2
LW
88static void
89del_sv(p)
90SV* p;
91{
a0d0e21e
LW
92 if (debug & 32768) {
93 SV* sv;
94 SV* svend;
95 int ok = 0;
96 for (sv = sv_arenaroot; sv; sv = (SV *) SvANY(svend)) {
97 svend = &sv[1008 / sizeof(SV)];
98 if (p >= sv && p < svend)
99 ok = 1;
100 }
101 if (!ok) {
102 warn("Attempt to free non-arena SV: 0x%lx", (unsigned long)p);
103 return;
104 }
105 }
106 SvANY(p) = (void *) sv_root;
463ee0b2 107 sv_root = p;
8990e307 108 --sv_count;
463ee0b2 109}
a0d0e21e
LW
110#else
111#define del_SV(p) \
112 SvANY(p) = (void *)sv_root; \
113 sv_root = p; \
114 --sv_count;
115
116#endif
463ee0b2
LW
117
118static SV*
119more_sv()
120{
463ee0b2
LW
121 register SV* sv;
122 register SV* svend;
8990e307 123 sv_root = (SV*)safemalloc(1012);
463ee0b2 124 sv = sv_root;
85e6fe83 125 Zero(sv, 1012, char);
463ee0b2
LW
126 svend = &sv[1008 / sizeof(SV) - 1];
127 while (sv < svend) {
a0d0e21e 128 SvANY(sv) = (void *)(SV*)(sv + 1);
8990e307 129 SvFLAGS(sv) = SVTYPEMASK;
463ee0b2
LW
130 sv++;
131 }
132 SvANY(sv) = 0;
8990e307 133 sv++;
a0d0e21e 134 SvANY(sv) = (void *) sv_arenaroot;
8990e307 135 sv_arenaroot = sv_root;
463ee0b2
LW
136 return new_sv();
137}
748a9306 138#endif
463ee0b2 139
8990e307
LW
140void
141sv_report_used()
142{
143 SV* sv;
144 register SV* svend;
145
a0d0e21e 146 for (sv = sv_arenaroot; sv; sv = (SV *) SvANY(sv)) {
8990e307
LW
147 svend = &sv[1008 / sizeof(SV)];
148 while (sv < svend) {
149 if (SvTYPE(sv) != SVTYPEMASK) {
150 fprintf(stderr, "****\n");
151 sv_dump(sv);
152 }
153 ++sv;
154 }
155 }
156}
157
158void
a0d0e21e 159sv_clean_objs()
8990e307
LW
160{
161 register SV* sv;
162 register SV* svend;
a0d0e21e 163 SV* rv;
8990e307 164
a0d0e21e 165 for (sv = sv_arenaroot; sv; sv = (SV *) SvANY(sv)) {
8990e307
LW
166 svend = &sv[1008 / sizeof(SV)];
167 while (sv < svend) {
a0d0e21e
LW
168 if (SvROK(sv) && SvOBJECT(rv = SvRV(sv))) {
169 DEBUG_D((fprintf(stderr, "Cleaning object ref:\n "),
170 sv_dump(sv));)
171 SvROK_off(sv);
172 SvRV(sv) = 0;
173 SvREFCNT_dec(rv);
8990e307 174 }
a0d0e21e 175 /* XXX Might want to check arrays, etc. */
8990e307
LW
176 ++sv;
177 }
178 }
179}
180
181void
8990e307
LW
182sv_clean_all()
183{
184 register SV* sv;
185 register SV* svend;
186
a0d0e21e 187 for (sv = sv_arenaroot; sv; sv = (SV *) SvANY(sv)) {
8990e307
LW
188 svend = &sv[1008 / sizeof(SV)];
189 while (sv < svend) {
190 if (SvTYPE(sv) != SVTYPEMASK) {
191 DEBUG_D((fprintf(stderr, "Cleaning loops:\n "), sv_dump(sv));)
192 SvFLAGS(sv) |= SVf_BREAK;
193 SvREFCNT_dec(sv);
194 }
195 ++sv;
196 }
197 }
198}
463ee0b2 199
463ee0b2
LW
200static XPVIV*
201new_xiv()
202{
a0d0e21e 203 IV** xiv;
463ee0b2
LW
204 if (xiv_root) {
205 xiv = xiv_root;
85e6fe83
LW
206 /*
207 * See comment in more_xiv() -- RAM.
208 */
a0d0e21e 209 xiv_root = (IV**)*xiv;
463ee0b2
LW
210 return (XPVIV*)((char*)xiv - sizeof(XPV));
211 }
212 return more_xiv();
213}
214
215static void
216del_xiv(p)
217XPVIV* p;
218{
a0d0e21e
LW
219 IV** xiv = (IV**)((char*)(p) + sizeof(XPV));
220 *xiv = (IV *)xiv_root;
463ee0b2
LW
221 xiv_root = xiv;
222}
223
224static XPVIV*
225more_xiv()
226{
a0d0e21e
LW
227 register IV** xiv;
228 register IV** xivend;
229 XPV* ptr = (XPV*)safemalloc(1008);
230 ptr->xpv_pv = (char*)xiv_arenaroot; /* linked list of xiv arenas */
231 xiv_arenaroot = ptr; /* to keep Purify happy */
232
233 xiv = (IV**) ptr;
234 xivend = &xiv[1008 / sizeof(IV *) - 1];
235 xiv += (sizeof(XPV) - 1) / sizeof(IV *) + 1; /* fudge by size of XPV */
463ee0b2
LW
236 xiv_root = xiv;
237 while (xiv < xivend) {
a0d0e21e 238 *xiv = (IV *)(xiv + 1);
463ee0b2
LW
239 xiv++;
240 }
85e6fe83 241 *xiv = 0;
463ee0b2
LW
242 return new_xiv();
243}
244
463ee0b2
LW
245static XPVNV*
246new_xnv()
247{
248 double* xnv;
249 if (xnv_root) {
250 xnv = xnv_root;
251 xnv_root = *(double**)xnv;
252 return (XPVNV*)((char*)xnv - sizeof(XPVIV));
253 }
254 return more_xnv();
255}
256
257static void
258del_xnv(p)
259XPVNV* p;
260{
261 double* xnv = (double*)((char*)(p) + sizeof(XPVIV));
262 *(double**)xnv = xnv_root;
263 xnv_root = xnv;
264}
265
266static XPVNV*
267more_xnv()
268{
463ee0b2
LW
269 register double* xnv;
270 register double* xnvend;
8990e307 271 xnv = (double*)safemalloc(1008);
463ee0b2
LW
272 xnvend = &xnv[1008 / sizeof(double) - 1];
273 xnv += (sizeof(XPVIV) - 1) / sizeof(double) + 1; /* fudge by sizeof XPVIV */
274 xnv_root = xnv;
275 while (xnv < xnvend) {
276 *(double**)xnv = (double*)(xnv + 1);
277 xnv++;
278 }
279 *(double**)xnv = 0;
280 return new_xnv();
281}
282
ed6116ce
LW
283static XRV*
284new_xrv()
285{
286 XRV* xrv;
287 if (xrv_root) {
288 xrv = xrv_root;
289 xrv_root = (XRV*)xrv->xrv_rv;
290 return xrv;
291 }
292 return more_xrv();
293}
294
295static void
296del_xrv(p)
297XRV* p;
298{
299 p->xrv_rv = (SV*)xrv_root;
300 xrv_root = p;
301}
302
303static XRV*
304more_xrv()
305{
ed6116ce
LW
306 register XRV* xrv;
307 register XRV* xrvend;
8990e307 308 xrv_root = (XRV*)safemalloc(1008);
ed6116ce
LW
309 xrv = xrv_root;
310 xrvend = &xrv[1008 / sizeof(XRV) - 1];
311 while (xrv < xrvend) {
312 xrv->xrv_rv = (SV*)(xrv + 1);
313 xrv++;
314 }
315 xrv->xrv_rv = 0;
316 return new_xrv();
317}
318
463ee0b2
LW
319static XPV*
320new_xpv()
321{
322 XPV* xpv;
323 if (xpv_root) {
324 xpv = xpv_root;
325 xpv_root = (XPV*)xpv->xpv_pv;
326 return xpv;
327 }
328 return more_xpv();
329}
330
331static void
332del_xpv(p)
333XPV* p;
334{
335 p->xpv_pv = (char*)xpv_root;
336 xpv_root = p;
337}
338
339static XPV*
340more_xpv()
341{
463ee0b2
LW
342 register XPV* xpv;
343 register XPV* xpvend;
8990e307 344 xpv_root = (XPV*)safemalloc(1008);
463ee0b2
LW
345 xpv = xpv_root;
346 xpvend = &xpv[1008 / sizeof(XPV) - 1];
347 while (xpv < xpvend) {
348 xpv->xpv_pv = (char*)(xpv + 1);
349 xpv++;
350 }
351 xpv->xpv_pv = 0;
352 return new_xpv();
353}
354
355#ifdef PURIFY
8990e307 356#define new_XIV() (void*)safemalloc(sizeof(XPVIV))
463ee0b2
LW
357#define del_XIV(p) free((char*)p)
358#else
85e6fe83 359#define new_XIV() (void*)new_xiv()
463ee0b2
LW
360#define del_XIV(p) del_xiv(p)
361#endif
362
363#ifdef PURIFY
8990e307 364#define new_XNV() (void*)safemalloc(sizeof(XPVNV))
463ee0b2
LW
365#define del_XNV(p) free((char*)p)
366#else
85e6fe83 367#define new_XNV() (void*)new_xnv()
463ee0b2
LW
368#define del_XNV(p) del_xnv(p)
369#endif
370
371#ifdef PURIFY
8990e307 372#define new_XRV() (void*)safemalloc(sizeof(XRV))
ed6116ce
LW
373#define del_XRV(p) free((char*)p)
374#else
85e6fe83 375#define new_XRV() (void*)new_xrv()
ed6116ce
LW
376#define del_XRV(p) del_xrv(p)
377#endif
378
379#ifdef PURIFY
8990e307 380#define new_XPV() (void*)safemalloc(sizeof(XPV))
463ee0b2
LW
381#define del_XPV(p) free((char*)p)
382#else
85e6fe83 383#define new_XPV() (void*)new_xpv()
463ee0b2
LW
384#define del_XPV(p) del_xpv(p)
385#endif
386
8990e307 387#define new_XPVIV() (void*)safemalloc(sizeof(XPVIV))
463ee0b2
LW
388#define del_XPVIV(p) free((char*)p)
389
8990e307 390#define new_XPVNV() (void*)safemalloc(sizeof(XPVNV))
463ee0b2
LW
391#define del_XPVNV(p) free((char*)p)
392
8990e307 393#define new_XPVMG() (void*)safemalloc(sizeof(XPVMG))
463ee0b2
LW
394#define del_XPVMG(p) free((char*)p)
395
8990e307 396#define new_XPVLV() (void*)safemalloc(sizeof(XPVLV))
463ee0b2
LW
397#define del_XPVLV(p) free((char*)p)
398
8990e307 399#define new_XPVAV() (void*)safemalloc(sizeof(XPVAV))
463ee0b2
LW
400#define del_XPVAV(p) free((char*)p)
401
8990e307 402#define new_XPVHV() (void*)safemalloc(sizeof(XPVHV))
463ee0b2
LW
403#define del_XPVHV(p) free((char*)p)
404
8990e307 405#define new_XPVCV() (void*)safemalloc(sizeof(XPVCV))
463ee0b2
LW
406#define del_XPVCV(p) free((char*)p)
407
8990e307 408#define new_XPVGV() (void*)safemalloc(sizeof(XPVGV))
463ee0b2
LW
409#define del_XPVGV(p) free((char*)p)
410
8990e307 411#define new_XPVBM() (void*)safemalloc(sizeof(XPVBM))
463ee0b2
LW
412#define del_XPVBM(p) free((char*)p)
413
8990e307 414#define new_XPVFM() (void*)safemalloc(sizeof(XPVFM))
463ee0b2
LW
415#define del_XPVFM(p) free((char*)p)
416
8990e307
LW
417#define new_XPVIO() (void*)safemalloc(sizeof(XPVIO))
418#define del_XPVIO(p) free((char*)p)
419
79072805
LW
420bool
421sv_upgrade(sv, mt)
422register SV* sv;
423U32 mt;
424{
425 char* pv;
426 U32 cur;
427 U32 len;
a0d0e21e 428 IV iv;
79072805
LW
429 double nv;
430 MAGIC* magic;
431 HV* stash;
432
433 if (SvTYPE(sv) == mt)
434 return TRUE;
435
436 switch (SvTYPE(sv)) {
437 case SVt_NULL:
438 pv = 0;
439 cur = 0;
440 len = 0;
441 iv = 0;
442 nv = 0.0;
443 magic = 0;
444 stash = 0;
445 break;
79072805
LW
446 case SVt_IV:
447 pv = 0;
448 cur = 0;
449 len = 0;
463ee0b2
LW
450 iv = SvIVX(sv);
451 nv = (double)SvIVX(sv);
79072805
LW
452 del_XIV(SvANY(sv));
453 magic = 0;
454 stash = 0;
ed6116ce 455 if (mt == SVt_NV)
463ee0b2 456 mt = SVt_PVNV;
ed6116ce
LW
457 else if (mt < SVt_PVIV)
458 mt = SVt_PVIV;
79072805
LW
459 break;
460 case SVt_NV:
461 pv = 0;
462 cur = 0;
463 len = 0;
463ee0b2 464 nv = SvNVX(sv);
ed6116ce 465 iv = I_32(nv);
79072805
LW
466 magic = 0;
467 stash = 0;
468 del_XNV(SvANY(sv));
469 SvANY(sv) = 0;
ed6116ce 470 if (mt < SVt_PVNV)
79072805
LW
471 mt = SVt_PVNV;
472 break;
ed6116ce
LW
473 case SVt_RV:
474 pv = (char*)SvRV(sv);
475 cur = 0;
476 len = 0;
a0d0e21e 477 iv = (IV)pv;
ed6116ce
LW
478 nv = (double)(unsigned long)pv;
479 del_XRV(SvANY(sv));
480 magic = 0;
481 stash = 0;
482 break;
79072805
LW
483 case SVt_PV:
484 nv = 0.0;
463ee0b2 485 pv = SvPVX(sv);
79072805
LW
486 cur = SvCUR(sv);
487 len = SvLEN(sv);
488 iv = 0;
489 nv = 0.0;
490 magic = 0;
491 stash = 0;
492 del_XPV(SvANY(sv));
748a9306
LW
493 if (mt <= SVt_IV)
494 mt = SVt_PVIV;
495 else if (mt == SVt_NV)
496 mt = SVt_PVNV;
79072805
LW
497 break;
498 case SVt_PVIV:
499 nv = 0.0;
463ee0b2 500 pv = SvPVX(sv);
79072805
LW
501 cur = SvCUR(sv);
502 len = SvLEN(sv);
463ee0b2 503 iv = SvIVX(sv);
79072805
LW
504 nv = 0.0;
505 magic = 0;
506 stash = 0;
507 del_XPVIV(SvANY(sv));
508 break;
509 case SVt_PVNV:
463ee0b2
LW
510 nv = SvNVX(sv);
511 pv = SvPVX(sv);
79072805
LW
512 cur = SvCUR(sv);
513 len = SvLEN(sv);
463ee0b2
LW
514 iv = SvIVX(sv);
515 nv = SvNVX(sv);
79072805
LW
516 magic = 0;
517 stash = 0;
518 del_XPVNV(SvANY(sv));
519 break;
520 case SVt_PVMG:
463ee0b2 521 pv = SvPVX(sv);
79072805
LW
522 cur = SvCUR(sv);
523 len = SvLEN(sv);
463ee0b2
LW
524 iv = SvIVX(sv);
525 nv = SvNVX(sv);
79072805
LW
526 magic = SvMAGIC(sv);
527 stash = SvSTASH(sv);
528 del_XPVMG(SvANY(sv));
529 break;
530 default:
463ee0b2 531 croak("Can't upgrade that kind of scalar");
79072805
LW
532 }
533
534 switch (mt) {
535 case SVt_NULL:
463ee0b2 536 croak("Can't upgrade to undef");
79072805
LW
537 case SVt_IV:
538 SvANY(sv) = new_XIV();
463ee0b2 539 SvIVX(sv) = iv;
79072805
LW
540 break;
541 case SVt_NV:
542 SvANY(sv) = new_XNV();
463ee0b2 543 SvNVX(sv) = nv;
79072805 544 break;
ed6116ce
LW
545 case SVt_RV:
546 SvANY(sv) = new_XRV();
547 SvRV(sv) = (SV*)pv;
ed6116ce 548 break;
79072805
LW
549 case SVt_PV:
550 SvANY(sv) = new_XPV();
463ee0b2 551 SvPVX(sv) = pv;
79072805
LW
552 SvCUR(sv) = cur;
553 SvLEN(sv) = len;
554 break;
555 case SVt_PVIV:
556 SvANY(sv) = new_XPVIV();
463ee0b2 557 SvPVX(sv) = pv;
79072805
LW
558 SvCUR(sv) = cur;
559 SvLEN(sv) = len;
463ee0b2 560 SvIVX(sv) = iv;
79072805 561 if (SvNIOK(sv))
a0d0e21e 562 (void)SvIOK_on(sv);
79072805
LW
563 SvNOK_off(sv);
564 break;
565 case SVt_PVNV:
566 SvANY(sv) = new_XPVNV();
463ee0b2 567 SvPVX(sv) = pv;
79072805
LW
568 SvCUR(sv) = cur;
569 SvLEN(sv) = len;
463ee0b2
LW
570 SvIVX(sv) = iv;
571 SvNVX(sv) = nv;
79072805
LW
572 break;
573 case SVt_PVMG:
574 SvANY(sv) = new_XPVMG();
463ee0b2 575 SvPVX(sv) = pv;
79072805
LW
576 SvCUR(sv) = cur;
577 SvLEN(sv) = len;
463ee0b2
LW
578 SvIVX(sv) = iv;
579 SvNVX(sv) = nv;
79072805
LW
580 SvMAGIC(sv) = magic;
581 SvSTASH(sv) = stash;
582 break;
583 case SVt_PVLV:
584 SvANY(sv) = new_XPVLV();
463ee0b2 585 SvPVX(sv) = pv;
79072805
LW
586 SvCUR(sv) = cur;
587 SvLEN(sv) = len;
463ee0b2
LW
588 SvIVX(sv) = iv;
589 SvNVX(sv) = nv;
79072805
LW
590 SvMAGIC(sv) = magic;
591 SvSTASH(sv) = stash;
592 LvTARGOFF(sv) = 0;
593 LvTARGLEN(sv) = 0;
594 LvTARG(sv) = 0;
595 LvTYPE(sv) = 0;
596 break;
597 case SVt_PVAV:
598 SvANY(sv) = new_XPVAV();
463ee0b2
LW
599 if (pv)
600 Safefree(pv);
2304df62 601 SvPVX(sv) = 0;
79072805
LW
602 AvMAX(sv) = 0;
603 AvFILL(sv) = 0;
463ee0b2
LW
604 SvIVX(sv) = 0;
605 SvNVX(sv) = 0.0;
606 SvMAGIC(sv) = magic;
607 SvSTASH(sv) = stash;
608 AvALLOC(sv) = 0;
79072805
LW
609 AvARYLEN(sv) = 0;
610 AvFLAGS(sv) = 0;
611 break;
612 case SVt_PVHV:
613 SvANY(sv) = new_XPVHV();
463ee0b2
LW
614 if (pv)
615 Safefree(pv);
616 SvPVX(sv) = 0;
617 HvFILL(sv) = 0;
618 HvMAX(sv) = 0;
619 HvKEYS(sv) = 0;
620 SvNVX(sv) = 0.0;
79072805
LW
621 SvMAGIC(sv) = magic;
622 SvSTASH(sv) = stash;
79072805
LW
623 HvRITER(sv) = 0;
624 HvEITER(sv) = 0;
625 HvPMROOT(sv) = 0;
626 HvNAME(sv) = 0;
79072805
LW
627 break;
628 case SVt_PVCV:
629 SvANY(sv) = new_XPVCV();
748a9306 630 Zero(SvANY(sv), 1, XPVCV);
463ee0b2 631 SvPVX(sv) = pv;
79072805
LW
632 SvCUR(sv) = cur;
633 SvLEN(sv) = len;
463ee0b2
LW
634 SvIVX(sv) = iv;
635 SvNVX(sv) = nv;
79072805
LW
636 SvMAGIC(sv) = magic;
637 SvSTASH(sv) = stash;
79072805
LW
638 break;
639 case SVt_PVGV:
640 SvANY(sv) = new_XPVGV();
463ee0b2 641 SvPVX(sv) = pv;
79072805
LW
642 SvCUR(sv) = cur;
643 SvLEN(sv) = len;
463ee0b2
LW
644 SvIVX(sv) = iv;
645 SvNVX(sv) = nv;
79072805
LW
646 SvMAGIC(sv) = magic;
647 SvSTASH(sv) = stash;
93a17b20 648 GvGP(sv) = 0;
79072805
LW
649 GvNAME(sv) = 0;
650 GvNAMELEN(sv) = 0;
651 GvSTASH(sv) = 0;
652 break;
653 case SVt_PVBM:
654 SvANY(sv) = new_XPVBM();
463ee0b2 655 SvPVX(sv) = pv;
79072805
LW
656 SvCUR(sv) = cur;
657 SvLEN(sv) = len;
463ee0b2
LW
658 SvIVX(sv) = iv;
659 SvNVX(sv) = nv;
79072805
LW
660 SvMAGIC(sv) = magic;
661 SvSTASH(sv) = stash;
662 BmRARE(sv) = 0;
663 BmUSEFUL(sv) = 0;
664 BmPREVIOUS(sv) = 0;
665 break;
666 case SVt_PVFM:
667 SvANY(sv) = new_XPVFM();
748a9306 668 Zero(SvANY(sv), 1, XPVFM);
463ee0b2 669 SvPVX(sv) = pv;
79072805
LW
670 SvCUR(sv) = cur;
671 SvLEN(sv) = len;
463ee0b2
LW
672 SvIVX(sv) = iv;
673 SvNVX(sv) = nv;
79072805
LW
674 SvMAGIC(sv) = magic;
675 SvSTASH(sv) = stash;
79072805 676 break;
8990e307
LW
677 case SVt_PVIO:
678 SvANY(sv) = new_XPVIO();
748a9306 679 Zero(SvANY(sv), 1, XPVIO);
8990e307
LW
680 SvPVX(sv) = pv;
681 SvCUR(sv) = cur;
682 SvLEN(sv) = len;
683 SvIVX(sv) = iv;
684 SvNVX(sv) = nv;
685 SvMAGIC(sv) = magic;
686 SvSTASH(sv) = stash;
85e6fe83 687 IoPAGE_LEN(sv) = 60;
8990e307
LW
688 break;
689 }
690 SvFLAGS(sv) &= ~SVTYPEMASK;
691 SvFLAGS(sv) |= mt;
79072805
LW
692 return TRUE;
693}
694
a0d0e21e 695#ifdef DEBUGGING
79072805
LW
696char *
697sv_peek(sv)
698register SV *sv;
699{
700 char *t = tokenbuf;
a0d0e21e 701 int unref = 0;
79072805
LW
702
703 retry:
704 if (!sv) {
705 strcpy(t, "VOID");
a0d0e21e 706 goto finish;
79072805
LW
707 }
708 else if (sv == (SV*)0x55555555 || SvTYPE(sv) == 'U') {
709 strcpy(t, "WILD");
a0d0e21e
LW
710 goto finish;
711 }
712 else if (sv == &sv_undef || sv == &sv_no || sv == &sv_yes) {
713 if (sv == &sv_undef) {
714 strcpy(t, "SV_UNDEF");
715 if (!(SvFLAGS(sv) & (SVf_OK|SVf_OOK|SVs_OBJECT|
716 SVs_GMG|SVs_SMG|SVs_RMG)) &&
717 SvREADONLY(sv))
718 goto finish;
719 }
720 else if (sv == &sv_no) {
721 strcpy(t, "SV_NO");
722 if (!(SvFLAGS(sv) & (SVf_ROK|SVf_OOK|SVs_OBJECT|
723 SVs_GMG|SVs_SMG|SVs_RMG)) &&
724 !(~SvFLAGS(sv) & (SVf_POK|SVf_NOK|SVf_READONLY|
725 SVp_POK|SVp_NOK)) &&
726 SvCUR(sv) == 0 &&
727 SvNVX(sv) == 0.0)
728 goto finish;
729 }
730 else {
731 strcpy(t, "SV_YES");
732 if (!(SvFLAGS(sv) & (SVf_ROK|SVf_OOK|SVs_OBJECT|
733 SVs_GMG|SVs_SMG|SVs_RMG)) &&
734 !(~SvFLAGS(sv) & (SVf_POK|SVf_NOK|SVf_READONLY|
735 SVp_POK|SVp_NOK)) &&
736 SvCUR(sv) == 1 &&
737 SvPVX(sv) && *SvPVX(sv) == '1' &&
738 SvNVX(sv) == 1.0)
739 goto finish;
740 }
741 t += strlen(t);
742 *t++ = ':';
79072805 743 }
a0d0e21e
LW
744 else if (SvREFCNT(sv) == 0) {
745 *t++ = '(';
746 unref++;
79072805 747 }
a0d0e21e
LW
748 if (SvROK(sv)) {
749 *t++ = '\\';
750 if (t - tokenbuf + unref > 10) {
751 strcpy(tokenbuf + unref + 3,"...");
752 goto finish;
79072805 753 }
a0d0e21e
LW
754 sv = (SV*)SvRV(sv);
755 goto retry;
756 }
757 switch (SvTYPE(sv)) {
758 default:
759 strcpy(t,"FREED");
760 goto finish;
761
762 case SVt_NULL:
763 strcpy(t,"UNDEF");
764 return tokenbuf;
765 case SVt_IV:
766 strcpy(t,"IV");
767 break;
768 case SVt_NV:
769 strcpy(t,"NV");
770 break;
771 case SVt_RV:
772 strcpy(t,"RV");
773 break;
774 case SVt_PV:
775 strcpy(t,"PV");
776 break;
777 case SVt_PVIV:
778 strcpy(t,"PVIV");
779 break;
780 case SVt_PVNV:
781 strcpy(t,"PVNV");
782 break;
783 case SVt_PVMG:
784 strcpy(t,"PVMG");
785 break;
786 case SVt_PVLV:
787 strcpy(t,"PVLV");
788 break;
789 case SVt_PVAV:
790 strcpy(t,"AV");
791 break;
792 case SVt_PVHV:
793 strcpy(t,"HV");
794 break;
795 case SVt_PVCV:
796 if (CvGV(sv))
797 sprintf(t, "CV(%s)", GvNAME(CvGV(sv)));
798 else
799 strcpy(t, "CV()");
800 goto finish;
801 case SVt_PVGV:
802 strcpy(t,"GV");
803 break;
804 case SVt_PVBM:
805 strcpy(t,"BM");
806 break;
807 case SVt_PVFM:
808 strcpy(t,"FM");
809 break;
810 case SVt_PVIO:
811 strcpy(t,"IO");
812 break;
79072805
LW
813 }
814 t += strlen(t);
815
a0d0e21e 816 if (SvPOKp(sv)) {
463ee0b2 817 if (!SvPVX(sv))
a0d0e21e 818 strcpy(t, "(null)");
79072805 819 if (SvOOK(sv))
2304df62 820 sprintf(t,"(%ld+\"%.127s\")",(long)SvIVX(sv),SvPVX(sv));
79072805 821 else
2304df62 822 sprintf(t,"(\"%.127s\")",SvPVX(sv));
79072805 823 }
a0d0e21e 824 else if (SvNOKp(sv))
463ee0b2 825 sprintf(t,"(%g)",SvNVX(sv));
a0d0e21e 826 else if (SvIOKp(sv))
463ee0b2 827 sprintf(t,"(%ld)",(long)SvIVX(sv));
79072805
LW
828 else
829 strcpy(t,"()");
a0d0e21e
LW
830
831 finish:
832 if (unref) {
833 t += strlen(t);
834 while (unref--)
835 *t++ = ')';
836 *t = '\0';
837 }
79072805
LW
838 return tokenbuf;
839}
a0d0e21e 840#endif
79072805
LW
841
842int
843sv_backoff(sv)
844register SV *sv;
845{
846 assert(SvOOK(sv));
463ee0b2
LW
847 if (SvIVX(sv)) {
848 char *s = SvPVX(sv);
849 SvLEN(sv) += SvIVX(sv);
850 SvPVX(sv) -= SvIVX(sv);
79072805 851 SvIV_set(sv, 0);
463ee0b2 852 Move(s, SvPVX(sv), SvCUR(sv)+1, char);
79072805
LW
853 }
854 SvFLAGS(sv) &= ~SVf_OOK;
a0d0e21e 855 return 0;
79072805
LW
856}
857
858char *
859sv_grow(sv,newlen)
860register SV *sv;
861#ifndef DOSISH
862register I32 newlen;
863#else
864unsigned long newlen;
865#endif
866{
867 register char *s;
868
869#ifdef MSDOS
870 if (newlen >= 0x10000) {
871 fprintf(stderr, "Allocation too large: %lx\n", newlen);
872 my_exit(1);
873 }
874#endif /* MSDOS */
a0d0e21e
LW
875 if (SvROK(sv))
876 sv_unref(sv);
79072805
LW
877 if (SvTYPE(sv) < SVt_PV) {
878 sv_upgrade(sv, SVt_PV);
463ee0b2 879 s = SvPVX(sv);
79072805
LW
880 }
881 else if (SvOOK(sv)) { /* pv is offset? */
882 sv_backoff(sv);
463ee0b2 883 s = SvPVX(sv);
79072805
LW
884 if (newlen > SvLEN(sv))
885 newlen += 10 * (newlen - SvCUR(sv)); /* avoid copy each time */
886 }
887 else
463ee0b2 888 s = SvPVX(sv);
79072805 889 if (newlen > SvLEN(sv)) { /* need more room? */
85e6fe83 890 if (SvLEN(sv) && s)
79072805
LW
891 Renew(s,newlen,char);
892 else
893 New(703,s,newlen,char);
894 SvPV_set(sv, s);
895 SvLEN_set(sv, newlen);
896 }
897 return s;
898}
899
900void
901sv_setiv(sv,i)
902register SV *sv;
a0d0e21e 903IV i;
79072805 904{
ed6116ce 905 if (SvTHINKFIRST(sv)) {
8990e307 906 if (SvREADONLY(sv) && curcop != &compiling)
ed6116ce
LW
907 croak(no_modify);
908 if (SvROK(sv))
909 sv_unref(sv);
910 }
463ee0b2
LW
911 switch (SvTYPE(sv)) {
912 case SVt_NULL:
79072805 913 sv_upgrade(sv, SVt_IV);
463ee0b2
LW
914 break;
915 case SVt_NV:
916 sv_upgrade(sv, SVt_PVNV);
917 break;
ed6116ce 918 case SVt_RV:
463ee0b2 919 case SVt_PV:
79072805 920 sv_upgrade(sv, SVt_PVIV);
463ee0b2 921 break;
a0d0e21e
LW
922
923 case SVt_PVGV:
924 if (SvFAKE(sv)) {
925 sv_unglob(sv);
926 break;
927 }
928 /* FALL THROUGH */
929 case SVt_PVAV:
930 case SVt_PVHV:
931 case SVt_PVCV:
932 case SVt_PVFM:
933 case SVt_PVIO:
934 croak("Can't coerce %s to integer in %s", sv_reftype(sv,0),
935 op_name[op->op_type]);
463ee0b2
LW
936 }
937 SvIVX(sv) = i;
a0d0e21e 938 (void)SvIOK_only(sv); /* validate number */
463ee0b2 939 SvTAINT(sv);
79072805
LW
940}
941
942void
943sv_setnv(sv,num)
944register SV *sv;
945double num;
946{
ed6116ce 947 if (SvTHINKFIRST(sv)) {
8990e307 948 if (SvREADONLY(sv) && curcop != &compiling)
ed6116ce
LW
949 croak(no_modify);
950 if (SvROK(sv))
951 sv_unref(sv);
952 }
a0d0e21e
LW
953 switch (SvTYPE(sv)) {
954 case SVt_NULL:
955 case SVt_IV:
79072805 956 sv_upgrade(sv, SVt_NV);
a0d0e21e
LW
957 break;
958 case SVt_NV:
959 case SVt_RV:
960 case SVt_PV:
961 case SVt_PVIV:
79072805 962 sv_upgrade(sv, SVt_PVNV);
a0d0e21e
LW
963 /* FALL THROUGH */
964 case SVt_PVNV:
965 case SVt_PVMG:
966 case SVt_PVBM:
967 case SVt_PVLV:
968 if (SvOOK(sv))
969 (void)SvOOK_off(sv);
970 break;
971 case SVt_PVGV:
972 if (SvFAKE(sv)) {
973 sv_unglob(sv);
974 break;
975 }
976 /* FALL THROUGH */
977 case SVt_PVAV:
978 case SVt_PVHV:
979 case SVt_PVCV:
980 case SVt_PVFM:
981 case SVt_PVIO:
982 croak("Can't coerce %s to number in %s", sv_reftype(sv,0),
983 op_name[op->op_type]);
79072805 984 }
463ee0b2 985 SvNVX(sv) = num;
a0d0e21e 986 (void)SvNOK_only(sv); /* validate number */
463ee0b2 987 SvTAINT(sv);
79072805
LW
988}
989
a0d0e21e
LW
990static void
991not_a_number(sv)
992SV *sv;
993{
994 char tmpbuf[64];
995 char *d = tmpbuf;
996 char *s;
997 int i;
998
999 for (s = SvPVX(sv), i = 50; *s && i; s++,i--) {
1000 int ch = *s;
1001 if (ch & 128 && !isprint(ch)) {
1002 *d++ = 'M';
1003 *d++ = '-';
1004 ch &= 127;
1005 }
1006 if (isprint(ch))
1007 *d++ = ch;
1008 else {
1009 *d++ = '^';
1010 *d++ = ch ^ 64;
1011 }
1012 }
1013 if (*s) {
1014 *d++ = '.';
1015 *d++ = '.';
1016 *d++ = '.';
1017 }
1018 *d = '\0';
1019
1020 if (op)
1021 warn("Argument \"%s\" isn't numeric for %s", tmpbuf,
1022 op_name[op->op_type]);
1023 else
1024 warn("Argument \"%s\" isn't numeric", tmpbuf);
1025}
1026
1027IV
79072805
LW
1028sv_2iv(sv)
1029register SV *sv;
1030{
1031 if (!sv)
1032 return 0;
8990e307 1033 if (SvGMAGICAL(sv)) {
463ee0b2
LW
1034 mg_get(sv);
1035 if (SvIOKp(sv))
1036 return SvIVX(sv);
748a9306
LW
1037 if (SvNOKp(sv)) {
1038 if (SvNVX(sv) < 0.0)
1039 return I_V(SvNVX(sv));
1040 else
5d94fbed 1041 return (IV) U_V(SvNVX(sv));
748a9306 1042 }
a0d0e21e
LW
1043 if (SvPOKp(sv) && SvLEN(sv)) {
1044 if (dowarn && !looks_like_number(sv))
1045 not_a_number(sv);
1046 return (IV)atol(SvPVX(sv));
1047 }
16d20bd9
AD
1048 if (!SvROK(sv)) {
1049 return 0;
1050 }
463ee0b2 1051 }
ed6116ce 1052 if (SvTHINKFIRST(sv)) {
a0d0e21e
LW
1053 if (SvROK(sv)) {
1054#ifdef OVERLOAD
1055 SV* tmpstr;
1056 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv, numer)))
1057 return SvIV(tmpstr);
1058#endif /* OVERLOAD */
1059 return (IV)SvRV(sv);
1060 }
ed6116ce 1061 if (SvREADONLY(sv)) {
748a9306
LW
1062 if (SvNOKp(sv)) {
1063 if (SvNVX(sv) < 0.0)
1064 return I_V(SvNVX(sv));
1065 else
5d94fbed 1066 return (IV) U_V(SvNVX(sv));
748a9306
LW
1067 }
1068 if (SvPOKp(sv) && SvLEN(sv)) {
a0d0e21e
LW
1069 if (dowarn && !looks_like_number(sv))
1070 not_a_number(sv);
1071 return (IV)atol(SvPVX(sv));
1072 }
ed6116ce 1073 if (dowarn)
8990e307 1074 warn(warn_uninit);
ed6116ce
LW
1075 return 0;
1076 }
79072805 1077 }
463ee0b2 1078 switch (SvTYPE(sv)) {
463ee0b2 1079 case SVt_NULL:
79072805 1080 sv_upgrade(sv, SVt_IV);
463ee0b2
LW
1081 return SvIVX(sv);
1082 case SVt_PV:
79072805 1083 sv_upgrade(sv, SVt_PVIV);
463ee0b2
LW
1084 break;
1085 case SVt_NV:
1086 sv_upgrade(sv, SVt_PVNV);
1087 break;
1088 }
748a9306
LW
1089 if (SvNOKp(sv)) {
1090 if (SvNVX(sv) < 0.0)
1091 SvIVX(sv) = I_V(SvNVX(sv));
1092 else
5d94fbed 1093 SvIVX(sv) = (IV) U_V(SvNVX(sv));
748a9306
LW
1094 }
1095 else if (SvPOKp(sv) && SvLEN(sv)) {
a0d0e21e
LW
1096 if (dowarn && !looks_like_number(sv))
1097 not_a_number(sv);
1098 SvIVX(sv) = (IV)atol(SvPVX(sv));
93a17b20 1099 }
79072805 1100 else {
a0d0e21e 1101 if (dowarn && !localizing)
8990e307 1102 warn(warn_uninit);
a0d0e21e 1103 return 0;
79072805 1104 }
a0d0e21e
LW
1105 (void)SvIOK_on(sv);
1106 DEBUG_c(fprintf(stderr,"0x%lx 2iv(%ld)\n",
1107 (unsigned long)sv,(long)SvIVX(sv)));
463ee0b2 1108 return SvIVX(sv);
79072805
LW
1109}
1110
1111double
1112sv_2nv(sv)
1113register SV *sv;
1114{
1115 if (!sv)
1116 return 0.0;
8990e307 1117 if (SvGMAGICAL(sv)) {
463ee0b2
LW
1118 mg_get(sv);
1119 if (SvNOKp(sv))
1120 return SvNVX(sv);
a0d0e21e 1121 if (SvPOKp(sv) && SvLEN(sv)) {
748a9306 1122 if (dowarn && !SvIOKp(sv) && !looks_like_number(sv))
a0d0e21e 1123 not_a_number(sv);
463ee0b2 1124 return atof(SvPVX(sv));
a0d0e21e 1125 }
463ee0b2
LW
1126 if (SvIOKp(sv))
1127 return (double)SvIVX(sv);
16d20bd9
AD
1128 if (!SvROK(sv)) {
1129 return 0;
1130 }
463ee0b2 1131 }
ed6116ce 1132 if (SvTHINKFIRST(sv)) {
a0d0e21e
LW
1133 if (SvROK(sv)) {
1134#ifdef OVERLOAD
1135 SV* tmpstr;
1136 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)))
1137 return SvNV(tmpstr);
1138#endif /* OVERLOAD */
1139 return (double)(unsigned long)SvRV(sv);
1140 }
ed6116ce 1141 if (SvREADONLY(sv)) {
748a9306
LW
1142 if (SvPOKp(sv) && SvLEN(sv)) {
1143 if (dowarn && !SvIOKp(sv) && !looks_like_number(sv))
a0d0e21e 1144 not_a_number(sv);
ed6116ce 1145 return atof(SvPVX(sv));
a0d0e21e 1146 }
748a9306 1147 if (SvIOKp(sv))
8990e307 1148 return (double)SvIVX(sv);
ed6116ce 1149 if (dowarn)
8990e307 1150 warn(warn_uninit);
ed6116ce
LW
1151 return 0.0;
1152 }
79072805
LW
1153 }
1154 if (SvTYPE(sv) < SVt_NV) {
463ee0b2
LW
1155 if (SvTYPE(sv) == SVt_IV)
1156 sv_upgrade(sv, SVt_PVNV);
1157 else
1158 sv_upgrade(sv, SVt_NV);
a0d0e21e 1159 DEBUG_c(fprintf(stderr,"0x%lx num(%g)\n",(unsigned long)sv,SvNVX(sv)));
79072805
LW
1160 }
1161 else if (SvTYPE(sv) < SVt_PVNV)
1162 sv_upgrade(sv, SVt_PVNV);
748a9306
LW
1163 if (SvIOKp(sv) &&
1164 (!SvPOKp(sv) || !strchr(SvPVX(sv),'.') || !looks_like_number(sv)))
93a17b20 1165 {
463ee0b2 1166 SvNVX(sv) = (double)SvIVX(sv);
93a17b20 1167 }
748a9306
LW
1168 else if (SvPOKp(sv) && SvLEN(sv)) {
1169 if (dowarn && !SvIOKp(sv) && !looks_like_number(sv))
a0d0e21e 1170 not_a_number(sv);
463ee0b2 1171 SvNVX(sv) = atof(SvPVX(sv));
93a17b20 1172 }
79072805 1173 else {
a0d0e21e 1174 if (dowarn && !localizing)
8990e307 1175 warn(warn_uninit);
a0d0e21e 1176 return 0.0;
79072805
LW
1177 }
1178 SvNOK_on(sv);
a0d0e21e 1179 DEBUG_c(fprintf(stderr,"0x%lx 2nv(%g)\n",(unsigned long)sv,SvNVX(sv)));
463ee0b2 1180 return SvNVX(sv);
79072805
LW
1181}
1182
1183char *
463ee0b2 1184sv_2pv(sv, lp)
79072805 1185register SV *sv;
463ee0b2 1186STRLEN *lp;
79072805
LW
1187{
1188 register char *s;
1189 int olderrno;
1190
463ee0b2
LW
1191 if (!sv) {
1192 *lp = 0;
1193 return "";
1194 }
8990e307 1195 if (SvGMAGICAL(sv)) {
463ee0b2
LW
1196 mg_get(sv);
1197 if (SvPOKp(sv)) {
1198 *lp = SvCUR(sv);
1199 return SvPVX(sv);
1200 }
1201 if (SvIOKp(sv)) {
a0d0e21e
LW
1202 (void)sprintf(tokenbuf,"%ld",(long)SvIVX(sv));
1203 goto tokensave;
463ee0b2
LW
1204 }
1205 if (SvNOKp(sv)) {
a0d0e21e
LW
1206 Gconvert(SvNVX(sv), DBL_DIG, 0, tokenbuf);
1207 goto tokensave;
463ee0b2 1208 }
16d20bd9
AD
1209 if (!SvROK(sv)) {
1210 *lp = 0;
1211 return "";
1212 }
463ee0b2 1213 }
ed6116ce
LW
1214 if (SvTHINKFIRST(sv)) {
1215 if (SvROK(sv)) {
a0d0e21e
LW
1216#ifdef OVERLOAD
1217 SV* tmpstr;
1218 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,string)))
1219 return SvPV(tmpstr,*lp);
1220#endif /* OVERLOAD */
ed6116ce
LW
1221 sv = (SV*)SvRV(sv);
1222 if (!sv)
1223 s = "NULLREF";
1224 else {
1225 switch (SvTYPE(sv)) {
1226 case SVt_NULL:
1227 case SVt_IV:
1228 case SVt_NV:
1229 case SVt_RV:
1230 case SVt_PV:
1231 case SVt_PVIV:
1232 case SVt_PVNV:
1233 case SVt_PVBM:
1234 case SVt_PVMG: s = "SCALAR"; break;
1235 case SVt_PVLV: s = "LVALUE"; break;
1236 case SVt_PVAV: s = "ARRAY"; break;
1237 case SVt_PVHV: s = "HASH"; break;
1238 case SVt_PVCV: s = "CODE"; break;
1239 case SVt_PVGV: s = "GLOB"; break;
1240 case SVt_PVFM: s = "FORMATLINE"; break;
8990e307 1241 case SVt_PVIO: s = "FILEHANDLE"; break;
ed6116ce
LW
1242 default: s = "UNKNOWN"; break;
1243 }
1244 if (SvOBJECT(sv))
1245 sprintf(tokenbuf, "%s=%s(0x%lx)",
1246 HvNAME(SvSTASH(sv)), s, (unsigned long)sv);
1247 else
1248 sprintf(tokenbuf, "%s(0x%lx)", s, (unsigned long)sv);
a0d0e21e 1249 goto tokensaveref;
463ee0b2 1250 }
ed6116ce
LW
1251 *lp = strlen(s);
1252 return s;
79072805 1253 }
ed6116ce 1254 if (SvREADONLY(sv)) {
748a9306 1255 if (SvNOKp(sv)) {
a0d0e21e
LW
1256 Gconvert(SvNVX(sv), DBL_DIG, 0, tokenbuf);
1257 goto tokensave;
ed6116ce 1258 }
8bb9dbe4
LW
1259 if (SvIOKp(sv)) {
1260 (void)sprintf(tokenbuf,"%ld",(long)SvIVX(sv));
1261 goto tokensave;
1262 }
ed6116ce 1263 if (dowarn)
8990e307 1264 warn(warn_uninit);
ed6116ce
LW
1265 *lp = 0;
1266 return "";
79072805 1267 }
79072805
LW
1268 }
1269 if (!SvUPGRADE(sv, SVt_PV))
1270 return 0;
748a9306 1271 if (SvNOKp(sv)) {
79072805
LW
1272 if (SvTYPE(sv) < SVt_PVNV)
1273 sv_upgrade(sv, SVt_PVNV);
1274 SvGROW(sv, 28);
463ee0b2 1275 s = SvPVX(sv);
79072805 1276 olderrno = errno; /* some Xenix systems wipe out errno here */
79072805 1277#ifdef apollo
463ee0b2 1278 if (SvNVX(sv) == 0.0)
79072805
LW
1279 (void)strcpy(s,"0");
1280 else
1281#endif /*apollo*/
a0d0e21e 1282 Gconvert(SvNVX(sv), DBL_DIG, 0, s);
79072805 1283 errno = olderrno;
a0d0e21e
LW
1284#ifdef FIXNEGATIVEZERO
1285 if (*s == '-' && s[1] == '0' && !s[2])
1286 strcpy(s,"0");
1287#endif
79072805
LW
1288 while (*s) s++;
1289#ifdef hcx
1290 if (s[-1] == '.')
1291 s--;
1292#endif
1293 }
748a9306 1294 else if (SvIOKp(sv)) {
79072805
LW
1295 if (SvTYPE(sv) < SVt_PVIV)
1296 sv_upgrade(sv, SVt_PVIV);
1297 SvGROW(sv, 11);
463ee0b2 1298 s = SvPVX(sv);
79072805 1299 olderrno = errno; /* some Xenix systems wipe out errno here */
a0d0e21e 1300 (void)sprintf(s,"%ld",(long)SvIVX(sv));
79072805
LW
1301 errno = olderrno;
1302 while (*s) s++;
1303 }
1304 else {
a0d0e21e 1305 if (dowarn && !localizing)
8990e307 1306 warn(warn_uninit);
a0d0e21e
LW
1307 *lp = 0;
1308 return "";
79072805
LW
1309 }
1310 *s = '\0';
463ee0b2
LW
1311 *lp = s - SvPVX(sv);
1312 SvCUR_set(sv, *lp);
79072805 1313 SvPOK_on(sv);
a0d0e21e 1314 DEBUG_c(fprintf(stderr,"0x%lx 2pv(%s)\n",(unsigned long)sv,SvPVX(sv)));
463ee0b2 1315 return SvPVX(sv);
a0d0e21e
LW
1316
1317 tokensave:
1318 if (SvROK(sv)) { /* XXX Skip this when sv_pvn_force calls */
1319 /* Sneaky stuff here */
1320
1321 tokensaveref:
1322 sv = sv_newmortal();
1323 *lp = strlen(tokenbuf);
1324 sv_setpvn(sv, tokenbuf, *lp);
1325 return SvPVX(sv);
1326 }
1327 else {
1328 STRLEN len;
1329
1330#ifdef FIXNEGATIVEZERO
1331 if (*tokenbuf == '-' && tokenbuf[1] == '0' && !tokenbuf[2])
1332 strcpy(tokenbuf,"0");
1333#endif
1334 (void)SvUPGRADE(sv, SVt_PV);
1335 len = *lp = strlen(tokenbuf);
1336 s = SvGROW(sv, len + 1);
1337 SvCUR_set(sv, len);
1338 (void)strcpy(s, tokenbuf);
1339 /* NO SvPOK_on(sv) here! */
1340 return s;
1341 }
463ee0b2
LW
1342}
1343
1344/* This function is only called on magical items */
1345bool
1346sv_2bool(sv)
1347register SV *sv;
1348{
8990e307 1349 if (SvGMAGICAL(sv))
463ee0b2
LW
1350 mg_get(sv);
1351
a0d0e21e
LW
1352 if (!SvOK(sv))
1353 return 0;
1354 if (SvROK(sv)) {
1355#ifdef OVERLOAD
1356 {
1357 SV* tmpsv;
1358 if (SvAMAGIC(sv) && (tmpsv = AMG_CALLun(sv,bool_)))
1359 return SvTRUE(tmpsv);
1360 }
1361#endif /* OVERLOAD */
1362 return SvRV(sv) != 0;
1363 }
463ee0b2
LW
1364 if (SvPOKp(sv)) {
1365 register XPV* Xpv;
1366 if ((Xpv = (XPV*)SvANY(sv)) &&
1367 (*Xpv->xpv_pv > '0' ||
1368 Xpv->xpv_cur > 1 ||
1369 (Xpv->xpv_cur && *Xpv->xpv_pv != '0')))
1370 return 1;
1371 else
1372 return 0;
1373 }
1374 else {
1375 if (SvIOKp(sv))
1376 return SvIVX(sv) != 0;
1377 else {
1378 if (SvNOKp(sv))
1379 return SvNVX(sv) != 0.0;
1380 else
1381 return FALSE;
1382 }
1383 }
79072805
LW
1384}
1385
1386/* Note: sv_setsv() should not be called with a source string that needs
463ee0b2 1387 * to be reused, since it may destroy the source string if it is marked
79072805
LW
1388 * as temporary.
1389 */
1390
1391void
1392sv_setsv(dstr,sstr)
1393SV *dstr;
1394register SV *sstr;
1395{
8990e307
LW
1396 register U32 sflags;
1397 register int dtype;
1398 register int stype;
463ee0b2 1399
79072805
LW
1400 if (sstr == dstr)
1401 return;
ed6116ce 1402 if (SvTHINKFIRST(dstr)) {
8990e307 1403 if (SvREADONLY(dstr) && curcop != &compiling)
ed6116ce
LW
1404 croak(no_modify);
1405 if (SvROK(dstr))
1406 sv_unref(dstr);
1407 }
79072805
LW
1408 if (!sstr)
1409 sstr = &sv_undef;
8990e307
LW
1410 stype = SvTYPE(sstr);
1411 dtype = SvTYPE(dstr);
79072805 1412
8e07c86e
AD
1413 if (dtype == SVt_PVGV && (SvFLAGS(dstr) & SVf_FAKE)) {
1414 sv_unglob(dstr); /* so fake GLOB won't perpetuate */
1415 SvPOK_only(dstr);
1416 dtype = SvTYPE(dstr);
1417 }
1418
a0d0e21e
LW
1419#ifdef OVERLOAD
1420 SvAMAGIC_off(dstr);
1421#endif /* OVERLOAD */
463ee0b2 1422 /* There's a lot of redundancy below but we're going for speed here */
79072805 1423
8990e307 1424 switch (stype) {
79072805 1425 case SVt_NULL:
a0d0e21e 1426 (void)SvOK_off(dstr);
79072805 1427 return;
463ee0b2 1428 case SVt_IV:
8990e307
LW
1429 if (dtype <= SVt_PV) {
1430 if (dtype < SVt_IV)
1431 sv_upgrade(dstr, SVt_IV);
8990e307
LW
1432 else if (dtype == SVt_NV)
1433 sv_upgrade(dstr, SVt_PVNV);
a0d0e21e
LW
1434 else if (dtype <= SVt_PV)
1435 sv_upgrade(dstr, SVt_PVIV);
8990e307 1436 }
463ee0b2
LW
1437 break;
1438 case SVt_NV:
8990e307
LW
1439 if (dtype <= SVt_PVIV) {
1440 if (dtype < SVt_NV)
1441 sv_upgrade(dstr, SVt_NV);
8990e307
LW
1442 else if (dtype == SVt_PVIV)
1443 sv_upgrade(dstr, SVt_PVNV);
a0d0e21e
LW
1444 else if (dtype <= SVt_PV)
1445 sv_upgrade(dstr, SVt_PVNV);
8990e307 1446 }
463ee0b2 1447 break;
ed6116ce 1448 case SVt_RV:
8990e307 1449 if (dtype < SVt_RV)
ed6116ce 1450 sv_upgrade(dstr, SVt_RV);
ed6116ce 1451 break;
463ee0b2 1452 case SVt_PV:
8990e307 1453 if (dtype < SVt_PV)
463ee0b2 1454 sv_upgrade(dstr, SVt_PV);
463ee0b2
LW
1455 break;
1456 case SVt_PVIV:
8990e307 1457 if (dtype < SVt_PVIV)
463ee0b2 1458 sv_upgrade(dstr, SVt_PVIV);
463ee0b2
LW
1459 break;
1460 case SVt_PVNV:
8990e307 1461 if (dtype < SVt_PVNV)
463ee0b2 1462 sv_upgrade(dstr, SVt_PVNV);
463ee0b2 1463 break;
79072805 1464 case SVt_PVGV:
8990e307 1465 if (dtype <= SVt_PVGV) {
a0d0e21e
LW
1466 if (dtype < SVt_PVGV) {
1467 char *name = GvNAME(sstr);
1468 STRLEN len = GvNAMELEN(sstr);
463ee0b2 1469 sv_upgrade(dstr, SVt_PVGV);
a0d0e21e
LW
1470 sv_magic(dstr, dstr, '*', name, len);
1471 GvSTASH(dstr) = GvSTASH(sstr);
1472 GvNAME(dstr) = savepvn(name, len);
1473 GvNAMELEN(dstr) = len;
1474 SvFAKE_on(dstr); /* can coerce to non-glob */
1475 }
1476 (void)SvOK_off(dstr);
79072805
LW
1477 if (!GvAV(sstr))
1478 gv_AVadd(sstr);
1479 if (!GvHV(sstr))
1480 gv_HVadd(sstr);
1481 if (!GvIO(sstr))
a0d0e21e 1482 gv_IOadd(sstr);
79072805
LW
1483 if (GvGP(dstr))
1484 gp_free(dstr);
1485 GvGP(dstr) = gp_ref(GvGP(sstr));
8990e307 1486 SvTAINT(dstr);
a0d0e21e 1487 GvFLAGS(dstr) &= ~GVf_INTRO; /* one-shot flag */
748a9306 1488 SvMULTI_on(dstr);
79072805
LW
1489 return;
1490 }
1491 /* FALL THROUGH */
1492
1493 default:
8990e307
LW
1494 if (dtype < stype)
1495 sv_upgrade(dstr, stype);
1496 if (SvGMAGICAL(sstr))
79072805 1497 mg_get(sstr);
79072805
LW
1498 }
1499
8990e307
LW
1500 sflags = SvFLAGS(sstr);
1501
1502 if (sflags & SVf_ROK) {
1503 if (dtype >= SVt_PV) {
1504 if (dtype == SVt_PVGV) {
1505 SV *sref = SvREFCNT_inc(SvRV(sstr));
1506 SV *dref = 0;
a0d0e21e
LW
1507 int intro = GvFLAGS(dstr) & GVf_INTRO;
1508
1509 if (intro) {
1510 GP *gp;
1511 GvGP(dstr)->gp_refcnt--;
1512 Newz(602,gp, 1, GP);
1513 GvGP(dstr) = gp;
1514 GvREFCNT(dstr) = 1;
1515 GvSV(dstr) = NEWSV(72,0);
1516 GvLINE(dstr) = curcop->cop_line;
1517 GvEGV(dstr) = dstr;
1518 GvFLAGS(dstr) &= ~GVf_INTRO; /* one-shot flag */
1519 }
1520 SvMULTI_on(dstr);
8990e307
LW
1521 switch (SvTYPE(sref)) {
1522 case SVt_PVAV:
a0d0e21e
LW
1523 if (intro)
1524 SAVESPTR(GvAV(dstr));
1525 else
1526 dref = (SV*)GvAV(dstr);
8990e307
LW
1527 GvAV(dstr) = (AV*)sref;
1528 break;
1529 case SVt_PVHV:
a0d0e21e
LW
1530 if (intro)
1531 SAVESPTR(GvHV(dstr));
1532 else
1533 dref = (SV*)GvHV(dstr);
8990e307
LW
1534 GvHV(dstr) = (HV*)sref;
1535 break;
1536 case SVt_PVCV:
a0d0e21e
LW
1537 if (intro)
1538 SAVESPTR(GvCV(dstr));
748a9306
LW
1539 else {
1540 CV* cv = GvCV(dstr);
1541 dref = (SV*)cv;
1542 if (dowarn && cv && sref != dref &&
1543 !GvCVGEN((GV*)dstr) &&
1544 (CvROOT(cv) || CvXSUB(cv)) )
1545 warn("Subroutine %s redefined", GvENAME((GV*)dstr));
1546 }
a0d0e21e 1547 GvFLAGS(dstr) |= GVf_IMPORTED;
8990e307
LW
1548 GvCV(dstr) = (CV*)sref;
1549 break;
1550 default:
a0d0e21e
LW
1551 if (intro)
1552 SAVESPTR(GvSV(dstr));
1553 else
1554 dref = (SV*)GvSV(dstr);
8990e307
LW
1555 GvSV(dstr) = sref;
1556 break;
1557 }
1558 if (dref)
1559 SvREFCNT_dec(dref);
a0d0e21e
LW
1560 if (intro)
1561 SAVEFREESV(sref);
8990e307
LW
1562 SvTAINT(dstr);
1563 return;
1564 }
a0d0e21e 1565 if (SvPVX(dstr)) {
8990e307 1566 Safefree(SvPVX(dstr));
a0d0e21e
LW
1567 SvLEN(dstr)=SvCUR(dstr)=0;
1568 }
8990e307 1569 }
a0d0e21e 1570 (void)SvOK_off(dstr);
8990e307 1571 SvRV(dstr) = SvREFCNT_inc(SvRV(sstr));
ed6116ce 1572 SvROK_on(dstr);
8990e307 1573 if (sflags & SVp_NOK) {
ed6116ce
LW
1574 SvNOK_on(dstr);
1575 SvNVX(dstr) = SvNVX(sstr);
1576 }
8990e307 1577 if (sflags & SVp_IOK) {
a0d0e21e 1578 (void)SvIOK_on(dstr);
ed6116ce
LW
1579 SvIVX(dstr) = SvIVX(sstr);
1580 }
a0d0e21e
LW
1581#ifdef OVERLOAD
1582 if (SvAMAGIC(sstr)) {
1583 SvAMAGIC_on(dstr);
1584 }
1585#endif /* OVERLOAD */
ed6116ce 1586 }
8990e307 1587 else if (sflags & SVp_POK) {
79072805
LW
1588
1589 /*
1590 * Check to see if we can just swipe the string. If so, it's a
1591 * possible small lose on short strings, but a big win on long ones.
463ee0b2
LW
1592 * It might even be a win on short strings if SvPVX(dstr)
1593 * has to be allocated and SvPVX(sstr) has to be freed.
79072805
LW
1594 */
1595
1596 if (SvTEMP(sstr)) { /* slated for free anyway? */
adbc6bb1 1597 if (SvPVX(dstr)) { /* we know that dtype >= SVt_PV */
a0d0e21e 1598 (void)SvOOK_off(dstr);
463ee0b2 1599 Safefree(SvPVX(dstr));
79072805 1600 }
463ee0b2 1601 SvPV_set(dstr, SvPVX(sstr));
79072805
LW
1602 SvLEN_set(dstr, SvLEN(sstr));
1603 SvCUR_set(dstr, SvCUR(sstr));
a0d0e21e 1604 (void)SvPOK_only(dstr);
79072805
LW
1605 SvTEMP_off(dstr);
1606 SvPV_set(sstr, Nullch);
1607 SvLEN_set(sstr, 0);
1608 SvPOK_off(sstr); /* wipe out any weird flags */
463ee0b2 1609 SvPVX(sstr) = 0; /* so sstr frees uneventfully */
79072805
LW
1610 }
1611 else { /* have to copy actual string */
8990e307
LW
1612 STRLEN len = SvCUR(sstr);
1613
1614 SvGROW(dstr, len + 1); /* inlined from sv_setpvn */
1615 Move(SvPVX(sstr),SvPVX(dstr),len,char);
1616 SvCUR_set(dstr, len);
1617 *SvEND(dstr) = '\0';
a0d0e21e 1618 (void)SvPOK_only(dstr);
79072805
LW
1619 }
1620 /*SUPPRESS 560*/
8990e307 1621 if (sflags & SVp_NOK) {
79072805 1622 SvNOK_on(dstr);
463ee0b2 1623 SvNVX(dstr) = SvNVX(sstr);
79072805 1624 }
8990e307 1625 if (sflags & SVp_IOK) {
a0d0e21e 1626 (void)SvIOK_on(dstr);
463ee0b2 1627 SvIVX(dstr) = SvIVX(sstr);
79072805
LW
1628 }
1629 }
8990e307 1630 else if (sflags & SVp_NOK) {
463ee0b2 1631 SvNVX(dstr) = SvNVX(sstr);
a0d0e21e 1632 (void)SvNOK_only(dstr);
79072805 1633 if (SvIOK(sstr)) {
a0d0e21e 1634 (void)SvIOK_on(dstr);
463ee0b2 1635 SvIVX(dstr) = SvIVX(sstr);
79072805
LW
1636 }
1637 }
8990e307 1638 else if (sflags & SVp_IOK) {
a0d0e21e 1639 (void)SvIOK_only(dstr);
463ee0b2 1640 SvIVX(dstr) = SvIVX(sstr);
79072805
LW
1641 }
1642 else {
a0d0e21e
LW
1643 (void)SvOK_off(dstr);
1644 }
463ee0b2 1645 SvTAINT(dstr);
79072805
LW
1646}
1647
1648void
1649sv_setpvn(sv,ptr,len)
1650register SV *sv;
1651register char *ptr;
1652register STRLEN len;
1653{
748a9306 1654 assert(len >= 0);
ed6116ce 1655 if (SvTHINKFIRST(sv)) {
8990e307 1656 if (SvREADONLY(sv) && curcop != &compiling)
ed6116ce
LW
1657 croak(no_modify);
1658 if (SvROK(sv))
1659 sv_unref(sv);
1660 }
463ee0b2 1661 if (!ptr) {
a0d0e21e 1662 (void)SvOK_off(sv);
463ee0b2
LW
1663 return;
1664 }
79072805
LW
1665 if (!SvUPGRADE(sv, SVt_PV))
1666 return;
1667 SvGROW(sv, len + 1);
a0d0e21e 1668 Move(ptr,SvPVX(sv),len,char);
79072805
LW
1669 SvCUR_set(sv, len);
1670 *SvEND(sv) = '\0';
a0d0e21e 1671 (void)SvPOK_only(sv); /* validate pointer */
463ee0b2 1672 SvTAINT(sv);
79072805
LW
1673}
1674
1675void
1676sv_setpv(sv,ptr)
1677register SV *sv;
1678register char *ptr;
1679{
1680 register STRLEN len;
1681
ed6116ce 1682 if (SvTHINKFIRST(sv)) {
8990e307 1683 if (SvREADONLY(sv) && curcop != &compiling)
ed6116ce
LW
1684 croak(no_modify);
1685 if (SvROK(sv))
1686 sv_unref(sv);
1687 }
463ee0b2 1688 if (!ptr) {
a0d0e21e 1689 (void)SvOK_off(sv);
463ee0b2
LW
1690 return;
1691 }
79072805
LW
1692 len = strlen(ptr);
1693 if (!SvUPGRADE(sv, SVt_PV))
1694 return;
1695 SvGROW(sv, len + 1);
463ee0b2 1696 Move(ptr,SvPVX(sv),len+1,char);
79072805 1697 SvCUR_set(sv, len);
a0d0e21e 1698 (void)SvPOK_only(sv); /* validate pointer */
463ee0b2
LW
1699 SvTAINT(sv);
1700}
1701
1702void
1703sv_usepvn(sv,ptr,len)
1704register SV *sv;
1705register char *ptr;
1706register STRLEN len;
1707{
ed6116ce 1708 if (SvTHINKFIRST(sv)) {
8990e307 1709 if (SvREADONLY(sv) && curcop != &compiling)
ed6116ce
LW
1710 croak(no_modify);
1711 if (SvROK(sv))
1712 sv_unref(sv);
1713 }
463ee0b2
LW
1714 if (!SvUPGRADE(sv, SVt_PV))
1715 return;
1716 if (!ptr) {
a0d0e21e 1717 (void)SvOK_off(sv);
463ee0b2
LW
1718 return;
1719 }
1720 if (SvPVX(sv))
1721 Safefree(SvPVX(sv));
1722 Renew(ptr, len+1, char);
1723 SvPVX(sv) = ptr;
1724 SvCUR_set(sv, len);
1725 SvLEN_set(sv, len+1);
1726 *SvEND(sv) = '\0';
a0d0e21e 1727 (void)SvPOK_only(sv); /* validate pointer */
463ee0b2 1728 SvTAINT(sv);
79072805
LW
1729}
1730
1731void
1732sv_chop(sv,ptr) /* like set but assuming ptr is in sv */
1733register SV *sv;
1734register char *ptr;
1735{
1736 register STRLEN delta;
1737
a0d0e21e 1738 if (!ptr || !SvPOKp(sv))
79072805 1739 return;
ed6116ce 1740 if (SvTHINKFIRST(sv)) {
8990e307 1741 if (SvREADONLY(sv) && curcop != &compiling)
ed6116ce
LW
1742 croak(no_modify);
1743 if (SvROK(sv))
1744 sv_unref(sv);
1745 }
79072805
LW
1746 if (SvTYPE(sv) < SVt_PVIV)
1747 sv_upgrade(sv,SVt_PVIV);
1748
1749 if (!SvOOK(sv)) {
463ee0b2 1750 SvIVX(sv) = 0;
79072805
LW
1751 SvFLAGS(sv) |= SVf_OOK;
1752 }
8990e307 1753 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVp_IOK|SVp_NOK);
463ee0b2 1754 delta = ptr - SvPVX(sv);
79072805
LW
1755 SvLEN(sv) -= delta;
1756 SvCUR(sv) -= delta;
463ee0b2
LW
1757 SvPVX(sv) += delta;
1758 SvIVX(sv) += delta;
79072805
LW
1759}
1760
1761void
1762sv_catpvn(sv,ptr,len)
1763register SV *sv;
1764register char *ptr;
1765register STRLEN len;
1766{
463ee0b2 1767 STRLEN tlen;
748a9306 1768 char *junk;
a0d0e21e 1769
748a9306 1770 junk = SvPV_force(sv, tlen);
463ee0b2
LW
1771 SvGROW(sv, tlen + len + 1);
1772 Move(ptr,SvPVX(sv)+tlen,len,char);
79072805
LW
1773 SvCUR(sv) += len;
1774 *SvEND(sv) = '\0';
a0d0e21e 1775 (void)SvPOK_only(sv); /* validate pointer */
463ee0b2 1776 SvTAINT(sv);
79072805
LW
1777}
1778
1779void
1780sv_catsv(dstr,sstr)
1781SV *dstr;
1782register SV *sstr;
1783{
1784 char *s;
463ee0b2 1785 STRLEN len;
79072805
LW
1786 if (!sstr)
1787 return;
463ee0b2
LW
1788 if (s = SvPV(sstr, len))
1789 sv_catpvn(dstr,s,len);
79072805
LW
1790}
1791
1792void
1793sv_catpv(sv,ptr)
1794register SV *sv;
1795register char *ptr;
1796{
1797 register STRLEN len;
463ee0b2 1798 STRLEN tlen;
748a9306 1799 char *junk;
79072805 1800
79072805
LW
1801 if (!ptr)
1802 return;
748a9306 1803 junk = SvPV_force(sv, tlen);
79072805 1804 len = strlen(ptr);
463ee0b2
LW
1805 SvGROW(sv, tlen + len + 1);
1806 Move(ptr,SvPVX(sv)+tlen,len+1,char);
79072805 1807 SvCUR(sv) += len;
a0d0e21e 1808 (void)SvPOK_only(sv); /* validate pointer */
463ee0b2 1809 SvTAINT(sv);
79072805
LW
1810}
1811
79072805
LW
1812SV *
1813#ifdef LEAKTEST
1814newSV(x,len)
1815I32 x;
1816#else
1817newSV(len)
1818#endif
1819STRLEN len;
1820{
1821 register SV *sv;
1822
463ee0b2 1823 new_SV();
8990e307
LW
1824 SvANY(sv) = 0;
1825 SvREFCNT(sv) = 1;
1826 SvFLAGS(sv) = 0;
79072805
LW
1827 if (len) {
1828 sv_upgrade(sv, SVt_PV);
1829 SvGROW(sv, len + 1);
1830 }
1831 return sv;
1832}
1833
1834void
1835sv_magic(sv, obj, how, name, namlen)
1836register SV *sv;
1837SV *obj;
a0d0e21e 1838int how;
79072805 1839char *name;
463ee0b2 1840I32 namlen;
79072805
LW
1841{
1842 MAGIC* mg;
1843
a0d0e21e
LW
1844 if (SvREADONLY(sv) && curcop != &compiling && !strchr("gB", how))
1845 croak(no_modify);
463ee0b2 1846 if (SvMAGICAL(sv)) {
748a9306
LW
1847 if (SvMAGIC(sv) && (mg = mg_find(sv, how))) {
1848 if (how == 't')
1849 mg->mg_len |= 1;
463ee0b2 1850 return;
748a9306 1851 }
463ee0b2
LW
1852 }
1853 else {
1854 if (!SvUPGRADE(sv, SVt_PVMG))
1855 return;
463ee0b2 1856 }
79072805
LW
1857 Newz(702,mg, 1, MAGIC);
1858 mg->mg_moremagic = SvMAGIC(sv);
463ee0b2 1859
79072805 1860 SvMAGIC(sv) = mg;
748a9306 1861 if (!obj || obj == sv || how == '#')
8990e307 1862 mg->mg_obj = obj;
85e6fe83 1863 else {
8990e307 1864 mg->mg_obj = SvREFCNT_inc(obj);
85e6fe83
LW
1865 mg->mg_flags |= MGf_REFCOUNTED;
1866 }
79072805 1867 mg->mg_type = how;
463ee0b2
LW
1868 mg->mg_len = namlen;
1869 if (name && namlen >= 0)
a0d0e21e 1870 mg->mg_ptr = savepvn(name, namlen);
79072805
LW
1871 switch (how) {
1872 case 0:
1873 mg->mg_virtual = &vtbl_sv;
1874 break;
a0d0e21e
LW
1875#ifdef OVERLOAD
1876 case 'A':
1877 mg->mg_virtual = &vtbl_amagic;
1878 break;
1879 case 'a':
1880 mg->mg_virtual = &vtbl_amagicelem;
1881 break;
1882 case 'c':
1883 mg->mg_virtual = 0;
1884 break;
1885#endif /* OVERLOAD */
79072805
LW
1886 case 'B':
1887 mg->mg_virtual = &vtbl_bm;
1888 break;
79072805
LW
1889 case 'E':
1890 mg->mg_virtual = &vtbl_env;
1891 break;
1892 case 'e':
1893 mg->mg_virtual = &vtbl_envelem;
1894 break;
93a17b20
LW
1895 case 'g':
1896 mg->mg_virtual = &vtbl_mglob;
1897 break;
463ee0b2
LW
1898 case 'I':
1899 mg->mg_virtual = &vtbl_isa;
1900 break;
1901 case 'i':
1902 mg->mg_virtual = &vtbl_isaelem;
1903 break;
79072805 1904 case 'L':
a0d0e21e 1905 SvRMAGICAL_on(sv);
93a17b20
LW
1906 mg->mg_virtual = 0;
1907 break;
1908 case 'l':
79072805
LW
1909 mg->mg_virtual = &vtbl_dbline;
1910 break;
463ee0b2
LW
1911 case 'P':
1912 mg->mg_virtual = &vtbl_pack;
1913 break;
1914 case 'p':
a0d0e21e 1915 case 'q':
463ee0b2
LW
1916 mg->mg_virtual = &vtbl_packelem;
1917 break;
79072805
LW
1918 case 'S':
1919 mg->mg_virtual = &vtbl_sig;
1920 break;
1921 case 's':
1922 mg->mg_virtual = &vtbl_sigelem;
1923 break;
463ee0b2
LW
1924 case 't':
1925 mg->mg_virtual = &vtbl_taint;
748a9306 1926 mg->mg_len = 1;
463ee0b2 1927 break;
79072805
LW
1928 case 'U':
1929 mg->mg_virtual = &vtbl_uvar;
1930 break;
1931 case 'v':
1932 mg->mg_virtual = &vtbl_vec;
1933 break;
1934 case 'x':
1935 mg->mg_virtual = &vtbl_substr;
1936 break;
1937 case '*':
1938 mg->mg_virtual = &vtbl_glob;
1939 break;
1940 case '#':
1941 mg->mg_virtual = &vtbl_arylen;
1942 break;
a0d0e21e
LW
1943 case '.':
1944 mg->mg_virtual = &vtbl_pos;
1945 break;
1946 case '~': /* reserved for extensions but multiple extensions may clash */
1947 break;
79072805 1948 default:
463ee0b2
LW
1949 croak("Don't know how to handle magic of type '%c'", how);
1950 }
8990e307
LW
1951 mg_magical(sv);
1952 if (SvGMAGICAL(sv))
1953 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK);
463ee0b2
LW
1954}
1955
1956int
1957sv_unmagic(sv, type)
1958SV* sv;
a0d0e21e 1959int type;
463ee0b2
LW
1960{
1961 MAGIC* mg;
1962 MAGIC** mgp;
1963 if (!SvMAGICAL(sv))
1964 return 0;
1965 mgp = &SvMAGIC(sv);
1966 for (mg = *mgp; mg; mg = *mgp) {
1967 if (mg->mg_type == type) {
1968 MGVTBL* vtbl = mg->mg_virtual;
1969 *mgp = mg->mg_moremagic;
1970 if (vtbl && vtbl->svt_free)
1971 (*vtbl->svt_free)(sv, mg);
1972 if (mg->mg_ptr && mg->mg_type != 'g')
1973 Safefree(mg->mg_ptr);
a0d0e21e
LW
1974 if (mg->mg_flags & MGf_REFCOUNTED)
1975 SvREFCNT_dec(mg->mg_obj);
463ee0b2
LW
1976 Safefree(mg);
1977 }
1978 else
1979 mgp = &mg->mg_moremagic;
79072805 1980 }
463ee0b2
LW
1981 if (!SvMAGIC(sv)) {
1982 SvMAGICAL_off(sv);
8990e307 1983 SvFLAGS(sv) |= (SvFLAGS(sv) & (SVp_IOK|SVp_NOK|SVp_POK)) >> PRIVSHIFT;
463ee0b2
LW
1984 }
1985
1986 return 0;
79072805
LW
1987}
1988
1989void
1990sv_insert(bigstr,offset,len,little,littlelen)
1991SV *bigstr;
1992STRLEN offset;
1993STRLEN len;
1994char *little;
1995STRLEN littlelen;
1996{
1997 register char *big;
1998 register char *mid;
1999 register char *midend;
2000 register char *bigend;
2001 register I32 i;
2002
8990e307
LW
2003 if (!bigstr)
2004 croak("Can't modify non-existent substring");
a0d0e21e 2005 SvPV_force(bigstr, na);
79072805
LW
2006
2007 i = littlelen - len;
2008 if (i > 0) { /* string might grow */
a0d0e21e 2009 big = SvGROW(bigstr, SvCUR(bigstr) + i + 1);
79072805
LW
2010 mid = big + offset + len;
2011 midend = bigend = big + SvCUR(bigstr);
2012 bigend += i;
2013 *bigend = '\0';
2014 while (midend > mid) /* shove everything down */
2015 *--bigend = *--midend;
2016 Move(little,big+offset,littlelen,char);
2017 SvCUR(bigstr) += i;
2018 SvSETMAGIC(bigstr);
2019 return;
2020 }
2021 else if (i == 0) {
463ee0b2 2022 Move(little,SvPVX(bigstr)+offset,len,char);
79072805
LW
2023 SvSETMAGIC(bigstr);
2024 return;
2025 }
2026
463ee0b2 2027 big = SvPVX(bigstr);
79072805
LW
2028 mid = big + offset;
2029 midend = mid + len;
2030 bigend = big + SvCUR(bigstr);
2031
2032 if (midend > bigend)
463ee0b2 2033 croak("panic: sv_insert");
79072805
LW
2034
2035 if (mid - big > bigend - midend) { /* faster to shorten from end */
2036 if (littlelen) {
2037 Move(little, mid, littlelen,char);
2038 mid += littlelen;
2039 }
2040 i = bigend - midend;
2041 if (i > 0) {
2042 Move(midend, mid, i,char);
2043 mid += i;
2044 }
2045 *mid = '\0';
2046 SvCUR_set(bigstr, mid - big);
2047 }
2048 /*SUPPRESS 560*/
2049 else if (i = mid - big) { /* faster from front */
2050 midend -= littlelen;
2051 mid = midend;
2052 sv_chop(bigstr,midend-i);
2053 big += i;
2054 while (i--)
2055 *--midend = *--big;
2056 if (littlelen)
2057 Move(little, mid, littlelen,char);
2058 }
2059 else if (littlelen) {
2060 midend -= littlelen;
2061 sv_chop(bigstr,midend);
2062 Move(little,midend,littlelen,char);
2063 }
2064 else {
2065 sv_chop(bigstr,midend);
2066 }
2067 SvSETMAGIC(bigstr);
2068}
2069
2070/* make sv point to what nstr did */
2071
2072void
2073sv_replace(sv,nsv)
2074register SV *sv;
2075register SV *nsv;
2076{
2077 U32 refcnt = SvREFCNT(sv);
ed6116ce 2078 if (SvTHINKFIRST(sv)) {
8990e307 2079 if (SvREADONLY(sv) && curcop != &compiling)
ed6116ce
LW
2080 croak(no_modify);
2081 if (SvROK(sv))
2082 sv_unref(sv);
2083 }
79072805
LW
2084 if (SvREFCNT(nsv) != 1)
2085 warn("Reference miscount in sv_replace()");
93a17b20 2086 if (SvMAGICAL(sv)) {
a0d0e21e
LW
2087 if (SvMAGICAL(nsv))
2088 mg_free(nsv);
2089 else
2090 sv_upgrade(nsv, SVt_PVMG);
93a17b20 2091 SvMAGIC(nsv) = SvMAGIC(sv);
a0d0e21e 2092 SvFLAGS(nsv) |= SvMAGICAL(sv);
93a17b20
LW
2093 SvMAGICAL_off(sv);
2094 SvMAGIC(sv) = 0;
2095 }
79072805
LW
2096 SvREFCNT(sv) = 0;
2097 sv_clear(sv);
2098 StructCopy(nsv,sv,SV);
2099 SvREFCNT(sv) = refcnt;
463ee0b2 2100 del_SV(nsv);
79072805
LW
2101}
2102
2103void
2104sv_clear(sv)
2105register SV *sv;
2106{
2107 assert(sv);
2108 assert(SvREFCNT(sv) == 0);
2109
ed6116ce 2110 if (SvOBJECT(sv)) {
463ee0b2 2111 dSP;
463ee0b2
LW
2112 GV* destructor;
2113
a0d0e21e
LW
2114 if (defstash) { /* Still have a symbol table? */
2115 destructor = gv_fetchmethod(SvSTASH(sv), "DESTROY");
2116
2117 ENTER;
2118 SAVEFREESV(SvSTASH(sv));
2119 if (destructor && GvCV(destructor)) {
2120 SV ref;
2121
2122 Zero(&ref, 1, SV);
2123 sv_upgrade(&ref, SVt_RV);
2124 SAVEI32(SvREFCNT(sv));
2125 SvRV(&ref) = SvREFCNT_inc(sv);
2126 SvROK_on(&ref);
2127
2128 EXTEND(SP, 2);
2129 PUSHMARK(SP);
2130 PUSHs(&ref);
2131 PUTBACK;
2132 perl_call_sv((SV*)destructor, G_DISCARD|G_EVAL);
748a9306 2133 del_XRV(SvANY(&ref));
a0d0e21e
LW
2134 }
2135 LEAVE;
2136 }
2137 if (SvOBJECT(sv)) {
2138 SvOBJECT_off(sv); /* Curse the object. */
2139 if (SvTYPE(sv) != SVt_PVIO)
2140 --sv_objcount; /* XXX Might want something more general */
2141 }
463ee0b2 2142 }
a0d0e21e
LW
2143 if (SvMAGICAL(sv))
2144 mg_free(sv);
79072805 2145 switch (SvTYPE(sv)) {
8990e307
LW
2146 case SVt_PVIO:
2147 Safefree(IoTOP_NAME(sv));
2148 Safefree(IoFMT_NAME(sv));
2149 Safefree(IoBOTTOM_NAME(sv));
a0d0e21e 2150 /* FALL THROUGH */
79072805 2151 case SVt_PVBM:
a0d0e21e 2152 goto freescalar;
79072805 2153 case SVt_PVCV:
748a9306 2154 case SVt_PVFM:
85e6fe83 2155 cv_undef((CV*)sv);
a0d0e21e 2156 goto freescalar;
79072805 2157 case SVt_PVHV:
85e6fe83 2158 hv_undef((HV*)sv);
a0d0e21e 2159 break;
79072805 2160 case SVt_PVAV:
85e6fe83 2161 av_undef((AV*)sv);
a0d0e21e
LW
2162 break;
2163 case SVt_PVGV:
2164 gp_free(sv);
2165 Safefree(GvNAME(sv));
2166 /* FALL THROUGH */
79072805 2167 case SVt_PVLV:
79072805 2168 case SVt_PVMG:
79072805
LW
2169 case SVt_PVNV:
2170 case SVt_PVIV:
a0d0e21e
LW
2171 freescalar:
2172 (void)SvOOK_off(sv);
79072805
LW
2173 /* FALL THROUGH */
2174 case SVt_PV:
a0d0e21e 2175 case SVt_RV:
8990e307
LW
2176 if (SvROK(sv))
2177 SvREFCNT_dec(SvRV(sv));
2178 else if (SvPVX(sv))
463ee0b2 2179 Safefree(SvPVX(sv));
79072805 2180 break;
a0d0e21e 2181/*
79072805 2182 case SVt_NV:
79072805 2183 case SVt_IV:
79072805
LW
2184 case SVt_NULL:
2185 break;
a0d0e21e 2186*/
79072805
LW
2187 }
2188
2189 switch (SvTYPE(sv)) {
2190 case SVt_NULL:
2191 break;
79072805
LW
2192 case SVt_IV:
2193 del_XIV(SvANY(sv));
2194 break;
2195 case SVt_NV:
2196 del_XNV(SvANY(sv));
2197 break;
ed6116ce
LW
2198 case SVt_RV:
2199 del_XRV(SvANY(sv));
2200 break;
79072805
LW
2201 case SVt_PV:
2202 del_XPV(SvANY(sv));
2203 break;
2204 case SVt_PVIV:
2205 del_XPVIV(SvANY(sv));
2206 break;
2207 case SVt_PVNV:
2208 del_XPVNV(SvANY(sv));
2209 break;
2210 case SVt_PVMG:
2211 del_XPVMG(SvANY(sv));
2212 break;
2213 case SVt_PVLV:
2214 del_XPVLV(SvANY(sv));
2215 break;
2216 case SVt_PVAV:
2217 del_XPVAV(SvANY(sv));
2218 break;
2219 case SVt_PVHV:
2220 del_XPVHV(SvANY(sv));
2221 break;
2222 case SVt_PVCV:
2223 del_XPVCV(SvANY(sv));
2224 break;
2225 case SVt_PVGV:
2226 del_XPVGV(SvANY(sv));
2227 break;
2228 case SVt_PVBM:
2229 del_XPVBM(SvANY(sv));
2230 break;
2231 case SVt_PVFM:
2232 del_XPVFM(SvANY(sv));
2233 break;
8990e307
LW
2234 case SVt_PVIO:
2235 del_XPVIO(SvANY(sv));
2236 break;
79072805 2237 }
a0d0e21e 2238 SvFLAGS(sv) &= SVf_BREAK;
8990e307 2239 SvFLAGS(sv) |= SVTYPEMASK;
79072805
LW
2240}
2241
2242SV *
8990e307 2243sv_newref(sv)
79072805
LW
2244SV* sv;
2245{
463ee0b2
LW
2246 if (sv)
2247 SvREFCNT(sv)++;
79072805
LW
2248 return sv;
2249}
2250
2251void
2252sv_free(sv)
2253SV *sv;
2254{
2255 if (!sv)
2256 return;
a0d0e21e
LW
2257 if (SvREADONLY(sv)) {
2258 if (sv == &sv_undef || sv == &sv_yes || sv == &sv_no)
2259 return;
79072805 2260 }
a0d0e21e
LW
2261 if (SvREFCNT(sv) == 0) {
2262 if (SvFLAGS(sv) & SVf_BREAK)
2263 return;
79072805
LW
2264 warn("Attempt to free unreferenced scalar");
2265 return;
2266 }
8990e307
LW
2267 if (--SvREFCNT(sv) > 0)
2268 return;
463ee0b2
LW
2269#ifdef DEBUGGING
2270 if (SvTEMP(sv)) {
2271 warn("Attempt to free temp prematurely");
79072805 2272 return;
79072805 2273 }
463ee0b2 2274#endif
79072805 2275 sv_clear(sv);
79072805
LW
2276 del_SV(sv);
2277}
2278
2279STRLEN
2280sv_len(sv)
2281register SV *sv;
2282{
748a9306 2283 char *junk;
463ee0b2 2284 STRLEN len;
79072805
LW
2285
2286 if (!sv)
2287 return 0;
2288
8990e307
LW
2289 if (SvGMAGICAL(sv))
2290 len = mg_len(sv);
2291 else
748a9306 2292 junk = SvPV(sv, len);
463ee0b2 2293 return len;
79072805
LW
2294}
2295
2296I32
2297sv_eq(str1,str2)
2298register SV *str1;
2299register SV *str2;
2300{
2301 char *pv1;
463ee0b2 2302 STRLEN cur1;
79072805 2303 char *pv2;
463ee0b2 2304 STRLEN cur2;
79072805
LW
2305
2306 if (!str1) {
2307 pv1 = "";
2308 cur1 = 0;
2309 }
463ee0b2
LW
2310 else
2311 pv1 = SvPV(str1, cur1);
79072805
LW
2312
2313 if (!str2)
2314 return !cur1;
463ee0b2
LW
2315 else
2316 pv2 = SvPV(str2, cur2);
79072805
LW
2317
2318 if (cur1 != cur2)
2319 return 0;
2320
2321 return !bcmp(pv1, pv2, cur1);
2322}
2323
2324I32
2325sv_cmp(str1,str2)
2326register SV *str1;
2327register SV *str2;
2328{
2329 I32 retval;
2330 char *pv1;
463ee0b2 2331 STRLEN cur1;
79072805 2332 char *pv2;
463ee0b2 2333 STRLEN cur2;
79072805
LW
2334
2335 if (!str1) {
2336 pv1 = "";
2337 cur1 = 0;
2338 }
463ee0b2
LW
2339 else
2340 pv1 = SvPV(str1, cur1);
79072805
LW
2341
2342 if (!str2) {
2343 pv2 = "";
2344 cur2 = 0;
2345 }
463ee0b2
LW
2346 else
2347 pv2 = SvPV(str2, cur2);
79072805
LW
2348
2349 if (!cur1)
2350 return cur2 ? -1 : 0;
2351 if (!cur2)
2352 return 1;
2353
2354 if (cur1 < cur2) {
2355 /*SUPPRESS 560*/
85e6fe83 2356 if (retval = memcmp((void*)pv1, (void*)pv2, cur1))
79072805
LW
2357 return retval < 0 ? -1 : 1;
2358 else
2359 return -1;
2360 }
2361 /*SUPPRESS 560*/
85e6fe83 2362 else if (retval = memcmp((void*)pv1, (void*)pv2, cur2))
79072805
LW
2363 return retval < 0 ? -1 : 1;
2364 else if (cur1 == cur2)
2365 return 0;
2366 else
2367 return 1;
2368}
2369
2370char *
2371sv_gets(sv,fp,append)
2372register SV *sv;
2373register FILE *fp;
2374I32 append;
2375{
2376 register char *bp; /* we're going to steal some values */
c2960299 2377#if defined(USE_STDIO_PTR) && defined(STDIO_PTR_LVALUE) && defined(STDIO_CNT_LVALUE)
79072805
LW
2378 register I32 cnt; /* from the stdio struct and put EVERYTHING */
2379 register STDCHAR *ptr; /* in the innermost loop into registers */
79072805
LW
2380 STRLEN bpx;
2381 I32 shortbuffered;
748a9306
LW
2382#endif
2383 register I32 newline = rschar;/* (assuming >= 6 registers) */
2384 I32 i;
79072805 2385
ed6116ce 2386 if (SvTHINKFIRST(sv)) {
8990e307 2387 if (SvREADONLY(sv) && curcop != &compiling)
ed6116ce
LW
2388 croak(no_modify);
2389 if (SvROK(sv))
2390 sv_unref(sv);
2391 }
79072805 2392 if (!SvUPGRADE(sv, SVt_PV))
a0d0e21e 2393 return 0;
79072805
LW
2394 if (rspara) { /* have to do this both before and after */
2395 do { /* to make sure file boundaries work right */
a0d0e21e
LW
2396 if (feof(fp))
2397 return 0;
79072805
LW
2398 i = getc(fp);
2399 if (i != '\n') {
a0d0e21e
LW
2400 if (i == -1)
2401 return 0;
79072805
LW
2402 ungetc(i,fp);
2403 break;
2404 }
2405 } while (i != EOF);
2406 }
c2960299
AD
2407#if defined(USE_STDIO_PTR) && defined(STDIO_PTR_LVALUE) && defined(STDIO_CNT_LVALUE)
2408 /* Here is some breathtakingly efficient cheating */
16d20bd9 2409 cnt = FILE_cnt(fp); /* get count into register */
a0d0e21e 2410 (void)SvPOK_only(sv); /* validate pointer */
79072805
LW
2411 if (SvLEN(sv) - append <= cnt + 1) { /* make sure we have the room */
2412 if (cnt > 80 && SvLEN(sv) > append) {
2413 shortbuffered = cnt - SvLEN(sv) + append + 1;
2414 cnt -= shortbuffered;
2415 }
2416 else {
2417 shortbuffered = 0;
2418 SvGROW(sv, append+cnt+2);/* (remembering cnt can be -1) */
2419 }
2420 }
2421 else
2422 shortbuffered = 0;
463ee0b2 2423 bp = SvPVX(sv) + append; /* move these two too to registers */
16d20bd9 2424 ptr = FILE_ptr(fp);
79072805
LW
2425 for (;;) {
2426 screamer:
93a17b20
LW
2427 if (cnt > 0) {
2428 while (--cnt >= 0) { /* this */ /* eat */
2429 if ((*bp++ = *ptr++) == newline) /* really */ /* dust */
2430 goto thats_all_folks; /* screams */ /* sed :-) */
2431 }
79072805
LW
2432 }
2433
748a9306 2434 if (shortbuffered) { /* oh well, must extend */
79072805
LW
2435 cnt = shortbuffered;
2436 shortbuffered = 0;
463ee0b2 2437 bpx = bp - SvPVX(sv); /* prepare for possible relocation */
79072805
LW
2438 SvCUR_set(sv, bpx);
2439 SvGROW(sv, SvLEN(sv) + append + cnt + 2);
463ee0b2 2440 bp = SvPVX(sv) + bpx; /* reconstitute our pointer */
79072805
LW
2441 continue;
2442 }
2443
16d20bd9
AD
2444 FILE_cnt(fp) = cnt; /* deregisterize cnt and ptr */
2445 FILE_ptr(fp) = ptr;
79072805 2446 i = _filbuf(fp); /* get more characters */
16d20bd9
AD
2447 cnt = FILE_cnt(fp);
2448 ptr = FILE_ptr(fp); /* reregisterize cnt and ptr */
79072805 2449
748a9306
LW
2450 if (i == EOF) /* all done for ever? */
2451 goto thats_really_all_folks;
2452
2453 bpx = bp - SvPVX(sv); /* prepare for possible relocation */
79072805
LW
2454 SvCUR_set(sv, bpx);
2455 SvGROW(sv, bpx + cnt + 2);
748a9306 2456 bp = SvPVX(sv) + bpx; /* reconstitute our pointer */
79072805
LW
2457
2458 if (i == newline) { /* all done for now? */
2459 *bp++ = i;
2460 goto thats_all_folks;
2461 }
79072805
LW
2462 *bp++ = i; /* now go back to screaming loop */
2463 }
2464
2465thats_all_folks:
463ee0b2 2466 if (rslen > 1 && (bp - SvPVX(sv) < rslen || bcmp(bp - rslen, rs, rslen)))
748a9306 2467 goto screamer; /* go back to the fray */
79072805
LW
2468thats_really_all_folks:
2469 if (shortbuffered)
2470 cnt += shortbuffered;
16d20bd9
AD
2471 FILE_cnt(fp) = cnt; /* put these back or we're in trouble */
2472 FILE_ptr(fp) = ptr;
79072805 2473 *bp = '\0';
463ee0b2 2474 SvCUR_set(sv, bp - SvPVX(sv)); /* set length */
79072805 2475
c2960299
AD
2476#else /* USE_STDIO_PTR && STDIO_PTR_LVALUE && STDIO_CNT_LVALUE */
2477 /*The big, slow, and stupid way */
79072805
LW
2478 {
2479 char buf[8192];
2480 register char * bpe = buf + sizeof(buf) - 3;
2481
2482screamer:
2483 bp = buf;
2484 while ((i = getc(fp)) != EOF && (*bp++ = i) != newline && bp < bpe) ;
2485
2486 if (append)
2487 sv_catpvn(sv, buf, bp - buf);
2488 else
2489 sv_setpvn(sv, buf, bp - buf);
2490 if (i != EOF /* joy */
2491 &&
2492 (i != newline
2493 ||
2494 (rslen > 1
2495 &&
2496 (SvCUR(sv) < rslen
2497 ||
463ee0b2 2498 bcmp(SvPVX(sv) + SvCUR(sv) - rslen, rs, rslen)
79072805
LW
2499 )
2500 )
2501 )
2502 )
2503 {
2504 append = -1;
2505 goto screamer;
2506 }
2507 }
2508
c2960299 2509#endif /* USE_STDIO_PTR && STDIO_PTR_LVALUE && STDIO_CNT_LVALUE */
79072805
LW
2510
2511 if (rspara) {
2512 while (i != EOF) {
2513 i = getc(fp);
2514 if (i != '\n') {
2515 ungetc(i,fp);
2516 break;
2517 }
2518 }
2519 }
463ee0b2 2520 return SvCUR(sv) - append ? SvPVX(sv) : Nullch;
79072805
LW
2521}
2522
2523void
2524sv_inc(sv)
2525register SV *sv;
2526{
2527 register char *d;
463ee0b2 2528 int flags;
79072805
LW
2529
2530 if (!sv)
2531 return;
ed6116ce 2532 if (SvTHINKFIRST(sv)) {
8990e307 2533 if (SvREADONLY(sv) && curcop != &compiling)
ed6116ce 2534 croak(no_modify);
a0d0e21e
LW
2535 if (SvROK(sv)) {
2536#ifdef OVERLOAD
2537 if (SvAMAGIC(sv) && AMG_CALLun(sv,inc)) return;
2538#endif /* OVERLOAD */
2539 sv_unref(sv);
2540 }
ed6116ce 2541 }
8990e307 2542 if (SvGMAGICAL(sv))
79072805 2543 mg_get(sv);
8990e307
LW
2544 flags = SvFLAGS(sv);
2545 if (flags & SVp_IOK) {
463ee0b2 2546 ++SvIVX(sv);
a0d0e21e 2547 (void)SvIOK_only(sv);
79072805
LW
2548 return;
2549 }
8990e307 2550 if (flags & SVp_NOK) {
463ee0b2 2551 SvNVX(sv) += 1.0;
a0d0e21e 2552 (void)SvNOK_only(sv);
79072805
LW
2553 return;
2554 }
8990e307 2555 if (!(flags & SVp_POK) || !*SvPVX(sv)) {
748a9306 2556 sv_upgrade(sv, SVt_NV);
463ee0b2 2557 SvNVX(sv) = 1.0;
a0d0e21e 2558 (void)SvNOK_only(sv);
79072805
LW
2559 return;
2560 }
463ee0b2 2561 d = SvPVX(sv);
79072805
LW
2562 while (isALPHA(*d)) d++;
2563 while (isDIGIT(*d)) d++;
2564 if (*d) {
463ee0b2 2565 sv_setnv(sv,atof(SvPVX(sv)) + 1.0); /* punt */
79072805
LW
2566 return;
2567 }
2568 d--;
463ee0b2 2569 while (d >= SvPVX(sv)) {
79072805
LW
2570 if (isDIGIT(*d)) {
2571 if (++*d <= '9')
2572 return;
2573 *(d--) = '0';
2574 }
2575 else {
2576 ++*d;
2577 if (isALPHA(*d))
2578 return;
2579 *(d--) -= 'z' - 'a' + 1;
2580 }
2581 }
2582 /* oh,oh, the number grew */
2583 SvGROW(sv, SvCUR(sv) + 2);
2584 SvCUR(sv)++;
463ee0b2 2585 for (d = SvPVX(sv) + SvCUR(sv); d > SvPVX(sv); d--)
79072805
LW
2586 *d = d[-1];
2587 if (isDIGIT(d[1]))
2588 *d = '1';
2589 else
2590 *d = d[1];
2591}
2592
2593void
2594sv_dec(sv)
2595register SV *sv;
2596{
463ee0b2
LW
2597 int flags;
2598
79072805
LW
2599 if (!sv)
2600 return;
ed6116ce 2601 if (SvTHINKFIRST(sv)) {
8990e307 2602 if (SvREADONLY(sv) && curcop != &compiling)
ed6116ce 2603 croak(no_modify);
a0d0e21e
LW
2604 if (SvROK(sv)) {
2605#ifdef OVERLOAD
2606 if (SvAMAGIC(sv) && AMG_CALLun(sv,dec)) return;
2607#endif /* OVERLOAD */
2608 sv_unref(sv);
2609 }
ed6116ce 2610 }
8990e307 2611 if (SvGMAGICAL(sv))
79072805 2612 mg_get(sv);
8990e307
LW
2613 flags = SvFLAGS(sv);
2614 if (flags & SVp_IOK) {
463ee0b2 2615 --SvIVX(sv);
a0d0e21e 2616 (void)SvIOK_only(sv);
79072805
LW
2617 return;
2618 }
8990e307 2619 if (flags & SVp_NOK) {
463ee0b2 2620 SvNVX(sv) -= 1.0;
a0d0e21e 2621 (void)SvNOK_only(sv);
79072805
LW
2622 return;
2623 }
8990e307 2624 if (!(flags & SVp_POK)) {
748a9306 2625 sv_upgrade(sv, SVt_NV);
463ee0b2 2626 SvNVX(sv) = -1.0;
a0d0e21e 2627 (void)SvNOK_only(sv);
79072805
LW
2628 return;
2629 }
463ee0b2 2630 sv_setnv(sv,atof(SvPVX(sv)) - 1.0);
79072805
LW
2631}
2632
2633/* Make a string that will exist for the duration of the expression
2634 * evaluation. Actually, it may have to last longer than that, but
2635 * hopefully we won't free it until it has been assigned to a
2636 * permanent location. */
2637
8990e307
LW
2638static void
2639sv_mortalgrow()
2640{
2641 tmps_max += 128;
2642 Renew(tmps_stack, tmps_max, SV*);
2643}
2644
79072805
LW
2645SV *
2646sv_mortalcopy(oldstr)
2647SV *oldstr;
2648{
463ee0b2 2649 register SV *sv;
79072805 2650
463ee0b2 2651 new_SV();
8990e307
LW
2652 SvANY(sv) = 0;
2653 SvREFCNT(sv) = 1;
2654 SvFLAGS(sv) = 0;
79072805 2655 sv_setsv(sv,oldstr);
8990e307
LW
2656 if (++tmps_ix >= tmps_max)
2657 sv_mortalgrow();
2658 tmps_stack[tmps_ix] = sv;
2659 SvTEMP_on(sv);
2660 return sv;
2661}
2662
2663SV *
2664sv_newmortal()
2665{
2666 register SV *sv;
2667
2668 new_SV();
2669 SvANY(sv) = 0;
2670 SvREFCNT(sv) = 1;
2671 SvFLAGS(sv) = SVs_TEMP;
2672 if (++tmps_ix >= tmps_max)
2673 sv_mortalgrow();
79072805 2674 tmps_stack[tmps_ix] = sv;
79072805
LW
2675 return sv;
2676}
2677
2678/* same thing without the copying */
2679
2680SV *
2681sv_2mortal(sv)
2682register SV *sv;
2683{
2684 if (!sv)
2685 return sv;
a0d0e21e
LW
2686 if (SvREADONLY(sv) && curcop != &compiling)
2687 croak(no_modify);
8990e307
LW
2688 if (++tmps_ix >= tmps_max)
2689 sv_mortalgrow();
79072805 2690 tmps_stack[tmps_ix] = sv;
8990e307 2691 SvTEMP_on(sv);
79072805
LW
2692 return sv;
2693}
2694
2695SV *
2696newSVpv(s,len)
2697char *s;
2698STRLEN len;
2699{
463ee0b2 2700 register SV *sv;
79072805 2701
463ee0b2 2702 new_SV();
8990e307
LW
2703 SvANY(sv) = 0;
2704 SvREFCNT(sv) = 1;
2705 SvFLAGS(sv) = 0;
79072805
LW
2706 if (!len)
2707 len = strlen(s);
2708 sv_setpvn(sv,s,len);
2709 return sv;
2710}
2711
2712SV *
2713newSVnv(n)
2714double n;
2715{
463ee0b2 2716 register SV *sv;
79072805 2717
463ee0b2 2718 new_SV();
8990e307
LW
2719 SvANY(sv) = 0;
2720 SvREFCNT(sv) = 1;
2721 SvFLAGS(sv) = 0;
79072805
LW
2722 sv_setnv(sv,n);
2723 return sv;
2724}
2725
2726SV *
2727newSViv(i)
a0d0e21e 2728IV i;
79072805 2729{
463ee0b2 2730 register SV *sv;
79072805 2731
463ee0b2 2732 new_SV();
8990e307
LW
2733 SvANY(sv) = 0;
2734 SvREFCNT(sv) = 1;
2735 SvFLAGS(sv) = 0;
79072805
LW
2736 sv_setiv(sv,i);
2737 return sv;
2738}
2739
2304df62
AD
2740SV *
2741newRV(ref)
2742SV *ref;
2743{
2744 register SV *sv;
2745
2746 new_SV();
2747 SvANY(sv) = 0;
2748 SvREFCNT(sv) = 1;
2749 SvFLAGS(sv) = 0;
2750 sv_upgrade(sv, SVt_RV);
a0d0e21e 2751 SvTEMP_off(ref);
2304df62
AD
2752 SvRV(sv) = SvREFCNT_inc(ref);
2753 SvROK_on(sv);
2304df62
AD
2754 return sv;
2755}
2756
79072805
LW
2757/* make an exact duplicate of old */
2758
2759SV *
2760newSVsv(old)
2761register SV *old;
2762{
463ee0b2 2763 register SV *sv;
79072805
LW
2764
2765 if (!old)
2766 return Nullsv;
8990e307 2767 if (SvTYPE(old) == SVTYPEMASK) {
79072805
LW
2768 warn("semi-panic: attempt to dup freed string");
2769 return Nullsv;
2770 }
463ee0b2 2771 new_SV();
8990e307
LW
2772 SvANY(sv) = 0;
2773 SvREFCNT(sv) = 1;
2774 SvFLAGS(sv) = 0;
79072805
LW
2775 if (SvTEMP(old)) {
2776 SvTEMP_off(old);
463ee0b2 2777 sv_setsv(sv,old);
79072805
LW
2778 SvTEMP_on(old);
2779 }
2780 else
463ee0b2
LW
2781 sv_setsv(sv,old);
2782 return sv;
79072805
LW
2783}
2784
2785void
2786sv_reset(s,stash)
2787register char *s;
2788HV *stash;
2789{
2790 register HE *entry;
2791 register GV *gv;
2792 register SV *sv;
2793 register I32 i;
2794 register PMOP *pm;
2795 register I32 max;
463ee0b2 2796 char todo[256];
79072805
LW
2797
2798 if (!*s) { /* reset ?? searches */
2799 for (pm = HvPMROOT(stash); pm; pm = pm->op_pmnext) {
2800 pm->op_pmflags &= ~PMf_USED;
2801 }
2802 return;
2803 }
2804
2805 /* reset variables */
2806
2807 if (!HvARRAY(stash))
2808 return;
463ee0b2
LW
2809
2810 Zero(todo, 256, char);
79072805
LW
2811 while (*s) {
2812 i = *s;
2813 if (s[1] == '-') {
2814 s += 2;
2815 }
2816 max = *s++;
2817 for ( ; i <= max; i++) {
463ee0b2
LW
2818 todo[i] = 1;
2819 }
a0d0e21e 2820 for (i = 0; i <= (I32) HvMAX(stash); i++) {
79072805
LW
2821 for (entry = HvARRAY(stash)[i];
2822 entry;
2823 entry = entry->hent_next) {
463ee0b2
LW
2824 if (!todo[(U8)*entry->hent_key])
2825 continue;
79072805
LW
2826 gv = (GV*)entry->hent_val;
2827 sv = GvSV(gv);
a0d0e21e 2828 (void)SvOK_off(sv);
79072805
LW
2829 if (SvTYPE(sv) >= SVt_PV) {
2830 SvCUR_set(sv, 0);
463ee0b2
LW
2831 SvTAINT(sv);
2832 if (SvPVX(sv) != Nullch)
2833 *SvPVX(sv) = '\0';
79072805
LW
2834 }
2835 if (GvAV(gv)) {
2836 av_clear(GvAV(gv));
2837 }
2838 if (GvHV(gv)) {
a0d0e21e
LW
2839 if (HvNAME(GvHV(gv)))
2840 continue;
463ee0b2 2841 hv_clear(GvHV(gv));
a0d0e21e 2842#ifndef VMS /* VMS has no environ array */
79072805
LW
2843 if (gv == envgv)
2844 environ[0] = Nullch;
a0d0e21e 2845#endif
79072805
LW
2846 }
2847 }
2848 }
2849 }
2850}
2851
79072805
LW
2852CV *
2853sv_2cv(sv, st, gvp, lref)
2854SV *sv;
2855HV **st;
2856GV **gvp;
2857I32 lref;
2858{
2859 GV *gv;
2860 CV *cv;
2861
2862 if (!sv)
93a17b20 2863 return *gvp = Nullgv, Nullcv;
79072805 2864 switch (SvTYPE(sv)) {
79072805
LW
2865 case SVt_PVCV:
2866 *st = CvSTASH(sv);
2867 *gvp = Nullgv;
2868 return (CV*)sv;
2869 case SVt_PVHV:
2870 case SVt_PVAV:
2871 *gvp = Nullgv;
2872 return Nullcv;
8990e307
LW
2873 case SVt_PVGV:
2874 gv = (GV*)sv;
a0d0e21e 2875 *gvp = gv;
8990e307
LW
2876 *st = GvESTASH(gv);
2877 goto fix_gv;
2878
79072805 2879 default:
a0d0e21e
LW
2880 if (SvGMAGICAL(sv))
2881 mg_get(sv);
2882 if (SvROK(sv)) {
2883 cv = (CV*)SvRV(sv);
2884 if (SvTYPE(cv) != SVt_PVCV)
2885 croak("Not a subroutine reference");
2886 *gvp = Nullgv;
2887 *st = CvSTASH(cv);
2888 return cv;
2889 }
79072805
LW
2890 if (isGV(sv))
2891 gv = (GV*)sv;
2892 else
85e6fe83 2893 gv = gv_fetchpv(SvPV(sv, na), lref, SVt_PVCV);
79072805
LW
2894 *gvp = gv;
2895 if (!gv)
2896 return Nullcv;
2897 *st = GvESTASH(gv);
8990e307
LW
2898 fix_gv:
2899 if (lref && !GvCV(gv)) {
748a9306 2900 ENTER;
a0d0e21e 2901 sv = NEWSV(704,0);
8990e307 2902 gv_efullname(sv, gv);
748a9306 2903 newSUB(start_subparse(),
8990e307
LW
2904 newSVOP(OP_CONST, 0, sv),
2905 Nullop);
748a9306 2906 LEAVE;
8990e307 2907 }
79072805
LW
2908 return GvCV(gv);
2909 }
2910}
2911
2912#ifndef SvTRUE
2913I32
2914SvTRUE(sv)
2915register SV *sv;
2916{
8990e307
LW
2917 if (!sv)
2918 return 0;
2919 if (SvGMAGICAL(sv))
79072805
LW
2920 mg_get(sv);
2921 if (SvPOK(sv)) {
2922 register XPV* Xpv;
2923 if ((Xpv = (XPV*)SvANY(sv)) &&
2924 (*Xpv->xpv_pv > '0' ||
2925 Xpv->xpv_cur > 1 ||
2926 (Xpv->xpv_cur && *Xpv->xpv_pv != '0')))
2927 return 1;
2928 else
2929 return 0;
2930 }
2931 else {
2932 if (SvIOK(sv))
463ee0b2 2933 return SvIVX(sv) != 0;
79072805
LW
2934 else {
2935 if (SvNOK(sv))
463ee0b2 2936 return SvNVX(sv) != 0.0;
79072805 2937 else
463ee0b2 2938 return sv_2bool(sv);
79072805
LW
2939 }
2940 }
2941}
2942#endif /* SvTRUE */
2943
85e6fe83 2944#ifndef SvIV
a0d0e21e 2945IV SvIV(Sv)
85e6fe83
LW
2946register SV *Sv;
2947{
2948 if (SvIOK(Sv))
2949 return SvIVX(Sv);
2950 return sv_2iv(Sv);
2951}
2952#endif /* SvIV */
2953
2954
463ee0b2
LW
2955#ifndef SvNV
2956double SvNV(Sv)
79072805
LW
2957register SV *Sv;
2958{
79072805 2959 if (SvNOK(Sv))
463ee0b2 2960 return SvNVX(Sv);
79072805 2961 if (SvIOK(Sv))
463ee0b2 2962 return (double)SvIVX(Sv);
79072805
LW
2963 return sv_2nv(Sv);
2964}
463ee0b2 2965#endif /* SvNV */
79072805 2966
463ee0b2 2967#ifdef CRIPPLED_CC
79072805 2968char *
463ee0b2 2969sv_pvn(sv, lp)
79072805 2970SV *sv;
463ee0b2 2971STRLEN *lp;
79072805 2972{
85e6fe83
LW
2973 if (SvPOK(sv)) {
2974 *lp = SvCUR(sv);
a0d0e21e 2975 return SvPVX(sv);
85e6fe83 2976 }
463ee0b2 2977 return sv_2pv(sv, lp);
79072805
LW
2978}
2979#endif
2980
a0d0e21e
LW
2981char *
2982sv_pvn_force(sv, lp)
2983SV *sv;
2984STRLEN *lp;
2985{
2986 char *s;
2987
2988 if (SvREADONLY(sv) && curcop != &compiling)
2989 croak(no_modify);
2990
2991 if (SvPOK(sv)) {
2992 *lp = SvCUR(sv);
2993 }
2994 else {
748a9306 2995 if (SvTYPE(sv) > SVt_PVLV && SvTYPE(sv) != SVt_PVFM) {
8e07c86e 2996 if (SvTYPE(sv) == SVt_PVGV && SvFAKE(sv)) {
a0d0e21e 2997 sv_unglob(sv);
8e07c86e
AD
2998 s = SvPVX(sv);
2999 *lp = SvCUR(sv);
3000 }
a0d0e21e
LW
3001 else
3002 croak("Can't coerce %s to string in %s", sv_reftype(sv,0),
3003 op_name[op->op_type]);
3004 }
8e07c86e
AD
3005 else
3006 s = sv_2pv(sv, lp);
a0d0e21e
LW
3007 if (s != SvPVX(sv)) { /* Almost, but not quite, sv_setpvn() */
3008 STRLEN len = *lp;
3009
3010 if (SvROK(sv))
3011 sv_unref(sv);
3012 (void)SvUPGRADE(sv, SVt_PV); /* Never FALSE */
3013 SvGROW(sv, len + 1);
3014 Move(s,SvPVX(sv),len,char);
3015 SvCUR_set(sv, len);
3016 *SvEND(sv) = '\0';
3017 }
3018 if (!SvPOK(sv)) {
3019 SvPOK_on(sv); /* validate pointer */
3020 SvTAINT(sv);
3021 DEBUG_c(fprintf(stderr,"0x%lx 2pv(%s)\n",
3022 (unsigned long)sv,SvPVX(sv)));
3023 }
3024 }
3025 return SvPVX(sv);
3026}
3027
3028char *
3029sv_reftype(sv, ob)
3030SV* sv;
3031int ob;
3032{
3033 if (ob && SvOBJECT(sv))
3034 return HvNAME(SvSTASH(sv));
3035 else {
3036 switch (SvTYPE(sv)) {
3037 case SVt_NULL:
3038 case SVt_IV:
3039 case SVt_NV:
3040 case SVt_RV:
3041 case SVt_PV:
3042 case SVt_PVIV:
3043 case SVt_PVNV:
3044 case SVt_PVMG:
3045 case SVt_PVBM:
3046 if (SvROK(sv))
3047 return "REF";
3048 else
3049 return "SCALAR";
3050 case SVt_PVLV: return "LVALUE";
3051 case SVt_PVAV: return "ARRAY";
3052 case SVt_PVHV: return "HASH";
3053 case SVt_PVCV: return "CODE";
3054 case SVt_PVGV: return "GLOB";
3055 case SVt_PVFM: return "FORMLINE";
3056 default: return "UNKNOWN";
3057 }
3058 }
3059}
3060
463ee0b2 3061int
85e6fe83
LW
3062sv_isobject(sv)
3063SV *sv;
3064{
3065 if (!SvROK(sv))
3066 return 0;
3067 sv = (SV*)SvRV(sv);
3068 if (!SvOBJECT(sv))
3069 return 0;
3070 return 1;
3071}
3072
3073int
463ee0b2
LW
3074sv_isa(sv, name)
3075SV *sv;
3076char *name;
3077{
ed6116ce 3078 if (!SvROK(sv))
463ee0b2 3079 return 0;
ed6116ce
LW
3080 sv = (SV*)SvRV(sv);
3081 if (!SvOBJECT(sv))
463ee0b2
LW
3082 return 0;
3083
3084 return strEQ(HvNAME(SvSTASH(sv)), name);
3085}
3086
3087SV*
a0d0e21e 3088newSVrv(rv, classname)
463ee0b2 3089SV *rv;
a0d0e21e 3090char *classname;
463ee0b2 3091{
463ee0b2
LW
3092 SV *sv;
3093
463ee0b2 3094 new_SV();
8990e307 3095 SvANY(sv) = 0;
a0d0e21e 3096 SvREFCNT(sv) = 0;
8990e307 3097 SvFLAGS(sv) = 0;
ed6116ce 3098 sv_upgrade(rv, SVt_RV);
8990e307 3099 SvRV(rv) = SvREFCNT_inc(sv);
ed6116ce 3100 SvROK_on(rv);
463ee0b2 3101
a0d0e21e
LW
3102 if (classname) {
3103 HV* stash = gv_stashpv(classname, TRUE);
3104 (void)sv_bless(rv, stash);
3105 }
3106 return sv;
3107}
3108
3109SV*
3110sv_setref_pv(rv, classname, pv)
3111SV *rv;
3112char *classname;
3113void* pv;
3114{
3115 if (!pv)
3116 sv_setsv(rv, &sv_undef);
3117 else
3118 sv_setiv(newSVrv(rv,classname), (IV)pv);
3119 return rv;
3120}
3121
3122SV*
3123sv_setref_iv(rv, classname, iv)
3124SV *rv;
3125char *classname;
3126IV iv;
3127{
3128 sv_setiv(newSVrv(rv,classname), iv);
3129 return rv;
3130}
3131
3132SV*
3133sv_setref_nv(rv, classname, nv)
3134SV *rv;
3135char *classname;
3136double nv;
3137{
3138 sv_setnv(newSVrv(rv,classname), nv);
3139 return rv;
3140}
463ee0b2 3141
a0d0e21e
LW
3142SV*
3143sv_setref_pvn(rv, classname, pv, n)
3144SV *rv;
3145char *classname;
3146char* pv;
3147I32 n;
3148{
3149 sv_setpvn(newSVrv(rv,classname), pv, n);
463ee0b2
LW
3150 return rv;
3151}
3152
a0d0e21e
LW
3153SV*
3154sv_bless(sv,stash)
3155SV* sv;
3156HV* stash;
3157{
3158 SV *ref;
3159 if (!SvROK(sv))
3160 croak("Can't bless non-reference value");
3161 ref = SvRV(sv);
3162 if (SvFLAGS(ref) & (SVs_OBJECT|SVf_READONLY)) {
3163 if (SvREADONLY(ref))
3164 croak(no_modify);
3165 if (SvOBJECT(ref) && SvTYPE(ref) != SVt_PVIO)
3166 --sv_objcount;
3167 }
3168 SvOBJECT_on(ref);
3169 ++sv_objcount;
3170 (void)SvUPGRADE(ref, SVt_PVMG);
3171 SvSTASH(ref) = (HV*)SvREFCNT_inc(stash);
3172
3173#ifdef OVERLOAD
748a9306 3174 SvAMAGIC_off(sv);
a0d0e21e
LW
3175 if (Gv_AMG(stash)) {
3176 SvAMAGIC_on(sv);
3177 }
3178#endif /* OVERLOAD */
3179
3180 return sv;
3181}
3182
3183static void
3184sv_unglob(sv)
3185SV* sv;
3186{
3187 assert(SvTYPE(sv) == SVt_PVGV);
3188 SvFAKE_off(sv);
3189 if (GvGP(sv))
3190 gp_free(sv);
3191 sv_unmagic(sv, '*');
3192 Safefree(GvNAME(sv));
8e07c86e 3193 SvMULTI_off(sv);
a0d0e21e
LW
3194 SvFLAGS(sv) &= ~SVTYPEMASK;
3195 SvFLAGS(sv) |= SVt_PVMG;
3196}
3197
ed6116ce
LW
3198void
3199sv_unref(sv)
3200SV* sv;
3201{
a0d0e21e
LW
3202 SV* rv = SvRV(sv);
3203
ed6116ce
LW
3204 SvRV(sv) = 0;
3205 SvROK_off(sv);
8e07c86e
AD
3206 if (SvREFCNT(rv) == 1)
3207 sv_2mortal(rv);
3208 else
3209 SvREFCNT_dec(rv);
ed6116ce 3210}
8990e307
LW
3211
3212#ifdef DEBUGGING
3213void
3214sv_dump(sv)
3215SV* sv;
3216{
3217 char tmpbuf[1024];
3218 char *d = tmpbuf;
3219 U32 flags;
3220 U32 type;
3221
3222 if (!sv) {
3223 fprintf(stderr, "SV = 0\n");
3224 return;
3225 }
3226
3227 flags = SvFLAGS(sv);
3228 type = SvTYPE(sv);
3229
3230 sprintf(d, "(0x%lx)\n REFCNT = %ld\n FLAGS = (",
3231 (unsigned long)SvANY(sv), (long)SvREFCNT(sv));
3232 d += strlen(d);
3233 if (flags & SVs_PADBUSY) strcat(d, "PADBUSY,");
3234 if (flags & SVs_PADTMP) strcat(d, "PADTMP,");
3235 if (flags & SVs_PADMY) strcat(d, "PADMY,");
3236 if (flags & SVs_TEMP) strcat(d, "TEMP,");
3237 if (flags & SVs_OBJECT) strcat(d, "OBJECT,");
3238 if (flags & SVs_GMG) strcat(d, "GMG,");
3239 if (flags & SVs_SMG) strcat(d, "SMG,");
3240 if (flags & SVs_RMG) strcat(d, "RMG,");
3241 d += strlen(d);
3242
3243 if (flags & SVf_IOK) strcat(d, "IOK,");
3244 if (flags & SVf_NOK) strcat(d, "NOK,");
3245 if (flags & SVf_POK) strcat(d, "POK,");
3246 if (flags & SVf_ROK) strcat(d, "ROK,");
8990e307 3247 if (flags & SVf_OOK) strcat(d, "OOK,");
a0d0e21e 3248 if (flags & SVf_FAKE) strcat(d, "FAKE,");
8990e307
LW
3249 if (flags & SVf_READONLY) strcat(d, "READONLY,");
3250 d += strlen(d);
3251
3252 if (flags & SVp_IOK) strcat(d, "pIOK,");
3253 if (flags & SVp_NOK) strcat(d, "pNOK,");
3254 if (flags & SVp_POK) strcat(d, "pPOK,");
3255 if (flags & SVp_SCREAM) strcat(d, "SCREAM,");
3256 d += strlen(d);
3257 if (d[-1] == ',')
3258 d--;
3259 *d++ = ')';
3260 *d = '\0';
3261
3262 fprintf(stderr, "SV = ");
3263 switch (type) {
3264 case SVt_NULL:
3265 fprintf(stderr,"NULL%s\n", tmpbuf);
3266 return;
3267 case SVt_IV:
3268 fprintf(stderr,"IV%s\n", tmpbuf);
3269 break;
3270 case SVt_NV:
3271 fprintf(stderr,"NV%s\n", tmpbuf);
3272 break;
3273 case SVt_RV:
3274 fprintf(stderr,"RV%s\n", tmpbuf);
3275 break;
3276 case SVt_PV:
3277 fprintf(stderr,"PV%s\n", tmpbuf);
3278 break;
3279 case SVt_PVIV:
3280 fprintf(stderr,"PVIV%s\n", tmpbuf);
3281 break;
3282 case SVt_PVNV:
3283 fprintf(stderr,"PVNV%s\n", tmpbuf);
3284 break;
3285 case SVt_PVBM:
3286 fprintf(stderr,"PVBM%s\n", tmpbuf);
3287 break;
3288 case SVt_PVMG:
3289 fprintf(stderr,"PVMG%s\n", tmpbuf);
3290 break;
3291 case SVt_PVLV:
3292 fprintf(stderr,"PVLV%s\n", tmpbuf);
3293 break;
3294 case SVt_PVAV:
3295 fprintf(stderr,"PVAV%s\n", tmpbuf);
3296 break;
3297 case SVt_PVHV:
3298 fprintf(stderr,"PVHV%s\n", tmpbuf);
3299 break;
3300 case SVt_PVCV:
3301 fprintf(stderr,"PVCV%s\n", tmpbuf);
3302 break;
3303 case SVt_PVGV:
3304 fprintf(stderr,"PVGV%s\n", tmpbuf);
3305 break;
3306 case SVt_PVFM:
3307 fprintf(stderr,"PVFM%s\n", tmpbuf);
3308 break;
3309 case SVt_PVIO:
3310 fprintf(stderr,"PVIO%s\n", tmpbuf);
3311 break;
3312 default:
3313 fprintf(stderr,"UNKNOWN%s\n", tmpbuf);
3314 return;
3315 }
3316 if (type >= SVt_PVIV || type == SVt_IV)
3317 fprintf(stderr, " IV = %ld\n", (long)SvIVX(sv));
3318 if (type >= SVt_PVNV || type == SVt_NV)
a0d0e21e 3319 fprintf(stderr, " NV = %.*g\n", DBL_DIG, SvNVX(sv));
8990e307 3320 if (SvROK(sv)) {
a0d0e21e 3321 fprintf(stderr, " RV = 0x%lx\n", (long)SvRV(sv));
8990e307
LW
3322 sv_dump(SvRV(sv));
3323 return;
3324 }
3325 if (type < SVt_PV)
3326 return;
3327 if (type <= SVt_PVLV) {
3328 if (SvPVX(sv))
3329 fprintf(stderr, " PV = 0x%lx \"%s\"\n CUR = %ld\n LEN = %ld\n",
a0d0e21e 3330 (long)SvPVX(sv), SvPVX(sv), (long)SvCUR(sv), (long)SvLEN(sv));
8990e307
LW
3331 else
3332 fprintf(stderr, " PV = 0\n");
3333 }
3334 if (type >= SVt_PVMG) {
3335 if (SvMAGIC(sv)) {
a0d0e21e 3336 fprintf(stderr, " MAGIC = 0x%lx\n", (long)SvMAGIC(sv));
8990e307
LW
3337 }
3338 if (SvSTASH(sv))
3339 fprintf(stderr, " STASH = %s\n", HvNAME(SvSTASH(sv)));
3340 }
3341 switch (type) {
3342 case SVt_PVLV:
3343 fprintf(stderr, " TYPE = %c\n", LvTYPE(sv));
3344 fprintf(stderr, " TARGOFF = %ld\n", (long)LvTARGOFF(sv));
3345 fprintf(stderr, " TARGLEN = %ld\n", (long)LvTARGLEN(sv));
a0d0e21e 3346 fprintf(stderr, " TARG = 0x%lx\n", (long)LvTARG(sv));
8990e307
LW
3347 sv_dump(LvTARG(sv));
3348 break;
3349 case SVt_PVAV:
a0d0e21e
LW
3350 fprintf(stderr, " ARRAY = 0x%lx\n", (long)AvARRAY(sv));
3351 fprintf(stderr, " ALLOC = 0x%lx\n", (long)AvALLOC(sv));
8990e307
LW
3352 fprintf(stderr, " FILL = %ld\n", (long)AvFILL(sv));
3353 fprintf(stderr, " MAX = %ld\n", (long)AvMAX(sv));
a0d0e21e 3354 fprintf(stderr, " ARYLEN = 0x%lx\n", (long)AvARYLEN(sv));
8990e307
LW
3355 if (AvREAL(sv))
3356 fprintf(stderr, " FLAGS = (REAL)\n");
3357 else
3358 fprintf(stderr, " FLAGS = ()\n");
3359 break;
3360 case SVt_PVHV:
a0d0e21e 3361 fprintf(stderr, " ARRAY = 0x%lx\n",(long)HvARRAY(sv));
8990e307
LW
3362 fprintf(stderr, " KEYS = %ld\n", (long)HvKEYS(sv));
3363 fprintf(stderr, " FILL = %ld\n", (long)HvFILL(sv));
3364 fprintf(stderr, " MAX = %ld\n", (long)HvMAX(sv));
3365 fprintf(stderr, " RITER = %ld\n", (long)HvRITER(sv));
a0d0e21e 3366 fprintf(stderr, " EITER = 0x%lx\n",(long) HvEITER(sv));
8990e307 3367 if (HvPMROOT(sv))
a0d0e21e 3368 fprintf(stderr, " PMROOT = 0x%lx\n",(long)HvPMROOT(sv));
8990e307
LW
3369 if (HvNAME(sv))
3370 fprintf(stderr, " NAME = \"%s\"\n", HvNAME(sv));
3371 break;
3372 case SVt_PVFM:
3373 case SVt_PVCV:
a0d0e21e
LW
3374 fprintf(stderr, " STASH = 0x%lx\n", (long)CvSTASH(sv));
3375 fprintf(stderr, " START = 0x%lx\n", (long)CvSTART(sv));
3376 fprintf(stderr, " ROOT = 0x%lx\n", (long)CvROOT(sv));
3377 fprintf(stderr, " XSUB = 0x%lx\n", (long)CvXSUB(sv));
3378 fprintf(stderr, " XSUBANY = %ld\n", (long)CvXSUBANY(sv).any_i32);
3379 fprintf(stderr, " FILEGV = 0x%lx\n", (long)CvFILEGV(sv));
8990e307 3380 fprintf(stderr, " DEPTH = %ld\n", (long)CvDEPTH(sv));
a0d0e21e 3381 fprintf(stderr, " PADLIST = 0x%lx\n", (long)CvPADLIST(sv));
e9a444f0 3382 fprintf(stderr, " OUTSIDE = 0x%lx\n", (long)CvOUTSIDE(sv));
8990e307
LW
3383 if (type == SVt_PVFM)
3384 fprintf(stderr, " LINES = %ld\n", (long)FmLINES(sv));
3385 break;
3386 case SVt_PVGV:
3387 fprintf(stderr, " NAME = %s\n", GvNAME(sv));
3388 fprintf(stderr, " NAMELEN = %ld\n", (long)GvNAMELEN(sv));
a0d0e21e
LW
3389 fprintf(stderr, " STASH = 0x%lx\n", (long)GvSTASH(sv));
3390 fprintf(stderr, " GP = 0x%lx\n", (long)GvGP(sv));
3391 fprintf(stderr, " SV = 0x%lx\n", (long)GvSV(sv));
8990e307 3392 fprintf(stderr, " REFCNT = %ld\n", (long)GvREFCNT(sv));
a0d0e21e
LW
3393 fprintf(stderr, " IO = 0x%lx\n", (long)GvIOp(sv));
3394 fprintf(stderr, " FORM = 0x%lx\n", (long)GvFORM(sv));
3395 fprintf(stderr, " AV = 0x%lx\n", (long)GvAV(sv));
3396 fprintf(stderr, " HV = 0x%lx\n", (long)GvHV(sv));
3397 fprintf(stderr, " CV = 0x%lx\n", (long)GvCV(sv));
3398 fprintf(stderr, " CVGEN = 0x%lx\n", (long)GvCVGEN(sv));
8990e307
LW
3399 fprintf(stderr, " LASTEXPR = %ld\n", (long)GvLASTEXPR(sv));
3400 fprintf(stderr, " LINE = %ld\n", (long)GvLINE(sv));
3401 fprintf(stderr, " FLAGS = 0x%x\n", (int)GvFLAGS(sv));
a0d0e21e
LW
3402 fprintf(stderr, " STASH = 0x%lx\n", (long)GvSTASH(sv));
3403 fprintf(stderr, " EGV = 0x%lx\n", (long)GvEGV(sv));
8990e307
LW
3404 break;
3405 case SVt_PVIO:
a0d0e21e
LW
3406 fprintf(stderr, " IFP = 0x%lx\n", (long)IoIFP(sv));
3407 fprintf(stderr, " OFP = 0x%lx\n", (long)IoOFP(sv));
3408 fprintf(stderr, " DIRP = 0x%lx\n", (long)IoDIRP(sv));
8990e307
LW
3409 fprintf(stderr, " LINES = %ld\n", (long)IoLINES(sv));
3410 fprintf(stderr, " PAGE = %ld\n", (long)IoPAGE(sv));
3411 fprintf(stderr, " PAGE_LEN = %ld\n", (long)IoPAGE_LEN(sv));
3412 fprintf(stderr, " LINES_LEFT = %ld\n", (long)IoLINES_LEFT(sv));
3413 fprintf(stderr, " TOP_NAME = %s\n", IoTOP_NAME(sv));
a0d0e21e 3414 fprintf(stderr, " TOP_GV = 0x%lx\n", (long)IoTOP_GV(sv));
8990e307 3415 fprintf(stderr, " FMT_NAME = %s\n", IoFMT_NAME(sv));
a0d0e21e 3416 fprintf(stderr, " FMT_GV = 0x%lx\n", (long)IoFMT_GV(sv));
8990e307 3417 fprintf(stderr, " BOTTOM_NAME = %s\n", IoBOTTOM_NAME(sv));
a0d0e21e 3418 fprintf(stderr, " BOTTOM_GV = 0x%lx\n", (long)IoBOTTOM_GV(sv));
8990e307
LW
3419 fprintf(stderr, " SUBPROCESS = %ld\n", (long)IoSUBPROCESS(sv));
3420 fprintf(stderr, " TYPE = %c\n", IoTYPE(sv));
a0d0e21e 3421 fprintf(stderr, " FLAGS = 0x%lx\n", (long)IoFLAGS(sv));
8990e307
LW
3422 break;
3423 }
3424}
2304df62
AD
3425#else
3426void
3427sv_dump(sv)
3428SV* sv;
3429{
3430}
8990e307 3431#endif
a0d0e21e
LW
3432
3433IO*
3434sv_2io(sv)
3435SV *sv;
3436{
3437 IO* io;
3438 GV* gv;
3439
3440 switch (SvTYPE(sv)) {
3441 case SVt_PVIO:
3442 io = (IO*)sv;
3443 break;
3444 case SVt_PVGV:
3445 gv = (GV*)sv;
3446 io = GvIO(gv);
3447 if (!io)
3448 croak("Bad filehandle: %s", GvNAME(gv));
3449 break;
3450 default:
3451 if (!SvOK(sv))
3452 croak(no_usym, "filehandle");
3453 if (SvROK(sv))
3454 return sv_2io(SvRV(sv));
3455 gv = gv_fetchpv(SvPV(sv,na), FALSE, SVt_PVIO);
3456 if (gv)
3457 io = GvIO(gv);
3458 else
3459 io = 0;
3460 if (!io)
3461 croak("Bad filehandle: %s", SvPV(sv,na));
3462 break;
3463 }
3464 return io;
3465}
3466