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