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