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;
2465 perl_call_method("SPLICE",GIMME_V);
2474 offset = i = SvIVx(*MARK);
2476 offset += AvFILLp(ary) + 1;
2478 offset -= curcop->cop_arybase;
2482 length = SvIVx(*MARK++);
2487 length = AvMAX(ary) + 1; /* close enough to infinity */
2491 length = AvMAX(ary) + 1;
2493 if (offset > AvFILLp(ary) + 1)
2494 offset = AvFILLp(ary) + 1;
2495 after = AvFILLp(ary) + 1 - (offset + length);
2496 if (after < 0) { /* not that much array */
2497 length += after; /* offset+length now in array */
2503 /* At this point, MARK .. SP-1 is our new LIST */
2506 diff = newlen - length;
2507 if (newlen && !AvREAL(ary)) {
2511 assert(AvREAL(ary)); /* would leak, so croak */
2514 if (diff < 0) { /* shrinking the area */
2516 New(451, tmparyval, newlen, SV*); /* so remember insertion */
2517 Copy(MARK, tmparyval, newlen, SV*);
2520 MARK = ORIGMARK + 1;
2521 if (GIMME == G_ARRAY) { /* copy return vals to stack */
2522 MEXTEND(MARK, length);
2523 Copy(AvARRAY(ary)+offset, MARK, length, SV*);
2525 EXTEND_MORTAL(length);
2526 for (i = length, dst = MARK; i; i--) {
2527 if (!SvIMMORTAL(*dst))
2528 sv_2mortal(*dst); /* free them eventualy */
2535 *MARK = AvARRAY(ary)[offset+length-1];
2537 if (!SvIMMORTAL(*MARK))
2539 for (i = length - 1, dst = &AvARRAY(ary)[offset]; i > 0; i--)
2540 SvREFCNT_dec(*dst++); /* free them now */
2543 AvFILLp(ary) += diff;
2545 /* pull up or down? */
2547 if (offset < after) { /* easier to pull up */
2548 if (offset) { /* esp. if nothing to pull */
2549 src = &AvARRAY(ary)[offset-1];
2550 dst = src - diff; /* diff is negative */
2551 for (i = offset; i > 0; i--) /* can't trust Copy */
2555 SvPVX(ary) = (char*)(AvARRAY(ary) - diff); /* diff is negative */
2559 if (after) { /* anything to pull down? */
2560 src = AvARRAY(ary) + offset + length;
2561 dst = src + diff; /* diff is negative */
2562 Move(src, dst, after, SV*);
2564 dst = &AvARRAY(ary)[AvFILLp(ary)+1];
2565 /* avoid later double free */
2569 dst[--i] = &sv_undef;
2572 for (src = tmparyval, dst = AvARRAY(ary) + offset;
2574 *dst = NEWSV(46, 0);
2575 sv_setsv(*dst++, *src++);
2577 Safefree(tmparyval);
2580 else { /* no, expanding (or same) */
2582 New(452, tmparyval, length, SV*); /* so remember deletion */
2583 Copy(AvARRAY(ary)+offset, tmparyval, length, SV*);
2586 if (diff > 0) { /* expanding */
2588 /* push up or down? */
2590 if (offset < after && diff <= AvARRAY(ary) - AvALLOC(ary)) {
2594 Move(src, dst, offset, SV*);
2596 SvPVX(ary) = (char*)(AvARRAY(ary) - diff);/* diff is positive */
2598 AvFILLp(ary) += diff;
2601 if (AvFILLp(ary) + diff >= AvMAX(ary)) /* oh, well */
2602 av_extend(ary, AvFILLp(ary) + diff);
2603 AvFILLp(ary) += diff;
2606 dst = AvARRAY(ary) + AvFILLp(ary);
2608 for (i = after; i; i--) {
2615 for (src = MARK, dst = AvARRAY(ary) + offset; newlen; newlen--) {
2616 *dst = NEWSV(46, 0);
2617 sv_setsv(*dst++, *src++);
2619 MARK = ORIGMARK + 1;
2620 if (GIMME == G_ARRAY) { /* copy return vals to stack */
2622 Copy(tmparyval, MARK, length, SV*);
2624 EXTEND_MORTAL(length);
2625 for (i = length, dst = MARK; i; i--) {
2626 if (!SvIMMORTAL(*dst))
2627 sv_2mortal(*dst); /* free them eventualy */
2631 Safefree(tmparyval);
2635 else if (length--) {
2636 *MARK = tmparyval[length];
2638 if (!SvIMMORTAL(*MARK))
2640 while (length-- > 0)
2641 SvREFCNT_dec(tmparyval[length]);
2643 Safefree(tmparyval);
2654 djSP; dMARK; dORIGMARK; dTARGET;
2655 register AV *ary = (AV*)*++MARK;
2656 register SV *sv = &sv_undef;
2659 if (SvRMAGICAL(ary) && (mg = mg_find((SV*)ary,'P'))) {
2660 *MARK-- = mg->mg_obj;
2664 perl_call_method("PUSH",G_SCALAR|G_DISCARD);
2669 /* Why no pre-extend of ary here ? */
2670 for (++MARK; MARK <= SP; MARK++) {
2673 sv_setsv(sv, *MARK);
2678 PUSHi( AvFILL(ary) + 1 );
2686 SV *sv = av_pop(av);
2687 if (!SvIMMORTAL(sv) && AvREAL(av))
2688 (void)sv_2mortal(sv);
2697 SV *sv = av_shift(av);
2701 if (!SvIMMORTAL(sv) && AvREAL(av))
2702 (void)sv_2mortal(sv);
2709 djSP; dMARK; dORIGMARK; dTARGET;
2710 register AV *ary = (AV*)*++MARK;
2715 if (SvRMAGICAL(ary) && (mg = mg_find((SV*)ary,'P'))) {
2718 *MARK-- = mg->mg_obj;
2721 perl_call_method("UNSHIFT",G_SCALAR|G_DISCARD);
2726 av_unshift(ary, SP - MARK);
2729 sv_setsv(sv, *++MARK);
2730 (void)av_store(ary, i++, sv);
2734 PUSHi( AvFILL(ary) + 1 );
2744 if (GIMME == G_ARRAY) {
2755 register char *down;
2761 do_join(TARG, &sv_no, MARK, SP);
2763 sv_setsv(TARG, (SP > MARK) ? *SP : DEFSV);
2764 up = SvPV_force(TARG, len);
2766 down = SvPVX(TARG) + len - 1;
2772 (void)SvPOK_only(TARG);
2781 mul128(SV *sv, U8 m)
2784 char *s = SvPV(sv, len);
2788 if (!strnEQ(s, "0000", 4)) { /* need to grow sv */
2789 SV *New = newSVpv("0000000000", 10);
2792 SvREFCNT_dec(sv); /* free old sv */
2797 while (!*t) /* trailing '\0'? */
2800 i = ((*t - '0') << 7) + m;
2801 *(t--) = '0' + (i % 10);
2807 /* Explosives and implosives. */
2814 I32 gimme = GIMME_V;
2818 register char *pat = SvPV(left, llen);
2819 register char *s = SvPV(right, rlen);
2820 char *strend = s + rlen;
2822 register char *patend = pat + llen;
2827 /* These must not be in registers: */
2838 unsigned Quad_t auquad;
2844 register U32 culong;
2846 static char* bitcount = 0;
2849 if (gimme != G_ARRAY) { /* arrange to do first one only */
2851 for (patend = pat; !isALPHA(*patend) || *patend == 'x'; patend++) ;
2852 if (strchr("aAbBhHP", *patend) || *pat == '%') {
2854 while (isDIGIT(*patend) || *patend == '*')
2860 while (pat < patend) {
2862 datumtype = *pat++ & 0xFF;
2863 if (isSPACE(datumtype))
2867 else if (*pat == '*') {
2868 len = strend - strbeg; /* long enough */
2871 else if (isDIGIT(*pat)) {
2873 while (isDIGIT(*pat))
2874 len = (len * 10) + (*pat++ - '0');
2877 len = (datumtype != '@');
2880 croak("Invalid type in unpack: '%c'", (int)datumtype);
2881 case ',': /* grandfather in commas but with a warning */
2882 if (commas++ == 0 && dowarn)
2883 warn("Invalid type in unpack: '%c'", (int)datumtype);
2886 if (len == 1 && pat[-1] != '1')
2895 if (len > strend - strbeg)
2896 DIE("@ outside of string");
2900 if (len > s - strbeg)
2901 DIE("X outside of string");
2905 if (len > strend - s)
2906 DIE("x outside of string");
2911 if (len > strend - s)
2914 goto uchar_checksum;
2915 sv = NEWSV(35, len);
2916 sv_setpvn(sv, s, len);
2918 if (datumtype == 'A') {
2919 aptr = s; /* borrow register */
2920 s = SvPVX(sv) + len - 1;
2921 while (s >= SvPVX(sv) && (!*s || isSPACE(*s)))
2924 SvCUR_set(sv, s - SvPVX(sv));
2925 s = aptr; /* unborrow register */
2927 XPUSHs(sv_2mortal(sv));
2931 if (pat[-1] == '*' || len > (strend - s) * 8)
2932 len = (strend - s) * 8;
2935 Newz(601, bitcount, 256, char);
2936 for (bits = 1; bits < 256; bits++) {
2937 if (bits & 1) bitcount[bits]++;
2938 if (bits & 2) bitcount[bits]++;
2939 if (bits & 4) bitcount[bits]++;
2940 if (bits & 8) bitcount[bits]++;
2941 if (bits & 16) bitcount[bits]++;
2942 if (bits & 32) bitcount[bits]++;
2943 if (bits & 64) bitcount[bits]++;
2944 if (bits & 128) bitcount[bits]++;
2948 culong += bitcount[*(unsigned char*)s++];
2953 if (datumtype == 'b') {
2955 if (bits & 1) culong++;
2961 if (bits & 128) culong++;
2968 sv = NEWSV(35, len + 1);
2971 aptr = pat; /* borrow register */
2973 if (datumtype == 'b') {
2975 for (len = 0; len < aint; len++) {
2976 if (len & 7) /*SUPPRESS 595*/
2980 *pat++ = '0' + (bits & 1);
2985 for (len = 0; len < aint; len++) {
2990 *pat++ = '0' + ((bits & 128) != 0);
2994 pat = aptr; /* unborrow register */
2995 XPUSHs(sv_2mortal(sv));
2999 if (pat[-1] == '*' || len > (strend - s) * 2)
3000 len = (strend - s) * 2;
3001 sv = NEWSV(35, len + 1);
3004 aptr = pat; /* borrow register */
3006 if (datumtype == 'h') {
3008 for (len = 0; len < aint; len++) {
3013 *pat++ = hexdigit[bits & 15];
3018 for (len = 0; len < aint; len++) {
3023 *pat++ = hexdigit[(bits >> 4) & 15];
3027 pat = aptr; /* unborrow register */
3028 XPUSHs(sv_2mortal(sv));
3031 if (len > strend - s)
3036 if (aint >= 128) /* fake up signed chars */
3046 if (aint >= 128) /* fake up signed chars */
3049 sv_setiv(sv, (IV)aint);
3050 PUSHs(sv_2mortal(sv));
3055 if (len > strend - s)
3070 sv_setiv(sv, (IV)auint);
3071 PUSHs(sv_2mortal(sv));
3076 along = (strend - s) / SIZE16;
3093 sv_setiv(sv, (IV)ashort);
3094 PUSHs(sv_2mortal(sv));
3101 along = (strend - s) / SIZE16;
3106 COPY16(s, &aushort);
3109 if (datumtype == 'n')
3110 aushort = PerlSock_ntohs(aushort);
3113 if (datumtype == 'v')
3114 aushort = vtohs(aushort);
3123 COPY16(s, &aushort);
3127 if (datumtype == 'n')
3128 aushort = PerlSock_ntohs(aushort);
3131 if (datumtype == 'v')
3132 aushort = vtohs(aushort);
3134 sv_setiv(sv, (IV)aushort);
3135 PUSHs(sv_2mortal(sv));
3140 along = (strend - s) / sizeof(int);
3145 Copy(s, &aint, 1, int);
3148 cdouble += (double)aint;
3157 Copy(s, &aint, 1, int);
3160 sv_setiv(sv, (IV)aint);
3161 PUSHs(sv_2mortal(sv));
3166 along = (strend - s) / sizeof(unsigned int);
3171 Copy(s, &auint, 1, unsigned int);
3172 s += sizeof(unsigned int);
3174 cdouble += (double)auint;
3183 Copy(s, &auint, 1, unsigned int);
3184 s += sizeof(unsigned int);
3186 sv_setuv(sv, (UV)auint);
3187 PUSHs(sv_2mortal(sv));
3192 along = (strend - s) / SIZE32;
3200 cdouble += (double)along;
3212 sv_setiv(sv, (IV)along);
3213 PUSHs(sv_2mortal(sv));
3220 along = (strend - s) / SIZE32;
3228 if (datumtype == 'N')
3229 aulong = PerlSock_ntohl(aulong);
3232 if (datumtype == 'V')
3233 aulong = vtohl(aulong);
3236 cdouble += (double)aulong;
3248 if (datumtype == 'N')
3249 aulong = PerlSock_ntohl(aulong);
3252 if (datumtype == 'V')
3253 aulong = vtohl(aulong);
3256 sv_setuv(sv, (UV)aulong);
3257 PUSHs(sv_2mortal(sv));
3262 along = (strend - s) / sizeof(char*);
3268 if (sizeof(char*) > strend - s)
3271 Copy(s, &aptr, 1, char*);
3277 PUSHs(sv_2mortal(sv));
3287 while ((len > 0) && (s < strend)) {
3288 auv = (auv << 7) | (*s & 0x7f);
3289 if (!(*s++ & 0x80)) {
3293 PUSHs(sv_2mortal(sv));
3297 else if (++bytes >= sizeof(UV)) { /* promote to string */
3300 sv = newSVpvf("%.*Vu", (int)TYPE_DIGITS(UV), auv);
3301 while (s < strend) {
3302 sv = mul128(sv, *s & 0x7f);
3303 if (!(*s++ & 0x80)) {
3312 PUSHs(sv_2mortal(sv));
3317 if ((s >= strend) && bytes)
3318 croak("Unterminated compressed integer");
3323 if (sizeof(char*) > strend - s)
3326 Copy(s, &aptr, 1, char*);
3331 sv_setpvn(sv, aptr, len);
3332 PUSHs(sv_2mortal(sv));
3339 if (s + sizeof(Quad_t) > strend)
3342 Copy(s, &aquad, 1, Quad_t);
3343 s += sizeof(Quad_t);
3346 if (aquad >= IV_MIN && aquad <= IV_MAX)
3347 sv_setiv(sv, (IV)aquad);
3349 sv_setnv(sv, (double)aquad);
3350 PUSHs(sv_2mortal(sv));
3357 if (s + sizeof(unsigned Quad_t) > strend)
3360 Copy(s, &auquad, 1, unsigned Quad_t);
3361 s += sizeof(unsigned Quad_t);
3364 if (aquad <= UV_MAX)
3365 sv_setuv(sv, (UV)auquad);
3367 sv_setnv(sv, (double)auquad);
3368 PUSHs(sv_2mortal(sv));
3372 /* float and double added gnb@melba.bby.oz.au 22/11/89 */
3375 along = (strend - s) / sizeof(float);
3380 Copy(s, &afloat, 1, float);
3389 Copy(s, &afloat, 1, float);
3392 sv_setnv(sv, (double)afloat);
3393 PUSHs(sv_2mortal(sv));
3399 along = (strend - s) / sizeof(double);
3404 Copy(s, &adouble, 1, double);
3405 s += sizeof(double);
3413 Copy(s, &adouble, 1, double);
3414 s += sizeof(double);
3416 sv_setnv(sv, (double)adouble);
3417 PUSHs(sv_2mortal(sv));
3422 along = (strend - s) * 3 / 4;
3423 sv = NEWSV(42, along);
3426 while (s < strend && *s > ' ' && *s < 'a') {
3431 len = (*s++ - ' ') & 077;
3433 if (s < strend && *s >= ' ')
3434 a = (*s++ - ' ') & 077;
3437 if (s < strend && *s >= ' ')
3438 b = (*s++ - ' ') & 077;
3441 if (s < strend && *s >= ' ')
3442 c = (*s++ - ' ') & 077;
3445 if (s < strend && *s >= ' ')
3446 d = (*s++ - ' ') & 077;
3449 hunk[0] = (a << 2) | (b >> 4);
3450 hunk[1] = (b << 4) | (c >> 2);
3451 hunk[2] = (c << 6) | d;
3452 sv_catpvn(sv, hunk, (len > 3) ? 3 : len);
3457 else if (s[1] == '\n') /* possible checksum byte */
3460 XPUSHs(sv_2mortal(sv));
3465 if (strchr("fFdD", datumtype) ||
3466 (checksum > 32 && strchr("iIlLN", datumtype)) ) {
3470 while (checksum >= 16) {
3474 while (checksum >= 4) {
3480 along = (1 << checksum) - 1;
3481 while (cdouble < 0.0)
3483 cdouble = modf(cdouble / adouble, &trouble) * adouble;
3484 sv_setnv(sv, cdouble);
3487 if (checksum < 32) {
3488 aulong = (1 << checksum) - 1;
3491 sv_setuv(sv, (UV)culong);
3493 XPUSHs(sv_2mortal(sv));
3497 if (sp == oldsp && gimme == G_SCALAR)
3503 doencodes(register SV *sv, register char *s, register I32 len)
3508 sv_catpvn(sv, hunk, 1);
3511 hunk[0] = ' ' + (077 & (*s >> 2));
3512 hunk[1] = ' ' + (077 & (((*s << 4) & 060) | ((s[1] >> 4) & 017)));
3513 hunk[2] = ' ' + (077 & (((s[1] << 2) & 074) | ((s[2] >> 6) & 03)));
3514 hunk[3] = ' ' + (077 & (s[2] & 077));
3515 sv_catpvn(sv, hunk, 4);
3519 for (s = SvPVX(sv); *s; s++) {
3523 sv_catpvn(sv, "\n", 1);
3527 is_an_int(char *s, STRLEN l)
3529 SV *result = newSVpv("", l);
3530 char *result_c = SvPV(result, na); /* convenience */
3531 char *out = result_c;
3541 SvREFCNT_dec(result);
3564 SvREFCNT_dec(result);
3570 SvCUR_set(result, out - result_c);
3575 div128(SV *pnum, bool *done)
3576 /* must be '\0' terminated */
3580 char *s = SvPV(pnum, len);
3589 i = m * 10 + (*t - '0');
3591 r = (i >> 7); /* r < 10 */
3598 SvCUR_set(pnum, (STRLEN) (t - s));
3605 djSP; dMARK; dORIGMARK; dTARGET;
3606 register SV *cat = TARG;
3609 register char *pat = SvPVx(*++MARK, fromlen);
3610 register char *patend = pat + fromlen;
3615 static char null10[] = {0,0,0,0,0,0,0,0,0,0};
3616 static char *space10 = " ";
3618 /* These must not be in registers: */
3627 unsigned Quad_t auquad;
3636 sv_setpvn(cat, "", 0);
3637 while (pat < patend) {
3638 #define NEXTFROM (items-- > 0 ? *MARK++ : &sv_no)
3639 datumtype = *pat++ & 0xFF;
3640 if (isSPACE(datumtype))
3643 len = strchr("@Xxu", datumtype) ? 0 : items;
3646 else if (isDIGIT(*pat)) {
3648 while (isDIGIT(*pat))
3649 len = (len * 10) + (*pat++ - '0');
3655 croak("Invalid type in pack: '%c'", (int)datumtype);
3656 case ',': /* grandfather in commas but with a warning */
3657 if (commas++ == 0 && dowarn)
3658 warn("Invalid type in pack: '%c'", (int)datumtype);
3661 DIE("%% may only be used in unpack");
3672 if (SvCUR(cat) < len)
3673 DIE("X outside of string");
3680 sv_catpvn(cat, null10, 10);
3683 sv_catpvn(cat, null10, len);
3688 aptr = SvPV(fromstr, fromlen);
3692 sv_catpvn(cat, aptr, len);
3694 sv_catpvn(cat, aptr, fromlen);
3696 if (datumtype == 'A') {
3698 sv_catpvn(cat, space10, 10);
3701 sv_catpvn(cat, space10, len);
3705 sv_catpvn(cat, null10, 10);
3708 sv_catpvn(cat, null10, len);
3715 char *savepat = pat;
3720 aptr = SvPV(fromstr, fromlen);
3725 SvCUR(cat) += (len+7)/8;
3726 SvGROW(cat, SvCUR(cat) + 1);
3727 aptr = SvPVX(cat) + aint;
3732 if (datumtype == 'B') {
3733 for (len = 0; len++ < aint;) {
3734 items |= *pat++ & 1;
3738 *aptr++ = items & 0xff;
3744 for (len = 0; len++ < aint;) {
3750 *aptr++ = items & 0xff;
3756 if (datumtype == 'B')
3757 items <<= 7 - (aint & 7);
3759 items >>= 7 - (aint & 7);
3760 *aptr++ = items & 0xff;
3762 pat = SvPVX(cat) + SvCUR(cat);
3773 char *savepat = pat;
3778 aptr = SvPV(fromstr, fromlen);
3783 SvCUR(cat) += (len+1)/2;
3784 SvGROW(cat, SvCUR(cat) + 1);
3785 aptr = SvPVX(cat) + aint;
3790 if (datumtype == 'H') {
3791 for (len = 0; len++ < aint;) {
3793 items |= ((*pat++ & 15) + 9) & 15;
3795 items |= *pat++ & 15;
3799 *aptr++ = items & 0xff;
3805 for (len = 0; len++ < aint;) {
3807 items |= (((*pat++ & 15) + 9) & 15) << 4;
3809 items |= (*pat++ & 15) << 4;
3813 *aptr++ = items & 0xff;
3819 *aptr++ = items & 0xff;
3820 pat = SvPVX(cat) + SvCUR(cat);
3832 aint = SvIV(fromstr);
3834 sv_catpvn(cat, &achar, sizeof(char));
3837 /* Float and double added by gnb@melba.bby.oz.au 22/11/89 */
3842 afloat = (float)SvNV(fromstr);
3843 sv_catpvn(cat, (char *)&afloat, sizeof (float));
3850 adouble = (double)SvNV(fromstr);
3851 sv_catpvn(cat, (char *)&adouble, sizeof (double));
3857 ashort = (I16)SvIV(fromstr);
3859 ashort = PerlSock_htons(ashort);
3861 CAT16(cat, &ashort);
3867 ashort = (I16)SvIV(fromstr);
3869 ashort = htovs(ashort);
3871 CAT16(cat, &ashort);
3878 ashort = (I16)SvIV(fromstr);
3879 CAT16(cat, &ashort);
3885 auint = SvUV(fromstr);
3886 sv_catpvn(cat, (char*)&auint, sizeof(unsigned int));
3892 adouble = floor(SvNV(fromstr));
3895 croak("Cannot compress negative numbers");
3901 #ifdef CXUX_BROKEN_CONSTANT_CONVERT
3902 adouble <= UV_MAX_cxux
3909 char buf[1 + sizeof(UV)];
3910 char *in = buf + sizeof(buf);
3911 UV auv = U_V(adouble);;
3914 *--in = (auv & 0x7f) | 0x80;
3917 buf[sizeof(buf) - 1] &= 0x7f; /* clear continue bit */
3918 sv_catpvn(cat, in, (buf + sizeof(buf)) - in);
3920 else if (SvPOKp(fromstr)) { /* decimal string arithmetics */
3921 char *from, *result, *in;
3926 /* Copy string and check for compliance */
3927 from = SvPV(fromstr, len);
3928 if ((norm = is_an_int(from, len)) == NULL)
3929 croak("can compress only unsigned integer");
3931 New('w', result, len, char);
3935 *--in = div128(norm, &done) | 0x80;
3936 result[len - 1] &= 0x7F; /* clear continue bit */
3937 sv_catpvn(cat, in, (result + len) - in);
3939 SvREFCNT_dec(norm); /* free norm */
3941 else if (SvNOKp(fromstr)) {
3942 char buf[sizeof(double) * 2]; /* 8/7 <= 2 */
3943 char *in = buf + sizeof(buf);
3946 double next = floor(adouble / 128);
3947 *--in = (unsigned char)(adouble - (next * 128)) | 0x80;
3948 if (--in < buf) /* this cannot happen ;-) */
3949 croak ("Cannot compress integer");
3951 } while (adouble > 0);
3952 buf[sizeof(buf) - 1] &= 0x7f; /* clear continue bit */
3953 sv_catpvn(cat, in, (buf + sizeof(buf)) - in);
3956 croak("Cannot compress non integer");
3962 aint = SvIV(fromstr);
3963 sv_catpvn(cat, (char*)&aint, sizeof(int));
3969 aulong = SvUV(fromstr);
3971 aulong = PerlSock_htonl(aulong);
3973 CAT32(cat, &aulong);
3979 aulong = SvUV(fromstr);
3981 aulong = htovl(aulong);
3983 CAT32(cat, &aulong);
3989 aulong = SvUV(fromstr);
3990 CAT32(cat, &aulong);
3996 along = SvIV(fromstr);
4004 auquad = (unsigned Quad_t)SvIV(fromstr);
4005 sv_catpvn(cat, (char*)&auquad, sizeof(unsigned Quad_t));
4011 aquad = (Quad_t)SvIV(fromstr);
4012 sv_catpvn(cat, (char*)&aquad, sizeof(Quad_t));
4015 #endif /* HAS_QUAD */
4017 len = 1; /* assume SV is correct length */
4022 if (fromstr == &sv_undef)
4025 /* XXX better yet, could spirit away the string to
4026 * a safe spot and hang on to it until the result
4027 * of pack() (and all copies of the result) are
4030 if (dowarn && (SvTEMP(fromstr) || SvPADTMP(fromstr)))
4031 warn("Attempt to pack pointer to temporary value");
4032 if (SvPOK(fromstr) || SvNIOK(fromstr))
4033 aptr = SvPV(fromstr,na);
4035 aptr = SvPV_force(fromstr,na);
4037 sv_catpvn(cat, (char*)&aptr, sizeof(char*));
4042 aptr = SvPV(fromstr, fromlen);
4043 SvGROW(cat, fromlen * 4 / 3);
4048 while (fromlen > 0) {
4055 doencodes(cat, aptr, todo);
4074 register I32 limit = POPi; /* note, negative is forever */
4077 register char *s = SvPV(sv, len);
4078 char *strend = s + len;
4080 register REGEXP *rx;
4084 I32 maxiters = (strend - s) + 10;
4087 I32 origlimit = limit;
4090 AV *oldstack = curstack;
4091 I32 gimme = GIMME_V;
4092 I32 oldsave = savestack_ix;
4093 I32 make_mortal = 1;
4094 MAGIC *mg = (MAGIC *) NULL;
4097 Copy(&LvTARGOFF(POPs), &pm, 1, PMOP*);
4102 DIE("panic: do_split");
4103 rx = pm->op_pmregexp;
4105 TAINT_IF((pm->op_pmflags & PMf_LOCALE) &&
4106 (pm->op_pmflags & (PMf_WHITE | PMf_SKIPWHITE)));
4108 if (pm->op_pmreplroot)
4109 ary = GvAVn((GV*)pm->op_pmreplroot);
4110 else if (gimme != G_ARRAY)
4112 ary = (AV*)curpad[0];
4115 #endif /* USE_THREADS */
4118 if (ary && (gimme != G_ARRAY || (pm->op_pmflags & PMf_ONCE))) {
4124 if (SvRMAGICAL(ary) && (mg = mg_find((SV *) ary, 'P'))) {
4131 for (i = AvFILLp(ary); i >= 0; i--)
4132 AvARRAY(ary)[i] = &sv_undef; /* don't free mere refs */
4134 /* temporarily switch stacks */
4135 SWITCHSTACK(curstack, ary);
4139 base = SP - stack_base;
4141 if (pm->op_pmflags & PMf_SKIPWHITE) {
4142 if (pm->op_pmflags & PMf_LOCALE) {
4143 while (isSPACE_LC(*s))
4151 if (pm->op_pmflags & (PMf_MULTILINE|PMf_SINGLELINE)) {
4153 multiline = pm->op_pmflags & PMf_MULTILINE;
4157 limit = maxiters + 2;
4158 if (pm->op_pmflags & PMf_WHITE) {
4161 while (m < strend &&
4162 !((pm->op_pmflags & PMf_LOCALE)
4163 ? isSPACE_LC(*m) : isSPACE(*m)))
4168 dstr = NEWSV(30, m-s);
4169 sv_setpvn(dstr, s, m-s);
4175 while (s < strend &&
4176 ((pm->op_pmflags & PMf_LOCALE)
4177 ? isSPACE_LC(*s) : isSPACE(*s)))
4181 else if (strEQ("^", rx->precomp)) {
4184 for (m = s; m < strend && *m != '\n'; m++) ;
4188 dstr = NEWSV(30, m-s);
4189 sv_setpvn(dstr, s, m-s);
4196 else if (rx->check_substr && !rx->nparens
4197 && (rx->reganch & ROPT_CHECK_ALL)
4198 && !(rx->reganch & ROPT_ANCH)) {
4199 i = SvCUR(rx->check_substr);
4200 if (i == 1 && !SvTAIL(rx->check_substr)) {
4201 i = *SvPVX(rx->check_substr);
4204 for (m = s; m < strend && *m != i; m++) ;
4207 dstr = NEWSV(30, m-s);
4208 sv_setpvn(dstr, s, m-s);
4217 while (s < strend && --limit &&
4218 (m=fbm_instr((unsigned char*)s, (unsigned char*)strend,
4219 rx->check_substr)) )
4222 dstr = NEWSV(31, m-s);
4223 sv_setpvn(dstr, s, m-s);
4232 maxiters += (strend - s) * rx->nparens;
4233 while (s < strend && --limit &&
4234 regexec_flags(rx, s, strend, orig, 1, Nullsv, NULL, 0))
4236 TAINT_IF(RX_MATCH_TAINTED(rx));
4238 && rx->subbase != orig) {
4243 strend = s + (strend - m);
4246 dstr = NEWSV(32, m-s);
4247 sv_setpvn(dstr, s, m-s);
4252 for (i = 1; i <= rx->nparens; i++) {
4256 dstr = NEWSV(33, m-s);
4257 sv_setpvn(dstr, s, m-s);
4260 dstr = NEWSV(33, 0);
4270 LEAVE_SCOPE(oldsave);
4271 iters = (SP - stack_base) - base;
4272 if (iters > maxiters)
4275 /* keep field after final delim? */
4276 if (s < strend || (iters && origlimit)) {
4277 dstr = NEWSV(34, strend-s);
4278 sv_setpvn(dstr, s, strend-s);
4284 else if (!origlimit) {
4285 while (iters > 0 && (!TOPs || !SvANY(TOPs) || SvCUR(TOPs) == 0))
4291 SWITCHSTACK(ary, oldstack);
4292 if (SvSMAGICAL(ary)) {
4297 if (gimme == G_ARRAY) {
4299 Copy(AvARRAY(ary), SP + 1, iters, SV*);
4307 perl_call_method("PUSH",G_SCALAR|G_DISCARD);
4310 if (gimme == G_ARRAY) {
4311 /* EXTEND should not be needed - we just popped them */
4313 for (i=0; i < iters; i++) {
4314 SV **svp = av_fetch(ary, i, FALSE);
4315 PUSHs((svp) ? *svp : &sv_undef);
4322 if (gimme == G_ARRAY)
4325 if (iters || !pm->op_pmreplroot) {
4335 unlock_condpair(void *svv)
4338 MAGIC *mg = mg_find((SV*)svv, 'm');
4341 croak("panic: unlock_condpair unlocking non-mutex");
4342 MUTEX_LOCK(MgMUTEXP(mg));
4343 if (MgOWNER(mg) != thr)
4344 croak("panic: unlock_condpair unlocking mutex that we don't own");
4346 COND_SIGNAL(MgOWNERCONDP(mg));
4347 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "0x%lx: unlock 0x%lx\n",
4348 (unsigned long)thr, (unsigned long)svv);)
4349 MUTEX_UNLOCK(MgMUTEXP(mg));
4351 #endif /* USE_THREADS */
4364 mg = condpair_magic(sv);
4365 MUTEX_LOCK(MgMUTEXP(mg));
4366 if (MgOWNER(mg) == thr)
4367 MUTEX_UNLOCK(MgMUTEXP(mg));
4370 COND_WAIT(MgOWNERCONDP(mg), MgMUTEXP(mg));
4372 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "0x%lx: pp_lock lock 0x%lx\n",
4373 (unsigned long)thr, (unsigned long)sv);)
4374 MUTEX_UNLOCK(MgMUTEXP(mg));
4375 SvREFCNT_inc(sv); /* keep alive until magic_mutexfree */
4376 save_destructor(unlock_condpair, sv);
4378 #endif /* USE_THREADS */
4379 if (SvTYPE(retsv) == SVt_PVAV || SvTYPE(retsv) == SVt_PVHV
4380 || SvTYPE(retsv) == SVt_PVCV) {
4381 retsv = refto(retsv);
4392 if (op->op_private & OPpLVAL_INTRO)
4393 PUSHs(*save_threadsv(op->op_targ));
4395 PUSHs(*av_fetch(thr->threadsv, op->op_targ, FALSE));
4398 DIE("tried to access per-thread data in non-threaded perl");
4399 #endif /* USE_THREADS */