3 * Copyright (c) 1991-1999, 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
20 * The compiler on Concurrent CX/UX systems has a subtle bug which only
21 * seems to show up when compiling pp.c - it generates the wrong double
22 * precision constant value for (double)UV_MAX when used inline in the body
23 * of the code below, so this makes a static variable up front (which the
24 * compiler seems to get correct) and uses it in place of UV_MAX below.
26 #ifdef CXUX_BROKEN_CONSTANT_CONVERT
27 static double UV_MAX_cxux = ((double)UV_MAX);
31 * Types used in bitwise operations.
33 * Normally we'd just use IV and UV. However, some hardware and
34 * software combinations (e.g. Alpha and current OSF/1) don't have a
35 * floating-point type to use for NV that has adequate bits to fully
36 * hold an IV/UV. (In other words, sizeof(long) == sizeof(double).)
38 * It just so happens that "int" is the right size almost everywhere.
44 * Mask used after bitwise operations.
46 * There is at least one realm (Cray word machines) that doesn't
47 * have an integral type (except char) small enough to be represented
48 * in a double without loss; that is, it has no 32-bit type.
50 #if LONGSIZE > 4 && defined(_CRAY) && !defined(_CRAYMPP)
52 # define BW_MASK ((1 << BW_BITS) - 1)
53 # define BW_SIGN (1 << (BW_BITS - 1))
54 # define BWi(i) (((i) & BW_SIGN) ? ((i) | ~BW_MASK) : ((i) & BW_MASK))
55 # define BWu(u) ((u) & BW_MASK)
62 * Offset for integer pack/unpack.
64 * On architectures where I16 and I32 aren't really 16 and 32 bits,
65 * which for now are all Crays, pack and unpack have to play games.
69 * These values are required for portability of pack() output.
70 * If they're not right on your machine, then pack() and unpack()
71 * wouldn't work right anyway; you'll need to apply the Cray hack.
72 * (I'd like to check them with #if, but you can't use sizeof() in
73 * the preprocessor.) --???
76 The appropriate SHORTSIZE, INTSIZE, LONGSIZE, and LONGLONGSIZE
77 defines are now in config.h. --Andy Dougherty April 1998
82 /* CROSSCOMPILE and MULTIARCH are going to affect pp_pack() and pp_unpack().
85 #if SHORTSIZE != SIZE16 || LONGSIZE != SIZE32
86 # define PERL_NATINT_PACK
89 #if BYTEORDER > 0xFFFF && defined(_CRAY) && !defined(_CRAYMPP)
90 # if BYTEORDER == 0x12345678
91 # define OFF16(p) (char*)(p)
92 # define OFF32(p) (char*)(p)
94 # if BYTEORDER == 0x87654321
95 # define OFF16(p) ((char*)(p) + (sizeof(U16) - SIZE16))
96 # define OFF32(p) ((char*)(p) + (sizeof(U32) - SIZE32))
98 }}}} bad cray byte order
101 # define COPY16(s,p) (*(p) = 0, Copy(s, OFF16(p), SIZE16, char))
102 # define COPY32(s,p) (*(p) = 0, Copy(s, OFF32(p), SIZE32, char))
103 # define COPYNN(s,p,n) (*(p) = 0, Copy(s, (char *)(p), n, char))
104 # define CAT16(sv,p) sv_catpvn(sv, OFF16(p), SIZE16)
105 # define CAT32(sv,p) sv_catpvn(sv, OFF32(p), SIZE32)
107 # define COPY16(s,p) Copy(s, p, SIZE16, char)
108 # define COPY32(s,p) Copy(s, p, SIZE32, char)
109 # define COPYNN(s,p,n) Copy(s, (char *)(p), n, char)
110 # define CAT16(sv,p) sv_catpvn(sv, (char*)(p), SIZE16)
111 # define CAT32(sv,p) sv_catpvn(sv, (char*)(p), SIZE32)
114 /* variations on pp_null */
120 /* XXX I can't imagine anyone who doesn't have this actually _needs_
121 it, since pid_t is an integral type.
124 #ifdef NEED_GETPID_PROTO
125 extern Pid_t getpid (void);
131 if (GIMME_V == G_SCALAR)
132 XPUSHs(&PL_sv_undef);
146 if (PL_op->op_private & OPpLVAL_INTRO)
147 SAVECLEARSV(PL_curpad[PL_op->op_targ]);
149 if (PL_op->op_flags & OPf_REF) {
153 if (GIMME == G_ARRAY) {
154 I32 maxarg = AvFILL((AV*)TARG) + 1;
156 if (SvMAGICAL(TARG)) {
158 for (i=0; i < maxarg; i++) {
159 SV **svp = av_fetch((AV*)TARG, i, FALSE);
160 SP[i+1] = (svp) ? *svp : &PL_sv_undef;
164 Copy(AvARRAY((AV*)TARG), SP+1, maxarg, SV*);
169 SV* sv = sv_newmortal();
170 I32 maxarg = AvFILL((AV*)TARG) + 1;
171 sv_setiv(sv, maxarg);
183 if (PL_op->op_private & OPpLVAL_INTRO)
184 SAVECLEARSV(PL_curpad[PL_op->op_targ]);
185 if (PL_op->op_flags & OPf_REF)
188 if (gimme == G_ARRAY) {
189 RETURNOP(do_kv(ARGS));
191 else if (gimme == G_SCALAR) {
192 SV* sv = sv_newmortal();
193 if (HvFILL((HV*)TARG))
194 sv_setpvf(sv, "%ld/%ld",
195 (long)HvFILL((HV*)TARG), (long)HvMAX((HV*)TARG) + 1);
205 DIE("NOT IMPL LINE %d",__LINE__);
216 tryAMAGICunDEREF(to_gv);
219 if (SvTYPE(sv) == SVt_PVIO) {
220 GV *gv = (GV*) sv_newmortal();
221 gv_init(gv, 0, "", 0, 0);
222 GvIOp(gv) = (IO *)sv;
223 (void)SvREFCNT_inc(sv);
226 else if (SvTYPE(sv) != SVt_PVGV)
227 DIE("Not a GLOB reference");
230 if (SvTYPE(sv) != SVt_PVGV) {
234 if (SvGMAGICAL(sv)) {
240 /* If this is a 'my' scalar and flag is set then vivify
243 if (PL_op->op_private & OPpDEREF) {
244 GV *gv = (GV *) newSV(0);
247 if (cUNOP->op_first->op_type == OP_PADSV) {
248 SV *padname = *av_fetch(PL_comppad_name, cUNOP->op_first->op_targ, 4);
249 name = SvPV(padname,len);
251 gv_init(gv, PL_curcop->cop_stash, name, len, 0);
252 sv_upgrade(sv, SVt_RV);
253 SvRV(sv) = (SV *) gv;
258 if (PL_op->op_flags & OPf_REF ||
259 PL_op->op_private & HINT_STRICT_REFS)
260 DIE(PL_no_usym, "a symbol");
261 if (ckWARN(WARN_UNINITIALIZED))
262 warner(WARN_UNINITIALIZED, PL_warn_uninit);
266 if ((PL_op->op_flags & OPf_SPECIAL) &&
267 !(PL_op->op_flags & OPf_MOD))
269 sv = (SV*)gv_fetchpv(sym, FALSE, SVt_PVGV);
274 if (PL_op->op_private & HINT_STRICT_REFS)
275 DIE(PL_no_symref, sym, "a symbol");
276 sv = (SV*)gv_fetchpv(sym, TRUE, SVt_PVGV);
280 if (PL_op->op_private & OPpLVAL_INTRO)
281 save_gp((GV*)sv, !(PL_op->op_flags & OPf_SPECIAL));
292 tryAMAGICunDEREF(to_sv);
295 switch (SvTYPE(sv)) {
299 DIE("Not a SCALAR reference");
307 if (SvTYPE(gv) != SVt_PVGV) {
308 if (SvGMAGICAL(sv)) {
314 if (PL_op->op_flags & OPf_REF ||
315 PL_op->op_private & HINT_STRICT_REFS)
316 DIE(PL_no_usym, "a SCALAR");
317 if (ckWARN(WARN_UNINITIALIZED))
318 warner(WARN_UNINITIALIZED, PL_warn_uninit);
322 if ((PL_op->op_flags & OPf_SPECIAL) &&
323 !(PL_op->op_flags & OPf_MOD))
325 gv = (GV*)gv_fetchpv(sym, FALSE, SVt_PV);
330 if (PL_op->op_private & HINT_STRICT_REFS)
331 DIE(PL_no_symref, sym, "a SCALAR");
332 gv = (GV*)gv_fetchpv(sym, TRUE, SVt_PV);
337 if (PL_op->op_flags & OPf_MOD) {
338 if (PL_op->op_private & OPpLVAL_INTRO)
339 sv = save_scalar((GV*)TOPs);
340 else if (PL_op->op_private & OPpDEREF)
341 vivify_ref(sv, PL_op->op_private & OPpDEREF);
351 SV *sv = AvARYLEN(av);
353 AvARYLEN(av) = sv = NEWSV(0,0);
354 sv_upgrade(sv, SVt_IV);
355 sv_magic(sv, (SV*)av, '#', Nullch, 0);
363 djSP; dTARGET; dPOPss;
365 if (PL_op->op_flags & OPf_MOD) {
366 if (SvTYPE(TARG) < SVt_PVLV) {
367 sv_upgrade(TARG, SVt_PVLV);
368 sv_magic(TARG, Nullsv, '.', Nullch, 0);
372 if (LvTARG(TARG) != sv) {
374 SvREFCNT_dec(LvTARG(TARG));
375 LvTARG(TARG) = SvREFCNT_inc(sv);
377 PUSHs(TARG); /* no SvSETMAGIC */
383 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
384 mg = mg_find(sv, 'g');
385 if (mg && mg->mg_len >= 0) {
389 PUSHi(i + PL_curcop->cop_arybase);
403 /* We usually try to add a non-existent subroutine in case of AUTOLOAD. */
404 /* (But not in defined().) */
405 CV *cv = sv_2cv(TOPs, &stash, &gv, !(PL_op->op_flags & OPf_SPECIAL));
408 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
411 cv = (CV*)&PL_sv_undef;
425 if (SvPOK(TOPs) && SvCUR(TOPs) >= 7) {
426 char *s = SvPVX(TOPs);
427 if (strnEQ(s, "CORE::", 6)) {
430 code = keyword(s + 6, SvCUR(TOPs) - 6);
431 if (code < 0) { /* Overridable. */
432 #define MAX_ARGS_OP ((sizeof(I32) - 1) * 2)
433 int i = 0, n = 0, seen_question = 0;
435 char str[ MAX_ARGS_OP * 2 + 2 ]; /* One ';', one '\0' */
437 while (i < MAXO) { /* The slow way. */
438 if (strEQ(s + 6, PL_op_name[i])
439 || strEQ(s + 6, PL_op_desc[i]))
445 goto nonesuch; /* Should not happen... */
447 oa = PL_opargs[i] >> OASHIFT;
449 if (oa & OA_OPTIONAL) {
453 else if (seen_question)
454 goto set; /* XXXX system, exec */
455 if ((oa & (OA_OPTIONAL - 1)) >= OA_AVREF
456 && (oa & (OA_OPTIONAL - 1)) <= OA_HVREF) {
459 /* What to do with R ((un)tie, tied, (sys)read, recv)? */
460 str[n++] = ("?$@@%&*$")[oa & (OA_OPTIONAL - 1)];
464 ret = sv_2mortal(newSVpvn(str, n - 1));
466 else if (code) /* Non-Overridable */
468 else { /* None such */
470 croak("Can't find an opnumber for \"%s\"", s+6);
474 cv = sv_2cv(TOPs, &stash, &gv, FALSE);
476 ret = sv_2mortal(newSVpvn(SvPVX(cv), SvCUR(cv)));
485 CV* cv = (CV*)PL_curpad[PL_op->op_targ];
487 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
503 if (GIMME != G_ARRAY) {
507 *MARK = &PL_sv_undef;
508 *MARK = refto(*MARK);
512 EXTEND_MORTAL(SP - MARK);
514 *MARK = refto(*MARK);
523 if (SvTYPE(sv) == SVt_PVLV && LvTYPE(sv) == 'y') {
526 if (!(sv = LvTARG(sv)))
529 (void)SvREFCNT_inc(sv);
531 else if (SvPADTMP(sv))
535 (void)SvREFCNT_inc(sv);
538 sv_upgrade(rv, SVt_RV);
552 if (sv && SvGMAGICAL(sv))
555 if (!sv || !SvROK(sv))
559 pv = sv_reftype(sv,TRUE);
560 PUSHp(pv, strlen(pv));
570 stash = PL_curcop->cop_stash;
574 char *ptr = SvPV(ssv,len);
575 if (ckWARN(WARN_UNSAFE) && len == 0)
577 "Explicit blessing to '' (assuming package main)");
578 stash = gv_stashpvn(ptr, len, TRUE);
581 (void)sv_bless(TOPs, stash);
595 elem = SvPV(sv, n_a);
599 switch (elem ? *elem : '\0')
602 if (strEQ(elem, "ARRAY"))
603 tmpRef = (SV*)GvAV(gv);
606 if (strEQ(elem, "CODE"))
607 tmpRef = (SV*)GvCVu(gv);
610 if (strEQ(elem, "FILEHANDLE")) /* XXX deprecate in 5.005 */
611 tmpRef = (SV*)GvIOp(gv);
614 if (strEQ(elem, "GLOB"))
618 if (strEQ(elem, "HASH"))
619 tmpRef = (SV*)GvHV(gv);
622 if (strEQ(elem, "IO"))
623 tmpRef = (SV*)GvIOp(gv);
626 if (strEQ(elem, "NAME"))
627 sv = newSVpvn(GvNAME(gv), GvNAMELEN(gv));
630 if (strEQ(elem, "PACKAGE"))
631 sv = newSVpv(HvNAME(GvSTASH(gv)), 0);
634 if (strEQ(elem, "SCALAR"))
648 /* Pattern matching */
653 register unsigned char *s;
656 register I32 *sfirst;
660 if (sv == PL_lastscream) {
666 SvSCREAM_off(PL_lastscream);
667 SvREFCNT_dec(PL_lastscream);
669 PL_lastscream = SvREFCNT_inc(sv);
672 s = (unsigned char*)(SvPV(sv, len));
676 if (pos > PL_maxscream) {
677 if (PL_maxscream < 0) {
678 PL_maxscream = pos + 80;
679 New(301, PL_screamfirst, 256, I32);
680 New(302, PL_screamnext, PL_maxscream, I32);
683 PL_maxscream = pos + pos / 4;
684 Renew(PL_screamnext, PL_maxscream, I32);
688 sfirst = PL_screamfirst;
689 snext = PL_screamnext;
691 if (!sfirst || !snext)
692 DIE("do_study: out of memory");
694 for (ch = 256; ch; --ch)
701 snext[pos] = sfirst[ch] - pos;
708 sv_magic(sv, Nullsv, 'g', Nullch, 0); /* piggyback on m//g magic */
717 if (PL_op->op_flags & OPf_STACKED)
723 TARG = sv_newmortal();
728 /* Lvalue operators. */
740 djSP; dMARK; dTARGET;
750 SETi(do_chomp(TOPs));
756 djSP; dMARK; dTARGET;
757 register I32 count = 0;
760 count += do_chomp(POPs);
771 if (!sv || !SvANY(sv))
773 switch (SvTYPE(sv)) {
775 if (AvMAX(sv) >= 0 || SvGMAGICAL(sv) || (SvRMAGICAL(sv) && mg_find(sv,'P')))
779 if (HvARRAY(sv) || SvGMAGICAL(sv) || (SvRMAGICAL(sv) && mg_find(sv,'P')))
783 if (CvROOT(sv) || CvXSUB(sv))
800 if (!PL_op->op_private) {
809 if (SvTHINKFIRST(sv))
812 switch (SvTYPE(sv)) {
822 if (ckWARN(WARN_UNSAFE) && cv_const_sv((CV*)sv))
823 warner(WARN_UNSAFE, "Constant subroutine %s undefined",
824 CvANON((CV*)sv) ? "(anonymous)" : GvENAME(CvGV((CV*)sv)));
828 /* let user-undef'd sub keep its identity */
829 GV* gv = (GV*)SvREFCNT_inc(CvGV((CV*)sv));
836 SvSetMagicSV(sv, &PL_sv_undef);
840 Newz(602, gp, 1, GP);
841 GvGP(sv) = gp_ref(gp);
842 GvSV(sv) = NEWSV(72,0);
843 GvLINE(sv) = PL_curcop->cop_line;
849 if (SvTYPE(sv) >= SVt_PV && SvPVX(sv) && SvLEN(sv)) {
852 SvPV_set(sv, Nullch);
865 if (SvREADONLY(TOPs) || SvTYPE(TOPs) > SVt_PVLV)
867 if (SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) &&
868 SvIVX(TOPs) != IV_MIN)
871 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
882 if (SvREADONLY(TOPs) || SvTYPE(TOPs) > SVt_PVLV)
884 sv_setsv(TARG, TOPs);
885 if (SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) &&
886 SvIVX(TOPs) != IV_MAX)
889 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
903 if(SvREADONLY(TOPs) || SvTYPE(TOPs) > SVt_PVLV)
905 sv_setsv(TARG, TOPs);
906 if (SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) &&
907 SvIVX(TOPs) != IV_MIN)
910 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
919 /* Ordinary operators. */
923 djSP; dATARGET; tryAMAGICbin(pow,opASSIGN);
926 SETn( pow( left, right) );
933 djSP; dATARGET; tryAMAGICbin(mult,opASSIGN);
936 SETn( left * right );
943 djSP; dATARGET; tryAMAGICbin(div,opASSIGN);
948 DIE("Illegal division by zero");
950 /* insure that 20./5. == 4. */
953 if ((double)I_V(left) == left &&
954 (double)I_V(right) == right &&
955 (k = I_V(left)/I_V(right))*I_V(right) == I_V(left)) {
959 value = left / right;
963 value = left / right;
972 djSP; dATARGET; tryAMAGICbin(modulo,opASSIGN);
982 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)) {
984 right = (right_neg = (i < 0)) ? -i : i;
989 right_neg = dright < 0;
994 if (!use_double && SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)) {
996 left = (left_neg = (i < 0)) ? -i : i;
1004 left_neg = dleft < 0;
1013 /* Somehow U_V is pessimized even if CASTFLAGS is 0 */
1015 # define CAST_D2UV(d) U_V(d)
1017 # define CAST_D2UV(d) ((UV)(d))
1019 /* Tried to do this only in the case DOUBLESIZE <= UV_SIZE,
1020 * or, in other words, precision of UV more than of NV.
1021 * But in fact the approach below turned out to be an
1022 * optimization - floor() may be slow */
1023 if (dright <= UV_MAX && dleft <= UV_MAX) {
1024 right = CAST_D2UV(dright);
1025 left = CAST_D2UV(dleft);
1030 /* Backward-compatibility clause: */
1031 dright = floor(dright + 0.5);
1032 dleft = floor(dleft + 0.5);
1035 DIE("Illegal modulus zero");
1037 dans = fmod(dleft, dright);
1038 if ((left_neg != right_neg) && dans)
1039 dans = dright - dans;
1042 sv_setnv(TARG, dans);
1049 DIE("Illegal modulus zero");
1052 if ((left_neg != right_neg) && ans)
1055 /* XXX may warn: unary minus operator applied to unsigned type */
1056 /* could change -foo to be (~foo)+1 instead */
1057 if (ans <= ~((UV)IV_MAX)+1)
1058 sv_setiv(TARG, ~ans+1);
1060 sv_setnv(TARG, -(double)ans);
1063 sv_setuv(TARG, ans);
1072 djSP; dATARGET; tryAMAGICbin(repeat,opASSIGN);
1074 register I32 count = POPi;
1075 if (GIMME == G_ARRAY && PL_op->op_private & OPpREPEAT_DOLIST) {
1077 I32 items = SP - MARK;
1080 max = items * count;
1089 repeatcpy((char*)(MARK + items), (char*)MARK,
1090 items * sizeof(SV*), count - 1);
1093 else if (count <= 0)
1096 else { /* Note: mark already snarfed by pp_list */
1101 SvSetSV(TARG, tmpstr);
1102 SvPV_force(TARG, len);
1107 SvGROW(TARG, (count * len) + 1);
1108 repeatcpy(SvPVX(TARG) + len, SvPVX(TARG), len, count - 1);
1109 SvCUR(TARG) *= count;
1111 *SvEND(TARG) = '\0';
1113 (void)SvPOK_only(TARG);
1122 djSP; dATARGET; tryAMAGICbin(subtr,opASSIGN);
1125 SETn( left - right );
1132 djSP; dATARGET; tryAMAGICbin(lshift,opASSIGN);
1135 if (PL_op->op_private & HINT_INTEGER) {
1137 i = BWi(i) << shift;
1151 djSP; dATARGET; tryAMAGICbin(rshift,opASSIGN);
1154 if (PL_op->op_private & HINT_INTEGER) {
1156 i = BWi(i) >> shift;
1170 djSP; tryAMAGICbinSET(lt,0);
1173 SETs(boolSV(TOPn < value));
1180 djSP; tryAMAGICbinSET(gt,0);
1183 SETs(boolSV(TOPn > value));
1190 djSP; tryAMAGICbinSET(le,0);
1193 SETs(boolSV(TOPn <= value));
1200 djSP; tryAMAGICbinSET(ge,0);
1203 SETs(boolSV(TOPn >= value));
1210 djSP; tryAMAGICbinSET(ne,0);
1213 SETs(boolSV(TOPn != value));
1220 djSP; dTARGET; tryAMAGICbin(ncmp,0);
1227 else if (left < right)
1229 else if (left > right)
1242 djSP; tryAMAGICbinSET(slt,0);
1245 int cmp = ((PL_op->op_private & OPpLOCALE)
1246 ? sv_cmp_locale(left, right)
1247 : sv_cmp(left, right));
1248 SETs(boolSV(cmp < 0));
1255 djSP; tryAMAGICbinSET(sgt,0);
1258 int cmp = ((PL_op->op_private & OPpLOCALE)
1259 ? sv_cmp_locale(left, right)
1260 : sv_cmp(left, right));
1261 SETs(boolSV(cmp > 0));
1268 djSP; tryAMAGICbinSET(sle,0);
1271 int cmp = ((PL_op->op_private & OPpLOCALE)
1272 ? sv_cmp_locale(left, right)
1273 : sv_cmp(left, right));
1274 SETs(boolSV(cmp <= 0));
1281 djSP; tryAMAGICbinSET(sge,0);
1284 int cmp = ((PL_op->op_private & OPpLOCALE)
1285 ? sv_cmp_locale(left, right)
1286 : sv_cmp(left, right));
1287 SETs(boolSV(cmp >= 0));
1294 djSP; tryAMAGICbinSET(seq,0);
1297 SETs(boolSV(sv_eq(left, right)));
1304 djSP; tryAMAGICbinSET(sne,0);
1307 SETs(boolSV(!sv_eq(left, right)));
1314 djSP; dTARGET; tryAMAGICbin(scmp,0);
1317 int cmp = ((PL_op->op_private & OPpLOCALE)
1318 ? sv_cmp_locale(left, right)
1319 : sv_cmp(left, right));
1327 djSP; dATARGET; tryAMAGICbin(band,opASSIGN);
1330 if (SvNIOKp(left) || SvNIOKp(right)) {
1331 if (PL_op->op_private & HINT_INTEGER) {
1332 IBW value = SvIV(left) & SvIV(right);
1336 UBW value = SvUV(left) & SvUV(right);
1341 do_vop(PL_op->op_type, TARG, left, right);
1350 djSP; dATARGET; tryAMAGICbin(bxor,opASSIGN);
1353 if (SvNIOKp(left) || SvNIOKp(right)) {
1354 if (PL_op->op_private & HINT_INTEGER) {
1355 IBW value = (USE_LEFT(left) ? SvIV(left) : 0) ^ SvIV(right);
1359 UBW value = (USE_LEFT(left) ? SvUV(left) : 0) ^ SvUV(right);
1364 do_vop(PL_op->op_type, TARG, left, right);
1373 djSP; dATARGET; tryAMAGICbin(bor,opASSIGN);
1376 if (SvNIOKp(left) || SvNIOKp(right)) {
1377 if (PL_op->op_private & HINT_INTEGER) {
1378 IBW value = (USE_LEFT(left) ? SvIV(left) : 0) | SvIV(right);
1382 UBW value = (USE_LEFT(left) ? SvUV(left) : 0) | SvUV(right);
1387 do_vop(PL_op->op_type, TARG, left, right);
1396 djSP; dTARGET; tryAMAGICun(neg);
1401 if (SvIOKp(sv) && !SvNOKp(sv) && !SvPOKp(sv) && SvIVX(sv) != IV_MIN)
1403 else if (SvNIOKp(sv))
1405 else if (SvPOKp(sv)) {
1407 char *s = SvPV(sv, len);
1408 if (isIDFIRST(*s)) {
1409 sv_setpvn(TARG, "-", 1);
1412 else if (*s == '+' || *s == '-') {
1414 *SvPV_force(TARG, len) = *s == '-' ? '+' : '-';
1416 else if (IN_UTF8 && *(U8*)s >= 0xc0 && isIDFIRST_utf8((U8*)s)) {
1417 sv_setpvn(TARG, "-", 1);
1421 sv_setnv(TARG, -SvNV(sv));
1432 djSP; tryAMAGICunSET(not);
1433 *PL_stack_sp = boolSV(!SvTRUE(*PL_stack_sp));
1439 djSP; dTARGET; tryAMAGICun(compl);
1443 if (PL_op->op_private & HINT_INTEGER) {
1444 IBW value = ~SvIV(sv);
1448 UBW value = ~SvUV(sv);
1453 register char *tmps;
1454 register long *tmpl;
1459 tmps = SvPV_force(TARG, len);
1462 for ( ; anum && (unsigned long)tmps % sizeof(long); anum--, tmps++)
1465 for ( ; anum >= sizeof(long); anum -= sizeof(long), tmpl++)
1469 for ( ; anum > 0; anum--, tmps++)
1478 /* integer versions of some of the above */
1482 djSP; dATARGET; tryAMAGICbin(mult,opASSIGN);
1485 SETi( left * right );
1492 djSP; dATARGET; tryAMAGICbin(div,opASSIGN);
1496 DIE("Illegal division by zero");
1497 value = POPi / value;
1505 djSP; dATARGET; tryAMAGICbin(modulo,opASSIGN);
1509 DIE("Illegal modulus zero");
1510 SETi( left % right );
1517 djSP; dATARGET; tryAMAGICbin(add,opASSIGN);
1520 SETi( left + right );
1527 djSP; dATARGET; tryAMAGICbin(subtr,opASSIGN);
1530 SETi( left - right );
1537 djSP; tryAMAGICbinSET(lt,0);
1540 SETs(boolSV(left < right));
1547 djSP; tryAMAGICbinSET(gt,0);
1550 SETs(boolSV(left > right));
1557 djSP; tryAMAGICbinSET(le,0);
1560 SETs(boolSV(left <= right));
1567 djSP; tryAMAGICbinSET(ge,0);
1570 SETs(boolSV(left >= right));
1577 djSP; tryAMAGICbinSET(eq,0);
1580 SETs(boolSV(left == right));
1587 djSP; tryAMAGICbinSET(ne,0);
1590 SETs(boolSV(left != right));
1597 djSP; dTARGET; tryAMAGICbin(ncmp,0);
1604 else if (left < right)
1615 djSP; dTARGET; tryAMAGICun(neg);
1620 /* High falutin' math. */
1624 djSP; dTARGET; tryAMAGICbin(atan2,0);
1627 SETn(atan2(left, right));
1634 djSP; dTARGET; tryAMAGICun(sin);
1646 djSP; dTARGET; tryAMAGICun(cos);
1656 /* Support Configure command-line overrides for rand() functions.
1657 After 5.005, perhaps we should replace this by Configure support
1658 for drand48(), random(), or rand(). For 5.005, though, maintain
1659 compatibility by calling rand() but allow the user to override it.
1660 See INSTALL for details. --Andy Dougherty 15 July 1998
1662 /* Now it's after 5.005, and Configure supports drand48() and random(),
1663 in addition to rand(). So the overrides should not be needed any more.
1664 --Jarkko Hietaniemi 27 September 1998
1667 #ifndef HAS_DRAND48_PROTO
1668 extern double drand48 (void);
1681 if (!PL_srand_called) {
1682 (void)seedDrand01((Rand_seed_t)seed());
1683 PL_srand_called = TRUE;
1698 (void)seedDrand01((Rand_seed_t)anum);
1699 PL_srand_called = TRUE;
1708 * This is really just a quick hack which grabs various garbage
1709 * values. It really should be a real hash algorithm which
1710 * spreads the effect of every input bit onto every output bit,
1711 * if someone who knows about such things would bother to write it.
1712 * Might be a good idea to add that function to CORE as well.
1713 * No numbers below come from careful analysis or anything here,
1714 * except they are primes and SEED_C1 > 1E6 to get a full-width
1715 * value from (tv_sec * SEED_C1 + tv_usec). The multipliers should
1716 * probably be bigger too.
1719 # define SEED_C1 1000003
1720 #define SEED_C4 73819
1722 # define SEED_C1 25747
1723 #define SEED_C4 20639
1727 #define SEED_C5 26107
1730 #ifndef PERL_NO_DEV_RANDOM
1735 # include <starlet.h>
1736 /* when[] = (low 32 bits, high 32 bits) of time since epoch
1737 * in 100-ns units, typically incremented ever 10 ms. */
1738 unsigned int when[2];
1740 # ifdef HAS_GETTIMEOFDAY
1741 struct timeval when;
1747 /* This test is an escape hatch, this symbol isn't set by Configure. */
1748 #ifndef PERL_NO_DEV_RANDOM
1749 #ifndef PERL_RANDOM_DEVICE
1750 /* /dev/random isn't used by default because reads from it will block
1751 * if there isn't enough entropy available. You can compile with
1752 * PERL_RANDOM_DEVICE to it if you'd prefer Perl to block until there
1753 * is enough real entropy to fill the seed. */
1754 # define PERL_RANDOM_DEVICE "/dev/urandom"
1756 fd = PerlLIO_open(PERL_RANDOM_DEVICE, 0);
1758 if (PerlLIO_read(fd, &u, sizeof u) != sizeof u)
1767 _ckvmssts(sys$gettim(when));
1768 u = (U32)SEED_C1 * when[0] + (U32)SEED_C2 * when[1];
1770 # ifdef HAS_GETTIMEOFDAY
1771 gettimeofday(&when,(struct timezone *) 0);
1772 u = (U32)SEED_C1 * when.tv_sec + (U32)SEED_C2 * when.tv_usec;
1775 u = (U32)SEED_C1 * when;
1778 u += SEED_C3 * (U32)getpid();
1779 u += SEED_C4 * (U32)(UV)PL_stack_sp;
1780 #ifndef PLAN9 /* XXX Plan9 assembler chokes on this; fix needed */
1781 u += SEED_C5 * (U32)(UV)&when;
1788 djSP; dTARGET; tryAMAGICun(exp);
1800 djSP; dTARGET; tryAMAGICun(log);
1805 SET_NUMERIC_STANDARD();
1806 DIE("Can't take log of %g", value);
1816 djSP; dTARGET; tryAMAGICun(sqrt);
1821 SET_NUMERIC_STANDARD();
1822 DIE("Can't take sqrt of %g", value);
1824 value = sqrt(value);
1834 double value = TOPn;
1837 if (SvIOKp(TOPs) && !SvNOKp(TOPs) && !SvPOKp(TOPs)) {
1843 (void)modf(value, &value);
1845 (void)modf(-value, &value);
1860 djSP; dTARGET; tryAMAGICun(abs);
1862 double value = TOPn;
1865 if (SvIOKp(TOPs) && !SvNOKp(TOPs) && !SvPOKp(TOPs) &&
1866 (iv = SvIVX(TOPs)) != IV_MIN) {
1888 XPUSHu(scan_hex(tmps, 99, &argtype));
1901 while (*tmps && isSPACE(*tmps))
1906 value = scan_hex(++tmps, 99, &argtype);
1907 else if (*tmps == 'b')
1908 value = scan_bin(++tmps, 99, &argtype);
1910 value = scan_oct(tmps, 99, &argtype);
1922 SETi( sv_len_utf8(TOPs) );
1926 SETi( sv_len(TOPs) );
1940 I32 lvalue = PL_op->op_flags & OPf_MOD;
1942 I32 arybase = PL_curcop->cop_arybase;
1946 SvTAINTED_off(TARG); /* decontaminate */
1950 repl = SvPV(sv, repl_len);
1957 tmps = SvPV(sv, curlen);
1959 utfcurlen = sv_len_utf8(sv);
1960 if (utfcurlen == curlen)
1968 if (pos >= arybase) {
1986 else if (len >= 0) {
1988 if (rem > (I32)curlen)
2002 if (ckWARN(WARN_SUBSTR) || lvalue || repl)
2003 warner(WARN_SUBSTR, "substr outside of string");
2008 sv_pos_u2b(sv, &pos, &rem);
2010 sv_setpvn(TARG, tmps, rem);
2011 if (lvalue) { /* it's an lvalue! */
2012 if (!SvGMAGICAL(sv)) {
2016 if (ckWARN(WARN_SUBSTR))
2018 "Attempt to use reference as lvalue in substr");
2020 if (SvOK(sv)) /* is it defined ? */
2021 (void)SvPOK_only(sv);
2023 sv_setpvn(sv,"",0); /* avoid lexical reincarnation */
2026 if (SvTYPE(TARG) < SVt_PVLV) {
2027 sv_upgrade(TARG, SVt_PVLV);
2028 sv_magic(TARG, Nullsv, 'x', Nullch, 0);
2032 if (LvTARG(TARG) != sv) {
2034 SvREFCNT_dec(LvTARG(TARG));
2035 LvTARG(TARG) = SvREFCNT_inc(sv);
2037 LvTARGOFF(TARG) = pos;
2038 LvTARGLEN(TARG) = rem;
2041 sv_insert(sv, pos, rem, repl, repl_len);
2044 PUSHs(TARG); /* avoid SvSETMAGIC here */
2051 register I32 size = POPi;
2052 register I32 offset = POPi;
2053 register SV *src = POPs;
2054 I32 lvalue = PL_op->op_flags & OPf_MOD;
2056 unsigned char *s = (unsigned char*)SvPV(src, srclen);
2057 unsigned long retnum;
2060 SvTAINTED_off(TARG); /* decontaminate */
2061 offset *= size; /* turn into bit offset */
2062 len = (offset + size + 7) / 8;
2063 if (offset < 0 || size < 1)
2066 if (lvalue) { /* it's an lvalue! */
2067 if (SvTYPE(TARG) < SVt_PVLV) {
2068 sv_upgrade(TARG, SVt_PVLV);
2069 sv_magic(TARG, Nullsv, 'v', Nullch, 0);
2073 if (LvTARG(TARG) != src) {
2075 SvREFCNT_dec(LvTARG(TARG));
2076 LvTARG(TARG) = SvREFCNT_inc(src);
2078 LvTARGOFF(TARG) = offset;
2079 LvTARGLEN(TARG) = size;
2087 if (offset >= srclen)
2090 retnum = (unsigned long) s[offset] << 8;
2092 else if (size == 32) {
2093 if (offset >= srclen)
2095 else if (offset + 1 >= srclen)
2096 retnum = (unsigned long) s[offset] << 24;
2097 else if (offset + 2 >= srclen)
2098 retnum = ((unsigned long) s[offset] << 24) +
2099 ((unsigned long) s[offset + 1] << 16);
2101 retnum = ((unsigned long) s[offset] << 24) +
2102 ((unsigned long) s[offset + 1] << 16) +
2103 (s[offset + 2] << 8);
2108 retnum = (s[offset >> 3] >> (offset & 7)) & ((1 << size) - 1);
2113 else if (size == 16)
2114 retnum = ((unsigned long) s[offset] << 8) + s[offset+1];
2115 else if (size == 32)
2116 retnum = ((unsigned long) s[offset] << 24) +
2117 ((unsigned long) s[offset + 1] << 16) +
2118 (s[offset + 2] << 8) + s[offset+3];
2122 sv_setuv(TARG, (UV)retnum);
2137 I32 arybase = PL_curcop->cop_arybase;
2142 offset = POPi - arybase;
2145 tmps = SvPV(big, biglen);
2146 if (IN_UTF8 && offset > 0)
2147 sv_pos_u2b(big, &offset, 0);
2150 else if (offset > biglen)
2152 if (!(tmps2 = fbm_instr((unsigned char*)tmps + offset,
2153 (unsigned char*)tmps + biglen, little, 0)))
2156 retval = tmps2 - tmps;
2157 if (IN_UTF8 && retval > 0)
2158 sv_pos_b2u(big, &retval);
2159 PUSHi(retval + arybase);
2174 I32 arybase = PL_curcop->cop_arybase;
2180 tmps2 = SvPV(little, llen);
2181 tmps = SvPV(big, blen);
2185 if (IN_UTF8 && offset > 0)
2186 sv_pos_u2b(big, &offset, 0);
2187 offset = offset - arybase + llen;
2191 else if (offset > blen)
2193 if (!(tmps2 = rninstr(tmps, tmps + offset,
2194 tmps2, tmps2 + llen)))
2197 retval = tmps2 - tmps;
2198 if (IN_UTF8 && retval > 0)
2199 sv_pos_b2u(big, &retval);
2200 PUSHi(retval + arybase);
2206 djSP; dMARK; dORIGMARK; dTARGET;
2207 #ifdef USE_LOCALE_NUMERIC
2208 if (PL_op->op_private & OPpLOCALE)
2209 SET_NUMERIC_LOCAL();
2211 SET_NUMERIC_STANDARD();
2213 do_sprintf(TARG, SP-MARK, MARK+1);
2214 TAINT_IF(SvTAINTED(TARG));
2225 U8 *tmps = (U8*)POPpx;
2228 if (IN_UTF8 && (*tmps & 0x80))
2229 value = utf8_to_uv(tmps, &retlen);
2231 value = (UV)(*tmps & 255);
2242 (void)SvUPGRADE(TARG,SVt_PV);
2244 if (IN_UTF8 && value >= 128) {
2247 tmps = (char*)uv_to_utf8((U8*)tmps, (UV)value);
2248 SvCUR_set(TARG, tmps - SvPVX(TARG));
2250 (void)SvPOK_only(TARG);
2260 (void)SvPOK_only(TARG);
2267 djSP; dTARGET; dPOPTOPssrl;
2270 char *tmps = SvPV(left, n_a);
2272 sv_setpv(TARG, fcrypt(tmps, SvPV(right, n_a)));
2274 sv_setpv(TARG, PerlProc_crypt(tmps, SvPV(right, n_a)));
2278 "The crypt() function is unimplemented due to excessive paranoia.");
2291 if (IN_UTF8 && (s = (U8*)SvPV(sv, slen)) && slen && (*s & 0xc0) == 0xc0) {
2295 UV uv = utf8_to_uv(s, &ulen);
2297 if (PL_op->op_private & OPpLOCALE) {
2300 uv = toTITLE_LC_uni(uv);
2303 uv = toTITLE_utf8(s);
2305 tend = uv_to_utf8(tmpbuf, uv);
2307 if (!SvPADTMP(sv) || tend - tmpbuf != ulen) {
2309 sv_setpvn(TARG, (char*)tmpbuf, tend - tmpbuf);
2310 sv_catpvn(TARG, (char*)(s + ulen), slen - ulen);
2314 s = (U8*)SvPV_force(sv, slen);
2315 Copy(tmpbuf, s, ulen, U8);
2320 if (!SvPADTMP(sv)) {
2326 s = (U8*)SvPV_force(sv, slen);
2328 if (PL_op->op_private & OPpLOCALE) {
2331 *s = toUPPER_LC(*s);
2347 if (IN_UTF8 && (s = (U8*)SvPV(sv, slen)) && slen && (*s & 0xc0) == 0xc0) {
2351 UV uv = utf8_to_uv(s, &ulen);
2353 if (PL_op->op_private & OPpLOCALE) {
2356 uv = toLOWER_LC_uni(uv);
2359 uv = toLOWER_utf8(s);
2361 tend = uv_to_utf8(tmpbuf, uv);
2363 if (!SvPADTMP(sv) || tend - tmpbuf != ulen) {
2365 sv_setpvn(TARG, (char*)tmpbuf, tend - tmpbuf);
2366 sv_catpvn(TARG, (char*)(s + ulen), slen - ulen);
2370 s = (U8*)SvPV_force(sv, slen);
2371 Copy(tmpbuf, s, ulen, U8);
2376 if (!SvPADTMP(sv)) {
2382 s = (U8*)SvPV_force(sv, slen);
2384 if (PL_op->op_private & OPpLOCALE) {
2387 *s = toLOWER_LC(*s);
2410 s = (U8*)SvPV(sv,len);
2412 sv_setpvn(TARG, "", 0);
2417 (void)SvUPGRADE(TARG, SVt_PV);
2418 SvGROW(TARG, (len * 2) + 1);
2419 (void)SvPOK_only(TARG);
2420 d = (U8*)SvPVX(TARG);
2422 if (PL_op->op_private & OPpLOCALE) {
2426 d = uv_to_utf8(d, toUPPER_LC_uni( utf8_to_uv(s, &ulen)));
2432 d = uv_to_utf8(d, toUPPER_utf8( s ));
2437 SvCUR_set(TARG, d - (U8*)SvPVX(TARG));
2442 if (!SvPADTMP(sv)) {
2449 s = (U8*)SvPV_force(sv, len);
2451 register U8 *send = s + len;
2453 if (PL_op->op_private & OPpLOCALE) {
2456 for (; s < send; s++)
2457 *s = toUPPER_LC(*s);
2460 for (; s < send; s++)
2480 s = (U8*)SvPV(sv,len);
2482 sv_setpvn(TARG, "", 0);
2487 (void)SvUPGRADE(TARG, SVt_PV);
2488 SvGROW(TARG, (len * 2) + 1);
2489 (void)SvPOK_only(TARG);
2490 d = (U8*)SvPVX(TARG);
2492 if (PL_op->op_private & OPpLOCALE) {
2496 d = uv_to_utf8(d, toLOWER_LC_uni( utf8_to_uv(s, &ulen)));
2502 d = uv_to_utf8(d, toLOWER_utf8(s));
2507 SvCUR_set(TARG, d - (U8*)SvPVX(TARG));
2512 if (!SvPADTMP(sv)) {
2519 s = (U8*)SvPV_force(sv, len);
2521 register U8 *send = s + len;
2523 if (PL_op->op_private & OPpLOCALE) {
2526 for (; s < send; s++)
2527 *s = toLOWER_LC(*s);
2530 for (; s < send; s++)
2542 register char *s = SvPV(sv,len);
2546 (void)SvUPGRADE(TARG, SVt_PV);
2547 SvGROW(TARG, (len * 2) + 1);
2552 STRLEN ulen = UTF8SKIP(s);
2575 SvCUR_set(TARG, d - SvPVX(TARG));
2576 (void)SvPOK_only(TARG);
2579 sv_setpvn(TARG, s, len);
2588 djSP; dMARK; dORIGMARK;
2590 register AV* av = (AV*)POPs;
2591 register I32 lval = PL_op->op_flags & OPf_MOD;
2592 I32 arybase = PL_curcop->cop_arybase;
2595 if (SvTYPE(av) == SVt_PVAV) {
2596 if (lval && PL_op->op_private & OPpLVAL_INTRO) {
2598 for (svp = MARK + 1; svp <= SP; svp++) {
2603 if (max > AvMAX(av))
2606 while (++MARK <= SP) {
2607 elem = SvIVx(*MARK);
2611 svp = av_fetch(av, elem, lval);
2613 if (!svp || *svp == &PL_sv_undef)
2614 DIE(PL_no_aelem, elem);
2615 if (PL_op->op_private & OPpLVAL_INTRO)
2616 save_aelem(av, elem, svp);
2618 *MARK = svp ? *svp : &PL_sv_undef;
2621 if (GIMME != G_ARRAY) {
2629 /* Associative arrays. */
2634 HV *hash = (HV*)POPs;
2636 I32 gimme = GIMME_V;
2637 I32 realhv = (SvTYPE(hash) == SVt_PVHV);
2640 /* might clobber stack_sp */
2641 entry = realhv ? hv_iternext(hash) : avhv_iternext((AV*)hash);
2646 PUSHs(hv_iterkeysv(entry)); /* won't clobber stack_sp */
2647 if (gimme == G_ARRAY) {
2649 /* might clobber stack_sp */
2650 sv_setsv(TARG, realhv ?
2651 hv_iterval(hash, entry) : avhv_iterval((AV*)hash, entry));
2656 else if (gimme == G_SCALAR)
2675 I32 gimme = GIMME_V;
2676 I32 discard = (gimme == G_VOID) ? G_DISCARD : 0;
2680 if (PL_op->op_private & OPpSLICE) {
2684 hvtype = SvTYPE(hv);
2685 while (++MARK <= SP) {
2686 if (hvtype == SVt_PVHV)
2687 sv = hv_delete_ent(hv, *MARK, discard, 0);
2689 DIE("Not a HASH reference");
2690 *MARK = sv ? sv : &PL_sv_undef;
2694 else if (gimme == G_SCALAR) {
2703 if (SvTYPE(hv) == SVt_PVHV)
2704 sv = hv_delete_ent(hv, keysv, discard, 0);
2706 DIE("Not a HASH reference");
2720 if (SvTYPE(hv) == SVt_PVHV) {
2721 if (hv_exists_ent(hv, tmpsv, 0))
2724 else if (SvTYPE(hv) == SVt_PVAV) {
2725 if (avhv_exists_ent((AV*)hv, tmpsv, 0))
2729 DIE("Not a HASH reference");
2736 djSP; dMARK; dORIGMARK;
2737 register HV *hv = (HV*)POPs;
2738 register I32 lval = PL_op->op_flags & OPf_MOD;
2739 I32 realhv = (SvTYPE(hv) == SVt_PVHV);
2741 if (!realhv && PL_op->op_private & OPpLVAL_INTRO)
2742 DIE("Can't localize pseudo-hash element");
2744 if (realhv || SvTYPE(hv) == SVt_PVAV) {
2745 while (++MARK <= SP) {
2749 HE *he = hv_fetch_ent(hv, keysv, lval, 0);
2750 svp = he ? &HeVAL(he) : 0;
2753 svp = avhv_fetch_ent((AV*)hv, keysv, lval, 0);
2756 if (!svp || *svp == &PL_sv_undef) {
2758 DIE(PL_no_helem, SvPV(keysv, n_a));
2760 if (PL_op->op_private & OPpLVAL_INTRO)
2761 save_helem(hv, keysv, svp);
2763 *MARK = svp ? *svp : &PL_sv_undef;
2766 if (GIMME != G_ARRAY) {
2774 /* List operators. */
2779 if (GIMME != G_ARRAY) {
2781 *MARK = *SP; /* unwanted list, return last item */
2783 *MARK = &PL_sv_undef;
2792 SV **lastrelem = PL_stack_sp;
2793 SV **lastlelem = PL_stack_base + POPMARK;
2794 SV **firstlelem = PL_stack_base + POPMARK + 1;
2795 register SV **firstrelem = lastlelem + 1;
2796 I32 arybase = PL_curcop->cop_arybase;
2797 I32 lval = PL_op->op_flags & OPf_MOD;
2798 I32 is_something_there = lval;
2800 register I32 max = lastrelem - lastlelem;
2801 register SV **lelem;
2804 if (GIMME != G_ARRAY) {
2805 ix = SvIVx(*lastlelem);
2810 if (ix < 0 || ix >= max)
2811 *firstlelem = &PL_sv_undef;
2813 *firstlelem = firstrelem[ix];
2819 SP = firstlelem - 1;
2823 for (lelem = firstlelem; lelem <= lastlelem; lelem++) {
2829 if (ix < 0 || ix >= max)
2830 *lelem = &PL_sv_undef;
2832 is_something_there = TRUE;
2833 if (!(*lelem = firstrelem[ix]))
2834 *lelem = &PL_sv_undef;
2837 if (is_something_there)
2840 SP = firstlelem - 1;
2846 djSP; dMARK; dORIGMARK;
2847 I32 items = SP - MARK;
2848 SV *av = sv_2mortal((SV*)av_make(items, MARK+1));
2849 SP = ORIGMARK; /* av_make() might realloc stack_sp */
2856 djSP; dMARK; dORIGMARK;
2857 HV* hv = (HV*)sv_2mortal((SV*)newHV());
2861 SV *val = NEWSV(46, 0);
2863 sv_setsv(val, *++MARK);
2864 else if (ckWARN(WARN_UNSAFE))
2865 warner(WARN_UNSAFE, "Odd number of elements in hash assignment");
2866 (void)hv_store_ent(hv,key,val,0);
2875 djSP; dMARK; dORIGMARK;
2876 register AV *ary = (AV*)*++MARK;
2880 register I32 offset;
2881 register I32 length;
2888 if (mg = SvTIED_mg((SV*)ary, 'P')) {
2889 *MARK-- = SvTIED_obj((SV*)ary, mg);
2893 call_method("SPLICE",GIMME_V);
2902 offset = i = SvIVx(*MARK);
2904 offset += AvFILLp(ary) + 1;
2906 offset -= PL_curcop->cop_arybase;
2908 DIE(PL_no_aelem, i);
2910 length = SvIVx(*MARK++);
2912 length += AvFILLp(ary) - offset + 1;
2918 length = AvMAX(ary) + 1; /* close enough to infinity */
2922 length = AvMAX(ary) + 1;
2924 if (offset > AvFILLp(ary) + 1)
2925 offset = AvFILLp(ary) + 1;
2926 after = AvFILLp(ary) + 1 - (offset + length);
2927 if (after < 0) { /* not that much array */
2928 length += after; /* offset+length now in array */
2934 /* At this point, MARK .. SP-1 is our new LIST */
2937 diff = newlen - length;
2938 if (newlen && !AvREAL(ary) && AvREIFY(ary))
2941 if (diff < 0) { /* shrinking the area */
2943 New(451, tmparyval, newlen, SV*); /* so remember insertion */
2944 Copy(MARK, tmparyval, newlen, SV*);
2947 MARK = ORIGMARK + 1;
2948 if (GIMME == G_ARRAY) { /* copy return vals to stack */
2949 MEXTEND(MARK, length);
2950 Copy(AvARRAY(ary)+offset, MARK, length, SV*);
2952 EXTEND_MORTAL(length);
2953 for (i = length, dst = MARK; i; i--) {
2954 sv_2mortal(*dst); /* free them eventualy */
2961 *MARK = AvARRAY(ary)[offset+length-1];
2964 for (i = length - 1, dst = &AvARRAY(ary)[offset]; i > 0; i--)
2965 SvREFCNT_dec(*dst++); /* free them now */
2968 AvFILLp(ary) += diff;
2970 /* pull up or down? */
2972 if (offset < after) { /* easier to pull up */
2973 if (offset) { /* esp. if nothing to pull */
2974 src = &AvARRAY(ary)[offset-1];
2975 dst = src - diff; /* diff is negative */
2976 for (i = offset; i > 0; i--) /* can't trust Copy */
2980 SvPVX(ary) = (char*)(AvARRAY(ary) - diff); /* diff is negative */
2984 if (after) { /* anything to pull down? */
2985 src = AvARRAY(ary) + offset + length;
2986 dst = src + diff; /* diff is negative */
2987 Move(src, dst, after, SV*);
2989 dst = &AvARRAY(ary)[AvFILLp(ary)+1];
2990 /* avoid later double free */
2994 dst[--i] = &PL_sv_undef;
2997 for (src = tmparyval, dst = AvARRAY(ary) + offset;
2999 *dst = NEWSV(46, 0);
3000 sv_setsv(*dst++, *src++);
3002 Safefree(tmparyval);
3005 else { /* no, expanding (or same) */
3007 New(452, tmparyval, length, SV*); /* so remember deletion */
3008 Copy(AvARRAY(ary)+offset, tmparyval, length, SV*);
3011 if (diff > 0) { /* expanding */
3013 /* push up or down? */
3015 if (offset < after && diff <= AvARRAY(ary) - AvALLOC(ary)) {
3019 Move(src, dst, offset, SV*);
3021 SvPVX(ary) = (char*)(AvARRAY(ary) - diff);/* diff is positive */
3023 AvFILLp(ary) += diff;
3026 if (AvFILLp(ary) + diff >= AvMAX(ary)) /* oh, well */
3027 av_extend(ary, AvFILLp(ary) + diff);
3028 AvFILLp(ary) += diff;
3031 dst = AvARRAY(ary) + AvFILLp(ary);
3033 for (i = after; i; i--) {
3040 for (src = MARK, dst = AvARRAY(ary) + offset; newlen; newlen--) {
3041 *dst = NEWSV(46, 0);
3042 sv_setsv(*dst++, *src++);
3044 MARK = ORIGMARK + 1;
3045 if (GIMME == G_ARRAY) { /* copy return vals to stack */
3047 Copy(tmparyval, MARK, length, SV*);
3049 EXTEND_MORTAL(length);
3050 for (i = length, dst = MARK; i; i--) {
3051 sv_2mortal(*dst); /* free them eventualy */
3055 Safefree(tmparyval);
3059 else if (length--) {
3060 *MARK = tmparyval[length];
3063 while (length-- > 0)
3064 SvREFCNT_dec(tmparyval[length]);
3066 Safefree(tmparyval);
3069 *MARK = &PL_sv_undef;
3077 djSP; dMARK; dORIGMARK; dTARGET;
3078 register AV *ary = (AV*)*++MARK;
3079 register SV *sv = &PL_sv_undef;
3082 if (mg = SvTIED_mg((SV*)ary, 'P')) {
3083 *MARK-- = SvTIED_obj((SV*)ary, mg);
3087 call_method("PUSH",G_SCALAR|G_DISCARD);
3092 /* Why no pre-extend of ary here ? */
3093 for (++MARK; MARK <= SP; MARK++) {
3096 sv_setsv(sv, *MARK);
3101 PUSHi( AvFILL(ary) + 1 );
3109 SV *sv = av_pop(av);
3111 (void)sv_2mortal(sv);
3120 SV *sv = av_shift(av);
3125 (void)sv_2mortal(sv);
3132 djSP; dMARK; dORIGMARK; dTARGET;
3133 register AV *ary = (AV*)*++MARK;
3138 if (mg = SvTIED_mg((SV*)ary, 'P')) {
3139 *MARK-- = SvTIED_obj((SV*)ary, mg);
3143 call_method("UNSHIFT",G_SCALAR|G_DISCARD);
3148 av_unshift(ary, SP - MARK);
3151 sv_setsv(sv, *++MARK);
3152 (void)av_store(ary, i++, sv);
3156 PUSHi( AvFILL(ary) + 1 );
3166 if (GIMME == G_ARRAY) {
3177 register char *down;
3183 do_join(TARG, &PL_sv_no, MARK, SP);
3185 sv_setsv(TARG, (SP > MARK) ? *SP : DEFSV);
3186 up = SvPV_force(TARG, len);
3188 if (IN_UTF8) { /* first reverse each character */
3189 U8* s = (U8*)SvPVX(TARG);
3190 U8* send = (U8*)(s + len);
3199 down = (char*)(s - 1);
3200 if (s > send || !((*down & 0xc0) == 0x80)) {
3201 warn("Malformed UTF-8 character");
3213 down = SvPVX(TARG) + len - 1;
3219 (void)SvPOK_only(TARG);
3228 mul128(pTHX_ SV *sv, U8 m)
3231 char *s = SvPV(sv, len);
3235 if (!strnEQ(s, "0000", 4)) { /* need to grow sv */
3236 SV *tmpNew = newSVpvn("0000000000", 10);
3238 sv_catsv(tmpNew, sv);
3239 SvREFCNT_dec(sv); /* free old sv */
3244 while (!*t) /* trailing '\0'? */
3247 i = ((*t - '0') << 7) + m;
3248 *(t--) = '0' + (i % 10);
3254 /* Explosives and implosives. */
3256 #if 'I' == 73 && 'J' == 74
3257 /* On an ASCII/ISO kind of system */
3258 #define ISUUCHAR(ch) ((ch) >= ' ' && (ch) < 'a')
3261 Some other sort of character set - use memchr() so we don't match
3264 #define ISUUCHAR(ch) (memchr(PL_uuemap, (ch), sizeof(PL_uuemap)-1) || (ch) == ' ')
3272 I32 gimme = GIMME_V;
3276 register char *pat = SvPV(left, llen);
3277 register char *s = SvPV(right, rlen);
3278 char *strend = s + rlen;
3280 register char *patend = pat + llen;
3285 /* These must not be in registers: */
3302 register U32 culong;
3305 #ifdef PERL_NATINT_PACK
3306 int natint; /* native integer */
3307 int unatint; /* unsigned native integer */
3310 if (gimme != G_ARRAY) { /* arrange to do first one only */
3312 for (patend = pat; !isALPHA(*patend) || *patend == 'x'; patend++) ;
3313 if (strchr("aAZbBhHP", *patend) || *pat == '%') {
3315 while (isDIGIT(*patend) || *patend == '*')
3321 while (pat < patend) {
3323 datumtype = *pat++ & 0xFF;
3324 #ifdef PERL_NATINT_PACK
3327 if (isSPACE(datumtype))
3330 char *natstr = "sSiIlL";
3332 if (strchr(natstr, datumtype)) {
3333 #ifdef PERL_NATINT_PACK
3339 croak("'!' allowed only after types %s", natstr);
3343 else if (*pat == '*') {
3344 len = strend - strbeg; /* long enough */
3347 else if (isDIGIT(*pat)) {
3349 while (isDIGIT(*pat))
3350 len = (len * 10) + (*pat++ - '0');
3353 len = (datumtype != '@');
3356 croak("Invalid type in unpack: '%c'", (int)datumtype);
3357 case ',': /* grandfather in commas but with a warning */
3358 if (commas++ == 0 && ckWARN(WARN_UNSAFE))
3359 warner(WARN_UNSAFE, "Invalid type in unpack: '%c'", (int)datumtype);
3362 if (len == 1 && pat[-1] != '1')
3371 if (len > strend - strbeg)
3372 DIE("@ outside of string");
3376 if (len > s - strbeg)
3377 DIE("X outside of string");
3381 if (len > strend - s)
3382 DIE("x outside of string");
3388 if (len > strend - s)
3391 goto uchar_checksum;
3392 sv = NEWSV(35, len);
3393 sv_setpvn(sv, s, len);
3395 if (datumtype == 'A' || datumtype == 'Z') {
3396 aptr = s; /* borrow register */
3397 if (datumtype == 'Z') { /* 'Z' strips stuff after first null */
3402 else { /* 'A' strips both nulls and spaces */
3403 s = SvPVX(sv) + len - 1;
3404 while (s >= SvPVX(sv) && (!*s || isSPACE(*s)))
3408 SvCUR_set(sv, s - SvPVX(sv));
3409 s = aptr; /* unborrow register */
3411 XPUSHs(sv_2mortal(sv));
3415 if (pat[-1] == '*' || len > (strend - s) * 8)
3416 len = (strend - s) * 8;
3419 Newz(601, PL_bitcount, 256, char);
3420 for (bits = 1; bits < 256; bits++) {
3421 if (bits & 1) PL_bitcount[bits]++;
3422 if (bits & 2) PL_bitcount[bits]++;
3423 if (bits & 4) PL_bitcount[bits]++;
3424 if (bits & 8) PL_bitcount[bits]++;
3425 if (bits & 16) PL_bitcount[bits]++;
3426 if (bits & 32) PL_bitcount[bits]++;
3427 if (bits & 64) PL_bitcount[bits]++;
3428 if (bits & 128) PL_bitcount[bits]++;
3432 culong += PL_bitcount[*(unsigned char*)s++];
3437 if (datumtype == 'b') {
3439 if (bits & 1) culong++;
3445 if (bits & 128) culong++;
3452 sv = NEWSV(35, len + 1);
3455 aptr = pat; /* borrow register */
3457 if (datumtype == 'b') {
3459 for (len = 0; len < aint; len++) {
3460 if (len & 7) /*SUPPRESS 595*/
3464 *pat++ = '0' + (bits & 1);
3469 for (len = 0; len < aint; len++) {
3474 *pat++ = '0' + ((bits & 128) != 0);
3478 pat = aptr; /* unborrow register */
3479 XPUSHs(sv_2mortal(sv));
3483 if (pat[-1] == '*' || len > (strend - s) * 2)
3484 len = (strend - s) * 2;
3485 sv = NEWSV(35, len + 1);
3488 aptr = pat; /* borrow register */
3490 if (datumtype == 'h') {
3492 for (len = 0; len < aint; len++) {
3497 *pat++ = PL_hexdigit[bits & 15];
3502 for (len = 0; len < aint; len++) {
3507 *pat++ = PL_hexdigit[(bits >> 4) & 15];
3511 pat = aptr; /* unborrow register */
3512 XPUSHs(sv_2mortal(sv));
3515 if (len > strend - s)
3520 if (aint >= 128) /* fake up signed chars */
3530 if (aint >= 128) /* fake up signed chars */
3533 sv_setiv(sv, (IV)aint);
3534 PUSHs(sv_2mortal(sv));
3539 if (len > strend - s)
3554 sv_setiv(sv, (IV)auint);
3555 PUSHs(sv_2mortal(sv));
3560 if (len > strend - s)
3563 while (len-- > 0 && s < strend) {
3564 auint = utf8_to_uv((U8*)s, &along);
3567 cdouble += (double)auint;
3575 while (len-- > 0 && s < strend) {
3576 auint = utf8_to_uv((U8*)s, &along);
3579 sv_setuv(sv, (UV)auint);
3580 PUSHs(sv_2mortal(sv));
3585 #if SHORTSIZE == SIZE16
3586 along = (strend - s) / SIZE16;
3588 along = (strend - s) / (natint ? sizeof(short) : SIZE16);
3593 #if SHORTSIZE != SIZE16
3596 COPYNN(s, &ashort, sizeof(short));
3607 #if SHORTSIZE > SIZE16
3619 #if SHORTSIZE != SIZE16
3622 COPYNN(s, &ashort, sizeof(short));
3625 sv_setiv(sv, (IV)ashort);
3626 PUSHs(sv_2mortal(sv));
3634 #if SHORTSIZE > SIZE16
3640 sv_setiv(sv, (IV)ashort);
3641 PUSHs(sv_2mortal(sv));
3649 #if SHORTSIZE == SIZE16
3650 along = (strend - s) / SIZE16;
3652 unatint = natint && datumtype == 'S';
3653 along = (strend - s) / (unatint ? sizeof(unsigned short) : SIZE16);
3658 #if SHORTSIZE != SIZE16
3661 COPYNN(s, &aushort, sizeof(unsigned short));
3662 s += sizeof(unsigned short);
3670 COPY16(s, &aushort);
3673 if (datumtype == 'n')
3674 aushort = PerlSock_ntohs(aushort);
3677 if (datumtype == 'v')
3678 aushort = vtohs(aushort);
3687 #if SHORTSIZE != SIZE16
3690 COPYNN(s, &aushort, sizeof(unsigned short));
3691 s += sizeof(unsigned short);
3693 sv_setiv(sv, (UV)aushort);
3694 PUSHs(sv_2mortal(sv));
3701 COPY16(s, &aushort);
3705 if (datumtype == 'n')
3706 aushort = PerlSock_ntohs(aushort);
3709 if (datumtype == 'v')
3710 aushort = vtohs(aushort);
3712 sv_setiv(sv, (UV)aushort);
3713 PUSHs(sv_2mortal(sv));
3719 along = (strend - s) / sizeof(int);
3724 Copy(s, &aint, 1, int);
3727 cdouble += (double)aint;
3736 Copy(s, &aint, 1, int);
3740 /* Without the dummy below unpack("i", pack("i",-1))
3741 * return 0xFFffFFff instead of -1 for Digital Unix V4.0
3742 * cc with optimization turned on.
3744 * The bug was detected in
3745 * DEC C V5.8-009 on Digital UNIX V4.0 (Rev. 1091) (V4.0E)
3746 * with optimization (-O4) turned on.
3747 * DEC C V5.2-040 on Digital UNIX V4.0 (Rev. 564) (V4.0B)
3748 * does not have this problem even with -O4.
3750 * This bug was reported as DECC_BUGS 1431
3751 * and tracked internally as GEM_BUGS 7775.
3753 * The bug is fixed in
3754 * Tru64 UNIX V5.0: Compaq C V6.1-006 or later
3755 * UNIX V4.0F support: DEC C V5.9-006 or later
3756 * UNIX V4.0E support: DEC C V5.8-011 or later
3759 * See also few lines later for the same bug.
3762 sv_setiv(sv, (IV)aint) :
3764 sv_setiv(sv, (IV)aint);
3765 PUSHs(sv_2mortal(sv));
3770 along = (strend - s) / sizeof(unsigned int);
3775 Copy(s, &auint, 1, unsigned int);
3776 s += sizeof(unsigned int);
3778 cdouble += (double)auint;
3787 Copy(s, &auint, 1, unsigned int);
3788 s += sizeof(unsigned int);
3791 /* Without the dummy below unpack("I", pack("I",0xFFFFFFFF))
3792 * returns 1.84467440737096e+19 instead of 0xFFFFFFFF.
3793 * See details few lines earlier. */
3795 sv_setuv(sv, (UV)auint) :
3797 sv_setuv(sv, (UV)auint);
3798 PUSHs(sv_2mortal(sv));
3803 #if LONGSIZE == SIZE32
3804 along = (strend - s) / SIZE32;
3806 along = (strend - s) / (natint ? sizeof(long) : SIZE32);
3811 #if LONGSIZE != SIZE32
3814 COPYNN(s, &along, sizeof(long));
3817 cdouble += (double)along;
3827 #if LONGSIZE > SIZE32
3828 if (along > 2147483647)
3829 along -= 4294967296;
3833 cdouble += (double)along;
3842 #if LONGSIZE != SIZE32
3845 COPYNN(s, &along, sizeof(long));
3848 sv_setiv(sv, (IV)along);
3849 PUSHs(sv_2mortal(sv));
3857 #if LONGSIZE > SIZE32
3858 if (along > 2147483647)
3859 along -= 4294967296;
3863 sv_setiv(sv, (IV)along);
3864 PUSHs(sv_2mortal(sv));
3872 #if LONGSIZE == SIZE32
3873 along = (strend - s) / SIZE32;
3875 unatint = natint && datumtype == 'L';
3876 along = (strend - s) / (unatint ? sizeof(unsigned long) : SIZE32);
3881 #if LONGSIZE != SIZE32
3884 COPYNN(s, &aulong, sizeof(unsigned long));
3885 s += sizeof(unsigned long);
3887 cdouble += (double)aulong;
3899 if (datumtype == 'N')
3900 aulong = PerlSock_ntohl(aulong);
3903 if (datumtype == 'V')
3904 aulong = vtohl(aulong);
3907 cdouble += (double)aulong;
3916 #if LONGSIZE != SIZE32
3919 COPYNN(s, &aulong, sizeof(unsigned long));
3920 s += sizeof(unsigned long);
3922 sv_setuv(sv, (UV)aulong);
3923 PUSHs(sv_2mortal(sv));
3933 if (datumtype == 'N')
3934 aulong = PerlSock_ntohl(aulong);
3937 if (datumtype == 'V')
3938 aulong = vtohl(aulong);
3941 sv_setuv(sv, (UV)aulong);
3942 PUSHs(sv_2mortal(sv));
3948 along = (strend - s) / sizeof(char*);
3954 if (sizeof(char*) > strend - s)
3957 Copy(s, &aptr, 1, char*);
3963 PUSHs(sv_2mortal(sv));
3973 while ((len > 0) && (s < strend)) {
3974 auv = (auv << 7) | (*s & 0x7f);
3975 if (!(*s++ & 0x80)) {
3979 PUSHs(sv_2mortal(sv));
3983 else if (++bytes >= sizeof(UV)) { /* promote to string */
3987 sv = newSVpvf("%.*Vu", (int)TYPE_DIGITS(UV), auv);
3988 while (s < strend) {
3989 sv = mul128(sv, *s & 0x7f);
3990 if (!(*s++ & 0x80)) {
3999 PUSHs(sv_2mortal(sv));
4004 if ((s >= strend) && bytes)
4005 croak("Unterminated compressed integer");
4010 if (sizeof(char*) > strend - s)
4013 Copy(s, &aptr, 1, char*);
4018 sv_setpvn(sv, aptr, len);
4019 PUSHs(sv_2mortal(sv));
4023 along = (strend - s) / sizeof(Quad_t);
4029 if (s + sizeof(Quad_t) > strend)
4032 Copy(s, &aquad, 1, Quad_t);
4033 s += sizeof(Quad_t);
4036 if (aquad >= IV_MIN && aquad <= IV_MAX)
4037 sv_setiv(sv, (IV)aquad);
4039 sv_setnv(sv, (double)aquad);
4040 PUSHs(sv_2mortal(sv));
4044 along = (strend - s) / sizeof(Quad_t);
4050 if (s + sizeof(Uquad_t) > strend)
4053 Copy(s, &auquad, 1, Uquad_t);
4054 s += sizeof(Uquad_t);
4057 if (auquad <= UV_MAX)
4058 sv_setuv(sv, (UV)auquad);
4060 sv_setnv(sv, (double)auquad);
4061 PUSHs(sv_2mortal(sv));
4065 /* float and double added gnb@melba.bby.oz.au 22/11/89 */
4068 along = (strend - s) / sizeof(float);
4073 Copy(s, &afloat, 1, float);
4082 Copy(s, &afloat, 1, float);
4085 sv_setnv(sv, (double)afloat);
4086 PUSHs(sv_2mortal(sv));
4092 along = (strend - s) / sizeof(double);
4097 Copy(s, &adouble, 1, double);
4098 s += sizeof(double);
4106 Copy(s, &adouble, 1, double);
4107 s += sizeof(double);
4109 sv_setnv(sv, (double)adouble);
4110 PUSHs(sv_2mortal(sv));
4116 * Initialise the decode mapping. By using a table driven
4117 * algorithm, the code will be character-set independent
4118 * (and just as fast as doing character arithmetic)
4120 if (PL_uudmap['M'] == 0) {
4123 for (i = 0; i < sizeof(PL_uuemap); i += 1)
4124 PL_uudmap[PL_uuemap[i]] = i;
4126 * Because ' ' and '`' map to the same value,
4127 * we need to decode them both the same.
4132 along = (strend - s) * 3 / 4;
4133 sv = NEWSV(42, along);
4136 while (s < strend && *s > ' ' && ISUUCHAR(*s)) {
4141 len = PL_uudmap[*s++] & 077;
4143 if (s < strend && ISUUCHAR(*s))
4144 a = PL_uudmap[*s++] & 077;
4147 if (s < strend && ISUUCHAR(*s))
4148 b = PL_uudmap[*s++] & 077;
4151 if (s < strend && ISUUCHAR(*s))
4152 c = PL_uudmap[*s++] & 077;
4155 if (s < strend && ISUUCHAR(*s))
4156 d = PL_uudmap[*s++] & 077;
4159 hunk[0] = (a << 2) | (b >> 4);
4160 hunk[1] = (b << 4) | (c >> 2);
4161 hunk[2] = (c << 6) | d;
4162 sv_catpvn(sv, hunk, (len > 3) ? 3 : len);
4167 else if (s[1] == '\n') /* possible checksum byte */
4170 XPUSHs(sv_2mortal(sv));
4175 if (strchr("fFdD", datumtype) ||
4176 (checksum > 32 && strchr("iIlLNU", datumtype)) ) {
4180 while (checksum >= 16) {
4184 while (checksum >= 4) {
4190 along = (1 << checksum) - 1;
4191 while (cdouble < 0.0)
4193 cdouble = modf(cdouble / adouble, &trouble) * adouble;
4194 sv_setnv(sv, cdouble);
4197 if (checksum < 32) {
4198 aulong = (1 << checksum) - 1;
4201 sv_setuv(sv, (UV)culong);
4203 XPUSHs(sv_2mortal(sv));
4207 if (SP == oldsp && gimme == G_SCALAR)
4208 PUSHs(&PL_sv_undef);
4213 doencodes(pTHX_ register SV *sv, register char *s, register I32 len)
4217 *hunk = PL_uuemap[len];
4218 sv_catpvn(sv, hunk, 1);
4221 hunk[0] = PL_uuemap[(077 & (*s >> 2))];
4222 hunk[1] = PL_uuemap[(077 & (((*s << 4) & 060) | ((s[1] >> 4) & 017)))];
4223 hunk[2] = PL_uuemap[(077 & (((s[1] << 2) & 074) | ((s[2] >> 6) & 03)))];
4224 hunk[3] = PL_uuemap[(077 & (s[2] & 077))];
4225 sv_catpvn(sv, hunk, 4);
4230 char r = (len > 1 ? s[1] : '\0');
4231 hunk[0] = PL_uuemap[(077 & (*s >> 2))];
4232 hunk[1] = PL_uuemap[(077 & (((*s << 4) & 060) | ((r >> 4) & 017)))];
4233 hunk[2] = PL_uuemap[(077 & ((r << 2) & 074))];
4234 hunk[3] = PL_uuemap[0];
4235 sv_catpvn(sv, hunk, 4);
4237 sv_catpvn(sv, "\n", 1);
4241 is_an_int(pTHX_ char *s, STRLEN l)
4244 SV *result = newSVpvn(s, l);
4245 char *result_c = SvPV(result, n_a); /* convenience */
4246 char *out = result_c;
4256 SvREFCNT_dec(result);
4279 SvREFCNT_dec(result);
4285 SvCUR_set(result, out - result_c);
4289 /* pnum must be '\0' terminated */
4291 div128(pTHX_ SV *pnum, bool *done)
4294 char *s = SvPV(pnum, len);
4303 i = m * 10 + (*t - '0');
4305 r = (i >> 7); /* r < 10 */
4312 SvCUR_set(pnum, (STRLEN) (t - s));
4319 djSP; dMARK; dORIGMARK; dTARGET;
4320 register SV *cat = TARG;
4323 register char *pat = SvPVx(*++MARK, fromlen);
4324 register char *patend = pat + fromlen;
4329 static char null10[] = {0,0,0,0,0,0,0,0,0,0};
4330 static char *space10 = " ";
4332 /* These must not be in registers: */
4347 #ifdef PERL_NATINT_PACK
4348 int natint; /* native integer */
4353 sv_setpvn(cat, "", 0);
4354 while (pat < patend) {
4355 #define NEXTFROM (items-- > 0 ? *MARK++ : &PL_sv_no)
4356 datumtype = *pat++ & 0xFF;
4357 #ifdef PERL_NATINT_PACK
4360 if (isSPACE(datumtype))
4363 char *natstr = "sSiIlL";
4365 if (strchr(natstr, datumtype)) {
4366 #ifdef PERL_NATINT_PACK
4372 croak("'!' allowed only after types %s", natstr);
4375 len = strchr("@Xxu", datumtype) ? 0 : items;
4378 else if (isDIGIT(*pat)) {
4380 while (isDIGIT(*pat))
4381 len = (len * 10) + (*pat++ - '0');
4387 croak("Invalid type in pack: '%c'", (int)datumtype);
4388 case ',': /* grandfather in commas but with a warning */
4389 if (commas++ == 0 && ckWARN(WARN_UNSAFE))
4390 warner(WARN_UNSAFE, "Invalid type in pack: '%c'", (int)datumtype);
4393 DIE("%% may only be used in unpack");
4404 if (SvCUR(cat) < len)
4405 DIE("X outside of string");
4412 sv_catpvn(cat, null10, 10);
4415 sv_catpvn(cat, null10, len);
4421 aptr = SvPV(fromstr, fromlen);
4425 sv_catpvn(cat, aptr, len);
4427 sv_catpvn(cat, aptr, fromlen);
4429 if (datumtype == 'A') {
4431 sv_catpvn(cat, space10, 10);
4434 sv_catpvn(cat, space10, len);
4438 sv_catpvn(cat, null10, 10);
4441 sv_catpvn(cat, null10, len);
4448 char *savepat = pat;
4453 aptr = SvPV(fromstr, fromlen);
4458 SvCUR(cat) += (len+7)/8;
4459 SvGROW(cat, SvCUR(cat) + 1);
4460 aptr = SvPVX(cat) + aint;
4465 if (datumtype == 'B') {
4466 for (len = 0; len++ < aint;) {
4467 items |= *pat++ & 1;
4471 *aptr++ = items & 0xff;
4477 for (len = 0; len++ < aint;) {
4483 *aptr++ = items & 0xff;
4489 if (datumtype == 'B')
4490 items <<= 7 - (aint & 7);
4492 items >>= 7 - (aint & 7);
4493 *aptr++ = items & 0xff;
4495 pat = SvPVX(cat) + SvCUR(cat);
4506 char *savepat = pat;
4511 aptr = SvPV(fromstr, fromlen);
4516 SvCUR(cat) += (len+1)/2;
4517 SvGROW(cat, SvCUR(cat) + 1);
4518 aptr = SvPVX(cat) + aint;
4523 if (datumtype == 'H') {
4524 for (len = 0; len++ < aint;) {
4526 items |= ((*pat++ & 15) + 9) & 15;
4528 items |= *pat++ & 15;
4532 *aptr++ = items & 0xff;
4538 for (len = 0; len++ < aint;) {
4540 items |= (((*pat++ & 15) + 9) & 15) << 4;
4542 items |= (*pat++ & 15) << 4;
4546 *aptr++ = items & 0xff;
4552 *aptr++ = items & 0xff;
4553 pat = SvPVX(cat) + SvCUR(cat);
4565 aint = SvIV(fromstr);
4567 sv_catpvn(cat, &achar, sizeof(char));
4573 auint = SvUV(fromstr);
4574 SvGROW(cat, SvCUR(cat) + 10);
4575 SvCUR_set(cat, (char*)uv_to_utf8((U8*)SvEND(cat),auint)
4580 /* Float and double added by gnb@melba.bby.oz.au 22/11/89 */
4585 afloat = (float)SvNV(fromstr);
4586 sv_catpvn(cat, (char *)&afloat, sizeof (float));
4593 adouble = (double)SvNV(fromstr);
4594 sv_catpvn(cat, (char *)&adouble, sizeof (double));
4600 ashort = (I16)SvIV(fromstr);
4602 ashort = PerlSock_htons(ashort);
4604 CAT16(cat, &ashort);
4610 ashort = (I16)SvIV(fromstr);
4612 ashort = htovs(ashort);
4614 CAT16(cat, &ashort);
4618 #if SHORTSIZE != SIZE16
4620 unsigned short aushort;
4624 aushort = SvUV(fromstr);
4625 sv_catpvn(cat, (char *)&aushort, sizeof(unsigned short));
4635 aushort = (U16)SvUV(fromstr);
4636 CAT16(cat, &aushort);
4642 #if SHORTSIZE != SIZE16
4646 ashort = SvIV(fromstr);
4647 sv_catpvn(cat, (char *)&ashort, sizeof(short));
4655 ashort = (I16)SvIV(fromstr);
4656 CAT16(cat, &ashort);
4663 auint = SvUV(fromstr);
4664 sv_catpvn(cat, (char*)&auint, sizeof(unsigned int));
4670 adouble = floor(SvNV(fromstr));
4673 croak("Cannot compress negative numbers");
4679 #ifdef CXUX_BROKEN_CONSTANT_CONVERT
4680 adouble <= UV_MAX_cxux
4687 char buf[1 + sizeof(UV)];
4688 char *in = buf + sizeof(buf);
4689 UV auv = U_V(adouble);
4692 *--in = (auv & 0x7f) | 0x80;
4695 buf[sizeof(buf) - 1] &= 0x7f; /* clear continue bit */
4696 sv_catpvn(cat, in, (buf + sizeof(buf)) - in);
4698 else if (SvPOKp(fromstr)) { /* decimal string arithmetics */
4699 char *from, *result, *in;
4704 /* Copy string and check for compliance */
4705 from = SvPV(fromstr, len);
4706 if ((norm = is_an_int(from, len)) == NULL)
4707 croak("can compress only unsigned integer");
4709 New('w', result, len, char);
4713 *--in = div128(norm, &done) | 0x80;
4714 result[len - 1] &= 0x7F; /* clear continue bit */
4715 sv_catpvn(cat, in, (result + len) - in);
4717 SvREFCNT_dec(norm); /* free norm */
4719 else if (SvNOKp(fromstr)) {
4720 char buf[sizeof(double) * 2]; /* 8/7 <= 2 */
4721 char *in = buf + sizeof(buf);
4724 double next = floor(adouble / 128);
4725 *--in = (unsigned char)(adouble - (next * 128)) | 0x80;
4726 if (--in < buf) /* this cannot happen ;-) */
4727 croak ("Cannot compress integer");
4729 } while (adouble > 0);
4730 buf[sizeof(buf) - 1] &= 0x7f; /* clear continue bit */
4731 sv_catpvn(cat, in, (buf + sizeof(buf)) - in);
4734 croak("Cannot compress non integer");
4740 aint = SvIV(fromstr);
4741 sv_catpvn(cat, (char*)&aint, sizeof(int));
4747 aulong = SvUV(fromstr);
4749 aulong = PerlSock_htonl(aulong);
4751 CAT32(cat, &aulong);
4757 aulong = SvUV(fromstr);
4759 aulong = htovl(aulong);
4761 CAT32(cat, &aulong);
4765 #if LONGSIZE != SIZE32
4769 aulong = SvUV(fromstr);
4770 sv_catpvn(cat, (char *)&aulong, sizeof(unsigned long));
4778 aulong = SvUV(fromstr);
4779 CAT32(cat, &aulong);
4784 #if LONGSIZE != SIZE32
4788 along = SvIV(fromstr);
4789 sv_catpvn(cat, (char *)&along, sizeof(long));
4797 along = SvIV(fromstr);
4806 auquad = (Uquad_t)SvIV(fromstr);
4807 sv_catpvn(cat, (char*)&auquad, sizeof(Uquad_t));
4813 aquad = (Quad_t)SvIV(fromstr);
4814 sv_catpvn(cat, (char*)&aquad, sizeof(Quad_t));
4817 #endif /* HAS_QUAD */
4819 len = 1; /* assume SV is correct length */
4824 if (fromstr == &PL_sv_undef)
4828 /* XXX better yet, could spirit away the string to
4829 * a safe spot and hang on to it until the result
4830 * of pack() (and all copies of the result) are
4833 if (ckWARN(WARN_UNSAFE) && (SvTEMP(fromstr) || SvPADTMP(fromstr)))
4835 "Attempt to pack pointer to temporary value");
4836 if (SvPOK(fromstr) || SvNIOK(fromstr))
4837 aptr = SvPV(fromstr,n_a);
4839 aptr = SvPV_force(fromstr,n_a);
4841 sv_catpvn(cat, (char*)&aptr, sizeof(char*));
4846 aptr = SvPV(fromstr, fromlen);
4847 SvGROW(cat, fromlen * 4 / 3);
4852 while (fromlen > 0) {
4859 doencodes(cat, aptr, todo);
4878 register I32 limit = POPi; /* note, negative is forever */
4881 register char *s = SvPV(sv, len);
4882 char *strend = s + len;
4884 register REGEXP *rx;
4888 I32 maxiters = (strend - s) + 10;
4891 I32 origlimit = limit;
4894 AV *oldstack = PL_curstack;
4895 I32 gimme = GIMME_V;
4896 I32 oldsave = PL_savestack_ix;
4897 I32 make_mortal = 1;
4898 MAGIC *mg = (MAGIC *) NULL;
4901 Copy(&LvTARGOFF(POPs), &pm, 1, PMOP*);
4906 DIE("panic: do_split");
4907 rx = pm->op_pmregexp;
4909 TAINT_IF((pm->op_pmflags & PMf_LOCALE) &&
4910 (pm->op_pmflags & (PMf_WHITE | PMf_SKIPWHITE)));
4912 if (pm->op_pmreplroot)
4913 ary = GvAVn((GV*)pm->op_pmreplroot);
4914 else if (gimme != G_ARRAY)
4916 ary = (AV*)PL_curpad[0];
4918 ary = GvAVn(PL_defgv);
4919 #endif /* USE_THREADS */
4922 if (ary && (gimme != G_ARRAY || (pm->op_pmflags & PMf_ONCE))) {
4928 if (mg = SvTIED_mg((SV*)ary, 'P')) {
4930 XPUSHs(SvTIED_obj((SV*)ary, mg));
4935 for (i = AvFILLp(ary); i >= 0; i--)
4936 AvARRAY(ary)[i] = &PL_sv_undef; /* don't free mere refs */
4938 /* temporarily switch stacks */
4939 SWITCHSTACK(PL_curstack, ary);
4943 base = SP - PL_stack_base;
4945 if (pm->op_pmflags & PMf_SKIPWHITE) {
4946 if (pm->op_pmflags & PMf_LOCALE) {
4947 while (isSPACE_LC(*s))
4955 if (pm->op_pmflags & (PMf_MULTILINE|PMf_SINGLELINE)) {
4956 SAVEINT(PL_multiline);
4957 PL_multiline = pm->op_pmflags & PMf_MULTILINE;
4961 limit = maxiters + 2;
4962 if (pm->op_pmflags & PMf_WHITE) {
4965 while (m < strend &&
4966 !((pm->op_pmflags & PMf_LOCALE)
4967 ? isSPACE_LC(*m) : isSPACE(*m)))
4972 dstr = NEWSV(30, m-s);
4973 sv_setpvn(dstr, s, m-s);
4979 while (s < strend &&
4980 ((pm->op_pmflags & PMf_LOCALE)
4981 ? isSPACE_LC(*s) : isSPACE(*s)))
4985 else if (strEQ("^", rx->precomp)) {
4988 for (m = s; m < strend && *m != '\n'; m++) ;
4992 dstr = NEWSV(30, m-s);
4993 sv_setpvn(dstr, s, m-s);
5000 else if (rx->check_substr && !rx->nparens
5001 && (rx->reganch & ROPT_CHECK_ALL)
5002 && !(rx->reganch & ROPT_ANCH)) {
5003 int tail = SvTAIL(rx->check_substr) != 0;
5005 i = SvCUR(rx->check_substr);
5006 if (i == 1 && !tail) {
5007 i = *SvPVX(rx->check_substr);
5010 for (m = s; m < strend && *m != i; m++) ;
5013 dstr = NEWSV(30, m-s);
5014 sv_setpvn(dstr, s, m-s);
5023 while (s < strend && --limit &&
5024 (m=fbm_instr((unsigned char*)s, (unsigned char*)strend,
5025 rx->check_substr, PL_multiline ? FBMrf_MULTILINE : 0)) )
5028 dstr = NEWSV(31, m-s);
5029 sv_setpvn(dstr, s, m-s);
5033 s = m + i - tail; /* Fake \n at the end */
5038 maxiters += (strend - s) * rx->nparens;
5039 while (s < strend && --limit &&
5040 CALLREGEXEC(rx, s, strend, orig, 1, sv, NULL, 0))
5042 TAINT_IF(RX_MATCH_TAINTED(rx));
5043 if (RX_MATCH_COPIED(rx) && rx->subbeg != orig) {
5048 strend = s + (strend - m);
5050 m = rx->startp[0] + orig;
5051 dstr = NEWSV(32, m-s);
5052 sv_setpvn(dstr, s, m-s);
5057 for (i = 1; i <= rx->nparens; i++) {
5058 s = rx->startp[i] + orig;
5059 m = rx->endp[i] + orig;
5061 dstr = NEWSV(33, m-s);
5062 sv_setpvn(dstr, s, m-s);
5065 dstr = NEWSV(33, 0);
5071 s = rx->endp[0] + orig;
5075 LEAVE_SCOPE(oldsave);
5076 iters = (SP - PL_stack_base) - base;
5077 if (iters > maxiters)
5080 /* keep field after final delim? */
5081 if (s < strend || (iters && origlimit)) {
5082 dstr = NEWSV(34, strend-s);
5083 sv_setpvn(dstr, s, strend-s);
5089 else if (!origlimit) {
5090 while (iters > 0 && (!TOPs || !SvANY(TOPs) || SvCUR(TOPs) == 0))
5096 SWITCHSTACK(ary, oldstack);
5097 if (SvSMAGICAL(ary)) {
5102 if (gimme == G_ARRAY) {
5104 Copy(AvARRAY(ary), SP + 1, iters, SV*);
5112 call_method("PUSH",G_SCALAR|G_DISCARD);
5115 if (gimme == G_ARRAY) {
5116 /* EXTEND should not be needed - we just popped them */
5118 for (i=0; i < iters; i++) {
5119 SV **svp = av_fetch(ary, i, FALSE);
5120 PUSHs((svp) ? *svp : &PL_sv_undef);
5127 if (gimme == G_ARRAY)
5130 if (iters || !pm->op_pmreplroot) {
5140 Perl_unlock_condpair(pTHX_ void *svv)
5143 MAGIC *mg = mg_find((SV*)svv, 'm');
5146 croak("panic: unlock_condpair unlocking non-mutex");
5147 MUTEX_LOCK(MgMUTEXP(mg));
5148 if (MgOWNER(mg) != thr)
5149 croak("panic: unlock_condpair unlocking mutex that we don't own");
5151 COND_SIGNAL(MgOWNERCONDP(mg));
5152 DEBUG_S(PerlIO_printf(PerlIO_stderr(), "0x%lx: unlock 0x%lx\n",
5153 (unsigned long)thr, (unsigned long)svv);)
5154 MUTEX_UNLOCK(MgMUTEXP(mg));
5156 #endif /* USE_THREADS */
5169 mg = condpair_magic(sv);
5170 MUTEX_LOCK(MgMUTEXP(mg));
5171 if (MgOWNER(mg) == thr)
5172 MUTEX_UNLOCK(MgMUTEXP(mg));
5175 COND_WAIT(MgOWNERCONDP(mg), MgMUTEXP(mg));
5177 DEBUG_S(PerlIO_printf(PerlIO_stderr(), "0x%lx: pp_lock lock 0x%lx\n",
5178 (unsigned long)thr, (unsigned long)sv);)
5179 MUTEX_UNLOCK(MgMUTEXP(mg));
5180 save_destructor(unlock_condpair, sv);
5182 #endif /* USE_THREADS */
5183 if (SvTYPE(retsv) == SVt_PVAV || SvTYPE(retsv) == SVt_PVHV
5184 || SvTYPE(retsv) == SVt_PVCV) {
5185 retsv = refto(retsv);
5196 if (PL_op->op_private & OPpLVAL_INTRO)
5197 PUSHs(*save_threadsv(PL_op->op_targ));
5199 PUSHs(THREADSV(PL_op->op_targ));
5202 DIE("tried to access per-thread data in non-threaded perl");
5203 #endif /* USE_THREADS */