This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Not working yet - split problems ...
[perl5.git] / pp.c
CommitLineData
a0d0e21e 1/* pp.c
79072805 2 *
9607fc9c 3 * Copyright (c) 1991-1997, Larry Wall
79072805 4 *
a0d0e21e
LW
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.
79072805 7 *
a0d0e21e
LW
8 */
9
10/*
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
13 */
79072805
LW
14
15#include "EXTERN.h"
16#include "perl.h"
17
36477c24 18/*
ef2d312d
TH
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.
24 */
25#ifdef CXUX_BROKEN_CONSTANT_CONVERT
26static double UV_MAX_cxux = ((double)UV_MAX);
8ac85365 27#endif
ef2d312d
TH
28
29/*
36477c24 30 * Types used in bitwise operations.
31 *
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).)
36 *
96e4d5b1 37 * It just so happens that "int" is the right size almost everywhere.
36477c24 38 */
39typedef int IBW;
40typedef unsigned UBW;
41
96e4d5b1 42/*
43 * Mask used after bitwise operations.
44 *
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.
48 */
49#if BYTEORDER > 0xFFFF && defined(_CRAY) && !defined(_CRAYMPP)
46fc3d4c 50# define BW_BITS 32
51# define BW_MASK ((1 << BW_BITS) - 1)
52# define BW_SIGN (1 << (BW_BITS - 1))
96e4d5b1 53# define BWi(i) (((i) & BW_SIGN) ? ((i) | ~BW_MASK) : ((i) & BW_MASK))
54# define BWu(u) ((u) & BW_MASK)
55#else
56# define BWi(i) (i)
57# define BWu(u) (u)
58#endif
59
60/*
61 * Offset for integer pack/unpack.
62 *
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.
65 */
66
67/*
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.)
73 */
74#define SIZE16 2
75#define SIZE32 4
76
77#if BYTEORDER > 0xFFFF && defined(_CRAY) && !defined(_CRAYMPP)
78# if BYTEORDER == 0x12345678
79# define OFF16(p) (char*)(p)
80# define OFF32(p) (char*)(p)
81# else
82# if BYTEORDER == 0x87654321
83# define OFF16(p) ((char*)(p) + (sizeof(U16) - SIZE16))
84# define OFF32(p) ((char*)(p) + (sizeof(U32) - SIZE32))
85# else
86 }}}} bad cray byte order
87# endif
88# endif
89# define COPY16(s,p) (*(p) = 0, Copy(s, OFF16(p), SIZE16, char))
90# define COPY32(s,p) (*(p) = 0, Copy(s, OFF32(p), SIZE32, char))
91# define CAT16(sv,p) sv_catpvn(sv, OFF16(p), SIZE16)
92# define CAT32(sv,p) sv_catpvn(sv, OFF32(p), SIZE32)
93#else
94# define COPY16(s,p) Copy(s, p, SIZE16, char)
95# define COPY32(s,p) Copy(s, p, SIZE32, char)
96# define CAT16(sv,p) sv_catpvn(sv, (char*)(p), SIZE16)
97# define CAT32(sv,p) sv_catpvn(sv, (char*)(p), SIZE32)
98#endif
99
71be2cbc 100static void doencodes _((SV* sv, char* s, I32 len));
93dc8474
CS
101static SV* refto _((SV* sv));
102static U32 seed _((void));
103
104static bool srand_called = FALSE;
79072805 105
a0d0e21e 106/* variations on pp_null */
79072805 107
8ac85365
NIS
108#ifdef DONT_DECLARE_STD
109#ifdef I_UNISTD
110#include <unistd.h>
111#endif
112#else
113extern pid_t getpid (void);
114#endif
115
93a17b20
LW
116PP(pp_stub)
117{
4e35701f 118 djSP;
54310121 119 if (GIMME_V == G_SCALAR)
93a17b20 120 XPUSHs(&sv_undef);
93a17b20
LW
121 RETURN;
122}
123
79072805
LW
124PP(pp_scalar)
125{
126 return NORMAL;
127}
128
129/* Pushy stuff. */
130
93a17b20
LW
131PP(pp_padav)
132{
4e35701f 133 djSP; dTARGET;
a0d0e21e 134 if (op->op_private & OPpLVAL_INTRO)
8990e307 135 SAVECLEARSV(curpad[op->op_targ]);
85e6fe83 136 EXTEND(SP, 1);
a0d0e21e 137 if (op->op_flags & OPf_REF) {
85e6fe83 138 PUSHs(TARG);
93a17b20 139 RETURN;
85e6fe83
LW
140 }
141 if (GIMME == G_ARRAY) {
142 I32 maxarg = AvFILL((AV*)TARG) + 1;
93965878
NIS
143 EXTEND(SP, maxarg);
144 if (SvMAGICAL(TARG)) {
145 U32 i;
146 for (i=0; i < maxarg; i++) {
147 SV **svp = av_fetch((AV*)TARG, i, FALSE);
148 SP[i+1] = (svp) ? *svp : &sv_undef;
149 }
150 }
151 else {
152 Copy(AvARRAY((AV*)TARG), SP+1, maxarg, SV*);
153 }
85e6fe83
LW
154 SP += maxarg;
155 }
156 else {
157 SV* sv = sv_newmortal();
158 I32 maxarg = AvFILL((AV*)TARG) + 1;
159 sv_setiv(sv, maxarg);
160 PUSHs(sv);
161 }
162 RETURN;
93a17b20
LW
163}
164
165PP(pp_padhv)
166{
4e35701f 167 djSP; dTARGET;
54310121 168 I32 gimme;
169
93a17b20 170 XPUSHs(TARG);
a0d0e21e 171 if (op->op_private & OPpLVAL_INTRO)
8990e307 172 SAVECLEARSV(curpad[op->op_targ]);
a0d0e21e 173 if (op->op_flags & OPf_REF)
93a17b20 174 RETURN;
54310121 175 gimme = GIMME_V;
176 if (gimme == G_ARRAY) {
a0d0e21e 177 RETURNOP(do_kv(ARGS));
85e6fe83 178 }
54310121 179 else if (gimme == G_SCALAR) {
85e6fe83 180 SV* sv = sv_newmortal();
46fc3d4c 181 if (HvFILL((HV*)TARG))
182 sv_setpvf(sv, "%ld/%ld",
183 (long)HvFILL((HV*)TARG), (long)HvMAX((HV*)TARG) + 1);
85e6fe83
LW
184 else
185 sv_setiv(sv, 0);
186 SETs(sv);
85e6fe83 187 }
54310121 188 RETURN;
93a17b20
LW
189}
190
ed6116ce
LW
191PP(pp_padany)
192{
193 DIE("NOT IMPL LINE %d",__LINE__);
194}
195
79072805
LW
196/* Translations. */
197
198PP(pp_rv2gv)
199{
4e35701f 200 djSP; dTOPss;
a0d0e21e 201
ed6116ce 202 if (SvROK(sv)) {
a0d0e21e 203 wasref:
ed6116ce 204 sv = SvRV(sv);
b1dadf13 205 if (SvTYPE(sv) == SVt_PVIO) {
206 GV *gv = (GV*) sv_newmortal();
207 gv_init(gv, 0, "", 0, 0);
208 GvIOp(gv) = (IO *)sv;
3e3baf6d 209 (void)SvREFCNT_inc(sv);
b1dadf13 210 sv = (SV*) gv;
211 } else if (SvTYPE(sv) != SVt_PVGV)
a0d0e21e 212 DIE("Not a GLOB reference");
79072805
LW
213 }
214 else {
93a17b20 215 if (SvTYPE(sv) != SVt_PVGV) {
748a9306
LW
216 char *sym;
217
a0d0e21e
LW
218 if (SvGMAGICAL(sv)) {
219 mg_get(sv);
220 if (SvROK(sv))
221 goto wasref;
222 }
223 if (!SvOK(sv)) {
224 if (op->op_flags & OPf_REF ||
225 op->op_private & HINT_STRICT_REFS)
226 DIE(no_usym, "a symbol");
d83e6520
CS
227 if (dowarn)
228 warn(warn_uninit);
a0d0e21e
LW
229 RETSETUNDEF;
230 }
748a9306 231 sym = SvPV(sv, na);
85e6fe83 232 if (op->op_private & HINT_STRICT_REFS)
748a9306
LW
233 DIE(no_symref, sym, "a symbol");
234 sv = (SV*)gv_fetchpv(sym, TRUE, SVt_PVGV);
93a17b20 235 }
79072805 236 }
5f05dabc 237 if (op->op_private & OPpLVAL_INTRO)
238 save_gp((GV*)sv, !(op->op_flags & OPf_SPECIAL));
79072805
LW
239 SETs(sv);
240 RETURN;
241}
242
79072805
LW
243PP(pp_rv2sv)
244{
4e35701f 245 djSP; dTOPss;
79072805 246
ed6116ce 247 if (SvROK(sv)) {
a0d0e21e 248 wasref:
ed6116ce 249 sv = SvRV(sv);
79072805
LW
250 switch (SvTYPE(sv)) {
251 case SVt_PVAV:
252 case SVt_PVHV:
253 case SVt_PVCV:
a0d0e21e 254 DIE("Not a SCALAR reference");
79072805
LW
255 }
256 }
257 else {
f12c7020 258 GV *gv = (GV*)sv;
748a9306
LW
259 char *sym;
260
463ee0b2 261 if (SvTYPE(gv) != SVt_PVGV) {
a0d0e21e
LW
262 if (SvGMAGICAL(sv)) {
263 mg_get(sv);
264 if (SvROK(sv))
265 goto wasref;
266 }
267 if (!SvOK(sv)) {
268 if (op->op_flags & OPf_REF ||
269 op->op_private & HINT_STRICT_REFS)
270 DIE(no_usym, "a SCALAR");
d83e6520
CS
271 if (dowarn)
272 warn(warn_uninit);
a0d0e21e
LW
273 RETSETUNDEF;
274 }
748a9306 275 sym = SvPV(sv, na);
85e6fe83 276 if (op->op_private & HINT_STRICT_REFS)
748a9306 277 DIE(no_symref, sym, "a SCALAR");
f12c7020 278 gv = (GV*)gv_fetchpv(sym, TRUE, SVt_PV);
463ee0b2
LW
279 }
280 sv = GvSV(gv);
a0d0e21e
LW
281 }
282 if (op->op_flags & OPf_MOD) {
283 if (op->op_private & OPpLVAL_INTRO)
284 sv = save_scalar((GV*)TOPs);
5f05dabc 285 else if (op->op_private & OPpDEREF)
68dc0745 286 vivify_ref(sv, op->op_private & OPpDEREF);
79072805 287 }
a0d0e21e 288 SETs(sv);
79072805
LW
289 RETURN;
290}
291
292PP(pp_av2arylen)
293{
4e35701f 294 djSP;
79072805
LW
295 AV *av = (AV*)TOPs;
296 SV *sv = AvARYLEN(av);
297 if (!sv) {
298 AvARYLEN(av) = sv = NEWSV(0,0);
299 sv_upgrade(sv, SVt_IV);
300 sv_magic(sv, (SV*)av, '#', Nullch, 0);
301 }
302 SETs(sv);
303 RETURN;
304}
305
a0d0e21e
LW
306PP(pp_pos)
307{
4e35701f 308 djSP; dTARGET; dPOPss;
a0d0e21e
LW
309
310 if (op->op_flags & OPf_MOD) {
5f05dabc 311 if (SvTYPE(TARG) < SVt_PVLV) {
312 sv_upgrade(TARG, SVt_PVLV);
313 sv_magic(TARG, Nullsv, '.', Nullch, 0);
314 }
315
316 LvTYPE(TARG) = '.';
a0d0e21e
LW
317 LvTARG(TARG) = sv;
318 PUSHs(TARG); /* no SvSETMAGIC */
319 RETURN;
320 }
321 else {
322 MAGIC* mg;
323
324 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
325 mg = mg_find(sv, 'g');
326 if (mg && mg->mg_len >= 0) {
327 PUSHi(mg->mg_len + curcop->cop_arybase);
328 RETURN;
329 }
330 }
331 RETPUSHUNDEF;
332 }
333}
334
79072805
LW
335PP(pp_rv2cv)
336{
4e35701f 337 djSP;
79072805
LW
338 GV *gv;
339 HV *stash;
8990e307 340
4633a7c4
LW
341 /* We usually try to add a non-existent subroutine in case of AUTOLOAD. */
342 /* (But not in defined().) */
343 CV *cv = sv_2cv(TOPs, &stash, &gv, !(op->op_flags & OPf_SPECIAL));
07055b4c
CS
344 if (cv) {
345 if (CvCLONE(cv))
346 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
347 }
348 else
4633a7c4 349 cv = (CV*)&sv_undef;
79072805
LW
350 SETs((SV*)cv);
351 RETURN;
352}
353
c07a80fd 354PP(pp_prototype)
355{
4e35701f 356 djSP;
c07a80fd 357 CV *cv;
358 HV *stash;
359 GV *gv;
360 SV *ret;
361
362 ret = &sv_undef;
363 cv = sv_2cv(TOPs, &stash, &gv, FALSE);
5f05dabc 364 if (cv && SvPOK(cv))
365 ret = sv_2mortal(newSVpv(SvPVX(cv), SvCUR(cv)));
c07a80fd 366 SETs(ret);
367 RETURN;
368}
369
a0d0e21e
LW
370PP(pp_anoncode)
371{
4e35701f 372 djSP;
5f05dabc 373 CV* cv = (CV*)curpad[op->op_targ];
a5f75d66 374 if (CvCLONE(cv))
b355b4e0 375 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
5f05dabc 376 EXTEND(SP,1);
748a9306 377 PUSHs((SV*)cv);
a0d0e21e
LW
378 RETURN;
379}
380
381PP(pp_srefgen)
79072805 382{
4e35701f 383 djSP;
71be2cbc 384 *SP = refto(*SP);
79072805 385 RETURN;
a0d0e21e
LW
386}
387
388PP(pp_refgen)
389{
4e35701f 390 djSP; dMARK;
a0d0e21e
LW
391 if (GIMME != G_ARRAY) {
392 MARK[1] = *SP;
393 SP = MARK + 1;
394 }
bbce6d69 395 EXTEND_MORTAL(SP - MARK);
71be2cbc 396 while (++MARK <= SP)
397 *MARK = refto(*MARK);
a0d0e21e 398 RETURN;
79072805
LW
399}
400
71be2cbc 401static SV*
8ac85365 402refto(SV *sv)
71be2cbc 403{
404 SV* rv;
405
406 if (SvTYPE(sv) == SVt_PVLV && LvTYPE(sv) == 'y') {
407 if (LvTARGLEN(sv))
68dc0745 408 vivify_defelem(sv);
409 if (!(sv = LvTARG(sv)))
410 sv = &sv_undef;
71be2cbc 411 }
412 else if (SvPADTMP(sv))
413 sv = newSVsv(sv);
414 else {
415 SvTEMP_off(sv);
416 (void)SvREFCNT_inc(sv);
417 }
418 rv = sv_newmortal();
419 sv_upgrade(rv, SVt_RV);
420 SvRV(rv) = sv;
421 SvROK_on(rv);
422 return rv;
423}
424
79072805
LW
425PP(pp_ref)
426{
4e35701f 427 djSP; dTARGET;
463ee0b2 428 SV *sv;
79072805
LW
429 char *pv;
430
a0d0e21e 431 sv = POPs;
f12c7020 432
433 if (sv && SvGMAGICAL(sv))
434 mg_get(sv);
435
a0d0e21e 436 if (!sv || !SvROK(sv))
4633a7c4 437 RETPUSHNO;
79072805 438
ed6116ce 439 sv = SvRV(sv);
a0d0e21e 440 pv = sv_reftype(sv,TRUE);
463ee0b2 441 PUSHp(pv, strlen(pv));
79072805
LW
442 RETURN;
443}
444
445PP(pp_bless)
446{
4e35701f 447 djSP;
463ee0b2 448 HV *stash;
79072805 449
463ee0b2
LW
450 if (MAXARG == 1)
451 stash = curcop->cop_stash;
452 else
a0d0e21e
LW
453 stash = gv_stashsv(POPs, TRUE);
454
5d3fdfeb 455 (void)sv_bless(TOPs, stash);
79072805
LW
456 RETURN;
457}
458
fb73857a 459PP(pp_gelem)
460{
461 GV *gv;
462 SV *sv;
463 SV *ref;
464 char *elem;
4e35701f 465 djSP;
fb73857a 466
467 sv = POPs;
468 elem = SvPV(sv, na);
469 gv = (GV*)POPs;
470 ref = Nullsv;
471 sv = Nullsv;
472 switch (elem ? *elem : '\0')
473 {
474 case 'A':
475 if (strEQ(elem, "ARRAY"))
476 ref = (SV*)GvAV(gv);
477 break;
478 case 'C':
479 if (strEQ(elem, "CODE"))
480 ref = (SV*)GvCVu(gv);
481 break;
482 case 'F':
483 if (strEQ(elem, "FILEHANDLE")) /* XXX deprecate in 5.005 */
484 ref = (SV*)GvIOp(gv);
485 break;
486 case 'G':
487 if (strEQ(elem, "GLOB"))
488 ref = (SV*)gv;
489 break;
490 case 'H':
491 if (strEQ(elem, "HASH"))
492 ref = (SV*)GvHV(gv);
493 break;
494 case 'I':
495 if (strEQ(elem, "IO"))
496 ref = (SV*)GvIOp(gv);
497 break;
498 case 'N':
499 if (strEQ(elem, "NAME"))
500 sv = newSVpv(GvNAME(gv), GvNAMELEN(gv));
501 break;
502 case 'P':
503 if (strEQ(elem, "PACKAGE"))
504 sv = newSVpv(HvNAME(GvSTASH(gv)), 0);
505 break;
506 case 'S':
507 if (strEQ(elem, "SCALAR"))
508 ref = GvSV(gv);
509 break;
510 }
511 if (ref)
512 sv = newRV(ref);
513 if (sv)
514 sv_2mortal(sv);
515 else
516 sv = &sv_undef;
517 XPUSHs(sv);
518 RETURN;
519}
520
a0d0e21e 521/* Pattern matching */
79072805 522
a0d0e21e 523PP(pp_study)
79072805 524{
4e35701f 525 djSP; dPOPss;
c277df42 526 register UNOP *unop = cUNOP;
a0d0e21e
LW
527 register unsigned char *s;
528 register I32 pos;
529 register I32 ch;
530 register I32 *sfirst;
531 register I32 *snext;
a0d0e21e
LW
532 STRLEN len;
533
c277df42
IZ
534 if(unop->op_first && unop->op_first->op_type == OP_PUSHRE) {
535 PMOP *pm = (PMOP *)unop->op_first;
536 SV *rv = sv_newmortal();
537 sv = newSVrv(rv, "Regexp");
538 sv_magic(sv,(SV*)ReREFCNT_inc(pm->op_pmregexp),'r',0,0);
539 RETURNX(PUSHs(rv));
540 }
541
1e422769 542 if (sv == lastscream) {
543 if (SvSCREAM(sv))
544 RETPUSHYES;
545 }
c07a80fd 546 else {
547 if (lastscream) {
548 SvSCREAM_off(lastscream);
549 SvREFCNT_dec(lastscream);
550 }
551 lastscream = SvREFCNT_inc(sv);
552 }
1e422769 553
554 s = (unsigned char*)(SvPV(sv, len));
555 pos = len;
556 if (pos <= 0)
557 RETPUSHNO;
a0d0e21e
LW
558 if (pos > maxscream) {
559 if (maxscream < 0) {
560 maxscream = pos + 80;
561 New(301, screamfirst, 256, I32);
562 New(302, screamnext, maxscream, I32);
79072805
LW
563 }
564 else {
a0d0e21e
LW
565 maxscream = pos + pos / 4;
566 Renew(screamnext, maxscream, I32);
79072805 567 }
79072805 568 }
a0d0e21e
LW
569
570 sfirst = screamfirst;
571 snext = screamnext;
572
573 if (!sfirst || !snext)
574 DIE("do_study: out of memory");
575
576 for (ch = 256; ch; --ch)
577 *sfirst++ = -1;
578 sfirst -= 256;
579
580 while (--pos >= 0) {
581 ch = s[pos];
582 if (sfirst[ch] >= 0)
583 snext[pos] = sfirst[ch] - pos;
584 else
585 snext[pos] = -pos;
586 sfirst[ch] = pos;
79072805
LW
587 }
588
c07a80fd 589 SvSCREAM_on(sv);
464e2e8a 590 sv_magic(sv, Nullsv, 'g', Nullch, 0); /* piggyback on m//g magic */
1e422769 591 RETPUSHYES;
79072805
LW
592}
593
a0d0e21e 594PP(pp_trans)
79072805 595{
4e35701f 596 djSP; dTARG;
a0d0e21e
LW
597 SV *sv;
598
599 if (op->op_flags & OPf_STACKED)
600 sv = POPs;
79072805 601 else {
54b9620d 602 sv = DEFSV;
a0d0e21e 603 EXTEND(SP,1);
79072805 604 }
adbc6bb1 605 TARG = sv_newmortal();
a0d0e21e
LW
606 PUSHi(do_trans(sv, op));
607 RETURN;
79072805
LW
608}
609
a0d0e21e 610/* Lvalue operators. */
79072805 611
a0d0e21e
LW
612PP(pp_schop)
613{
4e35701f 614 djSP; dTARGET;
a0d0e21e
LW
615 do_chop(TARG, TOPs);
616 SETTARG;
617 RETURN;
79072805
LW
618}
619
a0d0e21e 620PP(pp_chop)
79072805 621{
4e35701f 622 djSP; dMARK; dTARGET;
a0d0e21e
LW
623 while (SP > MARK)
624 do_chop(TARG, POPs);
625 PUSHTARG;
626 RETURN;
79072805
LW
627}
628
a0d0e21e 629PP(pp_schomp)
79072805 630{
4e35701f 631 djSP; dTARGET;
a0d0e21e
LW
632 SETi(do_chomp(TOPs));
633 RETURN;
79072805
LW
634}
635
a0d0e21e 636PP(pp_chomp)
79072805 637{
4e35701f 638 djSP; dMARK; dTARGET;
a0d0e21e
LW
639 register I32 count = 0;
640
641 while (SP > MARK)
642 count += do_chomp(POPs);
643 PUSHi(count);
644 RETURN;
79072805
LW
645}
646
a0d0e21e 647PP(pp_defined)
463ee0b2 648{
4e35701f 649 djSP;
a0d0e21e
LW
650 register SV* sv;
651
652 sv = POPs;
653 if (!sv || !SvANY(sv))
654 RETPUSHNO;
655 switch (SvTYPE(sv)) {
656 case SVt_PVAV:
fb73857a 657 if (AvMAX(sv) >= 0 || SvGMAGICAL(sv))
a0d0e21e
LW
658 RETPUSHYES;
659 break;
660 case SVt_PVHV:
fb73857a 661 if (HvARRAY(sv) || SvGMAGICAL(sv))
a0d0e21e
LW
662 RETPUSHYES;
663 break;
664 case SVt_PVCV:
665 if (CvROOT(sv) || CvXSUB(sv))
666 RETPUSHYES;
667 break;
668 default:
669 if (SvGMAGICAL(sv))
670 mg_get(sv);
671 if (SvOK(sv))
672 RETPUSHYES;
673 }
674 RETPUSHNO;
463ee0b2
LW
675}
676
a0d0e21e
LW
677PP(pp_undef)
678{
4e35701f 679 djSP;
a0d0e21e
LW
680 SV *sv;
681
774d564b 682 if (!op->op_private) {
683 EXTEND(SP, 1);
a0d0e21e 684 RETPUSHUNDEF;
774d564b 685 }
79072805 686
a0d0e21e
LW
687 sv = POPs;
688 if (!sv)
689 RETPUSHUNDEF;
85e6fe83 690
a0d0e21e
LW
691 if (SvTHINKFIRST(sv)) {
692 if (SvREADONLY(sv))
693 RETPUSHUNDEF;
694 if (SvROK(sv))
695 sv_unref(sv);
85e6fe83
LW
696 }
697
a0d0e21e
LW
698 switch (SvTYPE(sv)) {
699 case SVt_NULL:
700 break;
701 case SVt_PVAV:
702 av_undef((AV*)sv);
703 break;
704 case SVt_PVHV:
705 hv_undef((HV*)sv);
706 break;
707 case SVt_PVCV:
54310121 708 if (cv_const_sv((CV*)sv))
9607fc9c 709 warn("Constant subroutine %s undefined",
54310121 710 CvANON((CV*)sv) ? "(anonymous)" : GvENAME(CvGV((CV*)sv)));
9607fc9c 711 /* FALL THROUGH */
712 case SVt_PVFM:
09280a33
CS
713 { GV* gv = (GV*)SvREFCNT_inc(CvGV((CV*)sv));
714 cv_undef((CV*)sv);
715 CvGV((CV*)sv) = gv; } /* let user-undef'd sub keep its identity */
a0d0e21e 716 break;
8e07c86e 717 case SVt_PVGV:
44a8e56a 718 if (SvFAKE(sv))
719 sv_setsv(sv, &sv_undef);
720 break;
a0d0e21e 721 default:
1e422769 722 if (SvTYPE(sv) >= SVt_PV && SvPVX(sv) && SvLEN(sv)) {
4633a7c4
LW
723 (void)SvOOK_off(sv);
724 Safefree(SvPVX(sv));
725 SvPV_set(sv, Nullch);
726 SvLEN_set(sv, 0);
a0d0e21e 727 }
4633a7c4
LW
728 (void)SvOK_off(sv);
729 SvSETMAGIC(sv);
79072805 730 }
a0d0e21e
LW
731
732 RETPUSHUNDEF;
79072805
LW
733}
734
a0d0e21e 735PP(pp_predec)
79072805 736{
4e35701f 737 djSP;
68dc0745 738 if (SvREADONLY(TOPs) || SvTYPE(TOPs) > SVt_PVLV)
44a8e56a 739 croak(no_modify);
55497cff 740 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) &&
741 SvIVX(TOPs) != IV_MIN)
742 {
748a9306 743 --SvIVX(TOPs);
55497cff 744 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
748a9306
LW
745 }
746 else
747 sv_dec(TOPs);
a0d0e21e
LW
748 SvSETMAGIC(TOPs);
749 return NORMAL;
750}
79072805 751
a0d0e21e
LW
752PP(pp_postinc)
753{
4e35701f 754 djSP; dTARGET;
68dc0745 755 if (SvREADONLY(TOPs) || SvTYPE(TOPs) > SVt_PVLV)
44a8e56a 756 croak(no_modify);
a0d0e21e 757 sv_setsv(TARG, TOPs);
55497cff 758 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) &&
759 SvIVX(TOPs) != IV_MAX)
760 {
748a9306 761 ++SvIVX(TOPs);
55497cff 762 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
748a9306
LW
763 }
764 else
765 sv_inc(TOPs);
a0d0e21e
LW
766 SvSETMAGIC(TOPs);
767 if (!SvOK(TARG))
768 sv_setiv(TARG, 0);
769 SETs(TARG);
770 return NORMAL;
771}
79072805 772
a0d0e21e
LW
773PP(pp_postdec)
774{
4e35701f 775 djSP; dTARGET;
68dc0745 776 if(SvREADONLY(TOPs) || SvTYPE(TOPs) > SVt_PVLV)
44a8e56a 777 croak(no_modify);
a0d0e21e 778 sv_setsv(TARG, TOPs);
55497cff 779 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) &&
780 SvIVX(TOPs) != IV_MIN)
781 {
748a9306 782 --SvIVX(TOPs);
55497cff 783 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
748a9306
LW
784 }
785 else
786 sv_dec(TOPs);
a0d0e21e
LW
787 SvSETMAGIC(TOPs);
788 SETs(TARG);
789 return NORMAL;
790}
79072805 791
a0d0e21e
LW
792/* Ordinary operators. */
793
794PP(pp_pow)
795{
4e35701f 796 djSP; dATARGET; tryAMAGICbin(pow,opASSIGN);
a0d0e21e
LW
797 {
798 dPOPTOPnnrl;
799 SETn( pow( left, right) );
800 RETURN;
93a17b20 801 }
a0d0e21e
LW
802}
803
804PP(pp_multiply)
805{
4e35701f 806 djSP; dATARGET; tryAMAGICbin(mult,opASSIGN);
a0d0e21e
LW
807 {
808 dPOPTOPnnrl;
809 SETn( left * right );
810 RETURN;
79072805 811 }
a0d0e21e
LW
812}
813
814PP(pp_divide)
815{
4e35701f 816 djSP; dATARGET; tryAMAGICbin(div,opASSIGN);
a0d0e21e 817 {
77676ba1 818 dPOPPOPnnrl;
7a4c00b4 819 double value;
820 if (right == 0.0)
a0d0e21e
LW
821 DIE("Illegal division by zero");
822#ifdef SLOPPYDIVIDE
823 /* insure that 20./5. == 4. */
824 {
7a4c00b4 825 IV k;
826 if ((double)I_V(left) == left &&
827 (double)I_V(right) == right &&
828 (k = I_V(left)/I_V(right))*I_V(right) == I_V(left)) {
a0d0e21e
LW
829 value = k;
830 } else {
7a4c00b4 831 value = left / right;
79072805 832 }
a0d0e21e
LW
833 }
834#else
7a4c00b4 835 value = left / right;
a0d0e21e
LW
836#endif
837 PUSHn( value );
838 RETURN;
79072805 839 }
a0d0e21e
LW
840}
841
842PP(pp_modulo)
843{
4e35701f 844 djSP; dATARGET; tryAMAGICbin(mod,opASSIGN);
a0d0e21e 845 {
68dc0745 846 UV left;
847 UV right;
beb18505
CS
848 bool left_neg;
849 bool right_neg;
68dc0745 850 UV ans;
a0d0e21e 851
68dc0745 852 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)) {
853 IV i = SvIVX(POPs);
beb18505 854 right = (right_neg = (i < 0)) ? -i : i;
68dc0745 855 }
856 else {
857 double n = POPn;
beb18505 858 right = U_V((right_neg = (n < 0)) ? -n : n);
68dc0745 859 }
a0d0e21e 860
36477c24 861 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)) {
68dc0745 862 IV i = SvIVX(POPs);
beb18505 863 left = (left_neg = (i < 0)) ? -i : i;
36477c24 864 }
a0d0e21e 865 else {
68dc0745 866 double n = POPn;
beb18505 867 left = U_V((left_neg = (n < 0)) ? -n : n);
a0d0e21e 868 }
68dc0745 869
870 if (!right)
871 DIE("Illegal modulus zero");
872
873 ans = left % right;
beb18505 874 if ((left_neg != right_neg) && ans)
68dc0745 875 ans = right - ans;
beb18505 876 if (right_neg) {
3e3baf6d
TB
877 /* XXX may warn: unary minus operator applied to unsigned type */
878 /* could change -foo to be (~foo)+1 instead */
4e35701f
NIS
879 if (ans <= ~((UV)IV_MAX)+1)
880 sv_setiv(TARG, ~ans+1);
beb18505
CS
881 else
882 sv_setnv(TARG, -(double)ans);
883 }
884 else
885 sv_setuv(TARG, ans);
886 PUSHTARG;
a0d0e21e 887 RETURN;
79072805 888 }
a0d0e21e 889}
79072805 890
a0d0e21e
LW
891PP(pp_repeat)
892{
4e35701f 893 djSP; dATARGET; tryAMAGICbin(repeat,opASSIGN);
748a9306 894 {
a0d0e21e
LW
895 register I32 count = POPi;
896 if (GIMME == G_ARRAY && op->op_private & OPpREPEAT_DOLIST) {
897 dMARK;
898 I32 items = SP - MARK;
899 I32 max;
79072805 900
a0d0e21e
LW
901 max = items * count;
902 MEXTEND(MARK, max);
903 if (count > 1) {
904 while (SP > MARK) {
905 if (*SP)
906 SvTEMP_off((*SP));
907 SP--;
79072805 908 }
a0d0e21e
LW
909 MARK++;
910 repeatcpy((char*)(MARK + items), (char*)MARK,
911 items * sizeof(SV*), count - 1);
912 SP += max;
79072805 913 }
a0d0e21e
LW
914 else if (count <= 0)
915 SP -= items;
79072805 916 }
a0d0e21e
LW
917 else { /* Note: mark already snarfed by pp_list */
918 SV *tmpstr;
919 STRLEN len;
920
921 tmpstr = POPs;
922 if (TARG == tmpstr && SvTHINKFIRST(tmpstr)) {
923 if (SvREADONLY(tmpstr) && curcop != &compiling)
924 DIE("Can't x= to readonly value");
925 if (SvROK(tmpstr))
926 sv_unref(tmpstr);
93a17b20 927 }
a0d0e21e
LW
928 SvSetSV(TARG, tmpstr);
929 SvPV_force(TARG, len);
8ebc5c01 930 if (count != 1) {
931 if (count < 1)
932 SvCUR_set(TARG, 0);
933 else {
934 SvGROW(TARG, (count * len) + 1);
a0d0e21e 935 repeatcpy(SvPVX(TARG) + len, SvPVX(TARG), len, count - 1);
8ebc5c01 936 SvCUR(TARG) *= count;
7a4c00b4 937 }
a0d0e21e 938 *SvEND(TARG) = '\0';
a0d0e21e 939 }
8ebc5c01 940 (void)SvPOK_only(TARG);
a0d0e21e 941 PUSHTARG;
79072805 942 }
a0d0e21e 943 RETURN;
748a9306 944 }
a0d0e21e 945}
79072805 946
a0d0e21e
LW
947PP(pp_subtract)
948{
4e35701f 949 djSP; dATARGET; tryAMAGICbin(subtr,opASSIGN);
a0d0e21e 950 {
7a4c00b4 951 dPOPTOPnnrl_ul;
a0d0e21e
LW
952 SETn( left - right );
953 RETURN;
79072805 954 }
a0d0e21e 955}
79072805 956
a0d0e21e
LW
957PP(pp_left_shift)
958{
4e35701f 959 djSP; dATARGET; tryAMAGICbin(lshift,opASSIGN);
a0d0e21e 960 {
36477c24 961 IBW shift = POPi;
ff68c719 962 if (op->op_private & HINT_INTEGER) {
36477c24 963 IBW i = TOPi;
46fc3d4c 964 i = BWi(i) << shift;
96e4d5b1 965 SETi(BWi(i));
ff68c719 966 }
967 else {
36477c24 968 UBW u = TOPu;
96e4d5b1 969 u <<= shift;
970 SETu(BWu(u));
ff68c719 971 }
55497cff 972 RETURN;
79072805 973 }
a0d0e21e 974}
79072805 975
a0d0e21e
LW
976PP(pp_right_shift)
977{
4e35701f 978 djSP; dATARGET; tryAMAGICbin(rshift,opASSIGN);
a0d0e21e 979 {
36477c24 980 IBW shift = POPi;
ff68c719 981 if (op->op_private & HINT_INTEGER) {
36477c24 982 IBW i = TOPi;
46fc3d4c 983 i = BWi(i) >> shift;
96e4d5b1 984 SETi(BWi(i));
ff68c719 985 }
986 else {
36477c24 987 UBW u = TOPu;
96e4d5b1 988 u >>= shift;
989 SETu(BWu(u));
ff68c719 990 }
a0d0e21e 991 RETURN;
93a17b20 992 }
79072805
LW
993}
994
a0d0e21e 995PP(pp_lt)
79072805 996{
4e35701f 997 djSP; tryAMAGICbinSET(lt,0);
a0d0e21e
LW
998 {
999 dPOPnv;
54310121 1000 SETs(boolSV(TOPn < value));
a0d0e21e 1001 RETURN;
79072805 1002 }
a0d0e21e 1003}
79072805 1004
a0d0e21e
LW
1005PP(pp_gt)
1006{
4e35701f 1007 djSP; tryAMAGICbinSET(gt,0);
a0d0e21e
LW
1008 {
1009 dPOPnv;
54310121 1010 SETs(boolSV(TOPn > value));
a0d0e21e 1011 RETURN;
79072805 1012 }
a0d0e21e
LW
1013}
1014
1015PP(pp_le)
1016{
4e35701f 1017 djSP; tryAMAGICbinSET(le,0);
a0d0e21e
LW
1018 {
1019 dPOPnv;
54310121 1020 SETs(boolSV(TOPn <= value));
a0d0e21e 1021 RETURN;
79072805 1022 }
a0d0e21e
LW
1023}
1024
1025PP(pp_ge)
1026{
4e35701f 1027 djSP; tryAMAGICbinSET(ge,0);
a0d0e21e
LW
1028 {
1029 dPOPnv;
54310121 1030 SETs(boolSV(TOPn >= value));
a0d0e21e 1031 RETURN;
79072805 1032 }
a0d0e21e 1033}
79072805 1034
a0d0e21e
LW
1035PP(pp_ne)
1036{
4e35701f 1037 djSP; tryAMAGICbinSET(ne,0);
a0d0e21e
LW
1038 {
1039 dPOPnv;
54310121 1040 SETs(boolSV(TOPn != value));
a0d0e21e
LW
1041 RETURN;
1042 }
79072805
LW
1043}
1044
a0d0e21e 1045PP(pp_ncmp)
79072805 1046{
4e35701f 1047 djSP; dTARGET; tryAMAGICbin(ncmp,0);
a0d0e21e
LW
1048 {
1049 dPOPTOPnnrl;
1050 I32 value;
79072805 1051
ff0cee69 1052 if (left == right)
a0d0e21e 1053 value = 0;
a0d0e21e
LW
1054 else if (left < right)
1055 value = -1;
44a8e56a 1056 else if (left > right)
1057 value = 1;
1058 else {
1059 SETs(&sv_undef);
1060 RETURN;
1061 }
a0d0e21e
LW
1062 SETi(value);
1063 RETURN;
79072805 1064 }
a0d0e21e 1065}
79072805 1066
a0d0e21e
LW
1067PP(pp_slt)
1068{
4e35701f 1069 djSP; tryAMAGICbinSET(slt,0);
a0d0e21e
LW
1070 {
1071 dPOPTOPssrl;
bbce6d69 1072 int cmp = ((op->op_private & OPpLOCALE)
1073 ? sv_cmp_locale(left, right)
1074 : sv_cmp(left, right));
54310121 1075 SETs(boolSV(cmp < 0));
a0d0e21e
LW
1076 RETURN;
1077 }
79072805
LW
1078}
1079
a0d0e21e 1080PP(pp_sgt)
79072805 1081{
4e35701f 1082 djSP; tryAMAGICbinSET(sgt,0);
a0d0e21e
LW
1083 {
1084 dPOPTOPssrl;
bbce6d69 1085 int cmp = ((op->op_private & OPpLOCALE)
1086 ? sv_cmp_locale(left, right)
1087 : sv_cmp(left, right));
54310121 1088 SETs(boolSV(cmp > 0));
a0d0e21e
LW
1089 RETURN;
1090 }
1091}
79072805 1092
a0d0e21e
LW
1093PP(pp_sle)
1094{
4e35701f 1095 djSP; tryAMAGICbinSET(sle,0);
a0d0e21e
LW
1096 {
1097 dPOPTOPssrl;
bbce6d69 1098 int cmp = ((op->op_private & OPpLOCALE)
1099 ? sv_cmp_locale(left, right)
1100 : sv_cmp(left, right));
54310121 1101 SETs(boolSV(cmp <= 0));
a0d0e21e 1102 RETURN;
79072805 1103 }
79072805
LW
1104}
1105
a0d0e21e
LW
1106PP(pp_sge)
1107{
4e35701f 1108 djSP; tryAMAGICbinSET(sge,0);
a0d0e21e
LW
1109 {
1110 dPOPTOPssrl;
bbce6d69 1111 int cmp = ((op->op_private & OPpLOCALE)
1112 ? sv_cmp_locale(left, right)
1113 : sv_cmp(left, right));
54310121 1114 SETs(boolSV(cmp >= 0));
a0d0e21e
LW
1115 RETURN;
1116 }
1117}
79072805 1118
36477c24 1119PP(pp_seq)
1120{
4e35701f 1121 djSP; tryAMAGICbinSET(seq,0);
36477c24 1122 {
1123 dPOPTOPssrl;
54310121 1124 SETs(boolSV(sv_eq(left, right)));
a0d0e21e
LW
1125 RETURN;
1126 }
1127}
79072805 1128
a0d0e21e 1129PP(pp_sne)
79072805 1130{
4e35701f 1131 djSP; tryAMAGICbinSET(sne,0);
a0d0e21e
LW
1132 {
1133 dPOPTOPssrl;
54310121 1134 SETs(boolSV(!sv_eq(left, right)));
a0d0e21e 1135 RETURN;
463ee0b2 1136 }
79072805
LW
1137}
1138
a0d0e21e 1139PP(pp_scmp)
79072805 1140{
4e35701f 1141 djSP; dTARGET; tryAMAGICbin(scmp,0);
a0d0e21e
LW
1142 {
1143 dPOPTOPssrl;
bbce6d69 1144 int cmp = ((op->op_private & OPpLOCALE)
1145 ? sv_cmp_locale(left, right)
1146 : sv_cmp(left, right));
1147 SETi( cmp );
a0d0e21e
LW
1148 RETURN;
1149 }
1150}
79072805 1151
55497cff 1152PP(pp_bit_and)
1153{
4e35701f 1154 djSP; dATARGET; tryAMAGICbin(band,opASSIGN);
a0d0e21e
LW
1155 {
1156 dPOPTOPssrl;
4633a7c4 1157 if (SvNIOKp(left) || SvNIOKp(right)) {
36477c24 1158 if (op->op_private & HINT_INTEGER) {
1159 IBW value = SvIV(left) & SvIV(right);
96e4d5b1 1160 SETi(BWi(value));
36477c24 1161 }
1162 else {
1163 UBW value = SvUV(left) & SvUV(right);
96e4d5b1 1164 SETu(BWu(value));
36477c24 1165 }
a0d0e21e
LW
1166 }
1167 else {
1168 do_vop(op->op_type, TARG, left, right);
1169 SETTARG;
1170 }
1171 RETURN;
1172 }
1173}
79072805 1174
a0d0e21e
LW
1175PP(pp_bit_xor)
1176{
4e35701f 1177 djSP; dATARGET; tryAMAGICbin(bxor,opASSIGN);
a0d0e21e
LW
1178 {
1179 dPOPTOPssrl;
4633a7c4 1180 if (SvNIOKp(left) || SvNIOKp(right)) {
36477c24 1181 if (op->op_private & HINT_INTEGER) {
1fbd88dc 1182 IBW value = (USE_LEFT(left) ? SvIV(left) : 0) ^ SvIV(right);
96e4d5b1 1183 SETi(BWi(value));
36477c24 1184 }
1185 else {
1fbd88dc 1186 UBW value = (USE_LEFT(left) ? SvUV(left) : 0) ^ SvUV(right);
96e4d5b1 1187 SETu(BWu(value));
36477c24 1188 }
a0d0e21e
LW
1189 }
1190 else {
1191 do_vop(op->op_type, TARG, left, right);
1192 SETTARG;
1193 }
1194 RETURN;
1195 }
1196}
79072805 1197
a0d0e21e
LW
1198PP(pp_bit_or)
1199{
4e35701f 1200 djSP; dATARGET; tryAMAGICbin(bor,opASSIGN);
a0d0e21e
LW
1201 {
1202 dPOPTOPssrl;
4633a7c4 1203 if (SvNIOKp(left) || SvNIOKp(right)) {
36477c24 1204 if (op->op_private & HINT_INTEGER) {
8ebc5c01 1205 IBW value = (USE_LEFT(left) ? SvIV(left) : 0) | SvIV(right);
96e4d5b1 1206 SETi(BWi(value));
36477c24 1207 }
1208 else {
8ebc5c01 1209 UBW value = (USE_LEFT(left) ? SvUV(left) : 0) | SvUV(right);
96e4d5b1 1210 SETu(BWu(value));
36477c24 1211 }
a0d0e21e
LW
1212 }
1213 else {
1214 do_vop(op->op_type, TARG, left, right);
1215 SETTARG;
1216 }
1217 RETURN;
79072805 1218 }
a0d0e21e 1219}
79072805 1220
a0d0e21e
LW
1221PP(pp_negate)
1222{
4e35701f 1223 djSP; dTARGET; tryAMAGICun(neg);
a0d0e21e
LW
1224 {
1225 dTOPss;
4633a7c4
LW
1226 if (SvGMAGICAL(sv))
1227 mg_get(sv);
55497cff 1228 if (SvIOKp(sv) && !SvNOKp(sv) && !SvPOKp(sv) && SvIVX(sv) != IV_MIN)
1229 SETi(-SvIVX(sv));
1230 else if (SvNIOKp(sv))
a0d0e21e 1231 SETn(-SvNV(sv));
4633a7c4 1232 else if (SvPOKp(sv)) {
a0d0e21e
LW
1233 STRLEN len;
1234 char *s = SvPV(sv, len);
bbce6d69 1235 if (isIDFIRST(*s)) {
a0d0e21e
LW
1236 sv_setpvn(TARG, "-", 1);
1237 sv_catsv(TARG, sv);
79072805 1238 }
a0d0e21e
LW
1239 else if (*s == '+' || *s == '-') {
1240 sv_setsv(TARG, sv);
1241 *SvPV_force(TARG, len) = *s == '-' ? '+' : '-';
79072805
LW
1242 }
1243 else
a0d0e21e
LW
1244 sv_setnv(TARG, -SvNV(sv));
1245 SETTARG;
79072805 1246 }
4633a7c4
LW
1247 else
1248 SETn(-SvNV(sv));
79072805 1249 }
a0d0e21e 1250 RETURN;
79072805
LW
1251}
1252
a0d0e21e 1253PP(pp_not)
79072805 1254{
a0d0e21e 1255#ifdef OVERLOAD
4e35701f 1256 djSP; tryAMAGICunSET(not);
a0d0e21e 1257#endif /* OVERLOAD */
54310121 1258 *stack_sp = boolSV(!SvTRUE(*stack_sp));
a0d0e21e 1259 return NORMAL;
79072805
LW
1260}
1261
a0d0e21e 1262PP(pp_complement)
79072805 1263{
4e35701f 1264 djSP; dTARGET; tryAMAGICun(compl);
a0d0e21e
LW
1265 {
1266 dTOPss;
4633a7c4 1267 if (SvNIOKp(sv)) {
36477c24 1268 if (op->op_private & HINT_INTEGER) {
1269 IBW value = ~SvIV(sv);
96e4d5b1 1270 SETi(BWi(value));
36477c24 1271 }
1272 else {
1273 UBW value = ~SvUV(sv);
96e4d5b1 1274 SETu(BWu(value));
36477c24 1275 }
a0d0e21e
LW
1276 }
1277 else {
1278 register char *tmps;
1279 register long *tmpl;
55497cff 1280 register I32 anum;
a0d0e21e
LW
1281 STRLEN len;
1282
1283 SvSetSV(TARG, sv);
1284 tmps = SvPV_force(TARG, len);
1285 anum = len;
1286#ifdef LIBERAL
1287 for ( ; anum && (unsigned long)tmps % sizeof(long); anum--, tmps++)
1288 *tmps = ~*tmps;
1289 tmpl = (long*)tmps;
1290 for ( ; anum >= sizeof(long); anum -= sizeof(long), tmpl++)
1291 *tmpl = ~*tmpl;
1292 tmps = (char*)tmpl;
1293#endif
1294 for ( ; anum > 0; anum--, tmps++)
1295 *tmps = ~*tmps;
1296
1297 SETs(TARG);
1298 }
1299 RETURN;
1300 }
79072805
LW
1301}
1302
a0d0e21e
LW
1303/* integer versions of some of the above */
1304
a0d0e21e 1305PP(pp_i_multiply)
79072805 1306{
4e35701f 1307 djSP; dATARGET; tryAMAGICbin(mult,opASSIGN);
a0d0e21e
LW
1308 {
1309 dPOPTOPiirl;
1310 SETi( left * right );
1311 RETURN;
1312 }
79072805
LW
1313}
1314
a0d0e21e 1315PP(pp_i_divide)
79072805 1316{
4e35701f 1317 djSP; dATARGET; tryAMAGICbin(div,opASSIGN);
a0d0e21e
LW
1318 {
1319 dPOPiv;
1320 if (value == 0)
1321 DIE("Illegal division by zero");
1322 value = POPi / value;
1323 PUSHi( value );
1324 RETURN;
1325 }
79072805
LW
1326}
1327
a0d0e21e 1328PP(pp_i_modulo)
79072805 1329{
4e35701f 1330 djSP; dATARGET; tryAMAGICbin(mod,opASSIGN);
79072805 1331 {
a0d0e21e 1332 dPOPTOPiirl;
aa306039
CS
1333 if (!right)
1334 DIE("Illegal modulus zero");
a0d0e21e
LW
1335 SETi( left % right );
1336 RETURN;
79072805 1337 }
79072805
LW
1338}
1339
a0d0e21e 1340PP(pp_i_add)
79072805 1341{
4e35701f 1342 djSP; dATARGET; tryAMAGICbin(add,opASSIGN);
a0d0e21e
LW
1343 {
1344 dPOPTOPiirl;
1345 SETi( left + right );
1346 RETURN;
79072805 1347 }
79072805
LW
1348}
1349
a0d0e21e 1350PP(pp_i_subtract)
79072805 1351{
4e35701f 1352 djSP; dATARGET; tryAMAGICbin(subtr,opASSIGN);
a0d0e21e
LW
1353 {
1354 dPOPTOPiirl;
1355 SETi( left - right );
1356 RETURN;
79072805 1357 }
79072805
LW
1358}
1359
a0d0e21e 1360PP(pp_i_lt)
79072805 1361{
4e35701f 1362 djSP; tryAMAGICbinSET(lt,0);
a0d0e21e
LW
1363 {
1364 dPOPTOPiirl;
54310121 1365 SETs(boolSV(left < right));
a0d0e21e
LW
1366 RETURN;
1367 }
79072805
LW
1368}
1369
a0d0e21e 1370PP(pp_i_gt)
79072805 1371{
4e35701f 1372 djSP; tryAMAGICbinSET(gt,0);
a0d0e21e
LW
1373 {
1374 dPOPTOPiirl;
54310121 1375 SETs(boolSV(left > right));
a0d0e21e
LW
1376 RETURN;
1377 }
79072805
LW
1378}
1379
a0d0e21e 1380PP(pp_i_le)
79072805 1381{
4e35701f 1382 djSP; tryAMAGICbinSET(le,0);
a0d0e21e
LW
1383 {
1384 dPOPTOPiirl;
54310121 1385 SETs(boolSV(left <= right));
a0d0e21e 1386 RETURN;
85e6fe83 1387 }
79072805
LW
1388}
1389
a0d0e21e 1390PP(pp_i_ge)
79072805 1391{
4e35701f 1392 djSP; tryAMAGICbinSET(ge,0);
a0d0e21e
LW
1393 {
1394 dPOPTOPiirl;
54310121 1395 SETs(boolSV(left >= right));
a0d0e21e
LW
1396 RETURN;
1397 }
79072805
LW
1398}
1399
a0d0e21e 1400PP(pp_i_eq)
79072805 1401{
4e35701f 1402 djSP; tryAMAGICbinSET(eq,0);
a0d0e21e
LW
1403 {
1404 dPOPTOPiirl;
54310121 1405 SETs(boolSV(left == right));
a0d0e21e
LW
1406 RETURN;
1407 }
79072805
LW
1408}
1409
a0d0e21e 1410PP(pp_i_ne)
79072805 1411{
4e35701f 1412 djSP; tryAMAGICbinSET(ne,0);
a0d0e21e
LW
1413 {
1414 dPOPTOPiirl;
54310121 1415 SETs(boolSV(left != right));
a0d0e21e
LW
1416 RETURN;
1417 }
79072805
LW
1418}
1419
a0d0e21e 1420PP(pp_i_ncmp)
79072805 1421{
4e35701f 1422 djSP; dTARGET; tryAMAGICbin(ncmp,0);
a0d0e21e
LW
1423 {
1424 dPOPTOPiirl;
1425 I32 value;
79072805 1426
a0d0e21e 1427 if (left > right)
79072805 1428 value = 1;
a0d0e21e 1429 else if (left < right)
79072805 1430 value = -1;
a0d0e21e 1431 else
79072805 1432 value = 0;
a0d0e21e
LW
1433 SETi(value);
1434 RETURN;
79072805 1435 }
85e6fe83
LW
1436}
1437
1438PP(pp_i_negate)
1439{
4e35701f 1440 djSP; dTARGET; tryAMAGICun(neg);
85e6fe83
LW
1441 SETi(-TOPi);
1442 RETURN;
1443}
1444
79072805
LW
1445/* High falutin' math. */
1446
1447PP(pp_atan2)
1448{
4e35701f 1449 djSP; dTARGET; tryAMAGICbin(atan2,0);
a0d0e21e
LW
1450 {
1451 dPOPTOPnnrl;
1452 SETn(atan2(left, right));
1453 RETURN;
1454 }
79072805
LW
1455}
1456
1457PP(pp_sin)
1458{
4e35701f 1459 djSP; dTARGET; tryAMAGICun(sin);
a0d0e21e
LW
1460 {
1461 double value;
1462 value = POPn;
1463 value = sin(value);
1464 XPUSHn(value);
1465 RETURN;
1466 }
79072805
LW
1467}
1468
1469PP(pp_cos)
1470{
4e35701f 1471 djSP; dTARGET; tryAMAGICun(cos);
a0d0e21e
LW
1472 {
1473 double value;
1474 value = POPn;
1475 value = cos(value);
1476 XPUSHn(value);
1477 RETURN;
1478 }
79072805
LW
1479}
1480
1481PP(pp_rand)
1482{
4e35701f 1483 djSP; dTARGET;
79072805
LW
1484 double value;
1485 if (MAXARG < 1)
1486 value = 1.0;
1487 else
1488 value = POPn;
1489 if (value == 0.0)
1490 value = 1.0;
93dc8474
CS
1491 if (!srand_called) {
1492 (void)srand((unsigned)seed());
1493 srand_called = TRUE;
1494 }
79072805
LW
1495#if RANDBITS == 31
1496 value = rand() * value / 2147483648.0;
1497#else
1498#if RANDBITS == 16
1499 value = rand() * value / 65536.0;
1500#else
1501#if RANDBITS == 15
1502 value = rand() * value / 32768.0;
1503#else
1504 value = rand() * value / (double)(((unsigned long)1) << RANDBITS);
1505#endif
1506#endif
1507#endif
1508 XPUSHn(value);
1509 RETURN;
1510}
1511
1512PP(pp_srand)
1513{
4e35701f 1514 djSP;
93dc8474
CS
1515 UV anum;
1516 if (MAXARG < 1)
1517 anum = seed();
79072805 1518 else
93dc8474
CS
1519 anum = POPu;
1520 (void)srand((unsigned)anum);
1521 srand_called = TRUE;
79072805
LW
1522 EXTEND(SP, 1);
1523 RETPUSHYES;
1524}
1525
93dc8474 1526static U32
8ac85365 1527seed(void)
93dc8474 1528{
54310121 1529 /*
1530 * This is really just a quick hack which grabs various garbage
1531 * values. It really should be a real hash algorithm which
1532 * spreads the effect of every input bit onto every output bit,
1533 * if someone who knows about such tings would bother to write it.
1534 * Might be a good idea to add that function to CORE as well.
1535 * No numbers below come from careful analysis or anyting here,
1536 * except they are primes and SEED_C1 > 1E6 to get a full-width
1537 * value from (tv_sec * SEED_C1 + tv_usec). The multipliers should
1538 * probably be bigger too.
1539 */
1540#if RANDBITS > 16
1541# define SEED_C1 1000003
1542#define SEED_C4 73819
1543#else
1544# define SEED_C1 25747
1545#define SEED_C4 20639
1546#endif
1547#define SEED_C2 3
1548#define SEED_C3 269
1549#define SEED_C5 26107
1550
e858de61 1551 dTHR;
93dc8474 1552 U32 u;
f12c7020 1553#ifdef VMS
1554# include <starlet.h>
43c92808
HF
1555 /* when[] = (low 32 bits, high 32 bits) of time since epoch
1556 * in 100-ns units, typically incremented ever 10 ms. */
93dc8474
CS
1557 unsigned int when[2];
1558 _ckvmssts(sys$gettim(when));
54310121 1559 u = (U32)SEED_C1 * when[0] + (U32)SEED_C2 * when[1];
f12c7020 1560#else
5f05dabc 1561# ifdef HAS_GETTIMEOFDAY
93dc8474
CS
1562 struct timeval when;
1563 gettimeofday(&when,(struct timezone *) 0);
54310121 1564 u = (U32)SEED_C1 * when.tv_sec + (U32)SEED_C2 * when.tv_usec;
f12c7020 1565# else
93dc8474
CS
1566 Time_t when;
1567 (void)time(&when);
54310121 1568 u = (U32)SEED_C1 * when;
f12c7020 1569# endif
1570#endif
54310121 1571 u += SEED_C3 * (U32)getpid();
1572 u += SEED_C4 * (U32)(UV)stack_sp;
1573#ifndef PLAN9 /* XXX Plan9 assembler chokes on this; fix needed */
1574 u += SEED_C5 * (U32)(UV)&when;
f12c7020 1575#endif
93dc8474 1576 return u;
79072805
LW
1577}
1578
1579PP(pp_exp)
1580{
4e35701f 1581 djSP; dTARGET; tryAMAGICun(exp);
a0d0e21e
LW
1582 {
1583 double value;
1584 value = POPn;
1585 value = exp(value);
1586 XPUSHn(value);
1587 RETURN;
1588 }
79072805
LW
1589}
1590
1591PP(pp_log)
1592{
4e35701f 1593 djSP; dTARGET; tryAMAGICun(log);
a0d0e21e
LW
1594 {
1595 double value;
1596 value = POPn;
bbce6d69 1597 if (value <= 0.0) {
36477c24 1598 SET_NUMERIC_STANDARD();
2304df62 1599 DIE("Can't take log of %g", value);
bbce6d69 1600 }
a0d0e21e
LW
1601 value = log(value);
1602 XPUSHn(value);
1603 RETURN;
1604 }
79072805
LW
1605}
1606
1607PP(pp_sqrt)
1608{
4e35701f 1609 djSP; dTARGET; tryAMAGICun(sqrt);
a0d0e21e
LW
1610 {
1611 double value;
1612 value = POPn;
bbce6d69 1613 if (value < 0.0) {
36477c24 1614 SET_NUMERIC_STANDARD();
2304df62 1615 DIE("Can't take sqrt of %g", value);
bbce6d69 1616 }
a0d0e21e
LW
1617 value = sqrt(value);
1618 XPUSHn(value);
1619 RETURN;
1620 }
79072805
LW
1621}
1622
1623PP(pp_int)
1624{
4e35701f 1625 djSP; dTARGET;
774d564b 1626 {
1627 double value = TOPn;
1628 IV iv;
1629
1630 if (SvIOKp(TOPs) && !SvNOKp(TOPs) && !SvPOKp(TOPs)) {
1631 iv = SvIVX(TOPs);
1632 SETi(iv);
1633 }
1634 else {
1635 if (value >= 0.0)
1636 (void)modf(value, &value);
1637 else {
1638 (void)modf(-value, &value);
1639 value = -value;
1640 }
1641 iv = I_V(value);
1642 if (iv == value)
1643 SETi(iv);
1644 else
1645 SETn(value);
1646 }
79072805 1647 }
79072805
LW
1648 RETURN;
1649}
1650
463ee0b2
LW
1651PP(pp_abs)
1652{
4e35701f 1653 djSP; dTARGET; tryAMAGICun(abs);
a0d0e21e 1654 {
774d564b 1655 double value = TOPn;
1656 IV iv;
463ee0b2 1657
774d564b 1658 if (SvIOKp(TOPs) && !SvNOKp(TOPs) && !SvPOKp(TOPs) &&
1659 (iv = SvIVX(TOPs)) != IV_MIN) {
1660 if (iv < 0)
1661 iv = -iv;
1662 SETi(iv);
1663 }
1664 else {
1665 if (value < 0.0)
1666 value = -value;
1667 SETn(value);
1668 }
a0d0e21e 1669 }
774d564b 1670 RETURN;
463ee0b2
LW
1671}
1672
79072805
LW
1673PP(pp_hex)
1674{
4e35701f 1675 djSP; dTARGET;
79072805
LW
1676 char *tmps;
1677 I32 argtype;
1678
a0d0e21e 1679 tmps = POPp;
55497cff 1680 XPUSHu(scan_hex(tmps, 99, &argtype));
79072805
LW
1681 RETURN;
1682}
1683
1684PP(pp_oct)
1685{
4e35701f 1686 djSP; dTARGET;
55497cff 1687 UV value;
79072805
LW
1688 I32 argtype;
1689 char *tmps;
1690
a0d0e21e 1691 tmps = POPp;
464e2e8a 1692 while (*tmps && isSPACE(*tmps))
1693 tmps++;
1694 if (*tmps == '0')
79072805
LW
1695 tmps++;
1696 if (*tmps == 'x')
464e2e8a 1697 value = scan_hex(++tmps, 99, &argtype);
1698 else
1699 value = scan_oct(tmps, 99, &argtype);
55497cff 1700 XPUSHu(value);
79072805
LW
1701 RETURN;
1702}
1703
1704/* String stuff. */
1705
1706PP(pp_length)
1707{
4e35701f 1708 djSP; dTARGET;
a0d0e21e 1709 SETi( sv_len(TOPs) );
79072805
LW
1710 RETURN;
1711}
1712
1713PP(pp_substr)
1714{
4e35701f 1715 djSP; dTARGET;
79072805
LW
1716 SV *sv;
1717 I32 len;
463ee0b2 1718 STRLEN curlen;
79072805
LW
1719 I32 pos;
1720 I32 rem;
84902520 1721 I32 fail;
a0d0e21e 1722 I32 lvalue = op->op_flags & OPf_MOD;
79072805 1723 char *tmps;
a0d0e21e 1724 I32 arybase = curcop->cop_arybase;
79072805
LW
1725
1726 if (MAXARG > 2)
1727 len = POPi;
84902520 1728 pos = POPi;
79072805 1729 sv = POPs;
a0d0e21e 1730 tmps = SvPV(sv, curlen);
84902520
TB
1731 if (pos >= arybase) {
1732 pos -= arybase;
1733 rem = curlen-pos;
1734 fail = rem;
1735 if (MAXARG > 2) {
1736 if (len < 0) {
1737 rem += len;
1738 if (rem < 0)
1739 rem = 0;
1740 }
1741 else if (rem > len)
1742 rem = len;
1743 }
68dc0745 1744 }
84902520
TB
1745 else {
1746 pos += curlen;
1747 if (MAXARG < 3)
1748 rem = curlen;
1749 else if (len >= 0) {
1750 rem = pos+len;
1751 if (rem > (I32)curlen)
1752 rem = curlen;
1753 }
1754 else {
1755 rem = curlen+len;
1756 if (rem < pos)
1757 rem = pos;
1758 }
1759 if (pos < 0)
1760 pos = 0;
1761 fail = rem;
1762 rem -= pos;
1763 }
1764 if (fail < 0) {
1765 if (dowarn || lvalue)
2304df62
AD
1766 warn("substr outside of string");
1767 RETPUSHUNDEF;
1768 }
79072805 1769 else {
79072805 1770 tmps += pos;
79072805
LW
1771 sv_setpvn(TARG, tmps, rem);
1772 if (lvalue) { /* it's an lvalue! */
dedeecda 1773 if (!SvGMAGICAL(sv)) {
1774 if (SvROK(sv)) {
1775 SvPV_force(sv,na);
1776 if (dowarn)
1777 warn("Attempt to use reference as lvalue in substr");
1778 }
1779 if (SvOK(sv)) /* is it defined ? */
1780 (void)SvPOK_only(sv);
1781 else
1782 sv_setpvn(sv,"",0); /* avoid lexical reincarnation */
1783 }
5f05dabc 1784
a0d0e21e
LW
1785 if (SvTYPE(TARG) < SVt_PVLV) {
1786 sv_upgrade(TARG, SVt_PVLV);
1787 sv_magic(TARG, Nullsv, 'x', Nullch, 0);
ed6116ce 1788 }
a0d0e21e 1789
5f05dabc 1790 LvTYPE(TARG) = 'x';
79072805 1791 LvTARG(TARG) = sv;
a0d0e21e 1792 LvTARGOFF(TARG) = pos;
79072805
LW
1793 LvTARGLEN(TARG) = rem;
1794 }
1795 }
1796 PUSHs(TARG); /* avoid SvSETMAGIC here */
1797 RETURN;
1798}
1799
1800PP(pp_vec)
1801{
4e35701f 1802 djSP; dTARGET;
79072805
LW
1803 register I32 size = POPi;
1804 register I32 offset = POPi;
1805 register SV *src = POPs;
a0d0e21e 1806 I32 lvalue = op->op_flags & OPf_MOD;
463ee0b2
LW
1807 STRLEN srclen;
1808 unsigned char *s = (unsigned char*)SvPV(src, srclen);
79072805
LW
1809 unsigned long retnum;
1810 I32 len;
1811
1812 offset *= size; /* turn into bit offset */
1813 len = (offset + size + 7) / 8;
1814 if (offset < 0 || size < 1)
1815 retnum = 0;
79072805 1816 else {
a0d0e21e
LW
1817 if (lvalue) { /* it's an lvalue! */
1818 if (SvTYPE(TARG) < SVt_PVLV) {
1819 sv_upgrade(TARG, SVt_PVLV);
1820 sv_magic(TARG, Nullsv, 'v', Nullch, 0);
1821 }
1822
1823 LvTYPE(TARG) = 'v';
1824 LvTARG(TARG) = src;
1825 LvTARGOFF(TARG) = offset;
1826 LvTARGLEN(TARG) = size;
1827 }
93a17b20 1828 if (len > srclen) {
a0d0e21e
LW
1829 if (size <= 8)
1830 retnum = 0;
1831 else {
1832 offset >>= 3;
748a9306
LW
1833 if (size == 16) {
1834 if (offset >= srclen)
1835 retnum = 0;
a0d0e21e 1836 else
748a9306
LW
1837 retnum = (unsigned long) s[offset] << 8;
1838 }
1839 else if (size == 32) {
1840 if (offset >= srclen)
1841 retnum = 0;
1842 else if (offset + 1 >= srclen)
a0d0e21e 1843 retnum = (unsigned long) s[offset] << 24;
748a9306
LW
1844 else if (offset + 2 >= srclen)
1845 retnum = ((unsigned long) s[offset] << 24) +
1846 ((unsigned long) s[offset + 1] << 16);
1847 else
1848 retnum = ((unsigned long) s[offset] << 24) +
1849 ((unsigned long) s[offset + 1] << 16) +
1850 (s[offset + 2] << 8);
a0d0e21e
LW
1851 }
1852 }
79072805 1853 }
a0d0e21e 1854 else if (size < 8)
79072805
LW
1855 retnum = (s[offset >> 3] >> (offset & 7)) & ((1 << size) - 1);
1856 else {
1857 offset >>= 3;
1858 if (size == 8)
1859 retnum = s[offset];
1860 else if (size == 16)
1861 retnum = ((unsigned long) s[offset] << 8) + s[offset+1];
1862 else if (size == 32)
1863 retnum = ((unsigned long) s[offset] << 24) +
1864 ((unsigned long) s[offset + 1] << 16) +
1865 (s[offset + 2] << 8) + s[offset+3];
1866 }
79072805
LW
1867 }
1868
1e422769 1869 sv_setiv(TARG, (IV)retnum);
79072805
LW
1870 PUSHs(TARG);
1871 RETURN;
1872}
1873
1874PP(pp_index)
1875{
4e35701f 1876 djSP; dTARGET;
79072805
LW
1877 SV *big;
1878 SV *little;
1879 I32 offset;
1880 I32 retval;
1881 char *tmps;
1882 char *tmps2;
463ee0b2 1883 STRLEN biglen;
a0d0e21e 1884 I32 arybase = curcop->cop_arybase;
79072805
LW
1885
1886 if (MAXARG < 3)
1887 offset = 0;
1888 else
1889 offset = POPi - arybase;
1890 little = POPs;
1891 big = POPs;
463ee0b2 1892 tmps = SvPV(big, biglen);
79072805
LW
1893 if (offset < 0)
1894 offset = 0;
93a17b20
LW
1895 else if (offset > biglen)
1896 offset = biglen;
79072805 1897 if (!(tmps2 = fbm_instr((unsigned char*)tmps + offset,
93a17b20 1898 (unsigned char*)tmps + biglen, little)))
79072805
LW
1899 retval = -1 + arybase;
1900 else
1901 retval = tmps2 - tmps + arybase;
1902 PUSHi(retval);
1903 RETURN;
1904}
1905
1906PP(pp_rindex)
1907{
4e35701f 1908 djSP; dTARGET;
79072805
LW
1909 SV *big;
1910 SV *little;
463ee0b2
LW
1911 STRLEN blen;
1912 STRLEN llen;
79072805
LW
1913 SV *offstr;
1914 I32 offset;
1915 I32 retval;
1916 char *tmps;
1917 char *tmps2;
a0d0e21e 1918 I32 arybase = curcop->cop_arybase;
79072805 1919
a0d0e21e 1920 if (MAXARG >= 3)
79072805
LW
1921 offstr = POPs;
1922 little = POPs;
1923 big = POPs;
463ee0b2
LW
1924 tmps2 = SvPV(little, llen);
1925 tmps = SvPV(big, blen);
79072805 1926 if (MAXARG < 3)
463ee0b2 1927 offset = blen;
79072805 1928 else
463ee0b2 1929 offset = SvIV(offstr) - arybase + llen;
79072805
LW
1930 if (offset < 0)
1931 offset = 0;
463ee0b2
LW
1932 else if (offset > blen)
1933 offset = blen;
79072805 1934 if (!(tmps2 = rninstr(tmps, tmps + offset,
463ee0b2 1935 tmps2, tmps2 + llen)))
79072805
LW
1936 retval = -1 + arybase;
1937 else
1938 retval = tmps2 - tmps + arybase;
1939 PUSHi(retval);
1940 RETURN;
1941}
1942
1943PP(pp_sprintf)
1944{
4e35701f 1945 djSP; dMARK; dORIGMARK; dTARGET;
36477c24 1946#ifdef USE_LOCALE_NUMERIC
bbce6d69 1947 if (op->op_private & OPpLOCALE)
36477c24 1948 SET_NUMERIC_LOCAL();
bbce6d69 1949 else
36477c24 1950 SET_NUMERIC_STANDARD();
1951#endif
79072805 1952 do_sprintf(TARG, SP-MARK, MARK+1);
bbce6d69 1953 TAINT_IF(SvTAINTED(TARG));
79072805
LW
1954 SP = ORIGMARK;
1955 PUSHTARG;
1956 RETURN;
1957}
1958
79072805
LW
1959PP(pp_ord)
1960{
4e35701f 1961 djSP; dTARGET;
79072805
LW
1962 I32 value;
1963 char *tmps;
79072805 1964
79072805 1965#ifndef I286
a0d0e21e 1966 tmps = POPp;
79072805
LW
1967 value = (I32) (*tmps & 255);
1968#else
a0d0e21e
LW
1969 I32 anum;
1970 tmps = POPp;
79072805
LW
1971 anum = (I32) *tmps;
1972 value = (I32) (anum & 255);
1973#endif
1974 XPUSHi(value);
1975 RETURN;
1976}
1977
463ee0b2
LW
1978PP(pp_chr)
1979{
4e35701f 1980 djSP; dTARGET;
463ee0b2
LW
1981 char *tmps;
1982
748a9306
LW
1983 (void)SvUPGRADE(TARG,SVt_PV);
1984 SvGROW(TARG,2);
463ee0b2
LW
1985 SvCUR_set(TARG, 1);
1986 tmps = SvPVX(TARG);
748a9306
LW
1987 *tmps++ = POPi;
1988 *tmps = '\0';
a0d0e21e 1989 (void)SvPOK_only(TARG);
463ee0b2
LW
1990 XPUSHs(TARG);
1991 RETURN;
1992}
1993
79072805
LW
1994PP(pp_crypt)
1995{
4e35701f 1996 djSP; dTARGET; dPOPTOPssrl;
79072805 1997#ifdef HAS_CRYPT
a0d0e21e 1998 char *tmps = SvPV(left, na);
79072805 1999#ifdef FCRYPT
a0d0e21e 2000 sv_setpv(TARG, fcrypt(tmps, SvPV(right, na)));
79072805 2001#else
a0d0e21e 2002 sv_setpv(TARG, crypt(tmps, SvPV(right, na)));
79072805
LW
2003#endif
2004#else
2005 DIE(
2006 "The crypt() function is unimplemented due to excessive paranoia.");
2007#endif
2008 SETs(TARG);
2009 RETURN;
2010}
2011
2012PP(pp_ucfirst)
2013{
4e35701f 2014 djSP;
79072805
LW
2015 SV *sv = TOPs;
2016 register char *s;
2017
ed6116ce 2018 if (!SvPADTMP(sv)) {
79072805
LW
2019 dTARGET;
2020 sv_setsv(TARG, sv);
2021 sv = TARG;
2022 SETs(sv);
2023 }
a0d0e21e 2024 s = SvPV_force(sv, na);
bbce6d69 2025 if (*s) {
2026 if (op->op_private & OPpLOCALE) {
2027 TAINT;
2028 SvTAINTED_on(sv);
2029 *s = toUPPER_LC(*s);
2030 }
2031 else
2032 *s = toUPPER(*s);
2033 }
79072805
LW
2034
2035 RETURN;
2036}
2037
2038PP(pp_lcfirst)
2039{
4e35701f 2040 djSP;
79072805
LW
2041 SV *sv = TOPs;
2042 register char *s;
2043
ed6116ce 2044 if (!SvPADTMP(sv)) {
79072805
LW
2045 dTARGET;
2046 sv_setsv(TARG, sv);
2047 sv = TARG;
2048 SETs(sv);
2049 }
a0d0e21e 2050 s = SvPV_force(sv, na);
bbce6d69 2051 if (*s) {
2052 if (op->op_private & OPpLOCALE) {
2053 TAINT;
2054 SvTAINTED_on(sv);
2055 *s = toLOWER_LC(*s);
2056 }
2057 else
2058 *s = toLOWER(*s);
2059 }
79072805
LW
2060
2061 SETs(sv);
2062 RETURN;
2063}
2064
2065PP(pp_uc)
2066{
4e35701f 2067 djSP;
79072805
LW
2068 SV *sv = TOPs;
2069 register char *s;
463ee0b2 2070 STRLEN len;
79072805 2071
ed6116ce 2072 if (!SvPADTMP(sv)) {
79072805
LW
2073 dTARGET;
2074 sv_setsv(TARG, sv);
2075 sv = TARG;
2076 SETs(sv);
2077 }
bbce6d69 2078
a0d0e21e 2079 s = SvPV_force(sv, len);
bbce6d69 2080 if (len) {
2081 register char *send = s + len;
2082
2083 if (op->op_private & OPpLOCALE) {
2084 TAINT;
2085 SvTAINTED_on(sv);
2086 for (; s < send; s++)
2087 *s = toUPPER_LC(*s);
2088 }
2089 else {
2090 for (; s < send; s++)
2091 *s = toUPPER(*s);
2092 }
79072805
LW
2093 }
2094 RETURN;
2095}
2096
2097PP(pp_lc)
2098{
4e35701f 2099 djSP;
79072805
LW
2100 SV *sv = TOPs;
2101 register char *s;
463ee0b2 2102 STRLEN len;
79072805 2103
ed6116ce 2104 if (!SvPADTMP(sv)) {
79072805
LW
2105 dTARGET;
2106 sv_setsv(TARG, sv);
2107 sv = TARG;
2108 SETs(sv);
2109 }
bbce6d69 2110
a0d0e21e 2111 s = SvPV_force(sv, len);
bbce6d69 2112 if (len) {
2113 register char *send = s + len;
2114
2115 if (op->op_private & OPpLOCALE) {
2116 TAINT;
2117 SvTAINTED_on(sv);
2118 for (; s < send; s++)
2119 *s = toLOWER_LC(*s);
2120 }
2121 else {
2122 for (; s < send; s++)
2123 *s = toLOWER(*s);
2124 }
79072805
LW
2125 }
2126 RETURN;
2127}
2128
a0d0e21e 2129PP(pp_quotemeta)
79072805 2130{
4e35701f 2131 djSP; dTARGET;
a0d0e21e
LW
2132 SV *sv = TOPs;
2133 STRLEN len;
2134 register char *s = SvPV(sv,len);
2135 register char *d;
79072805 2136
a0d0e21e
LW
2137 if (len) {
2138 (void)SvUPGRADE(TARG, SVt_PV);
c07a80fd 2139 SvGROW(TARG, (len * 2) + 1);
a0d0e21e
LW
2140 d = SvPVX(TARG);
2141 while (len--) {
2142 if (!isALNUM(*s))
2143 *d++ = '\\';
2144 *d++ = *s++;
79072805 2145 }
a0d0e21e
LW
2146 *d = '\0';
2147 SvCUR_set(TARG, d - SvPVX(TARG));
2148 (void)SvPOK_only(TARG);
79072805 2149 }
a0d0e21e
LW
2150 else
2151 sv_setpvn(TARG, s, len);
2152 SETs(TARG);
79072805
LW
2153 RETURN;
2154}
2155
a0d0e21e 2156/* Arrays. */
79072805 2157
a0d0e21e 2158PP(pp_aslice)
79072805 2159{
4e35701f 2160 djSP; dMARK; dORIGMARK;
a0d0e21e
LW
2161 register SV** svp;
2162 register AV* av = (AV*)POPs;
2163 register I32 lval = op->op_flags & OPf_MOD;
748a9306
LW
2164 I32 arybase = curcop->cop_arybase;
2165 I32 elem;
79072805 2166
a0d0e21e 2167 if (SvTYPE(av) == SVt_PVAV) {
748a9306
LW
2168 if (lval && op->op_private & OPpLVAL_INTRO) {
2169 I32 max = -1;
2170 for (svp = mark + 1; svp <= sp; svp++) {
2171 elem = SvIVx(*svp);
2172 if (elem > max)
2173 max = elem;
2174 }
2175 if (max > AvMAX(av))
2176 av_extend(av, max);
2177 }
a0d0e21e 2178 while (++MARK <= SP) {
748a9306 2179 elem = SvIVx(*MARK);
a0d0e21e 2180
748a9306
LW
2181 if (elem > 0)
2182 elem -= arybase;
a0d0e21e
LW
2183 svp = av_fetch(av, elem, lval);
2184 if (lval) {
2185 if (!svp || *svp == &sv_undef)
2186 DIE(no_aelem, elem);
2187 if (op->op_private & OPpLVAL_INTRO)
2188 save_svref(svp);
79072805 2189 }
a0d0e21e 2190 *MARK = svp ? *svp : &sv_undef;
79072805
LW
2191 }
2192 }
748a9306 2193 if (GIMME != G_ARRAY) {
a0d0e21e
LW
2194 MARK = ORIGMARK;
2195 *++MARK = *SP;
2196 SP = MARK;
2197 }
79072805
LW
2198 RETURN;
2199}
2200
2201/* Associative arrays. */
2202
2203PP(pp_each)
2204{
4e35701f 2205 djSP; dTARGET;
79072805 2206 HV *hash = (HV*)POPs;
c07a80fd 2207 HE *entry;
54310121 2208 I32 gimme = GIMME_V;
c750a3ec 2209 I32 realhv = (SvTYPE(hash) == SVt_PVHV);
c07a80fd 2210
2211 PUTBACK;
c750a3ec
MB
2212 /* might clobber stack_sp */
2213 entry = realhv ? hv_iternext(hash) : avhv_iternext((AV*)hash);
c07a80fd 2214 SPAGAIN;
79072805 2215
79072805
LW
2216 EXTEND(SP, 2);
2217 if (entry) {
54310121 2218 PUSHs(hv_iterkeysv(entry)); /* won't clobber stack_sp */
2219 if (gimme == G_ARRAY) {
c07a80fd 2220 PUTBACK;
c750a3ec
MB
2221 /* might clobber stack_sp */
2222 sv_setsv(TARG, realhv ?
2223 hv_iterval(hash, entry) : avhv_iterval((AV*)hash, entry));
c07a80fd 2224 SPAGAIN;
8990e307 2225 PUSHs(TARG);
79072805 2226 }
79072805 2227 }
54310121 2228 else if (gimme == G_SCALAR)
79072805
LW
2229 RETPUSHUNDEF;
2230
2231 RETURN;
2232}
2233
2234PP(pp_values)
2235{
2236 return do_kv(ARGS);
2237}
2238
2239PP(pp_keys)
2240{
2241 return do_kv(ARGS);
2242}
2243
2244PP(pp_delete)
2245{
4e35701f 2246 djSP;
54310121 2247 I32 gimme = GIMME_V;
2248 I32 discard = (gimme == G_VOID) ? G_DISCARD : 0;
79072805 2249 SV *sv;
5f05dabc 2250 HV *hv;
2251
2252 if (op->op_private & OPpSLICE) {
2253 dMARK; dORIGMARK;
97fcbf96 2254 U32 hvtype;
5f05dabc 2255 hv = (HV*)POPs;
97fcbf96 2256 hvtype = SvTYPE(hv);
5f05dabc 2257 while (++MARK <= SP) {
ae77835f
MB
2258 if (hvtype == SVt_PVHV)
2259 sv = hv_delete_ent(hv, *MARK, discard, 0);
2260 else if (hvtype == SVt_PVAV)
2261 sv = avhv_delete_ent((AV*)hv, *MARK, discard, 0);
2262 else
2263 DIE("Not a HASH reference");
5f05dabc 2264 *MARK = sv ? sv : &sv_undef;
2265 }
54310121 2266 if (discard)
2267 SP = ORIGMARK;
2268 else if (gimme == G_SCALAR) {
5f05dabc 2269 MARK = ORIGMARK;
2270 *++MARK = *SP;
2271 SP = MARK;
2272 }
2273 }
2274 else {
2275 SV *keysv = POPs;
2276 hv = (HV*)POPs;
97fcbf96
MB
2277 if (SvTYPE(hv) == SVt_PVHV)
2278 sv = hv_delete_ent(hv, keysv, discard, 0);
2279 else if (SvTYPE(hv) == SVt_PVAV)
2280 sv = avhv_delete_ent((AV*)hv, keysv, discard, 0);
2281 else
5f05dabc 2282 DIE("Not a HASH reference");
5f05dabc 2283 if (!sv)
2284 sv = &sv_undef;
54310121 2285 if (!discard)
2286 PUSHs(sv);
79072805 2287 }
79072805
LW
2288 RETURN;
2289}
2290
a0d0e21e 2291PP(pp_exists)
79072805 2292{
4e35701f 2293 djSP;
a0d0e21e
LW
2294 SV *tmpsv = POPs;
2295 HV *hv = (HV*)POPs;
c750a3ec 2296 if (SvTYPE(hv) == SVt_PVHV) {
ae77835f 2297 if (hv_exists_ent(hv, tmpsv, 0))
c750a3ec
MB
2298 RETPUSHYES;
2299 } else if (SvTYPE(hv) == SVt_PVAV) {
ae77835f 2300 if (avhv_exists_ent((AV*)hv, tmpsv, 0))
c750a3ec
MB
2301 RETPUSHYES;
2302 } else {
a0d0e21e
LW
2303 DIE("Not a HASH reference");
2304 }
a0d0e21e
LW
2305 RETPUSHNO;
2306}
79072805 2307
a0d0e21e
LW
2308PP(pp_hslice)
2309{
4e35701f 2310 djSP; dMARK; dORIGMARK;
f12c7020 2311 register HE *he;
a0d0e21e
LW
2312 register HV *hv = (HV*)POPs;
2313 register I32 lval = op->op_flags & OPf_MOD;
c750a3ec 2314 I32 realhv = (SvTYPE(hv) == SVt_PVHV);
79072805 2315
c750a3ec 2316 if (realhv || SvTYPE(hv) == SVt_PVAV) {
a0d0e21e 2317 while (++MARK <= SP) {
f12c7020 2318 SV *keysv = *MARK;
ae77835f
MB
2319 SV **svp;
2320 if (realhv) {
2321 he = hv_fetch_ent(hv, keysv, lval, 0);
2322 svp = he ? &HeVAL(he) : 0;
2323 } else {
97fcbf96 2324 svp = avhv_fetch_ent((AV*)hv, keysv, lval, 0);
ae77835f 2325 }
a0d0e21e 2326 if (lval) {
f12c7020 2327 if (!he || HeVAL(he) == &sv_undef)
2328 DIE(no_helem, SvPV(keysv, na));
a0d0e21e 2329 if (op->op_private & OPpLVAL_INTRO)
f12c7020 2330 save_svref(&HeVAL(he));
93a17b20 2331 }
f12c7020 2332 *MARK = he ? HeVAL(he) : &sv_undef;
79072805
LW
2333 }
2334 }
a0d0e21e
LW
2335 if (GIMME != G_ARRAY) {
2336 MARK = ORIGMARK;
2337 *++MARK = *SP;
2338 SP = MARK;
79072805 2339 }
a0d0e21e
LW
2340 RETURN;
2341}
2342
2343/* List operators. */
2344
2345PP(pp_list)
2346{
4e35701f 2347 djSP; dMARK;
a0d0e21e
LW
2348 if (GIMME != G_ARRAY) {
2349 if (++MARK <= SP)
2350 *MARK = *SP; /* unwanted list, return last item */
8990e307 2351 else
a0d0e21e
LW
2352 *MARK = &sv_undef;
2353 SP = MARK;
79072805 2354 }
a0d0e21e 2355 RETURN;
79072805
LW
2356}
2357
a0d0e21e 2358PP(pp_lslice)
79072805 2359{
4e35701f 2360 djSP;
a0d0e21e
LW
2361 SV **lastrelem = stack_sp;
2362 SV **lastlelem = stack_base + POPMARK;
2363 SV **firstlelem = stack_base + POPMARK + 1;
2364 register SV **firstrelem = lastlelem + 1;
2365 I32 arybase = curcop->cop_arybase;
4633a7c4
LW
2366 I32 lval = op->op_flags & OPf_MOD;
2367 I32 is_something_there = lval;
79072805 2368
a0d0e21e
LW
2369 register I32 max = lastrelem - lastlelem;
2370 register SV **lelem;
2371 register I32 ix;
2372
2373 if (GIMME != G_ARRAY) {
748a9306
LW
2374 ix = SvIVx(*lastlelem);
2375 if (ix < 0)
2376 ix += max;
2377 else
2378 ix -= arybase;
a0d0e21e
LW
2379 if (ix < 0 || ix >= max)
2380 *firstlelem = &sv_undef;
2381 else
2382 *firstlelem = firstrelem[ix];
2383 SP = firstlelem;
2384 RETURN;
2385 }
2386
2387 if (max == 0) {
2388 SP = firstlelem - 1;
2389 RETURN;
2390 }
2391
2392 for (lelem = firstlelem; lelem <= lastlelem; lelem++) {
748a9306 2393 ix = SvIVx(*lelem);
a0d0e21e
LW
2394 if (ix < 0) {
2395 ix += max;
2396 if (ix < 0)
2397 *lelem = &sv_undef;
2398 else if (!(*lelem = firstrelem[ix]))
2399 *lelem = &sv_undef;
79072805 2400 }
748a9306
LW
2401 else {
2402 ix -= arybase;
2403 if (ix >= max || !(*lelem = firstrelem[ix]))
2404 *lelem = &sv_undef;
2405 }
ff0cee69 2406 if (!is_something_there && (SvOK(*lelem) || SvGMAGICAL(*lelem)))
4633a7c4 2407 is_something_there = TRUE;
79072805 2408 }
4633a7c4
LW
2409 if (is_something_there)
2410 SP = lastlelem;
2411 else
2412 SP = firstlelem - 1;
79072805
LW
2413 RETURN;
2414}
2415
a0d0e21e
LW
2416PP(pp_anonlist)
2417{
4e35701f 2418 djSP; dMARK; dORIGMARK;
a0d0e21e 2419 I32 items = SP - MARK;
44a8e56a 2420 SV *av = sv_2mortal((SV*)av_make(items, MARK+1));
2421 SP = ORIGMARK; /* av_make() might realloc stack_sp */
2422 XPUSHs(av);
a0d0e21e
LW
2423 RETURN;
2424}
2425
2426PP(pp_anonhash)
79072805 2427{
4e35701f 2428 djSP; dMARK; dORIGMARK;
a0d0e21e
LW
2429 HV* hv = (HV*)sv_2mortal((SV*)newHV());
2430
2431 while (MARK < SP) {
2432 SV* key = *++MARK;
a0d0e21e
LW
2433 SV *val = NEWSV(46, 0);
2434 if (MARK < SP)
2435 sv_setsv(val, *++MARK);
fb73857a 2436 else if (dowarn)
a0d0e21e 2437 warn("Odd number of elements in hash list");
f12c7020 2438 (void)hv_store_ent(hv,key,val,0);
79072805 2439 }
a0d0e21e
LW
2440 SP = ORIGMARK;
2441 XPUSHs((SV*)hv);
79072805
LW
2442 RETURN;
2443}
2444
a0d0e21e 2445PP(pp_splice)
79072805 2446{
4e35701f 2447 djSP; dMARK; dORIGMARK;
a0d0e21e
LW
2448 register AV *ary = (AV*)*++MARK;
2449 register SV **src;
2450 register SV **dst;
2451 register I32 i;
2452 register I32 offset;
2453 register I32 length;
2454 I32 newlen;
2455 I32 after;
2456 I32 diff;
2457 SV **tmparyval = 0;
93965878
NIS
2458 MAGIC *mg;
2459
2460 if (SvRMAGICAL(ary) && (mg = mg_find((SV*)ary,'P'))) {
2461 *MARK-- = mg->mg_obj;
2462 PUSHMARK(MARK);
a60c0954
NIS
2463 PUTBACK;
2464 ENTER;
93965878 2465 perl_call_method("SPLICE",GIMME_V);
a60c0954 2466 LEAVE;
93965878
NIS
2467 SPAGAIN;
2468 RETURN;
2469 }
79072805 2470
a0d0e21e 2471 SP++;
79072805 2472
a0d0e21e 2473 if (++MARK < SP) {
84902520 2474 offset = i = SvIVx(*MARK);
a0d0e21e 2475 if (offset < 0)
93965878 2476 offset += AvFILLp(ary) + 1;
a0d0e21e
LW
2477 else
2478 offset -= curcop->cop_arybase;
84902520
TB
2479 if (offset < 0)
2480 DIE(no_aelem, i);
a0d0e21e
LW
2481 if (++MARK < SP) {
2482 length = SvIVx(*MARK++);
2483 if (length < 0)
2484 length = 0;
79072805
LW
2485 }
2486 else
a0d0e21e 2487 length = AvMAX(ary) + 1; /* close enough to infinity */
79072805 2488 }
a0d0e21e
LW
2489 else {
2490 offset = 0;
2491 length = AvMAX(ary) + 1;
2492 }
93965878
NIS
2493 if (offset > AvFILLp(ary) + 1)
2494 offset = AvFILLp(ary) + 1;
2495 after = AvFILLp(ary) + 1 - (offset + length);
a0d0e21e
LW
2496 if (after < 0) { /* not that much array */
2497 length += after; /* offset+length now in array */
2498 after = 0;
2499 if (!AvALLOC(ary))
2500 av_extend(ary, 0);
2501 }
2502
2503 /* At this point, MARK .. SP-1 is our new LIST */
2504
2505 newlen = SP - MARK;
2506 diff = newlen - length;
fb73857a 2507 if (newlen && !AvREAL(ary)) {
2508 if (AvREIFY(ary))
2509 av_reify(ary);
2510 else
2511 assert(AvREAL(ary)); /* would leak, so croak */
2512 }
a0d0e21e
LW
2513
2514 if (diff < 0) { /* shrinking the area */
2515 if (newlen) {
2516 New(451, tmparyval, newlen, SV*); /* so remember insertion */
2517 Copy(MARK, tmparyval, newlen, SV*);
79072805 2518 }
a0d0e21e
LW
2519
2520 MARK = ORIGMARK + 1;
2521 if (GIMME == G_ARRAY) { /* copy return vals to stack */
2522 MEXTEND(MARK, length);
2523 Copy(AvARRAY(ary)+offset, MARK, length, SV*);
2524 if (AvREAL(ary)) {
bbce6d69 2525 EXTEND_MORTAL(length);
36477c24 2526 for (i = length, dst = MARK; i; i--) {
2527 if (!SvIMMORTAL(*dst))
2528 sv_2mortal(*dst); /* free them eventualy */
2529 dst++;
2530 }
a0d0e21e
LW
2531 }
2532 MARK += length - 1;
79072805 2533 }
a0d0e21e
LW
2534 else {
2535 *MARK = AvARRAY(ary)[offset+length-1];
2536 if (AvREAL(ary)) {
36477c24 2537 if (!SvIMMORTAL(*MARK))
2538 sv_2mortal(*MARK);
a0d0e21e
LW
2539 for (i = length - 1, dst = &AvARRAY(ary)[offset]; i > 0; i--)
2540 SvREFCNT_dec(*dst++); /* free them now */
79072805 2541 }
a0d0e21e 2542 }
93965878 2543 AvFILLp(ary) += diff;
a0d0e21e
LW
2544
2545 /* pull up or down? */
2546
2547 if (offset < after) { /* easier to pull up */
2548 if (offset) { /* esp. if nothing to pull */
2549 src = &AvARRAY(ary)[offset-1];
2550 dst = src - diff; /* diff is negative */
2551 for (i = offset; i > 0; i--) /* can't trust Copy */
2552 *dst-- = *src--;
79072805 2553 }
a0d0e21e
LW
2554 dst = AvARRAY(ary);
2555 SvPVX(ary) = (char*)(AvARRAY(ary) - diff); /* diff is negative */
2556 AvMAX(ary) += diff;
2557 }
2558 else {
2559 if (after) { /* anything to pull down? */
2560 src = AvARRAY(ary) + offset + length;
2561 dst = src + diff; /* diff is negative */
2562 Move(src, dst, after, SV*);
79072805 2563 }
93965878 2564 dst = &AvARRAY(ary)[AvFILLp(ary)+1];
a0d0e21e
LW
2565 /* avoid later double free */
2566 }
2567 i = -diff;
2568 while (i)
2569 dst[--i] = &sv_undef;
2570
2571 if (newlen) {
2572 for (src = tmparyval, dst = AvARRAY(ary) + offset;
2573 newlen; newlen--) {
2574 *dst = NEWSV(46, 0);
2575 sv_setsv(*dst++, *src++);
79072805 2576 }
a0d0e21e
LW
2577 Safefree(tmparyval);
2578 }
2579 }
2580 else { /* no, expanding (or same) */
2581 if (length) {
2582 New(452, tmparyval, length, SV*); /* so remember deletion */
2583 Copy(AvARRAY(ary)+offset, tmparyval, length, SV*);
2584 }
2585
2586 if (diff > 0) { /* expanding */
2587
2588 /* push up or down? */
2589
2590 if (offset < after && diff <= AvARRAY(ary) - AvALLOC(ary)) {
2591 if (offset) {
2592 src = AvARRAY(ary);
2593 dst = src - diff;
2594 Move(src, dst, offset, SV*);
79072805 2595 }
a0d0e21e
LW
2596 SvPVX(ary) = (char*)(AvARRAY(ary) - diff);/* diff is positive */
2597 AvMAX(ary) += diff;
93965878 2598 AvFILLp(ary) += diff;
79072805
LW
2599 }
2600 else {
93965878
NIS
2601 if (AvFILLp(ary) + diff >= AvMAX(ary)) /* oh, well */
2602 av_extend(ary, AvFILLp(ary) + diff);
2603 AvFILLp(ary) += diff;
a0d0e21e
LW
2604
2605 if (after) {
93965878 2606 dst = AvARRAY(ary) + AvFILLp(ary);
a0d0e21e
LW
2607 src = dst - diff;
2608 for (i = after; i; i--) {
2609 *dst-- = *src--;
2610 }
79072805
LW
2611 }
2612 }
a0d0e21e
LW
2613 }
2614
2615 for (src = MARK, dst = AvARRAY(ary) + offset; newlen; newlen--) {
2616 *dst = NEWSV(46, 0);
2617 sv_setsv(*dst++, *src++);
2618 }
2619 MARK = ORIGMARK + 1;
2620 if (GIMME == G_ARRAY) { /* copy return vals to stack */
2621 if (length) {
2622 Copy(tmparyval, MARK, length, SV*);
2623 if (AvREAL(ary)) {
bbce6d69 2624 EXTEND_MORTAL(length);
36477c24 2625 for (i = length, dst = MARK; i; i--) {
2626 if (!SvIMMORTAL(*dst))
2627 sv_2mortal(*dst); /* free them eventualy */
2628 dst++;
2629 }
79072805 2630 }
a0d0e21e 2631 Safefree(tmparyval);
79072805 2632 }
a0d0e21e
LW
2633 MARK += length - 1;
2634 }
2635 else if (length--) {
2636 *MARK = tmparyval[length];
2637 if (AvREAL(ary)) {
36477c24 2638 if (!SvIMMORTAL(*MARK))
2639 sv_2mortal(*MARK);
a0d0e21e
LW
2640 while (length-- > 0)
2641 SvREFCNT_dec(tmparyval[length]);
79072805 2642 }
a0d0e21e 2643 Safefree(tmparyval);
79072805 2644 }
a0d0e21e
LW
2645 else
2646 *MARK = &sv_undef;
79072805 2647 }
a0d0e21e 2648 SP = MARK;
79072805
LW
2649 RETURN;
2650}
2651
a0d0e21e 2652PP(pp_push)
79072805 2653{
4e35701f 2654 djSP; dMARK; dORIGMARK; dTARGET;
a0d0e21e 2655 register AV *ary = (AV*)*++MARK;
93965878
NIS
2656 register SV *sv = &sv_undef;
2657 MAGIC *mg;
2658
2659 if (SvRMAGICAL(ary) && (mg = mg_find((SV*)ary,'P'))) {
2660 *MARK-- = mg->mg_obj;
2661 PUSHMARK(MARK);
2662 PUTBACK;
a60c0954
NIS
2663 ENTER;
2664 perl_call_method("PUSH",G_SCALAR|G_DISCARD);
2665 LEAVE;
93965878 2666 SPAGAIN;
93965878 2667 }
a60c0954
NIS
2668 else {
2669 /* Why no pre-extend of ary here ? */
2670 for (++MARK; MARK <= SP; MARK++) {
2671 sv = NEWSV(51, 0);
2672 if (*MARK)
2673 sv_setsv(sv, *MARK);
2674 av_push(ary, sv);
2675 }
79072805
LW
2676 }
2677 SP = ORIGMARK;
a0d0e21e 2678 PUSHi( AvFILL(ary) + 1 );
79072805
LW
2679 RETURN;
2680}
2681
a0d0e21e 2682PP(pp_pop)
79072805 2683{
4e35701f 2684 djSP;
a0d0e21e
LW
2685 AV *av = (AV*)POPs;
2686 SV *sv = av_pop(av);
36477c24 2687 if (!SvIMMORTAL(sv) && AvREAL(av))
a0d0e21e
LW
2688 (void)sv_2mortal(sv);
2689 PUSHs(sv);
79072805 2690 RETURN;
79072805
LW
2691}
2692
a0d0e21e 2693PP(pp_shift)
79072805 2694{
4e35701f 2695 djSP;
a0d0e21e
LW
2696 AV *av = (AV*)POPs;
2697 SV *sv = av_shift(av);
79072805 2698 EXTEND(SP, 1);
a0d0e21e 2699 if (!sv)
79072805 2700 RETPUSHUNDEF;
36477c24 2701 if (!SvIMMORTAL(sv) && AvREAL(av))
a0d0e21e
LW
2702 (void)sv_2mortal(sv);
2703 PUSHs(sv);
79072805 2704 RETURN;
79072805
LW
2705}
2706
a0d0e21e 2707PP(pp_unshift)
79072805 2708{
4e35701f 2709 djSP; dMARK; dORIGMARK; dTARGET;
a0d0e21e
LW
2710 register AV *ary = (AV*)*++MARK;
2711 register SV *sv;
2712 register I32 i = 0;
93965878
NIS
2713 MAGIC *mg;
2714
a60c0954
NIS
2715 if (SvRMAGICAL(ary) && (mg = mg_find((SV*)ary,'P'))) {
2716
2717
93965878 2718 *MARK-- = mg->mg_obj;
93965878 2719 PUTBACK;
a60c0954
NIS
2720 ENTER;
2721 perl_call_method("UNSHIFT",G_SCALAR|G_DISCARD);
2722 LEAVE;
93965878 2723 SPAGAIN;
93965878 2724 }
a60c0954
NIS
2725 else {
2726 av_unshift(ary, SP - MARK);
2727 while (MARK < SP) {
2728 sv = NEWSV(27, 0);
2729 sv_setsv(sv, *++MARK);
2730 (void)av_store(ary, i++, sv);
2731 }
79072805 2732 }
a0d0e21e
LW
2733 SP = ORIGMARK;
2734 PUSHi( AvFILL(ary) + 1 );
79072805 2735 RETURN;
79072805
LW
2736}
2737
a0d0e21e 2738PP(pp_reverse)
79072805 2739{
4e35701f 2740 djSP; dMARK;
a0d0e21e
LW
2741 register SV *tmp;
2742 SV **oldsp = SP;
79072805 2743
a0d0e21e
LW
2744 if (GIMME == G_ARRAY) {
2745 MARK++;
2746 while (MARK < SP) {
2747 tmp = *MARK;
2748 *MARK++ = *SP;
2749 *SP-- = tmp;
2750 }
2751 SP = oldsp;
79072805
LW
2752 }
2753 else {
a0d0e21e
LW
2754 register char *up;
2755 register char *down;
2756 register I32 tmp;
2757 dTARGET;
2758 STRLEN len;
79072805 2759
a0d0e21e
LW
2760 if (SP - MARK > 1)
2761 do_join(TARG, &sv_no, MARK, SP);
2762 else
54b9620d 2763 sv_setsv(TARG, (SP > MARK) ? *SP : DEFSV);
a0d0e21e
LW
2764 up = SvPV_force(TARG, len);
2765 if (len > 1) {
2766 down = SvPVX(TARG) + len - 1;
2767 while (down > up) {
2768 tmp = *up;
2769 *up++ = *down;
2770 *down-- = tmp;
2771 }
2772 (void)SvPOK_only(TARG);
79072805 2773 }
a0d0e21e
LW
2774 SP = MARK + 1;
2775 SETTARG;
79072805 2776 }
a0d0e21e 2777 RETURN;
79072805
LW
2778}
2779
55497cff 2780static SV *
8ac85365 2781mul128(SV *sv, U8 m)
55497cff 2782{
2783 STRLEN len;
2784 char *s = SvPV(sv, len);
2785 char *t;
2786 U32 i = 0;
2787
2788 if (!strnEQ(s, "0000", 4)) { /* need to grow sv */
8ac85365 2789 SV *New = newSVpv("0000000000", 10);
55497cff 2790
8ac85365 2791 sv_catsv(New, sv);
55497cff 2792 SvREFCNT_dec(sv); /* free old sv */
8ac85365 2793 sv = New;
55497cff 2794 s = SvPV(sv, len);
2795 }
2796 t = s + len - 1;
2797 while (!*t) /* trailing '\0'? */
2798 t--;
2799 while (t > s) {
2800 i = ((*t - '0') << 7) + m;
2801 *(t--) = '0' + (i % 10);
2802 m = i / 10;
2803 }
2804 return (sv);
2805}
2806
a0d0e21e
LW
2807/* Explosives and implosives. */
2808
2809PP(pp_unpack)
79072805 2810{
4e35701f 2811 djSP;
a0d0e21e 2812 dPOPPOPssrl;
36477c24 2813 SV **oldsp = sp;
54310121 2814 I32 gimme = GIMME_V;
ed6116ce 2815 SV *sv;
a0d0e21e
LW
2816 STRLEN llen;
2817 STRLEN rlen;
2818 register char *pat = SvPV(left, llen);
2819 register char *s = SvPV(right, rlen);
2820 char *strend = s + rlen;
2821 char *strbeg = s;
2822 register char *patend = pat + llen;
2823 I32 datumtype;
2824 register I32 len;
2825 register I32 bits;
79072805 2826
a0d0e21e
LW
2827 /* These must not be in registers: */
2828 I16 ashort;
2829 int aint;
2830 I32 along;
ecfc5424
AD
2831#ifdef HAS_QUAD
2832 Quad_t aquad;
a0d0e21e
LW
2833#endif
2834 U16 aushort;
2835 unsigned int auint;
2836 U32 aulong;
ecfc5424
AD
2837#ifdef HAS_QUAD
2838 unsigned Quad_t auquad;
a0d0e21e
LW
2839#endif
2840 char *aptr;
2841 float afloat;
2842 double adouble;
2843 I32 checksum = 0;
2844 register U32 culong;
2845 double cdouble;
2846 static char* bitcount = 0;
fb73857a 2847 int commas = 0;
79072805 2848
54310121 2849 if (gimme != G_ARRAY) { /* arrange to do first one only */
a0d0e21e
LW
2850 /*SUPPRESS 530*/
2851 for (patend = pat; !isALPHA(*patend) || *patend == 'x'; patend++) ;
748a9306 2852 if (strchr("aAbBhHP", *patend) || *pat == '%') {
a0d0e21e
LW
2853 patend++;
2854 while (isDIGIT(*patend) || *patend == '*')
2855 patend++;
2856 }
2857 else
2858 patend++;
79072805 2859 }
a0d0e21e
LW
2860 while (pat < patend) {
2861 reparse:
bbdab043
CS
2862 datumtype = *pat++ & 0xFF;
2863 if (isSPACE(datumtype))
2864 continue;
a0d0e21e
LW
2865 if (pat >= patend)
2866 len = 1;
2867 else if (*pat == '*') {
2868 len = strend - strbeg; /* long enough */
2869 pat++;
2870 }
2871 else if (isDIGIT(*pat)) {
2872 len = *pat++ - '0';
2873 while (isDIGIT(*pat))
2874 len = (len * 10) + (*pat++ - '0');
2875 }
2876 else
2877 len = (datumtype != '@');
2878 switch(datumtype) {
2879 default:
bbdab043 2880 croak("Invalid type in unpack: '%c'", (int)datumtype);
fb73857a 2881 case ',': /* grandfather in commas but with a warning */
2882 if (commas++ == 0 && dowarn)
2883 warn("Invalid type in unpack: '%c'", (int)datumtype);
2884 break;
a0d0e21e
LW
2885 case '%':
2886 if (len == 1 && pat[-1] != '1')
2887 len = 16;
2888 checksum = len;
2889 culong = 0;
2890 cdouble = 0;
2891 if (pat < patend)
2892 goto reparse;
2893 break;
2894 case '@':
2895 if (len > strend - strbeg)
2896 DIE("@ outside of string");
2897 s = strbeg + len;
2898 break;
2899 case 'X':
2900 if (len > s - strbeg)
2901 DIE("X outside of string");
2902 s -= len;
2903 break;
2904 case 'x':
2905 if (len > strend - s)
2906 DIE("x outside of string");
2907 s += len;
2908 break;
2909 case 'A':
2910 case 'a':
2911 if (len > strend - s)
2912 len = strend - s;
2913 if (checksum)
2914 goto uchar_checksum;
2915 sv = NEWSV(35, len);
2916 sv_setpvn(sv, s, len);
2917 s += len;
2918 if (datumtype == 'A') {
2919 aptr = s; /* borrow register */
2920 s = SvPVX(sv) + len - 1;
2921 while (s >= SvPVX(sv) && (!*s || isSPACE(*s)))
2922 s--;
2923 *++s = '\0';
2924 SvCUR_set(sv, s - SvPVX(sv));
2925 s = aptr; /* unborrow register */
2926 }
2927 XPUSHs(sv_2mortal(sv));
2928 break;
2929 case 'B':
2930 case 'b':
2931 if (pat[-1] == '*' || len > (strend - s) * 8)
2932 len = (strend - s) * 8;
2933 if (checksum) {
2934 if (!bitcount) {
2935 Newz(601, bitcount, 256, char);
2936 for (bits = 1; bits < 256; bits++) {
2937 if (bits & 1) bitcount[bits]++;
2938 if (bits & 2) bitcount[bits]++;
2939 if (bits & 4) bitcount[bits]++;
2940 if (bits & 8) bitcount[bits]++;
2941 if (bits & 16) bitcount[bits]++;
2942 if (bits & 32) bitcount[bits]++;
2943 if (bits & 64) bitcount[bits]++;
2944 if (bits & 128) bitcount[bits]++;
2945 }
2946 }
2947 while (len >= 8) {
2948 culong += bitcount[*(unsigned char*)s++];
2949 len -= 8;
2950 }
2951 if (len) {
2952 bits = *s;
2953 if (datumtype == 'b') {
2954 while (len-- > 0) {
2955 if (bits & 1) culong++;
2956 bits >>= 1;
2957 }
2958 }
2959 else {
2960 while (len-- > 0) {
2961 if (bits & 128) culong++;
2962 bits <<= 1;
2963 }
2964 }
2965 }
79072805
LW
2966 break;
2967 }
a0d0e21e
LW
2968 sv = NEWSV(35, len + 1);
2969 SvCUR_set(sv, len);
2970 SvPOK_on(sv);
2971 aptr = pat; /* borrow register */
2972 pat = SvPVX(sv);
2973 if (datumtype == 'b') {
2974 aint = len;
2975 for (len = 0; len < aint; len++) {
2976 if (len & 7) /*SUPPRESS 595*/
2977 bits >>= 1;
2978 else
2979 bits = *s++;
2980 *pat++ = '0' + (bits & 1);
2981 }
2982 }
2983 else {
2984 aint = len;
2985 for (len = 0; len < aint; len++) {
2986 if (len & 7)
2987 bits <<= 1;
2988 else
2989 bits = *s++;
2990 *pat++ = '0' + ((bits & 128) != 0);
2991 }
2992 }
2993 *pat = '\0';
2994 pat = aptr; /* unborrow register */
2995 XPUSHs(sv_2mortal(sv));
2996 break;
2997 case 'H':
2998 case 'h':
2999 if (pat[-1] == '*' || len > (strend - s) * 2)
3000 len = (strend - s) * 2;
3001 sv = NEWSV(35, len + 1);
3002 SvCUR_set(sv, len);
3003 SvPOK_on(sv);
3004 aptr = pat; /* borrow register */
3005 pat = SvPVX(sv);
3006 if (datumtype == 'h') {
3007 aint = len;
3008 for (len = 0; len < aint; len++) {
3009 if (len & 1)
3010 bits >>= 4;
3011 else
3012 bits = *s++;
3013 *pat++ = hexdigit[bits & 15];
3014 }
3015 }
3016 else {
3017 aint = len;
3018 for (len = 0; len < aint; len++) {
3019 if (len & 1)
3020 bits <<= 4;
3021 else
3022 bits = *s++;
3023 *pat++ = hexdigit[(bits >> 4) & 15];
3024 }
3025 }
3026 *pat = '\0';
3027 pat = aptr; /* unborrow register */
3028 XPUSHs(sv_2mortal(sv));
3029 break;
3030 case 'c':
3031 if (len > strend - s)
3032 len = strend - s;
3033 if (checksum) {
3034 while (len-- > 0) {
3035 aint = *s++;
3036 if (aint >= 128) /* fake up signed chars */
3037 aint -= 256;
3038 culong += aint;
3039 }
3040 }
3041 else {
3042 EXTEND(SP, len);
bbce6d69 3043 EXTEND_MORTAL(len);
a0d0e21e
LW
3044 while (len-- > 0) {
3045 aint = *s++;
3046 if (aint >= 128) /* fake up signed chars */
3047 aint -= 256;
3048 sv = NEWSV(36, 0);
1e422769 3049 sv_setiv(sv, (IV)aint);
a0d0e21e
LW
3050 PUSHs(sv_2mortal(sv));
3051 }
3052 }
3053 break;
3054 case 'C':
3055 if (len > strend - s)
3056 len = strend - s;
3057 if (checksum) {
3058 uchar_checksum:
3059 while (len-- > 0) {
3060 auint = *s++ & 255;
3061 culong += auint;
3062 }
3063 }
3064 else {
3065 EXTEND(SP, len);
bbce6d69 3066 EXTEND_MORTAL(len);
a0d0e21e
LW
3067 while (len-- > 0) {
3068 auint = *s++ & 255;
3069 sv = NEWSV(37, 0);
1e422769 3070 sv_setiv(sv, (IV)auint);
a0d0e21e
LW
3071 PUSHs(sv_2mortal(sv));
3072 }
3073 }
3074 break;
3075 case 's':
96e4d5b1 3076 along = (strend - s) / SIZE16;
a0d0e21e
LW
3077 if (len > along)
3078 len = along;
3079 if (checksum) {
3080 while (len-- > 0) {
96e4d5b1 3081 COPY16(s, &ashort);
3082 s += SIZE16;
a0d0e21e
LW
3083 culong += ashort;
3084 }
3085 }
3086 else {
3087 EXTEND(SP, len);
bbce6d69 3088 EXTEND_MORTAL(len);
a0d0e21e 3089 while (len-- > 0) {
96e4d5b1 3090 COPY16(s, &ashort);
3091 s += SIZE16;
a0d0e21e 3092 sv = NEWSV(38, 0);
1e422769 3093 sv_setiv(sv, (IV)ashort);
a0d0e21e
LW
3094 PUSHs(sv_2mortal(sv));
3095 }
3096 }
3097 break;
3098 case 'v':
3099 case 'n':
3100 case 'S':
96e4d5b1 3101 along = (strend - s) / SIZE16;
a0d0e21e
LW
3102 if (len > along)
3103 len = along;
3104 if (checksum) {
3105 while (len-- > 0) {
96e4d5b1 3106 COPY16(s, &aushort);
3107 s += SIZE16;
a0d0e21e
LW
3108#ifdef HAS_NTOHS
3109 if (datumtype == 'n')
3110 aushort = ntohs(aushort);
79072805 3111#endif
a0d0e21e
LW
3112#ifdef HAS_VTOHS
3113 if (datumtype == 'v')
3114 aushort = vtohs(aushort);
79072805 3115#endif
a0d0e21e
LW
3116 culong += aushort;
3117 }
3118 }
3119 else {
3120 EXTEND(SP, len);
bbce6d69 3121 EXTEND_MORTAL(len);
a0d0e21e 3122 while (len-- > 0) {
96e4d5b1 3123 COPY16(s, &aushort);
3124 s += SIZE16;
a0d0e21e
LW
3125 sv = NEWSV(39, 0);
3126#ifdef HAS_NTOHS
3127 if (datumtype == 'n')
3128 aushort = ntohs(aushort);
79072805 3129#endif
a0d0e21e
LW
3130#ifdef HAS_VTOHS
3131 if (datumtype == 'v')
3132 aushort = vtohs(aushort);
79072805 3133#endif
1e422769 3134 sv_setiv(sv, (IV)aushort);
a0d0e21e
LW
3135 PUSHs(sv_2mortal(sv));
3136 }
3137 }
3138 break;
3139 case 'i':
3140 along = (strend - s) / sizeof(int);
3141 if (len > along)
3142 len = along;
3143 if (checksum) {
3144 while (len-- > 0) {
3145 Copy(s, &aint, 1, int);
3146 s += sizeof(int);
3147 if (checksum > 32)
3148 cdouble += (double)aint;
3149 else
3150 culong += aint;
3151 }
3152 }
3153 else {
3154 EXTEND(SP, len);
bbce6d69 3155 EXTEND_MORTAL(len);
a0d0e21e
LW
3156 while (len-- > 0) {
3157 Copy(s, &aint, 1, int);
3158 s += sizeof(int);
3159 sv = NEWSV(40, 0);
1e422769 3160 sv_setiv(sv, (IV)aint);
a0d0e21e
LW
3161 PUSHs(sv_2mortal(sv));
3162 }
3163 }
3164 break;
3165 case 'I':
3166 along = (strend - s) / sizeof(unsigned int);
3167 if (len > along)
3168 len = along;
3169 if (checksum) {
3170 while (len-- > 0) {
3171 Copy(s, &auint, 1, unsigned int);
3172 s += sizeof(unsigned int);
3173 if (checksum > 32)
3174 cdouble += (double)auint;
3175 else
3176 culong += auint;
3177 }
3178 }
3179 else {
3180 EXTEND(SP, len);
bbce6d69 3181 EXTEND_MORTAL(len);
a0d0e21e
LW
3182 while (len-- > 0) {
3183 Copy(s, &auint, 1, unsigned int);
3184 s += sizeof(unsigned int);
3185 sv = NEWSV(41, 0);
1e422769 3186 sv_setuv(sv, (UV)auint);
a0d0e21e
LW
3187 PUSHs(sv_2mortal(sv));
3188 }
3189 }
3190 break;
3191 case 'l':
96e4d5b1 3192 along = (strend - s) / SIZE32;
a0d0e21e
LW
3193 if (len > along)
3194 len = along;
3195 if (checksum) {
3196 while (len-- > 0) {
96e4d5b1 3197 COPY32(s, &along);
3198 s += SIZE32;
a0d0e21e
LW
3199 if (checksum > 32)
3200 cdouble += (double)along;
3201 else
3202 culong += along;
3203 }
3204 }
3205 else {
3206 EXTEND(SP, len);
bbce6d69 3207 EXTEND_MORTAL(len);
a0d0e21e 3208 while (len-- > 0) {
96e4d5b1 3209 COPY32(s, &along);
3210 s += SIZE32;
a0d0e21e 3211 sv = NEWSV(42, 0);
1e422769 3212 sv_setiv(sv, (IV)along);
a0d0e21e
LW
3213 PUSHs(sv_2mortal(sv));
3214 }
79072805 3215 }
a0d0e21e
LW
3216 break;
3217 case 'V':
3218 case 'N':
3219 case 'L':
96e4d5b1 3220 along = (strend - s) / SIZE32;
a0d0e21e
LW
3221 if (len > along)
3222 len = along;
3223 if (checksum) {
3224 while (len-- > 0) {
96e4d5b1 3225 COPY32(s, &aulong);
3226 s += SIZE32;
a0d0e21e
LW
3227#ifdef HAS_NTOHL
3228 if (datumtype == 'N')
3229 aulong = ntohl(aulong);
79072805 3230#endif
a0d0e21e
LW
3231#ifdef HAS_VTOHL
3232 if (datumtype == 'V')
3233 aulong = vtohl(aulong);
79072805 3234#endif
a0d0e21e
LW
3235 if (checksum > 32)
3236 cdouble += (double)aulong;
3237 else
3238 culong += aulong;
3239 }
3240 }
3241 else {
3242 EXTEND(SP, len);
bbce6d69 3243 EXTEND_MORTAL(len);
a0d0e21e 3244 while (len-- > 0) {
96e4d5b1 3245 COPY32(s, &aulong);
3246 s += SIZE32;
a0d0e21e
LW
3247#ifdef HAS_NTOHL
3248 if (datumtype == 'N')
3249 aulong = ntohl(aulong);
79072805 3250#endif
a0d0e21e
LW
3251#ifdef HAS_VTOHL
3252 if (datumtype == 'V')
3253 aulong = vtohl(aulong);
79072805 3254#endif
1e422769 3255 sv = NEWSV(43, 0);
3256 sv_setuv(sv, (UV)aulong);
a0d0e21e
LW
3257 PUSHs(sv_2mortal(sv));
3258 }
3259 }
3260 break;
3261 case 'p':
3262 along = (strend - s) / sizeof(char*);
3263 if (len > along)
3264 len = along;
3265 EXTEND(SP, len);
bbce6d69 3266 EXTEND_MORTAL(len);
a0d0e21e
LW
3267 while (len-- > 0) {
3268 if (sizeof(char*) > strend - s)
3269 break;
3270 else {
3271 Copy(s, &aptr, 1, char*);
3272 s += sizeof(char*);
3273 }
3274 sv = NEWSV(44, 0);
3275 if (aptr)
3276 sv_setpv(sv, aptr);
3277 PUSHs(sv_2mortal(sv));
3278 }
3279 break;
def98dd4 3280 case 'w':
def98dd4 3281 EXTEND(SP, len);
bbce6d69 3282 EXTEND_MORTAL(len);
3283 {
3284 UV auv = 0;
3285 U32 bytes = 0;
3286
3287 while ((len > 0) && (s < strend)) {
3288 auv = (auv << 7) | (*s & 0x7f);
3289 if (!(*s++ & 0x80)) {
3290 bytes = 0;
3291 sv = NEWSV(40, 0);
3292 sv_setuv(sv, auv);
3293 PUSHs(sv_2mortal(sv));
3294 len--;
3295 auv = 0;
3296 }
3297 else if (++bytes >= sizeof(UV)) { /* promote to string */
bbce6d69 3298 char *t;
3299
fc36a67e 3300 sv = newSVpvf("%.*Vu", (int)TYPE_DIGITS(UV), auv);
bbce6d69 3301 while (s < strend) {
3302 sv = mul128(sv, *s & 0x7f);
3303 if (!(*s++ & 0x80)) {
3304 bytes = 0;
3305 break;
3306 }
3307 }
3308 t = SvPV(sv, na);
3309 while (*t == '0')
3310 t++;
3311 sv_chop(sv, t);
3312 PUSHs(sv_2mortal(sv));
3313 len--;
3314 auv = 0;
3315 }
3316 }
3317 if ((s >= strend) && bytes)
3318 croak("Unterminated compressed integer");
3319 }
def98dd4 3320 break;
a0d0e21e
LW
3321 case 'P':
3322 EXTEND(SP, 1);
3323 if (sizeof(char*) > strend - s)
3324 break;
3325 else {
3326 Copy(s, &aptr, 1, char*);
3327 s += sizeof(char*);
3328 }
3329 sv = NEWSV(44, 0);
3330 if (aptr)
3331 sv_setpvn(sv, aptr, len);
3332 PUSHs(sv_2mortal(sv));
3333 break;
ecfc5424 3334#ifdef HAS_QUAD
a0d0e21e
LW
3335 case 'q':
3336 EXTEND(SP, len);
bbce6d69 3337 EXTEND_MORTAL(len);
a0d0e21e 3338 while (len-- > 0) {
ecfc5424 3339 if (s + sizeof(Quad_t) > strend)
a0d0e21e
LW
3340 aquad = 0;
3341 else {
ecfc5424
AD
3342 Copy(s, &aquad, 1, Quad_t);
3343 s += sizeof(Quad_t);
a0d0e21e
LW
3344 }
3345 sv = NEWSV(42, 0);
96e4d5b1 3346 if (aquad >= IV_MIN && aquad <= IV_MAX)
3347 sv_setiv(sv, (IV)aquad);
3348 else
3349 sv_setnv(sv, (double)aquad);
a0d0e21e
LW
3350 PUSHs(sv_2mortal(sv));
3351 }
3352 break;
3353 case 'Q':
3354 EXTEND(SP, len);
bbce6d69 3355 EXTEND_MORTAL(len);
a0d0e21e 3356 while (len-- > 0) {
ecfc5424 3357 if (s + sizeof(unsigned Quad_t) > strend)
a0d0e21e
LW
3358 auquad = 0;
3359 else {
ecfc5424
AD
3360 Copy(s, &auquad, 1, unsigned Quad_t);
3361 s += sizeof(unsigned Quad_t);
a0d0e21e
LW
3362 }
3363 sv = NEWSV(43, 0);
96e4d5b1 3364 if (aquad <= UV_MAX)
3365 sv_setuv(sv, (UV)auquad);
3366 else
3367 sv_setnv(sv, (double)auquad);
a0d0e21e
LW
3368 PUSHs(sv_2mortal(sv));
3369 }
3370 break;
79072805 3371#endif
a0d0e21e
LW
3372 /* float and double added gnb@melba.bby.oz.au 22/11/89 */
3373 case 'f':
3374 case 'F':
3375 along = (strend - s) / sizeof(float);
3376 if (len > along)
3377 len = along;
3378 if (checksum) {
3379 while (len-- > 0) {
3380 Copy(s, &afloat, 1, float);
3381 s += sizeof(float);
3382 cdouble += afloat;
3383 }
3384 }
3385 else {
3386 EXTEND(SP, len);
bbce6d69 3387 EXTEND_MORTAL(len);
a0d0e21e
LW
3388 while (len-- > 0) {
3389 Copy(s, &afloat, 1, float);
3390 s += sizeof(float);
3391 sv = NEWSV(47, 0);
3392 sv_setnv(sv, (double)afloat);
3393 PUSHs(sv_2mortal(sv));
3394 }
3395 }
3396 break;
3397 case 'd':
3398 case 'D':
3399 along = (strend - s) / sizeof(double);
3400 if (len > along)
3401 len = along;
3402 if (checksum) {
3403 while (len-- > 0) {
3404 Copy(s, &adouble, 1, double);
3405 s += sizeof(double);
3406 cdouble += adouble;
3407 }
3408 }
3409 else {
3410 EXTEND(SP, len);
bbce6d69 3411 EXTEND_MORTAL(len);
a0d0e21e
LW
3412 while (len-- > 0) {
3413 Copy(s, &adouble, 1, double);
3414 s += sizeof(double);
3415 sv = NEWSV(48, 0);
3416 sv_setnv(sv, (double)adouble);
3417 PUSHs(sv_2mortal(sv));
3418 }
3419 }
3420 break;
3421 case 'u':
3422 along = (strend - s) * 3 / 4;
3423 sv = NEWSV(42, along);
f12c7020 3424 if (along)
3425 SvPOK_on(sv);
a0d0e21e
LW
3426 while (s < strend && *s > ' ' && *s < 'a') {
3427 I32 a, b, c, d;
3428 char hunk[4];
79072805 3429
a0d0e21e
LW
3430 hunk[3] = '\0';
3431 len = (*s++ - ' ') & 077;
3432 while (len > 0) {
3433 if (s < strend && *s >= ' ')
3434 a = (*s++ - ' ') & 077;
3435 else
3436 a = 0;
3437 if (s < strend && *s >= ' ')
3438 b = (*s++ - ' ') & 077;
3439 else
3440 b = 0;
3441 if (s < strend && *s >= ' ')
3442 c = (*s++ - ' ') & 077;
3443 else
3444 c = 0;
3445 if (s < strend && *s >= ' ')
3446 d = (*s++ - ' ') & 077;
3447 else
3448 d = 0;
4e35701f
NIS
3449 hunk[0] = (a << 2) | (b >> 4);
3450 hunk[1] = (b << 4) | (c >> 2);
3451 hunk[2] = (c << 6) | d;
3452 sv_catpvn(sv, hunk, (len > 3) ? 3 : len);
a0d0e21e
LW
3453 len -= 3;
3454 }
3455 if (*s == '\n')
3456 s++;
3457 else if (s[1] == '\n') /* possible checksum byte */
3458 s += 2;
79072805 3459 }
a0d0e21e
LW
3460 XPUSHs(sv_2mortal(sv));
3461 break;
79072805 3462 }
a0d0e21e
LW
3463 if (checksum) {
3464 sv = NEWSV(42, 0);
3465 if (strchr("fFdD", datumtype) ||
3466 (checksum > 32 && strchr("iIlLN", datumtype)) ) {
3467 double trouble;
79072805 3468
a0d0e21e
LW
3469 adouble = 1.0;
3470 while (checksum >= 16) {
3471 checksum -= 16;
3472 adouble *= 65536.0;
3473 }
3474 while (checksum >= 4) {
3475 checksum -= 4;
3476 adouble *= 16.0;
3477 }
3478 while (checksum--)
3479 adouble *= 2.0;
3480 along = (1 << checksum) - 1;
3481 while (cdouble < 0.0)
3482 cdouble += adouble;
3483 cdouble = modf(cdouble / adouble, &trouble) * adouble;
3484 sv_setnv(sv, cdouble);
3485 }
3486 else {
3487 if (checksum < 32) {
96e4d5b1 3488 aulong = (1 << checksum) - 1;
3489 culong &= aulong;
a0d0e21e 3490 }
96e4d5b1 3491 sv_setuv(sv, (UV)culong);
a0d0e21e
LW
3492 }
3493 XPUSHs(sv_2mortal(sv));
3494 checksum = 0;
79072805 3495 }
79072805 3496 }
54310121 3497 if (sp == oldsp && gimme == G_SCALAR)
36477c24 3498 PUSHs(&sv_undef);
79072805 3499 RETURN;
79072805
LW
3500}
3501
a0d0e21e 3502static void
8ac85365 3503doencodes(register SV *sv, register char *s, register I32 len)
79072805 3504{
a0d0e21e 3505 char hunk[5];
79072805 3506
a0d0e21e
LW
3507 *hunk = len + ' ';
3508 sv_catpvn(sv, hunk, 1);
3509 hunk[4] = '\0';
3510 while (len > 0) {
3511 hunk[0] = ' ' + (077 & (*s >> 2));
4e35701f
NIS
3512 hunk[1] = ' ' + (077 & (((*s << 4) & 060) | ((s[1] >> 4) & 017)));
3513 hunk[2] = ' ' + (077 & (((s[1] << 2) & 074) | ((s[2] >> 6) & 03)));
a0d0e21e
LW
3514 hunk[3] = ' ' + (077 & (s[2] & 077));
3515 sv_catpvn(sv, hunk, 4);
3516 s += 3;
3517 len -= 3;
3518 }
3519 for (s = SvPVX(sv); *s; s++) {
3520 if (*s == ' ')
3521 *s = '`';
3522 }
3523 sv_catpvn(sv, "\n", 1);
79072805
LW
3524}
3525
55497cff 3526static SV *
8ac85365 3527is_an_int(char *s, STRLEN l)
55497cff 3528{
3529 SV *result = newSVpv("", l);
3530 char *result_c = SvPV(result, na); /* convenience */
3531 char *out = result_c;
3532 bool skip = 1;
3533 bool ignore = 0;
3534
3535 while (*s) {
3536 switch (*s) {
3537 case ' ':
3538 break;
3539 case '+':
3540 if (!skip) {
3541 SvREFCNT_dec(result);
3542 return (NULL);
3543 }
3544 break;
3545 case '0':
3546 case '1':
3547 case '2':
3548 case '3':
3549 case '4':
3550 case '5':
3551 case '6':
3552 case '7':
3553 case '8':
3554 case '9':
3555 skip = 0;
3556 if (!ignore) {
3557 *(out++) = *s;
3558 }
3559 break;
3560 case '.':
3561 ignore = 1;
3562 break;
3563 default:
3564 SvREFCNT_dec(result);
3565 return (NULL);
3566 }
3567 s++;
3568 }
3569 *(out++) = '\0';
3570 SvCUR_set(result, out - result_c);
3571 return (result);
3572}
3573
3574static int
61bb5906 3575div128(SV *pnum, bool *done)
8ac85365
NIS
3576 /* must be '\0' terminated */
3577
55497cff 3578{
3579 STRLEN len;
3580 char *s = SvPV(pnum, len);
3581 int m = 0;
3582 int r = 0;
3583 char *t = s;
3584
3585 *done = 1;
3586 while (*t) {
3587 int i;
3588
3589 i = m * 10 + (*t - '0');
3590 m = i & 0x7F;
3591 r = (i >> 7); /* r < 10 */
3592 if (r) {
3593 *done = 0;
3594 }
3595 *(t++) = '0' + r;
3596 }
3597 *(t++) = '\0';
3598 SvCUR_set(pnum, (STRLEN) (t - s));
3599 return (m);
3600}
3601
3602
a0d0e21e 3603PP(pp_pack)
79072805 3604{
4e35701f 3605 djSP; dMARK; dORIGMARK; dTARGET;
a0d0e21e
LW
3606 register SV *cat = TARG;
3607 register I32 items;
3608 STRLEN fromlen;
3609 register char *pat = SvPVx(*++MARK, fromlen);
3610 register char *patend = pat + fromlen;
3611 register I32 len;
3612 I32 datumtype;
3613 SV *fromstr;
3614 /*SUPPRESS 442*/
3615 static char null10[] = {0,0,0,0,0,0,0,0,0,0};
3616 static char *space10 = " ";
79072805 3617
a0d0e21e
LW
3618 /* These must not be in registers: */
3619 char achar;
3620 I16 ashort;
3621 int aint;
3622 unsigned int auint;
3623 I32 along;
3624 U32 aulong;
ecfc5424
AD
3625#ifdef HAS_QUAD
3626 Quad_t aquad;
3627 unsigned Quad_t auquad;
79072805 3628#endif
a0d0e21e
LW
3629 char *aptr;
3630 float afloat;
3631 double adouble;
fb73857a 3632 int commas = 0;
79072805 3633
a0d0e21e
LW
3634 items = SP - MARK;
3635 MARK++;
3636 sv_setpvn(cat, "", 0);
3637 while (pat < patend) {
3638#define NEXTFROM (items-- > 0 ? *MARK++ : &sv_no)
bbdab043
CS
3639 datumtype = *pat++ & 0xFF;
3640 if (isSPACE(datumtype))
3641 continue;
a0d0e21e
LW
3642 if (*pat == '*') {
3643 len = strchr("@Xxu", datumtype) ? 0 : items;
3644 pat++;
3645 }
3646 else if (isDIGIT(*pat)) {
3647 len = *pat++ - '0';
3648 while (isDIGIT(*pat))
3649 len = (len * 10) + (*pat++ - '0');
3650 }
3651 else
3652 len = 1;
3653 switch(datumtype) {
3654 default:
bbdab043 3655 croak("Invalid type in pack: '%c'", (int)datumtype);
fb73857a 3656 case ',': /* grandfather in commas but with a warning */
3657 if (commas++ == 0 && dowarn)
3658 warn("Invalid type in pack: '%c'", (int)datumtype);
3659 break;
a0d0e21e
LW
3660 case '%':
3661 DIE("%% may only be used in unpack");
3662 case '@':
3663 len -= SvCUR(cat);
3664 if (len > 0)
3665 goto grow;
3666 len = -len;
3667 if (len > 0)
3668 goto shrink;
3669 break;
3670 case 'X':
3671 shrink:
3672 if (SvCUR(cat) < len)
3673 DIE("X outside of string");
3674 SvCUR(cat) -= len;
3675 *SvEND(cat) = '\0';
3676 break;
3677 case 'x':
3678 grow:
3679 while (len >= 10) {
3680 sv_catpvn(cat, null10, 10);
3681 len -= 10;
3682 }
3683 sv_catpvn(cat, null10, len);
3684 break;
3685 case 'A':
3686 case 'a':
3687 fromstr = NEXTFROM;
3688 aptr = SvPV(fromstr, fromlen);
3689 if (pat[-1] == '*')
3690 len = fromlen;
3691 if (fromlen > len)
3692 sv_catpvn(cat, aptr, len);
3693 else {
3694 sv_catpvn(cat, aptr, fromlen);
3695 len -= fromlen;
3696 if (datumtype == 'A') {
3697 while (len >= 10) {
3698 sv_catpvn(cat, space10, 10);
3699 len -= 10;
3700 }
3701 sv_catpvn(cat, space10, len);
3702 }
3703 else {
3704 while (len >= 10) {
3705 sv_catpvn(cat, null10, 10);
3706 len -= 10;
3707 }
3708 sv_catpvn(cat, null10, len);
3709 }
3710 }
3711 break;
3712 case 'B':
3713 case 'b':
3714 {
3715 char *savepat = pat;
3716 I32 saveitems;
79072805 3717
a0d0e21e
LW
3718 fromstr = NEXTFROM;
3719 saveitems = items;
3720 aptr = SvPV(fromstr, fromlen);
3721 if (pat[-1] == '*')
3722 len = fromlen;
3723 pat = aptr;
3724 aint = SvCUR(cat);
3725 SvCUR(cat) += (len+7)/8;
3726 SvGROW(cat, SvCUR(cat) + 1);
3727 aptr = SvPVX(cat) + aint;
3728 if (len > fromlen)
3729 len = fromlen;
3730 aint = len;
3731 items = 0;
3732 if (datumtype == 'B') {
3733 for (len = 0; len++ < aint;) {
3734 items |= *pat++ & 1;
3735 if (len & 7)
3736 items <<= 1;
3737 else {
3738 *aptr++ = items & 0xff;
3739 items = 0;
3740 }
3741 }
3742 }
3743 else {
3744 for (len = 0; len++ < aint;) {
3745 if (*pat++ & 1)
3746 items |= 128;
3747 if (len & 7)
3748 items >>= 1;
3749 else {
3750 *aptr++ = items & 0xff;
3751 items = 0;
3752 }
3753 }
3754 }
3755 if (aint & 7) {
3756 if (datumtype == 'B')
3757 items <<= 7 - (aint & 7);
3758 else
3759 items >>= 7 - (aint & 7);
3760 *aptr++ = items & 0xff;
3761 }
3762 pat = SvPVX(cat) + SvCUR(cat);
3763 while (aptr <= pat)
3764 *aptr++ = '\0';
79072805 3765
a0d0e21e
LW
3766 pat = savepat;
3767 items = saveitems;
3768 }
3769 break;
3770 case 'H':
3771 case 'h':
3772 {
3773 char *savepat = pat;
3774 I32 saveitems;
79072805 3775
a0d0e21e
LW
3776 fromstr = NEXTFROM;
3777 saveitems = items;
3778 aptr = SvPV(fromstr, fromlen);
3779 if (pat[-1] == '*')
3780 len = fromlen;
3781 pat = aptr;
3782 aint = SvCUR(cat);
3783 SvCUR(cat) += (len+1)/2;
3784 SvGROW(cat, SvCUR(cat) + 1);
3785 aptr = SvPVX(cat) + aint;
3786 if (len > fromlen)
3787 len = fromlen;
3788 aint = len;
3789 items = 0;
3790 if (datumtype == 'H') {
3791 for (len = 0; len++ < aint;) {
3792 if (isALPHA(*pat))
3793 items |= ((*pat++ & 15) + 9) & 15;
3794 else
3795 items |= *pat++ & 15;
3796 if (len & 1)
3797 items <<= 4;
3798 else {
3799 *aptr++ = items & 0xff;
3800 items = 0;
3801 }
3802 }
3803 }
3804 else {
3805 for (len = 0; len++ < aint;) {
3806 if (isALPHA(*pat))
3807 items |= (((*pat++ & 15) + 9) & 15) << 4;
3808 else
3809 items |= (*pat++ & 15) << 4;
3810 if (len & 1)
3811 items >>= 4;
3812 else {
3813 *aptr++ = items & 0xff;
3814 items = 0;
3815 }
3816 }
3817 }
3818 if (aint & 1)
3819 *aptr++ = items & 0xff;
3820 pat = SvPVX(cat) + SvCUR(cat);
3821 while (aptr <= pat)
3822 *aptr++ = '\0';
79072805 3823
a0d0e21e
LW
3824 pat = savepat;
3825 items = saveitems;
3826 }
3827 break;
3828 case 'C':
3829 case 'c':
3830 while (len-- > 0) {
3831 fromstr = NEXTFROM;
3832 aint = SvIV(fromstr);
3833 achar = aint;
3834 sv_catpvn(cat, &achar, sizeof(char));
3835 }
3836 break;
3837 /* Float and double added by gnb@melba.bby.oz.au 22/11/89 */
3838 case 'f':
3839 case 'F':
3840 while (len-- > 0) {
3841 fromstr = NEXTFROM;
3842 afloat = (float)SvNV(fromstr);
3843 sv_catpvn(cat, (char *)&afloat, sizeof (float));
3844 }
3845 break;
3846 case 'd':
3847 case 'D':
3848 while (len-- > 0) {
3849 fromstr = NEXTFROM;
3850 adouble = (double)SvNV(fromstr);
3851 sv_catpvn(cat, (char *)&adouble, sizeof (double));
3852 }
3853 break;
3854 case 'n':
3855 while (len-- > 0) {
3856 fromstr = NEXTFROM;
3857 ashort = (I16)SvIV(fromstr);
3858#ifdef HAS_HTONS
3859 ashort = htons(ashort);
79072805 3860#endif
96e4d5b1 3861 CAT16(cat, &ashort);
a0d0e21e
LW
3862 }
3863 break;
3864 case 'v':
3865 while (len-- > 0) {
3866 fromstr = NEXTFROM;
3867 ashort = (I16)SvIV(fromstr);
3868#ifdef HAS_HTOVS
3869 ashort = htovs(ashort);
79072805 3870#endif
96e4d5b1 3871 CAT16(cat, &ashort);
a0d0e21e
LW
3872 }
3873 break;
3874 case 'S':
3875 case 's':
3876 while (len-- > 0) {
3877 fromstr = NEXTFROM;
3878 ashort = (I16)SvIV(fromstr);
96e4d5b1 3879 CAT16(cat, &ashort);
a0d0e21e
LW
3880 }
3881 break;
3882 case 'I':
3883 while (len-- > 0) {
3884 fromstr = NEXTFROM;
96e4d5b1 3885 auint = SvUV(fromstr);
a0d0e21e
LW
3886 sv_catpvn(cat, (char*)&auint, sizeof(unsigned int));
3887 }
3888 break;
def98dd4
UP
3889 case 'w':
3890 while (len-- > 0) {
bbce6d69 3891 fromstr = NEXTFROM;
3892 adouble = floor(SvNV(fromstr));
3893
3894 if (adouble < 0)
3895 croak("Cannot compress negative numbers");
3896
46fc3d4c 3897 if (
3898#ifdef BW_BITS
3899 adouble <= BW_MASK
3900#else
ef2d312d
TH
3901#ifdef CXUX_BROKEN_CONSTANT_CONVERT
3902 adouble <= UV_MAX_cxux
3903#else
46fc3d4c 3904 adouble <= UV_MAX
3905#endif
ef2d312d 3906#endif
46fc3d4c 3907 )
3908 {
bbce6d69 3909 char buf[1 + sizeof(UV)];
3910 char *in = buf + sizeof(buf);
3911 UV auv = U_V(adouble);;
3912
3913 do {
3914 *--in = (auv & 0x7f) | 0x80;
3915 auv >>= 7;
3916 } while (auv);
3917 buf[sizeof(buf) - 1] &= 0x7f; /* clear continue bit */
3918 sv_catpvn(cat, in, (buf + sizeof(buf)) - in);
3919 }
3920 else if (SvPOKp(fromstr)) { /* decimal string arithmetics */
3921 char *from, *result, *in;
3922 SV *norm;
3923 STRLEN len;
3924 bool done;
55497cff 3925
bbce6d69 3926 /* Copy string and check for compliance */
3927 from = SvPV(fromstr, len);
3928 if ((norm = is_an_int(from, len)) == NULL)
3929 croak("can compress only unsigned integer");
3930
3931 New('w', result, len, char);
3932 in = result + len;
3933 done = FALSE;
3934 while (!done)
3935 *--in = div128(norm, &done) | 0x80;
3936 result[len - 1] &= 0x7F; /* clear continue bit */
3937 sv_catpvn(cat, in, (result + len) - in);
5f05dabc 3938 Safefree(result);
bbce6d69 3939 SvREFCNT_dec(norm); /* free norm */
def98dd4 3940 }