3 * Copyright (c) 1991-1997, Larry Wall
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
11 * "It's a big house this, and very peculiar. Always a bit more to discover,
12 * and no knowing what you'll find around a corner. And Elves, sir!" --Samwise
19 * The compiler on Concurrent CX/UX systems has a subtle bug which only
20 * seems to show up when compiling pp.c - it generates the wrong double
21 * precision constant value for (double)UV_MAX when used inline in the body
22 * of the code below, so this makes a static variable up front (which the
23 * compiler seems to get correct) and uses it in place of UV_MAX below.
25 #ifdef CXUX_BROKEN_CONSTANT_CONVERT
26 static double UV_MAX_cxux = ((double)UV_MAX);
30 * Types used in bitwise operations.
32 * Normally we'd just use IV and UV. However, some hardware and
33 * software combinations (e.g. Alpha and current OSF/1) don't have a
34 * floating-point type to use for NV that has adequate bits to fully
35 * hold an IV/UV. (In other words, sizeof(long) == sizeof(double).)
37 * It just so happens that "int" is the right size almost everywhere.
43 * Mask used after bitwise operations.
45 * There is at least one realm (Cray word machines) that doesn't
46 * have an integral type (except char) small enough to be represented
47 * in a double without loss; that is, it has no 32-bit type.
49 #if 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 #if BYTEORDER > 0xFFFF && defined(_CRAY) && !defined(_CRAYMPP)
82 # if BYTEORDER == 0x12345678
83 # define OFF16(p) (char*)(p)
84 # define OFF32(p) (char*)(p)
86 # if BYTEORDER == 0x87654321
87 # define OFF16(p) ((char*)(p) + (sizeof(U16) - SIZE16))
88 # define OFF32(p) ((char*)(p) + (sizeof(U32) - SIZE32))
90 }}}} bad cray byte order
93 # define COPY16(s,p) (*(p) = 0, Copy(s, OFF16(p), SIZE16, char))
94 # define COPY32(s,p) (*(p) = 0, Copy(s, OFF32(p), SIZE32, char))
95 # define CAT16(sv,p) sv_catpvn(sv, OFF16(p), SIZE16)
96 # define CAT32(sv,p) sv_catpvn(sv, OFF32(p), SIZE32)
98 # define COPY16(s,p) Copy(s, p, SIZE16, char)
99 # define COPY32(s,p) Copy(s, p, SIZE32, char)
100 # define CAT16(sv,p) sv_catpvn(sv, (char*)(p), SIZE16)
101 # define CAT32(sv,p) sv_catpvn(sv, (char*)(p), SIZE32)
105 static void doencodes _((SV* sv, char* s, I32 len));
106 static SV* refto _((SV* sv));
107 static U32 seed _((void));
110 /* variations on pp_null */
116 /* XXX I can't imagine anyone who doesn't have this actually _needs_
117 it, since pid_t is an integral type.
120 #ifdef NEED_GETPID_PROTO
121 extern Pid_t getpid (void);
127 if (GIMME_V == G_SCALAR)
128 XPUSHs(&PL_sv_undef);
142 if (PL_op->op_private & OPpLVAL_INTRO)
143 SAVECLEARSV(PL_curpad[PL_op->op_targ]);
145 if (PL_op->op_flags & OPf_REF) {
149 if (GIMME == G_ARRAY) {
150 I32 maxarg = AvFILL((AV*)TARG) + 1;
152 if (SvMAGICAL(TARG)) {
154 for (i=0; i < maxarg; i++) {
155 SV **svp = av_fetch((AV*)TARG, i, FALSE);
156 SP[i+1] = (svp) ? *svp : &PL_sv_undef;
160 Copy(AvARRAY((AV*)TARG), SP+1, maxarg, SV*);
165 SV* sv = sv_newmortal();
166 I32 maxarg = AvFILL((AV*)TARG) + 1;
167 sv_setiv(sv, maxarg);
179 if (PL_op->op_private & OPpLVAL_INTRO)
180 SAVECLEARSV(PL_curpad[PL_op->op_targ]);
181 if (PL_op->op_flags & OPf_REF)
184 if (gimme == G_ARRAY) {
185 RETURNOP(do_kv(ARGS));
187 else if (gimme == G_SCALAR) {
188 SV* sv = sv_newmortal();
189 if (HvFILL((HV*)TARG))
190 sv_setpvf(sv, "%ld/%ld",
191 (long)HvFILL((HV*)TARG), (long)HvMAX((HV*)TARG) + 1);
201 DIE("NOT IMPL LINE %d",__LINE__);
212 tryAMAGICunDEREF(to_gv);
215 if (SvTYPE(sv) == SVt_PVIO) {
216 GV *gv = (GV*) sv_newmortal();
217 gv_init(gv, 0, "", 0, 0);
218 GvIOp(gv) = (IO *)sv;
219 (void)SvREFCNT_inc(sv);
221 } else if (SvTYPE(sv) != SVt_PVGV)
222 DIE("Not a GLOB reference");
225 if (SvTYPE(sv) != SVt_PVGV) {
229 if (SvGMAGICAL(sv)) {
235 if (PL_op->op_flags & OPf_REF ||
236 PL_op->op_private & HINT_STRICT_REFS)
237 DIE(PL_no_usym, "a symbol");
238 if (ckWARN(WARN_UNINITIALIZED))
239 warner(WARN_UNINITIALIZED, PL_warn_uninit);
243 if ((PL_op->op_flags & OPf_SPECIAL) &&
244 !(PL_op->op_flags & OPf_MOD))
246 sv = (SV*)gv_fetchpv(sym, FALSE, SVt_PVGV);
251 if (PL_op->op_private & HINT_STRICT_REFS)
252 DIE(PL_no_symref, sym, "a symbol");
253 sv = (SV*)gv_fetchpv(sym, TRUE, SVt_PVGV);
257 if (PL_op->op_private & OPpLVAL_INTRO)
258 save_gp((GV*)sv, !(PL_op->op_flags & OPf_SPECIAL));
269 tryAMAGICunDEREF(to_sv);
272 switch (SvTYPE(sv)) {
276 DIE("Not a SCALAR reference");
284 if (SvTYPE(gv) != SVt_PVGV) {
285 if (SvGMAGICAL(sv)) {
291 if (PL_op->op_flags & OPf_REF ||
292 PL_op->op_private & HINT_STRICT_REFS)
293 DIE(PL_no_usym, "a SCALAR");
294 if (ckWARN(WARN_UNINITIALIZED))
295 warner(WARN_UNINITIALIZED, PL_warn_uninit);
299 if ((PL_op->op_flags & OPf_SPECIAL) &&
300 !(PL_op->op_flags & OPf_MOD))
302 gv = (GV*)gv_fetchpv(sym, FALSE, SVt_PV);
307 if (PL_op->op_private & HINT_STRICT_REFS)
308 DIE(PL_no_symref, sym, "a SCALAR");
309 gv = (GV*)gv_fetchpv(sym, TRUE, SVt_PV);
314 if (PL_op->op_flags & OPf_MOD) {
315 if (PL_op->op_private & OPpLVAL_INTRO)
316 sv = save_scalar((GV*)TOPs);
317 else if (PL_op->op_private & OPpDEREF)
318 vivify_ref(sv, PL_op->op_private & OPpDEREF);
328 SV *sv = AvARYLEN(av);
330 AvARYLEN(av) = sv = NEWSV(0,0);
331 sv_upgrade(sv, SVt_IV);
332 sv_magic(sv, (SV*)av, '#', Nullch, 0);
340 djSP; dTARGET; dPOPss;
342 if (PL_op->op_flags & OPf_MOD) {
343 if (SvTYPE(TARG) < SVt_PVLV) {
344 sv_upgrade(TARG, SVt_PVLV);
345 sv_magic(TARG, Nullsv, '.', Nullch, 0);
349 if (LvTARG(TARG) != sv) {
351 SvREFCNT_dec(LvTARG(TARG));
352 LvTARG(TARG) = SvREFCNT_inc(sv);
354 PUSHs(TARG); /* no SvSETMAGIC */
360 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
361 mg = mg_find(sv, 'g');
362 if (mg && mg->mg_len >= 0) {
366 PUSHi(i + PL_curcop->cop_arybase);
380 /* We usually try to add a non-existent subroutine in case of AUTOLOAD. */
381 /* (But not in defined().) */
382 CV *cv = sv_2cv(TOPs, &stash, &gv, !(PL_op->op_flags & OPf_SPECIAL));
385 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
388 cv = (CV*)&PL_sv_undef;
402 if (SvPOK(TOPs) && SvCUR(TOPs) >= 7) {
403 char *s = SvPVX(TOPs);
404 if (strnEQ(s, "CORE::", 6)) {
407 code = keyword(s + 6, SvCUR(TOPs) - 6);
408 if (code < 0) { /* Overridable. */
409 #define MAX_ARGS_OP ((sizeof(I32) - 1) * 2)
410 int i = 0, n = 0, seen_question = 0;
412 char str[ MAX_ARGS_OP * 2 + 2 ]; /* One ';', one '\0' */
414 while (i < MAXO) { /* The slow way. */
415 if (strEQ(s + 6, PL_op_name[i])
416 || strEQ(s + 6, PL_op_desc[i]))
422 goto nonesuch; /* Should not happen... */
424 oa = PL_opargs[i] >> OASHIFT;
426 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(newSVpv(str, n - 1));
441 } else if (code) /* Non-Overridable */
443 else { /* None such */
445 croak("Cannot find an opnumber for \"%s\"", s+6);
449 cv = sv_2cv(TOPs, &stash, &gv, FALSE);
451 ret = sv_2mortal(newSVpv(SvPVX(cv), SvCUR(cv)));
460 CV* cv = (CV*)PL_curpad[PL_op->op_targ];
462 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
478 if (GIMME != G_ARRAY) {
482 *MARK = &PL_sv_undef;
483 *MARK = refto(*MARK);
487 EXTEND_MORTAL(SP - MARK);
489 *MARK = refto(*MARK);
498 if (SvTYPE(sv) == SVt_PVLV && LvTYPE(sv) == 'y') {
501 if (!(sv = LvTARG(sv)))
504 else if (SvPADTMP(sv))
508 (void)SvREFCNT_inc(sv);
511 sv_upgrade(rv, SVt_RV);
525 if (sv && SvGMAGICAL(sv))
528 if (!sv || !SvROK(sv))
532 pv = sv_reftype(sv,TRUE);
533 PUSHp(pv, strlen(pv));
543 stash = PL_curcop->cop_stash;
547 char *ptr = SvPV(ssv,len);
548 if (ckWARN(WARN_UNSAFE) && len == 0)
550 "Explicit blessing to '' (assuming package main)");
551 stash = gv_stashpvn(ptr, len, TRUE);
554 (void)sv_bless(TOPs, stash);
568 elem = SvPV(sv, n_a);
572 switch (elem ? *elem : '\0')
575 if (strEQ(elem, "ARRAY"))
576 tmpRef = (SV*)GvAV(gv);
579 if (strEQ(elem, "CODE"))
580 tmpRef = (SV*)GvCVu(gv);
583 if (strEQ(elem, "FILEHANDLE")) /* XXX deprecate in 5.005 */
584 tmpRef = (SV*)GvIOp(gv);
587 if (strEQ(elem, "GLOB"))
591 if (strEQ(elem, "HASH"))
592 tmpRef = (SV*)GvHV(gv);
595 if (strEQ(elem, "IO"))
596 tmpRef = (SV*)GvIOp(gv);
599 if (strEQ(elem, "NAME"))
600 sv = newSVpv(GvNAME(gv), GvNAMELEN(gv));
603 if (strEQ(elem, "PACKAGE"))
604 sv = newSVpv(HvNAME(GvSTASH(gv)), 0);
607 if (strEQ(elem, "SCALAR"))
621 /* Pattern matching */
626 register UNOP *unop = cUNOP;
627 register unsigned char *s;
630 register I32 *sfirst;
634 if (sv == PL_lastscream) {
640 SvSCREAM_off(PL_lastscream);
641 SvREFCNT_dec(PL_lastscream);
643 PL_lastscream = SvREFCNT_inc(sv);
646 s = (unsigned char*)(SvPV(sv, len));
650 if (pos > PL_maxscream) {
651 if (PL_maxscream < 0) {
652 PL_maxscream = pos + 80;
653 New(301, PL_screamfirst, 256, I32);
654 New(302, PL_screamnext, PL_maxscream, I32);
657 PL_maxscream = pos + pos / 4;
658 Renew(PL_screamnext, PL_maxscream, I32);
662 sfirst = PL_screamfirst;
663 snext = PL_screamnext;
665 if (!sfirst || !snext)
666 DIE("do_study: out of memory");
668 for (ch = 256; ch; --ch)
675 snext[pos] = sfirst[ch] - pos;
682 sv_magic(sv, Nullsv, 'g', Nullch, 0); /* piggyback on m//g magic */
691 if (PL_op->op_flags & OPf_STACKED)
697 TARG = sv_newmortal();
702 /* Lvalue operators. */
714 djSP; dMARK; dTARGET;
724 SETi(do_chomp(TOPs));
730 djSP; dMARK; dTARGET;
731 register I32 count = 0;
734 count += do_chomp(POPs);
745 if (!sv || !SvANY(sv))
747 switch (SvTYPE(sv)) {
749 if (AvMAX(sv) >= 0 || SvGMAGICAL(sv) || (SvRMAGICAL(sv) && mg_find(sv,'P')))
753 if (HvARRAY(sv) || SvGMAGICAL(sv) || (SvRMAGICAL(sv) && mg_find(sv,'P')))
757 if (CvROOT(sv) || CvXSUB(sv))
774 if (!PL_op->op_private) {
783 if (SvTHINKFIRST(sv)) {
784 if (SvREADONLY(sv)) {
786 if (PL_curcop != &PL_compiling)
793 switch (SvTYPE(sv)) {
803 if (ckWARN(WARN_UNSAFE) && cv_const_sv((CV*)sv))
804 warner(WARN_UNSAFE, "Constant subroutine %s undefined",
805 CvANON((CV*)sv) ? "(anonymous)" : GvENAME(CvGV((CV*)sv)));
808 { GV* gv = (GV*)SvREFCNT_inc(CvGV((CV*)sv));
810 CvGV((CV*)sv) = gv; } /* let user-undef'd sub keep its identity */
814 SvSetMagicSV(sv, &PL_sv_undef);
818 Newz(602, gp, 1, GP);
819 GvGP(sv) = gp_ref(gp);
820 GvSV(sv) = NEWSV(72,0);
821 GvLINE(sv) = PL_curcop->cop_line;
827 if (SvTYPE(sv) >= SVt_PV && SvPVX(sv) && SvLEN(sv)) {
830 SvPV_set(sv, Nullch);
843 if (SvREADONLY(TOPs) || SvTYPE(TOPs) > SVt_PVLV)
845 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) &&
846 SvIVX(TOPs) != IV_MIN)
849 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
860 if (SvREADONLY(TOPs) || SvTYPE(TOPs) > SVt_PVLV)
862 sv_setsv(TARG, TOPs);
863 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) &&
864 SvIVX(TOPs) != IV_MAX)
867 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
881 if(SvREADONLY(TOPs) || SvTYPE(TOPs) > SVt_PVLV)
883 sv_setsv(TARG, TOPs);
884 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) &&
885 SvIVX(TOPs) != IV_MIN)
888 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
897 /* Ordinary operators. */
901 djSP; dATARGET; tryAMAGICbin(pow,opASSIGN);
904 SETn( pow( left, right) );
911 djSP; dATARGET; tryAMAGICbin(mult,opASSIGN);
914 SETn( left * right );
921 djSP; dATARGET; tryAMAGICbin(div,opASSIGN);
926 DIE("Illegal division by zero");
928 /* insure that 20./5. == 4. */
931 if ((double)I_V(left) == left &&
932 (double)I_V(right) == right &&
933 (k = I_V(left)/I_V(right))*I_V(right) == I_V(left)) {
936 value = left / right;
940 value = left / right;
949 djSP; dATARGET; tryAMAGICbin(modulo,opASSIGN);
957 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)) {
959 right = (right_neg = (i < 0)) ? -i : i;
963 right = U_V((right_neg = (n < 0)) ? -n : n);
966 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)) {
968 left = (left_neg = (i < 0)) ? -i : i;
972 left = U_V((left_neg = (n < 0)) ? -n : n);
976 DIE("Illegal modulus zero");
979 if ((left_neg != right_neg) && ans)
982 /* XXX may warn: unary minus operator applied to unsigned type */
983 /* could change -foo to be (~foo)+1 instead */
984 if (ans <= ~((UV)IV_MAX)+1)
985 sv_setiv(TARG, ~ans+1);
987 sv_setnv(TARG, -(double)ans);
998 djSP; dATARGET; tryAMAGICbin(repeat,opASSIGN);
1000 register I32 count = POPi;
1001 if (GIMME == G_ARRAY && PL_op->op_private & OPpREPEAT_DOLIST) {
1003 I32 items = SP - MARK;
1006 max = items * count;
1015 repeatcpy((char*)(MARK + items), (char*)MARK,
1016 items * sizeof(SV*), count - 1);
1019 else if (count <= 0)
1022 else { /* Note: mark already snarfed by pp_list */
1027 if (TARG == tmpstr && SvTHINKFIRST(tmpstr)) {
1028 if (SvREADONLY(tmpstr) && PL_curcop != &PL_compiling)
1029 DIE("Can't x= to readonly value");
1033 SvSetSV(TARG, tmpstr);
1034 SvPV_force(TARG, len);
1039 SvGROW(TARG, (count * len) + 1);
1040 repeatcpy(SvPVX(TARG) + len, SvPVX(TARG), len, count - 1);
1041 SvCUR(TARG) *= count;
1043 *SvEND(TARG) = '\0';
1045 (void)SvPOK_only(TARG);
1054 djSP; dATARGET; tryAMAGICbin(subtr,opASSIGN);
1057 SETn( left - right );
1064 djSP; dATARGET; tryAMAGICbin(lshift,opASSIGN);
1067 if (PL_op->op_private & HINT_INTEGER) {
1069 i = BWi(i) << shift;
1083 djSP; dATARGET; tryAMAGICbin(rshift,opASSIGN);
1086 if (PL_op->op_private & HINT_INTEGER) {
1088 i = BWi(i) >> shift;
1102 djSP; tryAMAGICbinSET(lt,0);
1105 SETs(boolSV(TOPn < value));
1112 djSP; tryAMAGICbinSET(gt,0);
1115 SETs(boolSV(TOPn > value));
1122 djSP; tryAMAGICbinSET(le,0);
1125 SETs(boolSV(TOPn <= value));
1132 djSP; tryAMAGICbinSET(ge,0);
1135 SETs(boolSV(TOPn >= value));
1142 djSP; tryAMAGICbinSET(ne,0);
1145 SETs(boolSV(TOPn != value));
1152 djSP; dTARGET; tryAMAGICbin(ncmp,0);
1159 else if (left < right)
1161 else if (left > right)
1174 djSP; tryAMAGICbinSET(slt,0);
1177 int cmp = ((PL_op->op_private & OPpLOCALE)
1178 ? sv_cmp_locale(left, right)
1179 : sv_cmp(left, right));
1180 SETs(boolSV(cmp < 0));
1187 djSP; tryAMAGICbinSET(sgt,0);
1190 int cmp = ((PL_op->op_private & OPpLOCALE)
1191 ? sv_cmp_locale(left, right)
1192 : sv_cmp(left, right));
1193 SETs(boolSV(cmp > 0));
1200 djSP; tryAMAGICbinSET(sle,0);
1203 int cmp = ((PL_op->op_private & OPpLOCALE)
1204 ? sv_cmp_locale(left, right)
1205 : sv_cmp(left, right));
1206 SETs(boolSV(cmp <= 0));
1213 djSP; tryAMAGICbinSET(sge,0);
1216 int cmp = ((PL_op->op_private & OPpLOCALE)
1217 ? sv_cmp_locale(left, right)
1218 : sv_cmp(left, right));
1219 SETs(boolSV(cmp >= 0));
1226 djSP; tryAMAGICbinSET(seq,0);
1229 SETs(boolSV(sv_eq(left, right)));
1236 djSP; tryAMAGICbinSET(sne,0);
1239 SETs(boolSV(!sv_eq(left, right)));
1246 djSP; dTARGET; tryAMAGICbin(scmp,0);
1249 int cmp = ((PL_op->op_private & OPpLOCALE)
1250 ? sv_cmp_locale(left, right)
1251 : sv_cmp(left, right));
1259 djSP; dATARGET; tryAMAGICbin(band,opASSIGN);
1262 if (SvNIOKp(left) || SvNIOKp(right)) {
1263 if (PL_op->op_private & HINT_INTEGER) {
1264 IBW value = SvIV(left) & SvIV(right);
1268 UBW value = SvUV(left) & SvUV(right);
1273 do_vop(PL_op->op_type, TARG, left, right);
1282 djSP; dATARGET; tryAMAGICbin(bxor,opASSIGN);
1285 if (SvNIOKp(left) || SvNIOKp(right)) {
1286 if (PL_op->op_private & HINT_INTEGER) {
1287 IBW value = (USE_LEFT(left) ? SvIV(left) : 0) ^ SvIV(right);
1291 UBW value = (USE_LEFT(left) ? SvUV(left) : 0) ^ SvUV(right);
1296 do_vop(PL_op->op_type, TARG, left, right);
1305 djSP; dATARGET; tryAMAGICbin(bor,opASSIGN);
1308 if (SvNIOKp(left) || SvNIOKp(right)) {
1309 if (PL_op->op_private & HINT_INTEGER) {
1310 IBW value = (USE_LEFT(left) ? SvIV(left) : 0) | SvIV(right);
1314 UBW value = (USE_LEFT(left) ? SvUV(left) : 0) | SvUV(right);
1319 do_vop(PL_op->op_type, TARG, left, right);
1328 djSP; dTARGET; tryAMAGICun(neg);
1333 if (SvIOKp(sv) && !SvNOKp(sv) && !SvPOKp(sv) && SvIVX(sv) != IV_MIN)
1335 else if (SvNIOKp(sv))
1337 else if (SvPOKp(sv)) {
1339 char *s = SvPV(sv, len);
1340 if (isIDFIRST(*s)) {
1341 sv_setpvn(TARG, "-", 1);
1344 else if (*s == '+' || *s == '-') {
1346 *SvPV_force(TARG, len) = *s == '-' ? '+' : '-';
1348 else if (IN_UTF8 && *(U8*)s >= 0xc0 && isIDFIRST_utf8((U8*)s)) {
1349 sv_setpvn(TARG, "-", 1);
1353 sv_setnv(TARG, -SvNV(sv));
1364 djSP; tryAMAGICunSET(not);
1365 *PL_stack_sp = boolSV(!SvTRUE(*PL_stack_sp));
1371 djSP; dTARGET; tryAMAGICun(compl);
1375 if (PL_op->op_private & HINT_INTEGER) {
1376 IBW value = ~SvIV(sv);
1380 UBW value = ~SvUV(sv);
1385 register char *tmps;
1386 register long *tmpl;
1391 tmps = SvPV_force(TARG, len);
1394 for ( ; anum && (unsigned long)tmps % sizeof(long); anum--, tmps++)
1397 for ( ; anum >= sizeof(long); anum -= sizeof(long), tmpl++)
1401 for ( ; anum > 0; anum--, tmps++)
1410 /* integer versions of some of the above */
1414 djSP; dATARGET; tryAMAGICbin(mult,opASSIGN);
1417 SETi( left * right );
1424 djSP; dATARGET; tryAMAGICbin(div,opASSIGN);
1428 DIE("Illegal division by zero");
1429 value = POPi / value;
1437 djSP; dATARGET; tryAMAGICbin(modulo,opASSIGN);
1441 DIE("Illegal modulus zero");
1442 SETi( left % right );
1449 djSP; dATARGET; tryAMAGICbin(add,opASSIGN);
1452 SETi( left + right );
1459 djSP; dATARGET; tryAMAGICbin(subtr,opASSIGN);
1462 SETi( left - right );
1469 djSP; tryAMAGICbinSET(lt,0);
1472 SETs(boolSV(left < right));
1479 djSP; tryAMAGICbinSET(gt,0);
1482 SETs(boolSV(left > right));
1489 djSP; tryAMAGICbinSET(le,0);
1492 SETs(boolSV(left <= right));
1499 djSP; tryAMAGICbinSET(ge,0);
1502 SETs(boolSV(left >= right));
1509 djSP; tryAMAGICbinSET(eq,0);
1512 SETs(boolSV(left == right));
1519 djSP; tryAMAGICbinSET(ne,0);
1522 SETs(boolSV(left != right));
1529 djSP; dTARGET; tryAMAGICbin(ncmp,0);
1536 else if (left < right)
1547 djSP; dTARGET; tryAMAGICun(neg);
1552 /* High falutin' math. */
1556 djSP; dTARGET; tryAMAGICbin(atan2,0);
1559 SETn(atan2(left, right));
1566 djSP; dTARGET; tryAMAGICun(sin);
1578 djSP; dTARGET; tryAMAGICun(cos);
1588 /* Support Configure command-line overrides for rand() functions.
1589 After 5.005, perhaps we should replace this by Configure support
1590 for drand48(), random(), or rand(). For 5.005, though, maintain
1591 compatibility by calling rand() but allow the user to override it.
1592 See INSTALL for details. --Andy Dougherty 15 July 1998
1594 /* Now it's after 5.005, and Configure supports drand48() and random(),
1595 in addition to rand(). So the overrides should not be needed any more.
1596 --Jarkko Hietaniemi 27 September 1998
1599 #ifndef HAS_DRAND48_PROTO
1600 extern double drand48 _((void));
1613 if (!PL_srand_called) {
1614 (void)seedDrand01((Rand_seed_t)seed());
1615 PL_srand_called = TRUE;
1630 (void)seedDrand01((Rand_seed_t)anum);
1631 PL_srand_called = TRUE;
1640 * This is really just a quick hack which grabs various garbage
1641 * values. It really should be a real hash algorithm which
1642 * spreads the effect of every input bit onto every output bit,
1643 * if someone who knows about such things would bother to write it.
1644 * Might be a good idea to add that function to CORE as well.
1645 * No numbers below come from careful analysis or anything here,
1646 * except they are primes and SEED_C1 > 1E6 to get a full-width
1647 * value from (tv_sec * SEED_C1 + tv_usec). The multipliers should
1648 * probably be bigger too.
1651 # define SEED_C1 1000003
1652 #define SEED_C4 73819
1654 # define SEED_C1 25747
1655 #define SEED_C4 20639
1659 #define SEED_C5 26107
1662 #ifndef PERL_NO_DEV_RANDOM
1667 # include <starlet.h>
1668 /* when[] = (low 32 bits, high 32 bits) of time since epoch
1669 * in 100-ns units, typically incremented ever 10 ms. */
1670 unsigned int when[2];
1672 # ifdef HAS_GETTIMEOFDAY
1673 struct timeval when;
1679 /* This test is an escape hatch, this symbol isn't set by Configure. */
1680 #ifndef PERL_NO_DEV_RANDOM
1681 #ifndef PERL_RANDOM_DEVICE
1682 /* /dev/random isn't used by default because reads from it will block
1683 * if there isn't enough entropy available. You can compile with
1684 * PERL_RANDOM_DEVICE to it if you'd prefer Perl to block until there
1685 * is enough real entropy to fill the seed. */
1686 # define PERL_RANDOM_DEVICE "/dev/urandom"
1688 fd = PerlLIO_open(PERL_RANDOM_DEVICE, 0);
1690 if (PerlLIO_read(fd, &u, sizeof u) != sizeof u)
1699 _ckvmssts(sys$gettim(when));
1700 u = (U32)SEED_C1 * when[0] + (U32)SEED_C2 * when[1];
1702 # ifdef HAS_GETTIMEOFDAY
1703 gettimeofday(&when,(struct timezone *) 0);
1704 u = (U32)SEED_C1 * when.tv_sec + (U32)SEED_C2 * when.tv_usec;
1707 u = (U32)SEED_C1 * when;
1710 u += SEED_C3 * (U32)getpid();
1711 u += SEED_C4 * (U32)(UV)PL_stack_sp;
1712 #ifndef PLAN9 /* XXX Plan9 assembler chokes on this; fix needed */
1713 u += SEED_C5 * (U32)(UV)&when;
1720 djSP; dTARGET; tryAMAGICun(exp);
1732 djSP; dTARGET; tryAMAGICun(log);
1737 SET_NUMERIC_STANDARD();
1738 DIE("Can't take log of %g", value);
1748 djSP; dTARGET; tryAMAGICun(sqrt);
1753 SET_NUMERIC_STANDARD();
1754 DIE("Can't take sqrt of %g", value);
1756 value = sqrt(value);
1766 double value = TOPn;
1769 if (SvIOKp(TOPs) && !SvNOKp(TOPs) && !SvPOKp(TOPs)) {
1775 (void)modf(value, &value);
1777 (void)modf(-value, &value);
1792 djSP; dTARGET; tryAMAGICun(abs);
1794 double value = TOPn;
1797 if (SvIOKp(TOPs) && !SvNOKp(TOPs) && !SvPOKp(TOPs) &&
1798 (iv = SvIVX(TOPs)) != IV_MIN) {
1820 XPUSHu(scan_hex(tmps, 99, &argtype));
1833 while (*tmps && isSPACE(*tmps))
1838 value = scan_hex(++tmps, 99, &argtype);
1839 else if (*tmps == 'b')
1840 value = scan_bin(++tmps, 99, &argtype);
1842 value = scan_oct(tmps, 99, &argtype);
1854 SETi( sv_len_utf8(TOPs) );
1858 SETi( sv_len(TOPs) );
1872 I32 lvalue = PL_op->op_flags & OPf_MOD;
1874 I32 arybase = PL_curcop->cop_arybase;
1878 SvTAINTED_off(TARG); /* decontaminate */
1882 repl = SvPV(sv, repl_len);
1889 tmps = SvPV(sv, curlen);
1891 utfcurlen = sv_len_utf8(sv);
1892 if (utfcurlen == curlen)
1900 if (pos >= arybase) {
1918 else if (len >= 0) {
1920 if (rem > (I32)curlen)
1934 if (ckWARN(WARN_SUBSTR) || lvalue || repl)
1935 warner(WARN_SUBSTR, "substr outside of string");
1940 sv_pos_u2b(sv, &pos, &rem);
1942 sv_setpvn(TARG, tmps, rem);
1943 if (lvalue) { /* it's an lvalue! */
1944 if (!SvGMAGICAL(sv)) {
1948 if (ckWARN(WARN_SUBSTR))
1950 "Attempt to use reference as lvalue in substr");
1952 if (SvOK(sv)) /* is it defined ? */
1953 (void)SvPOK_only(sv);
1955 sv_setpvn(sv,"",0); /* avoid lexical reincarnation */
1958 if (SvTYPE(TARG) < SVt_PVLV) {
1959 sv_upgrade(TARG, SVt_PVLV);
1960 sv_magic(TARG, Nullsv, 'x', Nullch, 0);
1964 if (LvTARG(TARG) != sv) {
1966 SvREFCNT_dec(LvTARG(TARG));
1967 LvTARG(TARG) = SvREFCNT_inc(sv);
1969 LvTARGOFF(TARG) = pos;
1970 LvTARGLEN(TARG) = rem;
1973 sv_insert(sv, pos, rem, repl, repl_len);
1976 PUSHs(TARG); /* avoid SvSETMAGIC here */
1983 register I32 size = POPi;
1984 register I32 offset = POPi;
1985 register SV *src = POPs;
1986 I32 lvalue = PL_op->op_flags & OPf_MOD;
1988 unsigned char *s = (unsigned char*)SvPV(src, srclen);
1989 unsigned long retnum;
1992 SvTAINTED_off(TARG); /* decontaminate */
1993 offset *= size; /* turn into bit offset */
1994 len = (offset + size + 7) / 8;
1995 if (offset < 0 || size < 1)
1998 if (lvalue) { /* it's an lvalue! */
1999 if (SvTYPE(TARG) < SVt_PVLV) {
2000 sv_upgrade(TARG, SVt_PVLV);
2001 sv_magic(TARG, Nullsv, 'v', Nullch, 0);
2005 if (LvTARG(TARG) != src) {
2007 SvREFCNT_dec(LvTARG(TARG));
2008 LvTARG(TARG) = SvREFCNT_inc(src);
2010 LvTARGOFF(TARG) = offset;
2011 LvTARGLEN(TARG) = size;
2019 if (offset >= srclen)
2022 retnum = (unsigned long) s[offset] << 8;
2024 else if (size == 32) {
2025 if (offset >= srclen)
2027 else if (offset + 1 >= srclen)
2028 retnum = (unsigned long) s[offset] << 24;
2029 else if (offset + 2 >= srclen)
2030 retnum = ((unsigned long) s[offset] << 24) +
2031 ((unsigned long) s[offset + 1] << 16);
2033 retnum = ((unsigned long) s[offset] << 24) +
2034 ((unsigned long) s[offset + 1] << 16) +
2035 (s[offset + 2] << 8);
2040 retnum = (s[offset >> 3] >> (offset & 7)) & ((1 << size) - 1);
2045 else if (size == 16)
2046 retnum = ((unsigned long) s[offset] << 8) + s[offset+1];
2047 else if (size == 32)
2048 retnum = ((unsigned long) s[offset] << 24) +
2049 ((unsigned long) s[offset + 1] << 16) +
2050 (s[offset + 2] << 8) + s[offset+3];
2054 sv_setuv(TARG, (UV)retnum);
2069 I32 arybase = PL_curcop->cop_arybase;
2074 offset = POPi - arybase;
2077 tmps = SvPV(big, biglen);
2078 if (IN_UTF8 && offset > 0)
2079 sv_pos_u2b(big, &offset, 0);
2082 else if (offset > biglen)
2084 if (!(tmps2 = fbm_instr((unsigned char*)tmps + offset,
2085 (unsigned char*)tmps + biglen, little, 0)))
2088 retval = tmps2 - tmps;
2089 if (IN_UTF8 && retval > 0)
2090 sv_pos_b2u(big, &retval);
2091 PUSHi(retval + arybase);
2106 I32 arybase = PL_curcop->cop_arybase;
2112 tmps2 = SvPV(little, llen);
2113 tmps = SvPV(big, blen);
2117 if (IN_UTF8 && offset > 0)
2118 sv_pos_u2b(big, &offset, 0);
2119 offset = offset - arybase + llen;
2123 else if (offset > blen)
2125 if (!(tmps2 = rninstr(tmps, tmps + offset,
2126 tmps2, tmps2 + llen)))
2129 retval = tmps2 - tmps;
2130 if (IN_UTF8 && retval > 0)
2131 sv_pos_b2u(big, &retval);
2132 PUSHi(retval + arybase);
2138 djSP; dMARK; dORIGMARK; dTARGET;
2139 #ifdef USE_LOCALE_NUMERIC
2140 if (PL_op->op_private & OPpLOCALE)
2141 SET_NUMERIC_LOCAL();
2143 SET_NUMERIC_STANDARD();
2145 do_sprintf(TARG, SP-MARK, MARK+1);
2146 TAINT_IF(SvTAINTED(TARG));
2157 U8 *tmps = (U8*)POPpx;
2160 if (IN_UTF8 && (*tmps & 0x80))
2161 value = utf8_to_uv(tmps, &retlen);
2163 value = (UV)(*tmps & 255);
2174 (void)SvUPGRADE(TARG,SVt_PV);
2176 if (IN_UTF8 && value >= 128) {
2179 tmps = (char*)uv_to_utf8((U8*)tmps, (UV)value);
2180 SvCUR_set(TARG, tmps - SvPVX(TARG));
2182 (void)SvPOK_only(TARG);
2192 (void)SvPOK_only(TARG);
2199 djSP; dTARGET; dPOPTOPssrl;
2202 char *tmps = SvPV(left, n_a);
2204 sv_setpv(TARG, fcrypt(tmps, SvPV(right, n_a)));
2206 sv_setpv(TARG, PerlProc_crypt(tmps, SvPV(right, n_a)));
2210 "The crypt() function is unimplemented due to excessive paranoia.");
2223 if (IN_UTF8 && (s = (U8*)SvPV(sv, slen)) && slen && (*s & 0xc0) == 0xc0) {
2227 UV uv = utf8_to_uv(s, &ulen);
2229 if (PL_op->op_private & OPpLOCALE) {
2232 uv = toTITLE_LC_uni(uv);
2235 uv = toTITLE_utf8(s);
2237 tend = uv_to_utf8(tmpbuf, uv);
2239 if (!SvPADTMP(sv) || tend - tmpbuf != ulen) {
2241 sv_setpvn(TARG, (char*)tmpbuf, tend - tmpbuf);
2242 sv_catpvn(TARG, (char*)(s + ulen), slen - ulen);
2246 s = (U8*)SvPV_force(sv, slen);
2247 Copy(tmpbuf, s, ulen, U8);
2252 if (!SvPADTMP(sv)) {
2258 s = (U8*)SvPV_force(sv, slen);
2260 if (PL_op->op_private & OPpLOCALE) {
2263 *s = toUPPER_LC(*s);
2279 if (IN_UTF8 && (s = (U8*)SvPV(sv, slen)) && slen && (*s & 0xc0) == 0xc0) {
2283 UV uv = utf8_to_uv(s, &ulen);
2285 if (PL_op->op_private & OPpLOCALE) {
2288 uv = toLOWER_LC_uni(uv);
2291 uv = toLOWER_utf8(s);
2293 tend = uv_to_utf8(tmpbuf, uv);
2295 if (!SvPADTMP(sv) || tend - tmpbuf != ulen) {
2297 sv_setpvn(TARG, (char*)tmpbuf, tend - tmpbuf);
2298 sv_catpvn(TARG, (char*)(s + ulen), slen - ulen);
2302 s = (U8*)SvPV_force(sv, slen);
2303 Copy(tmpbuf, s, ulen, U8);
2308 if (!SvPADTMP(sv)) {
2314 s = (U8*)SvPV_force(sv, slen);
2316 if (PL_op->op_private & OPpLOCALE) {
2319 *s = toLOWER_LC(*s);
2342 s = (U8*)SvPV(sv,len);
2344 sv_setpvn(TARG, "", 0);
2349 (void)SvUPGRADE(TARG, SVt_PV);
2350 SvGROW(TARG, (len * 2) + 1);
2351 (void)SvPOK_only(TARG);
2352 d = (U8*)SvPVX(TARG);
2354 if (PL_op->op_private & OPpLOCALE) {
2358 d = uv_to_utf8(d, toUPPER_LC_uni( utf8_to_uv(s, &ulen)));
2364 d = uv_to_utf8(d, toUPPER_utf8( s ));
2369 SvCUR_set(TARG, d - (U8*)SvPVX(TARG));
2374 if (!SvPADTMP(sv)) {
2381 s = (U8*)SvPV_force(sv, len);
2383 register U8 *send = s + len;
2385 if (PL_op->op_private & OPpLOCALE) {
2388 for (; s < send; s++)
2389 *s = toUPPER_LC(*s);
2392 for (; s < send; s++)
2412 s = (U8*)SvPV(sv,len);
2414 sv_setpvn(TARG, "", 0);
2419 (void)SvUPGRADE(TARG, SVt_PV);
2420 SvGROW(TARG, (len * 2) + 1);
2421 (void)SvPOK_only(TARG);
2422 d = (U8*)SvPVX(TARG);
2424 if (PL_op->op_private & OPpLOCALE) {
2428 d = uv_to_utf8(d, toLOWER_LC_uni( utf8_to_uv(s, &ulen)));
2434 d = uv_to_utf8(d, toLOWER_utf8(s));
2439 SvCUR_set(TARG, d - (U8*)SvPVX(TARG));
2444 if (!SvPADTMP(sv)) {
2451 s = (U8*)SvPV_force(sv, len);
2453 register U8 *send = s + len;
2455 if (PL_op->op_private & OPpLOCALE) {
2458 for (; s < send; s++)
2459 *s = toLOWER_LC(*s);
2462 for (; s < send; s++)
2474 register char *s = SvPV(sv,len);
2478 (void)SvUPGRADE(TARG, SVt_PV);
2479 SvGROW(TARG, (len * 2) + 1);
2484 STRLEN ulen = UTF8SKIP(s);
2507 SvCUR_set(TARG, d - SvPVX(TARG));
2508 (void)SvPOK_only(TARG);
2511 sv_setpvn(TARG, s, len);
2520 djSP; dMARK; dORIGMARK;
2522 register AV* av = (AV*)POPs;
2523 register I32 lval = PL_op->op_flags & OPf_MOD;
2524 I32 arybase = PL_curcop->cop_arybase;
2527 if (SvTYPE(av) == SVt_PVAV) {
2528 if (lval && PL_op->op_private & OPpLVAL_INTRO) {
2530 for (svp = MARK + 1; svp <= SP; svp++) {
2535 if (max > AvMAX(av))
2538 while (++MARK <= SP) {
2539 elem = SvIVx(*MARK);
2543 svp = av_fetch(av, elem, lval);
2545 if (!svp || *svp == &PL_sv_undef)
2546 DIE(PL_no_aelem, elem);
2547 if (PL_op->op_private & OPpLVAL_INTRO)
2548 save_aelem(av, elem, svp);
2550 *MARK = svp ? *svp : &PL_sv_undef;
2553 if (GIMME != G_ARRAY) {
2561 /* Associative arrays. */
2566 HV *hash = (HV*)POPs;
2568 I32 gimme = GIMME_V;
2569 I32 realhv = (SvTYPE(hash) == SVt_PVHV);
2572 /* might clobber stack_sp */
2573 entry = realhv ? hv_iternext(hash) : avhv_iternext((AV*)hash);
2578 PUSHs(hv_iterkeysv(entry)); /* won't clobber stack_sp */
2579 if (gimme == G_ARRAY) {
2581 /* might clobber stack_sp */
2582 sv_setsv(TARG, realhv ?
2583 hv_iterval(hash, entry) : avhv_iterval((AV*)hash, entry));
2588 else if (gimme == G_SCALAR)
2607 I32 gimme = GIMME_V;
2608 I32 discard = (gimme == G_VOID) ? G_DISCARD : 0;
2612 if (PL_op->op_private & OPpSLICE) {
2616 hvtype = SvTYPE(hv);
2617 while (++MARK <= SP) {
2618 if (hvtype == SVt_PVHV)
2619 sv = hv_delete_ent(hv, *MARK, discard, 0);
2621 DIE("Not a HASH reference");
2622 *MARK = sv ? sv : &PL_sv_undef;
2626 else if (gimme == G_SCALAR) {
2635 if (SvTYPE(hv) == SVt_PVHV)
2636 sv = hv_delete_ent(hv, keysv, discard, 0);
2638 DIE("Not a HASH reference");
2652 if (SvTYPE(hv) == SVt_PVHV) {
2653 if (hv_exists_ent(hv, tmpsv, 0))
2655 } else if (SvTYPE(hv) == SVt_PVAV) {
2656 if (avhv_exists_ent((AV*)hv, tmpsv, 0))
2659 DIE("Not a HASH reference");
2666 djSP; dMARK; dORIGMARK;
2667 register HV *hv = (HV*)POPs;
2668 register I32 lval = PL_op->op_flags & OPf_MOD;
2669 I32 realhv = (SvTYPE(hv) == SVt_PVHV);
2671 if (!realhv && PL_op->op_private & OPpLVAL_INTRO)
2672 DIE("Can't localize pseudo-hash element");
2674 if (realhv || SvTYPE(hv) == SVt_PVAV) {
2675 while (++MARK <= SP) {
2679 HE *he = hv_fetch_ent(hv, keysv, lval, 0);
2680 svp = he ? &HeVAL(he) : 0;
2682 svp = avhv_fetch_ent((AV*)hv, keysv, lval, 0);
2685 if (!svp || *svp == &PL_sv_undef) {
2687 DIE(PL_no_helem, SvPV(keysv, n_a));
2689 if (PL_op->op_private & OPpLVAL_INTRO)
2690 save_helem(hv, keysv, svp);
2692 *MARK = svp ? *svp : &PL_sv_undef;
2695 if (GIMME != G_ARRAY) {
2703 /* List operators. */
2708 if (GIMME != G_ARRAY) {
2710 *MARK = *SP; /* unwanted list, return last item */
2712 *MARK = &PL_sv_undef;
2721 SV **lastrelem = PL_stack_sp;
2722 SV **lastlelem = PL_stack_base + POPMARK;
2723 SV **firstlelem = PL_stack_base + POPMARK + 1;
2724 register SV **firstrelem = lastlelem + 1;
2725 I32 arybase = PL_curcop->cop_arybase;
2726 I32 lval = PL_op->op_flags & OPf_MOD;
2727 I32 is_something_there = lval;
2729 register I32 max = lastrelem - lastlelem;
2730 register SV **lelem;
2733 if (GIMME != G_ARRAY) {
2734 ix = SvIVx(*lastlelem);
2739 if (ix < 0 || ix >= max)
2740 *firstlelem = &PL_sv_undef;
2742 *firstlelem = firstrelem[ix];
2748 SP = firstlelem - 1;
2752 for (lelem = firstlelem; lelem <= lastlelem; lelem++) {
2757 *lelem = &PL_sv_undef;
2758 else if (!(*lelem = firstrelem[ix]))
2759 *lelem = &PL_sv_undef;
2763 if (ix >= max || !(*lelem = firstrelem[ix]))
2764 *lelem = &PL_sv_undef;
2766 if (!is_something_there && (SvOK(*lelem) || SvGMAGICAL(*lelem)))
2767 is_something_there = TRUE;
2769 if (is_something_there)
2772 SP = firstlelem - 1;
2778 djSP; dMARK; dORIGMARK;
2779 I32 items = SP - MARK;
2780 SV *av = sv_2mortal((SV*)av_make(items, MARK+1));
2781 SP = ORIGMARK; /* av_make() might realloc stack_sp */
2788 djSP; dMARK; dORIGMARK;
2789 HV* hv = (HV*)sv_2mortal((SV*)newHV());
2793 SV *val = NEWSV(46, 0);
2795 sv_setsv(val, *++MARK);
2796 else if (ckWARN(WARN_UNSAFE))
2797 warner(WARN_UNSAFE, "Odd number of elements in hash assignment");
2798 (void)hv_store_ent(hv,key,val,0);
2807 djSP; dMARK; dORIGMARK;
2808 register AV *ary = (AV*)*++MARK;
2812 register I32 offset;
2813 register I32 length;
2820 if (mg = SvTIED_mg((SV*)ary, 'P')) {
2821 *MARK-- = SvTIED_obj((SV*)ary, mg);
2825 perl_call_method("SPLICE",GIMME_V);
2834 offset = i = SvIVx(*MARK);
2836 offset += AvFILLp(ary) + 1;
2838 offset -= PL_curcop->cop_arybase;
2840 DIE(PL_no_aelem, i);
2842 length = SvIVx(*MARK++);
2844 length += AvFILLp(ary) - offset + 1;
2850 length = AvMAX(ary) + 1; /* close enough to infinity */
2854 length = AvMAX(ary) + 1;
2856 if (offset > AvFILLp(ary) + 1)
2857 offset = AvFILLp(ary) + 1;
2858 after = AvFILLp(ary) + 1 - (offset + length);
2859 if (after < 0) { /* not that much array */
2860 length += after; /* offset+length now in array */
2866 /* At this point, MARK .. SP-1 is our new LIST */
2869 diff = newlen - length;
2870 if (newlen && !AvREAL(ary) && AvREIFY(ary))
2873 if (diff < 0) { /* shrinking the area */
2875 New(451, tmparyval, newlen, SV*); /* so remember insertion */
2876 Copy(MARK, tmparyval, newlen, SV*);
2879 MARK = ORIGMARK + 1;
2880 if (GIMME == G_ARRAY) { /* copy return vals to stack */
2881 MEXTEND(MARK, length);
2882 Copy(AvARRAY(ary)+offset, MARK, length, SV*);
2884 EXTEND_MORTAL(length);
2885 for (i = length, dst = MARK; i; i--) {
2886 sv_2mortal(*dst); /* free them eventualy */
2893 *MARK = AvARRAY(ary)[offset+length-1];
2896 for (i = length - 1, dst = &AvARRAY(ary)[offset]; i > 0; i--)
2897 SvREFCNT_dec(*dst++); /* free them now */
2900 AvFILLp(ary) += diff;
2902 /* pull up or down? */
2904 if (offset < after) { /* easier to pull up */
2905 if (offset) { /* esp. if nothing to pull */
2906 src = &AvARRAY(ary)[offset-1];
2907 dst = src - diff; /* diff is negative */
2908 for (i = offset; i > 0; i--) /* can't trust Copy */
2912 SvPVX(ary) = (char*)(AvARRAY(ary) - diff); /* diff is negative */
2916 if (after) { /* anything to pull down? */
2917 src = AvARRAY(ary) + offset + length;
2918 dst = src + diff; /* diff is negative */
2919 Move(src, dst, after, SV*);
2921 dst = &AvARRAY(ary)[AvFILLp(ary)+1];
2922 /* avoid later double free */
2926 dst[--i] = &PL_sv_undef;
2929 for (src = tmparyval, dst = AvARRAY(ary) + offset;
2931 *dst = NEWSV(46, 0);
2932 sv_setsv(*dst++, *src++);
2934 Safefree(tmparyval);
2937 else { /* no, expanding (or same) */
2939 New(452, tmparyval, length, SV*); /* so remember deletion */
2940 Copy(AvARRAY(ary)+offset, tmparyval, length, SV*);
2943 if (diff > 0) { /* expanding */
2945 /* push up or down? */
2947 if (offset < after && diff <= AvARRAY(ary) - AvALLOC(ary)) {
2951 Move(src, dst, offset, SV*);
2953 SvPVX(ary) = (char*)(AvARRAY(ary) - diff);/* diff is positive */
2955 AvFILLp(ary) += diff;
2958 if (AvFILLp(ary) + diff >= AvMAX(ary)) /* oh, well */
2959 av_extend(ary, AvFILLp(ary) + diff);
2960 AvFILLp(ary) += diff;
2963 dst = AvARRAY(ary) + AvFILLp(ary);
2965 for (i = after; i; i--) {
2972 for (src = MARK, dst = AvARRAY(ary) + offset; newlen; newlen--) {
2973 *dst = NEWSV(46, 0);
2974 sv_setsv(*dst++, *src++);
2976 MARK = ORIGMARK + 1;
2977 if (GIMME == G_ARRAY) { /* copy return vals to stack */
2979 Copy(tmparyval, MARK, length, SV*);
2981 EXTEND_MORTAL(length);
2982 for (i = length, dst = MARK; i; i--) {
2983 sv_2mortal(*dst); /* free them eventualy */
2987 Safefree(tmparyval);
2991 else if (length--) {
2992 *MARK = tmparyval[length];
2995 while (length-- > 0)
2996 SvREFCNT_dec(tmparyval[length]);
2998 Safefree(tmparyval);
3001 *MARK = &PL_sv_undef;
3009 djSP; dMARK; dORIGMARK; dTARGET;
3010 register AV *ary = (AV*)*++MARK;
3011 register SV *sv = &PL_sv_undef;
3014 if (mg = SvTIED_mg((SV*)ary, 'P')) {
3015 *MARK-- = SvTIED_obj((SV*)ary, mg);
3019 perl_call_method("PUSH",G_SCALAR|G_DISCARD);
3024 /* Why no pre-extend of ary here ? */
3025 for (++MARK; MARK <= SP; MARK++) {
3028 sv_setsv(sv, *MARK);
3033 PUSHi( AvFILL(ary) + 1 );
3041 SV *sv = av_pop(av);
3043 (void)sv_2mortal(sv);
3052 SV *sv = av_shift(av);
3057 (void)sv_2mortal(sv);
3064 djSP; dMARK; dORIGMARK; dTARGET;
3065 register AV *ary = (AV*)*++MARK;
3070 if (mg = SvTIED_mg((SV*)ary, 'P')) {
3071 *MARK-- = SvTIED_obj((SV*)ary, mg);
3075 perl_call_method("UNSHIFT",G_SCALAR|G_DISCARD);
3080 av_unshift(ary, SP - MARK);
3083 sv_setsv(sv, *++MARK);
3084 (void)av_store(ary, i++, sv);
3088 PUSHi( AvFILL(ary) + 1 );
3098 if (GIMME == G_ARRAY) {
3109 register char *down;
3115 do_join(TARG, &PL_sv_no, MARK, SP);
3117 sv_setsv(TARG, (SP > MARK) ? *SP : DEFSV);
3118 up = SvPV_force(TARG, len);
3120 if (IN_UTF8) { /* first reverse each character */
3121 U8* s = (U8*)SvPVX(TARG);
3122 U8* send = (U8*)(s + len);
3131 down = (char*)(s - 1);
3132 if (s > send || !((*down & 0xc0) == 0x80)) {
3133 warn("Malformed UTF-8 character");
3145 down = SvPVX(TARG) + len - 1;
3151 (void)SvPOK_only(TARG);
3160 mul128(SV *sv, U8 m)
3163 char *s = SvPV(sv, len);
3167 if (!strnEQ(s, "0000", 4)) { /* need to grow sv */
3168 SV *tmpNew = newSVpv("0000000000", 10);
3170 sv_catsv(tmpNew, sv);
3171 SvREFCNT_dec(sv); /* free old sv */
3176 while (!*t) /* trailing '\0'? */
3179 i = ((*t - '0') << 7) + m;
3180 *(t--) = '0' + (i % 10);
3186 /* Explosives and implosives. */
3188 #if 'I' == 73 && 'J' == 74
3189 /* On an ASCII/ISO kind of system */
3190 #define ISUUCHAR(ch) ((ch) >= ' ' && (ch) < 'a')
3193 Some other sort of character set - use memchr() so we don't match
3196 #define ISUUCHAR(ch) (memchr(PL_uuemap, (ch), sizeof(PL_uuemap)-1) || (ch) == ' ')
3204 I32 gimme = GIMME_V;
3208 register char *pat = SvPV(left, llen);
3209 register char *s = SvPV(right, rlen);
3210 char *strend = s + rlen;
3212 register char *patend = pat + llen;
3217 /* These must not be in registers: */
3234 register U32 culong;
3238 if (gimme != G_ARRAY) { /* arrange to do first one only */
3240 for (patend = pat; !isALPHA(*patend) || *patend == 'x'; patend++) ;
3241 if (strchr("aAZbBhHP", *patend) || *pat == '%') {
3243 while (isDIGIT(*patend) || *patend == '*')
3249 while (pat < patend) {
3251 datumtype = *pat++ & 0xFF;
3252 if (isSPACE(datumtype))
3256 else if (*pat == '*') {
3257 len = strend - strbeg; /* long enough */
3260 else if (isDIGIT(*pat)) {
3262 while (isDIGIT(*pat))
3263 len = (len * 10) + (*pat++ - '0');
3266 len = (datumtype != '@');
3269 croak("Invalid type in unpack: '%c'", (int)datumtype);
3270 case ',': /* grandfather in commas but with a warning */
3271 if (commas++ == 0 && ckWARN(WARN_UNSAFE))
3272 warner(WARN_UNSAFE, "Invalid type in unpack: '%c'", (int)datumtype);
3275 if (len == 1 && pat[-1] != '1')
3284 if (len > strend - strbeg)
3285 DIE("@ outside of string");
3289 if (len > s - strbeg)
3290 DIE("X outside of string");
3294 if (len > strend - s)
3295 DIE("x outside of string");
3301 if (len > strend - s)
3304 goto uchar_checksum;
3305 sv = NEWSV(35, len);
3306 sv_setpvn(sv, s, len);
3308 if (datumtype == 'A' || datumtype == 'Z') {
3309 aptr = s; /* borrow register */
3310 if (datumtype == 'Z') { /* 'Z' strips stuff after first null */
3315 else { /* 'A' strips both nulls and spaces */
3316 s = SvPVX(sv) + len - 1;
3317 while (s >= SvPVX(sv) && (!*s || isSPACE(*s)))
3321 SvCUR_set(sv, s - SvPVX(sv));
3322 s = aptr; /* unborrow register */
3324 XPUSHs(sv_2mortal(sv));
3328 if (pat[-1] == '*' || len > (strend - s) * 8)
3329 len = (strend - s) * 8;
3332 Newz(601, PL_bitcount, 256, char);
3333 for (bits = 1; bits < 256; bits++) {
3334 if (bits & 1) PL_bitcount[bits]++;
3335 if (bits & 2) PL_bitcount[bits]++;
3336 if (bits & 4) PL_bitcount[bits]++;
3337 if (bits & 8) PL_bitcount[bits]++;
3338 if (bits & 16) PL_bitcount[bits]++;
3339 if (bits & 32) PL_bitcount[bits]++;
3340 if (bits & 64) PL_bitcount[bits]++;
3341 if (bits & 128) PL_bitcount[bits]++;
3345 culong += PL_bitcount[*(unsigned char*)s++];
3350 if (datumtype == 'b') {
3352 if (bits & 1) culong++;
3358 if (bits & 128) culong++;
3365 sv = NEWSV(35, len + 1);
3368 aptr = pat; /* borrow register */
3370 if (datumtype == 'b') {
3372 for (len = 0; len < aint; len++) {
3373 if (len & 7) /*SUPPRESS 595*/
3377 *pat++ = '0' + (bits & 1);
3382 for (len = 0; len < aint; len++) {
3387 *pat++ = '0' + ((bits & 128) != 0);
3391 pat = aptr; /* unborrow register */
3392 XPUSHs(sv_2mortal(sv));
3396 if (pat[-1] == '*' || len > (strend - s) * 2)
3397 len = (strend - s) * 2;
3398 sv = NEWSV(35, len + 1);
3401 aptr = pat; /* borrow register */
3403 if (datumtype == 'h') {
3405 for (len = 0; len < aint; len++) {
3410 *pat++ = PL_hexdigit[bits & 15];
3415 for (len = 0; len < aint; len++) {
3420 *pat++ = PL_hexdigit[(bits >> 4) & 15];
3424 pat = aptr; /* unborrow register */
3425 XPUSHs(sv_2mortal(sv));
3428 if (len > strend - s)
3433 if (aint >= 128) /* fake up signed chars */
3443 if (aint >= 128) /* fake up signed chars */
3446 sv_setiv(sv, (IV)aint);
3447 PUSHs(sv_2mortal(sv));
3452 if (len > strend - s)
3467 sv_setiv(sv, (IV)auint);
3468 PUSHs(sv_2mortal(sv));
3473 if (len > strend - s)
3476 while (len-- > 0 && s < strend) {
3477 auint = utf8_to_uv((U8*)s, &along);
3480 cdouble += (double)auint;
3488 while (len-- > 0 && s < strend) {
3489 auint = utf8_to_uv((U8*)s, &along);
3492 sv_setuv(sv, (UV)auint);
3493 PUSHs(sv_2mortal(sv));
3498 along = (strend - s) / SIZE16;
3515 sv_setiv(sv, (IV)ashort);
3516 PUSHs(sv_2mortal(sv));
3523 along = (strend - s) / SIZE16;
3528 COPY16(s, &aushort);
3531 if (datumtype == 'n')
3532 aushort = PerlSock_ntohs(aushort);
3535 if (datumtype == 'v')
3536 aushort = vtohs(aushort);
3545 COPY16(s, &aushort);
3549 if (datumtype == 'n')
3550 aushort = PerlSock_ntohs(aushort);
3553 if (datumtype == 'v')
3554 aushort = vtohs(aushort);
3556 sv_setiv(sv, (IV)aushort);
3557 PUSHs(sv_2mortal(sv));
3562 along = (strend - s) / sizeof(int);
3567 Copy(s, &aint, 1, int);
3570 cdouble += (double)aint;
3579 Copy(s, &aint, 1, int);
3583 /* Without the dummy below unpack("i", pack("i",-1))
3584 * return 0xFFffFFff instead of -1 for Digital Unix V4.0
3585 * cc with optimization turned on */
3587 sv_setiv(sv, (IV)aint) :
3589 sv_setiv(sv, (IV)aint);
3590 PUSHs(sv_2mortal(sv));
3595 along = (strend - s) / sizeof(unsigned int);
3600 Copy(s, &auint, 1, unsigned int);
3601 s += sizeof(unsigned int);
3603 cdouble += (double)auint;
3612 Copy(s, &auint, 1, unsigned int);
3613 s += sizeof(unsigned int);
3616 /* Without the dummy below unpack("I", pack("I",0xFFFFFFFF))
3617 * returns 1.84467440737096e+19 instead of 0xFFFFFFFF for
3618 * DEC C V5.8-009 on Digital UNIX V4.0 (Rev. 1091) (aka V4.0D)
3619 * with optimization turned on.
3620 * (DEC C V5.2-040 on Digital UNIX V4.0 (Rev. 564) (aka V4.0B)
3621 * does not have this problem even with -O4)
3624 sv_setuv(sv, (UV)auint) :
3626 sv_setuv(sv, (UV)auint);
3627 PUSHs(sv_2mortal(sv));
3632 along = (strend - s) / SIZE32;
3640 cdouble += (double)along;
3652 sv_setiv(sv, (IV)along);
3653 PUSHs(sv_2mortal(sv));
3660 along = (strend - s) / SIZE32;
3668 if (datumtype == 'N')
3669 aulong = PerlSock_ntohl(aulong);
3672 if (datumtype == 'V')
3673 aulong = vtohl(aulong);
3676 cdouble += (double)aulong;
3688 if (datumtype == 'N')
3689 aulong = PerlSock_ntohl(aulong);
3692 if (datumtype == 'V')
3693 aulong = vtohl(aulong);
3696 sv_setuv(sv, (UV)aulong);
3697 PUSHs(sv_2mortal(sv));
3702 along = (strend - s) / sizeof(char*);
3708 if (sizeof(char*) > strend - s)
3711 Copy(s, &aptr, 1, char*);
3717 PUSHs(sv_2mortal(sv));
3727 while ((len > 0) && (s < strend)) {
3728 auv = (auv << 7) | (*s & 0x7f);
3729 if (!(*s++ & 0x80)) {
3733 PUSHs(sv_2mortal(sv));
3737 else if (++bytes >= sizeof(UV)) { /* promote to string */
3741 sv = newSVpvf("%.*Vu", (int)TYPE_DIGITS(UV), auv);
3742 while (s < strend) {
3743 sv = mul128(sv, *s & 0x7f);
3744 if (!(*s++ & 0x80)) {
3753 PUSHs(sv_2mortal(sv));
3758 if ((s >= strend) && bytes)
3759 croak("Unterminated compressed integer");
3764 if (sizeof(char*) > strend - s)
3767 Copy(s, &aptr, 1, char*);
3772 sv_setpvn(sv, aptr, len);
3773 PUSHs(sv_2mortal(sv));
3777 along = (strend - s) / sizeof(Quad_t);
3783 if (s + sizeof(Quad_t) > strend)
3786 Copy(s, &aquad, 1, Quad_t);
3787 s += sizeof(Quad_t);
3790 if (aquad >= IV_MIN && aquad <= IV_MAX)
3791 sv_setiv(sv, (IV)aquad);
3793 sv_setnv(sv, (double)aquad);
3794 PUSHs(sv_2mortal(sv));
3798 along = (strend - s) / sizeof(Quad_t);
3804 if (s + sizeof(Uquad_t) > strend)
3807 Copy(s, &auquad, 1, Uquad_t);
3808 s += sizeof(Uquad_t);
3811 if (auquad <= UV_MAX)
3812 sv_setuv(sv, (UV)auquad);
3814 sv_setnv(sv, (double)auquad);
3815 PUSHs(sv_2mortal(sv));
3819 /* float and double added gnb@melba.bby.oz.au 22/11/89 */
3822 along = (strend - s) / sizeof(float);
3827 Copy(s, &afloat, 1, float);
3836 Copy(s, &afloat, 1, float);
3839 sv_setnv(sv, (double)afloat);
3840 PUSHs(sv_2mortal(sv));
3846 along = (strend - s) / sizeof(double);
3851 Copy(s, &adouble, 1, double);
3852 s += sizeof(double);
3860 Copy(s, &adouble, 1, double);
3861 s += sizeof(double);
3863 sv_setnv(sv, (double)adouble);
3864 PUSHs(sv_2mortal(sv));
3870 * Initialise the decode mapping. By using a table driven
3871 * algorithm, the code will be character-set independent
3872 * (and just as fast as doing character arithmetic)
3874 if (PL_uudmap['M'] == 0) {
3877 for (i = 0; i < sizeof(PL_uuemap); i += 1)
3878 PL_uudmap[PL_uuemap[i]] = i;
3880 * Because ' ' and '`' map to the same value,
3881 * we need to decode them both the same.
3886 along = (strend - s) * 3 / 4;
3887 sv = NEWSV(42, along);
3890 while (s < strend && *s > ' ' && ISUUCHAR(*s)) {
3895 len = PL_uudmap[*s++] & 077;
3897 if (s < strend && ISUUCHAR(*s))
3898 a = PL_uudmap[*s++] & 077;
3901 if (s < strend && ISUUCHAR(*s))
3902 b = PL_uudmap[*s++] & 077;
3905 if (s < strend && ISUUCHAR(*s))
3906 c = PL_uudmap[*s++] & 077;
3909 if (s < strend && ISUUCHAR(*s))
3910 d = PL_uudmap[*s++] & 077;
3913 hunk[0] = (a << 2) | (b >> 4);
3914 hunk[1] = (b << 4) | (c >> 2);
3915 hunk[2] = (c << 6) | d;
3916 sv_catpvn(sv, hunk, (len > 3) ? 3 : len);
3921 else if (s[1] == '\n') /* possible checksum byte */
3924 XPUSHs(sv_2mortal(sv));
3929 if (strchr("fFdD", datumtype) ||
3930 (checksum > 32 && strchr("iIlLNU", datumtype)) ) {
3934 while (checksum >= 16) {
3938 while (checksum >= 4) {
3944 along = (1 << checksum) - 1;
3945 while (cdouble < 0.0)
3947 cdouble = modf(cdouble / adouble, &trouble) * adouble;
3948 sv_setnv(sv, cdouble);
3951 if (checksum < 32) {
3952 aulong = (1 << checksum) - 1;
3955 sv_setuv(sv, (UV)culong);
3957 XPUSHs(sv_2mortal(sv));
3961 if (SP == oldsp && gimme == G_SCALAR)
3962 PUSHs(&PL_sv_undef);
3967 doencodes(register SV *sv, register char *s, register I32 len)
3971 *hunk = PL_uuemap[len];
3972 sv_catpvn(sv, hunk, 1);
3975 hunk[0] = PL_uuemap[(077 & (*s >> 2))];
3976 hunk[1] = PL_uuemap[(077 & (((*s << 4) & 060) | ((s[1] >> 4) & 017)))];
3977 hunk[2] = PL_uuemap[(077 & (((s[1] << 2) & 074) | ((s[2] >> 6) & 03)))];
3978 hunk[3] = PL_uuemap[(077 & (s[2] & 077))];
3979 sv_catpvn(sv, hunk, 4);
3984 char r = (len > 1 ? s[1] : '\0');
3985 hunk[0] = PL_uuemap[(077 & (*s >> 2))];
3986 hunk[1] = PL_uuemap[(077 & (((*s << 4) & 060) | ((r >> 4) & 017)))];
3987 hunk[2] = PL_uuemap[(077 & ((r << 2) & 074))];
3988 hunk[3] = PL_uuemap[0];
3989 sv_catpvn(sv, hunk, 4);
3991 sv_catpvn(sv, "\n", 1);
3995 is_an_int(char *s, STRLEN l)
3998 SV *result = newSVpv("", l);
3999 char *result_c = SvPV(result, n_a); /* convenience */
4000 char *out = result_c;
4010 SvREFCNT_dec(result);
4033 SvREFCNT_dec(result);
4039 SvCUR_set(result, out - result_c);
4044 div128(SV *pnum, bool *done)
4045 /* must be '\0' terminated */
4049 char *s = SvPV(pnum, len);
4058 i = m * 10 + (*t - '0');
4060 r = (i >> 7); /* r < 10 */
4067 SvCUR_set(pnum, (STRLEN) (t - s));
4074 djSP; dMARK; dORIGMARK; dTARGET;
4075 register SV *cat = TARG;
4078 register char *pat = SvPVx(*++MARK, fromlen);
4079 register char *patend = pat + fromlen;
4084 static char null10[] = {0,0,0,0,0,0,0,0,0,0};
4085 static char *space10 = " ";
4087 /* These must not be in registers: */
4105 sv_setpvn(cat, "", 0);
4106 while (pat < patend) {
4107 #define NEXTFROM (items-- > 0 ? *MARK++ : &PL_sv_no)
4108 datumtype = *pat++ & 0xFF;
4109 if (isSPACE(datumtype))
4112 len = strchr("@Xxu", datumtype) ? 0 : items;
4115 else if (isDIGIT(*pat)) {
4117 while (isDIGIT(*pat))
4118 len = (len * 10) + (*pat++ - '0');
4124 croak("Invalid type in pack: '%c'", (int)datumtype);
4125 case ',': /* grandfather in commas but with a warning */
4126 if (commas++ == 0 && ckWARN(WARN_UNSAFE))
4127 warner(WARN_UNSAFE, "Invalid type in pack: '%c'", (int)datumtype);
4130 DIE("%% may only be used in unpack");
4141 if (SvCUR(cat) < len)
4142 DIE("X outside of string");
4149 sv_catpvn(cat, null10, 10);
4152 sv_catpvn(cat, null10, len);
4158 aptr = SvPV(fromstr, fromlen);
4162 sv_catpvn(cat, aptr, len);
4164 sv_catpvn(cat, aptr, fromlen);
4166 if (datumtype == 'A') {
4168 sv_catpvn(cat, space10, 10);
4171 sv_catpvn(cat, space10, len);
4175 sv_catpvn(cat, null10, 10);
4178 sv_catpvn(cat, null10, len);
4185 char *savepat = pat;
4190 aptr = SvPV(fromstr, fromlen);
4195 SvCUR(cat) += (len+7)/8;
4196 SvGROW(cat, SvCUR(cat) + 1);
4197 aptr = SvPVX(cat) + aint;
4202 if (datumtype == 'B') {
4203 for (len = 0; len++ < aint;) {
4204 items |= *pat++ & 1;
4208 *aptr++ = items & 0xff;
4214 for (len = 0; len++ < aint;) {
4220 *aptr++ = items & 0xff;
4226 if (datumtype == 'B')
4227 items <<= 7 - (aint & 7);
4229 items >>= 7 - (aint & 7);
4230 *aptr++ = items & 0xff;
4232 pat = SvPVX(cat) + SvCUR(cat);
4243 char *savepat = pat;
4248 aptr = SvPV(fromstr, fromlen);
4253 SvCUR(cat) += (len+1)/2;
4254 SvGROW(cat, SvCUR(cat) + 1);
4255 aptr = SvPVX(cat) + aint;
4260 if (datumtype == 'H') {
4261 for (len = 0; len++ < aint;) {
4263 items |= ((*pat++ & 15) + 9) & 15;
4265 items |= *pat++ & 15;
4269 *aptr++ = items & 0xff;
4275 for (len = 0; len++ < aint;) {
4277 items |= (((*pat++ & 15) + 9) & 15) << 4;
4279 items |= (*pat++ & 15) << 4;
4283 *aptr++ = items & 0xff;
4289 *aptr++ = items & 0xff;
4290 pat = SvPVX(cat) + SvCUR(cat);
4302 aint = SvIV(fromstr);
4304 sv_catpvn(cat, &achar, sizeof(char));
4310 auint = SvUV(fromstr);
4311 SvGROW(cat, SvCUR(cat) + 10);
4312 SvCUR_set(cat, (char*)uv_to_utf8((U8*)SvEND(cat),auint)
4317 /* Float and double added by gnb@melba.bby.oz.au 22/11/89 */
4322 afloat = (float)SvNV(fromstr);
4323 sv_catpvn(cat, (char *)&afloat, sizeof (float));
4330 adouble = (double)SvNV(fromstr);
4331 sv_catpvn(cat, (char *)&adouble, sizeof (double));
4337 ashort = (I16)SvIV(fromstr);
4339 ashort = PerlSock_htons(ashort);
4341 CAT16(cat, &ashort);
4347 ashort = (I16)SvIV(fromstr);
4349 ashort = htovs(ashort);
4351 CAT16(cat, &ashort);
4358 ashort = (I16)SvIV(fromstr);
4359 CAT16(cat, &ashort);
4365 auint = SvUV(fromstr);
4366 sv_catpvn(cat, (char*)&auint, sizeof(unsigned int));
4372 adouble = floor(SvNV(fromstr));
4375 croak("Cannot compress negative numbers");
4381 #ifdef CXUX_BROKEN_CONSTANT_CONVERT
4382 adouble <= UV_MAX_cxux
4389 char buf[1 + sizeof(UV)];
4390 char *in = buf + sizeof(buf);
4391 UV auv = U_V(adouble);;
4394 *--in = (auv & 0x7f) | 0x80;
4397 buf[sizeof(buf) - 1] &= 0x7f; /* clear continue bit */
4398 sv_catpvn(cat, in, (buf + sizeof(buf)) - in);
4400 else if (SvPOKp(fromstr)) { /* decimal string arithmetics */
4401 char *from, *result, *in;
4406 /* Copy string and check for compliance */
4407 from = SvPV(fromstr, len);
4408 if ((norm = is_an_int(from, len)) == NULL)
4409 croak("can compress only unsigned integer");
4411 New('w', result, len, char);
4415 *--in = div128(norm, &done) | 0x80;
4416 result[len - 1] &= 0x7F; /* clear continue bit */
4417 sv_catpvn(cat, in, (result + len) - in);
4419 SvREFCNT_dec(norm); /* free norm */
4421 else if (SvNOKp(fromstr)) {
4422 char buf[sizeof(double) * 2]; /* 8/7 <= 2 */
4423 char *in = buf + sizeof(buf);
4426 double next = floor(adouble / 128);
4427 *--in = (unsigned char)(adouble - (next * 128)) | 0x80;
4428 if (--in < buf) /* this cannot happen ;-) */
4429 croak ("Cannot compress integer");
4431 } while (adouble > 0);
4432 buf[sizeof(buf) - 1] &= 0x7f; /* clear continue bit */
4433 sv_catpvn(cat, in, (buf + sizeof(buf)) - in);
4436 croak("Cannot compress non integer");
4442 aint = SvIV(fromstr);
4443 sv_catpvn(cat, (char*)&aint, sizeof(int));
4449 aulong = SvUV(fromstr);
4451 aulong = PerlSock_htonl(aulong);
4453 CAT32(cat, &aulong);
4459 aulong = SvUV(fromstr);
4461 aulong = htovl(aulong);
4463 CAT32(cat, &aulong);
4469 aulong = SvUV(fromstr);
4470 CAT32(cat, &aulong);
4476 along = SvIV(fromstr);
4484 auquad = (Uquad_t)SvIV(fromstr);
4485 sv_catpvn(cat, (char*)&auquad, sizeof(Uquad_t));
4491 aquad = (Quad_t)SvIV(fromstr);
4492 sv_catpvn(cat, (char*)&aquad, sizeof(Quad_t));
4495 #endif /* HAS_QUAD */
4497 len = 1; /* assume SV is correct length */
4502 if (fromstr == &PL_sv_undef)
4506 /* XXX better yet, could spirit away the string to
4507 * a safe spot and hang on to it until the result
4508 * of pack() (and all copies of the result) are
4511 if (ckWARN(WARN_UNSAFE) && (SvTEMP(fromstr) || SvPADTMP(fromstr)))
4513 "Attempt to pack pointer to temporary value");
4514 if (SvPOK(fromstr) || SvNIOK(fromstr))
4515 aptr = SvPV(fromstr,n_a);
4517 aptr = SvPV_force(fromstr,n_a);
4519 sv_catpvn(cat, (char*)&aptr, sizeof(char*));
4524 aptr = SvPV(fromstr, fromlen);
4525 SvGROW(cat, fromlen * 4 / 3);
4530 while (fromlen > 0) {
4537 doencodes(cat, aptr, todo);
4556 register I32 limit = POPi; /* note, negative is forever */
4559 register char *s = SvPV(sv, len);
4560 char *strend = s + len;
4562 register REGEXP *rx;
4566 I32 maxiters = (strend - s) + 10;
4569 I32 origlimit = limit;
4572 AV *oldstack = PL_curstack;
4573 I32 gimme = GIMME_V;
4574 I32 oldsave = PL_savestack_ix;
4575 I32 make_mortal = 1;
4576 MAGIC *mg = (MAGIC *) NULL;
4579 Copy(&LvTARGOFF(POPs), &pm, 1, PMOP*);
4584 DIE("panic: do_split");
4585 rx = pm->op_pmregexp;
4587 TAINT_IF((pm->op_pmflags & PMf_LOCALE) &&
4588 (pm->op_pmflags & (PMf_WHITE | PMf_SKIPWHITE)));
4590 if (pm->op_pmreplroot)
4591 ary = GvAVn((GV*)pm->op_pmreplroot);
4592 else if (gimme != G_ARRAY)
4594 ary = (AV*)PL_curpad[0];
4596 ary = GvAVn(PL_defgv);
4597 #endif /* USE_THREADS */
4600 if (ary && (gimme != G_ARRAY || (pm->op_pmflags & PMf_ONCE))) {
4606 if (mg = SvTIED_mg((SV*)ary, 'P')) {
4608 XPUSHs(SvTIED_obj((SV*)ary, mg));
4613 for (i = AvFILLp(ary); i >= 0; i--)
4614 AvARRAY(ary)[i] = &PL_sv_undef; /* don't free mere refs */
4616 /* temporarily switch stacks */
4617 SWITCHSTACK(PL_curstack, ary);
4621 base = SP - PL_stack_base;
4623 if (pm->op_pmflags & PMf_SKIPWHITE) {
4624 if (pm->op_pmflags & PMf_LOCALE) {
4625 while (isSPACE_LC(*s))
4633 if (pm->op_pmflags & (PMf_MULTILINE|PMf_SINGLELINE)) {
4634 SAVEINT(PL_multiline);
4635 PL_multiline = pm->op_pmflags & PMf_MULTILINE;
4639 limit = maxiters + 2;
4640 if (pm->op_pmflags & PMf_WHITE) {
4643 while (m < strend &&
4644 !((pm->op_pmflags & PMf_LOCALE)
4645 ? isSPACE_LC(*m) : isSPACE(*m)))
4650 dstr = NEWSV(30, m-s);
4651 sv_setpvn(dstr, s, m-s);
4657 while (s < strend &&
4658 ((pm->op_pmflags & PMf_LOCALE)
4659 ? isSPACE_LC(*s) : isSPACE(*s)))
4663 else if (strEQ("^", rx->precomp)) {
4666 for (m = s; m < strend && *m != '\n'; m++) ;
4670 dstr = NEWSV(30, m-s);
4671 sv_setpvn(dstr, s, m-s);
4678 else if (rx->check_substr && !rx->nparens
4679 && (rx->reganch & ROPT_CHECK_ALL)
4680 && !(rx->reganch & ROPT_ANCH)) {
4681 i = SvCUR(rx->check_substr);
4682 if (i == 1 && !SvTAIL(rx->check_substr)) {
4683 i = *SvPVX(rx->check_substr);
4686 for (m = s; m < strend && *m != i; m++) ;
4689 dstr = NEWSV(30, m-s);
4690 sv_setpvn(dstr, s, m-s);
4699 while (s < strend && --limit &&
4700 (m=fbm_instr((unsigned char*)s, (unsigned char*)strend,
4701 rx->check_substr, 0)) )
4704 dstr = NEWSV(31, m-s);
4705 sv_setpvn(dstr, s, m-s);
4714 maxiters += (strend - s) * rx->nparens;
4715 while (s < strend && --limit &&
4716 CALLREGEXEC(rx, s, strend, orig, 1, sv, NULL, 0))
4718 TAINT_IF(RX_MATCH_TAINTED(rx));
4720 && rx->subbase != orig) {
4725 strend = s + (strend - m);
4728 dstr = NEWSV(32, m-s);
4729 sv_setpvn(dstr, s, m-s);
4734 for (i = 1; i <= rx->nparens; i++) {
4738 dstr = NEWSV(33, m-s);
4739 sv_setpvn(dstr, s, m-s);
4742 dstr = NEWSV(33, 0);
4752 LEAVE_SCOPE(oldsave);
4753 iters = (SP - PL_stack_base) - base;
4754 if (iters > maxiters)
4757 /* keep field after final delim? */
4758 if (s < strend || (iters && origlimit)) {
4759 dstr = NEWSV(34, strend-s);
4760 sv_setpvn(dstr, s, strend-s);
4766 else if (!origlimit) {
4767 while (iters > 0 && (!TOPs || !SvANY(TOPs) || SvCUR(TOPs) == 0))
4773 SWITCHSTACK(ary, oldstack);
4774 if (SvSMAGICAL(ary)) {
4779 if (gimme == G_ARRAY) {
4781 Copy(AvARRAY(ary), SP + 1, iters, SV*);
4789 perl_call_method("PUSH",G_SCALAR|G_DISCARD);
4792 if (gimme == G_ARRAY) {
4793 /* EXTEND should not be needed - we just popped them */
4795 for (i=0; i < iters; i++) {
4796 SV **svp = av_fetch(ary, i, FALSE);
4797 PUSHs((svp) ? *svp : &PL_sv_undef);
4804 if (gimme == G_ARRAY)
4807 if (iters || !pm->op_pmreplroot) {
4817 unlock_condpair(void *svv)
4820 MAGIC *mg = mg_find((SV*)svv, 'm');
4823 croak("panic: unlock_condpair unlocking non-mutex");
4824 MUTEX_LOCK(MgMUTEXP(mg));
4825 if (MgOWNER(mg) != thr)
4826 croak("panic: unlock_condpair unlocking mutex that we don't own");
4828 COND_SIGNAL(MgOWNERCONDP(mg));
4829 DEBUG_S(PerlIO_printf(PerlIO_stderr(), "0x%lx: unlock 0x%lx\n",
4830 (unsigned long)thr, (unsigned long)svv);)
4831 MUTEX_UNLOCK(MgMUTEXP(mg));
4833 #endif /* USE_THREADS */
4846 mg = condpair_magic(sv);
4847 MUTEX_LOCK(MgMUTEXP(mg));
4848 if (MgOWNER(mg) == thr)
4849 MUTEX_UNLOCK(MgMUTEXP(mg));
4852 COND_WAIT(MgOWNERCONDP(mg), MgMUTEXP(mg));
4854 DEBUG_S(PerlIO_printf(PerlIO_stderr(), "0x%lx: pp_lock lock 0x%lx\n",
4855 (unsigned long)thr, (unsigned long)sv);)
4856 MUTEX_UNLOCK(MgMUTEXP(mg));
4857 save_destructor(unlock_condpair, sv);
4859 #endif /* USE_THREADS */
4860 if (SvTYPE(retsv) == SVt_PVAV || SvTYPE(retsv) == SVt_PVHV
4861 || SvTYPE(retsv) == SVt_PVCV) {
4862 retsv = refto(retsv);
4873 if (PL_op->op_private & OPpLVAL_INTRO)
4874 PUSHs(*save_threadsv(PL_op->op_targ));
4876 PUSHs(THREADSV(PL_op->op_targ));
4879 DIE("tried to access per-thread data in non-threaded perl");
4880 #endif /* USE_THREADS */