3 * Copyright (c) 1991-2000, 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 * Offset for integer pack/unpack.
33 * On architectures where I16 and I32 aren't really 16 and 32 bits,
34 * which for now are all Crays, pack and unpack have to play games.
38 * These values are required for portability of pack() output.
39 * If they're not right on your machine, then pack() and unpack()
40 * wouldn't work right anyway; you'll need to apply the Cray hack.
41 * (I'd like to check them with #if, but you can't use sizeof() in
42 * the preprocessor.) --???
45 The appropriate SHORTSIZE, INTSIZE, LONGSIZE, and LONGLONGSIZE
46 defines are now in config.h. --Andy Dougherty April 1998
51 /* CROSSCOMPILE and MULTIARCH are going to affect pp_pack() and pp_unpack().
54 #if SHORTSIZE != SIZE16 || LONGSIZE != SIZE32
55 # define PERL_NATINT_PACK
58 #if LONGSIZE > 4 && defined(_CRAY)
59 # if BYTEORDER == 0x12345678
60 # define OFF16(p) (char*)(p)
61 # define OFF32(p) (char*)(p)
63 # if BYTEORDER == 0x87654321
64 # define OFF16(p) ((char*)(p) + (sizeof(U16) - SIZE16))
65 # define OFF32(p) ((char*)(p) + (sizeof(U32) - SIZE32))
67 }}}} bad cray byte order
70 # define COPY16(s,p) (*(p) = 0, Copy(s, OFF16(p), SIZE16, char))
71 # define COPY32(s,p) (*(p) = 0, Copy(s, OFF32(p), SIZE32, char))
72 # define COPYNN(s,p,n) (*(p) = 0, Copy(s, (char *)(p), n, char))
73 # define CAT16(sv,p) sv_catpvn(sv, OFF16(p), SIZE16)
74 # define CAT32(sv,p) sv_catpvn(sv, OFF32(p), SIZE32)
76 # define COPY16(s,p) Copy(s, p, SIZE16, char)
77 # define COPY32(s,p) Copy(s, p, SIZE32, char)
78 # define COPYNN(s,p,n) Copy(s, (char *)(p), n, char)
79 # define CAT16(sv,p) sv_catpvn(sv, (char*)(p), SIZE16)
80 # define CAT32(sv,p) sv_catpvn(sv, (char*)(p), SIZE32)
83 /* variations on pp_null */
89 /* XXX I can't imagine anyone who doesn't have this actually _needs_
90 it, since pid_t is an integral type.
93 #ifdef NEED_GETPID_PROTO
94 extern Pid_t getpid (void);
100 if (GIMME_V == G_SCALAR)
101 XPUSHs(&PL_sv_undef);
115 if (PL_op->op_private & OPpLVAL_INTRO)
116 SAVECLEARSV(PL_curpad[PL_op->op_targ]);
118 if (PL_op->op_flags & OPf_REF) {
122 if (GIMME == G_ARRAY) {
123 I32 maxarg = AvFILL((AV*)TARG) + 1;
125 if (SvMAGICAL(TARG)) {
127 for (i=0; i < maxarg; i++) {
128 SV **svp = av_fetch((AV*)TARG, i, FALSE);
129 SP[i+1] = (svp) ? *svp : &PL_sv_undef;
133 Copy(AvARRAY((AV*)TARG), SP+1, maxarg, SV*);
138 SV* sv = sv_newmortal();
139 I32 maxarg = AvFILL((AV*)TARG) + 1;
140 sv_setiv(sv, maxarg);
152 if (PL_op->op_private & OPpLVAL_INTRO)
153 SAVECLEARSV(PL_curpad[PL_op->op_targ]);
154 if (PL_op->op_flags & OPf_REF)
157 if (gimme == G_ARRAY) {
160 else if (gimme == G_SCALAR) {
161 SV* sv = sv_newmortal();
162 if (HvFILL((HV*)TARG))
163 Perl_sv_setpvf(aTHX_ sv, "%ld/%ld",
164 (long)HvFILL((HV*)TARG), (long)HvMAX((HV*)TARG) + 1);
174 DIE(aTHX_ "NOT IMPL LINE %d",__LINE__);
185 tryAMAGICunDEREF(to_gv);
188 if (SvTYPE(sv) == SVt_PVIO) {
189 GV *gv = (GV*) sv_newmortal();
190 gv_init(gv, 0, "", 0, 0);
191 GvIOp(gv) = (IO *)sv;
192 (void)SvREFCNT_inc(sv);
195 else if (SvTYPE(sv) != SVt_PVGV)
196 DIE(aTHX_ "Not a GLOB reference");
199 if (SvTYPE(sv) != SVt_PVGV) {
203 if (SvGMAGICAL(sv)) {
209 /* If this is a 'my' scalar and flag is set then vivify
212 if (PL_op->op_private & OPpDEREF) {
215 if (cUNOP->op_targ) {
217 SV *namesv = PL_curpad[cUNOP->op_targ];
218 name = SvPV(namesv, len);
219 gv = (GV*)NEWSV(0,0);
220 gv_init(gv, CopSTASH(PL_curcop), name, len, 0);
223 name = CopSTASHPV(PL_curcop);
226 sv_upgrade(sv, SVt_RV);
232 if (PL_op->op_flags & OPf_REF ||
233 PL_op->op_private & HINT_STRICT_REFS)
234 DIE(aTHX_ PL_no_usym, "a symbol");
235 if (ckWARN(WARN_UNINITIALIZED))
240 if ((PL_op->op_flags & OPf_SPECIAL) &&
241 !(PL_op->op_flags & OPf_MOD))
243 sv = (SV*)gv_fetchpv(sym, FALSE, SVt_PVGV);
248 if (PL_op->op_private & HINT_STRICT_REFS)
249 DIE(aTHX_ PL_no_symref, sym, "a symbol");
250 sv = (SV*)gv_fetchpv(sym, TRUE, SVt_PVGV);
254 if (PL_op->op_private & OPpLVAL_INTRO)
255 save_gp((GV*)sv, !(PL_op->op_flags & OPf_SPECIAL));
266 tryAMAGICunDEREF(to_sv);
269 switch (SvTYPE(sv)) {
273 DIE(aTHX_ "Not a SCALAR reference");
281 if (SvTYPE(gv) != SVt_PVGV) {
282 if (SvGMAGICAL(sv)) {
288 if (PL_op->op_flags & OPf_REF ||
289 PL_op->op_private & HINT_STRICT_REFS)
290 DIE(aTHX_ PL_no_usym, "a SCALAR");
291 if (ckWARN(WARN_UNINITIALIZED))
296 if ((PL_op->op_flags & OPf_SPECIAL) &&
297 !(PL_op->op_flags & OPf_MOD))
299 gv = (GV*)gv_fetchpv(sym, FALSE, SVt_PV);
304 if (PL_op->op_private & HINT_STRICT_REFS)
305 DIE(aTHX_ PL_no_symref, sym, "a SCALAR");
306 gv = (GV*)gv_fetchpv(sym, TRUE, SVt_PV);
311 if (PL_op->op_flags & OPf_MOD) {
312 if (PL_op->op_private & OPpLVAL_INTRO)
313 sv = save_scalar((GV*)TOPs);
314 else if (PL_op->op_private & OPpDEREF)
315 vivify_ref(sv, PL_op->op_private & OPpDEREF);
325 SV *sv = AvARYLEN(av);
327 AvARYLEN(av) = sv = NEWSV(0,0);
328 sv_upgrade(sv, SVt_IV);
329 sv_magic(sv, (SV*)av, '#', Nullch, 0);
337 djSP; dTARGET; dPOPss;
339 if (PL_op->op_flags & OPf_MOD) {
340 if (SvTYPE(TARG) < SVt_PVLV) {
341 sv_upgrade(TARG, SVt_PVLV);
342 sv_magic(TARG, Nullsv, '.', Nullch, 0);
346 if (LvTARG(TARG) != sv) {
348 SvREFCNT_dec(LvTARG(TARG));
349 LvTARG(TARG) = SvREFCNT_inc(sv);
351 PUSHs(TARG); /* no SvSETMAGIC */
357 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
358 mg = mg_find(sv, 'g');
359 if (mg && mg->mg_len >= 0) {
363 PUSHi(i + PL_curcop->cop_arybase);
377 /* We usually try to add a non-existent subroutine in case of AUTOLOAD. */
378 /* (But not in defined().) */
379 CV *cv = sv_2cv(TOPs, &stash, &gv, !(PL_op->op_flags & OPf_SPECIAL));
382 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
383 if ((PL_op->op_private & OPpLVAL_INTRO) && !CvLVALUE(cv))
384 DIE(aTHX_ "Can't modify non-lvalue subroutine call");
387 cv = (CV*)&PL_sv_undef;
401 if (SvPOK(TOPs) && SvCUR(TOPs) >= 7) {
402 char *s = SvPVX(TOPs);
403 if (strnEQ(s, "CORE::", 6)) {
406 code = keyword(s + 6, SvCUR(TOPs) - 6);
407 if (code < 0) { /* Overridable. */
408 #define MAX_ARGS_OP ((sizeof(I32) - 1) * 2)
409 int i = 0, n = 0, seen_question = 0;
411 char str[ MAX_ARGS_OP * 2 + 2 ]; /* One ';', one '\0' */
413 while (i < MAXO) { /* The slow way. */
414 if (strEQ(s + 6, PL_op_name[i])
415 || strEQ(s + 6, PL_op_desc[i]))
421 goto nonesuch; /* Should not happen... */
423 oa = PL_opargs[i] >> OASHIFT;
425 if (oa & OA_OPTIONAL) {
429 else if (seen_question)
430 goto set; /* XXXX system, exec */
431 if ((oa & (OA_OPTIONAL - 1)) >= OA_AVREF
432 && (oa & (OA_OPTIONAL - 1)) <= OA_HVREF) {
435 /* What to do with R ((un)tie, tied, (sys)read, recv)? */
436 str[n++] = ("?$@@%&*$")[oa & (OA_OPTIONAL - 1)];
440 ret = sv_2mortal(newSVpvn(str, n - 1));
442 else if (code) /* Non-Overridable */
444 else { /* None such */
446 DIE(aTHX_ "Can't find an opnumber for \"%s\"", s+6);
450 cv = sv_2cv(TOPs, &stash, &gv, FALSE);
452 ret = sv_2mortal(newSVpvn(SvPVX(cv), SvCUR(cv)));
461 CV* cv = (CV*)PL_curpad[PL_op->op_targ];
463 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
479 if (GIMME != G_ARRAY) {
483 *MARK = &PL_sv_undef;
484 *MARK = refto(*MARK);
488 EXTEND_MORTAL(SP - MARK);
490 *MARK = refto(*MARK);
495 S_refto(pTHX_ SV *sv)
499 if (SvTYPE(sv) == SVt_PVLV && LvTYPE(sv) == 'y') {
502 if (!(sv = LvTARG(sv)))
505 (void)SvREFCNT_inc(sv);
507 else if (SvTYPE(sv) == SVt_PVAV) {
508 if (!AvREAL((AV*)sv) && AvREIFY((AV*)sv))
511 (void)SvREFCNT_inc(sv);
513 else if (SvPADTMP(sv))
517 (void)SvREFCNT_inc(sv);
520 sv_upgrade(rv, SVt_RV);
534 if (sv && SvGMAGICAL(sv))
537 if (!sv || !SvROK(sv))
541 pv = sv_reftype(sv,TRUE);
542 PUSHp(pv, strlen(pv));
552 stash = CopSTASH(PL_curcop);
556 char *ptr = SvPV(ssv,len);
557 if (ckWARN(WARN_MISC) && len == 0)
558 Perl_warner(aTHX_ WARN_MISC,
559 "Explicit blessing to '' (assuming package main)");
560 stash = gv_stashpvn(ptr, len, TRUE);
563 (void)sv_bless(TOPs, stash);
577 elem = SvPV(sv, n_a);
581 switch (elem ? *elem : '\0')
584 if (strEQ(elem, "ARRAY"))
585 tmpRef = (SV*)GvAV(gv);
588 if (strEQ(elem, "CODE"))
589 tmpRef = (SV*)GvCVu(gv);
592 if (strEQ(elem, "FILEHANDLE")) /* XXX deprecate in 5.005 */
593 tmpRef = (SV*)GvIOp(gv);
596 if (strEQ(elem, "GLOB"))
600 if (strEQ(elem, "HASH"))
601 tmpRef = (SV*)GvHV(gv);
604 if (strEQ(elem, "IO"))
605 tmpRef = (SV*)GvIOp(gv);
608 if (strEQ(elem, "NAME"))
609 sv = newSVpvn(GvNAME(gv), GvNAMELEN(gv));
612 if (strEQ(elem, "PACKAGE"))
613 sv = newSVpv(HvNAME(GvSTASH(gv)), 0);
616 if (strEQ(elem, "SCALAR"))
630 /* Pattern matching */
635 register unsigned char *s;
638 register I32 *sfirst;
642 if (sv == PL_lastscream) {
648 SvSCREAM_off(PL_lastscream);
649 SvREFCNT_dec(PL_lastscream);
651 PL_lastscream = SvREFCNT_inc(sv);
654 s = (unsigned char*)(SvPV(sv, len));
658 if (pos > PL_maxscream) {
659 if (PL_maxscream < 0) {
660 PL_maxscream = pos + 80;
661 New(301, PL_screamfirst, 256, I32);
662 New(302, PL_screamnext, PL_maxscream, I32);
665 PL_maxscream = pos + pos / 4;
666 Renew(PL_screamnext, PL_maxscream, I32);
670 sfirst = PL_screamfirst;
671 snext = PL_screamnext;
673 if (!sfirst || !snext)
674 DIE(aTHX_ "do_study: out of memory");
676 for (ch = 256; ch; --ch)
683 snext[pos] = sfirst[ch] - pos;
690 sv_magic(sv, Nullsv, 'g', Nullch, 0); /* piggyback on m//g magic */
699 if (PL_op->op_flags & OPf_STACKED)
705 TARG = sv_newmortal();
710 /* Lvalue operators. */
722 djSP; dMARK; dTARGET;
732 SETi(do_chomp(TOPs));
738 djSP; dMARK; dTARGET;
739 register I32 count = 0;
742 count += do_chomp(POPs);
753 if (!sv || !SvANY(sv))
755 switch (SvTYPE(sv)) {
757 if (AvMAX(sv) >= 0 || SvGMAGICAL(sv) || (SvRMAGICAL(sv) && mg_find(sv,'P')))
761 if (HvARRAY(sv) || SvGMAGICAL(sv) || (SvRMAGICAL(sv) && mg_find(sv,'P')))
765 if (CvROOT(sv) || CvXSUB(sv))
782 if (!PL_op->op_private) {
791 if (SvTHINKFIRST(sv))
794 switch (SvTYPE(sv)) {
804 if (ckWARN(WARN_MISC) && cv_const_sv((CV*)sv))
805 Perl_warner(aTHX_ WARN_MISC, "Constant subroutine %s undefined",
806 CvANON((CV*)sv) ? "(anonymous)" : GvENAME(CvGV((CV*)sv)));
810 /* let user-undef'd sub keep its identity */
811 GV* gv = (GV*)SvREFCNT_inc(CvGV((CV*)sv));
818 SvSetMagicSV(sv, &PL_sv_undef);
822 Newz(602, gp, 1, GP);
823 GvGP(sv) = gp_ref(gp);
824 GvSV(sv) = NEWSV(72,0);
825 GvLINE(sv) = CopLINE(PL_curcop);
831 if (SvTYPE(sv) >= SVt_PV && SvPVX(sv) && SvLEN(sv)) {
834 SvPV_set(sv, Nullch);
847 if (SvREADONLY(TOPs) || SvTYPE(TOPs) > SVt_PVLV)
848 DIE(aTHX_ PL_no_modify);
849 if (SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) &&
850 SvIVX(TOPs) != IV_MIN)
853 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
864 if (SvREADONLY(TOPs) || SvTYPE(TOPs) > SVt_PVLV)
865 DIE(aTHX_ PL_no_modify);
866 sv_setsv(TARG, TOPs);
867 if (SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) &&
868 SvIVX(TOPs) != IV_MAX)
871 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
885 if (SvREADONLY(TOPs) || SvTYPE(TOPs) > SVt_PVLV)
886 DIE(aTHX_ PL_no_modify);
887 sv_setsv(TARG, TOPs);
888 if (SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) &&
889 SvIVX(TOPs) != IV_MIN)
892 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
901 /* Ordinary operators. */
905 djSP; dATARGET; tryAMAGICbin(pow,opASSIGN);
908 SETn( Perl_pow( left, right) );
915 djSP; dATARGET; tryAMAGICbin(mult,opASSIGN);
918 SETn( left * right );
925 djSP; dATARGET; tryAMAGICbin(div,opASSIGN);
930 DIE(aTHX_ "Illegal division by zero");
932 /* insure that 20./5. == 4. */
935 if ((NV)I_V(left) == left &&
936 (NV)I_V(right) == right &&
937 (k = I_V(left)/I_V(right))*I_V(right) == I_V(left)) {
941 value = left / right;
945 value = left / right;
954 djSP; dATARGET; tryAMAGICbin(modulo,opASSIGN);
964 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)) {
966 right = (right_neg = (i < 0)) ? -i : i;
971 right_neg = dright < 0;
976 if (!use_double && SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)) {
978 left = (left_neg = (i < 0)) ? -i : i;
986 left_neg = dleft < 0;
995 /* Somehow U_V is pessimized even if CASTFLAGS is 0 */
997 # define CAST_D2UV(d) U_V(d)
999 # define CAST_D2UV(d) ((UV)(d))
1001 /* Tried to do this only in the case DOUBLESIZE <= UV_SIZE,
1002 * or, in other words, precision of UV more than of NV.
1003 * But in fact the approach below turned out to be an
1004 * optimization - floor() may be slow */
1005 if (dright <= UV_MAX && dleft <= UV_MAX) {
1006 right = CAST_D2UV(dright);
1007 left = CAST_D2UV(dleft);
1012 /* Backward-compatibility clause: */
1013 dright = Perl_floor(dright + 0.5);
1014 dleft = Perl_floor(dleft + 0.5);
1017 DIE(aTHX_ "Illegal modulus zero");
1019 dans = Perl_fmod(dleft, dright);
1020 if ((left_neg != right_neg) && dans)
1021 dans = dright - dans;
1024 sv_setnv(TARG, dans);
1031 DIE(aTHX_ "Illegal modulus zero");
1034 if ((left_neg != right_neg) && ans)
1037 /* XXX may warn: unary minus operator applied to unsigned type */
1038 /* could change -foo to be (~foo)+1 instead */
1039 if (ans <= ~((UV)IV_MAX)+1)
1040 sv_setiv(TARG, ~ans+1);
1042 sv_setnv(TARG, -(NV)ans);
1045 sv_setuv(TARG, ans);
1054 djSP; dATARGET; tryAMAGICbin(repeat,opASSIGN);
1056 register I32 count = POPi;
1057 if (GIMME == G_ARRAY && PL_op->op_private & OPpREPEAT_DOLIST) {
1059 I32 items = SP - MARK;
1062 max = items * count;
1071 repeatcpy((char*)(MARK + items), (char*)MARK,
1072 items * sizeof(SV*), count - 1);
1075 else if (count <= 0)
1078 else { /* Note: mark already snarfed by pp_list */
1083 SvSetSV(TARG, tmpstr);
1084 SvPV_force(TARG, len);
1089 SvGROW(TARG, (count * len) + 1);
1090 repeatcpy(SvPVX(TARG) + len, SvPVX(TARG), len, count - 1);
1091 SvCUR(TARG) *= count;
1093 *SvEND(TARG) = '\0';
1095 (void)SvPOK_only(TARG);
1104 djSP; dATARGET; tryAMAGICbin(subtr,opASSIGN);
1107 SETn( left - right );
1114 djSP; dATARGET; tryAMAGICbin(lshift,opASSIGN);
1117 if (PL_op->op_private & HINT_INTEGER) {
1131 djSP; dATARGET; tryAMAGICbin(rshift,opASSIGN);
1134 if (PL_op->op_private & HINT_INTEGER) {
1148 djSP; tryAMAGICbinSET(lt,0);
1151 SETs(boolSV(TOPn < value));
1158 djSP; tryAMAGICbinSET(gt,0);
1161 SETs(boolSV(TOPn > value));
1168 djSP; tryAMAGICbinSET(le,0);
1171 SETs(boolSV(TOPn <= value));
1178 djSP; tryAMAGICbinSET(ge,0);
1181 SETs(boolSV(TOPn >= value));
1188 djSP; tryAMAGICbinSET(ne,0);
1191 SETs(boolSV(TOPn != value));
1198 djSP; dTARGET; tryAMAGICbin(ncmp,0);
1202 #ifdef __osf__ /* XXX fix in 5.6.1 --jhi */
1203 #if defined(USE_LONG_DOUBLE) && defined(HAS_LONG_DOUBLE)
1204 #define Perl_isnan isnanl
1206 #define Perl_isnan isnan
1210 #ifdef __osf__ /* XXX fix in 5.6.1 --jhi */
1211 if (Perl_isnan(left) || Perl_isnan(right)) {
1215 value = (left > right) - (left < right);
1219 else if (left < right)
1221 else if (left > right)
1235 djSP; tryAMAGICbinSET(slt,0);
1238 int cmp = ((PL_op->op_private & OPpLOCALE)
1239 ? sv_cmp_locale(left, right)
1240 : sv_cmp(left, right));
1241 SETs(boolSV(cmp < 0));
1248 djSP; tryAMAGICbinSET(sgt,0);
1251 int cmp = ((PL_op->op_private & OPpLOCALE)
1252 ? sv_cmp_locale(left, right)
1253 : sv_cmp(left, right));
1254 SETs(boolSV(cmp > 0));
1261 djSP; tryAMAGICbinSET(sle,0);
1264 int cmp = ((PL_op->op_private & OPpLOCALE)
1265 ? sv_cmp_locale(left, right)
1266 : sv_cmp(left, right));
1267 SETs(boolSV(cmp <= 0));
1274 djSP; tryAMAGICbinSET(sge,0);
1277 int cmp = ((PL_op->op_private & OPpLOCALE)
1278 ? sv_cmp_locale(left, right)
1279 : sv_cmp(left, right));
1280 SETs(boolSV(cmp >= 0));
1287 djSP; tryAMAGICbinSET(seq,0);
1290 SETs(boolSV(sv_eq(left, right)));
1297 djSP; tryAMAGICbinSET(sne,0);
1300 SETs(boolSV(!sv_eq(left, right)));
1307 djSP; dTARGET; tryAMAGICbin(scmp,0);
1310 int cmp = ((PL_op->op_private & OPpLOCALE)
1311 ? sv_cmp_locale(left, right)
1312 : sv_cmp(left, right));
1320 djSP; dATARGET; tryAMAGICbin(band,opASSIGN);
1323 if (SvNIOKp(left) || SvNIOKp(right)) {
1324 if (PL_op->op_private & HINT_INTEGER) {
1325 IV i = SvIV(left) & SvIV(right);
1329 UV u = SvUV(left) & SvUV(right);
1334 do_vop(PL_op->op_type, TARG, left, right);
1343 djSP; dATARGET; tryAMAGICbin(bxor,opASSIGN);
1346 if (SvNIOKp(left) || SvNIOKp(right)) {
1347 if (PL_op->op_private & HINT_INTEGER) {
1348 IV i = (USE_LEFT(left) ? SvIV(left) : 0) ^ SvIV(right);
1352 UV u = (USE_LEFT(left) ? SvUV(left) : 0) ^ SvUV(right);
1357 do_vop(PL_op->op_type, TARG, left, right);
1366 djSP; dATARGET; tryAMAGICbin(bor,opASSIGN);
1369 if (SvNIOKp(left) || SvNIOKp(right)) {
1370 if (PL_op->op_private & HINT_INTEGER) {
1371 IV i = (USE_LEFT(left) ? SvIV(left) : 0) | SvIV(right);
1375 UV u = (USE_LEFT(left) ? SvUV(left) : 0) | SvUV(right);
1380 do_vop(PL_op->op_type, TARG, left, right);
1389 djSP; dTARGET; tryAMAGICun(neg);
1394 if (SvIOKp(sv) && !SvNOKp(sv) && !SvPOKp(sv) && SvIVX(sv) != IV_MIN)
1396 else if (SvNIOKp(sv))
1398 else if (SvPOKp(sv)) {
1400 char *s = SvPV(sv, len);
1401 if (isIDFIRST(*s)) {
1402 sv_setpvn(TARG, "-", 1);
1405 else if (*s == '+' || *s == '-') {
1407 *SvPV_force(TARG, len) = *s == '-' ? '+' : '-';
1409 else if (DO_UTF8(sv) && *(U8*)s >= 0xc0 && isIDFIRST_utf8((U8*)s)) {
1410 sv_setpvn(TARG, "-", 1);
1414 sv_setnv(TARG, -SvNV(sv));
1425 djSP; tryAMAGICunSET(not);
1426 *PL_stack_sp = boolSV(!SvTRUE(*PL_stack_sp));
1432 djSP; dTARGET; tryAMAGICun(compl);
1436 if (PL_op->op_private & HINT_INTEGER) {
1446 register char *tmps;
1447 register long *tmpl;
1452 tmps = SvPV_force(TARG, len);
1455 for ( ; anum && (unsigned long)tmps % sizeof(long); anum--, tmps++)
1458 for ( ; anum >= sizeof(long); anum -= sizeof(long), tmpl++)
1462 for ( ; anum > 0; anum--, tmps++)
1471 /* integer versions of some of the above */
1475 djSP; dATARGET; tryAMAGICbin(mult,opASSIGN);
1478 SETi( left * right );
1485 djSP; dATARGET; tryAMAGICbin(div,opASSIGN);
1489 DIE(aTHX_ "Illegal division by zero");
1490 value = POPi / value;
1498 djSP; dATARGET; tryAMAGICbin(modulo,opASSIGN);
1502 DIE(aTHX_ "Illegal modulus zero");
1503 SETi( left % right );
1510 djSP; dATARGET; tryAMAGICbin(add,opASSIGN);
1513 SETi( left + right );
1520 djSP; dATARGET; tryAMAGICbin(subtr,opASSIGN);
1523 SETi( left - right );
1530 djSP; tryAMAGICbinSET(lt,0);
1533 SETs(boolSV(left < right));
1540 djSP; tryAMAGICbinSET(gt,0);
1543 SETs(boolSV(left > right));
1550 djSP; tryAMAGICbinSET(le,0);
1553 SETs(boolSV(left <= right));
1560 djSP; tryAMAGICbinSET(ge,0);
1563 SETs(boolSV(left >= right));
1570 djSP; tryAMAGICbinSET(eq,0);
1573 SETs(boolSV(left == right));
1580 djSP; tryAMAGICbinSET(ne,0);
1583 SETs(boolSV(left != right));
1590 djSP; dTARGET; tryAMAGICbin(ncmp,0);
1597 else if (left < right)
1608 djSP; dTARGET; tryAMAGICun(neg);
1613 /* High falutin' math. */
1617 djSP; dTARGET; tryAMAGICbin(atan2,0);
1620 SETn(Perl_atan2(left, right));
1627 djSP; dTARGET; tryAMAGICun(sin);
1631 value = Perl_sin(value);
1639 djSP; dTARGET; tryAMAGICun(cos);
1643 value = Perl_cos(value);
1649 /* Support Configure command-line overrides for rand() functions.
1650 After 5.005, perhaps we should replace this by Configure support
1651 for drand48(), random(), or rand(). For 5.005, though, maintain
1652 compatibility by calling rand() but allow the user to override it.
1653 See INSTALL for details. --Andy Dougherty 15 July 1998
1655 /* Now it's after 5.005, and Configure supports drand48() and random(),
1656 in addition to rand(). So the overrides should not be needed any more.
1657 --Jarkko Hietaniemi 27 September 1998
1660 #ifndef HAS_DRAND48_PROTO
1661 extern double drand48 (void);
1674 if (!PL_srand_called) {
1675 (void)seedDrand01((Rand_seed_t)seed());
1676 PL_srand_called = TRUE;
1691 (void)seedDrand01((Rand_seed_t)anum);
1692 PL_srand_called = TRUE;
1701 * This is really just a quick hack which grabs various garbage
1702 * values. It really should be a real hash algorithm which
1703 * spreads the effect of every input bit onto every output bit,
1704 * if someone who knows about such things would bother to write it.
1705 * Might be a good idea to add that function to CORE as well.
1706 * No numbers below come from careful analysis or anything here,
1707 * except they are primes and SEED_C1 > 1E6 to get a full-width
1708 * value from (tv_sec * SEED_C1 + tv_usec). The multipliers should
1709 * probably be bigger too.
1712 # define SEED_C1 1000003
1713 #define SEED_C4 73819
1715 # define SEED_C1 25747
1716 #define SEED_C4 20639
1720 #define SEED_C5 26107
1723 #ifndef PERL_NO_DEV_RANDOM
1728 # include <starlet.h>
1729 /* when[] = (low 32 bits, high 32 bits) of time since epoch
1730 * in 100-ns units, typically incremented ever 10 ms. */
1731 unsigned int when[2];
1733 # ifdef HAS_GETTIMEOFDAY
1734 struct timeval when;
1740 /* This test is an escape hatch, this symbol isn't set by Configure. */
1741 #ifndef PERL_NO_DEV_RANDOM
1742 #ifndef PERL_RANDOM_DEVICE
1743 /* /dev/random isn't used by default because reads from it will block
1744 * if there isn't enough entropy available. You can compile with
1745 * PERL_RANDOM_DEVICE to it if you'd prefer Perl to block until there
1746 * is enough real entropy to fill the seed. */
1747 # define PERL_RANDOM_DEVICE "/dev/urandom"
1749 fd = PerlLIO_open(PERL_RANDOM_DEVICE, 0);
1751 if (PerlLIO_read(fd, &u, sizeof u) != sizeof u)
1760 _ckvmssts(sys$gettim(when));
1761 u = (U32)SEED_C1 * when[0] + (U32)SEED_C2 * when[1];
1763 # ifdef HAS_GETTIMEOFDAY
1764 gettimeofday(&when,(struct timezone *) 0);
1765 u = (U32)SEED_C1 * when.tv_sec + (U32)SEED_C2 * when.tv_usec;
1768 u = (U32)SEED_C1 * when;
1771 u += SEED_C3 * (U32)PerlProc_getpid();
1772 u += SEED_C4 * (U32)PTR2UV(PL_stack_sp);
1773 #ifndef PLAN9 /* XXX Plan9 assembler chokes on this; fix needed */
1774 u += SEED_C5 * (U32)PTR2UV(&when);
1781 djSP; dTARGET; tryAMAGICun(exp);
1785 value = Perl_exp(value);
1793 djSP; dTARGET; tryAMAGICun(log);
1798 RESTORE_NUMERIC_STANDARD();
1799 DIE(aTHX_ "Can't take log of %g", value);
1801 value = Perl_log(value);
1809 djSP; dTARGET; tryAMAGICun(sqrt);
1814 RESTORE_NUMERIC_STANDARD();
1815 DIE(aTHX_ "Can't take sqrt of %g", value);
1817 value = Perl_sqrt(value);
1830 if (SvIOKp(TOPs) && !SvNOKp(TOPs) && !SvPOKp(TOPs)) {
1836 (void)Perl_modf(value, &value);
1838 (void)Perl_modf(-value, &value);
1853 djSP; dTARGET; tryAMAGICun(abs);
1858 if (SvIOKp(TOPs) && !SvNOKp(TOPs) && !SvPOKp(TOPs) &&
1859 (iv = SvIVX(TOPs)) != IV_MIN) {
1881 XPUSHn(scan_hex(tmps, 99, &argtype));
1894 while (*tmps && isSPACE(*tmps))
1899 value = scan_hex(++tmps, 99, &argtype);
1900 else if (*tmps == 'b')
1901 value = scan_bin(++tmps, 99, &argtype);
1903 value = scan_oct(tmps, 99, &argtype);
1916 SETi(sv_len_utf8(sv));
1932 I32 lvalue = PL_op->op_flags & OPf_MOD;
1934 I32 arybase = PL_curcop->cop_arybase;
1938 SvTAINTED_off(TARG); /* decontaminate */
1939 SvUTF8_off(TARG); /* decontaminate */
1943 repl = SvPV(sv, repl_len);
1950 tmps = SvPV(sv, curlen);
1952 utfcurlen = sv_len_utf8(sv);
1953 if (utfcurlen == curlen)
1961 if (pos >= arybase) {
1979 else if (len >= 0) {
1981 if (rem > (I32)curlen)
1996 Perl_croak(aTHX_ "substr outside of string");
1997 if (ckWARN(WARN_SUBSTR))
1998 Perl_warner(aTHX_ WARN_SUBSTR, "substr outside of string");
2003 sv_pos_u2b(sv, &pos, &rem);
2007 sv_setpvn(TARG, tmps, rem);
2009 sv_insert(sv, pos, rem, repl, repl_len);
2010 else if (lvalue) { /* it's an lvalue! */
2011 if (!SvGMAGICAL(sv)) {
2015 if (ckWARN(WARN_SUBSTR))
2016 Perl_warner(aTHX_ WARN_SUBSTR,
2017 "Attempt to use reference as lvalue in substr");
2019 if (SvOK(sv)) /* is it defined ? */
2020 (void)SvPOK_only(sv);
2022 sv_setpvn(sv,"",0); /* avoid lexical reincarnation */
2025 if (SvTYPE(TARG) < SVt_PVLV) {
2026 sv_upgrade(TARG, SVt_PVLV);
2027 sv_magic(TARG, Nullsv, 'x', Nullch, 0);
2031 if (LvTARG(TARG) != sv) {
2033 SvREFCNT_dec(LvTARG(TARG));
2034 LvTARG(TARG) = SvREFCNT_inc(sv);
2036 LvTARGOFF(TARG) = pos;
2037 LvTARGLEN(TARG) = rem;
2041 PUSHs(TARG); /* avoid SvSETMAGIC here */
2048 register I32 size = POPi;
2049 register I32 offset = POPi;
2050 register SV *src = POPs;
2051 I32 lvalue = PL_op->op_flags & OPf_MOD;
2053 SvTAINTED_off(TARG); /* decontaminate */
2054 if (lvalue) { /* it's an lvalue! */
2055 if (SvTYPE(TARG) < SVt_PVLV) {
2056 sv_upgrade(TARG, SVt_PVLV);
2057 sv_magic(TARG, Nullsv, 'v', Nullch, 0);
2060 if (LvTARG(TARG) != src) {
2062 SvREFCNT_dec(LvTARG(TARG));
2063 LvTARG(TARG) = SvREFCNT_inc(src);
2065 LvTARGOFF(TARG) = offset;
2066 LvTARGLEN(TARG) = size;
2069 sv_setuv(TARG, do_vecget(src, offset, size));
2084 I32 arybase = PL_curcop->cop_arybase;
2089 offset = POPi - arybase;
2092 tmps = SvPV(big, biglen);
2093 if (offset > 0 && DO_UTF8(big))
2094 sv_pos_u2b(big, &offset, 0);
2097 else if (offset > biglen)
2099 if (!(tmps2 = fbm_instr((unsigned char*)tmps + offset,
2100 (unsigned char*)tmps + biglen, little, 0)))
2103 retval = tmps2 - tmps;
2104 if (retval > 0 && DO_UTF8(big))
2105 sv_pos_b2u(big, &retval);
2106 PUSHi(retval + arybase);
2121 I32 arybase = PL_curcop->cop_arybase;
2127 tmps2 = SvPV(little, llen);
2128 tmps = SvPV(big, blen);
2132 if (offset > 0 && DO_UTF8(big))
2133 sv_pos_u2b(big, &offset, 0);
2134 offset = offset - arybase + llen;
2138 else if (offset > blen)
2140 if (!(tmps2 = rninstr(tmps, tmps + offset,
2141 tmps2, tmps2 + llen)))
2144 retval = tmps2 - tmps;
2145 if (retval > 0 && DO_UTF8(big))
2146 sv_pos_b2u(big, &retval);
2147 PUSHi(retval + arybase);
2153 djSP; dMARK; dORIGMARK; dTARGET;
2154 do_sprintf(TARG, SP-MARK, MARK+1);
2155 TAINT_IF(SvTAINTED(TARG));
2167 U8 *tmps = (U8*)SvPVx(tmpsv,n_a);
2170 if ((*tmps & 0x80) && DO_UTF8(tmpsv))
2171 value = utf8_to_uv(tmps, &retlen);
2173 value = (UV)(*tmps & 255);
2184 (void)SvUPGRADE(TARG,SVt_PV);
2186 if (value > 255 && !IN_BYTE) {
2187 SvGROW(TARG, UTF8_MAXLEN+1);
2189 tmps = (char*)uv_to_utf8((U8*)tmps, (UV)value);
2190 SvCUR_set(TARG, tmps - SvPVX(TARG));
2192 (void)SvPOK_only(TARG);
2203 SvUTF8_off(TARG); /* decontaminate */
2204 (void)SvPOK_only(TARG);
2211 djSP; dTARGET; dPOPTOPssrl;
2214 char *tmps = SvPV(left, n_a);
2216 sv_setpv(TARG, fcrypt(tmps, SvPV(right, n_a)));
2218 sv_setpv(TARG, PerlProc_crypt(tmps, SvPV(right, n_a)));
2222 "The crypt() function is unimplemented due to excessive paranoia.");
2235 if (DO_UTF8(sv) && (s = (U8*)SvPV(sv, slen)) && slen && (*s & 0xc0) == 0xc0) {
2237 U8 tmpbuf[UTF8_MAXLEN];
2239 UV uv = utf8_to_uv(s, &ulen);
2241 if (PL_op->op_private & OPpLOCALE) {
2244 uv = toTITLE_LC_uni(uv);
2247 uv = toTITLE_utf8(s);
2249 tend = uv_to_utf8(tmpbuf, uv);
2251 if (!SvPADTMP(sv) || tend - tmpbuf != ulen || SvREADONLY(sv)) {
2253 sv_setpvn(TARG, (char*)tmpbuf, tend - tmpbuf);
2254 sv_catpvn(TARG, (char*)(s + ulen), slen - ulen);
2259 s = (U8*)SvPV_force(sv, slen);
2260 Copy(tmpbuf, s, ulen, U8);
2264 if (!SvPADTMP(sv) || SvREADONLY(sv)) {
2266 SvUTF8_off(TARG); /* decontaminate */
2271 s = (U8*)SvPV_force(sv, slen);
2273 if (PL_op->op_private & OPpLOCALE) {
2276 *s = toUPPER_LC(*s);
2294 if (DO_UTF8(sv) && (s = (U8*)SvPV(sv, slen)) && slen && (*s & 0xc0) == 0xc0) {
2296 U8 tmpbuf[UTF8_MAXLEN];
2298 UV uv = utf8_to_uv(s, &ulen);
2300 if (PL_op->op_private & OPpLOCALE) {
2303 uv = toLOWER_LC_uni(uv);
2306 uv = toLOWER_utf8(s);
2308 tend = uv_to_utf8(tmpbuf, uv);
2310 if (!SvPADTMP(sv) || tend - tmpbuf != ulen || SvREADONLY(sv)) {
2312 sv_setpvn(TARG, (char*)tmpbuf, tend - tmpbuf);
2313 sv_catpvn(TARG, (char*)(s + ulen), slen - ulen);
2318 s = (U8*)SvPV_force(sv, slen);
2319 Copy(tmpbuf, s, ulen, U8);
2323 if (!SvPADTMP(sv) || SvREADONLY(sv)) {
2325 SvUTF8_off(TARG); /* decontaminate */
2330 s = (U8*)SvPV_force(sv, slen);
2332 if (PL_op->op_private & OPpLOCALE) {
2335 *s = toLOWER_LC(*s);
2359 s = (U8*)SvPV(sv,len);
2361 SvUTF8_off(TARG); /* decontaminate */
2362 sv_setpvn(TARG, "", 0);
2366 (void)SvUPGRADE(TARG, SVt_PV);
2367 SvGROW(TARG, (len * 2) + 1);
2368 (void)SvPOK_only(TARG);
2369 d = (U8*)SvPVX(TARG);
2371 if (PL_op->op_private & OPpLOCALE) {
2375 d = uv_to_utf8(d, toUPPER_LC_uni( utf8_to_uv(s, &ulen)));
2381 d = uv_to_utf8(d, toUPPER_utf8( s ));
2387 SvCUR_set(TARG, d - (U8*)SvPVX(TARG));
2392 if (!SvPADTMP(sv) || SvREADONLY(sv)) {
2394 SvUTF8_off(TARG); /* decontaminate */
2399 s = (U8*)SvPV_force(sv, len);
2401 register U8 *send = s + len;
2403 if (PL_op->op_private & OPpLOCALE) {
2406 for (; s < send; s++)
2407 *s = toUPPER_LC(*s);
2410 for (; s < send; s++)
2433 s = (U8*)SvPV(sv,len);
2435 SvUTF8_off(TARG); /* decontaminate */
2436 sv_setpvn(TARG, "", 0);
2440 (void)SvUPGRADE(TARG, SVt_PV);
2441 SvGROW(TARG, (len * 2) + 1);
2442 (void)SvPOK_only(TARG);
2443 d = (U8*)SvPVX(TARG);
2445 if (PL_op->op_private & OPpLOCALE) {
2449 d = uv_to_utf8(d, toLOWER_LC_uni( utf8_to_uv(s, &ulen)));
2455 d = uv_to_utf8(d, toLOWER_utf8(s));
2461 SvCUR_set(TARG, d - (U8*)SvPVX(TARG));
2466 if (!SvPADTMP(sv) || SvREADONLY(sv)) {
2468 SvUTF8_off(TARG); /* decontaminate */
2474 s = (U8*)SvPV_force(sv, len);
2476 register U8 *send = s + len;
2478 if (PL_op->op_private & OPpLOCALE) {
2481 for (; s < send; s++)
2482 *s = toLOWER_LC(*s);
2485 for (; s < send; s++)
2500 register char *s = SvPV(sv,len);
2503 SvUTF8_off(TARG); /* decontaminate */
2505 (void)SvUPGRADE(TARG, SVt_PV);
2506 SvGROW(TARG, (len * 2) + 1);
2511 STRLEN ulen = UTF8SKIP(s);
2535 SvCUR_set(TARG, d - SvPVX(TARG));
2536 (void)SvPOK_only(TARG);
2539 sv_setpvn(TARG, s, len);
2541 if (SvSMAGICAL(TARG))
2550 djSP; dMARK; dORIGMARK;
2552 register AV* av = (AV*)POPs;
2553 register I32 lval = PL_op->op_flags & OPf_MOD;
2554 I32 arybase = PL_curcop->cop_arybase;
2557 if (SvTYPE(av) == SVt_PVAV) {
2558 if (lval && PL_op->op_private & OPpLVAL_INTRO) {
2560 for (svp = MARK + 1; svp <= SP; svp++) {
2565 if (max > AvMAX(av))
2568 while (++MARK <= SP) {
2569 elem = SvIVx(*MARK);
2573 svp = av_fetch(av, elem, lval);
2575 if (!svp || *svp == &PL_sv_undef)
2576 DIE(aTHX_ PL_no_aelem, elem);
2577 if (PL_op->op_private & OPpLVAL_INTRO)
2578 save_aelem(av, elem, svp);
2580 *MARK = svp ? *svp : &PL_sv_undef;
2583 if (GIMME != G_ARRAY) {
2591 /* Associative arrays. */
2596 HV *hash = (HV*)POPs;
2598 I32 gimme = GIMME_V;
2599 I32 realhv = (SvTYPE(hash) == SVt_PVHV);
2602 /* might clobber stack_sp */
2603 entry = realhv ? hv_iternext(hash) : avhv_iternext((AV*)hash);
2608 PUSHs(hv_iterkeysv(entry)); /* won't clobber stack_sp */
2609 if (gimme == G_ARRAY) {
2612 /* might clobber stack_sp */
2614 hv_iterval(hash, entry) : avhv_iterval((AV*)hash, entry);
2619 else if (gimme == G_SCALAR)
2638 I32 gimme = GIMME_V;
2639 I32 discard = (gimme == G_VOID) ? G_DISCARD : 0;
2643 if (PL_op->op_private & OPpSLICE) {
2647 hvtype = SvTYPE(hv);
2648 if (hvtype == SVt_PVHV) { /* hash element */
2649 while (++MARK <= SP) {
2650 sv = hv_delete_ent(hv, *MARK, discard, 0);
2651 *MARK = sv ? sv : &PL_sv_undef;
2654 else if (hvtype == SVt_PVAV) {
2655 if (PL_op->op_flags & OPf_SPECIAL) { /* array element */
2656 while (++MARK <= SP) {
2657 sv = av_delete((AV*)hv, SvIV(*MARK), discard);
2658 *MARK = sv ? sv : &PL_sv_undef;
2661 else { /* pseudo-hash element */
2662 while (++MARK <= SP) {
2663 sv = avhv_delete_ent((AV*)hv, *MARK, discard, 0);
2664 *MARK = sv ? sv : &PL_sv_undef;
2669 DIE(aTHX_ "Not a HASH reference");
2672 else if (gimme == G_SCALAR) {
2681 if (SvTYPE(hv) == SVt_PVHV)
2682 sv = hv_delete_ent(hv, keysv, discard, 0);
2683 else if (SvTYPE(hv) == SVt_PVAV) {
2684 if (PL_op->op_flags & OPf_SPECIAL)
2685 sv = av_delete((AV*)hv, SvIV(keysv), discard);
2687 sv = avhv_delete_ent((AV*)hv, keysv, discard, 0);
2690 DIE(aTHX_ "Not a HASH reference");
2705 if (PL_op->op_private & OPpEXISTS_SUB) {
2709 cv = sv_2cv(sv, &hv, &gv, FALSE);
2712 if (gv && isGV(gv) && GvCV(gv) && !GvCVGEN(gv))
2718 if (SvTYPE(hv) == SVt_PVHV) {
2719 if (hv_exists_ent(hv, tmpsv, 0))
2722 else if (SvTYPE(hv) == SVt_PVAV) {
2723 if (PL_op->op_flags & OPf_SPECIAL) { /* array element */
2724 if (av_exists((AV*)hv, SvIV(tmpsv)))
2727 else if (avhv_exists_ent((AV*)hv, tmpsv, 0)) /* pseudo-hash element */
2731 DIE(aTHX_ "Not a HASH reference");
2738 djSP; dMARK; dORIGMARK;
2739 register HV *hv = (HV*)POPs;
2740 register I32 lval = PL_op->op_flags & OPf_MOD;
2741 I32 realhv = (SvTYPE(hv) == SVt_PVHV);
2743 if (!realhv && PL_op->op_private & OPpLVAL_INTRO)
2744 DIE(aTHX_ "Can't localize pseudo-hash element");
2746 if (realhv || SvTYPE(hv) == SVt_PVAV) {
2747 while (++MARK <= SP) {
2751 HE *he = hv_fetch_ent(hv, keysv, lval, 0);
2752 svp = he ? &HeVAL(he) : 0;
2755 svp = avhv_fetch_ent((AV*)hv, keysv, lval, 0);
2758 if (!svp || *svp == &PL_sv_undef) {
2760 DIE(aTHX_ PL_no_helem, SvPV(keysv, n_a));
2762 if (PL_op->op_private & OPpLVAL_INTRO)
2763 save_helem(hv, keysv, svp);
2765 *MARK = svp ? *svp : &PL_sv_undef;
2768 if (GIMME != G_ARRAY) {
2776 /* List operators. */
2781 if (GIMME != G_ARRAY) {
2783 *MARK = *SP; /* unwanted list, return last item */
2785 *MARK = &PL_sv_undef;
2794 SV **lastrelem = PL_stack_sp;
2795 SV **lastlelem = PL_stack_base + POPMARK;
2796 SV **firstlelem = PL_stack_base + POPMARK + 1;
2797 register SV **firstrelem = lastlelem + 1;
2798 I32 arybase = PL_curcop->cop_arybase;
2799 I32 lval = PL_op->op_flags & OPf_MOD;
2800 I32 is_something_there = lval;
2802 register I32 max = lastrelem - lastlelem;
2803 register SV **lelem;
2806 if (GIMME != G_ARRAY) {
2807 ix = SvIVx(*lastlelem);
2812 if (ix < 0 || ix >= max)
2813 *firstlelem = &PL_sv_undef;
2815 *firstlelem = firstrelem[ix];
2821 SP = firstlelem - 1;
2825 for (lelem = firstlelem; lelem <= lastlelem; lelem++) {
2831 if (ix < 0 || ix >= max)
2832 *lelem = &PL_sv_undef;
2834 is_something_there = TRUE;
2835 if (!(*lelem = firstrelem[ix]))
2836 *lelem = &PL_sv_undef;
2839 if (is_something_there)
2842 SP = firstlelem - 1;
2848 djSP; dMARK; dORIGMARK;
2849 I32 items = SP - MARK;
2850 SV *av = sv_2mortal((SV*)av_make(items, MARK+1));
2851 SP = ORIGMARK; /* av_make() might realloc stack_sp */
2858 djSP; dMARK; dORIGMARK;
2859 HV* hv = (HV*)sv_2mortal((SV*)newHV());
2863 SV *val = NEWSV(46, 0);
2865 sv_setsv(val, *++MARK);
2866 else if (ckWARN(WARN_MISC))
2867 Perl_warner(aTHX_ WARN_MISC, "Odd number of elements in hash assignment");
2868 (void)hv_store_ent(hv,key,val,0);
2877 djSP; dMARK; dORIGMARK;
2878 register AV *ary = (AV*)*++MARK;
2882 register I32 offset;
2883 register I32 length;
2890 if (mg = SvTIED_mg((SV*)ary, 'P')) {
2891 *MARK-- = SvTIED_obj((SV*)ary, mg);
2895 call_method("SPLICE",GIMME_V);
2904 offset = i = SvIVx(*MARK);
2906 offset += AvFILLp(ary) + 1;
2908 offset -= PL_curcop->cop_arybase;
2910 DIE(aTHX_ PL_no_aelem, i);
2912 length = SvIVx(*MARK++);
2914 length += AvFILLp(ary) - offset + 1;
2920 length = AvMAX(ary) + 1; /* close enough to infinity */
2924 length = AvMAX(ary) + 1;
2926 if (offset > AvFILLp(ary) + 1)
2927 offset = AvFILLp(ary) + 1;
2928 after = AvFILLp(ary) + 1 - (offset + length);
2929 if (after < 0) { /* not that much array */
2930 length += after; /* offset+length now in array */
2936 /* At this point, MARK .. SP-1 is our new LIST */
2939 diff = newlen - length;
2940 if (newlen && !AvREAL(ary) && AvREIFY(ary))
2943 if (diff < 0) { /* shrinking the area */
2945 New(451, tmparyval, newlen, SV*); /* so remember insertion */
2946 Copy(MARK, tmparyval, newlen, SV*);
2949 MARK = ORIGMARK + 1;
2950 if (GIMME == G_ARRAY) { /* copy return vals to stack */
2951 MEXTEND(MARK, length);
2952 Copy(AvARRAY(ary)+offset, MARK, length, SV*);
2954 EXTEND_MORTAL(length);
2955 for (i = length, dst = MARK; i; i--) {
2956 sv_2mortal(*dst); /* free them eventualy */
2963 *MARK = AvARRAY(ary)[offset+length-1];
2966 for (i = length - 1, dst = &AvARRAY(ary)[offset]; i > 0; i--)
2967 SvREFCNT_dec(*dst++); /* free them now */
2970 AvFILLp(ary) += diff;
2972 /* pull up or down? */
2974 if (offset < after) { /* easier to pull up */
2975 if (offset) { /* esp. if nothing to pull */
2976 src = &AvARRAY(ary)[offset-1];
2977 dst = src - diff; /* diff is negative */
2978 for (i = offset; i > 0; i--) /* can't trust Copy */
2982 SvPVX(ary) = (char*)(AvARRAY(ary) - diff); /* diff is negative */
2986 if (after) { /* anything to pull down? */
2987 src = AvARRAY(ary) + offset + length;
2988 dst = src + diff; /* diff is negative */
2989 Move(src, dst, after, SV*);
2991 dst = &AvARRAY(ary)[AvFILLp(ary)+1];
2992 /* avoid later double free */
2996 dst[--i] = &PL_sv_undef;
2999 for (src = tmparyval, dst = AvARRAY(ary) + offset;
3001 *dst = NEWSV(46, 0);
3002 sv_setsv(*dst++, *src++);
3004 Safefree(tmparyval);
3007 else { /* no, expanding (or same) */
3009 New(452, tmparyval, length, SV*); /* so remember deletion */
3010 Copy(AvARRAY(ary)+offset, tmparyval, length, SV*);
3013 if (diff > 0) { /* expanding */
3015 /* push up or down? */
3017 if (offset < after && diff <= AvARRAY(ary) - AvALLOC(ary)) {
3021 Move(src, dst, offset, SV*);
3023 SvPVX(ary) = (char*)(AvARRAY(ary) - diff);/* diff is positive */
3025 AvFILLp(ary) += diff;
3028 if (AvFILLp(ary) + diff >= AvMAX(ary)) /* oh, well */
3029 av_extend(ary, AvFILLp(ary) + diff);
3030 AvFILLp(ary) += diff;
3033 dst = AvARRAY(ary) + AvFILLp(ary);
3035 for (i = after; i; i--) {
3042 for (src = MARK, dst = AvARRAY(ary) + offset; newlen; newlen--) {
3043 *dst = NEWSV(46, 0);
3044 sv_setsv(*dst++, *src++);
3046 MARK = ORIGMARK + 1;
3047 if (GIMME == G_ARRAY) { /* copy return vals to stack */
3049 Copy(tmparyval, MARK, length, SV*);
3051 EXTEND_MORTAL(length);
3052 for (i = length, dst = MARK; i; i--) {
3053 sv_2mortal(*dst); /* free them eventualy */
3057 Safefree(tmparyval);
3061 else if (length--) {
3062 *MARK = tmparyval[length];
3065 while (length-- > 0)
3066 SvREFCNT_dec(tmparyval[length]);
3068 Safefree(tmparyval);
3071 *MARK = &PL_sv_undef;
3079 djSP; dMARK; dORIGMARK; dTARGET;
3080 register AV *ary = (AV*)*++MARK;
3081 register SV *sv = &PL_sv_undef;
3084 if (mg = SvTIED_mg((SV*)ary, 'P')) {
3085 *MARK-- = SvTIED_obj((SV*)ary, mg);
3089 call_method("PUSH",G_SCALAR|G_DISCARD);
3094 /* Why no pre-extend of ary here ? */
3095 for (++MARK; MARK <= SP; MARK++) {
3098 sv_setsv(sv, *MARK);
3103 PUSHi( AvFILL(ary) + 1 );
3111 SV *sv = av_pop(av);
3113 (void)sv_2mortal(sv);
3122 SV *sv = av_shift(av);
3127 (void)sv_2mortal(sv);
3134 djSP; dMARK; dORIGMARK; dTARGET;
3135 register AV *ary = (AV*)*++MARK;
3140 if (mg = SvTIED_mg((SV*)ary, 'P')) {
3141 *MARK-- = SvTIED_obj((SV*)ary, mg);
3145 call_method("UNSHIFT",G_SCALAR|G_DISCARD);
3150 av_unshift(ary, SP - MARK);
3153 sv_setsv(sv, *++MARK);
3154 (void)av_store(ary, i++, sv);
3158 PUSHi( AvFILL(ary) + 1 );
3168 if (GIMME == G_ARRAY) {
3175 /* safe as long as stack cannot get extended in the above */
3180 register char *down;
3185 SvUTF8_off(TARG); /* decontaminate */
3187 do_join(TARG, &PL_sv_no, MARK, SP);
3189 sv_setsv(TARG, (SP > MARK) ? *SP : DEFSV);
3190 up = SvPV_force(TARG, len);
3192 if (DO_UTF8(TARG)) { /* first reverse each character */
3193 U8* s = (U8*)SvPVX(TARG);
3194 U8* send = (U8*)(s + len);
3203 down = (char*)(s - 1);
3204 if (s > send || !((*down & 0xc0) == 0x80)) {
3205 if (ckWARN_d(WARN_UTF8))
3206 Perl_warner(aTHX_ WARN_UTF8,
3207 "Malformed UTF-8 character");
3219 down = SvPVX(TARG) + len - 1;
3225 (void)SvPOK_only(TARG);
3234 S_mul128(pTHX_ SV *sv, U8 m)
3237 char *s = SvPV(sv, len);
3241 if (!strnEQ(s, "0000", 4)) { /* need to grow sv */
3242 SV *tmpNew = newSVpvn("0000000000", 10);
3244 sv_catsv(tmpNew, sv);
3245 SvREFCNT_dec(sv); /* free old sv */
3250 while (!*t) /* trailing '\0'? */
3253 i = ((*t - '0') << 7) + m;
3254 *(t--) = '0' + (i % 10);
3260 /* Explosives and implosives. */
3262 #if 'I' == 73 && 'J' == 74
3263 /* On an ASCII/ISO kind of system */
3264 #define ISUUCHAR(ch) ((ch) >= ' ' && (ch) < 'a')
3267 Some other sort of character set - use memchr() so we don't match
3270 #define ISUUCHAR(ch) (memchr(PL_uuemap, (ch), sizeof(PL_uuemap)-1) || (ch) == ' ')
3277 I32 start_sp_offset = SP - PL_stack_base;
3278 I32 gimme = GIMME_V;
3282 register char *pat = SvPV(left, llen);
3283 register char *s = SvPV(right, rlen);
3284 char *strend = s + rlen;
3286 register char *patend = pat + llen;
3292 /* These must not be in registers: */
3309 register U32 culong;
3313 #ifdef PERL_NATINT_PACK
3314 int natint; /* native integer */
3315 int unatint; /* unsigned native integer */
3318 if (gimme != G_ARRAY) { /* arrange to do first one only */
3320 for (patend = pat; !isALPHA(*patend) || *patend == 'x'; patend++) ;
3321 if (strchr("aAZbBhHP", *patend) || *pat == '%') {
3323 while (isDIGIT(*patend) || *patend == '*')
3329 while (pat < patend) {
3331 datumtype = *pat++ & 0xFF;
3332 #ifdef PERL_NATINT_PACK
3335 if (isSPACE(datumtype))
3337 if (datumtype == '#') {
3338 while (pat < patend && *pat != '\n')
3343 char *natstr = "sSiIlL";
3345 if (strchr(natstr, datumtype)) {
3346 #ifdef PERL_NATINT_PACK
3352 DIE(aTHX_ "'!' allowed only after types %s", natstr);
3357 else if (*pat == '*') {
3358 len = strend - strbeg; /* long enough */
3362 else if (isDIGIT(*pat)) {
3364 while (isDIGIT(*pat)) {
3365 len = (len * 10) + (*pat++ - '0');
3367 DIE(aTHX_ "Repeat count in unpack overflows");
3371 len = (datumtype != '@');
3375 DIE(aTHX_ "Invalid type in unpack: '%c'", (int)datumtype);
3376 case ',': /* grandfather in commas but with a warning */
3377 if (commas++ == 0 && ckWARN(WARN_UNPACK))
3378 Perl_warner(aTHX_ WARN_UNPACK,
3379 "Invalid type in unpack: '%c'", (int)datumtype);
3382 if (len == 1 && pat[-1] != '1')
3391 if (len > strend - strbeg)
3392 DIE(aTHX_ "@ outside of string");
3396 if (len > s - strbeg)
3397 DIE(aTHX_ "X outside of string");
3401 if (len > strend - s)
3402 DIE(aTHX_ "x outside of string");
3406 if (start_sp_offset >= SP - PL_stack_base)
3407 DIE(aTHX_ "/ must follow a numeric type");
3410 pat++; /* ignore '*' for compatibility with pack */
3412 DIE(aTHX_ "/ cannot take a count" );
3419 if (len > strend - s)
3422 goto uchar_checksum;
3423 sv = NEWSV(35, len);
3424 sv_setpvn(sv, s, len);
3426 if (datumtype == 'A' || datumtype == 'Z') {
3427 aptr = s; /* borrow register */
3428 if (datumtype == 'Z') { /* 'Z' strips stuff after first null */
3433 else { /* 'A' strips both nulls and spaces */
3434 s = SvPVX(sv) + len - 1;
3435 while (s >= SvPVX(sv) && (!*s || isSPACE(*s)))
3439 SvCUR_set(sv, s - SvPVX(sv));
3440 s = aptr; /* unborrow register */
3442 XPUSHs(sv_2mortal(sv));
3446 if (star || len > (strend - s) * 8)
3447 len = (strend - s) * 8;
3450 Newz(601, PL_bitcount, 256, char);
3451 for (bits = 1; bits < 256; bits++) {
3452 if (bits & 1) PL_bitcount[bits]++;
3453 if (bits & 2) PL_bitcount[bits]++;
3454 if (bits & 4) PL_bitcount[bits]++;
3455 if (bits & 8) PL_bitcount[bits]++;
3456 if (bits & 16) PL_bitcount[bits]++;
3457 if (bits & 32) PL_bitcount[bits]++;
3458 if (bits & 64) PL_bitcount[bits]++;
3459 if (bits & 128) PL_bitcount[bits]++;
3463 culong += PL_bitcount[*(unsigned char*)s++];
3468 if (datumtype == 'b') {
3470 if (bits & 1) culong++;
3476 if (bits & 128) culong++;
3483 sv = NEWSV(35, len + 1);
3487 if (datumtype == 'b') {
3489 for (len = 0; len < aint; len++) {
3490 if (len & 7) /*SUPPRESS 595*/
3494 *str++ = '0' + (bits & 1);
3499 for (len = 0; len < aint; len++) {
3504 *str++ = '0' + ((bits & 128) != 0);
3508 XPUSHs(sv_2mortal(sv));
3512 if (star || len > (strend - s) * 2)
3513 len = (strend - s) * 2;
3514 sv = NEWSV(35, len + 1);
3518 if (datumtype == 'h') {
3520 for (len = 0; len < aint; len++) {
3525 *str++ = PL_hexdigit[bits & 15];
3530 for (len = 0; len < aint; len++) {
3535 *str++ = PL_hexdigit[(bits >> 4) & 15];
3539 XPUSHs(sv_2mortal(sv));
3542 if (len > strend - s)
3547 if (aint >= 128) /* fake up signed chars */
3557 if (aint >= 128) /* fake up signed chars */
3560 sv_setiv(sv, (IV)aint);
3561 PUSHs(sv_2mortal(sv));
3566 if (len > strend - s)
3581 sv_setiv(sv, (IV)auint);
3582 PUSHs(sv_2mortal(sv));
3587 if (len > strend - s)
3590 while (len-- > 0 && s < strend) {
3591 auint = utf8_to_uv((U8*)s, &along);
3594 cdouble += (NV)auint;
3602 while (len-- > 0 && s < strend) {
3603 auint = utf8_to_uv((U8*)s, &along);
3606 sv_setuv(sv, (UV)auint);
3607 PUSHs(sv_2mortal(sv));
3612 #if SHORTSIZE == SIZE16
3613 along = (strend - s) / SIZE16;
3615 along = (strend - s) / (natint ? sizeof(short) : SIZE16);
3620 #if SHORTSIZE != SIZE16
3624 COPYNN(s, &ashort, sizeof(short));
3635 #if SHORTSIZE > SIZE16
3647 #if SHORTSIZE != SIZE16
3651 COPYNN(s, &ashort, sizeof(short));
3654 sv_setiv(sv, (IV)ashort);
3655 PUSHs(sv_2mortal(sv));
3663 #if SHORTSIZE > SIZE16
3669 sv_setiv(sv, (IV)ashort);
3670 PUSHs(sv_2mortal(sv));
3678 #if SHORTSIZE == SIZE16
3679 along = (strend - s) / SIZE16;
3681 unatint = natint && datumtype == 'S';
3682 along = (strend - s) / (unatint ? sizeof(unsigned short) : SIZE16);
3687 #if SHORTSIZE != SIZE16
3689 unsigned short aushort;
3691 COPYNN(s, &aushort, sizeof(unsigned short));
3692 s += sizeof(unsigned short);
3700 COPY16(s, &aushort);
3703 if (datumtype == 'n')
3704 aushort = PerlSock_ntohs(aushort);
3707 if (datumtype == 'v')
3708 aushort = vtohs(aushort);
3717 #if SHORTSIZE != SIZE16
3719 unsigned short aushort;
3721 COPYNN(s, &aushort, sizeof(unsigned short));
3722 s += sizeof(unsigned short);
3724 sv_setiv(sv, (UV)aushort);
3725 PUSHs(sv_2mortal(sv));
3732 COPY16(s, &aushort);
3736 if (datumtype == 'n')
3737 aushort = PerlSock_ntohs(aushort);
3740 if (datumtype == 'v')
3741 aushort = vtohs(aushort);
3743 sv_setiv(sv, (UV)aushort);
3744 PUSHs(sv_2mortal(sv));
3750 along = (strend - s) / sizeof(int);
3755 Copy(s, &aint, 1, int);
3758 cdouble += (NV)aint;
3767 Copy(s, &aint, 1, int);
3771 /* Without the dummy below unpack("i", pack("i",-1))
3772 * return 0xFFffFFff instead of -1 for Digital Unix V4.0
3773 * cc with optimization turned on.
3775 * The bug was detected in
3776 * DEC C V5.8-009 on Digital UNIX V4.0 (Rev. 1091) (V4.0E)
3777 * with optimization (-O4) turned on.
3778 * DEC C V5.2-040 on Digital UNIX V4.0 (Rev. 564) (V4.0B)
3779 * does not have this problem even with -O4.
3781 * This bug was reported as DECC_BUGS 1431
3782 * and tracked internally as GEM_BUGS 7775.
3784 * The bug is fixed in
3785 * Tru64 UNIX V5.0: Compaq C V6.1-006 or later
3786 * UNIX V4.0F support: DEC C V5.9-006 or later
3787 * UNIX V4.0E support: DEC C V5.8-011 or later
3790 * See also few lines later for the same bug.
3793 sv_setiv(sv, (IV)aint) :
3795 sv_setiv(sv, (IV)aint);
3796 PUSHs(sv_2mortal(sv));
3801 along = (strend - s) / sizeof(unsigned int);
3806 Copy(s, &auint, 1, unsigned int);
3807 s += sizeof(unsigned int);
3809 cdouble += (NV)auint;
3818 Copy(s, &auint, 1, unsigned int);
3819 s += sizeof(unsigned int);
3822 /* Without the dummy below unpack("I", pack("I",0xFFFFFFFF))
3823 * returns 1.84467440737096e+19 instead of 0xFFFFFFFF.
3824 * See details few lines earlier. */
3826 sv_setuv(sv, (UV)auint) :
3828 sv_setuv(sv, (UV)auint);
3829 PUSHs(sv_2mortal(sv));
3834 #if LONGSIZE == SIZE32
3835 along = (strend - s) / SIZE32;
3837 along = (strend - s) / (natint ? sizeof(long) : SIZE32);
3842 #if LONGSIZE != SIZE32
3846 COPYNN(s, &along, sizeof(long));
3849 cdouble += (NV)along;
3859 #if LONGSIZE > SIZE32
3860 if (along > 2147483647)
3861 along -= 4294967296;
3865 cdouble += (NV)along;
3874 #if LONGSIZE != SIZE32
3878 COPYNN(s, &along, sizeof(long));
3881 sv_setiv(sv, (IV)along);
3882 PUSHs(sv_2mortal(sv));
3890 #if LONGSIZE > SIZE32
3891 if (along > 2147483647)
3892 along -= 4294967296;
3896 sv_setiv(sv, (IV)along);
3897 PUSHs(sv_2mortal(sv));
3905 #if LONGSIZE == SIZE32
3906 along = (strend - s) / SIZE32;
3908 unatint = natint && datumtype == 'L';
3909 along = (strend - s) / (unatint ? sizeof(unsigned long) : SIZE32);
3914 #if LONGSIZE != SIZE32
3916 unsigned long aulong;
3918 COPYNN(s, &aulong, sizeof(unsigned long));
3919 s += sizeof(unsigned long);
3921 cdouble += (NV)aulong;
3933 if (datumtype == 'N')
3934 aulong = PerlSock_ntohl(aulong);
3937 if (datumtype == 'V')
3938 aulong = vtohl(aulong);
3941 cdouble += (NV)aulong;
3950 #if LONGSIZE != SIZE32
3952 unsigned long aulong;
3954 COPYNN(s, &aulong, sizeof(unsigned long));
3955 s += sizeof(unsigned long);
3957 sv_setuv(sv, (UV)aulong);
3958 PUSHs(sv_2mortal(sv));
3968 if (datumtype == 'N')
3969 aulong = PerlSock_ntohl(aulong);
3972 if (datumtype == 'V')
3973 aulong = vtohl(aulong);
3976 sv_setuv(sv, (UV)aulong);
3977 PUSHs(sv_2mortal(sv));
3983 along = (strend - s) / sizeof(char*);
3989 if (sizeof(char*) > strend - s)
3992 Copy(s, &aptr, 1, char*);
3998 PUSHs(sv_2mortal(sv));
4008 while ((len > 0) && (s < strend)) {
4009 auv = (auv << 7) | (*s & 0x7f);
4010 if (!(*s++ & 0x80)) {
4014 PUSHs(sv_2mortal(sv));
4018 else if (++bytes >= sizeof(UV)) { /* promote to string */
4022 sv = Perl_newSVpvf(aTHX_ "%.*Vu", (int)TYPE_DIGITS(UV), auv);
4023 while (s < strend) {
4024 sv = mul128(sv, *s & 0x7f);
4025 if (!(*s++ & 0x80)) {
4034 PUSHs(sv_2mortal(sv));
4039 if ((s >= strend) && bytes)
4040 DIE(aTHX_ "Unterminated compressed integer");
4045 if (sizeof(char*) > strend - s)
4048 Copy(s, &aptr, 1, char*);
4053 sv_setpvn(sv, aptr, len);
4054 PUSHs(sv_2mortal(sv));
4058 along = (strend - s) / sizeof(Quad_t);
4064 if (s + sizeof(Quad_t) > strend)
4067 Copy(s, &aquad, 1, Quad_t);
4068 s += sizeof(Quad_t);
4071 if (aquad >= IV_MIN && aquad <= IV_MAX)
4072 sv_setiv(sv, (IV)aquad);
4074 sv_setnv(sv, (NV)aquad);
4075 PUSHs(sv_2mortal(sv));
4079 along = (strend - s) / sizeof(Quad_t);
4085 if (s + sizeof(Uquad_t) > strend)
4088 Copy(s, &auquad, 1, Uquad_t);
4089 s += sizeof(Uquad_t);
4092 if (auquad <= UV_MAX)
4093 sv_setuv(sv, (UV)auquad);
4095 sv_setnv(sv, (NV)auquad);
4096 PUSHs(sv_2mortal(sv));
4100 /* float and double added gnb@melba.bby.oz.au 22/11/89 */
4103 along = (strend - s) / sizeof(float);
4108 Copy(s, &afloat, 1, float);
4117 Copy(s, &afloat, 1, float);
4120 sv_setnv(sv, (NV)afloat);
4121 PUSHs(sv_2mortal(sv));
4127 along = (strend - s) / sizeof(double);
4132 Copy(s, &adouble, 1, double);
4133 s += sizeof(double);
4141 Copy(s, &adouble, 1, double);
4142 s += sizeof(double);
4144 sv_setnv(sv, (NV)adouble);
4145 PUSHs(sv_2mortal(sv));
4151 * Initialise the decode mapping. By using a table driven
4152 * algorithm, the code will be character-set independent
4153 * (and just as fast as doing character arithmetic)
4155 if (PL_uudmap['M'] == 0) {
4158 for (i = 0; i < sizeof(PL_uuemap); i += 1)
4159 PL_uudmap[PL_uuemap[i]] = i;
4161 * Because ' ' and '`' map to the same value,
4162 * we need to decode them both the same.
4167 along = (strend - s) * 3 / 4;
4168 sv = NEWSV(42, along);
4171 while (s < strend && *s > ' ' && ISUUCHAR(*s)) {
4176 len = PL_uudmap[*s++] & 077;
4178 if (s < strend && ISUUCHAR(*s))
4179 a = PL_uudmap[*s++] & 077;
4182 if (s < strend && ISUUCHAR(*s))
4183 b = PL_uudmap[*s++] & 077;
4186 if (s < strend && ISUUCHAR(*s))
4187 c = PL_uudmap[*s++] & 077;
4190 if (s < strend && ISUUCHAR(*s))
4191 d = PL_uudmap[*s++] & 077;
4194 hunk[0] = (a << 2) | (b >> 4);
4195 hunk[1] = (b << 4) | (c >> 2);
4196 hunk[2] = (c << 6) | d;
4197 sv_catpvn(sv, hunk, (len > 3) ? 3 : len);
4202 else if (s[1] == '\n') /* possible checksum byte */
4205 XPUSHs(sv_2mortal(sv));
4210 if (strchr("fFdD", datumtype) ||
4211 (checksum > 32 && strchr("iIlLNU", datumtype)) ) {
4215 while (checksum >= 16) {
4219 while (checksum >= 4) {
4225 along = (1 << checksum) - 1;
4226 while (cdouble < 0.0)
4228 cdouble = Perl_modf(cdouble / adouble, &trouble) * adouble;
4229 sv_setnv(sv, cdouble);
4232 if (checksum < 32) {
4233 aulong = (1 << checksum) - 1;
4236 sv_setuv(sv, (UV)culong);
4238 XPUSHs(sv_2mortal(sv));
4242 if (SP - PL_stack_base == start_sp_offset && gimme == G_SCALAR)
4243 PUSHs(&PL_sv_undef);
4248 S_doencodes(pTHX_ register SV *sv, register char *s, register I32 len)
4252 *hunk = PL_uuemap[len];
4253 sv_catpvn(sv, hunk, 1);
4256 hunk[0] = PL_uuemap[(077 & (*s >> 2))];
4257 hunk[1] = PL_uuemap[(077 & (((*s << 4) & 060) | ((s[1] >> 4) & 017)))];
4258 hunk[2] = PL_uuemap[(077 & (((s[1] << 2) & 074) | ((s[2] >> 6) & 03)))];
4259 hunk[3] = PL_uuemap[(077 & (s[2] & 077))];
4260 sv_catpvn(sv, hunk, 4);
4265 char r = (len > 1 ? s[1] : '\0');
4266 hunk[0] = PL_uuemap[(077 & (*s >> 2))];
4267 hunk[1] = PL_uuemap[(077 & (((*s << 4) & 060) | ((r >> 4) & 017)))];
4268 hunk[2] = PL_uuemap[(077 & ((r << 2) & 074))];
4269 hunk[3] = PL_uuemap[0];
4270 sv_catpvn(sv, hunk, 4);
4272 sv_catpvn(sv, "\n", 1);
4276 S_is_an_int(pTHX_ char *s, STRLEN l)
4279 SV *result = newSVpvn(s, l);
4280 char *result_c = SvPV(result, n_a); /* convenience */
4281 char *out = result_c;
4291 SvREFCNT_dec(result);
4314 SvREFCNT_dec(result);
4320 SvCUR_set(result, out - result_c);
4324 /* pnum must be '\0' terminated */
4326 S_div128(pTHX_ SV *pnum, bool *done)
4329 char *s = SvPV(pnum, len);
4338 i = m * 10 + (*t - '0');
4340 r = (i >> 7); /* r < 10 */
4347 SvCUR_set(pnum, (STRLEN) (t - s));
4354 djSP; dMARK; dORIGMARK; dTARGET;
4355 register SV *cat = TARG;
4358 register char *pat = SvPVx(*++MARK, fromlen);
4359 register char *patend = pat + fromlen;
4364 static char null10[] = {0,0,0,0,0,0,0,0,0,0};
4365 static char *space10 = " ";
4367 /* These must not be in registers: */
4382 #ifdef PERL_NATINT_PACK
4383 int natint; /* native integer */
4388 sv_setpvn(cat, "", 0);
4389 while (pat < patend) {
4390 SV *lengthcode = Nullsv;
4391 #define NEXTFROM ( lengthcode ? lengthcode : items-- > 0 ? *MARK++ : &PL_sv_no)
4392 datumtype = *pat++ & 0xFF;
4393 #ifdef PERL_NATINT_PACK
4396 if (isSPACE(datumtype))
4398 if (datumtype == '#') {
4399 while (pat < patend && *pat != '\n')
4404 char *natstr = "sSiIlL";
4406 if (strchr(natstr, datumtype)) {
4407 #ifdef PERL_NATINT_PACK
4413 DIE(aTHX_ "'!' allowed only after types %s", natstr);
4416 len = strchr("@Xxu", datumtype) ? 0 : items;
4419 else if (isDIGIT(*pat)) {
4421 while (isDIGIT(*pat)) {
4422 len = (len * 10) + (*pat++ - '0');
4424 DIE(aTHX_ "Repeat count in pack overflows");
4431 if (*pat != 'a' && *pat != 'A' && *pat != 'Z' || pat[1] != '*')
4432 DIE(aTHX_ "/ must be followed by a*, A* or Z*");
4433 lengthcode = sv_2mortal(newSViv(sv_len(items > 0
4434 ? *MARK : &PL_sv_no)));
4438 DIE(aTHX_ "Invalid type in pack: '%c'", (int)datumtype);
4439 case ',': /* grandfather in commas but with a warning */
4440 if (commas++ == 0 && ckWARN(WARN_PACK))
4441 Perl_warner(aTHX_ WARN_PACK,
4442 "Invalid type in pack: '%c'", (int)datumtype);
4445 DIE(aTHX_ "%% may only be used in unpack");
4456 if (SvCUR(cat) < len)
4457 DIE(aTHX_ "X outside of string");
4464 sv_catpvn(cat, null10, 10);
4467 sv_catpvn(cat, null10, len);
4473 aptr = SvPV(fromstr, fromlen);
4474 if (pat[-1] == '*') {
4476 if (datumtype == 'Z')
4479 if (fromlen >= len) {
4480 sv_catpvn(cat, aptr, len);
4481 if (datumtype == 'Z')
4482 *(SvEND(cat)-1) = '\0';
4485 sv_catpvn(cat, aptr, fromlen);
4487 if (datumtype == 'A') {
4489 sv_catpvn(cat, space10, 10);
4492 sv_catpvn(cat, space10, len);
4496 sv_catpvn(cat, null10, 10);
4499 sv_catpvn(cat, null10, len);
4511 str = SvPV(fromstr, fromlen);
4515 SvCUR(cat) += (len+7)/8;
4516 SvGROW(cat, SvCUR(cat) + 1);
4517 aptr = SvPVX(cat) + aint;
4522 if (datumtype == 'B') {
4523 for (len = 0; len++ < aint;) {
4524 items |= *str++ & 1;
4528 *aptr++ = items & 0xff;
4534 for (len = 0; len++ < aint;) {
4540 *aptr++ = items & 0xff;
4546 if (datumtype == 'B')
4547 items <<= 7 - (aint & 7);
4549 items >>= 7 - (aint & 7);
4550 *aptr++ = items & 0xff;
4552 str = SvPVX(cat) + SvCUR(cat);
4567 str = SvPV(fromstr, fromlen);
4571 SvCUR(cat) += (len+1)/2;
4572 SvGROW(cat, SvCUR(cat) + 1);
4573 aptr = SvPVX(cat) + aint;
4578 if (datumtype == 'H') {
4579 for (len = 0; len++ < aint;) {
4581 items |= ((*str++ & 15) + 9) & 15;
4583 items |= *str++ & 15;
4587 *aptr++ = items & 0xff;
4593 for (len = 0; len++ < aint;) {
4595 items |= (((*str++ & 15) + 9) & 15) << 4;
4597 items |= (*str++ & 15) << 4;
4601 *aptr++ = items & 0xff;
4607 *aptr++ = items & 0xff;
4608 str = SvPVX(cat) + SvCUR(cat);
4619 aint = SvIV(fromstr);
4621 sv_catpvn(cat, &achar, sizeof(char));
4627 auint = SvUV(fromstr);
4628 SvGROW(cat, SvCUR(cat) + UTF8_MAXLEN);
4629 SvCUR_set(cat, (char*)uv_to_utf8((U8*)SvEND(cat),auint)
4634 /* Float and double added by gnb@melba.bby.oz.au 22/11/89 */
4639 afloat = (float)SvNV(fromstr);
4640 sv_catpvn(cat, (char *)&afloat, sizeof (float));
4647 adouble = (double)SvNV(fromstr);
4648 sv_catpvn(cat, (char *)&adouble, sizeof (double));
4654 ashort = (I16)SvIV(fromstr);
4656 ashort = PerlSock_htons(ashort);
4658 CAT16(cat, &ashort);
4664 ashort = (I16)SvIV(fromstr);
4666 ashort = htovs(ashort);
4668 CAT16(cat, &ashort);
4672 #if SHORTSIZE != SIZE16
4674 unsigned short aushort;
4678 aushort = SvUV(fromstr);
4679 sv_catpvn(cat, (char *)&aushort, sizeof(unsigned short));
4689 aushort = (U16)SvUV(fromstr);
4690 CAT16(cat, &aushort);
4696 #if SHORTSIZE != SIZE16
4702 ashort = SvIV(fromstr);
4703 sv_catpvn(cat, (char *)&ashort, sizeof(short));
4711 ashort = (I16)SvIV(fromstr);
4712 CAT16(cat, &ashort);
4719 auint = SvUV(fromstr);
4720 sv_catpvn(cat, (char*)&auint, sizeof(unsigned int));
4726 adouble = Perl_floor(SvNV(fromstr));
4729 DIE(aTHX_ "Cannot compress negative numbers");
4732 #ifdef CXUX_BROKEN_CONSTANT_CONVERT
4733 adouble <= UV_MAX_cxux
4739 char buf[1 + sizeof(UV)];
4740 char *in = buf + sizeof(buf);
4741 UV auv = U_V(adouble);
4744 *--in = (auv & 0x7f) | 0x80;
4747 buf[sizeof(buf) - 1] &= 0x7f; /* clear continue bit */
4748 sv_catpvn(cat, in, (buf + sizeof(buf)) - in);
4750 else if (SvPOKp(fromstr)) { /* decimal string arithmetics */
4751 char *from, *result, *in;
4756 /* Copy string and check for compliance */
4757 from = SvPV(fromstr, len);
4758 if ((norm = is_an_int(from, len)) == NULL)
4759 DIE(aTHX_ "can compress only unsigned integer");
4761 New('w', result, len, char);
4765 *--in = div128(norm, &done) | 0x80;
4766 result[len - 1] &= 0x7F; /* clear continue bit */
4767 sv_catpvn(cat, in, (result + len) - in);
4769 SvREFCNT_dec(norm); /* free norm */
4771 else if (SvNOKp(fromstr)) {
4772 char buf[sizeof(double) * 2]; /* 8/7 <= 2 */
4773 char *in = buf + sizeof(buf);
4776 double next = floor(adouble / 128);
4777 *--in = (unsigned char)(adouble - (next * 128)) | 0x80;
4778 if (--in < buf) /* this cannot happen ;-) */
4779 DIE(aTHX_ "Cannot compress integer");
4781 } while (adouble > 0);
4782 buf[sizeof(buf) - 1] &= 0x7f; /* clear continue bit */
4783 sv_catpvn(cat, in, (buf + sizeof(buf)) - in);
4786 DIE(aTHX_ "Cannot compress non integer");
4792 aint = SvIV(fromstr);
4793 sv_catpvn(cat, (char*)&aint, sizeof(int));
4799 aulong = SvUV(fromstr);
4801 aulong = PerlSock_htonl(aulong);
4803 CAT32(cat, &aulong);
4809 aulong = SvUV(fromstr);
4811 aulong = htovl(aulong);
4813 CAT32(cat, &aulong);
4817 #if LONGSIZE != SIZE32
4819 unsigned long aulong;
4823 aulong = SvUV(fromstr);
4824 sv_catpvn(cat, (char *)&aulong, sizeof(unsigned long));
4832 aulong = SvUV(fromstr);
4833 CAT32(cat, &aulong);
4838 #if LONGSIZE != SIZE32
4844 along = SvIV(fromstr);
4845 sv_catpvn(cat, (char *)&along, sizeof(long));
4853 along = SvIV(fromstr);
4862 auquad = (Uquad_t)SvUV(fromstr);
4863 sv_catpvn(cat, (char*)&auquad, sizeof(Uquad_t));
4869 aquad = (Quad_t)SvIV(fromstr);
4870 sv_catpvn(cat, (char*)&aquad, sizeof(Quad_t));
4875 len = 1; /* assume SV is correct length */
4880 if (fromstr == &PL_sv_undef)
4884 /* XXX better yet, could spirit away the string to
4885 * a safe spot and hang on to it until the result
4886 * of pack() (and all copies of the result) are
4889 if (ckWARN(WARN_PACK) && (SvTEMP(fromstr)
4890 || (SvPADTMP(fromstr)
4891 && !SvREADONLY(fromstr))))
4893 Perl_warner(aTHX_ WARN_PACK,
4894 "Attempt to pack pointer to temporary value");
4896 if (SvPOK(fromstr) || SvNIOK(fromstr))
4897 aptr = SvPV(fromstr,n_a);
4899 aptr = SvPV_force(fromstr,n_a);
4901 sv_catpvn(cat, (char*)&aptr, sizeof(char*));
4906 aptr = SvPV(fromstr, fromlen);
4907 SvGROW(cat, fromlen * 4 / 3);
4912 while (fromlen > 0) {
4919 doencodes(cat, aptr, todo);
4938 register I32 limit = POPi; /* note, negative is forever */
4941 register char *s = SvPV(sv, len);
4942 char *strend = s + len;
4944 register REGEXP *rx;
4948 I32 maxiters = (strend - s) + 10;
4951 I32 origlimit = limit;
4954 AV *oldstack = PL_curstack;
4955 I32 gimme = GIMME_V;
4956 I32 oldsave = PL_savestack_ix;
4957 I32 make_mortal = 1;
4958 MAGIC *mg = (MAGIC *) NULL;
4961 Copy(&LvTARGOFF(POPs), &pm, 1, PMOP*);
4966 DIE(aTHX_ "panic: do_split");
4967 rx = pm->op_pmregexp;
4969 TAINT_IF((pm->op_pmflags & PMf_LOCALE) &&
4970 (pm->op_pmflags & (PMf_WHITE | PMf_SKIPWHITE)));
4972 if (pm->op_pmreplroot) {
4974 ary = GvAVn((GV*)PL_curpad[(PADOFFSET)pm->op_pmreplroot]);
4976 ary = GvAVn((GV*)pm->op_pmreplroot);
4979 else if (gimme != G_ARRAY)
4981 ary = (AV*)PL_curpad[0];
4983 ary = GvAVn(PL_defgv);
4984 #endif /* USE_THREADS */
4987 if (ary && (gimme != G_ARRAY || (pm->op_pmflags & PMf_ONCE))) {
4993 if (mg = SvTIED_mg((SV*)ary, 'P')) {
4995 XPUSHs(SvTIED_obj((SV*)ary, mg));
5001 for (i = AvFILLp(ary); i >= 0; i--)
5002 AvARRAY(ary)[i] = &PL_sv_undef; /* don't free mere refs */
5004 /* temporarily switch stacks */
5005 SWITCHSTACK(PL_curstack, ary);
5009 base = SP - PL_stack_base;
5011 if (pm->op_pmflags & PMf_SKIPWHITE) {
5012 if (pm->op_pmflags & PMf_LOCALE) {
5013 while (isSPACE_LC(*s))
5021 if (pm->op_pmflags & (PMf_MULTILINE|PMf_SINGLELINE)) {
5022 SAVEINT(PL_multiline);
5023 PL_multiline = pm->op_pmflags & PMf_MULTILINE;
5027 limit = maxiters + 2;
5028 if (pm->op_pmflags & PMf_WHITE) {
5031 while (m < strend &&
5032 !((pm->op_pmflags & PMf_LOCALE)
5033 ? isSPACE_LC(*m) : isSPACE(*m)))
5038 dstr = NEWSV(30, m-s);
5039 sv_setpvn(dstr, s, m-s);
5045 while (s < strend &&
5046 ((pm->op_pmflags & PMf_LOCALE)
5047 ? isSPACE_LC(*s) : isSPACE(*s)))
5051 else if (strEQ("^", rx->precomp)) {
5054 for (m = s; m < strend && *m != '\n'; m++) ;
5058 dstr = NEWSV(30, m-s);
5059 sv_setpvn(dstr, s, m-s);
5066 else if ((rx->reganch & RE_USE_INTUIT) && !rx->nparens
5067 && (rx->reganch & ROPT_CHECK_ALL)
5068 && !(rx->reganch & ROPT_ANCH)) {
5069 int tail = (rx->reganch & RE_INTUIT_TAIL);
5070 SV *csv = CALLREG_INTUIT_STRING(aTHX_ rx);
5074 if (len == 1 && !tail) {
5078 for (m = s; m < strend && *m != c; m++) ;
5081 dstr = NEWSV(30, m-s);
5082 sv_setpvn(dstr, s, m-s);
5091 while (s < strend && --limit &&
5092 (m = fbm_instr((unsigned char*)s, (unsigned char*)strend,
5093 csv, PL_multiline ? FBMrf_MULTILINE : 0)) )
5096 dstr = NEWSV(31, m-s);
5097 sv_setpvn(dstr, s, m-s);
5101 s = m + len; /* Fake \n at the end */
5106 maxiters += (strend - s) * rx->nparens;
5107 while (s < strend && --limit
5108 /* && (!rx->check_substr
5109 || ((s = CALLREG_INTUIT_START(aTHX_ rx, sv, s, strend,
5111 */ && CALLREGEXEC(aTHX_ rx, s, strend, orig,
5112 1 /* minend */, sv, NULL, 0))
5114 TAINT_IF(RX_MATCH_TAINTED(rx));
5115 if (RX_MATCH_COPIED(rx) && rx->subbeg != orig) {
5120 strend = s + (strend - m);
5122 m = rx->startp[0] + orig;
5123 dstr = NEWSV(32, m-s);
5124 sv_setpvn(dstr, s, m-s);
5129 for (i = 1; i <= rx->nparens; i++) {
5130 s = rx->startp[i] + orig;
5131 m = rx->endp[i] + orig;
5133 dstr = NEWSV(33, m-s);
5134 sv_setpvn(dstr, s, m-s);
5137 dstr = NEWSV(33, 0);
5143 s = rx->endp[0] + orig;
5147 LEAVE_SCOPE(oldsave);
5148 iters = (SP - PL_stack_base) - base;
5149 if (iters > maxiters)
5150 DIE(aTHX_ "Split loop");
5152 /* keep field after final delim? */
5153 if (s < strend || (iters && origlimit)) {
5154 dstr = NEWSV(34, strend-s);
5155 sv_setpvn(dstr, s, strend-s);
5161 else if (!origlimit) {
5162 while (iters > 0 && (!TOPs || !SvANY(TOPs) || SvCUR(TOPs) == 0))
5168 SWITCHSTACK(ary, oldstack);
5169 if (SvSMAGICAL(ary)) {
5174 if (gimme == G_ARRAY) {
5176 Copy(AvARRAY(ary), SP + 1, iters, SV*);
5184 call_method("PUSH",G_SCALAR|G_DISCARD);
5187 if (gimme == G_ARRAY) {
5188 /* EXTEND should not be needed - we just popped them */
5190 for (i=0; i < iters; i++) {
5191 SV **svp = av_fetch(ary, i, FALSE);
5192 PUSHs((svp) ? *svp : &PL_sv_undef);
5199 if (gimme == G_ARRAY)
5202 if (iters || !pm->op_pmreplroot) {
5212 Perl_unlock_condpair(pTHX_ void *svv)
5215 MAGIC *mg = mg_find((SV*)svv, 'm');
5218 Perl_croak(aTHX_ "panic: unlock_condpair unlocking non-mutex");
5219 MUTEX_LOCK(MgMUTEXP(mg));
5220 if (MgOWNER(mg) != thr)
5221 Perl_croak(aTHX_ "panic: unlock_condpair unlocking mutex that we don't own");
5223 COND_SIGNAL(MgOWNERCONDP(mg));
5224 DEBUG_S(PerlIO_printf(Perl_debug_log, "0x%"UVxf": unlock 0x%"UVxf"\n",
5225 PTR2UV(thr), PTR2UV(svv));)
5226 MUTEX_UNLOCK(MgMUTEXP(mg));
5228 #endif /* USE_THREADS */
5241 mg = condpair_magic(sv);
5242 MUTEX_LOCK(MgMUTEXP(mg));
5243 if (MgOWNER(mg) == thr)
5244 MUTEX_UNLOCK(MgMUTEXP(mg));
5247 COND_WAIT(MgOWNERCONDP(mg), MgMUTEXP(mg));
5249 DEBUG_S(PerlIO_printf(Perl_debug_log, "0x%"UVxf": pp_lock lock 0x%"UVxf"\n",
5250 PTR2UV(thr), PTR2UV(sv));)
5251 MUTEX_UNLOCK(MgMUTEXP(mg));
5252 SAVEDESTRUCTOR_X(Perl_unlock_condpair, sv);
5254 #endif /* USE_THREADS */
5255 if (SvTYPE(retsv) == SVt_PVAV || SvTYPE(retsv) == SVt_PVHV
5256 || SvTYPE(retsv) == SVt_PVCV) {
5257 retsv = refto(retsv);
5268 if (PL_op->op_private & OPpLVAL_INTRO)
5269 PUSHs(*save_threadsv(PL_op->op_targ));
5271 PUSHs(THREADSV(PL_op->op_targ));
5274 DIE(aTHX_ "tried to access per-thread data in non-threaded perl");
5275 #endif /* USE_THREADS */