3 * Copyright (c) 1991-1997, Larry Wall
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
11 * "It's a big house this, and very peculiar. Always a bit more to discover,
12 * and no knowing what you'll find around a corner. And Elves, sir!" --Samwise
19 * The compiler on Concurrent CX/UX systems has a subtle bug which only
20 * seems to show up when compiling pp.c - it generates the wrong double
21 * precision constant value for (double)UV_MAX when used inline in the body
22 * of the code below, so this makes a static variable up front (which the
23 * compiler seems to get correct) and uses it in place of UV_MAX below.
25 #ifdef CXUX_BROKEN_CONSTANT_CONVERT
26 static double UV_MAX_cxux = ((double)UV_MAX);
30 * Types used in bitwise operations.
32 * Normally we'd just use IV and UV. However, some hardware and
33 * software combinations (e.g. Alpha and current OSF/1) don't have a
34 * floating-point type to use for NV that has adequate bits to fully
35 * hold an IV/UV. (In other words, sizeof(long) == sizeof(double).)
37 * It just so happens that "int" is the right size almost everywhere.
43 * Mask used after bitwise operations.
45 * There is at least one realm (Cray word machines) that doesn't
46 * have an integral type (except char) small enough to be represented
47 * in a double without loss; that is, it has no 32-bit type.
49 #if BYTEORDER > 0xFFFF && defined(_CRAY) && !defined(_CRAYMPP)
51 # define BW_MASK ((1 << BW_BITS) - 1)
52 # define BW_SIGN (1 << (BW_BITS - 1))
53 # define BWi(i) (((i) & BW_SIGN) ? ((i) | ~BW_MASK) : ((i) & BW_MASK))
54 # define BWu(u) ((u) & BW_MASK)
61 * Offset for integer pack/unpack.
63 * On architectures where I16 and I32 aren't really 16 and 32 bits,
64 * which for now are all Crays, pack and unpack have to play games.
68 * These values are required for portability of pack() output.
69 * If they're not right on your machine, then pack() and unpack()
70 * wouldn't work right anyway; you'll need to apply the Cray hack.
71 * (I'd like to check them with #if, but you can't use sizeof() in
77 #if BYTEORDER > 0xFFFF && defined(_CRAY) && !defined(_CRAYMPP)
78 # if BYTEORDER == 0x12345678
79 # define OFF16(p) (char*)(p)
80 # define OFF32(p) (char*)(p)
82 # if BYTEORDER == 0x87654321
83 # define OFF16(p) ((char*)(p) + (sizeof(U16) - SIZE16))
84 # define OFF32(p) ((char*)(p) + (sizeof(U32) - SIZE32))
86 }}}} bad cray byte order
89 # define COPY16(s,p) (*(p) = 0, Copy(s, OFF16(p), SIZE16, char))
90 # define COPY32(s,p) (*(p) = 0, Copy(s, OFF32(p), SIZE32, char))
91 # define CAT16(sv,p) sv_catpvn(sv, OFF16(p), SIZE16)
92 # define CAT32(sv,p) sv_catpvn(sv, OFF32(p), SIZE32)
94 # define COPY16(s,p) Copy(s, p, SIZE16, char)
95 # define COPY32(s,p) Copy(s, p, SIZE32, char)
96 # define CAT16(sv,p) sv_catpvn(sv, (char*)(p), SIZE16)
97 # define CAT32(sv,p) sv_catpvn(sv, (char*)(p), SIZE32)
101 static void doencodes _((SV* sv, char* s, I32 len));
102 static SV* refto _((SV* sv));
103 static U32 seed _((void));
106 static bool srand_called = FALSE;
108 /* variations on pp_null */
110 #ifdef DONT_DECLARE_STD
115 extern pid_t getpid (void);
121 if (GIMME_V == G_SCALAR)
136 if (op->op_private & OPpLVAL_INTRO)
137 SAVECLEARSV(curpad[op->op_targ]);
139 if (op->op_flags & OPf_REF) {
143 if (GIMME == G_ARRAY) {
144 I32 maxarg = AvFILL((AV*)TARG) + 1;
146 if (SvMAGICAL(TARG)) {
148 for (i=0; i < maxarg; i++) {
149 SV **svp = av_fetch((AV*)TARG, i, FALSE);
150 SP[i+1] = (svp) ? *svp : &sv_undef;
154 Copy(AvARRAY((AV*)TARG), SP+1, maxarg, SV*);
159 SV* sv = sv_newmortal();
160 I32 maxarg = AvFILL((AV*)TARG) + 1;
161 sv_setiv(sv, maxarg);
173 if (op->op_private & OPpLVAL_INTRO)
174 SAVECLEARSV(curpad[op->op_targ]);
175 if (op->op_flags & OPf_REF)
178 if (gimme == G_ARRAY) {
179 RETURNOP(do_kv(ARGS));
181 else if (gimme == G_SCALAR) {
182 SV* sv = sv_newmortal();
183 if (HvFILL((HV*)TARG))
184 sv_setpvf(sv, "%ld/%ld",
185 (long)HvFILL((HV*)TARG), (long)HvMAX((HV*)TARG) + 1);
195 DIE("NOT IMPL LINE %d",__LINE__);
207 if (SvTYPE(sv) == SVt_PVIO) {
208 GV *gv = (GV*) sv_newmortal();
209 gv_init(gv, 0, "", 0, 0);
210 GvIOp(gv) = (IO *)sv;
211 (void)SvREFCNT_inc(sv);
213 } else if (SvTYPE(sv) != SVt_PVGV)
214 DIE("Not a GLOB reference");
217 if (SvTYPE(sv) != SVt_PVGV) {
220 if (SvGMAGICAL(sv)) {
226 if (op->op_flags & OPf_REF ||
227 op->op_private & HINT_STRICT_REFS)
228 DIE(no_usym, "a symbol");
234 if (op->op_private & HINT_STRICT_REFS)
235 DIE(no_symref, sym, "a symbol");
236 sv = (SV*)gv_fetchpv(sym, TRUE, SVt_PVGV);
239 if (op->op_private & OPpLVAL_INTRO)
240 save_gp((GV*)sv, !(op->op_flags & OPf_SPECIAL));
252 switch (SvTYPE(sv)) {
256 DIE("Not a SCALAR reference");
263 if (SvTYPE(gv) != SVt_PVGV) {
264 if (SvGMAGICAL(sv)) {
270 if (op->op_flags & OPf_REF ||
271 op->op_private & HINT_STRICT_REFS)
272 DIE(no_usym, "a SCALAR");
278 if (op->op_private & HINT_STRICT_REFS)
279 DIE(no_symref, sym, "a SCALAR");
280 gv = (GV*)gv_fetchpv(sym, TRUE, SVt_PV);
284 if (op->op_flags & OPf_MOD) {
285 if (op->op_private & OPpLVAL_INTRO)
286 sv = save_scalar((GV*)TOPs);
287 else if (op->op_private & OPpDEREF)
288 vivify_ref(sv, op->op_private & OPpDEREF);
298 SV *sv = AvARYLEN(av);
300 AvARYLEN(av) = sv = NEWSV(0,0);
301 sv_upgrade(sv, SVt_IV);
302 sv_magic(sv, (SV*)av, '#', Nullch, 0);
310 djSP; dTARGET; dPOPss;
312 if (op->op_flags & OPf_MOD) {
313 if (SvTYPE(TARG) < SVt_PVLV) {
314 sv_upgrade(TARG, SVt_PVLV);
315 sv_magic(TARG, Nullsv, '.', Nullch, 0);
320 PUSHs(TARG); /* no SvSETMAGIC */
326 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
327 mg = mg_find(sv, 'g');
328 if (mg && mg->mg_len >= 0) {
329 PUSHi(mg->mg_len + curcop->cop_arybase);
343 /* We usually try to add a non-existent subroutine in case of AUTOLOAD. */
344 /* (But not in defined().) */
345 CV *cv = sv_2cv(TOPs, &stash, &gv, !(op->op_flags & OPf_SPECIAL));
348 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
365 if (SvPOK(TOPs) && SvCUR(TOPs) >= 7) {
366 char *s = SvPVX(TOPs);
367 if (strnEQ(s, "CORE::", 6)) {
370 code = keyword(s + 6, SvCUR(TOPs) - 6);
371 if (code < 0) { /* Overridable. */
372 #define MAX_ARGS_OP ((sizeof(I32) - 1) * 2)
373 int i = 0, n = 0, seen_question = 0;
375 char str[ MAX_ARGS_OP * 2 + 2 ]; /* One ';', one '\0' */
377 while (i < MAXO) { /* The slow way. */
378 if (strEQ(s + 6, op_name[i]) || strEQ(s + 6, op_desc[i]))
382 goto nonesuch; /* Should not happen... */
384 oa = opargs[i] >> OASHIFT;
386 if (oa & OA_OPTIONAL) {
389 } else if (seen_question)
390 goto set; /* XXXX system, exec */
391 if ((oa & (OA_OPTIONAL - 1)) >= OA_AVREF
392 && (oa & (OA_OPTIONAL - 1)) <= OA_HVREF) {
395 /* What to do with R ((un)tie, tied, (sys)read, recv)? */
396 str[n++] = ("?$@@%&*$")[oa & (OA_OPTIONAL - 1)];
400 ret = sv_2mortal(newSVpv(str, n - 1));
401 } else if (code) /* Non-Overridable */
403 else { /* None such */
405 croak("Cannot find an opnumber for \"%s\"", s+6);
409 cv = sv_2cv(TOPs, &stash, &gv, FALSE);
411 ret = sv_2mortal(newSVpv(SvPVX(cv), SvCUR(cv)));
420 CV* cv = (CV*)curpad[op->op_targ];
422 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
438 if (GIMME != G_ARRAY) {
442 EXTEND_MORTAL(SP - MARK);
444 *MARK = refto(*MARK);
453 if (SvTYPE(sv) == SVt_PVLV && LvTYPE(sv) == 'y') {
456 if (!(sv = LvTARG(sv)))
459 else if (SvPADTMP(sv))
463 (void)SvREFCNT_inc(sv);
466 sv_upgrade(rv, SVt_RV);
480 if (sv && SvGMAGICAL(sv))
483 if (!sv || !SvROK(sv))
487 pv = sv_reftype(sv,TRUE);
488 PUSHp(pv, strlen(pv));
498 stash = curcop->cop_stash;
500 stash = gv_stashsv(POPs, TRUE);
502 (void)sv_bless(TOPs, stash);
519 switch (elem ? *elem : '\0')
522 if (strEQ(elem, "ARRAY"))
523 tmpRef = (SV*)GvAV(gv);
526 if (strEQ(elem, "CODE"))
527 tmpRef = (SV*)GvCVu(gv);
530 if (strEQ(elem, "FILEHANDLE")) /* XXX deprecate in 5.005 */
531 tmpRef = (SV*)GvIOp(gv);
534 if (strEQ(elem, "GLOB"))
538 if (strEQ(elem, "HASH"))
539 tmpRef = (SV*)GvHV(gv);
542 if (strEQ(elem, "IO"))
543 tmpRef = (SV*)GvIOp(gv);
546 if (strEQ(elem, "NAME"))
547 sv = newSVpv(GvNAME(gv), GvNAMELEN(gv));
550 if (strEQ(elem, "PACKAGE"))
551 sv = newSVpv(HvNAME(GvSTASH(gv)), 0);
554 if (strEQ(elem, "SCALAR"))
568 /* Pattern matching */
573 register UNOP *unop = cUNOP;
574 register unsigned char *s;
577 register I32 *sfirst;
581 if(unop->op_first && unop->op_first->op_type == OP_PUSHRE) {
582 PMOP *pm = (PMOP *)unop->op_first;
583 SV *rv = sv_newmortal();
584 sv = newSVrv(rv, "Regexp");
585 sv_magic(sv,(SV*)ReREFCNT_inc(pm->op_pmregexp),'r',0,0);
589 if (sv == lastscream) {
595 SvSCREAM_off(lastscream);
596 SvREFCNT_dec(lastscream);
598 lastscream = SvREFCNT_inc(sv);
601 s = (unsigned char*)(SvPV(sv, len));
605 if (pos > maxscream) {
607 maxscream = pos + 80;
608 New(301, screamfirst, 256, I32);
609 New(302, screamnext, maxscream, I32);
612 maxscream = pos + pos / 4;
613 Renew(screamnext, maxscream, I32);
617 sfirst = screamfirst;
620 if (!sfirst || !snext)
621 DIE("do_study: out of memory");
623 for (ch = 256; ch; --ch)
630 snext[pos] = sfirst[ch] - pos;
637 sv_magic(sv, Nullsv, 'g', Nullch, 0); /* piggyback on m//g magic */
646 if (op->op_flags & OPf_STACKED)
652 TARG = sv_newmortal();
653 PUSHi(do_trans(sv, op));
657 /* Lvalue operators. */
669 djSP; dMARK; dTARGET;
679 SETi(do_chomp(TOPs));
685 djSP; dMARK; dTARGET;
686 register I32 count = 0;
689 count += do_chomp(POPs);
700 if (!sv || !SvANY(sv))
702 switch (SvTYPE(sv)) {
704 if (AvMAX(sv) >= 0 || SvGMAGICAL(sv))
708 if (HvARRAY(sv) || SvGMAGICAL(sv))
712 if (CvROOT(sv) || CvXSUB(sv))
729 if (!op->op_private) {
738 if (SvTHINKFIRST(sv)) {
745 switch (SvTYPE(sv)) {
755 if (cv_const_sv((CV*)sv))
756 warn("Constant subroutine %s undefined",
757 CvANON((CV*)sv) ? "(anonymous)" : GvENAME(CvGV((CV*)sv)));
760 { GV* gv = (GV*)SvREFCNT_inc(CvGV((CV*)sv));
762 CvGV((CV*)sv) = gv; } /* let user-undef'd sub keep its identity */
766 sv_setsv(sv, &sv_undef);
769 if (SvTYPE(sv) >= SVt_PV && SvPVX(sv) && SvLEN(sv)) {
772 SvPV_set(sv, Nullch);
785 if (SvREADONLY(TOPs) || SvTYPE(TOPs) > SVt_PVLV)
787 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) &&
788 SvIVX(TOPs) != IV_MIN)
791 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
802 if (SvREADONLY(TOPs) || SvTYPE(TOPs) > SVt_PVLV)
804 sv_setsv(TARG, TOPs);
805 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) &&
806 SvIVX(TOPs) != IV_MAX)
809 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
823 if(SvREADONLY(TOPs) || SvTYPE(TOPs) > SVt_PVLV)
825 sv_setsv(TARG, TOPs);
826 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) &&
827 SvIVX(TOPs) != IV_MIN)
830 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
839 /* Ordinary operators. */
843 djSP; dATARGET; tryAMAGICbin(pow,opASSIGN);
846 SETn( pow( left, right) );
853 djSP; dATARGET; tryAMAGICbin(mult,opASSIGN);
856 SETn( left * right );
863 djSP; dATARGET; tryAMAGICbin(div,opASSIGN);
868 DIE("Illegal division by zero");
870 /* insure that 20./5. == 4. */
873 if ((double)I_V(left) == left &&
874 (double)I_V(right) == right &&
875 (k = I_V(left)/I_V(right))*I_V(right) == I_V(left)) {
878 value = left / right;
882 value = left / right;
891 djSP; dATARGET; tryAMAGICbin(modulo,opASSIGN);
899 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)) {
901 right = (right_neg = (i < 0)) ? -i : i;
905 right = U_V((right_neg = (n < 0)) ? -n : n);
908 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)) {
910 left = (left_neg = (i < 0)) ? -i : i;
914 left = U_V((left_neg = (n < 0)) ? -n : n);
918 DIE("Illegal modulus zero");
921 if ((left_neg != right_neg) && ans)
924 /* XXX may warn: unary minus operator applied to unsigned type */
925 /* could change -foo to be (~foo)+1 instead */
926 if (ans <= ~((UV)IV_MAX)+1)
927 sv_setiv(TARG, ~ans+1);
929 sv_setnv(TARG, -(double)ans);
940 djSP; dATARGET; tryAMAGICbin(repeat,opASSIGN);
942 register I32 count = POPi;
943 if (GIMME == G_ARRAY && op->op_private & OPpREPEAT_DOLIST) {
945 I32 items = SP - MARK;
957 repeatcpy((char*)(MARK + items), (char*)MARK,
958 items * sizeof(SV*), count - 1);
964 else { /* Note: mark already snarfed by pp_list */
969 if (TARG == tmpstr && SvTHINKFIRST(tmpstr)) {
970 if (SvREADONLY(tmpstr) && curcop != &compiling)
971 DIE("Can't x= to readonly value");
975 SvSetSV(TARG, tmpstr);
976 SvPV_force(TARG, len);
981 SvGROW(TARG, (count * len) + 1);
982 repeatcpy(SvPVX(TARG) + len, SvPVX(TARG), len, count - 1);
983 SvCUR(TARG) *= count;
987 (void)SvPOK_only(TARG);
996 djSP; dATARGET; tryAMAGICbin(subtr,opASSIGN);
999 SETn( left - right );
1006 djSP; dATARGET; tryAMAGICbin(lshift,opASSIGN);
1009 if (op->op_private & HINT_INTEGER) {
1011 i = BWi(i) << shift;
1025 djSP; dATARGET; tryAMAGICbin(rshift,opASSIGN);
1028 if (op->op_private & HINT_INTEGER) {
1030 i = BWi(i) >> shift;
1044 djSP; tryAMAGICbinSET(lt,0);
1047 SETs(boolSV(TOPn < value));
1054 djSP; tryAMAGICbinSET(gt,0);
1057 SETs(boolSV(TOPn > value));
1064 djSP; tryAMAGICbinSET(le,0);
1067 SETs(boolSV(TOPn <= value));
1074 djSP; tryAMAGICbinSET(ge,0);
1077 SETs(boolSV(TOPn >= value));
1084 djSP; tryAMAGICbinSET(ne,0);
1087 SETs(boolSV(TOPn != value));
1094 djSP; dTARGET; tryAMAGICbin(ncmp,0);
1101 else if (left < right)
1103 else if (left > right)
1116 djSP; tryAMAGICbinSET(slt,0);
1119 int cmp = ((op->op_private & OPpLOCALE)
1120 ? sv_cmp_locale(left, right)
1121 : sv_cmp(left, right));
1122 SETs(boolSV(cmp < 0));
1129 djSP; tryAMAGICbinSET(sgt,0);
1132 int cmp = ((op->op_private & OPpLOCALE)
1133 ? sv_cmp_locale(left, right)
1134 : sv_cmp(left, right));
1135 SETs(boolSV(cmp > 0));
1142 djSP; tryAMAGICbinSET(sle,0);
1145 int cmp = ((op->op_private & OPpLOCALE)
1146 ? sv_cmp_locale(left, right)
1147 : sv_cmp(left, right));
1148 SETs(boolSV(cmp <= 0));
1155 djSP; tryAMAGICbinSET(sge,0);
1158 int cmp = ((op->op_private & OPpLOCALE)
1159 ? sv_cmp_locale(left, right)
1160 : sv_cmp(left, right));
1161 SETs(boolSV(cmp >= 0));
1168 djSP; tryAMAGICbinSET(seq,0);
1171 SETs(boolSV(sv_eq(left, right)));
1178 djSP; tryAMAGICbinSET(sne,0);
1181 SETs(boolSV(!sv_eq(left, right)));
1188 djSP; dTARGET; tryAMAGICbin(scmp,0);
1191 int cmp = ((op->op_private & OPpLOCALE)
1192 ? sv_cmp_locale(left, right)
1193 : sv_cmp(left, right));
1201 djSP; dATARGET; tryAMAGICbin(band,opASSIGN);
1204 if (SvNIOKp(left) || SvNIOKp(right)) {
1205 if (op->op_private & HINT_INTEGER) {
1206 IBW value = SvIV(left) & SvIV(right);
1210 UBW value = SvUV(left) & SvUV(right);
1215 do_vop(op->op_type, TARG, left, right);
1224 djSP; dATARGET; tryAMAGICbin(bxor,opASSIGN);
1227 if (SvNIOKp(left) || SvNIOKp(right)) {
1228 if (op->op_private & HINT_INTEGER) {
1229 IBW value = (USE_LEFT(left) ? SvIV(left) : 0) ^ SvIV(right);
1233 UBW value = (USE_LEFT(left) ? SvUV(left) : 0) ^ SvUV(right);
1238 do_vop(op->op_type, TARG, left, right);
1247 djSP; dATARGET; tryAMAGICbin(bor,opASSIGN);
1250 if (SvNIOKp(left) || SvNIOKp(right)) {
1251 if (op->op_private & HINT_INTEGER) {
1252 IBW value = (USE_LEFT(left) ? SvIV(left) : 0) | SvIV(right);
1256 UBW value = (USE_LEFT(left) ? SvUV(left) : 0) | SvUV(right);
1261 do_vop(op->op_type, TARG, left, right);
1270 djSP; dTARGET; tryAMAGICun(neg);
1275 if (SvIOKp(sv) && !SvNOKp(sv) && !SvPOKp(sv) && SvIVX(sv) != IV_MIN)
1277 else if (SvNIOKp(sv))
1279 else if (SvPOKp(sv)) {
1281 char *s = SvPV(sv, len);
1282 if (isIDFIRST(*s)) {
1283 sv_setpvn(TARG, "-", 1);
1286 else if (*s == '+' || *s == '-') {
1288 *SvPV_force(TARG, len) = *s == '-' ? '+' : '-';
1291 sv_setnv(TARG, -SvNV(sv));
1303 djSP; tryAMAGICunSET(not);
1304 #endif /* OVERLOAD */
1305 *stack_sp = boolSV(!SvTRUE(*stack_sp));
1311 djSP; dTARGET; tryAMAGICun(compl);
1315 if (op->op_private & HINT_INTEGER) {
1316 IBW value = ~SvIV(sv);
1320 UBW value = ~SvUV(sv);
1325 register char *tmps;
1326 register long *tmpl;
1331 tmps = SvPV_force(TARG, len);
1334 for ( ; anum && (unsigned long)tmps % sizeof(long); anum--, tmps++)
1337 for ( ; anum >= sizeof(long); anum -= sizeof(long), tmpl++)
1341 for ( ; anum > 0; anum--, tmps++)
1350 /* integer versions of some of the above */
1354 djSP; dATARGET; tryAMAGICbin(mult,opASSIGN);
1357 SETi( left * right );
1364 djSP; dATARGET; tryAMAGICbin(div,opASSIGN);
1368 DIE("Illegal division by zero");
1369 value = POPi / value;
1377 djSP; dATARGET; tryAMAGICbin(modulo,opASSIGN);
1381 DIE("Illegal modulus zero");
1382 SETi( left % right );
1389 djSP; dATARGET; tryAMAGICbin(add,opASSIGN);
1392 SETi( left + right );
1399 djSP; dATARGET; tryAMAGICbin(subtr,opASSIGN);
1402 SETi( left - right );
1409 djSP; tryAMAGICbinSET(lt,0);
1412 SETs(boolSV(left < right));
1419 djSP; tryAMAGICbinSET(gt,0);
1422 SETs(boolSV(left > right));
1429 djSP; tryAMAGICbinSET(le,0);
1432 SETs(boolSV(left <= right));
1439 djSP; tryAMAGICbinSET(ge,0);
1442 SETs(boolSV(left >= right));
1449 djSP; tryAMAGICbinSET(eq,0);
1452 SETs(boolSV(left == right));
1459 djSP; tryAMAGICbinSET(ne,0);
1462 SETs(boolSV(left != right));
1469 djSP; dTARGET; tryAMAGICbin(ncmp,0);
1476 else if (left < right)
1487 djSP; dTARGET; tryAMAGICun(neg);
1492 /* High falutin' math. */
1496 djSP; dTARGET; tryAMAGICbin(atan2,0);
1499 SETn(atan2(left, right));
1506 djSP; dTARGET; tryAMAGICun(sin);
1518 djSP; dTARGET; tryAMAGICun(cos);
1538 if (!srand_called) {
1539 (void)srand((unsigned)seed());
1540 srand_called = TRUE;
1543 value = rand() * value / 2147483648.0;
1546 value = rand() * value / 65536.0;
1549 value = rand() * value / 32768.0;
1551 value = rand() * value / (double)(((unsigned long)1) << RANDBITS);
1567 (void)srand((unsigned)anum);
1568 srand_called = TRUE;
1577 * This is really just a quick hack which grabs various garbage
1578 * values. It really should be a real hash algorithm which
1579 * spreads the effect of every input bit onto every output bit,
1580 * if someone who knows about such tings would bother to write it.
1581 * Might be a good idea to add that function to CORE as well.
1582 * No numbers below come from careful analysis or anyting here,
1583 * except they are primes and SEED_C1 > 1E6 to get a full-width
1584 * value from (tv_sec * SEED_C1 + tv_usec). The multipliers should
1585 * probably be bigger too.
1588 # define SEED_C1 1000003
1589 #define SEED_C4 73819
1591 # define SEED_C1 25747
1592 #define SEED_C4 20639
1596 #define SEED_C5 26107
1601 # include <starlet.h>
1602 /* when[] = (low 32 bits, high 32 bits) of time since epoch
1603 * in 100-ns units, typically incremented ever 10 ms. */
1604 unsigned int when[2];
1605 _ckvmssts(sys$gettim(when));
1606 u = (U32)SEED_C1 * when[0] + (U32)SEED_C2 * when[1];
1608 # ifdef HAS_GETTIMEOFDAY
1609 struct timeval when;
1610 gettimeofday(&when,(struct timezone *) 0);
1611 u = (U32)SEED_C1 * when.tv_sec + (U32)SEED_C2 * when.tv_usec;
1615 u = (U32)SEED_C1 * when;
1618 u += SEED_C3 * (U32)getpid();
1619 u += SEED_C4 * (U32)(UV)stack_sp;
1620 #ifndef PLAN9 /* XXX Plan9 assembler chokes on this; fix needed */
1621 u += SEED_C5 * (U32)(UV)&when;
1628 djSP; dTARGET; tryAMAGICun(exp);
1640 djSP; dTARGET; tryAMAGICun(log);
1645 SET_NUMERIC_STANDARD();
1646 DIE("Can't take log of %g", value);
1656 djSP; dTARGET; tryAMAGICun(sqrt);
1661 SET_NUMERIC_STANDARD();
1662 DIE("Can't take sqrt of %g", value);
1664 value = sqrt(value);
1674 double value = TOPn;
1677 if (SvIOKp(TOPs) && !SvNOKp(TOPs) && !SvPOKp(TOPs)) {
1683 (void)modf(value, &value);
1685 (void)modf(-value, &value);
1700 djSP; dTARGET; tryAMAGICun(abs);
1702 double value = TOPn;
1705 if (SvIOKp(TOPs) && !SvNOKp(TOPs) && !SvPOKp(TOPs) &&
1706 (iv = SvIVX(TOPs)) != IV_MIN) {
1727 XPUSHu(scan_hex(tmps, 99, &argtype));
1739 while (*tmps && isSPACE(*tmps))
1744 value = scan_hex(++tmps, 99, &argtype);
1746 value = scan_oct(tmps, 99, &argtype);
1756 SETi( sv_len(TOPs) );
1769 I32 lvalue = op->op_flags & OPf_MOD;
1771 I32 arybase = curcop->cop_arybase;
1777 tmps = SvPV(sv, curlen);
1778 if (pos >= arybase) {
1796 else if (len >= 0) {
1798 if (rem > (I32)curlen)
1812 if (dowarn || lvalue)
1813 warn("substr outside of string");
1818 sv_setpvn(TARG, tmps, rem);
1819 if (lvalue) { /* it's an lvalue! */
1820 if (!SvGMAGICAL(sv)) {
1824 warn("Attempt to use reference as lvalue in substr");
1826 if (SvOK(sv)) /* is it defined ? */
1827 (void)SvPOK_only(sv);
1829 sv_setpvn(sv,"",0); /* avoid lexical reincarnation */
1832 if (SvTYPE(TARG) < SVt_PVLV) {
1833 sv_upgrade(TARG, SVt_PVLV);
1834 sv_magic(TARG, Nullsv, 'x', Nullch, 0);
1839 LvTARGOFF(TARG) = pos;
1840 LvTARGLEN(TARG) = rem;
1843 PUSHs(TARG); /* avoid SvSETMAGIC here */
1850 register I32 size = POPi;
1851 register I32 offset = POPi;
1852 register SV *src = POPs;
1853 I32 lvalue = op->op_flags & OPf_MOD;
1855 unsigned char *s = (unsigned char*)SvPV(src, srclen);
1856 unsigned long retnum;
1859 offset *= size; /* turn into bit offset */
1860 len = (offset + size + 7) / 8;
1861 if (offset < 0 || size < 1)
1864 if (lvalue) { /* it's an lvalue! */
1865 if (SvTYPE(TARG) < SVt_PVLV) {
1866 sv_upgrade(TARG, SVt_PVLV);
1867 sv_magic(TARG, Nullsv, 'v', Nullch, 0);
1872 LvTARGOFF(TARG) = offset;
1873 LvTARGLEN(TARG) = size;
1881 if (offset >= srclen)
1884 retnum = (unsigned long) s[offset] << 8;
1886 else if (size == 32) {
1887 if (offset >= srclen)
1889 else if (offset + 1 >= srclen)
1890 retnum = (unsigned long) s[offset] << 24;
1891 else if (offset + 2 >= srclen)
1892 retnum = ((unsigned long) s[offset] << 24) +
1893 ((unsigned long) s[offset + 1] << 16);
1895 retnum = ((unsigned long) s[offset] << 24) +
1896 ((unsigned long) s[offset + 1] << 16) +
1897 (s[offset + 2] << 8);
1902 retnum = (s[offset >> 3] >> (offset & 7)) & ((1 << size) - 1);
1907 else if (size == 16)
1908 retnum = ((unsigned long) s[offset] << 8) + s[offset+1];
1909 else if (size == 32)
1910 retnum = ((unsigned long) s[offset] << 24) +
1911 ((unsigned long) s[offset + 1] << 16) +
1912 (s[offset + 2] << 8) + s[offset+3];
1916 sv_setuv(TARG, (UV)retnum);
1931 I32 arybase = curcop->cop_arybase;
1936 offset = POPi - arybase;
1939 tmps = SvPV(big, biglen);
1942 else if (offset > biglen)
1944 if (!(tmps2 = fbm_instr((unsigned char*)tmps + offset,
1945 (unsigned char*)tmps + biglen, little)))
1946 retval = -1 + arybase;
1948 retval = tmps2 - tmps + arybase;
1965 I32 arybase = curcop->cop_arybase;
1971 tmps2 = SvPV(little, llen);
1972 tmps = SvPV(big, blen);
1976 offset = SvIV(offstr) - arybase + llen;
1979 else if (offset > blen)
1981 if (!(tmps2 = rninstr(tmps, tmps + offset,
1982 tmps2, tmps2 + llen)))
1983 retval = -1 + arybase;
1985 retval = tmps2 - tmps + arybase;
1992 djSP; dMARK; dORIGMARK; dTARGET;
1993 #ifdef USE_LOCALE_NUMERIC
1994 if (op->op_private & OPpLOCALE)
1995 SET_NUMERIC_LOCAL();
1997 SET_NUMERIC_STANDARD();
1999 do_sprintf(TARG, SP-MARK, MARK+1);
2000 TAINT_IF(SvTAINTED(TARG));
2014 value = (I32) (*tmps & 255);
2019 value = (I32) (anum & 255);
2030 (void)SvUPGRADE(TARG,SVt_PV);
2036 (void)SvPOK_only(TARG);
2043 djSP; dTARGET; dPOPTOPssrl;
2045 char *tmps = SvPV(left, na);
2047 sv_setpv(TARG, fcrypt(tmps, SvPV(right, na)));
2049 sv_setpv(TARG, crypt(tmps, SvPV(right, na)));
2053 "The crypt() function is unimplemented due to excessive paranoia.");
2065 if (!SvPADTMP(sv)) {
2071 s = SvPV_force(sv, na);
2073 if (op->op_private & OPpLOCALE) {
2076 *s = toUPPER_LC(*s);
2091 if (!SvPADTMP(sv)) {
2097 s = SvPV_force(sv, na);
2099 if (op->op_private & OPpLOCALE) {
2102 *s = toLOWER_LC(*s);
2119 if (!SvPADTMP(sv)) {
2126 s = SvPV_force(sv, len);
2128 register char *send = s + len;
2130 if (op->op_private & OPpLOCALE) {
2133 for (; s < send; s++)
2134 *s = toUPPER_LC(*s);
2137 for (; s < send; s++)
2151 if (!SvPADTMP(sv)) {
2158 s = SvPV_force(sv, len);
2160 register char *send = s + len;
2162 if (op->op_private & OPpLOCALE) {
2165 for (; s < send; s++)
2166 *s = toLOWER_LC(*s);
2169 for (; s < send; s++)
2181 register char *s = SvPV(sv,len);
2185 (void)SvUPGRADE(TARG, SVt_PV);
2186 SvGROW(TARG, (len * 2) + 1);
2194 SvCUR_set(TARG, d - SvPVX(TARG));
2195 (void)SvPOK_only(TARG);
2198 sv_setpvn(TARG, s, len);
2207 djSP; dMARK; dORIGMARK;
2209 register AV* av = (AV*)POPs;
2210 register I32 lval = op->op_flags & OPf_MOD;
2211 I32 arybase = curcop->cop_arybase;
2214 if (SvTYPE(av) == SVt_PVAV) {
2215 if (lval && op->op_private & OPpLVAL_INTRO) {
2217 for (svp = mark + 1; svp <= sp; svp++) {
2222 if (max > AvMAX(av))
2225 while (++MARK <= SP) {
2226 elem = SvIVx(*MARK);
2230 svp = av_fetch(av, elem, lval);
2232 if (!svp || *svp == &sv_undef)
2233 DIE(no_aelem, elem);
2234 if (op->op_private & OPpLVAL_INTRO)
2237 *MARK = svp ? *svp : &sv_undef;
2240 if (GIMME != G_ARRAY) {
2248 /* Associative arrays. */
2253 HV *hash = (HV*)POPs;
2255 I32 gimme = GIMME_V;
2256 I32 realhv = (SvTYPE(hash) == SVt_PVHV);
2259 /* might clobber stack_sp */
2260 entry = realhv ? hv_iternext(hash) : avhv_iternext((AV*)hash);
2265 PUSHs(hv_iterkeysv(entry)); /* won't clobber stack_sp */
2266 if (gimme == G_ARRAY) {
2268 /* might clobber stack_sp */
2269 sv_setsv(TARG, realhv ?
2270 hv_iterval(hash, entry) : avhv_iterval((AV*)hash, entry));
2275 else if (gimme == G_SCALAR)
2294 I32 gimme = GIMME_V;
2295 I32 discard = (gimme == G_VOID) ? G_DISCARD : 0;
2299 if (op->op_private & OPpSLICE) {
2303 hvtype = SvTYPE(hv);
2304 while (++MARK <= SP) {
2305 if (hvtype == SVt_PVHV)
2306 sv = hv_delete_ent(hv, *MARK, discard, 0);
2307 else if (hvtype == SVt_PVAV)
2308 sv = avhv_delete_ent((AV*)hv, *MARK, discard, 0);
2310 DIE("Not a HASH reference");
2311 *MARK = sv ? sv : &sv_undef;
2315 else if (gimme == G_SCALAR) {
2324 if (SvTYPE(hv) == SVt_PVHV)
2325 sv = hv_delete_ent(hv, keysv, discard, 0);
2326 else if (SvTYPE(hv) == SVt_PVAV)
2327 sv = avhv_delete_ent((AV*)hv, keysv, discard, 0);
2329 DIE("Not a HASH reference");
2343 if (SvTYPE(hv) == SVt_PVHV) {
2344 if (hv_exists_ent(hv, tmpsv, 0))
2346 } else if (SvTYPE(hv) == SVt_PVAV) {
2347 if (avhv_exists_ent((AV*)hv, tmpsv, 0))
2350 DIE("Not a HASH reference");
2357 djSP; dMARK; dORIGMARK;
2359 register HV *hv = (HV*)POPs;
2360 register I32 lval = op->op_flags & OPf_MOD;
2361 I32 realhv = (SvTYPE(hv) == SVt_PVHV);
2363 if (realhv || SvTYPE(hv) == SVt_PVAV) {
2364 while (++MARK <= SP) {
2368 he = hv_fetch_ent(hv, keysv, lval, 0);
2369 svp = he ? &HeVAL(he) : 0;
2371 svp = avhv_fetch_ent((AV*)hv, keysv, lval, 0);
2374 if (!he || HeVAL(he) == &sv_undef)
2375 DIE(no_helem, SvPV(keysv, na));
2376 if (op->op_private & OPpLVAL_INTRO)
2377 save_svref(&HeVAL(he));
2379 *MARK = he ? HeVAL(he) : &sv_undef;
2382 if (GIMME != G_ARRAY) {
2390 /* List operators. */
2395 if (GIMME != G_ARRAY) {
2397 *MARK = *SP; /* unwanted list, return last item */
2408 SV **lastrelem = stack_sp;
2409 SV **lastlelem = stack_base + POPMARK;
2410 SV **firstlelem = stack_base + POPMARK + 1;
2411 register SV **firstrelem = lastlelem + 1;
2412 I32 arybase = curcop->cop_arybase;
2413 I32 lval = op->op_flags & OPf_MOD;
2414 I32 is_something_there = lval;
2416 register I32 max = lastrelem - lastlelem;
2417 register SV **lelem;
2420 if (GIMME != G_ARRAY) {
2421 ix = SvIVx(*lastlelem);
2426 if (ix < 0 || ix >= max)
2427 *firstlelem = &sv_undef;
2429 *firstlelem = firstrelem[ix];
2435 SP = firstlelem - 1;
2439 for (lelem = firstlelem; lelem <= lastlelem; lelem++) {
2445 else if (!(*lelem = firstrelem[ix]))
2450 if (ix >= max || !(*lelem = firstrelem[ix]))
2453 if (!is_something_there && (SvOK(*lelem) || SvGMAGICAL(*lelem)))
2454 is_something_there = TRUE;
2456 if (is_something_there)
2459 SP = firstlelem - 1;
2465 djSP; dMARK; dORIGMARK;
2466 I32 items = SP - MARK;
2467 SV *av = sv_2mortal((SV*)av_make(items, MARK+1));
2468 SP = ORIGMARK; /* av_make() might realloc stack_sp */
2475 djSP; dMARK; dORIGMARK;
2476 HV* hv = (HV*)sv_2mortal((SV*)newHV());
2480 SV *val = NEWSV(46, 0);
2482 sv_setsv(val, *++MARK);
2484 warn("Odd number of elements in hash list");
2485 (void)hv_store_ent(hv,key,val,0);
2494 djSP; dMARK; dORIGMARK;
2495 register AV *ary = (AV*)*++MARK;
2499 register I32 offset;
2500 register I32 length;
2507 if (SvRMAGICAL(ary) && (mg = mg_find((SV*)ary,'P'))) {
2508 *MARK-- = mg->mg_obj;
2512 perl_call_method("SPLICE",GIMME_V);
2521 offset = i = SvIVx(*MARK);
2523 offset += AvFILLp(ary) + 1;
2525 offset -= curcop->cop_arybase;
2529 length = SvIVx(*MARK++);
2534 length = AvMAX(ary) + 1; /* close enough to infinity */
2538 length = AvMAX(ary) + 1;
2540 if (offset > AvFILLp(ary) + 1)
2541 offset = AvFILLp(ary) + 1;
2542 after = AvFILLp(ary) + 1 - (offset + length);
2543 if (after < 0) { /* not that much array */
2544 length += after; /* offset+length now in array */
2550 /* At this point, MARK .. SP-1 is our new LIST */
2553 diff = newlen - length;
2554 if (newlen && !AvREAL(ary)) {
2558 assert(AvREAL(ary)); /* would leak, so croak */
2561 if (diff < 0) { /* shrinking the area */
2563 New(451, tmparyval, newlen, SV*); /* so remember insertion */
2564 Copy(MARK, tmparyval, newlen, SV*);
2567 MARK = ORIGMARK + 1;
2568 if (GIMME == G_ARRAY) { /* copy return vals to stack */
2569 MEXTEND(MARK, length);
2570 Copy(AvARRAY(ary)+offset, MARK, length, SV*);
2572 EXTEND_MORTAL(length);
2573 for (i = length, dst = MARK; i; i--) {
2574 if (!SvIMMORTAL(*dst))
2575 sv_2mortal(*dst); /* free them eventualy */
2582 *MARK = AvARRAY(ary)[offset+length-1];
2584 if (!SvIMMORTAL(*MARK))
2586 for (i = length - 1, dst = &AvARRAY(ary)[offset]; i > 0; i--)
2587 SvREFCNT_dec(*dst++); /* free them now */
2590 AvFILLp(ary) += diff;
2592 /* pull up or down? */
2594 if (offset < after) { /* easier to pull up */
2595 if (offset) { /* esp. if nothing to pull */
2596 src = &AvARRAY(ary)[offset-1];
2597 dst = src - diff; /* diff is negative */
2598 for (i = offset; i > 0; i--) /* can't trust Copy */
2602 SvPVX(ary) = (char*)(AvARRAY(ary) - diff); /* diff is negative */
2606 if (after) { /* anything to pull down? */
2607 src = AvARRAY(ary) + offset + length;
2608 dst = src + diff; /* diff is negative */
2609 Move(src, dst, after, SV*);
2611 dst = &AvARRAY(ary)[AvFILLp(ary)+1];
2612 /* avoid later double free */
2616 dst[--i] = &sv_undef;
2619 for (src = tmparyval, dst = AvARRAY(ary) + offset;
2621 *dst = NEWSV(46, 0);
2622 sv_setsv(*dst++, *src++);
2624 Safefree(tmparyval);
2627 else { /* no, expanding (or same) */
2629 New(452, tmparyval, length, SV*); /* so remember deletion */
2630 Copy(AvARRAY(ary)+offset, tmparyval, length, SV*);
2633 if (diff > 0) { /* expanding */
2635 /* push up or down? */
2637 if (offset < after && diff <= AvARRAY(ary) - AvALLOC(ary)) {
2641 Move(src, dst, offset, SV*);
2643 SvPVX(ary) = (char*)(AvARRAY(ary) - diff);/* diff is positive */
2645 AvFILLp(ary) += diff;
2648 if (AvFILLp(ary) + diff >= AvMAX(ary)) /* oh, well */
2649 av_extend(ary, AvFILLp(ary) + diff);
2650 AvFILLp(ary) += diff;
2653 dst = AvARRAY(ary) + AvFILLp(ary);
2655 for (i = after; i; i--) {
2662 for (src = MARK, dst = AvARRAY(ary) + offset; newlen; newlen--) {
2663 *dst = NEWSV(46, 0);
2664 sv_setsv(*dst++, *src++);
2666 MARK = ORIGMARK + 1;
2667 if (GIMME == G_ARRAY) { /* copy return vals to stack */
2669 Copy(tmparyval, MARK, length, SV*);
2671 EXTEND_MORTAL(length);
2672 for (i = length, dst = MARK; i; i--) {
2673 if (!SvIMMORTAL(*dst))
2674 sv_2mortal(*dst); /* free them eventualy */
2678 Safefree(tmparyval);
2682 else if (length--) {
2683 *MARK = tmparyval[length];
2685 if (!SvIMMORTAL(*MARK))
2687 while (length-- > 0)
2688 SvREFCNT_dec(tmparyval[length]);
2690 Safefree(tmparyval);
2701 djSP; dMARK; dORIGMARK; dTARGET;
2702 register AV *ary = (AV*)*++MARK;
2703 register SV *sv = &sv_undef;
2706 if (SvRMAGICAL(ary) && (mg = mg_find((SV*)ary,'P'))) {
2707 *MARK-- = mg->mg_obj;
2711 perl_call_method("PUSH",G_SCALAR|G_DISCARD);
2716 /* Why no pre-extend of ary here ? */
2717 for (++MARK; MARK <= SP; MARK++) {
2720 sv_setsv(sv, *MARK);
2725 PUSHi( AvFILL(ary) + 1 );
2733 SV *sv = av_pop(av);
2734 if (!SvIMMORTAL(sv) && AvREAL(av))
2735 (void)sv_2mortal(sv);
2744 SV *sv = av_shift(av);
2748 if (!SvIMMORTAL(sv) && AvREAL(av))
2749 (void)sv_2mortal(sv);
2756 djSP; dMARK; dORIGMARK; dTARGET;
2757 register AV *ary = (AV*)*++MARK;
2762 if (SvRMAGICAL(ary) && (mg = mg_find((SV*)ary,'P'))) {
2765 *MARK-- = mg->mg_obj;
2768 perl_call_method("UNSHIFT",G_SCALAR|G_DISCARD);
2773 av_unshift(ary, SP - MARK);
2776 sv_setsv(sv, *++MARK);
2777 (void)av_store(ary, i++, sv);
2781 PUSHi( AvFILL(ary) + 1 );
2791 if (GIMME == G_ARRAY) {
2802 register char *down;
2808 do_join(TARG, &sv_no, MARK, SP);
2810 sv_setsv(TARG, (SP > MARK) ? *SP : DEFSV);
2811 up = SvPV_force(TARG, len);
2813 down = SvPVX(TARG) + len - 1;
2819 (void)SvPOK_only(TARG);
2828 mul128(SV *sv, U8 m)
2831 char *s = SvPV(sv, len);
2835 if (!strnEQ(s, "0000", 4)) { /* need to grow sv */
2836 SV *New = newSVpv("0000000000", 10);
2839 SvREFCNT_dec(sv); /* free old sv */
2844 while (!*t) /* trailing '\0'? */
2847 i = ((*t - '0') << 7) + m;
2848 *(t--) = '0' + (i % 10);
2854 /* Explosives and implosives. */
2861 I32 gimme = GIMME_V;
2865 register char *pat = SvPV(left, llen);
2866 register char *s = SvPV(right, rlen);
2867 char *strend = s + rlen;
2869 register char *patend = pat + llen;
2874 /* These must not be in registers: */
2885 unsigned Quad_t auquad;
2891 register U32 culong;
2893 static char* bitcount = 0;
2896 if (gimme != G_ARRAY) { /* arrange to do first one only */
2898 for (patend = pat; !isALPHA(*patend) || *patend == 'x'; patend++) ;
2899 if (strchr("aAbBhHP", *patend) || *pat == '%') {
2901 while (isDIGIT(*patend) || *patend == '*')
2907 while (pat < patend) {
2909 datumtype = *pat++ & 0xFF;
2910 if (isSPACE(datumtype))
2914 else if (*pat == '*') {
2915 len = strend - strbeg; /* long enough */
2918 else if (isDIGIT(*pat)) {
2920 while (isDIGIT(*pat))
2921 len = (len * 10) + (*pat++ - '0');
2924 len = (datumtype != '@');
2927 croak("Invalid type in unpack: '%c'", (int)datumtype);
2928 case ',': /* grandfather in commas but with a warning */
2929 if (commas++ == 0 && dowarn)
2930 warn("Invalid type in unpack: '%c'", (int)datumtype);
2933 if (len == 1 && pat[-1] != '1')
2942 if (len > strend - strbeg)
2943 DIE("@ outside of string");
2947 if (len > s - strbeg)
2948 DIE("X outside of string");
2952 if (len > strend - s)
2953 DIE("x outside of string");
2958 if (len > strend - s)
2961 goto uchar_checksum;
2962 sv = NEWSV(35, len);
2963 sv_setpvn(sv, s, len);
2965 if (datumtype == 'A') {
2966 aptr = s; /* borrow register */
2967 s = SvPVX(sv) + len - 1;
2968 while (s >= SvPVX(sv) && (!*s || isSPACE(*s)))
2971 SvCUR_set(sv, s - SvPVX(sv));
2972 s = aptr; /* unborrow register */
2974 XPUSHs(sv_2mortal(sv));
2978 if (pat[-1] == '*' || len > (strend - s) * 8)
2979 len = (strend - s) * 8;
2982 Newz(601, bitcount, 256, char);
2983 for (bits = 1; bits < 256; bits++) {
2984 if (bits & 1) bitcount[bits]++;
2985 if (bits & 2) bitcount[bits]++;
2986 if (bits & 4) bitcount[bits]++;
2987 if (bits & 8) bitcount[bits]++;
2988 if (bits & 16) bitcount[bits]++;
2989 if (bits & 32) bitcount[bits]++;
2990 if (bits & 64) bitcount[bits]++;
2991 if (bits & 128) bitcount[bits]++;
2995 culong += bitcount[*(unsigned char*)s++];
3000 if (datumtype == 'b') {
3002 if (bits & 1) culong++;
3008 if (bits & 128) culong++;
3015 sv = NEWSV(35, len + 1);
3018 aptr = pat; /* borrow register */
3020 if (datumtype == 'b') {
3022 for (len = 0; len < aint; len++) {
3023 if (len & 7) /*SUPPRESS 595*/
3027 *pat++ = '0' + (bits & 1);
3032 for (len = 0; len < aint; len++) {
3037 *pat++ = '0' + ((bits & 128) != 0);
3041 pat = aptr; /* unborrow register */
3042 XPUSHs(sv_2mortal(sv));
3046 if (pat[-1] == '*' || len > (strend - s) * 2)
3047 len = (strend - s) * 2;
3048 sv = NEWSV(35, len + 1);
3051 aptr = pat; /* borrow register */
3053 if (datumtype == 'h') {
3055 for (len = 0; len < aint; len++) {
3060 *pat++ = hexdigit[bits & 15];
3065 for (len = 0; len < aint; len++) {
3070 *pat++ = hexdigit[(bits >> 4) & 15];
3074 pat = aptr; /* unborrow register */
3075 XPUSHs(sv_2mortal(sv));
3078 if (len > strend - s)
3083 if (aint >= 128) /* fake up signed chars */
3093 if (aint >= 128) /* fake up signed chars */
3096 sv_setiv(sv, (IV)aint);
3097 PUSHs(sv_2mortal(sv));
3102 if (len > strend - s)
3117 sv_setiv(sv, (IV)auint);
3118 PUSHs(sv_2mortal(sv));
3123 along = (strend - s) / SIZE16;
3140 sv_setiv(sv, (IV)ashort);
3141 PUSHs(sv_2mortal(sv));
3148 along = (strend - s) / SIZE16;
3153 COPY16(s, &aushort);
3156 if (datumtype == 'n')
3157 aushort = PerlSock_ntohs(aushort);
3160 if (datumtype == 'v')
3161 aushort = vtohs(aushort);
3170 COPY16(s, &aushort);
3174 if (datumtype == 'n')
3175 aushort = PerlSock_ntohs(aushort);
3178 if (datumtype == 'v')
3179 aushort = vtohs(aushort);
3181 sv_setiv(sv, (IV)aushort);
3182 PUSHs(sv_2mortal(sv));
3187 along = (strend - s) / sizeof(int);
3192 Copy(s, &aint, 1, int);
3195 cdouble += (double)aint;
3204 Copy(s, &aint, 1, int);
3207 sv_setiv(sv, (IV)aint);
3208 PUSHs(sv_2mortal(sv));
3213 along = (strend - s) / sizeof(unsigned int);
3218 Copy(s, &auint, 1, unsigned int);
3219 s += sizeof(unsigned int);
3221 cdouble += (double)auint;
3230 Copy(s, &auint, 1, unsigned int);
3231 s += sizeof(unsigned int);
3233 sv_setuv(sv, (UV)auint);
3234 PUSHs(sv_2mortal(sv));
3239 along = (strend - s) / SIZE32;
3247 cdouble += (double)along;
3259 sv_setiv(sv, (IV)along);
3260 PUSHs(sv_2mortal(sv));
3267 along = (strend - s) / SIZE32;
3275 if (datumtype == 'N')
3276 aulong = PerlSock_ntohl(aulong);
3279 if (datumtype == 'V')
3280 aulong = vtohl(aulong);
3283 cdouble += (double)aulong;
3295 if (datumtype == 'N')
3296 aulong = PerlSock_ntohl(aulong);
3299 if (datumtype == 'V')
3300 aulong = vtohl(aulong);
3303 sv_setuv(sv, (UV)aulong);
3304 PUSHs(sv_2mortal(sv));
3309 along = (strend - s) / sizeof(char*);
3315 if (sizeof(char*) > strend - s)
3318 Copy(s, &aptr, 1, char*);
3324 PUSHs(sv_2mortal(sv));
3334 while ((len > 0) && (s < strend)) {
3335 auv = (auv << 7) | (*s & 0x7f);
3336 if (!(*s++ & 0x80)) {
3340 PUSHs(sv_2mortal(sv));
3344 else if (++bytes >= sizeof(UV)) { /* promote to string */
3347 sv = newSVpvf("%.*Vu", (int)TYPE_DIGITS(UV), auv);
3348 while (s < strend) {
3349 sv = mul128(sv, *s & 0x7f);
3350 if (!(*s++ & 0x80)) {
3359 PUSHs(sv_2mortal(sv));
3364 if ((s >= strend) && bytes)
3365 croak("Unterminated compressed integer");
3370 if (sizeof(char*) > strend - s)
3373 Copy(s, &aptr, 1, char*);
3378 sv_setpvn(sv, aptr, len);
3379 PUSHs(sv_2mortal(sv));
3386 if (s + sizeof(Quad_t) > strend)
3389 Copy(s, &aquad, 1, Quad_t);
3390 s += sizeof(Quad_t);
3393 if (aquad >= IV_MIN && aquad <= IV_MAX)
3394 sv_setiv(sv, (IV)aquad);
3396 sv_setnv(sv, (double)aquad);
3397 PUSHs(sv_2mortal(sv));
3404 if (s + sizeof(unsigned Quad_t) > strend)
3407 Copy(s, &auquad, 1, unsigned Quad_t);
3408 s += sizeof(unsigned Quad_t);
3411 if (aquad <= UV_MAX)
3412 sv_setuv(sv, (UV)auquad);
3414 sv_setnv(sv, (double)auquad);
3415 PUSHs(sv_2mortal(sv));
3419 /* float and double added gnb@melba.bby.oz.au 22/11/89 */
3422 along = (strend - s) / sizeof(float);
3427 Copy(s, &afloat, 1, float);
3436 Copy(s, &afloat, 1, float);
3439 sv_setnv(sv, (double)afloat);
3440 PUSHs(sv_2mortal(sv));
3446 along = (strend - s) / sizeof(double);
3451 Copy(s, &adouble, 1, double);
3452 s += sizeof(double);
3460 Copy(s, &adouble, 1, double);
3461 s += sizeof(double);
3463 sv_setnv(sv, (double)adouble);
3464 PUSHs(sv_2mortal(sv));
3469 along = (strend - s) * 3 / 4;
3470 sv = NEWSV(42, along);
3473 while (s < strend && *s > ' ' && *s < 'a') {
3478 len = (*s++ - ' ') & 077;
3480 if (s < strend && *s >= ' ')
3481 a = (*s++ - ' ') & 077;
3484 if (s < strend && *s >= ' ')
3485 b = (*s++ - ' ') & 077;
3488 if (s < strend && *s >= ' ')
3489 c = (*s++ - ' ') & 077;
3492 if (s < strend && *s >= ' ')
3493 d = (*s++ - ' ') & 077;
3496 hunk[0] = (a << 2) | (b >> 4);
3497 hunk[1] = (b << 4) | (c >> 2);
3498 hunk[2] = (c << 6) | d;
3499 sv_catpvn(sv, hunk, (len > 3) ? 3 : len);
3504 else if (s[1] == '\n') /* possible checksum byte */
3507 XPUSHs(sv_2mortal(sv));
3512 if (strchr("fFdD", datumtype) ||
3513 (checksum > 32 && strchr("iIlLN", datumtype)) ) {
3517 while (checksum >= 16) {
3521 while (checksum >= 4) {
3527 along = (1 << checksum) - 1;
3528 while (cdouble < 0.0)
3530 cdouble = modf(cdouble / adouble, &trouble) * adouble;
3531 sv_setnv(sv, cdouble);
3534 if (checksum < 32) {
3535 aulong = (1 << checksum) - 1;
3538 sv_setuv(sv, (UV)culong);
3540 XPUSHs(sv_2mortal(sv));
3544 if (sp == oldsp && gimme == G_SCALAR)
3550 doencodes(register SV *sv, register char *s, register I32 len)
3555 sv_catpvn(sv, hunk, 1);
3558 hunk[0] = ' ' + (077 & (*s >> 2));
3559 hunk[1] = ' ' + (077 & (((*s << 4) & 060) | ((s[1] >> 4) & 017)));
3560 hunk[2] = ' ' + (077 & (((s[1] << 2) & 074) | ((s[2] >> 6) & 03)));
3561 hunk[3] = ' ' + (077 & (s[2] & 077));
3562 sv_catpvn(sv, hunk, 4);
3566 for (s = SvPVX(sv); *s; s++) {
3570 sv_catpvn(sv, "\n", 1);
3574 is_an_int(char *s, STRLEN l)
3576 SV *result = newSVpv("", l);
3577 char *result_c = SvPV(result, na); /* convenience */
3578 char *out = result_c;
3588 SvREFCNT_dec(result);
3611 SvREFCNT_dec(result);
3617 SvCUR_set(result, out - result_c);
3622 div128(SV *pnum, bool *done)
3623 /* must be '\0' terminated */
3627 char *s = SvPV(pnum, len);
3636 i = m * 10 + (*t - '0');
3638 r = (i >> 7); /* r < 10 */
3645 SvCUR_set(pnum, (STRLEN) (t - s));
3652 djSP; dMARK; dORIGMARK; dTARGET;
3653 register SV *cat = TARG;
3656 register char *pat = SvPVx(*++MARK, fromlen);
3657 register char *patend = pat + fromlen;
3662 static char null10[] = {0,0,0,0,0,0,0,0,0,0};
3663 static char *space10 = " ";
3665 /* These must not be in registers: */
3674 unsigned Quad_t auquad;
3683 sv_setpvn(cat, "", 0);
3684 while (pat < patend) {
3685 #define NEXTFROM (items-- > 0 ? *MARK++ : &sv_no)
3686 datumtype = *pat++ & 0xFF;
3687 if (isSPACE(datumtype))
3690 len = strchr("@Xxu", datumtype) ? 0 : items;
3693 else if (isDIGIT(*pat)) {
3695 while (isDIGIT(*pat))
3696 len = (len * 10) + (*pat++ - '0');
3702 croak("Invalid type in pack: '%c'", (int)datumtype);
3703 case ',': /* grandfather in commas but with a warning */
3704 if (commas++ == 0 && dowarn)
3705 warn("Invalid type in pack: '%c'", (int)datumtype);
3708 DIE("%% may only be used in unpack");
3719 if (SvCUR(cat) < len)
3720 DIE("X outside of string");
3727 sv_catpvn(cat, null10, 10);
3730 sv_catpvn(cat, null10, len);
3735 aptr = SvPV(fromstr, fromlen);
3739 sv_catpvn(cat, aptr, len);
3741 sv_catpvn(cat, aptr, fromlen);
3743 if (datumtype == 'A') {
3745 sv_catpvn(cat, space10, 10);
3748 sv_catpvn(cat, space10, len);
3752 sv_catpvn(cat, null10, 10);
3755 sv_catpvn(cat, null10, len);
3762 char *savepat = pat;
3767 aptr = SvPV(fromstr, fromlen);
3772 SvCUR(cat) += (len+7)/8;
3773 SvGROW(cat, SvCUR(cat) + 1);
3774 aptr = SvPVX(cat) + aint;
3779 if (datumtype == 'B') {
3780 for (len = 0; len++ < aint;) {
3781 items |= *pat++ & 1;
3785 *aptr++ = items & 0xff;
3791 for (len = 0; len++ < aint;) {
3797 *aptr++ = items & 0xff;
3803 if (datumtype == 'B')
3804 items <<= 7 - (aint & 7);
3806 items >>= 7 - (aint & 7);
3807 *aptr++ = items & 0xff;
3809 pat = SvPVX(cat) + SvCUR(cat);
3820 char *savepat = pat;
3825 aptr = SvPV(fromstr, fromlen);
3830 SvCUR(cat) += (len+1)/2;
3831 SvGROW(cat, SvCUR(cat) + 1);
3832 aptr = SvPVX(cat) + aint;
3837 if (datumtype == 'H') {
3838 for (len = 0; len++ < aint;) {
3840 items |= ((*pat++ & 15) + 9) & 15;
3842 items |= *pat++ & 15;
3846 *aptr++ = items & 0xff;
3852 for (len = 0; len++ < aint;) {
3854 items |= (((*pat++ & 15) + 9) & 15) << 4;
3856 items |= (*pat++ & 15) << 4;
3860 *aptr++ = items & 0xff;
3866 *aptr++ = items & 0xff;
3867 pat = SvPVX(cat) + SvCUR(cat);
3879 aint = SvIV(fromstr);
3881 sv_catpvn(cat, &achar, sizeof(char));
3884 /* Float and double added by gnb@melba.bby.oz.au 22/11/89 */
3889 afloat = (float)SvNV(fromstr);
3890 sv_catpvn(cat, (char *)&afloat, sizeof (float));
3897 adouble = (double)SvNV(fromstr);
3898 sv_catpvn(cat, (char *)&adouble, sizeof (double));
3904 ashort = (I16)SvIV(fromstr);
3906 ashort = PerlSock_htons(ashort);
3908 CAT16(cat, &ashort);
3914 ashort = (I16)SvIV(fromstr);
3916 ashort = htovs(ashort);
3918 CAT16(cat, &ashort);
3925 ashort = (I16)SvIV(fromstr);
3926 CAT16(cat, &ashort);
3932 auint = SvUV(fromstr);
3933 sv_catpvn(cat, (char*)&auint, sizeof(unsigned int));
3939 adouble = floor(SvNV(fromstr));
3942 croak("Cannot compress negative numbers");
3948 #ifdef CXUX_BROKEN_CONSTANT_CONVERT
3949 adouble <= UV_MAX_cxux
3956 char buf[1 + sizeof(UV)];
3957 char *in = buf + sizeof(buf);
3958 UV auv = U_V(adouble);;
3961 *--in = (auv & 0x7f) | 0x80;
3964 buf[sizeof(buf) - 1] &= 0x7f; /* clear continue bit */
3965 sv_catpvn(cat, in, (buf + sizeof(buf)) - in);
3967 else if (SvPOKp(fromstr)) { /* decimal string arithmetics */
3968 char *from, *result, *in;
3973 /* Copy string and check for compliance */
3974 from = SvPV(fromstr, len);
3975 if ((norm = is_an_int(from, len)) == NULL)
3976 croak("can compress only unsigned integer");
3978 New('w', result, len, char);
3982 *--in = div128(norm, &done) | 0x80;
3983 result[len - 1] &= 0x7F; /* clear continue bit */
3984 sv_catpvn(cat, in, (result + len) - in);
3986 SvREFCNT_dec(norm); /* free norm */
3988 else if (SvNOKp(fromstr)) {
3989 char buf[sizeof(double) * 2]; /* 8/7 <= 2 */
3990 char *in = buf + sizeof(buf);
3993 double next = floor(adouble / 128);
3994 *--in = (unsigned char)(adouble - (next * 128)) | 0x80;
3995 if (--in < buf) /* this cannot happen ;-) */
3996 croak ("Cannot compress integer");
3998 } while (adouble > 0);
3999 buf[sizeof(buf) - 1] &= 0x7f; /* clear continue bit */
4000 sv_catpvn(cat, in, (buf + sizeof(buf)) - in);
4003 croak("Cannot compress non integer");
4009 aint = SvIV(fromstr);
4010 sv_catpvn(cat, (char*)&aint, sizeof(int));
4016 aulong = SvUV(fromstr);
4018 aulong = PerlSock_htonl(aulong);
4020 CAT32(cat, &aulong);
4026 aulong = SvUV(fromstr);
4028 aulong = htovl(aulong);
4030 CAT32(cat, &aulong);
4036 aulong = SvUV(fromstr);
4037 CAT32(cat, &aulong);
4043 along = SvIV(fromstr);
4051 auquad = (unsigned Quad_t)SvIV(fromstr);
4052 sv_catpvn(cat, (char*)&auquad, sizeof(unsigned Quad_t));
4058 aquad = (Quad_t)SvIV(fromstr);
4059 sv_catpvn(cat, (char*)&aquad, sizeof(Quad_t));
4062 #endif /* HAS_QUAD */
4064 len = 1; /* assume SV is correct length */
4069 if (fromstr == &sv_undef)
4072 /* XXX better yet, could spirit away the string to
4073 * a safe spot and hang on to it until the result
4074 * of pack() (and all copies of the result) are
4077 if (dowarn && (SvTEMP(fromstr) || SvPADTMP(fromstr)))
4078 warn("Attempt to pack pointer to temporary value");
4079 if (SvPOK(fromstr) || SvNIOK(fromstr))
4080 aptr = SvPV(fromstr,na);
4082 aptr = SvPV_force(fromstr,na);
4084 sv_catpvn(cat, (char*)&aptr, sizeof(char*));
4089 aptr = SvPV(fromstr, fromlen);
4090 SvGROW(cat, fromlen * 4 / 3);
4095 while (fromlen > 0) {
4102 doencodes(cat, aptr, todo);
4121 register I32 limit = POPi; /* note, negative is forever */
4124 register char *s = SvPV(sv, len);
4125 char *strend = s + len;
4127 register REGEXP *rx;
4131 I32 maxiters = (strend - s) + 10;
4134 I32 origlimit = limit;
4137 AV *oldstack = curstack;
4138 I32 gimme = GIMME_V;
4139 I32 oldsave = savestack_ix;
4140 I32 make_mortal = 1;
4141 MAGIC *mg = (MAGIC *) NULL;
4144 Copy(&LvTARGOFF(POPs), &pm, 1, PMOP*);
4149 DIE("panic: do_split");
4150 rx = pm->op_pmregexp;
4152 TAINT_IF((pm->op_pmflags & PMf_LOCALE) &&
4153 (pm->op_pmflags & (PMf_WHITE | PMf_SKIPWHITE)));
4155 if (pm->op_pmreplroot)
4156 ary = GvAVn((GV*)pm->op_pmreplroot);
4157 else if (gimme != G_ARRAY)
4159 ary = (AV*)curpad[0];
4162 #endif /* USE_THREADS */
4165 if (ary && (gimme != G_ARRAY || (pm->op_pmflags & PMf_ONCE))) {
4171 if (SvRMAGICAL(ary) && (mg = mg_find((SV *) ary, 'P'))) {
4178 for (i = AvFILLp(ary); i >= 0; i--)
4179 AvARRAY(ary)[i] = &sv_undef; /* don't free mere refs */
4181 /* temporarily switch stacks */
4182 SWITCHSTACK(curstack, ary);
4186 base = SP - stack_base;
4188 if (pm->op_pmflags & PMf_SKIPWHITE) {
4189 if (pm->op_pmflags & PMf_LOCALE) {
4190 while (isSPACE_LC(*s))
4198 if (pm->op_pmflags & (PMf_MULTILINE|PMf_SINGLELINE)) {
4200 multiline = pm->op_pmflags & PMf_MULTILINE;
4204 limit = maxiters + 2;
4205 if (pm->op_pmflags & PMf_WHITE) {
4208 while (m < strend &&
4209 !((pm->op_pmflags & PMf_LOCALE)
4210 ? isSPACE_LC(*m) : isSPACE(*m)))
4215 dstr = NEWSV(30, m-s);
4216 sv_setpvn(dstr, s, m-s);
4222 while (s < strend &&
4223 ((pm->op_pmflags & PMf_LOCALE)
4224 ? isSPACE_LC(*s) : isSPACE(*s)))
4228 else if (strEQ("^", rx->precomp)) {
4231 for (m = s; m < strend && *m != '\n'; m++) ;
4235 dstr = NEWSV(30, m-s);
4236 sv_setpvn(dstr, s, m-s);
4243 else if (rx->check_substr && !rx->nparens
4244 && (rx->reganch & ROPT_CHECK_ALL)
4245 && !(rx->reganch & ROPT_ANCH)) {
4246 i = SvCUR(rx->check_substr);
4247 if (i == 1 && !SvTAIL(rx->check_substr)) {
4248 i = *SvPVX(rx->check_substr);
4251 for (m = s; m < strend && *m != i; m++) ;
4254 dstr = NEWSV(30, m-s);
4255 sv_setpvn(dstr, s, m-s);
4264 while (s < strend && --limit &&
4265 (m=fbm_instr((unsigned char*)s, (unsigned char*)strend,
4266 rx->check_substr)) )
4269 dstr = NEWSV(31, m-s);
4270 sv_setpvn(dstr, s, m-s);
4279 maxiters += (strend - s) * rx->nparens;
4280 while (s < strend && --limit &&
4281 regexec_flags(rx, s, strend, orig, 1, Nullsv, NULL, 0))
4283 TAINT_IF(RX_MATCH_TAINTED(rx));
4285 && rx->subbase != orig) {
4290 strend = s + (strend - m);
4293 dstr = NEWSV(32, m-s);
4294 sv_setpvn(dstr, s, m-s);
4299 for (i = 1; i <= rx->nparens; i++) {
4303 dstr = NEWSV(33, m-s);
4304 sv_setpvn(dstr, s, m-s);
4307 dstr = NEWSV(33, 0);
4317 LEAVE_SCOPE(oldsave);
4318 iters = (SP - stack_base) - base;
4319 if (iters > maxiters)
4322 /* keep field after final delim? */
4323 if (s < strend || (iters && origlimit)) {
4324 dstr = NEWSV(34, strend-s);
4325 sv_setpvn(dstr, s, strend-s);
4331 else if (!origlimit) {
4332 while (iters > 0 && (!TOPs || !SvANY(TOPs) || SvCUR(TOPs) == 0))
4338 SWITCHSTACK(ary, oldstack);
4339 if (SvSMAGICAL(ary)) {
4344 if (gimme == G_ARRAY) {
4346 Copy(AvARRAY(ary), SP + 1, iters, SV*);
4354 perl_call_method("PUSH",G_SCALAR|G_DISCARD);
4357 if (gimme == G_ARRAY) {
4358 /* EXTEND should not be needed - we just popped them */
4360 for (i=0; i < iters; i++) {
4361 SV **svp = av_fetch(ary, i, FALSE);
4362 PUSHs((svp) ? *svp : &sv_undef);
4369 if (gimme == G_ARRAY)
4372 if (iters || !pm->op_pmreplroot) {
4382 unlock_condpair(void *svv)
4385 MAGIC *mg = mg_find((SV*)svv, 'm');
4388 croak("panic: unlock_condpair unlocking non-mutex");
4389 MUTEX_LOCK(MgMUTEXP(mg));
4390 if (MgOWNER(mg) != thr)
4391 croak("panic: unlock_condpair unlocking mutex that we don't own");
4393 COND_SIGNAL(MgOWNERCONDP(mg));
4394 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "0x%lx: unlock 0x%lx\n",
4395 (unsigned long)thr, (unsigned long)svv);)
4396 MUTEX_UNLOCK(MgMUTEXP(mg));
4398 #endif /* USE_THREADS */
4411 mg = condpair_magic(sv);
4412 MUTEX_LOCK(MgMUTEXP(mg));
4413 if (MgOWNER(mg) == thr)
4414 MUTEX_UNLOCK(MgMUTEXP(mg));
4417 COND_WAIT(MgOWNERCONDP(mg), MgMUTEXP(mg));
4419 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "0x%lx: pp_lock lock 0x%lx\n",
4420 (unsigned long)thr, (unsigned long)sv);)
4421 MUTEX_UNLOCK(MgMUTEXP(mg));
4422 SvREFCNT_inc(sv); /* keep alive until magic_mutexfree */
4423 save_destructor(unlock_condpair, sv);
4425 #endif /* USE_THREADS */
4426 if (SvTYPE(retsv) == SVt_PVAV || SvTYPE(retsv) == SVt_PVHV
4427 || SvTYPE(retsv) == SVt_PVCV) {
4428 retsv = refto(retsv);
4439 if (op->op_private & OPpLVAL_INTRO)
4440 PUSHs(*save_threadsv(op->op_targ));
4442 PUSHs(THREADSV(op->op_targ));
4445 DIE("tried to access per-thread data in non-threaded perl");
4446 #endif /* USE_THREADS */