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
19 * The compiler on Concurrent CX/UX systems has a subtle bug which only
20 * seems to show up when compiling pp.c - it generates the wrong double
21 * precision constant value for (double)UV_MAX when used inline in the body
22 * of the code below, so this makes a static variable up front (which the
23 * compiler seems to get correct) and uses it in place of UV_MAX below.
25 #ifdef CXUX_BROKEN_CONSTANT_CONVERT
26 static double UV_MAX_cxux = ((double)UV_MAX);
30 * Types used in bitwise operations.
32 * Normally we'd just use IV and UV. However, some hardware and
33 * software combinations (e.g. Alpha and current OSF/1) don't have a
34 * floating-point type to use for NV that has adequate bits to fully
35 * hold an IV/UV. (In other words, sizeof(long) == sizeof(double).)
37 * It just so happens that "int" is the right size almost everywhere.
43 * Mask used after bitwise operations.
45 * There is at least one realm (Cray word machines) that doesn't
46 * have an integral type (except char) small enough to be represented
47 * in a double without loss; that is, it has no 32-bit type.
49 #if LONGSIZE > 4 && defined(_CRAY) && !defined(_CRAYMPP)
51 # define BW_MASK ((1 << BW_BITS) - 1)
52 # define BW_SIGN (1 << (BW_BITS - 1))
53 # define BWi(i) (((i) & BW_SIGN) ? ((i) | ~BW_MASK) : ((i) & BW_MASK))
54 # define BWu(u) ((u) & BW_MASK)
61 * Offset for integer pack/unpack.
63 * On architectures where I16 and I32 aren't really 16 and 32 bits,
64 * which for now are all Crays, pack and unpack have to play games.
68 * These values are required for portability of pack() output.
69 * If they're not right on your machine, then pack() and unpack()
70 * wouldn't work right anyway; you'll need to apply the Cray hack.
71 * (I'd like to check them with #if, but you can't use sizeof() in
72 * the preprocessor.) --???
75 The appropriate SHORTSIZE, INTSIZE, LONGSIZE, and LONGLONGSIZE
76 defines are now in config.h. --Andy Dougherty April 1998
81 /* CROSSCOMPILE and MULTIARCH are going to affect pp_pack() and pp_unpack().
84 #if SHORTSIZE != SIZE16 || LONGSIZE != SIZE32
85 # define PERL_NATINT_PACK
88 #if BYTEORDER > 0xFFFF && defined(_CRAY) && !defined(_CRAYMPP)
89 # if BYTEORDER == 0x12345678
90 # define OFF16(p) (char*)(p)
91 # define OFF32(p) (char*)(p)
93 # if BYTEORDER == 0x87654321
94 # define OFF16(p) ((char*)(p) + (sizeof(U16) - SIZE16))
95 # define OFF32(p) ((char*)(p) + (sizeof(U32) - SIZE32))
97 }}}} bad cray byte order
100 # define COPY16(s,p) (*(p) = 0, Copy(s, OFF16(p), SIZE16, char))
101 # define COPY32(s,p) (*(p) = 0, Copy(s, OFF32(p), SIZE32, char))
102 # define COPYNN(s,p,n) (*(p) = 0, Copy(s, (char *)(p), n, char))
103 # define CAT16(sv,p) sv_catpvn(sv, OFF16(p), SIZE16)
104 # define CAT32(sv,p) sv_catpvn(sv, OFF32(p), SIZE32)
106 # define COPY16(s,p) Copy(s, p, SIZE16, char)
107 # define COPY32(s,p) Copy(s, p, SIZE32, char)
108 # define COPYNN(s,p,n) Copy(s, (char *)(p), n, char)
109 # define CAT16(sv,p) sv_catpvn(sv, (char*)(p), SIZE16)
110 # define CAT32(sv,p) sv_catpvn(sv, (char*)(p), SIZE32)
114 static void doencodes (SV* sv, char* s, I32 len);
115 static SV* refto (SV* sv);
116 static U32 seed (void);
119 /* variations on pp_null */
125 /* XXX I can't imagine anyone who doesn't have this actually _needs_
126 it, since pid_t is an integral type.
129 #ifdef NEED_GETPID_PROTO
130 extern Pid_t getpid (void);
136 if (GIMME_V == G_SCALAR)
137 XPUSHs(&PL_sv_undef);
151 if (PL_op->op_private & OPpLVAL_INTRO)
152 SAVECLEARSV(PL_curpad[PL_op->op_targ]);
154 if (PL_op->op_flags & OPf_REF) {
158 if (GIMME == G_ARRAY) {
159 I32 maxarg = AvFILL((AV*)TARG) + 1;
161 if (SvMAGICAL(TARG)) {
163 for (i=0; i < maxarg; i++) {
164 SV **svp = av_fetch((AV*)TARG, i, FALSE);
165 SP[i+1] = (svp) ? *svp : &PL_sv_undef;
169 Copy(AvARRAY((AV*)TARG), SP+1, maxarg, SV*);
174 SV* sv = sv_newmortal();
175 I32 maxarg = AvFILL((AV*)TARG) + 1;
176 sv_setiv(sv, maxarg);
188 if (PL_op->op_private & OPpLVAL_INTRO)
189 SAVECLEARSV(PL_curpad[PL_op->op_targ]);
190 if (PL_op->op_flags & OPf_REF)
193 if (gimme == G_ARRAY) {
194 RETURNOP(do_kv(ARGS));
196 else if (gimme == G_SCALAR) {
197 SV* sv = sv_newmortal();
198 if (HvFILL((HV*)TARG))
199 sv_setpvf(sv, "%ld/%ld",
200 (long)HvFILL((HV*)TARG), (long)HvMAX((HV*)TARG) + 1);
210 DIE("NOT IMPL LINE %d",__LINE__);
221 tryAMAGICunDEREF(to_gv);
224 if (SvTYPE(sv) == SVt_PVIO) {
225 GV *gv = (GV*) sv_newmortal();
226 gv_init(gv, 0, "", 0, 0);
227 GvIOp(gv) = (IO *)sv;
228 (void)SvREFCNT_inc(sv);
231 else if (SvTYPE(sv) != SVt_PVGV)
232 DIE("Not a GLOB reference");
235 if (SvTYPE(sv) != SVt_PVGV) {
239 if (SvGMAGICAL(sv)) {
245 /* If this is a 'my' scalar and flag is set then vivify
248 if (PL_op->op_private & OPpDEREF) {
249 GV *gv = (GV *) newSV(0);
252 if (cUNOP->op_first->op_type == OP_PADSV) {
253 SV *padname = *av_fetch(PL_comppad_name, cUNOP->op_first->op_targ, 4);
254 name = SvPV(padname,len);
256 gv_init(gv, PL_curcop->cop_stash, name, len, 0);
257 sv_upgrade(sv, SVt_RV);
258 SvRV(sv) = (SV *) gv;
263 if (PL_op->op_flags & OPf_REF ||
264 PL_op->op_private & HINT_STRICT_REFS)
265 DIE(PL_no_usym, "a symbol");
266 if (ckWARN(WARN_UNINITIALIZED))
267 warner(WARN_UNINITIALIZED, PL_warn_uninit);
271 if ((PL_op->op_flags & OPf_SPECIAL) &&
272 !(PL_op->op_flags & OPf_MOD))
274 sv = (SV*)gv_fetchpv(sym, FALSE, SVt_PVGV);
279 if (PL_op->op_private & HINT_STRICT_REFS)
280 DIE(PL_no_symref, sym, "a symbol");
281 sv = (SV*)gv_fetchpv(sym, TRUE, SVt_PVGV);
285 if (PL_op->op_private & OPpLVAL_INTRO)
286 save_gp((GV*)sv, !(PL_op->op_flags & OPf_SPECIAL));
297 tryAMAGICunDEREF(to_sv);
300 switch (SvTYPE(sv)) {
304 DIE("Not a SCALAR reference");
312 if (SvTYPE(gv) != SVt_PVGV) {
313 if (SvGMAGICAL(sv)) {
319 if (PL_op->op_flags & OPf_REF ||
320 PL_op->op_private & HINT_STRICT_REFS)
321 DIE(PL_no_usym, "a SCALAR");
322 if (ckWARN(WARN_UNINITIALIZED))
323 warner(WARN_UNINITIALIZED, PL_warn_uninit);
327 if ((PL_op->op_flags & OPf_SPECIAL) &&
328 !(PL_op->op_flags & OPf_MOD))
330 gv = (GV*)gv_fetchpv(sym, FALSE, SVt_PV);
335 if (PL_op->op_private & HINT_STRICT_REFS)
336 DIE(PL_no_symref, sym, "a SCALAR");
337 gv = (GV*)gv_fetchpv(sym, TRUE, SVt_PV);
342 if (PL_op->op_flags & OPf_MOD) {
343 if (PL_op->op_private & OPpLVAL_INTRO)
344 sv = save_scalar((GV*)TOPs);
345 else if (PL_op->op_private & OPpDEREF)
346 vivify_ref(sv, PL_op->op_private & OPpDEREF);
356 SV *sv = AvARYLEN(av);
358 AvARYLEN(av) = sv = NEWSV(0,0);
359 sv_upgrade(sv, SVt_IV);
360 sv_magic(sv, (SV*)av, '#', Nullch, 0);
368 djSP; dTARGET; dPOPss;
370 if (PL_op->op_flags & OPf_MOD) {
371 if (SvTYPE(TARG) < SVt_PVLV) {
372 sv_upgrade(TARG, SVt_PVLV);
373 sv_magic(TARG, Nullsv, '.', Nullch, 0);
377 if (LvTARG(TARG) != sv) {
379 SvREFCNT_dec(LvTARG(TARG));
380 LvTARG(TARG) = SvREFCNT_inc(sv);
382 PUSHs(TARG); /* no SvSETMAGIC */
388 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
389 mg = mg_find(sv, 'g');
390 if (mg && mg->mg_len >= 0) {
394 PUSHi(i + PL_curcop->cop_arybase);
408 /* We usually try to add a non-existent subroutine in case of AUTOLOAD. */
409 /* (But not in defined().) */
410 CV *cv = sv_2cv(TOPs, &stash, &gv, !(PL_op->op_flags & OPf_SPECIAL));
413 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
416 cv = (CV*)&PL_sv_undef;
430 if (SvPOK(TOPs) && SvCUR(TOPs) >= 7) {
431 char *s = SvPVX(TOPs);
432 if (strnEQ(s, "CORE::", 6)) {
435 code = keyword(s + 6, SvCUR(TOPs) - 6);
436 if (code < 0) { /* Overridable. */
437 #define MAX_ARGS_OP ((sizeof(I32) - 1) * 2)
438 int i = 0, n = 0, seen_question = 0;
440 char str[ MAX_ARGS_OP * 2 + 2 ]; /* One ';', one '\0' */
442 while (i < MAXO) { /* The slow way. */
443 if (strEQ(s + 6, PL_op_name[i])
444 || strEQ(s + 6, PL_op_desc[i]))
450 goto nonesuch; /* Should not happen... */
452 oa = PL_opargs[i] >> OASHIFT;
454 if (oa & OA_OPTIONAL) {
458 else if (seen_question)
459 goto set; /* XXXX system, exec */
460 if ((oa & (OA_OPTIONAL - 1)) >= OA_AVREF
461 && (oa & (OA_OPTIONAL - 1)) <= OA_HVREF) {
464 /* What to do with R ((un)tie, tied, (sys)read, recv)? */
465 str[n++] = ("?$@@%&*$")[oa & (OA_OPTIONAL - 1)];
469 ret = sv_2mortal(newSVpvn(str, n - 1));
471 else if (code) /* Non-Overridable */
473 else { /* None such */
475 croak("Can't find an opnumber for \"%s\"", s+6);
479 cv = sv_2cv(TOPs, &stash, &gv, FALSE);
481 ret = sv_2mortal(newSVpvn(SvPVX(cv), SvCUR(cv)));
490 CV* cv = (CV*)PL_curpad[PL_op->op_targ];
492 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
508 if (GIMME != G_ARRAY) {
512 *MARK = &PL_sv_undef;
513 *MARK = refto(*MARK);
517 EXTEND_MORTAL(SP - MARK);
519 *MARK = refto(*MARK);
528 if (SvTYPE(sv) == SVt_PVLV && LvTYPE(sv) == 'y') {
531 if (!(sv = LvTARG(sv)))
534 (void)SvREFCNT_inc(sv);
536 else if (SvPADTMP(sv))
540 (void)SvREFCNT_inc(sv);
543 sv_upgrade(rv, SVt_RV);
557 if (sv && SvGMAGICAL(sv))
560 if (!sv || !SvROK(sv))
564 pv = sv_reftype(sv,TRUE);
565 PUSHp(pv, strlen(pv));
575 stash = PL_curcop->cop_stash;
579 char *ptr = SvPV(ssv,len);
580 if (ckWARN(WARN_UNSAFE) && len == 0)
582 "Explicit blessing to '' (assuming package main)");
583 stash = gv_stashpvn(ptr, len, TRUE);
586 (void)sv_bless(TOPs, stash);
600 elem = SvPV(sv, n_a);
604 switch (elem ? *elem : '\0')
607 if (strEQ(elem, "ARRAY"))
608 tmpRef = (SV*)GvAV(gv);
611 if (strEQ(elem, "CODE"))
612 tmpRef = (SV*)GvCVu(gv);
615 if (strEQ(elem, "FILEHANDLE")) /* XXX deprecate in 5.005 */
616 tmpRef = (SV*)GvIOp(gv);
619 if (strEQ(elem, "GLOB"))
623 if (strEQ(elem, "HASH"))
624 tmpRef = (SV*)GvHV(gv);
627 if (strEQ(elem, "IO"))
628 tmpRef = (SV*)GvIOp(gv);
631 if (strEQ(elem, "NAME"))
632 sv = newSVpvn(GvNAME(gv), GvNAMELEN(gv));
635 if (strEQ(elem, "PACKAGE"))
636 sv = newSVpv(HvNAME(GvSTASH(gv)), 0);
639 if (strEQ(elem, "SCALAR"))
653 /* Pattern matching */
658 register unsigned char *s;
661 register I32 *sfirst;
665 if (sv == PL_lastscream) {
671 SvSCREAM_off(PL_lastscream);
672 SvREFCNT_dec(PL_lastscream);
674 PL_lastscream = SvREFCNT_inc(sv);
677 s = (unsigned char*)(SvPV(sv, len));
681 if (pos > PL_maxscream) {
682 if (PL_maxscream < 0) {
683 PL_maxscream = pos + 80;
684 New(301, PL_screamfirst, 256, I32);
685 New(302, PL_screamnext, PL_maxscream, I32);
688 PL_maxscream = pos + pos / 4;
689 Renew(PL_screamnext, PL_maxscream, I32);
693 sfirst = PL_screamfirst;
694 snext = PL_screamnext;
696 if (!sfirst || !snext)
697 DIE("do_study: out of memory");
699 for (ch = 256; ch; --ch)
706 snext[pos] = sfirst[ch] - pos;
713 sv_magic(sv, Nullsv, 'g', Nullch, 0); /* piggyback on m//g magic */
722 if (PL_op->op_flags & OPf_STACKED)
728 TARG = sv_newmortal();
733 /* Lvalue operators. */
745 djSP; dMARK; dTARGET;
755 SETi(do_chomp(TOPs));
761 djSP; dMARK; dTARGET;
762 register I32 count = 0;
765 count += do_chomp(POPs);
776 if (!sv || !SvANY(sv))
778 switch (SvTYPE(sv)) {
780 if (AvMAX(sv) >= 0 || SvGMAGICAL(sv) || (SvRMAGICAL(sv) && mg_find(sv,'P')))
784 if (HvARRAY(sv) || SvGMAGICAL(sv) || (SvRMAGICAL(sv) && mg_find(sv,'P')))
788 if (CvROOT(sv) || CvXSUB(sv))
805 if (!PL_op->op_private) {
814 if (SvTHINKFIRST(sv))
817 switch (SvTYPE(sv)) {
827 if (ckWARN(WARN_UNSAFE) && cv_const_sv((CV*)sv))
828 warner(WARN_UNSAFE, "Constant subroutine %s undefined",
829 CvANON((CV*)sv) ? "(anonymous)" : GvENAME(CvGV((CV*)sv)));
833 /* let user-undef'd sub keep its identity */
834 GV* gv = (GV*)SvREFCNT_inc(CvGV((CV*)sv));
841 SvSetMagicSV(sv, &PL_sv_undef);
845 Newz(602, gp, 1, GP);
846 GvGP(sv) = gp_ref(gp);
847 GvSV(sv) = NEWSV(72,0);
848 GvLINE(sv) = PL_curcop->cop_line;
854 if (SvTYPE(sv) >= SVt_PV && SvPVX(sv) && SvLEN(sv)) {
857 SvPV_set(sv, Nullch);
870 if (SvREADONLY(TOPs) || SvTYPE(TOPs) > SVt_PVLV)
872 if (SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) &&
873 SvIVX(TOPs) != IV_MIN)
876 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
887 if (SvREADONLY(TOPs) || SvTYPE(TOPs) > SVt_PVLV)
889 sv_setsv(TARG, TOPs);
890 if (SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) &&
891 SvIVX(TOPs) != IV_MAX)
894 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
908 if(SvREADONLY(TOPs) || SvTYPE(TOPs) > SVt_PVLV)
910 sv_setsv(TARG, TOPs);
911 if (SvIOK_notUV(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) &&
912 SvIVX(TOPs) != IV_MIN)
915 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
924 /* Ordinary operators. */
928 djSP; dATARGET; tryAMAGICbin(pow,opASSIGN);
931 SETn( pow( left, right) );
938 djSP; dATARGET; tryAMAGICbin(mult,opASSIGN);
941 SETn( left * right );
948 djSP; dATARGET; tryAMAGICbin(div,opASSIGN);
953 DIE("Illegal division by zero");
955 /* insure that 20./5. == 4. */
958 if ((double)I_V(left) == left &&
959 (double)I_V(right) == right &&
960 (k = I_V(left)/I_V(right))*I_V(right) == I_V(left)) {
964 value = left / right;
968 value = left / right;
977 djSP; dATARGET; tryAMAGICbin(modulo,opASSIGN);
987 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)) {
989 right = (right_neg = (i < 0)) ? -i : i;
994 right_neg = dright < 0;
999 if (!use_double && SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)) {
1001 left = (left_neg = (i < 0)) ? -i : i;
1009 left_neg = dleft < 0;
1018 /* Somehow U_V is pessimized even if CASTFLAGS is 0 */
1020 # define CAST_D2UV(d) U_V(d)
1022 # define CAST_D2UV(d) ((UV)(d))
1024 /* Tried to do this only in the case DOUBLESIZE <= UV_SIZE,
1025 * or, in other words, precision of UV more than of NV.
1026 * But in fact the approach below turned out to be an
1027 * optimization - floor() may be slow */
1028 if (dright <= UV_MAX && dleft <= UV_MAX) {
1029 right = CAST_D2UV(dright);
1030 left = CAST_D2UV(dleft);
1035 /* Backward-compatibility clause: */
1036 dright = floor(dright + 0.5);
1037 dleft = floor(dleft + 0.5);
1040 DIE("Illegal modulus zero");
1042 dans = fmod(dleft, dright);
1043 if ((left_neg != right_neg) && dans)
1044 dans = dright - dans;
1047 sv_setnv(TARG, dans);
1054 DIE("Illegal modulus zero");
1057 if ((left_neg != right_neg) && ans)
1060 /* XXX may warn: unary minus operator applied to unsigned type */
1061 /* could change -foo to be (~foo)+1 instead */
1062 if (ans <= ~((UV)IV_MAX)+1)
1063 sv_setiv(TARG, ~ans+1);
1065 sv_setnv(TARG, -(double)ans);
1068 sv_setuv(TARG, ans);
1077 djSP; dATARGET; tryAMAGICbin(repeat,opASSIGN);
1079 register I32 count = POPi;
1080 if (GIMME == G_ARRAY && PL_op->op_private & OPpREPEAT_DOLIST) {
1082 I32 items = SP - MARK;
1085 max = items * count;
1094 repeatcpy((char*)(MARK + items), (char*)MARK,
1095 items * sizeof(SV*), count - 1);
1098 else if (count <= 0)
1101 else { /* Note: mark already snarfed by pp_list */
1106 SvSetSV(TARG, tmpstr);
1107 SvPV_force(TARG, len);
1112 SvGROW(TARG, (count * len) + 1);
1113 repeatcpy(SvPVX(TARG) + len, SvPVX(TARG), len, count - 1);
1114 SvCUR(TARG) *= count;
1116 *SvEND(TARG) = '\0';
1118 (void)SvPOK_only(TARG);
1127 djSP; dATARGET; tryAMAGICbin(subtr,opASSIGN);
1130 SETn( left - right );
1137 djSP; dATARGET; tryAMAGICbin(lshift,opASSIGN);
1140 if (PL_op->op_private & HINT_INTEGER) {
1142 i = BWi(i) << shift;
1156 djSP; dATARGET; tryAMAGICbin(rshift,opASSIGN);
1159 if (PL_op->op_private & HINT_INTEGER) {
1161 i = BWi(i) >> shift;
1175 djSP; tryAMAGICbinSET(lt,0);
1178 SETs(boolSV(TOPn < value));
1185 djSP; tryAMAGICbinSET(gt,0);
1188 SETs(boolSV(TOPn > value));
1195 djSP; tryAMAGICbinSET(le,0);
1198 SETs(boolSV(TOPn <= value));
1205 djSP; tryAMAGICbinSET(ge,0);
1208 SETs(boolSV(TOPn >= value));
1215 djSP; tryAMAGICbinSET(ne,0);
1218 SETs(boolSV(TOPn != value));
1225 djSP; dTARGET; tryAMAGICbin(ncmp,0);
1232 else if (left < right)
1234 else if (left > right)
1247 djSP; tryAMAGICbinSET(slt,0);
1250 int cmp = ((PL_op->op_private & OPpLOCALE)
1251 ? sv_cmp_locale(left, right)
1252 : sv_cmp(left, right));
1253 SETs(boolSV(cmp < 0));
1260 djSP; tryAMAGICbinSET(sgt,0);
1263 int cmp = ((PL_op->op_private & OPpLOCALE)
1264 ? sv_cmp_locale(left, right)
1265 : sv_cmp(left, right));
1266 SETs(boolSV(cmp > 0));
1273 djSP; tryAMAGICbinSET(sle,0);
1276 int cmp = ((PL_op->op_private & OPpLOCALE)
1277 ? sv_cmp_locale(left, right)
1278 : sv_cmp(left, right));
1279 SETs(boolSV(cmp <= 0));
1286 djSP; tryAMAGICbinSET(sge,0);
1289 int cmp = ((PL_op->op_private & OPpLOCALE)
1290 ? sv_cmp_locale(left, right)
1291 : sv_cmp(left, right));
1292 SETs(boolSV(cmp >= 0));
1299 djSP; tryAMAGICbinSET(seq,0);
1302 SETs(boolSV(sv_eq(left, right)));
1309 djSP; tryAMAGICbinSET(sne,0);
1312 SETs(boolSV(!sv_eq(left, right)));
1319 djSP; dTARGET; tryAMAGICbin(scmp,0);
1322 int cmp = ((PL_op->op_private & OPpLOCALE)
1323 ? sv_cmp_locale(left, right)
1324 : sv_cmp(left, right));
1332 djSP; dATARGET; tryAMAGICbin(band,opASSIGN);
1335 if (SvNIOKp(left) || SvNIOKp(right)) {
1336 if (PL_op->op_private & HINT_INTEGER) {
1337 IBW value = SvIV(left) & SvIV(right);
1341 UBW value = SvUV(left) & SvUV(right);
1346 do_vop(PL_op->op_type, TARG, left, right);
1355 djSP; dATARGET; tryAMAGICbin(bxor,opASSIGN);
1358 if (SvNIOKp(left) || SvNIOKp(right)) {
1359 if (PL_op->op_private & HINT_INTEGER) {
1360 IBW value = (USE_LEFT(left) ? SvIV(left) : 0) ^ SvIV(right);
1364 UBW value = (USE_LEFT(left) ? SvUV(left) : 0) ^ SvUV(right);
1369 do_vop(PL_op->op_type, TARG, left, right);
1378 djSP; dATARGET; tryAMAGICbin(bor,opASSIGN);
1381 if (SvNIOKp(left) || SvNIOKp(right)) {
1382 if (PL_op->op_private & HINT_INTEGER) {
1383 IBW value = (USE_LEFT(left) ? SvIV(left) : 0) | SvIV(right);
1387 UBW value = (USE_LEFT(left) ? SvUV(left) : 0) | SvUV(right);
1392 do_vop(PL_op->op_type, TARG, left, right);
1401 djSP; dTARGET; tryAMAGICun(neg);
1406 if (SvIOKp(sv) && !SvNOKp(sv) && !SvPOKp(sv) && SvIVX(sv) != IV_MIN)
1408 else if (SvNIOKp(sv))
1410 else if (SvPOKp(sv)) {
1412 char *s = SvPV(sv, len);
1413 if (isIDFIRST(*s)) {
1414 sv_setpvn(TARG, "-", 1);
1417 else if (*s == '+' || *s == '-') {
1419 *SvPV_force(TARG, len) = *s == '-' ? '+' : '-';
1421 else if (IN_UTF8 && *(U8*)s >= 0xc0 && isIDFIRST_utf8((U8*)s)) {
1422 sv_setpvn(TARG, "-", 1);
1426 sv_setnv(TARG, -SvNV(sv));
1437 djSP; tryAMAGICunSET(not);
1438 *PL_stack_sp = boolSV(!SvTRUE(*PL_stack_sp));
1444 djSP; dTARGET; tryAMAGICun(compl);
1448 if (PL_op->op_private & HINT_INTEGER) {
1449 IBW value = ~SvIV(sv);
1453 UBW value = ~SvUV(sv);
1458 register char *tmps;
1459 register long *tmpl;
1464 tmps = SvPV_force(TARG, len);
1467 for ( ; anum && (unsigned long)tmps % sizeof(long); anum--, tmps++)
1470 for ( ; anum >= sizeof(long); anum -= sizeof(long), tmpl++)
1474 for ( ; anum > 0; anum--, tmps++)
1483 /* integer versions of some of the above */
1487 djSP; dATARGET; tryAMAGICbin(mult,opASSIGN);
1490 SETi( left * right );
1497 djSP; dATARGET; tryAMAGICbin(div,opASSIGN);
1501 DIE("Illegal division by zero");
1502 value = POPi / value;
1510 djSP; dATARGET; tryAMAGICbin(modulo,opASSIGN);
1514 DIE("Illegal modulus zero");
1515 SETi( left % right );
1522 djSP; dATARGET; tryAMAGICbin(add,opASSIGN);
1525 SETi( left + right );
1532 djSP; dATARGET; tryAMAGICbin(subtr,opASSIGN);
1535 SETi( left - right );
1542 djSP; tryAMAGICbinSET(lt,0);
1545 SETs(boolSV(left < right));
1552 djSP; tryAMAGICbinSET(gt,0);
1555 SETs(boolSV(left > right));
1562 djSP; tryAMAGICbinSET(le,0);
1565 SETs(boolSV(left <= right));
1572 djSP; tryAMAGICbinSET(ge,0);
1575 SETs(boolSV(left >= right));
1582 djSP; tryAMAGICbinSET(eq,0);
1585 SETs(boolSV(left == right));
1592 djSP; tryAMAGICbinSET(ne,0);
1595 SETs(boolSV(left != right));
1602 djSP; dTARGET; tryAMAGICbin(ncmp,0);
1609 else if (left < right)
1620 djSP; dTARGET; tryAMAGICun(neg);
1625 /* High falutin' math. */
1629 djSP; dTARGET; tryAMAGICbin(atan2,0);
1632 SETn(atan2(left, right));
1639 djSP; dTARGET; tryAMAGICun(sin);
1651 djSP; dTARGET; tryAMAGICun(cos);
1661 /* Support Configure command-line overrides for rand() functions.
1662 After 5.005, perhaps we should replace this by Configure support
1663 for drand48(), random(), or rand(). For 5.005, though, maintain
1664 compatibility by calling rand() but allow the user to override it.
1665 See INSTALL for details. --Andy Dougherty 15 July 1998
1667 /* Now it's after 5.005, and Configure supports drand48() and random(),
1668 in addition to rand(). So the overrides should not be needed any more.
1669 --Jarkko Hietaniemi 27 September 1998
1672 #ifndef HAS_DRAND48_PROTO
1673 extern double drand48 (void);
1686 if (!PL_srand_called) {
1687 (void)seedDrand01((Rand_seed_t)seed());
1688 PL_srand_called = TRUE;
1703 (void)seedDrand01((Rand_seed_t)anum);
1704 PL_srand_called = TRUE;
1713 * This is really just a quick hack which grabs various garbage
1714 * values. It really should be a real hash algorithm which
1715 * spreads the effect of every input bit onto every output bit,
1716 * if someone who knows about such things would bother to write it.
1717 * Might be a good idea to add that function to CORE as well.
1718 * No numbers below come from careful analysis or anything here,
1719 * except they are primes and SEED_C1 > 1E6 to get a full-width
1720 * value from (tv_sec * SEED_C1 + tv_usec). The multipliers should
1721 * probably be bigger too.
1724 # define SEED_C1 1000003
1725 #define SEED_C4 73819
1727 # define SEED_C1 25747
1728 #define SEED_C4 20639
1732 #define SEED_C5 26107
1735 #ifndef PERL_NO_DEV_RANDOM
1740 # include <starlet.h>
1741 /* when[] = (low 32 bits, high 32 bits) of time since epoch
1742 * in 100-ns units, typically incremented ever 10 ms. */
1743 unsigned int when[2];
1745 # ifdef HAS_GETTIMEOFDAY
1746 struct timeval when;
1752 /* This test is an escape hatch, this symbol isn't set by Configure. */
1753 #ifndef PERL_NO_DEV_RANDOM
1754 #ifndef PERL_RANDOM_DEVICE
1755 /* /dev/random isn't used by default because reads from it will block
1756 * if there isn't enough entropy available. You can compile with
1757 * PERL_RANDOM_DEVICE to it if you'd prefer Perl to block until there
1758 * is enough real entropy to fill the seed. */
1759 # define PERL_RANDOM_DEVICE "/dev/urandom"
1761 fd = PerlLIO_open(PERL_RANDOM_DEVICE, 0);
1763 if (PerlLIO_read(fd, &u, sizeof u) != sizeof u)
1772 _ckvmssts(sys$gettim(when));
1773 u = (U32)SEED_C1 * when[0] + (U32)SEED_C2 * when[1];
1775 # ifdef HAS_GETTIMEOFDAY
1776 gettimeofday(&when,(struct timezone *) 0);
1777 u = (U32)SEED_C1 * when.tv_sec + (U32)SEED_C2 * when.tv_usec;
1780 u = (U32)SEED_C1 * when;
1783 u += SEED_C3 * (U32)getpid();
1784 u += SEED_C4 * (U32)(UV)PL_stack_sp;
1785 #ifndef PLAN9 /* XXX Plan9 assembler chokes on this; fix needed */
1786 u += SEED_C5 * (U32)(UV)&when;
1793 djSP; dTARGET; tryAMAGICun(exp);
1805 djSP; dTARGET; tryAMAGICun(log);
1810 SET_NUMERIC_STANDARD();
1811 DIE("Can't take log of %g", value);
1821 djSP; dTARGET; tryAMAGICun(sqrt);
1826 SET_NUMERIC_STANDARD();
1827 DIE("Can't take sqrt of %g", value);
1829 value = sqrt(value);
1839 double value = TOPn;
1842 if (SvIOKp(TOPs) && !SvNOKp(TOPs) && !SvPOKp(TOPs)) {
1848 (void)modf(value, &value);
1850 (void)modf(-value, &value);
1865 djSP; dTARGET; tryAMAGICun(abs);
1867 double value = TOPn;
1870 if (SvIOKp(TOPs) && !SvNOKp(TOPs) && !SvPOKp(TOPs) &&
1871 (iv = SvIVX(TOPs)) != IV_MIN) {
1893 XPUSHu(scan_hex(tmps, 99, &argtype));
1906 while (*tmps && isSPACE(*tmps))
1911 value = scan_hex(++tmps, 99, &argtype);
1912 else if (*tmps == 'b')
1913 value = scan_bin(++tmps, 99, &argtype);
1915 value = scan_oct(tmps, 99, &argtype);
1927 SETi( sv_len_utf8(TOPs) );
1931 SETi( sv_len(TOPs) );
1945 I32 lvalue = PL_op->op_flags & OPf_MOD;
1947 I32 arybase = PL_curcop->cop_arybase;
1951 SvTAINTED_off(TARG); /* decontaminate */
1955 repl = SvPV(sv, repl_len);
1962 tmps = SvPV(sv, curlen);
1964 utfcurlen = sv_len_utf8(sv);
1965 if (utfcurlen == curlen)
1973 if (pos >= arybase) {
1991 else if (len >= 0) {
1993 if (rem > (I32)curlen)
2007 if (ckWARN(WARN_SUBSTR) || lvalue || repl)
2008 warner(WARN_SUBSTR, "substr outside of string");
2013 sv_pos_u2b(sv, &pos, &rem);
2015 sv_setpvn(TARG, tmps, rem);
2016 if (lvalue) { /* it's an lvalue! */
2017 if (!SvGMAGICAL(sv)) {
2021 if (ckWARN(WARN_SUBSTR))
2023 "Attempt to use reference as lvalue in substr");
2025 if (SvOK(sv)) /* is it defined ? */
2026 (void)SvPOK_only(sv);
2028 sv_setpvn(sv,"",0); /* avoid lexical reincarnation */
2031 if (SvTYPE(TARG) < SVt_PVLV) {
2032 sv_upgrade(TARG, SVt_PVLV);
2033 sv_magic(TARG, Nullsv, 'x', Nullch, 0);
2037 if (LvTARG(TARG) != sv) {
2039 SvREFCNT_dec(LvTARG(TARG));
2040 LvTARG(TARG) = SvREFCNT_inc(sv);
2042 LvTARGOFF(TARG) = pos;
2043 LvTARGLEN(TARG) = rem;
2046 sv_insert(sv, pos, rem, repl, repl_len);
2049 PUSHs(TARG); /* avoid SvSETMAGIC here */
2056 register I32 size = POPi;
2057 register I32 offset = POPi;
2058 register SV *src = POPs;
2059 I32 lvalue = PL_op->op_flags & OPf_MOD;
2061 unsigned char *s = (unsigned char*)SvPV(src, srclen);
2062 unsigned long retnum;
2065 SvTAINTED_off(TARG); /* decontaminate */
2066 offset *= size; /* turn into bit offset */
2067 len = (offset + size + 7) / 8;
2068 if (offset < 0 || size < 1)
2071 if (lvalue) { /* it's an lvalue! */
2072 if (SvTYPE(TARG) < SVt_PVLV) {
2073 sv_upgrade(TARG, SVt_PVLV);
2074 sv_magic(TARG, Nullsv, 'v', Nullch, 0);
2078 if (LvTARG(TARG) != src) {
2080 SvREFCNT_dec(LvTARG(TARG));
2081 LvTARG(TARG) = SvREFCNT_inc(src);
2083 LvTARGOFF(TARG) = offset;
2084 LvTARGLEN(TARG) = size;
2092 if (offset >= srclen)
2095 retnum = (unsigned long) s[offset] << 8;
2097 else if (size == 32) {
2098 if (offset >= srclen)
2100 else if (offset + 1 >= srclen)
2101 retnum = (unsigned long) s[offset] << 24;
2102 else if (offset + 2 >= srclen)
2103 retnum = ((unsigned long) s[offset] << 24) +
2104 ((unsigned long) s[offset + 1] << 16);
2106 retnum = ((unsigned long) s[offset] << 24) +
2107 ((unsigned long) s[offset + 1] << 16) +
2108 (s[offset + 2] << 8);
2113 retnum = (s[offset >> 3] >> (offset & 7)) & ((1 << size) - 1);
2118 else if (size == 16)
2119 retnum = ((unsigned long) s[offset] << 8) + s[offset+1];
2120 else if (size == 32)
2121 retnum = ((unsigned long) s[offset] << 24) +
2122 ((unsigned long) s[offset + 1] << 16) +
2123 (s[offset + 2] << 8) + s[offset+3];
2127 sv_setuv(TARG, (UV)retnum);
2142 I32 arybase = PL_curcop->cop_arybase;
2147 offset = POPi - arybase;
2150 tmps = SvPV(big, biglen);
2151 if (IN_UTF8 && offset > 0)
2152 sv_pos_u2b(big, &offset, 0);
2155 else if (offset > biglen)
2157 if (!(tmps2 = fbm_instr((unsigned char*)tmps + offset,
2158 (unsigned char*)tmps + biglen, little, 0)))
2161 retval = tmps2 - tmps;
2162 if (IN_UTF8 && retval > 0)
2163 sv_pos_b2u(big, &retval);
2164 PUSHi(retval + arybase);
2179 I32 arybase = PL_curcop->cop_arybase;
2185 tmps2 = SvPV(little, llen);
2186 tmps = SvPV(big, blen);
2190 if (IN_UTF8 && offset > 0)
2191 sv_pos_u2b(big, &offset, 0);
2192 offset = offset - arybase + llen;
2196 else if (offset > blen)
2198 if (!(tmps2 = rninstr(tmps, tmps + offset,
2199 tmps2, tmps2 + llen)))
2202 retval = tmps2 - tmps;
2203 if (IN_UTF8 && retval > 0)
2204 sv_pos_b2u(big, &retval);
2205 PUSHi(retval + arybase);
2211 djSP; dMARK; dORIGMARK; dTARGET;
2212 #ifdef USE_LOCALE_NUMERIC
2213 if (PL_op->op_private & OPpLOCALE)
2214 SET_NUMERIC_LOCAL();
2216 SET_NUMERIC_STANDARD();
2218 do_sprintf(TARG, SP-MARK, MARK+1);
2219 TAINT_IF(SvTAINTED(TARG));
2230 U8 *tmps = (U8*)POPpx;
2233 if (IN_UTF8 && (*tmps & 0x80))
2234 value = utf8_to_uv(tmps, &retlen);
2236 value = (UV)(*tmps & 255);
2247 (void)SvUPGRADE(TARG,SVt_PV);
2249 if (IN_UTF8 && value >= 128) {
2252 tmps = (char*)uv_to_utf8((U8*)tmps, (UV)value);
2253 SvCUR_set(TARG, tmps - SvPVX(TARG));
2255 (void)SvPOK_only(TARG);
2265 (void)SvPOK_only(TARG);
2272 djSP; dTARGET; dPOPTOPssrl;
2275 char *tmps = SvPV(left, n_a);
2277 sv_setpv(TARG, fcrypt(tmps, SvPV(right, n_a)));
2279 sv_setpv(TARG, PerlProc_crypt(tmps, SvPV(right, n_a)));
2283 "The crypt() function is unimplemented due to excessive paranoia.");
2296 if (IN_UTF8 && (s = (U8*)SvPV(sv, slen)) && slen && (*s & 0xc0) == 0xc0) {
2300 UV uv = utf8_to_uv(s, &ulen);
2302 if (PL_op->op_private & OPpLOCALE) {
2305 uv = toTITLE_LC_uni(uv);
2308 uv = toTITLE_utf8(s);
2310 tend = uv_to_utf8(tmpbuf, uv);
2312 if (!SvPADTMP(sv) || tend - tmpbuf != ulen) {
2314 sv_setpvn(TARG, (char*)tmpbuf, tend - tmpbuf);
2315 sv_catpvn(TARG, (char*)(s + ulen), slen - ulen);
2319 s = (U8*)SvPV_force(sv, slen);
2320 Copy(tmpbuf, s, ulen, U8);
2325 if (!SvPADTMP(sv)) {
2331 s = (U8*)SvPV_force(sv, slen);
2333 if (PL_op->op_private & OPpLOCALE) {
2336 *s = toUPPER_LC(*s);
2352 if (IN_UTF8 && (s = (U8*)SvPV(sv, slen)) && slen && (*s & 0xc0) == 0xc0) {
2356 UV uv = utf8_to_uv(s, &ulen);
2358 if (PL_op->op_private & OPpLOCALE) {
2361 uv = toLOWER_LC_uni(uv);
2364 uv = toLOWER_utf8(s);
2366 tend = uv_to_utf8(tmpbuf, uv);
2368 if (!SvPADTMP(sv) || tend - tmpbuf != ulen) {
2370 sv_setpvn(TARG, (char*)tmpbuf, tend - tmpbuf);
2371 sv_catpvn(TARG, (char*)(s + ulen), slen - ulen);
2375 s = (U8*)SvPV_force(sv, slen);
2376 Copy(tmpbuf, s, ulen, U8);
2381 if (!SvPADTMP(sv)) {
2387 s = (U8*)SvPV_force(sv, slen);
2389 if (PL_op->op_private & OPpLOCALE) {
2392 *s = toLOWER_LC(*s);
2415 s = (U8*)SvPV(sv,len);
2417 sv_setpvn(TARG, "", 0);
2422 (void)SvUPGRADE(TARG, SVt_PV);
2423 SvGROW(TARG, (len * 2) + 1);
2424 (void)SvPOK_only(TARG);
2425 d = (U8*)SvPVX(TARG);
2427 if (PL_op->op_private & OPpLOCALE) {
2431 d = uv_to_utf8(d, toUPPER_LC_uni( utf8_to_uv(s, &ulen)));
2437 d = uv_to_utf8(d, toUPPER_utf8( s ));
2442 SvCUR_set(TARG, d - (U8*)SvPVX(TARG));
2447 if (!SvPADTMP(sv)) {
2454 s = (U8*)SvPV_force(sv, len);
2456 register U8 *send = s + len;
2458 if (PL_op->op_private & OPpLOCALE) {
2461 for (; s < send; s++)
2462 *s = toUPPER_LC(*s);
2465 for (; s < send; s++)
2485 s = (U8*)SvPV(sv,len);
2487 sv_setpvn(TARG, "", 0);
2492 (void)SvUPGRADE(TARG, SVt_PV);
2493 SvGROW(TARG, (len * 2) + 1);
2494 (void)SvPOK_only(TARG);
2495 d = (U8*)SvPVX(TARG);
2497 if (PL_op->op_private & OPpLOCALE) {
2501 d = uv_to_utf8(d, toLOWER_LC_uni( utf8_to_uv(s, &ulen)));
2507 d = uv_to_utf8(d, toLOWER_utf8(s));
2512 SvCUR_set(TARG, d - (U8*)SvPVX(TARG));
2517 if (!SvPADTMP(sv)) {
2524 s = (U8*)SvPV_force(sv, len);
2526 register U8 *send = s + len;
2528 if (PL_op->op_private & OPpLOCALE) {
2531 for (; s < send; s++)
2532 *s = toLOWER_LC(*s);
2535 for (; s < send; s++)
2547 register char *s = SvPV(sv,len);
2551 (void)SvUPGRADE(TARG, SVt_PV);
2552 SvGROW(TARG, (len * 2) + 1);
2557 STRLEN ulen = UTF8SKIP(s);
2580 SvCUR_set(TARG, d - SvPVX(TARG));
2581 (void)SvPOK_only(TARG);
2584 sv_setpvn(TARG, s, len);
2593 djSP; dMARK; dORIGMARK;
2595 register AV* av = (AV*)POPs;
2596 register I32 lval = PL_op->op_flags & OPf_MOD;
2597 I32 arybase = PL_curcop->cop_arybase;
2600 if (SvTYPE(av) == SVt_PVAV) {
2601 if (lval && PL_op->op_private & OPpLVAL_INTRO) {
2603 for (svp = MARK + 1; svp <= SP; svp++) {
2608 if (max > AvMAX(av))
2611 while (++MARK <= SP) {
2612 elem = SvIVx(*MARK);
2616 svp = av_fetch(av, elem, lval);
2618 if (!svp || *svp == &PL_sv_undef)
2619 DIE(PL_no_aelem, elem);
2620 if (PL_op->op_private & OPpLVAL_INTRO)
2621 save_aelem(av, elem, svp);
2623 *MARK = svp ? *svp : &PL_sv_undef;
2626 if (GIMME != G_ARRAY) {
2634 /* Associative arrays. */
2639 HV *hash = (HV*)POPs;
2641 I32 gimme = GIMME_V;
2642 I32 realhv = (SvTYPE(hash) == SVt_PVHV);
2645 /* might clobber stack_sp */
2646 entry = realhv ? hv_iternext(hash) : avhv_iternext((AV*)hash);
2651 PUSHs(hv_iterkeysv(entry)); /* won't clobber stack_sp */
2652 if (gimme == G_ARRAY) {
2654 /* might clobber stack_sp */
2655 sv_setsv(TARG, realhv ?
2656 hv_iterval(hash, entry) : avhv_iterval((AV*)hash, entry));
2661 else if (gimme == G_SCALAR)
2680 I32 gimme = GIMME_V;
2681 I32 discard = (gimme == G_VOID) ? G_DISCARD : 0;
2685 if (PL_op->op_private & OPpSLICE) {
2689 hvtype = SvTYPE(hv);
2690 while (++MARK <= SP) {
2691 if (hvtype == SVt_PVHV)
2692 sv = hv_delete_ent(hv, *MARK, discard, 0);
2694 DIE("Not a HASH reference");
2695 *MARK = sv ? sv : &PL_sv_undef;
2699 else if (gimme == G_SCALAR) {
2708 if (SvTYPE(hv) == SVt_PVHV)
2709 sv = hv_delete_ent(hv, keysv, discard, 0);
2711 DIE("Not a HASH reference");
2725 if (SvTYPE(hv) == SVt_PVHV) {
2726 if (hv_exists_ent(hv, tmpsv, 0))
2729 else if (SvTYPE(hv) == SVt_PVAV) {
2730 if (avhv_exists_ent((AV*)hv, tmpsv, 0))
2734 DIE("Not a HASH reference");
2741 djSP; dMARK; dORIGMARK;
2742 register HV *hv = (HV*)POPs;
2743 register I32 lval = PL_op->op_flags & OPf_MOD;
2744 I32 realhv = (SvTYPE(hv) == SVt_PVHV);
2746 if (!realhv && PL_op->op_private & OPpLVAL_INTRO)
2747 DIE("Can't localize pseudo-hash element");
2749 if (realhv || SvTYPE(hv) == SVt_PVAV) {
2750 while (++MARK <= SP) {
2754 HE *he = hv_fetch_ent(hv, keysv, lval, 0);
2755 svp = he ? &HeVAL(he) : 0;
2758 svp = avhv_fetch_ent((AV*)hv, keysv, lval, 0);
2761 if (!svp || *svp == &PL_sv_undef) {
2763 DIE(PL_no_helem, SvPV(keysv, n_a));
2765 if (PL_op->op_private & OPpLVAL_INTRO)
2766 save_helem(hv, keysv, svp);
2768 *MARK = svp ? *svp : &PL_sv_undef;
2771 if (GIMME != G_ARRAY) {
2779 /* List operators. */
2784 if (GIMME != G_ARRAY) {
2786 *MARK = *SP; /* unwanted list, return last item */
2788 *MARK = &PL_sv_undef;
2797 SV **lastrelem = PL_stack_sp;
2798 SV **lastlelem = PL_stack_base + POPMARK;
2799 SV **firstlelem = PL_stack_base + POPMARK + 1;
2800 register SV **firstrelem = lastlelem + 1;
2801 I32 arybase = PL_curcop->cop_arybase;
2802 I32 lval = PL_op->op_flags & OPf_MOD;
2803 I32 is_something_there = lval;
2805 register I32 max = lastrelem - lastlelem;
2806 register SV **lelem;
2809 if (GIMME != G_ARRAY) {
2810 ix = SvIVx(*lastlelem);
2815 if (ix < 0 || ix >= max)
2816 *firstlelem = &PL_sv_undef;
2818 *firstlelem = firstrelem[ix];
2824 SP = firstlelem - 1;
2828 for (lelem = firstlelem; lelem <= lastlelem; lelem++) {
2834 if (ix < 0 || ix >= max)
2835 *lelem = &PL_sv_undef;
2837 is_something_there = TRUE;
2838 if (!(*lelem = firstrelem[ix]))
2839 *lelem = &PL_sv_undef;
2842 if (is_something_there)
2845 SP = firstlelem - 1;
2851 djSP; dMARK; dORIGMARK;
2852 I32 items = SP - MARK;
2853 SV *av = sv_2mortal((SV*)av_make(items, MARK+1));
2854 SP = ORIGMARK; /* av_make() might realloc stack_sp */
2861 djSP; dMARK; dORIGMARK;
2862 HV* hv = (HV*)sv_2mortal((SV*)newHV());
2866 SV *val = NEWSV(46, 0);
2868 sv_setsv(val, *++MARK);
2869 else if (ckWARN(WARN_UNSAFE))
2870 warner(WARN_UNSAFE, "Odd number of elements in hash assignment");
2871 (void)hv_store_ent(hv,key,val,0);
2880 djSP; dMARK; dORIGMARK;
2881 register AV *ary = (AV*)*++MARK;
2885 register I32 offset;
2886 register I32 length;
2893 if (mg = SvTIED_mg((SV*)ary, 'P')) {
2894 *MARK-- = SvTIED_obj((SV*)ary, mg);
2898 perl_call_method("SPLICE",GIMME_V);
2907 offset = i = SvIVx(*MARK);
2909 offset += AvFILLp(ary) + 1;
2911 offset -= PL_curcop->cop_arybase;
2913 DIE(PL_no_aelem, i);
2915 length = SvIVx(*MARK++);
2917 length += AvFILLp(ary) - offset + 1;
2923 length = AvMAX(ary) + 1; /* close enough to infinity */
2927 length = AvMAX(ary) + 1;
2929 if (offset > AvFILLp(ary) + 1)
2930 offset = AvFILLp(ary) + 1;
2931 after = AvFILLp(ary) + 1 - (offset + length);
2932 if (after < 0) { /* not that much array */
2933 length += after; /* offset+length now in array */
2939 /* At this point, MARK .. SP-1 is our new LIST */
2942 diff = newlen - length;
2943 if (newlen && !AvREAL(ary) && AvREIFY(ary))
2946 if (diff < 0) { /* shrinking the area */
2948 New(451, tmparyval, newlen, SV*); /* so remember insertion */
2949 Copy(MARK, tmparyval, newlen, SV*);
2952 MARK = ORIGMARK + 1;
2953 if (GIMME == G_ARRAY) { /* copy return vals to stack */
2954 MEXTEND(MARK, length);
2955 Copy(AvARRAY(ary)+offset, MARK, length, SV*);
2957 EXTEND_MORTAL(length);
2958 for (i = length, dst = MARK; i; i--) {
2959 sv_2mortal(*dst); /* free them eventualy */
2966 *MARK = AvARRAY(ary)[offset+length-1];
2969 for (i = length - 1, dst = &AvARRAY(ary)[offset]; i > 0; i--)
2970 SvREFCNT_dec(*dst++); /* free them now */
2973 AvFILLp(ary) += diff;
2975 /* pull up or down? */
2977 if (offset < after) { /* easier to pull up */
2978 if (offset) { /* esp. if nothing to pull */
2979 src = &AvARRAY(ary)[offset-1];
2980 dst = src - diff; /* diff is negative */
2981 for (i = offset; i > 0; i--) /* can't trust Copy */
2985 SvPVX(ary) = (char*)(AvARRAY(ary) - diff); /* diff is negative */
2989 if (after) { /* anything to pull down? */
2990 src = AvARRAY(ary) + offset + length;
2991 dst = src + diff; /* diff is negative */
2992 Move(src, dst, after, SV*);
2994 dst = &AvARRAY(ary)[AvFILLp(ary)+1];
2995 /* avoid later double free */
2999 dst[--i] = &PL_sv_undef;
3002 for (src = tmparyval, dst = AvARRAY(ary) + offset;
3004 *dst = NEWSV(46, 0);
3005 sv_setsv(*dst++, *src++);
3007 Safefree(tmparyval);
3010 else { /* no, expanding (or same) */
3012 New(452, tmparyval, length, SV*); /* so remember deletion */
3013 Copy(AvARRAY(ary)+offset, tmparyval, length, SV*);
3016 if (diff > 0) { /* expanding */
3018 /* push up or down? */
3020 if (offset < after && diff <= AvARRAY(ary) - AvALLOC(ary)) {
3024 Move(src, dst, offset, SV*);
3026 SvPVX(ary) = (char*)(AvARRAY(ary) - diff);/* diff is positive */
3028 AvFILLp(ary) += diff;
3031 if (AvFILLp(ary) + diff >= AvMAX(ary)) /* oh, well */
3032 av_extend(ary, AvFILLp(ary) + diff);
3033 AvFILLp(ary) += diff;
3036 dst = AvARRAY(ary) + AvFILLp(ary);
3038 for (i = after; i; i--) {
3045 for (src = MARK, dst = AvARRAY(ary) + offset; newlen; newlen--) {
3046 *dst = NEWSV(46, 0);
3047 sv_setsv(*dst++, *src++);
3049 MARK = ORIGMARK + 1;
3050 if (GIMME == G_ARRAY) { /* copy return vals to stack */
3052 Copy(tmparyval, MARK, length, SV*);
3054 EXTEND_MORTAL(length);
3055 for (i = length, dst = MARK; i; i--) {
3056 sv_2mortal(*dst); /* free them eventualy */
3060 Safefree(tmparyval);
3064 else if (length--) {
3065 *MARK = tmparyval[length];
3068 while (length-- > 0)
3069 SvREFCNT_dec(tmparyval[length]);
3071 Safefree(tmparyval);
3074 *MARK = &PL_sv_undef;
3082 djSP; dMARK; dORIGMARK; dTARGET;
3083 register AV *ary = (AV*)*++MARK;
3084 register SV *sv = &PL_sv_undef;
3087 if (mg = SvTIED_mg((SV*)ary, 'P')) {
3088 *MARK-- = SvTIED_obj((SV*)ary, mg);
3092 perl_call_method("PUSH",G_SCALAR|G_DISCARD);
3097 /* Why no pre-extend of ary here ? */
3098 for (++MARK; MARK <= SP; MARK++) {
3101 sv_setsv(sv, *MARK);
3106 PUSHi( AvFILL(ary) + 1 );
3114 SV *sv = av_pop(av);
3116 (void)sv_2mortal(sv);
3125 SV *sv = av_shift(av);
3130 (void)sv_2mortal(sv);
3137 djSP; dMARK; dORIGMARK; dTARGET;
3138 register AV *ary = (AV*)*++MARK;
3143 if (mg = SvTIED_mg((SV*)ary, 'P')) {
3144 *MARK-- = SvTIED_obj((SV*)ary, mg);
3148 perl_call_method("UNSHIFT",G_SCALAR|G_DISCARD);
3153 av_unshift(ary, SP - MARK);
3156 sv_setsv(sv, *++MARK);
3157 (void)av_store(ary, i++, sv);
3161 PUSHi( AvFILL(ary) + 1 );
3171 if (GIMME == G_ARRAY) {
3182 register char *down;
3188 do_join(TARG, &PL_sv_no, MARK, SP);
3190 sv_setsv(TARG, (SP > MARK) ? *SP : DEFSV);
3191 up = SvPV_force(TARG, len);
3193 if (IN_UTF8) { /* first reverse each character */
3194 U8* s = (U8*)SvPVX(TARG);
3195 U8* send = (U8*)(s + len);
3204 down = (char*)(s - 1);
3205 if (s > send || !((*down & 0xc0) == 0x80)) {
3206 warn("Malformed UTF-8 character");
3218 down = SvPVX(TARG) + len - 1;
3224 (void)SvPOK_only(TARG);
3233 mul128(SV *sv, U8 m)
3236 char *s = SvPV(sv, len);
3240 if (!strnEQ(s, "0000", 4)) { /* need to grow sv */
3241 SV *tmpNew = newSVpvn("0000000000", 10);
3243 sv_catsv(tmpNew, sv);
3244 SvREFCNT_dec(sv); /* free old sv */
3249 while (!*t) /* trailing '\0'? */
3252 i = ((*t - '0') << 7) + m;
3253 *(t--) = '0' + (i % 10);
3259 /* Explosives and implosives. */
3261 #if 'I' == 73 && 'J' == 74
3262 /* On an ASCII/ISO kind of system */
3263 #define ISUUCHAR(ch) ((ch) >= ' ' && (ch) < 'a')
3266 Some other sort of character set - use memchr() so we don't match
3269 #define ISUUCHAR(ch) (memchr(PL_uuemap, (ch), sizeof(PL_uuemap)-1) || (ch) == ' ')
3277 I32 gimme = GIMME_V;
3281 register char *pat = SvPV(left, llen);
3282 register char *s = SvPV(right, rlen);
3283 char *strend = s + rlen;
3285 register char *patend = pat + llen;
3290 /* These must not be in registers: */
3307 register U32 culong;
3310 #ifdef PERL_NATINT_PACK
3311 int natint; /* native integer */
3312 int unatint; /* unsigned native integer */
3315 if (gimme != G_ARRAY) { /* arrange to do first one only */
3317 for (patend = pat; !isALPHA(*patend) || *patend == 'x'; patend++) ;
3318 if (strchr("aAZbBhHP", *patend) || *pat == '%') {
3320 while (isDIGIT(*patend) || *patend == '*')
3326 while (pat < patend) {
3328 datumtype = *pat++ & 0xFF;
3329 #ifdef PERL_NATINT_PACK
3332 if (isSPACE(datumtype))
3335 char *natstr = "sSiIlL";
3337 if (strchr(natstr, datumtype)) {
3338 #ifdef PERL_NATINT_PACK
3344 croak("'!' allowed only after types %s", natstr);
3348 else if (*pat == '*') {
3349 len = strend - strbeg; /* long enough */
3352 else if (isDIGIT(*pat)) {
3354 while (isDIGIT(*pat))
3355 len = (len * 10) + (*pat++ - '0');
3358 len = (datumtype != '@');
3361 croak("Invalid type in unpack: '%c'", (int)datumtype);
3362 case ',': /* grandfather in commas but with a warning */
3363 if (commas++ == 0 && ckWARN(WARN_UNSAFE))
3364 warner(WARN_UNSAFE, "Invalid type in unpack: '%c'", (int)datumtype);
3367 if (len == 1 && pat[-1] != '1')
3376 if (len > strend - strbeg)
3377 DIE("@ outside of string");
3381 if (len > s - strbeg)
3382 DIE("X outside of string");
3386 if (len > strend - s)
3387 DIE("x outside of string");
3393 if (len > strend - s)
3396 goto uchar_checksum;
3397 sv = NEWSV(35, len);
3398 sv_setpvn(sv, s, len);
3400 if (datumtype == 'A' || datumtype == 'Z') {
3401 aptr = s; /* borrow register */
3402 if (datumtype == 'Z') { /* 'Z' strips stuff after first null */
3407 else { /* 'A' strips both nulls and spaces */
3408 s = SvPVX(sv) + len - 1;
3409 while (s >= SvPVX(sv) && (!*s || isSPACE(*s)))
3413 SvCUR_set(sv, s - SvPVX(sv));
3414 s = aptr; /* unborrow register */
3416 XPUSHs(sv_2mortal(sv));
3420 if (pat[-1] == '*' || len > (strend - s) * 8)
3421 len = (strend - s) * 8;
3424 Newz(601, PL_bitcount, 256, char);
3425 for (bits = 1; bits < 256; bits++) {
3426 if (bits & 1) PL_bitcount[bits]++;
3427 if (bits & 2) PL_bitcount[bits]++;
3428 if (bits & 4) PL_bitcount[bits]++;
3429 if (bits & 8) PL_bitcount[bits]++;
3430 if (bits & 16) PL_bitcount[bits]++;
3431 if (bits & 32) PL_bitcount[bits]++;
3432 if (bits & 64) PL_bitcount[bits]++;
3433 if (bits & 128) PL_bitcount[bits]++;
3437 culong += PL_bitcount[*(unsigned char*)s++];
3442 if (datumtype == 'b') {
3444 if (bits & 1) culong++;
3450 if (bits & 128) culong++;
3457 sv = NEWSV(35, len + 1);
3460 aptr = pat; /* borrow register */
3462 if (datumtype == 'b') {
3464 for (len = 0; len < aint; len++) {
3465 if (len & 7) /*SUPPRESS 595*/
3469 *pat++ = '0' + (bits & 1);
3474 for (len = 0; len < aint; len++) {
3479 *pat++ = '0' + ((bits & 128) != 0);
3483 pat = aptr; /* unborrow register */
3484 XPUSHs(sv_2mortal(sv));
3488 if (pat[-1] == '*' || len > (strend - s) * 2)
3489 len = (strend - s) * 2;
3490 sv = NEWSV(35, len + 1);
3493 aptr = pat; /* borrow register */
3495 if (datumtype == 'h') {
3497 for (len = 0; len < aint; len++) {
3502 *pat++ = PL_hexdigit[bits & 15];
3507 for (len = 0; len < aint; len++) {
3512 *pat++ = PL_hexdigit[(bits >> 4) & 15];
3516 pat = aptr; /* unborrow register */
3517 XPUSHs(sv_2mortal(sv));
3520 if (len > strend - s)
3525 if (aint >= 128) /* fake up signed chars */
3535 if (aint >= 128) /* fake up signed chars */
3538 sv_setiv(sv, (IV)aint);
3539 PUSHs(sv_2mortal(sv));
3544 if (len > strend - s)
3559 sv_setiv(sv, (IV)auint);
3560 PUSHs(sv_2mortal(sv));
3565 if (len > strend - s)
3568 while (len-- > 0 && s < strend) {
3569 auint = utf8_to_uv((U8*)s, &along);
3572 cdouble += (double)auint;
3580 while (len-- > 0 && s < strend) {
3581 auint = utf8_to_uv((U8*)s, &along);
3584 sv_setuv(sv, (UV)auint);
3585 PUSHs(sv_2mortal(sv));
3590 #if SHORTSIZE == SIZE16
3591 along = (strend - s) / SIZE16;
3593 along = (strend - s) / (natint ? sizeof(short) : SIZE16);
3598 #if SHORTSIZE != SIZE16
3601 COPYNN(s, &ashort, sizeof(short));
3612 #if SHORTSIZE > SIZE16
3624 #if SHORTSIZE != SIZE16
3627 COPYNN(s, &ashort, sizeof(short));
3630 sv_setiv(sv, (IV)ashort);
3631 PUSHs(sv_2mortal(sv));
3639 #if SHORTSIZE > SIZE16
3645 sv_setiv(sv, (IV)ashort);
3646 PUSHs(sv_2mortal(sv));
3654 #if SHORTSIZE == SIZE16
3655 along = (strend - s) / SIZE16;
3657 unatint = natint && datumtype == 'S';
3658 along = (strend - s) / (unatint ? sizeof(unsigned short) : SIZE16);
3663 #if SHORTSIZE != SIZE16
3666 COPYNN(s, &aushort, sizeof(unsigned short));
3667 s += sizeof(unsigned short);
3675 COPY16(s, &aushort);
3678 if (datumtype == 'n')
3679 aushort = PerlSock_ntohs(aushort);
3682 if (datumtype == 'v')
3683 aushort = vtohs(aushort);
3692 #if SHORTSIZE != SIZE16
3695 COPYNN(s, &aushort, sizeof(unsigned short));
3696 s += sizeof(unsigned short);
3698 sv_setiv(sv, (UV)aushort);
3699 PUSHs(sv_2mortal(sv));
3706 COPY16(s, &aushort);
3710 if (datumtype == 'n')
3711 aushort = PerlSock_ntohs(aushort);
3714 if (datumtype == 'v')
3715 aushort = vtohs(aushort);
3717 sv_setiv(sv, (UV)aushort);
3718 PUSHs(sv_2mortal(sv));
3724 along = (strend - s) / sizeof(int);
3729 Copy(s, &aint, 1, int);
3732 cdouble += (double)aint;
3741 Copy(s, &aint, 1, int);
3745 /* Without the dummy below unpack("i", pack("i",-1))
3746 * return 0xFFffFFff instead of -1 for Digital Unix V4.0
3747 * cc with optimization turned on.
3749 * The bug was detected in
3750 * DEC C V5.8-009 on Digital UNIX V4.0 (Rev. 1091) (V4.0E)
3751 * with optimization (-O4) turned on.
3752 * DEC C V5.2-040 on Digital UNIX V4.0 (Rev. 564) (V4.0B)
3753 * does not have this problem even with -O4.
3755 * This bug was reported as DECC_BUGS 1431
3756 * and tracked internally as GEM_BUGS 7775.
3758 * The bug is fixed in
3759 * Tru64 UNIX V5.0: Compaq C V6.1-006 or later
3760 * UNIX V4.0F support: DEC C V5.9-006 or later
3761 * UNIX V4.0E support: DEC C V5.8-011 or later
3764 * See also few lines later for the same bug.
3767 sv_setiv(sv, (IV)aint) :
3769 sv_setiv(sv, (IV)aint);
3770 PUSHs(sv_2mortal(sv));
3775 along = (strend - s) / sizeof(unsigned int);
3780 Copy(s, &auint, 1, unsigned int);
3781 s += sizeof(unsigned int);
3783 cdouble += (double)auint;
3792 Copy(s, &auint, 1, unsigned int);
3793 s += sizeof(unsigned int);
3796 /* Without the dummy below unpack("I", pack("I",0xFFFFFFFF))
3797 * returns 1.84467440737096e+19 instead of 0xFFFFFFFF.
3798 * See details few lines earlier. */
3800 sv_setuv(sv, (UV)auint) :
3802 sv_setuv(sv, (UV)auint);
3803 PUSHs(sv_2mortal(sv));
3808 #if LONGSIZE == SIZE32
3809 along = (strend - s) / SIZE32;
3811 along = (strend - s) / (natint ? sizeof(long) : SIZE32);
3816 #if LONGSIZE != SIZE32
3819 COPYNN(s, &along, sizeof(long));
3822 cdouble += (double)along;
3832 #if LONGSIZE > SIZE32
3833 if (along > 2147483647)
3834 along -= 4294967296;
3838 cdouble += (double)along;
3847 #if LONGSIZE != SIZE32
3850 COPYNN(s, &along, sizeof(long));
3853 sv_setiv(sv, (IV)along);
3854 PUSHs(sv_2mortal(sv));
3862 #if LONGSIZE > SIZE32
3863 if (along > 2147483647)
3864 along -= 4294967296;
3868 sv_setiv(sv, (IV)along);
3869 PUSHs(sv_2mortal(sv));
3877 #if LONGSIZE == SIZE32
3878 along = (strend - s) / SIZE32;
3880 unatint = natint && datumtype == 'L';
3881 along = (strend - s) / (unatint ? sizeof(unsigned long) : SIZE32);
3886 #if LONGSIZE != SIZE32
3889 COPYNN(s, &aulong, sizeof(unsigned long));
3890 s += sizeof(unsigned long);
3892 cdouble += (double)aulong;
3904 if (datumtype == 'N')
3905 aulong = PerlSock_ntohl(aulong);
3908 if (datumtype == 'V')
3909 aulong = vtohl(aulong);
3912 cdouble += (double)aulong;
3921 #if LONGSIZE != SIZE32
3924 COPYNN(s, &aulong, sizeof(unsigned long));
3925 s += sizeof(unsigned long);
3927 sv_setuv(sv, (UV)aulong);
3928 PUSHs(sv_2mortal(sv));
3938 if (datumtype == 'N')
3939 aulong = PerlSock_ntohl(aulong);
3942 if (datumtype == 'V')
3943 aulong = vtohl(aulong);
3946 sv_setuv(sv, (UV)aulong);
3947 PUSHs(sv_2mortal(sv));
3953 along = (strend - s) / sizeof(char*);
3959 if (sizeof(char*) > strend - s)
3962 Copy(s, &aptr, 1, char*);
3968 PUSHs(sv_2mortal(sv));
3978 while ((len > 0) && (s < strend)) {
3979 auv = (auv << 7) | (*s & 0x7f);
3980 if (!(*s++ & 0x80)) {
3984 PUSHs(sv_2mortal(sv));
3988 else if (++bytes >= sizeof(UV)) { /* promote to string */
3992 sv = newSVpvf("%.*Vu", (int)TYPE_DIGITS(UV), auv);
3993 while (s < strend) {
3994 sv = mul128(sv, *s & 0x7f);
3995 if (!(*s++ & 0x80)) {
4004 PUSHs(sv_2mortal(sv));
4009 if ((s >= strend) && bytes)
4010 croak("Unterminated compressed integer");
4015 if (sizeof(char*) > strend - s)
4018 Copy(s, &aptr, 1, char*);
4023 sv_setpvn(sv, aptr, len);
4024 PUSHs(sv_2mortal(sv));
4028 along = (strend - s) / sizeof(Quad_t);
4034 if (s + sizeof(Quad_t) > strend)
4037 Copy(s, &aquad, 1, Quad_t);
4038 s += sizeof(Quad_t);
4041 if (aquad >= IV_MIN && aquad <= IV_MAX)
4042 sv_setiv(sv, (IV)aquad);
4044 sv_setnv(sv, (double)aquad);
4045 PUSHs(sv_2mortal(sv));
4049 along = (strend - s) / sizeof(Quad_t);
4055 if (s + sizeof(Uquad_t) > strend)
4058 Copy(s, &auquad, 1, Uquad_t);
4059 s += sizeof(Uquad_t);
4062 if (auquad <= UV_MAX)
4063 sv_setuv(sv, (UV)auquad);
4065 sv_setnv(sv, (double)auquad);
4066 PUSHs(sv_2mortal(sv));
4070 /* float and double added gnb@melba.bby.oz.au 22/11/89 */
4073 along = (strend - s) / sizeof(float);
4078 Copy(s, &afloat, 1, float);
4087 Copy(s, &afloat, 1, float);
4090 sv_setnv(sv, (double)afloat);
4091 PUSHs(sv_2mortal(sv));
4097 along = (strend - s) / sizeof(double);
4102 Copy(s, &adouble, 1, double);
4103 s += sizeof(double);
4111 Copy(s, &adouble, 1, double);
4112 s += sizeof(double);
4114 sv_setnv(sv, (double)adouble);
4115 PUSHs(sv_2mortal(sv));
4121 * Initialise the decode mapping. By using a table driven
4122 * algorithm, the code will be character-set independent
4123 * (and just as fast as doing character arithmetic)
4125 if (PL_uudmap['M'] == 0) {
4128 for (i = 0; i < sizeof(PL_uuemap); i += 1)
4129 PL_uudmap[PL_uuemap[i]] = i;
4131 * Because ' ' and '`' map to the same value,
4132 * we need to decode them both the same.
4137 along = (strend - s) * 3 / 4;
4138 sv = NEWSV(42, along);
4141 while (s < strend && *s > ' ' && ISUUCHAR(*s)) {
4146 len = PL_uudmap[*s++] & 077;
4148 if (s < strend && ISUUCHAR(*s))
4149 a = PL_uudmap[*s++] & 077;
4152 if (s < strend && ISUUCHAR(*s))
4153 b = PL_uudmap[*s++] & 077;
4156 if (s < strend && ISUUCHAR(*s))
4157 c = PL_uudmap[*s++] & 077;
4160 if (s < strend && ISUUCHAR(*s))
4161 d = PL_uudmap[*s++] & 077;
4164 hunk[0] = (a << 2) | (b >> 4);
4165 hunk[1] = (b << 4) | (c >> 2);
4166 hunk[2] = (c << 6) | d;
4167 sv_catpvn(sv, hunk, (len > 3) ? 3 : len);
4172 else if (s[1] == '\n') /* possible checksum byte */
4175 XPUSHs(sv_2mortal(sv));
4180 if (strchr("fFdD", datumtype) ||
4181 (checksum > 32 && strchr("iIlLNU", datumtype)) ) {
4185 while (checksum >= 16) {
4189 while (checksum >= 4) {
4195 along = (1 << checksum) - 1;
4196 while (cdouble < 0.0)
4198 cdouble = modf(cdouble / adouble, &trouble) * adouble;
4199 sv_setnv(sv, cdouble);
4202 if (checksum < 32) {
4203 aulong = (1 << checksum) - 1;
4206 sv_setuv(sv, (UV)culong);
4208 XPUSHs(sv_2mortal(sv));
4212 if (SP == oldsp && gimme == G_SCALAR)
4213 PUSHs(&PL_sv_undef);
4218 doencodes(register SV *sv, register char *s, register I32 len)
4222 *hunk = PL_uuemap[len];
4223 sv_catpvn(sv, hunk, 1);
4226 hunk[0] = PL_uuemap[(077 & (*s >> 2))];
4227 hunk[1] = PL_uuemap[(077 & (((*s << 4) & 060) | ((s[1] >> 4) & 017)))];
4228 hunk[2] = PL_uuemap[(077 & (((s[1] << 2) & 074) | ((s[2] >> 6) & 03)))];
4229 hunk[3] = PL_uuemap[(077 & (s[2] & 077))];
4230 sv_catpvn(sv, hunk, 4);
4235 char r = (len > 1 ? s[1] : '\0');
4236 hunk[0] = PL_uuemap[(077 & (*s >> 2))];
4237 hunk[1] = PL_uuemap[(077 & (((*s << 4) & 060) | ((r >> 4) & 017)))];
4238 hunk[2] = PL_uuemap[(077 & ((r << 2) & 074))];
4239 hunk[3] = PL_uuemap[0];
4240 sv_catpvn(sv, hunk, 4);
4242 sv_catpvn(sv, "\n", 1);
4246 is_an_int(char *s, STRLEN l)
4249 SV *result = newSVpvn(s, l);
4250 char *result_c = SvPV(result, n_a); /* convenience */
4251 char *out = result_c;
4261 SvREFCNT_dec(result);
4284 SvREFCNT_dec(result);
4290 SvCUR_set(result, out - result_c);
4295 div128(SV *pnum, bool *done)
4296 /* must be '\0' terminated */
4300 char *s = SvPV(pnum, len);
4309 i = m * 10 + (*t - '0');
4311 r = (i >> 7); /* r < 10 */
4318 SvCUR_set(pnum, (STRLEN) (t - s));
4325 djSP; dMARK; dORIGMARK; dTARGET;
4326 register SV *cat = TARG;
4329 register char *pat = SvPVx(*++MARK, fromlen);
4330 register char *patend = pat + fromlen;
4335 static char null10[] = {0,0,0,0,0,0,0,0,0,0};
4336 static char *space10 = " ";
4338 /* These must not be in registers: */
4353 #ifdef PERL_NATINT_PACK
4354 int natint; /* native integer */
4359 sv_setpvn(cat, "", 0);
4360 while (pat < patend) {
4361 #define NEXTFROM (items-- > 0 ? *MARK++ : &PL_sv_no)
4362 datumtype = *pat++ & 0xFF;
4363 #ifdef PERL_NATINT_PACK
4366 if (isSPACE(datumtype))
4369 char *natstr = "sSiIlL";
4371 if (strchr(natstr, datumtype)) {
4372 #ifdef PERL_NATINT_PACK
4378 croak("'!' allowed only after types %s", natstr);
4381 len = strchr("@Xxu", datumtype) ? 0 : items;
4384 else if (isDIGIT(*pat)) {
4386 while (isDIGIT(*pat))
4387 len = (len * 10) + (*pat++ - '0');
4393 croak("Invalid type in pack: '%c'", (int)datumtype);
4394 case ',': /* grandfather in commas but with a warning */
4395 if (commas++ == 0 && ckWARN(WARN_UNSAFE))
4396 warner(WARN_UNSAFE, "Invalid type in pack: '%c'", (int)datumtype);
4399 DIE("%% may only be used in unpack");
4410 if (SvCUR(cat) < len)
4411 DIE("X outside of string");
4418 sv_catpvn(cat, null10, 10);
4421 sv_catpvn(cat, null10, len);
4427 aptr = SvPV(fromstr, fromlen);
4431 sv_catpvn(cat, aptr, len);
4433 sv_catpvn(cat, aptr, fromlen);
4435 if (datumtype == 'A') {
4437 sv_catpvn(cat, space10, 10);
4440 sv_catpvn(cat, space10, len);
4444 sv_catpvn(cat, null10, 10);
4447 sv_catpvn(cat, null10, len);
4454 char *savepat = pat;
4459 aptr = SvPV(fromstr, fromlen);
4464 SvCUR(cat) += (len+7)/8;
4465 SvGROW(cat, SvCUR(cat) + 1);
4466 aptr = SvPVX(cat) + aint;
4471 if (datumtype == 'B') {
4472 for (len = 0; len++ < aint;) {
4473 items |= *pat++ & 1;
4477 *aptr++ = items & 0xff;
4483 for (len = 0; len++ < aint;) {
4489 *aptr++ = items & 0xff;
4495 if (datumtype == 'B')
4496 items <<= 7 - (aint & 7);
4498 items >>= 7 - (aint & 7);
4499 *aptr++ = items & 0xff;
4501 pat = SvPVX(cat) + SvCUR(cat);
4512 char *savepat = pat;
4517 aptr = SvPV(fromstr, fromlen);
4522 SvCUR(cat) += (len+1)/2;
4523 SvGROW(cat, SvCUR(cat) + 1);
4524 aptr = SvPVX(cat) + aint;
4529 if (datumtype == 'H') {
4530 for (len = 0; len++ < aint;) {
4532 items |= ((*pat++ & 15) + 9) & 15;
4534 items |= *pat++ & 15;
4538 *aptr++ = items & 0xff;
4544 for (len = 0; len++ < aint;) {
4546 items |= (((*pat++ & 15) + 9) & 15) << 4;
4548 items |= (*pat++ & 15) << 4;
4552 *aptr++ = items & 0xff;
4558 *aptr++ = items & 0xff;
4559 pat = SvPVX(cat) + SvCUR(cat);
4571 aint = SvIV(fromstr);
4573 sv_catpvn(cat, &achar, sizeof(char));
4579 auint = SvUV(fromstr);
4580 SvGROW(cat, SvCUR(cat) + 10);
4581 SvCUR_set(cat, (char*)uv_to_utf8((U8*)SvEND(cat),auint)
4586 /* Float and double added by gnb@melba.bby.oz.au 22/11/89 */
4591 afloat = (float)SvNV(fromstr);
4592 sv_catpvn(cat, (char *)&afloat, sizeof (float));
4599 adouble = (double)SvNV(fromstr);
4600 sv_catpvn(cat, (char *)&adouble, sizeof (double));
4606 ashort = (I16)SvIV(fromstr);
4608 ashort = PerlSock_htons(ashort);
4610 CAT16(cat, &ashort);
4616 ashort = (I16)SvIV(fromstr);
4618 ashort = htovs(ashort);
4620 CAT16(cat, &ashort);
4624 #if SHORTSIZE != SIZE16
4626 unsigned short aushort;
4630 aushort = SvUV(fromstr);
4631 sv_catpvn(cat, (char *)&aushort, sizeof(unsigned short));
4641 aushort = (U16)SvUV(fromstr);
4642 CAT16(cat, &aushort);
4648 #if SHORTSIZE != SIZE16
4652 ashort = SvIV(fromstr);
4653 sv_catpvn(cat, (char *)&ashort, sizeof(short));
4661 ashort = (I16)SvIV(fromstr);
4662 CAT16(cat, &ashort);
4669 auint = SvUV(fromstr);
4670 sv_catpvn(cat, (char*)&auint, sizeof(unsigned int));
4676 adouble = floor(SvNV(fromstr));
4679 croak("Cannot compress negative numbers");
4685 #ifdef CXUX_BROKEN_CONSTANT_CONVERT
4686 adouble <= UV_MAX_cxux
4693 char buf[1 + sizeof(UV)];
4694 char *in = buf + sizeof(buf);
4695 UV auv = U_V(adouble);
4698 *--in = (auv & 0x7f) | 0x80;
4701 buf[sizeof(buf) - 1] &= 0x7f; /* clear continue bit */
4702 sv_catpvn(cat, in, (buf + sizeof(buf)) - in);
4704 else if (SvPOKp(fromstr)) { /* decimal string arithmetics */
4705 char *from, *result, *in;
4710 /* Copy string and check for compliance */
4711 from = SvPV(fromstr, len);
4712 if ((norm = is_an_int(from, len)) == NULL)
4713 croak("can compress only unsigned integer");
4715 New('w', result, len, char);
4719 *--in = div128(norm, &done) | 0x80;
4720 result[len - 1] &= 0x7F; /* clear continue bit */
4721 sv_catpvn(cat, in, (result + len) - in);
4723 SvREFCNT_dec(norm); /* free norm */
4725 else if (SvNOKp(fromstr)) {
4726 char buf[sizeof(double) * 2]; /* 8/7 <= 2 */
4727 char *in = buf + sizeof(buf);
4730 double next = floor(adouble / 128);
4731 *--in = (unsigned char)(adouble - (next * 128)) | 0x80;
4732 if (--in < buf) /* this cannot happen ;-) */
4733 croak ("Cannot compress integer");
4735 } while (adouble > 0);
4736 buf[sizeof(buf) - 1] &= 0x7f; /* clear continue bit */
4737 sv_catpvn(cat, in, (buf + sizeof(buf)) - in);
4740 croak("Cannot compress non integer");
4746 aint = SvIV(fromstr);
4747 sv_catpvn(cat, (char*)&aint, sizeof(int));
4753 aulong = SvUV(fromstr);
4755 aulong = PerlSock_htonl(aulong);
4757 CAT32(cat, &aulong);
4763 aulong = SvUV(fromstr);
4765 aulong = htovl(aulong);
4767 CAT32(cat, &aulong);
4771 #if LONGSIZE != SIZE32
4775 aulong = SvUV(fromstr);
4776 sv_catpvn(cat, (char *)&aulong, sizeof(unsigned long));
4784 aulong = SvUV(fromstr);
4785 CAT32(cat, &aulong);
4790 #if LONGSIZE != SIZE32
4794 along = SvIV(fromstr);
4795 sv_catpvn(cat, (char *)&along, sizeof(long));
4803 along = SvIV(fromstr);
4812 auquad = (Uquad_t)SvIV(fromstr);
4813 sv_catpvn(cat, (char*)&auquad, sizeof(Uquad_t));
4819 aquad = (Quad_t)SvIV(fromstr);
4820 sv_catpvn(cat, (char*)&aquad, sizeof(Quad_t));
4823 #endif /* HAS_QUAD */
4825 len = 1; /* assume SV is correct length */
4830 if (fromstr == &PL_sv_undef)
4834 /* XXX better yet, could spirit away the string to
4835 * a safe spot and hang on to it until the result
4836 * of pack() (and all copies of the result) are
4839 if (ckWARN(WARN_UNSAFE) && (SvTEMP(fromstr) || SvPADTMP(fromstr)))
4841 "Attempt to pack pointer to temporary value");
4842 if (SvPOK(fromstr) || SvNIOK(fromstr))
4843 aptr = SvPV(fromstr,n_a);
4845 aptr = SvPV_force(fromstr,n_a);
4847 sv_catpvn(cat, (char*)&aptr, sizeof(char*));
4852 aptr = SvPV(fromstr, fromlen);
4853 SvGROW(cat, fromlen * 4 / 3);
4858 while (fromlen > 0) {
4865 doencodes(cat, aptr, todo);
4884 register I32 limit = POPi; /* note, negative is forever */
4887 register char *s = SvPV(sv, len);
4888 char *strend = s + len;
4890 register REGEXP *rx;
4894 I32 maxiters = (strend - s) + 10;
4897 I32 origlimit = limit;
4900 AV *oldstack = PL_curstack;
4901 I32 gimme = GIMME_V;
4902 I32 oldsave = PL_savestack_ix;
4903 I32 make_mortal = 1;
4904 MAGIC *mg = (MAGIC *) NULL;
4907 Copy(&LvTARGOFF(POPs), &pm, 1, PMOP*);
4912 DIE("panic: do_split");
4913 rx = pm->op_pmregexp;
4915 TAINT_IF((pm->op_pmflags & PMf_LOCALE) &&
4916 (pm->op_pmflags & (PMf_WHITE | PMf_SKIPWHITE)));
4918 if (pm->op_pmreplroot)
4919 ary = GvAVn((GV*)pm->op_pmreplroot);
4920 else if (gimme != G_ARRAY)
4922 ary = (AV*)PL_curpad[0];
4924 ary = GvAVn(PL_defgv);
4925 #endif /* USE_THREADS */
4928 if (ary && (gimme != G_ARRAY || (pm->op_pmflags & PMf_ONCE))) {
4934 if (mg = SvTIED_mg((SV*)ary, 'P')) {
4936 XPUSHs(SvTIED_obj((SV*)ary, mg));
4941 for (i = AvFILLp(ary); i >= 0; i--)
4942 AvARRAY(ary)[i] = &PL_sv_undef; /* don't free mere refs */
4944 /* temporarily switch stacks */
4945 SWITCHSTACK(PL_curstack, ary);
4949 base = SP - PL_stack_base;
4951 if (pm->op_pmflags & PMf_SKIPWHITE) {
4952 if (pm->op_pmflags & PMf_LOCALE) {
4953 while (isSPACE_LC(*s))
4961 if (pm->op_pmflags & (PMf_MULTILINE|PMf_SINGLELINE)) {
4962 SAVEINT(PL_multiline);
4963 PL_multiline = pm->op_pmflags & PMf_MULTILINE;
4967 limit = maxiters + 2;
4968 if (pm->op_pmflags & PMf_WHITE) {
4971 while (m < strend &&
4972 !((pm->op_pmflags & PMf_LOCALE)
4973 ? isSPACE_LC(*m) : isSPACE(*m)))
4978 dstr = NEWSV(30, m-s);
4979 sv_setpvn(dstr, s, m-s);
4985 while (s < strend &&
4986 ((pm->op_pmflags & PMf_LOCALE)
4987 ? isSPACE_LC(*s) : isSPACE(*s)))
4991 else if (strEQ("^", rx->precomp)) {
4994 for (m = s; m < strend && *m != '\n'; m++) ;
4998 dstr = NEWSV(30, m-s);
4999 sv_setpvn(dstr, s, m-s);
5006 else if (rx->check_substr && !rx->nparens
5007 && (rx->reganch & ROPT_CHECK_ALL)
5008 && !(rx->reganch & ROPT_ANCH)) {
5009 int tail = SvTAIL(rx->check_substr) != 0;
5011 i = SvCUR(rx->check_substr);
5012 if (i == 1 && !tail) {
5013 i = *SvPVX(rx->check_substr);
5016 for (m = s; m < strend && *m != i; m++) ;
5019 dstr = NEWSV(30, m-s);
5020 sv_setpvn(dstr, s, m-s);
5029 while (s < strend && --limit &&
5030 (m=fbm_instr((unsigned char*)s, (unsigned char*)strend,
5031 rx->check_substr, PL_multiline ? FBMrf_MULTILINE : 0)) )
5034 dstr = NEWSV(31, m-s);
5035 sv_setpvn(dstr, s, m-s);
5039 s = m + i - tail; /* Fake \n at the end */
5044 maxiters += (strend - s) * rx->nparens;
5045 while (s < strend && --limit &&
5046 CALLREGEXEC(rx, s, strend, orig, 1, sv, NULL, 0))
5048 TAINT_IF(RX_MATCH_TAINTED(rx));
5049 if (RX_MATCH_COPIED(rx) && rx->subbeg != orig) {
5054 strend = s + (strend - m);
5056 m = rx->startp[0] + orig;
5057 dstr = NEWSV(32, m-s);
5058 sv_setpvn(dstr, s, m-s);
5063 for (i = 1; i <= rx->nparens; i++) {
5064 s = rx->startp[i] + orig;
5065 m = rx->endp[i] + orig;
5067 dstr = NEWSV(33, m-s);
5068 sv_setpvn(dstr, s, m-s);
5071 dstr = NEWSV(33, 0);
5077 s = rx->endp[0] + orig;
5081 LEAVE_SCOPE(oldsave);
5082 iters = (SP - PL_stack_base) - base;
5083 if (iters > maxiters)
5086 /* keep field after final delim? */
5087 if (s < strend || (iters && origlimit)) {
5088 dstr = NEWSV(34, strend-s);
5089 sv_setpvn(dstr, s, strend-s);
5095 else if (!origlimit) {
5096 while (iters > 0 && (!TOPs || !SvANY(TOPs) || SvCUR(TOPs) == 0))
5102 SWITCHSTACK(ary, oldstack);
5103 if (SvSMAGICAL(ary)) {
5108 if (gimme == G_ARRAY) {
5110 Copy(AvARRAY(ary), SP + 1, iters, SV*);
5118 perl_call_method("PUSH",G_SCALAR|G_DISCARD);
5121 if (gimme == G_ARRAY) {
5122 /* EXTEND should not be needed - we just popped them */
5124 for (i=0; i < iters; i++) {
5125 SV **svp = av_fetch(ary, i, FALSE);
5126 PUSHs((svp) ? *svp : &PL_sv_undef);
5133 if (gimme == G_ARRAY)
5136 if (iters || !pm->op_pmreplroot) {
5146 unlock_condpair(void *svv)
5149 MAGIC *mg = mg_find((SV*)svv, 'm');
5152 croak("panic: unlock_condpair unlocking non-mutex");
5153 MUTEX_LOCK(MgMUTEXP(mg));
5154 if (MgOWNER(mg) != thr)
5155 croak("panic: unlock_condpair unlocking mutex that we don't own");
5157 COND_SIGNAL(MgOWNERCONDP(mg));
5158 DEBUG_S(PerlIO_printf(PerlIO_stderr(), "0x%lx: unlock 0x%lx\n",
5159 (unsigned long)thr, (unsigned long)svv);)
5160 MUTEX_UNLOCK(MgMUTEXP(mg));
5162 #endif /* USE_THREADS */
5175 mg = condpair_magic(sv);
5176 MUTEX_LOCK(MgMUTEXP(mg));
5177 if (MgOWNER(mg) == thr)
5178 MUTEX_UNLOCK(MgMUTEXP(mg));
5181 COND_WAIT(MgOWNERCONDP(mg), MgMUTEXP(mg));
5183 DEBUG_S(PerlIO_printf(PerlIO_stderr(), "0x%lx: pp_lock lock 0x%lx\n",
5184 (unsigned long)thr, (unsigned long)sv);)
5185 MUTEX_UNLOCK(MgMUTEXP(mg));
5186 save_destructor(unlock_condpair, sv);
5188 #endif /* USE_THREADS */
5189 if (SvTYPE(retsv) == SVt_PVAV || SvTYPE(retsv) == SVt_PVHV
5190 || SvTYPE(retsv) == SVt_PVCV) {
5191 retsv = refto(retsv);
5202 if (PL_op->op_private & OPpLVAL_INTRO)
5203 PUSHs(*save_threadsv(PL_op->op_targ));
5205 PUSHs(THREADSV(PL_op->op_targ));
5208 DIE("tried to access per-thread data in non-threaded perl");
5209 #endif /* USE_THREADS */