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