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