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 */
108 #ifdef DONT_DECLARE_STD
113 extern pid_t getpid (void);
119 if (GIMME_V == G_SCALAR)
134 if (op->op_private & OPpLVAL_INTRO)
135 SAVECLEARSV(curpad[op->op_targ]);
137 if (op->op_flags & OPf_REF) {
141 if (GIMME == G_ARRAY) {
142 I32 maxarg = AvFILL((AV*)TARG) + 1;
144 if (SvMAGICAL(TARG)) {
146 for (i=0; i < maxarg; i++) {
147 SV **svp = av_fetch((AV*)TARG, i, FALSE);
148 SP[i+1] = (svp) ? *svp : &sv_undef;
152 Copy(AvARRAY((AV*)TARG), SP+1, maxarg, SV*);
157 SV* sv = sv_newmortal();
158 I32 maxarg = AvFILL((AV*)TARG) + 1;
159 sv_setiv(sv, maxarg);
171 if (op->op_private & OPpLVAL_INTRO)
172 SAVECLEARSV(curpad[op->op_targ]);
173 if (op->op_flags & OPf_REF)
176 if (gimme == G_ARRAY) {
177 RETURNOP(do_kv(ARGS));
179 else if (gimme == G_SCALAR) {
180 SV* sv = sv_newmortal();
181 if (HvFILL((HV*)TARG))
182 sv_setpvf(sv, "%ld/%ld",
183 (long)HvFILL((HV*)TARG), (long)HvMAX((HV*)TARG) + 1);
193 DIE("NOT IMPL LINE %d",__LINE__);
205 if (SvTYPE(sv) == SVt_PVIO) {
206 GV *gv = (GV*) sv_newmortal();
207 gv_init(gv, 0, "", 0, 0);
208 GvIOp(gv) = (IO *)sv;
209 (void)SvREFCNT_inc(sv);
211 } else if (SvTYPE(sv) != SVt_PVGV)
212 DIE("Not a GLOB reference");
215 if (SvTYPE(sv) != SVt_PVGV) {
218 if (SvGMAGICAL(sv)) {
224 if (op->op_flags & OPf_REF ||
225 op->op_private & HINT_STRICT_REFS)
226 DIE(no_usym, "a symbol");
232 if (op->op_private & HINT_STRICT_REFS)
233 DIE(no_symref, sym, "a symbol");
234 sv = (SV*)gv_fetchpv(sym, TRUE, SVt_PVGV);
237 if (op->op_private & OPpLVAL_INTRO)
238 save_gp((GV*)sv, !(op->op_flags & OPf_SPECIAL));
250 switch (SvTYPE(sv)) {
254 DIE("Not a SCALAR reference");
261 if (SvTYPE(gv) != SVt_PVGV) {
262 if (SvGMAGICAL(sv)) {
268 if (op->op_flags & OPf_REF ||
269 op->op_private & HINT_STRICT_REFS)
270 DIE(no_usym, "a SCALAR");
276 if (op->op_private & HINT_STRICT_REFS)
277 DIE(no_symref, sym, "a SCALAR");
278 gv = (GV*)gv_fetchpv(sym, TRUE, SVt_PV);
282 if (op->op_flags & OPf_MOD) {
283 if (op->op_private & OPpLVAL_INTRO)
284 sv = save_scalar((GV*)TOPs);
285 else if (op->op_private & OPpDEREF)
286 vivify_ref(sv, op->op_private & OPpDEREF);
296 SV *sv = AvARYLEN(av);
298 AvARYLEN(av) = sv = NEWSV(0,0);
299 sv_upgrade(sv, SVt_IV);
300 sv_magic(sv, (SV*)av, '#', Nullch, 0);
308 djSP; dTARGET; dPOPss;
310 if (op->op_flags & OPf_MOD) {
311 if (SvTYPE(TARG) < SVt_PVLV) {
312 sv_upgrade(TARG, SVt_PVLV);
313 sv_magic(TARG, Nullsv, '.', Nullch, 0);
318 PUSHs(TARG); /* no SvSETMAGIC */
324 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
325 mg = mg_find(sv, 'g');
326 if (mg && mg->mg_len >= 0) {
327 PUSHi(mg->mg_len + curcop->cop_arybase);
341 /* We usually try to add a non-existent subroutine in case of AUTOLOAD. */
342 /* (But not in defined().) */
343 CV *cv = sv_2cv(TOPs, &stash, &gv, !(op->op_flags & OPf_SPECIAL));
346 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
363 cv = sv_2cv(TOPs, &stash, &gv, FALSE);
365 ret = sv_2mortal(newSVpv(SvPVX(cv), SvCUR(cv)));
373 CV* cv = (CV*)curpad[op->op_targ];
375 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
391 if (GIMME != G_ARRAY) {
395 EXTEND_MORTAL(SP - MARK);
397 *MARK = refto(*MARK);
406 if (SvTYPE(sv) == SVt_PVLV && LvTYPE(sv) == 'y') {
409 if (!(sv = LvTARG(sv)))
412 else if (SvPADTMP(sv))
416 (void)SvREFCNT_inc(sv);
419 sv_upgrade(rv, SVt_RV);
433 if (sv && SvGMAGICAL(sv))
436 if (!sv || !SvROK(sv))
440 pv = sv_reftype(sv,TRUE);
441 PUSHp(pv, strlen(pv));
451 stash = curcop->cop_stash;
453 stash = gv_stashsv(POPs, TRUE);
455 (void)sv_bless(TOPs, stash);
472 switch (elem ? *elem : '\0')
475 if (strEQ(elem, "ARRAY"))
479 if (strEQ(elem, "CODE"))
480 ref = (SV*)GvCVu(gv);
483 if (strEQ(elem, "FILEHANDLE")) /* XXX deprecate in 5.005 */
484 ref = (SV*)GvIOp(gv);
487 if (strEQ(elem, "GLOB"))
491 if (strEQ(elem, "HASH"))
495 if (strEQ(elem, "IO"))
496 ref = (SV*)GvIOp(gv);
499 if (strEQ(elem, "NAME"))
500 sv = newSVpv(GvNAME(gv), GvNAMELEN(gv));
503 if (strEQ(elem, "PACKAGE"))
504 sv = newSVpv(HvNAME(GvSTASH(gv)), 0);
507 if (strEQ(elem, "SCALAR"))
521 /* Pattern matching */
526 register UNOP *unop = cUNOP;
527 register unsigned char *s;
530 register I32 *sfirst;
534 if(unop->op_first && unop->op_first->op_type == OP_PUSHRE) {
535 PMOP *pm = (PMOP *)unop->op_first;
536 SV *rv = sv_newmortal();
537 sv = newSVrv(rv, "Regexp");
538 sv_magic(sv,(SV*)ReREFCNT_inc(pm->op_pmregexp),'r',0,0);
542 if (sv == lastscream) {
548 SvSCREAM_off(lastscream);
549 SvREFCNT_dec(lastscream);
551 lastscream = SvREFCNT_inc(sv);
554 s = (unsigned char*)(SvPV(sv, len));
558 if (pos > maxscream) {
560 maxscream = pos + 80;
561 New(301, screamfirst, 256, I32);
562 New(302, screamnext, maxscream, I32);
565 maxscream = pos + pos / 4;
566 Renew(screamnext, maxscream, I32);
570 sfirst = screamfirst;
573 if (!sfirst || !snext)
574 DIE("do_study: out of memory");
576 for (ch = 256; ch; --ch)
583 snext[pos] = sfirst[ch] - pos;
590 sv_magic(sv, Nullsv, 'g', Nullch, 0); /* piggyback on m//g magic */
599 if (op->op_flags & OPf_STACKED)
605 TARG = sv_newmortal();
606 PUSHi(do_trans(sv, op));
610 /* Lvalue operators. */
622 djSP; dMARK; dTARGET;
632 SETi(do_chomp(TOPs));
638 djSP; dMARK; dTARGET;
639 register I32 count = 0;
642 count += do_chomp(POPs);
653 if (!sv || !SvANY(sv))
655 switch (SvTYPE(sv)) {
657 if (AvMAX(sv) >= 0 || SvGMAGICAL(sv))
661 if (HvARRAY(sv) || SvGMAGICAL(sv))
665 if (CvROOT(sv) || CvXSUB(sv))
682 if (!op->op_private) {
691 if (SvTHINKFIRST(sv)) {
698 switch (SvTYPE(sv)) {
708 if (cv_const_sv((CV*)sv))
709 warn("Constant subroutine %s undefined",
710 CvANON((CV*)sv) ? "(anonymous)" : GvENAME(CvGV((CV*)sv)));
713 { GV* gv = (GV*)SvREFCNT_inc(CvGV((CV*)sv));
715 CvGV((CV*)sv) = gv; } /* let user-undef'd sub keep its identity */
719 sv_setsv(sv, &sv_undef);
722 if (SvTYPE(sv) >= SVt_PV && SvPVX(sv) && SvLEN(sv)) {
725 SvPV_set(sv, Nullch);
738 if (SvREADONLY(TOPs) || SvTYPE(TOPs) > SVt_PVLV)
740 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) &&
741 SvIVX(TOPs) != IV_MIN)
744 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
755 if (SvREADONLY(TOPs) || SvTYPE(TOPs) > SVt_PVLV)
757 sv_setsv(TARG, TOPs);
758 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) &&
759 SvIVX(TOPs) != IV_MAX)
762 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
776 if(SvREADONLY(TOPs) || SvTYPE(TOPs) > SVt_PVLV)
778 sv_setsv(TARG, TOPs);
779 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) &&
780 SvIVX(TOPs) != IV_MIN)
783 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
792 /* Ordinary operators. */
796 djSP; dATARGET; tryAMAGICbin(pow,opASSIGN);
799 SETn( pow( left, right) );
806 djSP; dATARGET; tryAMAGICbin(mult,opASSIGN);
809 SETn( left * right );
816 djSP; dATARGET; tryAMAGICbin(div,opASSIGN);
821 DIE("Illegal division by zero");
823 /* insure that 20./5. == 4. */
826 if ((double)I_V(left) == left &&
827 (double)I_V(right) == right &&
828 (k = I_V(left)/I_V(right))*I_V(right) == I_V(left)) {
831 value = left / right;
835 value = left / right;
844 djSP; dATARGET; tryAMAGICbin(mod,opASSIGN);
852 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)) {
854 right = (right_neg = (i < 0)) ? -i : i;
858 right = U_V((right_neg = (n < 0)) ? -n : n);
861 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)) {
863 left = (left_neg = (i < 0)) ? -i : i;
867 left = U_V((left_neg = (n < 0)) ? -n : n);
871 DIE("Illegal modulus zero");
874 if ((left_neg != right_neg) && ans)
877 /* XXX may warn: unary minus operator applied to unsigned type */
878 /* could change -foo to be (~foo)+1 instead */
879 if (ans <= ~((UV)IV_MAX)+1)
880 sv_setiv(TARG, ~ans+1);
882 sv_setnv(TARG, -(double)ans);
893 djSP; dATARGET; tryAMAGICbin(repeat,opASSIGN);
895 register I32 count = POPi;
896 if (GIMME == G_ARRAY && op->op_private & OPpREPEAT_DOLIST) {
898 I32 items = SP - MARK;
910 repeatcpy((char*)(MARK + items), (char*)MARK,
911 items * sizeof(SV*), count - 1);
917 else { /* Note: mark already snarfed by pp_list */
922 if (TARG == tmpstr && SvTHINKFIRST(tmpstr)) {
923 if (SvREADONLY(tmpstr) && curcop != &compiling)
924 DIE("Can't x= to readonly value");
928 SvSetSV(TARG, tmpstr);
929 SvPV_force(TARG, len);
934 SvGROW(TARG, (count * len) + 1);
935 repeatcpy(SvPVX(TARG) + len, SvPVX(TARG), len, count - 1);
936 SvCUR(TARG) *= count;
940 (void)SvPOK_only(TARG);
949 djSP; dATARGET; tryAMAGICbin(subtr,opASSIGN);
952 SETn( left - right );
959 djSP; dATARGET; tryAMAGICbin(lshift,opASSIGN);
962 if (op->op_private & HINT_INTEGER) {
978 djSP; dATARGET; tryAMAGICbin(rshift,opASSIGN);
981 if (op->op_private & HINT_INTEGER) {
997 djSP; tryAMAGICbinSET(lt,0);
1000 SETs(boolSV(TOPn < value));
1007 djSP; tryAMAGICbinSET(gt,0);
1010 SETs(boolSV(TOPn > value));
1017 djSP; tryAMAGICbinSET(le,0);
1020 SETs(boolSV(TOPn <= value));
1027 djSP; tryAMAGICbinSET(ge,0);
1030 SETs(boolSV(TOPn >= value));
1037 djSP; tryAMAGICbinSET(ne,0);
1040 SETs(boolSV(TOPn != value));
1047 djSP; dTARGET; tryAMAGICbin(ncmp,0);
1054 else if (left < right)
1056 else if (left > right)
1069 djSP; tryAMAGICbinSET(slt,0);
1072 int cmp = ((op->op_private & OPpLOCALE)
1073 ? sv_cmp_locale(left, right)
1074 : sv_cmp(left, right));
1075 SETs(boolSV(cmp < 0));
1082 djSP; tryAMAGICbinSET(sgt,0);
1085 int cmp = ((op->op_private & OPpLOCALE)
1086 ? sv_cmp_locale(left, right)
1087 : sv_cmp(left, right));
1088 SETs(boolSV(cmp > 0));
1095 djSP; tryAMAGICbinSET(sle,0);
1098 int cmp = ((op->op_private & OPpLOCALE)
1099 ? sv_cmp_locale(left, right)
1100 : sv_cmp(left, right));
1101 SETs(boolSV(cmp <= 0));
1108 djSP; tryAMAGICbinSET(sge,0);
1111 int cmp = ((op->op_private & OPpLOCALE)
1112 ? sv_cmp_locale(left, right)
1113 : sv_cmp(left, right));
1114 SETs(boolSV(cmp >= 0));
1121 djSP; tryAMAGICbinSET(seq,0);
1124 SETs(boolSV(sv_eq(left, right)));
1131 djSP; tryAMAGICbinSET(sne,0);
1134 SETs(boolSV(!sv_eq(left, right)));
1141 djSP; dTARGET; tryAMAGICbin(scmp,0);
1144 int cmp = ((op->op_private & OPpLOCALE)
1145 ? sv_cmp_locale(left, right)
1146 : sv_cmp(left, right));
1154 djSP; dATARGET; tryAMAGICbin(band,opASSIGN);
1157 if (SvNIOKp(left) || SvNIOKp(right)) {
1158 if (op->op_private & HINT_INTEGER) {
1159 IBW value = SvIV(left) & SvIV(right);
1163 UBW value = SvUV(left) & SvUV(right);
1168 do_vop(op->op_type, TARG, left, right);
1177 djSP; dATARGET; tryAMAGICbin(bxor,opASSIGN);
1180 if (SvNIOKp(left) || SvNIOKp(right)) {
1181 if (op->op_private & HINT_INTEGER) {
1182 IBW value = (USE_LEFT(left) ? SvIV(left) : 0) ^ SvIV(right);
1186 UBW value = (USE_LEFT(left) ? SvUV(left) : 0) ^ SvUV(right);
1191 do_vop(op->op_type, TARG, left, right);
1200 djSP; dATARGET; tryAMAGICbin(bor,opASSIGN);
1203 if (SvNIOKp(left) || SvNIOKp(right)) {
1204 if (op->op_private & HINT_INTEGER) {
1205 IBW value = (USE_LEFT(left) ? SvIV(left) : 0) | SvIV(right);
1209 UBW value = (USE_LEFT(left) ? SvUV(left) : 0) | SvUV(right);
1214 do_vop(op->op_type, TARG, left, right);
1223 djSP; dTARGET; tryAMAGICun(neg);
1228 if (SvIOKp(sv) && !SvNOKp(sv) && !SvPOKp(sv) && SvIVX(sv) != IV_MIN)
1230 else if (SvNIOKp(sv))
1232 else if (SvPOKp(sv)) {
1234 char *s = SvPV(sv, len);
1235 if (isIDFIRST(*s)) {
1236 sv_setpvn(TARG, "-", 1);
1239 else if (*s == '+' || *s == '-') {
1241 *SvPV_force(TARG, len) = *s == '-' ? '+' : '-';
1244 sv_setnv(TARG, -SvNV(sv));
1256 djSP; tryAMAGICunSET(not);
1257 #endif /* OVERLOAD */
1258 *stack_sp = boolSV(!SvTRUE(*stack_sp));
1264 djSP; dTARGET; tryAMAGICun(compl);
1268 if (op->op_private & HINT_INTEGER) {
1269 IBW value = ~SvIV(sv);
1273 UBW value = ~SvUV(sv);
1278 register char *tmps;
1279 register long *tmpl;
1284 tmps = SvPV_force(TARG, len);
1287 for ( ; anum && (unsigned long)tmps % sizeof(long); anum--, tmps++)
1290 for ( ; anum >= sizeof(long); anum -= sizeof(long), tmpl++)
1294 for ( ; anum > 0; anum--, tmps++)
1303 /* integer versions of some of the above */
1307 djSP; dATARGET; tryAMAGICbin(mult,opASSIGN);
1310 SETi( left * right );
1317 djSP; dATARGET; tryAMAGICbin(div,opASSIGN);
1321 DIE("Illegal division by zero");
1322 value = POPi / value;
1330 djSP; dATARGET; tryAMAGICbin(mod,opASSIGN);
1334 DIE("Illegal modulus zero");
1335 SETi( left % right );
1342 djSP; dATARGET; tryAMAGICbin(add,opASSIGN);
1345 SETi( left + right );
1352 djSP; dATARGET; tryAMAGICbin(subtr,opASSIGN);
1355 SETi( left - right );
1362 djSP; tryAMAGICbinSET(lt,0);
1365 SETs(boolSV(left < right));
1372 djSP; tryAMAGICbinSET(gt,0);
1375 SETs(boolSV(left > right));
1382 djSP; tryAMAGICbinSET(le,0);
1385 SETs(boolSV(left <= right));
1392 djSP; tryAMAGICbinSET(ge,0);
1395 SETs(boolSV(left >= right));
1402 djSP; tryAMAGICbinSET(eq,0);
1405 SETs(boolSV(left == right));
1412 djSP; tryAMAGICbinSET(ne,0);
1415 SETs(boolSV(left != right));
1422 djSP; dTARGET; tryAMAGICbin(ncmp,0);
1429 else if (left < right)
1440 djSP; dTARGET; tryAMAGICun(neg);
1445 /* High falutin' math. */
1449 djSP; dTARGET; tryAMAGICbin(atan2,0);
1452 SETn(atan2(left, right));
1459 djSP; dTARGET; tryAMAGICun(sin);
1471 djSP; dTARGET; tryAMAGICun(cos);
1491 if (!srand_called) {
1492 (void)srand((unsigned)seed());
1493 srand_called = TRUE;
1496 value = rand() * value / 2147483648.0;
1499 value = rand() * value / 65536.0;
1502 value = rand() * value / 32768.0;
1504 value = rand() * value / (double)(((unsigned long)1) << RANDBITS);
1520 (void)srand((unsigned)anum);
1521 srand_called = TRUE;
1530 * This is really just a quick hack which grabs various garbage
1531 * values. It really should be a real hash algorithm which
1532 * spreads the effect of every input bit onto every output bit,
1533 * if someone who knows about such tings would bother to write it.
1534 * Might be a good idea to add that function to CORE as well.
1535 * No numbers below come from careful analysis or anyting here,
1536 * except they are primes and SEED_C1 > 1E6 to get a full-width
1537 * value from (tv_sec * SEED_C1 + tv_usec). The multipliers should
1538 * probably be bigger too.
1541 # define SEED_C1 1000003
1542 #define SEED_C4 73819
1544 # define SEED_C1 25747
1545 #define SEED_C4 20639
1549 #define SEED_C5 26107
1554 # include <starlet.h>
1555 /* when[] = (low 32 bits, high 32 bits) of time since epoch
1556 * in 100-ns units, typically incremented ever 10 ms. */
1557 unsigned int when[2];
1558 _ckvmssts(sys$gettim(when));
1559 u = (U32)SEED_C1 * when[0] + (U32)SEED_C2 * when[1];
1561 # ifdef HAS_GETTIMEOFDAY
1562 struct timeval when;
1563 gettimeofday(&when,(struct timezone *) 0);
1564 u = (U32)SEED_C1 * when.tv_sec + (U32)SEED_C2 * when.tv_usec;
1568 u = (U32)SEED_C1 * when;
1571 u += SEED_C3 * (U32)getpid();
1572 u += SEED_C4 * (U32)(UV)stack_sp;
1573 #ifndef PLAN9 /* XXX Plan9 assembler chokes on this; fix needed */
1574 u += SEED_C5 * (U32)(UV)&when;
1581 djSP; dTARGET; tryAMAGICun(exp);
1593 djSP; dTARGET; tryAMAGICun(log);
1598 SET_NUMERIC_STANDARD();
1599 DIE("Can't take log of %g", value);
1609 djSP; dTARGET; tryAMAGICun(sqrt);
1614 SET_NUMERIC_STANDARD();
1615 DIE("Can't take sqrt of %g", value);
1617 value = sqrt(value);
1627 double value = TOPn;
1630 if (SvIOKp(TOPs) && !SvNOKp(TOPs) && !SvPOKp(TOPs)) {
1636 (void)modf(value, &value);
1638 (void)modf(-value, &value);
1653 djSP; dTARGET; tryAMAGICun(abs);
1655 double value = TOPn;
1658 if (SvIOKp(TOPs) && !SvNOKp(TOPs) && !SvPOKp(TOPs) &&
1659 (iv = SvIVX(TOPs)) != IV_MIN) {
1680 XPUSHu(scan_hex(tmps, 99, &argtype));
1692 while (*tmps && isSPACE(*tmps))
1697 value = scan_hex(++tmps, 99, &argtype);
1699 value = scan_oct(tmps, 99, &argtype);
1709 SETi( sv_len(TOPs) );
1722 I32 lvalue = op->op_flags & OPf_MOD;
1724 I32 arybase = curcop->cop_arybase;
1730 tmps = SvPV(sv, curlen);
1731 if (pos >= arybase) {
1749 else if (len >= 0) {
1751 if (rem > (I32)curlen)
1765 if (dowarn || lvalue)
1766 warn("substr outside of string");
1771 sv_setpvn(TARG, tmps, rem);
1772 if (lvalue) { /* it's an lvalue! */
1773 if (!SvGMAGICAL(sv)) {
1777 warn("Attempt to use reference as lvalue in substr");
1779 if (SvOK(sv)) /* is it defined ? */
1780 (void)SvPOK_only(sv);
1782 sv_setpvn(sv,"",0); /* avoid lexical reincarnation */
1785 if (SvTYPE(TARG) < SVt_PVLV) {
1786 sv_upgrade(TARG, SVt_PVLV);
1787 sv_magic(TARG, Nullsv, 'x', Nullch, 0);
1792 LvTARGOFF(TARG) = pos;
1793 LvTARGLEN(TARG) = rem;
1796 PUSHs(TARG); /* avoid SvSETMAGIC here */
1803 register I32 size = POPi;
1804 register I32 offset = POPi;
1805 register SV *src = POPs;
1806 I32 lvalue = op->op_flags & OPf_MOD;
1808 unsigned char *s = (unsigned char*)SvPV(src, srclen);
1809 unsigned long retnum;
1812 offset *= size; /* turn into bit offset */
1813 len = (offset + size + 7) / 8;
1814 if (offset < 0 || size < 1)
1817 if (lvalue) { /* it's an lvalue! */
1818 if (SvTYPE(TARG) < SVt_PVLV) {
1819 sv_upgrade(TARG, SVt_PVLV);
1820 sv_magic(TARG, Nullsv, 'v', Nullch, 0);
1825 LvTARGOFF(TARG) = offset;
1826 LvTARGLEN(TARG) = size;
1834 if (offset >= srclen)
1837 retnum = (unsigned long) s[offset] << 8;
1839 else if (size == 32) {
1840 if (offset >= srclen)
1842 else if (offset + 1 >= srclen)
1843 retnum = (unsigned long) s[offset] << 24;
1844 else if (offset + 2 >= srclen)
1845 retnum = ((unsigned long) s[offset] << 24) +
1846 ((unsigned long) s[offset + 1] << 16);
1848 retnum = ((unsigned long) s[offset] << 24) +
1849 ((unsigned long) s[offset + 1] << 16) +
1850 (s[offset + 2] << 8);
1855 retnum = (s[offset >> 3] >> (offset & 7)) & ((1 << size) - 1);
1860 else if (size == 16)
1861 retnum = ((unsigned long) s[offset] << 8) + s[offset+1];
1862 else if (size == 32)
1863 retnum = ((unsigned long) s[offset] << 24) +
1864 ((unsigned long) s[offset + 1] << 16) +
1865 (s[offset + 2] << 8) + s[offset+3];
1869 sv_setiv(TARG, (IV)retnum);
1884 I32 arybase = curcop->cop_arybase;
1889 offset = POPi - arybase;
1892 tmps = SvPV(big, biglen);
1895 else if (offset > biglen)
1897 if (!(tmps2 = fbm_instr((unsigned char*)tmps + offset,
1898 (unsigned char*)tmps + biglen, little)))
1899 retval = -1 + arybase;
1901 retval = tmps2 - tmps + arybase;
1918 I32 arybase = curcop->cop_arybase;
1924 tmps2 = SvPV(little, llen);
1925 tmps = SvPV(big, blen);
1929 offset = SvIV(offstr) - arybase + llen;
1932 else if (offset > blen)
1934 if (!(tmps2 = rninstr(tmps, tmps + offset,
1935 tmps2, tmps2 + llen)))
1936 retval = -1 + arybase;
1938 retval = tmps2 - tmps + arybase;
1945 djSP; dMARK; dORIGMARK; dTARGET;
1946 #ifdef USE_LOCALE_NUMERIC
1947 if (op->op_private & OPpLOCALE)
1948 SET_NUMERIC_LOCAL();
1950 SET_NUMERIC_STANDARD();
1952 do_sprintf(TARG, SP-MARK, MARK+1);
1953 TAINT_IF(SvTAINTED(TARG));
1967 value = (I32) (*tmps & 255);
1972 value = (I32) (anum & 255);
1983 (void)SvUPGRADE(TARG,SVt_PV);
1989 (void)SvPOK_only(TARG);
1996 djSP; dTARGET; dPOPTOPssrl;
1998 char *tmps = SvPV(left, na);
2000 sv_setpv(TARG, fcrypt(tmps, SvPV(right, na)));
2002 sv_setpv(TARG, crypt(tmps, SvPV(right, na)));
2006 "The crypt() function is unimplemented due to excessive paranoia.");
2018 if (!SvPADTMP(sv)) {
2024 s = SvPV_force(sv, na);
2026 if (op->op_private & OPpLOCALE) {
2029 *s = toUPPER_LC(*s);
2044 if (!SvPADTMP(sv)) {
2050 s = SvPV_force(sv, na);
2052 if (op->op_private & OPpLOCALE) {
2055 *s = toLOWER_LC(*s);
2072 if (!SvPADTMP(sv)) {
2079 s = SvPV_force(sv, len);
2081 register char *send = s + len;
2083 if (op->op_private & OPpLOCALE) {
2086 for (; s < send; s++)
2087 *s = toUPPER_LC(*s);
2090 for (; s < send; s++)
2104 if (!SvPADTMP(sv)) {
2111 s = SvPV_force(sv, len);
2113 register char *send = s + len;
2115 if (op->op_private & OPpLOCALE) {
2118 for (; s < send; s++)
2119 *s = toLOWER_LC(*s);
2122 for (; s < send; s++)
2134 register char *s = SvPV(sv,len);
2138 (void)SvUPGRADE(TARG, SVt_PV);
2139 SvGROW(TARG, (len * 2) + 1);
2147 SvCUR_set(TARG, d - SvPVX(TARG));
2148 (void)SvPOK_only(TARG);
2151 sv_setpvn(TARG, s, len);
2160 djSP; dMARK; dORIGMARK;
2162 register AV* av = (AV*)POPs;
2163 register I32 lval = op->op_flags & OPf_MOD;
2164 I32 arybase = curcop->cop_arybase;
2167 if (SvTYPE(av) == SVt_PVAV) {
2168 if (lval && op->op_private & OPpLVAL_INTRO) {
2170 for (svp = mark + 1; svp <= sp; svp++) {
2175 if (max > AvMAX(av))
2178 while (++MARK <= SP) {
2179 elem = SvIVx(*MARK);
2183 svp = av_fetch(av, elem, lval);
2185 if (!svp || *svp == &sv_undef)
2186 DIE(no_aelem, elem);
2187 if (op->op_private & OPpLVAL_INTRO)
2190 *MARK = svp ? *svp : &sv_undef;
2193 if (GIMME != G_ARRAY) {
2201 /* Associative arrays. */
2206 HV *hash = (HV*)POPs;
2208 I32 gimme = GIMME_V;
2209 I32 realhv = (SvTYPE(hash) == SVt_PVHV);
2212 /* might clobber stack_sp */
2213 entry = realhv ? hv_iternext(hash) : avhv_iternext((AV*)hash);
2218 PUSHs(hv_iterkeysv(entry)); /* won't clobber stack_sp */
2219 if (gimme == G_ARRAY) {
2221 /* might clobber stack_sp */
2222 sv_setsv(TARG, realhv ?
2223 hv_iterval(hash, entry) : avhv_iterval((AV*)hash, entry));
2228 else if (gimme == G_SCALAR)
2247 I32 gimme = GIMME_V;
2248 I32 discard = (gimme == G_VOID) ? G_DISCARD : 0;
2252 if (op->op_private & OPpSLICE) {
2256 hvtype = SvTYPE(hv);
2257 while (++MARK <= SP) {
2258 if (hvtype == SVt_PVHV)
2259 sv = hv_delete_ent(hv, *MARK, discard, 0);
2260 else if (hvtype == SVt_PVAV)
2261 sv = avhv_delete_ent((AV*)hv, *MARK, discard, 0);
2263 DIE("Not a HASH reference");
2264 *MARK = sv ? sv : &sv_undef;
2268 else if (gimme == G_SCALAR) {
2277 if (SvTYPE(hv) == SVt_PVHV)
2278 sv = hv_delete_ent(hv, keysv, discard, 0);
2279 else if (SvTYPE(hv) == SVt_PVAV)
2280 sv = avhv_delete_ent((AV*)hv, keysv, discard, 0);
2282 DIE("Not a HASH reference");
2296 if (SvTYPE(hv) == SVt_PVHV) {
2297 if (hv_exists_ent(hv, tmpsv, 0))
2299 } else if (SvTYPE(hv) == SVt_PVAV) {
2300 if (avhv_exists_ent((AV*)hv, tmpsv, 0))
2303 DIE("Not a HASH reference");
2310 djSP; dMARK; dORIGMARK;
2312 register HV *hv = (HV*)POPs;
2313 register I32 lval = op->op_flags & OPf_MOD;
2314 I32 realhv = (SvTYPE(hv) == SVt_PVHV);
2316 if (realhv || SvTYPE(hv) == SVt_PVAV) {
2317 while (++MARK <= SP) {
2321 he = hv_fetch_ent(hv, keysv, lval, 0);
2322 svp = he ? &HeVAL(he) : 0;
2324 svp = avhv_fetch_ent((AV*)hv, keysv, lval, 0);
2327 if (!he || HeVAL(he) == &sv_undef)
2328 DIE(no_helem, SvPV(keysv, na));
2329 if (op->op_private & OPpLVAL_INTRO)
2330 save_svref(&HeVAL(he));
2332 *MARK = he ? HeVAL(he) : &sv_undef;
2335 if (GIMME != G_ARRAY) {
2343 /* List operators. */
2348 if (GIMME != G_ARRAY) {
2350 *MARK = *SP; /* unwanted list, return last item */
2361 SV **lastrelem = stack_sp;
2362 SV **lastlelem = stack_base + POPMARK;
2363 SV **firstlelem = stack_base + POPMARK + 1;
2364 register SV **firstrelem = lastlelem + 1;
2365 I32 arybase = curcop->cop_arybase;
2366 I32 lval = op->op_flags & OPf_MOD;
2367 I32 is_something_there = lval;
2369 register I32 max = lastrelem - lastlelem;
2370 register SV **lelem;
2373 if (GIMME != G_ARRAY) {
2374 ix = SvIVx(*lastlelem);
2379 if (ix < 0 || ix >= max)
2380 *firstlelem = &sv_undef;
2382 *firstlelem = firstrelem[ix];
2388 SP = firstlelem - 1;
2392 for (lelem = firstlelem; lelem <= lastlelem; lelem++) {
2398 else if (!(*lelem = firstrelem[ix]))
2403 if (ix >= max || !(*lelem = firstrelem[ix]))
2406 if (!is_something_there && (SvOK(*lelem) || SvGMAGICAL(*lelem)))
2407 is_something_there = TRUE;
2409 if (is_something_there)
2412 SP = firstlelem - 1;
2418 djSP; dMARK; dORIGMARK;
2419 I32 items = SP - MARK;
2420 SV *av = sv_2mortal((SV*)av_make(items, MARK+1));
2421 SP = ORIGMARK; /* av_make() might realloc stack_sp */
2428 djSP; dMARK; dORIGMARK;
2429 HV* hv = (HV*)sv_2mortal((SV*)newHV());
2433 SV *val = NEWSV(46, 0);
2435 sv_setsv(val, *++MARK);
2437 warn("Odd number of elements in hash list");
2438 (void)hv_store_ent(hv,key,val,0);
2447 djSP; dMARK; dORIGMARK;
2448 register AV *ary = (AV*)*++MARK;
2452 register I32 offset;
2453 register I32 length;
2460 if (SvRMAGICAL(ary) && (mg = mg_find((SV*)ary,'P'))) {
2461 *MARK-- = mg->mg_obj;
2464 perl_call_method("SPLICE",GIMME_V);
2472 offset = i = SvIVx(*MARK);
2474 offset += AvFILLp(ary) + 1;
2476 offset -= curcop->cop_arybase;
2480 length = SvIVx(*MARK++);
2485 length = AvMAX(ary) + 1; /* close enough to infinity */
2489 length = AvMAX(ary) + 1;
2491 if (offset > AvFILLp(ary) + 1)
2492 offset = AvFILLp(ary) + 1;
2493 after = AvFILLp(ary) + 1 - (offset + length);
2494 if (after < 0) { /* not that much array */
2495 length += after; /* offset+length now in array */
2501 /* At this point, MARK .. SP-1 is our new LIST */
2504 diff = newlen - length;
2505 if (newlen && !AvREAL(ary)) {
2509 assert(AvREAL(ary)); /* would leak, so croak */
2512 if (diff < 0) { /* shrinking the area */
2514 New(451, tmparyval, newlen, SV*); /* so remember insertion */
2515 Copy(MARK, tmparyval, newlen, SV*);
2518 MARK = ORIGMARK + 1;
2519 if (GIMME == G_ARRAY) { /* copy return vals to stack */
2520 MEXTEND(MARK, length);
2521 Copy(AvARRAY(ary)+offset, MARK, length, SV*);
2523 EXTEND_MORTAL(length);
2524 for (i = length, dst = MARK; i; i--) {
2525 if (!SvIMMORTAL(*dst))
2526 sv_2mortal(*dst); /* free them eventualy */
2533 *MARK = AvARRAY(ary)[offset+length-1];
2535 if (!SvIMMORTAL(*MARK))
2537 for (i = length - 1, dst = &AvARRAY(ary)[offset]; i > 0; i--)
2538 SvREFCNT_dec(*dst++); /* free them now */
2541 AvFILLp(ary) += diff;
2543 /* pull up or down? */
2545 if (offset < after) { /* easier to pull up */
2546 if (offset) { /* esp. if nothing to pull */
2547 src = &AvARRAY(ary)[offset-1];
2548 dst = src - diff; /* diff is negative */
2549 for (i = offset; i > 0; i--) /* can't trust Copy */
2553 SvPVX(ary) = (char*)(AvARRAY(ary) - diff); /* diff is negative */
2557 if (after) { /* anything to pull down? */
2558 src = AvARRAY(ary) + offset + length;
2559 dst = src + diff; /* diff is negative */
2560 Move(src, dst, after, SV*);
2562 dst = &AvARRAY(ary)[AvFILLp(ary)+1];
2563 /* avoid later double free */
2567 dst[--i] = &sv_undef;
2570 for (src = tmparyval, dst = AvARRAY(ary) + offset;
2572 *dst = NEWSV(46, 0);
2573 sv_setsv(*dst++, *src++);
2575 Safefree(tmparyval);
2578 else { /* no, expanding (or same) */
2580 New(452, tmparyval, length, SV*); /* so remember deletion */
2581 Copy(AvARRAY(ary)+offset, tmparyval, length, SV*);
2584 if (diff > 0) { /* expanding */
2586 /* push up or down? */
2588 if (offset < after && diff <= AvARRAY(ary) - AvALLOC(ary)) {
2592 Move(src, dst, offset, SV*);
2594 SvPVX(ary) = (char*)(AvARRAY(ary) - diff);/* diff is positive */
2596 AvFILLp(ary) += diff;
2599 if (AvFILLp(ary) + diff >= AvMAX(ary)) /* oh, well */
2600 av_extend(ary, AvFILLp(ary) + diff);
2601 AvFILLp(ary) += diff;
2604 dst = AvARRAY(ary) + AvFILLp(ary);
2606 for (i = after; i; i--) {
2613 for (src = MARK, dst = AvARRAY(ary) + offset; newlen; newlen--) {
2614 *dst = NEWSV(46, 0);
2615 sv_setsv(*dst++, *src++);
2617 MARK = ORIGMARK + 1;
2618 if (GIMME == G_ARRAY) { /* copy return vals to stack */
2620 Copy(tmparyval, MARK, length, SV*);
2622 EXTEND_MORTAL(length);
2623 for (i = length, dst = MARK; i; i--) {
2624 if (!SvIMMORTAL(*dst))
2625 sv_2mortal(*dst); /* free them eventualy */
2629 Safefree(tmparyval);
2633 else if (length--) {
2634 *MARK = tmparyval[length];
2636 if (!SvIMMORTAL(*MARK))
2638 while (length-- > 0)
2639 SvREFCNT_dec(tmparyval[length]);
2641 Safefree(tmparyval);
2652 djSP; dMARK; dORIGMARK; dTARGET;
2653 register AV *ary = (AV*)*++MARK;
2654 register SV *sv = &sv_undef;
2657 if (SvRMAGICAL(ary) && (mg = mg_find((SV*)ary,'P'))) {
2658 *MARK-- = mg->mg_obj;
2661 perl_call_method("PUSH",GIMME_V);
2666 /* Why no pre-extend of ary here ? */
2667 for (++MARK; MARK <= SP; MARK++) {
2670 sv_setsv(sv, *MARK);
2674 PUSHi( AvFILL(ary) + 1 );
2682 SV *sv = av_pop(av);
2683 if (!SvIMMORTAL(sv) && AvREAL(av))
2684 (void)sv_2mortal(sv);
2693 SV *sv = av_shift(av);
2697 if (!SvIMMORTAL(sv) && AvREAL(av))
2698 (void)sv_2mortal(sv);
2705 djSP; dMARK; dORIGMARK; dTARGET;
2706 register AV *ary = (AV*)*++MARK;
2711 if (SvRMAGICAL(ary) && (mg = mg_find((SV*)ary,'P'))) {
2712 *MARK-- = mg->mg_obj;
2715 perl_call_method("UNSHIFT",GIMME_V);
2720 av_unshift(ary, SP - MARK);
2723 sv_setsv(sv, *++MARK);
2724 (void)av_store(ary, i++, sv);
2727 PUSHi( AvFILL(ary) + 1 );
2737 if (GIMME == G_ARRAY) {
2748 register char *down;
2754 do_join(TARG, &sv_no, MARK, SP);
2756 sv_setsv(TARG, (SP > MARK) ? *SP : DEFSV);
2757 up = SvPV_force(TARG, len);
2759 down = SvPVX(TARG) + len - 1;
2765 (void)SvPOK_only(TARG);
2774 mul128(SV *sv, U8 m)
2777 char *s = SvPV(sv, len);
2781 if (!strnEQ(s, "0000", 4)) { /* need to grow sv */
2782 SV *New = newSVpv("0000000000", 10);
2785 SvREFCNT_dec(sv); /* free old sv */
2790 while (!*t) /* trailing '\0'? */
2793 i = ((*t - '0') << 7) + m;
2794 *(t--) = '0' + (i % 10);
2800 /* Explosives and implosives. */
2807 I32 gimme = GIMME_V;
2811 register char *pat = SvPV(left, llen);
2812 register char *s = SvPV(right, rlen);
2813 char *strend = s + rlen;
2815 register char *patend = pat + llen;
2820 /* These must not be in registers: */
2831 unsigned Quad_t auquad;
2837 register U32 culong;
2839 static char* bitcount = 0;
2842 if (gimme != G_ARRAY) { /* arrange to do first one only */
2844 for (patend = pat; !isALPHA(*patend) || *patend == 'x'; patend++) ;
2845 if (strchr("aAbBhHP", *patend) || *pat == '%') {
2847 while (isDIGIT(*patend) || *patend == '*')
2853 while (pat < patend) {
2855 datumtype = *pat++ & 0xFF;
2856 if (isSPACE(datumtype))
2860 else if (*pat == '*') {
2861 len = strend - strbeg; /* long enough */
2864 else if (isDIGIT(*pat)) {
2866 while (isDIGIT(*pat))
2867 len = (len * 10) + (*pat++ - '0');
2870 len = (datumtype != '@');
2873 croak("Invalid type in unpack: '%c'", (int)datumtype);
2874 case ',': /* grandfather in commas but with a warning */
2875 if (commas++ == 0 && dowarn)
2876 warn("Invalid type in unpack: '%c'", (int)datumtype);
2879 if (len == 1 && pat[-1] != '1')
2888 if (len > strend - strbeg)
2889 DIE("@ outside of string");
2893 if (len > s - strbeg)
2894 DIE("X outside of string");
2898 if (len > strend - s)
2899 DIE("x outside of string");
2904 if (len > strend - s)
2907 goto uchar_checksum;
2908 sv = NEWSV(35, len);
2909 sv_setpvn(sv, s, len);
2911 if (datumtype == 'A') {
2912 aptr = s; /* borrow register */
2913 s = SvPVX(sv) + len - 1;
2914 while (s >= SvPVX(sv) && (!*s || isSPACE(*s)))
2917 SvCUR_set(sv, s - SvPVX(sv));
2918 s = aptr; /* unborrow register */
2920 XPUSHs(sv_2mortal(sv));
2924 if (pat[-1] == '*' || len > (strend - s) * 8)
2925 len = (strend - s) * 8;
2928 Newz(601, bitcount, 256, char);
2929 for (bits = 1; bits < 256; bits++) {
2930 if (bits & 1) bitcount[bits]++;
2931 if (bits & 2) bitcount[bits]++;
2932 if (bits & 4) bitcount[bits]++;
2933 if (bits & 8) bitcount[bits]++;
2934 if (bits & 16) bitcount[bits]++;
2935 if (bits & 32) bitcount[bits]++;
2936 if (bits & 64) bitcount[bits]++;
2937 if (bits & 128) bitcount[bits]++;
2941 culong += bitcount[*(unsigned char*)s++];
2946 if (datumtype == 'b') {
2948 if (bits & 1) culong++;
2954 if (bits & 128) culong++;
2961 sv = NEWSV(35, len + 1);
2964 aptr = pat; /* borrow register */
2966 if (datumtype == 'b') {
2968 for (len = 0; len < aint; len++) {
2969 if (len & 7) /*SUPPRESS 595*/
2973 *pat++ = '0' + (bits & 1);
2978 for (len = 0; len < aint; len++) {
2983 *pat++ = '0' + ((bits & 128) != 0);
2987 pat = aptr; /* unborrow register */
2988 XPUSHs(sv_2mortal(sv));
2992 if (pat[-1] == '*' || len > (strend - s) * 2)
2993 len = (strend - s) * 2;
2994 sv = NEWSV(35, len + 1);
2997 aptr = pat; /* borrow register */
2999 if (datumtype == 'h') {
3001 for (len = 0; len < aint; len++) {
3006 *pat++ = hexdigit[bits & 15];
3011 for (len = 0; len < aint; len++) {
3016 *pat++ = hexdigit[(bits >> 4) & 15];
3020 pat = aptr; /* unborrow register */
3021 XPUSHs(sv_2mortal(sv));
3024 if (len > strend - s)
3029 if (aint >= 128) /* fake up signed chars */
3039 if (aint >= 128) /* fake up signed chars */
3042 sv_setiv(sv, (IV)aint);
3043 PUSHs(sv_2mortal(sv));
3048 if (len > strend - s)
3063 sv_setiv(sv, (IV)auint);
3064 PUSHs(sv_2mortal(sv));
3069 along = (strend - s) / SIZE16;
3086 sv_setiv(sv, (IV)ashort);
3087 PUSHs(sv_2mortal(sv));
3094 along = (strend - s) / SIZE16;
3099 COPY16(s, &aushort);
3102 if (datumtype == 'n')
3103 aushort = ntohs(aushort);
3106 if (datumtype == 'v')
3107 aushort = vtohs(aushort);
3116 COPY16(s, &aushort);
3120 if (datumtype == 'n')
3121 aushort = ntohs(aushort);
3124 if (datumtype == 'v')
3125 aushort = vtohs(aushort);
3127 sv_setiv(sv, (IV)aushort);
3128 PUSHs(sv_2mortal(sv));
3133 along = (strend - s) / sizeof(int);
3138 Copy(s, &aint, 1, int);
3141 cdouble += (double)aint;
3150 Copy(s, &aint, 1, int);
3153 sv_setiv(sv, (IV)aint);
3154 PUSHs(sv_2mortal(sv));
3159 along = (strend - s) / sizeof(unsigned int);
3164 Copy(s, &auint, 1, unsigned int);
3165 s += sizeof(unsigned int);
3167 cdouble += (double)auint;
3176 Copy(s, &auint, 1, unsigned int);
3177 s += sizeof(unsigned int);
3179 sv_setuv(sv, (UV)auint);
3180 PUSHs(sv_2mortal(sv));
3185 along = (strend - s) / SIZE32;
3193 cdouble += (double)along;
3205 sv_setiv(sv, (IV)along);
3206 PUSHs(sv_2mortal(sv));
3213 along = (strend - s) / SIZE32;
3221 if (datumtype == 'N')
3222 aulong = ntohl(aulong);
3225 if (datumtype == 'V')
3226 aulong = vtohl(aulong);
3229 cdouble += (double)aulong;
3241 if (datumtype == 'N')
3242 aulong = ntohl(aulong);
3245 if (datumtype == 'V')
3246 aulong = vtohl(aulong);
3249 sv_setuv(sv, (UV)aulong);
3250 PUSHs(sv_2mortal(sv));
3255 along = (strend - s) / sizeof(char*);
3261 if (sizeof(char*) > strend - s)
3264 Copy(s, &aptr, 1, char*);
3270 PUSHs(sv_2mortal(sv));
3280 while ((len > 0) && (s < strend)) {
3281 auv = (auv << 7) | (*s & 0x7f);
3282 if (!(*s++ & 0x80)) {
3286 PUSHs(sv_2mortal(sv));
3290 else if (++bytes >= sizeof(UV)) { /* promote to string */
3293 sv = newSVpvf("%.*Vu", (int)TYPE_DIGITS(UV), auv);
3294 while (s < strend) {
3295 sv = mul128(sv, *s & 0x7f);
3296 if (!(*s++ & 0x80)) {
3305 PUSHs(sv_2mortal(sv));
3310 if ((s >= strend) && bytes)
3311 croak("Unterminated compressed integer");
3316 if (sizeof(char*) > strend - s)
3319 Copy(s, &aptr, 1, char*);
3324 sv_setpvn(sv, aptr, len);
3325 PUSHs(sv_2mortal(sv));
3332 if (s + sizeof(Quad_t) > strend)
3335 Copy(s, &aquad, 1, Quad_t);
3336 s += sizeof(Quad_t);
3339 if (aquad >= IV_MIN && aquad <= IV_MAX)
3340 sv_setiv(sv, (IV)aquad);
3342 sv_setnv(sv, (double)aquad);
3343 PUSHs(sv_2mortal(sv));
3350 if (s + sizeof(unsigned Quad_t) > strend)
3353 Copy(s, &auquad, 1, unsigned Quad_t);
3354 s += sizeof(unsigned Quad_t);
3357 if (aquad <= UV_MAX)
3358 sv_setuv(sv, (UV)auquad);
3360 sv_setnv(sv, (double)auquad);
3361 PUSHs(sv_2mortal(sv));
3365 /* float and double added gnb@melba.bby.oz.au 22/11/89 */
3368 along = (strend - s) / sizeof(float);
3373 Copy(s, &afloat, 1, float);
3382 Copy(s, &afloat, 1, float);
3385 sv_setnv(sv, (double)afloat);
3386 PUSHs(sv_2mortal(sv));
3392 along = (strend - s) / sizeof(double);
3397 Copy(s, &adouble, 1, double);
3398 s += sizeof(double);
3406 Copy(s, &adouble, 1, double);
3407 s += sizeof(double);
3409 sv_setnv(sv, (double)adouble);
3410 PUSHs(sv_2mortal(sv));
3415 along = (strend - s) * 3 / 4;
3416 sv = NEWSV(42, along);
3419 while (s < strend && *s > ' ' && *s < 'a') {
3424 len = (*s++ - ' ') & 077;
3426 if (s < strend && *s >= ' ')
3427 a = (*s++ - ' ') & 077;
3430 if (s < strend && *s >= ' ')
3431 b = (*s++ - ' ') & 077;
3434 if (s < strend && *s >= ' ')
3435 c = (*s++ - ' ') & 077;
3438 if (s < strend && *s >= ' ')
3439 d = (*s++ - ' ') & 077;
3442 hunk[0] = (a << 2) | (b >> 4);
3443 hunk[1] = (b << 4) | (c >> 2);
3444 hunk[2] = (c << 6) | d;
3445 sv_catpvn(sv, hunk, (len > 3) ? 3 : len);
3450 else if (s[1] == '\n') /* possible checksum byte */
3453 XPUSHs(sv_2mortal(sv));
3458 if (strchr("fFdD", datumtype) ||
3459 (checksum > 32 && strchr("iIlLN", datumtype)) ) {
3463 while (checksum >= 16) {
3467 while (checksum >= 4) {
3473 along = (1 << checksum) - 1;
3474 while (cdouble < 0.0)
3476 cdouble = modf(cdouble / adouble, &trouble) * adouble;
3477 sv_setnv(sv, cdouble);
3480 if (checksum < 32) {
3481 aulong = (1 << checksum) - 1;
3484 sv_setuv(sv, (UV)culong);
3486 XPUSHs(sv_2mortal(sv));
3490 if (sp == oldsp && gimme == G_SCALAR)
3496 doencodes(register SV *sv, register char *s, register I32 len)
3501 sv_catpvn(sv, hunk, 1);
3504 hunk[0] = ' ' + (077 & (*s >> 2));
3505 hunk[1] = ' ' + (077 & (((*s << 4) & 060) | ((s[1] >> 4) & 017)));
3506 hunk[2] = ' ' + (077 & (((s[1] << 2) & 074) | ((s[2] >> 6) & 03)));
3507 hunk[3] = ' ' + (077 & (s[2] & 077));
3508 sv_catpvn(sv, hunk, 4);
3512 for (s = SvPVX(sv); *s; s++) {
3516 sv_catpvn(sv, "\n", 1);
3520 is_an_int(char *s, STRLEN l)
3522 SV *result = newSVpv("", l);
3523 char *result_c = SvPV(result, na); /* convenience */
3524 char *out = result_c;
3534 SvREFCNT_dec(result);
3557 SvREFCNT_dec(result);
3563 SvCUR_set(result, out - result_c);
3568 div128(SV *pnum, bool *done)
3569 /* must be '\0' terminated */
3573 char *s = SvPV(pnum, len);
3582 i = m * 10 + (*t - '0');
3584 r = (i >> 7); /* r < 10 */
3591 SvCUR_set(pnum, (STRLEN) (t - s));
3598 djSP; dMARK; dORIGMARK; dTARGET;
3599 register SV *cat = TARG;
3602 register char *pat = SvPVx(*++MARK, fromlen);
3603 register char *patend = pat + fromlen;
3608 static char null10[] = {0,0,0,0,0,0,0,0,0,0};
3609 static char *space10 = " ";
3611 /* These must not be in registers: */
3620 unsigned Quad_t auquad;
3629 sv_setpvn(cat, "", 0);
3630 while (pat < patend) {
3631 #define NEXTFROM (items-- > 0 ? *MARK++ : &sv_no)
3632 datumtype = *pat++ & 0xFF;
3633 if (isSPACE(datumtype))
3636 len = strchr("@Xxu", datumtype) ? 0 : items;
3639 else if (isDIGIT(*pat)) {
3641 while (isDIGIT(*pat))
3642 len = (len * 10) + (*pat++ - '0');
3648 croak("Invalid type in pack: '%c'", (int)datumtype);
3649 case ',': /* grandfather in commas but with a warning */
3650 if (commas++ == 0 && dowarn)
3651 warn("Invalid type in pack: '%c'", (int)datumtype);
3654 DIE("%% may only be used in unpack");
3665 if (SvCUR(cat) < len)
3666 DIE("X outside of string");
3673 sv_catpvn(cat, null10, 10);
3676 sv_catpvn(cat, null10, len);
3681 aptr = SvPV(fromstr, fromlen);
3685 sv_catpvn(cat, aptr, len);
3687 sv_catpvn(cat, aptr, fromlen);
3689 if (datumtype == 'A') {
3691 sv_catpvn(cat, space10, 10);
3694 sv_catpvn(cat, space10, len);
3698 sv_catpvn(cat, null10, 10);
3701 sv_catpvn(cat, null10, len);
3708 char *savepat = pat;
3713 aptr = SvPV(fromstr, fromlen);
3718 SvCUR(cat) += (len+7)/8;
3719 SvGROW(cat, SvCUR(cat) + 1);
3720 aptr = SvPVX(cat) + aint;
3725 if (datumtype == 'B') {
3726 for (len = 0; len++ < aint;) {
3727 items |= *pat++ & 1;
3731 *aptr++ = items & 0xff;
3737 for (len = 0; len++ < aint;) {
3743 *aptr++ = items & 0xff;
3749 if (datumtype == 'B')
3750 items <<= 7 - (aint & 7);
3752 items >>= 7 - (aint & 7);
3753 *aptr++ = items & 0xff;
3755 pat = SvPVX(cat) + SvCUR(cat);
3766 char *savepat = pat;
3771 aptr = SvPV(fromstr, fromlen);
3776 SvCUR(cat) += (len+1)/2;
3777 SvGROW(cat, SvCUR(cat) + 1);
3778 aptr = SvPVX(cat) + aint;
3783 if (datumtype == 'H') {
3784 for (len = 0; len++ < aint;) {
3786 items |= ((*pat++ & 15) + 9) & 15;
3788 items |= *pat++ & 15;
3792 *aptr++ = items & 0xff;
3798 for (len = 0; len++ < aint;) {
3800 items |= (((*pat++ & 15) + 9) & 15) << 4;
3802 items |= (*pat++ & 15) << 4;
3806 *aptr++ = items & 0xff;
3812 *aptr++ = items & 0xff;
3813 pat = SvPVX(cat) + SvCUR(cat);
3825 aint = SvIV(fromstr);
3827 sv_catpvn(cat, &achar, sizeof(char));
3830 /* Float and double added by gnb@melba.bby.oz.au 22/11/89 */
3835 afloat = (float)SvNV(fromstr);
3836 sv_catpvn(cat, (char *)&afloat, sizeof (float));
3843 adouble = (double)SvNV(fromstr);
3844 sv_catpvn(cat, (char *)&adouble, sizeof (double));
3850 ashort = (I16)SvIV(fromstr);
3852 ashort = htons(ashort);
3854 CAT16(cat, &ashort);
3860 ashort = (I16)SvIV(fromstr);
3862 ashort = htovs(ashort);
3864 CAT16(cat, &ashort);
3871 ashort = (I16)SvIV(fromstr);
3872 CAT16(cat, &ashort);
3878 auint = SvUV(fromstr);
3879 sv_catpvn(cat, (char*)&auint, sizeof(unsigned int));
3885 adouble = floor(SvNV(fromstr));
3888 croak("Cannot compress negative numbers");
3894 #ifdef CXUX_BROKEN_CONSTANT_CONVERT
3895 adouble <= UV_MAX_cxux
3902 char buf[1 + sizeof(UV)];
3903 char *in = buf + sizeof(buf);
3904 UV auv = U_V(adouble);;
3907 *--in = (auv & 0x7f) | 0x80;
3910 buf[sizeof(buf) - 1] &= 0x7f; /* clear continue bit */
3911 sv_catpvn(cat, in, (buf + sizeof(buf)) - in);
3913 else if (SvPOKp(fromstr)) { /* decimal string arithmetics */
3914 char *from, *result, *in;
3919 /* Copy string and check for compliance */
3920 from = SvPV(fromstr, len);
3921 if ((norm = is_an_int(from, len)) == NULL)
3922 croak("can compress only unsigned integer");
3924 New('w', result, len, char);
3928 *--in = div128(norm, &done) | 0x80;
3929 result[len - 1] &= 0x7F; /* clear continue bit */
3930 sv_catpvn(cat, in, (result + len) - in);
3932 SvREFCNT_dec(norm); /* free norm */
3934 else if (SvNOKp(fromstr)) {
3935 char buf[sizeof(double) * 2]; /* 8/7 <= 2 */
3936 char *in = buf + sizeof(buf);
3939 double next = floor(adouble / 128);
3940 *--in = (unsigned char)(adouble - (next * 128)) | 0x80;
3941 if (--in < buf) /* this cannot happen ;-) */
3942 croak ("Cannot compress integer");
3944 } while (adouble > 0);
3945 buf[sizeof(buf) - 1] &= 0x7f; /* clear continue bit */
3946 sv_catpvn(cat, in, (buf + sizeof(buf)) - in);
3949 croak("Cannot compress non integer");
3955 aint = SvIV(fromstr);
3956 sv_catpvn(cat, (char*)&aint, sizeof(int));
3962 aulong = SvUV(fromstr);
3964 aulong = htonl(aulong);
3966 CAT32(cat, &aulong);
3972 aulong = SvUV(fromstr);
3974 aulong = htovl(aulong);
3976 CAT32(cat, &aulong);
3982 aulong = SvUV(fromstr);
3983 CAT32(cat, &aulong);
3989 along = SvIV(fromstr);
3997 auquad = (unsigned Quad_t)SvIV(fromstr);
3998 sv_catpvn(cat, (char*)&auquad, sizeof(unsigned Quad_t));
4004 aquad = (Quad_t)SvIV(fromstr);
4005 sv_catpvn(cat, (char*)&aquad, sizeof(Quad_t));
4008 #endif /* HAS_QUAD */
4010 len = 1; /* assume SV is correct length */
4015 if (fromstr == &sv_undef)
4018 /* XXX better yet, could spirit away the string to
4019 * a safe spot and hang on to it until the result
4020 * of pack() (and all copies of the result) are
4023 if (dowarn && (SvTEMP(fromstr) || SvPADTMP(fromstr)))
4024 warn("Attempt to pack pointer to temporary value");
4025 if (SvPOK(fromstr) || SvNIOK(fromstr))
4026 aptr = SvPV(fromstr,na);
4028 aptr = SvPV_force(fromstr,na);
4030 sv_catpvn(cat, (char*)&aptr, sizeof(char*));
4035 aptr = SvPV(fromstr, fromlen);
4036 SvGROW(cat, fromlen * 4 / 3);
4041 while (fromlen > 0) {
4048 doencodes(cat, aptr, todo);
4066 register I32 limit = POPi; /* note, negative is forever */
4069 register char *s = SvPV(sv, len);
4070 char *strend = s + len;
4072 register REGEXP *rx;
4076 I32 maxiters = (strend - s) + 10;
4079 I32 origlimit = limit;
4082 AV *oldstack = curstack;
4083 I32 gimme = GIMME_V;
4084 I32 oldsave = savestack_ix;
4087 Copy(&LvTARGOFF(POPs), &pm, 1, PMOP*);
4092 DIE("panic: do_split");
4093 rx = pm->op_pmregexp;
4095 TAINT_IF((pm->op_pmflags & PMf_LOCALE) &&
4096 (pm->op_pmflags & (PMf_WHITE | PMf_SKIPWHITE)));
4098 if (pm->op_pmreplroot)
4099 ary = GvAVn((GV*)pm->op_pmreplroot);
4100 else if (gimme != G_ARRAY)
4102 ary = (AV*)curpad[0];
4105 #endif /* USE_THREADS */
4108 if (ary && (gimme != G_ARRAY || (pm->op_pmflags & PMf_ONCE))) {
4112 for (i = AvFILLp(ary); i >= 0; i--)
4113 AvARRAY(ary)[i] = &sv_undef; /* don't free mere refs */
4117 /* temporarily switch stacks */
4118 SWITCHSTACK(curstack, ary);
4120 base = SP - stack_base;
4122 if (pm->op_pmflags & PMf_SKIPWHITE) {
4123 if (pm->op_pmflags & PMf_LOCALE) {
4124 while (isSPACE_LC(*s))
4132 if (pm->op_pmflags & (PMf_MULTILINE|PMf_SINGLELINE)) {
4134 multiline = pm->op_pmflags & PMf_MULTILINE;
4138 limit = maxiters + 2;
4139 if (pm->op_pmflags & PMf_WHITE) {
4142 while (m < strend &&
4143 !((pm->op_pmflags & PMf_LOCALE)
4144 ? isSPACE_LC(*m) : isSPACE(*m)))
4149 dstr = NEWSV(30, m-s);
4150 sv_setpvn(dstr, s, m-s);
4156 while (s < strend &&
4157 ((pm->op_pmflags & PMf_LOCALE)
4158 ? isSPACE_LC(*s) : isSPACE(*s)))
4162 else if (strEQ("^", rx->precomp)) {
4165 for (m = s; m < strend && *m != '\n'; m++) ;
4169 dstr = NEWSV(30, m-s);
4170 sv_setpvn(dstr, s, m-s);
4177 else if (rx->check_substr && !rx->nparens
4178 && (rx->reganch & ROPT_CHECK_ALL)
4179 && !(rx->reganch & ROPT_ANCH)) {
4180 i = SvCUR(rx->check_substr);
4181 if (i == 1 && !SvTAIL(rx->check_substr)) {
4182 i = *SvPVX(rx->check_substr);
4185 for (m = s; m < strend && *m != i; m++) ;
4188 dstr = NEWSV(30, m-s);
4189 sv_setpvn(dstr, s, m-s);
4198 while (s < strend && --limit &&
4199 (m=fbm_instr((unsigned char*)s, (unsigned char*)strend,
4200 rx->check_substr)) )
4203 dstr = NEWSV(31, m-s);
4204 sv_setpvn(dstr, s, m-s);
4213 maxiters += (strend - s) * rx->nparens;
4214 while (s < strend && --limit &&
4215 regexec_flags(rx, s, strend, orig, 1, Nullsv, NULL, 0))
4217 TAINT_IF(RX_MATCH_TAINTED(rx));
4219 && rx->subbase != orig) {
4224 strend = s + (strend - m);
4227 dstr = NEWSV(32, m-s);
4228 sv_setpvn(dstr, s, m-s);
4233 for (i = 1; i <= rx->nparens; i++) {
4237 dstr = NEWSV(33, m-s);
4238 sv_setpvn(dstr, s, m-s);
4241 dstr = NEWSV(33, 0);
4250 LEAVE_SCOPE(oldsave);
4251 iters = (SP - stack_base) - base;
4252 if (iters > maxiters)
4255 /* keep field after final delim? */
4256 if (s < strend || (iters && origlimit)) {
4257 dstr = NEWSV(34, strend-s);
4258 sv_setpvn(dstr, s, strend-s);
4264 else if (!origlimit) {
4265 while (iters > 0 && (!TOPs || !SvANY(TOPs) || SvCUR(TOPs) == 0))
4269 SWITCHSTACK(ary, oldstack);
4270 if (SvSMAGICAL(ary)) {
4275 if (gimme == G_ARRAY) {
4277 Copy(AvARRAY(ary), SP + 1, iters, SV*);
4283 if (gimme == G_ARRAY)
4286 if (iters || !pm->op_pmreplroot) {
4296 unlock_condpair(void *svv)
4299 MAGIC *mg = mg_find((SV*)svv, 'm');
4302 croak("panic: unlock_condpair unlocking non-mutex");
4303 MUTEX_LOCK(MgMUTEXP(mg));
4304 if (MgOWNER(mg) != thr)
4305 croak("panic: unlock_condpair unlocking mutex that we don't own");
4307 COND_SIGNAL(MgOWNERCONDP(mg));
4308 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "0x%lx: unlock 0x%lx\n",
4309 (unsigned long)thr, (unsigned long)svv);)
4310 MUTEX_UNLOCK(MgMUTEXP(mg));
4312 #endif /* USE_THREADS */
4325 mg = condpair_magic(sv);
4326 MUTEX_LOCK(MgMUTEXP(mg));
4327 if (MgOWNER(mg) == thr)
4328 MUTEX_UNLOCK(MgMUTEXP(mg));
4331 COND_WAIT(MgOWNERCONDP(mg), MgMUTEXP(mg));
4333 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "0x%lx: pp_lock lock 0x%lx\n",
4334 (unsigned long)thr, (unsigned long)sv);)
4335 MUTEX_UNLOCK(MgMUTEXP(mg));
4336 SvREFCNT_inc(sv); /* keep alive until magic_mutexfree */
4337 save_destructor(unlock_condpair, sv);
4339 #endif /* USE_THREADS */
4340 if (SvTYPE(retsv) == SVt_PVAV || SvTYPE(retsv) == SVt_PVHV
4341 || SvTYPE(retsv) == SVt_PVCV) {
4342 retsv = refto(retsv);
4353 if (op->op_private & OPpLVAL_INTRO)
4354 PUSHs(*save_threadsv(op->op_targ));
4356 PUSHs(*av_fetch(thr->threadsv, op->op_targ, FALSE));
4359 DIE("tried to access per-thread data in non-threaded perl");
4360 #endif /* USE_THREADS */