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 */
112 /* XXX I can't imagine anyone who doesn't have this actually _needs_
113 it, since pid_t is an integral type.
116 #ifdef NEED_GETPID_PROTO
117 extern Pid_t getpid (void);
123 if (GIMME_V == G_SCALAR)
138 if (op->op_private & OPpLVAL_INTRO)
139 SAVECLEARSV(curpad[op->op_targ]);
141 if (op->op_flags & OPf_REF) {
145 if (GIMME == G_ARRAY) {
146 I32 maxarg = AvFILL((AV*)TARG) + 1;
148 if (SvMAGICAL(TARG)) {
150 for (i=0; i < maxarg; i++) {
151 SV **svp = av_fetch((AV*)TARG, i, FALSE);
152 SP[i+1] = (svp) ? *svp : &sv_undef;
156 Copy(AvARRAY((AV*)TARG), SP+1, maxarg, SV*);
161 SV* sv = sv_newmortal();
162 I32 maxarg = AvFILL((AV*)TARG) + 1;
163 sv_setiv(sv, maxarg);
175 if (op->op_private & OPpLVAL_INTRO)
176 SAVECLEARSV(curpad[op->op_targ]);
177 if (op->op_flags & OPf_REF)
180 if (gimme == G_ARRAY) {
181 RETURNOP(do_kv(ARGS));
183 else if (gimme == G_SCALAR) {
184 SV* sv = sv_newmortal();
185 if (HvFILL((HV*)TARG))
186 sv_setpvf(sv, "%ld/%ld",
187 (long)HvFILL((HV*)TARG), (long)HvMAX((HV*)TARG) + 1);
197 DIE("NOT IMPL LINE %d",__LINE__);
209 if (SvTYPE(sv) == SVt_PVIO) {
210 GV *gv = (GV*) sv_newmortal();
211 gv_init(gv, 0, "", 0, 0);
212 GvIOp(gv) = (IO *)sv;
213 (void)SvREFCNT_inc(sv);
215 } else if (SvTYPE(sv) != SVt_PVGV)
216 DIE("Not a GLOB reference");
219 if (SvTYPE(sv) != SVt_PVGV) {
222 if (SvGMAGICAL(sv)) {
228 if (op->op_flags & OPf_REF ||
229 op->op_private & HINT_STRICT_REFS)
230 DIE(no_usym, "a symbol");
236 if (op->op_private & HINT_STRICT_REFS)
237 DIE(no_symref, sym, "a symbol");
238 sv = (SV*)gv_fetchpv(sym, TRUE, SVt_PVGV);
241 if (op->op_private & OPpLVAL_INTRO)
242 save_gp((GV*)sv, !(op->op_flags & OPf_SPECIAL));
254 switch (SvTYPE(sv)) {
258 DIE("Not a SCALAR reference");
265 if (SvTYPE(gv) != SVt_PVGV) {
266 if (SvGMAGICAL(sv)) {
272 if (op->op_flags & OPf_REF ||
273 op->op_private & HINT_STRICT_REFS)
274 DIE(no_usym, "a SCALAR");
280 if (op->op_private & HINT_STRICT_REFS)
281 DIE(no_symref, sym, "a SCALAR");
282 gv = (GV*)gv_fetchpv(sym, TRUE, SVt_PV);
286 if (op->op_flags & OPf_MOD) {
287 if (op->op_private & OPpLVAL_INTRO)
288 sv = save_scalar((GV*)TOPs);
289 else if (op->op_private & OPpDEREF)
290 vivify_ref(sv, op->op_private & OPpDEREF);
300 SV *sv = AvARYLEN(av);
302 AvARYLEN(av) = sv = NEWSV(0,0);
303 sv_upgrade(sv, SVt_IV);
304 sv_magic(sv, (SV*)av, '#', Nullch, 0);
312 djSP; dTARGET; dPOPss;
314 if (op->op_flags & OPf_MOD) {
315 if (SvTYPE(TARG) < SVt_PVLV) {
316 sv_upgrade(TARG, SVt_PVLV);
317 sv_magic(TARG, Nullsv, '.', Nullch, 0);
322 PUSHs(TARG); /* no SvSETMAGIC */
328 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
329 mg = mg_find(sv, 'g');
330 if (mg && mg->mg_len >= 0) {
331 PUSHi(mg->mg_len + curcop->cop_arybase);
345 /* We usually try to add a non-existent subroutine in case of AUTOLOAD. */
346 /* (But not in defined().) */
347 CV *cv = sv_2cv(TOPs, &stash, &gv, !(op->op_flags & OPf_SPECIAL));
350 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
367 if (SvPOK(TOPs) && SvCUR(TOPs) >= 7) {
368 char *s = SvPVX(TOPs);
369 if (strnEQ(s, "CORE::", 6)) {
372 code = keyword(s + 6, SvCUR(TOPs) - 6);
373 if (code < 0) { /* Overridable. */
374 #define MAX_ARGS_OP ((sizeof(I32) - 1) * 2)
375 int i = 0, n = 0, seen_question = 0;
377 char str[ MAX_ARGS_OP * 2 + 2 ]; /* One ';', one '\0' */
379 while (i < MAXO) { /* The slow way. */
380 if (strEQ(s + 6, op_name[i]) || strEQ(s + 6, op_desc[i]))
384 goto nonesuch; /* Should not happen... */
386 oa = opargs[i] >> OASHIFT;
388 if (oa & OA_OPTIONAL) {
391 } else if (seen_question)
392 goto set; /* XXXX system, exec */
393 if ((oa & (OA_OPTIONAL - 1)) >= OA_AVREF
394 && (oa & (OA_OPTIONAL - 1)) <= OA_HVREF) {
397 /* What to do with R ((un)tie, tied, (sys)read, recv)? */
398 str[n++] = ("?$@@%&*$")[oa & (OA_OPTIONAL - 1)];
402 ret = sv_2mortal(newSVpv(str, n - 1));
403 } else if (code) /* Non-Overridable */
405 else { /* None such */
407 croak("Cannot find an opnumber for \"%s\"", s+6);
411 cv = sv_2cv(TOPs, &stash, &gv, FALSE);
413 ret = sv_2mortal(newSVpv(SvPVX(cv), SvCUR(cv)));
422 CV* cv = (CV*)curpad[op->op_targ];
424 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
440 if (GIMME != G_ARRAY) {
444 EXTEND_MORTAL(SP - MARK);
446 *MARK = refto(*MARK);
455 if (SvTYPE(sv) == SVt_PVLV && LvTYPE(sv) == 'y') {
458 if (!(sv = LvTARG(sv)))
461 else if (SvPADTMP(sv))
465 (void)SvREFCNT_inc(sv);
468 sv_upgrade(rv, SVt_RV);
482 if (sv && SvGMAGICAL(sv))
485 if (!sv || !SvROK(sv))
489 pv = sv_reftype(sv,TRUE);
490 PUSHp(pv, strlen(pv));
500 stash = curcop->cop_stash;
502 stash = gv_stashsv(POPs, TRUE);
504 (void)sv_bless(TOPs, stash);
521 switch (elem ? *elem : '\0')
524 if (strEQ(elem, "ARRAY"))
528 if (strEQ(elem, "CODE"))
529 ref = (SV*)GvCVu(gv);
532 if (strEQ(elem, "FILEHANDLE")) /* XXX deprecate in 5.005 */
533 ref = (SV*)GvIOp(gv);
536 if (strEQ(elem, "GLOB"))
540 if (strEQ(elem, "HASH"))
544 if (strEQ(elem, "IO"))
545 ref = (SV*)GvIOp(gv);
548 if (strEQ(elem, "NAME"))
549 sv = newSVpv(GvNAME(gv), GvNAMELEN(gv));
552 if (strEQ(elem, "PACKAGE"))
553 sv = newSVpv(HvNAME(GvSTASH(gv)), 0);
556 if (strEQ(elem, "SCALAR"))
570 /* Pattern matching */
575 register UNOP *unop = cUNOP;
576 register unsigned char *s;
579 register I32 *sfirst;
583 if(unop->op_first && unop->op_first->op_type == OP_PUSHRE) {
584 PMOP *pm = (PMOP *)unop->op_first;
585 SV *rv = sv_newmortal();
586 sv = newSVrv(rv, "Regexp");
587 sv_magic(sv,(SV*)ReREFCNT_inc(pm->op_pmregexp),'r',0,0);
591 if (sv == lastscream) {
597 SvSCREAM_off(lastscream);
598 SvREFCNT_dec(lastscream);
600 lastscream = SvREFCNT_inc(sv);
603 s = (unsigned char*)(SvPV(sv, len));
607 if (pos > maxscream) {
609 maxscream = pos + 80;
610 New(301, screamfirst, 256, I32);
611 New(302, screamnext, maxscream, I32);
614 maxscream = pos + pos / 4;
615 Renew(screamnext, maxscream, I32);
619 sfirst = screamfirst;
622 if (!sfirst || !snext)
623 DIE("do_study: out of memory");
625 for (ch = 256; ch; --ch)
632 snext[pos] = sfirst[ch] - pos;
639 sv_magic(sv, Nullsv, 'g', Nullch, 0); /* piggyback on m//g magic */
648 if (op->op_flags & OPf_STACKED)
654 TARG = sv_newmortal();
655 PUSHi(do_trans(sv, op));
659 /* Lvalue operators. */
671 djSP; dMARK; dTARGET;
681 SETi(do_chomp(TOPs));
687 djSP; dMARK; dTARGET;
688 register I32 count = 0;
691 count += do_chomp(POPs);
702 if (!sv || !SvANY(sv))
704 switch (SvTYPE(sv)) {
706 if (AvMAX(sv) >= 0 || SvGMAGICAL(sv))
710 if (HvARRAY(sv) || SvGMAGICAL(sv))
714 if (CvROOT(sv) || CvXSUB(sv))
731 if (!op->op_private) {
740 if (SvTHINKFIRST(sv)) {
747 switch (SvTYPE(sv)) {
757 if (cv_const_sv((CV*)sv))
758 warn("Constant subroutine %s undefined",
759 CvANON((CV*)sv) ? "(anonymous)" : GvENAME(CvGV((CV*)sv)));
762 { GV* gv = (GV*)SvREFCNT_inc(CvGV((CV*)sv));
764 CvGV((CV*)sv) = gv; } /* let user-undef'd sub keep its identity */
768 sv_setsv(sv, &sv_undef);
771 if (SvTYPE(sv) >= SVt_PV && SvPVX(sv) && SvLEN(sv)) {
774 SvPV_set(sv, Nullch);
787 if (SvREADONLY(TOPs) || SvTYPE(TOPs) > SVt_PVLV)
789 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) &&
790 SvIVX(TOPs) != IV_MIN)
793 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
804 if (SvREADONLY(TOPs) || SvTYPE(TOPs) > SVt_PVLV)
806 sv_setsv(TARG, TOPs);
807 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) &&
808 SvIVX(TOPs) != IV_MAX)
811 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
825 if(SvREADONLY(TOPs) || SvTYPE(TOPs) > SVt_PVLV)
827 sv_setsv(TARG, TOPs);
828 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) &&
829 SvIVX(TOPs) != IV_MIN)
832 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
841 /* Ordinary operators. */
845 djSP; dATARGET; tryAMAGICbin(pow,opASSIGN);
848 SETn( pow( left, right) );
855 djSP; dATARGET; tryAMAGICbin(mult,opASSIGN);
858 SETn( left * right );
865 djSP; dATARGET; tryAMAGICbin(div,opASSIGN);
870 DIE("Illegal division by zero");
872 /* insure that 20./5. == 4. */
875 if ((double)I_V(left) == left &&
876 (double)I_V(right) == right &&
877 (k = I_V(left)/I_V(right))*I_V(right) == I_V(left)) {
880 value = left / right;
884 value = left / right;
893 djSP; dATARGET; tryAMAGICbin(mod,opASSIGN);
901 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)) {
903 right = (right_neg = (i < 0)) ? -i : i;
907 right = U_V((right_neg = (n < 0)) ? -n : n);
910 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)) {
912 left = (left_neg = (i < 0)) ? -i : i;
916 left = U_V((left_neg = (n < 0)) ? -n : n);
920 DIE("Illegal modulus zero");
923 if ((left_neg != right_neg) && ans)
926 /* XXX may warn: unary minus operator applied to unsigned type */
927 /* could change -foo to be (~foo)+1 instead */
928 if (ans <= ~((UV)IV_MAX)+1)
929 sv_setiv(TARG, ~ans+1);
931 sv_setnv(TARG, -(double)ans);
942 djSP; dATARGET; tryAMAGICbin(repeat,opASSIGN);
944 register I32 count = POPi;
945 if (GIMME == G_ARRAY && op->op_private & OPpREPEAT_DOLIST) {
947 I32 items = SP - MARK;
959 repeatcpy((char*)(MARK + items), (char*)MARK,
960 items * sizeof(SV*), count - 1);
966 else { /* Note: mark already snarfed by pp_list */
971 if (TARG == tmpstr && SvTHINKFIRST(tmpstr)) {
972 if (SvREADONLY(tmpstr) && curcop != &compiling)
973 DIE("Can't x= to readonly value");
977 SvSetSV(TARG, tmpstr);
978 SvPV_force(TARG, len);
983 SvGROW(TARG, (count * len) + 1);
984 repeatcpy(SvPVX(TARG) + len, SvPVX(TARG), len, count - 1);
985 SvCUR(TARG) *= count;
989 (void)SvPOK_only(TARG);
998 djSP; dATARGET; tryAMAGICbin(subtr,opASSIGN);
1001 SETn( left - right );
1008 djSP; dATARGET; tryAMAGICbin(lshift,opASSIGN);
1011 if (op->op_private & HINT_INTEGER) {
1013 i = BWi(i) << shift;
1027 djSP; dATARGET; tryAMAGICbin(rshift,opASSIGN);
1030 if (op->op_private & HINT_INTEGER) {
1032 i = BWi(i) >> shift;
1046 djSP; tryAMAGICbinSET(lt,0);
1049 SETs(boolSV(TOPn < value));
1056 djSP; tryAMAGICbinSET(gt,0);
1059 SETs(boolSV(TOPn > value));
1066 djSP; tryAMAGICbinSET(le,0);
1069 SETs(boolSV(TOPn <= value));
1076 djSP; tryAMAGICbinSET(ge,0);
1079 SETs(boolSV(TOPn >= value));
1086 djSP; tryAMAGICbinSET(ne,0);
1089 SETs(boolSV(TOPn != value));
1096 djSP; dTARGET; tryAMAGICbin(ncmp,0);
1103 else if (left < right)
1105 else if (left > right)
1118 djSP; tryAMAGICbinSET(slt,0);
1121 int cmp = ((op->op_private & OPpLOCALE)
1122 ? sv_cmp_locale(left, right)
1123 : sv_cmp(left, right));
1124 SETs(boolSV(cmp < 0));
1131 djSP; tryAMAGICbinSET(sgt,0);
1134 int cmp = ((op->op_private & OPpLOCALE)
1135 ? sv_cmp_locale(left, right)
1136 : sv_cmp(left, right));
1137 SETs(boolSV(cmp > 0));
1144 djSP; tryAMAGICbinSET(sle,0);
1147 int cmp = ((op->op_private & OPpLOCALE)
1148 ? sv_cmp_locale(left, right)
1149 : sv_cmp(left, right));
1150 SETs(boolSV(cmp <= 0));
1157 djSP; tryAMAGICbinSET(sge,0);
1160 int cmp = ((op->op_private & OPpLOCALE)
1161 ? sv_cmp_locale(left, right)
1162 : sv_cmp(left, right));
1163 SETs(boolSV(cmp >= 0));
1170 djSP; tryAMAGICbinSET(seq,0);
1173 SETs(boolSV(sv_eq(left, right)));
1180 djSP; tryAMAGICbinSET(sne,0);
1183 SETs(boolSV(!sv_eq(left, right)));
1190 djSP; dTARGET; tryAMAGICbin(scmp,0);
1193 int cmp = ((op->op_private & OPpLOCALE)
1194 ? sv_cmp_locale(left, right)
1195 : sv_cmp(left, right));
1203 djSP; dATARGET; tryAMAGICbin(band,opASSIGN);
1206 if (SvNIOKp(left) || SvNIOKp(right)) {
1207 if (op->op_private & HINT_INTEGER) {
1208 IBW value = SvIV(left) & SvIV(right);
1212 UBW value = SvUV(left) & SvUV(right);
1217 do_vop(op->op_type, TARG, left, right);
1226 djSP; dATARGET; tryAMAGICbin(bxor,opASSIGN);
1229 if (SvNIOKp(left) || SvNIOKp(right)) {
1230 if (op->op_private & HINT_INTEGER) {
1231 IBW value = (USE_LEFT(left) ? SvIV(left) : 0) ^ SvIV(right);
1235 UBW value = (USE_LEFT(left) ? SvUV(left) : 0) ^ SvUV(right);
1240 do_vop(op->op_type, TARG, left, right);
1249 djSP; dATARGET; tryAMAGICbin(bor,opASSIGN);
1252 if (SvNIOKp(left) || SvNIOKp(right)) {
1253 if (op->op_private & HINT_INTEGER) {
1254 IBW value = (USE_LEFT(left) ? SvIV(left) : 0) | SvIV(right);
1258 UBW value = (USE_LEFT(left) ? SvUV(left) : 0) | SvUV(right);
1263 do_vop(op->op_type, TARG, left, right);
1272 djSP; dTARGET; tryAMAGICun(neg);
1277 if (SvIOKp(sv) && !SvNOKp(sv) && !SvPOKp(sv) && SvIVX(sv) != IV_MIN)
1279 else if (SvNIOKp(sv))
1281 else if (SvPOKp(sv)) {
1283 char *s = SvPV(sv, len);
1284 if (isIDFIRST(*s)) {
1285 sv_setpvn(TARG, "-", 1);
1288 else if (*s == '+' || *s == '-') {
1290 *SvPV_force(TARG, len) = *s == '-' ? '+' : '-';
1293 sv_setnv(TARG, -SvNV(sv));
1305 djSP; tryAMAGICunSET(not);
1306 #endif /* OVERLOAD */
1307 *stack_sp = boolSV(!SvTRUE(*stack_sp));
1313 djSP; dTARGET; tryAMAGICun(compl);
1317 if (op->op_private & HINT_INTEGER) {
1318 IBW value = ~SvIV(sv);
1322 UBW value = ~SvUV(sv);
1327 register char *tmps;
1328 register long *tmpl;
1333 tmps = SvPV_force(TARG, len);
1336 for ( ; anum && (unsigned long)tmps % sizeof(long); anum--, tmps++)
1339 for ( ; anum >= sizeof(long); anum -= sizeof(long), tmpl++)
1343 for ( ; anum > 0; anum--, tmps++)
1352 /* integer versions of some of the above */
1356 djSP; dATARGET; tryAMAGICbin(mult,opASSIGN);
1359 SETi( left * right );
1366 djSP; dATARGET; tryAMAGICbin(div,opASSIGN);
1370 DIE("Illegal division by zero");
1371 value = POPi / value;
1379 djSP; dATARGET; tryAMAGICbin(mod,opASSIGN);
1383 DIE("Illegal modulus zero");
1384 SETi( left % right );
1391 djSP; dATARGET; tryAMAGICbin(add,opASSIGN);
1394 SETi( left + right );
1401 djSP; dATARGET; tryAMAGICbin(subtr,opASSIGN);
1404 SETi( left - right );
1411 djSP; tryAMAGICbinSET(lt,0);
1414 SETs(boolSV(left < right));
1421 djSP; tryAMAGICbinSET(gt,0);
1424 SETs(boolSV(left > right));
1431 djSP; tryAMAGICbinSET(le,0);
1434 SETs(boolSV(left <= right));
1441 djSP; tryAMAGICbinSET(ge,0);
1444 SETs(boolSV(left >= right));
1451 djSP; tryAMAGICbinSET(eq,0);
1454 SETs(boolSV(left == right));
1461 djSP; tryAMAGICbinSET(ne,0);
1464 SETs(boolSV(left != right));
1471 djSP; dTARGET; tryAMAGICbin(ncmp,0);
1478 else if (left < right)
1489 djSP; dTARGET; tryAMAGICun(neg);
1494 /* High falutin' math. */
1498 djSP; dTARGET; tryAMAGICbin(atan2,0);
1501 SETn(atan2(left, right));
1508 djSP; dTARGET; tryAMAGICun(sin);
1520 djSP; dTARGET; tryAMAGICun(cos);
1540 if (!srand_called) {
1541 (void)srand((unsigned)seed());
1542 srand_called = TRUE;
1545 value = rand() * value / 2147483648.0;
1548 value = rand() * value / 65536.0;
1551 value = rand() * value / 32768.0;
1553 value = rand() * value / (double)(((unsigned long)1) << RANDBITS);
1569 (void)srand((unsigned)anum);
1570 srand_called = TRUE;
1579 * This is really just a quick hack which grabs various garbage
1580 * values. It really should be a real hash algorithm which
1581 * spreads the effect of every input bit onto every output bit,
1582 * if someone who knows about such tings would bother to write it.
1583 * Might be a good idea to add that function to CORE as well.
1584 * No numbers below come from careful analysis or anyting here,
1585 * except they are primes and SEED_C1 > 1E6 to get a full-width
1586 * value from (tv_sec * SEED_C1 + tv_usec). The multipliers should
1587 * probably be bigger too.
1590 # define SEED_C1 1000003
1591 #define SEED_C4 73819
1593 # define SEED_C1 25747
1594 #define SEED_C4 20639
1598 #define SEED_C5 26107
1603 # include <starlet.h>
1604 /* when[] = (low 32 bits, high 32 bits) of time since epoch
1605 * in 100-ns units, typically incremented ever 10 ms. */
1606 unsigned int when[2];
1607 _ckvmssts(sys$gettim(when));
1608 u = (U32)SEED_C1 * when[0] + (U32)SEED_C2 * when[1];
1610 # ifdef HAS_GETTIMEOFDAY
1611 struct timeval when;
1612 gettimeofday(&when,(struct timezone *) 0);
1613 u = (U32)SEED_C1 * when.tv_sec + (U32)SEED_C2 * when.tv_usec;
1617 u = (U32)SEED_C1 * when;
1620 u += SEED_C3 * (U32)getpid();
1621 u += SEED_C4 * (U32)(UV)stack_sp;
1622 #ifndef PLAN9 /* XXX Plan9 assembler chokes on this; fix needed */
1623 u += SEED_C5 * (U32)(UV)&when;
1630 djSP; dTARGET; tryAMAGICun(exp);
1642 djSP; dTARGET; tryAMAGICun(log);
1647 SET_NUMERIC_STANDARD();
1648 DIE("Can't take log of %g", value);
1658 djSP; dTARGET; tryAMAGICun(sqrt);
1663 SET_NUMERIC_STANDARD();
1664 DIE("Can't take sqrt of %g", value);
1666 value = sqrt(value);
1676 double value = TOPn;
1679 if (SvIOKp(TOPs) && !SvNOKp(TOPs) && !SvPOKp(TOPs)) {
1685 (void)modf(value, &value);
1687 (void)modf(-value, &value);
1702 djSP; dTARGET; tryAMAGICun(abs);
1704 double value = TOPn;
1707 if (SvIOKp(TOPs) && !SvNOKp(TOPs) && !SvPOKp(TOPs) &&
1708 (iv = SvIVX(TOPs)) != IV_MIN) {
1729 XPUSHu(scan_hex(tmps, 99, &argtype));
1741 while (*tmps && isSPACE(*tmps))
1746 value = scan_hex(++tmps, 99, &argtype);
1748 value = scan_oct(tmps, 99, &argtype);
1758 SETi( sv_len(TOPs) );
1771 I32 lvalue = op->op_flags & OPf_MOD;
1773 I32 arybase = curcop->cop_arybase;
1779 tmps = SvPV(sv, curlen);
1780 if (pos >= arybase) {
1798 else if (len >= 0) {
1800 if (rem > (I32)curlen)
1814 if (dowarn || lvalue)
1815 warn("substr outside of string");
1820 sv_setpvn(TARG, tmps, rem);
1821 if (lvalue) { /* it's an lvalue! */
1822 if (!SvGMAGICAL(sv)) {
1826 warn("Attempt to use reference as lvalue in substr");
1828 if (SvOK(sv)) /* is it defined ? */
1829 (void)SvPOK_only(sv);
1831 sv_setpvn(sv,"",0); /* avoid lexical reincarnation */
1834 if (SvTYPE(TARG) < SVt_PVLV) {
1835 sv_upgrade(TARG, SVt_PVLV);
1836 sv_magic(TARG, Nullsv, 'x', Nullch, 0);
1841 LvTARGOFF(TARG) = pos;
1842 LvTARGLEN(TARG) = rem;
1845 PUSHs(TARG); /* avoid SvSETMAGIC here */
1852 register I32 size = POPi;
1853 register I32 offset = POPi;
1854 register SV *src = POPs;
1855 I32 lvalue = op->op_flags & OPf_MOD;
1857 unsigned char *s = (unsigned char*)SvPV(src, srclen);
1858 unsigned long retnum;
1861 offset *= size; /* turn into bit offset */
1862 len = (offset + size + 7) / 8;
1863 if (offset < 0 || size < 1)
1866 if (lvalue) { /* it's an lvalue! */
1867 if (SvTYPE(TARG) < SVt_PVLV) {
1868 sv_upgrade(TARG, SVt_PVLV);
1869 sv_magic(TARG, Nullsv, 'v', Nullch, 0);
1874 LvTARGOFF(TARG) = offset;
1875 LvTARGLEN(TARG) = size;
1883 if (offset >= srclen)
1886 retnum = (unsigned long) s[offset] << 8;
1888 else if (size == 32) {
1889 if (offset >= srclen)
1891 else if (offset + 1 >= srclen)
1892 retnum = (unsigned long) s[offset] << 24;
1893 else if (offset + 2 >= srclen)
1894 retnum = ((unsigned long) s[offset] << 24) +
1895 ((unsigned long) s[offset + 1] << 16);
1897 retnum = ((unsigned long) s[offset] << 24) +
1898 ((unsigned long) s[offset + 1] << 16) +
1899 (s[offset + 2] << 8);
1904 retnum = (s[offset >> 3] >> (offset & 7)) & ((1 << size) - 1);
1909 else if (size == 16)
1910 retnum = ((unsigned long) s[offset] << 8) + s[offset+1];
1911 else if (size == 32)
1912 retnum = ((unsigned long) s[offset] << 24) +
1913 ((unsigned long) s[offset + 1] << 16) +
1914 (s[offset + 2] << 8) + s[offset+3];
1918 sv_setuv(TARG, (UV)retnum);
1933 I32 arybase = curcop->cop_arybase;
1938 offset = POPi - arybase;
1941 tmps = SvPV(big, biglen);
1944 else if (offset > biglen)
1946 if (!(tmps2 = fbm_instr((unsigned char*)tmps + offset,
1947 (unsigned char*)tmps + biglen, little)))
1948 retval = -1 + arybase;
1950 retval = tmps2 - tmps + arybase;
1967 I32 arybase = curcop->cop_arybase;
1973 tmps2 = SvPV(little, llen);
1974 tmps = SvPV(big, blen);
1978 offset = SvIV(offstr) - arybase + llen;
1981 else if (offset > blen)
1983 if (!(tmps2 = rninstr(tmps, tmps + offset,
1984 tmps2, tmps2 + llen)))
1985 retval = -1 + arybase;
1987 retval = tmps2 - tmps + arybase;
1994 djSP; dMARK; dORIGMARK; dTARGET;
1995 #ifdef USE_LOCALE_NUMERIC
1996 if (op->op_private & OPpLOCALE)
1997 SET_NUMERIC_LOCAL();
1999 SET_NUMERIC_STANDARD();
2001 do_sprintf(TARG, SP-MARK, MARK+1);
2002 TAINT_IF(SvTAINTED(TARG));
2016 value = (I32) (*tmps & 255);
2021 value = (I32) (anum & 255);
2032 (void)SvUPGRADE(TARG,SVt_PV);
2038 (void)SvPOK_only(TARG);
2045 djSP; dTARGET; dPOPTOPssrl;
2047 char *tmps = SvPV(left, na);
2049 sv_setpv(TARG, fcrypt(tmps, SvPV(right, na)));
2051 sv_setpv(TARG, crypt(tmps, SvPV(right, na)));
2055 "The crypt() function is unimplemented due to excessive paranoia.");
2067 if (!SvPADTMP(sv)) {
2073 s = SvPV_force(sv, na);
2075 if (op->op_private & OPpLOCALE) {
2078 *s = toUPPER_LC(*s);
2093 if (!SvPADTMP(sv)) {
2099 s = SvPV_force(sv, na);
2101 if (op->op_private & OPpLOCALE) {
2104 *s = toLOWER_LC(*s);
2121 if (!SvPADTMP(sv)) {
2128 s = SvPV_force(sv, len);
2130 register char *send = s + len;
2132 if (op->op_private & OPpLOCALE) {
2135 for (; s < send; s++)
2136 *s = toUPPER_LC(*s);
2139 for (; s < send; s++)
2153 if (!SvPADTMP(sv)) {
2160 s = SvPV_force(sv, len);
2162 register char *send = s + len;
2164 if (op->op_private & OPpLOCALE) {
2167 for (; s < send; s++)
2168 *s = toLOWER_LC(*s);
2171 for (; s < send; s++)
2183 register char *s = SvPV(sv,len);
2187 (void)SvUPGRADE(TARG, SVt_PV);
2188 SvGROW(TARG, (len * 2) + 1);
2196 SvCUR_set(TARG, d - SvPVX(TARG));
2197 (void)SvPOK_only(TARG);
2200 sv_setpvn(TARG, s, len);
2209 djSP; dMARK; dORIGMARK;
2211 register AV* av = (AV*)POPs;
2212 register I32 lval = op->op_flags & OPf_MOD;
2213 I32 arybase = curcop->cop_arybase;
2216 if (SvTYPE(av) == SVt_PVAV) {
2217 if (lval && op->op_private & OPpLVAL_INTRO) {
2219 for (svp = mark + 1; svp <= sp; svp++) {
2224 if (max > AvMAX(av))
2227 while (++MARK <= SP) {
2228 elem = SvIVx(*MARK);
2232 svp = av_fetch(av, elem, lval);
2234 if (!svp || *svp == &sv_undef)
2235 DIE(no_aelem, elem);
2236 if (op->op_private & OPpLVAL_INTRO)
2237 save_aelem(av, elem, svp);
2239 *MARK = svp ? *svp : &sv_undef;
2242 if (GIMME != G_ARRAY) {
2250 /* Associative arrays. */
2255 HV *hash = (HV*)POPs;
2257 I32 gimme = GIMME_V;
2258 I32 realhv = (SvTYPE(hash) == SVt_PVHV);
2261 /* might clobber stack_sp */
2262 entry = realhv ? hv_iternext(hash) : avhv_iternext((AV*)hash);
2267 PUSHs(hv_iterkeysv(entry)); /* won't clobber stack_sp */
2268 if (gimme == G_ARRAY) {
2270 /* might clobber stack_sp */
2271 sv_setsv(TARG, realhv ?
2272 hv_iterval(hash, entry) : avhv_iterval((AV*)hash, entry));
2277 else if (gimme == G_SCALAR)
2296 I32 gimme = GIMME_V;
2297 I32 discard = (gimme == G_VOID) ? G_DISCARD : 0;
2301 if (op->op_private & OPpSLICE) {
2305 hvtype = SvTYPE(hv);
2306 while (++MARK <= SP) {
2307 if (hvtype == SVt_PVHV)
2308 sv = hv_delete_ent(hv, *MARK, discard, 0);
2309 else if (hvtype == SVt_PVAV)
2310 sv = avhv_delete_ent((AV*)hv, *MARK, discard, 0);
2312 DIE("Not a HASH reference");
2313 *MARK = sv ? sv : &sv_undef;
2317 else if (gimme == G_SCALAR) {
2326 if (SvTYPE(hv) == SVt_PVHV)
2327 sv = hv_delete_ent(hv, keysv, discard, 0);
2328 else if (SvTYPE(hv) == SVt_PVAV)
2329 sv = avhv_delete_ent((AV*)hv, keysv, discard, 0);
2331 DIE("Not a HASH reference");
2345 if (SvTYPE(hv) == SVt_PVHV) {
2346 if (hv_exists_ent(hv, tmpsv, 0))
2348 } else if (SvTYPE(hv) == SVt_PVAV) {
2349 if (avhv_exists_ent((AV*)hv, tmpsv, 0))
2352 DIE("Not a HASH reference");
2359 djSP; dMARK; dORIGMARK;
2361 register HV *hv = (HV*)POPs;
2362 register I32 lval = op->op_flags & OPf_MOD;
2363 I32 realhv = (SvTYPE(hv) == SVt_PVHV);
2365 if (realhv || SvTYPE(hv) == SVt_PVAV) {
2366 while (++MARK <= SP) {
2370 he = hv_fetch_ent(hv, keysv, lval, 0);
2371 svp = he ? &HeVAL(he) : 0;
2373 svp = avhv_fetch_ent((AV*)hv, keysv, lval, 0);
2376 if (!he || HeVAL(he) == &sv_undef)
2377 DIE(no_helem, SvPV(keysv, na));
2378 if (op->op_private & OPpLVAL_INTRO)
2379 save_helem(hv, keysv, &HeVAL(he));
2381 *MARK = he ? HeVAL(he) : &sv_undef;
2384 if (GIMME != G_ARRAY) {
2392 /* List operators. */
2397 if (GIMME != G_ARRAY) {
2399 *MARK = *SP; /* unwanted list, return last item */
2410 SV **lastrelem = stack_sp;
2411 SV **lastlelem = stack_base + POPMARK;
2412 SV **firstlelem = stack_base + POPMARK + 1;
2413 register SV **firstrelem = lastlelem + 1;
2414 I32 arybase = curcop->cop_arybase;
2415 I32 lval = op->op_flags & OPf_MOD;
2416 I32 is_something_there = lval;
2418 register I32 max = lastrelem - lastlelem;
2419 register SV **lelem;
2422 if (GIMME != G_ARRAY) {
2423 ix = SvIVx(*lastlelem);
2428 if (ix < 0 || ix >= max)
2429 *firstlelem = &sv_undef;
2431 *firstlelem = firstrelem[ix];
2437 SP = firstlelem - 1;
2441 for (lelem = firstlelem; lelem <= lastlelem; lelem++) {
2447 else if (!(*lelem = firstrelem[ix]))
2452 if (ix >= max || !(*lelem = firstrelem[ix]))
2455 if (!is_something_there && (SvOK(*lelem) || SvGMAGICAL(*lelem)))
2456 is_something_there = TRUE;
2458 if (is_something_there)
2461 SP = firstlelem - 1;
2467 djSP; dMARK; dORIGMARK;
2468 I32 items = SP - MARK;
2469 SV *av = sv_2mortal((SV*)av_make(items, MARK+1));
2470 SP = ORIGMARK; /* av_make() might realloc stack_sp */
2477 djSP; dMARK; dORIGMARK;
2478 HV* hv = (HV*)sv_2mortal((SV*)newHV());
2482 SV *val = NEWSV(46, 0);
2484 sv_setsv(val, *++MARK);
2486 warn("Odd number of elements in hash list");
2487 (void)hv_store_ent(hv,key,val,0);
2496 djSP; dMARK; dORIGMARK;
2497 register AV *ary = (AV*)*++MARK;
2501 register I32 offset;
2502 register I32 length;
2509 if (SvRMAGICAL(ary) && (mg = mg_find((SV*)ary,'P'))) {
2510 *MARK-- = mg->mg_obj;
2514 perl_call_method("SPLICE",GIMME_V);
2523 offset = i = SvIVx(*MARK);
2525 offset += AvFILLp(ary) + 1;
2527 offset -= curcop->cop_arybase;
2531 length = SvIVx(*MARK++);
2536 length = AvMAX(ary) + 1; /* close enough to infinity */
2540 length = AvMAX(ary) + 1;
2542 if (offset > AvFILLp(ary) + 1)
2543 offset = AvFILLp(ary) + 1;
2544 after = AvFILLp(ary) + 1 - (offset + length);
2545 if (after < 0) { /* not that much array */
2546 length += after; /* offset+length now in array */
2552 /* At this point, MARK .. SP-1 is our new LIST */
2555 diff = newlen - length;
2556 if (newlen && !AvREAL(ary)) {
2560 assert(AvREAL(ary)); /* would leak, so croak */
2563 if (diff < 0) { /* shrinking the area */
2565 New(451, tmparyval, newlen, SV*); /* so remember insertion */
2566 Copy(MARK, tmparyval, newlen, SV*);
2569 MARK = ORIGMARK + 1;
2570 if (GIMME == G_ARRAY) { /* copy return vals to stack */
2571 MEXTEND(MARK, length);
2572 Copy(AvARRAY(ary)+offset, MARK, length, SV*);
2574 EXTEND_MORTAL(length);
2575 for (i = length, dst = MARK; i; i--) {
2576 if (!SvIMMORTAL(*dst))
2577 sv_2mortal(*dst); /* free them eventualy */
2584 *MARK = AvARRAY(ary)[offset+length-1];
2586 if (!SvIMMORTAL(*MARK))
2588 for (i = length - 1, dst = &AvARRAY(ary)[offset]; i > 0; i--)
2589 SvREFCNT_dec(*dst++); /* free them now */
2592 AvFILLp(ary) += diff;
2594 /* pull up or down? */
2596 if (offset < after) { /* easier to pull up */
2597 if (offset) { /* esp. if nothing to pull */
2598 src = &AvARRAY(ary)[offset-1];
2599 dst = src - diff; /* diff is negative */
2600 for (i = offset; i > 0; i--) /* can't trust Copy */
2604 SvPVX(ary) = (char*)(AvARRAY(ary) - diff); /* diff is negative */
2608 if (after) { /* anything to pull down? */
2609 src = AvARRAY(ary) + offset + length;
2610 dst = src + diff; /* diff is negative */
2611 Move(src, dst, after, SV*);
2613 dst = &AvARRAY(ary)[AvFILLp(ary)+1];
2614 /* avoid later double free */
2618 dst[--i] = &sv_undef;
2621 for (src = tmparyval, dst = AvARRAY(ary) + offset;
2623 *dst = NEWSV(46, 0);
2624 sv_setsv(*dst++, *src++);
2626 Safefree(tmparyval);
2629 else { /* no, expanding (or same) */
2631 New(452, tmparyval, length, SV*); /* so remember deletion */
2632 Copy(AvARRAY(ary)+offset, tmparyval, length, SV*);
2635 if (diff > 0) { /* expanding */
2637 /* push up or down? */
2639 if (offset < after && diff <= AvARRAY(ary) - AvALLOC(ary)) {
2643 Move(src, dst, offset, SV*);
2645 SvPVX(ary) = (char*)(AvARRAY(ary) - diff);/* diff is positive */
2647 AvFILLp(ary) += diff;
2650 if (AvFILLp(ary) + diff >= AvMAX(ary)) /* oh, well */
2651 av_extend(ary, AvFILLp(ary) + diff);
2652 AvFILLp(ary) += diff;
2655 dst = AvARRAY(ary) + AvFILLp(ary);
2657 for (i = after; i; i--) {
2664 for (src = MARK, dst = AvARRAY(ary) + offset; newlen; newlen--) {
2665 *dst = NEWSV(46, 0);
2666 sv_setsv(*dst++, *src++);
2668 MARK = ORIGMARK + 1;
2669 if (GIMME == G_ARRAY) { /* copy return vals to stack */
2671 Copy(tmparyval, MARK, length, SV*);
2673 EXTEND_MORTAL(length);
2674 for (i = length, dst = MARK; i; i--) {
2675 if (!SvIMMORTAL(*dst))
2676 sv_2mortal(*dst); /* free them eventualy */
2680 Safefree(tmparyval);
2684 else if (length--) {
2685 *MARK = tmparyval[length];
2687 if (!SvIMMORTAL(*MARK))
2689 while (length-- > 0)
2690 SvREFCNT_dec(tmparyval[length]);
2692 Safefree(tmparyval);
2703 djSP; dMARK; dORIGMARK; dTARGET;
2704 register AV *ary = (AV*)*++MARK;
2705 register SV *sv = &sv_undef;
2708 if (SvRMAGICAL(ary) && (mg = mg_find((SV*)ary,'P'))) {
2709 *MARK-- = mg->mg_obj;
2713 perl_call_method("PUSH",G_SCALAR|G_DISCARD);
2718 /* Why no pre-extend of ary here ? */
2719 for (++MARK; MARK <= SP; MARK++) {
2722 sv_setsv(sv, *MARK);
2727 PUSHi( AvFILL(ary) + 1 );
2735 SV *sv = av_pop(av);
2736 if (!SvIMMORTAL(sv) && AvREAL(av))
2737 (void)sv_2mortal(sv);
2746 SV *sv = av_shift(av);
2750 if (!SvIMMORTAL(sv) && AvREAL(av))
2751 (void)sv_2mortal(sv);
2758 djSP; dMARK; dORIGMARK; dTARGET;
2759 register AV *ary = (AV*)*++MARK;
2764 if (SvRMAGICAL(ary) && (mg = mg_find((SV*)ary,'P'))) {
2765 *MARK-- = mg->mg_obj;
2769 perl_call_method("UNSHIFT",G_SCALAR|G_DISCARD);
2774 av_unshift(ary, SP - MARK);
2777 sv_setsv(sv, *++MARK);
2778 (void)av_store(ary, i++, sv);
2782 PUSHi( AvFILL(ary) + 1 );
2792 if (GIMME == G_ARRAY) {
2803 register char *down;
2809 do_join(TARG, &sv_no, MARK, SP);
2811 sv_setsv(TARG, (SP > MARK) ? *SP : DEFSV);
2812 up = SvPV_force(TARG, len);
2814 down = SvPVX(TARG) + len - 1;
2820 (void)SvPOK_only(TARG);
2829 mul128(SV *sv, U8 m)
2832 char *s = SvPV(sv, len);
2836 if (!strnEQ(s, "0000", 4)) { /* need to grow sv */
2837 SV *New = newSVpv("0000000000", 10);
2840 SvREFCNT_dec(sv); /* free old sv */
2845 while (!*t) /* trailing '\0'? */
2848 i = ((*t - '0') << 7) + m;
2849 *(t--) = '0' + (i % 10);
2855 /* Explosives and implosives. */
2862 I32 gimme = GIMME_V;
2866 register char *pat = SvPV(left, llen);
2867 register char *s = SvPV(right, rlen);
2868 char *strend = s + rlen;
2870 register char *patend = pat + llen;
2875 /* These must not be in registers: */
2886 unsigned Quad_t auquad;
2892 register U32 culong;
2894 static char* bitcount = 0;
2897 if (gimme != G_ARRAY) { /* arrange to do first one only */
2899 for (patend = pat; !isALPHA(*patend) || *patend == 'x'; patend++) ;
2900 if (strchr("aAbBhHP", *patend) || *pat == '%') {
2902 while (isDIGIT(*patend) || *patend == '*')
2908 while (pat < patend) {
2910 datumtype = *pat++ & 0xFF;
2911 if (isSPACE(datumtype))
2915 else if (*pat == '*') {
2916 len = strend - strbeg; /* long enough */
2919 else if (isDIGIT(*pat)) {
2921 while (isDIGIT(*pat))
2922 len = (len * 10) + (*pat++ - '0');
2925 len = (datumtype != '@');
2928 croak("Invalid type in unpack: '%c'", (int)datumtype);
2929 case ',': /* grandfather in commas but with a warning */
2930 if (commas++ == 0 && dowarn)
2931 warn("Invalid type in unpack: '%c'", (int)datumtype);
2934 if (len == 1 && pat[-1] != '1')
2943 if (len > strend - strbeg)
2944 DIE("@ outside of string");
2948 if (len > s - strbeg)
2949 DIE("X outside of string");
2953 if (len > strend - s)
2954 DIE("x outside of string");
2959 if (len > strend - s)
2962 goto uchar_checksum;
2963 sv = NEWSV(35, len);
2964 sv_setpvn(sv, s, len);
2966 if (datumtype == 'A') {
2967 aptr = s; /* borrow register */
2968 s = SvPVX(sv) + len - 1;
2969 while (s >= SvPVX(sv) && (!*s || isSPACE(*s)))
2972 SvCUR_set(sv, s - SvPVX(sv));
2973 s = aptr; /* unborrow register */
2975 XPUSHs(sv_2mortal(sv));
2979 if (pat[-1] == '*' || len > (strend - s) * 8)
2980 len = (strend - s) * 8;
2983 Newz(601, bitcount, 256, char);
2984 for (bits = 1; bits < 256; bits++) {
2985 if (bits & 1) bitcount[bits]++;
2986 if (bits & 2) bitcount[bits]++;
2987 if (bits & 4) bitcount[bits]++;
2988 if (bits & 8) bitcount[bits]++;
2989 if (bits & 16) bitcount[bits]++;
2990 if (bits & 32) bitcount[bits]++;
2991 if (bits & 64) bitcount[bits]++;
2992 if (bits & 128) bitcount[bits]++;
2996 culong += bitcount[*(unsigned char*)s++];
3001 if (datumtype == 'b') {
3003 if (bits & 1) culong++;
3009 if (bits & 128) culong++;
3016 sv = NEWSV(35, len + 1);
3019 aptr = pat; /* borrow register */
3021 if (datumtype == 'b') {
3023 for (len = 0; len < aint; len++) {
3024 if (len & 7) /*SUPPRESS 595*/
3028 *pat++ = '0' + (bits & 1);
3033 for (len = 0; len < aint; len++) {
3038 *pat++ = '0' + ((bits & 128) != 0);
3042 pat = aptr; /* unborrow register */
3043 XPUSHs(sv_2mortal(sv));
3047 if (pat[-1] == '*' || len > (strend - s) * 2)
3048 len = (strend - s) * 2;
3049 sv = NEWSV(35, len + 1);
3052 aptr = pat; /* borrow register */
3054 if (datumtype == 'h') {
3056 for (len = 0; len < aint; len++) {
3061 *pat++ = hexdigit[bits & 15];
3066 for (len = 0; len < aint; len++) {
3071 *pat++ = hexdigit[(bits >> 4) & 15];
3075 pat = aptr; /* unborrow register */
3076 XPUSHs(sv_2mortal(sv));
3079 if (len > strend - s)
3084 if (aint >= 128) /* fake up signed chars */
3094 if (aint >= 128) /* fake up signed chars */
3097 sv_setiv(sv, (IV)aint);
3098 PUSHs(sv_2mortal(sv));
3103 if (len > strend - s)
3118 sv_setiv(sv, (IV)auint);
3119 PUSHs(sv_2mortal(sv));
3124 along = (strend - s) / SIZE16;
3141 sv_setiv(sv, (IV)ashort);
3142 PUSHs(sv_2mortal(sv));
3149 along = (strend - s) / SIZE16;
3154 COPY16(s, &aushort);
3157 if (datumtype == 'n')
3158 aushort = PerlSock_ntohs(aushort);
3161 if (datumtype == 'v')
3162 aushort = vtohs(aushort);
3171 COPY16(s, &aushort);
3175 if (datumtype == 'n')
3176 aushort = PerlSock_ntohs(aushort);
3179 if (datumtype == 'v')
3180 aushort = vtohs(aushort);
3182 sv_setiv(sv, (IV)aushort);
3183 PUSHs(sv_2mortal(sv));
3188 along = (strend - s) / sizeof(int);
3193 Copy(s, &aint, 1, int);
3196 cdouble += (double)aint;
3205 Copy(s, &aint, 1, int);
3208 sv_setiv(sv, (IV)aint);
3209 PUSHs(sv_2mortal(sv));
3214 along = (strend - s) / sizeof(unsigned int);
3219 Copy(s, &auint, 1, unsigned int);
3220 s += sizeof(unsigned int);
3222 cdouble += (double)auint;
3231 Copy(s, &auint, 1, unsigned int);
3232 s += sizeof(unsigned int);
3234 sv_setuv(sv, (UV)auint);
3235 PUSHs(sv_2mortal(sv));
3240 along = (strend - s) / SIZE32;
3248 cdouble += (double)along;
3260 sv_setiv(sv, (IV)along);
3261 PUSHs(sv_2mortal(sv));
3268 along = (strend - s) / SIZE32;
3276 if (datumtype == 'N')
3277 aulong = PerlSock_ntohl(aulong);
3280 if (datumtype == 'V')
3281 aulong = vtohl(aulong);
3284 cdouble += (double)aulong;
3296 if (datumtype == 'N')
3297 aulong = PerlSock_ntohl(aulong);
3300 if (datumtype == 'V')
3301 aulong = vtohl(aulong);
3304 sv_setuv(sv, (UV)aulong);
3305 PUSHs(sv_2mortal(sv));
3310 along = (strend - s) / sizeof(char*);
3316 if (sizeof(char*) > strend - s)
3319 Copy(s, &aptr, 1, char*);
3325 PUSHs(sv_2mortal(sv));
3335 while ((len > 0) && (s < strend)) {
3336 auv = (auv << 7) | (*s & 0x7f);
3337 if (!(*s++ & 0x80)) {
3341 PUSHs(sv_2mortal(sv));
3345 else if (++bytes >= sizeof(UV)) { /* promote to string */
3348 sv = newSVpvf("%.*Vu", (int)TYPE_DIGITS(UV), auv);
3349 while (s < strend) {
3350 sv = mul128(sv, *s & 0x7f);
3351 if (!(*s++ & 0x80)) {
3360 PUSHs(sv_2mortal(sv));
3365 if ((s >= strend) && bytes)
3366 croak("Unterminated compressed integer");
3371 if (sizeof(char*) > strend - s)
3374 Copy(s, &aptr, 1, char*);
3379 sv_setpvn(sv, aptr, len);
3380 PUSHs(sv_2mortal(sv));
3387 if (s + sizeof(Quad_t) > strend)
3390 Copy(s, &aquad, 1, Quad_t);
3391 s += sizeof(Quad_t);
3394 if (aquad >= IV_MIN && aquad <= IV_MAX)
3395 sv_setiv(sv, (IV)aquad);
3397 sv_setnv(sv, (double)aquad);
3398 PUSHs(sv_2mortal(sv));
3405 if (s + sizeof(unsigned Quad_t) > strend)
3408 Copy(s, &auquad, 1, unsigned Quad_t);
3409 s += sizeof(unsigned Quad_t);
3412 if (aquad <= UV_MAX)
3413 sv_setuv(sv, (UV)auquad);
3415 sv_setnv(sv, (double)auquad);
3416 PUSHs(sv_2mortal(sv));
3420 /* float and double added gnb@melba.bby.oz.au 22/11/89 */
3423 along = (strend - s) / sizeof(float);
3428 Copy(s, &afloat, 1, float);
3437 Copy(s, &afloat, 1, float);
3440 sv_setnv(sv, (double)afloat);
3441 PUSHs(sv_2mortal(sv));
3447 along = (strend - s) / sizeof(double);
3452 Copy(s, &adouble, 1, double);
3453 s += sizeof(double);
3461 Copy(s, &adouble, 1, double);
3462 s += sizeof(double);
3464 sv_setnv(sv, (double)adouble);
3465 PUSHs(sv_2mortal(sv));
3470 along = (strend - s) * 3 / 4;
3471 sv = NEWSV(42, along);
3474 while (s < strend && *s > ' ' && *s < 'a') {
3479 len = (*s++ - ' ') & 077;
3481 if (s < strend && *s >= ' ')
3482 a = (*s++ - ' ') & 077;
3485 if (s < strend && *s >= ' ')
3486 b = (*s++ - ' ') & 077;
3489 if (s < strend && *s >= ' ')
3490 c = (*s++ - ' ') & 077;
3493 if (s < strend && *s >= ' ')
3494 d = (*s++ - ' ') & 077;
3497 hunk[0] = (a << 2) | (b >> 4);
3498 hunk[1] = (b << 4) | (c >> 2);
3499 hunk[2] = (c << 6) | d;
3500 sv_catpvn(sv, hunk, (len > 3) ? 3 : len);
3505 else if (s[1] == '\n') /* possible checksum byte */
3508 XPUSHs(sv_2mortal(sv));
3513 if (strchr("fFdD", datumtype) ||
3514 (checksum > 32 && strchr("iIlLN", datumtype)) ) {
3518 while (checksum >= 16) {
3522 while (checksum >= 4) {
3528 along = (1 << checksum) - 1;
3529 while (cdouble < 0.0)
3531 cdouble = modf(cdouble / adouble, &trouble) * adouble;
3532 sv_setnv(sv, cdouble);
3535 if (checksum < 32) {
3536 aulong = (1 << checksum) - 1;
3539 sv_setuv(sv, (UV)culong);
3541 XPUSHs(sv_2mortal(sv));
3545 if (sp == oldsp && gimme == G_SCALAR)
3551 doencodes(register SV *sv, register char *s, register I32 len)
3556 sv_catpvn(sv, hunk, 1);
3559 hunk[0] = ' ' + (077 & (*s >> 2));
3560 hunk[1] = ' ' + (077 & (((*s << 4) & 060) | ((s[1] >> 4) & 017)));
3561 hunk[2] = ' ' + (077 & (((s[1] << 2) & 074) | ((s[2] >> 6) & 03)));
3562 hunk[3] = ' ' + (077 & (s[2] & 077));
3563 sv_catpvn(sv, hunk, 4);
3567 for (s = SvPVX(sv); *s; s++) {
3571 sv_catpvn(sv, "\n", 1);
3575 is_an_int(char *s, STRLEN l)
3577 SV *result = newSVpv("", l);
3578 char *result_c = SvPV(result, na); /* convenience */
3579 char *out = result_c;
3589 SvREFCNT_dec(result);
3612 SvREFCNT_dec(result);
3618 SvCUR_set(result, out - result_c);
3623 div128(SV *pnum, bool *done)
3624 /* must be '\0' terminated */
3628 char *s = SvPV(pnum, len);
3637 i = m * 10 + (*t - '0');
3639 r = (i >> 7); /* r < 10 */
3646 SvCUR_set(pnum, (STRLEN) (t - s));
3653 djSP; dMARK; dORIGMARK; dTARGET;
3654 register SV *cat = TARG;
3657 register char *pat = SvPVx(*++MARK, fromlen);
3658 register char *patend = pat + fromlen;
3663 static char null10[] = {0,0,0,0,0,0,0,0,0,0};
3664 static char *space10 = " ";
3666 /* These must not be in registers: */
3675 unsigned Quad_t auquad;
3684 sv_setpvn(cat, "", 0);
3685 while (pat < patend) {
3686 #define NEXTFROM (items-- > 0 ? *MARK++ : &sv_no)
3687 datumtype = *pat++ & 0xFF;
3688 if (isSPACE(datumtype))
3691 len = strchr("@Xxu", datumtype) ? 0 : items;
3694 else if (isDIGIT(*pat)) {
3696 while (isDIGIT(*pat))
3697 len = (len * 10) + (*pat++ - '0');
3703 croak("Invalid type in pack: '%c'", (int)datumtype);
3704 case ',': /* grandfather in commas but with a warning */
3705 if (commas++ == 0 && dowarn)
3706 warn("Invalid type in pack: '%c'", (int)datumtype);
3709 DIE("%% may only be used in unpack");
3720 if (SvCUR(cat) < len)
3721 DIE("X outside of string");
3728 sv_catpvn(cat, null10, 10);
3731 sv_catpvn(cat, null10, len);
3736 aptr = SvPV(fromstr, fromlen);
3740 sv_catpvn(cat, aptr, len);
3742 sv_catpvn(cat, aptr, fromlen);
3744 if (datumtype == 'A') {
3746 sv_catpvn(cat, space10, 10);
3749 sv_catpvn(cat, space10, len);
3753 sv_catpvn(cat, null10, 10);
3756 sv_catpvn(cat, null10, len);
3763 char *savepat = pat;
3768 aptr = SvPV(fromstr, fromlen);
3773 SvCUR(cat) += (len+7)/8;
3774 SvGROW(cat, SvCUR(cat) + 1);
3775 aptr = SvPVX(cat) + aint;
3780 if (datumtype == 'B') {
3781 for (len = 0; len++ < aint;) {
3782 items |= *pat++ & 1;
3786 *aptr++ = items & 0xff;
3792 for (len = 0; len++ < aint;) {
3798 *aptr++ = items & 0xff;
3804 if (datumtype == 'B')
3805 items <<= 7 - (aint & 7);
3807 items >>= 7 - (aint & 7);
3808 *aptr++ = items & 0xff;
3810 pat = SvPVX(cat) + SvCUR(cat);
3821 char *savepat = pat;
3826 aptr = SvPV(fromstr, fromlen);
3831 SvCUR(cat) += (len+1)/2;
3832 SvGROW(cat, SvCUR(cat) + 1);
3833 aptr = SvPVX(cat) + aint;
3838 if (datumtype == 'H') {
3839 for (len = 0; len++ < aint;) {
3841 items |= ((*pat++ & 15) + 9) & 15;
3843 items |= *pat++ & 15;
3847 *aptr++ = items & 0xff;
3853 for (len = 0; len++ < aint;) {
3855 items |= (((*pat++ & 15) + 9) & 15) << 4;
3857 items |= (*pat++ & 15) << 4;
3861 *aptr++ = items & 0xff;
3867 *aptr++ = items & 0xff;
3868 pat = SvPVX(cat) + SvCUR(cat);
3880 aint = SvIV(fromstr);
3882 sv_catpvn(cat, &achar, sizeof(char));
3885 /* Float and double added by gnb@melba.bby.oz.au 22/11/89 */
3890 afloat = (float)SvNV(fromstr);
3891 sv_catpvn(cat, (char *)&afloat, sizeof (float));
3898 adouble = (double)SvNV(fromstr);
3899 sv_catpvn(cat, (char *)&adouble, sizeof (double));
3905 ashort = (I16)SvIV(fromstr);
3907 ashort = PerlSock_htons(ashort);
3909 CAT16(cat, &ashort);
3915 ashort = (I16)SvIV(fromstr);
3917 ashort = htovs(ashort);
3919 CAT16(cat, &ashort);
3926 ashort = (I16)SvIV(fromstr);
3927 CAT16(cat, &ashort);
3933 auint = SvUV(fromstr);
3934 sv_catpvn(cat, (char*)&auint, sizeof(unsigned int));
3940 adouble = floor(SvNV(fromstr));
3943 croak("Cannot compress negative numbers");
3949 #ifdef CXUX_BROKEN_CONSTANT_CONVERT
3950 adouble <= UV_MAX_cxux
3957 char buf[1 + sizeof(UV)];
3958 char *in = buf + sizeof(buf);
3959 UV auv = U_V(adouble);;
3962 *--in = (auv & 0x7f) | 0x80;
3965 buf[sizeof(buf) - 1] &= 0x7f; /* clear continue bit */
3966 sv_catpvn(cat, in, (buf + sizeof(buf)) - in);
3968 else if (SvPOKp(fromstr)) { /* decimal string arithmetics */
3969 char *from, *result, *in;
3974 /* Copy string and check for compliance */
3975 from = SvPV(fromstr, len);
3976 if ((norm = is_an_int(from, len)) == NULL)
3977 croak("can compress only unsigned integer");
3979 New('w', result, len, char);
3983 *--in = div128(norm, &done) | 0x80;
3984 result[len - 1] &= 0x7F; /* clear continue bit */
3985 sv_catpvn(cat, in, (result + len) - in);
3987 SvREFCNT_dec(norm); /* free norm */
3989 else if (SvNOKp(fromstr)) {
3990 char buf[sizeof(double) * 2]; /* 8/7 <= 2 */
3991 char *in = buf + sizeof(buf);
3994 double next = floor(adouble / 128);
3995 *--in = (unsigned char)(adouble - (next * 128)) | 0x80;
3996 if (--in < buf) /* this cannot happen ;-) */
3997 croak ("Cannot compress integer");
3999 } while (adouble > 0);
4000 buf[sizeof(buf) - 1] &= 0x7f; /* clear continue bit */
4001 sv_catpvn(cat, in, (buf + sizeof(buf)) - in);
4004 croak("Cannot compress non integer");
4010 aint = SvIV(fromstr);
4011 sv_catpvn(cat, (char*)&aint, sizeof(int));
4017 aulong = SvUV(fromstr);
4019 aulong = PerlSock_htonl(aulong);
4021 CAT32(cat, &aulong);
4027 aulong = SvUV(fromstr);
4029 aulong = htovl(aulong);
4031 CAT32(cat, &aulong);
4037 aulong = SvUV(fromstr);
4038 CAT32(cat, &aulong);
4044 along = SvIV(fromstr);
4052 auquad = (unsigned Quad_t)SvIV(fromstr);
4053 sv_catpvn(cat, (char*)&auquad, sizeof(unsigned Quad_t));
4059 aquad = (Quad_t)SvIV(fromstr);
4060 sv_catpvn(cat, (char*)&aquad, sizeof(Quad_t));
4063 #endif /* HAS_QUAD */
4065 len = 1; /* assume SV is correct length */
4070 if (fromstr == &sv_undef)
4073 /* XXX better yet, could spirit away the string to
4074 * a safe spot and hang on to it until the result
4075 * of pack() (and all copies of the result) are
4078 if (dowarn && (SvTEMP(fromstr) || SvPADTMP(fromstr)))
4079 warn("Attempt to pack pointer to temporary value");
4080 if (SvPOK(fromstr) || SvNIOK(fromstr))
4081 aptr = SvPV(fromstr,na);
4083 aptr = SvPV_force(fromstr,na);
4085 sv_catpvn(cat, (char*)&aptr, sizeof(char*));
4090 aptr = SvPV(fromstr, fromlen);
4091 SvGROW(cat, fromlen * 4 / 3);
4096 while (fromlen > 0) {
4103 doencodes(cat, aptr, todo);
4122 register I32 limit = POPi; /* note, negative is forever */
4125 register char *s = SvPV(sv, len);
4126 char *strend = s + len;
4128 register REGEXP *rx;
4132 I32 maxiters = (strend - s) + 10;
4135 I32 origlimit = limit;
4138 AV *oldstack = curstack;
4139 I32 gimme = GIMME_V;
4140 I32 oldsave = savestack_ix;
4141 I32 make_mortal = 1;
4142 MAGIC *mg = (MAGIC *) NULL;
4145 Copy(&LvTARGOFF(POPs), &pm, 1, PMOP*);
4150 DIE("panic: do_split");
4151 rx = pm->op_pmregexp;
4153 TAINT_IF((pm->op_pmflags & PMf_LOCALE) &&
4154 (pm->op_pmflags & (PMf_WHITE | PMf_SKIPWHITE)));
4156 if (pm->op_pmreplroot)
4157 ary = GvAVn((GV*)pm->op_pmreplroot);
4158 else if (gimme != G_ARRAY)
4160 ary = (AV*)curpad[0];
4163 #endif /* USE_THREADS */
4166 if (ary && (gimme != G_ARRAY || (pm->op_pmflags & PMf_ONCE))) {
4172 if (SvRMAGICAL(ary) && (mg = mg_find((SV *) ary, 'P'))) {
4179 for (i = AvFILLp(ary); i >= 0; i--)
4180 AvARRAY(ary)[i] = &sv_undef; /* don't free mere refs */
4182 /* temporarily switch stacks */
4183 SWITCHSTACK(curstack, ary);
4187 base = SP - stack_base;
4189 if (pm->op_pmflags & PMf_SKIPWHITE) {
4190 if (pm->op_pmflags & PMf_LOCALE) {
4191 while (isSPACE_LC(*s))
4199 if (pm->op_pmflags & (PMf_MULTILINE|PMf_SINGLELINE)) {
4201 multiline = pm->op_pmflags & PMf_MULTILINE;
4205 limit = maxiters + 2;
4206 if (pm->op_pmflags & PMf_WHITE) {
4209 while (m < strend &&
4210 !((pm->op_pmflags & PMf_LOCALE)
4211 ? isSPACE_LC(*m) : isSPACE(*m)))
4216 dstr = NEWSV(30, m-s);
4217 sv_setpvn(dstr, s, m-s);
4223 while (s < strend &&
4224 ((pm->op_pmflags & PMf_LOCALE)
4225 ? isSPACE_LC(*s) : isSPACE(*s)))
4229 else if (strEQ("^", rx->precomp)) {
4232 for (m = s; m < strend && *m != '\n'; m++) ;
4236 dstr = NEWSV(30, m-s);
4237 sv_setpvn(dstr, s, m-s);
4244 else if (rx->check_substr && !rx->nparens
4245 && (rx->reganch & ROPT_CHECK_ALL)
4246 && !(rx->reganch & ROPT_ANCH)) {
4247 i = SvCUR(rx->check_substr);
4248 if (i == 1 && !SvTAIL(rx->check_substr)) {
4249 i = *SvPVX(rx->check_substr);
4252 for (m = s; m < strend && *m != i; m++) ;
4255 dstr = NEWSV(30, m-s);
4256 sv_setpvn(dstr, s, m-s);
4265 while (s < strend && --limit &&
4266 (m=fbm_instr((unsigned char*)s, (unsigned char*)strend,
4267 rx->check_substr)) )
4270 dstr = NEWSV(31, m-s);
4271 sv_setpvn(dstr, s, m-s);
4280 maxiters += (strend - s) * rx->nparens;
4281 while (s < strend && --limit &&
4282 regexec_flags(rx, s, strend, orig, 1, Nullsv, NULL, 0))
4284 TAINT_IF(RX_MATCH_TAINTED(rx));
4286 && rx->subbase != orig) {
4291 strend = s + (strend - m);
4294 dstr = NEWSV(32, m-s);
4295 sv_setpvn(dstr, s, m-s);
4300 for (i = 1; i <= rx->nparens; i++) {
4304 dstr = NEWSV(33, m-s);
4305 sv_setpvn(dstr, s, m-s);
4308 dstr = NEWSV(33, 0);
4318 LEAVE_SCOPE(oldsave);
4319 iters = (SP - stack_base) - base;
4320 if (iters > maxiters)
4323 /* keep field after final delim? */
4324 if (s < strend || (iters && origlimit)) {
4325 dstr = NEWSV(34, strend-s);
4326 sv_setpvn(dstr, s, strend-s);
4332 else if (!origlimit) {
4333 while (iters > 0 && (!TOPs || !SvANY(TOPs) || SvCUR(TOPs) == 0))
4339 SWITCHSTACK(ary, oldstack);
4340 if (SvSMAGICAL(ary)) {
4345 if (gimme == G_ARRAY) {
4347 Copy(AvARRAY(ary), SP + 1, iters, SV*);
4355 perl_call_method("PUSH",G_SCALAR|G_DISCARD);
4358 if (gimme == G_ARRAY) {
4359 /* EXTEND should not be needed - we just popped them */
4361 for (i=0; i < iters; i++) {
4362 SV **svp = av_fetch(ary, i, FALSE);
4363 PUSHs((svp) ? *svp : &sv_undef);
4370 if (gimme == G_ARRAY)
4373 if (iters || !pm->op_pmreplroot) {
4383 unlock_condpair(void *svv)
4386 MAGIC *mg = mg_find((SV*)svv, 'm');
4389 croak("panic: unlock_condpair unlocking non-mutex");
4390 MUTEX_LOCK(MgMUTEXP(mg));
4391 if (MgOWNER(mg) != thr)
4392 croak("panic: unlock_condpair unlocking mutex that we don't own");
4394 COND_SIGNAL(MgOWNERCONDP(mg));
4395 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "0x%lx: unlock 0x%lx\n",
4396 (unsigned long)thr, (unsigned long)svv);)
4397 MUTEX_UNLOCK(MgMUTEXP(mg));
4399 #endif /* USE_THREADS */
4412 mg = condpair_magic(sv);
4413 MUTEX_LOCK(MgMUTEXP(mg));
4414 if (MgOWNER(mg) == thr)
4415 MUTEX_UNLOCK(MgMUTEXP(mg));
4418 COND_WAIT(MgOWNERCONDP(mg), MgMUTEXP(mg));
4420 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "0x%lx: pp_lock lock 0x%lx\n",
4421 (unsigned long)thr, (unsigned long)sv);)
4422 MUTEX_UNLOCK(MgMUTEXP(mg));
4423 SvREFCNT_inc(sv); /* keep alive until magic_mutexfree */
4424 save_destructor(unlock_condpair, sv);
4426 #endif /* USE_THREADS */
4427 if (SvTYPE(retsv) == SVt_PVAV || SvTYPE(retsv) == SVt_PVHV
4428 || SvTYPE(retsv) == SVt_PVCV) {
4429 retsv = refto(retsv);
4440 if (op->op_private & OPpLVAL_INTRO)
4441 PUSHs(*save_threadsv(op->op_targ));
4443 PUSHs(THREADSV(op->op_targ));
4446 DIE("tried to access per-thread data in non-threaded perl");
4447 #endif /* USE_THREADS */