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