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);
444 /* Pattern matching */
449 register unsigned char *s;
452 register I32 *sfirst;
456 if (sv == lastscream) {
462 SvSCREAM_off(lastscream);
463 SvREFCNT_dec(lastscream);
465 lastscream = SvREFCNT_inc(sv);
468 s = (unsigned char*)(SvPV(sv, len));
472 if (pos > maxscream) {
474 maxscream = pos + 80;
475 New(301, screamfirst, 256, I32);
476 New(302, screamnext, maxscream, I32);
479 maxscream = pos + pos / 4;
480 Renew(screamnext, maxscream, I32);
484 sfirst = screamfirst;
487 if (!sfirst || !snext)
488 DIE("do_study: out of memory");
490 for (ch = 256; ch; --ch)
497 snext[pos] = sfirst[ch] - pos;
504 sv_magic(sv, Nullsv, 'g', Nullch, 0); /* piggyback on m//g magic */
513 if (op->op_flags & OPf_STACKED)
519 TARG = sv_newmortal();
520 PUSHi(do_trans(sv, op));
524 /* Lvalue operators. */
546 SETi(do_chomp(TOPs));
553 register I32 count = 0;
556 count += do_chomp(POPs);
567 if (!sv || !SvANY(sv))
569 switch (SvTYPE(sv)) {
571 if (AvMAX(sv) >= 0 || SvRMAGICAL(sv))
575 if (HvARRAY(sv) || SvRMAGICAL(sv))
579 if (CvROOT(sv) || CvXSUB(sv))
596 if (!op->op_private) {
605 if (SvTHINKFIRST(sv)) {
612 switch (SvTYPE(sv)) {
622 if (cv_const_sv((CV*)sv))
623 warn("Constant subroutine %s undefined",
624 CvANON((CV*)sv) ? "(anonymous)" : GvENAME(CvGV((CV*)sv)));
627 { GV* gv = (GV*)SvREFCNT_inc(CvGV((CV*)sv));
629 CvGV((CV*)sv) = gv; } /* let user-undef'd sub keep its identity */
633 sv_setsv(sv, &sv_undef);
636 if (SvTYPE(sv) >= SVt_PV && SvPVX(sv) && SvLEN(sv)) {
639 SvPV_set(sv, Nullch);
652 if (SvREADONLY(TOPs) || SvTYPE(TOPs) > SVt_PVLV)
654 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) &&
655 SvIVX(TOPs) != IV_MIN)
658 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
669 if (SvREADONLY(TOPs) || SvTYPE(TOPs) > SVt_PVLV)
671 sv_setsv(TARG, TOPs);
672 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) &&
673 SvIVX(TOPs) != IV_MAX)
676 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
690 if(SvREADONLY(TOPs) || SvTYPE(TOPs) > SVt_PVLV)
692 sv_setsv(TARG, TOPs);
693 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) &&
694 SvIVX(TOPs) != IV_MIN)
697 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
706 /* Ordinary operators. */
710 dSP; dATARGET; tryAMAGICbin(pow,opASSIGN);
713 SETn( pow( left, right) );
720 dSP; dATARGET; tryAMAGICbin(mult,opASSIGN);
723 SETn( left * right );
730 dSP; dATARGET; tryAMAGICbin(div,opASSIGN);
735 DIE("Illegal division by zero");
737 /* insure that 20./5. == 4. */
740 if ((double)I_V(left) == left &&
741 (double)I_V(right) == right &&
742 (k = I_V(left)/I_V(right))*I_V(right) == I_V(left)) {
745 value = left / right;
749 value = left / right;
758 dSP; dATARGET; tryAMAGICbin(mod,opASSIGN);
766 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)) {
768 right = (right_neg = (i < 0)) ? -i : i;
772 right = U_V((right_neg = (n < 0)) ? -n : n);
775 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)) {
777 left = (left_neg = (i < 0)) ? -i : i;
781 left = U_V((left_neg = (n < 0)) ? -n : n);
785 DIE("Illegal modulus zero");
788 if ((left_neg != right_neg) && ans)
791 /* XXX may warn: unary minus operator applied to unsigned type */
792 /* could change -foo to be (~foo)+1 instead */
793 if (ans <= -(UV)IV_MAX)
794 sv_setiv(TARG, (IV) -ans);
796 sv_setnv(TARG, -(double)ans);
807 dSP; dATARGET; tryAMAGICbin(repeat,opASSIGN);
809 register I32 count = POPi;
810 if (GIMME == G_ARRAY && op->op_private & OPpREPEAT_DOLIST) {
812 I32 items = SP - MARK;
824 repeatcpy((char*)(MARK + items), (char*)MARK,
825 items * sizeof(SV*), count - 1);
831 else { /* Note: mark already snarfed by pp_list */
836 if (TARG == tmpstr && SvTHINKFIRST(tmpstr)) {
837 if (SvREADONLY(tmpstr) && curcop != &compiling)
838 DIE("Can't x= to readonly value");
842 SvSetSV(TARG, tmpstr);
843 SvPV_force(TARG, len);
848 SvGROW(TARG, (count * len) + 1);
849 repeatcpy(SvPVX(TARG) + len, SvPVX(TARG), len, count - 1);
850 SvCUR(TARG) *= count;
854 (void)SvPOK_only(TARG);
863 dSP; dATARGET; tryAMAGICbin(subtr,opASSIGN);
866 SETn( left - right );
873 dSP; dATARGET; tryAMAGICbin(lshift,opASSIGN);
876 if (op->op_private & HINT_INTEGER) {
892 dSP; dATARGET; tryAMAGICbin(rshift,opASSIGN);
895 if (op->op_private & HINT_INTEGER) {
911 dSP; tryAMAGICbinSET(lt,0);
914 SETs(boolSV(TOPn < value));
921 dSP; tryAMAGICbinSET(gt,0);
924 SETs(boolSV(TOPn > value));
931 dSP; tryAMAGICbinSET(le,0);
934 SETs(boolSV(TOPn <= value));
941 dSP; tryAMAGICbinSET(ge,0);
944 SETs(boolSV(TOPn >= value));
951 dSP; tryAMAGICbinSET(ne,0);
954 SETs(boolSV(TOPn != value));
961 dSP; dTARGET; tryAMAGICbin(ncmp,0);
968 else if (left < right)
970 else if (left > right)
983 dSP; tryAMAGICbinSET(slt,0);
986 int cmp = ((op->op_private & OPpLOCALE)
987 ? sv_cmp_locale(left, right)
988 : sv_cmp(left, right));
989 SETs(boolSV(cmp < 0));
996 dSP; tryAMAGICbinSET(sgt,0);
999 int cmp = ((op->op_private & OPpLOCALE)
1000 ? sv_cmp_locale(left, right)
1001 : sv_cmp(left, right));
1002 SETs(boolSV(cmp > 0));
1009 dSP; tryAMAGICbinSET(sle,0);
1012 int cmp = ((op->op_private & OPpLOCALE)
1013 ? sv_cmp_locale(left, right)
1014 : sv_cmp(left, right));
1015 SETs(boolSV(cmp <= 0));
1022 dSP; tryAMAGICbinSET(sge,0);
1025 int cmp = ((op->op_private & OPpLOCALE)
1026 ? sv_cmp_locale(left, right)
1027 : sv_cmp(left, right));
1028 SETs(boolSV(cmp >= 0));
1035 dSP; tryAMAGICbinSET(seq,0);
1038 SETs(boolSV(sv_eq(left, right)));
1045 dSP; tryAMAGICbinSET(sne,0);
1048 SETs(boolSV(!sv_eq(left, right)));
1055 dSP; dTARGET; tryAMAGICbin(scmp,0);
1058 int cmp = ((op->op_private & OPpLOCALE)
1059 ? sv_cmp_locale(left, right)
1060 : sv_cmp(left, right));
1068 dSP; dATARGET; tryAMAGICbin(band,opASSIGN);
1071 if (SvNIOKp(left) || SvNIOKp(right)) {
1072 if (op->op_private & HINT_INTEGER) {
1073 IBW value = SvIV(left) & SvIV(right);
1077 UBW value = SvUV(left) & SvUV(right);
1082 do_vop(op->op_type, TARG, left, right);
1091 dSP; dATARGET; tryAMAGICbin(bxor,opASSIGN);
1094 if (SvNIOKp(left) || SvNIOKp(right)) {
1095 if (op->op_private & HINT_INTEGER) {
1096 IBW value = (USE_LEFT(left) ? SvIV(left) : 0) ^ SvIV(right);
1100 UBW value = (USE_LEFT(left) ? SvUV(left) : 0) ^ SvUV(right);
1105 do_vop(op->op_type, TARG, left, right);
1114 dSP; dATARGET; tryAMAGICbin(bor,opASSIGN);
1117 if (SvNIOKp(left) || SvNIOKp(right)) {
1118 if (op->op_private & HINT_INTEGER) {
1119 IBW value = (USE_LEFT(left) ? SvIV(left) : 0) | SvIV(right);
1123 UBW value = (USE_LEFT(left) ? SvUV(left) : 0) | SvUV(right);
1128 do_vop(op->op_type, TARG, left, right);
1137 dSP; dTARGET; tryAMAGICun(neg);
1142 if (SvIOKp(sv) && !SvNOKp(sv) && !SvPOKp(sv) && SvIVX(sv) != IV_MIN)
1144 else if (SvNIOKp(sv))
1146 else if (SvPOKp(sv)) {
1148 char *s = SvPV(sv, len);
1149 if (isIDFIRST(*s)) {
1150 sv_setpvn(TARG, "-", 1);
1153 else if (*s == '+' || *s == '-') {
1155 *SvPV_force(TARG, len) = *s == '-' ? '+' : '-';
1158 sv_setnv(TARG, -SvNV(sv));
1170 dSP; tryAMAGICunSET(not);
1171 #endif /* OVERLOAD */
1172 *stack_sp = boolSV(!SvTRUE(*stack_sp));
1178 dSP; dTARGET; tryAMAGICun(compl);
1182 if (op->op_private & HINT_INTEGER) {
1183 IBW value = ~SvIV(sv);
1187 UBW value = ~SvUV(sv);
1192 register char *tmps;
1193 register long *tmpl;
1198 tmps = SvPV_force(TARG, len);
1201 for ( ; anum && (unsigned long)tmps % sizeof(long); anum--, tmps++)
1204 for ( ; anum >= sizeof(long); anum -= sizeof(long), tmpl++)
1208 for ( ; anum > 0; anum--, tmps++)
1217 /* integer versions of some of the above */
1221 dSP; dATARGET; tryAMAGICbin(mult,opASSIGN);
1224 SETi( left * right );
1231 dSP; dATARGET; tryAMAGICbin(div,opASSIGN);
1235 DIE("Illegal division by zero");
1236 value = POPi / value;
1244 dSP; dATARGET; tryAMAGICbin(mod,opASSIGN);
1248 DIE("Illegal modulus zero");
1249 SETi( left % right );
1256 dSP; dATARGET; tryAMAGICbin(add,opASSIGN);
1259 SETi( left + right );
1266 dSP; dATARGET; tryAMAGICbin(subtr,opASSIGN);
1269 SETi( left - right );
1276 dSP; tryAMAGICbinSET(lt,0);
1279 SETs(boolSV(left < right));
1286 dSP; tryAMAGICbinSET(gt,0);
1289 SETs(boolSV(left > right));
1296 dSP; tryAMAGICbinSET(le,0);
1299 SETs(boolSV(left <= right));
1306 dSP; tryAMAGICbinSET(ge,0);
1309 SETs(boolSV(left >= right));
1316 dSP; tryAMAGICbinSET(eq,0);
1319 SETs(boolSV(left == right));
1326 dSP; tryAMAGICbinSET(ne,0);
1329 SETs(boolSV(left != right));
1336 dSP; dTARGET; tryAMAGICbin(ncmp,0);
1343 else if (left < right)
1354 dSP; dTARGET; tryAMAGICun(neg);
1359 /* High falutin' math. */
1363 dSP; dTARGET; tryAMAGICbin(atan2,0);
1366 SETn(atan2(left, right));
1373 dSP; dTARGET; tryAMAGICun(sin);
1385 dSP; dTARGET; tryAMAGICun(cos);
1405 if (!srand_called) {
1406 (void)srand((unsigned)seed());
1407 srand_called = TRUE;
1410 value = rand() * value / 2147483648.0;
1413 value = rand() * value / 65536.0;
1416 value = rand() * value / 32768.0;
1418 value = rand() * value / (double)(((unsigned long)1) << RANDBITS);
1434 (void)srand((unsigned)anum);
1435 srand_called = TRUE;
1444 * This is really just a quick hack which grabs various garbage
1445 * values. It really should be a real hash algorithm which
1446 * spreads the effect of every input bit onto every output bit,
1447 * if someone who knows about such tings would bother to write it.
1448 * Might be a good idea to add that function to CORE as well.
1449 * No numbers below come from careful analysis or anyting here,
1450 * except they are primes and SEED_C1 > 1E6 to get a full-width
1451 * value from (tv_sec * SEED_C1 + tv_usec). The multipliers should
1452 * probably be bigger too.
1455 # define SEED_C1 1000003
1456 #define SEED_C4 73819
1458 # define SEED_C1 25747
1459 #define SEED_C4 20639
1463 #define SEED_C5 26107
1468 # include <starlet.h>
1469 /* when[] = (low 32 bits, high 32 bits) of time since epoch
1470 * in 100-ns units, typically incremented ever 10 ms. */
1471 unsigned int when[2];
1472 _ckvmssts(sys$gettim(when));
1473 u = (U32)SEED_C1 * when[0] + (U32)SEED_C2 * when[1];
1475 # ifdef HAS_GETTIMEOFDAY
1476 struct timeval when;
1477 gettimeofday(&when,(struct timezone *) 0);
1478 u = (U32)SEED_C1 * when.tv_sec + (U32)SEED_C2 * when.tv_usec;
1482 u = (U32)SEED_C1 * when;
1485 u += SEED_C3 * (U32)getpid();
1486 u += SEED_C4 * (U32)(UV)stack_sp;
1487 #ifndef PLAN9 /* XXX Plan9 assembler chokes on this; fix needed */
1488 u += SEED_C5 * (U32)(UV)&when;
1495 dSP; dTARGET; tryAMAGICun(exp);
1507 dSP; dTARGET; tryAMAGICun(log);
1512 SET_NUMERIC_STANDARD();
1513 DIE("Can't take log of %g", value);
1523 dSP; dTARGET; tryAMAGICun(sqrt);
1528 SET_NUMERIC_STANDARD();
1529 DIE("Can't take sqrt of %g", value);
1531 value = sqrt(value);
1541 double value = TOPn;
1544 if (SvIOKp(TOPs) && !SvNOKp(TOPs) && !SvPOKp(TOPs)) {
1550 (void)modf(value, &value);
1552 (void)modf(-value, &value);
1567 dSP; dTARGET; tryAMAGICun(abs);
1569 double value = TOPn;
1572 if (SvIOKp(TOPs) && !SvNOKp(TOPs) && !SvPOKp(TOPs) &&
1573 (iv = SvIVX(TOPs)) != IV_MIN) {
1594 XPUSHu(scan_hex(tmps, 99, &argtype));
1606 while (*tmps && isSPACE(*tmps))
1611 value = scan_hex(++tmps, 99, &argtype);
1613 value = scan_oct(tmps, 99, &argtype);
1623 SETi( sv_len(TOPs) );
1636 I32 lvalue = op->op_flags & OPf_MOD;
1638 I32 arybase = curcop->cop_arybase;
1644 tmps = SvPV(sv, curlen);
1645 if (pos >= arybase) {
1663 else if (len >= 0) {
1665 if (rem > (I32)curlen)
1679 if (dowarn || lvalue)
1680 warn("substr outside of string");
1685 sv_setpvn(TARG, tmps, rem);
1686 if (lvalue) { /* it's an lvalue! */
1687 if (!SvGMAGICAL(sv)) {
1691 warn("Attempt to use reference as lvalue in substr");
1693 if (SvOK(sv)) /* is it defined ? */
1694 (void)SvPOK_only(sv);
1696 sv_setpvn(sv,"",0); /* avoid lexical reincarnation */
1699 if (SvTYPE(TARG) < SVt_PVLV) {
1700 sv_upgrade(TARG, SVt_PVLV);
1701 sv_magic(TARG, Nullsv, 'x', Nullch, 0);
1706 LvTARGOFF(TARG) = pos;
1707 LvTARGLEN(TARG) = rem;
1710 PUSHs(TARG); /* avoid SvSETMAGIC here */
1717 register I32 size = POPi;
1718 register I32 offset = POPi;
1719 register SV *src = POPs;
1720 I32 lvalue = op->op_flags & OPf_MOD;
1722 unsigned char *s = (unsigned char*)SvPV(src, srclen);
1723 unsigned long retnum;
1726 offset *= size; /* turn into bit offset */
1727 len = (offset + size + 7) / 8;
1728 if (offset < 0 || size < 1)
1731 if (lvalue) { /* it's an lvalue! */
1732 if (SvTYPE(TARG) < SVt_PVLV) {
1733 sv_upgrade(TARG, SVt_PVLV);
1734 sv_magic(TARG, Nullsv, 'v', Nullch, 0);
1739 LvTARGOFF(TARG) = offset;
1740 LvTARGLEN(TARG) = size;
1748 if (offset >= srclen)
1751 retnum = (unsigned long) s[offset] << 8;
1753 else if (size == 32) {
1754 if (offset >= srclen)
1756 else if (offset + 1 >= srclen)
1757 retnum = (unsigned long) s[offset] << 24;
1758 else if (offset + 2 >= srclen)
1759 retnum = ((unsigned long) s[offset] << 24) +
1760 ((unsigned long) s[offset + 1] << 16);
1762 retnum = ((unsigned long) s[offset] << 24) +
1763 ((unsigned long) s[offset + 1] << 16) +
1764 (s[offset + 2] << 8);
1769 retnum = (s[offset >> 3] >> (offset & 7)) & ((1 << size) - 1);
1774 else if (size == 16)
1775 retnum = ((unsigned long) s[offset] << 8) + s[offset+1];
1776 else if (size == 32)
1777 retnum = ((unsigned long) s[offset] << 24) +
1778 ((unsigned long) s[offset + 1] << 16) +
1779 (s[offset + 2] << 8) + s[offset+3];
1783 sv_setiv(TARG, (IV)retnum);
1798 I32 arybase = curcop->cop_arybase;
1803 offset = POPi - arybase;
1806 tmps = SvPV(big, biglen);
1809 else if (offset > biglen)
1811 if (!(tmps2 = fbm_instr((unsigned char*)tmps + offset,
1812 (unsigned char*)tmps + biglen, little)))
1813 retval = -1 + arybase;
1815 retval = tmps2 - tmps + arybase;
1832 I32 arybase = curcop->cop_arybase;
1838 tmps2 = SvPV(little, llen);
1839 tmps = SvPV(big, blen);
1843 offset = SvIV(offstr) - arybase + llen;
1846 else if (offset > blen)
1848 if (!(tmps2 = rninstr(tmps, tmps + offset,
1849 tmps2, tmps2 + llen)))
1850 retval = -1 + arybase;
1852 retval = tmps2 - tmps + arybase;
1859 dSP; dMARK; dORIGMARK; dTARGET;
1860 #ifdef USE_LOCALE_NUMERIC
1861 if (op->op_private & OPpLOCALE)
1862 SET_NUMERIC_LOCAL();
1864 SET_NUMERIC_STANDARD();
1866 do_sprintf(TARG, SP-MARK, MARK+1);
1867 TAINT_IF(SvTAINTED(TARG));
1881 value = (I32) (*tmps & 255);
1886 value = (I32) (anum & 255);
1897 (void)SvUPGRADE(TARG,SVt_PV);
1903 (void)SvPOK_only(TARG);
1910 dSP; dTARGET; dPOPTOPssrl;
1912 char *tmps = SvPV(left, na);
1914 sv_setpv(TARG, fcrypt(tmps, SvPV(right, na)));
1916 sv_setpv(TARG, crypt(tmps, SvPV(right, na)));
1920 "The crypt() function is unimplemented due to excessive paranoia.");
1932 if (!SvPADTMP(sv)) {
1938 s = SvPV_force(sv, na);
1940 if (op->op_private & OPpLOCALE) {
1943 *s = toUPPER_LC(*s);
1958 if (!SvPADTMP(sv)) {
1964 s = SvPV_force(sv, na);
1966 if (op->op_private & OPpLOCALE) {
1969 *s = toLOWER_LC(*s);
1986 if (!SvPADTMP(sv)) {
1993 s = SvPV_force(sv, len);
1995 register char *send = s + len;
1997 if (op->op_private & OPpLOCALE) {
2000 for (; s < send; s++)
2001 *s = toUPPER_LC(*s);
2004 for (; s < send; s++)
2018 if (!SvPADTMP(sv)) {
2025 s = SvPV_force(sv, len);
2027 register char *send = s + len;
2029 if (op->op_private & OPpLOCALE) {
2032 for (; s < send; s++)
2033 *s = toLOWER_LC(*s);
2036 for (; s < send; s++)
2048 register char *s = SvPV(sv,len);
2052 (void)SvUPGRADE(TARG, SVt_PV);
2053 SvGROW(TARG, (len * 2) + 1);
2061 SvCUR_set(TARG, d - SvPVX(TARG));
2062 (void)SvPOK_only(TARG);
2065 sv_setpvn(TARG, s, len);
2074 dSP; dMARK; dORIGMARK;
2076 register AV* av = (AV*)POPs;
2077 register I32 lval = op->op_flags & OPf_MOD;
2078 I32 arybase = curcop->cop_arybase;
2081 if (SvTYPE(av) == SVt_PVAV) {
2082 if (lval && op->op_private & OPpLVAL_INTRO) {
2084 for (svp = mark + 1; svp <= sp; svp++) {
2089 if (max > AvMAX(av))
2092 while (++MARK <= SP) {
2093 elem = SvIVx(*MARK);
2097 svp = av_fetch(av, elem, lval);
2099 if (!svp || *svp == &sv_undef)
2100 DIE(no_aelem, elem);
2101 if (op->op_private & OPpLVAL_INTRO)
2104 *MARK = svp ? *svp : &sv_undef;
2107 if (GIMME != G_ARRAY) {
2115 /* Associative arrays. */
2120 HV *hash = (HV*)POPs;
2122 I32 gimme = GIMME_V;
2123 I32 realhv = (SvTYPE(hash) == SVt_PVHV);
2126 /* might clobber stack_sp */
2127 entry = realhv ? hv_iternext(hash) : avhv_iternext((AV*)hash);
2132 PUSHs(hv_iterkeysv(entry)); /* won't clobber stack_sp */
2133 if (gimme == G_ARRAY) {
2135 /* might clobber stack_sp */
2136 sv_setsv(TARG, realhv ?
2137 hv_iterval(hash, entry) : avhv_iterval((AV*)hash, entry));
2142 else if (gimme == G_SCALAR)
2161 I32 gimme = GIMME_V;
2162 I32 discard = (gimme == G_VOID) ? G_DISCARD : 0;
2166 if (op->op_private & OPpSLICE) {
2170 hvtype = SvTYPE(hv);
2171 while (++MARK <= SP) {
2172 if (hvtype == SVt_PVHV)
2173 sv = hv_delete_ent(hv, *MARK, discard, 0);
2174 else if (hvtype == SVt_PVAV)
2175 sv = avhv_delete_ent((AV*)hv, *MARK, discard, 0);
2177 DIE("Not a HASH reference");
2178 *MARK = sv ? sv : &sv_undef;
2182 else if (gimme == G_SCALAR) {
2191 if (SvTYPE(hv) == SVt_PVHV)
2192 sv = hv_delete_ent(hv, keysv, discard, 0);
2193 else if (SvTYPE(hv) == SVt_PVAV)
2194 sv = avhv_delete_ent((AV*)hv, keysv, discard, 0);
2196 DIE("Not a HASH reference");
2210 if (SvTYPE(hv) == SVt_PVHV) {
2211 if (hv_exists_ent(hv, tmpsv, 0))
2213 } else if (SvTYPE(hv) == SVt_PVAV) {
2214 if (avhv_exists_ent((AV*)hv, tmpsv, 0))
2217 DIE("Not a HASH reference");
2224 dSP; dMARK; dORIGMARK;
2226 register HV *hv = (HV*)POPs;
2227 register I32 lval = op->op_flags & OPf_MOD;
2228 I32 realhv = (SvTYPE(hv) == SVt_PVHV);
2230 if (realhv || SvTYPE(hv) == SVt_PVAV) {
2231 while (++MARK <= SP) {
2235 he = hv_fetch_ent(hv, keysv, lval, 0);
2236 svp = he ? &HeVAL(he) : 0;
2238 svp = avhv_fetch_ent((AV*)hv, keysv, lval, 0);
2241 if (!he || HeVAL(he) == &sv_undef)
2242 DIE(no_helem, SvPV(keysv, na));
2243 if (op->op_private & OPpLVAL_INTRO)
2244 save_svref(&HeVAL(he));
2246 *MARK = he ? HeVAL(he) : &sv_undef;
2249 if (GIMME != G_ARRAY) {
2257 /* List operators. */
2262 if (GIMME != G_ARRAY) {
2264 *MARK = *SP; /* unwanted list, return last item */
2275 SV **lastrelem = stack_sp;
2276 SV **lastlelem = stack_base + POPMARK;
2277 SV **firstlelem = stack_base + POPMARK + 1;
2278 register SV **firstrelem = lastlelem + 1;
2279 I32 arybase = curcop->cop_arybase;
2280 I32 lval = op->op_flags & OPf_MOD;
2281 I32 is_something_there = lval;
2283 register I32 max = lastrelem - lastlelem;
2284 register SV **lelem;
2287 if (GIMME != G_ARRAY) {
2288 ix = SvIVx(*lastlelem);
2293 if (ix < 0 || ix >= max)
2294 *firstlelem = &sv_undef;
2296 *firstlelem = firstrelem[ix];
2302 SP = firstlelem - 1;
2306 for (lelem = firstlelem; lelem <= lastlelem; lelem++) {
2312 else if (!(*lelem = firstrelem[ix]))
2317 if (ix >= max || !(*lelem = firstrelem[ix]))
2320 if (!is_something_there && (SvOK(*lelem) || SvGMAGICAL(*lelem)))
2321 is_something_there = TRUE;
2323 if (is_something_there)
2326 SP = firstlelem - 1;
2332 dSP; dMARK; dORIGMARK;
2333 I32 items = SP - MARK;
2334 SV *av = sv_2mortal((SV*)av_make(items, MARK+1));
2335 SP = ORIGMARK; /* av_make() might realloc stack_sp */
2342 dSP; dMARK; dORIGMARK;
2343 HV* hv = (HV*)sv_2mortal((SV*)newHV());
2347 SV *val = NEWSV(46, 0);
2349 sv_setsv(val, *++MARK);
2351 warn("Odd number of elements in hash list");
2352 (void)hv_store_ent(hv,key,val,0);
2361 dSP; dMARK; dORIGMARK;
2362 register AV *ary = (AV*)*++MARK;
2366 register I32 offset;
2367 register I32 length;
2376 offset = i = SvIVx(*MARK);
2378 offset += AvFILL(ary) + 1;
2380 offset -= curcop->cop_arybase;
2384 length = SvIVx(*MARK++);
2389 length = AvMAX(ary) + 1; /* close enough to infinity */
2393 length = AvMAX(ary) + 1;
2395 if (offset > AvFILL(ary) + 1)
2396 offset = AvFILL(ary) + 1;
2397 after = AvFILL(ary) + 1 - (offset + length);
2398 if (after < 0) { /* not that much array */
2399 length += after; /* offset+length now in array */
2405 /* At this point, MARK .. SP-1 is our new LIST */
2408 diff = newlen - length;
2410 if (diff < 0) { /* shrinking the area */
2412 New(451, tmparyval, newlen, SV*); /* so remember insertion */
2413 Copy(MARK, tmparyval, newlen, SV*);
2416 MARK = ORIGMARK + 1;
2417 if (GIMME == G_ARRAY) { /* copy return vals to stack */
2418 MEXTEND(MARK, length);
2419 Copy(AvARRAY(ary)+offset, MARK, length, SV*);
2421 EXTEND_MORTAL(length);
2422 for (i = length, dst = MARK; i; i--) {
2423 if (!SvIMMORTAL(*dst))
2424 sv_2mortal(*dst); /* free them eventualy */
2431 *MARK = AvARRAY(ary)[offset+length-1];
2433 if (!SvIMMORTAL(*MARK))
2435 for (i = length - 1, dst = &AvARRAY(ary)[offset]; i > 0; i--)
2436 SvREFCNT_dec(*dst++); /* free them now */
2439 AvFILL(ary) += diff;
2441 /* pull up or down? */
2443 if (offset < after) { /* easier to pull up */
2444 if (offset) { /* esp. if nothing to pull */
2445 src = &AvARRAY(ary)[offset-1];
2446 dst = src - diff; /* diff is negative */
2447 for (i = offset; i > 0; i--) /* can't trust Copy */
2451 SvPVX(ary) = (char*)(AvARRAY(ary) - diff); /* diff is negative */
2455 if (after) { /* anything to pull down? */
2456 src = AvARRAY(ary) + offset + length;
2457 dst = src + diff; /* diff is negative */
2458 Move(src, dst, after, SV*);
2460 dst = &AvARRAY(ary)[AvFILL(ary)+1];
2461 /* avoid later double free */
2465 dst[--i] = &sv_undef;
2468 for (src = tmparyval, dst = AvARRAY(ary) + offset;
2470 *dst = NEWSV(46, 0);
2471 sv_setsv(*dst++, *src++);
2473 Safefree(tmparyval);
2476 else { /* no, expanding (or same) */
2478 New(452, tmparyval, length, SV*); /* so remember deletion */
2479 Copy(AvARRAY(ary)+offset, tmparyval, length, SV*);
2482 if (diff > 0) { /* expanding */
2484 /* push up or down? */
2486 if (offset < after && diff <= AvARRAY(ary) - AvALLOC(ary)) {
2490 Move(src, dst, offset, SV*);
2492 SvPVX(ary) = (char*)(AvARRAY(ary) - diff);/* diff is positive */
2494 AvFILL(ary) += diff;
2497 if (AvFILL(ary) + diff >= AvMAX(ary)) /* oh, well */
2498 av_extend(ary, AvFILL(ary) + diff);
2499 AvFILL(ary) += diff;
2502 dst = AvARRAY(ary) + AvFILL(ary);
2504 for (i = after; i; i--) {
2511 for (src = MARK, dst = AvARRAY(ary) + offset; newlen; newlen--) {
2512 *dst = NEWSV(46, 0);
2513 sv_setsv(*dst++, *src++);
2515 MARK = ORIGMARK + 1;
2516 if (GIMME == G_ARRAY) { /* copy return vals to stack */
2518 Copy(tmparyval, MARK, length, SV*);
2520 EXTEND_MORTAL(length);
2521 for (i = length, dst = MARK; i; i--) {
2522 if (!SvIMMORTAL(*dst))
2523 sv_2mortal(*dst); /* free them eventualy */
2527 Safefree(tmparyval);
2531 else if (length--) {
2532 *MARK = tmparyval[length];
2534 if (!SvIMMORTAL(*MARK))
2536 while (length-- > 0)
2537 SvREFCNT_dec(tmparyval[length]);
2539 Safefree(tmparyval);
2550 dSP; dMARK; dORIGMARK; dTARGET;
2551 register AV *ary = (AV*)*++MARK;
2552 register SV *sv = &sv_undef;
2554 for (++MARK; MARK <= SP; MARK++) {
2557 sv_setsv(sv, *MARK);
2561 PUSHi( AvFILL(ary) + 1 );
2569 SV *sv = av_pop(av);
2570 if (!SvIMMORTAL(sv) && AvREAL(av))
2571 (void)sv_2mortal(sv);
2580 SV *sv = av_shift(av);
2584 if (!SvIMMORTAL(sv) && AvREAL(av))
2585 (void)sv_2mortal(sv);
2592 dSP; dMARK; dORIGMARK; dTARGET;
2593 register AV *ary = (AV*)*++MARK;
2597 av_unshift(ary, SP - MARK);
2600 sv_setsv(sv, *++MARK);
2601 (void)av_store(ary, i++, sv);
2605 PUSHi( AvFILL(ary) + 1 );
2615 if (GIMME == G_ARRAY) {
2626 register char *down;
2632 do_join(TARG, &sv_no, MARK, SP);
2634 sv_setsv(TARG, (SP > MARK) ? *SP : GvSV(defgv));
2635 up = SvPV_force(TARG, len);
2637 down = SvPVX(TARG) + len - 1;
2643 (void)SvPOK_only(TARG);
2657 char *s = SvPV(sv, len);
2661 if (!strnEQ(s, "0000", 4)) { /* need to grow sv */
2662 SV *new = newSVpv("0000000000", 10);
2665 SvREFCNT_dec(sv); /* free old sv */
2670 while (!*t) /* trailing '\0'? */
2673 i = ((*t - '0') << 7) + m;
2674 *(t--) = '0' + (i % 10);
2680 /* Explosives and implosives. */
2687 I32 gimme = GIMME_V;
2691 register char *pat = SvPV(left, llen);
2692 register char *s = SvPV(right, rlen);
2693 char *strend = s + rlen;
2695 register char *patend = pat + llen;
2700 /* These must not be in registers: */
2711 unsigned Quad_t auquad;
2717 register U32 culong;
2719 static char* bitcount = 0;
2721 if (gimme != G_ARRAY) { /* arrange to do first one only */
2723 for (patend = pat; !isALPHA(*patend) || *patend == 'x'; patend++) ;
2724 if (strchr("aAbBhHP", *patend) || *pat == '%') {
2726 while (isDIGIT(*patend) || *patend == '*')
2732 while (pat < patend) {
2734 datumtype = *pat++ & 0xFF;
2735 if (isSPACE(datumtype))
2739 else if (*pat == '*') {
2740 len = strend - strbeg; /* long enough */
2743 else if (isDIGIT(*pat)) {
2745 while (isDIGIT(*pat))
2746 len = (len * 10) + (*pat++ - '0');
2749 len = (datumtype != '@');
2752 croak("Invalid type in unpack: '%c'", (int)datumtype);
2754 if (len == 1 && pat[-1] != '1')
2763 if (len > strend - strbeg)
2764 DIE("@ outside of string");
2768 if (len > s - strbeg)
2769 DIE("X outside of string");
2773 if (len > strend - s)
2774 DIE("x outside of string");
2779 if (len > strend - s)
2782 goto uchar_checksum;
2783 sv = NEWSV(35, len);
2784 sv_setpvn(sv, s, len);
2786 if (datumtype == 'A') {
2787 aptr = s; /* borrow register */
2788 s = SvPVX(sv) + len - 1;
2789 while (s >= SvPVX(sv) && (!*s || isSPACE(*s)))
2792 SvCUR_set(sv, s - SvPVX(sv));
2793 s = aptr; /* unborrow register */
2795 XPUSHs(sv_2mortal(sv));
2799 if (pat[-1] == '*' || len > (strend - s) * 8)
2800 len = (strend - s) * 8;
2803 Newz(601, bitcount, 256, char);
2804 for (bits = 1; bits < 256; bits++) {
2805 if (bits & 1) bitcount[bits]++;
2806 if (bits & 2) bitcount[bits]++;
2807 if (bits & 4) bitcount[bits]++;
2808 if (bits & 8) bitcount[bits]++;
2809 if (bits & 16) bitcount[bits]++;
2810 if (bits & 32) bitcount[bits]++;
2811 if (bits & 64) bitcount[bits]++;
2812 if (bits & 128) bitcount[bits]++;
2816 culong += bitcount[*(unsigned char*)s++];
2821 if (datumtype == 'b') {
2823 if (bits & 1) culong++;
2829 if (bits & 128) culong++;
2836 sv = NEWSV(35, len + 1);
2839 aptr = pat; /* borrow register */
2841 if (datumtype == 'b') {
2843 for (len = 0; len < aint; len++) {
2844 if (len & 7) /*SUPPRESS 595*/
2848 *pat++ = '0' + (bits & 1);
2853 for (len = 0; len < aint; len++) {
2858 *pat++ = '0' + ((bits & 128) != 0);
2862 pat = aptr; /* unborrow register */
2863 XPUSHs(sv_2mortal(sv));
2867 if (pat[-1] == '*' || len > (strend - s) * 2)
2868 len = (strend - s) * 2;
2869 sv = NEWSV(35, len + 1);
2872 aptr = pat; /* borrow register */
2874 if (datumtype == 'h') {
2876 for (len = 0; len < aint; len++) {
2881 *pat++ = hexdigit[bits & 15];
2886 for (len = 0; len < aint; len++) {
2891 *pat++ = hexdigit[(bits >> 4) & 15];
2895 pat = aptr; /* unborrow register */
2896 XPUSHs(sv_2mortal(sv));
2899 if (len > strend - s)
2904 if (aint >= 128) /* fake up signed chars */
2914 if (aint >= 128) /* fake up signed chars */
2917 sv_setiv(sv, (IV)aint);
2918 PUSHs(sv_2mortal(sv));
2923 if (len > strend - s)
2938 sv_setiv(sv, (IV)auint);
2939 PUSHs(sv_2mortal(sv));
2944 along = (strend - s) / SIZE16;
2961 sv_setiv(sv, (IV)ashort);
2962 PUSHs(sv_2mortal(sv));
2969 along = (strend - s) / SIZE16;
2974 COPY16(s, &aushort);
2977 if (datumtype == 'n')
2978 aushort = ntohs(aushort);
2981 if (datumtype == 'v')
2982 aushort = vtohs(aushort);
2991 COPY16(s, &aushort);
2995 if (datumtype == 'n')
2996 aushort = ntohs(aushort);
2999 if (datumtype == 'v')
3000 aushort = vtohs(aushort);
3002 sv_setiv(sv, (IV)aushort);
3003 PUSHs(sv_2mortal(sv));
3008 along = (strend - s) / sizeof(int);
3013 Copy(s, &aint, 1, int);
3016 cdouble += (double)aint;
3025 Copy(s, &aint, 1, int);
3028 sv_setiv(sv, (IV)aint);
3029 PUSHs(sv_2mortal(sv));
3034 along = (strend - s) / sizeof(unsigned int);
3039 Copy(s, &auint, 1, unsigned int);
3040 s += sizeof(unsigned int);
3042 cdouble += (double)auint;
3051 Copy(s, &auint, 1, unsigned int);
3052 s += sizeof(unsigned int);
3054 sv_setuv(sv, (UV)auint);
3055 PUSHs(sv_2mortal(sv));
3060 along = (strend - s) / SIZE32;
3068 cdouble += (double)along;
3080 sv_setiv(sv, (IV)along);
3081 PUSHs(sv_2mortal(sv));
3088 along = (strend - s) / SIZE32;
3096 if (datumtype == 'N')
3097 aulong = ntohl(aulong);
3100 if (datumtype == 'V')
3101 aulong = vtohl(aulong);
3104 cdouble += (double)aulong;
3116 if (datumtype == 'N')
3117 aulong = ntohl(aulong);
3120 if (datumtype == 'V')
3121 aulong = vtohl(aulong);
3124 sv_setuv(sv, (UV)aulong);
3125 PUSHs(sv_2mortal(sv));
3130 along = (strend - s) / sizeof(char*);
3136 if (sizeof(char*) > strend - s)
3139 Copy(s, &aptr, 1, char*);
3145 PUSHs(sv_2mortal(sv));
3155 while ((len > 0) && (s < strend)) {
3156 auv = (auv << 7) | (*s & 0x7f);
3157 if (!(*s++ & 0x80)) {
3161 PUSHs(sv_2mortal(sv));
3165 else if (++bytes >= sizeof(UV)) { /* promote to string */
3168 sv = newSVpvf("%.*Vu", (int)TYPE_DIGITS(UV), auv);
3169 while (s < strend) {
3170 sv = mul128(sv, *s & 0x7f);
3171 if (!(*s++ & 0x80)) {
3180 PUSHs(sv_2mortal(sv));
3185 if ((s >= strend) && bytes)
3186 croak("Unterminated compressed integer");
3191 if (sizeof(char*) > strend - s)
3194 Copy(s, &aptr, 1, char*);
3199 sv_setpvn(sv, aptr, len);
3200 PUSHs(sv_2mortal(sv));
3207 if (s + sizeof(Quad_t) > strend)
3210 Copy(s, &aquad, 1, Quad_t);
3211 s += sizeof(Quad_t);
3214 if (aquad >= IV_MIN && aquad <= IV_MAX)
3215 sv_setiv(sv, (IV)aquad);
3217 sv_setnv(sv, (double)aquad);
3218 PUSHs(sv_2mortal(sv));
3225 if (s + sizeof(unsigned Quad_t) > strend)
3228 Copy(s, &auquad, 1, unsigned Quad_t);
3229 s += sizeof(unsigned Quad_t);
3232 if (aquad <= UV_MAX)
3233 sv_setuv(sv, (UV)auquad);
3235 sv_setnv(sv, (double)auquad);
3236 PUSHs(sv_2mortal(sv));
3240 /* float and double added gnb@melba.bby.oz.au 22/11/89 */
3243 along = (strend - s) / sizeof(float);
3248 Copy(s, &afloat, 1, float);
3257 Copy(s, &afloat, 1, float);
3260 sv_setnv(sv, (double)afloat);
3261 PUSHs(sv_2mortal(sv));
3267 along = (strend - s) / sizeof(double);
3272 Copy(s, &adouble, 1, double);
3273 s += sizeof(double);
3281 Copy(s, &adouble, 1, double);
3282 s += sizeof(double);
3284 sv_setnv(sv, (double)adouble);
3285 PUSHs(sv_2mortal(sv));
3290 along = (strend - s) * 3 / 4;
3291 sv = NEWSV(42, along);
3294 while (s < strend && *s > ' ' && *s < 'a') {
3299 len = (*s++ - ' ') & 077;
3301 if (s < strend && *s >= ' ')
3302 a = (*s++ - ' ') & 077;
3305 if (s < strend && *s >= ' ')
3306 b = (*s++ - ' ') & 077;
3309 if (s < strend && *s >= ' ')
3310 c = (*s++ - ' ') & 077;
3313 if (s < strend && *s >= ' ')
3314 d = (*s++ - ' ') & 077;
3317 hunk[0] = a << 2 | b >> 4;
3318 hunk[1] = b << 4 | c >> 2;
3319 hunk[2] = c << 6 | d;
3320 sv_catpvn(sv, hunk, len > 3 ? 3 : len);
3325 else if (s[1] == '\n') /* possible checksum byte */
3328 XPUSHs(sv_2mortal(sv));
3333 if (strchr("fFdD", datumtype) ||
3334 (checksum > 32 && strchr("iIlLN", datumtype)) ) {
3338 while (checksum >= 16) {
3342 while (checksum >= 4) {
3348 along = (1 << checksum) - 1;
3349 while (cdouble < 0.0)
3351 cdouble = modf(cdouble / adouble, &trouble) * adouble;
3352 sv_setnv(sv, cdouble);
3355 if (checksum < 32) {
3356 aulong = (1 << checksum) - 1;
3359 sv_setuv(sv, (UV)culong);
3361 XPUSHs(sv_2mortal(sv));
3365 if (sp == oldsp && gimme == G_SCALAR)
3371 doencodes(sv, s, len)
3379 sv_catpvn(sv, hunk, 1);
3382 hunk[0] = ' ' + (077 & (*s >> 2));
3383 hunk[1] = ' ' + (077 & ((*s << 4) & 060 | (s[1] >> 4) & 017));
3384 hunk[2] = ' ' + (077 & ((s[1] << 2) & 074 | (s[2] >> 6) & 03));
3385 hunk[3] = ' ' + (077 & (s[2] & 077));
3386 sv_catpvn(sv, hunk, 4);
3390 for (s = SvPVX(sv); *s; s++) {
3394 sv_catpvn(sv, "\n", 1);
3402 SV *result = newSVpv("", l);
3403 char *result_c = SvPV(result, na); /* convenience */
3404 char *out = result_c;
3414 SvREFCNT_dec(result);
3437 SvREFCNT_dec(result);
3443 SvCUR_set(result, out - result_c);
3449 SV *pnum; /* must be '\0' terminated */
3453 char *s = SvPV(pnum, len);
3462 i = m * 10 + (*t - '0');
3464 r = (i >> 7); /* r < 10 */
3471 SvCUR_set(pnum, (STRLEN) (t - s));
3478 dSP; dMARK; dORIGMARK; dTARGET;
3479 register SV *cat = TARG;
3482 register char *pat = SvPVx(*++MARK, fromlen);
3483 register char *patend = pat + fromlen;
3488 static char null10[] = {0,0,0,0,0,0,0,0,0,0};
3489 static char *space10 = " ";
3491 /* These must not be in registers: */
3500 unsigned Quad_t auquad;
3508 sv_setpvn(cat, "", 0);
3509 while (pat < patend) {
3510 #define NEXTFROM (items-- > 0 ? *MARK++ : &sv_no)
3511 datumtype = *pat++ & 0xFF;
3512 if (isSPACE(datumtype))
3515 len = strchr("@Xxu", datumtype) ? 0 : items;
3518 else if (isDIGIT(*pat)) {
3520 while (isDIGIT(*pat))
3521 len = (len * 10) + (*pat++ - '0');
3527 croak("Invalid type in pack: '%c'", (int)datumtype);
3529 DIE("%% may only be used in unpack");
3540 if (SvCUR(cat) < len)
3541 DIE("X outside of string");
3548 sv_catpvn(cat, null10, 10);
3551 sv_catpvn(cat, null10, len);
3556 aptr = SvPV(fromstr, fromlen);
3560 sv_catpvn(cat, aptr, len);
3562 sv_catpvn(cat, aptr, fromlen);
3564 if (datumtype == 'A') {
3566 sv_catpvn(cat, space10, 10);
3569 sv_catpvn(cat, space10, len);
3573 sv_catpvn(cat, null10, 10);
3576 sv_catpvn(cat, null10, len);
3583 char *savepat = pat;
3588 aptr = SvPV(fromstr, fromlen);
3593 SvCUR(cat) += (len+7)/8;
3594 SvGROW(cat, SvCUR(cat) + 1);
3595 aptr = SvPVX(cat) + aint;
3600 if (datumtype == 'B') {
3601 for (len = 0; len++ < aint;) {
3602 items |= *pat++ & 1;
3606 *aptr++ = items & 0xff;
3612 for (len = 0; len++ < aint;) {
3618 *aptr++ = items & 0xff;
3624 if (datumtype == 'B')
3625 items <<= 7 - (aint & 7);
3627 items >>= 7 - (aint & 7);
3628 *aptr++ = items & 0xff;
3630 pat = SvPVX(cat) + SvCUR(cat);
3641 char *savepat = pat;
3646 aptr = SvPV(fromstr, fromlen);
3651 SvCUR(cat) += (len+1)/2;
3652 SvGROW(cat, SvCUR(cat) + 1);
3653 aptr = SvPVX(cat) + aint;
3658 if (datumtype == 'H') {
3659 for (len = 0; len++ < aint;) {
3661 items |= ((*pat++ & 15) + 9) & 15;
3663 items |= *pat++ & 15;
3667 *aptr++ = items & 0xff;
3673 for (len = 0; len++ < aint;) {
3675 items |= (((*pat++ & 15) + 9) & 15) << 4;
3677 items |= (*pat++ & 15) << 4;
3681 *aptr++ = items & 0xff;
3687 *aptr++ = items & 0xff;
3688 pat = SvPVX(cat) + SvCUR(cat);
3700 aint = SvIV(fromstr);
3702 sv_catpvn(cat, &achar, sizeof(char));
3705 /* Float and double added by gnb@melba.bby.oz.au 22/11/89 */
3710 afloat = (float)SvNV(fromstr);
3711 sv_catpvn(cat, (char *)&afloat, sizeof (float));
3718 adouble = (double)SvNV(fromstr);
3719 sv_catpvn(cat, (char *)&adouble, sizeof (double));
3725 ashort = (I16)SvIV(fromstr);
3727 ashort = htons(ashort);
3729 CAT16(cat, &ashort);
3735 ashort = (I16)SvIV(fromstr);
3737 ashort = htovs(ashort);
3739 CAT16(cat, &ashort);
3746 ashort = (I16)SvIV(fromstr);
3747 CAT16(cat, &ashort);
3753 auint = SvUV(fromstr);
3754 sv_catpvn(cat, (char*)&auint, sizeof(unsigned int));
3760 adouble = floor(SvNV(fromstr));
3763 croak("Cannot compress negative numbers");
3769 #ifdef CXUX_BROKEN_CONSTANT_CONVERT
3770 adouble <= UV_MAX_cxux
3777 char buf[1 + sizeof(UV)];
3778 char *in = buf + sizeof(buf);
3779 UV auv = U_V(adouble);;
3782 *--in = (auv & 0x7f) | 0x80;
3785 buf[sizeof(buf) - 1] &= 0x7f; /* clear continue bit */
3786 sv_catpvn(cat, in, (buf + sizeof(buf)) - in);
3788 else if (SvPOKp(fromstr)) { /* decimal string arithmetics */
3789 char *from, *result, *in;
3794 /* Copy string and check for compliance */
3795 from = SvPV(fromstr, len);
3796 if ((norm = is_an_int(from, len)) == NULL)
3797 croak("can compress only unsigned integer");
3799 New('w', result, len, char);
3803 *--in = div128(norm, &done) | 0x80;
3804 result[len - 1] &= 0x7F; /* clear continue bit */
3805 sv_catpvn(cat, in, (result + len) - in);
3807 SvREFCNT_dec(norm); /* free norm */
3809 else if (SvNOKp(fromstr)) {
3810 char buf[sizeof(double) * 2]; /* 8/7 <= 2 */
3811 char *in = buf + sizeof(buf);
3814 double next = floor(adouble / 128);
3815 *--in = (unsigned char)(adouble - (next * 128)) | 0x80;
3816 if (--in < buf) /* this cannot happen ;-) */
3817 croak ("Cannot compress integer");
3819 } while (adouble > 0);
3820 buf[sizeof(buf) - 1] &= 0x7f; /* clear continue bit */
3821 sv_catpvn(cat, in, (buf + sizeof(buf)) - in);
3824 croak("Cannot compress non integer");
3830 aint = SvIV(fromstr);
3831 sv_catpvn(cat, (char*)&aint, sizeof(int));
3837 aulong = SvUV(fromstr);
3839 aulong = htonl(aulong);
3841 CAT32(cat, &aulong);
3847 aulong = SvUV(fromstr);
3849 aulong = htovl(aulong);
3851 CAT32(cat, &aulong);
3857 aulong = SvUV(fromstr);
3858 CAT32(cat, &aulong);
3864 along = SvIV(fromstr);
3872 auquad = (unsigned Quad_t)SvIV(fromstr);
3873 sv_catpvn(cat, (char*)&auquad, sizeof(unsigned Quad_t));
3879 aquad = (Quad_t)SvIV(fromstr);
3880 sv_catpvn(cat, (char*)&aquad, sizeof(Quad_t));
3883 #endif /* HAS_QUAD */
3885 len = 1; /* assume SV is correct length */
3890 if (fromstr == &sv_undef)
3893 /* XXX better yet, could spirit away the string to
3894 * a safe spot and hang on to it until the result
3895 * of pack() (and all copies of the result) are
3898 if (dowarn && (SvTEMP(fromstr) || SvPADTMP(fromstr)))
3899 warn("Attempt to pack pointer to temporary value");
3900 if (SvPOK(fromstr) || SvNIOK(fromstr))
3901 aptr = SvPV(fromstr,na);
3903 aptr = SvPV_force(fromstr,na);
3905 sv_catpvn(cat, (char*)&aptr, sizeof(char*));
3910 aptr = SvPV(fromstr, fromlen);
3911 SvGROW(cat, fromlen * 4 / 3);
3916 while (fromlen > 0) {
3923 doencodes(cat, aptr, todo);
3941 register I32 limit = POPi; /* note, negative is forever */
3944 register char *s = SvPV(sv, len);
3945 char *strend = s + len;
3947 register REGEXP *rx;
3951 I32 maxiters = (strend - s) + 10;
3954 I32 origlimit = limit;
3957 AV *oldstack = curstack;
3958 I32 gimme = GIMME_V;
3959 I32 oldsave = savestack_ix;
3962 Copy(&LvTARGOFF(POPs), &pm, 1, PMOP*);
3967 DIE("panic: do_split");
3968 rx = pm->op_pmregexp;
3970 TAINT_IF((pm->op_pmflags & PMf_LOCALE) &&
3971 (pm->op_pmflags & (PMf_WHITE | PMf_SKIPWHITE)));
3973 if (pm->op_pmreplroot)
3974 ary = GvAVn((GV*)pm->op_pmreplroot);
3975 else if (gimme != G_ARRAY)
3977 ary = (AV*)curpad[0];
3980 #endif /* USE_THREADS */
3983 if (ary && (gimme != G_ARRAY || (pm->op_pmflags & PMf_ONCE))) {
3987 for (i = AvFILL(ary); i >= 0; i--)
3988 AvARRAY(ary)[i] = &sv_undef; /* don't free mere refs */
3992 /* temporarily switch stacks */
3993 SWITCHSTACK(curstack, ary);
3995 base = SP - stack_base;
3997 if (pm->op_pmflags & PMf_SKIPWHITE) {
3998 if (pm->op_pmflags & PMf_LOCALE) {
3999 while (isSPACE_LC(*s))
4007 if (pm->op_pmflags & (PMf_MULTILINE|PMf_SINGLELINE)) {
4009 multiline = pm->op_pmflags & PMf_MULTILINE;
4013 limit = maxiters + 2;
4014 if (pm->op_pmflags & PMf_WHITE) {
4017 while (m < strend &&
4018 !((pm->op_pmflags & PMf_LOCALE)
4019 ? isSPACE_LC(*m) : isSPACE(*m)))
4024 dstr = NEWSV(30, m-s);
4025 sv_setpvn(dstr, s, m-s);
4031 while (s < strend &&
4032 ((pm->op_pmflags & PMf_LOCALE)
4033 ? isSPACE_LC(*s) : isSPACE(*s)))
4037 else if (strEQ("^", rx->precomp)) {
4040 for (m = s; m < strend && *m != '\n'; m++) ;
4044 dstr = NEWSV(30, m-s);
4045 sv_setpvn(dstr, s, m-s);
4052 else if (pm->op_pmshort && !rx->nparens) {
4053 i = SvCUR(pm->op_pmshort);
4055 i = *SvPVX(pm->op_pmshort);
4058 for (m = s; m < strend && *m != i; m++) ;
4061 dstr = NEWSV(30, m-s);
4062 sv_setpvn(dstr, s, m-s);
4071 while (s < strend && --limit &&
4072 (m=fbm_instr((unsigned char*)s, (unsigned char*)strend,
4076 dstr = NEWSV(31, m-s);
4077 sv_setpvn(dstr, s, m-s);
4086 maxiters += (strend - s) * rx->nparens;
4087 while (s < strend && --limit &&
4088 pregexec(rx, s, strend, orig, 1, Nullsv, TRUE))
4090 TAINT_IF(rx->exec_tainted);
4092 && rx->subbase != orig) {
4097 strend = s + (strend - m);
4100 dstr = NEWSV(32, m-s);
4101 sv_setpvn(dstr, s, m-s);
4106 for (i = 1; i <= rx->nparens; i++) {
4110 dstr = NEWSV(33, m-s);
4111 sv_setpvn(dstr, s, m-s);
4114 dstr = NEWSV(33, 0);
4123 LEAVE_SCOPE(oldsave);
4124 iters = (SP - stack_base) - base;
4125 if (iters > maxiters)
4128 /* keep field after final delim? */
4129 if (s < strend || (iters && origlimit)) {
4130 dstr = NEWSV(34, strend-s);
4131 sv_setpvn(dstr, s, strend-s);
4137 else if (!origlimit) {
4138 while (iters > 0 && (!TOPs || !SvANY(TOPs) || SvCUR(TOPs) == 0))
4142 SWITCHSTACK(ary, oldstack);
4143 if (gimme == G_ARRAY) {
4145 Copy(AvARRAY(ary), SP + 1, iters, SV*);
4151 if (gimme == G_ARRAY)
4154 if (iters || !pm->op_pmreplroot) {
4164 unlock_condpair(svv)
4168 MAGIC *mg = mg_find((SV*)svv, 'm');
4171 croak("panic: unlock_condpair unlocking non-mutex");
4172 MUTEX_LOCK(MgMUTEXP(mg));
4173 if (MgOWNER(mg) != thr)
4174 croak("panic: unlock_condpair unlocking mutex that we don't own");
4176 COND_SIGNAL(MgOWNERCONDP(mg));
4177 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "0x%lx: unlock 0x%lx\n",
4178 (unsigned long)thr, (unsigned long)svv);)
4179 MUTEX_UNLOCK(MgMUTEXP(mg));
4181 #endif /* USE_THREADS */
4193 mg = condpair_magic(sv);
4194 MUTEX_LOCK(MgMUTEXP(mg));
4195 if (MgOWNER(mg) == thr)
4196 MUTEX_UNLOCK(MgMUTEXP(mg));
4199 COND_WAIT(MgOWNERCONDP(mg), MgMUTEXP(mg));
4201 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "0x%lx: pp_lock lock 0x%lx\n",
4202 (unsigned long)thr, (unsigned long)sv);)
4203 MUTEX_UNLOCK(MgMUTEXP(mg));
4204 save_destructor(unlock_condpair, sv);
4206 #endif /* USE_THREADS */