3 * Copyright (c) 1991-1997, Larry Wall
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.
11 * "It's a big house this, and very peculiar. Always a bit more to discover,
12 * and no knowing what you'll find around a corner. And Elves, sir!" --Samwise
19 * The compiler on Concurrent CX/UX systems has a subtle bug which only
20 * seems to show up when compiling pp.c - it generates the wrong double
21 * precision constant value for (double)UV_MAX when used inline in the body
22 * of the code below, so this makes a static variable up front (which the
23 * compiler seems to get correct) and uses it in place of UV_MAX below.
25 #ifdef CXUX_BROKEN_CONSTANT_CONVERT
26 static double UV_MAX_cxux = ((double)UV_MAX);
30 * Types used in bitwise operations.
32 * Normally we'd just use IV and UV. However, some hardware and
33 * software combinations (e.g. Alpha and current OSF/1) don't have a
34 * floating-point type to use for NV that has adequate bits to fully
35 * hold an IV/UV. (In other words, sizeof(long) == sizeof(double).)
37 * It just so happens that "int" is the right size almost everywhere.
43 * Mask used after bitwise operations.
45 * There is at least one realm (Cray word machines) that doesn't
46 * have an integral type (except char) small enough to be represented
47 * in a double without loss; that is, it has no 32-bit type.
49 #if BYTEORDER > 0xFFFF && defined(_CRAY) && !defined(_CRAYMPP)
51 # define BW_MASK ((1 << BW_BITS) - 1)
52 # define BW_SIGN (1 << (BW_BITS - 1))
53 # define BWi(i) (((i) & BW_SIGN) ? ((i) | ~BW_MASK) : ((i) & BW_MASK))
54 # define BWu(u) ((u) & BW_MASK)
61 * Offset for integer pack/unpack.
63 * On architectures where I16 and I32 aren't really 16 and 32 bits,
64 * which for now are all Crays, pack and unpack have to play games.
68 * These values are required for portability of pack() output.
69 * If they're not right on your machine, then pack() and unpack()
70 * wouldn't work right anyway; you'll need to apply the Cray hack.
71 * (I'd like to check them with #if, but you can't use sizeof() in
77 #if BYTEORDER > 0xFFFF && defined(_CRAY) && !defined(_CRAYMPP)
78 # if BYTEORDER == 0x12345678
79 # define OFF16(p) (char*)(p)
80 # define OFF32(p) (char*)(p)
82 # if BYTEORDER == 0x87654321
83 # define OFF16(p) ((char*)(p) + (sizeof(U16) - SIZE16))
84 # define OFF32(p) ((char*)(p) + (sizeof(U32) - SIZE32))
86 }}}} bad cray byte order
89 # define COPY16(s,p) (*(p) = 0, Copy(s, OFF16(p), SIZE16, char))
90 # define COPY32(s,p) (*(p) = 0, Copy(s, OFF32(p), SIZE32, char))
91 # define CAT16(sv,p) sv_catpvn(sv, OFF16(p), SIZE16)
92 # define CAT32(sv,p) sv_catpvn(sv, OFF32(p), SIZE32)
94 # define COPY16(s,p) Copy(s, p, SIZE16, char)
95 # define COPY32(s,p) Copy(s, p, SIZE32, char)
96 # define CAT16(sv,p) sv_catpvn(sv, (char*)(p), SIZE16)
97 # define CAT32(sv,p) sv_catpvn(sv, (char*)(p), SIZE32)
100 static void doencodes _((SV* sv, char* s, I32 len));
101 static SV* refto _((SV* sv));
102 static U32 seed _((void));
104 static bool srand_called = FALSE;
106 /* variations on pp_null */
111 if (GIMME_V == G_SCALAR)
126 if (op->op_private & OPpLVAL_INTRO)
127 SAVECLEARSV(curpad[op->op_targ]);
129 if (op->op_flags & OPf_REF) {
133 if (GIMME == G_ARRAY) {
134 I32 maxarg = AvFILL((AV*)TARG) + 1;
136 Copy(AvARRAY((AV*)TARG), SP+1, maxarg, SV*);
140 SV* sv = sv_newmortal();
141 I32 maxarg = AvFILL((AV*)TARG) + 1;
142 sv_setiv(sv, maxarg);
154 if (op->op_private & OPpLVAL_INTRO)
155 SAVECLEARSV(curpad[op->op_targ]);
156 if (op->op_flags & OPf_REF)
159 if (gimme == G_ARRAY) {
160 RETURNOP(do_kv(ARGS));
162 else if (gimme == G_SCALAR) {
163 SV* sv = sv_newmortal();
164 if (HvFILL((HV*)TARG))
165 sv_setpvf(sv, "%ld/%ld",
166 (long)HvFILL((HV*)TARG), (long)HvMAX((HV*)TARG) + 1);
176 DIE("NOT IMPL LINE %d",__LINE__);
188 if (SvTYPE(sv) == SVt_PVIO) {
189 GV *gv = (GV*) sv_newmortal();
190 gv_init(gv, 0, "", 0, 0);
191 GvIOp(gv) = (IO *)sv;
192 (void)SvREFCNT_inc(sv);
194 } else if (SvTYPE(sv) != SVt_PVGV)
195 DIE("Not a GLOB reference");
198 if (SvTYPE(sv) != SVt_PVGV) {
201 if (SvGMAGICAL(sv)) {
207 if (op->op_flags & OPf_REF ||
208 op->op_private & HINT_STRICT_REFS)
209 DIE(no_usym, "a symbol");
215 if (op->op_private & HINT_STRICT_REFS)
216 DIE(no_symref, sym, "a symbol");
217 sv = (SV*)gv_fetchpv(sym, TRUE, SVt_PVGV);
220 if (op->op_private & OPpLVAL_INTRO)
221 save_gp((GV*)sv, !(op->op_flags & OPf_SPECIAL));
233 switch (SvTYPE(sv)) {
237 DIE("Not a SCALAR reference");
244 if (SvTYPE(gv) != SVt_PVGV) {
245 if (SvGMAGICAL(sv)) {
251 if (op->op_flags & OPf_REF ||
252 op->op_private & HINT_STRICT_REFS)
253 DIE(no_usym, "a SCALAR");
259 if (op->op_private & HINT_STRICT_REFS)
260 DIE(no_symref, sym, "a SCALAR");
261 gv = (GV*)gv_fetchpv(sym, TRUE, SVt_PV);
265 if (op->op_flags & OPf_MOD) {
266 if (op->op_private & OPpLVAL_INTRO)
267 sv = save_scalar((GV*)TOPs);
268 else if (op->op_private & OPpDEREF)
269 vivify_ref(sv, op->op_private & OPpDEREF);
279 SV *sv = AvARYLEN(av);
281 AvARYLEN(av) = sv = NEWSV(0,0);
282 sv_upgrade(sv, SVt_IV);
283 sv_magic(sv, (SV*)av, '#', Nullch, 0);
291 dSP; dTARGET; dPOPss;
293 if (op->op_flags & OPf_MOD) {
294 if (SvTYPE(TARG) < SVt_PVLV) {
295 sv_upgrade(TARG, SVt_PVLV);
296 sv_magic(TARG, Nullsv, '.', Nullch, 0);
301 PUSHs(TARG); /* no SvSETMAGIC */
307 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
308 mg = mg_find(sv, 'g');
309 if (mg && mg->mg_len >= 0) {
310 PUSHi(mg->mg_len + curcop->cop_arybase);
324 /* We usually try to add a non-existent subroutine in case of AUTOLOAD. */
325 /* (But not in defined().) */
326 CV *cv = sv_2cv(TOPs, &stash, &gv, !(op->op_flags & OPf_SPECIAL));
329 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
346 cv = sv_2cv(TOPs, &stash, &gv, FALSE);
348 ret = sv_2mortal(newSVpv(SvPVX(cv), SvCUR(cv)));
356 CV* cv = (CV*)curpad[op->op_targ];
358 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
374 if (GIMME != G_ARRAY) {
378 EXTEND_MORTAL(SP - MARK);
380 *MARK = refto(*MARK);
390 if (SvTYPE(sv) == SVt_PVLV && LvTYPE(sv) == 'y') {
393 if (!(sv = LvTARG(sv)))
396 else if (SvPADTMP(sv))
399 dTHR; /* just for SvREFCNT_inc */
401 (void)SvREFCNT_inc(sv);
404 sv_upgrade(rv, SVt_RV);
418 if (sv && SvGMAGICAL(sv))
421 if (!sv || !SvROK(sv))
425 pv = sv_reftype(sv,TRUE);
426 PUSHp(pv, strlen(pv));
436 stash = curcop->cop_stash;
438 stash = gv_stashsv(POPs, TRUE);
440 (void)sv_bless(TOPs, stash);
457 switch (elem ? *elem : '\0')
460 if (strEQ(elem, "ARRAY"))
464 if (strEQ(elem, "CODE"))
465 ref = (SV*)GvCVu(gv);
468 if (strEQ(elem, "FILEHANDLE")) /* XXX deprecate in 5.005 */
469 ref = (SV*)GvIOp(gv);
472 if (strEQ(elem, "GLOB"))
476 if (strEQ(elem, "HASH"))
480 if (strEQ(elem, "IO"))
481 ref = (SV*)GvIOp(gv);
484 if (strEQ(elem, "NAME"))
485 sv = newSVpv(GvNAME(gv), GvNAMELEN(gv));
488 if (strEQ(elem, "PACKAGE"))
489 sv = newSVpv(HvNAME(GvSTASH(gv)), 0);
492 if (strEQ(elem, "SCALAR"))
506 /* Pattern matching */
511 register unsigned char *s;
514 register I32 *sfirst;
518 if (sv == lastscream) {
524 SvSCREAM_off(lastscream);
525 SvREFCNT_dec(lastscream);
527 lastscream = SvREFCNT_inc(sv);
530 s = (unsigned char*)(SvPV(sv, len));
534 if (pos > maxscream) {
536 maxscream = pos + 80;
537 New(301, screamfirst, 256, I32);
538 New(302, screamnext, maxscream, I32);
541 maxscream = pos + pos / 4;
542 Renew(screamnext, maxscream, I32);
546 sfirst = screamfirst;
549 if (!sfirst || !snext)
550 DIE("do_study: out of memory");
552 for (ch = 256; ch; --ch)
559 snext[pos] = sfirst[ch] - pos;
566 sv_magic(sv, Nullsv, 'g', Nullch, 0); /* piggyback on m//g magic */
575 if (op->op_flags & OPf_STACKED)
581 TARG = sv_newmortal();
582 PUSHi(do_trans(sv, op));
586 /* Lvalue operators. */
608 SETi(do_chomp(TOPs));
615 register I32 count = 0;
618 count += do_chomp(POPs);
629 if (!sv || !SvANY(sv))
631 switch (SvTYPE(sv)) {
633 if (AvMAX(sv) >= 0 || SvGMAGICAL(sv))
637 if (HvARRAY(sv) || SvGMAGICAL(sv))
641 if (CvROOT(sv) || CvXSUB(sv))
658 if (!op->op_private) {
667 if (SvTHINKFIRST(sv)) {
674 switch (SvTYPE(sv)) {
684 if (cv_const_sv((CV*)sv))
685 warn("Constant subroutine %s undefined",
686 CvANON((CV*)sv) ? "(anonymous)" : GvENAME(CvGV((CV*)sv)));
689 { GV* gv = (GV*)SvREFCNT_inc(CvGV((CV*)sv));
691 CvGV((CV*)sv) = gv; } /* let user-undef'd sub keep its identity */
695 sv_setsv(sv, &sv_undef);
698 if (SvTYPE(sv) >= SVt_PV && SvPVX(sv) && SvLEN(sv)) {
701 SvPV_set(sv, Nullch);
714 if (SvREADONLY(TOPs) || SvTYPE(TOPs) > SVt_PVLV)
716 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) &&
717 SvIVX(TOPs) != IV_MIN)
720 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
731 if (SvREADONLY(TOPs) || SvTYPE(TOPs) > SVt_PVLV)
733 sv_setsv(TARG, TOPs);
734 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) &&
735 SvIVX(TOPs) != IV_MAX)
738 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
752 if(SvREADONLY(TOPs) || SvTYPE(TOPs) > SVt_PVLV)
754 sv_setsv(TARG, TOPs);
755 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) &&
756 SvIVX(TOPs) != IV_MIN)
759 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
768 /* Ordinary operators. */
772 dSP; dATARGET; tryAMAGICbin(pow,opASSIGN);
775 SETn( pow( left, right) );
782 dSP; dATARGET; tryAMAGICbin(mult,opASSIGN);
785 SETn( left * right );
792 dSP; dATARGET; tryAMAGICbin(div,opASSIGN);
797 DIE("Illegal division by zero");
799 /* insure that 20./5. == 4. */
802 if ((double)I_V(left) == left &&
803 (double)I_V(right) == right &&
804 (k = I_V(left)/I_V(right))*I_V(right) == I_V(left)) {
807 value = left / right;
811 value = left / right;
820 dSP; dATARGET; tryAMAGICbin(mod,opASSIGN);
828 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)) {
830 right = (right_neg = (i < 0)) ? -i : i;
834 right = U_V((right_neg = (n < 0)) ? -n : n);
837 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)) {
839 left = (left_neg = (i < 0)) ? -i : i;
843 left = U_V((left_neg = (n < 0)) ? -n : n);
847 DIE("Illegal modulus zero");
850 if ((left_neg != right_neg) && ans)
853 /* XXX may warn: unary minus operator applied to unsigned type */
854 /* could change -foo to be (~foo)+1 instead */
855 if (ans <= -(UV)IV_MAX)
856 sv_setiv(TARG, (IV) -ans);
858 sv_setnv(TARG, -(double)ans);
869 dSP; dATARGET; tryAMAGICbin(repeat,opASSIGN);
871 register I32 count = POPi;
872 if (GIMME == G_ARRAY && op->op_private & OPpREPEAT_DOLIST) {
874 I32 items = SP - MARK;
886 repeatcpy((char*)(MARK + items), (char*)MARK,
887 items * sizeof(SV*), count - 1);
893 else { /* Note: mark already snarfed by pp_list */
898 if (TARG == tmpstr && SvTHINKFIRST(tmpstr)) {
899 if (SvREADONLY(tmpstr) && curcop != &compiling)
900 DIE("Can't x= to readonly value");
904 SvSetSV(TARG, tmpstr);
905 SvPV_force(TARG, len);
910 SvGROW(TARG, (count * len) + 1);
911 repeatcpy(SvPVX(TARG) + len, SvPVX(TARG), len, count - 1);
912 SvCUR(TARG) *= count;
916 (void)SvPOK_only(TARG);
925 dSP; dATARGET; tryAMAGICbin(subtr,opASSIGN);
928 SETn( left - right );
935 dSP; dATARGET; tryAMAGICbin(lshift,opASSIGN);
938 if (op->op_private & HINT_INTEGER) {
954 dSP; dATARGET; tryAMAGICbin(rshift,opASSIGN);
957 if (op->op_private & HINT_INTEGER) {
973 dSP; tryAMAGICbinSET(lt,0);
976 SETs(boolSV(TOPn < value));
983 dSP; tryAMAGICbinSET(gt,0);
986 SETs(boolSV(TOPn > value));
993 dSP; tryAMAGICbinSET(le,0);
996 SETs(boolSV(TOPn <= value));
1003 dSP; tryAMAGICbinSET(ge,0);
1006 SETs(boolSV(TOPn >= value));
1013 dSP; tryAMAGICbinSET(ne,0);
1016 SETs(boolSV(TOPn != value));
1023 dSP; dTARGET; tryAMAGICbin(ncmp,0);
1030 else if (left < right)
1032 else if (left > right)
1045 dSP; tryAMAGICbinSET(slt,0);
1048 int cmp = ((op->op_private & OPpLOCALE)
1049 ? sv_cmp_locale(left, right)
1050 : sv_cmp(left, right));
1051 SETs(boolSV(cmp < 0));
1058 dSP; tryAMAGICbinSET(sgt,0);
1061 int cmp = ((op->op_private & OPpLOCALE)
1062 ? sv_cmp_locale(left, right)
1063 : sv_cmp(left, right));
1064 SETs(boolSV(cmp > 0));
1071 dSP; tryAMAGICbinSET(sle,0);
1074 int cmp = ((op->op_private & OPpLOCALE)
1075 ? sv_cmp_locale(left, right)
1076 : sv_cmp(left, right));
1077 SETs(boolSV(cmp <= 0));
1084 dSP; tryAMAGICbinSET(sge,0);
1087 int cmp = ((op->op_private & OPpLOCALE)
1088 ? sv_cmp_locale(left, right)
1089 : sv_cmp(left, right));
1090 SETs(boolSV(cmp >= 0));
1097 dSP; tryAMAGICbinSET(seq,0);
1100 SETs(boolSV(sv_eq(left, right)));
1107 dSP; tryAMAGICbinSET(sne,0);
1110 SETs(boolSV(!sv_eq(left, right)));
1117 dSP; dTARGET; tryAMAGICbin(scmp,0);
1120 int cmp = ((op->op_private & OPpLOCALE)
1121 ? sv_cmp_locale(left, right)
1122 : sv_cmp(left, right));
1130 dSP; dATARGET; tryAMAGICbin(band,opASSIGN);
1133 if (SvNIOKp(left) || SvNIOKp(right)) {
1134 if (op->op_private & HINT_INTEGER) {
1135 IBW value = SvIV(left) & SvIV(right);
1139 UBW value = SvUV(left) & SvUV(right);
1144 do_vop(op->op_type, TARG, left, right);
1153 dSP; dATARGET; tryAMAGICbin(bxor,opASSIGN);
1156 if (SvNIOKp(left) || SvNIOKp(right)) {
1157 if (op->op_private & HINT_INTEGER) {
1158 IBW value = (USE_LEFT(left) ? SvIV(left) : 0) ^ SvIV(right);
1162 UBW value = (USE_LEFT(left) ? SvUV(left) : 0) ^ SvUV(right);
1167 do_vop(op->op_type, TARG, left, right);
1176 dSP; dATARGET; tryAMAGICbin(bor,opASSIGN);
1179 if (SvNIOKp(left) || SvNIOKp(right)) {
1180 if (op->op_private & HINT_INTEGER) {
1181 IBW value = (USE_LEFT(left) ? SvIV(left) : 0) | SvIV(right);
1185 UBW value = (USE_LEFT(left) ? SvUV(left) : 0) | SvUV(right);
1190 do_vop(op->op_type, TARG, left, right);
1199 dSP; dTARGET; tryAMAGICun(neg);
1204 if (SvIOKp(sv) && !SvNOKp(sv) && !SvPOKp(sv) && SvIVX(sv) != IV_MIN)
1206 else if (SvNIOKp(sv))
1208 else if (SvPOKp(sv)) {
1210 char *s = SvPV(sv, len);
1211 if (isIDFIRST(*s)) {
1212 sv_setpvn(TARG, "-", 1);
1215 else if (*s == '+' || *s == '-') {
1217 *SvPV_force(TARG, len) = *s == '-' ? '+' : '-';
1220 sv_setnv(TARG, -SvNV(sv));
1232 dSP; tryAMAGICunSET(not);
1233 #endif /* OVERLOAD */
1234 *stack_sp = boolSV(!SvTRUE(*stack_sp));
1240 dSP; dTARGET; tryAMAGICun(compl);
1244 if (op->op_private & HINT_INTEGER) {
1245 IBW value = ~SvIV(sv);
1249 UBW value = ~SvUV(sv);
1254 register char *tmps;
1255 register long *tmpl;
1260 tmps = SvPV_force(TARG, len);
1263 for ( ; anum && (unsigned long)tmps % sizeof(long); anum--, tmps++)
1266 for ( ; anum >= sizeof(long); anum -= sizeof(long), tmpl++)
1270 for ( ; anum > 0; anum--, tmps++)
1279 /* integer versions of some of the above */
1283 dSP; dATARGET; tryAMAGICbin(mult,opASSIGN);
1286 SETi( left * right );
1293 dSP; dATARGET; tryAMAGICbin(div,opASSIGN);
1297 DIE("Illegal division by zero");
1298 value = POPi / value;
1306 dSP; dATARGET; tryAMAGICbin(mod,opASSIGN);
1310 DIE("Illegal modulus zero");
1311 SETi( left % right );
1318 dSP; dATARGET; tryAMAGICbin(add,opASSIGN);
1321 SETi( left + right );
1328 dSP; dATARGET; tryAMAGICbin(subtr,opASSIGN);
1331 SETi( left - right );
1338 dSP; tryAMAGICbinSET(lt,0);
1341 SETs(boolSV(left < right));
1348 dSP; tryAMAGICbinSET(gt,0);
1351 SETs(boolSV(left > right));
1358 dSP; tryAMAGICbinSET(le,0);
1361 SETs(boolSV(left <= right));
1368 dSP; tryAMAGICbinSET(ge,0);
1371 SETs(boolSV(left >= right));
1378 dSP; tryAMAGICbinSET(eq,0);
1381 SETs(boolSV(left == right));
1388 dSP; tryAMAGICbinSET(ne,0);
1391 SETs(boolSV(left != right));
1398 dSP; dTARGET; tryAMAGICbin(ncmp,0);
1405 else if (left < right)
1416 dSP; dTARGET; tryAMAGICun(neg);
1421 /* High falutin' math. */
1425 dSP; dTARGET; tryAMAGICbin(atan2,0);
1428 SETn(atan2(left, right));
1435 dSP; dTARGET; tryAMAGICun(sin);
1447 dSP; dTARGET; tryAMAGICun(cos);
1467 if (!srand_called) {
1468 (void)srand((unsigned)seed());
1469 srand_called = TRUE;
1472 value = rand() * value / 2147483648.0;
1475 value = rand() * value / 65536.0;
1478 value = rand() * value / 32768.0;
1480 value = rand() * value / (double)(((unsigned long)1) << RANDBITS);
1496 (void)srand((unsigned)anum);
1497 srand_called = TRUE;
1506 * This is really just a quick hack which grabs various garbage
1507 * values. It really should be a real hash algorithm which
1508 * spreads the effect of every input bit onto every output bit,
1509 * if someone who knows about such tings would bother to write it.
1510 * Might be a good idea to add that function to CORE as well.
1511 * No numbers below come from careful analysis or anyting here,
1512 * except they are primes and SEED_C1 > 1E6 to get a full-width
1513 * value from (tv_sec * SEED_C1 + tv_usec). The multipliers should
1514 * probably be bigger too.
1517 # define SEED_C1 1000003
1518 #define SEED_C4 73819
1520 # define SEED_C1 25747
1521 #define SEED_C4 20639
1525 #define SEED_C5 26107
1530 # include <starlet.h>
1531 /* when[] = (low 32 bits, high 32 bits) of time since epoch
1532 * in 100-ns units, typically incremented ever 10 ms. */
1533 unsigned int when[2];
1534 _ckvmssts(sys$gettim(when));
1535 u = (U32)SEED_C1 * when[0] + (U32)SEED_C2 * when[1];
1537 # ifdef HAS_GETTIMEOFDAY
1538 struct timeval when;
1539 gettimeofday(&when,(struct timezone *) 0);
1540 u = (U32)SEED_C1 * when.tv_sec + (U32)SEED_C2 * when.tv_usec;
1544 u = (U32)SEED_C1 * when;
1547 u += SEED_C3 * (U32)getpid();
1548 u += SEED_C4 * (U32)(UV)stack_sp;
1549 #ifndef PLAN9 /* XXX Plan9 assembler chokes on this; fix needed */
1550 u += SEED_C5 * (U32)(UV)&when;
1557 dSP; dTARGET; tryAMAGICun(exp);
1569 dSP; dTARGET; tryAMAGICun(log);
1574 SET_NUMERIC_STANDARD();
1575 DIE("Can't take log of %g", value);
1585 dSP; dTARGET; tryAMAGICun(sqrt);
1590 SET_NUMERIC_STANDARD();
1591 DIE("Can't take sqrt of %g", value);
1593 value = sqrt(value);
1603 double value = TOPn;
1606 if (SvIOKp(TOPs) && !SvNOKp(TOPs) && !SvPOKp(TOPs)) {
1612 (void)modf(value, &value);
1614 (void)modf(-value, &value);
1629 dSP; dTARGET; tryAMAGICun(abs);
1631 double value = TOPn;
1634 if (SvIOKp(TOPs) && !SvNOKp(TOPs) && !SvPOKp(TOPs) &&
1635 (iv = SvIVX(TOPs)) != IV_MIN) {
1656 XPUSHu(scan_hex(tmps, 99, &argtype));
1668 while (*tmps && isSPACE(*tmps))
1673 value = scan_hex(++tmps, 99, &argtype);
1675 value = scan_oct(tmps, 99, &argtype);
1685 SETi( sv_len(TOPs) );
1698 I32 lvalue = op->op_flags & OPf_MOD;
1700 I32 arybase = curcop->cop_arybase;
1706 tmps = SvPV(sv, curlen);
1707 if (pos >= arybase) {
1725 else if (len >= 0) {
1727 if (rem > (I32)curlen)
1741 if (dowarn || lvalue)
1742 warn("substr outside of string");
1747 sv_setpvn(TARG, tmps, rem);
1748 if (lvalue) { /* it's an lvalue! */
1749 if (!SvGMAGICAL(sv)) {
1753 warn("Attempt to use reference as lvalue in substr");
1755 if (SvOK(sv)) /* is it defined ? */
1756 (void)SvPOK_only(sv);
1758 sv_setpvn(sv,"",0); /* avoid lexical reincarnation */
1761 if (SvTYPE(TARG) < SVt_PVLV) {
1762 sv_upgrade(TARG, SVt_PVLV);
1763 sv_magic(TARG, Nullsv, 'x', Nullch, 0);
1768 LvTARGOFF(TARG) = pos;
1769 LvTARGLEN(TARG) = rem;
1772 PUSHs(TARG); /* avoid SvSETMAGIC here */
1779 register I32 size = POPi;
1780 register I32 offset = POPi;
1781 register SV *src = POPs;
1782 I32 lvalue = op->op_flags & OPf_MOD;
1784 unsigned char *s = (unsigned char*)SvPV(src, srclen);
1785 unsigned long retnum;
1788 offset *= size; /* turn into bit offset */
1789 len = (offset + size + 7) / 8;
1790 if (offset < 0 || size < 1)
1793 if (lvalue) { /* it's an lvalue! */
1794 if (SvTYPE(TARG) < SVt_PVLV) {
1795 sv_upgrade(TARG, SVt_PVLV);
1796 sv_magic(TARG, Nullsv, 'v', Nullch, 0);
1801 LvTARGOFF(TARG) = offset;
1802 LvTARGLEN(TARG) = size;
1810 if (offset >= srclen)
1813 retnum = (unsigned long) s[offset] << 8;
1815 else if (size == 32) {
1816 if (offset >= srclen)
1818 else if (offset + 1 >= srclen)
1819 retnum = (unsigned long) s[offset] << 24;
1820 else if (offset + 2 >= srclen)
1821 retnum = ((unsigned long) s[offset] << 24) +
1822 ((unsigned long) s[offset + 1] << 16);
1824 retnum = ((unsigned long) s[offset] << 24) +
1825 ((unsigned long) s[offset + 1] << 16) +
1826 (s[offset + 2] << 8);
1831 retnum = (s[offset >> 3] >> (offset & 7)) & ((1 << size) - 1);
1836 else if (size == 16)
1837 retnum = ((unsigned long) s[offset] << 8) + s[offset+1];
1838 else if (size == 32)
1839 retnum = ((unsigned long) s[offset] << 24) +
1840 ((unsigned long) s[offset + 1] << 16) +
1841 (s[offset + 2] << 8) + s[offset+3];
1845 sv_setiv(TARG, (IV)retnum);
1860 I32 arybase = curcop->cop_arybase;
1865 offset = POPi - arybase;
1868 tmps = SvPV(big, biglen);
1871 else if (offset > biglen)
1873 if (!(tmps2 = fbm_instr((unsigned char*)tmps + offset,
1874 (unsigned char*)tmps + biglen, little)))
1875 retval = -1 + arybase;
1877 retval = tmps2 - tmps + arybase;
1894 I32 arybase = curcop->cop_arybase;
1900 tmps2 = SvPV(little, llen);
1901 tmps = SvPV(big, blen);
1905 offset = SvIV(offstr) - arybase + llen;
1908 else if (offset > blen)
1910 if (!(tmps2 = rninstr(tmps, tmps + offset,
1911 tmps2, tmps2 + llen)))
1912 retval = -1 + arybase;
1914 retval = tmps2 - tmps + arybase;
1921 dSP; dMARK; dORIGMARK; dTARGET;
1922 #ifdef USE_LOCALE_NUMERIC
1923 if (op->op_private & OPpLOCALE)
1924 SET_NUMERIC_LOCAL();
1926 SET_NUMERIC_STANDARD();
1928 do_sprintf(TARG, SP-MARK, MARK+1);
1929 TAINT_IF(SvTAINTED(TARG));
1943 value = (I32) (*tmps & 255);
1948 value = (I32) (anum & 255);
1959 (void)SvUPGRADE(TARG,SVt_PV);
1965 (void)SvPOK_only(TARG);
1972 dSP; dTARGET; dPOPTOPssrl;
1974 char *tmps = SvPV(left, na);
1976 sv_setpv(TARG, fcrypt(tmps, SvPV(right, na)));
1978 sv_setpv(TARG, crypt(tmps, SvPV(right, na)));
1982 "The crypt() function is unimplemented due to excessive paranoia.");
1994 if (!SvPADTMP(sv)) {
2000 s = SvPV_force(sv, na);
2002 if (op->op_private & OPpLOCALE) {
2005 *s = toUPPER_LC(*s);
2020 if (!SvPADTMP(sv)) {
2026 s = SvPV_force(sv, na);
2028 if (op->op_private & OPpLOCALE) {
2031 *s = toLOWER_LC(*s);
2048 if (!SvPADTMP(sv)) {
2055 s = SvPV_force(sv, len);
2057 register char *send = s + len;
2059 if (op->op_private & OPpLOCALE) {
2062 for (; s < send; s++)
2063 *s = toUPPER_LC(*s);
2066 for (; s < send; s++)
2080 if (!SvPADTMP(sv)) {
2087 s = SvPV_force(sv, len);
2089 register char *send = s + len;
2091 if (op->op_private & OPpLOCALE) {
2094 for (; s < send; s++)
2095 *s = toLOWER_LC(*s);
2098 for (; s < send; s++)
2110 register char *s = SvPV(sv,len);
2114 (void)SvUPGRADE(TARG, SVt_PV);
2115 SvGROW(TARG, (len * 2) + 1);
2123 SvCUR_set(TARG, d - SvPVX(TARG));
2124 (void)SvPOK_only(TARG);
2127 sv_setpvn(TARG, s, len);
2136 dSP; dMARK; dORIGMARK;
2138 register AV* av = (AV*)POPs;
2139 register I32 lval = op->op_flags & OPf_MOD;
2140 I32 arybase = curcop->cop_arybase;
2143 if (SvTYPE(av) == SVt_PVAV) {
2144 if (lval && op->op_private & OPpLVAL_INTRO) {
2146 for (svp = mark + 1; svp <= sp; svp++) {
2151 if (max > AvMAX(av))
2154 while (++MARK <= SP) {
2155 elem = SvIVx(*MARK);
2159 svp = av_fetch(av, elem, lval);
2161 if (!svp || *svp == &sv_undef)
2162 DIE(no_aelem, elem);
2163 if (op->op_private & OPpLVAL_INTRO)
2166 *MARK = svp ? *svp : &sv_undef;
2169 if (GIMME != G_ARRAY) {
2177 /* Associative arrays. */
2182 HV *hash = (HV*)POPs;
2184 I32 gimme = GIMME_V;
2185 I32 realhv = (SvTYPE(hash) == SVt_PVHV);
2188 /* might clobber stack_sp */
2189 entry = realhv ? hv_iternext(hash) : avhv_iternext((AV*)hash);
2194 PUSHs(hv_iterkeysv(entry)); /* won't clobber stack_sp */
2195 if (gimme == G_ARRAY) {
2197 /* might clobber stack_sp */
2198 sv_setsv(TARG, realhv ?
2199 hv_iterval(hash, entry) : avhv_iterval((AV*)hash, entry));
2204 else if (gimme == G_SCALAR)
2223 I32 gimme = GIMME_V;
2224 I32 discard = (gimme == G_VOID) ? G_DISCARD : 0;
2228 if (op->op_private & OPpSLICE) {
2232 hvtype = SvTYPE(hv);
2233 while (++MARK <= SP) {
2234 if (hvtype == SVt_PVHV)
2235 sv = hv_delete_ent(hv, *MARK, discard, 0);
2236 else if (hvtype == SVt_PVAV)
2237 sv = avhv_delete_ent((AV*)hv, *MARK, discard, 0);
2239 DIE("Not a HASH reference");
2240 *MARK = sv ? sv : &sv_undef;
2244 else if (gimme == G_SCALAR) {
2253 if (SvTYPE(hv) == SVt_PVHV)
2254 sv = hv_delete_ent(hv, keysv, discard, 0);
2255 else if (SvTYPE(hv) == SVt_PVAV)
2256 sv = avhv_delete_ent((AV*)hv, keysv, discard, 0);
2258 DIE("Not a HASH reference");
2272 if (SvTYPE(hv) == SVt_PVHV) {
2273 if (hv_exists_ent(hv, tmpsv, 0))
2275 } else if (SvTYPE(hv) == SVt_PVAV) {
2276 if (avhv_exists_ent((AV*)hv, tmpsv, 0))
2279 DIE("Not a HASH reference");
2286 dSP; dMARK; dORIGMARK;
2288 register HV *hv = (HV*)POPs;
2289 register I32 lval = op->op_flags & OPf_MOD;
2290 I32 realhv = (SvTYPE(hv) == SVt_PVHV);
2292 if (realhv || SvTYPE(hv) == SVt_PVAV) {
2293 while (++MARK <= SP) {
2297 he = hv_fetch_ent(hv, keysv, lval, 0);
2298 svp = he ? &HeVAL(he) : 0;
2300 svp = avhv_fetch_ent((AV*)hv, keysv, lval, 0);
2303 if (!he || HeVAL(he) == &sv_undef)
2304 DIE(no_helem, SvPV(keysv, na));
2305 if (op->op_private & OPpLVAL_INTRO)
2306 save_svref(&HeVAL(he));
2308 *MARK = he ? HeVAL(he) : &sv_undef;
2311 if (GIMME != G_ARRAY) {
2319 /* List operators. */
2324 if (GIMME != G_ARRAY) {
2326 *MARK = *SP; /* unwanted list, return last item */
2337 SV **lastrelem = stack_sp;
2338 SV **lastlelem = stack_base + POPMARK;
2339 SV **firstlelem = stack_base + POPMARK + 1;
2340 register SV **firstrelem = lastlelem + 1;
2341 I32 arybase = curcop->cop_arybase;
2342 I32 lval = op->op_flags & OPf_MOD;
2343 I32 is_something_there = lval;
2345 register I32 max = lastrelem - lastlelem;
2346 register SV **lelem;
2349 if (GIMME != G_ARRAY) {
2350 ix = SvIVx(*lastlelem);
2355 if (ix < 0 || ix >= max)
2356 *firstlelem = &sv_undef;
2358 *firstlelem = firstrelem[ix];
2364 SP = firstlelem - 1;
2368 for (lelem = firstlelem; lelem <= lastlelem; lelem++) {
2374 else if (!(*lelem = firstrelem[ix]))
2379 if (ix >= max || !(*lelem = firstrelem[ix]))
2382 if (!is_something_there && (SvOK(*lelem) || SvGMAGICAL(*lelem)))
2383 is_something_there = TRUE;
2385 if (is_something_there)
2388 SP = firstlelem - 1;
2394 dSP; dMARK; dORIGMARK;
2395 I32 items = SP - MARK;
2396 SV *av = sv_2mortal((SV*)av_make(items, MARK+1));
2397 SP = ORIGMARK; /* av_make() might realloc stack_sp */
2404 dSP; dMARK; dORIGMARK;
2405 HV* hv = (HV*)sv_2mortal((SV*)newHV());
2409 SV *val = NEWSV(46, 0);
2411 sv_setsv(val, *++MARK);
2413 warn("Odd number of elements in hash list");
2414 (void)hv_store_ent(hv,key,val,0);
2423 dSP; dMARK; dORIGMARK;
2424 register AV *ary = (AV*)*++MARK;
2428 register I32 offset;
2429 register I32 length;
2438 offset = i = SvIVx(*MARK);
2440 offset += AvFILL(ary) + 1;
2442 offset -= curcop->cop_arybase;
2446 length = SvIVx(*MARK++);
2451 length = AvMAX(ary) + 1; /* close enough to infinity */
2455 length = AvMAX(ary) + 1;
2457 if (offset > AvFILL(ary) + 1)
2458 offset = AvFILL(ary) + 1;
2459 after = AvFILL(ary) + 1 - (offset + length);
2460 if (after < 0) { /* not that much array */
2461 length += after; /* offset+length now in array */
2467 /* At this point, MARK .. SP-1 is our new LIST */
2470 diff = newlen - length;
2471 if (newlen && !AvREAL(ary)) {
2475 assert(AvREAL(ary)); /* would leak, so croak */
2478 if (diff < 0) { /* shrinking the area */
2480 New(451, tmparyval, newlen, SV*); /* so remember insertion */
2481 Copy(MARK, tmparyval, newlen, SV*);
2484 MARK = ORIGMARK + 1;
2485 if (GIMME == G_ARRAY) { /* copy return vals to stack */
2486 MEXTEND(MARK, length);
2487 Copy(AvARRAY(ary)+offset, MARK, length, SV*);
2489 EXTEND_MORTAL(length);
2490 for (i = length, dst = MARK; i; i--) {
2491 if (!SvIMMORTAL(*dst))
2492 sv_2mortal(*dst); /* free them eventualy */
2499 *MARK = AvARRAY(ary)[offset+length-1];
2501 if (!SvIMMORTAL(*MARK))
2503 for (i = length - 1, dst = &AvARRAY(ary)[offset]; i > 0; i--)
2504 SvREFCNT_dec(*dst++); /* free them now */
2507 AvFILL(ary) += diff;
2509 /* pull up or down? */
2511 if (offset < after) { /* easier to pull up */
2512 if (offset) { /* esp. if nothing to pull */
2513 src = &AvARRAY(ary)[offset-1];
2514 dst = src - diff; /* diff is negative */
2515 for (i = offset; i > 0; i--) /* can't trust Copy */
2519 SvPVX(ary) = (char*)(AvARRAY(ary) - diff); /* diff is negative */
2523 if (after) { /* anything to pull down? */
2524 src = AvARRAY(ary) + offset + length;
2525 dst = src + diff; /* diff is negative */
2526 Move(src, dst, after, SV*);
2528 dst = &AvARRAY(ary)[AvFILL(ary)+1];
2529 /* avoid later double free */
2533 dst[--i] = &sv_undef;
2536 for (src = tmparyval, dst = AvARRAY(ary) + offset;
2538 *dst = NEWSV(46, 0);
2539 sv_setsv(*dst++, *src++);
2541 Safefree(tmparyval);
2544 else { /* no, expanding (or same) */
2546 New(452, tmparyval, length, SV*); /* so remember deletion */
2547 Copy(AvARRAY(ary)+offset, tmparyval, length, SV*);
2550 if (diff > 0) { /* expanding */
2552 /* push up or down? */
2554 if (offset < after && diff <= AvARRAY(ary) - AvALLOC(ary)) {
2558 Move(src, dst, offset, SV*);
2560 SvPVX(ary) = (char*)(AvARRAY(ary) - diff);/* diff is positive */
2562 AvFILL(ary) += diff;
2565 if (AvFILL(ary) + diff >= AvMAX(ary)) /* oh, well */
2566 av_extend(ary, AvFILL(ary) + diff);
2567 AvFILL(ary) += diff;
2570 dst = AvARRAY(ary) + AvFILL(ary);
2572 for (i = after; i; i--) {
2579 for (src = MARK, dst = AvARRAY(ary) + offset; newlen; newlen--) {
2580 *dst = NEWSV(46, 0);
2581 sv_setsv(*dst++, *src++);
2583 MARK = ORIGMARK + 1;
2584 if (GIMME == G_ARRAY) { /* copy return vals to stack */
2586 Copy(tmparyval, MARK, length, SV*);
2588 EXTEND_MORTAL(length);
2589 for (i = length, dst = MARK; i; i--) {
2590 if (!SvIMMORTAL(*dst))
2591 sv_2mortal(*dst); /* free them eventualy */
2595 Safefree(tmparyval);
2599 else if (length--) {
2600 *MARK = tmparyval[length];
2602 if (!SvIMMORTAL(*MARK))
2604 while (length-- > 0)
2605 SvREFCNT_dec(tmparyval[length]);
2607 Safefree(tmparyval);
2618 dSP; dMARK; dORIGMARK; dTARGET;
2619 register AV *ary = (AV*)*++MARK;
2620 register SV *sv = &sv_undef;
2622 for (++MARK; MARK <= SP; MARK++) {
2625 sv_setsv(sv, *MARK);
2629 PUSHi( AvFILL(ary) + 1 );
2637 SV *sv = av_pop(av);
2638 if (!SvIMMORTAL(sv) && AvREAL(av))
2639 (void)sv_2mortal(sv);
2648 SV *sv = av_shift(av);
2652 if (!SvIMMORTAL(sv) && AvREAL(av))
2653 (void)sv_2mortal(sv);
2660 dSP; dMARK; dORIGMARK; dTARGET;
2661 register AV *ary = (AV*)*++MARK;
2665 av_unshift(ary, SP - MARK);
2668 sv_setsv(sv, *++MARK);
2669 (void)av_store(ary, i++, sv);
2673 PUSHi( AvFILL(ary) + 1 );
2683 if (GIMME == G_ARRAY) {
2694 register char *down;
2700 do_join(TARG, &sv_no, MARK, SP);
2702 sv_setsv(TARG, (SP > MARK) ? *SP : GvSV(defgv));
2703 up = SvPV_force(TARG, len);
2705 down = SvPVX(TARG) + len - 1;
2711 (void)SvPOK_only(TARG);
2725 char *s = SvPV(sv, len);
2729 if (!strnEQ(s, "0000", 4)) { /* need to grow sv */
2730 SV *new = newSVpv("0000000000", 10);
2733 SvREFCNT_dec(sv); /* free old sv */
2738 while (!*t) /* trailing '\0'? */
2741 i = ((*t - '0') << 7) + m;
2742 *(t--) = '0' + (i % 10);
2748 /* Explosives and implosives. */
2755 I32 gimme = GIMME_V;
2759 register char *pat = SvPV(left, llen);
2760 register char *s = SvPV(right, rlen);
2761 char *strend = s + rlen;
2763 register char *patend = pat + llen;
2768 /* These must not be in registers: */
2779 unsigned Quad_t auquad;
2785 register U32 culong;
2787 static char* bitcount = 0;
2790 if (gimme != G_ARRAY) { /* arrange to do first one only */
2792 for (patend = pat; !isALPHA(*patend) || *patend == 'x'; patend++) ;
2793 if (strchr("aAbBhHP", *patend) || *pat == '%') {
2795 while (isDIGIT(*patend) || *patend == '*')
2801 while (pat < patend) {
2803 datumtype = *pat++ & 0xFF;
2804 if (isSPACE(datumtype))
2808 else if (*pat == '*') {
2809 len = strend - strbeg; /* long enough */
2812 else if (isDIGIT(*pat)) {
2814 while (isDIGIT(*pat))
2815 len = (len * 10) + (*pat++ - '0');
2818 len = (datumtype != '@');
2821 croak("Invalid type in unpack: '%c'", (int)datumtype);
2822 case ',': /* grandfather in commas but with a warning */
2823 if (commas++ == 0 && dowarn)
2824 warn("Invalid type in unpack: '%c'", (int)datumtype);
2827 if (len == 1 && pat[-1] != '1')
2836 if (len > strend - strbeg)
2837 DIE("@ outside of string");
2841 if (len > s - strbeg)
2842 DIE("X outside of string");
2846 if (len > strend - s)
2847 DIE("x outside of string");
2852 if (len > strend - s)
2855 goto uchar_checksum;
2856 sv = NEWSV(35, len);
2857 sv_setpvn(sv, s, len);
2859 if (datumtype == 'A') {
2860 aptr = s; /* borrow register */
2861 s = SvPVX(sv) + len - 1;
2862 while (s >= SvPVX(sv) && (!*s || isSPACE(*s)))
2865 SvCUR_set(sv, s - SvPVX(sv));
2866 s = aptr; /* unborrow register */
2868 XPUSHs(sv_2mortal(sv));
2872 if (pat[-1] == '*' || len > (strend - s) * 8)
2873 len = (strend - s) * 8;
2876 Newz(601, bitcount, 256, char);
2877 for (bits = 1; bits < 256; bits++) {
2878 if (bits & 1) bitcount[bits]++;
2879 if (bits & 2) bitcount[bits]++;
2880 if (bits & 4) bitcount[bits]++;
2881 if (bits & 8) bitcount[bits]++;
2882 if (bits & 16) bitcount[bits]++;
2883 if (bits & 32) bitcount[bits]++;
2884 if (bits & 64) bitcount[bits]++;
2885 if (bits & 128) bitcount[bits]++;
2889 culong += bitcount[*(unsigned char*)s++];
2894 if (datumtype == 'b') {
2896 if (bits & 1) culong++;
2902 if (bits & 128) culong++;
2909 sv = NEWSV(35, len + 1);
2912 aptr = pat; /* borrow register */
2914 if (datumtype == 'b') {
2916 for (len = 0; len < aint; len++) {
2917 if (len & 7) /*SUPPRESS 595*/
2921 *pat++ = '0' + (bits & 1);
2926 for (len = 0; len < aint; len++) {
2931 *pat++ = '0' + ((bits & 128) != 0);
2935 pat = aptr; /* unborrow register */
2936 XPUSHs(sv_2mortal(sv));
2940 if (pat[-1] == '*' || len > (strend - s) * 2)
2941 len = (strend - s) * 2;
2942 sv = NEWSV(35, len + 1);
2945 aptr = pat; /* borrow register */
2947 if (datumtype == 'h') {
2949 for (len = 0; len < aint; len++) {
2954 *pat++ = hexdigit[bits & 15];
2959 for (len = 0; len < aint; len++) {
2964 *pat++ = hexdigit[(bits >> 4) & 15];
2968 pat = aptr; /* unborrow register */
2969 XPUSHs(sv_2mortal(sv));
2972 if (len > strend - s)
2977 if (aint >= 128) /* fake up signed chars */
2987 if (aint >= 128) /* fake up signed chars */
2990 sv_setiv(sv, (IV)aint);
2991 PUSHs(sv_2mortal(sv));
2996 if (len > strend - s)
3011 sv_setiv(sv, (IV)auint);
3012 PUSHs(sv_2mortal(sv));
3017 along = (strend - s) / SIZE16;
3034 sv_setiv(sv, (IV)ashort);
3035 PUSHs(sv_2mortal(sv));
3042 along = (strend - s) / SIZE16;
3047 COPY16(s, &aushort);
3050 if (datumtype == 'n')
3051 aushort = ntohs(aushort);
3054 if (datumtype == 'v')
3055 aushort = vtohs(aushort);
3064 COPY16(s, &aushort);
3068 if (datumtype == 'n')
3069 aushort = ntohs(aushort);
3072 if (datumtype == 'v')
3073 aushort = vtohs(aushort);
3075 sv_setiv(sv, (IV)aushort);
3076 PUSHs(sv_2mortal(sv));
3081 along = (strend - s) / sizeof(int);
3086 Copy(s, &aint, 1, int);
3089 cdouble += (double)aint;
3098 Copy(s, &aint, 1, int);
3101 sv_setiv(sv, (IV)aint);
3102 PUSHs(sv_2mortal(sv));
3107 along = (strend - s) / sizeof(unsigned int);
3112 Copy(s, &auint, 1, unsigned int);
3113 s += sizeof(unsigned int);
3115 cdouble += (double)auint;
3124 Copy(s, &auint, 1, unsigned int);
3125 s += sizeof(unsigned int);
3127 sv_setuv(sv, (UV)auint);
3128 PUSHs(sv_2mortal(sv));
3133 along = (strend - s) / SIZE32;
3141 cdouble += (double)along;
3153 sv_setiv(sv, (IV)along);
3154 PUSHs(sv_2mortal(sv));
3161 along = (strend - s) / SIZE32;
3169 if (datumtype == 'N')
3170 aulong = ntohl(aulong);
3173 if (datumtype == 'V')
3174 aulong = vtohl(aulong);
3177 cdouble += (double)aulong;
3189 if (datumtype == 'N')
3190 aulong = ntohl(aulong);
3193 if (datumtype == 'V')
3194 aulong = vtohl(aulong);
3197 sv_setuv(sv, (UV)aulong);
3198 PUSHs(sv_2mortal(sv));
3203 along = (strend - s) / sizeof(char*);
3209 if (sizeof(char*) > strend - s)
3212 Copy(s, &aptr, 1, char*);
3218 PUSHs(sv_2mortal(sv));
3228 while ((len > 0) && (s < strend)) {
3229 auv = (auv << 7) | (*s & 0x7f);
3230 if (!(*s++ & 0x80)) {
3234 PUSHs(sv_2mortal(sv));
3238 else if (++bytes >= sizeof(UV)) { /* promote to string */
3241 sv = newSVpvf("%.*Vu", (int)TYPE_DIGITS(UV), auv);
3242 while (s < strend) {
3243 sv = mul128(sv, *s & 0x7f);
3244 if (!(*s++ & 0x80)) {
3253 PUSHs(sv_2mortal(sv));
3258 if ((s >= strend) && bytes)
3259 croak("Unterminated compressed integer");
3264 if (sizeof(char*) > strend - s)
3267 Copy(s, &aptr, 1, char*);
3272 sv_setpvn(sv, aptr, len);
3273 PUSHs(sv_2mortal(sv));
3280 if (s + sizeof(Quad_t) > strend)
3283 Copy(s, &aquad, 1, Quad_t);
3284 s += sizeof(Quad_t);
3287 if (aquad >= IV_MIN && aquad <= IV_MAX)
3288 sv_setiv(sv, (IV)aquad);
3290 sv_setnv(sv, (double)aquad);
3291 PUSHs(sv_2mortal(sv));
3298 if (s + sizeof(unsigned Quad_t) > strend)
3301 Copy(s, &auquad, 1, unsigned Quad_t);
3302 s += sizeof(unsigned Quad_t);
3305 if (aquad <= UV_MAX)
3306 sv_setuv(sv, (UV)auquad);
3308 sv_setnv(sv, (double)auquad);
3309 PUSHs(sv_2mortal(sv));
3313 /* float and double added gnb@melba.bby.oz.au 22/11/89 */
3316 along = (strend - s) / sizeof(float);
3321 Copy(s, &afloat, 1, float);
3330 Copy(s, &afloat, 1, float);
3333 sv_setnv(sv, (double)afloat);
3334 PUSHs(sv_2mortal(sv));
3340 along = (strend - s) / sizeof(double);
3345 Copy(s, &adouble, 1, double);
3346 s += sizeof(double);
3354 Copy(s, &adouble, 1, double);
3355 s += sizeof(double);
3357 sv_setnv(sv, (double)adouble);
3358 PUSHs(sv_2mortal(sv));
3363 along = (strend - s) * 3 / 4;
3364 sv = NEWSV(42, along);
3367 while (s < strend && *s > ' ' && *s < 'a') {
3372 len = (*s++ - ' ') & 077;
3374 if (s < strend && *s >= ' ')
3375 a = (*s++ - ' ') & 077;
3378 if (s < strend && *s >= ' ')
3379 b = (*s++ - ' ') & 077;
3382 if (s < strend && *s >= ' ')
3383 c = (*s++ - ' ') & 077;
3386 if (s < strend && *s >= ' ')
3387 d = (*s++ - ' ') & 077;
3390 hunk[0] = a << 2 | b >> 4;
3391 hunk[1] = b << 4 | c >> 2;
3392 hunk[2] = c << 6 | d;
3393 sv_catpvn(sv, hunk, len > 3 ? 3 : len);
3398 else if (s[1] == '\n') /* possible checksum byte */
3401 XPUSHs(sv_2mortal(sv));
3406 if (strchr("fFdD", datumtype) ||
3407 (checksum > 32 && strchr("iIlLN", datumtype)) ) {
3411 while (checksum >= 16) {
3415 while (checksum >= 4) {
3421 along = (1 << checksum) - 1;
3422 while (cdouble < 0.0)
3424 cdouble = modf(cdouble / adouble, &trouble) * adouble;
3425 sv_setnv(sv, cdouble);
3428 if (checksum < 32) {
3429 aulong = (1 << checksum) - 1;
3432 sv_setuv(sv, (UV)culong);
3434 XPUSHs(sv_2mortal(sv));
3438 if (sp == oldsp && gimme == G_SCALAR)
3444 doencodes(sv, s, len)
3452 sv_catpvn(sv, hunk, 1);
3455 hunk[0] = ' ' + (077 & (*s >> 2));
3456 hunk[1] = ' ' + (077 & ((*s << 4) & 060 | (s[1] >> 4) & 017));
3457 hunk[2] = ' ' + (077 & ((s[1] << 2) & 074 | (s[2] >> 6) & 03));
3458 hunk[3] = ' ' + (077 & (s[2] & 077));
3459 sv_catpvn(sv, hunk, 4);
3463 for (s = SvPVX(sv); *s; s++) {
3467 sv_catpvn(sv, "\n", 1);
3475 SV *result = newSVpv("", l);
3476 char *result_c = SvPV(result, na); /* convenience */
3477 char *out = result_c;
3487 SvREFCNT_dec(result);
3510 SvREFCNT_dec(result);
3516 SvCUR_set(result, out - result_c);
3522 SV *pnum; /* must be '\0' terminated */
3526 char *s = SvPV(pnum, len);
3535 i = m * 10 + (*t - '0');
3537 r = (i >> 7); /* r < 10 */
3544 SvCUR_set(pnum, (STRLEN) (t - s));
3551 dSP; dMARK; dORIGMARK; dTARGET;
3552 register SV *cat = TARG;
3555 register char *pat = SvPVx(*++MARK, fromlen);
3556 register char *patend = pat + fromlen;
3561 static char null10[] = {0,0,0,0,0,0,0,0,0,0};
3562 static char *space10 = " ";
3564 /* These must not be in registers: */
3573 unsigned Quad_t auquad;
3582 sv_setpvn(cat, "", 0);
3583 while (pat < patend) {
3584 #define NEXTFROM (items-- > 0 ? *MARK++ : &sv_no)
3585 datumtype = *pat++ & 0xFF;
3586 if (isSPACE(datumtype))
3589 len = strchr("@Xxu", datumtype) ? 0 : items;
3592 else if (isDIGIT(*pat)) {
3594 while (isDIGIT(*pat))
3595 len = (len * 10) + (*pat++ - '0');
3601 croak("Invalid type in pack: '%c'", (int)datumtype);
3602 case ',': /* grandfather in commas but with a warning */
3603 if (commas++ == 0 && dowarn)
3604 warn("Invalid type in pack: '%c'", (int)datumtype);
3607 DIE("%% may only be used in unpack");
3618 if (SvCUR(cat) < len)
3619 DIE("X outside of string");
3626 sv_catpvn(cat, null10, 10);
3629 sv_catpvn(cat, null10, len);
3634 aptr = SvPV(fromstr, fromlen);
3638 sv_catpvn(cat, aptr, len);
3640 sv_catpvn(cat, aptr, fromlen);
3642 if (datumtype == 'A') {
3644 sv_catpvn(cat, space10, 10);
3647 sv_catpvn(cat, space10, len);
3651 sv_catpvn(cat, null10, 10);
3654 sv_catpvn(cat, null10, len);
3661 char *savepat = pat;
3666 aptr = SvPV(fromstr, fromlen);
3671 SvCUR(cat) += (len+7)/8;
3672 SvGROW(cat, SvCUR(cat) + 1);
3673 aptr = SvPVX(cat) + aint;
3678 if (datumtype == 'B') {
3679 for (len = 0; len++ < aint;) {
3680 items |= *pat++ & 1;
3684 *aptr++ = items & 0xff;
3690 for (len = 0; len++ < aint;) {
3696 *aptr++ = items & 0xff;
3702 if (datumtype == 'B')
3703 items <<= 7 - (aint & 7);
3705 items >>= 7 - (aint & 7);
3706 *aptr++ = items & 0xff;
3708 pat = SvPVX(cat) + SvCUR(cat);
3719 char *savepat = pat;
3724 aptr = SvPV(fromstr, fromlen);
3729 SvCUR(cat) += (len+1)/2;
3730 SvGROW(cat, SvCUR(cat) + 1);
3731 aptr = SvPVX(cat) + aint;
3736 if (datumtype == 'H') {
3737 for (len = 0; len++ < aint;) {
3739 items |= ((*pat++ & 15) + 9) & 15;
3741 items |= *pat++ & 15;
3745 *aptr++ = items & 0xff;
3751 for (len = 0; len++ < aint;) {
3753 items |= (((*pat++ & 15) + 9) & 15) << 4;
3755 items |= (*pat++ & 15) << 4;
3759 *aptr++ = items & 0xff;
3765 *aptr++ = items & 0xff;
3766 pat = SvPVX(cat) + SvCUR(cat);
3778 aint = SvIV(fromstr);
3780 sv_catpvn(cat, &achar, sizeof(char));
3783 /* Float and double added by gnb@melba.bby.oz.au 22/11/89 */
3788 afloat = (float)SvNV(fromstr);
3789 sv_catpvn(cat, (char *)&afloat, sizeof (float));
3796 adouble = (double)SvNV(fromstr);
3797 sv_catpvn(cat, (char *)&adouble, sizeof (double));
3803 ashort = (I16)SvIV(fromstr);
3805 ashort = htons(ashort);
3807 CAT16(cat, &ashort);
3813 ashort = (I16)SvIV(fromstr);
3815 ashort = htovs(ashort);
3817 CAT16(cat, &ashort);
3824 ashort = (I16)SvIV(fromstr);
3825 CAT16(cat, &ashort);
3831 auint = SvUV(fromstr);
3832 sv_catpvn(cat, (char*)&auint, sizeof(unsigned int));
3838 adouble = floor(SvNV(fromstr));
3841 croak("Cannot compress negative numbers");
3847 #ifdef CXUX_BROKEN_CONSTANT_CONVERT
3848 adouble <= UV_MAX_cxux
3855 char buf[1 + sizeof(UV)];
3856 char *in = buf + sizeof(buf);
3857 UV auv = U_V(adouble);;
3860 *--in = (auv & 0x7f) | 0x80;
3863 buf[sizeof(buf) - 1] &= 0x7f; /* clear continue bit */
3864 sv_catpvn(cat, in, (buf + sizeof(buf)) - in);
3866 else if (SvPOKp(fromstr)) { /* decimal string arithmetics */
3867 char *from, *result, *in;
3872 /* Copy string and check for compliance */
3873 from = SvPV(fromstr, len);
3874 if ((norm = is_an_int(from, len)) == NULL)
3875 croak("can compress only unsigned integer");
3877 New('w', result, len, char);
3881 *--in = div128(norm, &done) | 0x80;
3882 result[len - 1] &= 0x7F; /* clear continue bit */
3883 sv_catpvn(cat, in, (result + len) - in);
3885 SvREFCNT_dec(norm); /* free norm */
3887 else if (SvNOKp(fromstr)) {
3888 char buf[sizeof(double) * 2]; /* 8/7 <= 2 */
3889 char *in = buf + sizeof(buf);
3892 double next = floor(adouble / 128);
3893 *--in = (unsigned char)(adouble - (next * 128)) | 0x80;
3894 if (--in < buf) /* this cannot happen ;-) */
3895 croak ("Cannot compress integer");
3897 } while (adouble > 0);
3898 buf[sizeof(buf) - 1] &= 0x7f; /* clear continue bit */
3899 sv_catpvn(cat, in, (buf + sizeof(buf)) - in);
3902 croak("Cannot compress non integer");
3908 aint = SvIV(fromstr);
3909 sv_catpvn(cat, (char*)&aint, sizeof(int));
3915 aulong = SvUV(fromstr);
3917 aulong = htonl(aulong);
3919 CAT32(cat, &aulong);
3925 aulong = SvUV(fromstr);
3927 aulong = htovl(aulong);
3929 CAT32(cat, &aulong);
3935 aulong = SvUV(fromstr);
3936 CAT32(cat, &aulong);
3942 along = SvIV(fromstr);
3950 auquad = (unsigned Quad_t)SvIV(fromstr);
3951 sv_catpvn(cat, (char*)&auquad, sizeof(unsigned Quad_t));
3957 aquad = (Quad_t)SvIV(fromstr);
3958 sv_catpvn(cat, (char*)&aquad, sizeof(Quad_t));
3961 #endif /* HAS_QUAD */
3963 len = 1; /* assume SV is correct length */
3968 if (fromstr == &sv_undef)
3971 /* XXX better yet, could spirit away the string to
3972 * a safe spot and hang on to it until the result
3973 * of pack() (and all copies of the result) are
3976 if (dowarn && (SvTEMP(fromstr) || SvPADTMP(fromstr)))
3977 warn("Attempt to pack pointer to temporary value");
3978 if (SvPOK(fromstr) || SvNIOK(fromstr))
3979 aptr = SvPV(fromstr,na);
3981 aptr = SvPV_force(fromstr,na);
3983 sv_catpvn(cat, (char*)&aptr, sizeof(char*));
3988 aptr = SvPV(fromstr, fromlen);
3989 SvGROW(cat, fromlen * 4 / 3);
3994 while (fromlen > 0) {
4001 doencodes(cat, aptr, todo);
4019 register I32 limit = POPi; /* note, negative is forever */
4022 register char *s = SvPV(sv, len);
4023 char *strend = s + len;
4025 register REGEXP *rx;
4029 I32 maxiters = (strend - s) + 10;
4032 I32 origlimit = limit;
4035 AV *oldstack = curstack;
4036 I32 gimme = GIMME_V;
4037 I32 oldsave = savestack_ix;
4040 Copy(&LvTARGOFF(POPs), &pm, 1, PMOP*);
4045 DIE("panic: do_split");
4046 rx = pm->op_pmregexp;
4048 TAINT_IF((pm->op_pmflags & PMf_LOCALE) &&
4049 (pm->op_pmflags & (PMf_WHITE | PMf_SKIPWHITE)));
4051 if (pm->op_pmreplroot)
4052 ary = GvAVn((GV*)pm->op_pmreplroot);
4053 else if (gimme != G_ARRAY)
4055 ary = (AV*)curpad[0];
4058 #endif /* USE_THREADS */
4061 if (ary && (gimme != G_ARRAY || (pm->op_pmflags & PMf_ONCE))) {
4065 for (i = AvFILL(ary); i >= 0; i--)
4066 AvARRAY(ary)[i] = &sv_undef; /* don't free mere refs */
4070 /* temporarily switch stacks */
4071 SWITCHSTACK(curstack, ary);
4073 base = SP - stack_base;
4075 if (pm->op_pmflags & PMf_SKIPWHITE) {
4076 if (pm->op_pmflags & PMf_LOCALE) {
4077 while (isSPACE_LC(*s))
4085 if (pm->op_pmflags & (PMf_MULTILINE|PMf_SINGLELINE)) {
4087 multiline = pm->op_pmflags & PMf_MULTILINE;
4091 limit = maxiters + 2;
4092 if (pm->op_pmflags & PMf_WHITE) {
4095 while (m < strend &&
4096 !((pm->op_pmflags & PMf_LOCALE)
4097 ? isSPACE_LC(*m) : isSPACE(*m)))
4102 dstr = NEWSV(30, m-s);
4103 sv_setpvn(dstr, s, m-s);
4109 while (s < strend &&
4110 ((pm->op_pmflags & PMf_LOCALE)
4111 ? isSPACE_LC(*s) : isSPACE(*s)))
4115 else if (strEQ("^", rx->precomp)) {
4118 for (m = s; m < strend && *m != '\n'; m++) ;
4122 dstr = NEWSV(30, m-s);
4123 sv_setpvn(dstr, s, m-s);
4130 else if (pm->op_pmshort && !rx->nparens) {
4131 i = SvCUR(pm->op_pmshort);
4133 i = *SvPVX(pm->op_pmshort);
4136 for (m = s; m < strend && *m != i; m++) ;
4139 dstr = NEWSV(30, m-s);
4140 sv_setpvn(dstr, s, m-s);
4149 while (s < strend && --limit &&
4150 (m=fbm_instr((unsigned char*)s, (unsigned char*)strend,
4154 dstr = NEWSV(31, m-s);
4155 sv_setpvn(dstr, s, m-s);
4164 maxiters += (strend - s) * rx->nparens;
4165 while (s < strend && --limit &&
4166 pregexec(rx, s, strend, orig, 1, Nullsv, TRUE))
4168 TAINT_IF(rx->exec_tainted);
4170 && rx->subbase != orig) {
4175 strend = s + (strend - m);
4178 dstr = NEWSV(32, m-s);
4179 sv_setpvn(dstr, s, m-s);
4184 for (i = 1; i <= rx->nparens; i++) {
4188 dstr = NEWSV(33, m-s);
4189 sv_setpvn(dstr, s, m-s);
4192 dstr = NEWSV(33, 0);
4201 LEAVE_SCOPE(oldsave);
4202 iters = (SP - stack_base) - base;
4203 if (iters > maxiters)
4206 /* keep field after final delim? */
4207 if (s < strend || (iters && origlimit)) {
4208 dstr = NEWSV(34, strend-s);
4209 sv_setpvn(dstr, s, strend-s);
4215 else if (!origlimit) {
4216 while (iters > 0 && (!TOPs || !SvANY(TOPs) || SvCUR(TOPs) == 0))
4220 SWITCHSTACK(ary, oldstack);
4221 if (SvSMAGICAL(ary)) {
4226 if (gimme == G_ARRAY) {
4228 Copy(AvARRAY(ary), SP + 1, iters, SV*);
4234 if (gimme == G_ARRAY)
4237 if (iters || !pm->op_pmreplroot) {
4247 unlock_condpair(svv)
4251 MAGIC *mg = mg_find((SV*)svv, 'm');
4254 croak("panic: unlock_condpair unlocking non-mutex");
4255 MUTEX_LOCK(MgMUTEXP(mg));
4256 if (MgOWNER(mg) != thr)
4257 croak("panic: unlock_condpair unlocking mutex that we don't own");
4259 COND_SIGNAL(MgOWNERCONDP(mg));
4260 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "0x%lx: unlock 0x%lx\n",
4261 (unsigned long)thr, (unsigned long)svv);)
4262 MUTEX_UNLOCK(MgMUTEXP(mg));
4264 #endif /* USE_THREADS */
4277 mg = condpair_magic(sv);
4278 MUTEX_LOCK(MgMUTEXP(mg));
4279 if (MgOWNER(mg) == thr)
4280 MUTEX_UNLOCK(MgMUTEXP(mg));
4283 COND_WAIT(MgOWNERCONDP(mg), MgMUTEXP(mg));
4285 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "0x%lx: pp_lock lock 0x%lx\n",
4286 (unsigned long)thr, (unsigned long)sv);)
4287 MUTEX_UNLOCK(MgMUTEXP(mg));
4288 SvREFCNT_inc(sv); /* keep alive until magic_mutexfree */
4289 save_destructor(unlock_condpair, sv);
4291 #endif /* USE_THREADS */
4292 if (SvTYPE(retsv) == SVt_PVAV || SvTYPE(retsv) == SVt_PVHV
4293 || SvTYPE(retsv) == SVt_PVCV) {
4294 retsv = refto(retsv);