This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Avoid hard-coding op numbers
[perl5.git] / util.c
CommitLineData
a0d0e21e 1/* util.c
a687059c 2 *
9607fc9c 3 * Copyright (c) 1991-1997, Larry Wall
a687059c 4 *
d48672a2
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.
8d063cd8 7 *
8d063cd8 8 */
a0d0e21e
LW
9
10/*
11 * "Very useful, no doubt, that was to Saruman; yet it seems that he was
12 * not content." --Gandalf
13 */
8d063cd8 14
8d063cd8 15#include "EXTERN.h"
8d063cd8 16#include "perl.h"
62b28dd9 17
a953d64f
JH
18/* XXX Configure test needed */
19#if !defined(NSIG) || defined(M_UNIX) || defined(M_XENIX) || defined(__NetBSD__)
a687059c 20#include <signal.h>
62b28dd9 21#endif
a687059c 22
36477c24 23#ifndef SIG_ERR
24# define SIG_ERR ((Sighandler_t) -1)
25#endif
26
bd4080b3 27/* XXX If this causes problems, set i_unistd=undef in the hint file. */
85e6fe83 28#ifdef I_UNISTD
8990e307
LW
29# include <unistd.h>
30#endif
31
a687059c
LW
32#ifdef I_VFORK
33# include <vfork.h>
34#endif
35
94b6baf5
AD
36/* Put this after #includes because fork and vfork prototypes may
37 conflict.
38*/
39#ifndef HAS_VFORK
40# define vfork fork
41#endif
42
fe14fcc3
LW
43#ifdef I_FCNTL
44# include <fcntl.h>
45#endif
46#ifdef I_SYS_FILE
47# include <sys/file.h>
48#endif
49
ff68c719 50#ifdef I_SYS_WAIT
51# include <sys/wait.h>
52#endif
53
8d063cd8 54#define FLUSH
8d063cd8 55
a0d0e21e 56#ifdef LEAKTEST
a0d0e21e 57
8c52afec
IZ
58static void xstat _((int));
59long xcount[MAXXCOUNT];
60long lastxcount[MAXXCOUNT];
61long xycount[MAXXCOUNT][MAXYCOUNT];
62long lastxycount[MAXXCOUNT][MAXYCOUNT];
63
a0d0e21e 64#endif
a863c7d1 65
55497cff 66#ifndef MYMALLOC
de3bb511 67
8d063cd8
LW
68/* paranoid version of malloc */
69
a687059c
LW
70/* NOTE: Do not call the next three routines directly. Use the macros
71 * in handy.h, so that we can easily redefine everything to do tracking of
72 * allocated hunks back to the original New to track down any memory leaks.
20cec16a 73 * XXX This advice seems to be widely ignored :-( --AD August 1996.
a687059c
LW
74 */
75
bd4080b3 76Malloc_t
f0f333f4 77safemalloc(MEM_SIZE size)
8d063cd8 78{
bd4080b3 79 Malloc_t ptr;
55497cff 80#ifdef HAS_64K_LIMIT
62b28dd9 81 if (size > 0xffff) {
760ac839 82 PerlIO_printf(PerlIO_stderr(), "Allocation too large: %lx\n", size) FLUSH;
79072805 83 my_exit(1);
62b28dd9 84 }
55497cff 85#endif /* HAS_64K_LIMIT */
34de22dd
LW
86#ifdef DEBUGGING
87 if ((long)size < 0)
463ee0b2 88 croak("panic: malloc");
34de22dd 89#endif
6ad3d225 90 ptr = PerlMem_malloc(size?size:1); /* malloc(0) is NASTY on our system */
79072805 91#if !(defined(I286) || defined(atarist))
3280af22 92 DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%x: (%05d) malloc %ld bytes\n",ptr,PL_an++,(long)size));
79072805 93#else
6b88bc9c 94 DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) malloc %ld bytes\n",ptr,PL_an++,(long)size));
8d063cd8
LW
95#endif
96 if (ptr != Nullch)
97 return ptr;
3280af22 98 else if (PL_nomemok)
7c0587c8 99 return Nullch;
8d063cd8 100 else {
22c35a8c 101 PerlIO_puts(PerlIO_stderr(),PL_no_mem) FLUSH;
79072805 102 my_exit(1);
4e35701f 103 return Nullch;
8d063cd8
LW
104 }
105 /*NOTREACHED*/
106}
107
108/* paranoid version of realloc */
109
bd4080b3 110Malloc_t
f0f333f4 111saferealloc(Malloc_t where,MEM_SIZE size)
8d063cd8 112{
bd4080b3 113 Malloc_t ptr;
ecfc5424 114#if !defined(STANDARD_C) && !defined(HAS_REALLOC_PROTOTYPE)
6ad3d225 115 Malloc_t PerlMem_realloc();
ecfc5424 116#endif /* !defined(STANDARD_C) && !defined(HAS_REALLOC_PROTOTYPE) */
8d063cd8 117
55497cff 118#ifdef HAS_64K_LIMIT
5f05dabc 119 if (size > 0xffff) {
120 PerlIO_printf(PerlIO_stderr(),
121 "Reallocation too large: %lx\n", size) FLUSH;
122 my_exit(1);
123 }
55497cff 124#endif /* HAS_64K_LIMIT */
7614df0c
JD
125 if (!size) {
126 safefree(where);
127 return NULL;
128 }
129
378cc40b 130 if (!where)
7614df0c 131 return safemalloc(size);
34de22dd
LW
132#ifdef DEBUGGING
133 if ((long)size < 0)
463ee0b2 134 croak("panic: realloc");
34de22dd 135#endif
7614df0c 136 ptr = PerlMem_realloc(where,size);
79072805
LW
137
138#if !(defined(I286) || defined(atarist))
139 DEBUG_m( {
3280af22
NIS
140 PerlIO_printf(Perl_debug_log, "0x%x: (%05d) rfree\n",where,PL_an++);
141 PerlIO_printf(Perl_debug_log, "0x%x: (%05d) realloc %ld bytes\n",ptr,PL_an++,(long)size);
79072805
LW
142 } )
143#else
144 DEBUG_m( {
6b88bc9c
GS
145 PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) rfree\n",where,PL_an++);
146 PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) realloc %ld bytes\n",ptr,PL_an++,(long)size);
79072805 147 } )
8d063cd8 148#endif
79072805 149
8d063cd8
LW
150 if (ptr != Nullch)
151 return ptr;
3280af22 152 else if (PL_nomemok)
7c0587c8 153 return Nullch;
8d063cd8 154 else {
22c35a8c 155 PerlIO_puts(PerlIO_stderr(),PL_no_mem) FLUSH;
79072805 156 my_exit(1);
4e35701f 157 return Nullch;
8d063cd8
LW
158 }
159 /*NOTREACHED*/
160}
161
162/* safe version of free */
163
54310121 164Free_t
f0f333f4 165safefree(Malloc_t where)
8d063cd8 166{
79072805 167#if !(defined(I286) || defined(atarist))
3280af22 168 DEBUG_m( PerlIO_printf(Perl_debug_log, "0x%x: (%05d) free\n",(char *) where,PL_an++));
79072805 169#else
6b88bc9c 170 DEBUG_m( PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) free\n",(char *) where,PL_an++));
8d063cd8 171#endif
378cc40b 172 if (where) {
de3bb511 173 /*SUPPRESS 701*/
6ad3d225 174 PerlMem_free(where);
378cc40b 175 }
8d063cd8
LW
176}
177
1050c9ca 178/* safe version of calloc */
179
bd4080b3 180Malloc_t
f0f333f4 181safecalloc(MEM_SIZE count, MEM_SIZE size)
1050c9ca 182{
bd4080b3 183 Malloc_t ptr;
1050c9ca 184
55497cff 185#ifdef HAS_64K_LIMIT
5f05dabc 186 if (size * count > 0xffff) {
187 PerlIO_printf(PerlIO_stderr(),
188 "Allocation too large: %lx\n", size * count) FLUSH;
189 my_exit(1);
190 }
55497cff 191#endif /* HAS_64K_LIMIT */
1050c9ca 192#ifdef DEBUGGING
193 if ((long)size < 0 || (long)count < 0)
194 croak("panic: calloc");
195#endif
0b7c1c42 196 size *= count;
6ad3d225 197 ptr = PerlMem_malloc(size?size:1); /* malloc(0) is NASTY on our system */
1050c9ca 198#if !(defined(I286) || defined(atarist))
3280af22 199 DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%x: (%05d) calloc %ld x %ld bytes\n",ptr,PL_an++,(long)count,(long)size));
1050c9ca 200#else
6b88bc9c 201 DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%lx: (%05d) calloc %ld x %ld bytes\n",ptr,PL_an++,(long)count,(long)size));
1050c9ca 202#endif
1050c9ca 203 if (ptr != Nullch) {
204 memset((void*)ptr, 0, size);
205 return ptr;
206 }
3280af22 207 else if (PL_nomemok)
1050c9ca 208 return Nullch;
209 else {
22c35a8c 210 PerlIO_puts(PerlIO_stderr(),PL_no_mem) FLUSH;
1050c9ca 211 my_exit(1);
4e35701f 212 return Nullch;
1050c9ca 213 }
214 /*NOTREACHED*/
215}
216
55497cff 217#endif /* !MYMALLOC */
de3bb511 218
a687059c
LW
219#ifdef LEAKTEST
220
8c52afec
IZ
221struct mem_test_strut {
222 union {
223 long type;
224 char c[2];
225 } u;
226 long size;
227};
228
229# define ALIGN sizeof(struct mem_test_strut)
230
231# define sizeof_chunk(ch) (((struct mem_test_strut*) (ch))->size)
232# define typeof_chunk(ch) \
233 (((struct mem_test_strut*) (ch))->u.c[0] + ((struct mem_test_strut*) (ch))->u.c[1]*100)
234# define set_typeof_chunk(ch,t) \
235 (((struct mem_test_strut*) (ch))->u.c[0] = t % 100, ((struct mem_test_strut*) (ch))->u.c[1] = t / 100)
236#define SIZE_TO_Y(size) ( (size) > MAXY_SIZE \
237 ? MAXYCOUNT - 1 \
238 : ( (size) > 40 \
239 ? ((size) - 1)/8 + 5 \
240 : ((size) - 1)/4))
8d063cd8 241
bd4080b3 242Malloc_t
f0f333f4 243safexmalloc(I32 x, MEM_SIZE size)
8d063cd8 244{
8c52afec 245 register char* where = (char*)safemalloc(size + ALIGN);
8d063cd8 246
8c52afec
IZ
247 xcount[x] += size;
248 xycount[x][SIZE_TO_Y(size)]++;
249 set_typeof_chunk(where, x);
250 sizeof_chunk(where) = size;
251 return (Malloc_t)(where + ALIGN);
8d063cd8 252}
8d063cd8 253
bd4080b3 254Malloc_t
8c52afec 255safexrealloc(Malloc_t wh, MEM_SIZE size)
a687059c 256{
8c52afec
IZ
257 char *where = (char*)wh;
258
259 if (!wh)
260 return safexmalloc(0,size);
261
262 {
263 MEM_SIZE old = sizeof_chunk(where - ALIGN);
264 int t = typeof_chunk(where - ALIGN);
265 register char* new = (char*)saferealloc(where - ALIGN, size + ALIGN);
266
267 xycount[t][SIZE_TO_Y(old)]--;
268 xycount[t][SIZE_TO_Y(size)]++;
269 xcount[t] += size - old;
270 sizeof_chunk(new) = size;
271 return (Malloc_t)(new + ALIGN);
272 }
a687059c
LW
273}
274
275void
8c52afec 276safexfree(Malloc_t wh)
a687059c 277{
79072805 278 I32 x;
8c52afec
IZ
279 char *where = (char*)wh;
280 MEM_SIZE size;
281
a687059c
LW
282 if (!where)
283 return;
284 where -= ALIGN;
8c52afec 285 size = sizeof_chunk(where);
a687059c 286 x = where[0] + 100 * where[1];
8c52afec
IZ
287 xcount[x] -= size;
288 xycount[x][SIZE_TO_Y(size)]--;
a687059c
LW
289 safefree(where);
290}
291
bd4080b3 292Malloc_t
f0f333f4 293safexcalloc(I32 x,MEM_SIZE count, MEM_SIZE size)
1050c9ca 294{
8c52afec
IZ
295 register char * where = (char*)safexmalloc(x, size * count + ALIGN);
296 xcount[x] += size;
297 xycount[x][SIZE_TO_Y(size)]++;
298 memset((void*)(where + ALIGN), 0, size * count);
299 set_typeof_chunk(where, x);
300 sizeof_chunk(where) = size;
301 return (Malloc_t)(where + ALIGN);
1050c9ca 302}
303
7c0587c8 304static void
8c52afec 305xstat(int flag)
8d063cd8 306{
8c52afec
IZ
307 register I32 i, j, total = 0;
308 I32 subtot[MAXYCOUNT];
8d063cd8 309
8c52afec
IZ
310 for (j = 0; j < MAXYCOUNT; j++) {
311 subtot[j] = 0;
312 }
313
314 PerlIO_printf(PerlIO_stderr(), " Id subtot 4 8 12 16 20 24 28 32 36 40 48 56 64 72 80 80+\n", total);
a687059c 315 for (i = 0; i < MAXXCOUNT; i++) {
8c52afec
IZ
316 total += xcount[i];
317 for (j = 0; j < MAXYCOUNT; j++) {
318 subtot[j] += xycount[i][j];
319 }
320 if (flag == 0
321 ? xcount[i] /* Have something */
322 : (flag == 2
323 ? xcount[i] != lastxcount[i] /* Changed */
324 : xcount[i] > lastxcount[i])) { /* Growed */
325 PerlIO_printf(PerlIO_stderr(),"%2d %02d %7ld ", i / 100, i % 100,
326 flag == 2 ? xcount[i] - lastxcount[i] : xcount[i]);
a687059c 327 lastxcount[i] = xcount[i];
8c52afec
IZ
328 for (j = 0; j < MAXYCOUNT; j++) {
329 if ( flag == 0
330 ? xycount[i][j] /* Have something */
331 : (flag == 2
332 ? xycount[i][j] != lastxycount[i][j] /* Changed */
333 : xycount[i][j] > lastxycount[i][j])) { /* Growed */
334 PerlIO_printf(PerlIO_stderr(),"%3ld ",
335 flag == 2
336 ? xycount[i][j] - lastxycount[i][j]
337 : xycount[i][j]);
338 lastxycount[i][j] = xycount[i][j];
339 } else {
340 PerlIO_printf(PerlIO_stderr(), " . ", xycount[i][j]);
341 }
342 }
343 PerlIO_printf(PerlIO_stderr(), "\n");
344 }
345 }
346 if (flag != 2) {
347 PerlIO_printf(PerlIO_stderr(), "Total %7ld ", total);
348 for (j = 0; j < MAXYCOUNT; j++) {
349 if (subtot[j]) {
350 PerlIO_printf(PerlIO_stderr(), "%3ld ", subtot[j]);
351 } else {
352 PerlIO_printf(PerlIO_stderr(), " . ");
353 }
8d063cd8 354 }
8c52afec 355 PerlIO_printf(PerlIO_stderr(), "\n");
8d063cd8 356 }
8d063cd8 357}
a687059c
LW
358
359#endif /* LEAKTEST */
8d063cd8
LW
360
361/* copy a string up to some (non-backslashed) delimiter, if any */
362
363char *
8ac85365 364delimcpy(register char *to, register char *toend, register char *from, register char *fromend, register int delim, I32 *retlen)
8d063cd8 365{
fc36a67e 366 register I32 tolen;
367 for (tolen = 0; from < fromend; from++, tolen++) {
378cc40b
LW
368 if (*from == '\\') {
369 if (from[1] == delim)
370 from++;
fc36a67e 371 else {
372 if (to < toend)
373 *to++ = *from;
374 tolen++;
375 from++;
376 }
378cc40b 377 }
bedebaa5 378 else if (*from == delim)
8d063cd8 379 break;
fc36a67e 380 if (to < toend)
381 *to++ = *from;
8d063cd8 382 }
bedebaa5
CS
383 if (to < toend)
384 *to = '\0';
fc36a67e 385 *retlen = tolen;
8d063cd8
LW
386 return from;
387}
388
389/* return ptr to little string in big string, NULL if not found */
378cc40b 390/* This routine was donated by Corey Satten. */
8d063cd8
LW
391
392char *
8ac85365 393instr(register char *big, register char *little)
378cc40b
LW
394{
395 register char *s, *x;
79072805 396 register I32 first;
378cc40b 397
a687059c
LW
398 if (!little)
399 return big;
400 first = *little++;
378cc40b
LW
401 if (!first)
402 return big;
403 while (*big) {
404 if (*big++ != first)
405 continue;
406 for (x=big,s=little; *s; /**/ ) {
407 if (!*x)
408 return Nullch;
409 if (*s++ != *x++) {
410 s--;
411 break;
412 }
413 }
414 if (!*s)
415 return big-1;
416 }
417 return Nullch;
418}
8d063cd8 419
a687059c
LW
420/* same as instr but allow embedded nulls */
421
422char *
8ac85365 423ninstr(register char *big, register char *bigend, char *little, char *lend)
8d063cd8 424{
a687059c 425 register char *s, *x;
79072805 426 register I32 first = *little;
a687059c 427 register char *littleend = lend;
378cc40b 428
a0d0e21e 429 if (!first && little >= littleend)
a687059c 430 return big;
de3bb511
LW
431 if (bigend - big < littleend - little)
432 return Nullch;
a687059c
LW
433 bigend -= littleend - little++;
434 while (big <= bigend) {
435 if (*big++ != first)
436 continue;
437 for (x=big,s=little; s < littleend; /**/ ) {
438 if (*s++ != *x++) {
439 s--;
440 break;
441 }
442 }
443 if (s >= littleend)
444 return big-1;
378cc40b 445 }
a687059c
LW
446 return Nullch;
447}
448
449/* reverse of the above--find last substring */
450
451char *
8ac85365 452rninstr(register char *big, char *bigend, char *little, char *lend)
a687059c
LW
453{
454 register char *bigbeg;
455 register char *s, *x;
79072805 456 register I32 first = *little;
a687059c
LW
457 register char *littleend = lend;
458
a0d0e21e 459 if (!first && little >= littleend)
a687059c
LW
460 return bigend;
461 bigbeg = big;
462 big = bigend - (littleend - little++);
463 while (big >= bigbeg) {
464 if (*big-- != first)
465 continue;
466 for (x=big+2,s=little; s < littleend; /**/ ) {
467 if (*s++ != *x++) {
468 s--;
469 break;
470 }
471 }
472 if (s >= littleend)
473 return big+1;
378cc40b 474 }
a687059c 475 return Nullch;
378cc40b 476}
a687059c 477
bbce6d69 478/*
479 * Set up for a new ctype locale.
480 */
55497cff 481void
8ac85365 482perl_new_ctype(char *newctype)
ef7eada9 483{
36477c24 484#ifdef USE_LOCALE_CTYPE
485
bbce6d69 486 int i;
ef7eada9 487
bbce6d69 488 for (i = 0; i < 256; i++) {
489 if (isUPPER_LC(i))
22c35a8c 490 PL_fold_locale[i] = toLOWER_LC(i);
bbce6d69 491 else if (isLOWER_LC(i))
22c35a8c 492 PL_fold_locale[i] = toUPPER_LC(i);
bbce6d69 493 else
22c35a8c 494 PL_fold_locale[i] = i;
bbce6d69 495 }
bbce6d69 496
36477c24 497#endif /* USE_LOCALE_CTYPE */
498}
bbce6d69 499
500/*
501 * Set up for a new collation locale.
502 */
503void
8ac85365 504perl_new_collate(char *newcoll)
bbce6d69 505{
36477c24 506#ifdef USE_LOCALE_COLLATE
507
bbce6d69 508 if (! newcoll) {
3280af22
NIS
509 if (PL_collation_name) {
510 ++PL_collation_ix;
511 Safefree(PL_collation_name);
512 PL_collation_name = NULL;
513 PL_collation_standard = TRUE;
514 PL_collxfrm_base = 0;
515 PL_collxfrm_mult = 2;
bbce6d69 516 }
517 return;
518 }
519
3280af22
NIS
520 if (! PL_collation_name || strNE(PL_collation_name, newcoll)) {
521 ++PL_collation_ix;
522 Safefree(PL_collation_name);
523 PL_collation_name = savepv(newcoll);
524 PL_collation_standard = (strEQ(newcoll, "C") || strEQ(newcoll, "POSIX"));
bbce6d69 525
bbce6d69 526 {
527 /* 2: at most so many chars ('a', 'b'). */
528 /* 50: surely no system expands a char more. */
529#define XFRMBUFSIZE (2 * 50)
530 char xbuf[XFRMBUFSIZE];
531 Size_t fa = strxfrm(xbuf, "a", XFRMBUFSIZE);
532 Size_t fb = strxfrm(xbuf, "ab", XFRMBUFSIZE);
533 SSize_t mult = fb - fa;
534 if (mult < 1)
535 croak("strxfrm() gets absurd");
3280af22
NIS
536 PL_collxfrm_base = (fa > mult) ? (fa - mult) : 0;
537 PL_collxfrm_mult = mult;
bbce6d69 538 }
bbce6d69 539 }
bbce6d69 540
36477c24 541#endif /* USE_LOCALE_COLLATE */
542}
bbce6d69 543
544/*
545 * Set up for a new numeric locale.
546 */
547void
8ac85365 548perl_new_numeric(char *newnum)
bbce6d69 549{
36477c24 550#ifdef USE_LOCALE_NUMERIC
551
bbce6d69 552 if (! newnum) {
3280af22
NIS
553 if (PL_numeric_name) {
554 Safefree(PL_numeric_name);
555 PL_numeric_name = NULL;
556 PL_numeric_standard = TRUE;
557 PL_numeric_local = TRUE;
bbce6d69 558 }
559 return;
560 }
561
3280af22
NIS
562 if (! PL_numeric_name || strNE(PL_numeric_name, newnum)) {
563 Safefree(PL_numeric_name);
564 PL_numeric_name = savepv(newnum);
565 PL_numeric_standard = (strEQ(newnum, "C") || strEQ(newnum, "POSIX"));
566 PL_numeric_local = TRUE;
bbce6d69 567 }
36477c24 568
569#endif /* USE_LOCALE_NUMERIC */
bbce6d69 570}
571
572void
8ac85365 573perl_set_numeric_standard(void)
bbce6d69 574{
5f05dabc 575#ifdef USE_LOCALE_NUMERIC
576
3280af22 577 if (! PL_numeric_standard) {
bbce6d69 578 setlocale(LC_NUMERIC, "C");
3280af22
NIS
579 PL_numeric_standard = TRUE;
580 PL_numeric_local = FALSE;
bbce6d69 581 }
5f05dabc 582
583#endif /* USE_LOCALE_NUMERIC */
bbce6d69 584}
585
586void
8ac85365 587perl_set_numeric_local(void)
bbce6d69 588{
5f05dabc 589#ifdef USE_LOCALE_NUMERIC
590
3280af22
NIS
591 if (! PL_numeric_local) {
592 setlocale(LC_NUMERIC, PL_numeric_name);
593 PL_numeric_standard = FALSE;
594 PL_numeric_local = TRUE;
bbce6d69 595 }
bbce6d69 596
36477c24 597#endif /* USE_LOCALE_NUMERIC */
5f05dabc 598}
36477c24 599
bbce6d69 600
36477c24 601/*
602 * Initialize locale awareness.
603 */
f0c5b223 604int
8ac85365 605perl_init_i18nl10n(int printwarn)
f0c5b223
TB
606{
607 int ok = 1;
608 /* returns
609 * 1 = set ok or not applicable,
610 * 0 = fallback to C locale,
611 * -1 = fallback to C locale failed
612 */
bbce6d69 613
36477c24 614#ifdef USE_LOCALE
bbce6d69 615
36477c24 616#ifdef USE_LOCALE_CTYPE
bbce6d69 617 char *curctype = NULL;
36477c24 618#endif /* USE_LOCALE_CTYPE */
619#ifdef USE_LOCALE_COLLATE
bbce6d69 620 char *curcoll = NULL;
36477c24 621#endif /* USE_LOCALE_COLLATE */
622#ifdef USE_LOCALE_NUMERIC
bbce6d69 623 char *curnum = NULL;
36477c24 624#endif /* USE_LOCALE_NUMERIC */
76e3520e
GS
625 char *lc_all = PerlEnv_getenv("LC_ALL");
626 char *lang = PerlEnv_getenv("LANG");
bbce6d69 627 bool setlocale_failure = FALSE;
f0c5b223 628
02b32252
CS
629#ifdef LOCALE_ENVIRON_REQUIRED
630
631 /*
632 * Ultrix setlocale(..., "") fails if there are no environment
633 * variables from which to get a locale name.
634 */
635
636 bool done = FALSE;
637
638#ifdef LC_ALL
639 if (lang) {
640 if (setlocale(LC_ALL, ""))
641 done = TRUE;
642 else
643 setlocale_failure = TRUE;
644 }
0644c9cb 645 if (!setlocale_failure) {
02b32252 646#ifdef USE_LOCALE_CTYPE
0644c9cb
IS
647 if (! (curctype =
648 setlocale(LC_CTYPE,
649 (!done && (lang || PerlEnv_getenv("LC_CTYPE")))
02b32252
CS
650 ? "" : Nullch)))
651 setlocale_failure = TRUE;
652#endif /* USE_LOCALE_CTYPE */
653#ifdef USE_LOCALE_COLLATE
0644c9cb
IS
654 if (! (curcoll =
655 setlocale(LC_COLLATE,
656 (!done && (lang || PerlEnv_getenv("LC_COLLATE")))
02b32252
CS
657 ? "" : Nullch)))
658 setlocale_failure = TRUE;
659#endif /* USE_LOCALE_COLLATE */
660#ifdef USE_LOCALE_NUMERIC
0644c9cb
IS
661 if (! (curnum =
662 setlocale(LC_NUMERIC,
663 (!done && (lang || PerlEnv_getenv("LC_NUMERIC")))
02b32252
CS
664 ? "" : Nullch)))
665 setlocale_failure = TRUE;
666#endif /* USE_LOCALE_NUMERIC */
667 }
668
0644c9cb 669#endif /* LC_ALL */
02b32252 670
0644c9cb 671#endif /* !LOCALE_ENVIRON_REQUIRED */
5f05dabc 672
0644c9cb 673#ifdef LC_ALL
bbce6d69 674 if (! setlocale(LC_ALL, ""))
675 setlocale_failure = TRUE;
0644c9cb 676#endif /* LC_ALL */
bbce6d69 677
0644c9cb 678 if (!setlocale_failure) {
36477c24 679#ifdef USE_LOCALE_CTYPE
0644c9cb
IS
680 if (! (curctype = setlocale(LC_CTYPE, "")))
681 setlocale_failure = TRUE;
36477c24 682#endif /* USE_LOCALE_CTYPE */
683#ifdef USE_LOCALE_COLLATE
0644c9cb
IS
684 if (! (curcoll = setlocale(LC_COLLATE, "")))
685 setlocale_failure = TRUE;
36477c24 686#endif /* USE_LOCALE_COLLATE */
687#ifdef USE_LOCALE_NUMERIC
0644c9cb
IS
688 if (! (curnum = setlocale(LC_NUMERIC, "")))
689 setlocale_failure = TRUE;
36477c24 690#endif /* USE_LOCALE_NUMERIC */
0644c9cb 691 }
02b32252 692
5f05dabc 693 if (setlocale_failure) {
694 char *p;
695 bool locwarn = (printwarn > 1 ||
696 printwarn &&
76e3520e 697 (!(p = PerlEnv_getenv("PERL_BADLANG")) || atoi(p)));
20cec16a 698
5f05dabc 699 if (locwarn) {
700#ifdef LC_ALL
701
702 PerlIO_printf(PerlIO_stderr(),
703 "perl: warning: Setting locale failed.\n");
704
705#else /* !LC_ALL */
706
ef7eada9 707 PerlIO_printf(PerlIO_stderr(),
bbce6d69 708 "perl: warning: Setting locale failed for the categories:\n\t");
36477c24 709#ifdef USE_LOCALE_CTYPE
bbce6d69 710 if (! curctype)
5f05dabc 711 PerlIO_printf(PerlIO_stderr(), "LC_CTYPE ");
36477c24 712#endif /* USE_LOCALE_CTYPE */
713#ifdef USE_LOCALE_COLLATE
bbce6d69 714 if (! curcoll)
5f05dabc 715 PerlIO_printf(PerlIO_stderr(), "LC_COLLATE ");
36477c24 716#endif /* USE_LOCALE_COLLATE */
717#ifdef USE_LOCALE_NUMERIC
bbce6d69 718 if (! curnum)
5f05dabc 719 PerlIO_printf(PerlIO_stderr(), "LC_NUMERIC ");
36477c24 720#endif /* USE_LOCALE_NUMERIC */
bbce6d69 721 PerlIO_printf(PerlIO_stderr(), "\n");
722
5f05dabc 723#endif /* LC_ALL */
724
760ac839 725 PerlIO_printf(PerlIO_stderr(),
bbce6d69 726 "perl: warning: Please check that your locale settings:\n");
ef7eada9
JH
727
728 PerlIO_printf(PerlIO_stderr(),
bbce6d69 729 "\tLC_ALL = %c%s%c,\n",
730 lc_all ? '"' : '(',
731 lc_all ? lc_all : "unset",
732 lc_all ? '"' : ')');
5f05dabc 733
734 {
735 char **e;
736 for (e = environ; *e; e++) {
737 if (strnEQ(*e, "LC_", 3)
738 && strnNE(*e, "LC_ALL=", 7)
739 && (p = strchr(*e, '=')))
740 PerlIO_printf(PerlIO_stderr(), "\t%.*s = \"%s\",\n",
fb73857a 741 (int)(p - *e), *e, p + 1);
5f05dabc 742 }
743 }
744
ef7eada9 745 PerlIO_printf(PerlIO_stderr(),
bbce6d69 746 "\tLANG = %c%s%c\n",
5f05dabc 747 lang ? '"' : '(',
bbce6d69 748 lang ? lang : "unset",
749 lang ? '"' : ')');
ef7eada9 750
bbce6d69 751 PerlIO_printf(PerlIO_stderr(),
752 " are supported and installed on your system.\n");
5f05dabc 753 }
ef7eada9 754
5f05dabc 755#ifdef LC_ALL
756
757 if (setlocale(LC_ALL, "C")) {
758 if (locwarn)
759 PerlIO_printf(PerlIO_stderr(),
760 "perl: warning: Falling back to the standard locale (\"C\").\n");
bbce6d69 761 ok = 0;
ef7eada9 762 }
5f05dabc 763 else {
764 if (locwarn)
765 PerlIO_printf(PerlIO_stderr(),
766 "perl: warning: Failed to fall back to the standard locale (\"C\").\n");
767 ok = -1;
768 }
bbce6d69 769
5f05dabc 770#else /* ! LC_ALL */
771
772 if (0
36477c24 773#ifdef USE_LOCALE_CTYPE
5f05dabc 774 || !(curctype || setlocale(LC_CTYPE, "C"))
36477c24 775#endif /* USE_LOCALE_CTYPE */
776#ifdef USE_LOCALE_COLLATE
5f05dabc 777 || !(curcoll || setlocale(LC_COLLATE, "C"))
36477c24 778#endif /* USE_LOCALE_COLLATE */
779#ifdef USE_LOCALE_NUMERIC
5f05dabc 780 || !(curnum || setlocale(LC_NUMERIC, "C"))
36477c24 781#endif /* USE_LOCALE_NUMERIC */
5f05dabc 782 )
783 {
784 if (locwarn)
bbce6d69 785 PerlIO_printf(PerlIO_stderr(),
5f05dabc 786 "perl: warning: Cannot fall back to the standard locale (\"C\").\n");
787 ok = -1;
bbce6d69 788 }
5f05dabc 789
bbce6d69 790#endif /* ! LC_ALL */
5f05dabc 791
792#ifdef USE_LOCALE_CTYPE
793 curctype = setlocale(LC_CTYPE, Nullch);
794#endif /* USE_LOCALE_CTYPE */
795#ifdef USE_LOCALE_COLLATE
796 curcoll = setlocale(LC_COLLATE, Nullch);
797#endif /* USE_LOCALE_COLLATE */
798#ifdef USE_LOCALE_NUMERIC
799 curnum = setlocale(LC_NUMERIC, Nullch);
800#endif /* USE_LOCALE_NUMERIC */
ef7eada9
JH
801 }
802
36477c24 803#ifdef USE_LOCALE_CTYPE
bbce6d69 804 perl_new_ctype(curctype);
36477c24 805#endif /* USE_LOCALE_CTYPE */
bbce6d69 806
36477c24 807#ifdef USE_LOCALE_COLLATE
bbce6d69 808 perl_new_collate(curcoll);
36477c24 809#endif /* USE_LOCALE_COLLATE */
bbce6d69 810
36477c24 811#ifdef USE_LOCALE_NUMERIC
bbce6d69 812 perl_new_numeric(curnum);
36477c24 813#endif /* USE_LOCALE_NUMERIC */
ef7eada9 814
36477c24 815#endif /* USE_LOCALE */
ef7eada9 816
f0c5b223
TB
817 return ok;
818}
819
bbce6d69 820/* Backwards compatibility. */
821int
8ac85365 822perl_init_i18nl14n(int printwarn)
bbce6d69 823{
5f05dabc 824 return perl_init_i18nl10n(printwarn);
bbce6d69 825}
ef7eada9 826
36477c24 827#ifdef USE_LOCALE_COLLATE
ef7eada9 828
bbce6d69 829/*
830 * mem_collxfrm() is a bit like strxfrm() but with two important
831 * differences. First, it handles embedded NULs. Second, it allocates
832 * a bit more memory than needed for the transformed data itself.
833 * The real transformed data begins at offset sizeof(collationix).
834 * Please see sv_collxfrm() to see how this is used.
835 */
836char *
8ac85365 837mem_collxfrm(const char *s, STRLEN len, STRLEN *xlen)
bbce6d69 838{
839 char *xbuf;
76e3520e 840 STRLEN xAlloc, xin, xout; /* xalloc is a reserved word in VC */
bbce6d69 841
842 /* the first sizeof(collationix) bytes are used by sv_collxfrm(). */
843 /* the +1 is for the terminating NUL. */
844
3280af22 845 xAlloc = sizeof(PL_collation_ix) + PL_collxfrm_base + (PL_collxfrm_mult * len) + 1;
76e3520e 846 New(171, xbuf, xAlloc, char);
bbce6d69 847 if (! xbuf)
848 goto bad;
849
3280af22
NIS
850 *(U32*)xbuf = PL_collation_ix;
851 xout = sizeof(PL_collation_ix);
bbce6d69 852 for (xin = 0; xin < len; ) {
853 SSize_t xused;
854
855 for (;;) {
76e3520e 856 xused = strxfrm(xbuf + xout, s + xin, xAlloc - xout);
bbce6d69 857 if (xused == -1)
858 goto bad;
76e3520e 859 if (xused < xAlloc - xout)
bbce6d69 860 break;
76e3520e
GS
861 xAlloc = (2 * xAlloc) + 1;
862 Renew(xbuf, xAlloc, char);
bbce6d69 863 if (! xbuf)
864 goto bad;
865 }
ef7eada9 866
bbce6d69 867 xin += strlen(s + xin) + 1;
868 xout += xused;
869
870 /* Embedded NULs are understood but silently skipped
871 * because they make no sense in locale collation. */
872 }
ef7eada9 873
bbce6d69 874 xbuf[xout] = '\0';
3280af22 875 *xlen = xout - sizeof(PL_collation_ix);
bbce6d69 876 return xbuf;
877
878 bad:
879 Safefree(xbuf);
880 *xlen = 0;
881 return NULL;
ef7eada9
JH
882}
883
36477c24 884#endif /* USE_LOCALE_COLLATE */
bbce6d69 885
378cc40b 886void
2779dcf1 887fbm_compile(SV *sv, U32 flags /* not used yet */)
378cc40b 888{
942e002e
GS
889 register U8 *s;
890 register U8 *table;
79072805 891 register U32 i;
0b71040e 892 STRLEN len;
79072805
LW
893 I32 rarest = 0;
894 U32 frequency = 256;
895
942e002e 896 s = (U8*)SvPV_force(sv, len);
07f14f54 897 (void)SvUPGRADE(sv, SVt_PVBM);
c277df42 898 if (len > 255 || len == 0) /* TAIL might be on on a zero-length string. */
748a9306 899 return; /* can't have offsets that big */
02128f11
IZ
900 if (len > 2) {
901 Sv_Grow(sv,len + 258);
902 table = (unsigned char*)(SvPVX(sv) + len + 1);
903 s = table - 2;
904 for (i = 0; i < 256; i++) {
905 table[i] = len;
906 }
907 i = 0;
908 while (s >= (unsigned char*)(SvPVX(sv)))
909 {
910 if (table[*s] == len)
911 table[*s] = i;
912 s--,i++;
913 }
378cc40b 914 }
bbce6d69 915 sv_magic(sv, Nullsv, 'B', Nullch, 0); /* deep magic */
79072805 916 SvVALID_on(sv);
378cc40b 917
463ee0b2 918 s = (unsigned char*)(SvPVX(sv)); /* deeper magic */
bbce6d69 919 for (i = 0; i < len; i++) {
22c35a8c 920 if (PL_freq[s[i]] < frequency) {
bbce6d69 921 rarest = i;
22c35a8c 922 frequency = PL_freq[s[i]];
378cc40b
LW
923 }
924 }
79072805
LW
925 BmRARE(sv) = s[rarest];
926 BmPREVIOUS(sv) = rarest;
760ac839 927 DEBUG_r(PerlIO_printf(Perl_debug_log, "rarest char %c at %d\n",BmRARE(sv),BmPREVIOUS(sv)));
378cc40b
LW
928}
929
378cc40b 930char *
411d5715 931fbm_instr(unsigned char *big, register unsigned char *bigend, SV *littlestr, U32 flags)
378cc40b 932{
a687059c 933 register unsigned char *s;
79072805
LW
934 register I32 tmp;
935 register I32 littlelen;
a687059c
LW
936 register unsigned char *little;
937 register unsigned char *table;
938 register unsigned char *olds;
939 register unsigned char *oldlittle;
378cc40b 940
79072805 941 if (SvTYPE(littlestr) != SVt_PVBM || !SvVALID(littlestr)) {
a0d0e21e
LW
942 STRLEN len;
943 char *l = SvPV(littlestr,len);
c277df42 944 if (!len) {
02128f11
IZ
945 if (SvTAIL(littlestr)) { /* Can be only 0-len constant
946 substr => we can ignore SvVALID */
3280af22 947 if (PL_multiline) {
02128f11 948 char *t = "\n";
3fe6f2dc
MB
949 if ((s = (unsigned char*)ninstr((char*)big, (char*)bigend,
950 t, t + len))) {
951 return (char*)s;
952 }
02128f11 953 }
c277df42 954 if (bigend > big && bigend[-1] == '\n')
161b471a 955 return (char *)(bigend - 1);
c277df42 956 else
161b471a 957 return (char *) bigend;
c277df42 958 }
d48672a2 959 return (char*)big;
c277df42 960 }
a0d0e21e 961 return ninstr((char*)big,(char*)bigend, l, l + len);
d48672a2 962 }
378cc40b 963
79072805 964 littlelen = SvCUR(littlestr);
3280af22 965 if (SvTAIL(littlestr) && !PL_multiline) { /* tail anchored? */
0f85fab0
LW
966 if (littlelen > bigend - big)
967 return Nullch;
463ee0b2 968 little = (unsigned char*)SvPVX(littlestr);
bbce6d69 969 s = bigend - littlelen;
02128f11
IZ
970 if (s > big
971 && bigend[-1] == '\n'
972 && s[-1] == *little && memEQ((char*)s - 1,(char*)little,littlelen))
973 return (char*)s - 1; /* how sweet it is */
974 else if (*s == *little && memEQ((char*)s,(char*)little,littlelen))
bbce6d69 975 return (char*)s; /* how sweet it is */
02128f11
IZ
976 return Nullch;
977 }
978 if (littlelen <= 2) {
979 unsigned char c1 = (unsigned char)SvPVX(littlestr)[0];
980 unsigned char c2 = (unsigned char)SvPVX(littlestr)[1];
981 /* This may do extra comparisons if littlelen == 2, but this
982 should be hidden in the noise since we do less indirection. */
983
984 s = big;
985 bigend -= littlelen;
986 while (s <= bigend) {
987 if (s[0] == c1
988 && (littlelen == 1 || s[1] == c2)
989 && (!SvTAIL(littlestr)
990 || s == bigend
991 || s[littlelen] == '\n')) /* Automatically multiline */
3fe6f2dc
MB
992 {
993 return (char*)s;
994 }
02128f11 995 s++;
a687059c 996 }
bbce6d69 997 return Nullch;
a687059c 998 }
463ee0b2 999 table = (unsigned char*)(SvPVX(littlestr) + littlelen + 1);
62b28dd9
LW
1000 if (--littlelen >= bigend - big)
1001 return Nullch;
1002 s = big + littlelen;
a687059c 1003 oldlittle = little = table - 2;
bbce6d69 1004 if (s < bigend) {
1005 top2:
1006 /*SUPPRESS 560*/
1007 if (tmp = table[*s]) {
62b28dd9 1008#ifdef POINTERRIGOR
bbce6d69 1009 if (bigend - s > tmp) {
1010 s += tmp;
1011 goto top2;
1012 }
62b28dd9 1013#else
bbce6d69 1014 if ((s += tmp) < bigend)
1015 goto top2;
62b28dd9 1016#endif
bbce6d69 1017 return Nullch;
a687059c 1018 }
bbce6d69 1019 else {
1020 tmp = littlelen; /* less expensive than calling strncmp() */
1021 olds = s;
1022 while (tmp--) {
1023 if (*--s == *--little)
1024 continue;
c277df42 1025 differ:
bbce6d69 1026 s = olds + 1; /* here we pay the price for failure */
1027 little = oldlittle;
1028 if (s < bigend) /* fake up continue to outer loop */
62b28dd9 1029 goto top2;
62b28dd9 1030 return Nullch;
a687059c 1031 }
c277df42
IZ
1032 if (SvTAIL(littlestr) /* automatically multiline */
1033 && olds + 1 != bigend
1034 && olds[1] != '\n')
1035 goto differ;
bbce6d69 1036 return (char *)s;
378cc40b
LW
1037 }
1038 }
1039 return Nullch;
1040}
1041
c277df42
IZ
1042/* start_shift, end_shift are positive quantities which give offsets
1043 of ends of some substring of bigstr.
1044 If `last' we want the last occurence.
1045 old_posp is the way of communication between consequent calls if
1046 the next call needs to find the .
1047 The initial *old_posp should be -1.
1048 Note that we do not take into account SvTAIL, so it may give wrong
1049 positives if _ALL flag is set.
1050 */
1051
378cc40b 1052char *
c277df42 1053screaminstr(SV *bigstr, SV *littlestr, I32 start_shift, I32 end_shift, I32 *old_posp, I32 last)
378cc40b 1054{
5c0ca799 1055 dTHR;
a687059c
LW
1056 register unsigned char *s, *x;
1057 register unsigned char *big;
79072805
LW
1058 register I32 pos;
1059 register I32 previous;
1060 register I32 first;
a687059c 1061 register unsigned char *little;
c277df42 1062 register I32 stop_pos;
a687059c 1063 register unsigned char *littleend;
c277df42 1064 I32 found = 0;
378cc40b 1065
c277df42 1066 if (*old_posp == -1
3280af22
NIS
1067 ? (pos = PL_screamfirst[BmRARE(littlestr)]) < 0
1068 : (((pos = *old_posp), pos += PL_screamnext[pos]) == 0))
378cc40b 1069 return Nullch;
463ee0b2 1070 little = (unsigned char *)(SvPVX(littlestr));
79072805 1071 littleend = little + SvCUR(littlestr);
378cc40b 1072 first = *little++;
c277df42 1073 /* The value of pos we can start at: */
79072805 1074 previous = BmPREVIOUS(littlestr);
463ee0b2 1075 big = (unsigned char *)(SvPVX(bigstr));
c277df42
IZ
1076 /* The value of pos we can stop at: */
1077 stop_pos = SvCUR(bigstr) - end_shift - (SvCUR(littlestr) - 1 - previous);
1078 if (previous + start_shift > stop_pos) return Nullch;
1079 while (pos < previous + start_shift) {
3280af22 1080 if (!(pos += PL_screamnext[pos]))
378cc40b
LW
1081 return Nullch;
1082 }
de3bb511 1083#ifdef POINTERRIGOR
bbce6d69 1084 do {
ef64f398 1085 if (pos >= stop_pos) break;
bbce6d69 1086 if (big[pos-previous] != first)
1087 continue;
1088 for (x=big+pos+1-previous,s=little; s < littleend; /**/ ) {
bbce6d69 1089 if (*s++ != *x++) {
1090 s--;
1091 break;
de3bb511 1092 }
bbce6d69 1093 }
c277df42
IZ
1094 if (s == littleend) {
1095 *old_posp = pos;
1096 if (!last) return (char *)(big+pos-previous);
1097 found = 1;
1098 }
6b88bc9c 1099 } while ( pos += PL_screamnext[pos] );
c277df42 1100 return (last && found) ? (char *)(big+(*old_posp)-previous) : Nullch;
de3bb511
LW
1101#else /* !POINTERRIGOR */
1102 big -= previous;
bbce6d69 1103 do {
ef64f398 1104 if (pos >= stop_pos) break;
bbce6d69 1105 if (big[pos] != first)
1106 continue;
1107 for (x=big+pos+1,s=little; s < littleend; /**/ ) {
bbce6d69 1108 if (*s++ != *x++) {
1109 s--;
1110 break;
378cc40b 1111 }
bbce6d69 1112 }
c277df42
IZ
1113 if (s == littleend) {
1114 *old_posp = pos;
1115 if (!last) return (char *)(big+pos);
1116 found = 1;
1117 }
3280af22 1118 } while ( pos += PL_screamnext[pos] );
c277df42 1119 return (last && found) ? (char *)(big+(*old_posp)) : Nullch;
de3bb511 1120#endif /* POINTERRIGOR */
8d063cd8
LW
1121}
1122
79072805 1123I32
8ac85365 1124ibcmp(char *s1, char *s2, register I32 len)
79072805 1125{
bbce6d69 1126 register U8 *a = (U8 *)s1;
1127 register U8 *b = (U8 *)s2;
79072805 1128 while (len--) {
22c35a8c 1129 if (*a != *b && *a != PL_fold[*b])
bbce6d69 1130 return 1;
1131 a++,b++;
1132 }
1133 return 0;
1134}
1135
1136I32
8ac85365 1137ibcmp_locale(char *s1, char *s2, register I32 len)
bbce6d69 1138{
1139 register U8 *a = (U8 *)s1;
1140 register U8 *b = (U8 *)s2;
1141 while (len--) {
22c35a8c 1142 if (*a != *b && *a != PL_fold_locale[*b])
bbce6d69 1143 return 1;
1144 a++,b++;
79072805
LW
1145 }
1146 return 0;
1147}
1148
8d063cd8
LW
1149/* copy a string to a safe spot */
1150
1151char *
8ac85365 1152savepv(char *sv)
8d063cd8 1153{
a687059c 1154 register char *newaddr;
8d063cd8 1155
79072805
LW
1156 New(902,newaddr,strlen(sv)+1,char);
1157 (void)strcpy(newaddr,sv);
8d063cd8
LW
1158 return newaddr;
1159}
1160
a687059c
LW
1161/* same thing but with a known length */
1162
1163char *
8ac85365 1164savepvn(char *sv, register I32 len)
a687059c
LW
1165{
1166 register char *newaddr;
1167
1168 New(903,newaddr,len+1,char);
79072805 1169 Copy(sv,newaddr,len,char); /* might not be null terminated */
a687059c
LW
1170 newaddr[len] = '\0'; /* is now */
1171 return newaddr;
1172}
1173
fc36a67e 1174/* the SV for form() and mess() is not kept in an arena */
1175
76e3520e 1176STATIC SV *
8ac85365 1177mess_alloc(void)
fc36a67e 1178{
e72dc28c 1179 dTHR;
fc36a67e 1180 SV *sv;
1181 XPVMG *any;
1182
e72dc28c
GS
1183 if (!PL_dirty)
1184 return sv_2mortal(newSVpvn("",0));
1185
0372dbb6
GS
1186 if (PL_mess_sv)
1187 return PL_mess_sv;
1188
fc36a67e 1189 /* Create as PVMG now, to avoid any upgrading later */
1190 New(905, sv, 1, SV);
1191 Newz(905, any, 1, XPVMG);
1192 SvFLAGS(sv) = SVt_PVMG;
1193 SvANY(sv) = (void*)any;
1194 SvREFCNT(sv) = 1 << 30; /* practically infinite */
e72dc28c 1195 PL_mess_sv = sv;
fc36a67e 1196 return sv;
1197}
1198
8990e307 1199char *
46fc3d4c 1200form(const char* pat, ...)
8990e307 1201{
e72dc28c 1202 SV *sv = mess_alloc();
46fc3d4c 1203 va_list args;
46fc3d4c 1204 va_start(args, pat);
e72dc28c 1205 sv_vsetpvfn(sv, pat, strlen(pat), &args, Null(SV**), 0, Null(bool*));
46fc3d4c 1206 va_end(args);
e72dc28c 1207 return SvPVX(sv);
46fc3d4c 1208}
a687059c 1209
46fc3d4c 1210char *
8ac85365 1211mess(const char *pat, va_list *args)
46fc3d4c 1212{
e72dc28c 1213 SV *sv = mess_alloc();
46fc3d4c 1214 static char dgd[] = " during global destruction.\n";
1215
fc36a67e 1216 sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*));
46fc3d4c 1217 if (!SvCUR(sv) || *(SvEND(sv) - 1) != '\n') {
e858de61 1218 dTHR;
3280af22 1219 if (PL_dirty)
46fc3d4c 1220 sv_catpv(sv, dgd);
2304df62 1221 else {
3280af22 1222 if (PL_curcop->cop_line)
fc36a67e 1223 sv_catpvf(sv, " at %_ line %ld",
3280af22
NIS
1224 GvSV(PL_curcop->cop_filegv), (long)PL_curcop->cop_line);
1225 if (GvIO(PL_last_in_gv) && IoLINES(GvIOp(PL_last_in_gv))) {
1226 bool line_mode = (RsSIMPLE(PL_rs) &&
1227 SvLEN(PL_rs) == 1 && *SvPVX(PL_rs) == '\n');
46fc3d4c 1228 sv_catpvf(sv, ", <%s> %s %ld",
3280af22 1229 PL_last_in_gv == PL_argvgv ? "" : GvNAME(PL_last_in_gv),
46fc3d4c 1230 line_mode ? "line" : "chunk",
3280af22 1231 (long)IoLINES(GvIOp(PL_last_in_gv)));
2304df62 1232 }
46fc3d4c 1233 sv_catpv(sv, ".\n");
a687059c 1234 }
a687059c 1235 }
46fc3d4c 1236 return SvPVX(sv);
a687059c
LW
1237}
1238
36477c24 1239OP *
71be2cbc 1240die(const char* pat, ...)
36477c24 1241{
5dc0d613 1242 dTHR;
36477c24 1243 va_list args;
1244 char *message;
3280af22 1245 int was_in_eval = PL_in_eval;
36477c24 1246 HV *stash;
1247 GV *gv;
1248 CV *cv;
1249
8b73bbec 1250 DEBUG_S(PerlIO_printf(PerlIO_stderr(),
199100c8 1251 "%p: die: curstack = %p, mainstack = %p\n",
533c011a 1252 thr, PL_curstack, PL_mainstack));
36477c24 1253
36477c24 1254 va_start(args, pat);
4e6ea2c3 1255 message = pat ? mess(pat, &args) : Nullch;
36477c24 1256 va_end(args);
1257
8b73bbec 1258 DEBUG_S(PerlIO_printf(PerlIO_stderr(),
199100c8 1259 "%p: die: message = %s\ndiehook = %p\n",
533c011a 1260 thr, message, PL_diehook));
3280af22 1261 if (PL_diehook) {
1738f5c4 1262 /* sv_2cv might call croak() */
3280af22 1263 SV *olddiehook = PL_diehook;
1738f5c4 1264 ENTER;
3280af22
NIS
1265 SAVESPTR(PL_diehook);
1266 PL_diehook = Nullsv;
1738f5c4
CS
1267 cv = sv_2cv(olddiehook, &stash, &gv, 0);
1268 LEAVE;
1269 if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) {
1270 dSP;
774d564b 1271 SV *msg;
1272
1273 ENTER;
4e6ea2c3
GS
1274 if(message) {
1275 msg = newSVpv(message, 0);
1276 SvREADONLY_on(msg);
1277 SAVEFREESV(msg);
1278 }
1279 else {
1280 msg = ERRSV;
1281 }
1738f5c4 1282
e788e7d3 1283 PUSHSTACKi(PERLSI_DIEHOOK);
924508f0 1284 PUSHMARK(SP);
1738f5c4
CS
1285 XPUSHs(msg);
1286 PUTBACK;
1287 perl_call_sv((SV*)cv, G_DISCARD);
d3acc0f7 1288 POPSTACK;
774d564b 1289 LEAVE;
1738f5c4 1290 }
36477c24 1291 }
1292
3280af22 1293 PL_restartop = die_where(message);
8b73bbec 1294 DEBUG_S(PerlIO_printf(PerlIO_stderr(),
7c06b590 1295 "%p: die: restartop = %p, was_in_eval = %d, top_env = %p\n",
533c011a 1296 thr, PL_restartop, was_in_eval, PL_top_env));
3280af22 1297 if ((!PL_restartop && was_in_eval) || PL_top_env->je_prev)
6224f72b 1298 JMPENV_JUMP(3);
3280af22 1299 return PL_restartop;
36477c24 1300}
1301
79072805 1302void
71be2cbc 1303croak(const char* pat, ...)
a687059c 1304{
11343788 1305 dTHR;
a687059c 1306 va_list args;
de3bb511 1307 char *message;
748a9306
LW
1308 HV *stash;
1309 GV *gv;
1310 CV *cv;
a687059c 1311
8990e307 1312 va_start(args, pat);
2304df62 1313 message = mess(pat, &args);
a687059c 1314 va_end(args);
8b73bbec 1315 DEBUG_S(PerlIO_printf(PerlIO_stderr(), "croak: 0x%lx %s", (unsigned long) thr, message));
3280af22 1316 if (PL_diehook) {
1738f5c4 1317 /* sv_2cv might call croak() */
3280af22 1318 SV *olddiehook = PL_diehook;
1738f5c4 1319 ENTER;
3280af22
NIS
1320 SAVESPTR(PL_diehook);
1321 PL_diehook = Nullsv;
20cec16a 1322 cv = sv_2cv(olddiehook, &stash, &gv, 0);
1738f5c4
CS
1323 LEAVE;
1324 if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) {
20cec16a 1325 dSP;
774d564b 1326 SV *msg;
1327
1328 ENTER;
1329 msg = newSVpv(message, 0);
1330 SvREADONLY_on(msg);
1331 SAVEFREESV(msg);
20cec16a 1332
e788e7d3 1333 PUSHSTACKi(PERLSI_DIEHOOK);
924508f0 1334 PUSHMARK(SP);
1738f5c4 1335 XPUSHs(msg);
20cec16a 1336 PUTBACK;
1337 perl_call_sv((SV*)cv, G_DISCARD);
d3acc0f7 1338 POPSTACK;
774d564b 1339 LEAVE;
20cec16a 1340 }
748a9306 1341 }
3280af22
NIS
1342 if (PL_in_eval) {
1343 PL_restartop = die_where(message);
6224f72b 1344 JMPENV_JUMP(3);
a0d0e21e 1345 }
760ac839
LW
1346 PerlIO_puts(PerlIO_stderr(),message);
1347 (void)PerlIO_flush(PerlIO_stderr());
f86702cc 1348 my_failure_exit();
a687059c
LW
1349}
1350
8990e307 1351void
71be2cbc 1352warn(const char* pat,...)
a687059c
LW
1353{
1354 va_list args;
de3bb511 1355 char *message;
748a9306
LW
1356 HV *stash;
1357 GV *gv;
1358 CV *cv;
a687059c 1359
8990e307 1360 va_start(args, pat);
2304df62 1361 message = mess(pat, &args);
a687059c
LW
1362 va_end(args);
1363
3280af22 1364 if (PL_warnhook) {
1738f5c4 1365 /* sv_2cv might call warn() */
11343788 1366 dTHR;
3280af22 1367 SV *oldwarnhook = PL_warnhook;
1738f5c4 1368 ENTER;
3280af22
NIS
1369 SAVESPTR(PL_warnhook);
1370 PL_warnhook = Nullsv;
20cec16a 1371 cv = sv_2cv(oldwarnhook, &stash, &gv, 0);
1738f5c4
CS
1372 LEAVE;
1373 if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) {
20cec16a 1374 dSP;
774d564b 1375 SV *msg;
1376
1377 ENTER;
1378 msg = newSVpv(message, 0);
1379 SvREADONLY_on(msg);
1380 SAVEFREESV(msg);
1381
e788e7d3 1382 PUSHSTACKi(PERLSI_WARNHOOK);
924508f0 1383 PUSHMARK(SP);
774d564b 1384 XPUSHs(msg);
20cec16a 1385 PUTBACK;
1386 perl_call_sv((SV*)cv, G_DISCARD);
d3acc0f7 1387 POPSTACK;
774d564b 1388 LEAVE;
20cec16a 1389 return;
1390 }
748a9306 1391 }
20cec16a 1392 PerlIO_puts(PerlIO_stderr(),message);
a687059c 1393#ifdef LEAKTEST
8c52afec
IZ
1394 DEBUG_L(*message == '!'
1395 ? (xstat(message[1]=='!'
1396 ? (message[2]=='!' ? 2 : 1)
1397 : 0)
1398 , 0)
1399 : 0);
a687059c 1400#endif
20cec16a 1401 (void)PerlIO_flush(PerlIO_stderr());
a687059c 1402}
8d063cd8 1403
599cee73
PM
1404void
1405warner(U32 err, const char* pat,...)
1406{
d008e5eb 1407 dTHR;
599cee73
PM
1408 va_list args;
1409 char *message;
1410 HV *stash;
1411 GV *gv;
1412 CV *cv;
1413
1414 va_start(args, pat);
1415 message = mess(pat, &args);
1416 va_end(args);
1417
1418 if (ckDEAD(err)) {
1419#ifdef USE_THREADS
d008e5eb 1420 DEBUG_S(PerlIO_printf(PerlIO_stderr(), "croak: 0x%lx %s", (unsigned long) thr, message));
599cee73
PM
1421#endif /* USE_THREADS */
1422 if (PL_diehook) {
1423 /* sv_2cv might call croak() */
1424 SV *olddiehook = PL_diehook;
1425 ENTER;
1426 SAVESPTR(PL_diehook);
1427 PL_diehook = Nullsv;
1428 cv = sv_2cv(olddiehook, &stash, &gv, 0);
1429 LEAVE;
1430 if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) {
1431 dSP;
1432 SV *msg;
1433
1434 ENTER;
1435 msg = newSVpv(message, 0);
1436 SvREADONLY_on(msg);
1437 SAVEFREESV(msg);
1438
1439 PUSHMARK(sp);
1440 XPUSHs(msg);
1441 PUTBACK;
1442 perl_call_sv((SV*)cv, G_DISCARD);
1443
1444 LEAVE;
1445 }
1446 }
1447 if (PL_in_eval) {
1448 PL_restartop = die_where(message);
1449 JMPENV_JUMP(3);
1450 }
1451 PerlIO_puts(PerlIO_stderr(),message);
1452 (void)PerlIO_flush(PerlIO_stderr());
1453 my_failure_exit();
1454
1455 }
1456 else {
1457 if (PL_warnhook) {
1458 /* sv_2cv might call warn() */
1459 dTHR;
1460 SV *oldwarnhook = PL_warnhook;
1461 ENTER;
1462 SAVESPTR(PL_warnhook);
1463 PL_warnhook = Nullsv;
1464 cv = sv_2cv(oldwarnhook, &stash, &gv, 0);
1465 LEAVE;
1466 if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) {
1467 dSP;
1468 SV *msg;
1469
1470 ENTER;
1471 msg = newSVpv(message, 0);
1472 SvREADONLY_on(msg);
1473 SAVEFREESV(msg);
1474
1475 PUSHMARK(sp);
1476 XPUSHs(msg);
1477 PUTBACK;
1478 perl_call_sv((SV*)cv, G_DISCARD);
1479
1480 LEAVE;
1481 return;
1482 }
1483 }
1484 PerlIO_puts(PerlIO_stderr(),message);
1485#ifdef LEAKTEST
1486 DEBUG_L(xstat());
1487#endif
1488 (void)PerlIO_flush(PerlIO_stderr());
1489 }
1490}
1491
a0d0e21e 1492#ifndef VMS /* VMS' my_setenv() is in VMS.c */
3e3baf6d 1493#ifndef WIN32
8d063cd8 1494void
8ac85365 1495my_setenv(char *nam, char *val)
8d063cd8 1496{
79072805 1497 register I32 i=setenv_getix(nam); /* where does it go? */
8d063cd8 1498
3280af22 1499 if (environ == PL_origenviron) { /* need we copy environment? */
79072805
LW
1500 I32 j;
1501 I32 max;
fe14fcc3
LW
1502 char **tmpenv;
1503
de3bb511 1504 /*SUPPRESS 530*/
fe14fcc3
LW
1505 for (max = i; environ[max]; max++) ;
1506 New(901,tmpenv, max+2, char*);
1507 for (j=0; j<max; j++) /* copy environment */
a0d0e21e 1508 tmpenv[j] = savepv(environ[j]);
fe14fcc3
LW
1509 tmpenv[max] = Nullch;
1510 environ = tmpenv; /* tell exec where it is now */
1511 }
a687059c 1512 if (!val) {
e5ebf479 1513 Safefree(environ[i]);
a687059c
LW
1514 while (environ[i]) {
1515 environ[i] = environ[i+1];
1516 i++;
1517 }
1518 return;
1519 }
8d063cd8 1520 if (!environ[i]) { /* does not exist yet */
fe14fcc3 1521 Renew(environ, i+2, char*); /* just expand it a bit */
8d063cd8
LW
1522 environ[i+1] = Nullch; /* make sure it's null terminated */
1523 }
fe14fcc3
LW
1524 else
1525 Safefree(environ[i]);
a687059c 1526 New(904, environ[i], strlen(nam) + strlen(val) + 2, char);
62b28dd9 1527#ifndef MSDOS
a687059c 1528 (void)sprintf(environ[i],"%s=%s",nam,val);/* all that work just for this */
62b28dd9
LW
1529#else
1530 /* MS-DOS requires environment variable names to be in uppercase */
fe14fcc3
LW
1531 /* [Tom Dinger, 27 August 1990: Well, it doesn't _require_ it, but
1532 * some utilities and applications may break because they only look
1533 * for upper case strings. (Fixed strupr() bug here.)]
1534 */
1535 strcpy(environ[i],nam); strupr(environ[i]);
62b28dd9
LW
1536 (void)sprintf(environ[i] + strlen(nam),"=%s",val);
1537#endif /* MSDOS */
8d063cd8
LW
1538}
1539
3e3baf6d 1540#else /* if WIN32 */
68dc0745 1541
1542void
4e35701f 1543my_setenv(char *nam,char *val)
68dc0745 1544{
3e3baf6d
TB
1545
1546#ifdef USE_WIN32_RTL_ENV
1547
68dc0745 1548 register char *envstr;
1549 STRLEN namlen = strlen(nam);
3e3baf6d
TB
1550 STRLEN vallen;
1551 char *oldstr = environ[setenv_getix(nam)];
1552
1553 /* putenv() has totally broken semantics in both the Borland
1554 * and Microsoft CRTLs. They either store the passed pointer in
1555 * the environment without making a copy, or make a copy and don't
1556 * free it. And on top of that, they dont free() old entries that
1557 * are being replaced/deleted. This means the caller must
1558 * free any old entries somehow, or we end up with a memory
1559 * leak every time my_setenv() is called. One might think
1560 * one could directly manipulate environ[], like the UNIX code
1561 * above, but direct changes to environ are not allowed when
1562 * calling putenv(), since the RTLs maintain an internal
1563 * *copy* of environ[]. Bad, bad, *bad* stink.
1564 * GSAR 97-06-07
1565 */
68dc0745 1566
3e3baf6d
TB
1567 if (!val) {
1568 if (!oldstr)
1569 return;
1570 val = "";
1571 vallen = 0;
1572 }
1573 else
1574 vallen = strlen(val);
fc36a67e 1575 New(904, envstr, namlen + vallen + 3, char);
68dc0745 1576 (void)sprintf(envstr,"%s=%s",nam,val);
76e3520e 1577 (void)PerlEnv_putenv(envstr);
3e3baf6d
TB
1578 if (oldstr)
1579 Safefree(oldstr);
1580#ifdef _MSC_VER
1581 Safefree(envstr); /* MSVCRT leaks without this */
1582#endif
1583
1584#else /* !USE_WIN32_RTL_ENV */
1585
1586 /* The sane way to deal with the environment.
1587 * Has these advantages over putenv() & co.:
1588 * * enables us to store a truly empty value in the
1589 * environment (like in UNIX).
1590 * * we don't have to deal with RTL globals, bugs and leaks.
1591 * * Much faster.
1592 * Why you may want to enable USE_WIN32_RTL_ENV:
1593 * * environ[] and RTL functions will not reflect changes,
1594 * which might be an issue if extensions want to access
1595 * the env. via RTL. This cuts both ways, since RTL will
1596 * not see changes made by extensions that call the Win32
1597 * functions directly, either.
1598 * GSAR 97-06-07
1599 */
1600 SetEnvironmentVariable(nam,val);
1601
1602#endif
1603}
1604
1605#endif /* WIN32 */
1606
1607I32
8ac85365 1608setenv_getix(char *nam)
3e3baf6d
TB
1609{
1610 register I32 i, len = strlen(nam);
1611
1612 for (i = 0; environ[i]; i++) {
1613 if (
1614#ifdef WIN32
1615 strnicmp(environ[i],nam,len) == 0
1616#else
1617 strnEQ(environ[i],nam,len)
1618#endif
1619 && environ[i][len] == '=')
1620 break; /* strnEQ must come first to avoid */
1621 } /* potential SEGV's */
1622 return i;
68dc0745 1623}
1624
a0d0e21e 1625#endif /* !VMS */
378cc40b 1626
16d20bd9 1627#ifdef UNLINK_ALL_VERSIONS
79072805 1628I32
378cc40b
LW
1629unlnk(f) /* unlink all versions of a file */
1630char *f;
1631{
79072805 1632 I32 i;
378cc40b 1633
6ad3d225 1634 for (i = 0; PerlLIO_unlink(f) >= 0; i++) ;
378cc40b
LW
1635 return i ? 0 : -1;
1636}
1637#endif
1638
85e6fe83 1639#if !defined(HAS_BCOPY) || !defined(HAS_SAFE_BCOPY)
378cc40b 1640char *
4e35701f 1641my_bcopy(register char *from,register char *to,register I32 len)
378cc40b
LW
1642{
1643 char *retval = to;
1644
7c0587c8
LW
1645 if (from - to >= 0) {
1646 while (len--)
1647 *to++ = *from++;
1648 }
1649 else {
1650 to += len;
1651 from += len;
1652 while (len--)
faf8582f 1653 *(--to) = *(--from);
7c0587c8 1654 }
378cc40b
LW
1655 return retval;
1656}
ffed7fef 1657#endif
378cc40b 1658
fc36a67e 1659#ifndef HAS_MEMSET
1660void *
1661my_memset(loc,ch,len)
1662register char *loc;
1663register I32 ch;
1664register I32 len;
1665{
1666 char *retval = loc;
1667
1668 while (len--)
1669 *loc++ = ch;
1670 return retval;
1671}
1672#endif
1673
7c0587c8 1674#if !defined(HAS_BZERO) && !defined(HAS_MEMSET)
378cc40b 1675char *
7c0587c8 1676my_bzero(loc,len)
378cc40b 1677register char *loc;
79072805 1678register I32 len;
378cc40b
LW
1679{
1680 char *retval = loc;
1681
1682 while (len--)
1683 *loc++ = 0;
1684 return retval;
1685}
1686#endif
7c0587c8 1687
36477c24 1688#if !defined(HAS_MEMCMP) || !defined(HAS_SANE_MEMCMP)
79072805 1689I32
7c0587c8 1690my_memcmp(s1,s2,len)
36477c24 1691char *s1;
1692char *s2;
79072805 1693register I32 len;
7c0587c8 1694{
36477c24 1695 register U8 *a = (U8 *)s1;
1696 register U8 *b = (U8 *)s2;
79072805 1697 register I32 tmp;
7c0587c8
LW
1698
1699 while (len--) {
36477c24 1700 if (tmp = *a++ - *b++)
7c0587c8
LW
1701 return tmp;
1702 }
1703 return 0;
1704}
36477c24 1705#endif /* !HAS_MEMCMP || !HAS_SANE_MEMCMP */
a687059c 1706
fe14fcc3 1707#ifndef HAS_VPRINTF
a687059c 1708
85e6fe83 1709#ifdef USE_CHAR_VSPRINTF
a687059c
LW
1710char *
1711#else
1712int
1713#endif
1714vsprintf(dest, pat, args)
71be2cbc 1715char *dest;
1716const char *pat;
1717char *args;
a687059c
LW
1718{
1719 FILE fakebuf;
1720
1721 fakebuf._ptr = dest;
1722 fakebuf._cnt = 32767;
35c8bce7
LW
1723#ifndef _IOSTRG
1724#define _IOSTRG 0
1725#endif
a687059c
LW
1726 fakebuf._flag = _IOWRT|_IOSTRG;
1727 _doprnt(pat, args, &fakebuf); /* what a kludge */
1728 (void)putc('\0', &fakebuf);
85e6fe83 1729#ifdef USE_CHAR_VSPRINTF
a687059c
LW
1730 return(dest);
1731#else
1732 return 0; /* perl doesn't use return value */
1733#endif
1734}
1735
fe14fcc3 1736#endif /* HAS_VPRINTF */
a687059c
LW
1737
1738#ifdef MYSWAP
ffed7fef 1739#if BYTEORDER != 0x4321
a687059c 1740short
748a9306 1741my_swap(short s)
a687059c
LW
1742{
1743#if (BYTEORDER & 1) == 0
1744 short result;
1745
1746 result = ((s & 255) << 8) + ((s >> 8) & 255);
1747 return result;
1748#else
1749 return s;
1750#endif
1751}
1752
1753long
748a9306 1754my_htonl(long l)
a687059c
LW
1755{
1756 union {
1757 long result;
ffed7fef 1758 char c[sizeof(long)];
a687059c
LW
1759 } u;
1760
ffed7fef 1761#if BYTEORDER == 0x1234
a687059c
LW
1762 u.c[0] = (l >> 24) & 255;
1763 u.c[1] = (l >> 16) & 255;
1764 u.c[2] = (l >> 8) & 255;
1765 u.c[3] = l & 255;
1766 return u.result;
1767#else
ffed7fef 1768#if ((BYTEORDER - 0x1111) & 0x444) || !(BYTEORDER & 0xf)
463ee0b2 1769 croak("Unknown BYTEORDER\n");
a687059c 1770#else
79072805
LW
1771 register I32 o;
1772 register I32 s;
a687059c 1773
ffed7fef
LW
1774 for (o = BYTEORDER - 0x1111, s = 0; s < (sizeof(long)*8); o >>= 4, s += 8) {
1775 u.c[o & 0xf] = (l >> s) & 255;
a687059c
LW
1776 }
1777 return u.result;
1778#endif
1779#endif
1780}
1781
1782long
748a9306 1783my_ntohl(long l)
a687059c
LW
1784{
1785 union {
1786 long l;
ffed7fef 1787 char c[sizeof(long)];
a687059c
LW
1788 } u;
1789
ffed7fef 1790#if BYTEORDER == 0x1234
a687059c
LW
1791 u.c[0] = (l >> 24) & 255;
1792 u.c[1] = (l >> 16) & 255;
1793 u.c[2] = (l >> 8) & 255;
1794 u.c[3] = l & 255;
1795 return u.l;
1796#else
ffed7fef 1797#if ((BYTEORDER - 0x1111) & 0x444) || !(BYTEORDER & 0xf)
463ee0b2 1798 croak("Unknown BYTEORDER\n");
a687059c 1799#else
79072805
LW
1800 register I32 o;
1801 register I32 s;
a687059c
LW
1802
1803 u.l = l;
1804 l = 0;
ffed7fef
LW
1805 for (o = BYTEORDER - 0x1111, s = 0; s < (sizeof(long)*8); o >>= 4, s += 8) {
1806 l |= (u.c[o & 0xf] & 255) << s;
a687059c
LW
1807 }
1808 return l;
1809#endif
1810#endif
1811}
1812
ffed7fef 1813#endif /* BYTEORDER != 0x4321 */
988174c1
LW
1814#endif /* MYSWAP */
1815
1816/*
1817 * Little-endian byte order functions - 'v' for 'VAX', or 'reVerse'.
1818 * If these functions are defined,
1819 * the BYTEORDER is neither 0x1234 nor 0x4321.
1820 * However, this is not assumed.
1821 * -DWS
1822 */
1823
1824#define HTOV(name,type) \
1825 type \
1826 name (n) \
1827 register type n; \
1828 { \
1829 union { \
1830 type value; \
1831 char c[sizeof(type)]; \
1832 } u; \
79072805
LW
1833 register I32 i; \
1834 register I32 s; \
988174c1
LW
1835 for (i = 0, s = 0; i < sizeof(u.c); i++, s += 8) { \
1836 u.c[i] = (n >> s) & 0xFF; \
1837 } \
1838 return u.value; \
1839 }
1840
1841#define VTOH(name,type) \
1842 type \
1843 name (n) \
1844 register type n; \
1845 { \
1846 union { \
1847 type value; \
1848 char c[sizeof(type)]; \
1849 } u; \
79072805
LW
1850 register I32 i; \
1851 register I32 s; \
988174c1
LW
1852 u.value = n; \
1853 n = 0; \
1854 for (i = 0, s = 0; i < sizeof(u.c); i++, s += 8) { \
1855 n += (u.c[i] & 0xFF) << s; \
1856 } \
1857 return n; \
1858 }
1859
1860#if defined(HAS_HTOVS) && !defined(htovs)
1861HTOV(htovs,short)
1862#endif
1863#if defined(HAS_HTOVL) && !defined(htovl)
1864HTOV(htovl,long)
1865#endif
1866#if defined(HAS_VTOHS) && !defined(vtohs)
1867VTOH(vtohs,short)
1868#endif
1869#if defined(HAS_VTOHL) && !defined(vtohl)
1870VTOH(vtohl,long)
1871#endif
a687059c 1872
5f05dabc 1873 /* VMS' my_popen() is in VMS.c, same with OS/2. */
092bebab 1874#if (!defined(DOSISH) || defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS) && !defined(__OPEN_VM)
760ac839 1875PerlIO *
8ac85365 1876my_popen(char *cmd, char *mode)
a687059c
LW
1877{
1878 int p[2];
8ac85365 1879 register I32 This, that;
79072805
LW
1880 register I32 pid;
1881 SV *sv;
1738f5c4 1882 I32 doexec = strNE(cmd,"-");
a687059c 1883
ddcf38b7
IZ
1884#ifdef OS2
1885 if (doexec) {
1886 return my_syspopen(cmd,mode);
1887 }
1888#endif
8ac85365
NIS
1889 This = (*mode == 'w');
1890 that = !This;
3280af22 1891 if (doexec && PL_tainting) {
bbce6d69 1892 taint_env();
1893 taint_proper("Insecure %s%s", "EXEC");
d48672a2 1894 }
c2267164
IZ
1895 if (PerlProc_pipe(p) < 0)
1896 return Nullfp;
a687059c
LW
1897 while ((pid = (doexec?vfork():fork())) < 0) {
1898 if (errno != EAGAIN) {
6ad3d225 1899 PerlLIO_close(p[This]);
a687059c 1900 if (!doexec)
463ee0b2 1901 croak("Can't fork");
a687059c
LW
1902 return Nullfp;
1903 }
1904 sleep(5);
1905 }
1906 if (pid == 0) {
79072805
LW
1907 GV* tmpgv;
1908
30ac6d9b
GS
1909#undef THIS
1910#undef THAT
a687059c 1911#define THIS that
8ac85365 1912#define THAT This
6ad3d225 1913 PerlLIO_close(p[THAT]);
a687059c 1914 if (p[THIS] != (*mode == 'r')) {
6ad3d225
GS
1915 PerlLIO_dup2(p[THIS], *mode == 'r');
1916 PerlLIO_close(p[THIS]);
a687059c
LW
1917 }
1918 if (doexec) {
a0d0e21e 1919#if !defined(HAS_FCNTL) || !defined(F_SETFD)
ae986130
LW
1920 int fd;
1921
1922#ifndef NOFILE
1923#define NOFILE 20
1924#endif
6b88bc9c 1925 for (fd = PL_maxsysfd + 1; fd < NOFILE; fd++)
6ad3d225 1926 PerlLIO_close(fd);
ae986130 1927#endif
a687059c 1928 do_exec(cmd); /* may or may not use the shell */
6ad3d225 1929 PerlProc__exit(1);
a687059c 1930 }
de3bb511 1931 /*SUPPRESS 560*/
85e6fe83 1932 if (tmpgv = gv_fetchpv("$",TRUE, SVt_PV))
1e422769 1933 sv_setiv(GvSV(tmpgv), (IV)getpid());
3280af22
NIS
1934 PL_forkprocess = 0;
1935 hv_clear(PL_pidstatus); /* we have no children */
a687059c
LW
1936 return Nullfp;
1937#undef THIS
1938#undef THAT
1939 }
62b28dd9 1940 do_execfree(); /* free any memory malloced by child on vfork */
6ad3d225 1941 PerlLIO_close(p[that]);
8ac85365 1942 if (p[that] < p[This]) {
6ad3d225
GS
1943 PerlLIO_dup2(p[This], p[that]);
1944 PerlLIO_close(p[This]);
8ac85365 1945 p[This] = p[that];
62b28dd9 1946 }
3280af22 1947 sv = *av_fetch(PL_fdpid,p[This],TRUE);
a0d0e21e 1948 (void)SvUPGRADE(sv,SVt_IV);
463ee0b2 1949 SvIVX(sv) = pid;
3280af22 1950 PL_forkprocess = pid;
8ac85365 1951 return PerlIO_fdopen(p[This], mode);
a687059c 1952}
7c0587c8 1953#else
55497cff 1954#if defined(atarist) || defined(DJGPP)
7c0587c8 1955FILE *popen();
760ac839 1956PerlIO *
79072805 1957my_popen(cmd,mode)
7c0587c8
LW
1958char *cmd;
1959char *mode;
1960{
760ac839 1961 /* Needs work for PerlIO ! */
55497cff 1962 /* used 0 for 2nd parameter to PerlIO-exportFILE; apparently not used */
1963 return popen(PerlIO_exportFILE(cmd, 0), mode);
7c0587c8
LW
1964}
1965#endif
1966
1967#endif /* !DOSISH */
a687059c 1968
748a9306 1969#ifdef DUMP_FDS
35ff7856
GS
1970void
1971dump_fds(char *s)
ae986130
LW
1972{
1973 int fd;
1974 struct stat tmpstatbuf;
1975
760ac839 1976 PerlIO_printf(PerlIO_stderr(),"%s", s);
ae986130 1977 for (fd = 0; fd < 32; fd++) {
6ad3d225 1978 if (PerlLIO_fstat(fd,&tmpstatbuf) >= 0)
760ac839 1979 PerlIO_printf(PerlIO_stderr()," %d",fd);
ae986130 1980 }
760ac839 1981 PerlIO_printf(PerlIO_stderr(),"\n");
ae986130 1982}
35ff7856 1983#endif /* DUMP_FDS */
ae986130 1984
fe14fcc3 1985#ifndef HAS_DUP2
fec02dd3 1986int
a687059c
LW
1987dup2(oldfd,newfd)
1988int oldfd;
1989int newfd;
1990{
a0d0e21e 1991#if defined(HAS_FCNTL) && defined(F_DUPFD)
fec02dd3
AD
1992 if (oldfd == newfd)
1993 return oldfd;
6ad3d225 1994 PerlLIO_close(newfd);
fec02dd3 1995 return fcntl(oldfd, F_DUPFD, newfd);
62b28dd9 1996#else
fc36a67e 1997#define DUP2_MAX_FDS 256
1998 int fdtmp[DUP2_MAX_FDS];
79072805 1999 I32 fdx = 0;
ae986130
LW
2000 int fd;
2001
fe14fcc3 2002 if (oldfd == newfd)
fec02dd3 2003 return oldfd;
6ad3d225 2004 PerlLIO_close(newfd);
fc36a67e 2005 /* good enough for low fd's... */
6ad3d225 2006 while ((fd = PerlLIO_dup(oldfd)) != newfd && fd >= 0) {
fc36a67e 2007 if (fdx >= DUP2_MAX_FDS) {
6ad3d225 2008 PerlLIO_close(fd);
fc36a67e 2009 fd = -1;
2010 break;
2011 }
ae986130 2012 fdtmp[fdx++] = fd;
fc36a67e 2013 }
ae986130 2014 while (fdx > 0)
6ad3d225 2015 PerlLIO_close(fdtmp[--fdx]);
fec02dd3 2016 return fd;
62b28dd9 2017#endif
a687059c
LW
2018}
2019#endif
2020
ff68c719 2021
2022#ifdef HAS_SIGACTION
2023
2024Sighandler_t
8ac85365 2025rsignal(int signo, Sighandler_t handler)
ff68c719 2026{
2027 struct sigaction act, oact;
2028
2029 act.sa_handler = handler;
2030 sigemptyset(&act.sa_mask);
2031 act.sa_flags = 0;
2032#ifdef SA_RESTART
2033 act.sa_flags |= SA_RESTART; /* SVR4, 4.3+BSD */
2034#endif
85264bed
CS
2035#ifdef SA_NOCLDWAIT
2036 if (signo == SIGCHLD && handler == (Sighandler_t)SIG_IGN)
2037 act.sa_flags |= SA_NOCLDWAIT;
2038#endif
ff68c719 2039 if (sigaction(signo, &act, &oact) == -1)
36477c24 2040 return SIG_ERR;
ff68c719 2041 else
36477c24 2042 return oact.sa_handler;
ff68c719 2043}
2044
2045Sighandler_t
8ac85365 2046rsignal_state(int signo)
ff68c719 2047{
2048 struct sigaction oact;
2049
2050 if (sigaction(signo, (struct sigaction *)NULL, &oact) == -1)
2051 return SIG_ERR;
2052 else
2053 return oact.sa_handler;
2054}
2055
2056int
8ac85365 2057rsignal_save(int signo, Sighandler_t handler, Sigsave_t *save)
ff68c719 2058{
2059 struct sigaction act;
2060
2061 act.sa_handler = handler;
2062 sigemptyset(&act.sa_mask);
2063 act.sa_flags = 0;
2064#ifdef SA_RESTART
2065 act.sa_flags |= SA_RESTART; /* SVR4, 4.3+BSD */
2066#endif
85264bed
CS
2067#ifdef SA_NOCLDWAIT
2068 if (signo == SIGCHLD && handler == (Sighandler_t)SIG_IGN)
2069 act.sa_flags |= SA_NOCLDWAIT;
2070#endif
ff68c719 2071 return sigaction(signo, &act, save);
2072}
2073
2074int
8ac85365 2075rsignal_restore(int signo, Sigsave_t *save)
ff68c719 2076{
2077 return sigaction(signo, save, (struct sigaction *)NULL);
2078}
2079
2080#else /* !HAS_SIGACTION */
2081
2082Sighandler_t
4e35701f 2083rsignal(int signo, Sighandler_t handler)
ff68c719 2084{
6ad3d225 2085 return PerlProc_signal(signo, handler);
ff68c719 2086}
2087
2088static int sig_trapped;
2089
2090static
2091Signal_t
4e35701f 2092sig_trap(int signo)
ff68c719 2093{
2094 sig_trapped++;
2095}
2096
2097Sighandler_t
4e35701f 2098rsignal_state(int signo)
ff68c719 2099{
2100 Sighandler_t oldsig;
2101
2102 sig_trapped = 0;
6ad3d225
GS
2103 oldsig = PerlProc_signal(signo, sig_trap);
2104 PerlProc_signal(signo, oldsig);
ff68c719 2105 if (sig_trapped)
6ad3d225 2106 PerlProc_kill(getpid(), signo);
ff68c719 2107 return oldsig;
2108}
2109
2110int
4e35701f 2111rsignal_save(int signo, Sighandler_t handler, Sigsave_t *save)
ff68c719 2112{
6ad3d225 2113 *save = PerlProc_signal(signo, handler);
ff68c719 2114 return (*save == SIG_ERR) ? -1 : 0;
2115}
2116
2117int
4e35701f 2118rsignal_restore(int signo, Sigsave_t *save)
ff68c719 2119{
6ad3d225 2120 return (PerlProc_signal(signo, *save) == SIG_ERR) ? -1 : 0;
ff68c719 2121}
2122
2123#endif /* !HAS_SIGACTION */
2124
5f05dabc 2125 /* VMS' my_pclose() is in VMS.c; same with OS/2 */
092bebab 2126#if (!defined(DOSISH) || defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS) && !defined(__OPEN_VM)
79072805 2127I32
6acef3b7 2128my_pclose(PerlIO *ptr)
a687059c 2129{
ff68c719 2130 Sigsave_t hstat, istat, qstat;
a687059c 2131 int status;
a0d0e21e 2132 SV **svp;
20188a90 2133 int pid;
1d3434b8 2134 int pid2;
03136e13
CS
2135 bool close_failed;
2136 int saved_errno;
2137#ifdef VMS
2138 int saved_vaxc_errno;
2139#endif
22fae026
TM
2140#ifdef WIN32
2141 int saved_win32_errno;
2142#endif
a687059c 2143
3280af22 2144 svp = av_fetch(PL_fdpid,PerlIO_fileno(ptr),TRUE);
748a9306 2145 pid = (int)SvIVX(*svp);
a0d0e21e 2146 SvREFCNT_dec(*svp);
3280af22 2147 *svp = &PL_sv_undef;
ddcf38b7
IZ
2148#ifdef OS2
2149 if (pid == -1) { /* Opened by popen. */
2150 return my_syspclose(ptr);
2151 }
2152#endif
03136e13
CS
2153 if ((close_failed = (PerlIO_close(ptr) == EOF))) {
2154 saved_errno = errno;
2155#ifdef VMS
2156 saved_vaxc_errno = vaxc$errno;
2157#endif
22fae026
TM
2158#ifdef WIN32
2159 saved_win32_errno = GetLastError();
2160#endif
03136e13 2161 }
7c0587c8 2162#ifdef UTS
6ad3d225 2163 if(PerlProc_kill(pid, 0) < 0) { return(pid); } /* HOM 12/23/91 */
7c0587c8 2164#endif
ff68c719 2165 rsignal_save(SIGHUP, SIG_IGN, &hstat);
2166 rsignal_save(SIGINT, SIG_IGN, &istat);
2167 rsignal_save(SIGQUIT, SIG_IGN, &qstat);
748a9306 2168 do {
1d3434b8
GS
2169 pid2 = wait4pid(pid, &status, 0);
2170 } while (pid2 == -1 && errno == EINTR);
ff68c719 2171 rsignal_restore(SIGHUP, &hstat);
2172 rsignal_restore(SIGINT, &istat);
2173 rsignal_restore(SIGQUIT, &qstat);
03136e13
CS
2174 if (close_failed) {
2175 SETERRNO(saved_errno, saved_vaxc_errno);
2176 return -1;
2177 }
1d3434b8 2178 return(pid2 < 0 ? pid2 : status == 0 ? 0 : (errno = 0, status));
20188a90 2179}
4633a7c4
LW
2180#endif /* !DOSISH */
2181
2d7a9237 2182#if !defined(DOSISH) || defined(OS2) || defined(WIN32)
79072805 2183I32
8ac85365 2184wait4pid(int pid, int *statusp, int flags)
20188a90 2185{
79072805
LW
2186 SV *sv;
2187 SV** svp;
fc36a67e 2188 char spid[TYPE_CHARS(int)];
20188a90
LW
2189
2190 if (!pid)
2191 return -1;
20188a90
LW
2192 if (pid > 0) {
2193 sprintf(spid, "%d", pid);
3280af22
NIS
2194 svp = hv_fetch(PL_pidstatus,spid,strlen(spid),FALSE);
2195 if (svp && *svp != &PL_sv_undef) {
463ee0b2 2196 *statusp = SvIVX(*svp);
3280af22 2197 (void)hv_delete(PL_pidstatus,spid,strlen(spid),G_DISCARD);
20188a90
LW
2198 return pid;
2199 }
2200 }
2201 else {
79072805 2202 HE *entry;
20188a90 2203
3280af22
NIS
2204 hv_iterinit(PL_pidstatus);
2205 if (entry = hv_iternext(PL_pidstatus)) {
a0d0e21e 2206 pid = atoi(hv_iterkey(entry,(I32*)statusp));
3280af22 2207 sv = hv_iterval(PL_pidstatus,entry);
463ee0b2 2208 *statusp = SvIVX(sv);
20188a90 2209 sprintf(spid, "%d", pid);
3280af22 2210 (void)hv_delete(PL_pidstatus,spid,strlen(spid),G_DISCARD);
20188a90
LW
2211 return pid;
2212 }
2213 }
79072805 2214#ifdef HAS_WAITPID
367f3c24
IZ
2215# ifdef HAS_WAITPID_RUNTIME
2216 if (!HAS_WAITPID_RUNTIME)
2217 goto hard_way;
2218# endif
f55ee38a 2219 return PerlProc_waitpid(pid,statusp,flags);
367f3c24
IZ
2220#endif
2221#if !defined(HAS_WAITPID) && defined(HAS_WAIT4)
a0d0e21e 2222 return wait4((pid==-1)?0:pid,statusp,flags,Null(struct rusage *));
367f3c24
IZ
2223#endif
2224#if !defined(HAS_WAITPID) && !defined(HAS_WAIT4) || defined(HAS_WAITPID_RUNTIME)
2225 hard_way:
a0d0e21e
LW
2226 {
2227 I32 result;
2228 if (flags)
2229 croak("Can't do waitpid with flags");
2230 else {
76e3520e 2231 while ((result = PerlProc_wait(statusp)) != pid && pid > 0 && result >= 0)
a0d0e21e
LW
2232 pidgone(result,*statusp);
2233 if (result < 0)
2234 *statusp = -1;
2235 }
2236 return result;
a687059c
LW
2237 }
2238#endif
a687059c 2239}
2d7a9237 2240#endif /* !DOSISH || OS2 || WIN32 */
a687059c 2241
7c0587c8 2242void
de3bb511 2243/*SUPPRESS 590*/
8ac85365 2244pidgone(int pid, int status)
a687059c 2245{
79072805 2246 register SV *sv;
fc36a67e 2247 char spid[TYPE_CHARS(int)];
a687059c 2248
20188a90 2249 sprintf(spid, "%d", pid);
3280af22 2250 sv = *hv_fetch(PL_pidstatus,spid,strlen(spid),TRUE);
a0d0e21e 2251 (void)SvUPGRADE(sv,SVt_IV);
463ee0b2 2252 SvIVX(sv) = status;
20188a90 2253 return;
a687059c
LW
2254}
2255
55497cff 2256#if defined(atarist) || defined(OS2) || defined(DJGPP)
7c0587c8 2257int pclose();
ddcf38b7
IZ
2258#ifdef HAS_FORK
2259int /* Cannot prototype with I32
2260 in os2ish.h. */
2261my_syspclose(ptr)
2262#else
79072805
LW
2263I32
2264my_pclose(ptr)
ddcf38b7 2265#endif
760ac839 2266PerlIO *ptr;
a687059c 2267{
760ac839
LW
2268 /* Needs work for PerlIO ! */
2269 FILE *f = PerlIO_findFILE(ptr);
2270 I32 result = pclose(f);
2271 PerlIO_releaseFILE(ptr,f);
2272 return result;
a687059c 2273}
7c0587c8 2274#endif
9f68db38
LW
2275
2276void
8ac85365 2277repeatcpy(register char *to, register char *from, I32 len, register I32 count)
9f68db38 2278{
79072805 2279 register I32 todo;
9f68db38
LW
2280 register char *frombase = from;
2281
2282 if (len == 1) {
2283 todo = *from;
2284 while (count-- > 0)
2285 *to++ = todo;
2286 return;
2287 }
2288 while (count-- > 0) {
2289 for (todo = len; todo > 0; todo--) {
2290 *to++ = *from++;
2291 }
2292 from = frombase;
2293 }
2294}
0f85fab0 2295
463ee0b2 2296U32
22c35a8c 2297cast_ulong(double f)
0f85fab0
LW
2298{
2299 long along;
2300
27e2fb84 2301#if CASTFLAGS & 2
34de22dd
LW
2302# define BIGDOUBLE 2147483648.0
2303 if (f >= BIGDOUBLE)
2304 return (unsigned long)(f-(long)(f/BIGDOUBLE)*BIGDOUBLE)|0x80000000;
2305#endif
0f85fab0
LW
2306 if (f >= 0.0)
2307 return (unsigned long)f;
2308 along = (long)f;
2309 return (unsigned long)along;
2310}
ed6116ce 2311# undef BIGDOUBLE
5d94fbed 2312
5d94fbed
AD
2313/* Unfortunately, on some systems the cast_uv() function doesn't
2314 work with the system-supplied definition of ULONG_MAX. The
2315 comparison (f >= ULONG_MAX) always comes out true. It must be a
2316 problem with the compiler constant folding.
2317
2318 In any case, this workaround should be fine on any two's complement
2319 system. If it's not, supply a '-DMY_ULONG_MAX=whatever' in your
2320 ccflags.
2321 --Andy Dougherty <doughera@lafcol.lafayette.edu>
2322*/
1eb770ff 2323
2324/* Code modified to prefer proper named type ranges, I32, IV, or UV, instead
2325 of LONG_(MIN/MAX).
2326 -- Kenneth Albanowski <kjahds@kjahds.com>
2327*/
2328
20cec16a 2329#ifndef MY_UV_MAX
2330# define MY_UV_MAX ((UV)IV_MAX * (UV)2 + (UV)1)
5d94fbed
AD
2331#endif
2332
ed6116ce 2333I32
22c35a8c 2334cast_i32(double f)
ed6116ce 2335{
20cec16a 2336 if (f >= I32_MAX)
2337 return (I32) I32_MAX;
2338 if (f <= I32_MIN)
2339 return (I32) I32_MIN;
ed6116ce
LW
2340 return (I32) f;
2341}
a0d0e21e
LW
2342
2343IV
22c35a8c 2344cast_iv(double f)
a0d0e21e 2345{
20cec16a 2346 if (f >= IV_MAX)
2347 return (IV) IV_MAX;
2348 if (f <= IV_MIN)
2349 return (IV) IV_MIN;
a0d0e21e
LW
2350 return (IV) f;
2351}
5d94fbed
AD
2352
2353UV
22c35a8c 2354cast_uv(double f)
5d94fbed 2355{
20cec16a 2356 if (f >= MY_UV_MAX)
2357 return (UV) MY_UV_MAX;
5d94fbed
AD
2358 return (UV) f;
2359}
2360
fe14fcc3 2361#ifndef HAS_RENAME
79072805 2362I32
22c35a8c 2363same_dirent(char *a, char *b)
62b28dd9 2364{
93a17b20
LW
2365 char *fa = strrchr(a,'/');
2366 char *fb = strrchr(b,'/');
62b28dd9
LW
2367 struct stat tmpstatbuf1;
2368 struct stat tmpstatbuf2;
46fc3d4c 2369 SV *tmpsv = sv_newmortal();
62b28dd9
LW
2370
2371 if (fa)
2372 fa++;
2373 else
2374 fa = a;
2375 if (fb)
2376 fb++;
2377 else
2378 fb = b;
2379 if (strNE(a,b))
2380 return FALSE;
2381 if (fa == a)
46fc3d4c 2382 sv_setpv(tmpsv, ".");
62b28dd9 2383 else
46fc3d4c 2384 sv_setpvn(tmpsv, a, fa - a);
c6ed36e1 2385 if (PerlLIO_stat(SvPVX(tmpsv), &tmpstatbuf1) < 0)
62b28dd9
LW
2386 return FALSE;
2387 if (fb == b)
46fc3d4c 2388 sv_setpv(tmpsv, ".");
62b28dd9 2389 else
46fc3d4c 2390 sv_setpvn(tmpsv, b, fb - b);
c6ed36e1 2391 if (PerlLIO_stat(SvPVX(tmpsv), &tmpstatbuf2) < 0)
62b28dd9
LW
2392 return FALSE;
2393 return tmpstatbuf1.st_dev == tmpstatbuf2.st_dev &&
2394 tmpstatbuf1.st_ino == tmpstatbuf2.st_ino;
2395}
fe14fcc3
LW
2396#endif /* !HAS_RENAME */
2397
55497cff 2398UV
8ac85365 2399scan_oct(char *start, I32 len, I32 *retlen)
fe14fcc3
LW
2400{
2401 register char *s = start;
55497cff 2402 register UV retval = 0;
2403 bool overflowed = FALSE;
fe14fcc3 2404
748a9306 2405 while (len && *s >= '0' && *s <= '7') {
55497cff 2406 register UV n = retval << 3;
2407 if (!overflowed && (n >> 3) != retval) {
2408 warn("Integer overflow in octal number");
2409 overflowed = TRUE;
2410 }
2411 retval = n | (*s++ - '0');
748a9306 2412 len--;
fe14fcc3 2413 }
d008e5eb
GS
2414 if (len && (*s == '8' || *s == '9')) {
2415 dTHR;
2416 if (ckWARN(WARN_OCTAL))
2417 warner(WARN_OCTAL, "Illegal octal digit ignored");
2418 }
fe14fcc3
LW
2419 *retlen = s - start;
2420 return retval;
2421}
2422
71be2cbc 2423UV
8ac85365 2424scan_hex(char *start, I32 len, I32 *retlen)
fe14fcc3
LW
2425{
2426 register char *s = start;
55497cff 2427 register UV retval = 0;
2428 bool overflowed = FALSE;
6ff81951 2429 char *tmp = s;
a0ed51b3 2430 register UV n;
fe14fcc3 2431
a0ed51b3
LW
2432 while (len-- && *s) {
2433 tmp = strchr((char *) PL_hexdigit, *s++);
2434 if (!tmp) {
e3fdf988 2435 if (*(s-1) == '_' || (*(s-1) == 'x' && retval == 0))
a0ed51b3
LW
2436 continue;
2437 else {
d008e5eb 2438 dTHR;
a0ed51b3 2439 --s;
599cee73
PM
2440 if (ckWARN(WARN_UNSAFE))
2441 warner(WARN_UNSAFE,"Illegal hex digit ignored");
a0ed51b3
LW
2442 break;
2443 }
2444 }
2445 n = retval << 4;
55497cff 2446 if (!overflowed && (n >> 4) != retval) {
2447 warn("Integer overflow in hex number");
2448 overflowed = TRUE;
2449 }
3280af22 2450 retval = n | ((tmp - PL_hexdigit) & 15);
6ff81951 2451 }
fe14fcc3
LW
2452 *retlen = s - start;
2453 return retval;
2454}
760ac839 2455
491527d0
GS
2456char*
2457find_script(char *scriptname, bool dosearch, char **search_ext, I32 flags)
2458{
2459 dTHR;
2460 char *xfound = Nullch;
2461 char *xfailed = Nullch;
84486fc6 2462 char tmpbuf[512];
491527d0
GS
2463 register char *s;
2464 I32 len;
2465 int retval;
2466#if defined(DOSISH) && !defined(OS2) && !defined(atarist)
2467# define SEARCH_EXTS ".bat", ".cmd", NULL
2468# define MAX_EXT_LEN 4
2469#endif
2470#ifdef OS2
2471# define SEARCH_EXTS ".cmd", ".btm", ".bat", ".pl", NULL
2472# define MAX_EXT_LEN 4
2473#endif
2474#ifdef VMS
2475# define SEARCH_EXTS ".pl", ".com", NULL
2476# define MAX_EXT_LEN 4
2477#endif
2478 /* additional extensions to try in each dir if scriptname not found */
2479#ifdef SEARCH_EXTS
2480 char *exts[] = { SEARCH_EXTS };
2481 char **ext = search_ext ? search_ext : exts;
2482 int extidx = 0, i = 0;
2483 char *curext = Nullch;
2484#else
2485# define MAX_EXT_LEN 0
2486#endif
2487
2488 /*
2489 * If dosearch is true and if scriptname does not contain path
2490 * delimiters, search the PATH for scriptname.
2491 *
2492 * If SEARCH_EXTS is also defined, will look for each
2493 * scriptname{SEARCH_EXTS} whenever scriptname is not found
2494 * while searching the PATH.
2495 *
2496 * Assuming SEARCH_EXTS is C<".foo",".bar",NULL>, PATH search
2497 * proceeds as follows:
2498 * If DOSISH or VMSISH:
2499 * + look for ./scriptname{,.foo,.bar}
2500 * + search the PATH for scriptname{,.foo,.bar}
2501 *
2502 * If !DOSISH:
2503 * + look *only* in the PATH for scriptname{,.foo,.bar} (note
2504 * this will not look in '.' if it's not in the PATH)
2505 */
84486fc6 2506 tmpbuf[0] = '\0';
491527d0
GS
2507
2508#ifdef VMS
2509# ifdef ALWAYS_DEFTYPES
2510 len = strlen(scriptname);
2511 if (!(len == 1 && *scriptname == '-') && scriptname[len-1] != ':') {
2512 int hasdir, idx = 0, deftypes = 1;
2513 bool seen_dot = 1;
2514
2515 hasdir = !dosearch || (strpbrk(scriptname,":[</") != Nullch) ;
2516# else
2517 if (dosearch) {
2518 int hasdir, idx = 0, deftypes = 1;
2519 bool seen_dot = 1;
2520
2521 hasdir = (strpbrk(scriptname,":[</") != Nullch) ;
2522# endif
2523 /* The first time through, just add SEARCH_EXTS to whatever we
2524 * already have, so we can check for default file types. */
2525 while (deftypes ||
84486fc6 2526 (!hasdir && my_trnlnm("DCL$PATH",tmpbuf,idx++)) )
491527d0
GS
2527 {
2528 if (deftypes) {
2529 deftypes = 0;
84486fc6 2530 *tmpbuf = '\0';
491527d0 2531 }
84486fc6
GS
2532 if ((strlen(tmpbuf) + strlen(scriptname)
2533 + MAX_EXT_LEN) >= sizeof tmpbuf)
491527d0 2534 continue; /* don't search dir with too-long name */
84486fc6 2535 strcat(tmpbuf, scriptname);
491527d0
GS
2536#else /* !VMS */
2537
2538#ifdef DOSISH
2539 if (strEQ(scriptname, "-"))
2540 dosearch = 0;
2541 if (dosearch) { /* Look in '.' first. */
2542 char *cur = scriptname;
2543#ifdef SEARCH_EXTS
2544 if ((curext = strrchr(scriptname,'.'))) /* possible current ext */
2545 while (ext[i])
2546 if (strEQ(ext[i++],curext)) {
2547 extidx = -1; /* already has an ext */
2548 break;
2549 }
2550 do {
2551#endif
2552 DEBUG_p(PerlIO_printf(Perl_debug_log,
2553 "Looking for %s\n",cur));
017f25f1
IZ
2554 if (PerlLIO_stat(cur,&PL_statbuf) >= 0
2555 && !S_ISDIR(PL_statbuf.st_mode)) {
491527d0
GS
2556 dosearch = 0;
2557 scriptname = cur;
2558#ifdef SEARCH_EXTS
2559 break;
2560#endif
2561 }
2562#ifdef SEARCH_EXTS
2563 if (cur == scriptname) {
2564 len = strlen(scriptname);
84486fc6 2565 if (len+MAX_EXT_LEN+1 >= sizeof(tmpbuf))
491527d0 2566 break;
84486fc6 2567 cur = strcpy(tmpbuf, scriptname);
491527d0
GS
2568 }
2569 } while (extidx >= 0 && ext[extidx] /* try an extension? */
84486fc6 2570 && strcpy(tmpbuf+len, ext[extidx++]));
491527d0
GS
2571#endif
2572 }
2573#endif
2574
2575 if (dosearch && !strchr(scriptname, '/')
2576#ifdef DOSISH
2577 && !strchr(scriptname, '\\')
2578#endif
2579 && (s = PerlEnv_getenv("PATH"))) {
2580 bool seen_dot = 0;
2581
3280af22
NIS
2582 PL_bufend = s + strlen(s);
2583 while (s < PL_bufend) {
491527d0
GS
2584#if defined(atarist) || defined(DOSISH)
2585 for (len = 0; *s
2586# ifdef atarist
2587 && *s != ','
2588# endif
2589 && *s != ';'; len++, s++) {
84486fc6
GS
2590 if (len < sizeof tmpbuf)
2591 tmpbuf[len] = *s;
491527d0 2592 }
84486fc6
GS
2593 if (len < sizeof tmpbuf)
2594 tmpbuf[len] = '\0';
491527d0 2595#else /* ! (atarist || DOSISH) */
3280af22 2596 s = delimcpy(tmpbuf, tmpbuf + sizeof tmpbuf, s, PL_bufend,
491527d0
GS
2597 ':',
2598 &len);
2599#endif /* ! (atarist || DOSISH) */
3280af22 2600 if (s < PL_bufend)
491527d0 2601 s++;
84486fc6 2602 if (len + 1 + strlen(scriptname) + MAX_EXT_LEN >= sizeof tmpbuf)
491527d0
GS
2603 continue; /* don't search dir with too-long name */
2604 if (len
2605#if defined(atarist) || defined(DOSISH)
84486fc6
GS
2606 && tmpbuf[len - 1] != '/'
2607 && tmpbuf[len - 1] != '\\'
491527d0
GS
2608#endif
2609 )
84486fc6
GS
2610 tmpbuf[len++] = '/';
2611 if (len == 2 && tmpbuf[0] == '.')
491527d0 2612 seen_dot = 1;
84486fc6 2613 (void)strcpy(tmpbuf + len, scriptname);
491527d0
GS
2614#endif /* !VMS */
2615
2616#ifdef SEARCH_EXTS
84486fc6 2617 len = strlen(tmpbuf);
491527d0
GS
2618 if (extidx > 0) /* reset after previous loop */
2619 extidx = 0;
2620 do {
2621#endif
84486fc6 2622 DEBUG_p(PerlIO_printf(Perl_debug_log, "Looking for %s\n",tmpbuf));
3280af22 2623 retval = PerlLIO_stat(tmpbuf,&PL_statbuf);
017f25f1
IZ
2624 if (S_ISDIR(PL_statbuf.st_mode)) {
2625 retval = -1;
2626 }
491527d0
GS
2627#ifdef SEARCH_EXTS
2628 } while ( retval < 0 /* not there */
2629 && extidx>=0 && ext[extidx] /* try an extension? */
84486fc6 2630 && strcpy(tmpbuf+len, ext[extidx++])
491527d0
GS
2631 );
2632#endif
2633 if (retval < 0)
2634 continue;
3280af22
NIS
2635 if (S_ISREG(PL_statbuf.st_mode)
2636 && cando(S_IRUSR,TRUE,&PL_statbuf)
491527d0 2637#ifndef DOSISH
3280af22 2638 && cando(S_IXUSR,TRUE,&PL_statbuf)
491527d0
GS
2639#endif
2640 )
2641 {
84486fc6 2642 xfound = tmpbuf; /* bingo! */
491527d0
GS
2643 break;
2644 }
2645 if (!xfailed)
84486fc6 2646 xfailed = savepv(tmpbuf);
491527d0
GS
2647 }
2648#ifndef DOSISH
017f25f1
IZ
2649 if (!xfound && !seen_dot && !xfailed &&
2650 (PerlLIO_stat(scriptname,&PL_statbuf) < 0
2651 || S_ISDIR(PL_statbuf.st_mode)))
491527d0
GS
2652#endif
2653 seen_dot = 1; /* Disable message. */
9ccb31f9
GS
2654 if (!xfound) {
2655 if (flags & 1) { /* do or die? */
2656 croak("Can't %s %s%s%s",
2657 (xfailed ? "execute" : "find"),
2658 (xfailed ? xfailed : scriptname),
2659 (xfailed ? "" : " on PATH"),
2660 (xfailed || seen_dot) ? "" : ", '.' not in PATH");
2661 }
2662 scriptname = Nullch;
2663 }
491527d0
GS
2664 if (xfailed)
2665 Safefree(xfailed);
2666 scriptname = xfound;
2667 }
9ccb31f9 2668 return (scriptname ? savepv(scriptname) : Nullch);
491527d0
GS
2669}
2670
2671
11343788 2672#ifdef USE_THREADS
12ca11f6
MB
2673#ifdef FAKE_THREADS
2674/* Very simplistic scheduler for now */
2675void
2676schedule(void)
2677{
c7848ba1 2678 thr = thr->i.next_run;
12ca11f6
MB
2679}
2680
2681void
22c35a8c 2682perl_cond_init(perl_cond *cp)
12ca11f6
MB
2683{
2684 *cp = 0;
2685}
2686
2687void
22c35a8c 2688perl_cond_signal(perl_cond *cp)
12ca11f6 2689{
51dd5992 2690 perl_os_thread t;
12ca11f6
MB
2691 perl_cond cond = *cp;
2692
2693 if (!cond)
2694 return;
2695 t = cond->thread;
2696 /* Insert t in the runnable queue just ahead of us */
c7848ba1
MB
2697 t->i.next_run = thr->i.next_run;
2698 thr->i.next_run->i.prev_run = t;
2699 t->i.prev_run = thr;
2700 thr->i.next_run = t;
2701 thr->i.wait_queue = 0;
12ca11f6
MB
2702 /* Remove from the wait queue */
2703 *cp = cond->next;
2704 Safefree(cond);
2705}
2706
2707void
22c35a8c 2708perl_cond_broadcast(perl_cond *cp)
12ca11f6 2709{
51dd5992 2710 perl_os_thread t;
12ca11f6
MB
2711 perl_cond cond, cond_next;
2712
2713 for (cond = *cp; cond; cond = cond_next) {
2714 t = cond->thread;
2715 /* Insert t in the runnable queue just ahead of us */
c7848ba1
MB
2716 t->i.next_run = thr->i.next_run;
2717 thr->i.next_run->i.prev_run = t;
2718 t->i.prev_run = thr;
2719 thr->i.next_run = t;
2720 thr->i.wait_queue = 0;
12ca11f6
MB
2721 /* Remove from the wait queue */
2722 cond_next = cond->next;
2723 Safefree(cond);
2724 }
2725 *cp = 0;
2726}
2727
2728void
22c35a8c 2729perl_cond_wait(perl_cond *cp)
12ca11f6
MB
2730{
2731 perl_cond cond;
2732
c7848ba1 2733 if (thr->i.next_run == thr)
12ca11f6
MB
2734 croak("panic: perl_cond_wait called by last runnable thread");
2735
0f15f207 2736 New(666, cond, 1, struct perl_wait_queue);
12ca11f6
MB
2737 cond->thread = thr;
2738 cond->next = *cp;
2739 *cp = cond;
c7848ba1 2740 thr->i.wait_queue = cond;
12ca11f6 2741 /* Remove ourselves from runnable queue */
c7848ba1
MB
2742 thr->i.next_run->i.prev_run = thr->i.prev_run;
2743 thr->i.prev_run->i.next_run = thr->i.next_run;
12ca11f6
MB
2744}
2745#endif /* FAKE_THREADS */
2746
0d85d877 2747#ifdef PTHREAD_GETSPECIFIC_INT
52e1cb5e 2748struct perl_thread *
11343788
MB
2749getTHR _((void))
2750{
2751 pthread_addr_t t;
2752
6b88bc9c 2753 if (pthread_getspecific(PL_thr_key, &t))
11343788 2754 croak("panic: pthread_getspecific");
52e1cb5e 2755 return (struct perl_thread *) t;
11343788 2756}
0d85d877 2757#endif
f93b4edd
MB
2758
2759MAGIC *
8ac85365 2760condpair_magic(SV *sv)
f93b4edd
MB
2761{
2762 MAGIC *mg;
2763
2764 SvUPGRADE(sv, SVt_PVMG);
2765 mg = mg_find(sv, 'm');
2766 if (!mg) {
2767 condpair_t *cp;
2768
2769 New(53, cp, 1, condpair_t);
2770 MUTEX_INIT(&cp->mutex);
2771 COND_INIT(&cp->owner_cond);
2772 COND_INIT(&cp->cond);
2773 cp->owner = 0;
940cb80d 2774 LOCK_SV_MUTEX;
f93b4edd
MB
2775 mg = mg_find(sv, 'm');
2776 if (mg) {
2777 /* someone else beat us to initialising it */
940cb80d 2778 UNLOCK_SV_MUTEX;
f93b4edd
MB
2779 MUTEX_DESTROY(&cp->mutex);
2780 COND_DESTROY(&cp->owner_cond);
2781 COND_DESTROY(&cp->cond);
2782 Safefree(cp);
2783 }
2784 else {
2785 sv_magic(sv, Nullsv, 'm', 0, 0);
2786 mg = SvMAGIC(sv);
2787 mg->mg_ptr = (char *)cp;
565764a8 2788 mg->mg_len = sizeof(cp);
940cb80d 2789 UNLOCK_SV_MUTEX;
8b73bbec 2790 DEBUG_S(WITH_THR(PerlIO_printf(PerlIO_stderr(),
c7848ba1 2791 "%p: condpair_magic %p\n", thr, sv));)
f93b4edd
MB
2792 }
2793 }
2794 return mg;
2795}
a863c7d1
MB
2796
2797/*
199100c8
MB
2798 * Make a new perl thread structure using t as a prototype. Some of the
2799 * fields for the new thread are copied from the prototype thread, t,
2800 * so t should not be running in perl at the time this function is
2801 * called. The use by ext/Thread/Thread.xs in core perl (where t is the
2802 * thread calling new_struct_thread) clearly satisfies this constraint.
a863c7d1 2803 */
52e1cb5e
JH
2804struct perl_thread *
2805new_struct_thread(struct perl_thread *t)
a863c7d1 2806{
52e1cb5e 2807 struct perl_thread *thr;
a863c7d1 2808 SV *sv;
199100c8
MB
2809 SV **svp;
2810 I32 i;
2811
2812 sv = newSVpv("", 0);
52e1cb5e
JH
2813 SvGROW(sv, sizeof(struct perl_thread) + 1);
2814 SvCUR_set(sv, sizeof(struct perl_thread));
199100c8 2815 thr = (Thread) SvPVX(sv);
949ced2d 2816#ifdef DEBUGGING
52e1cb5e 2817 memset(thr, 0xab, sizeof(struct perl_thread));
533c011a
NIS
2818 PL_markstack = 0;
2819 PL_scopestack = 0;
2820 PL_savestack = 0;
2821 PL_retstack = 0;
2822 PL_dirty = 0;
2823 PL_localizing = 0;
949ced2d
GS
2824 Zero(&PL_hv_fetch_ent_mh, 1, HE);
2825#else
2826 Zero(thr, 1, struct perl_thread);
2827#endif
199100c8
MB
2828
2829 thr->oursv = sv;
d55594ae 2830 init_stacks(ARGS);
a863c7d1 2831
533c011a 2832 PL_curcop = &PL_compiling;
199100c8 2833 thr->cvcache = newHV();
54b9620d 2834 thr->threadsv = newAV();
a863c7d1 2835 thr->specific = newAV();
38a03e6e
MB
2836 thr->errsv = newSVpv("", 0);
2837 thr->errhv = newHV();
a863c7d1
MB
2838 thr->flags = THRf_R_JOINABLE;
2839 MUTEX_INIT(&thr->mutex);
199100c8 2840
d55594ae
GS
2841 /* top_env needs to be non-zero. It points to an area
2842 in which longjmp() stuff is stored, as C callstack
2843 info there at least is thread specific this has to
2844 be per-thread. Otherwise a 'die' in a thread gives
2845 that thread the C stack of last thread to do an eval {}!
2846 See comments in scope.h
2847 Initialize top entry (as in perl.c for main thread)
2848 */
533c011a
NIS
2849 PL_start_env.je_prev = NULL;
2850 PL_start_env.je_ret = -1;
2851 PL_start_env.je_mustcatch = TRUE;
2852 PL_top_env = &PL_start_env;
2853
2854 PL_in_eval = FALSE;
2855 PL_restartop = 0;
2856
b099ddc0
GS
2857 PL_statname = NEWSV(66,0);
2858 PL_maxscream = -1;
2859 PL_regcompp = FUNC_NAME_TO_PTR(pregcomp);
2860 PL_regexecp = FUNC_NAME_TO_PTR(regexec_flags);
2861 PL_regindent = 0;
2862 PL_reginterp_cnt = 0;
2863 PL_lastscream = Nullsv;
2864 PL_screamfirst = 0;
2865 PL_screamnext = 0;
2866 PL_reg_start_tmp = 0;
2867 PL_reg_start_tmpl = 0;
2868
2869 /* parent thread's data needs to be locked while we make copy */
2870 MUTEX_LOCK(&t->mutex);
2871
2872 PL_curcop = t->Tcurcop; /* XXX As good a guess as any? */
2873 PL_defstash = t->Tdefstash; /* XXX maybe these should */
2874 PL_curstash = t->Tcurstash; /* always be set to main? */
2875
6b88bc9c 2876 PL_tainted = t->Ttainted;
84fee439
NIS
2877 PL_curpm = t->Tcurpm; /* XXX No PMOP ref count */
2878 PL_nrs = newSVsv(t->Tnrs);
2879 PL_rs = SvREFCNT_inc(PL_nrs);
2880 PL_last_in_gv = Nullgv;
2881 PL_ofslen = t->Tofslen;
2882 PL_ofs = savepvn(t->Tofs, PL_ofslen);
2883 PL_defoutgv = (GV*)SvREFCNT_inc(t->Tdefoutgv);
2884 PL_chopset = t->Tchopset;
2885 PL_formtarget = newSVsv(t->Tformtarget);
2886 PL_bodytarget = newSVsv(t->Tbodytarget);
2887 PL_toptarget = newSVsv(t->Ttoptarget);
533c011a 2888
54b9620d
MB
2889 /* Initialise all per-thread SVs that the template thread used */
2890 svp = AvARRAY(t->threadsv);
93965878 2891 for (i = 0; i <= AvFILLp(t->threadsv); i++, svp++) {
533c011a 2892 if (*svp && *svp != &PL_sv_undef) {
199100c8 2893 SV *sv = newSVsv(*svp);
54b9620d 2894 av_store(thr->threadsv, i, sv);
533c011a 2895 sv_magic(sv, 0, 0, &PL_threadsv_names[i], 1);
8b73bbec 2896 DEBUG_S(PerlIO_printf(PerlIO_stderr(),
54b9620d 2897 "new_struct_thread: copied threadsv %d %p->%p\n",i, t, thr));
199100c8
MB
2898 }
2899 }
940cb80d 2900 thr->threadsvp = AvARRAY(thr->threadsv);
199100c8 2901
533c011a
NIS
2902 MUTEX_LOCK(&PL_threads_mutex);
2903 PL_nthreads++;
2904 thr->tid = ++PL_threadnum;
199100c8
MB
2905 thr->next = t->next;
2906 thr->prev = t;
2907 t->next = thr;
2908 thr->next->prev = thr;
533c011a 2909 MUTEX_UNLOCK(&PL_threads_mutex);
a863c7d1 2910
b099ddc0
GS
2911 /* done copying parent's state */
2912 MUTEX_UNLOCK(&t->mutex);
2913
a863c7d1
MB
2914#ifdef HAVE_THREAD_INTERN
2915 init_thread_intern(thr);
a863c7d1 2916#endif /* HAVE_THREAD_INTERN */
a863c7d1
MB
2917 return thr;
2918}
11343788 2919#endif /* USE_THREADS */
760ac839
LW
2920
2921#ifdef HUGE_VAL
2922/*
2923 * This hack is to force load of "huge" support from libm.a
2924 * So it is in perl for (say) POSIX to use.
2925 * Needed for SunOS with Sun's 'acc' for example.
2926 */
2927double
8ac85365 2928Perl_huge(void)
760ac839
LW
2929{
2930 return HUGE_VAL;
2931}
2932#endif
4e35701f 2933
22239a37
NIS
2934#ifdef PERL_GLOBAL_STRUCT
2935struct perl_vars *
2936Perl_GetVars(void)
2937{
533c011a 2938 return &PL_Vars;
22239a37 2939}
31fb1209
NIS
2940#endif
2941
2942char **
2943get_op_names(void)
2944{
22c35a8c 2945 return PL_op_name;
31fb1209
NIS
2946}
2947
2948char **
2949get_op_descs(void)
2950{
22c35a8c 2951 return PL_op_desc;
31fb1209 2952}
9e6b2b00
GS
2953
2954char *
2955get_no_modify(void)
2956{
22c35a8c 2957 return (char*)PL_no_modify;
9e6b2b00
GS
2958}
2959
2960U32 *
2961get_opargs(void)
2962{
22c35a8c 2963 return PL_opargs;
9e6b2b00 2964}
51aa15f3 2965
51aa15f3
GS
2966SV **
2967get_specialsv_list(void)
2968{
3280af22 2969 return PL_specialsv_list;
f55ee38a 2970}
dc9e4912
GS
2971
2972
2973MGVTBL*
2974get_vtbl(int vtbl_id)
2975{
2976 MGVTBL* result = Null(MGVTBL*);
2977
2978 switch(vtbl_id) {
2979 case want_vtbl_sv:
2980 result = &PL_vtbl_sv;
2981 break;
2982 case want_vtbl_env:
2983 result = &PL_vtbl_env;
2984 break;
2985 case want_vtbl_envelem:
2986 result = &PL_vtbl_envelem;
2987 break;
2988 case want_vtbl_sig:
2989 result = &PL_vtbl_sig;
2990 break;
2991 case want_vtbl_sigelem:
2992 result = &PL_vtbl_sigelem;
2993 break;
2994 case want_vtbl_pack:
2995 result = &PL_vtbl_pack;
2996 break;
2997 case want_vtbl_packelem:
2998 result = &PL_vtbl_packelem;
2999 break;
3000 case want_vtbl_dbline:
3001 result = &PL_vtbl_dbline;
3002 break;
3003 case want_vtbl_isa:
3004 result = &PL_vtbl_isa;
3005 break;
3006 case want_vtbl_isaelem:
3007 result = &PL_vtbl_isaelem;
3008 break;
3009 case want_vtbl_arylen:
3010 result = &PL_vtbl_arylen;
3011 break;
3012 case want_vtbl_glob:
3013 result = &PL_vtbl_glob;
3014 break;
3015 case want_vtbl_mglob:
3016 result = &PL_vtbl_mglob;
3017 break;
3018 case want_vtbl_nkeys:
3019 result = &PL_vtbl_nkeys;
3020 break;
3021 case want_vtbl_taint:
3022 result = &PL_vtbl_taint;
3023 break;
3024 case want_vtbl_substr:
3025 result = &PL_vtbl_substr;
3026 break;
3027 case want_vtbl_vec:
3028 result = &PL_vtbl_vec;
3029 break;
3030 case want_vtbl_pos:
3031 result = &PL_vtbl_pos;
3032 break;
3033 case want_vtbl_bm:
3034 result = &PL_vtbl_bm;
3035 break;
3036 case want_vtbl_fm:
3037 result = &PL_vtbl_fm;
3038 break;
3039 case want_vtbl_uvar:
3040 result = &PL_vtbl_uvar;
3041 break;
3042#ifdef USE_THREADS
3043 case want_vtbl_mutex:
3044 result = &PL_vtbl_mutex;
3045 break;
3046#endif
3047 case want_vtbl_defelem:
3048 result = &PL_vtbl_defelem;
3049 break;
3050 case want_vtbl_regexp:
3051 result = &PL_vtbl_regexp;
3052 break;
3053 case want_vtbl_regdata:
3054 result = &PL_vtbl_regdata;
3055 break;
3056 case want_vtbl_regdatum:
3057 result = &PL_vtbl_regdatum;
3058 break;
3059 case want_vtbl_collxfrm:
3060 result = &PL_vtbl_collxfrm;
3061 break;
3062 case want_vtbl_amagic:
3063 result = &PL_vtbl_amagic;
3064 break;
3065 case want_vtbl_amagicelem:
3066 result = &PL_vtbl_amagicelem;
3067 break;
3068 }
3069 return result;
3070}
3071