Commit | Line | Data |
---|---|---|
f4a2945e JH |
1 | /* Copyright (c) 1997-2000 Graham Barr <gbarr@pobox.com>. All rights reserved. |
2 | * This program is free software; you can redistribute it and/or | |
3 | * modify it under the same terms as Perl itself. | |
4 | */ | |
5 | ||
6 | #include <EXTERN.h> | |
7 | #include <perl.h> | |
8 | #include <XSUB.h> | |
f4a2945e | 9 | |
92731555 | 10 | #ifndef PERL_VERSION |
37736e65 JH |
11 | # include <patchlevel.h> |
12 | # if !(defined(PERL_VERSION) || (SUBVERSION > 0 && defined(PATCHLEVEL))) | |
13 | # include <could_not_find_Perl_patchlevel.h> | |
14 | # endif | |
92731555 DM |
15 | # define PERL_REVISION 5 |
16 | # define PERL_VERSION PATCHLEVEL | |
17 | # define PERL_SUBVERSION SUBVERSION | |
18 | #endif | |
19 | ||
1bfb5477 GB |
20 | #ifndef aTHX |
21 | # define aTHX | |
9c3c560b JH |
22 | # define pTHX |
23 | #endif | |
24 | ||
25 | /* Some platforms have strict exports. And before 5.7.3 cxinc (or Perl_cxinc) | |
26 | was not exported. Therefore platforms like win32, VMS etc have problems | |
27 | so we redefine it here -- GMB | |
28 | */ | |
29 | #if PERL_VERSION < 7 | |
30 | /* Not in 5.6.1. */ | |
31 | # define SvUOK(sv) SvIOK_UV(sv) | |
32 | # ifdef cxinc | |
33 | # undef cxinc | |
34 | # endif | |
35 | # define cxinc() my_cxinc(aTHX) | |
36 | static I32 | |
37 | my_cxinc(pTHX) | |
38 | { | |
39 | cxstack_max = cxstack_max * 3 / 2; | |
40 | Renew(cxstack, cxstack_max + 1, struct context); /* XXX should fix CXINC macro */ | |
41 | return cxstack_ix + 1; | |
42 | } | |
1bfb5477 GB |
43 | #endif |
44 | ||
45 | #if PERL_VERSION < 6 | |
46 | # define NV double | |
47 | #endif | |
48 | ||
5b7ea690 | 49 | #ifdef SVf_IVisUV |
96db91ed | 50 | # define slu_sv_value(sv) (SvIOK(sv)) ? (SvIOK_UV(sv)) ? (NV)(SvUVX(sv)) : (NV)(SvIVX(sv)) : (SvNV(sv)) |
5b7ea690 | 51 | #else |
2794f15c | 52 | # define slu_sv_value(sv) (SvIOK(sv)) ? (NV)(SvIVX(sv)) : (SvNV(sv)) |
5b7ea690 JH |
53 | #endif |
54 | ||
1bfb5477 GB |
55 | #ifndef Drand01 |
56 | # define Drand01() ((rand() & 0x7FFF) / (double) ((unsigned long)1 << 15)) | |
57 | #endif | |
58 | ||
92731555 | 59 | #if PERL_VERSION < 5 |
f4a2945e JH |
60 | # ifndef gv_stashpvn |
61 | # define gv_stashpvn(n,l,c) gv_stashpv(n,c) | |
62 | # endif | |
63 | # ifndef SvTAINTED | |
64 | ||
65 | static bool | |
66 | sv_tainted(SV *sv) | |
67 | { | |
68 | if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) { | |
69 | MAGIC *mg = mg_find(sv, 't'); | |
70 | if (mg && ((mg->mg_len & 1) || (mg->mg_len & 2) && mg->mg_obj == sv)) | |
71 | return TRUE; | |
72 | } | |
73 | return FALSE; | |
74 | } | |
75 | ||
76 | # define SvTAINTED_on(sv) sv_magic((sv), Nullsv, 't', Nullch, 0) | |
77 | # define SvTAINTED(sv) (SvMAGICAL(sv) && sv_tainted(sv)) | |
78 | # endif | |
79 | # define PL_defgv defgv | |
80 | # define PL_op op | |
81 | # define PL_curpad curpad | |
82 | # define CALLRUNOPS runops | |
83 | # define PL_curpm curpm | |
84 | # define PL_sv_undef sv_undef | |
85 | # define PERL_CONTEXT struct context | |
86 | #endif | |
92731555 | 87 | #if (PERL_VERSION < 5) || (PERL_VERSION == 5 && PERL_SUBVERSION <50) |
f4a2945e JH |
88 | # ifndef PL_tainting |
89 | # define PL_tainting tainting | |
90 | # endif | |
91 | # ifndef PL_stack_base | |
92 | # define PL_stack_base stack_base | |
93 | # endif | |
94 | # ifndef PL_stack_sp | |
95 | # define PL_stack_sp stack_sp | |
96 | # endif | |
97 | # ifndef PL_ppaddr | |
98 | # define PL_ppaddr ppaddr | |
99 | # endif | |
100 | #endif | |
101 | ||
975adce1 JH |
102 | #ifndef PTR2UV |
103 | # define PTR2UV(ptr) (UV)(ptr) | |
5b7ea690 JH |
104 | #endif |
105 | ||
24c2fff4 NC |
106 | #ifndef SvUV_set |
107 | # define SvUV_set(sv, val) (((XPVUV*)SvANY(sv))->xuv_uv = (val)) | |
108 | #endif | |
109 | ||
110 | #ifndef PERL_UNUSED_DECL | |
111 | # ifdef HASATTRIBUTE | |
112 | # if (defined(__GNUC__) && defined(__cplusplus)) || defined(__INTEL_COMPILER) | |
113 | # define PERL_UNUSED_DECL | |
114 | # else | |
115 | # define PERL_UNUSED_DECL __attribute__((unused)) | |
116 | # endif | |
117 | # else | |
118 | # define PERL_UNUSED_DECL | |
119 | # endif | |
120 | #endif | |
121 | ||
122 | #ifndef dNOOP | |
123 | #define dNOOP extern int Perl___notused PERL_UNUSED_DECL | |
124 | #endif | |
125 | ||
126 | #ifndef dVAR | |
127 | #define dVAR dNOOP | |
128 | #endif | |
129 | ||
f4a2945e JH |
130 | MODULE=List::Util PACKAGE=List::Util |
131 | ||
132 | void | |
133 | min(...) | |
134 | PROTOTYPE: @ | |
135 | ALIAS: | |
136 | min = 0 | |
137 | max = 1 | |
138 | CODE: | |
139 | { | |
140 | int index; | |
141 | NV retval; | |
142 | SV *retsv; | |
143 | if(!items) { | |
144 | XSRETURN_UNDEF; | |
145 | } | |
146 | retsv = ST(0); | |
5b7ea690 | 147 | retval = slu_sv_value(retsv); |
f4a2945e JH |
148 | for(index = 1 ; index < items ; index++) { |
149 | SV *stacksv = ST(index); | |
5b7ea690 | 150 | NV val = slu_sv_value(stacksv); |
f4a2945e JH |
151 | if(val < retval ? !ix : ix) { |
152 | retsv = stacksv; | |
153 | retval = val; | |
154 | } | |
155 | } | |
156 | ST(0) = retsv; | |
157 | XSRETURN(1); | |
158 | } | |
159 | ||
160 | ||
161 | ||
162 | NV | |
163 | sum(...) | |
164 | PROTOTYPE: @ | |
165 | CODE: | |
166 | { | |
5b7ea690 | 167 | SV *sv; |
f4a2945e | 168 | int index; |
f4a2945e JH |
169 | if(!items) { |
170 | XSRETURN_UNDEF; | |
171 | } | |
5b7ea690 JH |
172 | sv = ST(0); |
173 | RETVAL = slu_sv_value(sv); | |
f4a2945e | 174 | for(index = 1 ; index < items ; index++) { |
5b7ea690 JH |
175 | sv = ST(index); |
176 | RETVAL += slu_sv_value(sv); | |
f4a2945e JH |
177 | } |
178 | } | |
179 | OUTPUT: | |
180 | RETVAL | |
181 | ||
182 | ||
183 | void | |
184 | minstr(...) | |
185 | PROTOTYPE: @ | |
186 | ALIAS: | |
187 | minstr = 2 | |
188 | maxstr = 0 | |
189 | CODE: | |
190 | { | |
191 | SV *left; | |
192 | int index; | |
193 | if(!items) { | |
194 | XSRETURN_UNDEF; | |
195 | } | |
196 | /* | |
197 | sv_cmp & sv_cmp_locale return 1,0,-1 for gt,eq,lt | |
198 | so we set ix to the value we are looking for | |
199 | xsubpp does not allow -ve values, so we start with 0,2 and subtract 1 | |
200 | */ | |
201 | ix -= 1; | |
202 | left = ST(0); | |
203 | #ifdef OPpLOCALE | |
204 | if(MAXARG & OPpLOCALE) { | |
205 | for(index = 1 ; index < items ; index++) { | |
206 | SV *right = ST(index); | |
207 | if(sv_cmp_locale(left, right) == ix) | |
208 | left = right; | |
209 | } | |
210 | } | |
211 | else { | |
212 | #endif | |
213 | for(index = 1 ; index < items ; index++) { | |
214 | SV *right = ST(index); | |
215 | if(sv_cmp(left, right) == ix) | |
216 | left = right; | |
217 | } | |
218 | #ifdef OPpLOCALE | |
219 | } | |
220 | #endif | |
221 | ST(0) = left; | |
222 | XSRETURN(1); | |
223 | } | |
224 | ||
225 | ||
226 | ||
227 | void | |
228 | reduce(block,...) | |
229 | SV * block | |
230 | PROTOTYPE: &@ | |
231 | CODE: | |
232 | { | |
0e0a3cd3 | 233 | SV *ret = sv_newmortal(); |
f4a2945e | 234 | int index; |
f4a2945e JH |
235 | GV *agv,*bgv,*gv; |
236 | HV *stash; | |
237 | CV *cv; | |
238 | OP *reducecop; | |
1bfb5477 GB |
239 | PERL_CONTEXT *cx; |
240 | SV** newsp; | |
241 | I32 gimme = G_SCALAR; | |
c43a4d73 | 242 | U8 hasargs = 0; |
1bfb5477 GB |
243 | bool oldcatch = CATCH_GET; |
244 | ||
f4a2945e JH |
245 | if(items <= 1) { |
246 | XSRETURN_UNDEF; | |
247 | } | |
248 | agv = gv_fetchpv("a", TRUE, SVt_PV); | |
249 | bgv = gv_fetchpv("b", TRUE, SVt_PV); | |
250 | SAVESPTR(GvSV(agv)); | |
251 | SAVESPTR(GvSV(bgv)); | |
0e0a3cd3 | 252 | GvSV(agv) = ret; |
f4a2945e JH |
253 | cv = sv_2cv(block, &stash, &gv, 0); |
254 | reducecop = CvSTART(cv); | |
255 | SAVESPTR(CvROOT(cv)->op_ppaddr); | |
256 | CvROOT(cv)->op_ppaddr = PL_ppaddr[OP_NULL]; | |
d7afa7f5 JH |
257 | #ifdef PAD_SET_CUR |
258 | PAD_SET_CUR(CvPADLIST(cv),1); | |
259 | #else | |
f4a2945e JH |
260 | SAVESPTR(PL_curpad); |
261 | PL_curpad = AvARRAY((AV*)AvARRAY(CvPADLIST(cv))[1]); | |
d7afa7f5 | 262 | #endif |
f4a2945e JH |
263 | SAVETMPS; |
264 | SAVESPTR(PL_op); | |
0e0a3cd3 | 265 | SvSetSV(ret, ST(1)); |
1bfb5477 | 266 | CATCH_SET(TRUE); |
5b7ea690 JH |
267 | PUSHBLOCK(cx, CXt_SUB, SP); |
268 | PUSHSUB(cx); | |
f4a2945e | 269 | for(index = 2 ; index < items ; index++) { |
f4a2945e JH |
270 | GvSV(bgv) = ST(index); |
271 | PL_op = reducecop; | |
da53b6b0 | 272 | CALLRUNOPS(aTHX); |
0e0a3cd3 | 273 | SvSetSV(ret, *PL_stack_sp); |
f4a2945e | 274 | } |
0e0a3cd3 | 275 | ST(0) = ret; |
1bfb5477 GB |
276 | POPBLOCK(cx,PL_curpm); |
277 | CATCH_SET(oldcatch); | |
f4a2945e JH |
278 | XSRETURN(1); |
279 | } | |
280 | ||
281 | void | |
282 | first(block,...) | |
283 | SV * block | |
284 | PROTOTYPE: &@ | |
285 | CODE: | |
286 | { | |
f4a2945e | 287 | int index; |
f4a2945e JH |
288 | GV *gv; |
289 | HV *stash; | |
290 | CV *cv; | |
291 | OP *reducecop; | |
1bfb5477 GB |
292 | PERL_CONTEXT *cx; |
293 | SV** newsp; | |
294 | I32 gimme = G_SCALAR; | |
c43a4d73 | 295 | U8 hasargs = 0; |
1bfb5477 GB |
296 | bool oldcatch = CATCH_GET; |
297 | ||
f4a2945e JH |
298 | if(items <= 1) { |
299 | XSRETURN_UNDEF; | |
300 | } | |
301 | SAVESPTR(GvSV(PL_defgv)); | |
302 | cv = sv_2cv(block, &stash, &gv, 0); | |
303 | reducecop = CvSTART(cv); | |
304 | SAVESPTR(CvROOT(cv)->op_ppaddr); | |
305 | CvROOT(cv)->op_ppaddr = PL_ppaddr[OP_NULL]; | |
d7afa7f5 JH |
306 | #ifdef PAD_SET_CUR |
307 | PAD_SET_CUR(CvPADLIST(cv),1); | |
308 | #else | |
f4a2945e JH |
309 | SAVESPTR(PL_curpad); |
310 | PL_curpad = AvARRAY((AV*)AvARRAY(CvPADLIST(cv))[1]); | |
d7afa7f5 | 311 | #endif |
f4a2945e JH |
312 | SAVETMPS; |
313 | SAVESPTR(PL_op); | |
1bfb5477 | 314 | CATCH_SET(TRUE); |
5b7ea690 JH |
315 | PUSHBLOCK(cx, CXt_SUB, SP); |
316 | PUSHSUB(cx); | |
317 | if (!CvDEPTH(cv)) | |
318 | (void)SvREFCNT_inc(cv); | |
319 | ||
f4a2945e JH |
320 | for(index = 1 ; index < items ; index++) { |
321 | GvSV(PL_defgv) = ST(index); | |
322 | PL_op = reducecop; | |
da53b6b0 | 323 | CALLRUNOPS(aTHX); |
f4a2945e JH |
324 | if (SvTRUE(*PL_stack_sp)) { |
325 | ST(0) = ST(index); | |
1bfb5477 GB |
326 | POPBLOCK(cx,PL_curpm); |
327 | CATCH_SET(oldcatch); | |
f4a2945e JH |
328 | XSRETURN(1); |
329 | } | |
330 | } | |
1bfb5477 GB |
331 | POPBLOCK(cx,PL_curpm); |
332 | CATCH_SET(oldcatch); | |
f4a2945e JH |
333 | XSRETURN_UNDEF; |
334 | } | |
335 | ||
1bfb5477 GB |
336 | void |
337 | shuffle(...) | |
338 | PROTOTYPE: @ | |
339 | CODE: | |
340 | { | |
341 | int index; | |
342 | struct op dmy_op; | |
343 | struct op *old_op = PL_op; | |
1bfb5477 | 344 | |
c29e891d GB |
345 | /* We call pp_rand here so that Drand01 get initialized if rand() |
346 | or srand() has not already been called | |
347 | */ | |
1bfb5477 | 348 | memzero((char*)(&dmy_op), sizeof(struct op)); |
d7afa7f5 JH |
349 | /* we let pp_rand() borrow the TARG allocated for this XS sub */ |
350 | dmy_op.op_targ = PL_op->op_targ; | |
1bfb5477 | 351 | PL_op = &dmy_op; |
20d72259 | 352 | (void)*(PL_ppaddr[OP_RAND])(aTHX); |
1bfb5477 | 353 | PL_op = old_op; |
1bfb5477 GB |
354 | for (index = items ; index > 1 ; ) { |
355 | int swap = (int)(Drand01() * (double)(index--)); | |
356 | SV *tmp = ST(swap); | |
357 | ST(swap) = ST(index); | |
358 | ST(index) = tmp; | |
359 | } | |
360 | XSRETURN(items); | |
361 | } | |
362 | ||
363 | ||
f4a2945e JH |
364 | MODULE=List::Util PACKAGE=Scalar::Util |
365 | ||
366 | void | |
367 | dualvar(num,str) | |
368 | SV * num | |
369 | SV * str | |
370 | PROTOTYPE: $$ | |
371 | CODE: | |
372 | { | |
373 | STRLEN len; | |
374 | char *ptr = SvPV(str,len); | |
375 | ST(0) = sv_newmortal(); | |
9c5ffd7c | 376 | (void)SvUPGRADE(ST(0),SVt_PVNV); |
f4a2945e | 377 | sv_setpvn(ST(0),ptr,len); |
1bfb5477 | 378 | if(SvNOK(num) || SvPOK(num) || SvMAGICAL(num)) { |
0da6cfda | 379 | SvNV_set(ST(0), SvNV(num)); |
f4a2945e JH |
380 | SvNOK_on(ST(0)); |
381 | } | |
1bfb5477 GB |
382 | #ifdef SVf_IVisUV |
383 | else if (SvUOK(num)) { | |
0da6cfda | 384 | SvUV_set(ST(0), SvUV(num)); |
1bfb5477 GB |
385 | SvIOK_on(ST(0)); |
386 | SvIsUV_on(ST(0)); | |
387 | } | |
388 | #endif | |
f4a2945e | 389 | else { |
0da6cfda | 390 | SvIV_set(ST(0), SvIV(num)); |
f4a2945e JH |
391 | SvIOK_on(ST(0)); |
392 | } | |
393 | if(PL_tainting && (SvTAINTED(num) || SvTAINTED(str))) | |
394 | SvTAINTED_on(ST(0)); | |
395 | XSRETURN(1); | |
396 | } | |
397 | ||
398 | char * | |
399 | blessed(sv) | |
400 | SV * sv | |
401 | PROTOTYPE: $ | |
402 | CODE: | |
403 | { | |
404 | if (SvMAGICAL(sv)) | |
405 | mg_get(sv); | |
406 | if(!sv_isobject(sv)) { | |
407 | XSRETURN_UNDEF; | |
408 | } | |
409 | RETVAL = sv_reftype(SvRV(sv),TRUE); | |
410 | } | |
411 | OUTPUT: | |
412 | RETVAL | |
413 | ||
414 | char * | |
415 | reftype(sv) | |
416 | SV * sv | |
417 | PROTOTYPE: $ | |
418 | CODE: | |
419 | { | |
420 | if (SvMAGICAL(sv)) | |
421 | mg_get(sv); | |
422 | if(!SvROK(sv)) { | |
423 | XSRETURN_UNDEF; | |
424 | } | |
425 | RETVAL = sv_reftype(SvRV(sv),FALSE); | |
426 | } | |
427 | OUTPUT: | |
428 | RETVAL | |
429 | ||
53f57a5f | 430 | UV |
5b7ea690 JH |
431 | refaddr(sv) |
432 | SV * sv | |
433 | PROTOTYPE: $ | |
434 | CODE: | |
435 | { | |
fc3d430d MHM |
436 | if (SvMAGICAL(sv)) |
437 | mg_get(sv); | |
5b7ea690 JH |
438 | if(!SvROK(sv)) { |
439 | XSRETURN_UNDEF; | |
440 | } | |
53f57a5f | 441 | RETVAL = PTR2UV(SvRV(sv)); |
5b7ea690 JH |
442 | } |
443 | OUTPUT: | |
444 | RETVAL | |
445 | ||
f4a2945e JH |
446 | void |
447 | weaken(sv) | |
448 | SV *sv | |
449 | PROTOTYPE: $ | |
450 | CODE: | |
451 | #ifdef SvWEAKREF | |
452 | sv_rvweaken(sv); | |
453 | #else | |
454 | croak("weak references are not implemented in this release of perl"); | |
455 | #endif | |
456 | ||
c6c619a9 | 457 | void |
f4a2945e JH |
458 | isweak(sv) |
459 | SV *sv | |
460 | PROTOTYPE: $ | |
461 | CODE: | |
462 | #ifdef SvWEAKREF | |
463 | ST(0) = boolSV(SvROK(sv) && SvWEAKREF(sv)); | |
464 | XSRETURN(1); | |
465 | #else | |
466 | croak("weak references are not implemented in this release of perl"); | |
467 | #endif | |
468 | ||
469 | int | |
470 | readonly(sv) | |
471 | SV *sv | |
472 | PROTOTYPE: $ | |
473 | CODE: | |
474 | RETVAL = SvREADONLY(sv); | |
475 | OUTPUT: | |
476 | RETVAL | |
477 | ||
478 | int | |
479 | tainted(sv) | |
480 | SV *sv | |
481 | PROTOTYPE: $ | |
482 | CODE: | |
483 | RETVAL = SvTAINTED(sv); | |
484 | OUTPUT: | |
485 | RETVAL | |
486 | ||
5b7ea690 JH |
487 | void |
488 | isvstring(sv) | |
489 | SV *sv | |
490 | PROTOTYPE: $ | |
491 | CODE: | |
492 | #ifdef SvVOK | |
493 | ST(0) = boolSV(SvVOK(sv)); | |
494 | XSRETURN(1); | |
495 | #else | |
496 | croak("vstrings are not implemented in this release of perl"); | |
497 | #endif | |
498 | ||
975adce1 JH |
499 | int |
500 | looks_like_number(sv) | |
501 | SV *sv | |
502 | PROTOTYPE: $ | |
503 | CODE: | |
504 | RETVAL = looks_like_number(sv); | |
505 | OUTPUT: | |
506 | RETVAL | |
507 | ||
c43a4d73 | 508 | void |
37736e65 JH |
509 | set_prototype(subref, proto) |
510 | SV *subref | |
511 | SV *proto | |
512 | PROTOTYPE: &$ | |
513 | CODE: | |
514 | { | |
515 | if (SvROK(subref)) { | |
516 | SV *sv = SvRV(subref); | |
517 | if (SvTYPE(sv) != SVt_PVCV) { | |
518 | /* not a subroutine reference */ | |
519 | croak("set_prototype: not a subroutine reference"); | |
520 | } | |
521 | if (SvPOK(proto)) { | |
522 | /* set the prototype */ | |
523 | STRLEN len; | |
524 | char *ptr = SvPV(proto, len); | |
525 | sv_setpvn(sv, ptr, len); | |
526 | } | |
527 | else { | |
528 | /* delete the prototype */ | |
529 | SvPOK_off(sv); | |
530 | } | |
531 | } | |
532 | else { | |
533 | croak("set_prototype: not a reference"); | |
534 | } | |
535 | XSRETURN(1); | |
536 | } | |
5b7ea690 | 537 | |
f4a2945e JH |
538 | BOOT: |
539 | { | |
5b7ea690 | 540 | #if !defined(SvWEAKREF) || !defined(SvVOK) |
f4a2945e JH |
541 | HV *stash = gv_stashpvn("Scalar::Util", 12, TRUE); |
542 | GV *vargv = *(GV**)hv_fetch(stash, "EXPORT_FAIL", 11, TRUE); | |
543 | AV *varav; | |
544 | if (SvTYPE(vargv) != SVt_PVGV) | |
545 | gv_init(vargv, stash, "Scalar::Util", 12, TRUE); | |
546 | varav = GvAVn(vargv); | |
5b7ea690 JH |
547 | #endif |
548 | #ifndef SvWEAKREF | |
f4a2945e JH |
549 | av_push(varav, newSVpv("weaken",6)); |
550 | av_push(varav, newSVpv("isweak",6)); | |
551 | #endif | |
5b7ea690 JH |
552 | #ifndef SvVOK |
553 | av_push(varav, newSVpv("isvstring",9)); | |
554 | #endif | |
f4a2945e | 555 | } |