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);
32 extern "C" char *fcrypt(char *,char *);
34 extern "C" char *crypt(char *,char *);
40 * Types used in bitwise operations.
42 * Normally we'd just use IV and UV. However, some hardware and
43 * software combinations (e.g. Alpha and current OSF/1) don't have a
44 * floating-point type to use for NV that has adequate bits to fully
45 * hold an IV/UV. (In other words, sizeof(long) == sizeof(double).)
47 * It just so happens that "int" is the right size almost everywhere.
53 * Mask used after bitwise operations.
55 * There is at least one realm (Cray word machines) that doesn't
56 * have an integral type (except char) small enough to be represented
57 * in a double without loss; that is, it has no 32-bit type.
59 #if BYTEORDER > 0xFFFF && defined(_CRAY) && !defined(_CRAYMPP)
61 # define BW_MASK ((1 << BW_BITS) - 1)
62 # define BW_SIGN (1 << (BW_BITS - 1))
63 # define BWi(i) (((i) & BW_SIGN) ? ((i) | ~BW_MASK) : ((i) & BW_MASK))
64 # define BWu(u) ((u) & BW_MASK)
71 * Offset for integer pack/unpack.
73 * On architectures where I16 and I32 aren't really 16 and 32 bits,
74 * which for now are all Crays, pack and unpack have to play games.
78 * These values are required for portability of pack() output.
79 * If they're not right on your machine, then pack() and unpack()
80 * wouldn't work right anyway; you'll need to apply the Cray hack.
81 * (I'd like to check them with #if, but you can't use sizeof() in
87 #if BYTEORDER > 0xFFFF && defined(_CRAY) && !defined(_CRAYMPP)
88 # if BYTEORDER == 0x12345678
89 # define OFF16(p) (char*)(p)
90 # define OFF32(p) (char*)(p)
92 # if BYTEORDER == 0x87654321
93 # define OFF16(p) ((char*)(p) + (sizeof(U16) - SIZE16))
94 # define OFF32(p) ((char*)(p) + (sizeof(U32) - SIZE32))
96 }}}} bad cray byte order
99 # define COPY16(s,p) (*(p) = 0, Copy(s, OFF16(p), SIZE16, char))
100 # define COPY32(s,p) (*(p) = 0, Copy(s, OFF32(p), SIZE32, char))
101 # define CAT16(sv,p) sv_catpvn(sv, OFF16(p), SIZE16)
102 # define CAT32(sv,p) sv_catpvn(sv, OFF32(p), SIZE32)
104 # define COPY16(s,p) Copy(s, p, SIZE16, char)
105 # define COPY32(s,p) Copy(s, p, SIZE32, char)
106 # define CAT16(sv,p) sv_catpvn(sv, (char*)(p), SIZE16)
107 # define CAT32(sv,p) sv_catpvn(sv, (char*)(p), SIZE32)
110 static void doencodes _((SV* sv, char* s, I32 len));
111 static SV* refto _((SV* sv));
112 static U32 seed _((void));
114 static bool srand_called = FALSE;
116 /* variations on pp_null */
118 #ifdef DONT_DECLARE_STD
123 extern pid_t getpid (void);
129 if (GIMME_V == G_SCALAR)
144 if (op->op_private & OPpLVAL_INTRO)
145 SAVECLEARSV(curpad[op->op_targ]);
147 if (op->op_flags & OPf_REF) {
151 if (GIMME == G_ARRAY) {
152 I32 maxarg = AvFILL((AV*)TARG) + 1;
154 Copy(AvARRAY((AV*)TARG), SP+1, maxarg, SV*);
158 SV* sv = sv_newmortal();
159 I32 maxarg = AvFILL((AV*)TARG) + 1;
160 sv_setiv(sv, maxarg);
172 if (op->op_private & OPpLVAL_INTRO)
173 SAVECLEARSV(curpad[op->op_targ]);
174 if (op->op_flags & OPf_REF)
177 if (gimme == G_ARRAY) {
178 RETURNOP(do_kv(ARGS));
180 else if (gimme == G_SCALAR) {
181 SV* sv = sv_newmortal();
182 if (HvFILL((HV*)TARG))
183 sv_setpvf(sv, "%ld/%ld",
184 (long)HvFILL((HV*)TARG), (long)HvMAX((HV*)TARG) + 1);
194 DIE("NOT IMPL LINE %d",__LINE__);
206 if (SvTYPE(sv) == SVt_PVIO) {
207 GV *gv = (GV*) sv_newmortal();
208 gv_init(gv, 0, "", 0, 0);
209 GvIOp(gv) = (IO *)sv;
210 (void)SvREFCNT_inc(sv);
212 } else if (SvTYPE(sv) != SVt_PVGV)
213 DIE("Not a GLOB reference");
216 if (SvTYPE(sv) != SVt_PVGV) {
219 if (SvGMAGICAL(sv)) {
225 if (op->op_flags & OPf_REF ||
226 op->op_private & HINT_STRICT_REFS)
227 DIE(no_usym, "a symbol");
233 if (op->op_private & HINT_STRICT_REFS)
234 DIE(no_symref, sym, "a symbol");
235 sv = (SV*)gv_fetchpv(sym, TRUE, SVt_PVGV);
238 if (op->op_private & OPpLVAL_INTRO)
239 save_gp((GV*)sv, !(op->op_flags & OPf_SPECIAL));
251 switch (SvTYPE(sv)) {
255 DIE("Not a SCALAR reference");
262 if (SvTYPE(gv) != SVt_PVGV) {
263 if (SvGMAGICAL(sv)) {
269 if (op->op_flags & OPf_REF ||
270 op->op_private & HINT_STRICT_REFS)
271 DIE(no_usym, "a SCALAR");
277 if (op->op_private & HINT_STRICT_REFS)
278 DIE(no_symref, sym, "a SCALAR");
279 gv = (GV*)gv_fetchpv(sym, TRUE, SVt_PV);
283 if (op->op_flags & OPf_MOD) {
284 if (op->op_private & OPpLVAL_INTRO)
285 sv = save_scalar((GV*)TOPs);
286 else if (op->op_private & OPpDEREF)
287 vivify_ref(sv, op->op_private & OPpDEREF);
297 SV *sv = AvARYLEN(av);
299 AvARYLEN(av) = sv = NEWSV(0,0);
300 sv_upgrade(sv, SVt_IV);
301 sv_magic(sv, (SV*)av, '#', Nullch, 0);
309 dSP; dTARGET; dPOPss;
311 if (op->op_flags & OPf_MOD) {
312 if (SvTYPE(TARG) < SVt_PVLV) {
313 sv_upgrade(TARG, SVt_PVLV);
314 sv_magic(TARG, Nullsv, '.', Nullch, 0);
319 PUSHs(TARG); /* no SvSETMAGIC */
325 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
326 mg = mg_find(sv, 'g');
327 if (mg && mg->mg_len >= 0) {
328 PUSHi(mg->mg_len + curcop->cop_arybase);
342 /* We usually try to add a non-existent subroutine in case of AUTOLOAD. */
343 /* (But not in defined().) */
344 CV *cv = sv_2cv(TOPs, &stash, &gv, !(op->op_flags & OPf_SPECIAL));
347 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
364 cv = sv_2cv(TOPs, &stash, &gv, FALSE);
366 ret = sv_2mortal(newSVpv(SvPVX(cv), SvCUR(cv)));
374 CV* cv = (CV*)curpad[op->op_targ];
376 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
392 if (GIMME != G_ARRAY) {
396 EXTEND_MORTAL(SP - MARK);
398 *MARK = refto(*MARK);
407 if (SvTYPE(sv) == SVt_PVLV && LvTYPE(sv) == 'y') {
410 if (!(sv = LvTARG(sv)))
413 else if (SvPADTMP(sv))
416 dTHR; /* just for SvREFCNT_inc */
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"))
481 if (strEQ(elem, "CODE"))
482 ref = (SV*)GvCVu(gv);
485 if (strEQ(elem, "FILEHANDLE")) /* XXX deprecate in 5.005 */
486 ref = (SV*)GvIOp(gv);
489 if (strEQ(elem, "GLOB"))
493 if (strEQ(elem, "HASH"))
497 if (strEQ(elem, "IO"))
498 ref = (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 unsigned char *s;
531 register I32 *sfirst;
535 if (sv == lastscream) {
541 SvSCREAM_off(lastscream);
542 SvREFCNT_dec(lastscream);
544 lastscream = SvREFCNT_inc(sv);
547 s = (unsigned char*)(SvPV(sv, len));
551 if (pos > maxscream) {
553 maxscream = pos + 80;
554 New(301, screamfirst, 256, I32);
555 New(302, screamnext, maxscream, I32);
558 maxscream = pos + pos / 4;
559 Renew(screamnext, maxscream, I32);
563 sfirst = screamfirst;
566 if (!sfirst || !snext)
567 DIE("do_study: out of memory");
569 for (ch = 256; ch; --ch)
576 snext[pos] = sfirst[ch] - pos;
583 sv_magic(sv, Nullsv, 'g', Nullch, 0); /* piggyback on m//g magic */
592 if (op->op_flags & OPf_STACKED)
598 TARG = sv_newmortal();
599 PUSHi(do_trans(sv, op));
603 /* Lvalue operators. */
625 SETi(do_chomp(TOPs));
632 register I32 count = 0;
635 count += do_chomp(POPs);
646 if (!sv || !SvANY(sv))
648 switch (SvTYPE(sv)) {
650 if (AvMAX(sv) >= 0 || SvGMAGICAL(sv))
654 if (HvARRAY(sv) || SvGMAGICAL(sv))
658 if (CvROOT(sv) || CvXSUB(sv))
675 if (!op->op_private) {
684 if (SvTHINKFIRST(sv)) {
691 switch (SvTYPE(sv)) {
701 if (cv_const_sv((CV*)sv))
702 warn("Constant subroutine %s undefined",
703 CvANON((CV*)sv) ? "(anonymous)" : GvENAME(CvGV((CV*)sv)));
706 { GV* gv = (GV*)SvREFCNT_inc(CvGV((CV*)sv));
708 CvGV((CV*)sv) = gv; } /* let user-undef'd sub keep its identity */
712 sv_setsv(sv, &sv_undef);
715 if (SvTYPE(sv) >= SVt_PV && SvPVX(sv) && SvLEN(sv)) {
718 SvPV_set(sv, Nullch);
731 if (SvREADONLY(TOPs) || SvTYPE(TOPs) > SVt_PVLV)
733 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) &&
734 SvIVX(TOPs) != IV_MIN)
737 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
748 if (SvREADONLY(TOPs) || SvTYPE(TOPs) > SVt_PVLV)
750 sv_setsv(TARG, TOPs);
751 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) &&
752 SvIVX(TOPs) != IV_MAX)
755 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
769 if(SvREADONLY(TOPs) || SvTYPE(TOPs) > SVt_PVLV)
771 sv_setsv(TARG, TOPs);
772 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) &&
773 SvIVX(TOPs) != IV_MIN)
776 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
785 /* Ordinary operators. */
789 dSP; dATARGET; tryAMAGICbin(pow,opASSIGN);
792 SETn( pow( left, right) );
799 dSP; dATARGET; tryAMAGICbin(mult,opASSIGN);
802 SETn( left * right );
809 dSP; dATARGET; tryAMAGICbin(div,opASSIGN);
814 DIE("Illegal division by zero");
816 /* insure that 20./5. == 4. */
819 if ((double)I_V(left) == left &&
820 (double)I_V(right) == right &&
821 (k = I_V(left)/I_V(right))*I_V(right) == I_V(left)) {
824 value = left / right;
828 value = left / right;
837 dSP; dATARGET; tryAMAGICbin(mod,opASSIGN);
845 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)) {
847 right = (right_neg = (i < 0)) ? -i : i;
851 right = U_V((right_neg = (n < 0)) ? -n : n);
854 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)) {
856 left = (left_neg = (i < 0)) ? -i : i;
860 left = U_V((left_neg = (n < 0)) ? -n : n);
864 DIE("Illegal modulus zero");
867 if ((left_neg != right_neg) && ans)
870 /* XXX may warn: unary minus operator applied to unsigned type */
871 /* could change -foo to be (~foo)+1 instead */
872 if (ans <= -(UV)IV_MAX)
873 sv_setiv(TARG, (IV) -ans);
875 sv_setnv(TARG, -(double)ans);
886 dSP; dATARGET; tryAMAGICbin(repeat,opASSIGN);
888 register I32 count = POPi;
889 if (GIMME == G_ARRAY && op->op_private & OPpREPEAT_DOLIST) {
891 I32 items = SP - MARK;
903 repeatcpy((char*)(MARK + items), (char*)MARK,
904 items * sizeof(SV*), count - 1);
910 else { /* Note: mark already snarfed by pp_list */
915 if (TARG == tmpstr && SvTHINKFIRST(tmpstr)) {
916 if (SvREADONLY(tmpstr) && curcop != &compiling)
917 DIE("Can't x= to readonly value");
921 SvSetSV(TARG, tmpstr);
922 SvPV_force(TARG, len);
927 SvGROW(TARG, (count * len) + 1);
928 repeatcpy(SvPVX(TARG) + len, SvPVX(TARG), len, count - 1);
929 SvCUR(TARG) *= count;
933 (void)SvPOK_only(TARG);
942 dSP; dATARGET; tryAMAGICbin(subtr,opASSIGN);
945 SETn( left - right );
952 dSP; dATARGET; tryAMAGICbin(lshift,opASSIGN);
955 if (op->op_private & HINT_INTEGER) {
971 dSP; dATARGET; tryAMAGICbin(rshift,opASSIGN);
974 if (op->op_private & HINT_INTEGER) {
990 dSP; tryAMAGICbinSET(lt,0);
993 SETs(boolSV(TOPn < value));
1000 dSP; tryAMAGICbinSET(gt,0);
1003 SETs(boolSV(TOPn > value));
1010 dSP; tryAMAGICbinSET(le,0);
1013 SETs(boolSV(TOPn <= value));
1020 dSP; tryAMAGICbinSET(ge,0);
1023 SETs(boolSV(TOPn >= value));
1030 dSP; tryAMAGICbinSET(ne,0);
1033 SETs(boolSV(TOPn != value));
1040 dSP; dTARGET; tryAMAGICbin(ncmp,0);
1047 else if (left < right)
1049 else if (left > right)
1062 dSP; tryAMAGICbinSET(slt,0);
1065 int cmp = ((op->op_private & OPpLOCALE)
1066 ? sv_cmp_locale(left, right)
1067 : sv_cmp(left, right));
1068 SETs(boolSV(cmp < 0));
1075 dSP; tryAMAGICbinSET(sgt,0);
1078 int cmp = ((op->op_private & OPpLOCALE)
1079 ? sv_cmp_locale(left, right)
1080 : sv_cmp(left, right));
1081 SETs(boolSV(cmp > 0));
1088 dSP; tryAMAGICbinSET(sle,0);
1091 int cmp = ((op->op_private & OPpLOCALE)
1092 ? sv_cmp_locale(left, right)
1093 : sv_cmp(left, right));
1094 SETs(boolSV(cmp <= 0));
1101 dSP; tryAMAGICbinSET(sge,0);
1104 int cmp = ((op->op_private & OPpLOCALE)
1105 ? sv_cmp_locale(left, right)
1106 : sv_cmp(left, right));
1107 SETs(boolSV(cmp >= 0));
1114 dSP; tryAMAGICbinSET(seq,0);
1117 SETs(boolSV(sv_eq(left, right)));
1124 dSP; tryAMAGICbinSET(sne,0);
1127 SETs(boolSV(!sv_eq(left, right)));
1134 dSP; dTARGET; tryAMAGICbin(scmp,0);
1137 int cmp = ((op->op_private & OPpLOCALE)
1138 ? sv_cmp_locale(left, right)
1139 : sv_cmp(left, right));
1147 dSP; dATARGET; tryAMAGICbin(band,opASSIGN);
1150 if (SvNIOKp(left) || SvNIOKp(right)) {
1151 if (op->op_private & HINT_INTEGER) {
1152 IBW value = SvIV(left) & SvIV(right);
1156 UBW value = SvUV(left) & SvUV(right);
1161 do_vop(op->op_type, TARG, left, right);
1170 dSP; dATARGET; tryAMAGICbin(bxor,opASSIGN);
1173 if (SvNIOKp(left) || SvNIOKp(right)) {
1174 if (op->op_private & HINT_INTEGER) {
1175 IBW value = (USE_LEFT(left) ? SvIV(left) : 0) ^ SvIV(right);
1179 UBW value = (USE_LEFT(left) ? SvUV(left) : 0) ^ SvUV(right);
1184 do_vop(op->op_type, TARG, left, right);
1193 dSP; dATARGET; tryAMAGICbin(bor,opASSIGN);
1196 if (SvNIOKp(left) || SvNIOKp(right)) {
1197 if (op->op_private & HINT_INTEGER) {
1198 IBW value = (USE_LEFT(left) ? SvIV(left) : 0) | SvIV(right);
1202 UBW value = (USE_LEFT(left) ? SvUV(left) : 0) | SvUV(right);
1207 do_vop(op->op_type, TARG, left, right);
1216 dSP; dTARGET; tryAMAGICun(neg);
1221 if (SvIOKp(sv) && !SvNOKp(sv) && !SvPOKp(sv) && SvIVX(sv) != IV_MIN)
1223 else if (SvNIOKp(sv))
1225 else if (SvPOKp(sv)) {
1227 char *s = SvPV(sv, len);
1228 if (isIDFIRST(*s)) {
1229 sv_setpvn(TARG, "-", 1);
1232 else if (*s == '+' || *s == '-') {
1234 *SvPV_force(TARG, len) = *s == '-' ? '+' : '-';
1237 sv_setnv(TARG, -SvNV(sv));
1249 dSP; tryAMAGICunSET(not);
1250 #endif /* OVERLOAD */
1251 *stack_sp = boolSV(!SvTRUE(*stack_sp));
1257 dSP; dTARGET; tryAMAGICun(compl);
1261 if (op->op_private & HINT_INTEGER) {
1262 IBW value = ~SvIV(sv);
1266 UBW value = ~SvUV(sv);
1271 register char *tmps;
1272 register long *tmpl;
1277 tmps = SvPV_force(TARG, len);
1280 for ( ; anum && (unsigned long)tmps % sizeof(long); anum--, tmps++)
1283 for ( ; anum >= sizeof(long); anum -= sizeof(long), tmpl++)
1287 for ( ; anum > 0; anum--, tmps++)
1296 /* integer versions of some of the above */
1300 dSP; dATARGET; tryAMAGICbin(mult,opASSIGN);
1303 SETi( left * right );
1310 dSP; dATARGET; tryAMAGICbin(div,opASSIGN);
1314 DIE("Illegal division by zero");
1315 value = POPi / value;
1323 dSP; dATARGET; tryAMAGICbin(mod,opASSIGN);
1327 DIE("Illegal modulus zero");
1328 SETi( left % right );
1335 dSP; dATARGET; tryAMAGICbin(add,opASSIGN);
1338 SETi( left + right );
1345 dSP; dATARGET; tryAMAGICbin(subtr,opASSIGN);
1348 SETi( left - right );
1355 dSP; tryAMAGICbinSET(lt,0);
1358 SETs(boolSV(left < right));
1365 dSP; tryAMAGICbinSET(gt,0);
1368 SETs(boolSV(left > right));
1375 dSP; tryAMAGICbinSET(le,0);
1378 SETs(boolSV(left <= right));
1385 dSP; tryAMAGICbinSET(ge,0);
1388 SETs(boolSV(left >= right));
1395 dSP; tryAMAGICbinSET(eq,0);
1398 SETs(boolSV(left == right));
1405 dSP; tryAMAGICbinSET(ne,0);
1408 SETs(boolSV(left != right));
1415 dSP; dTARGET; tryAMAGICbin(ncmp,0);
1422 else if (left < right)
1433 dSP; dTARGET; tryAMAGICun(neg);
1438 /* High falutin' math. */
1442 dSP; dTARGET; tryAMAGICbin(atan2,0);
1445 SETn(atan2(left, right));
1452 dSP; dTARGET; tryAMAGICun(sin);
1464 dSP; dTARGET; tryAMAGICun(cos);
1484 if (!srand_called) {
1485 (void)srand((unsigned)seed());
1486 srand_called = TRUE;
1489 value = rand() * value / 2147483648.0;
1492 value = rand() * value / 65536.0;
1495 value = rand() * value / 32768.0;
1497 value = rand() * value / (double)(((unsigned long)1) << RANDBITS);
1513 (void)srand((unsigned)anum);
1514 srand_called = TRUE;
1523 * This is really just a quick hack which grabs various garbage
1524 * values. It really should be a real hash algorithm which
1525 * spreads the effect of every input bit onto every output bit,
1526 * if someone who knows about such tings would bother to write it.
1527 * Might be a good idea to add that function to CORE as well.
1528 * No numbers below come from careful analysis or anyting here,
1529 * except they are primes and SEED_C1 > 1E6 to get a full-width
1530 * value from (tv_sec * SEED_C1 + tv_usec). The multipliers should
1531 * probably be bigger too.
1534 # define SEED_C1 1000003
1535 #define SEED_C4 73819
1537 # define SEED_C1 25747
1538 #define SEED_C4 20639
1542 #define SEED_C5 26107
1547 # include <starlet.h>
1548 /* when[] = (low 32 bits, high 32 bits) of time since epoch
1549 * in 100-ns units, typically incremented ever 10 ms. */
1550 unsigned int when[2];
1551 _ckvmssts(sys$gettim(when));
1552 u = (U32)SEED_C1 * when[0] + (U32)SEED_C2 * when[1];
1554 # ifdef HAS_GETTIMEOFDAY
1555 struct timeval when;
1556 gettimeofday(&when,(struct timezone *) 0);
1557 u = (U32)SEED_C1 * when.tv_sec + (U32)SEED_C2 * when.tv_usec;
1561 u = (U32)SEED_C1 * when;
1564 u += SEED_C3 * (U32)getpid();
1565 u += SEED_C4 * (U32)(UV)stack_sp;
1566 #ifndef PLAN9 /* XXX Plan9 assembler chokes on this; fix needed */
1567 u += SEED_C5 * (U32)(UV)&when;
1574 dSP; dTARGET; tryAMAGICun(exp);
1586 dSP; dTARGET; tryAMAGICun(log);
1591 SET_NUMERIC_STANDARD();
1592 DIE("Can't take log of %g", value);
1602 dSP; dTARGET; tryAMAGICun(sqrt);
1607 SET_NUMERIC_STANDARD();
1608 DIE("Can't take sqrt of %g", value);
1610 value = sqrt(value);
1620 double value = TOPn;
1623 if (SvIOKp(TOPs) && !SvNOKp(TOPs) && !SvPOKp(TOPs)) {
1629 (void)modf(value, &value);
1631 (void)modf(-value, &value);
1646 dSP; dTARGET; tryAMAGICun(abs);
1648 double value = TOPn;
1651 if (SvIOKp(TOPs) && !SvNOKp(TOPs) && !SvPOKp(TOPs) &&
1652 (iv = SvIVX(TOPs)) != IV_MIN) {
1673 XPUSHu(scan_hex(tmps, 99, &argtype));
1685 while (*tmps && isSPACE(*tmps))
1690 value = scan_hex(++tmps, 99, &argtype);
1692 value = scan_oct(tmps, 99, &argtype);
1702 SETi( sv_len(TOPs) );
1715 I32 lvalue = op->op_flags & OPf_MOD;
1717 I32 arybase = curcop->cop_arybase;
1723 tmps = SvPV(sv, curlen);
1724 if (pos >= arybase) {
1742 else if (len >= 0) {
1744 if (rem > (I32)curlen)
1758 if (dowarn || lvalue)
1759 warn("substr outside of string");
1764 sv_setpvn(TARG, tmps, rem);
1765 if (lvalue) { /* it's an lvalue! */
1766 if (!SvGMAGICAL(sv)) {
1770 warn("Attempt to use reference as lvalue in substr");
1772 if (SvOK(sv)) /* is it defined ? */
1773 (void)SvPOK_only(sv);
1775 sv_setpvn(sv,"",0); /* avoid lexical reincarnation */
1778 if (SvTYPE(TARG) < SVt_PVLV) {
1779 sv_upgrade(TARG, SVt_PVLV);
1780 sv_magic(TARG, Nullsv, 'x', Nullch, 0);
1785 LvTARGOFF(TARG) = pos;
1786 LvTARGLEN(TARG) = rem;
1789 PUSHs(TARG); /* avoid SvSETMAGIC here */
1796 register I32 size = POPi;
1797 register I32 offset = POPi;
1798 register SV *src = POPs;
1799 I32 lvalue = op->op_flags & OPf_MOD;
1801 unsigned char *s = (unsigned char*)SvPV(src, srclen);
1802 unsigned long retnum;
1805 offset *= size; /* turn into bit offset */
1806 len = (offset + size + 7) / 8;
1807 if (offset < 0 || size < 1)
1810 if (lvalue) { /* it's an lvalue! */
1811 if (SvTYPE(TARG) < SVt_PVLV) {
1812 sv_upgrade(TARG, SVt_PVLV);
1813 sv_magic(TARG, Nullsv, 'v', Nullch, 0);
1818 LvTARGOFF(TARG) = offset;
1819 LvTARGLEN(TARG) = size;
1827 if (offset >= srclen)
1830 retnum = (unsigned long) s[offset] << 8;
1832 else if (size == 32) {
1833 if (offset >= srclen)
1835 else if (offset + 1 >= srclen)
1836 retnum = (unsigned long) s[offset] << 24;
1837 else if (offset + 2 >= srclen)
1838 retnum = ((unsigned long) s[offset] << 24) +
1839 ((unsigned long) s[offset + 1] << 16);
1841 retnum = ((unsigned long) s[offset] << 24) +
1842 ((unsigned long) s[offset + 1] << 16) +
1843 (s[offset + 2] << 8);
1848 retnum = (s[offset >> 3] >> (offset & 7)) & ((1 << size) - 1);
1853 else if (size == 16)
1854 retnum = ((unsigned long) s[offset] << 8) + s[offset+1];
1855 else if (size == 32)
1856 retnum = ((unsigned long) s[offset] << 24) +
1857 ((unsigned long) s[offset + 1] << 16) +
1858 (s[offset + 2] << 8) + s[offset+3];
1862 sv_setiv(TARG, (IV)retnum);
1877 I32 arybase = curcop->cop_arybase;
1882 offset = POPi - arybase;
1885 tmps = SvPV(big, biglen);
1888 else if (offset > biglen)
1890 if (!(tmps2 = fbm_instr((unsigned char*)tmps + offset,
1891 (unsigned char*)tmps + biglen, little)))
1892 retval = -1 + arybase;
1894 retval = tmps2 - tmps + arybase;
1911 I32 arybase = curcop->cop_arybase;
1917 tmps2 = SvPV(little, llen);
1918 tmps = SvPV(big, blen);
1922 offset = SvIV(offstr) - arybase + llen;
1925 else if (offset > blen)
1927 if (!(tmps2 = rninstr(tmps, tmps + offset,
1928 tmps2, tmps2 + llen)))
1929 retval = -1 + arybase;
1931 retval = tmps2 - tmps + arybase;
1938 dSP; dMARK; dORIGMARK; dTARGET;
1939 #ifdef USE_LOCALE_NUMERIC
1940 if (op->op_private & OPpLOCALE)
1941 SET_NUMERIC_LOCAL();
1943 SET_NUMERIC_STANDARD();
1945 do_sprintf(TARG, SP-MARK, MARK+1);
1946 TAINT_IF(SvTAINTED(TARG));
1960 value = (I32) (*tmps & 255);
1965 value = (I32) (anum & 255);
1976 (void)SvUPGRADE(TARG,SVt_PV);
1982 (void)SvPOK_only(TARG);
1989 dSP; dTARGET; dPOPTOPssrl;
1991 char *tmps = SvPV(left, na);
1993 sv_setpv(TARG, fcrypt(tmps, SvPV(right, na)));
1995 sv_setpv(TARG, crypt(tmps, SvPV(right, na)));
1999 "The crypt() function is unimplemented due to excessive paranoia.");
2011 if (!SvPADTMP(sv)) {
2017 s = SvPV_force(sv, na);
2019 if (op->op_private & OPpLOCALE) {
2022 *s = toUPPER_LC(*s);
2037 if (!SvPADTMP(sv)) {
2043 s = SvPV_force(sv, na);
2045 if (op->op_private & OPpLOCALE) {
2048 *s = toLOWER_LC(*s);
2065 if (!SvPADTMP(sv)) {
2072 s = SvPV_force(sv, len);
2074 register char *send = s + len;
2076 if (op->op_private & OPpLOCALE) {
2079 for (; s < send; s++)
2080 *s = toUPPER_LC(*s);
2083 for (; s < send; s++)
2097 if (!SvPADTMP(sv)) {
2104 s = SvPV_force(sv, len);
2106 register char *send = s + len;
2108 if (op->op_private & OPpLOCALE) {
2111 for (; s < send; s++)
2112 *s = toLOWER_LC(*s);
2115 for (; s < send; s++)
2127 register char *s = SvPV(sv,len);
2131 (void)SvUPGRADE(TARG, SVt_PV);
2132 SvGROW(TARG, (len * 2) + 1);
2140 SvCUR_set(TARG, d - SvPVX(TARG));
2141 (void)SvPOK_only(TARG);
2144 sv_setpvn(TARG, s, len);
2153 dSP; dMARK; dORIGMARK;
2155 register AV* av = (AV*)POPs;
2156 register I32 lval = op->op_flags & OPf_MOD;
2157 I32 arybase = curcop->cop_arybase;
2160 if (SvTYPE(av) == SVt_PVAV) {
2161 if (lval && op->op_private & OPpLVAL_INTRO) {
2163 for (svp = mark + 1; svp <= sp; svp++) {
2168 if (max > AvMAX(av))
2171 while (++MARK <= SP) {
2172 elem = SvIVx(*MARK);
2176 svp = av_fetch(av, elem, lval);
2178 if (!svp || *svp == &sv_undef)
2179 DIE(no_aelem, elem);
2180 if (op->op_private & OPpLVAL_INTRO)
2183 *MARK = svp ? *svp : &sv_undef;
2186 if (GIMME != G_ARRAY) {
2194 /* Associative arrays. */
2199 HV *hash = (HV*)POPs;
2201 I32 gimme = GIMME_V;
2202 I32 realhv = (SvTYPE(hash) == SVt_PVHV);
2205 /* might clobber stack_sp */
2206 entry = realhv ? hv_iternext(hash) : avhv_iternext((AV*)hash);
2211 PUSHs(hv_iterkeysv(entry)); /* won't clobber stack_sp */
2212 if (gimme == G_ARRAY) {
2214 /* might clobber stack_sp */
2215 sv_setsv(TARG, realhv ?
2216 hv_iterval(hash, entry) : avhv_iterval((AV*)hash, entry));
2221 else if (gimme == G_SCALAR)
2240 I32 gimme = GIMME_V;
2241 I32 discard = (gimme == G_VOID) ? G_DISCARD : 0;
2245 if (op->op_private & OPpSLICE) {
2249 hvtype = SvTYPE(hv);
2250 while (++MARK <= SP) {
2251 if (hvtype == SVt_PVHV)
2252 sv = hv_delete_ent(hv, *MARK, discard, 0);
2253 else if (hvtype == SVt_PVAV)
2254 sv = avhv_delete_ent((AV*)hv, *MARK, discard, 0);
2256 DIE("Not a HASH reference");
2257 *MARK = sv ? sv : &sv_undef;
2261 else if (gimme == G_SCALAR) {
2270 if (SvTYPE(hv) == SVt_PVHV)
2271 sv = hv_delete_ent(hv, keysv, discard, 0);
2272 else if (SvTYPE(hv) == SVt_PVAV)
2273 sv = avhv_delete_ent((AV*)hv, keysv, discard, 0);
2275 DIE("Not a HASH reference");
2289 if (SvTYPE(hv) == SVt_PVHV) {
2290 if (hv_exists_ent(hv, tmpsv, 0))
2292 } else if (SvTYPE(hv) == SVt_PVAV) {
2293 if (avhv_exists_ent((AV*)hv, tmpsv, 0))
2296 DIE("Not a HASH reference");
2303 dSP; dMARK; dORIGMARK;
2305 register HV *hv = (HV*)POPs;
2306 register I32 lval = op->op_flags & OPf_MOD;
2307 I32 realhv = (SvTYPE(hv) == SVt_PVHV);
2309 if (realhv || SvTYPE(hv) == SVt_PVAV) {
2310 while (++MARK <= SP) {
2314 he = hv_fetch_ent(hv, keysv, lval, 0);
2315 svp = he ? &HeVAL(he) : 0;
2317 svp = avhv_fetch_ent((AV*)hv, keysv, lval, 0);
2320 if (!he || HeVAL(he) == &sv_undef)
2321 DIE(no_helem, SvPV(keysv, na));
2322 if (op->op_private & OPpLVAL_INTRO)
2323 save_svref(&HeVAL(he));
2325 *MARK = he ? HeVAL(he) : &sv_undef;
2328 if (GIMME != G_ARRAY) {
2336 /* List operators. */
2341 if (GIMME != G_ARRAY) {
2343 *MARK = *SP; /* unwanted list, return last item */
2354 SV **lastrelem = stack_sp;
2355 SV **lastlelem = stack_base + POPMARK;
2356 SV **firstlelem = stack_base + POPMARK + 1;
2357 register SV **firstrelem = lastlelem + 1;
2358 I32 arybase = curcop->cop_arybase;
2359 I32 lval = op->op_flags & OPf_MOD;
2360 I32 is_something_there = lval;
2362 register I32 max = lastrelem - lastlelem;
2363 register SV **lelem;
2366 if (GIMME != G_ARRAY) {
2367 ix = SvIVx(*lastlelem);
2372 if (ix < 0 || ix >= max)
2373 *firstlelem = &sv_undef;
2375 *firstlelem = firstrelem[ix];
2381 SP = firstlelem - 1;
2385 for (lelem = firstlelem; lelem <= lastlelem; lelem++) {
2391 else if (!(*lelem = firstrelem[ix]))
2396 if (ix >= max || !(*lelem = firstrelem[ix]))
2399 if (!is_something_there && (SvOK(*lelem) || SvGMAGICAL(*lelem)))
2400 is_something_there = TRUE;
2402 if (is_something_there)
2405 SP = firstlelem - 1;
2411 dSP; dMARK; dORIGMARK;
2412 I32 items = SP - MARK;
2413 SV *av = sv_2mortal((SV*)av_make(items, MARK+1));
2414 SP = ORIGMARK; /* av_make() might realloc stack_sp */
2421 dSP; dMARK; dORIGMARK;
2422 HV* hv = (HV*)sv_2mortal((SV*)newHV());
2426 SV *val = NEWSV(46, 0);
2428 sv_setsv(val, *++MARK);
2430 warn("Odd number of elements in hash list");
2431 (void)hv_store_ent(hv,key,val,0);
2440 dSP; dMARK; dORIGMARK;
2441 register AV *ary = (AV*)*++MARK;
2445 register I32 offset;
2446 register I32 length;
2455 offset = i = SvIVx(*MARK);
2457 offset += AvFILL(ary) + 1;
2459 offset -= curcop->cop_arybase;
2463 length = SvIVx(*MARK++);
2468 length = AvMAX(ary) + 1; /* close enough to infinity */
2472 length = AvMAX(ary) + 1;
2474 if (offset > AvFILL(ary) + 1)
2475 offset = AvFILL(ary) + 1;
2476 after = AvFILL(ary) + 1 - (offset + length);
2477 if (after < 0) { /* not that much array */
2478 length += after; /* offset+length now in array */
2484 /* At this point, MARK .. SP-1 is our new LIST */
2487 diff = newlen - length;
2488 if (newlen && !AvREAL(ary)) {
2492 assert(AvREAL(ary)); /* would leak, so croak */
2495 if (diff < 0) { /* shrinking the area */
2497 New(451, tmparyval, newlen, SV*); /* so remember insertion */
2498 Copy(MARK, tmparyval, newlen, SV*);
2501 MARK = ORIGMARK + 1;
2502 if (GIMME == G_ARRAY) { /* copy return vals to stack */
2503 MEXTEND(MARK, length);
2504 Copy(AvARRAY(ary)+offset, MARK, length, SV*);
2506 EXTEND_MORTAL(length);
2507 for (i = length, dst = MARK; i; i--) {
2508 if (!SvIMMORTAL(*dst))
2509 sv_2mortal(*dst); /* free them eventualy */
2516 *MARK = AvARRAY(ary)[offset+length-1];
2518 if (!SvIMMORTAL(*MARK))
2520 for (i = length - 1, dst = &AvARRAY(ary)[offset]; i > 0; i--)
2521 SvREFCNT_dec(*dst++); /* free them now */
2524 AvFILL(ary) += diff;
2526 /* pull up or down? */
2528 if (offset < after) { /* easier to pull up */
2529 if (offset) { /* esp. if nothing to pull */
2530 src = &AvARRAY(ary)[offset-1];
2531 dst = src - diff; /* diff is negative */
2532 for (i = offset; i > 0; i--) /* can't trust Copy */
2536 SvPVX(ary) = (char*)(AvARRAY(ary) - diff); /* diff is negative */
2540 if (after) { /* anything to pull down? */
2541 src = AvARRAY(ary) + offset + length;
2542 dst = src + diff; /* diff is negative */
2543 Move(src, dst, after, SV*);
2545 dst = &AvARRAY(ary)[AvFILL(ary)+1];
2546 /* avoid later double free */
2550 dst[--i] = &sv_undef;
2553 for (src = tmparyval, dst = AvARRAY(ary) + offset;
2555 *dst = NEWSV(46, 0);
2556 sv_setsv(*dst++, *src++);
2558 Safefree(tmparyval);
2561 else { /* no, expanding (or same) */
2563 New(452, tmparyval, length, SV*); /* so remember deletion */
2564 Copy(AvARRAY(ary)+offset, tmparyval, length, SV*);
2567 if (diff > 0) { /* expanding */
2569 /* push up or down? */
2571 if (offset < after && diff <= AvARRAY(ary) - AvALLOC(ary)) {
2575 Move(src, dst, offset, SV*);
2577 SvPVX(ary) = (char*)(AvARRAY(ary) - diff);/* diff is positive */
2579 AvFILL(ary) += diff;
2582 if (AvFILL(ary) + diff >= AvMAX(ary)) /* oh, well */
2583 av_extend(ary, AvFILL(ary) + diff);
2584 AvFILL(ary) += diff;
2587 dst = AvARRAY(ary) + AvFILL(ary);
2589 for (i = after; i; i--) {
2596 for (src = MARK, dst = AvARRAY(ary) + offset; newlen; newlen--) {
2597 *dst = NEWSV(46, 0);
2598 sv_setsv(*dst++, *src++);
2600 MARK = ORIGMARK + 1;
2601 if (GIMME == G_ARRAY) { /* copy return vals to stack */
2603 Copy(tmparyval, MARK, length, SV*);
2605 EXTEND_MORTAL(length);
2606 for (i = length, dst = MARK; i; i--) {
2607 if (!SvIMMORTAL(*dst))
2608 sv_2mortal(*dst); /* free them eventualy */
2612 Safefree(tmparyval);
2616 else if (length--) {
2617 *MARK = tmparyval[length];
2619 if (!SvIMMORTAL(*MARK))
2621 while (length-- > 0)
2622 SvREFCNT_dec(tmparyval[length]);
2624 Safefree(tmparyval);
2635 dSP; dMARK; dORIGMARK; dTARGET;
2636 register AV *ary = (AV*)*++MARK;
2637 register SV *sv = &sv_undef;
2639 for (++MARK; MARK <= SP; MARK++) {
2642 sv_setsv(sv, *MARK);
2646 PUSHi( AvFILL(ary) + 1 );
2654 SV *sv = av_pop(av);
2655 if (!SvIMMORTAL(sv) && AvREAL(av))
2656 (void)sv_2mortal(sv);
2665 SV *sv = av_shift(av);
2669 if (!SvIMMORTAL(sv) && AvREAL(av))
2670 (void)sv_2mortal(sv);
2677 dSP; dMARK; dORIGMARK; dTARGET;
2678 register AV *ary = (AV*)*++MARK;
2682 av_unshift(ary, SP - MARK);
2685 sv_setsv(sv, *++MARK);
2686 (void)av_store(ary, i++, sv);
2690 PUSHi( AvFILL(ary) + 1 );
2700 if (GIMME == G_ARRAY) {
2711 register char *down;
2717 do_join(TARG, &sv_no, MARK, SP);
2719 sv_setsv(TARG, (SP > MARK) ? *SP : GvSV(defgv));
2720 up = SvPV_force(TARG, len);
2722 down = SvPVX(TARG) + len - 1;
2728 (void)SvPOK_only(TARG);
2737 mul128(SV *sv, U8 m)
2740 char *s = SvPV(sv, len);
2744 if (!strnEQ(s, "0000", 4)) { /* need to grow sv */
2745 SV *New = newSVpv("0000000000", 10);
2748 SvREFCNT_dec(sv); /* free old sv */
2753 while (!*t) /* trailing '\0'? */
2756 i = ((*t - '0') << 7) + m;
2757 *(t--) = '0' + (i % 10);
2763 /* Explosives and implosives. */
2770 I32 gimme = GIMME_V;
2774 register char *pat = SvPV(left, llen);
2775 register char *s = SvPV(right, rlen);
2776 char *strend = s + rlen;
2778 register char *patend = pat + llen;
2783 /* These must not be in registers: */
2794 unsigned Quad_t auquad;
2800 register U32 culong;
2802 static char* bitcount = 0;
2805 if (gimme != G_ARRAY) { /* arrange to do first one only */
2807 for (patend = pat; !isALPHA(*patend) || *patend == 'x'; patend++) ;
2808 if (strchr("aAbBhHP", *patend) || *pat == '%') {
2810 while (isDIGIT(*patend) || *patend == '*')
2816 while (pat < patend) {
2818 datumtype = *pat++ & 0xFF;
2819 if (isSPACE(datumtype))
2823 else if (*pat == '*') {
2824 len = strend - strbeg; /* long enough */
2827 else if (isDIGIT(*pat)) {
2829 while (isDIGIT(*pat))
2830 len = (len * 10) + (*pat++ - '0');
2833 len = (datumtype != '@');
2836 croak("Invalid type in unpack: '%c'", (int)datumtype);
2837 case ',': /* grandfather in commas but with a warning */
2838 if (commas++ == 0 && dowarn)
2839 warn("Invalid type in unpack: '%c'", (int)datumtype);
2842 if (len == 1 && pat[-1] != '1')
2851 if (len > strend - strbeg)
2852 DIE("@ outside of string");
2856 if (len > s - strbeg)
2857 DIE("X outside of string");
2861 if (len > strend - s)
2862 DIE("x outside of string");
2867 if (len > strend - s)
2870 goto uchar_checksum;
2871 sv = NEWSV(35, len);
2872 sv_setpvn(sv, s, len);
2874 if (datumtype == 'A') {
2875 aptr = s; /* borrow register */
2876 s = SvPVX(sv) + len - 1;
2877 while (s >= SvPVX(sv) && (!*s || isSPACE(*s)))
2880 SvCUR_set(sv, s - SvPVX(sv));
2881 s = aptr; /* unborrow register */
2883 XPUSHs(sv_2mortal(sv));
2887 if (pat[-1] == '*' || len > (strend - s) * 8)
2888 len = (strend - s) * 8;
2891 Newz(601, bitcount, 256, char);
2892 for (bits = 1; bits < 256; bits++) {
2893 if (bits & 1) bitcount[bits]++;
2894 if (bits & 2) bitcount[bits]++;
2895 if (bits & 4) bitcount[bits]++;
2896 if (bits & 8) bitcount[bits]++;
2897 if (bits & 16) bitcount[bits]++;
2898 if (bits & 32) bitcount[bits]++;
2899 if (bits & 64) bitcount[bits]++;
2900 if (bits & 128) bitcount[bits]++;
2904 culong += bitcount[*(unsigned char*)s++];
2909 if (datumtype == 'b') {
2911 if (bits & 1) culong++;
2917 if (bits & 128) culong++;
2924 sv = NEWSV(35, len + 1);
2927 aptr = pat; /* borrow register */
2929 if (datumtype == 'b') {
2931 for (len = 0; len < aint; len++) {
2932 if (len & 7) /*SUPPRESS 595*/
2936 *pat++ = '0' + (bits & 1);
2941 for (len = 0; len < aint; len++) {
2946 *pat++ = '0' + ((bits & 128) != 0);
2950 pat = aptr; /* unborrow register */
2951 XPUSHs(sv_2mortal(sv));
2955 if (pat[-1] == '*' || len > (strend - s) * 2)
2956 len = (strend - s) * 2;
2957 sv = NEWSV(35, len + 1);
2960 aptr = pat; /* borrow register */
2962 if (datumtype == 'h') {
2964 for (len = 0; len < aint; len++) {
2969 *pat++ = hexdigit[bits & 15];
2974 for (len = 0; len < aint; len++) {
2979 *pat++ = hexdigit[(bits >> 4) & 15];
2983 pat = aptr; /* unborrow register */
2984 XPUSHs(sv_2mortal(sv));
2987 if (len > strend - s)
2992 if (aint >= 128) /* fake up signed chars */
3002 if (aint >= 128) /* fake up signed chars */
3005 sv_setiv(sv, (IV)aint);
3006 PUSHs(sv_2mortal(sv));
3011 if (len > strend - s)
3026 sv_setiv(sv, (IV)auint);
3027 PUSHs(sv_2mortal(sv));
3032 along = (strend - s) / SIZE16;
3049 sv_setiv(sv, (IV)ashort);
3050 PUSHs(sv_2mortal(sv));
3057 along = (strend - s) / SIZE16;
3062 COPY16(s, &aushort);
3065 if (datumtype == 'n')
3066 aushort = ntohs(aushort);
3069 if (datumtype == 'v')
3070 aushort = vtohs(aushort);
3079 COPY16(s, &aushort);
3083 if (datumtype == 'n')
3084 aushort = ntohs(aushort);
3087 if (datumtype == 'v')
3088 aushort = vtohs(aushort);
3090 sv_setiv(sv, (IV)aushort);
3091 PUSHs(sv_2mortal(sv));
3096 along = (strend - s) / sizeof(int);
3101 Copy(s, &aint, 1, int);
3104 cdouble += (double)aint;
3113 Copy(s, &aint, 1, int);
3116 sv_setiv(sv, (IV)aint);
3117 PUSHs(sv_2mortal(sv));
3122 along = (strend - s) / sizeof(unsigned int);
3127 Copy(s, &auint, 1, unsigned int);
3128 s += sizeof(unsigned int);
3130 cdouble += (double)auint;
3139 Copy(s, &auint, 1, unsigned int);
3140 s += sizeof(unsigned int);
3142 sv_setuv(sv, (UV)auint);
3143 PUSHs(sv_2mortal(sv));
3148 along = (strend - s) / SIZE32;
3156 cdouble += (double)along;
3168 sv_setiv(sv, (IV)along);
3169 PUSHs(sv_2mortal(sv));
3176 along = (strend - s) / SIZE32;
3184 if (datumtype == 'N')
3185 aulong = ntohl(aulong);
3188 if (datumtype == 'V')
3189 aulong = vtohl(aulong);
3192 cdouble += (double)aulong;
3204 if (datumtype == 'N')
3205 aulong = ntohl(aulong);
3208 if (datumtype == 'V')
3209 aulong = vtohl(aulong);
3212 sv_setuv(sv, (UV)aulong);
3213 PUSHs(sv_2mortal(sv));
3218 along = (strend - s) / sizeof(char*);
3224 if (sizeof(char*) > strend - s)
3227 Copy(s, &aptr, 1, char*);
3233 PUSHs(sv_2mortal(sv));
3243 while ((len > 0) && (s < strend)) {
3244 auv = (auv << 7) | (*s & 0x7f);
3245 if (!(*s++ & 0x80)) {
3249 PUSHs(sv_2mortal(sv));
3253 else if (++bytes >= sizeof(UV)) { /* promote to string */
3256 sv = newSVpvf("%.*Vu", (int)TYPE_DIGITS(UV), auv);
3257 while (s < strend) {
3258 sv = mul128(sv, *s & 0x7f);
3259 if (!(*s++ & 0x80)) {
3268 PUSHs(sv_2mortal(sv));
3273 if ((s >= strend) && bytes)
3274 croak("Unterminated compressed integer");
3279 if (sizeof(char*) > strend - s)
3282 Copy(s, &aptr, 1, char*);
3287 sv_setpvn(sv, aptr, len);
3288 PUSHs(sv_2mortal(sv));
3295 if (s + sizeof(Quad_t) > strend)
3298 Copy(s, &aquad, 1, Quad_t);
3299 s += sizeof(Quad_t);
3302 if (aquad >= IV_MIN && aquad <= IV_MAX)
3303 sv_setiv(sv, (IV)aquad);
3305 sv_setnv(sv, (double)aquad);
3306 PUSHs(sv_2mortal(sv));
3313 if (s + sizeof(unsigned Quad_t) > strend)
3316 Copy(s, &auquad, 1, unsigned Quad_t);
3317 s += sizeof(unsigned Quad_t);
3320 if (aquad <= UV_MAX)
3321 sv_setuv(sv, (UV)auquad);
3323 sv_setnv(sv, (double)auquad);
3324 PUSHs(sv_2mortal(sv));
3328 /* float and double added gnb@melba.bby.oz.au 22/11/89 */
3331 along = (strend - s) / sizeof(float);
3336 Copy(s, &afloat, 1, float);
3345 Copy(s, &afloat, 1, float);
3348 sv_setnv(sv, (double)afloat);
3349 PUSHs(sv_2mortal(sv));
3355 along = (strend - s) / sizeof(double);
3360 Copy(s, &adouble, 1, double);
3361 s += sizeof(double);
3369 Copy(s, &adouble, 1, double);
3370 s += sizeof(double);
3372 sv_setnv(sv, (double)adouble);
3373 PUSHs(sv_2mortal(sv));
3378 along = (strend - s) * 3 / 4;
3379 sv = NEWSV(42, along);
3382 while (s < strend && *s > ' ' && *s < 'a') {
3387 len = (*s++ - ' ') & 077;
3389 if (s < strend && *s >= ' ')
3390 a = (*s++ - ' ') & 077;
3393 if (s < strend && *s >= ' ')
3394 b = (*s++ - ' ') & 077;
3397 if (s < strend && *s >= ' ')
3398 c = (*s++ - ' ') & 077;
3401 if (s < strend && *s >= ' ')
3402 d = (*s++ - ' ') & 077;
3405 hunk[0] = a << 2 | b >> 4;
3406 hunk[1] = b << 4 | c >> 2;
3407 hunk[2] = c << 6 | d;
3408 sv_catpvn(sv, hunk, len > 3 ? 3 : len);
3413 else if (s[1] == '\n') /* possible checksum byte */
3416 XPUSHs(sv_2mortal(sv));
3421 if (strchr("fFdD", datumtype) ||
3422 (checksum > 32 && strchr("iIlLN", datumtype)) ) {
3426 while (checksum >= 16) {
3430 while (checksum >= 4) {
3436 along = (1 << checksum) - 1;
3437 while (cdouble < 0.0)
3439 cdouble = modf(cdouble / adouble, &trouble) * adouble;
3440 sv_setnv(sv, cdouble);
3443 if (checksum < 32) {
3444 aulong = (1 << checksum) - 1;
3447 sv_setuv(sv, (UV)culong);
3449 XPUSHs(sv_2mortal(sv));
3453 if (sp == oldsp && gimme == G_SCALAR)
3459 doencodes(register SV *sv, register char *s, register I32 len)
3464 sv_catpvn(sv, hunk, 1);
3467 hunk[0] = ' ' + (077 & (*s >> 2));
3468 hunk[1] = ' ' + (077 & ((*s << 4) & 060 | (s[1] >> 4) & 017));
3469 hunk[2] = ' ' + (077 & ((s[1] << 2) & 074 | (s[2] >> 6) & 03));
3470 hunk[3] = ' ' + (077 & (s[2] & 077));
3471 sv_catpvn(sv, hunk, 4);
3475 for (s = SvPVX(sv); *s; s++) {
3479 sv_catpvn(sv, "\n", 1);
3483 is_an_int(char *s, STRLEN l)
3485 SV *result = newSVpv("", l);
3486 char *result_c = SvPV(result, na); /* convenience */
3487 char *out = result_c;
3497 SvREFCNT_dec(result);
3520 SvREFCNT_dec(result);
3526 SvCUR_set(result, out - result_c);
3531 div128(SV *pnum, char *done)
3532 /* must be '\0' terminated */
3536 char *s = SvPV(pnum, len);
3545 i = m * 10 + (*t - '0');
3547 r = (i >> 7); /* r < 10 */
3554 SvCUR_set(pnum, (STRLEN) (t - s));
3561 dSP; dMARK; dORIGMARK; dTARGET;
3562 register SV *cat = TARG;
3565 register char *pat = SvPVx(*++MARK, fromlen);
3566 register char *patend = pat + fromlen;
3571 static char null10[] = {0,0,0,0,0,0,0,0,0,0};
3572 static char *space10 = " ";
3574 /* These must not be in registers: */
3583 unsigned Quad_t auquad;
3592 sv_setpvn(cat, "", 0);
3593 while (pat < patend) {
3594 #define NEXTFROM (items-- > 0 ? *MARK++ : &sv_no)
3595 datumtype = *pat++ & 0xFF;
3596 if (isSPACE(datumtype))
3599 len = strchr("@Xxu", datumtype) ? 0 : items;
3602 else if (isDIGIT(*pat)) {
3604 while (isDIGIT(*pat))
3605 len = (len * 10) + (*pat++ - '0');
3611 croak("Invalid type in pack: '%c'", (int)datumtype);
3612 case ',': /* grandfather in commas but with a warning */
3613 if (commas++ == 0 && dowarn)
3614 warn("Invalid type in pack: '%c'", (int)datumtype);
3617 DIE("%% may only be used in unpack");
3628 if (SvCUR(cat) < len)
3629 DIE("X outside of string");
3636 sv_catpvn(cat, null10, 10);
3639 sv_catpvn(cat, null10, len);
3644 aptr = SvPV(fromstr, fromlen);
3648 sv_catpvn(cat, aptr, len);
3650 sv_catpvn(cat, aptr, fromlen);
3652 if (datumtype == 'A') {
3654 sv_catpvn(cat, space10, 10);
3657 sv_catpvn(cat, space10, len);
3661 sv_catpvn(cat, null10, 10);
3664 sv_catpvn(cat, null10, len);
3671 char *savepat = pat;
3676 aptr = SvPV(fromstr, fromlen);
3681 SvCUR(cat) += (len+7)/8;
3682 SvGROW(cat, SvCUR(cat) + 1);
3683 aptr = SvPVX(cat) + aint;
3688 if (datumtype == 'B') {
3689 for (len = 0; len++ < aint;) {
3690 items |= *pat++ & 1;
3694 *aptr++ = items & 0xff;
3700 for (len = 0; len++ < aint;) {
3706 *aptr++ = items & 0xff;
3712 if (datumtype == 'B')
3713 items <<= 7 - (aint & 7);
3715 items >>= 7 - (aint & 7);
3716 *aptr++ = items & 0xff;
3718 pat = SvPVX(cat) + SvCUR(cat);
3729 char *savepat = pat;
3734 aptr = SvPV(fromstr, fromlen);
3739 SvCUR(cat) += (len+1)/2;
3740 SvGROW(cat, SvCUR(cat) + 1);
3741 aptr = SvPVX(cat) + aint;
3746 if (datumtype == 'H') {
3747 for (len = 0; len++ < aint;) {
3749 items |= ((*pat++ & 15) + 9) & 15;
3751 items |= *pat++ & 15;
3755 *aptr++ = items & 0xff;
3761 for (len = 0; len++ < aint;) {
3763 items |= (((*pat++ & 15) + 9) & 15) << 4;
3765 items |= (*pat++ & 15) << 4;
3769 *aptr++ = items & 0xff;
3775 *aptr++ = items & 0xff;
3776 pat = SvPVX(cat) + SvCUR(cat);
3788 aint = SvIV(fromstr);
3790 sv_catpvn(cat, &achar, sizeof(char));
3793 /* Float and double added by gnb@melba.bby.oz.au 22/11/89 */
3798 afloat = (float)SvNV(fromstr);
3799 sv_catpvn(cat, (char *)&afloat, sizeof (float));
3806 adouble = (double)SvNV(fromstr);
3807 sv_catpvn(cat, (char *)&adouble, sizeof (double));
3813 ashort = (I16)SvIV(fromstr);
3815 ashort = htons(ashort);
3817 CAT16(cat, &ashort);
3823 ashort = (I16)SvIV(fromstr);
3825 ashort = htovs(ashort);
3827 CAT16(cat, &ashort);
3834 ashort = (I16)SvIV(fromstr);
3835 CAT16(cat, &ashort);
3841 auint = SvUV(fromstr);
3842 sv_catpvn(cat, (char*)&auint, sizeof(unsigned int));
3848 adouble = floor(SvNV(fromstr));
3851 croak("Cannot compress negative numbers");
3857 #ifdef CXUX_BROKEN_CONSTANT_CONVERT
3858 adouble <= UV_MAX_cxux
3865 char buf[1 + sizeof(UV)];
3866 char *in = buf + sizeof(buf);
3867 UV auv = U_V(adouble);;
3870 *--in = (auv & 0x7f) | 0x80;
3873 buf[sizeof(buf) - 1] &= 0x7f; /* clear continue bit */
3874 sv_catpvn(cat, in, (buf + sizeof(buf)) - in);
3876 else if (SvPOKp(fromstr)) { /* decimal string arithmetics */
3877 char *from, *result, *in;
3882 /* Copy string and check for compliance */
3883 from = SvPV(fromstr, len);
3884 if ((norm = is_an_int(from, len)) == NULL)
3885 croak("can compress only unsigned integer");
3887 New('w', result, len, char);
3891 *--in = div128(norm, &done) | 0x80;
3892 result[len - 1] &= 0x7F; /* clear continue bit */
3893 sv_catpvn(cat, in, (result + len) - in);
3895 SvREFCNT_dec(norm); /* free norm */
3897 else if (SvNOKp(fromstr)) {
3898 char buf[sizeof(double) * 2]; /* 8/7 <= 2 */
3899 char *in = buf + sizeof(buf);
3902 double next = floor(adouble / 128);
3903 *--in = (unsigned char)(adouble - (next * 128)) | 0x80;
3904 if (--in < buf) /* this cannot happen ;-) */
3905 croak ("Cannot compress integer");
3907 } while (adouble > 0);
3908 buf[sizeof(buf) - 1] &= 0x7f; /* clear continue bit */
3909 sv_catpvn(cat, in, (buf + sizeof(buf)) - in);
3912 croak("Cannot compress non integer");
3918 aint = SvIV(fromstr);
3919 sv_catpvn(cat, (char*)&aint, sizeof(int));
3925 aulong = SvUV(fromstr);
3927 aulong = htonl(aulong);
3929 CAT32(cat, &aulong);
3935 aulong = SvUV(fromstr);
3937 aulong = htovl(aulong);
3939 CAT32(cat, &aulong);
3945 aulong = SvUV(fromstr);
3946 CAT32(cat, &aulong);
3952 along = SvIV(fromstr);
3960 auquad = (unsigned Quad_t)SvIV(fromstr);
3961 sv_catpvn(cat, (char*)&auquad, sizeof(unsigned Quad_t));
3967 aquad = (Quad_t)SvIV(fromstr);
3968 sv_catpvn(cat, (char*)&aquad, sizeof(Quad_t));
3971 #endif /* HAS_QUAD */
3973 len = 1; /* assume SV is correct length */
3978 if (fromstr == &sv_undef)
3981 /* XXX better yet, could spirit away the string to
3982 * a safe spot and hang on to it until the result
3983 * of pack() (and all copies of the result) are
3986 if (dowarn && (SvTEMP(fromstr) || SvPADTMP(fromstr)))
3987 warn("Attempt to pack pointer to temporary value");
3988 if (SvPOK(fromstr) || SvNIOK(fromstr))
3989 aptr = SvPV(fromstr,na);
3991 aptr = SvPV_force(fromstr,na);
3993 sv_catpvn(cat, (char*)&aptr, sizeof(char*));
3998 aptr = SvPV(fromstr, fromlen);
3999 SvGROW(cat, fromlen * 4 / 3);
4004 while (fromlen > 0) {
4011 doencodes(cat, aptr, todo);
4029 register I32 limit = POPi; /* note, negative is forever */
4032 register char *s = SvPV(sv, len);
4033 char *strend = s + len;
4035 register REGEXP *rx;
4039 I32 maxiters = (strend - s) + 10;
4042 I32 origlimit = limit;
4045 AV *oldstack = curstack;
4046 I32 gimme = GIMME_V;
4047 I32 oldsave = savestack_ix;
4050 Copy(&LvTARGOFF(POPs), &pm, 1, PMOP*);
4055 DIE("panic: do_split");
4056 rx = pm->op_pmregexp;
4058 TAINT_IF((pm->op_pmflags & PMf_LOCALE) &&
4059 (pm->op_pmflags & (PMf_WHITE | PMf_SKIPWHITE)));
4061 if (pm->op_pmreplroot)
4062 ary = GvAVn((GV*)pm->op_pmreplroot);
4063 else if (gimme != G_ARRAY)
4065 ary = (AV*)curpad[0];
4068 #endif /* USE_THREADS */
4071 if (ary && (gimme != G_ARRAY || (pm->op_pmflags & PMf_ONCE))) {
4075 for (i = AvFILL(ary); i >= 0; i--)
4076 AvARRAY(ary)[i] = &sv_undef; /* don't free mere refs */
4080 /* temporarily switch stacks */
4081 SWITCHSTACK(curstack, ary);
4083 base = SP - stack_base;
4085 if (pm->op_pmflags & PMf_SKIPWHITE) {
4086 if (pm->op_pmflags & PMf_LOCALE) {
4087 while (isSPACE_LC(*s))
4095 if (pm->op_pmflags & (PMf_MULTILINE|PMf_SINGLELINE)) {
4097 multiline = pm->op_pmflags & PMf_MULTILINE;
4101 limit = maxiters + 2;
4102 if (pm->op_pmflags & PMf_WHITE) {
4105 while (m < strend &&
4106 !((pm->op_pmflags & PMf_LOCALE)
4107 ? isSPACE_LC(*m) : isSPACE(*m)))
4112 dstr = NEWSV(30, m-s);
4113 sv_setpvn(dstr, s, m-s);
4119 while (s < strend &&
4120 ((pm->op_pmflags & PMf_LOCALE)
4121 ? isSPACE_LC(*s) : isSPACE(*s)))
4125 else if (strEQ("^", rx->precomp)) {
4128 for (m = s; m < strend && *m != '\n'; m++) ;
4132 dstr = NEWSV(30, m-s);
4133 sv_setpvn(dstr, s, m-s);
4140 else if (pm->op_pmshort && !rx->nparens) {
4141 i = SvCUR(pm->op_pmshort);
4143 i = *SvPVX(pm->op_pmshort);
4146 for (m = s; m < strend && *m != i; m++) ;
4149 dstr = NEWSV(30, m-s);
4150 sv_setpvn(dstr, s, m-s);
4159 while (s < strend && --limit &&
4160 (m=fbm_instr((unsigned char*)s, (unsigned char*)strend,
4164 dstr = NEWSV(31, m-s);
4165 sv_setpvn(dstr, s, m-s);
4174 maxiters += (strend - s) * rx->nparens;
4175 while (s < strend && --limit &&
4176 pregexec(rx, s, strend, orig, 1, Nullsv, TRUE))
4178 TAINT_IF(rx->exec_tainted);
4180 && rx->subbase != orig) {
4185 strend = s + (strend - m);
4188 dstr = NEWSV(32, m-s);
4189 sv_setpvn(dstr, s, m-s);
4194 for (i = 1; i <= rx->nparens; i++) {
4198 dstr = NEWSV(33, m-s);
4199 sv_setpvn(dstr, s, m-s);
4202 dstr = NEWSV(33, 0);
4211 LEAVE_SCOPE(oldsave);
4212 iters = (SP - stack_base) - base;
4213 if (iters > maxiters)
4216 /* keep field after final delim? */
4217 if (s < strend || (iters && origlimit)) {
4218 dstr = NEWSV(34, strend-s);
4219 sv_setpvn(dstr, s, strend-s);
4225 else if (!origlimit) {
4226 while (iters > 0 && (!TOPs || !SvANY(TOPs) || SvCUR(TOPs) == 0))
4230 SWITCHSTACK(ary, oldstack);
4231 if (SvSMAGICAL(ary)) {
4236 if (gimme == G_ARRAY) {
4238 Copy(AvARRAY(ary), SP + 1, iters, SV*);
4244 if (gimme == G_ARRAY)
4247 if (iters || !pm->op_pmreplroot) {
4257 unlock_condpair(void *svv)
4260 MAGIC *mg = mg_find((SV*)svv, 'm');
4263 croak("panic: unlock_condpair unlocking non-mutex");
4264 MUTEX_LOCK(MgMUTEXP(mg));
4265 if (MgOWNER(mg) != thr)
4266 croak("panic: unlock_condpair unlocking mutex that we don't own");
4268 COND_SIGNAL(MgOWNERCONDP(mg));
4269 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "0x%lx: unlock 0x%lx\n",
4270 (unsigned long)thr, (unsigned long)svv);)
4271 MUTEX_UNLOCK(MgMUTEXP(mg));
4273 #endif /* USE_THREADS */
4286 mg = condpair_magic(sv);
4287 MUTEX_LOCK(MgMUTEXP(mg));
4288 if (MgOWNER(mg) == thr)
4289 MUTEX_UNLOCK(MgMUTEXP(mg));
4292 COND_WAIT(MgOWNERCONDP(mg), MgMUTEXP(mg));
4294 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "0x%lx: pp_lock lock 0x%lx\n",
4295 (unsigned long)thr, (unsigned long)sv);)
4296 MUTEX_UNLOCK(MgMUTEXP(mg));
4297 SvREFCNT_inc(sv); /* keep alive until magic_mutexfree */
4298 save_destructor(unlock_condpair, sv);
4300 #endif /* USE_THREADS */
4301 if (SvTYPE(retsv) == SVt_PVAV || SvTYPE(retsv) == SVt_PVHV
4302 || SvTYPE(retsv) == SVt_PVCV) {
4303 retsv = refto(retsv);