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))
400 (void)SvREFCNT_inc(sv);
403 sv_upgrade(rv, SVt_RV);
417 if (sv && SvGMAGICAL(sv))
420 if (!sv || !SvROK(sv))
424 pv = sv_reftype(sv,TRUE);
425 PUSHp(pv, strlen(pv));
435 stash = curcop->cop_stash;
437 stash = gv_stashsv(POPs, TRUE);
439 (void)sv_bless(TOPs, stash);
456 switch (elem ? *elem : '\0')
459 if (strEQ(elem, "ARRAY"))
463 if (strEQ(elem, "CODE"))
464 ref = (SV*)GvCVu(gv);
467 if (strEQ(elem, "FILEHANDLE")) /* XXX deprecate in 5.005 */
468 ref = (SV*)GvIOp(gv);
471 if (strEQ(elem, "GLOB"))
475 if (strEQ(elem, "HASH"))
479 if (strEQ(elem, "IO"))
480 ref = (SV*)GvIOp(gv);
483 if (strEQ(elem, "NAME"))
484 sv = newSVpv(GvNAME(gv), GvNAMELEN(gv));
487 if (strEQ(elem, "PACKAGE"))
488 sv = newSVpv(HvNAME(GvSTASH(gv)), 0);
491 if (strEQ(elem, "SCALAR"))
505 /* Pattern matching */
510 register unsigned char *s;
513 register I32 *sfirst;
517 if (sv == lastscream) {
523 SvSCREAM_off(lastscream);
524 SvREFCNT_dec(lastscream);
526 lastscream = SvREFCNT_inc(sv);
529 s = (unsigned char*)(SvPV(sv, len));
533 if (pos > maxscream) {
535 maxscream = pos + 80;
536 New(301, screamfirst, 256, I32);
537 New(302, screamnext, maxscream, I32);
540 maxscream = pos + pos / 4;
541 Renew(screamnext, maxscream, I32);
545 sfirst = screamfirst;
548 if (!sfirst || !snext)
549 DIE("do_study: out of memory");
551 for (ch = 256; ch; --ch)
558 snext[pos] = sfirst[ch] - pos;
565 sv_magic(sv, Nullsv, 'g', Nullch, 0); /* piggyback on m//g magic */
574 if (op->op_flags & OPf_STACKED)
580 TARG = sv_newmortal();
581 PUSHi(do_trans(sv, op));
585 /* Lvalue operators. */
607 SETi(do_chomp(TOPs));
614 register I32 count = 0;
617 count += do_chomp(POPs);
628 if (!sv || !SvANY(sv))
630 switch (SvTYPE(sv)) {
632 if (AvMAX(sv) >= 0 || SvGMAGICAL(sv))
636 if (HvARRAY(sv) || SvGMAGICAL(sv))
640 if (CvROOT(sv) || CvXSUB(sv))
657 if (!op->op_private) {
666 if (SvTHINKFIRST(sv)) {
673 switch (SvTYPE(sv)) {
683 if (cv_const_sv((CV*)sv))
684 warn("Constant subroutine %s undefined",
685 CvANON((CV*)sv) ? "(anonymous)" : GvENAME(CvGV((CV*)sv)));
688 { GV* gv = (GV*)SvREFCNT_inc(CvGV((CV*)sv));
690 CvGV((CV*)sv) = gv; } /* let user-undef'd sub keep its identity */
694 sv_setsv(sv, &sv_undef);
697 if (SvTYPE(sv) >= SVt_PV && SvPVX(sv) && SvLEN(sv)) {
700 SvPV_set(sv, Nullch);
713 if (SvREADONLY(TOPs) || SvTYPE(TOPs) > SVt_PVLV)
715 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) &&
716 SvIVX(TOPs) != IV_MIN)
719 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
730 if (SvREADONLY(TOPs) || SvTYPE(TOPs) > SVt_PVLV)
732 sv_setsv(TARG, TOPs);
733 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) &&
734 SvIVX(TOPs) != IV_MAX)
737 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
751 if(SvREADONLY(TOPs) || SvTYPE(TOPs) > SVt_PVLV)
753 sv_setsv(TARG, TOPs);
754 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) &&
755 SvIVX(TOPs) != IV_MIN)
758 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
767 /* Ordinary operators. */
771 dSP; dATARGET; tryAMAGICbin(pow,opASSIGN);
774 SETn( pow( left, right) );
781 dSP; dATARGET; tryAMAGICbin(mult,opASSIGN);
784 SETn( left * right );
791 dSP; dATARGET; tryAMAGICbin(div,opASSIGN);
796 DIE("Illegal division by zero");
798 /* insure that 20./5. == 4. */
801 if ((double)I_V(left) == left &&
802 (double)I_V(right) == right &&
803 (k = I_V(left)/I_V(right))*I_V(right) == I_V(left)) {
806 value = left / right;
810 value = left / right;
819 dSP; dATARGET; tryAMAGICbin(mod,opASSIGN);
827 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)) {
829 right = (right_neg = (i < 0)) ? -i : i;
833 right = U_V((right_neg = (n < 0)) ? -n : n);
836 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)) {
838 left = (left_neg = (i < 0)) ? -i : i;
842 left = U_V((left_neg = (n < 0)) ? -n : n);
846 DIE("Illegal modulus zero");
849 if ((left_neg != right_neg) && ans)
852 /* XXX may warn: unary minus operator applied to unsigned type */
853 /* could change -foo to be (~foo)+1 instead */
854 if (ans <= -(UV)IV_MAX)
855 sv_setiv(TARG, (IV) -ans);
857 sv_setnv(TARG, -(double)ans);
868 dSP; dATARGET; tryAMAGICbin(repeat,opASSIGN);
870 register I32 count = POPi;
871 if (GIMME == G_ARRAY && op->op_private & OPpREPEAT_DOLIST) {
873 I32 items = SP - MARK;
885 repeatcpy((char*)(MARK + items), (char*)MARK,
886 items * sizeof(SV*), count - 1);
892 else { /* Note: mark already snarfed by pp_list */
897 if (TARG == tmpstr && SvTHINKFIRST(tmpstr)) {
898 if (SvREADONLY(tmpstr) && curcop != &compiling)
899 DIE("Can't x= to readonly value");
903 SvSetSV(TARG, tmpstr);
904 SvPV_force(TARG, len);
909 SvGROW(TARG, (count * len) + 1);
910 repeatcpy(SvPVX(TARG) + len, SvPVX(TARG), len, count - 1);
911 SvCUR(TARG) *= count;
915 (void)SvPOK_only(TARG);
924 dSP; dATARGET; tryAMAGICbin(subtr,opASSIGN);
927 SETn( left - right );
934 dSP; dATARGET; tryAMAGICbin(lshift,opASSIGN);
937 if (op->op_private & HINT_INTEGER) {
953 dSP; dATARGET; tryAMAGICbin(rshift,opASSIGN);
956 if (op->op_private & HINT_INTEGER) {
972 dSP; tryAMAGICbinSET(lt,0);
975 SETs(boolSV(TOPn < value));
982 dSP; tryAMAGICbinSET(gt,0);
985 SETs(boolSV(TOPn > value));
992 dSP; tryAMAGICbinSET(le,0);
995 SETs(boolSV(TOPn <= value));
1002 dSP; tryAMAGICbinSET(ge,0);
1005 SETs(boolSV(TOPn >= value));
1012 dSP; tryAMAGICbinSET(ne,0);
1015 SETs(boolSV(TOPn != value));
1022 dSP; dTARGET; tryAMAGICbin(ncmp,0);
1029 else if (left < right)
1031 else if (left > right)
1044 dSP; tryAMAGICbinSET(slt,0);
1047 int cmp = ((op->op_private & OPpLOCALE)
1048 ? sv_cmp_locale(left, right)
1049 : sv_cmp(left, right));
1050 SETs(boolSV(cmp < 0));
1057 dSP; tryAMAGICbinSET(sgt,0);
1060 int cmp = ((op->op_private & OPpLOCALE)
1061 ? sv_cmp_locale(left, right)
1062 : sv_cmp(left, right));
1063 SETs(boolSV(cmp > 0));
1070 dSP; tryAMAGICbinSET(sle,0);
1073 int cmp = ((op->op_private & OPpLOCALE)
1074 ? sv_cmp_locale(left, right)
1075 : sv_cmp(left, right));
1076 SETs(boolSV(cmp <= 0));
1083 dSP; tryAMAGICbinSET(sge,0);
1086 int cmp = ((op->op_private & OPpLOCALE)
1087 ? sv_cmp_locale(left, right)
1088 : sv_cmp(left, right));
1089 SETs(boolSV(cmp >= 0));
1096 dSP; tryAMAGICbinSET(seq,0);
1099 SETs(boolSV(sv_eq(left, right)));
1106 dSP; tryAMAGICbinSET(sne,0);
1109 SETs(boolSV(!sv_eq(left, right)));
1116 dSP; dTARGET; tryAMAGICbin(scmp,0);
1119 int cmp = ((op->op_private & OPpLOCALE)
1120 ? sv_cmp_locale(left, right)
1121 : sv_cmp(left, right));
1129 dSP; dATARGET; tryAMAGICbin(band,opASSIGN);
1132 if (SvNIOKp(left) || SvNIOKp(right)) {
1133 if (op->op_private & HINT_INTEGER) {
1134 IBW value = SvIV(left) & SvIV(right);
1138 UBW value = SvUV(left) & SvUV(right);
1143 do_vop(op->op_type, TARG, left, right);
1152 dSP; dATARGET; tryAMAGICbin(bxor,opASSIGN);
1155 if (SvNIOKp(left) || SvNIOKp(right)) {
1156 if (op->op_private & HINT_INTEGER) {
1157 IBW value = (USE_LEFT(left) ? SvIV(left) : 0) ^ SvIV(right);
1161 UBW value = (USE_LEFT(left) ? SvUV(left) : 0) ^ SvUV(right);
1166 do_vop(op->op_type, TARG, left, right);
1175 dSP; dATARGET; tryAMAGICbin(bor,opASSIGN);
1178 if (SvNIOKp(left) || SvNIOKp(right)) {
1179 if (op->op_private & HINT_INTEGER) {
1180 IBW value = (USE_LEFT(left) ? SvIV(left) : 0) | SvIV(right);
1184 UBW value = (USE_LEFT(left) ? SvUV(left) : 0) | SvUV(right);
1189 do_vop(op->op_type, TARG, left, right);
1198 dSP; dTARGET; tryAMAGICun(neg);
1203 if (SvIOKp(sv) && !SvNOKp(sv) && !SvPOKp(sv) && SvIVX(sv) != IV_MIN)
1205 else if (SvNIOKp(sv))
1207 else if (SvPOKp(sv)) {
1209 char *s = SvPV(sv, len);
1210 if (isIDFIRST(*s)) {
1211 sv_setpvn(TARG, "-", 1);
1214 else if (*s == '+' || *s == '-') {
1216 *SvPV_force(TARG, len) = *s == '-' ? '+' : '-';
1219 sv_setnv(TARG, -SvNV(sv));
1231 dSP; tryAMAGICunSET(not);
1232 #endif /* OVERLOAD */
1233 *stack_sp = boolSV(!SvTRUE(*stack_sp));
1239 dSP; dTARGET; tryAMAGICun(compl);
1243 if (op->op_private & HINT_INTEGER) {
1244 IBW value = ~SvIV(sv);
1248 UBW value = ~SvUV(sv);
1253 register char *tmps;
1254 register long *tmpl;
1259 tmps = SvPV_force(TARG, len);
1262 for ( ; anum && (unsigned long)tmps % sizeof(long); anum--, tmps++)
1265 for ( ; anum >= sizeof(long); anum -= sizeof(long), tmpl++)
1269 for ( ; anum > 0; anum--, tmps++)
1278 /* integer versions of some of the above */
1282 dSP; dATARGET; tryAMAGICbin(mult,opASSIGN);
1285 SETi( left * right );
1292 dSP; dATARGET; tryAMAGICbin(div,opASSIGN);
1296 DIE("Illegal division by zero");
1297 value = POPi / value;
1305 dSP; dATARGET; tryAMAGICbin(mod,opASSIGN);
1309 DIE("Illegal modulus zero");
1310 SETi( left % right );
1317 dSP; dATARGET; tryAMAGICbin(add,opASSIGN);
1320 SETi( left + right );
1327 dSP; dATARGET; tryAMAGICbin(subtr,opASSIGN);
1330 SETi( left - right );
1337 dSP; tryAMAGICbinSET(lt,0);
1340 SETs(boolSV(left < right));
1347 dSP; tryAMAGICbinSET(gt,0);
1350 SETs(boolSV(left > right));
1357 dSP; tryAMAGICbinSET(le,0);
1360 SETs(boolSV(left <= right));
1367 dSP; tryAMAGICbinSET(ge,0);
1370 SETs(boolSV(left >= right));
1377 dSP; tryAMAGICbinSET(eq,0);
1380 SETs(boolSV(left == right));
1387 dSP; tryAMAGICbinSET(ne,0);
1390 SETs(boolSV(left != right));
1397 dSP; dTARGET; tryAMAGICbin(ncmp,0);
1404 else if (left < right)
1415 dSP; dTARGET; tryAMAGICun(neg);
1420 /* High falutin' math. */
1424 dSP; dTARGET; tryAMAGICbin(atan2,0);
1427 SETn(atan2(left, right));
1434 dSP; dTARGET; tryAMAGICun(sin);
1446 dSP; dTARGET; tryAMAGICun(cos);
1466 if (!srand_called) {
1467 (void)srand((unsigned)seed());
1468 srand_called = TRUE;
1471 value = rand() * value / 2147483648.0;
1474 value = rand() * value / 65536.0;
1477 value = rand() * value / 32768.0;
1479 value = rand() * value / (double)(((unsigned long)1) << RANDBITS);
1495 (void)srand((unsigned)anum);
1496 srand_called = TRUE;
1505 * This is really just a quick hack which grabs various garbage
1506 * values. It really should be a real hash algorithm which
1507 * spreads the effect of every input bit onto every output bit,
1508 * if someone who knows about such tings would bother to write it.
1509 * Might be a good idea to add that function to CORE as well.
1510 * No numbers below come from careful analysis or anyting here,
1511 * except they are primes and SEED_C1 > 1E6 to get a full-width
1512 * value from (tv_sec * SEED_C1 + tv_usec). The multipliers should
1513 * probably be bigger too.
1516 # define SEED_C1 1000003
1517 #define SEED_C4 73819
1519 # define SEED_C1 25747
1520 #define SEED_C4 20639
1524 #define SEED_C5 26107
1529 # include <starlet.h>
1530 /* when[] = (low 32 bits, high 32 bits) of time since epoch
1531 * in 100-ns units, typically incremented ever 10 ms. */
1532 unsigned int when[2];
1533 _ckvmssts(sys$gettim(when));
1534 u = (U32)SEED_C1 * when[0] + (U32)SEED_C2 * when[1];
1536 # ifdef HAS_GETTIMEOFDAY
1537 struct timeval when;
1538 gettimeofday(&when,(struct timezone *) 0);
1539 u = (U32)SEED_C1 * when.tv_sec + (U32)SEED_C2 * when.tv_usec;
1543 u = (U32)SEED_C1 * when;
1546 u += SEED_C3 * (U32)getpid();
1547 u += SEED_C4 * (U32)(UV)stack_sp;
1548 #ifndef PLAN9 /* XXX Plan9 assembler chokes on this; fix needed */
1549 u += SEED_C5 * (U32)(UV)&when;
1556 dSP; dTARGET; tryAMAGICun(exp);
1568 dSP; dTARGET; tryAMAGICun(log);
1573 SET_NUMERIC_STANDARD();
1574 DIE("Can't take log of %g", value);
1584 dSP; dTARGET; tryAMAGICun(sqrt);
1589 SET_NUMERIC_STANDARD();
1590 DIE("Can't take sqrt of %g", value);
1592 value = sqrt(value);
1602 double value = TOPn;
1605 if (SvIOKp(TOPs) && !SvNOKp(TOPs) && !SvPOKp(TOPs)) {
1611 (void)modf(value, &value);
1613 (void)modf(-value, &value);
1628 dSP; dTARGET; tryAMAGICun(abs);
1630 double value = TOPn;
1633 if (SvIOKp(TOPs) && !SvNOKp(TOPs) && !SvPOKp(TOPs) &&
1634 (iv = SvIVX(TOPs)) != IV_MIN) {
1655 XPUSHu(scan_hex(tmps, 99, &argtype));
1667 while (*tmps && isSPACE(*tmps))
1672 value = scan_hex(++tmps, 99, &argtype);
1674 value = scan_oct(tmps, 99, &argtype);
1684 SETi( sv_len(TOPs) );
1697 I32 lvalue = op->op_flags & OPf_MOD;
1699 I32 arybase = curcop->cop_arybase;
1705 tmps = SvPV(sv, curlen);
1706 if (pos >= arybase) {
1724 else if (len >= 0) {
1726 if (rem > (I32)curlen)
1740 if (dowarn || lvalue)
1741 warn("substr outside of string");
1746 sv_setpvn(TARG, tmps, rem);
1747 if (lvalue) { /* it's an lvalue! */
1748 if (!SvGMAGICAL(sv)) {
1752 warn("Attempt to use reference as lvalue in substr");
1754 if (SvOK(sv)) /* is it defined ? */
1755 (void)SvPOK_only(sv);
1757 sv_setpvn(sv,"",0); /* avoid lexical reincarnation */
1760 if (SvTYPE(TARG) < SVt_PVLV) {
1761 sv_upgrade(TARG, SVt_PVLV);
1762 sv_magic(TARG, Nullsv, 'x', Nullch, 0);
1767 LvTARGOFF(TARG) = pos;
1768 LvTARGLEN(TARG) = rem;
1771 PUSHs(TARG); /* avoid SvSETMAGIC here */
1778 register I32 size = POPi;
1779 register I32 offset = POPi;
1780 register SV *src = POPs;
1781 I32 lvalue = op->op_flags & OPf_MOD;
1783 unsigned char *s = (unsigned char*)SvPV(src, srclen);
1784 unsigned long retnum;
1787 offset *= size; /* turn into bit offset */
1788 len = (offset + size + 7) / 8;
1789 if (offset < 0 || size < 1)
1792 if (lvalue) { /* it's an lvalue! */
1793 if (SvTYPE(TARG) < SVt_PVLV) {
1794 sv_upgrade(TARG, SVt_PVLV);
1795 sv_magic(TARG, Nullsv, 'v', Nullch, 0);
1800 LvTARGOFF(TARG) = offset;
1801 LvTARGLEN(TARG) = size;
1809 if (offset >= srclen)
1812 retnum = (unsigned long) s[offset] << 8;
1814 else if (size == 32) {
1815 if (offset >= srclen)
1817 else if (offset + 1 >= srclen)
1818 retnum = (unsigned long) s[offset] << 24;
1819 else if (offset + 2 >= srclen)
1820 retnum = ((unsigned long) s[offset] << 24) +
1821 ((unsigned long) s[offset + 1] << 16);
1823 retnum = ((unsigned long) s[offset] << 24) +
1824 ((unsigned long) s[offset + 1] << 16) +
1825 (s[offset + 2] << 8);
1830 retnum = (s[offset >> 3] >> (offset & 7)) & ((1 << size) - 1);
1835 else if (size == 16)
1836 retnum = ((unsigned long) s[offset] << 8) + s[offset+1];
1837 else if (size == 32)
1838 retnum = ((unsigned long) s[offset] << 24) +
1839 ((unsigned long) s[offset + 1] << 16) +
1840 (s[offset + 2] << 8) + s[offset+3];
1844 sv_setiv(TARG, (IV)retnum);
1859 I32 arybase = curcop->cop_arybase;
1864 offset = POPi - arybase;
1867 tmps = SvPV(big, biglen);
1870 else if (offset > biglen)
1872 if (!(tmps2 = fbm_instr((unsigned char*)tmps + offset,
1873 (unsigned char*)tmps + biglen, little)))
1874 retval = -1 + arybase;
1876 retval = tmps2 - tmps + arybase;
1893 I32 arybase = curcop->cop_arybase;
1899 tmps2 = SvPV(little, llen);
1900 tmps = SvPV(big, blen);
1904 offset = SvIV(offstr) - arybase + llen;
1907 else if (offset > blen)
1909 if (!(tmps2 = rninstr(tmps, tmps + offset,
1910 tmps2, tmps2 + llen)))
1911 retval = -1 + arybase;
1913 retval = tmps2 - tmps + arybase;
1920 dSP; dMARK; dORIGMARK; dTARGET;
1921 #ifdef USE_LOCALE_NUMERIC
1922 if (op->op_private & OPpLOCALE)
1923 SET_NUMERIC_LOCAL();
1925 SET_NUMERIC_STANDARD();
1927 do_sprintf(TARG, SP-MARK, MARK+1);
1928 TAINT_IF(SvTAINTED(TARG));
1942 value = (I32) (*tmps & 255);
1947 value = (I32) (anum & 255);
1958 (void)SvUPGRADE(TARG,SVt_PV);
1964 (void)SvPOK_only(TARG);
1971 dSP; dTARGET; dPOPTOPssrl;
1973 char *tmps = SvPV(left, na);
1975 sv_setpv(TARG, fcrypt(tmps, SvPV(right, na)));
1977 sv_setpv(TARG, crypt(tmps, SvPV(right, na)));
1981 "The crypt() function is unimplemented due to excessive paranoia.");
1993 if (!SvPADTMP(sv)) {
1999 s = SvPV_force(sv, na);
2001 if (op->op_private & OPpLOCALE) {
2004 *s = toUPPER_LC(*s);
2019 if (!SvPADTMP(sv)) {
2025 s = SvPV_force(sv, na);
2027 if (op->op_private & OPpLOCALE) {
2030 *s = toLOWER_LC(*s);
2047 if (!SvPADTMP(sv)) {
2054 s = SvPV_force(sv, len);
2056 register char *send = s + len;
2058 if (op->op_private & OPpLOCALE) {
2061 for (; s < send; s++)
2062 *s = toUPPER_LC(*s);
2065 for (; s < send; s++)
2079 if (!SvPADTMP(sv)) {
2086 s = SvPV_force(sv, len);
2088 register char *send = s + len;
2090 if (op->op_private & OPpLOCALE) {
2093 for (; s < send; s++)
2094 *s = toLOWER_LC(*s);
2097 for (; s < send; s++)
2109 register char *s = SvPV(sv,len);
2113 (void)SvUPGRADE(TARG, SVt_PV);
2114 SvGROW(TARG, (len * 2) + 1);
2122 SvCUR_set(TARG, d - SvPVX(TARG));
2123 (void)SvPOK_only(TARG);
2126 sv_setpvn(TARG, s, len);
2135 dSP; dMARK; dORIGMARK;
2137 register AV* av = (AV*)POPs;
2138 register I32 lval = op->op_flags & OPf_MOD;
2139 I32 arybase = curcop->cop_arybase;
2142 if (SvTYPE(av) == SVt_PVAV) {
2143 if (lval && op->op_private & OPpLVAL_INTRO) {
2145 for (svp = mark + 1; svp <= sp; svp++) {
2150 if (max > AvMAX(av))
2153 while (++MARK <= SP) {
2154 elem = SvIVx(*MARK);
2158 svp = av_fetch(av, elem, lval);
2160 if (!svp || *svp == &sv_undef)
2161 DIE(no_aelem, elem);
2162 if (op->op_private & OPpLVAL_INTRO)
2165 *MARK = svp ? *svp : &sv_undef;
2168 if (GIMME != G_ARRAY) {
2176 /* Associative arrays. */
2181 HV *hash = (HV*)POPs;
2183 I32 gimme = GIMME_V;
2184 I32 realhv = (SvTYPE(hash) == SVt_PVHV);
2187 /* might clobber stack_sp */
2188 entry = realhv ? hv_iternext(hash) : avhv_iternext((AV*)hash);
2193 PUSHs(hv_iterkeysv(entry)); /* won't clobber stack_sp */
2194 if (gimme == G_ARRAY) {
2196 /* might clobber stack_sp */
2197 sv_setsv(TARG, realhv ?
2198 hv_iterval(hash, entry) : avhv_iterval((AV*)hash, entry));
2203 else if (gimme == G_SCALAR)
2222 I32 gimme = GIMME_V;
2223 I32 discard = (gimme == G_VOID) ? G_DISCARD : 0;
2227 if (op->op_private & OPpSLICE) {
2231 hvtype = SvTYPE(hv);
2232 while (++MARK <= SP) {
2233 if (hvtype == SVt_PVHV)
2234 sv = hv_delete_ent(hv, *MARK, discard, 0);
2235 else if (hvtype == SVt_PVAV)
2236 sv = avhv_delete_ent((AV*)hv, *MARK, discard, 0);
2238 DIE("Not a HASH reference");
2239 *MARK = sv ? sv : &sv_undef;
2243 else if (gimme == G_SCALAR) {
2252 if (SvTYPE(hv) == SVt_PVHV)
2253 sv = hv_delete_ent(hv, keysv, discard, 0);
2254 else if (SvTYPE(hv) == SVt_PVAV)
2255 sv = avhv_delete_ent((AV*)hv, keysv, discard, 0);
2257 DIE("Not a HASH reference");
2271 if (SvTYPE(hv) == SVt_PVHV) {
2272 if (hv_exists_ent(hv, tmpsv, 0))
2274 } else if (SvTYPE(hv) == SVt_PVAV) {
2275 if (avhv_exists_ent((AV*)hv, tmpsv, 0))
2278 DIE("Not a HASH reference");
2285 dSP; dMARK; dORIGMARK;
2287 register HV *hv = (HV*)POPs;
2288 register I32 lval = op->op_flags & OPf_MOD;
2289 I32 realhv = (SvTYPE(hv) == SVt_PVHV);
2291 if (realhv || SvTYPE(hv) == SVt_PVAV) {
2292 while (++MARK <= SP) {
2296 he = hv_fetch_ent(hv, keysv, lval, 0);
2297 svp = he ? &HeVAL(he) : 0;
2299 svp = avhv_fetch_ent((AV*)hv, keysv, lval, 0);
2302 if (!he || HeVAL(he) == &sv_undef)
2303 DIE(no_helem, SvPV(keysv, na));
2304 if (op->op_private & OPpLVAL_INTRO)
2305 save_svref(&HeVAL(he));
2307 *MARK = he ? HeVAL(he) : &sv_undef;
2310 if (GIMME != G_ARRAY) {
2318 /* List operators. */
2323 if (GIMME != G_ARRAY) {
2325 *MARK = *SP; /* unwanted list, return last item */
2336 SV **lastrelem = stack_sp;
2337 SV **lastlelem = stack_base + POPMARK;
2338 SV **firstlelem = stack_base + POPMARK + 1;
2339 register SV **firstrelem = lastlelem + 1;
2340 I32 arybase = curcop->cop_arybase;
2341 I32 lval = op->op_flags & OPf_MOD;
2342 I32 is_something_there = lval;
2344 register I32 max = lastrelem - lastlelem;
2345 register SV **lelem;
2348 if (GIMME != G_ARRAY) {
2349 ix = SvIVx(*lastlelem);
2354 if (ix < 0 || ix >= max)
2355 *firstlelem = &sv_undef;
2357 *firstlelem = firstrelem[ix];
2363 SP = firstlelem - 1;
2367 for (lelem = firstlelem; lelem <= lastlelem; lelem++) {
2373 else if (!(*lelem = firstrelem[ix]))
2378 if (ix >= max || !(*lelem = firstrelem[ix]))
2381 if (!is_something_there && (SvOK(*lelem) || SvGMAGICAL(*lelem)))
2382 is_something_there = TRUE;
2384 if (is_something_there)
2387 SP = firstlelem - 1;
2393 dSP; dMARK; dORIGMARK;
2394 I32 items = SP - MARK;
2395 SV *av = sv_2mortal((SV*)av_make(items, MARK+1));
2396 SP = ORIGMARK; /* av_make() might realloc stack_sp */
2403 dSP; dMARK; dORIGMARK;
2404 HV* hv = (HV*)sv_2mortal((SV*)newHV());
2408 SV *val = NEWSV(46, 0);
2410 sv_setsv(val, *++MARK);
2412 warn("Odd number of elements in hash list");
2413 (void)hv_store_ent(hv,key,val,0);
2422 dSP; dMARK; dORIGMARK;
2423 register AV *ary = (AV*)*++MARK;
2427 register I32 offset;
2428 register I32 length;
2437 offset = i = SvIVx(*MARK);
2439 offset += AvFILL(ary) + 1;
2441 offset -= curcop->cop_arybase;
2445 length = SvIVx(*MARK++);
2450 length = AvMAX(ary) + 1; /* close enough to infinity */
2454 length = AvMAX(ary) + 1;
2456 if (offset > AvFILL(ary) + 1)
2457 offset = AvFILL(ary) + 1;
2458 after = AvFILL(ary) + 1 - (offset + length);
2459 if (after < 0) { /* not that much array */
2460 length += after; /* offset+length now in array */
2466 /* At this point, MARK .. SP-1 is our new LIST */
2469 diff = newlen - length;
2470 if (newlen && !AvREAL(ary)) {
2474 assert(AvREAL(ary)); /* would leak, so croak */
2477 if (diff < 0) { /* shrinking the area */
2479 New(451, tmparyval, newlen, SV*); /* so remember insertion */
2480 Copy(MARK, tmparyval, newlen, SV*);
2483 MARK = ORIGMARK + 1;
2484 if (GIMME == G_ARRAY) { /* copy return vals to stack */
2485 MEXTEND(MARK, length);
2486 Copy(AvARRAY(ary)+offset, MARK, length, SV*);
2488 EXTEND_MORTAL(length);
2489 for (i = length, dst = MARK; i; i--) {
2490 if (!SvIMMORTAL(*dst))
2491 sv_2mortal(*dst); /* free them eventualy */
2498 *MARK = AvARRAY(ary)[offset+length-1];
2500 if (!SvIMMORTAL(*MARK))
2502 for (i = length - 1, dst = &AvARRAY(ary)[offset]; i > 0; i--)
2503 SvREFCNT_dec(*dst++); /* free them now */
2506 AvFILL(ary) += diff;
2508 /* pull up or down? */
2510 if (offset < after) { /* easier to pull up */
2511 if (offset) { /* esp. if nothing to pull */
2512 src = &AvARRAY(ary)[offset-1];
2513 dst = src - diff; /* diff is negative */
2514 for (i = offset; i > 0; i--) /* can't trust Copy */
2518 SvPVX(ary) = (char*)(AvARRAY(ary) - diff); /* diff is negative */
2522 if (after) { /* anything to pull down? */
2523 src = AvARRAY(ary) + offset + length;
2524 dst = src + diff; /* diff is negative */
2525 Move(src, dst, after, SV*);
2527 dst = &AvARRAY(ary)[AvFILL(ary)+1];
2528 /* avoid later double free */
2532 dst[--i] = &sv_undef;
2535 for (src = tmparyval, dst = AvARRAY(ary) + offset;
2537 *dst = NEWSV(46, 0);
2538 sv_setsv(*dst++, *src++);
2540 Safefree(tmparyval);
2543 else { /* no, expanding (or same) */
2545 New(452, tmparyval, length, SV*); /* so remember deletion */
2546 Copy(AvARRAY(ary)+offset, tmparyval, length, SV*);
2549 if (diff > 0) { /* expanding */
2551 /* push up or down? */
2553 if (offset < after && diff <= AvARRAY(ary) - AvALLOC(ary)) {
2557 Move(src, dst, offset, SV*);
2559 SvPVX(ary) = (char*)(AvARRAY(ary) - diff);/* diff is positive */
2561 AvFILL(ary) += diff;
2564 if (AvFILL(ary) + diff >= AvMAX(ary)) /* oh, well */
2565 av_extend(ary, AvFILL(ary) + diff);
2566 AvFILL(ary) += diff;
2569 dst = AvARRAY(ary) + AvFILL(ary);
2571 for (i = after; i; i--) {
2578 for (src = MARK, dst = AvARRAY(ary) + offset; newlen; newlen--) {
2579 *dst = NEWSV(46, 0);
2580 sv_setsv(*dst++, *src++);
2582 MARK = ORIGMARK + 1;
2583 if (GIMME == G_ARRAY) { /* copy return vals to stack */
2585 Copy(tmparyval, MARK, length, SV*);
2587 EXTEND_MORTAL(length);
2588 for (i = length, dst = MARK; i; i--) {
2589 if (!SvIMMORTAL(*dst))
2590 sv_2mortal(*dst); /* free them eventualy */
2594 Safefree(tmparyval);
2598 else if (length--) {
2599 *MARK = tmparyval[length];
2601 if (!SvIMMORTAL(*MARK))
2603 while (length-- > 0)
2604 SvREFCNT_dec(tmparyval[length]);
2606 Safefree(tmparyval);
2617 dSP; dMARK; dORIGMARK; dTARGET;
2618 register AV *ary = (AV*)*++MARK;
2619 register SV *sv = &sv_undef;
2621 for (++MARK; MARK <= SP; MARK++) {
2624 sv_setsv(sv, *MARK);
2628 PUSHi( AvFILL(ary) + 1 );
2636 SV *sv = av_pop(av);
2637 if (!SvIMMORTAL(sv) && AvREAL(av))
2638 (void)sv_2mortal(sv);
2647 SV *sv = av_shift(av);
2651 if (!SvIMMORTAL(sv) && AvREAL(av))
2652 (void)sv_2mortal(sv);
2659 dSP; dMARK; dORIGMARK; dTARGET;
2660 register AV *ary = (AV*)*++MARK;
2664 av_unshift(ary, SP - MARK);
2667 sv_setsv(sv, *++MARK);
2668 (void)av_store(ary, i++, sv);
2672 PUSHi( AvFILL(ary) + 1 );
2682 if (GIMME == G_ARRAY) {
2693 register char *down;
2699 do_join(TARG, &sv_no, MARK, SP);
2701 sv_setsv(TARG, (SP > MARK) ? *SP : GvSV(defgv));
2702 up = SvPV_force(TARG, len);
2704 down = SvPVX(TARG) + len - 1;
2710 (void)SvPOK_only(TARG);
2724 char *s = SvPV(sv, len);
2728 if (!strnEQ(s, "0000", 4)) { /* need to grow sv */
2729 SV *new = newSVpv("0000000000", 10);
2732 SvREFCNT_dec(sv); /* free old sv */
2737 while (!*t) /* trailing '\0'? */
2740 i = ((*t - '0') << 7) + m;
2741 *(t--) = '0' + (i % 10);
2747 /* Explosives and implosives. */
2754 I32 gimme = GIMME_V;
2758 register char *pat = SvPV(left, llen);
2759 register char *s = SvPV(right, rlen);
2760 char *strend = s + rlen;
2762 register char *patend = pat + llen;
2767 /* These must not be in registers: */
2778 unsigned Quad_t auquad;
2784 register U32 culong;
2786 static char* bitcount = 0;
2789 if (gimme != G_ARRAY) { /* arrange to do first one only */
2791 for (patend = pat; !isALPHA(*patend) || *patend == 'x'; patend++) ;
2792 if (strchr("aAbBhHP", *patend) || *pat == '%') {
2794 while (isDIGIT(*patend) || *patend == '*')
2800 while (pat < patend) {
2802 datumtype = *pat++ & 0xFF;
2803 if (isSPACE(datumtype))
2807 else if (*pat == '*') {
2808 len = strend - strbeg; /* long enough */
2811 else if (isDIGIT(*pat)) {
2813 while (isDIGIT(*pat))
2814 len = (len * 10) + (*pat++ - '0');
2817 len = (datumtype != '@');
2820 croak("Invalid type in unpack: '%c'", (int)datumtype);
2821 case ',': /* grandfather in commas but with a warning */
2822 if (commas++ == 0 && dowarn)
2823 warn("Invalid type in unpack: '%c'", (int)datumtype);
2826 if (len == 1 && pat[-1] != '1')
2835 if (len > strend - strbeg)
2836 DIE("@ outside of string");
2840 if (len > s - strbeg)
2841 DIE("X outside of string");
2845 if (len > strend - s)
2846 DIE("x outside of string");
2851 if (len > strend - s)
2854 goto uchar_checksum;
2855 sv = NEWSV(35, len);
2856 sv_setpvn(sv, s, len);
2858 if (datumtype == 'A') {
2859 aptr = s; /* borrow register */
2860 s = SvPVX(sv) + len - 1;
2861 while (s >= SvPVX(sv) && (!*s || isSPACE(*s)))
2864 SvCUR_set(sv, s - SvPVX(sv));
2865 s = aptr; /* unborrow register */
2867 XPUSHs(sv_2mortal(sv));
2871 if (pat[-1] == '*' || len > (strend - s) * 8)
2872 len = (strend - s) * 8;
2875 Newz(601, bitcount, 256, char);
2876 for (bits = 1; bits < 256; bits++) {
2877 if (bits & 1) bitcount[bits]++;
2878 if (bits & 2) bitcount[bits]++;
2879 if (bits & 4) bitcount[bits]++;
2880 if (bits & 8) bitcount[bits]++;
2881 if (bits & 16) bitcount[bits]++;
2882 if (bits & 32) bitcount[bits]++;
2883 if (bits & 64) bitcount[bits]++;
2884 if (bits & 128) bitcount[bits]++;
2888 culong += bitcount[*(unsigned char*)s++];
2893 if (datumtype == 'b') {
2895 if (bits & 1) culong++;
2901 if (bits & 128) culong++;
2908 sv = NEWSV(35, len + 1);
2911 aptr = pat; /* borrow register */
2913 if (datumtype == 'b') {
2915 for (len = 0; len < aint; len++) {
2916 if (len & 7) /*SUPPRESS 595*/
2920 *pat++ = '0' + (bits & 1);
2925 for (len = 0; len < aint; len++) {
2930 *pat++ = '0' + ((bits & 128) != 0);
2934 pat = aptr; /* unborrow register */
2935 XPUSHs(sv_2mortal(sv));
2939 if (pat[-1] == '*' || len > (strend - s) * 2)
2940 len = (strend - s) * 2;
2941 sv = NEWSV(35, len + 1);
2944 aptr = pat; /* borrow register */
2946 if (datumtype == 'h') {
2948 for (len = 0; len < aint; len++) {
2953 *pat++ = hexdigit[bits & 15];
2958 for (len = 0; len < aint; len++) {
2963 *pat++ = hexdigit[(bits >> 4) & 15];
2967 pat = aptr; /* unborrow register */
2968 XPUSHs(sv_2mortal(sv));
2971 if (len > strend - s)
2976 if (aint >= 128) /* fake up signed chars */
2986 if (aint >= 128) /* fake up signed chars */
2989 sv_setiv(sv, (IV)aint);
2990 PUSHs(sv_2mortal(sv));
2995 if (len > strend - s)
3010 sv_setiv(sv, (IV)auint);
3011 PUSHs(sv_2mortal(sv));
3016 along = (strend - s) / SIZE16;
3033 sv_setiv(sv, (IV)ashort);
3034 PUSHs(sv_2mortal(sv));
3041 along = (strend - s) / SIZE16;
3046 COPY16(s, &aushort);
3049 if (datumtype == 'n')
3050 aushort = ntohs(aushort);
3053 if (datumtype == 'v')
3054 aushort = vtohs(aushort);
3063 COPY16(s, &aushort);
3067 if (datumtype == 'n')
3068 aushort = ntohs(aushort);
3071 if (datumtype == 'v')
3072 aushort = vtohs(aushort);
3074 sv_setiv(sv, (IV)aushort);
3075 PUSHs(sv_2mortal(sv));
3080 along = (strend - s) / sizeof(int);
3085 Copy(s, &aint, 1, int);
3088 cdouble += (double)aint;
3097 Copy(s, &aint, 1, int);
3100 sv_setiv(sv, (IV)aint);
3101 PUSHs(sv_2mortal(sv));
3106 along = (strend - s) / sizeof(unsigned int);
3111 Copy(s, &auint, 1, unsigned int);
3112 s += sizeof(unsigned int);
3114 cdouble += (double)auint;
3123 Copy(s, &auint, 1, unsigned int);
3124 s += sizeof(unsigned int);
3126 sv_setuv(sv, (UV)auint);
3127 PUSHs(sv_2mortal(sv));
3132 along = (strend - s) / SIZE32;
3140 cdouble += (double)along;
3152 sv_setiv(sv, (IV)along);
3153 PUSHs(sv_2mortal(sv));
3160 along = (strend - s) / SIZE32;
3168 if (datumtype == 'N')
3169 aulong = ntohl(aulong);
3172 if (datumtype == 'V')
3173 aulong = vtohl(aulong);
3176 cdouble += (double)aulong;
3188 if (datumtype == 'N')
3189 aulong = ntohl(aulong);
3192 if (datumtype == 'V')
3193 aulong = vtohl(aulong);
3196 sv_setuv(sv, (UV)aulong);
3197 PUSHs(sv_2mortal(sv));
3202 along = (strend - s) / sizeof(char*);
3208 if (sizeof(char*) > strend - s)
3211 Copy(s, &aptr, 1, char*);
3217 PUSHs(sv_2mortal(sv));
3227 while ((len > 0) && (s < strend)) {
3228 auv = (auv << 7) | (*s & 0x7f);
3229 if (!(*s++ & 0x80)) {
3233 PUSHs(sv_2mortal(sv));
3237 else if (++bytes >= sizeof(UV)) { /* promote to string */
3240 sv = newSVpvf("%.*Vu", (int)TYPE_DIGITS(UV), auv);
3241 while (s < strend) {
3242 sv = mul128(sv, *s & 0x7f);
3243 if (!(*s++ & 0x80)) {
3252 PUSHs(sv_2mortal(sv));
3257 if ((s >= strend) && bytes)
3258 croak("Unterminated compressed integer");
3263 if (sizeof(char*) > strend - s)
3266 Copy(s, &aptr, 1, char*);
3271 sv_setpvn(sv, aptr, len);
3272 PUSHs(sv_2mortal(sv));
3279 if (s + sizeof(Quad_t) > strend)
3282 Copy(s, &aquad, 1, Quad_t);
3283 s += sizeof(Quad_t);
3286 if (aquad >= IV_MIN && aquad <= IV_MAX)
3287 sv_setiv(sv, (IV)aquad);
3289 sv_setnv(sv, (double)aquad);
3290 PUSHs(sv_2mortal(sv));
3297 if (s + sizeof(unsigned Quad_t) > strend)
3300 Copy(s, &auquad, 1, unsigned Quad_t);
3301 s += sizeof(unsigned Quad_t);
3304 if (aquad <= UV_MAX)
3305 sv_setuv(sv, (UV)auquad);
3307 sv_setnv(sv, (double)auquad);
3308 PUSHs(sv_2mortal(sv));
3312 /* float and double added gnb@melba.bby.oz.au 22/11/89 */
3315 along = (strend - s) / sizeof(float);
3320 Copy(s, &afloat, 1, float);
3329 Copy(s, &afloat, 1, float);
3332 sv_setnv(sv, (double)afloat);
3333 PUSHs(sv_2mortal(sv));
3339 along = (strend - s) / sizeof(double);
3344 Copy(s, &adouble, 1, double);
3345 s += sizeof(double);
3353 Copy(s, &adouble, 1, double);
3354 s += sizeof(double);
3356 sv_setnv(sv, (double)adouble);
3357 PUSHs(sv_2mortal(sv));
3362 along = (strend - s) * 3 / 4;
3363 sv = NEWSV(42, along);
3366 while (s < strend && *s > ' ' && *s < 'a') {
3371 len = (*s++ - ' ') & 077;
3373 if (s < strend && *s >= ' ')
3374 a = (*s++ - ' ') & 077;
3377 if (s < strend && *s >= ' ')
3378 b = (*s++ - ' ') & 077;
3381 if (s < strend && *s >= ' ')
3382 c = (*s++ - ' ') & 077;
3385 if (s < strend && *s >= ' ')
3386 d = (*s++ - ' ') & 077;
3389 hunk[0] = a << 2 | b >> 4;
3390 hunk[1] = b << 4 | c >> 2;
3391 hunk[2] = c << 6 | d;
3392 sv_catpvn(sv, hunk, len > 3 ? 3 : len);
3397 else if (s[1] == '\n') /* possible checksum byte */
3400 XPUSHs(sv_2mortal(sv));
3405 if (strchr("fFdD", datumtype) ||
3406 (checksum > 32 && strchr("iIlLN", datumtype)) ) {
3410 while (checksum >= 16) {
3414 while (checksum >= 4) {
3420 along = (1 << checksum) - 1;
3421 while (cdouble < 0.0)
3423 cdouble = modf(cdouble / adouble, &trouble) * adouble;
3424 sv_setnv(sv, cdouble);
3427 if (checksum < 32) {
3428 aulong = (1 << checksum) - 1;
3431 sv_setuv(sv, (UV)culong);
3433 XPUSHs(sv_2mortal(sv));
3437 if (sp == oldsp && gimme == G_SCALAR)
3443 doencodes(sv, s, len)
3451 sv_catpvn(sv, hunk, 1);
3454 hunk[0] = ' ' + (077 & (*s >> 2));
3455 hunk[1] = ' ' + (077 & ((*s << 4) & 060 | (s[1] >> 4) & 017));
3456 hunk[2] = ' ' + (077 & ((s[1] << 2) & 074 | (s[2] >> 6) & 03));
3457 hunk[3] = ' ' + (077 & (s[2] & 077));
3458 sv_catpvn(sv, hunk, 4);
3462 for (s = SvPVX(sv); *s; s++) {
3466 sv_catpvn(sv, "\n", 1);
3474 SV *result = newSVpv("", l);
3475 char *result_c = SvPV(result, na); /* convenience */
3476 char *out = result_c;
3486 SvREFCNT_dec(result);
3509 SvREFCNT_dec(result);
3515 SvCUR_set(result, out - result_c);
3521 SV *pnum; /* must be '\0' terminated */
3525 char *s = SvPV(pnum, len);
3534 i = m * 10 + (*t - '0');
3536 r = (i >> 7); /* r < 10 */
3543 SvCUR_set(pnum, (STRLEN) (t - s));
3550 dSP; dMARK; dORIGMARK; dTARGET;
3551 register SV *cat = TARG;
3554 register char *pat = SvPVx(*++MARK, fromlen);
3555 register char *patend = pat + fromlen;
3560 static char null10[] = {0,0,0,0,0,0,0,0,0,0};
3561 static char *space10 = " ";
3563 /* These must not be in registers: */
3572 unsigned Quad_t auquad;
3581 sv_setpvn(cat, "", 0);
3582 while (pat < patend) {
3583 #define NEXTFROM (items-- > 0 ? *MARK++ : &sv_no)
3584 datumtype = *pat++ & 0xFF;
3585 if (isSPACE(datumtype))
3588 len = strchr("@Xxu", datumtype) ? 0 : items;
3591 else if (isDIGIT(*pat)) {
3593 while (isDIGIT(*pat))
3594 len = (len * 10) + (*pat++ - '0');
3600 croak("Invalid type in pack: '%c'", (int)datumtype);
3601 case ',': /* grandfather in commas but with a warning */
3602 if (commas++ == 0 && dowarn)
3603 warn("Invalid type in pack: '%c'", (int)datumtype);
3606 DIE("%% may only be used in unpack");
3617 if (SvCUR(cat) < len)
3618 DIE("X outside of string");
3625 sv_catpvn(cat, null10, 10);
3628 sv_catpvn(cat, null10, len);
3633 aptr = SvPV(fromstr, fromlen);
3637 sv_catpvn(cat, aptr, len);
3639 sv_catpvn(cat, aptr, fromlen);
3641 if (datumtype == 'A') {
3643 sv_catpvn(cat, space10, 10);
3646 sv_catpvn(cat, space10, len);
3650 sv_catpvn(cat, null10, 10);
3653 sv_catpvn(cat, null10, len);
3660 char *savepat = pat;
3665 aptr = SvPV(fromstr, fromlen);
3670 SvCUR(cat) += (len+7)/8;
3671 SvGROW(cat, SvCUR(cat) + 1);
3672 aptr = SvPVX(cat) + aint;
3677 if (datumtype == 'B') {
3678 for (len = 0; len++ < aint;) {
3679 items |= *pat++ & 1;
3683 *aptr++ = items & 0xff;
3689 for (len = 0; len++ < aint;) {
3695 *aptr++ = items & 0xff;
3701 if (datumtype == 'B')
3702 items <<= 7 - (aint & 7);
3704 items >>= 7 - (aint & 7);
3705 *aptr++ = items & 0xff;
3707 pat = SvPVX(cat) + SvCUR(cat);
3718 char *savepat = pat;
3723 aptr = SvPV(fromstr, fromlen);
3728 SvCUR(cat) += (len+1)/2;
3729 SvGROW(cat, SvCUR(cat) + 1);
3730 aptr = SvPVX(cat) + aint;
3735 if (datumtype == 'H') {
3736 for (len = 0; len++ < aint;) {
3738 items |= ((*pat++ & 15) + 9) & 15;
3740 items |= *pat++ & 15;
3744 *aptr++ = items & 0xff;
3750 for (len = 0; len++ < aint;) {
3752 items |= (((*pat++ & 15) + 9) & 15) << 4;
3754 items |= (*pat++ & 15) << 4;
3758 *aptr++ = items & 0xff;
3764 *aptr++ = items & 0xff;
3765 pat = SvPVX(cat) + SvCUR(cat);
3777 aint = SvIV(fromstr);
3779 sv_catpvn(cat, &achar, sizeof(char));
3782 /* Float and double added by gnb@melba.bby.oz.au 22/11/89 */
3787 afloat = (float)SvNV(fromstr);
3788 sv_catpvn(cat, (char *)&afloat, sizeof (float));
3795 adouble = (double)SvNV(fromstr);
3796 sv_catpvn(cat, (char *)&adouble, sizeof (double));
3802 ashort = (I16)SvIV(fromstr);
3804 ashort = htons(ashort);
3806 CAT16(cat, &ashort);
3812 ashort = (I16)SvIV(fromstr);
3814 ashort = htovs(ashort);
3816 CAT16(cat, &ashort);
3823 ashort = (I16)SvIV(fromstr);
3824 CAT16(cat, &ashort);
3830 auint = SvUV(fromstr);
3831 sv_catpvn(cat, (char*)&auint, sizeof(unsigned int));
3837 adouble = floor(SvNV(fromstr));
3840 croak("Cannot compress negative numbers");
3846 #ifdef CXUX_BROKEN_CONSTANT_CONVERT
3847 adouble <= UV_MAX_cxux
3854 char buf[1 + sizeof(UV)];
3855 char *in = buf + sizeof(buf);
3856 UV auv = U_V(adouble);;
3859 *--in = (auv & 0x7f) | 0x80;
3862 buf[sizeof(buf) - 1] &= 0x7f; /* clear continue bit */
3863 sv_catpvn(cat, in, (buf + sizeof(buf)) - in);
3865 else if (SvPOKp(fromstr)) { /* decimal string arithmetics */
3866 char *from, *result, *in;
3871 /* Copy string and check for compliance */
3872 from = SvPV(fromstr, len);
3873 if ((norm = is_an_int(from, len)) == NULL)
3874 croak("can compress only unsigned integer");
3876 New('w', result, len, char);
3880 *--in = div128(norm, &done) | 0x80;
3881 result[len - 1] &= 0x7F; /* clear continue bit */
3882 sv_catpvn(cat, in, (result + len) - in);
3884 SvREFCNT_dec(norm); /* free norm */
3886 else if (SvNOKp(fromstr)) {
3887 char buf[sizeof(double) * 2]; /* 8/7 <= 2 */
3888 char *in = buf + sizeof(buf);
3891 double next = floor(adouble / 128);
3892 *--in = (unsigned char)(adouble - (next * 128)) | 0x80;
3893 if (--in < buf) /* this cannot happen ;-) */
3894 croak ("Cannot compress integer");
3896 } while (adouble > 0);
3897 buf[sizeof(buf) - 1] &= 0x7f; /* clear continue bit */
3898 sv_catpvn(cat, in, (buf + sizeof(buf)) - in);
3901 croak("Cannot compress non integer");
3907 aint = SvIV(fromstr);
3908 sv_catpvn(cat, (char*)&aint, sizeof(int));
3914 aulong = SvUV(fromstr);
3916 aulong = htonl(aulong);
3918 CAT32(cat, &aulong);
3924 aulong = SvUV(fromstr);
3926 aulong = htovl(aulong);
3928 CAT32(cat, &aulong);
3934 aulong = SvUV(fromstr);
3935 CAT32(cat, &aulong);
3941 along = SvIV(fromstr);
3949 auquad = (unsigned Quad_t)SvIV(fromstr);
3950 sv_catpvn(cat, (char*)&auquad, sizeof(unsigned Quad_t));
3956 aquad = (Quad_t)SvIV(fromstr);
3957 sv_catpvn(cat, (char*)&aquad, sizeof(Quad_t));
3960 #endif /* HAS_QUAD */
3962 len = 1; /* assume SV is correct length */
3967 if (fromstr == &sv_undef)
3970 /* XXX better yet, could spirit away the string to
3971 * a safe spot and hang on to it until the result
3972 * of pack() (and all copies of the result) are
3975 if (dowarn && (SvTEMP(fromstr) || SvPADTMP(fromstr)))
3976 warn("Attempt to pack pointer to temporary value");
3977 if (SvPOK(fromstr) || SvNIOK(fromstr))
3978 aptr = SvPV(fromstr,na);
3980 aptr = SvPV_force(fromstr,na);
3982 sv_catpvn(cat, (char*)&aptr, sizeof(char*));
3987 aptr = SvPV(fromstr, fromlen);
3988 SvGROW(cat, fromlen * 4 / 3);
3993 while (fromlen > 0) {
4000 doencodes(cat, aptr, todo);
4018 register I32 limit = POPi; /* note, negative is forever */
4021 register char *s = SvPV(sv, len);
4022 char *strend = s + len;
4024 register REGEXP *rx;
4028 I32 maxiters = (strend - s) + 10;
4031 I32 origlimit = limit;
4034 AV *oldstack = curstack;
4035 I32 gimme = GIMME_V;
4036 I32 oldsave = savestack_ix;
4039 Copy(&LvTARGOFF(POPs), &pm, 1, PMOP*);
4044 DIE("panic: do_split");
4045 rx = pm->op_pmregexp;
4047 TAINT_IF((pm->op_pmflags & PMf_LOCALE) &&
4048 (pm->op_pmflags & (PMf_WHITE | PMf_SKIPWHITE)));
4050 if (pm->op_pmreplroot)
4051 ary = GvAVn((GV*)pm->op_pmreplroot);
4052 else if (gimme != G_ARRAY)
4054 ary = (AV*)curpad[0];
4057 #endif /* USE_THREADS */
4060 if (ary && (gimme != G_ARRAY || (pm->op_pmflags & PMf_ONCE))) {
4064 for (i = AvFILL(ary); i >= 0; i--)
4065 AvARRAY(ary)[i] = &sv_undef; /* don't free mere refs */
4069 /* temporarily switch stacks */
4070 SWITCHSTACK(curstack, ary);
4072 base = SP - stack_base;
4074 if (pm->op_pmflags & PMf_SKIPWHITE) {
4075 if (pm->op_pmflags & PMf_LOCALE) {
4076 while (isSPACE_LC(*s))
4084 if (pm->op_pmflags & (PMf_MULTILINE|PMf_SINGLELINE)) {
4086 multiline = pm->op_pmflags & PMf_MULTILINE;
4090 limit = maxiters + 2;
4091 if (pm->op_pmflags & PMf_WHITE) {
4094 while (m < strend &&
4095 !((pm->op_pmflags & PMf_LOCALE)
4096 ? isSPACE_LC(*m) : isSPACE(*m)))
4101 dstr = NEWSV(30, m-s);
4102 sv_setpvn(dstr, s, m-s);
4108 while (s < strend &&
4109 ((pm->op_pmflags & PMf_LOCALE)
4110 ? isSPACE_LC(*s) : isSPACE(*s)))
4114 else if (strEQ("^", rx->precomp)) {
4117 for (m = s; m < strend && *m != '\n'; m++) ;
4121 dstr = NEWSV(30, m-s);
4122 sv_setpvn(dstr, s, m-s);
4129 else if (pm->op_pmshort && !rx->nparens) {
4130 i = SvCUR(pm->op_pmshort);
4132 i = *SvPVX(pm->op_pmshort);
4135 for (m = s; m < strend && *m != i; m++) ;
4138 dstr = NEWSV(30, m-s);
4139 sv_setpvn(dstr, s, m-s);
4148 while (s < strend && --limit &&
4149 (m=fbm_instr((unsigned char*)s, (unsigned char*)strend,
4153 dstr = NEWSV(31, m-s);
4154 sv_setpvn(dstr, s, m-s);
4163 maxiters += (strend - s) * rx->nparens;
4164 while (s < strend && --limit &&
4165 pregexec(rx, s, strend, orig, 1, Nullsv, TRUE))
4167 TAINT_IF(rx->exec_tainted);
4169 && rx->subbase != orig) {
4174 strend = s + (strend - m);
4177 dstr = NEWSV(32, m-s);
4178 sv_setpvn(dstr, s, m-s);
4183 for (i = 1; i <= rx->nparens; i++) {
4187 dstr = NEWSV(33, m-s);
4188 sv_setpvn(dstr, s, m-s);
4191 dstr = NEWSV(33, 0);
4200 LEAVE_SCOPE(oldsave);
4201 iters = (SP - stack_base) - base;
4202 if (iters > maxiters)
4205 /* keep field after final delim? */
4206 if (s < strend || (iters && origlimit)) {
4207 dstr = NEWSV(34, strend-s);
4208 sv_setpvn(dstr, s, strend-s);
4214 else if (!origlimit) {
4215 while (iters > 0 && (!TOPs || !SvANY(TOPs) || SvCUR(TOPs) == 0))
4219 SWITCHSTACK(ary, oldstack);
4220 if (SvSMAGICAL(ary)) {
4225 if (gimme == G_ARRAY) {
4227 Copy(AvARRAY(ary), SP + 1, iters, SV*);
4233 if (gimme == G_ARRAY)
4236 if (iters || !pm->op_pmreplroot) {
4246 unlock_condpair(svv)
4250 MAGIC *mg = mg_find((SV*)svv, 'm');
4253 croak("panic: unlock_condpair unlocking non-mutex");
4254 MUTEX_LOCK(MgMUTEXP(mg));
4255 if (MgOWNER(mg) != thr)
4256 croak("panic: unlock_condpair unlocking mutex that we don't own");
4258 COND_SIGNAL(MgOWNERCONDP(mg));
4259 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "0x%lx: unlock 0x%lx\n",
4260 (unsigned long)thr, (unsigned long)svv);)
4261 MUTEX_UNLOCK(MgMUTEXP(mg));
4263 #endif /* USE_THREADS */
4276 mg = condpair_magic(sv);
4277 MUTEX_LOCK(MgMUTEXP(mg));
4278 if (MgOWNER(mg) == thr)
4279 MUTEX_UNLOCK(MgMUTEXP(mg));
4282 COND_WAIT(MgOWNERCONDP(mg), MgMUTEXP(mg));
4284 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "0x%lx: pp_lock lock 0x%lx\n",
4285 (unsigned long)thr, (unsigned long)sv);)
4286 MUTEX_UNLOCK(MgMUTEXP(mg));
4287 SvREFCNT_inc(sv); /* keep alive until magic_mutexfree */
4288 save_destructor(unlock_condpair, sv);
4290 #endif /* USE_THREADS */
4291 if (SvTYPE(retsv) == SVt_PVAV || SvTYPE(retsv) == SVt_PVHV
4292 || SvTYPE(retsv) == SVt_PVCV) {
4293 retsv = refto(retsv);
4303 SV **svp = av_fetch(thr->magicals, op->op_targ, FALSE);
4305 croak("panic: pp_specific");
4307 if (op->op_private & OPpLVAL_INTRO)
4308 PUSHs(save_svref(svp));
4312 DIE("tried to access thread-specific data in non-threaded perl");
4313 #endif /* USE_THREADS */