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)
101 static void doencodes _((SV* sv, char* s, I32 len));
102 static SV* refto _((SV* sv));
103 static U32 seed _((void));
106 static bool srand_called = FALSE;
108 /* variations on pp_null */
110 #ifdef DONT_DECLARE_STD
115 extern pid_t getpid (void);
121 if (GIMME_V == G_SCALAR)
136 if (op->op_private & OPpLVAL_INTRO)
137 SAVECLEARSV(curpad[op->op_targ]);
139 if (op->op_flags & OPf_REF) {
143 if (GIMME == G_ARRAY) {
144 I32 maxarg = AvFILL((AV*)TARG) + 1;
146 if (SvMAGICAL(TARG)) {
148 for (i=0; i < maxarg; i++) {
149 SV **svp = av_fetch((AV*)TARG, i, FALSE);
150 SP[i+1] = (svp) ? *svp : &sv_undef;
154 Copy(AvARRAY((AV*)TARG), SP+1, maxarg, SV*);
159 SV* sv = sv_newmortal();
160 I32 maxarg = AvFILL((AV*)TARG) + 1;
161 sv_setiv(sv, maxarg);
173 if (op->op_private & OPpLVAL_INTRO)
174 SAVECLEARSV(curpad[op->op_targ]);
175 if (op->op_flags & OPf_REF)
178 if (gimme == G_ARRAY) {
179 RETURNOP(do_kv(ARGS));
181 else if (gimme == G_SCALAR) {
182 SV* sv = sv_newmortal();
183 if (HvFILL((HV*)TARG))
184 sv_setpvf(sv, "%ld/%ld",
185 (long)HvFILL((HV*)TARG), (long)HvMAX((HV*)TARG) + 1);
195 DIE("NOT IMPL LINE %d",__LINE__);
207 if (SvTYPE(sv) == SVt_PVIO) {
208 GV *gv = (GV*) sv_newmortal();
209 gv_init(gv, 0, "", 0, 0);
210 GvIOp(gv) = (IO *)sv;
211 (void)SvREFCNT_inc(sv);
213 } else if (SvTYPE(sv) != SVt_PVGV)
214 DIE("Not a GLOB reference");
217 if (SvTYPE(sv) != SVt_PVGV) {
220 if (SvGMAGICAL(sv)) {
226 if (op->op_flags & OPf_REF ||
227 op->op_private & HINT_STRICT_REFS)
228 DIE(no_usym, "a symbol");
234 if (op->op_private & HINT_STRICT_REFS)
235 DIE(no_symref, sym, "a symbol");
236 sv = (SV*)gv_fetchpv(sym, TRUE, SVt_PVGV);
239 if (op->op_private & OPpLVAL_INTRO)
240 save_gp((GV*)sv, !(op->op_flags & OPf_SPECIAL));
252 switch (SvTYPE(sv)) {
256 DIE("Not a SCALAR reference");
263 if (SvTYPE(gv) != SVt_PVGV) {
264 if (SvGMAGICAL(sv)) {
270 if (op->op_flags & OPf_REF ||
271 op->op_private & HINT_STRICT_REFS)
272 DIE(no_usym, "a SCALAR");
278 if (op->op_private & HINT_STRICT_REFS)
279 DIE(no_symref, sym, "a SCALAR");
280 gv = (GV*)gv_fetchpv(sym, TRUE, SVt_PV);
284 if (op->op_flags & OPf_MOD) {
285 if (op->op_private & OPpLVAL_INTRO)
286 sv = save_scalar((GV*)TOPs);
287 else if (op->op_private & OPpDEREF)
288 vivify_ref(sv, op->op_private & OPpDEREF);
298 SV *sv = AvARYLEN(av);
300 AvARYLEN(av) = sv = NEWSV(0,0);
301 sv_upgrade(sv, SVt_IV);
302 sv_magic(sv, (SV*)av, '#', Nullch, 0);
310 djSP; dTARGET; dPOPss;
312 if (op->op_flags & OPf_MOD) {
313 if (SvTYPE(TARG) < SVt_PVLV) {
314 sv_upgrade(TARG, SVt_PVLV);
315 sv_magic(TARG, Nullsv, '.', Nullch, 0);
320 PUSHs(TARG); /* no SvSETMAGIC */
326 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
327 mg = mg_find(sv, 'g');
328 if (mg && mg->mg_len >= 0) {
329 PUSHi(mg->mg_len + curcop->cop_arybase);
343 /* We usually try to add a non-existent subroutine in case of AUTOLOAD. */
344 /* (But not in defined().) */
345 CV *cv = sv_2cv(TOPs, &stash, &gv, !(op->op_flags & OPf_SPECIAL));
348 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
365 cv = sv_2cv(TOPs, &stash, &gv, FALSE);
367 ret = sv_2mortal(newSVpv(SvPVX(cv), SvCUR(cv)));
375 CV* cv = (CV*)curpad[op->op_targ];
377 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
393 if (GIMME != G_ARRAY) {
397 EXTEND_MORTAL(SP - MARK);
399 *MARK = refto(*MARK);
408 if (SvTYPE(sv) == SVt_PVLV && LvTYPE(sv) == 'y') {
411 if (!(sv = LvTARG(sv)))
414 else if (SvPADTMP(sv))
418 (void)SvREFCNT_inc(sv);
421 sv_upgrade(rv, SVt_RV);
435 if (sv && SvGMAGICAL(sv))
438 if (!sv || !SvROK(sv))
442 pv = sv_reftype(sv,TRUE);
443 PUSHp(pv, strlen(pv));
453 stash = curcop->cop_stash;
455 stash = gv_stashsv(POPs, TRUE);
457 (void)sv_bless(TOPs, stash);
474 switch (elem ? *elem : '\0')
477 if (strEQ(elem, "ARRAY"))
478 tmpRef = (SV*)GvAV(gv);
481 if (strEQ(elem, "CODE"))
482 tmpRef = (SV*)GvCVu(gv);
485 if (strEQ(elem, "FILEHANDLE")) /* XXX deprecate in 5.005 */
486 tmpRef = (SV*)GvIOp(gv);
489 if (strEQ(elem, "GLOB"))
493 if (strEQ(elem, "HASH"))
494 tmpRef = (SV*)GvHV(gv);
497 if (strEQ(elem, "IO"))
498 tmpRef = (SV*)GvIOp(gv);
501 if (strEQ(elem, "NAME"))
502 sv = newSVpv(GvNAME(gv), GvNAMELEN(gv));
505 if (strEQ(elem, "PACKAGE"))
506 sv = newSVpv(HvNAME(GvSTASH(gv)), 0);
509 if (strEQ(elem, "SCALAR"))
523 /* Pattern matching */
528 register UNOP *unop = cUNOP;
529 register unsigned char *s;
532 register I32 *sfirst;
536 if(unop->op_first && unop->op_first->op_type == OP_PUSHRE) {
537 PMOP *pm = (PMOP *)unop->op_first;
538 SV *rv = sv_newmortal();
539 sv = newSVrv(rv, "Regexp");
540 sv_magic(sv,(SV*)ReREFCNT_inc(pm->op_pmregexp),'r',0,0);
544 if (sv == lastscream) {
550 SvSCREAM_off(lastscream);
551 SvREFCNT_dec(lastscream);
553 lastscream = SvREFCNT_inc(sv);
556 s = (unsigned char*)(SvPV(sv, len));
560 if (pos > maxscream) {
562 maxscream = pos + 80;
563 New(301, screamfirst, 256, I32);
564 New(302, screamnext, maxscream, I32);
567 maxscream = pos + pos / 4;
568 Renew(screamnext, maxscream, I32);
572 sfirst = screamfirst;
575 if (!sfirst || !snext)
576 DIE("do_study: out of memory");
578 for (ch = 256; ch; --ch)
585 snext[pos] = sfirst[ch] - pos;
592 sv_magic(sv, Nullsv, 'g', Nullch, 0); /* piggyback on m//g magic */
601 if (op->op_flags & OPf_STACKED)
607 TARG = sv_newmortal();
608 PUSHi(do_trans(sv, op));
612 /* Lvalue operators. */
624 djSP; dMARK; dTARGET;
634 SETi(do_chomp(TOPs));
640 djSP; dMARK; dTARGET;
641 register I32 count = 0;
644 count += do_chomp(POPs);
655 if (!sv || !SvANY(sv))
657 switch (SvTYPE(sv)) {
659 if (AvMAX(sv) >= 0 || SvGMAGICAL(sv))
663 if (HvARRAY(sv) || SvGMAGICAL(sv))
667 if (CvROOT(sv) || CvXSUB(sv))
684 if (!op->op_private) {
693 if (SvTHINKFIRST(sv)) {
700 switch (SvTYPE(sv)) {
710 if (cv_const_sv((CV*)sv))
711 warn("Constant subroutine %s undefined",
712 CvANON((CV*)sv) ? "(anonymous)" : GvENAME(CvGV((CV*)sv)));
715 { GV* gv = (GV*)SvREFCNT_inc(CvGV((CV*)sv));
717 CvGV((CV*)sv) = gv; } /* let user-undef'd sub keep its identity */
721 sv_setsv(sv, &sv_undef);
724 if (SvTYPE(sv) >= SVt_PV && SvPVX(sv) && SvLEN(sv)) {
727 SvPV_set(sv, Nullch);
740 if (SvREADONLY(TOPs) || SvTYPE(TOPs) > SVt_PVLV)
742 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) &&
743 SvIVX(TOPs) != IV_MIN)
746 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
757 if (SvREADONLY(TOPs) || SvTYPE(TOPs) > SVt_PVLV)
759 sv_setsv(TARG, TOPs);
760 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) &&
761 SvIVX(TOPs) != IV_MAX)
764 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
778 if(SvREADONLY(TOPs) || SvTYPE(TOPs) > SVt_PVLV)
780 sv_setsv(TARG, TOPs);
781 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) &&
782 SvIVX(TOPs) != IV_MIN)
785 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
794 /* Ordinary operators. */
798 djSP; dATARGET; tryAMAGICbin(pow,opASSIGN);
801 SETn( pow( left, right) );
808 djSP; dATARGET; tryAMAGICbin(mult,opASSIGN);
811 SETn( left * right );
818 djSP; dATARGET; tryAMAGICbin(div,opASSIGN);
823 DIE("Illegal division by zero");
825 /* insure that 20./5. == 4. */
828 if ((double)I_V(left) == left &&
829 (double)I_V(right) == right &&
830 (k = I_V(left)/I_V(right))*I_V(right) == I_V(left)) {
833 value = left / right;
837 value = left / right;
846 djSP; dATARGET; tryAMAGICbin(modulo,opASSIGN);
854 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)) {
856 right = (right_neg = (i < 0)) ? -i : i;
860 right = U_V((right_neg = (n < 0)) ? -n : n);
863 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)) {
865 left = (left_neg = (i < 0)) ? -i : i;
869 left = U_V((left_neg = (n < 0)) ? -n : n);
873 DIE("Illegal modulus zero");
876 if ((left_neg != right_neg) && ans)
879 /* XXX may warn: unary minus operator applied to unsigned type */
880 /* could change -foo to be (~foo)+1 instead */
881 if (ans <= ~((UV)IV_MAX)+1)
882 sv_setiv(TARG, ~ans+1);
884 sv_setnv(TARG, -(double)ans);
895 djSP; dATARGET; tryAMAGICbin(repeat,opASSIGN);
897 register I32 count = POPi;
898 if (GIMME == G_ARRAY && op->op_private & OPpREPEAT_DOLIST) {
900 I32 items = SP - MARK;
912 repeatcpy((char*)(MARK + items), (char*)MARK,
913 items * sizeof(SV*), count - 1);
919 else { /* Note: mark already snarfed by pp_list */
924 if (TARG == tmpstr && SvTHINKFIRST(tmpstr)) {
925 if (SvREADONLY(tmpstr) && curcop != &compiling)
926 DIE("Can't x= to readonly value");
930 SvSetSV(TARG, tmpstr);
931 SvPV_force(TARG, len);
936 SvGROW(TARG, (count * len) + 1);
937 repeatcpy(SvPVX(TARG) + len, SvPVX(TARG), len, count - 1);
938 SvCUR(TARG) *= count;
942 (void)SvPOK_only(TARG);
951 djSP; dATARGET; tryAMAGICbin(subtr,opASSIGN);
954 SETn( left - right );
961 djSP; dATARGET; tryAMAGICbin(lshift,opASSIGN);
964 if (op->op_private & HINT_INTEGER) {
980 djSP; dATARGET; tryAMAGICbin(rshift,opASSIGN);
983 if (op->op_private & HINT_INTEGER) {
999 djSP; tryAMAGICbinSET(lt,0);
1002 SETs(boolSV(TOPn < value));
1009 djSP; tryAMAGICbinSET(gt,0);
1012 SETs(boolSV(TOPn > value));
1019 djSP; tryAMAGICbinSET(le,0);
1022 SETs(boolSV(TOPn <= value));
1029 djSP; tryAMAGICbinSET(ge,0);
1032 SETs(boolSV(TOPn >= value));
1039 djSP; tryAMAGICbinSET(ne,0);
1042 SETs(boolSV(TOPn != value));
1049 djSP; dTARGET; tryAMAGICbin(ncmp,0);
1056 else if (left < right)
1058 else if (left > right)
1071 djSP; tryAMAGICbinSET(slt,0);
1074 int cmp = ((op->op_private & OPpLOCALE)
1075 ? sv_cmp_locale(left, right)
1076 : sv_cmp(left, right));
1077 SETs(boolSV(cmp < 0));
1084 djSP; tryAMAGICbinSET(sgt,0);
1087 int cmp = ((op->op_private & OPpLOCALE)
1088 ? sv_cmp_locale(left, right)
1089 : sv_cmp(left, right));
1090 SETs(boolSV(cmp > 0));
1097 djSP; tryAMAGICbinSET(sle,0);
1100 int cmp = ((op->op_private & OPpLOCALE)
1101 ? sv_cmp_locale(left, right)
1102 : sv_cmp(left, right));
1103 SETs(boolSV(cmp <= 0));
1110 djSP; tryAMAGICbinSET(sge,0);
1113 int cmp = ((op->op_private & OPpLOCALE)
1114 ? sv_cmp_locale(left, right)
1115 : sv_cmp(left, right));
1116 SETs(boolSV(cmp >= 0));
1123 djSP; tryAMAGICbinSET(seq,0);
1126 SETs(boolSV(sv_eq(left, right)));
1133 djSP; tryAMAGICbinSET(sne,0);
1136 SETs(boolSV(!sv_eq(left, right)));
1143 djSP; dTARGET; tryAMAGICbin(scmp,0);
1146 int cmp = ((op->op_private & OPpLOCALE)
1147 ? sv_cmp_locale(left, right)
1148 : sv_cmp(left, right));
1156 djSP; dATARGET; tryAMAGICbin(band,opASSIGN);
1159 if (SvNIOKp(left) || SvNIOKp(right)) {
1160 if (op->op_private & HINT_INTEGER) {
1161 IBW value = SvIV(left) & SvIV(right);
1165 UBW value = SvUV(left) & SvUV(right);
1170 do_vop(op->op_type, TARG, left, right);
1179 djSP; dATARGET; tryAMAGICbin(bxor,opASSIGN);
1182 if (SvNIOKp(left) || SvNIOKp(right)) {
1183 if (op->op_private & HINT_INTEGER) {
1184 IBW value = (USE_LEFT(left) ? SvIV(left) : 0) ^ SvIV(right);
1188 UBW value = (USE_LEFT(left) ? SvUV(left) : 0) ^ SvUV(right);
1193 do_vop(op->op_type, TARG, left, right);
1202 djSP; dATARGET; tryAMAGICbin(bor,opASSIGN);
1205 if (SvNIOKp(left) || SvNIOKp(right)) {
1206 if (op->op_private & HINT_INTEGER) {
1207 IBW value = (USE_LEFT(left) ? SvIV(left) : 0) | SvIV(right);
1211 UBW value = (USE_LEFT(left) ? SvUV(left) : 0) | SvUV(right);
1216 do_vop(op->op_type, TARG, left, right);
1225 djSP; dTARGET; tryAMAGICun(neg);
1230 if (SvIOKp(sv) && !SvNOKp(sv) && !SvPOKp(sv) && SvIVX(sv) != IV_MIN)
1232 else if (SvNIOKp(sv))
1234 else if (SvPOKp(sv)) {
1236 char *s = SvPV(sv, len);
1237 if (isIDFIRST(*s)) {
1238 sv_setpvn(TARG, "-", 1);
1241 else if (*s == '+' || *s == '-') {
1243 *SvPV_force(TARG, len) = *s == '-' ? '+' : '-';
1246 sv_setnv(TARG, -SvNV(sv));
1258 djSP; tryAMAGICunSET(not);
1259 #endif /* OVERLOAD */
1260 *stack_sp = boolSV(!SvTRUE(*stack_sp));
1266 djSP; dTARGET; tryAMAGICun(compl);
1270 if (op->op_private & HINT_INTEGER) {
1271 IBW value = ~SvIV(sv);
1275 UBW value = ~SvUV(sv);
1280 register char *tmps;
1281 register long *tmpl;
1286 tmps = SvPV_force(TARG, len);
1289 for ( ; anum && (unsigned long)tmps % sizeof(long); anum--, tmps++)
1292 for ( ; anum >= sizeof(long); anum -= sizeof(long), tmpl++)
1296 for ( ; anum > 0; anum--, tmps++)
1305 /* integer versions of some of the above */
1309 djSP; dATARGET; tryAMAGICbin(mult,opASSIGN);
1312 SETi( left * right );
1319 djSP; dATARGET; tryAMAGICbin(div,opASSIGN);
1323 DIE("Illegal division by zero");
1324 value = POPi / value;
1332 djSP; dATARGET; tryAMAGICbin(modulo,opASSIGN);
1336 DIE("Illegal modulus zero");
1337 SETi( left % right );
1344 djSP; dATARGET; tryAMAGICbin(add,opASSIGN);
1347 SETi( left + right );
1354 djSP; dATARGET; tryAMAGICbin(subtr,opASSIGN);
1357 SETi( left - right );
1364 djSP; tryAMAGICbinSET(lt,0);
1367 SETs(boolSV(left < right));
1374 djSP; tryAMAGICbinSET(gt,0);
1377 SETs(boolSV(left > right));
1384 djSP; tryAMAGICbinSET(le,0);
1387 SETs(boolSV(left <= right));
1394 djSP; tryAMAGICbinSET(ge,0);
1397 SETs(boolSV(left >= right));
1404 djSP; tryAMAGICbinSET(eq,0);
1407 SETs(boolSV(left == right));
1414 djSP; tryAMAGICbinSET(ne,0);
1417 SETs(boolSV(left != right));
1424 djSP; dTARGET; tryAMAGICbin(ncmp,0);
1431 else if (left < right)
1442 djSP; dTARGET; tryAMAGICun(neg);
1447 /* High falutin' math. */
1451 djSP; dTARGET; tryAMAGICbin(atan2,0);
1454 SETn(atan2(left, right));
1461 djSP; dTARGET; tryAMAGICun(sin);
1473 djSP; dTARGET; tryAMAGICun(cos);
1493 if (!srand_called) {
1494 (void)srand((unsigned)seed());
1495 srand_called = TRUE;
1498 value = rand() * value / 2147483648.0;
1501 value = rand() * value / 65536.0;
1504 value = rand() * value / 32768.0;
1506 value = rand() * value / (double)(((unsigned long)1) << RANDBITS);
1522 (void)srand((unsigned)anum);
1523 srand_called = TRUE;
1532 * This is really just a quick hack which grabs various garbage
1533 * values. It really should be a real hash algorithm which
1534 * spreads the effect of every input bit onto every output bit,
1535 * if someone who knows about such tings would bother to write it.
1536 * Might be a good idea to add that function to CORE as well.
1537 * No numbers below come from careful analysis or anyting here,
1538 * except they are primes and SEED_C1 > 1E6 to get a full-width
1539 * value from (tv_sec * SEED_C1 + tv_usec). The multipliers should
1540 * probably be bigger too.
1543 # define SEED_C1 1000003
1544 #define SEED_C4 73819
1546 # define SEED_C1 25747
1547 #define SEED_C4 20639
1551 #define SEED_C5 26107
1556 # include <starlet.h>
1557 /* when[] = (low 32 bits, high 32 bits) of time since epoch
1558 * in 100-ns units, typically incremented ever 10 ms. */
1559 unsigned int when[2];
1560 _ckvmssts(sys$gettim(when));
1561 u = (U32)SEED_C1 * when[0] + (U32)SEED_C2 * when[1];
1563 # ifdef HAS_GETTIMEOFDAY
1564 struct timeval when;
1565 gettimeofday(&when,(struct timezone *) 0);
1566 u = (U32)SEED_C1 * when.tv_sec + (U32)SEED_C2 * when.tv_usec;
1570 u = (U32)SEED_C1 * when;
1573 u += SEED_C3 * (U32)getpid();
1574 u += SEED_C4 * (U32)(UV)stack_sp;
1575 #ifndef PLAN9 /* XXX Plan9 assembler chokes on this; fix needed */
1576 u += SEED_C5 * (U32)(UV)&when;
1583 djSP; dTARGET; tryAMAGICun(exp);
1595 djSP; dTARGET; tryAMAGICun(log);
1600 SET_NUMERIC_STANDARD();
1601 DIE("Can't take log of %g", value);
1611 djSP; dTARGET; tryAMAGICun(sqrt);
1616 SET_NUMERIC_STANDARD();
1617 DIE("Can't take sqrt of %g", value);
1619 value = sqrt(value);
1629 double value = TOPn;
1632 if (SvIOKp(TOPs) && !SvNOKp(TOPs) && !SvPOKp(TOPs)) {
1638 (void)modf(value, &value);
1640 (void)modf(-value, &value);
1655 djSP; dTARGET; tryAMAGICun(abs);
1657 double value = TOPn;
1660 if (SvIOKp(TOPs) && !SvNOKp(TOPs) && !SvPOKp(TOPs) &&
1661 (iv = SvIVX(TOPs)) != IV_MIN) {
1682 XPUSHu(scan_hex(tmps, 99, &argtype));
1694 while (*tmps && isSPACE(*tmps))
1699 value = scan_hex(++tmps, 99, &argtype);
1701 value = scan_oct(tmps, 99, &argtype);
1711 SETi( sv_len(TOPs) );
1724 I32 lvalue = op->op_flags & OPf_MOD;
1726 I32 arybase = curcop->cop_arybase;
1732 tmps = SvPV(sv, curlen);
1733 if (pos >= arybase) {
1751 else if (len >= 0) {
1753 if (rem > (I32)curlen)
1767 if (dowarn || lvalue)
1768 warn("substr outside of string");
1773 sv_setpvn(TARG, tmps, rem);
1774 if (lvalue) { /* it's an lvalue! */
1775 if (!SvGMAGICAL(sv)) {
1779 warn("Attempt to use reference as lvalue in substr");
1781 if (SvOK(sv)) /* is it defined ? */
1782 (void)SvPOK_only(sv);
1784 sv_setpvn(sv,"",0); /* avoid lexical reincarnation */
1787 if (SvTYPE(TARG) < SVt_PVLV) {
1788 sv_upgrade(TARG, SVt_PVLV);
1789 sv_magic(TARG, Nullsv, 'x', Nullch, 0);
1794 LvTARGOFF(TARG) = pos;
1795 LvTARGLEN(TARG) = rem;
1798 PUSHs(TARG); /* avoid SvSETMAGIC here */
1805 register I32 size = POPi;
1806 register I32 offset = POPi;
1807 register SV *src = POPs;
1808 I32 lvalue = op->op_flags & OPf_MOD;
1810 unsigned char *s = (unsigned char*)SvPV(src, srclen);
1811 unsigned long retnum;
1814 offset *= size; /* turn into bit offset */
1815 len = (offset + size + 7) / 8;
1816 if (offset < 0 || size < 1)
1819 if (lvalue) { /* it's an lvalue! */
1820 if (SvTYPE(TARG) < SVt_PVLV) {
1821 sv_upgrade(TARG, SVt_PVLV);
1822 sv_magic(TARG, Nullsv, 'v', Nullch, 0);
1827 LvTARGOFF(TARG) = offset;
1828 LvTARGLEN(TARG) = size;
1836 if (offset >= srclen)
1839 retnum = (unsigned long) s[offset] << 8;
1841 else if (size == 32) {
1842 if (offset >= srclen)
1844 else if (offset + 1 >= srclen)
1845 retnum = (unsigned long) s[offset] << 24;
1846 else if (offset + 2 >= srclen)
1847 retnum = ((unsigned long) s[offset] << 24) +
1848 ((unsigned long) s[offset + 1] << 16);
1850 retnum = ((unsigned long) s[offset] << 24) +
1851 ((unsigned long) s[offset + 1] << 16) +
1852 (s[offset + 2] << 8);
1857 retnum = (s[offset >> 3] >> (offset & 7)) & ((1 << size) - 1);
1862 else if (size == 16)
1863 retnum = ((unsigned long) s[offset] << 8) + s[offset+1];
1864 else if (size == 32)
1865 retnum = ((unsigned long) s[offset] << 24) +
1866 ((unsigned long) s[offset + 1] << 16) +
1867 (s[offset + 2] << 8) + s[offset+3];
1871 sv_setiv(TARG, (IV)retnum);
1886 I32 arybase = curcop->cop_arybase;
1891 offset = POPi - arybase;
1894 tmps = SvPV(big, biglen);
1897 else if (offset > biglen)
1899 if (!(tmps2 = fbm_instr((unsigned char*)tmps + offset,
1900 (unsigned char*)tmps + biglen, little)))
1901 retval = -1 + arybase;
1903 retval = tmps2 - tmps + arybase;
1920 I32 arybase = curcop->cop_arybase;
1926 tmps2 = SvPV(little, llen);
1927 tmps = SvPV(big, blen);
1931 offset = SvIV(offstr) - arybase + llen;
1934 else if (offset > blen)
1936 if (!(tmps2 = rninstr(tmps, tmps + offset,
1937 tmps2, tmps2 + llen)))
1938 retval = -1 + arybase;
1940 retval = tmps2 - tmps + arybase;
1947 djSP; dMARK; dORIGMARK; dTARGET;
1948 #ifdef USE_LOCALE_NUMERIC
1949 if (op->op_private & OPpLOCALE)
1950 SET_NUMERIC_LOCAL();
1952 SET_NUMERIC_STANDARD();
1954 do_sprintf(TARG, SP-MARK, MARK+1);
1955 TAINT_IF(SvTAINTED(TARG));
1969 value = (I32) (*tmps & 255);
1974 value = (I32) (anum & 255);
1985 (void)SvUPGRADE(TARG,SVt_PV);
1991 (void)SvPOK_only(TARG);
1998 djSP; dTARGET; dPOPTOPssrl;
2000 char *tmps = SvPV(left, na);
2002 sv_setpv(TARG, fcrypt(tmps, SvPV(right, na)));
2004 sv_setpv(TARG, crypt(tmps, SvPV(right, na)));
2008 "The crypt() function is unimplemented due to excessive paranoia.");
2020 if (!SvPADTMP(sv)) {
2026 s = SvPV_force(sv, na);
2028 if (op->op_private & OPpLOCALE) {
2031 *s = toUPPER_LC(*s);
2046 if (!SvPADTMP(sv)) {
2052 s = SvPV_force(sv, na);
2054 if (op->op_private & OPpLOCALE) {
2057 *s = toLOWER_LC(*s);
2074 if (!SvPADTMP(sv)) {
2081 s = SvPV_force(sv, len);
2083 register char *send = s + len;
2085 if (op->op_private & OPpLOCALE) {
2088 for (; s < send; s++)
2089 *s = toUPPER_LC(*s);
2092 for (; s < send; s++)
2106 if (!SvPADTMP(sv)) {
2113 s = SvPV_force(sv, len);
2115 register char *send = s + len;
2117 if (op->op_private & OPpLOCALE) {
2120 for (; s < send; s++)
2121 *s = toLOWER_LC(*s);
2124 for (; s < send; s++)
2136 register char *s = SvPV(sv,len);
2140 (void)SvUPGRADE(TARG, SVt_PV);
2141 SvGROW(TARG, (len * 2) + 1);
2149 SvCUR_set(TARG, d - SvPVX(TARG));
2150 (void)SvPOK_only(TARG);
2153 sv_setpvn(TARG, s, len);
2162 djSP; dMARK; dORIGMARK;
2164 register AV* av = (AV*)POPs;
2165 register I32 lval = op->op_flags & OPf_MOD;
2166 I32 arybase = curcop->cop_arybase;
2169 if (SvTYPE(av) == SVt_PVAV) {
2170 if (lval && op->op_private & OPpLVAL_INTRO) {
2172 for (svp = mark + 1; svp <= sp; svp++) {
2177 if (max > AvMAX(av))
2180 while (++MARK <= SP) {
2181 elem = SvIVx(*MARK);
2185 svp = av_fetch(av, elem, lval);
2187 if (!svp || *svp == &sv_undef)
2188 DIE(no_aelem, elem);
2189 if (op->op_private & OPpLVAL_INTRO)
2192 *MARK = svp ? *svp : &sv_undef;
2195 if (GIMME != G_ARRAY) {
2203 /* Associative arrays. */
2208 HV *hash = (HV*)POPs;
2210 I32 gimme = GIMME_V;
2211 I32 realhv = (SvTYPE(hash) == SVt_PVHV);
2214 /* might clobber stack_sp */
2215 entry = realhv ? hv_iternext(hash) : avhv_iternext((AV*)hash);
2220 PUSHs(hv_iterkeysv(entry)); /* won't clobber stack_sp */
2221 if (gimme == G_ARRAY) {
2223 /* might clobber stack_sp */
2224 sv_setsv(TARG, realhv ?
2225 hv_iterval(hash, entry) : avhv_iterval((AV*)hash, entry));
2230 else if (gimme == G_SCALAR)
2249 I32 gimme = GIMME_V;
2250 I32 discard = (gimme == G_VOID) ? G_DISCARD : 0;
2254 if (op->op_private & OPpSLICE) {
2258 hvtype = SvTYPE(hv);
2259 while (++MARK <= SP) {
2260 if (hvtype == SVt_PVHV)
2261 sv = hv_delete_ent(hv, *MARK, discard, 0);
2262 else if (hvtype == SVt_PVAV)
2263 sv = avhv_delete_ent((AV*)hv, *MARK, discard, 0);
2265 DIE("Not a HASH reference");
2266 *MARK = sv ? sv : &sv_undef;
2270 else if (gimme == G_SCALAR) {
2279 if (SvTYPE(hv) == SVt_PVHV)
2280 sv = hv_delete_ent(hv, keysv, discard, 0);
2281 else if (SvTYPE(hv) == SVt_PVAV)
2282 sv = avhv_delete_ent((AV*)hv, keysv, discard, 0);
2284 DIE("Not a HASH reference");
2298 if (SvTYPE(hv) == SVt_PVHV) {
2299 if (hv_exists_ent(hv, tmpsv, 0))
2301 } else if (SvTYPE(hv) == SVt_PVAV) {
2302 if (avhv_exists_ent((AV*)hv, tmpsv, 0))
2305 DIE("Not a HASH reference");
2312 djSP; dMARK; dORIGMARK;
2314 register HV *hv = (HV*)POPs;
2315 register I32 lval = op->op_flags & OPf_MOD;
2316 I32 realhv = (SvTYPE(hv) == SVt_PVHV);
2318 if (realhv || SvTYPE(hv) == SVt_PVAV) {
2319 while (++MARK <= SP) {
2323 he = hv_fetch_ent(hv, keysv, lval, 0);
2324 svp = he ? &HeVAL(he) : 0;
2326 svp = avhv_fetch_ent((AV*)hv, keysv, lval, 0);
2329 if (!he || HeVAL(he) == &sv_undef)
2330 DIE(no_helem, SvPV(keysv, na));
2331 if (op->op_private & OPpLVAL_INTRO)
2332 save_svref(&HeVAL(he));
2334 *MARK = he ? HeVAL(he) : &sv_undef;
2337 if (GIMME != G_ARRAY) {
2345 /* List operators. */
2350 if (GIMME != G_ARRAY) {
2352 *MARK = *SP; /* unwanted list, return last item */
2363 SV **lastrelem = stack_sp;
2364 SV **lastlelem = stack_base + POPMARK;
2365 SV **firstlelem = stack_base + POPMARK + 1;
2366 register SV **firstrelem = lastlelem + 1;
2367 I32 arybase = curcop->cop_arybase;
2368 I32 lval = op->op_flags & OPf_MOD;
2369 I32 is_something_there = lval;
2371 register I32 max = lastrelem - lastlelem;
2372 register SV **lelem;
2375 if (GIMME != G_ARRAY) {
2376 ix = SvIVx(*lastlelem);
2381 if (ix < 0 || ix >= max)
2382 *firstlelem = &sv_undef;
2384 *firstlelem = firstrelem[ix];
2390 SP = firstlelem - 1;
2394 for (lelem = firstlelem; lelem <= lastlelem; lelem++) {
2400 else if (!(*lelem = firstrelem[ix]))
2405 if (ix >= max || !(*lelem = firstrelem[ix]))
2408 if (!is_something_there && (SvOK(*lelem) || SvGMAGICAL(*lelem)))
2409 is_something_there = TRUE;
2411 if (is_something_there)
2414 SP = firstlelem - 1;
2420 djSP; dMARK; dORIGMARK;
2421 I32 items = SP - MARK;
2422 SV *av = sv_2mortal((SV*)av_make(items, MARK+1));
2423 SP = ORIGMARK; /* av_make() might realloc stack_sp */
2430 djSP; dMARK; dORIGMARK;
2431 HV* hv = (HV*)sv_2mortal((SV*)newHV());
2435 SV *val = NEWSV(46, 0);
2437 sv_setsv(val, *++MARK);
2439 warn("Odd number of elements in hash list");
2440 (void)hv_store_ent(hv,key,val,0);
2449 djSP; dMARK; dORIGMARK;
2450 register AV *ary = (AV*)*++MARK;
2454 register I32 offset;
2455 register I32 length;
2462 if (SvRMAGICAL(ary) && (mg = mg_find((SV*)ary,'P'))) {
2463 *MARK-- = mg->mg_obj;
2467 perl_call_method("SPLICE",GIMME_V);
2476 offset = i = SvIVx(*MARK);
2478 offset += AvFILLp(ary) + 1;
2480 offset -= curcop->cop_arybase;
2484 length = SvIVx(*MARK++);
2489 length = AvMAX(ary) + 1; /* close enough to infinity */
2493 length = AvMAX(ary) + 1;
2495 if (offset > AvFILLp(ary) + 1)
2496 offset = AvFILLp(ary) + 1;
2497 after = AvFILLp(ary) + 1 - (offset + length);
2498 if (after < 0) { /* not that much array */
2499 length += after; /* offset+length now in array */
2505 /* At this point, MARK .. SP-1 is our new LIST */
2508 diff = newlen - length;
2509 if (newlen && !AvREAL(ary)) {
2513 assert(AvREAL(ary)); /* would leak, so croak */
2516 if (diff < 0) { /* shrinking the area */
2518 New(451, tmparyval, newlen, SV*); /* so remember insertion */
2519 Copy(MARK, tmparyval, newlen, SV*);
2522 MARK = ORIGMARK + 1;
2523 if (GIMME == G_ARRAY) { /* copy return vals to stack */
2524 MEXTEND(MARK, length);
2525 Copy(AvARRAY(ary)+offset, MARK, length, SV*);
2527 EXTEND_MORTAL(length);
2528 for (i = length, dst = MARK; i; i--) {
2529 if (!SvIMMORTAL(*dst))
2530 sv_2mortal(*dst); /* free them eventualy */
2537 *MARK = AvARRAY(ary)[offset+length-1];
2539 if (!SvIMMORTAL(*MARK))
2541 for (i = length - 1, dst = &AvARRAY(ary)[offset]; i > 0; i--)
2542 SvREFCNT_dec(*dst++); /* free them now */
2545 AvFILLp(ary) += diff;
2547 /* pull up or down? */
2549 if (offset < after) { /* easier to pull up */
2550 if (offset) { /* esp. if nothing to pull */
2551 src = &AvARRAY(ary)[offset-1];
2552 dst = src - diff; /* diff is negative */
2553 for (i = offset; i > 0; i--) /* can't trust Copy */
2557 SvPVX(ary) = (char*)(AvARRAY(ary) - diff); /* diff is negative */
2561 if (after) { /* anything to pull down? */
2562 src = AvARRAY(ary) + offset + length;
2563 dst = src + diff; /* diff is negative */
2564 Move(src, dst, after, SV*);
2566 dst = &AvARRAY(ary)[AvFILLp(ary)+1];
2567 /* avoid later double free */
2571 dst[--i] = &sv_undef;
2574 for (src = tmparyval, dst = AvARRAY(ary) + offset;
2576 *dst = NEWSV(46, 0);
2577 sv_setsv(*dst++, *src++);
2579 Safefree(tmparyval);
2582 else { /* no, expanding (or same) */
2584 New(452, tmparyval, length, SV*); /* so remember deletion */
2585 Copy(AvARRAY(ary)+offset, tmparyval, length, SV*);
2588 if (diff > 0) { /* expanding */
2590 /* push up or down? */
2592 if (offset < after && diff <= AvARRAY(ary) - AvALLOC(ary)) {
2596 Move(src, dst, offset, SV*);
2598 SvPVX(ary) = (char*)(AvARRAY(ary) - diff);/* diff is positive */
2600 AvFILLp(ary) += diff;
2603 if (AvFILLp(ary) + diff >= AvMAX(ary)) /* oh, well */
2604 av_extend(ary, AvFILLp(ary) + diff);
2605 AvFILLp(ary) += diff;
2608 dst = AvARRAY(ary) + AvFILLp(ary);
2610 for (i = after; i; i--) {
2617 for (src = MARK, dst = AvARRAY(ary) + offset; newlen; newlen--) {
2618 *dst = NEWSV(46, 0);
2619 sv_setsv(*dst++, *src++);
2621 MARK = ORIGMARK + 1;
2622 if (GIMME == G_ARRAY) { /* copy return vals to stack */
2624 Copy(tmparyval, MARK, length, SV*);
2626 EXTEND_MORTAL(length);
2627 for (i = length, dst = MARK; i; i--) {
2628 if (!SvIMMORTAL(*dst))
2629 sv_2mortal(*dst); /* free them eventualy */
2633 Safefree(tmparyval);
2637 else if (length--) {
2638 *MARK = tmparyval[length];
2640 if (!SvIMMORTAL(*MARK))
2642 while (length-- > 0)
2643 SvREFCNT_dec(tmparyval[length]);
2645 Safefree(tmparyval);
2656 djSP; dMARK; dORIGMARK; dTARGET;
2657 register AV *ary = (AV*)*++MARK;
2658 register SV *sv = &sv_undef;
2661 if (SvRMAGICAL(ary) && (mg = mg_find((SV*)ary,'P'))) {
2662 *MARK-- = mg->mg_obj;
2666 perl_call_method("PUSH",G_SCALAR|G_DISCARD);
2671 /* Why no pre-extend of ary here ? */
2672 for (++MARK; MARK <= SP; MARK++) {
2675 sv_setsv(sv, *MARK);
2680 PUSHi( AvFILL(ary) + 1 );
2688 SV *sv = av_pop(av);
2689 if (!SvIMMORTAL(sv) && AvREAL(av))
2690 (void)sv_2mortal(sv);
2699 SV *sv = av_shift(av);
2703 if (!SvIMMORTAL(sv) && AvREAL(av))
2704 (void)sv_2mortal(sv);
2711 djSP; dMARK; dORIGMARK; dTARGET;
2712 register AV *ary = (AV*)*++MARK;
2717 if (SvRMAGICAL(ary) && (mg = mg_find((SV*)ary,'P'))) {
2720 *MARK-- = mg->mg_obj;
2723 perl_call_method("UNSHIFT",G_SCALAR|G_DISCARD);
2728 av_unshift(ary, SP - MARK);
2731 sv_setsv(sv, *++MARK);
2732 (void)av_store(ary, i++, sv);
2736 PUSHi( AvFILL(ary) + 1 );
2746 if (GIMME == G_ARRAY) {
2757 register char *down;
2763 do_join(TARG, &sv_no, MARK, SP);
2765 sv_setsv(TARG, (SP > MARK) ? *SP : DEFSV);
2766 up = SvPV_force(TARG, len);
2768 down = SvPVX(TARG) + len - 1;
2774 (void)SvPOK_only(TARG);
2783 mul128(SV *sv, U8 m)
2786 char *s = SvPV(sv, len);
2790 if (!strnEQ(s, "0000", 4)) { /* need to grow sv */
2791 SV *New = newSVpv("0000000000", 10);
2794 SvREFCNT_dec(sv); /* free old sv */
2799 while (!*t) /* trailing '\0'? */
2802 i = ((*t - '0') << 7) + m;
2803 *(t--) = '0' + (i % 10);
2809 /* Explosives and implosives. */
2816 I32 gimme = GIMME_V;
2820 register char *pat = SvPV(left, llen);
2821 register char *s = SvPV(right, rlen);
2822 char *strend = s + rlen;
2824 register char *patend = pat + llen;
2829 /* These must not be in registers: */
2840 unsigned Quad_t auquad;
2846 register U32 culong;
2848 static char* bitcount = 0;
2851 if (gimme != G_ARRAY) { /* arrange to do first one only */
2853 for (patend = pat; !isALPHA(*patend) || *patend == 'x'; patend++) ;
2854 if (strchr("aAbBhHP", *patend) || *pat == '%') {
2856 while (isDIGIT(*patend) || *patend == '*')
2862 while (pat < patend) {
2864 datumtype = *pat++ & 0xFF;
2865 if (isSPACE(datumtype))
2869 else if (*pat == '*') {
2870 len = strend - strbeg; /* long enough */
2873 else if (isDIGIT(*pat)) {
2875 while (isDIGIT(*pat))
2876 len = (len * 10) + (*pat++ - '0');
2879 len = (datumtype != '@');
2882 croak("Invalid type in unpack: '%c'", (int)datumtype);
2883 case ',': /* grandfather in commas but with a warning */
2884 if (commas++ == 0 && dowarn)
2885 warn("Invalid type in unpack: '%c'", (int)datumtype);
2888 if (len == 1 && pat[-1] != '1')
2897 if (len > strend - strbeg)
2898 DIE("@ outside of string");
2902 if (len > s - strbeg)
2903 DIE("X outside of string");
2907 if (len > strend - s)
2908 DIE("x outside of string");
2913 if (len > strend - s)
2916 goto uchar_checksum;
2917 sv = NEWSV(35, len);
2918 sv_setpvn(sv, s, len);
2920 if (datumtype == 'A') {
2921 aptr = s; /* borrow register */
2922 s = SvPVX(sv) + len - 1;
2923 while (s >= SvPVX(sv) && (!*s || isSPACE(*s)))
2926 SvCUR_set(sv, s - SvPVX(sv));
2927 s = aptr; /* unborrow register */
2929 XPUSHs(sv_2mortal(sv));
2933 if (pat[-1] == '*' || len > (strend - s) * 8)
2934 len = (strend - s) * 8;
2937 Newz(601, bitcount, 256, char);
2938 for (bits = 1; bits < 256; bits++) {
2939 if (bits & 1) bitcount[bits]++;
2940 if (bits & 2) bitcount[bits]++;
2941 if (bits & 4) bitcount[bits]++;
2942 if (bits & 8) bitcount[bits]++;
2943 if (bits & 16) bitcount[bits]++;
2944 if (bits & 32) bitcount[bits]++;
2945 if (bits & 64) bitcount[bits]++;
2946 if (bits & 128) bitcount[bits]++;
2950 culong += bitcount[*(unsigned char*)s++];
2955 if (datumtype == 'b') {
2957 if (bits & 1) culong++;
2963 if (bits & 128) culong++;
2970 sv = NEWSV(35, len + 1);
2973 aptr = pat; /* borrow register */
2975 if (datumtype == 'b') {
2977 for (len = 0; len < aint; len++) {
2978 if (len & 7) /*SUPPRESS 595*/
2982 *pat++ = '0' + (bits & 1);
2987 for (len = 0; len < aint; len++) {
2992 *pat++ = '0' + ((bits & 128) != 0);
2996 pat = aptr; /* unborrow register */
2997 XPUSHs(sv_2mortal(sv));
3001 if (pat[-1] == '*' || len > (strend - s) * 2)
3002 len = (strend - s) * 2;
3003 sv = NEWSV(35, len + 1);
3006 aptr = pat; /* borrow register */
3008 if (datumtype == 'h') {
3010 for (len = 0; len < aint; len++) {
3015 *pat++ = hexdigit[bits & 15];
3020 for (len = 0; len < aint; len++) {
3025 *pat++ = hexdigit[(bits >> 4) & 15];
3029 pat = aptr; /* unborrow register */
3030 XPUSHs(sv_2mortal(sv));
3033 if (len > strend - s)
3038 if (aint >= 128) /* fake up signed chars */
3048 if (aint >= 128) /* fake up signed chars */
3051 sv_setiv(sv, (IV)aint);
3052 PUSHs(sv_2mortal(sv));
3057 if (len > strend - s)
3072 sv_setiv(sv, (IV)auint);
3073 PUSHs(sv_2mortal(sv));
3078 along = (strend - s) / SIZE16;
3095 sv_setiv(sv, (IV)ashort);
3096 PUSHs(sv_2mortal(sv));
3103 along = (strend - s) / SIZE16;
3108 COPY16(s, &aushort);
3111 if (datumtype == 'n')
3112 aushort = PerlSock_ntohs(aushort);
3115 if (datumtype == 'v')
3116 aushort = vtohs(aushort);
3125 COPY16(s, &aushort);
3129 if (datumtype == 'n')
3130 aushort = PerlSock_ntohs(aushort);
3133 if (datumtype == 'v')
3134 aushort = vtohs(aushort);
3136 sv_setiv(sv, (IV)aushort);
3137 PUSHs(sv_2mortal(sv));
3142 along = (strend - s) / sizeof(int);
3147 Copy(s, &aint, 1, int);
3150 cdouble += (double)aint;
3159 Copy(s, &aint, 1, int);
3162 sv_setiv(sv, (IV)aint);
3163 PUSHs(sv_2mortal(sv));
3168 along = (strend - s) / sizeof(unsigned int);
3173 Copy(s, &auint, 1, unsigned int);
3174 s += sizeof(unsigned int);
3176 cdouble += (double)auint;
3185 Copy(s, &auint, 1, unsigned int);
3186 s += sizeof(unsigned int);
3188 sv_setuv(sv, (UV)auint);
3189 PUSHs(sv_2mortal(sv));
3194 along = (strend - s) / SIZE32;
3202 cdouble += (double)along;
3214 sv_setiv(sv, (IV)along);
3215 PUSHs(sv_2mortal(sv));
3222 along = (strend - s) / SIZE32;
3230 if (datumtype == 'N')
3231 aulong = PerlSock_ntohl(aulong);
3234 if (datumtype == 'V')
3235 aulong = vtohl(aulong);
3238 cdouble += (double)aulong;
3250 if (datumtype == 'N')
3251 aulong = PerlSock_ntohl(aulong);
3254 if (datumtype == 'V')
3255 aulong = vtohl(aulong);
3258 sv_setuv(sv, (UV)aulong);
3259 PUSHs(sv_2mortal(sv));
3264 along = (strend - s) / sizeof(char*);
3270 if (sizeof(char*) > strend - s)
3273 Copy(s, &aptr, 1, char*);
3279 PUSHs(sv_2mortal(sv));
3289 while ((len > 0) && (s < strend)) {
3290 auv = (auv << 7) | (*s & 0x7f);
3291 if (!(*s++ & 0x80)) {
3295 PUSHs(sv_2mortal(sv));
3299 else if (++bytes >= sizeof(UV)) { /* promote to string */
3302 sv = newSVpvf("%.*Vu", (int)TYPE_DIGITS(UV), auv);
3303 while (s < strend) {
3304 sv = mul128(sv, *s & 0x7f);
3305 if (!(*s++ & 0x80)) {
3314 PUSHs(sv_2mortal(sv));
3319 if ((s >= strend) && bytes)
3320 croak("Unterminated compressed integer");
3325 if (sizeof(char*) > strend - s)
3328 Copy(s, &aptr, 1, char*);
3333 sv_setpvn(sv, aptr, len);
3334 PUSHs(sv_2mortal(sv));
3341 if (s + sizeof(Quad_t) > strend)
3344 Copy(s, &aquad, 1, Quad_t);
3345 s += sizeof(Quad_t);
3348 if (aquad >= IV_MIN && aquad <= IV_MAX)
3349 sv_setiv(sv, (IV)aquad);
3351 sv_setnv(sv, (double)aquad);
3352 PUSHs(sv_2mortal(sv));
3359 if (s + sizeof(unsigned Quad_t) > strend)
3362 Copy(s, &auquad, 1, unsigned Quad_t);
3363 s += sizeof(unsigned Quad_t);
3366 if (aquad <= UV_MAX)
3367 sv_setuv(sv, (UV)auquad);
3369 sv_setnv(sv, (double)auquad);
3370 PUSHs(sv_2mortal(sv));
3374 /* float and double added gnb@melba.bby.oz.au 22/11/89 */
3377 along = (strend - s) / sizeof(float);
3382 Copy(s, &afloat, 1, float);
3391 Copy(s, &afloat, 1, float);
3394 sv_setnv(sv, (double)afloat);
3395 PUSHs(sv_2mortal(sv));
3401 along = (strend - s) / sizeof(double);
3406 Copy(s, &adouble, 1, double);
3407 s += sizeof(double);
3415 Copy(s, &adouble, 1, double);
3416 s += sizeof(double);
3418 sv_setnv(sv, (double)adouble);
3419 PUSHs(sv_2mortal(sv));
3424 along = (strend - s) * 3 / 4;
3425 sv = NEWSV(42, along);
3428 while (s < strend && *s > ' ' && *s < 'a') {
3433 len = (*s++ - ' ') & 077;
3435 if (s < strend && *s >= ' ')
3436 a = (*s++ - ' ') & 077;
3439 if (s < strend && *s >= ' ')
3440 b = (*s++ - ' ') & 077;
3443 if (s < strend && *s >= ' ')
3444 c = (*s++ - ' ') & 077;
3447 if (s < strend && *s >= ' ')
3448 d = (*s++ - ' ') & 077;
3451 hunk[0] = (a << 2) | (b >> 4);
3452 hunk[1] = (b << 4) | (c >> 2);
3453 hunk[2] = (c << 6) | d;
3454 sv_catpvn(sv, hunk, (len > 3) ? 3 : len);
3459 else if (s[1] == '\n') /* possible checksum byte */
3462 XPUSHs(sv_2mortal(sv));
3467 if (strchr("fFdD", datumtype) ||
3468 (checksum > 32 && strchr("iIlLN", datumtype)) ) {
3472 while (checksum >= 16) {
3476 while (checksum >= 4) {
3482 along = (1 << checksum) - 1;
3483 while (cdouble < 0.0)
3485 cdouble = modf(cdouble / adouble, &trouble) * adouble;
3486 sv_setnv(sv, cdouble);
3489 if (checksum < 32) {
3490 aulong = (1 << checksum) - 1;
3493 sv_setuv(sv, (UV)culong);
3495 XPUSHs(sv_2mortal(sv));
3499 if (sp == oldsp && gimme == G_SCALAR)
3505 doencodes(register SV *sv, register char *s, register I32 len)
3510 sv_catpvn(sv, hunk, 1);
3513 hunk[0] = ' ' + (077 & (*s >> 2));
3514 hunk[1] = ' ' + (077 & (((*s << 4) & 060) | ((s[1] >> 4) & 017)));
3515 hunk[2] = ' ' + (077 & (((s[1] << 2) & 074) | ((s[2] >> 6) & 03)));
3516 hunk[3] = ' ' + (077 & (s[2] & 077));
3517 sv_catpvn(sv, hunk, 4);
3521 for (s = SvPVX(sv); *s; s++) {
3525 sv_catpvn(sv, "\n", 1);
3529 is_an_int(char *s, STRLEN l)
3531 SV *result = newSVpv("", l);
3532 char *result_c = SvPV(result, na); /* convenience */
3533 char *out = result_c;
3543 SvREFCNT_dec(result);
3566 SvREFCNT_dec(result);
3572 SvCUR_set(result, out - result_c);
3577 div128(SV *pnum, bool *done)
3578 /* must be '\0' terminated */
3582 char *s = SvPV(pnum, len);
3591 i = m * 10 + (*t - '0');
3593 r = (i >> 7); /* r < 10 */
3600 SvCUR_set(pnum, (STRLEN) (t - s));
3607 djSP; dMARK; dORIGMARK; dTARGET;
3608 register SV *cat = TARG;
3611 register char *pat = SvPVx(*++MARK, fromlen);
3612 register char *patend = pat + fromlen;
3617 static char null10[] = {0,0,0,0,0,0,0,0,0,0};
3618 static char *space10 = " ";
3620 /* These must not be in registers: */
3629 unsigned Quad_t auquad;
3638 sv_setpvn(cat, "", 0);
3639 while (pat < patend) {
3640 #define NEXTFROM (items-- > 0 ? *MARK++ : &sv_no)
3641 datumtype = *pat++ & 0xFF;
3642 if (isSPACE(datumtype))
3645 len = strchr("@Xxu", datumtype) ? 0 : items;
3648 else if (isDIGIT(*pat)) {
3650 while (isDIGIT(*pat))
3651 len = (len * 10) + (*pat++ - '0');
3657 croak("Invalid type in pack: '%c'", (int)datumtype);
3658 case ',': /* grandfather in commas but with a warning */
3659 if (commas++ == 0 && dowarn)
3660 warn("Invalid type in pack: '%c'", (int)datumtype);
3663 DIE("%% may only be used in unpack");
3674 if (SvCUR(cat) < len)
3675 DIE("X outside of string");
3682 sv_catpvn(cat, null10, 10);
3685 sv_catpvn(cat, null10, len);
3690 aptr = SvPV(fromstr, fromlen);
3694 sv_catpvn(cat, aptr, len);
3696 sv_catpvn(cat, aptr, fromlen);
3698 if (datumtype == 'A') {
3700 sv_catpvn(cat, space10, 10);
3703 sv_catpvn(cat, space10, len);
3707 sv_catpvn(cat, null10, 10);
3710 sv_catpvn(cat, null10, len);
3717 char *savepat = pat;
3722 aptr = SvPV(fromstr, fromlen);
3727 SvCUR(cat) += (len+7)/8;
3728 SvGROW(cat, SvCUR(cat) + 1);
3729 aptr = SvPVX(cat) + aint;
3734 if (datumtype == 'B') {
3735 for (len = 0; len++ < aint;) {
3736 items |= *pat++ & 1;
3740 *aptr++ = items & 0xff;
3746 for (len = 0; len++ < aint;) {
3752 *aptr++ = items & 0xff;
3758 if (datumtype == 'B')
3759 items <<= 7 - (aint & 7);
3761 items >>= 7 - (aint & 7);
3762 *aptr++ = items & 0xff;
3764 pat = SvPVX(cat) + SvCUR(cat);
3775 char *savepat = pat;
3780 aptr = SvPV(fromstr, fromlen);
3785 SvCUR(cat) += (len+1)/2;
3786 SvGROW(cat, SvCUR(cat) + 1);
3787 aptr = SvPVX(cat) + aint;
3792 if (datumtype == 'H') {
3793 for (len = 0; len++ < aint;) {
3795 items |= ((*pat++ & 15) + 9) & 15;
3797 items |= *pat++ & 15;
3801 *aptr++ = items & 0xff;
3807 for (len = 0; len++ < aint;) {
3809 items |= (((*pat++ & 15) + 9) & 15) << 4;
3811 items |= (*pat++ & 15) << 4;
3815 *aptr++ = items & 0xff;
3821 *aptr++ = items & 0xff;
3822 pat = SvPVX(cat) + SvCUR(cat);
3834 aint = SvIV(fromstr);
3836 sv_catpvn(cat, &achar, sizeof(char));
3839 /* Float and double added by gnb@melba.bby.oz.au 22/11/89 */
3844 afloat = (float)SvNV(fromstr);
3845 sv_catpvn(cat, (char *)&afloat, sizeof (float));
3852 adouble = (double)SvNV(fromstr);
3853 sv_catpvn(cat, (char *)&adouble, sizeof (double));
3859 ashort = (I16)SvIV(fromstr);
3861 ashort = PerlSock_htons(ashort);
3863 CAT16(cat, &ashort);
3869 ashort = (I16)SvIV(fromstr);
3871 ashort = htovs(ashort);
3873 CAT16(cat, &ashort);
3880 ashort = (I16)SvIV(fromstr);
3881 CAT16(cat, &ashort);
3887 auint = SvUV(fromstr);
3888 sv_catpvn(cat, (char*)&auint, sizeof(unsigned int));
3894 adouble = floor(SvNV(fromstr));
3897 croak("Cannot compress negative numbers");
3903 #ifdef CXUX_BROKEN_CONSTANT_CONVERT
3904 adouble <= UV_MAX_cxux
3911 char buf[1 + sizeof(UV)];
3912 char *in = buf + sizeof(buf);
3913 UV auv = U_V(adouble);;
3916 *--in = (auv & 0x7f) | 0x80;
3919 buf[sizeof(buf) - 1] &= 0x7f; /* clear continue bit */
3920 sv_catpvn(cat, in, (buf + sizeof(buf)) - in);
3922 else if (SvPOKp(fromstr)) { /* decimal string arithmetics */
3923 char *from, *result, *in;
3928 /* Copy string and check for compliance */
3929 from = SvPV(fromstr, len);
3930 if ((norm = is_an_int(from, len)) == NULL)
3931 croak("can compress only unsigned integer");
3933 New('w', result, len, char);
3937 *--in = div128(norm, &done) | 0x80;
3938 result[len - 1] &= 0x7F; /* clear continue bit */
3939 sv_catpvn(cat, in, (result + len) - in);
3941 SvREFCNT_dec(norm); /* free norm */
3943 else if (SvNOKp(fromstr)) {
3944 char buf[sizeof(double) * 2]; /* 8/7 <= 2 */
3945 char *in = buf + sizeof(buf);
3948 double next = floor(adouble / 128);
3949 *--in = (unsigned char)(adouble - (next * 128)) | 0x80;
3950 if (--in < buf) /* this cannot happen ;-) */
3951 croak ("Cannot compress integer");
3953 } while (adouble > 0);
3954 buf[sizeof(buf) - 1] &= 0x7f; /* clear continue bit */
3955 sv_catpvn(cat, in, (buf + sizeof(buf)) - in);
3958 croak("Cannot compress non integer");
3964 aint = SvIV(fromstr);
3965 sv_catpvn(cat, (char*)&aint, sizeof(int));
3971 aulong = SvUV(fromstr);
3973 aulong = PerlSock_htonl(aulong);
3975 CAT32(cat, &aulong);
3981 aulong = SvUV(fromstr);
3983 aulong = htovl(aulong);
3985 CAT32(cat, &aulong);
3991 aulong = SvUV(fromstr);
3992 CAT32(cat, &aulong);
3998 along = SvIV(fromstr);
4006 auquad = (unsigned Quad_t)SvIV(fromstr);
4007 sv_catpvn(cat, (char*)&auquad, sizeof(unsigned Quad_t));
4013 aquad = (Quad_t)SvIV(fromstr);
4014 sv_catpvn(cat, (char*)&aquad, sizeof(Quad_t));
4017 #endif /* HAS_QUAD */
4019 len = 1; /* assume SV is correct length */
4024 if (fromstr == &sv_undef)
4027 /* XXX better yet, could spirit away the string to
4028 * a safe spot and hang on to it until the result
4029 * of pack() (and all copies of the result) are
4032 if (dowarn && (SvTEMP(fromstr) || SvPADTMP(fromstr)))
4033 warn("Attempt to pack pointer to temporary value");
4034 if (SvPOK(fromstr) || SvNIOK(fromstr))
4035 aptr = SvPV(fromstr,na);
4037 aptr = SvPV_force(fromstr,na);
4039 sv_catpvn(cat, (char*)&aptr, sizeof(char*));
4044 aptr = SvPV(fromstr, fromlen);
4045 SvGROW(cat, fromlen * 4 / 3);
4050 while (fromlen > 0) {
4057 doencodes(cat, aptr, todo);
4076 register I32 limit = POPi; /* note, negative is forever */
4079 register char *s = SvPV(sv, len);
4080 char *strend = s + len;
4082 register REGEXP *rx;
4086 I32 maxiters = (strend - s) + 10;
4089 I32 origlimit = limit;
4092 AV *oldstack = curstack;
4093 I32 gimme = GIMME_V;
4094 I32 oldsave = savestack_ix;
4095 I32 make_mortal = 1;
4096 MAGIC *mg = (MAGIC *) NULL;
4099 Copy(&LvTARGOFF(POPs), &pm, 1, PMOP*);
4104 DIE("panic: do_split");
4105 rx = pm->op_pmregexp;
4107 TAINT_IF((pm->op_pmflags & PMf_LOCALE) &&
4108 (pm->op_pmflags & (PMf_WHITE | PMf_SKIPWHITE)));
4110 if (pm->op_pmreplroot)
4111 ary = GvAVn((GV*)pm->op_pmreplroot);
4112 else if (gimme != G_ARRAY)
4114 ary = (AV*)curpad[0];
4117 #endif /* USE_THREADS */
4120 if (ary && (gimme != G_ARRAY || (pm->op_pmflags & PMf_ONCE))) {
4126 if (SvRMAGICAL(ary) && (mg = mg_find((SV *) ary, 'P'))) {
4133 for (i = AvFILLp(ary); i >= 0; i--)
4134 AvARRAY(ary)[i] = &sv_undef; /* don't free mere refs */
4136 /* temporarily switch stacks */
4137 SWITCHSTACK(curstack, ary);
4141 base = SP - stack_base;
4143 if (pm->op_pmflags & PMf_SKIPWHITE) {
4144 if (pm->op_pmflags & PMf_LOCALE) {
4145 while (isSPACE_LC(*s))
4153 if (pm->op_pmflags & (PMf_MULTILINE|PMf_SINGLELINE)) {
4155 multiline = pm->op_pmflags & PMf_MULTILINE;
4159 limit = maxiters + 2;
4160 if (pm->op_pmflags & PMf_WHITE) {
4163 while (m < strend &&
4164 !((pm->op_pmflags & PMf_LOCALE)
4165 ? isSPACE_LC(*m) : isSPACE(*m)))
4170 dstr = NEWSV(30, m-s);
4171 sv_setpvn(dstr, s, m-s);
4177 while (s < strend &&
4178 ((pm->op_pmflags & PMf_LOCALE)
4179 ? isSPACE_LC(*s) : isSPACE(*s)))
4183 else if (strEQ("^", rx->precomp)) {
4186 for (m = s; m < strend && *m != '\n'; m++) ;
4190 dstr = NEWSV(30, m-s);
4191 sv_setpvn(dstr, s, m-s);
4198 else if (rx->check_substr && !rx->nparens
4199 && (rx->reganch & ROPT_CHECK_ALL)
4200 && !(rx->reganch & ROPT_ANCH)) {
4201 i = SvCUR(rx->check_substr);
4202 if (i == 1 && !SvTAIL(rx->check_substr)) {
4203 i = *SvPVX(rx->check_substr);
4206 for (m = s; m < strend && *m != i; m++) ;
4209 dstr = NEWSV(30, m-s);
4210 sv_setpvn(dstr, s, m-s);
4219 while (s < strend && --limit &&
4220 (m=fbm_instr((unsigned char*)s, (unsigned char*)strend,
4221 rx->check_substr)) )
4224 dstr = NEWSV(31, m-s);
4225 sv_setpvn(dstr, s, m-s);
4234 maxiters += (strend - s) * rx->nparens;
4235 while (s < strend && --limit &&
4236 regexec_flags(rx, s, strend, orig, 1, Nullsv, NULL, 0))
4238 TAINT_IF(RX_MATCH_TAINTED(rx));
4240 && rx->subbase != orig) {
4245 strend = s + (strend - m);
4248 dstr = NEWSV(32, m-s);
4249 sv_setpvn(dstr, s, m-s);
4254 for (i = 1; i <= rx->nparens; i++) {
4258 dstr = NEWSV(33, m-s);
4259 sv_setpvn(dstr, s, m-s);
4262 dstr = NEWSV(33, 0);
4272 LEAVE_SCOPE(oldsave);
4273 iters = (SP - stack_base) - base;
4274 if (iters > maxiters)
4277 /* keep field after final delim? */
4278 if (s < strend || (iters && origlimit)) {
4279 dstr = NEWSV(34, strend-s);
4280 sv_setpvn(dstr, s, strend-s);
4286 else if (!origlimit) {
4287 while (iters > 0 && (!TOPs || !SvANY(TOPs) || SvCUR(TOPs) == 0))
4293 SWITCHSTACK(ary, oldstack);
4294 if (SvSMAGICAL(ary)) {
4299 if (gimme == G_ARRAY) {
4301 Copy(AvARRAY(ary), SP + 1, iters, SV*);
4309 perl_call_method("PUSH",G_SCALAR|G_DISCARD);
4312 if (gimme == G_ARRAY) {
4313 /* EXTEND should not be needed - we just popped them */
4315 for (i=0; i < iters; i++) {
4316 SV **svp = av_fetch(ary, i, FALSE);
4317 PUSHs((svp) ? *svp : &sv_undef);
4324 if (gimme == G_ARRAY)
4327 if (iters || !pm->op_pmreplroot) {
4337 unlock_condpair(void *svv)
4340 MAGIC *mg = mg_find((SV*)svv, 'm');
4343 croak("panic: unlock_condpair unlocking non-mutex");
4344 MUTEX_LOCK(MgMUTEXP(mg));
4345 if (MgOWNER(mg) != thr)
4346 croak("panic: unlock_condpair unlocking mutex that we don't own");
4348 COND_SIGNAL(MgOWNERCONDP(mg));
4349 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "0x%lx: unlock 0x%lx\n",
4350 (unsigned long)thr, (unsigned long)svv);)
4351 MUTEX_UNLOCK(MgMUTEXP(mg));
4353 #endif /* USE_THREADS */
4366 mg = condpair_magic(sv);
4367 MUTEX_LOCK(MgMUTEXP(mg));
4368 if (MgOWNER(mg) == thr)
4369 MUTEX_UNLOCK(MgMUTEXP(mg));
4372 COND_WAIT(MgOWNERCONDP(mg), MgMUTEXP(mg));
4374 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "0x%lx: pp_lock lock 0x%lx\n",
4375 (unsigned long)thr, (unsigned long)sv);)
4376 MUTEX_UNLOCK(MgMUTEXP(mg));
4377 SvREFCNT_inc(sv); /* keep alive until magic_mutexfree */
4378 save_destructor(unlock_condpair, sv);
4380 #endif /* USE_THREADS */
4381 if (SvTYPE(retsv) == SVt_PVAV || SvTYPE(retsv) == SVt_PVHV
4382 || SvTYPE(retsv) == SVt_PVCV) {
4383 retsv = refto(retsv);
4394 if (op->op_private & OPpLVAL_INTRO)
4395 PUSHs(*save_threadsv(op->op_targ));
4397 PUSHs(*av_fetch(thr->threadsv, op->op_targ, FALSE));
4400 DIE("tried to access per-thread data in non-threaded perl");
4401 #endif /* USE_THREADS */