3 * Copyright (c) 1991-1997, Larry Wall
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
11 * "It's a big house this, and very peculiar. Always a bit more to discover,
12 * and no knowing what you'll find around a corner. And Elves, sir!" --Samwise
19 * The compiler on Concurrent CX/UX systems has a subtle bug which only
20 * seems to show up when compiling pp.c - it generates the wrong double
21 * precision constant value for (double)UV_MAX when used inline in the body
22 * of the code below, so this makes a static variable up front (which the
23 * compiler seems to get correct) and uses it in place of UV_MAX below.
25 #ifdef CXUX_BROKEN_CONSTANT_CONVERT
26 static double UV_MAX_cxux = ((double)UV_MAX);
30 * Types used in bitwise operations.
32 * Normally we'd just use IV and UV. However, some hardware and
33 * software combinations (e.g. Alpha and current OSF/1) don't have a
34 * floating-point type to use for NV that has adequate bits to fully
35 * hold an IV/UV. (In other words, sizeof(long) == sizeof(double).)
37 * It just so happens that "int" is the right size almost everywhere.
43 * Mask used after bitwise operations.
45 * There is at least one realm (Cray word machines) that doesn't
46 * have an integral type (except char) small enough to be represented
47 * in a double without loss; that is, it has no 32-bit type.
49 #if BYTEORDER > 0xFFFF && defined(_CRAY) && !defined(_CRAYMPP)
51 # define BW_MASK ((1 << BW_BITS) - 1)
52 # define BW_SIGN (1 << (BW_BITS - 1))
53 # define BWi(i) (((i) & BW_SIGN) ? ((i) | ~BW_MASK) : ((i) & BW_MASK))
54 # define BWu(u) ((u) & BW_MASK)
61 * Offset for integer pack/unpack.
63 * On architectures where I16 and I32 aren't really 16 and 32 bits,
64 * which for now are all Crays, pack and unpack have to play games.
68 * These values are required for portability of pack() output.
69 * If they're not right on your machine, then pack() and unpack()
70 * wouldn't work right anyway; you'll need to apply the Cray hack.
71 * (I'd like to check them with #if, but you can't use sizeof() in
77 #if BYTEORDER > 0xFFFF && defined(_CRAY) && !defined(_CRAYMPP)
78 # if BYTEORDER == 0x12345678
79 # define OFF16(p) (char*)(p)
80 # define OFF32(p) (char*)(p)
82 # if BYTEORDER == 0x87654321
83 # define OFF16(p) ((char*)(p) + (sizeof(U16) - SIZE16))
84 # define OFF32(p) ((char*)(p) + (sizeof(U32) - SIZE32))
86 }}}} bad cray byte order
89 # define COPY16(s,p) (*(p) = 0, Copy(s, OFF16(p), SIZE16, char))
90 # define COPY32(s,p) (*(p) = 0, Copy(s, OFF32(p), SIZE32, char))
91 # define CAT16(sv,p) sv_catpvn(sv, OFF16(p), SIZE16)
92 # define CAT32(sv,p) sv_catpvn(sv, OFF32(p), SIZE32)
94 # define COPY16(s,p) Copy(s, p, SIZE16, char)
95 # define COPY32(s,p) Copy(s, p, SIZE32, char)
96 # define CAT16(sv,p) sv_catpvn(sv, (char*)(p), SIZE16)
97 # define CAT32(sv,p) sv_catpvn(sv, (char*)(p), SIZE32)
100 static void doencodes _((SV* sv, char* s, I32 len));
101 static SV* refto _((SV* sv));
102 static U32 seed _((void));
104 static bool srand_called = FALSE;
106 /* variations on pp_null */
108 #ifdef DONT_DECLARE_STD
113 extern pid_t getpid (void);
119 if (GIMME_V == G_SCALAR)
134 if (op->op_private & OPpLVAL_INTRO)
135 SAVECLEARSV(curpad[op->op_targ]);
137 if (op->op_flags & OPf_REF) {
141 if (GIMME == G_ARRAY) {
142 I32 maxarg = AvFILL((AV*)TARG) + 1;
144 Copy(AvARRAY((AV*)TARG), SP+1, maxarg, SV*);
148 SV* sv = sv_newmortal();
149 I32 maxarg = AvFILL((AV*)TARG) + 1;
150 sv_setiv(sv, maxarg);
162 if (op->op_private & OPpLVAL_INTRO)
163 SAVECLEARSV(curpad[op->op_targ]);
164 if (op->op_flags & OPf_REF)
167 if (gimme == G_ARRAY) {
168 RETURNOP(do_kv(ARGS));
170 else if (gimme == G_SCALAR) {
171 SV* sv = sv_newmortal();
172 if (HvFILL((HV*)TARG))
173 sv_setpvf(sv, "%ld/%ld",
174 (long)HvFILL((HV*)TARG), (long)HvMAX((HV*)TARG) + 1);
184 DIE("NOT IMPL LINE %d",__LINE__);
196 if (SvTYPE(sv) == SVt_PVIO) {
197 GV *gv = (GV*) sv_newmortal();
198 gv_init(gv, 0, "", 0, 0);
199 GvIOp(gv) = (IO *)sv;
200 (void)SvREFCNT_inc(sv);
202 } else if (SvTYPE(sv) != SVt_PVGV)
203 DIE("Not a GLOB reference");
206 if (SvTYPE(sv) != SVt_PVGV) {
209 if (SvGMAGICAL(sv)) {
215 if (op->op_flags & OPf_REF ||
216 op->op_private & HINT_STRICT_REFS)
217 DIE(no_usym, "a symbol");
223 if (op->op_private & HINT_STRICT_REFS)
224 DIE(no_symref, sym, "a symbol");
225 sv = (SV*)gv_fetchpv(sym, TRUE, SVt_PVGV);
228 if (op->op_private & OPpLVAL_INTRO)
229 save_gp((GV*)sv, !(op->op_flags & OPf_SPECIAL));
241 switch (SvTYPE(sv)) {
245 DIE("Not a SCALAR reference");
252 if (SvTYPE(gv) != SVt_PVGV) {
253 if (SvGMAGICAL(sv)) {
259 if (op->op_flags & OPf_REF ||
260 op->op_private & HINT_STRICT_REFS)
261 DIE(no_usym, "a SCALAR");
267 if (op->op_private & HINT_STRICT_REFS)
268 DIE(no_symref, sym, "a SCALAR");
269 gv = (GV*)gv_fetchpv(sym, TRUE, SVt_PV);
273 if (op->op_flags & OPf_MOD) {
274 if (op->op_private & OPpLVAL_INTRO)
275 sv = save_scalar((GV*)TOPs);
276 else if (op->op_private & OPpDEREF)
277 vivify_ref(sv, op->op_private & OPpDEREF);
287 SV *sv = AvARYLEN(av);
289 AvARYLEN(av) = sv = NEWSV(0,0);
290 sv_upgrade(sv, SVt_IV);
291 sv_magic(sv, (SV*)av, '#', Nullch, 0);
299 djSP; dTARGET; dPOPss;
301 if (op->op_flags & OPf_MOD) {
302 if (SvTYPE(TARG) < SVt_PVLV) {
303 sv_upgrade(TARG, SVt_PVLV);
304 sv_magic(TARG, Nullsv, '.', Nullch, 0);
309 PUSHs(TARG); /* no SvSETMAGIC */
315 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
316 mg = mg_find(sv, 'g');
317 if (mg && mg->mg_len >= 0) {
318 PUSHi(mg->mg_len + curcop->cop_arybase);
332 /* We usually try to add a non-existent subroutine in case of AUTOLOAD. */
333 /* (But not in defined().) */
334 CV *cv = sv_2cv(TOPs, &stash, &gv, !(op->op_flags & OPf_SPECIAL));
337 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
354 cv = sv_2cv(TOPs, &stash, &gv, FALSE);
356 ret = sv_2mortal(newSVpv(SvPVX(cv), SvCUR(cv)));
364 CV* cv = (CV*)curpad[op->op_targ];
366 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
382 if (GIMME != G_ARRAY) {
386 EXTEND_MORTAL(SP - MARK);
388 *MARK = refto(*MARK);
397 if (SvTYPE(sv) == SVt_PVLV && LvTYPE(sv) == 'y') {
400 if (!(sv = LvTARG(sv)))
403 else if (SvPADTMP(sv))
407 (void)SvREFCNT_inc(sv);
410 sv_upgrade(rv, SVt_RV);
424 if (sv && SvGMAGICAL(sv))
427 if (!sv || !SvROK(sv))
431 pv = sv_reftype(sv,TRUE);
432 PUSHp(pv, strlen(pv));
442 stash = curcop->cop_stash;
444 stash = gv_stashsv(POPs, TRUE);
446 (void)sv_bless(TOPs, stash);
463 switch (elem ? *elem : '\0')
466 if (strEQ(elem, "ARRAY"))
470 if (strEQ(elem, "CODE"))
471 ref = (SV*)GvCVu(gv);
474 if (strEQ(elem, "FILEHANDLE")) /* XXX deprecate in 5.005 */
475 ref = (SV*)GvIOp(gv);
478 if (strEQ(elem, "GLOB"))
482 if (strEQ(elem, "HASH"))
486 if (strEQ(elem, "IO"))
487 ref = (SV*)GvIOp(gv);
490 if (strEQ(elem, "NAME"))
491 sv = newSVpv(GvNAME(gv), GvNAMELEN(gv));
494 if (strEQ(elem, "PACKAGE"))
495 sv = newSVpv(HvNAME(GvSTASH(gv)), 0);
498 if (strEQ(elem, "SCALAR"))
512 /* Pattern matching */
517 register unsigned char *s;
520 register I32 *sfirst;
524 if (sv == lastscream) {
530 SvSCREAM_off(lastscream);
531 SvREFCNT_dec(lastscream);
533 lastscream = SvREFCNT_inc(sv);
536 s = (unsigned char*)(SvPV(sv, len));
540 if (pos > maxscream) {
542 maxscream = pos + 80;
543 New(301, screamfirst, 256, I32);
544 New(302, screamnext, maxscream, I32);
547 maxscream = pos + pos / 4;
548 Renew(screamnext, maxscream, I32);
552 sfirst = screamfirst;
555 if (!sfirst || !snext)
556 DIE("do_study: out of memory");
558 for (ch = 256; ch; --ch)
565 snext[pos] = sfirst[ch] - pos;
572 sv_magic(sv, Nullsv, 'g', Nullch, 0); /* piggyback on m//g magic */
581 if (op->op_flags & OPf_STACKED)
587 TARG = sv_newmortal();
588 PUSHi(do_trans(sv, op));
592 /* Lvalue operators. */
604 djSP; dMARK; dTARGET;
614 SETi(do_chomp(TOPs));
620 djSP; dMARK; dTARGET;
621 register I32 count = 0;
624 count += do_chomp(POPs);
635 if (!sv || !SvANY(sv))
637 switch (SvTYPE(sv)) {
639 if (AvMAX(sv) >= 0 || SvGMAGICAL(sv))
643 if (HvARRAY(sv) || SvGMAGICAL(sv))
647 if (CvROOT(sv) || CvXSUB(sv))
664 if (!op->op_private) {
673 if (SvTHINKFIRST(sv)) {
680 switch (SvTYPE(sv)) {
690 if (cv_const_sv((CV*)sv))
691 warn("Constant subroutine %s undefined",
692 CvANON((CV*)sv) ? "(anonymous)" : GvENAME(CvGV((CV*)sv)));
695 { GV* gv = (GV*)SvREFCNT_inc(CvGV((CV*)sv));
697 CvGV((CV*)sv) = gv; } /* let user-undef'd sub keep its identity */
701 sv_setsv(sv, &sv_undef);
704 if (SvTYPE(sv) >= SVt_PV && SvPVX(sv) && SvLEN(sv)) {
707 SvPV_set(sv, Nullch);
720 if (SvREADONLY(TOPs) || SvTYPE(TOPs) > SVt_PVLV)
722 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) &&
723 SvIVX(TOPs) != IV_MIN)
726 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
737 if (SvREADONLY(TOPs) || SvTYPE(TOPs) > SVt_PVLV)
739 sv_setsv(TARG, TOPs);
740 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) &&
741 SvIVX(TOPs) != IV_MAX)
744 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
758 if(SvREADONLY(TOPs) || SvTYPE(TOPs) > SVt_PVLV)
760 sv_setsv(TARG, TOPs);
761 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) &&
762 SvIVX(TOPs) != IV_MIN)
765 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
774 /* Ordinary operators. */
778 djSP; dATARGET; tryAMAGICbin(pow,opASSIGN);
781 SETn( pow( left, right) );
788 djSP; dATARGET; tryAMAGICbin(mult,opASSIGN);
791 SETn( left * right );
798 djSP; dATARGET; tryAMAGICbin(div,opASSIGN);
803 DIE("Illegal division by zero");
805 /* insure that 20./5. == 4. */
808 if ((double)I_V(left) == left &&
809 (double)I_V(right) == right &&
810 (k = I_V(left)/I_V(right))*I_V(right) == I_V(left)) {
813 value = left / right;
817 value = left / right;
826 djSP; dATARGET; tryAMAGICbin(mod,opASSIGN);
834 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)) {
836 right = (right_neg = (i < 0)) ? -i : i;
840 right = U_V((right_neg = (n < 0)) ? -n : n);
843 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)) {
845 left = (left_neg = (i < 0)) ? -i : i;
849 left = U_V((left_neg = (n < 0)) ? -n : n);
853 DIE("Illegal modulus zero");
856 if ((left_neg != right_neg) && ans)
859 /* XXX may warn: unary minus operator applied to unsigned type */
860 /* could change -foo to be (~foo)+1 instead */
861 if (ans <= ~((UV)IV_MAX)+1)
862 sv_setiv(TARG, ~ans+1);
864 sv_setnv(TARG, -(double)ans);
875 djSP; dATARGET; tryAMAGICbin(repeat,opASSIGN);
877 register I32 count = POPi;
878 if (GIMME == G_ARRAY && op->op_private & OPpREPEAT_DOLIST) {
880 I32 items = SP - MARK;
892 repeatcpy((char*)(MARK + items), (char*)MARK,
893 items * sizeof(SV*), count - 1);
899 else { /* Note: mark already snarfed by pp_list */
904 if (TARG == tmpstr && SvTHINKFIRST(tmpstr)) {
905 if (SvREADONLY(tmpstr) && curcop != &compiling)
906 DIE("Can't x= to readonly value");
910 SvSetSV(TARG, tmpstr);
911 SvPV_force(TARG, len);
916 SvGROW(TARG, (count * len) + 1);
917 repeatcpy(SvPVX(TARG) + len, SvPVX(TARG), len, count - 1);
918 SvCUR(TARG) *= count;
922 (void)SvPOK_only(TARG);
931 djSP; dATARGET; tryAMAGICbin(subtr,opASSIGN);
934 SETn( left - right );
941 djSP; dATARGET; tryAMAGICbin(lshift,opASSIGN);
944 if (op->op_private & HINT_INTEGER) {
960 djSP; dATARGET; tryAMAGICbin(rshift,opASSIGN);
963 if (op->op_private & HINT_INTEGER) {
979 djSP; tryAMAGICbinSET(lt,0);
982 SETs(boolSV(TOPn < value));
989 djSP; tryAMAGICbinSET(gt,0);
992 SETs(boolSV(TOPn > value));
999 djSP; tryAMAGICbinSET(le,0);
1002 SETs(boolSV(TOPn <= value));
1009 djSP; tryAMAGICbinSET(ge,0);
1012 SETs(boolSV(TOPn >= value));
1019 djSP; tryAMAGICbinSET(ne,0);
1022 SETs(boolSV(TOPn != value));
1029 djSP; dTARGET; tryAMAGICbin(ncmp,0);
1036 else if (left < right)
1038 else if (left > right)
1051 djSP; tryAMAGICbinSET(slt,0);
1054 int cmp = ((op->op_private & OPpLOCALE)
1055 ? sv_cmp_locale(left, right)
1056 : sv_cmp(left, right));
1057 SETs(boolSV(cmp < 0));
1064 djSP; tryAMAGICbinSET(sgt,0);
1067 int cmp = ((op->op_private & OPpLOCALE)
1068 ? sv_cmp_locale(left, right)
1069 : sv_cmp(left, right));
1070 SETs(boolSV(cmp > 0));
1077 djSP; tryAMAGICbinSET(sle,0);
1080 int cmp = ((op->op_private & OPpLOCALE)
1081 ? sv_cmp_locale(left, right)
1082 : sv_cmp(left, right));
1083 SETs(boolSV(cmp <= 0));
1090 djSP; tryAMAGICbinSET(sge,0);
1093 int cmp = ((op->op_private & OPpLOCALE)
1094 ? sv_cmp_locale(left, right)
1095 : sv_cmp(left, right));
1096 SETs(boolSV(cmp >= 0));
1103 djSP; tryAMAGICbinSET(seq,0);
1106 SETs(boolSV(sv_eq(left, right)));
1113 djSP; tryAMAGICbinSET(sne,0);
1116 SETs(boolSV(!sv_eq(left, right)));
1123 djSP; dTARGET; tryAMAGICbin(scmp,0);
1126 int cmp = ((op->op_private & OPpLOCALE)
1127 ? sv_cmp_locale(left, right)
1128 : sv_cmp(left, right));
1136 djSP; dATARGET; tryAMAGICbin(band,opASSIGN);
1139 if (SvNIOKp(left) || SvNIOKp(right)) {
1140 if (op->op_private & HINT_INTEGER) {
1141 IBW value = SvIV(left) & SvIV(right);
1145 UBW value = SvUV(left) & SvUV(right);
1150 do_vop(op->op_type, TARG, left, right);
1159 djSP; dATARGET; tryAMAGICbin(bxor,opASSIGN);
1162 if (SvNIOKp(left) || SvNIOKp(right)) {
1163 if (op->op_private & HINT_INTEGER) {
1164 IBW value = (USE_LEFT(left) ? SvIV(left) : 0) ^ SvIV(right);
1168 UBW value = (USE_LEFT(left) ? SvUV(left) : 0) ^ SvUV(right);
1173 do_vop(op->op_type, TARG, left, right);
1182 djSP; dATARGET; tryAMAGICbin(bor,opASSIGN);
1185 if (SvNIOKp(left) || SvNIOKp(right)) {
1186 if (op->op_private & HINT_INTEGER) {
1187 IBW value = (USE_LEFT(left) ? SvIV(left) : 0) | SvIV(right);
1191 UBW value = (USE_LEFT(left) ? SvUV(left) : 0) | SvUV(right);
1196 do_vop(op->op_type, TARG, left, right);
1205 djSP; dTARGET; tryAMAGICun(neg);
1210 if (SvIOKp(sv) && !SvNOKp(sv) && !SvPOKp(sv) && SvIVX(sv) != IV_MIN)
1212 else if (SvNIOKp(sv))
1214 else if (SvPOKp(sv)) {
1216 char *s = SvPV(sv, len);
1217 if (isIDFIRST(*s)) {
1218 sv_setpvn(TARG, "-", 1);
1221 else if (*s == '+' || *s == '-') {
1223 *SvPV_force(TARG, len) = *s == '-' ? '+' : '-';
1226 sv_setnv(TARG, -SvNV(sv));
1238 djSP; tryAMAGICunSET(not);
1239 #endif /* OVERLOAD */
1240 *stack_sp = boolSV(!SvTRUE(*stack_sp));
1246 djSP; dTARGET; tryAMAGICun(compl);
1250 if (op->op_private & HINT_INTEGER) {
1251 IBW value = ~SvIV(sv);
1255 UBW value = ~SvUV(sv);
1260 register char *tmps;
1261 register long *tmpl;
1266 tmps = SvPV_force(TARG, len);
1269 for ( ; anum && (unsigned long)tmps % sizeof(long); anum--, tmps++)
1272 for ( ; anum >= sizeof(long); anum -= sizeof(long), tmpl++)
1276 for ( ; anum > 0; anum--, tmps++)
1285 /* integer versions of some of the above */
1289 djSP; dATARGET; tryAMAGICbin(mult,opASSIGN);
1292 SETi( left * right );
1299 djSP; dATARGET; tryAMAGICbin(div,opASSIGN);
1303 DIE("Illegal division by zero");
1304 value = POPi / value;
1312 djSP; dATARGET; tryAMAGICbin(mod,opASSIGN);
1316 DIE("Illegal modulus zero");
1317 SETi( left % right );
1324 djSP; dATARGET; tryAMAGICbin(add,opASSIGN);
1327 SETi( left + right );
1334 djSP; dATARGET; tryAMAGICbin(subtr,opASSIGN);
1337 SETi( left - right );
1344 djSP; tryAMAGICbinSET(lt,0);
1347 SETs(boolSV(left < right));
1354 djSP; tryAMAGICbinSET(gt,0);
1357 SETs(boolSV(left > right));
1364 djSP; tryAMAGICbinSET(le,0);
1367 SETs(boolSV(left <= right));
1374 djSP; tryAMAGICbinSET(ge,0);
1377 SETs(boolSV(left >= right));
1384 djSP; tryAMAGICbinSET(eq,0);
1387 SETs(boolSV(left == right));
1394 djSP; tryAMAGICbinSET(ne,0);
1397 SETs(boolSV(left != right));
1404 djSP; dTARGET; tryAMAGICbin(ncmp,0);
1411 else if (left < right)
1422 djSP; dTARGET; tryAMAGICun(neg);
1427 /* High falutin' math. */
1431 djSP; dTARGET; tryAMAGICbin(atan2,0);
1434 SETn(atan2(left, right));
1441 djSP; dTARGET; tryAMAGICun(sin);
1453 djSP; dTARGET; tryAMAGICun(cos);
1473 if (!srand_called) {
1474 (void)srand((unsigned)seed());
1475 srand_called = TRUE;
1478 value = rand() * value / 2147483648.0;
1481 value = rand() * value / 65536.0;
1484 value = rand() * value / 32768.0;
1486 value = rand() * value / (double)(((unsigned long)1) << RANDBITS);
1502 (void)srand((unsigned)anum);
1503 srand_called = TRUE;
1512 * This is really just a quick hack which grabs various garbage
1513 * values. It really should be a real hash algorithm which
1514 * spreads the effect of every input bit onto every output bit,
1515 * if someone who knows about such tings would bother to write it.
1516 * Might be a good idea to add that function to CORE as well.
1517 * No numbers below come from careful analysis or anyting here,
1518 * except they are primes and SEED_C1 > 1E6 to get a full-width
1519 * value from (tv_sec * SEED_C1 + tv_usec). The multipliers should
1520 * probably be bigger too.
1523 # define SEED_C1 1000003
1524 #define SEED_C4 73819
1526 # define SEED_C1 25747
1527 #define SEED_C4 20639
1531 #define SEED_C5 26107
1536 # include <starlet.h>
1537 /* when[] = (low 32 bits, high 32 bits) of time since epoch
1538 * in 100-ns units, typically incremented ever 10 ms. */
1539 unsigned int when[2];
1540 _ckvmssts(sys$gettim(when));
1541 u = (U32)SEED_C1 * when[0] + (U32)SEED_C2 * when[1];
1543 # ifdef HAS_GETTIMEOFDAY
1544 struct timeval when;
1545 gettimeofday(&when,(struct timezone *) 0);
1546 u = (U32)SEED_C1 * when.tv_sec + (U32)SEED_C2 * when.tv_usec;
1550 u = (U32)SEED_C1 * when;
1553 u += SEED_C3 * (U32)getpid();
1554 u += SEED_C4 * (U32)(UV)stack_sp;
1555 #ifndef PLAN9 /* XXX Plan9 assembler chokes on this; fix needed */
1556 u += SEED_C5 * (U32)(UV)&when;
1563 djSP; dTARGET; tryAMAGICun(exp);
1575 djSP; dTARGET; tryAMAGICun(log);
1580 SET_NUMERIC_STANDARD();
1581 DIE("Can't take log of %g", value);
1591 djSP; dTARGET; tryAMAGICun(sqrt);
1596 SET_NUMERIC_STANDARD();
1597 DIE("Can't take sqrt of %g", value);
1599 value = sqrt(value);
1609 double value = TOPn;
1612 if (SvIOKp(TOPs) && !SvNOKp(TOPs) && !SvPOKp(TOPs)) {
1618 (void)modf(value, &value);
1620 (void)modf(-value, &value);
1635 djSP; dTARGET; tryAMAGICun(abs);
1637 double value = TOPn;
1640 if (SvIOKp(TOPs) && !SvNOKp(TOPs) && !SvPOKp(TOPs) &&
1641 (iv = SvIVX(TOPs)) != IV_MIN) {
1662 XPUSHu(scan_hex(tmps, 99, &argtype));
1674 while (*tmps && isSPACE(*tmps))
1679 value = scan_hex(++tmps, 99, &argtype);
1681 value = scan_oct(tmps, 99, &argtype);
1691 SETi( sv_len(TOPs) );
1704 I32 lvalue = op->op_flags & OPf_MOD;
1706 I32 arybase = curcop->cop_arybase;
1712 tmps = SvPV(sv, curlen);
1713 if (pos >= arybase) {
1731 else if (len >= 0) {
1733 if (rem > (I32)curlen)
1747 if (dowarn || lvalue)
1748 warn("substr outside of string");
1753 sv_setpvn(TARG, tmps, rem);
1754 if (lvalue) { /* it's an lvalue! */
1755 if (!SvGMAGICAL(sv)) {
1759 warn("Attempt to use reference as lvalue in substr");
1761 if (SvOK(sv)) /* is it defined ? */
1762 (void)SvPOK_only(sv);
1764 sv_setpvn(sv,"",0); /* avoid lexical reincarnation */
1767 if (SvTYPE(TARG) < SVt_PVLV) {
1768 sv_upgrade(TARG, SVt_PVLV);
1769 sv_magic(TARG, Nullsv, 'x', Nullch, 0);
1774 LvTARGOFF(TARG) = pos;
1775 LvTARGLEN(TARG) = rem;
1778 PUSHs(TARG); /* avoid SvSETMAGIC here */
1785 register I32 size = POPi;
1786 register I32 offset = POPi;
1787 register SV *src = POPs;
1788 I32 lvalue = op->op_flags & OPf_MOD;
1790 unsigned char *s = (unsigned char*)SvPV(src, srclen);
1791 unsigned long retnum;
1794 offset *= size; /* turn into bit offset */
1795 len = (offset + size + 7) / 8;
1796 if (offset < 0 || size < 1)
1799 if (lvalue) { /* it's an lvalue! */
1800 if (SvTYPE(TARG) < SVt_PVLV) {
1801 sv_upgrade(TARG, SVt_PVLV);
1802 sv_magic(TARG, Nullsv, 'v', Nullch, 0);
1807 LvTARGOFF(TARG) = offset;
1808 LvTARGLEN(TARG) = size;
1816 if (offset >= srclen)
1819 retnum = (unsigned long) s[offset] << 8;
1821 else if (size == 32) {
1822 if (offset >= srclen)
1824 else if (offset + 1 >= srclen)
1825 retnum = (unsigned long) s[offset] << 24;
1826 else if (offset + 2 >= srclen)
1827 retnum = ((unsigned long) s[offset] << 24) +
1828 ((unsigned long) s[offset + 1] << 16);
1830 retnum = ((unsigned long) s[offset] << 24) +
1831 ((unsigned long) s[offset + 1] << 16) +
1832 (s[offset + 2] << 8);
1837 retnum = (s[offset >> 3] >> (offset & 7)) & ((1 << size) - 1);
1842 else if (size == 16)
1843 retnum = ((unsigned long) s[offset] << 8) + s[offset+1];
1844 else if (size == 32)
1845 retnum = ((unsigned long) s[offset] << 24) +
1846 ((unsigned long) s[offset + 1] << 16) +
1847 (s[offset + 2] << 8) + s[offset+3];
1851 sv_setiv(TARG, (IV)retnum);
1866 I32 arybase = curcop->cop_arybase;
1871 offset = POPi - arybase;
1874 tmps = SvPV(big, biglen);
1877 else if (offset > biglen)
1879 if (!(tmps2 = fbm_instr((unsigned char*)tmps + offset,
1880 (unsigned char*)tmps + biglen, little)))
1881 retval = -1 + arybase;
1883 retval = tmps2 - tmps + arybase;
1900 I32 arybase = curcop->cop_arybase;
1906 tmps2 = SvPV(little, llen);
1907 tmps = SvPV(big, blen);
1911 offset = SvIV(offstr) - arybase + llen;
1914 else if (offset > blen)
1916 if (!(tmps2 = rninstr(tmps, tmps + offset,
1917 tmps2, tmps2 + llen)))
1918 retval = -1 + arybase;
1920 retval = tmps2 - tmps + arybase;
1927 djSP; dMARK; dORIGMARK; dTARGET;
1928 #ifdef USE_LOCALE_NUMERIC
1929 if (op->op_private & OPpLOCALE)
1930 SET_NUMERIC_LOCAL();
1932 SET_NUMERIC_STANDARD();
1934 do_sprintf(TARG, SP-MARK, MARK+1);
1935 TAINT_IF(SvTAINTED(TARG));
1949 value = (I32) (*tmps & 255);
1954 value = (I32) (anum & 255);
1965 (void)SvUPGRADE(TARG,SVt_PV);
1971 (void)SvPOK_only(TARG);
1978 djSP; dTARGET; dPOPTOPssrl;
1980 char *tmps = SvPV(left, na);
1982 sv_setpv(TARG, fcrypt(tmps, SvPV(right, na)));
1984 sv_setpv(TARG, crypt(tmps, SvPV(right, na)));
1988 "The crypt() function is unimplemented due to excessive paranoia.");
2000 if (!SvPADTMP(sv)) {
2006 s = SvPV_force(sv, na);
2008 if (op->op_private & OPpLOCALE) {
2011 *s = toUPPER_LC(*s);
2026 if (!SvPADTMP(sv)) {
2032 s = SvPV_force(sv, na);
2034 if (op->op_private & OPpLOCALE) {
2037 *s = toLOWER_LC(*s);
2054 if (!SvPADTMP(sv)) {
2061 s = SvPV_force(sv, len);
2063 register char *send = s + len;
2065 if (op->op_private & OPpLOCALE) {
2068 for (; s < send; s++)
2069 *s = toUPPER_LC(*s);
2072 for (; s < send; s++)
2086 if (!SvPADTMP(sv)) {
2093 s = SvPV_force(sv, len);
2095 register char *send = s + len;
2097 if (op->op_private & OPpLOCALE) {
2100 for (; s < send; s++)
2101 *s = toLOWER_LC(*s);
2104 for (; s < send; s++)
2116 register char *s = SvPV(sv,len);
2120 (void)SvUPGRADE(TARG, SVt_PV);
2121 SvGROW(TARG, (len * 2) + 1);
2129 SvCUR_set(TARG, d - SvPVX(TARG));
2130 (void)SvPOK_only(TARG);
2133 sv_setpvn(TARG, s, len);
2142 djSP; dMARK; dORIGMARK;
2144 register AV* av = (AV*)POPs;
2145 register I32 lval = op->op_flags & OPf_MOD;
2146 I32 arybase = curcop->cop_arybase;
2149 if (SvTYPE(av) == SVt_PVAV) {
2150 if (lval && op->op_private & OPpLVAL_INTRO) {
2152 for (svp = mark + 1; svp <= sp; svp++) {
2157 if (max > AvMAX(av))
2160 while (++MARK <= SP) {
2161 elem = SvIVx(*MARK);
2165 svp = av_fetch(av, elem, lval);
2167 if (!svp || *svp == &sv_undef)
2168 DIE(no_aelem, elem);
2169 if (op->op_private & OPpLVAL_INTRO)
2172 *MARK = svp ? *svp : &sv_undef;
2175 if (GIMME != G_ARRAY) {
2183 /* Associative arrays. */
2188 HV *hash = (HV*)POPs;
2190 I32 gimme = GIMME_V;
2191 I32 realhv = (SvTYPE(hash) == SVt_PVHV);
2194 /* might clobber stack_sp */
2195 entry = realhv ? hv_iternext(hash) : avhv_iternext((AV*)hash);
2200 PUSHs(hv_iterkeysv(entry)); /* won't clobber stack_sp */
2201 if (gimme == G_ARRAY) {
2203 /* might clobber stack_sp */
2204 sv_setsv(TARG, realhv ?
2205 hv_iterval(hash, entry) : avhv_iterval((AV*)hash, entry));
2210 else if (gimme == G_SCALAR)
2229 I32 gimme = GIMME_V;
2230 I32 discard = (gimme == G_VOID) ? G_DISCARD : 0;
2234 if (op->op_private & OPpSLICE) {
2238 hvtype = SvTYPE(hv);
2239 while (++MARK <= SP) {
2240 if (hvtype == SVt_PVHV)
2241 sv = hv_delete_ent(hv, *MARK, discard, 0);
2242 else if (hvtype == SVt_PVAV)
2243 sv = avhv_delete_ent((AV*)hv, *MARK, discard, 0);
2245 DIE("Not a HASH reference");
2246 *MARK = sv ? sv : &sv_undef;
2250 else if (gimme == G_SCALAR) {
2259 if (SvTYPE(hv) == SVt_PVHV)
2260 sv = hv_delete_ent(hv, keysv, discard, 0);
2261 else if (SvTYPE(hv) == SVt_PVAV)
2262 sv = avhv_delete_ent((AV*)hv, keysv, discard, 0);
2264 DIE("Not a HASH reference");
2278 if (SvTYPE(hv) == SVt_PVHV) {
2279 if (hv_exists_ent(hv, tmpsv, 0))
2281 } else if (SvTYPE(hv) == SVt_PVAV) {
2282 if (avhv_exists_ent((AV*)hv, tmpsv, 0))
2285 DIE("Not a HASH reference");
2292 djSP; dMARK; dORIGMARK;
2294 register HV *hv = (HV*)POPs;
2295 register I32 lval = op->op_flags & OPf_MOD;
2296 I32 realhv = (SvTYPE(hv) == SVt_PVHV);
2298 if (realhv || SvTYPE(hv) == SVt_PVAV) {
2299 while (++MARK <= SP) {
2303 he = hv_fetch_ent(hv, keysv, lval, 0);
2304 svp = he ? &HeVAL(he) : 0;
2306 svp = avhv_fetch_ent((AV*)hv, keysv, lval, 0);
2309 if (!he || HeVAL(he) == &sv_undef)
2310 DIE(no_helem, SvPV(keysv, na));
2311 if (op->op_private & OPpLVAL_INTRO)
2312 save_svref(&HeVAL(he));
2314 *MARK = he ? HeVAL(he) : &sv_undef;
2317 if (GIMME != G_ARRAY) {
2325 /* List operators. */
2330 if (GIMME != G_ARRAY) {
2332 *MARK = *SP; /* unwanted list, return last item */
2343 SV **lastrelem = stack_sp;
2344 SV **lastlelem = stack_base + POPMARK;
2345 SV **firstlelem = stack_base + POPMARK + 1;
2346 register SV **firstrelem = lastlelem + 1;
2347 I32 arybase = curcop->cop_arybase;
2348 I32 lval = op->op_flags & OPf_MOD;
2349 I32 is_something_there = lval;
2351 register I32 max = lastrelem - lastlelem;
2352 register SV **lelem;
2355 if (GIMME != G_ARRAY) {
2356 ix = SvIVx(*lastlelem);
2361 if (ix < 0 || ix >= max)
2362 *firstlelem = &sv_undef;
2364 *firstlelem = firstrelem[ix];
2370 SP = firstlelem - 1;
2374 for (lelem = firstlelem; lelem <= lastlelem; lelem++) {
2380 else if (!(*lelem = firstrelem[ix]))
2385 if (ix >= max || !(*lelem = firstrelem[ix]))
2388 if (!is_something_there && (SvOK(*lelem) || SvGMAGICAL(*lelem)))
2389 is_something_there = TRUE;
2391 if (is_something_there)
2394 SP = firstlelem - 1;
2400 djSP; dMARK; dORIGMARK;
2401 I32 items = SP - MARK;
2402 SV *av = sv_2mortal((SV*)av_make(items, MARK+1));
2403 SP = ORIGMARK; /* av_make() might realloc stack_sp */
2410 djSP; dMARK; dORIGMARK;
2411 HV* hv = (HV*)sv_2mortal((SV*)newHV());
2415 SV *val = NEWSV(46, 0);
2417 sv_setsv(val, *++MARK);
2419 warn("Odd number of elements in hash list");
2420 (void)hv_store_ent(hv,key,val,0);
2429 djSP; dMARK; dORIGMARK;
2430 register AV *ary = (AV*)*++MARK;
2434 register I32 offset;
2435 register I32 length;
2444 offset = i = SvIVx(*MARK);
2446 offset += AvFILL(ary) + 1;
2448 offset -= curcop->cop_arybase;
2452 length = SvIVx(*MARK++);
2457 length = AvMAX(ary) + 1; /* close enough to infinity */
2461 length = AvMAX(ary) + 1;
2463 if (offset > AvFILL(ary) + 1)
2464 offset = AvFILL(ary) + 1;
2465 after = AvFILL(ary) + 1 - (offset + length);
2466 if (after < 0) { /* not that much array */
2467 length += after; /* offset+length now in array */
2473 /* At this point, MARK .. SP-1 is our new LIST */
2476 diff = newlen - length;
2477 if (newlen && !AvREAL(ary)) {
2481 assert(AvREAL(ary)); /* would leak, so croak */
2484 if (diff < 0) { /* shrinking the area */
2486 New(451, tmparyval, newlen, SV*); /* so remember insertion */
2487 Copy(MARK, tmparyval, newlen, SV*);
2490 MARK = ORIGMARK + 1;
2491 if (GIMME == G_ARRAY) { /* copy return vals to stack */
2492 MEXTEND(MARK, length);
2493 Copy(AvARRAY(ary)+offset, MARK, length, SV*);
2495 EXTEND_MORTAL(length);
2496 for (i = length, dst = MARK; i; i--) {
2497 if (!SvIMMORTAL(*dst))
2498 sv_2mortal(*dst); /* free them eventualy */
2505 *MARK = AvARRAY(ary)[offset+length-1];
2507 if (!SvIMMORTAL(*MARK))
2509 for (i = length - 1, dst = &AvARRAY(ary)[offset]; i > 0; i--)
2510 SvREFCNT_dec(*dst++); /* free them now */
2513 AvFILL(ary) += diff;
2515 /* pull up or down? */
2517 if (offset < after) { /* easier to pull up */
2518 if (offset) { /* esp. if nothing to pull */
2519 src = &AvARRAY(ary)[offset-1];
2520 dst = src - diff; /* diff is negative */
2521 for (i = offset; i > 0; i--) /* can't trust Copy */
2525 SvPVX(ary) = (char*)(AvARRAY(ary) - diff); /* diff is negative */
2529 if (after) { /* anything to pull down? */
2530 src = AvARRAY(ary) + offset + length;
2531 dst = src + diff; /* diff is negative */
2532 Move(src, dst, after, SV*);
2534 dst = &AvARRAY(ary)[AvFILL(ary)+1];
2535 /* avoid later double free */
2539 dst[--i] = &sv_undef;
2542 for (src = tmparyval, dst = AvARRAY(ary) + offset;
2544 *dst = NEWSV(46, 0);
2545 sv_setsv(*dst++, *src++);
2547 Safefree(tmparyval);
2550 else { /* no, expanding (or same) */
2552 New(452, tmparyval, length, SV*); /* so remember deletion */
2553 Copy(AvARRAY(ary)+offset, tmparyval, length, SV*);
2556 if (diff > 0) { /* expanding */
2558 /* push up or down? */
2560 if (offset < after && diff <= AvARRAY(ary) - AvALLOC(ary)) {
2564 Move(src, dst, offset, SV*);
2566 SvPVX(ary) = (char*)(AvARRAY(ary) - diff);/* diff is positive */
2568 AvFILL(ary) += diff;
2571 if (AvFILL(ary) + diff >= AvMAX(ary)) /* oh, well */
2572 av_extend(ary, AvFILL(ary) + diff);
2573 AvFILL(ary) += diff;
2576 dst = AvARRAY(ary) + AvFILL(ary);
2578 for (i = after; i; i--) {
2585 for (src = MARK, dst = AvARRAY(ary) + offset; newlen; newlen--) {
2586 *dst = NEWSV(46, 0);
2587 sv_setsv(*dst++, *src++);
2589 MARK = ORIGMARK + 1;
2590 if (GIMME == G_ARRAY) { /* copy return vals to stack */
2592 Copy(tmparyval, MARK, length, SV*);
2594 EXTEND_MORTAL(length);
2595 for (i = length, dst = MARK; i; i--) {
2596 if (!SvIMMORTAL(*dst))
2597 sv_2mortal(*dst); /* free them eventualy */
2601 Safefree(tmparyval);
2605 else if (length--) {
2606 *MARK = tmparyval[length];
2608 if (!SvIMMORTAL(*MARK))
2610 while (length-- > 0)
2611 SvREFCNT_dec(tmparyval[length]);
2613 Safefree(tmparyval);
2624 djSP; dMARK; dORIGMARK; dTARGET;
2625 register AV *ary = (AV*)*++MARK;
2626 register SV *sv = &sv_undef;
2628 for (++MARK; MARK <= SP; MARK++) {
2631 sv_setsv(sv, *MARK);
2635 PUSHi( AvFILL(ary) + 1 );
2643 SV *sv = av_pop(av);
2644 if (!SvIMMORTAL(sv) && AvREAL(av))
2645 (void)sv_2mortal(sv);
2654 SV *sv = av_shift(av);
2658 if (!SvIMMORTAL(sv) && AvREAL(av))
2659 (void)sv_2mortal(sv);
2666 djSP; dMARK; dORIGMARK; dTARGET;
2667 register AV *ary = (AV*)*++MARK;
2671 av_unshift(ary, SP - MARK);
2674 sv_setsv(sv, *++MARK);
2675 (void)av_store(ary, i++, sv);
2679 PUSHi( AvFILL(ary) + 1 );
2689 if (GIMME == G_ARRAY) {
2700 register char *down;
2706 do_join(TARG, &sv_no, MARK, SP);
2708 sv_setsv(TARG, (SP > MARK) ? *SP : GvSV(defgv));
2709 up = SvPV_force(TARG, len);
2711 down = SvPVX(TARG) + len - 1;
2717 (void)SvPOK_only(TARG);
2726 mul128(SV *sv, U8 m)
2729 char *s = SvPV(sv, len);
2733 if (!strnEQ(s, "0000", 4)) { /* need to grow sv */
2734 SV *New = newSVpv("0000000000", 10);
2737 SvREFCNT_dec(sv); /* free old sv */
2742 while (!*t) /* trailing '\0'? */
2745 i = ((*t - '0') << 7) + m;
2746 *(t--) = '0' + (i % 10);
2752 /* Explosives and implosives. */
2759 I32 gimme = GIMME_V;
2763 register char *pat = SvPV(left, llen);
2764 register char *s = SvPV(right, rlen);
2765 char *strend = s + rlen;
2767 register char *patend = pat + llen;
2772 /* These must not be in registers: */
2783 unsigned Quad_t auquad;
2789 register U32 culong;
2791 static char* bitcount = 0;
2794 if (gimme != G_ARRAY) { /* arrange to do first one only */
2796 for (patend = pat; !isALPHA(*patend) || *patend == 'x'; patend++) ;
2797 if (strchr("aAbBhHP", *patend) || *pat == '%') {
2799 while (isDIGIT(*patend) || *patend == '*')
2805 while (pat < patend) {
2807 datumtype = *pat++ & 0xFF;
2808 if (isSPACE(datumtype))
2812 else if (*pat == '*') {
2813 len = strend - strbeg; /* long enough */
2816 else if (isDIGIT(*pat)) {
2818 while (isDIGIT(*pat))
2819 len = (len * 10) + (*pat++ - '0');
2822 len = (datumtype != '@');
2825 croak("Invalid type in unpack: '%c'", (int)datumtype);
2826 case ',': /* grandfather in commas but with a warning */
2827 if (commas++ == 0 && dowarn)
2828 warn("Invalid type in unpack: '%c'", (int)datumtype);
2831 if (len == 1 && pat[-1] != '1')
2840 if (len > strend - strbeg)
2841 DIE("@ outside of string");
2845 if (len > s - strbeg)
2846 DIE("X outside of string");
2850 if (len > strend - s)
2851 DIE("x outside of string");
2856 if (len > strend - s)
2859 goto uchar_checksum;
2860 sv = NEWSV(35, len);
2861 sv_setpvn(sv, s, len);
2863 if (datumtype == 'A') {
2864 aptr = s; /* borrow register */
2865 s = SvPVX(sv) + len - 1;
2866 while (s >= SvPVX(sv) && (!*s || isSPACE(*s)))
2869 SvCUR_set(sv, s - SvPVX(sv));
2870 s = aptr; /* unborrow register */
2872 XPUSHs(sv_2mortal(sv));
2876 if (pat[-1] == '*' || len > (strend - s) * 8)
2877 len = (strend - s) * 8;
2880 Newz(601, bitcount, 256, char);
2881 for (bits = 1; bits < 256; bits++) {
2882 if (bits & 1) bitcount[bits]++;
2883 if (bits & 2) bitcount[bits]++;
2884 if (bits & 4) bitcount[bits]++;
2885 if (bits & 8) bitcount[bits]++;
2886 if (bits & 16) bitcount[bits]++;
2887 if (bits & 32) bitcount[bits]++;
2888 if (bits & 64) bitcount[bits]++;
2889 if (bits & 128) bitcount[bits]++;
2893 culong += bitcount[*(unsigned char*)s++];
2898 if (datumtype == 'b') {
2900 if (bits & 1) culong++;
2906 if (bits & 128) culong++;
2913 sv = NEWSV(35, len + 1);
2916 aptr = pat; /* borrow register */
2918 if (datumtype == 'b') {
2920 for (len = 0; len < aint; len++) {
2921 if (len & 7) /*SUPPRESS 595*/
2925 *pat++ = '0' + (bits & 1);
2930 for (len = 0; len < aint; len++) {
2935 *pat++ = '0' + ((bits & 128) != 0);
2939 pat = aptr; /* unborrow register */
2940 XPUSHs(sv_2mortal(sv));
2944 if (pat[-1] == '*' || len > (strend - s) * 2)
2945 len = (strend - s) * 2;
2946 sv = NEWSV(35, len + 1);
2949 aptr = pat; /* borrow register */
2951 if (datumtype == 'h') {
2953 for (len = 0; len < aint; len++) {
2958 *pat++ = hexdigit[bits & 15];
2963 for (len = 0; len < aint; len++) {
2968 *pat++ = hexdigit[(bits >> 4) & 15];
2972 pat = aptr; /* unborrow register */
2973 XPUSHs(sv_2mortal(sv));
2976 if (len > strend - s)
2981 if (aint >= 128) /* fake up signed chars */
2991 if (aint >= 128) /* fake up signed chars */
2994 sv_setiv(sv, (IV)aint);
2995 PUSHs(sv_2mortal(sv));
3000 if (len > strend - s)
3015 sv_setiv(sv, (IV)auint);
3016 PUSHs(sv_2mortal(sv));
3021 along = (strend - s) / SIZE16;
3038 sv_setiv(sv, (IV)ashort);
3039 PUSHs(sv_2mortal(sv));
3046 along = (strend - s) / SIZE16;
3051 COPY16(s, &aushort);
3054 if (datumtype == 'n')
3055 aushort = ntohs(aushort);
3058 if (datumtype == 'v')
3059 aushort = vtohs(aushort);
3068 COPY16(s, &aushort);
3072 if (datumtype == 'n')
3073 aushort = ntohs(aushort);
3076 if (datumtype == 'v')
3077 aushort = vtohs(aushort);
3079 sv_setiv(sv, (IV)aushort);
3080 PUSHs(sv_2mortal(sv));
3085 along = (strend - s) / sizeof(int);
3090 Copy(s, &aint, 1, int);
3093 cdouble += (double)aint;
3102 Copy(s, &aint, 1, int);
3105 sv_setiv(sv, (IV)aint);
3106 PUSHs(sv_2mortal(sv));
3111 along = (strend - s) / sizeof(unsigned int);
3116 Copy(s, &auint, 1, unsigned int);
3117 s += sizeof(unsigned int);
3119 cdouble += (double)auint;
3128 Copy(s, &auint, 1, unsigned int);
3129 s += sizeof(unsigned int);
3131 sv_setuv(sv, (UV)auint);
3132 PUSHs(sv_2mortal(sv));
3137 along = (strend - s) / SIZE32;
3145 cdouble += (double)along;
3157 sv_setiv(sv, (IV)along);
3158 PUSHs(sv_2mortal(sv));
3165 along = (strend - s) / SIZE32;
3173 if (datumtype == 'N')
3174 aulong = ntohl(aulong);
3177 if (datumtype == 'V')
3178 aulong = vtohl(aulong);
3181 cdouble += (double)aulong;
3193 if (datumtype == 'N')
3194 aulong = ntohl(aulong);
3197 if (datumtype == 'V')
3198 aulong = vtohl(aulong);
3201 sv_setuv(sv, (UV)aulong);
3202 PUSHs(sv_2mortal(sv));
3207 along = (strend - s) / sizeof(char*);
3213 if (sizeof(char*) > strend - s)
3216 Copy(s, &aptr, 1, char*);
3222 PUSHs(sv_2mortal(sv));
3232 while ((len > 0) && (s < strend)) {
3233 auv = (auv << 7) | (*s & 0x7f);
3234 if (!(*s++ & 0x80)) {
3238 PUSHs(sv_2mortal(sv));
3242 else if (++bytes >= sizeof(UV)) { /* promote to string */
3245 sv = newSVpvf("%.*Vu", (int)TYPE_DIGITS(UV), auv);
3246 while (s < strend) {
3247 sv = mul128(sv, *s & 0x7f);
3248 if (!(*s++ & 0x80)) {
3257 PUSHs(sv_2mortal(sv));
3262 if ((s >= strend) && bytes)
3263 croak("Unterminated compressed integer");
3268 if (sizeof(char*) > strend - s)
3271 Copy(s, &aptr, 1, char*);
3276 sv_setpvn(sv, aptr, len);
3277 PUSHs(sv_2mortal(sv));
3284 if (s + sizeof(Quad_t) > strend)
3287 Copy(s, &aquad, 1, Quad_t);
3288 s += sizeof(Quad_t);
3291 if (aquad >= IV_MIN && aquad <= IV_MAX)
3292 sv_setiv(sv, (IV)aquad);
3294 sv_setnv(sv, (double)aquad);
3295 PUSHs(sv_2mortal(sv));
3302 if (s + sizeof(unsigned Quad_t) > strend)
3305 Copy(s, &auquad, 1, unsigned Quad_t);
3306 s += sizeof(unsigned Quad_t);
3309 if (aquad <= UV_MAX)
3310 sv_setuv(sv, (UV)auquad);
3312 sv_setnv(sv, (double)auquad);
3313 PUSHs(sv_2mortal(sv));
3317 /* float and double added gnb@melba.bby.oz.au 22/11/89 */
3320 along = (strend - s) / sizeof(float);
3325 Copy(s, &afloat, 1, float);
3334 Copy(s, &afloat, 1, float);
3337 sv_setnv(sv, (double)afloat);
3338 PUSHs(sv_2mortal(sv));
3344 along = (strend - s) / sizeof(double);
3349 Copy(s, &adouble, 1, double);
3350 s += sizeof(double);
3358 Copy(s, &adouble, 1, double);
3359 s += sizeof(double);
3361 sv_setnv(sv, (double)adouble);
3362 PUSHs(sv_2mortal(sv));
3367 along = (strend - s) * 3 / 4;
3368 sv = NEWSV(42, along);
3371 while (s < strend && *s > ' ' && *s < 'a') {
3376 len = (*s++ - ' ') & 077;
3378 if (s < strend && *s >= ' ')
3379 a = (*s++ - ' ') & 077;
3382 if (s < strend && *s >= ' ')
3383 b = (*s++ - ' ') & 077;
3386 if (s < strend && *s >= ' ')
3387 c = (*s++ - ' ') & 077;
3390 if (s < strend && *s >= ' ')
3391 d = (*s++ - ' ') & 077;
3394 hunk[0] = (a << 2) | (b >> 4);
3395 hunk[1] = (b << 4) | (c >> 2);
3396 hunk[2] = (c << 6) | d;
3397 sv_catpvn(sv, hunk, (len > 3) ? 3 : len);
3402 else if (s[1] == '\n') /* possible checksum byte */
3405 XPUSHs(sv_2mortal(sv));
3410 if (strchr("fFdD", datumtype) ||
3411 (checksum > 32 && strchr("iIlLN", datumtype)) ) {
3415 while (checksum >= 16) {
3419 while (checksum >= 4) {
3425 along = (1 << checksum) - 1;
3426 while (cdouble < 0.0)
3428 cdouble = modf(cdouble / adouble, &trouble) * adouble;
3429 sv_setnv(sv, cdouble);
3432 if (checksum < 32) {
3433 aulong = (1 << checksum) - 1;
3436 sv_setuv(sv, (UV)culong);
3438 XPUSHs(sv_2mortal(sv));
3442 if (sp == oldsp && gimme == G_SCALAR)
3448 doencodes(register SV *sv, register char *s, register I32 len)
3453 sv_catpvn(sv, hunk, 1);
3456 hunk[0] = ' ' + (077 & (*s >> 2));
3457 hunk[1] = ' ' + (077 & (((*s << 4) & 060) | ((s[1] >> 4) & 017)));
3458 hunk[2] = ' ' + (077 & (((s[1] << 2) & 074) | ((s[2] >> 6) & 03)));
3459 hunk[3] = ' ' + (077 & (s[2] & 077));
3460 sv_catpvn(sv, hunk, 4);
3464 for (s = SvPVX(sv); *s; s++) {
3468 sv_catpvn(sv, "\n", 1);
3472 is_an_int(char *s, STRLEN l)
3474 SV *result = newSVpv("", l);
3475 char *result_c = SvPV(result, na); /* convenience */
3476 char *out = result_c;
3486 SvREFCNT_dec(result);
3509 SvREFCNT_dec(result);
3515 SvCUR_set(result, out - result_c);
3520 div128(SV *pnum, char *done)
3521 /* must be '\0' terminated */
3525 char *s = SvPV(pnum, len);
3534 i = m * 10 + (*t - '0');
3536 r = (i >> 7); /* r < 10 */
3543 SvCUR_set(pnum, (STRLEN) (t - s));
3550 djSP; dMARK; dORIGMARK; dTARGET;
3551 register SV *cat = TARG;
3554 register char *pat = SvPVx(*++MARK, fromlen);
3555 register char *patend = pat + fromlen;
3560 static char null10[] = {0,0,0,0,0,0,0,0,0,0};
3561 static char *space10 = " ";
3563 /* These must not be in registers: */
3572 unsigned Quad_t auquad;
3581 sv_setpvn(cat, "", 0);
3582 while (pat < patend) {
3583 #define NEXTFROM (items-- > 0 ? *MARK++ : &sv_no)
3584 datumtype = *pat++ & 0xFF;
3585 if (isSPACE(datumtype))
3588 len = strchr("@Xxu", datumtype) ? 0 : items;
3591 else if (isDIGIT(*pat)) {
3593 while (isDIGIT(*pat))
3594 len = (len * 10) + (*pat++ - '0');
3600 croak("Invalid type in pack: '%c'", (int)datumtype);
3601 case ',': /* grandfather in commas but with a warning */
3602 if (commas++ == 0 && dowarn)
3603 warn("Invalid type in pack: '%c'", (int)datumtype);
3606 DIE("%% may only be used in unpack");
3617 if (SvCUR(cat) < len)
3618 DIE("X outside of string");
3625 sv_catpvn(cat, null10, 10);
3628 sv_catpvn(cat, null10, len);
3633 aptr = SvPV(fromstr, fromlen);
3637 sv_catpvn(cat, aptr, len);
3639 sv_catpvn(cat, aptr, fromlen);
3641 if (datumtype == 'A') {
3643 sv_catpvn(cat, space10, 10);
3646 sv_catpvn(cat, space10, len);
3650 sv_catpvn(cat, null10, 10);
3653 sv_catpvn(cat, null10, len);
3660 char *savepat = pat;
3665 aptr = SvPV(fromstr, fromlen);
3670 SvCUR(cat) += (len+7)/8;
3671 SvGROW(cat, SvCUR(cat) + 1);
3672 aptr = SvPVX(cat) + aint;
3677 if (datumtype == 'B') {
3678 for (len = 0; len++ < aint;) {
3679 items |= *pat++ & 1;
3683 *aptr++ = items & 0xff;
3689 for (len = 0; len++ < aint;) {
3695 *aptr++ = items & 0xff;
3701 if (datumtype == 'B')
3702 items <<= 7 - (aint & 7);
3704 items >>= 7 - (aint & 7);
3705 *aptr++ = items & 0xff;
3707 pat = SvPVX(cat) + SvCUR(cat);
3718 char *savepat = pat;
3723 aptr = SvPV(fromstr, fromlen);
3728 SvCUR(cat) += (len+1)/2;
3729 SvGROW(cat, SvCUR(cat) + 1);
3730 aptr = SvPVX(cat) + aint;
3735 if (datumtype == 'H') {
3736 for (len = 0; len++ < aint;) {
3738 items |= ((*pat++ & 15) + 9) & 15;
3740 items |= *pat++ & 15;
3744 *aptr++ = items & 0xff;
3750 for (len = 0; len++ < aint;) {
3752 items |= (((*pat++ & 15) + 9) & 15) << 4;
3754 items |= (*pat++ & 15) << 4;
3758 *aptr++ = items & 0xff;
3764 *aptr++ = items & 0xff;
3765 pat = SvPVX(cat) + SvCUR(cat);
3777 aint = SvIV(fromstr);
3779 sv_catpvn(cat, &achar, sizeof(char));
3782 /* Float and double added by gnb@melba.bby.oz.au 22/11/89 */
3787 afloat = (float)SvNV(fromstr);
3788 sv_catpvn(cat, (char *)&afloat, sizeof (float));
3795 adouble = (double)SvNV(fromstr);
3796 sv_catpvn(cat, (char *)&adouble, sizeof (double));
3802 ashort = (I16)SvIV(fromstr);
3804 ashort = htons(ashort);
3806 CAT16(cat, &ashort);
3812 ashort = (I16)SvIV(fromstr);
3814 ashort = htovs(ashort);
3816 CAT16(cat, &ashort);
3823 ashort = (I16)SvIV(fromstr);
3824 CAT16(cat, &ashort);
3830 auint = SvUV(fromstr);
3831 sv_catpvn(cat, (char*)&auint, sizeof(unsigned int));
3837 adouble = floor(SvNV(fromstr));
3840 croak("Cannot compress negative numbers");
3846 #ifdef CXUX_BROKEN_CONSTANT_CONVERT
3847 adouble <= UV_MAX_cxux
3854 char buf[1 + sizeof(UV)];
3855 char *in = buf + sizeof(buf);
3856 UV auv = U_V(adouble);;
3859 *--in = (auv & 0x7f) | 0x80;
3862 buf[sizeof(buf) - 1] &= 0x7f; /* clear continue bit */
3863 sv_catpvn(cat, in, (buf + sizeof(buf)) - in);
3865 else if (SvPOKp(fromstr)) { /* decimal string arithmetics */
3866 char *from, *result, *in;
3871 /* Copy string and check for compliance */
3872 from = SvPV(fromstr, len);
3873 if ((norm = is_an_int(from, len)) == NULL)
3874 croak("can compress only unsigned integer");
3876 New('w', result, len, char);
3880 *--in = div128(norm, &done) | 0x80;
3881 result[len - 1] &= 0x7F; /* clear continue bit */
3882 sv_catpvn(cat, in, (result + len) - in);
3884 SvREFCNT_dec(norm); /* free norm */
3886 else if (SvNOKp(fromstr)) {
3887 char buf[sizeof(double) * 2]; /* 8/7 <= 2 */
3888 char *in = buf + sizeof(buf);
3891 double next = floor(adouble / 128);
3892 *--in = (unsigned char)(adouble - (next * 128)) | 0x80;
3893 if (--in < buf) /* this cannot happen ;-) */
3894 croak ("Cannot compress integer");
3896 } while (adouble > 0);
3897 buf[sizeof(buf) - 1] &= 0x7f; /* clear continue bit */
3898 sv_catpvn(cat, in, (buf + sizeof(buf)) - in);
3901 croak("Cannot compress non integer");
3907 aint = SvIV(fromstr);
3908 sv_catpvn(cat, (char*)&aint, sizeof(int));
3914 aulong = SvUV(fromstr);
3916 aulong = htonl(aulong);
3918 CAT32(cat, &aulong);
3924 aulong = SvUV(fromstr);
3926 aulong = htovl(aulong);
3928 CAT32(cat, &aulong);
3934 aulong = SvUV(fromstr);
3935 CAT32(cat, &aulong);
3941 along = SvIV(fromstr);
3949 auquad = (unsigned Quad_t)SvIV(fromstr);
3950 sv_catpvn(cat, (char*)&auquad, sizeof(unsigned Quad_t));
3956 aquad = (Quad_t)SvIV(fromstr);
3957 sv_catpvn(cat, (char*)&aquad, sizeof(Quad_t));
3960 #endif /* HAS_QUAD */
3962 len = 1; /* assume SV is correct length */
3967 if (fromstr == &sv_undef)
3970 /* XXX better yet, could spirit away the string to
3971 * a safe spot and hang on to it until the result
3972 * of pack() (and all copies of the result) are
3975 if (dowarn && (SvTEMP(fromstr) || SvPADTMP(fromstr)))
3976 warn("Attempt to pack pointer to temporary value");
3977 if (SvPOK(fromstr) || SvNIOK(fromstr))
3978 aptr = SvPV(fromstr,na);
3980 aptr = SvPV_force(fromstr,na);
3982 sv_catpvn(cat, (char*)&aptr, sizeof(char*));
3987 aptr = SvPV(fromstr, fromlen);
3988 SvGROW(cat, fromlen * 4 / 3);
3993 while (fromlen > 0) {
4000 doencodes(cat, aptr, todo);
4018 register I32 limit = POPi; /* note, negative is forever */
4021 register char *s = SvPV(sv, len);
4022 char *strend = s + len;
4024 register REGEXP *rx;
4028 I32 maxiters = (strend - s) + 10;
4031 I32 origlimit = limit;
4034 AV *oldstack = curstack;
4035 I32 gimme = GIMME_V;
4036 I32 oldsave = savestack_ix;
4039 Copy(&LvTARGOFF(POPs), &pm, 1, PMOP*);
4044 DIE("panic: do_split");
4045 rx = pm->op_pmregexp;
4047 TAINT_IF((pm->op_pmflags & PMf_LOCALE) &&
4048 (pm->op_pmflags & (PMf_WHITE | PMf_SKIPWHITE)));
4050 if (pm->op_pmreplroot)
4051 ary = GvAVn((GV*)pm->op_pmreplroot);
4052 else if (gimme != G_ARRAY)
4054 ary = (AV*)curpad[0];
4057 #endif /* USE_THREADS */
4060 if (ary && (gimme != G_ARRAY || (pm->op_pmflags & PMf_ONCE))) {
4064 for (i = AvFILL(ary); i >= 0; i--)
4065 AvARRAY(ary)[i] = &sv_undef; /* don't free mere refs */
4069 /* temporarily switch stacks */
4070 SWITCHSTACK(curstack, ary);
4072 base = SP - stack_base;
4074 if (pm->op_pmflags & PMf_SKIPWHITE) {
4075 if (pm->op_pmflags & PMf_LOCALE) {
4076 while (isSPACE_LC(*s))
4084 if (pm->op_pmflags & (PMf_MULTILINE|PMf_SINGLELINE)) {
4086 multiline = pm->op_pmflags & PMf_MULTILINE;
4090 limit = maxiters + 2;
4091 if (pm->op_pmflags & PMf_WHITE) {
4094 while (m < strend &&
4095 !((pm->op_pmflags & PMf_LOCALE)
4096 ? isSPACE_LC(*m) : isSPACE(*m)))
4101 dstr = NEWSV(30, m-s);
4102 sv_setpvn(dstr, s, m-s);
4108 while (s < strend &&
4109 ((pm->op_pmflags & PMf_LOCALE)
4110 ? isSPACE_LC(*s) : isSPACE(*s)))
4114 else if (strEQ("^", rx->precomp)) {
4117 for (m = s; m < strend && *m != '\n'; m++) ;
4121 dstr = NEWSV(30, m-s);
4122 sv_setpvn(dstr, s, m-s);
4129 else if (pm->op_pmshort && !rx->nparens) {
4130 i = SvCUR(pm->op_pmshort);
4132 i = *SvPVX(pm->op_pmshort);
4135 for (m = s; m < strend && *m != i; m++) ;
4138 dstr = NEWSV(30, m-s);
4139 sv_setpvn(dstr, s, m-s);
4148 while (s < strend && --limit &&
4149 (m=fbm_instr((unsigned char*)s, (unsigned char*)strend,
4153 dstr = NEWSV(31, m-s);
4154 sv_setpvn(dstr, s, m-s);
4163 maxiters += (strend - s) * rx->nparens;
4164 while (s < strend && --limit &&
4165 pregexec(rx, s, strend, orig, 1, Nullsv, TRUE))
4167 TAINT_IF(rx->exec_tainted);
4169 && rx->subbase != orig) {
4174 strend = s + (strend - m);
4177 dstr = NEWSV(32, m-s);
4178 sv_setpvn(dstr, s, m-s);
4183 for (i = 1; i <= rx->nparens; i++) {
4187 dstr = NEWSV(33, m-s);
4188 sv_setpvn(dstr, s, m-s);
4191 dstr = NEWSV(33, 0);
4200 LEAVE_SCOPE(oldsave);
4201 iters = (SP - stack_base) - base;
4202 if (iters > maxiters)
4205 /* keep field after final delim? */
4206 if (s < strend || (iters && origlimit)) {
4207 dstr = NEWSV(34, strend-s);
4208 sv_setpvn(dstr, s, strend-s);
4214 else if (!origlimit) {
4215 while (iters > 0 && (!TOPs || !SvANY(TOPs) || SvCUR(TOPs) == 0))
4219 SWITCHSTACK(ary, oldstack);
4220 if (SvSMAGICAL(ary)) {
4225 if (gimme == G_ARRAY) {
4227 Copy(AvARRAY(ary), SP + 1, iters, SV*);
4233 if (gimme == G_ARRAY)
4236 if (iters || !pm->op_pmreplroot) {
4246 unlock_condpair(void *svv)
4249 MAGIC *mg = mg_find((SV*)svv, 'm');
4252 croak("panic: unlock_condpair unlocking non-mutex");
4253 MUTEX_LOCK(MgMUTEXP(mg));
4254 if (MgOWNER(mg) != thr)
4255 croak("panic: unlock_condpair unlocking mutex that we don't own");
4257 COND_SIGNAL(MgOWNERCONDP(mg));
4258 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "0x%lx: unlock 0x%lx\n",
4259 (unsigned long)thr, (unsigned long)svv);)
4260 MUTEX_UNLOCK(MgMUTEXP(mg));
4262 #endif /* USE_THREADS */
4275 mg = condpair_magic(sv);
4276 MUTEX_LOCK(MgMUTEXP(mg));
4277 if (MgOWNER(mg) == thr)
4278 MUTEX_UNLOCK(MgMUTEXP(mg));
4281 COND_WAIT(MgOWNERCONDP(mg), MgMUTEXP(mg));
4283 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "0x%lx: pp_lock lock 0x%lx\n",
4284 (unsigned long)thr, (unsigned long)sv);)
4285 MUTEX_UNLOCK(MgMUTEXP(mg));
4286 SvREFCNT_inc(sv); /* keep alive until magic_mutexfree */
4287 save_destructor(unlock_condpair, sv);
4289 #endif /* USE_THREADS */
4290 if (SvTYPE(retsv) == SVt_PVAV || SvTYPE(retsv) == SVt_PVHV
4291 || SvTYPE(retsv) == SVt_PVCV) {
4292 retsv = refto(retsv);
4302 SV **svp = av_fetch(thr->magicals, op->op_targ, FALSE);
4304 croak("panic: pp_specific");
4306 if (op->op_private & OPpLVAL_INTRO)
4307 PUSHs(save_svref(svp));
4311 DIE("tried to access thread-specific data in non-threaded perl");
4312 #endif /* USE_THREADS */