Commit | Line | Data |
---|---|---|
a0d0e21e | 1 | /* util.c |
a687059c | 2 | * |
4eb8286e | 3 | * Copyright (c) 1991-1999, 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" |
864dbfa3 | 16 | #define PERL_IN_UTIL_C |
8d063cd8 | 17 | #include "perl.h" |
62b28dd9 | 18 | |
e1dfb34b | 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 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 | ||
097ee67d JH |
54 | #ifdef I_LOCALE |
55 | # include <locale.h> | |
56 | #endif | |
57 | ||
8d063cd8 | 58 | #define FLUSH |
8d063cd8 | 59 | |
a0d0e21e | 60 | #ifdef LEAKTEST |
a0d0e21e | 61 | |
8c52afec IZ |
62 | long xcount[MAXXCOUNT]; |
63 | long lastxcount[MAXXCOUNT]; | |
64 | long xycount[MAXXCOUNT][MAXYCOUNT]; | |
65 | long lastxycount[MAXXCOUNT][MAXYCOUNT]; | |
66 | ||
a0d0e21e | 67 | #endif |
a863c7d1 | 68 | |
16cebae2 GS |
69 | #if defined(HAS_FCNTL) && defined(F_SETFD) && !defined(FD_CLOEXEC) |
70 | # define FD_CLOEXEC 1 /* NeXT needs this */ | |
71 | #endif | |
72 | ||
f2517201 | 73 | /* paranoid version of system's malloc() */ |
8d063cd8 | 74 | |
a687059c LW |
75 | /* NOTE: Do not call the next three routines directly. Use the macros |
76 | * in handy.h, so that we can easily redefine everything to do tracking of | |
77 | * allocated hunks back to the original New to track down any memory leaks. | |
20cec16a | 78 | * XXX This advice seems to be widely ignored :-( --AD August 1996. |
a687059c LW |
79 | */ |
80 | ||
bd4080b3 | 81 | Malloc_t |
4f63d024 | 82 | Perl_safesysmalloc(MEM_SIZE size) |
8d063cd8 | 83 | { |
54aff467 | 84 | dTHX; |
bd4080b3 | 85 | Malloc_t ptr; |
55497cff | 86 | #ifdef HAS_64K_LIMIT |
62b28dd9 | 87 | if (size > 0xffff) { |
bf49b057 | 88 | PerlIO_printf(Perl_error_log, |
16cebae2 | 89 | "Allocation too large: %lx\n", size) FLUSH; |
54aff467 | 90 | my_exit(1); |
62b28dd9 | 91 | } |
55497cff | 92 | #endif /* HAS_64K_LIMIT */ |
34de22dd LW |
93 | #ifdef DEBUGGING |
94 | if ((long)size < 0) | |
4f63d024 | 95 | Perl_croak_nocontext("panic: malloc"); |
34de22dd | 96 | #endif |
6ad3d225 | 97 | ptr = PerlMem_malloc(size?size:1); /* malloc(0) is NASTY on our system */ |
7b0972df | 98 | DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%"UVxf": (%05d) malloc %ld bytes\n",PTR2UV(ptr),PL_an++,(long)size)); |
8d063cd8 LW |
99 | if (ptr != Nullch) |
100 | return ptr; | |
3280af22 | 101 | else if (PL_nomemok) |
7c0587c8 | 102 | return Nullch; |
8d063cd8 | 103 | else { |
bf49b057 | 104 | PerlIO_puts(Perl_error_log,PL_no_mem) FLUSH; |
54aff467 | 105 | my_exit(1); |
4e35701f | 106 | return Nullch; |
8d063cd8 LW |
107 | } |
108 | /*NOTREACHED*/ | |
109 | } | |
110 | ||
f2517201 | 111 | /* paranoid version of system's realloc() */ |
8d063cd8 | 112 | |
bd4080b3 | 113 | Malloc_t |
4f63d024 | 114 | Perl_safesysrealloc(Malloc_t where,MEM_SIZE size) |
8d063cd8 | 115 | { |
54aff467 | 116 | dTHX; |
bd4080b3 | 117 | Malloc_t ptr; |
ecfc5424 | 118 | #if !defined(STANDARD_C) && !defined(HAS_REALLOC_PROTOTYPE) |
6ad3d225 | 119 | Malloc_t PerlMem_realloc(); |
ecfc5424 | 120 | #endif /* !defined(STANDARD_C) && !defined(HAS_REALLOC_PROTOTYPE) */ |
8d063cd8 | 121 | |
55497cff | 122 | #ifdef HAS_64K_LIMIT |
5f05dabc | 123 | if (size > 0xffff) { |
bf49b057 | 124 | PerlIO_printf(Perl_error_log, |
5f05dabc | 125 | "Reallocation too large: %lx\n", size) FLUSH; |
54aff467 | 126 | my_exit(1); |
5f05dabc | 127 | } |
55497cff | 128 | #endif /* HAS_64K_LIMIT */ |
7614df0c | 129 | if (!size) { |
f2517201 | 130 | safesysfree(where); |
7614df0c JD |
131 | return NULL; |
132 | } | |
133 | ||
378cc40b | 134 | if (!where) |
f2517201 | 135 | return safesysmalloc(size); |
34de22dd LW |
136 | #ifdef DEBUGGING |
137 | if ((long)size < 0) | |
4f63d024 | 138 | Perl_croak_nocontext("panic: realloc"); |
34de22dd | 139 | #endif |
7614df0c | 140 | ptr = PerlMem_realloc(where,size); |
79072805 | 141 | |
7b0972df JH |
142 | DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%"UVxf": (%05d) rfree\n",PTR2UV(where),PL_an++)); |
143 | DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%"UVxf": (%05d) realloc %ld bytes\n",PTR2UV(ptr),PL_an++,(long)size)); | |
79072805 | 144 | |
8d063cd8 LW |
145 | if (ptr != Nullch) |
146 | return ptr; | |
3280af22 | 147 | else if (PL_nomemok) |
7c0587c8 | 148 | return Nullch; |
8d063cd8 | 149 | else { |
bf49b057 | 150 | PerlIO_puts(Perl_error_log,PL_no_mem) FLUSH; |
54aff467 | 151 | my_exit(1); |
4e35701f | 152 | return Nullch; |
8d063cd8 LW |
153 | } |
154 | /*NOTREACHED*/ | |
155 | } | |
156 | ||
f2517201 | 157 | /* safe version of system's free() */ |
8d063cd8 | 158 | |
54310121 | 159 | Free_t |
4f63d024 | 160 | Perl_safesysfree(Malloc_t where) |
8d063cd8 | 161 | { |
54aff467 | 162 | dTHX; |
7b0972df | 163 | DEBUG_m( PerlIO_printf(Perl_debug_log, "0x%"UVxf": (%05d) free\n",PTR2UV(where),PL_an++)); |
378cc40b | 164 | if (where) { |
de3bb511 | 165 | /*SUPPRESS 701*/ |
6ad3d225 | 166 | PerlMem_free(where); |
378cc40b | 167 | } |
8d063cd8 LW |
168 | } |
169 | ||
f2517201 | 170 | /* safe version of system's calloc() */ |
1050c9ca | 171 | |
bd4080b3 | 172 | Malloc_t |
4f63d024 | 173 | Perl_safesyscalloc(MEM_SIZE count, MEM_SIZE size) |
1050c9ca | 174 | { |
54aff467 | 175 | dTHX; |
bd4080b3 | 176 | Malloc_t ptr; |
1050c9ca | 177 | |
55497cff | 178 | #ifdef HAS_64K_LIMIT |
5f05dabc | 179 | if (size * count > 0xffff) { |
bf49b057 | 180 | PerlIO_printf(Perl_error_log, |
5f05dabc | 181 | "Allocation too large: %lx\n", size * count) FLUSH; |
54aff467 | 182 | my_exit(1); |
5f05dabc | 183 | } |
55497cff | 184 | #endif /* HAS_64K_LIMIT */ |
1050c9ca | 185 | #ifdef DEBUGGING |
186 | if ((long)size < 0 || (long)count < 0) | |
4f63d024 | 187 | Perl_croak_nocontext("panic: calloc"); |
1050c9ca | 188 | #endif |
0b7c1c42 | 189 | size *= count; |
6ad3d225 | 190 | ptr = PerlMem_malloc(size?size:1); /* malloc(0) is NASTY on our system */ |
7b0972df | 191 | DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%"UVxf": (%05d) calloc %ld x %ld bytes\n",PTR2UV(ptr),PL_an++,(long)count,(long)size)); |
1050c9ca | 192 | if (ptr != Nullch) { |
193 | memset((void*)ptr, 0, size); | |
194 | return ptr; | |
195 | } | |
3280af22 | 196 | else if (PL_nomemok) |
1050c9ca | 197 | return Nullch; |
198 | else { | |
bf49b057 | 199 | PerlIO_puts(Perl_error_log,PL_no_mem) FLUSH; |
54aff467 | 200 | my_exit(1); |
4e35701f | 201 | return Nullch; |
1050c9ca | 202 | } |
203 | /*NOTREACHED*/ | |
204 | } | |
205 | ||
a687059c LW |
206 | #ifdef LEAKTEST |
207 | ||
8c52afec IZ |
208 | struct mem_test_strut { |
209 | union { | |
210 | long type; | |
211 | char c[2]; | |
212 | } u; | |
213 | long size; | |
214 | }; | |
215 | ||
216 | # define ALIGN sizeof(struct mem_test_strut) | |
217 | ||
218 | # define sizeof_chunk(ch) (((struct mem_test_strut*) (ch))->size) | |
219 | # define typeof_chunk(ch) \ | |
220 | (((struct mem_test_strut*) (ch))->u.c[0] + ((struct mem_test_strut*) (ch))->u.c[1]*100) | |
221 | # define set_typeof_chunk(ch,t) \ | |
222 | (((struct mem_test_strut*) (ch))->u.c[0] = t % 100, ((struct mem_test_strut*) (ch))->u.c[1] = t / 100) | |
223 | #define SIZE_TO_Y(size) ( (size) > MAXY_SIZE \ | |
224 | ? MAXYCOUNT - 1 \ | |
225 | : ( (size) > 40 \ | |
226 | ? ((size) - 1)/8 + 5 \ | |
227 | : ((size) - 1)/4)) | |
8d063cd8 | 228 | |
bd4080b3 | 229 | Malloc_t |
4f63d024 | 230 | Perl_safexmalloc(I32 x, MEM_SIZE size) |
8d063cd8 | 231 | { |
8c52afec | 232 | register char* where = (char*)safemalloc(size + ALIGN); |
8d063cd8 | 233 | |
8c52afec IZ |
234 | xcount[x] += size; |
235 | xycount[x][SIZE_TO_Y(size)]++; | |
236 | set_typeof_chunk(where, x); | |
237 | sizeof_chunk(where) = size; | |
238 | return (Malloc_t)(where + ALIGN); | |
8d063cd8 | 239 | } |
8d063cd8 | 240 | |
bd4080b3 | 241 | Malloc_t |
4f63d024 | 242 | Perl_safexrealloc(Malloc_t wh, MEM_SIZE size) |
a687059c | 243 | { |
8c52afec IZ |
244 | char *where = (char*)wh; |
245 | ||
246 | if (!wh) | |
247 | return safexmalloc(0,size); | |
248 | ||
249 | { | |
250 | MEM_SIZE old = sizeof_chunk(where - ALIGN); | |
251 | int t = typeof_chunk(where - ALIGN); | |
252 | register char* new = (char*)saferealloc(where - ALIGN, size + ALIGN); | |
253 | ||
254 | xycount[t][SIZE_TO_Y(old)]--; | |
255 | xycount[t][SIZE_TO_Y(size)]++; | |
256 | xcount[t] += size - old; | |
257 | sizeof_chunk(new) = size; | |
258 | return (Malloc_t)(new + ALIGN); | |
259 | } | |
a687059c LW |
260 | } |
261 | ||
262 | void | |
4f63d024 | 263 | Perl_safexfree(Malloc_t wh) |
a687059c | 264 | { |
79072805 | 265 | I32 x; |
8c52afec IZ |
266 | char *where = (char*)wh; |
267 | MEM_SIZE size; | |
268 | ||
a687059c LW |
269 | if (!where) |
270 | return; | |
271 | where -= ALIGN; | |
8c52afec | 272 | size = sizeof_chunk(where); |
a687059c | 273 | x = where[0] + 100 * where[1]; |
8c52afec IZ |
274 | xcount[x] -= size; |
275 | xycount[x][SIZE_TO_Y(size)]--; | |
a687059c LW |
276 | safefree(where); |
277 | } | |
278 | ||
bd4080b3 | 279 | Malloc_t |
4f63d024 | 280 | Perl_safexcalloc(I32 x,MEM_SIZE count, MEM_SIZE size) |
1050c9ca | 281 | { |
8c52afec IZ |
282 | register char * where = (char*)safexmalloc(x, size * count + ALIGN); |
283 | xcount[x] += size; | |
284 | xycount[x][SIZE_TO_Y(size)]++; | |
285 | memset((void*)(where + ALIGN), 0, size * count); | |
286 | set_typeof_chunk(where, x); | |
287 | sizeof_chunk(where) = size; | |
288 | return (Malloc_t)(where + ALIGN); | |
1050c9ca | 289 | } |
290 | ||
864dbfa3 | 291 | STATIC void |
cea2e8a9 | 292 | S_xstat(pTHX_ int flag) |
8d063cd8 | 293 | { |
8c52afec IZ |
294 | register I32 i, j, total = 0; |
295 | I32 subtot[MAXYCOUNT]; | |
8d063cd8 | 296 | |
8c52afec IZ |
297 | for (j = 0; j < MAXYCOUNT; j++) { |
298 | subtot[j] = 0; | |
299 | } | |
300 | ||
bf49b057 | 301 | PerlIO_printf(Perl_debug_log, " Id subtot 4 8 12 16 20 24 28 32 36 40 48 56 64 72 80 80+\n", total); |
a687059c | 302 | for (i = 0; i < MAXXCOUNT; i++) { |
8c52afec IZ |
303 | total += xcount[i]; |
304 | for (j = 0; j < MAXYCOUNT; j++) { | |
305 | subtot[j] += xycount[i][j]; | |
306 | } | |
307 | if (flag == 0 | |
308 | ? xcount[i] /* Have something */ | |
309 | : (flag == 2 | |
310 | ? xcount[i] != lastxcount[i] /* Changed */ | |
311 | : xcount[i] > lastxcount[i])) { /* Growed */ | |
bf49b057 | 312 | PerlIO_printf(Perl_debug_log,"%2d %02d %7ld ", i / 100, i % 100, |
8c52afec | 313 | flag == 2 ? xcount[i] - lastxcount[i] : xcount[i]); |
a687059c | 314 | lastxcount[i] = xcount[i]; |
8c52afec IZ |
315 | for (j = 0; j < MAXYCOUNT; j++) { |
316 | if ( flag == 0 | |
317 | ? xycount[i][j] /* Have something */ | |
318 | : (flag == 2 | |
319 | ? xycount[i][j] != lastxycount[i][j] /* Changed */ | |
320 | : xycount[i][j] > lastxycount[i][j])) { /* Growed */ | |
bf49b057 | 321 | PerlIO_printf(Perl_debug_log,"%3ld ", |
8c52afec IZ |
322 | flag == 2 |
323 | ? xycount[i][j] - lastxycount[i][j] | |
324 | : xycount[i][j]); | |
325 | lastxycount[i][j] = xycount[i][j]; | |
326 | } else { | |
bf49b057 | 327 | PerlIO_printf(Perl_debug_log, " . ", xycount[i][j]); |
8c52afec IZ |
328 | } |
329 | } | |
bf49b057 | 330 | PerlIO_printf(Perl_debug_log, "\n"); |
8c52afec IZ |
331 | } |
332 | } | |
333 | if (flag != 2) { | |
bf49b057 | 334 | PerlIO_printf(Perl_debug_log, "Total %7ld ", total); |
8c52afec IZ |
335 | for (j = 0; j < MAXYCOUNT; j++) { |
336 | if (subtot[j]) { | |
bf49b057 | 337 | PerlIO_printf(Perl_debug_log, "%3ld ", subtot[j]); |
8c52afec | 338 | } else { |
bf49b057 | 339 | PerlIO_printf(Perl_debug_log, " . "); |
8c52afec | 340 | } |
8d063cd8 | 341 | } |
bf49b057 | 342 | PerlIO_printf(Perl_debug_log, "\n"); |
8d063cd8 | 343 | } |
8d063cd8 | 344 | } |
a687059c LW |
345 | |
346 | #endif /* LEAKTEST */ | |
8d063cd8 LW |
347 | |
348 | /* copy a string up to some (non-backslashed) delimiter, if any */ | |
349 | ||
350 | char * | |
864dbfa3 | 351 | Perl_delimcpy(pTHX_ register char *to, register char *toend, register char *from, register char *fromend, register int delim, I32 *retlen) |
8d063cd8 | 352 | { |
fc36a67e | 353 | register I32 tolen; |
354 | for (tolen = 0; from < fromend; from++, tolen++) { | |
378cc40b LW |
355 | if (*from == '\\') { |
356 | if (from[1] == delim) | |
357 | from++; | |
fc36a67e | 358 | else { |
359 | if (to < toend) | |
360 | *to++ = *from; | |
361 | tolen++; | |
362 | from++; | |
363 | } | |
378cc40b | 364 | } |
bedebaa5 | 365 | else if (*from == delim) |
8d063cd8 | 366 | break; |
fc36a67e | 367 | if (to < toend) |
368 | *to++ = *from; | |
8d063cd8 | 369 | } |
bedebaa5 CS |
370 | if (to < toend) |
371 | *to = '\0'; | |
fc36a67e | 372 | *retlen = tolen; |
8d063cd8 LW |
373 | return from; |
374 | } | |
375 | ||
376 | /* return ptr to little string in big string, NULL if not found */ | |
378cc40b | 377 | /* This routine was donated by Corey Satten. */ |
8d063cd8 LW |
378 | |
379 | char * | |
864dbfa3 | 380 | Perl_instr(pTHX_ register const char *big, register const char *little) |
378cc40b | 381 | { |
08105a92 | 382 | register const char *s, *x; |
79072805 | 383 | register I32 first; |
378cc40b | 384 | |
a687059c | 385 | if (!little) |
08105a92 | 386 | return (char*)big; |
a687059c | 387 | first = *little++; |
378cc40b | 388 | if (!first) |
08105a92 | 389 | return (char*)big; |
378cc40b LW |
390 | while (*big) { |
391 | if (*big++ != first) | |
392 | continue; | |
393 | for (x=big,s=little; *s; /**/ ) { | |
394 | if (!*x) | |
395 | return Nullch; | |
396 | if (*s++ != *x++) { | |
397 | s--; | |
398 | break; | |
399 | } | |
400 | } | |
401 | if (!*s) | |
08105a92 | 402 | return (char*)(big-1); |
378cc40b LW |
403 | } |
404 | return Nullch; | |
405 | } | |
8d063cd8 | 406 | |
a687059c LW |
407 | /* same as instr but allow embedded nulls */ |
408 | ||
409 | char * | |
864dbfa3 | 410 | Perl_ninstr(pTHX_ register const char *big, register const char *bigend, const char *little, const char *lend) |
8d063cd8 | 411 | { |
08105a92 | 412 | register const char *s, *x; |
79072805 | 413 | register I32 first = *little; |
08105a92 | 414 | register const char *littleend = lend; |
378cc40b | 415 | |
a0d0e21e | 416 | if (!first && little >= littleend) |
08105a92 | 417 | return (char*)big; |
de3bb511 LW |
418 | if (bigend - big < littleend - little) |
419 | return Nullch; | |
a687059c LW |
420 | bigend -= littleend - little++; |
421 | while (big <= bigend) { | |
422 | if (*big++ != first) | |
423 | continue; | |
424 | for (x=big,s=little; s < littleend; /**/ ) { | |
425 | if (*s++ != *x++) { | |
426 | s--; | |
427 | break; | |
428 | } | |
429 | } | |
430 | if (s >= littleend) | |
08105a92 | 431 | return (char*)(big-1); |
378cc40b | 432 | } |
a687059c LW |
433 | return Nullch; |
434 | } | |
435 | ||
436 | /* reverse of the above--find last substring */ | |
437 | ||
438 | char * | |
864dbfa3 | 439 | Perl_rninstr(pTHX_ register const char *big, const char *bigend, const char *little, const char *lend) |
a687059c | 440 | { |
08105a92 GS |
441 | register const char *bigbeg; |
442 | register const char *s, *x; | |
79072805 | 443 | register I32 first = *little; |
08105a92 | 444 | register const char *littleend = lend; |
a687059c | 445 | |
a0d0e21e | 446 | if (!first && little >= littleend) |
08105a92 | 447 | return (char*)bigend; |
a687059c LW |
448 | bigbeg = big; |
449 | big = bigend - (littleend - little++); | |
450 | while (big >= bigbeg) { | |
451 | if (*big-- != first) | |
452 | continue; | |
453 | for (x=big+2,s=little; s < littleend; /**/ ) { | |
454 | if (*s++ != *x++) { | |
455 | s--; | |
456 | break; | |
457 | } | |
458 | } | |
459 | if (s >= littleend) | |
08105a92 | 460 | return (char*)(big+1); |
378cc40b | 461 | } |
a687059c | 462 | return Nullch; |
378cc40b | 463 | } |
a687059c | 464 | |
bbce6d69 | 465 | /* |
466 | * Set up for a new ctype locale. | |
467 | */ | |
55497cff | 468 | void |
864dbfa3 | 469 | Perl_new_ctype(pTHX_ const char *newctype) |
ef7eada9 | 470 | { |
36477c24 | 471 | #ifdef USE_LOCALE_CTYPE |
472 | ||
bbce6d69 | 473 | int i; |
ef7eada9 | 474 | |
bbce6d69 | 475 | for (i = 0; i < 256; i++) { |
476 | if (isUPPER_LC(i)) | |
22c35a8c | 477 | PL_fold_locale[i] = toLOWER_LC(i); |
bbce6d69 | 478 | else if (isLOWER_LC(i)) |
22c35a8c | 479 | PL_fold_locale[i] = toUPPER_LC(i); |
bbce6d69 | 480 | else |
22c35a8c | 481 | PL_fold_locale[i] = i; |
bbce6d69 | 482 | } |
bbce6d69 | 483 | |
36477c24 | 484 | #endif /* USE_LOCALE_CTYPE */ |
485 | } | |
bbce6d69 | 486 | |
487 | /* | |
488 | * Set up for a new collation locale. | |
489 | */ | |
490 | void | |
864dbfa3 | 491 | Perl_new_collate(pTHX_ const char *newcoll) |
bbce6d69 | 492 | { |
36477c24 | 493 | #ifdef USE_LOCALE_COLLATE |
494 | ||
bbce6d69 | 495 | if (! newcoll) { |
3280af22 NIS |
496 | if (PL_collation_name) { |
497 | ++PL_collation_ix; | |
498 | Safefree(PL_collation_name); | |
499 | PL_collation_name = NULL; | |
500 | PL_collation_standard = TRUE; | |
501 | PL_collxfrm_base = 0; | |
502 | PL_collxfrm_mult = 2; | |
bbce6d69 | 503 | } |
504 | return; | |
505 | } | |
506 | ||
3280af22 NIS |
507 | if (! PL_collation_name || strNE(PL_collation_name, newcoll)) { |
508 | ++PL_collation_ix; | |
509 | Safefree(PL_collation_name); | |
510 | PL_collation_name = savepv(newcoll); | |
511 | PL_collation_standard = (strEQ(newcoll, "C") || strEQ(newcoll, "POSIX")); | |
bbce6d69 | 512 | |
bbce6d69 | 513 | { |
514 | /* 2: at most so many chars ('a', 'b'). */ | |
515 | /* 50: surely no system expands a char more. */ | |
516 | #define XFRMBUFSIZE (2 * 50) | |
517 | char xbuf[XFRMBUFSIZE]; | |
518 | Size_t fa = strxfrm(xbuf, "a", XFRMBUFSIZE); | |
519 | Size_t fb = strxfrm(xbuf, "ab", XFRMBUFSIZE); | |
520 | SSize_t mult = fb - fa; | |
521 | if (mult < 1) | |
cea2e8a9 | 522 | Perl_croak(aTHX_ "strxfrm() gets absurd"); |
3280af22 NIS |
523 | PL_collxfrm_base = (fa > mult) ? (fa - mult) : 0; |
524 | PL_collxfrm_mult = mult; | |
bbce6d69 | 525 | } |
bbce6d69 | 526 | } |
bbce6d69 | 527 | |
36477c24 | 528 | #endif /* USE_LOCALE_COLLATE */ |
529 | } | |
bbce6d69 | 530 | |
097ee67d | 531 | void |
51371543 | 532 | Perl_set_numeric_radix(pTHX) |
097ee67d JH |
533 | { |
534 | #ifdef USE_LOCALE_NUMERIC | |
535 | # ifdef HAS_LOCALECONV | |
536 | struct lconv* lc; | |
537 | ||
538 | lc = localeconv(); | |
539 | if (lc && lc->decimal_point) | |
540 | /* We assume that decimal separator aka the radix | |
541 | * character is always a single character. If it | |
542 | * ever is a string, this needs to be rethunk. */ | |
543 | PL_numeric_radix = *lc->decimal_point; | |
544 | else | |
545 | PL_numeric_radix = 0; | |
546 | # endif /* HAS_LOCALECONV */ | |
097ee67d JH |
547 | #endif /* USE_LOCALE_NUMERIC */ |
548 | } | |
549 | ||
bbce6d69 | 550 | /* |
551 | * Set up for a new numeric locale. | |
552 | */ | |
553 | void | |
864dbfa3 | 554 | Perl_new_numeric(pTHX_ const char *newnum) |
bbce6d69 | 555 | { |
36477c24 | 556 | #ifdef USE_LOCALE_NUMERIC |
557 | ||
bbce6d69 | 558 | if (! newnum) { |
3280af22 NIS |
559 | if (PL_numeric_name) { |
560 | Safefree(PL_numeric_name); | |
561 | PL_numeric_name = NULL; | |
562 | PL_numeric_standard = TRUE; | |
563 | PL_numeric_local = TRUE; | |
bbce6d69 | 564 | } |
565 | return; | |
566 | } | |
567 | ||
3280af22 NIS |
568 | if (! PL_numeric_name || strNE(PL_numeric_name, newnum)) { |
569 | Safefree(PL_numeric_name); | |
570 | PL_numeric_name = savepv(newnum); | |
571 | PL_numeric_standard = (strEQ(newnum, "C") || strEQ(newnum, "POSIX")); | |
572 | PL_numeric_local = TRUE; | |
51371543 | 573 | set_numeric_radix(); |
bbce6d69 | 574 | } |
36477c24 | 575 | |
576 | #endif /* USE_LOCALE_NUMERIC */ | |
bbce6d69 | 577 | } |
578 | ||
579 | void | |
864dbfa3 | 580 | Perl_set_numeric_standard(pTHX) |
bbce6d69 | 581 | { |
5f05dabc | 582 | #ifdef USE_LOCALE_NUMERIC |
583 | ||
3280af22 | 584 | if (! PL_numeric_standard) { |
bbce6d69 | 585 | setlocale(LC_NUMERIC, "C"); |
3280af22 NIS |
586 | PL_numeric_standard = TRUE; |
587 | PL_numeric_local = FALSE; | |
bbce6d69 | 588 | } |
5f05dabc | 589 | |
590 | #endif /* USE_LOCALE_NUMERIC */ | |
bbce6d69 | 591 | } |
592 | ||
593 | void | |
864dbfa3 | 594 | Perl_set_numeric_local(pTHX) |
bbce6d69 | 595 | { |
5f05dabc | 596 | #ifdef USE_LOCALE_NUMERIC |
597 | ||
3280af22 NIS |
598 | if (! PL_numeric_local) { |
599 | setlocale(LC_NUMERIC, PL_numeric_name); | |
600 | PL_numeric_standard = FALSE; | |
601 | PL_numeric_local = TRUE; | |
51371543 | 602 | set_numeric_radix(); |
bbce6d69 | 603 | } |
bbce6d69 | 604 | |
36477c24 | 605 | #endif /* USE_LOCALE_NUMERIC */ |
5f05dabc | 606 | } |
36477c24 | 607 | |
36477c24 | 608 | /* |
609 | * Initialize locale awareness. | |
610 | */ | |
f0c5b223 | 611 | int |
864dbfa3 | 612 | Perl_init_i18nl10n(pTHX_ int printwarn) |
f0c5b223 TB |
613 | { |
614 | int ok = 1; | |
615 | /* returns | |
616 | * 1 = set ok or not applicable, | |
617 | * 0 = fallback to C locale, | |
618 | * -1 = fallback to C locale failed | |
619 | */ | |
bbce6d69 | 620 | |
36477c24 | 621 | #ifdef USE_LOCALE |
bbce6d69 | 622 | |
36477c24 | 623 | #ifdef USE_LOCALE_CTYPE |
bbce6d69 | 624 | char *curctype = NULL; |
36477c24 | 625 | #endif /* USE_LOCALE_CTYPE */ |
626 | #ifdef USE_LOCALE_COLLATE | |
bbce6d69 | 627 | char *curcoll = NULL; |
36477c24 | 628 | #endif /* USE_LOCALE_COLLATE */ |
629 | #ifdef USE_LOCALE_NUMERIC | |
bbce6d69 | 630 | char *curnum = NULL; |
36477c24 | 631 | #endif /* USE_LOCALE_NUMERIC */ |
3aeabbed JH |
632 | #ifdef __GLIBC__ |
633 | char *language = PerlEnv_getenv("LANGUAGE"); | |
634 | #endif | |
76e3520e GS |
635 | char *lc_all = PerlEnv_getenv("LC_ALL"); |
636 | char *lang = PerlEnv_getenv("LANG"); | |
bbce6d69 | 637 | bool setlocale_failure = FALSE; |
f0c5b223 | 638 | |
02b32252 CS |
639 | #ifdef LOCALE_ENVIRON_REQUIRED |
640 | ||
641 | /* | |
642 | * Ultrix setlocale(..., "") fails if there are no environment | |
643 | * variables from which to get a locale name. | |
644 | */ | |
645 | ||
646 | bool done = FALSE; | |
647 | ||
648 | #ifdef LC_ALL | |
649 | if (lang) { | |
650 | if (setlocale(LC_ALL, "")) | |
651 | done = TRUE; | |
652 | else | |
653 | setlocale_failure = TRUE; | |
654 | } | |
0644c9cb | 655 | if (!setlocale_failure) { |
02b32252 | 656 | #ifdef USE_LOCALE_CTYPE |
0644c9cb IS |
657 | if (! (curctype = |
658 | setlocale(LC_CTYPE, | |
659 | (!done && (lang || PerlEnv_getenv("LC_CTYPE"))) | |
02b32252 CS |
660 | ? "" : Nullch))) |
661 | setlocale_failure = TRUE; | |
662 | #endif /* USE_LOCALE_CTYPE */ | |
663 | #ifdef USE_LOCALE_COLLATE | |
0644c9cb IS |
664 | if (! (curcoll = |
665 | setlocale(LC_COLLATE, | |
666 | (!done && (lang || PerlEnv_getenv("LC_COLLATE"))) | |
02b32252 CS |
667 | ? "" : Nullch))) |
668 | setlocale_failure = TRUE; | |
669 | #endif /* USE_LOCALE_COLLATE */ | |
670 | #ifdef USE_LOCALE_NUMERIC | |
0644c9cb IS |
671 | if (! (curnum = |
672 | setlocale(LC_NUMERIC, | |
673 | (!done && (lang || PerlEnv_getenv("LC_NUMERIC"))) | |
02b32252 CS |
674 | ? "" : Nullch))) |
675 | setlocale_failure = TRUE; | |
676 | #endif /* USE_LOCALE_NUMERIC */ | |
677 | } | |
678 | ||
0644c9cb | 679 | #endif /* LC_ALL */ |
02b32252 | 680 | |
0644c9cb | 681 | #endif /* !LOCALE_ENVIRON_REQUIRED */ |
5f05dabc | 682 | |
0644c9cb | 683 | #ifdef LC_ALL |
bbce6d69 | 684 | if (! setlocale(LC_ALL, "")) |
685 | setlocale_failure = TRUE; | |
0644c9cb | 686 | #endif /* LC_ALL */ |
bbce6d69 | 687 | |
0644c9cb | 688 | if (!setlocale_failure) { |
36477c24 | 689 | #ifdef USE_LOCALE_CTYPE |
0644c9cb IS |
690 | if (! (curctype = setlocale(LC_CTYPE, ""))) |
691 | setlocale_failure = TRUE; | |
36477c24 | 692 | #endif /* USE_LOCALE_CTYPE */ |
693 | #ifdef USE_LOCALE_COLLATE | |
0644c9cb IS |
694 | if (! (curcoll = setlocale(LC_COLLATE, ""))) |
695 | setlocale_failure = TRUE; | |
36477c24 | 696 | #endif /* USE_LOCALE_COLLATE */ |
697 | #ifdef USE_LOCALE_NUMERIC | |
0644c9cb IS |
698 | if (! (curnum = setlocale(LC_NUMERIC, ""))) |
699 | setlocale_failure = TRUE; | |
36477c24 | 700 | #endif /* USE_LOCALE_NUMERIC */ |
0644c9cb | 701 | } |
02b32252 | 702 | |
5f05dabc | 703 | if (setlocale_failure) { |
704 | char *p; | |
705 | bool locwarn = (printwarn > 1 || | |
706 | printwarn && | |
76e3520e | 707 | (!(p = PerlEnv_getenv("PERL_BADLANG")) || atoi(p))); |
20cec16a | 708 | |
5f05dabc | 709 | if (locwarn) { |
710 | #ifdef LC_ALL | |
711 | ||
bf49b057 | 712 | PerlIO_printf(Perl_error_log, |
5f05dabc | 713 | "perl: warning: Setting locale failed.\n"); |
714 | ||
715 | #else /* !LC_ALL */ | |
716 | ||
bf49b057 | 717 | PerlIO_printf(Perl_error_log, |
bbce6d69 | 718 | "perl: warning: Setting locale failed for the categories:\n\t"); |
36477c24 | 719 | #ifdef USE_LOCALE_CTYPE |
bbce6d69 | 720 | if (! curctype) |
bf49b057 | 721 | PerlIO_printf(Perl_error_log, "LC_CTYPE "); |
36477c24 | 722 | #endif /* USE_LOCALE_CTYPE */ |
723 | #ifdef USE_LOCALE_COLLATE | |
bbce6d69 | 724 | if (! curcoll) |
bf49b057 | 725 | PerlIO_printf(Perl_error_log, "LC_COLLATE "); |
36477c24 | 726 | #endif /* USE_LOCALE_COLLATE */ |
727 | #ifdef USE_LOCALE_NUMERIC | |
bbce6d69 | 728 | if (! curnum) |
bf49b057 | 729 | PerlIO_printf(Perl_error_log, "LC_NUMERIC "); |
36477c24 | 730 | #endif /* USE_LOCALE_NUMERIC */ |
bf49b057 | 731 | PerlIO_printf(Perl_error_log, "\n"); |
bbce6d69 | 732 | |
5f05dabc | 733 | #endif /* LC_ALL */ |
734 | ||
bf49b057 | 735 | PerlIO_printf(Perl_error_log, |
bbce6d69 | 736 | "perl: warning: Please check that your locale settings:\n"); |
ef7eada9 | 737 | |
3aeabbed | 738 | #ifdef __GLIBC__ |
bf49b057 | 739 | PerlIO_printf(Perl_error_log, |
3aeabbed JH |
740 | "\tLANGUAGE = %c%s%c,\n", |
741 | language ? '"' : '(', | |
742 | language ? language : "unset", | |
743 | language ? '"' : ')'); | |
744 | #endif | |
745 | ||
bf49b057 | 746 | PerlIO_printf(Perl_error_log, |
bbce6d69 | 747 | "\tLC_ALL = %c%s%c,\n", |
748 | lc_all ? '"' : '(', | |
749 | lc_all ? lc_all : "unset", | |
750 | lc_all ? '"' : ')'); | |
5f05dabc | 751 | |
752 | { | |
753 | char **e; | |
754 | for (e = environ; *e; e++) { | |
755 | if (strnEQ(*e, "LC_", 3) | |
756 | && strnNE(*e, "LC_ALL=", 7) | |
757 | && (p = strchr(*e, '='))) | |
bf49b057 | 758 | PerlIO_printf(Perl_error_log, "\t%.*s = \"%s\",\n", |
fb73857a | 759 | (int)(p - *e), *e, p + 1); |
5f05dabc | 760 | } |
761 | } | |
762 | ||
bf49b057 | 763 | PerlIO_printf(Perl_error_log, |
bbce6d69 | 764 | "\tLANG = %c%s%c\n", |
5f05dabc | 765 | lang ? '"' : '(', |
bbce6d69 | 766 | lang ? lang : "unset", |
767 | lang ? '"' : ')'); | |
ef7eada9 | 768 | |
bf49b057 | 769 | PerlIO_printf(Perl_error_log, |
bbce6d69 | 770 | " are supported and installed on your system.\n"); |
5f05dabc | 771 | } |
ef7eada9 | 772 | |
5f05dabc | 773 | #ifdef LC_ALL |
774 | ||
775 | if (setlocale(LC_ALL, "C")) { | |
776 | if (locwarn) | |
bf49b057 | 777 | PerlIO_printf(Perl_error_log, |
5f05dabc | 778 | "perl: warning: Falling back to the standard locale (\"C\").\n"); |
bbce6d69 | 779 | ok = 0; |
ef7eada9 | 780 | } |
5f05dabc | 781 | else { |
782 | if (locwarn) | |
bf49b057 | 783 | PerlIO_printf(Perl_error_log, |
5f05dabc | 784 | "perl: warning: Failed to fall back to the standard locale (\"C\").\n"); |
785 | ok = -1; | |
786 | } | |
bbce6d69 | 787 | |
5f05dabc | 788 | #else /* ! LC_ALL */ |
789 | ||
790 | if (0 | |
36477c24 | 791 | #ifdef USE_LOCALE_CTYPE |
5f05dabc | 792 | || !(curctype || setlocale(LC_CTYPE, "C")) |
36477c24 | 793 | #endif /* USE_LOCALE_CTYPE */ |
794 | #ifdef USE_LOCALE_COLLATE | |
5f05dabc | 795 | || !(curcoll || setlocale(LC_COLLATE, "C")) |
36477c24 | 796 | #endif /* USE_LOCALE_COLLATE */ |
797 | #ifdef USE_LOCALE_NUMERIC | |
5f05dabc | 798 | || !(curnum || setlocale(LC_NUMERIC, "C")) |
36477c24 | 799 | #endif /* USE_LOCALE_NUMERIC */ |
5f05dabc | 800 | ) |
801 | { | |
802 | if (locwarn) | |
bf49b057 | 803 | PerlIO_printf(Perl_error_log, |
5f05dabc | 804 | "perl: warning: Cannot fall back to the standard locale (\"C\").\n"); |
805 | ok = -1; | |
bbce6d69 | 806 | } |
5f05dabc | 807 | |
bbce6d69 | 808 | #endif /* ! LC_ALL */ |
5f05dabc | 809 | |
810 | #ifdef USE_LOCALE_CTYPE | |
811 | curctype = setlocale(LC_CTYPE, Nullch); | |
812 | #endif /* USE_LOCALE_CTYPE */ | |
813 | #ifdef USE_LOCALE_COLLATE | |
814 | curcoll = setlocale(LC_COLLATE, Nullch); | |
815 | #endif /* USE_LOCALE_COLLATE */ | |
816 | #ifdef USE_LOCALE_NUMERIC | |
817 | curnum = setlocale(LC_NUMERIC, Nullch); | |
818 | #endif /* USE_LOCALE_NUMERIC */ | |
ef7eada9 JH |
819 | } |
820 | ||
36477c24 | 821 | #ifdef USE_LOCALE_CTYPE |
864dbfa3 | 822 | new_ctype(curctype); |
36477c24 | 823 | #endif /* USE_LOCALE_CTYPE */ |
bbce6d69 | 824 | |
36477c24 | 825 | #ifdef USE_LOCALE_COLLATE |
864dbfa3 | 826 | new_collate(curcoll); |
36477c24 | 827 | #endif /* USE_LOCALE_COLLATE */ |
bbce6d69 | 828 | |
36477c24 | 829 | #ifdef USE_LOCALE_NUMERIC |
864dbfa3 | 830 | new_numeric(curnum); |
36477c24 | 831 | #endif /* USE_LOCALE_NUMERIC */ |
ef7eada9 | 832 | |
36477c24 | 833 | #endif /* USE_LOCALE */ |
ef7eada9 | 834 | |
f0c5b223 TB |
835 | return ok; |
836 | } | |
837 | ||
bbce6d69 | 838 | /* Backwards compatibility. */ |
839 | int | |
864dbfa3 | 840 | Perl_init_i18nl14n(pTHX_ int printwarn) |
bbce6d69 | 841 | { |
864dbfa3 | 842 | return init_i18nl10n(printwarn); |
bbce6d69 | 843 | } |
ef7eada9 | 844 | |
36477c24 | 845 | #ifdef USE_LOCALE_COLLATE |
ef7eada9 | 846 | |
bbce6d69 | 847 | /* |
848 | * mem_collxfrm() is a bit like strxfrm() but with two important | |
849 | * differences. First, it handles embedded NULs. Second, it allocates | |
850 | * a bit more memory than needed for the transformed data itself. | |
851 | * The real transformed data begins at offset sizeof(collationix). | |
852 | * Please see sv_collxfrm() to see how this is used. | |
853 | */ | |
854 | char * | |
864dbfa3 | 855 | Perl_mem_collxfrm(pTHX_ const char *s, STRLEN len, STRLEN *xlen) |
bbce6d69 | 856 | { |
857 | char *xbuf; | |
76e3520e | 858 | STRLEN xAlloc, xin, xout; /* xalloc is a reserved word in VC */ |
bbce6d69 | 859 | |
860 | /* the first sizeof(collationix) bytes are used by sv_collxfrm(). */ | |
861 | /* the +1 is for the terminating NUL. */ | |
862 | ||
3280af22 | 863 | xAlloc = sizeof(PL_collation_ix) + PL_collxfrm_base + (PL_collxfrm_mult * len) + 1; |
76e3520e | 864 | New(171, xbuf, xAlloc, char); |
bbce6d69 | 865 | if (! xbuf) |
866 | goto bad; | |
867 | ||
3280af22 NIS |
868 | *(U32*)xbuf = PL_collation_ix; |
869 | xout = sizeof(PL_collation_ix); | |
bbce6d69 | 870 | for (xin = 0; xin < len; ) { |
871 | SSize_t xused; | |
872 | ||
873 | for (;;) { | |
76e3520e | 874 | xused = strxfrm(xbuf + xout, s + xin, xAlloc - xout); |
bbce6d69 | 875 | if (xused == -1) |
876 | goto bad; | |
76e3520e | 877 | if (xused < xAlloc - xout) |
bbce6d69 | 878 | break; |
76e3520e GS |
879 | xAlloc = (2 * xAlloc) + 1; |
880 | Renew(xbuf, xAlloc, char); | |
bbce6d69 | 881 | if (! xbuf) |
882 | goto bad; | |
883 | } | |
ef7eada9 | 884 | |
bbce6d69 | 885 | xin += strlen(s + xin) + 1; |
886 | xout += xused; | |
887 | ||
888 | /* Embedded NULs are understood but silently skipped | |
889 | * because they make no sense in locale collation. */ | |
890 | } | |
ef7eada9 | 891 | |
bbce6d69 | 892 | xbuf[xout] = '\0'; |
3280af22 | 893 | *xlen = xout - sizeof(PL_collation_ix); |
bbce6d69 | 894 | return xbuf; |
895 | ||
896 | bad: | |
897 | Safefree(xbuf); | |
898 | *xlen = 0; | |
899 | return NULL; | |
ef7eada9 JH |
900 | } |
901 | ||
36477c24 | 902 | #endif /* USE_LOCALE_COLLATE */ |
bbce6d69 | 903 | |
cf93c79d IZ |
904 | #define FBM_TABLE_OFFSET 2 /* Number of bytes between EOS and table*/ |
905 | ||
906 | /* As a space optimization, we do not compile tables for strings of length | |
907 | 0 and 1, and for strings of length 2 unless FBMcf_TAIL. These are | |
908 | special-cased in fbm_instr(). | |
909 | ||
910 | If FBMcf_TAIL, the table is created as if the string has a trailing \n. */ | |
911 | ||
378cc40b | 912 | void |
7506f9c3 | 913 | Perl_fbm_compile(pTHX_ SV *sv, U32 flags) |
378cc40b | 914 | { |
942e002e GS |
915 | register U8 *s; |
916 | register U8 *table; | |
79072805 | 917 | register U32 i; |
0b71040e | 918 | STRLEN len; |
79072805 LW |
919 | I32 rarest = 0; |
920 | U32 frequency = 256; | |
921 | ||
cf93c79d IZ |
922 | if (flags & FBMcf_TAIL) |
923 | sv_catpvn(sv, "\n", 1); /* Taken into account in fbm_instr() */ | |
942e002e | 924 | s = (U8*)SvPV_force(sv, len); |
07f14f54 | 925 | (void)SvUPGRADE(sv, SVt_PVBM); |
cf93c79d IZ |
926 | if (len == 0) /* TAIL might be on on a zero-length string. */ |
927 | return; | |
02128f11 | 928 | if (len > 2) { |
7506f9c3 | 929 | U8 mlen; |
cf93c79d IZ |
930 | unsigned char *sb; |
931 | ||
7506f9c3 | 932 | if (len > 255) |
cf93c79d | 933 | mlen = 255; |
7506f9c3 GS |
934 | else |
935 | mlen = (U8)len; | |
936 | Sv_Grow(sv, len + 256 + FBM_TABLE_OFFSET); | |
cf93c79d | 937 | table = (unsigned char*)(SvPVX(sv) + len + FBM_TABLE_OFFSET); |
7506f9c3 GS |
938 | s = table - 1 - FBM_TABLE_OFFSET; /* last char */ |
939 | memset((void*)table, mlen, 256); | |
940 | table[-1] = (U8)flags; | |
02128f11 | 941 | i = 0; |
7506f9c3 | 942 | sb = s - mlen + 1; /* first char (maybe) */ |
cf93c79d IZ |
943 | while (s >= sb) { |
944 | if (table[*s] == mlen) | |
7506f9c3 | 945 | table[*s] = (U8)i; |
cf93c79d IZ |
946 | s--, i++; |
947 | } | |
378cc40b | 948 | } |
bbce6d69 | 949 | sv_magic(sv, Nullsv, 'B', Nullch, 0); /* deep magic */ |
79072805 | 950 | SvVALID_on(sv); |
378cc40b | 951 | |
463ee0b2 | 952 | s = (unsigned char*)(SvPVX(sv)); /* deeper magic */ |
bbce6d69 | 953 | for (i = 0; i < len; i++) { |
22c35a8c | 954 | if (PL_freq[s[i]] < frequency) { |
bbce6d69 | 955 | rarest = i; |
22c35a8c | 956 | frequency = PL_freq[s[i]]; |
378cc40b LW |
957 | } |
958 | } | |
79072805 LW |
959 | BmRARE(sv) = s[rarest]; |
960 | BmPREVIOUS(sv) = rarest; | |
cf93c79d IZ |
961 | BmUSEFUL(sv) = 100; /* Initial value */ |
962 | if (flags & FBMcf_TAIL) | |
963 | SvTAIL_on(sv); | |
7506f9c3 GS |
964 | DEBUG_r(PerlIO_printf(Perl_debug_log, "rarest char %c at %d\n", |
965 | BmRARE(sv),BmPREVIOUS(sv))); | |
378cc40b LW |
966 | } |
967 | ||
cf93c79d IZ |
968 | /* If SvTAIL(littlestr), it has a fake '\n' at end. */ |
969 | /* If SvTAIL is actually due to \Z or \z, this gives false positives | |
970 | if multiline */ | |
971 | ||
378cc40b | 972 | char * |
864dbfa3 | 973 | Perl_fbm_instr(pTHX_ unsigned char *big, register unsigned char *bigend, SV *littlestr, U32 flags) |
378cc40b | 974 | { |
a687059c | 975 | register unsigned char *s; |
cf93c79d IZ |
976 | STRLEN l; |
977 | register unsigned char *little = (unsigned char *)SvPV(littlestr,l); | |
978 | register STRLEN littlelen = l; | |
979 | register I32 multiline = flags & FBMrf_MULTILINE; | |
980 | ||
981 | if (bigend - big < littlelen) { | |
982 | check_tail: | |
983 | if ( SvTAIL(littlestr) | |
984 | && (bigend - big == littlelen - 1) | |
985 | && (littlelen == 1 | |
986 | || *big == *little && memEQ(big, little, littlelen - 1))) | |
987 | return (char*)big; | |
988 | return Nullch; | |
989 | } | |
378cc40b | 990 | |
cf93c79d IZ |
991 | if (littlelen <= 2) { /* Special-cased */ |
992 | register char c; | |
993 | ||
994 | if (littlelen == 1) { | |
995 | if (SvTAIL(littlestr) && !multiline) { /* Anchor only! */ | |
996 | /* Know that bigend != big. */ | |
997 | if (bigend[-1] == '\n') | |
998 | return (char *)(bigend - 1); | |
999 | return (char *) bigend; | |
1000 | } | |
1001 | s = big; | |
1002 | while (s < bigend) { | |
1003 | if (*s == *little) | |
1004 | return (char *)s; | |
1005 | s++; | |
1006 | } | |
1007 | if (SvTAIL(littlestr)) | |
1008 | return (char *) bigend; | |
1009 | return Nullch; | |
1010 | } | |
1011 | if (!littlelen) | |
1012 | return (char*)big; /* Cannot be SvTAIL! */ | |
1013 | ||
1014 | /* littlelen is 2 */ | |
1015 | if (SvTAIL(littlestr) && !multiline) { | |
1016 | if (bigend[-1] == '\n' && bigend[-2] == *little) | |
1017 | return (char*)bigend - 2; | |
1018 | if (bigend[-1] == *little) | |
1019 | return (char*)bigend - 1; | |
1020 | return Nullch; | |
1021 | } | |
1022 | { | |
1023 | /* This should be better than FBM if c1 == c2, and almost | |
1024 | as good otherwise: maybe better since we do less indirection. | |
1025 | And we save a lot of memory by caching no table. */ | |
1026 | register unsigned char c1 = little[0]; | |
1027 | register unsigned char c2 = little[1]; | |
1028 | ||
1029 | s = big + 1; | |
1030 | bigend--; | |
1031 | if (c1 != c2) { | |
1032 | while (s <= bigend) { | |
1033 | if (s[0] == c2) { | |
1034 | if (s[-1] == c1) | |
1035 | return (char*)s - 1; | |
1036 | s += 2; | |
1037 | continue; | |
3fe6f2dc | 1038 | } |
cf93c79d IZ |
1039 | next_chars: |
1040 | if (s[0] == c1) { | |
1041 | if (s == bigend) | |
1042 | goto check_1char_anchor; | |
1043 | if (s[1] == c2) | |
1044 | return (char*)s; | |
1045 | else { | |
1046 | s++; | |
1047 | goto next_chars; | |
1048 | } | |
1049 | } | |
1050 | else | |
1051 | s += 2; | |
1052 | } | |
1053 | goto check_1char_anchor; | |
1054 | } | |
1055 | /* Now c1 == c2 */ | |
1056 | while (s <= bigend) { | |
1057 | if (s[0] == c1) { | |
1058 | if (s[-1] == c1) | |
1059 | return (char*)s - 1; | |
1060 | if (s == bigend) | |
1061 | goto check_1char_anchor; | |
1062 | if (s[1] == c1) | |
1063 | return (char*)s; | |
1064 | s += 3; | |
02128f11 | 1065 | } |
c277df42 | 1066 | else |
cf93c79d | 1067 | s += 2; |
c277df42 | 1068 | } |
c277df42 | 1069 | } |
cf93c79d IZ |
1070 | check_1char_anchor: /* One char and anchor! */ |
1071 | if (SvTAIL(littlestr) && (*bigend == *little)) | |
1072 | return (char *)bigend; /* bigend is already decremented. */ | |
1073 | return Nullch; | |
d48672a2 | 1074 | } |
cf93c79d | 1075 | if (SvTAIL(littlestr) && !multiline) { /* tail anchored? */ |
bbce6d69 | 1076 | s = bigend - littlelen; |
7506f9c3 | 1077 | if (s >= big && bigend[-1] == '\n' && *s == *little |
cf93c79d IZ |
1078 | /* Automatically of length > 2 */ |
1079 | && memEQ((char*)s + 1, (char*)little + 1, littlelen - 2)) | |
7506f9c3 | 1080 | { |
bbce6d69 | 1081 | return (char*)s; /* how sweet it is */ |
7506f9c3 GS |
1082 | } |
1083 | if (s[1] == *little | |
1084 | && memEQ((char*)s + 2, (char*)little + 1, littlelen - 2)) | |
1085 | { | |
cf93c79d | 1086 | return (char*)s + 1; /* how sweet it is */ |
7506f9c3 | 1087 | } |
02128f11 IZ |
1088 | return Nullch; |
1089 | } | |
cf93c79d IZ |
1090 | if (SvTYPE(littlestr) != SVt_PVBM || !SvVALID(littlestr)) { |
1091 | char *b = ninstr((char*)big,(char*)bigend, | |
1092 | (char*)little, (char*)little + littlelen); | |
1093 | ||
1094 | if (!b && SvTAIL(littlestr)) { /* Automatically multiline! */ | |
1095 | /* Chop \n from littlestr: */ | |
1096 | s = bigend - littlelen + 1; | |
7506f9c3 GS |
1097 | if (*s == *little |
1098 | && memEQ((char*)s + 1, (char*)little + 1, littlelen - 2)) | |
1099 | { | |
3fe6f2dc | 1100 | return (char*)s; |
7506f9c3 | 1101 | } |
cf93c79d | 1102 | return Nullch; |
a687059c | 1103 | } |
cf93c79d | 1104 | return b; |
a687059c | 1105 | } |
cf93c79d IZ |
1106 | |
1107 | { /* Do actual FBM. */ | |
1108 | register unsigned char *table = little + littlelen + FBM_TABLE_OFFSET; | |
1109 | register unsigned char *oldlittle; | |
1110 | ||
1111 | if (littlelen > bigend - big) | |
1112 | return Nullch; | |
1113 | --littlelen; /* Last char found by table lookup */ | |
1114 | ||
1115 | s = big + littlelen; | |
1116 | little += littlelen; /* last char */ | |
1117 | oldlittle = little; | |
1118 | if (s < bigend) { | |
1119 | register I32 tmp; | |
1120 | ||
1121 | top2: | |
1122 | /*SUPPRESS 560*/ | |
7506f9c3 | 1123 | if ((tmp = table[*s])) { |
62b28dd9 | 1124 | #ifdef POINTERRIGOR |
cf93c79d IZ |
1125 | if (bigend - s > tmp) { |
1126 | s += tmp; | |
1127 | goto top2; | |
1128 | } | |
bbce6d69 | 1129 | s += tmp; |
62b28dd9 | 1130 | #else |
cf93c79d | 1131 | if ((s += tmp) < bigend) |
62b28dd9 | 1132 | goto top2; |
cf93c79d IZ |
1133 | #endif |
1134 | goto check_end; | |
1135 | } | |
1136 | else { /* less expensive than calling strncmp() */ | |
1137 | register unsigned char *olds = s; | |
1138 | ||
1139 | tmp = littlelen; | |
1140 | ||
1141 | while (tmp--) { | |
1142 | if (*--s == *--little) | |
1143 | continue; | |
1144 | differ: | |
1145 | s = olds + 1; /* here we pay the price for failure */ | |
1146 | little = oldlittle; | |
1147 | if (s < bigend) /* fake up continue to outer loop */ | |
1148 | goto top2; | |
1149 | goto check_end; | |
1150 | } | |
1151 | return (char *)s; | |
a687059c | 1152 | } |
378cc40b | 1153 | } |
cf93c79d IZ |
1154 | check_end: |
1155 | if ( s == bigend && (table[-1] & FBMcf_TAIL) | |
1156 | && memEQ(bigend - littlelen, oldlittle - littlelen, littlelen) ) | |
1157 | return (char*)bigend - littlelen; | |
1158 | return Nullch; | |
378cc40b | 1159 | } |
378cc40b LW |
1160 | } |
1161 | ||
c277df42 IZ |
1162 | /* start_shift, end_shift are positive quantities which give offsets |
1163 | of ends of some substring of bigstr. | |
1164 | If `last' we want the last occurence. | |
1165 | old_posp is the way of communication between consequent calls if | |
1166 | the next call needs to find the . | |
1167 | The initial *old_posp should be -1. | |
cf93c79d IZ |
1168 | |
1169 | Note that we take into account SvTAIL, so one can get extra | |
1170 | optimizations if _ALL flag is set. | |
c277df42 IZ |
1171 | */ |
1172 | ||
cf93c79d IZ |
1173 | /* If SvTAIL is actually due to \Z or \z, this gives false positives |
1174 | if PL_multiline. In fact if !PL_multiline the autoritative answer | |
1175 | is not supported yet. */ | |
1176 | ||
378cc40b | 1177 | char * |
864dbfa3 | 1178 | Perl_screaminstr(pTHX_ SV *bigstr, SV *littlestr, I32 start_shift, I32 end_shift, I32 *old_posp, I32 last) |
378cc40b | 1179 | { |
5c0ca799 | 1180 | dTHR; |
a687059c LW |
1181 | register unsigned char *s, *x; |
1182 | register unsigned char *big; | |
79072805 LW |
1183 | register I32 pos; |
1184 | register I32 previous; | |
1185 | register I32 first; | |
a687059c | 1186 | register unsigned char *little; |
c277df42 | 1187 | register I32 stop_pos; |
a687059c | 1188 | register unsigned char *littleend; |
c277df42 | 1189 | I32 found = 0; |
378cc40b | 1190 | |
c277df42 | 1191 | if (*old_posp == -1 |
3280af22 | 1192 | ? (pos = PL_screamfirst[BmRARE(littlestr)]) < 0 |
cf93c79d IZ |
1193 | : (((pos = *old_posp), pos += PL_screamnext[pos]) == 0)) { |
1194 | cant_find: | |
1195 | if ( BmRARE(littlestr) == '\n' | |
1196 | && BmPREVIOUS(littlestr) == SvCUR(littlestr) - 1) { | |
1197 | little = (unsigned char *)(SvPVX(littlestr)); | |
1198 | littleend = little + SvCUR(littlestr); | |
1199 | first = *little++; | |
1200 | goto check_tail; | |
1201 | } | |
378cc40b | 1202 | return Nullch; |
cf93c79d IZ |
1203 | } |
1204 | ||
463ee0b2 | 1205 | little = (unsigned char *)(SvPVX(littlestr)); |
79072805 | 1206 | littleend = little + SvCUR(littlestr); |
378cc40b | 1207 | first = *little++; |
c277df42 | 1208 | /* The value of pos we can start at: */ |
79072805 | 1209 | previous = BmPREVIOUS(littlestr); |
463ee0b2 | 1210 | big = (unsigned char *)(SvPVX(bigstr)); |
c277df42 IZ |
1211 | /* The value of pos we can stop at: */ |
1212 | stop_pos = SvCUR(bigstr) - end_shift - (SvCUR(littlestr) - 1 - previous); | |
cf93c79d IZ |
1213 | if (previous + start_shift > stop_pos) { |
1214 | if (previous + start_shift == stop_pos + 1) /* A fake '\n'? */ | |
1215 | goto check_tail; | |
1216 | return Nullch; | |
1217 | } | |
c277df42 | 1218 | while (pos < previous + start_shift) { |
3280af22 | 1219 | if (!(pos += PL_screamnext[pos])) |
cf93c79d | 1220 | goto cant_find; |
378cc40b | 1221 | } |
de3bb511 | 1222 | #ifdef POINTERRIGOR |
bbce6d69 | 1223 | do { |
ef64f398 | 1224 | if (pos >= stop_pos) break; |
bbce6d69 | 1225 | if (big[pos-previous] != first) |
1226 | continue; | |
1227 | for (x=big+pos+1-previous,s=little; s < littleend; /**/ ) { | |
bbce6d69 | 1228 | if (*s++ != *x++) { |
1229 | s--; | |
1230 | break; | |
de3bb511 | 1231 | } |
bbce6d69 | 1232 | } |
c277df42 IZ |
1233 | if (s == littleend) { |
1234 | *old_posp = pos; | |
1235 | if (!last) return (char *)(big+pos-previous); | |
1236 | found = 1; | |
1237 | } | |
6b88bc9c | 1238 | } while ( pos += PL_screamnext[pos] ); |
c277df42 | 1239 | return (last && found) ? (char *)(big+(*old_posp)-previous) : Nullch; |
de3bb511 LW |
1240 | #else /* !POINTERRIGOR */ |
1241 | big -= previous; | |
bbce6d69 | 1242 | do { |
ef64f398 | 1243 | if (pos >= stop_pos) break; |
bbce6d69 | 1244 | if (big[pos] != first) |
1245 | continue; | |
1246 | for (x=big+pos+1,s=little; s < littleend; /**/ ) { | |
bbce6d69 | 1247 | if (*s++ != *x++) { |
1248 | s--; | |
1249 | break; | |
378cc40b | 1250 | } |
bbce6d69 | 1251 | } |
c277df42 IZ |
1252 | if (s == littleend) { |
1253 | *old_posp = pos; | |
1254 | if (!last) return (char *)(big+pos); | |
1255 | found = 1; | |
1256 | } | |
3280af22 | 1257 | } while ( pos += PL_screamnext[pos] ); |
cf93c79d IZ |
1258 | if (last && found) |
1259 | return (char *)(big+(*old_posp)); | |
de3bb511 | 1260 | #endif /* POINTERRIGOR */ |
cf93c79d IZ |
1261 | check_tail: |
1262 | if (!SvTAIL(littlestr) || (end_shift > 0)) | |
1263 | return Nullch; | |
1264 | /* Ignore the trailing "\n". This code is not microoptimized */ | |
1265 | big = (unsigned char *)(SvPVX(bigstr) + SvCUR(bigstr)); | |
1266 | stop_pos = littleend - little; /* Actual littlestr len */ | |
1267 | if (stop_pos == 0) | |
1268 | return (char*)big; | |
1269 | big -= stop_pos; | |
1270 | if (*big == first | |
1271 | && ((stop_pos == 1) || memEQ(big + 1, little, stop_pos - 1))) | |
1272 | return (char*)big; | |
1273 | return Nullch; | |
8d063cd8 LW |
1274 | } |
1275 | ||
79072805 | 1276 | I32 |
864dbfa3 | 1277 | Perl_ibcmp(pTHX_ const char *s1, const char *s2, register I32 len) |
79072805 | 1278 | { |
bbce6d69 | 1279 | register U8 *a = (U8 *)s1; |
1280 | register U8 *b = (U8 *)s2; | |
79072805 | 1281 | while (len--) { |
22c35a8c | 1282 | if (*a != *b && *a != PL_fold[*b]) |
bbce6d69 | 1283 | return 1; |
1284 | a++,b++; | |
1285 | } | |
1286 | return 0; | |
1287 | } | |
1288 | ||
1289 | I32 | |
864dbfa3 | 1290 | Perl_ibcmp_locale(pTHX_ const char *s1, const char *s2, register I32 len) |
bbce6d69 | 1291 | { |
1292 | register U8 *a = (U8 *)s1; | |
1293 | register U8 *b = (U8 *)s2; | |
1294 | while (len--) { | |
22c35a8c | 1295 | if (*a != *b && *a != PL_fold_locale[*b]) |
bbce6d69 | 1296 | return 1; |
1297 | a++,b++; | |
79072805 LW |
1298 | } |
1299 | return 0; | |
1300 | } | |
1301 | ||
8d063cd8 LW |
1302 | /* copy a string to a safe spot */ |
1303 | ||
1304 | char * | |
864dbfa3 | 1305 | Perl_savepv(pTHX_ const char *sv) |
8d063cd8 | 1306 | { |
a687059c | 1307 | register char *newaddr; |
8d063cd8 | 1308 | |
79072805 LW |
1309 | New(902,newaddr,strlen(sv)+1,char); |
1310 | (void)strcpy(newaddr,sv); | |
8d063cd8 LW |
1311 | return newaddr; |
1312 | } | |
1313 | ||
a687059c LW |
1314 | /* same thing but with a known length */ |
1315 | ||
1316 | char * | |
864dbfa3 | 1317 | Perl_savepvn(pTHX_ const char *sv, register I32 len) |
a687059c LW |
1318 | { |
1319 | register char *newaddr; | |
1320 | ||
1321 | New(903,newaddr,len+1,char); | |
79072805 | 1322 | Copy(sv,newaddr,len,char); /* might not be null terminated */ |
a687059c LW |
1323 | newaddr[len] = '\0'; /* is now */ |
1324 | return newaddr; | |
1325 | } | |
1326 | ||
cea2e8a9 | 1327 | /* the SV for Perl_form() and mess() is not kept in an arena */ |
fc36a67e | 1328 | |
76e3520e | 1329 | STATIC SV * |
cea2e8a9 | 1330 | S_mess_alloc(pTHX) |
fc36a67e | 1331 | { |
e72dc28c | 1332 | dTHR; |
fc36a67e | 1333 | SV *sv; |
1334 | XPVMG *any; | |
1335 | ||
e72dc28c GS |
1336 | if (!PL_dirty) |
1337 | return sv_2mortal(newSVpvn("",0)); | |
1338 | ||
0372dbb6 GS |
1339 | if (PL_mess_sv) |
1340 | return PL_mess_sv; | |
1341 | ||
fc36a67e | 1342 | /* Create as PVMG now, to avoid any upgrading later */ |
1343 | New(905, sv, 1, SV); | |
1344 | Newz(905, any, 1, XPVMG); | |
1345 | SvFLAGS(sv) = SVt_PVMG; | |
1346 | SvANY(sv) = (void*)any; | |
1347 | SvREFCNT(sv) = 1 << 30; /* practically infinite */ | |
e72dc28c | 1348 | PL_mess_sv = sv; |
fc36a67e | 1349 | return sv; |
1350 | } | |
1351 | ||
c5be433b | 1352 | #if defined(PERL_IMPLICIT_CONTEXT) |
cea2e8a9 GS |
1353 | char * |
1354 | Perl_form_nocontext(const char* pat, ...) | |
1355 | { | |
1356 | dTHX; | |
c5be433b | 1357 | char *retval; |
cea2e8a9 GS |
1358 | va_list args; |
1359 | va_start(args, pat); | |
c5be433b | 1360 | retval = vform(pat, &args); |
cea2e8a9 | 1361 | va_end(args); |
c5be433b | 1362 | return retval; |
cea2e8a9 | 1363 | } |
c5be433b | 1364 | #endif /* PERL_IMPLICIT_CONTEXT */ |
cea2e8a9 | 1365 | |
8990e307 | 1366 | char * |
864dbfa3 | 1367 | Perl_form(pTHX_ const char* pat, ...) |
8990e307 | 1368 | { |
c5be433b | 1369 | char *retval; |
46fc3d4c | 1370 | va_list args; |
46fc3d4c | 1371 | va_start(args, pat); |
c5be433b | 1372 | retval = vform(pat, &args); |
46fc3d4c | 1373 | va_end(args); |
c5be433b GS |
1374 | return retval; |
1375 | } | |
1376 | ||
1377 | char * | |
1378 | Perl_vform(pTHX_ const char *pat, va_list *args) | |
1379 | { | |
1380 | SV *sv = mess_alloc(); | |
1381 | sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*)); | |
e72dc28c | 1382 | return SvPVX(sv); |
46fc3d4c | 1383 | } |
a687059c | 1384 | |
5a844595 GS |
1385 | #if defined(PERL_IMPLICIT_CONTEXT) |
1386 | SV * | |
1387 | Perl_mess_nocontext(const char *pat, ...) | |
1388 | { | |
1389 | dTHX; | |
1390 | SV *retval; | |
1391 | va_list args; | |
1392 | va_start(args, pat); | |
1393 | retval = vmess(pat, &args); | |
1394 | va_end(args); | |
1395 | return retval; | |
1396 | } | |
1397 | #endif /* PERL_IMPLICIT_CONTEXT */ | |
1398 | ||
06bf62c7 | 1399 | SV * |
5a844595 GS |
1400 | Perl_mess(pTHX_ const char *pat, ...) |
1401 | { | |
1402 | SV *retval; | |
1403 | va_list args; | |
1404 | va_start(args, pat); | |
1405 | retval = vmess(pat, &args); | |
1406 | va_end(args); | |
1407 | return retval; | |
1408 | } | |
1409 | ||
1410 | SV * | |
1411 | Perl_vmess(pTHX_ const char *pat, va_list *args) | |
46fc3d4c | 1412 | { |
e72dc28c | 1413 | SV *sv = mess_alloc(); |
46fc3d4c | 1414 | static char dgd[] = " during global destruction.\n"; |
1415 | ||
fc36a67e | 1416 | sv_vsetpvfn(sv, pat, strlen(pat), args, Null(SV**), 0, Null(bool*)); |
46fc3d4c | 1417 | if (!SvCUR(sv) || *(SvEND(sv) - 1) != '\n') { |
e858de61 | 1418 | dTHR; |
b9c39e73 | 1419 | if (PL_curcop->cop_line) |
57def98f | 1420 | Perl_sv_catpvf(aTHX_ sv, " at %_ line %"IVdf, |
cf2093f6 | 1421 | GvSV(PL_curcop->cop_filegv), (IV)PL_curcop->cop_line); |
515f54a1 GS |
1422 | if (GvIO(PL_last_in_gv) && IoLINES(GvIOp(PL_last_in_gv))) { |
1423 | bool line_mode = (RsSIMPLE(PL_rs) && | |
7c1e0849 | 1424 | SvCUR(PL_rs) == 1 && *SvPVX(PL_rs) == '\n'); |
57def98f | 1425 | Perl_sv_catpvf(aTHX_ sv, ", <%s> %s %"IVdf, |
cf2093f6 JH |
1426 | PL_last_in_gv == PL_argvgv ? "" : GvNAME(PL_last_in_gv), |
1427 | line_mode ? "line" : "chunk", | |
1428 | (IV)IoLINES(GvIOp(PL_last_in_gv))); | |
a687059c | 1429 | } |
9efbc0eb | 1430 | #ifdef USE_THREADS |
e8e6f333 GS |
1431 | if (thr->tid) |
1432 | Perl_sv_catpvf(aTHX_ sv, " thread %ld", thr->tid); | |
9efbc0eb | 1433 | #endif |
515f54a1 | 1434 | sv_catpv(sv, PL_dirty ? dgd : ".\n"); |
a687059c | 1435 | } |
06bf62c7 | 1436 | return sv; |
a687059c LW |
1437 | } |
1438 | ||
c5be433b GS |
1439 | OP * |
1440 | Perl_vdie(pTHX_ const char* pat, va_list *args) | |
36477c24 | 1441 | { |
5dc0d613 | 1442 | dTHR; |
36477c24 | 1443 | char *message; |
3280af22 | 1444 | int was_in_eval = PL_in_eval; |
36477c24 | 1445 | HV *stash; |
1446 | GV *gv; | |
1447 | CV *cv; | |
06bf62c7 GS |
1448 | SV *msv; |
1449 | STRLEN msglen; | |
36477c24 | 1450 | |
bf49b057 | 1451 | DEBUG_S(PerlIO_printf(Perl_debug_log, |
199100c8 | 1452 | "%p: die: curstack = %p, mainstack = %p\n", |
533c011a | 1453 | thr, PL_curstack, PL_mainstack)); |
36477c24 | 1454 | |
06bf62c7 | 1455 | if (pat) { |
5a844595 GS |
1456 | msv = vmess(pat, args); |
1457 | if (PL_errors && SvCUR(PL_errors)) { | |
1458 | sv_catsv(PL_errors, msv); | |
1459 | message = SvPV(PL_errors, msglen); | |
1460 | SvCUR_set(PL_errors, 0); | |
1461 | } | |
1462 | else | |
1463 | message = SvPV(msv,msglen); | |
06bf62c7 GS |
1464 | } |
1465 | else { | |
1466 | message = Nullch; | |
1467 | } | |
36477c24 | 1468 | |
bf49b057 | 1469 | DEBUG_S(PerlIO_printf(Perl_debug_log, |
199100c8 | 1470 | "%p: die: message = %s\ndiehook = %p\n", |
533c011a | 1471 | thr, message, PL_diehook)); |
3280af22 | 1472 | if (PL_diehook) { |
cea2e8a9 | 1473 | /* sv_2cv might call Perl_croak() */ |
3280af22 | 1474 | SV *olddiehook = PL_diehook; |
1738f5c4 | 1475 | ENTER; |
3280af22 NIS |
1476 | SAVESPTR(PL_diehook); |
1477 | PL_diehook = Nullsv; | |
1738f5c4 CS |
1478 | cv = sv_2cv(olddiehook, &stash, &gv, 0); |
1479 | LEAVE; | |
1480 | if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) { | |
1481 | dSP; | |
774d564b | 1482 | SV *msg; |
1483 | ||
1484 | ENTER; | |
79cb57f6 | 1485 | if (message) { |
06bf62c7 | 1486 | msg = newSVpvn(message, msglen); |
4e6ea2c3 GS |
1487 | SvREADONLY_on(msg); |
1488 | SAVEFREESV(msg); | |
1489 | } | |
1490 | else { | |
1491 | msg = ERRSV; | |
1492 | } | |
1738f5c4 | 1493 | |
e788e7d3 | 1494 | PUSHSTACKi(PERLSI_DIEHOOK); |
924508f0 | 1495 | PUSHMARK(SP); |
1738f5c4 CS |
1496 | XPUSHs(msg); |
1497 | PUTBACK; | |
0cdb2077 | 1498 | call_sv((SV*)cv, G_DISCARD); |
d3acc0f7 | 1499 | POPSTACK; |
774d564b | 1500 | LEAVE; |
1738f5c4 | 1501 | } |
36477c24 | 1502 | } |
1503 | ||
06bf62c7 | 1504 | PL_restartop = die_where(message, msglen); |
bf49b057 | 1505 | DEBUG_S(PerlIO_printf(Perl_debug_log, |
7c06b590 | 1506 | "%p: die: restartop = %p, was_in_eval = %d, top_env = %p\n", |
533c011a | 1507 | thr, PL_restartop, was_in_eval, PL_top_env)); |
3280af22 | 1508 | if ((!PL_restartop && was_in_eval) || PL_top_env->je_prev) |
6224f72b | 1509 | JMPENV_JUMP(3); |
3280af22 | 1510 | return PL_restartop; |
36477c24 | 1511 | } |
1512 | ||
c5be433b | 1513 | #if defined(PERL_IMPLICIT_CONTEXT) |
cea2e8a9 GS |
1514 | OP * |
1515 | Perl_die_nocontext(const char* pat, ...) | |
a687059c | 1516 | { |
cea2e8a9 GS |
1517 | dTHX; |
1518 | OP *o; | |
a687059c | 1519 | va_list args; |
cea2e8a9 | 1520 | va_start(args, pat); |
c5be433b | 1521 | o = vdie(pat, &args); |
cea2e8a9 GS |
1522 | va_end(args); |
1523 | return o; | |
1524 | } | |
c5be433b | 1525 | #endif /* PERL_IMPLICIT_CONTEXT */ |
cea2e8a9 GS |
1526 | |
1527 | OP * | |
1528 | Perl_die(pTHX_ const char* pat, ...) | |
1529 | { | |
1530 | OP *o; | |
1531 | va_list args; | |
1532 | va_start(args, pat); | |
c5be433b | 1533 | o = vdie(pat, &args); |
cea2e8a9 GS |
1534 | va_end(args); |
1535 | return o; | |
1536 | } | |
1537 | ||
c5be433b GS |
1538 | void |
1539 | Perl_vcroak(pTHX_ const char* pat, va_list *args) | |
cea2e8a9 GS |
1540 | { |
1541 | dTHR; | |
de3bb511 | 1542 | char *message; |
748a9306 LW |
1543 | HV *stash; |
1544 | GV *gv; | |
1545 | CV *cv; | |
06bf62c7 GS |
1546 | SV *msv; |
1547 | STRLEN msglen; | |
a687059c | 1548 | |
5a844595 GS |
1549 | msv = vmess(pat, args); |
1550 | if (PL_errors && SvCUR(PL_errors)) { | |
1551 | sv_catsv(PL_errors, msv); | |
1552 | message = SvPV(PL_errors, msglen); | |
1553 | SvCUR_set(PL_errors, 0); | |
1554 | } | |
1555 | else | |
1556 | message = SvPV(msv,msglen); | |
1557 | ||
b900a521 JH |
1558 | DEBUG_S(PerlIO_printf(Perl_debug_log, "croak: 0x%"UVxf" %s", |
1559 | PTR2UV(thr), message)); | |
5a844595 | 1560 | |
3280af22 | 1561 | if (PL_diehook) { |
cea2e8a9 | 1562 | /* sv_2cv might call Perl_croak() */ |
3280af22 | 1563 | SV *olddiehook = PL_diehook; |
1738f5c4 | 1564 | ENTER; |
3280af22 NIS |
1565 | SAVESPTR(PL_diehook); |
1566 | PL_diehook = Nullsv; | |
20cec16a | 1567 | cv = sv_2cv(olddiehook, &stash, &gv, 0); |
1738f5c4 CS |
1568 | LEAVE; |
1569 | if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) { | |
20cec16a | 1570 | dSP; |
774d564b | 1571 | SV *msg; |
1572 | ||
1573 | ENTER; | |
06bf62c7 | 1574 | msg = newSVpvn(message, msglen); |
774d564b | 1575 | SvREADONLY_on(msg); |
1576 | SAVEFREESV(msg); | |
20cec16a | 1577 | |
e788e7d3 | 1578 | PUSHSTACKi(PERLSI_DIEHOOK); |
924508f0 | 1579 | PUSHMARK(SP); |
1738f5c4 | 1580 | XPUSHs(msg); |
20cec16a | 1581 | PUTBACK; |
864dbfa3 | 1582 | call_sv((SV*)cv, G_DISCARD); |
d3acc0f7 | 1583 | POPSTACK; |
774d564b | 1584 | LEAVE; |
20cec16a | 1585 | } |
748a9306 | 1586 | } |
3280af22 | 1587 | if (PL_in_eval) { |
06bf62c7 | 1588 | PL_restartop = die_where(message, msglen); |
6224f72b | 1589 | JMPENV_JUMP(3); |
a0d0e21e | 1590 | } |
d175a3f0 GS |
1591 | { |
1592 | #ifdef USE_SFIO | |
1593 | /* SFIO can really mess with your errno */ | |
1594 | int e = errno; | |
1595 | #endif | |
bf49b057 GS |
1596 | PerlIO *serr = Perl_error_log; |
1597 | ||
1598 | PerlIO_write(serr, message, msglen); | |
1599 | (void)PerlIO_flush(serr); | |
d175a3f0 GS |
1600 | #ifdef USE_SFIO |
1601 | errno = e; | |
1602 | #endif | |
1603 | } | |
f86702cc | 1604 | my_failure_exit(); |
a687059c LW |
1605 | } |
1606 | ||
c5be433b | 1607 | #if defined(PERL_IMPLICIT_CONTEXT) |
8990e307 | 1608 | void |
cea2e8a9 | 1609 | Perl_croak_nocontext(const char *pat, ...) |
a687059c | 1610 | { |
cea2e8a9 | 1611 | dTHX; |
a687059c | 1612 | va_list args; |
cea2e8a9 | 1613 | va_start(args, pat); |
c5be433b | 1614 | vcroak(pat, &args); |
cea2e8a9 GS |
1615 | /* NOTREACHED */ |
1616 | va_end(args); | |
1617 | } | |
1618 | #endif /* PERL_IMPLICIT_CONTEXT */ | |
1619 | ||
1620 | void | |
1621 | Perl_croak(pTHX_ const char *pat, ...) | |
1622 | { | |
1623 | va_list args; | |
1624 | va_start(args, pat); | |
c5be433b | 1625 | vcroak(pat, &args); |
cea2e8a9 GS |
1626 | /* NOTREACHED */ |
1627 | va_end(args); | |
1628 | } | |
1629 | ||
c5be433b GS |
1630 | void |
1631 | Perl_vwarn(pTHX_ const char* pat, va_list *args) | |
cea2e8a9 | 1632 | { |
de3bb511 | 1633 | char *message; |
748a9306 LW |
1634 | HV *stash; |
1635 | GV *gv; | |
1636 | CV *cv; | |
06bf62c7 GS |
1637 | SV *msv; |
1638 | STRLEN msglen; | |
a687059c | 1639 | |
5a844595 | 1640 | msv = vmess(pat, args); |
06bf62c7 | 1641 | message = SvPV(msv, msglen); |
a687059c | 1642 | |
3280af22 | 1643 | if (PL_warnhook) { |
cea2e8a9 | 1644 | /* sv_2cv might call Perl_warn() */ |
11343788 | 1645 | dTHR; |
3280af22 | 1646 | SV *oldwarnhook = PL_warnhook; |
1738f5c4 | 1647 | ENTER; |
3280af22 NIS |
1648 | SAVESPTR(PL_warnhook); |
1649 | PL_warnhook = Nullsv; | |
20cec16a | 1650 | cv = sv_2cv(oldwarnhook, &stash, &gv, 0); |
1738f5c4 CS |
1651 | LEAVE; |
1652 | if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) { | |
20cec16a | 1653 | dSP; |
774d564b | 1654 | SV *msg; |
1655 | ||
1656 | ENTER; | |
06bf62c7 | 1657 | msg = newSVpvn(message, msglen); |
774d564b | 1658 | SvREADONLY_on(msg); |
1659 | SAVEFREESV(msg); | |
1660 | ||
e788e7d3 | 1661 | PUSHSTACKi(PERLSI_WARNHOOK); |
924508f0 | 1662 | PUSHMARK(SP); |
774d564b | 1663 | XPUSHs(msg); |
20cec16a | 1664 | PUTBACK; |
864dbfa3 | 1665 | call_sv((SV*)cv, G_DISCARD); |
d3acc0f7 | 1666 | POPSTACK; |
774d564b | 1667 | LEAVE; |
20cec16a | 1668 | return; |
1669 | } | |
748a9306 | 1670 | } |
bf49b057 GS |
1671 | { |
1672 | PerlIO *serr = Perl_error_log; | |
1673 | ||
1674 | PerlIO_write(serr, message, msglen); | |
a687059c | 1675 | #ifdef LEAKTEST |
bf49b057 GS |
1676 | DEBUG_L(*message == '!' |
1677 | ? (xstat(message[1]=='!' | |
1678 | ? (message[2]=='!' ? 2 : 1) | |
1679 | : 0) | |
1680 | , 0) | |
1681 | : 0); | |
a687059c | 1682 | #endif |
bf49b057 GS |
1683 | (void)PerlIO_flush(serr); |
1684 | } | |
a687059c | 1685 | } |
8d063cd8 | 1686 | |
c5be433b | 1687 | #if defined(PERL_IMPLICIT_CONTEXT) |
cea2e8a9 GS |
1688 | void |
1689 | Perl_warn_nocontext(const char *pat, ...) | |
1690 | { | |
1691 | dTHX; | |
1692 | va_list args; | |
1693 | va_start(args, pat); | |
c5be433b | 1694 | vwarn(pat, &args); |
cea2e8a9 GS |
1695 | va_end(args); |
1696 | } | |
1697 | #endif /* PERL_IMPLICIT_CONTEXT */ | |
1698 | ||
1699 | void | |
1700 | Perl_warn(pTHX_ const char *pat, ...) | |
1701 | { | |
1702 | va_list args; | |
1703 | va_start(args, pat); | |
c5be433b | 1704 | vwarn(pat, &args); |
cea2e8a9 GS |
1705 | va_end(args); |
1706 | } | |
1707 | ||
c5be433b GS |
1708 | #if defined(PERL_IMPLICIT_CONTEXT) |
1709 | void | |
1710 | Perl_warner_nocontext(U32 err, const char *pat, ...) | |
1711 | { | |
1712 | dTHX; | |
1713 | va_list args; | |
1714 | va_start(args, pat); | |
1715 | vwarner(err, pat, &args); | |
1716 | va_end(args); | |
1717 | } | |
1718 | #endif /* PERL_IMPLICIT_CONTEXT */ | |
1719 | ||
599cee73 | 1720 | void |
864dbfa3 | 1721 | Perl_warner(pTHX_ U32 err, const char* pat,...) |
599cee73 PM |
1722 | { |
1723 | va_list args; | |
c5be433b GS |
1724 | va_start(args, pat); |
1725 | vwarner(err, pat, &args); | |
1726 | va_end(args); | |
1727 | } | |
1728 | ||
1729 | void | |
1730 | Perl_vwarner(pTHX_ U32 err, const char* pat, va_list* args) | |
1731 | { | |
1732 | dTHR; | |
599cee73 PM |
1733 | char *message; |
1734 | HV *stash; | |
1735 | GV *gv; | |
1736 | CV *cv; | |
06bf62c7 GS |
1737 | SV *msv; |
1738 | STRLEN msglen; | |
599cee73 | 1739 | |
5a844595 | 1740 | msv = vmess(pat, args); |
06bf62c7 | 1741 | message = SvPV(msv, msglen); |
599cee73 PM |
1742 | |
1743 | if (ckDEAD(err)) { | |
1744 | #ifdef USE_THREADS | |
b900a521 | 1745 | DEBUG_S(PerlIO_printf(Perl_debug_log, "croak: 0x%"UVxf" %s", PTR2UV(thr), message)); |
599cee73 PM |
1746 | #endif /* USE_THREADS */ |
1747 | if (PL_diehook) { | |
cea2e8a9 | 1748 | /* sv_2cv might call Perl_croak() */ |
599cee73 PM |
1749 | SV *olddiehook = PL_diehook; |
1750 | ENTER; | |
1751 | SAVESPTR(PL_diehook); | |
1752 | PL_diehook = Nullsv; | |
1753 | cv = sv_2cv(olddiehook, &stash, &gv, 0); | |
1754 | LEAVE; | |
1755 | if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) { | |
1756 | dSP; | |
1757 | SV *msg; | |
1758 | ||
1759 | ENTER; | |
06bf62c7 | 1760 | msg = newSVpvn(message, msglen); |
599cee73 PM |
1761 | SvREADONLY_on(msg); |
1762 | SAVEFREESV(msg); | |
1763 | ||
1764 | PUSHMARK(sp); | |
1765 | XPUSHs(msg); | |
1766 | PUTBACK; | |
864dbfa3 | 1767 | call_sv((SV*)cv, G_DISCARD); |
599cee73 PM |
1768 | |
1769 | LEAVE; | |
1770 | } | |
1771 | } | |
1772 | if (PL_in_eval) { | |
06bf62c7 | 1773 | PL_restartop = die_where(message, msglen); |
599cee73 PM |
1774 | JMPENV_JUMP(3); |
1775 | } | |
bf49b057 GS |
1776 | { |
1777 | PerlIO *serr = Perl_error_log; | |
1778 | PerlIO_write(serr, message, msglen); | |
1779 | (void)PerlIO_flush(serr); | |
1780 | } | |
599cee73 PM |
1781 | my_failure_exit(); |
1782 | ||
1783 | } | |
1784 | else { | |
1785 | if (PL_warnhook) { | |
cea2e8a9 | 1786 | /* sv_2cv might call Perl_warn() */ |
599cee73 PM |
1787 | dTHR; |
1788 | SV *oldwarnhook = PL_warnhook; | |
1789 | ENTER; | |
1790 | SAVESPTR(PL_warnhook); | |
1791 | PL_warnhook = Nullsv; | |
1792 | cv = sv_2cv(oldwarnhook, &stash, &gv, 0); | |
1793 | LEAVE; | |
1794 | if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) { | |
1795 | dSP; | |
1796 | SV *msg; | |
1797 | ||
1798 | ENTER; | |
06bf62c7 | 1799 | msg = newSVpvn(message, msglen); |
599cee73 PM |
1800 | SvREADONLY_on(msg); |
1801 | SAVEFREESV(msg); | |
1802 | ||
1803 | PUSHMARK(sp); | |
1804 | XPUSHs(msg); | |
1805 | PUTBACK; | |
864dbfa3 | 1806 | call_sv((SV*)cv, G_DISCARD); |
599cee73 PM |
1807 | |
1808 | LEAVE; | |
1809 | return; | |
1810 | } | |
1811 | } | |
bf49b057 GS |
1812 | { |
1813 | PerlIO *serr = Perl_error_log; | |
1814 | PerlIO_write(serr, message, msglen); | |
599cee73 | 1815 | #ifdef LEAKTEST |
bf49b057 | 1816 | DEBUG_L(xstat()); |
599cee73 | 1817 | #endif |
bf49b057 GS |
1818 | (void)PerlIO_flush(serr); |
1819 | } | |
599cee73 PM |
1820 | } |
1821 | } | |
1822 | ||
a0d0e21e | 1823 | #ifndef VMS /* VMS' my_setenv() is in VMS.c */ |
2c2d71f5 | 1824 | #if !defined(WIN32) && !defined(CYGWIN) |
8d063cd8 | 1825 | void |
864dbfa3 | 1826 | Perl_my_setenv(pTHX_ char *nam, char *val) |
8d063cd8 | 1827 | { |
f2517201 GS |
1828 | #ifndef PERL_USE_SAFE_PUTENV |
1829 | /* most putenv()s leak, so we manipulate environ directly */ | |
79072805 | 1830 | register I32 i=setenv_getix(nam); /* where does it go? */ |
8d063cd8 | 1831 | |
3280af22 | 1832 | if (environ == PL_origenviron) { /* need we copy environment? */ |
79072805 LW |
1833 | I32 j; |
1834 | I32 max; | |
fe14fcc3 LW |
1835 | char **tmpenv; |
1836 | ||
de3bb511 | 1837 | /*SUPPRESS 530*/ |
fe14fcc3 | 1838 | for (max = i; environ[max]; max++) ; |
f2517201 GS |
1839 | tmpenv = (char**)safesysmalloc((max+2) * sizeof(char*)); |
1840 | for (j=0; j<max; j++) { /* copy environment */ | |
1841 | tmpenv[j] = (char*)safesysmalloc((strlen(environ[j])+1)*sizeof(char)); | |
1842 | strcpy(tmpenv[j], environ[j]); | |
1843 | } | |
fe14fcc3 LW |
1844 | tmpenv[max] = Nullch; |
1845 | environ = tmpenv; /* tell exec where it is now */ | |
1846 | } | |
a687059c | 1847 | if (!val) { |
f2517201 | 1848 | safesysfree(environ[i]); |
a687059c LW |
1849 | while (environ[i]) { |
1850 | environ[i] = environ[i+1]; | |
1851 | i++; | |
1852 | } | |
1853 | return; | |
1854 | } | |
8d063cd8 | 1855 | if (!environ[i]) { /* does not exist yet */ |
f2517201 | 1856 | environ = (char**)safesysrealloc(environ, (i+2) * sizeof(char*)); |
8d063cd8 LW |
1857 | environ[i+1] = Nullch; /* make sure it's null terminated */ |
1858 | } | |
fe14fcc3 | 1859 | else |
f2517201 GS |
1860 | safesysfree(environ[i]); |
1861 | environ[i] = (char*)safesysmalloc((strlen(nam)+strlen(val)+2) * sizeof(char)); | |
1862 | ||
a687059c | 1863 | (void)sprintf(environ[i],"%s=%s",nam,val);/* all that work just for this */ |
f2517201 GS |
1864 | |
1865 | #else /* PERL_USE_SAFE_PUTENV */ | |
1866 | char *new_env; | |
1867 | ||
1868 | new_env = (char*)safesysmalloc((strlen(nam) + strlen(val) + 2) * sizeof(char)); | |
f2517201 | 1869 | (void)sprintf(new_env,"%s=%s",nam,val);/* all that work just for this */ |
f2517201 GS |
1870 | (void)putenv(new_env); |
1871 | #endif /* PERL_USE_SAFE_PUTENV */ | |
8d063cd8 LW |
1872 | } |
1873 | ||
2c2d71f5 JH |
1874 | #else /* WIN32 || CYGWIN */ |
1875 | #if defined(CYGWIN) | |
1cab015a EF |
1876 | /* |
1877 | * Save environ of perl.exe, currently Cygwin links in separate environ's | |
1878 | * for each exe/dll. Probably should be a member of impure_ptr. | |
1879 | */ | |
1880 | static char ***Perl_main_environ; | |
1881 | ||
1882 | EXTERN_C void | |
1883 | Perl_my_setenv_init(char ***penviron) | |
1884 | { | |
1885 | Perl_main_environ = penviron; | |
1886 | } | |
1887 | ||
1888 | void | |
1889 | my_setenv(char *nam, char *val) | |
1890 | { | |
1891 | /* You can not directly manipulate the environ[] array because | |
1892 | * the routines do some additional work that syncs the Cygwin | |
1893 | * environment with the Windows environment. | |
1894 | */ | |
1895 | char *oldstr = environ[setenv_getix(nam)]; | |
1896 | ||
1897 | if (!val) { | |
1898 | if (!oldstr) | |
1899 | return; | |
1900 | unsetenv(nam); | |
1901 | Safefree(oldstr); | |
1902 | return; | |
1903 | } | |
1904 | setenv(nam, val, 1); | |
1905 | environ = *Perl_main_environ; /* environ realloc can occur in setenv */ | |
1906 | if(oldstr && environ[setenv_getix(nam)] != oldstr) | |
1907 | Safefree(oldstr); | |
1908 | } | |
3e3baf6d | 1909 | #else /* if WIN32 */ |
68dc0745 | 1910 | |
1911 | void | |
864dbfa3 | 1912 | Perl_my_setenv(pTHX_ char *nam,char *val) |
68dc0745 | 1913 | { |
3e3baf6d TB |
1914 | |
1915 | #ifdef USE_WIN32_RTL_ENV | |
1916 | ||
68dc0745 | 1917 | register char *envstr; |
1918 | STRLEN namlen = strlen(nam); | |
3e3baf6d TB |
1919 | STRLEN vallen; |
1920 | char *oldstr = environ[setenv_getix(nam)]; | |
1921 | ||
1922 | /* putenv() has totally broken semantics in both the Borland | |
1923 | * and Microsoft CRTLs. They either store the passed pointer in | |
1924 | * the environment without making a copy, or make a copy and don't | |
1925 | * free it. And on top of that, they dont free() old entries that | |
1926 | * are being replaced/deleted. This means the caller must | |
1927 | * free any old entries somehow, or we end up with a memory | |
1928 | * leak every time my_setenv() is called. One might think | |
1929 | * one could directly manipulate environ[], like the UNIX code | |
1930 | * above, but direct changes to environ are not allowed when | |
1931 | * calling putenv(), since the RTLs maintain an internal | |
1932 | * *copy* of environ[]. Bad, bad, *bad* stink. | |
1933 | * GSAR 97-06-07 | |
1934 | */ | |
68dc0745 | 1935 | |
3e3baf6d TB |
1936 | if (!val) { |
1937 | if (!oldstr) | |
1938 | return; | |
1939 | val = ""; | |
1940 | vallen = 0; | |
1941 | } | |
1942 | else | |
1943 | vallen = strlen(val); | |
f2517201 | 1944 | envstr = (char*)safesysmalloc((namlen + vallen + 3) * sizeof(char)); |
68dc0745 | 1945 | (void)sprintf(envstr,"%s=%s",nam,val); |
76e3520e | 1946 | (void)PerlEnv_putenv(envstr); |
3e3baf6d | 1947 | if (oldstr) |
f2517201 | 1948 | safesysfree(oldstr); |
3e3baf6d | 1949 | #ifdef _MSC_VER |
f2517201 | 1950 | safesysfree(envstr); /* MSVCRT leaks without this */ |
3e3baf6d TB |
1951 | #endif |
1952 | ||
1953 | #else /* !USE_WIN32_RTL_ENV */ | |
1954 | ||
ac5c734f GS |
1955 | register char *envstr; |
1956 | STRLEN len = strlen(nam) + 3; | |
1957 | if (!val) { | |
1958 | val = ""; | |
1959 | } | |
1960 | len += strlen(val); | |
1961 | New(904, envstr, len, char); | |
1962 | (void)sprintf(envstr,"%s=%s",nam,val); | |
1963 | (void)PerlEnv_putenv(envstr); | |
1964 | Safefree(envstr); | |
3e3baf6d TB |
1965 | |
1966 | #endif | |
1967 | } | |
1968 | ||
1969 | #endif /* WIN32 */ | |
1cab015a | 1970 | #endif |
3e3baf6d TB |
1971 | |
1972 | I32 | |
864dbfa3 | 1973 | Perl_setenv_getix(pTHX_ char *nam) |
3e3baf6d TB |
1974 | { |
1975 | register I32 i, len = strlen(nam); | |
1976 | ||
1977 | for (i = 0; environ[i]; i++) { | |
1978 | if ( | |
1979 | #ifdef WIN32 | |
1980 | strnicmp(environ[i],nam,len) == 0 | |
1981 | #else | |
1982 | strnEQ(environ[i],nam,len) | |
1983 | #endif | |
1984 | && environ[i][len] == '=') | |
1985 | break; /* strnEQ must come first to avoid */ | |
1986 | } /* potential SEGV's */ | |
1987 | return i; | |
68dc0745 | 1988 | } |
1989 | ||
a0d0e21e | 1990 | #endif /* !VMS */ |
378cc40b | 1991 | |
16d20bd9 | 1992 | #ifdef UNLINK_ALL_VERSIONS |
79072805 | 1993 | I32 |
864dbfa3 | 1994 | Perl_unlnk(pTHX_ char *f) /* unlink all versions of a file */ |
378cc40b | 1995 | { |
79072805 | 1996 | I32 i; |
378cc40b | 1997 | |
6ad3d225 | 1998 | for (i = 0; PerlLIO_unlink(f) >= 0; i++) ; |
378cc40b LW |
1999 | return i ? 0 : -1; |
2000 | } | |
2001 | #endif | |
2002 | ||
85e6fe83 | 2003 | #if !defined(HAS_BCOPY) || !defined(HAS_SAFE_BCOPY) |
378cc40b | 2004 | char * |
864dbfa3 | 2005 | Perl_my_bcopy(pTHX_ register const char *from,register char *to,register I32 len) |
378cc40b LW |
2006 | { |
2007 | char *retval = to; | |
2008 | ||
7c0587c8 LW |
2009 | if (from - to >= 0) { |
2010 | while (len--) | |
2011 | *to++ = *from++; | |
2012 | } | |
2013 | else { | |
2014 | to += len; | |
2015 | from += len; | |
2016 | while (len--) | |
faf8582f | 2017 | *(--to) = *(--from); |
7c0587c8 | 2018 | } |
378cc40b LW |
2019 | return retval; |
2020 | } | |
ffed7fef | 2021 | #endif |
378cc40b | 2022 | |
fc36a67e | 2023 | #ifndef HAS_MEMSET |
2024 | void * | |
864dbfa3 | 2025 | Perl_my_memset(pTHX_ register char *loc, register I32 ch, register I32 len) |
fc36a67e | 2026 | { |
2027 | char *retval = loc; | |
2028 | ||
2029 | while (len--) | |
2030 | *loc++ = ch; | |
2031 | return retval; | |
2032 | } | |
2033 | #endif | |
2034 | ||
7c0587c8 | 2035 | #if !defined(HAS_BZERO) && !defined(HAS_MEMSET) |
378cc40b | 2036 | char * |
864dbfa3 | 2037 | Perl_my_bzero(pTHX_ register char *loc, register I32 len) |
378cc40b LW |
2038 | { |
2039 | char *retval = loc; | |
2040 | ||
2041 | while (len--) | |
2042 | *loc++ = 0; | |
2043 | return retval; | |
2044 | } | |
2045 | #endif | |
7c0587c8 | 2046 | |
36477c24 | 2047 | #if !defined(HAS_MEMCMP) || !defined(HAS_SANE_MEMCMP) |
79072805 | 2048 | I32 |
864dbfa3 | 2049 | Perl_my_memcmp(pTHX_ const char *s1, const char *s2, register I32 len) |
7c0587c8 | 2050 | { |
36477c24 | 2051 | register U8 *a = (U8 *)s1; |
2052 | register U8 *b = (U8 *)s2; | |
79072805 | 2053 | register I32 tmp; |
7c0587c8 LW |
2054 | |
2055 | while (len--) { | |
36477c24 | 2056 | if (tmp = *a++ - *b++) |
7c0587c8 LW |
2057 | return tmp; |
2058 | } | |
2059 | return 0; | |
2060 | } | |
36477c24 | 2061 | #endif /* !HAS_MEMCMP || !HAS_SANE_MEMCMP */ |
a687059c | 2062 | |
fe14fcc3 | 2063 | #ifndef HAS_VPRINTF |
a687059c | 2064 | |
85e6fe83 | 2065 | #ifdef USE_CHAR_VSPRINTF |
a687059c LW |
2066 | char * |
2067 | #else | |
2068 | int | |
2069 | #endif | |
08105a92 | 2070 | vsprintf(char *dest, const char *pat, char *args) |
a687059c LW |
2071 | { |
2072 | FILE fakebuf; | |
2073 | ||
2074 | fakebuf._ptr = dest; | |
2075 | fakebuf._cnt = 32767; | |
35c8bce7 LW |
2076 | #ifndef _IOSTRG |
2077 | #define _IOSTRG 0 | |
2078 | #endif | |
a687059c LW |
2079 | fakebuf._flag = _IOWRT|_IOSTRG; |
2080 | _doprnt(pat, args, &fakebuf); /* what a kludge */ | |
2081 | (void)putc('\0', &fakebuf); | |
85e6fe83 | 2082 | #ifdef USE_CHAR_VSPRINTF |
a687059c LW |
2083 | return(dest); |
2084 | #else | |
2085 | return 0; /* perl doesn't use return value */ | |
2086 | #endif | |
2087 | } | |
2088 | ||
fe14fcc3 | 2089 | #endif /* HAS_VPRINTF */ |
a687059c LW |
2090 | |
2091 | #ifdef MYSWAP | |
ffed7fef | 2092 | #if BYTEORDER != 0x4321 |
a687059c | 2093 | short |
864dbfa3 | 2094 | Perl_my_swap(pTHX_ short s) |
a687059c LW |
2095 | { |
2096 | #if (BYTEORDER & 1) == 0 | |
2097 | short result; | |
2098 | ||
2099 | result = ((s & 255) << 8) + ((s >> 8) & 255); | |
2100 | return result; | |
2101 | #else | |
2102 | return s; | |
2103 | #endif | |
2104 | } | |
2105 | ||
2106 | long | |
864dbfa3 | 2107 | Perl_my_htonl(pTHX_ long l) |
a687059c LW |
2108 | { |
2109 | union { | |
2110 | long result; | |
ffed7fef | 2111 | char c[sizeof(long)]; |
a687059c LW |
2112 | } u; |
2113 | ||
ffed7fef | 2114 | #if BYTEORDER == 0x1234 |
a687059c LW |
2115 | u.c[0] = (l >> 24) & 255; |
2116 | u.c[1] = (l >> 16) & 255; | |
2117 | u.c[2] = (l >> 8) & 255; | |
2118 | u.c[3] = l & 255; | |
2119 | return u.result; | |
2120 | #else | |
ffed7fef | 2121 | #if ((BYTEORDER - 0x1111) & 0x444) || !(BYTEORDER & 0xf) |
cea2e8a9 | 2122 | Perl_croak(aTHX_ "Unknown BYTEORDER\n"); |
a687059c | 2123 | #else |
79072805 LW |
2124 | register I32 o; |
2125 | register I32 s; | |
a687059c | 2126 | |
ffed7fef LW |
2127 | for (o = BYTEORDER - 0x1111, s = 0; s < (sizeof(long)*8); o >>= 4, s += 8) { |
2128 | u.c[o & 0xf] = (l >> s) & 255; | |
a687059c LW |
2129 | } |
2130 | return u.result; | |
2131 | #endif | |
2132 | #endif | |
2133 | } | |
2134 | ||
2135 | long | |
864dbfa3 | 2136 | Perl_my_ntohl(pTHX_ long l) |
a687059c LW |
2137 | { |
2138 | union { | |
2139 | long l; | |
ffed7fef | 2140 | char c[sizeof(long)]; |
a687059c LW |
2141 | } u; |
2142 | ||
ffed7fef | 2143 | #if BYTEORDER == 0x1234 |
a687059c LW |
2144 | u.c[0] = (l >> 24) & 255; |
2145 | u.c[1] = (l >> 16) & 255; | |
2146 | u.c[2] = (l >> 8) & 255; | |
2147 | u.c[3] = l & 255; | |
2148 | return u.l; | |
2149 | #else | |
ffed7fef | 2150 | #if ((BYTEORDER - 0x1111) & 0x444) || !(BYTEORDER & 0xf) |
cea2e8a9 | 2151 | Perl_croak(aTHX_ "Unknown BYTEORDER\n"); |
a687059c | 2152 | #else |
79072805 LW |
2153 | register I32 o; |
2154 | register I32 s; | |
a687059c LW |
2155 | |
2156 | u.l = l; | |
2157 | l = 0; | |
ffed7fef LW |
2158 | for (o = BYTEORDER - 0x1111, s = 0; s < (sizeof(long)*8); o >>= 4, s += 8) { |
2159 | l |= (u.c[o & 0xf] & 255) << s; | |
a687059c LW |
2160 | } |
2161 | return l; | |
2162 | #endif | |
2163 | #endif | |
2164 | } | |
2165 | ||
ffed7fef | 2166 | #endif /* BYTEORDER != 0x4321 */ |
988174c1 LW |
2167 | #endif /* MYSWAP */ |
2168 | ||
2169 | /* | |
2170 | * Little-endian byte order functions - 'v' for 'VAX', or 'reVerse'. | |
2171 | * If these functions are defined, | |
2172 | * the BYTEORDER is neither 0x1234 nor 0x4321. | |
2173 | * However, this is not assumed. | |
2174 | * -DWS | |
2175 | */ | |
2176 | ||
2177 | #define HTOV(name,type) \ | |
2178 | type \ | |
ba106d47 | 2179 | name (register type n) \ |
988174c1 LW |
2180 | { \ |
2181 | union { \ | |
2182 | type value; \ | |
2183 | char c[sizeof(type)]; \ | |
2184 | } u; \ | |
79072805 LW |
2185 | register I32 i; \ |
2186 | register I32 s; \ | |
988174c1 LW |
2187 | for (i = 0, s = 0; i < sizeof(u.c); i++, s += 8) { \ |
2188 | u.c[i] = (n >> s) & 0xFF; \ | |
2189 | } \ | |
2190 | return u.value; \ | |
2191 | } | |
2192 | ||
2193 | #define VTOH(name,type) \ | |
2194 | type \ | |
ba106d47 | 2195 | name (register type n) \ |
988174c1 LW |
2196 | { \ |
2197 | union { \ | |
2198 | type value; \ | |
2199 | char c[sizeof(type)]; \ | |
2200 | } u; \ | |
79072805 LW |
2201 | register I32 i; \ |
2202 | register I32 s; \ | |
988174c1 LW |
2203 | u.value = n; \ |
2204 | n = 0; \ | |
2205 | for (i = 0, s = 0; i < sizeof(u.c); i++, s += 8) { \ | |
2206 | n += (u.c[i] & 0xFF) << s; \ | |
2207 | } \ | |
2208 | return n; \ | |
2209 | } | |
2210 | ||
2211 | #if defined(HAS_HTOVS) && !defined(htovs) | |
2212 | HTOV(htovs,short) | |
2213 | #endif | |
2214 | #if defined(HAS_HTOVL) && !defined(htovl) | |
2215 | HTOV(htovl,long) | |
2216 | #endif | |
2217 | #if defined(HAS_VTOHS) && !defined(vtohs) | |
2218 | VTOH(vtohs,short) | |
2219 | #endif | |
2220 | #if defined(HAS_VTOHL) && !defined(vtohl) | |
2221 | VTOH(vtohl,long) | |
2222 | #endif | |
a687059c | 2223 | |
5f05dabc | 2224 | /* VMS' my_popen() is in VMS.c, same with OS/2. */ |
4d2c4e07 | 2225 | #if (!defined(DOSISH) || defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS) && !defined(__OPEN_VM) && !defined(EPOC) |
760ac839 | 2226 | PerlIO * |
864dbfa3 | 2227 | Perl_my_popen(pTHX_ char *cmd, char *mode) |
a687059c LW |
2228 | { |
2229 | int p[2]; | |
8ac85365 | 2230 | register I32 This, that; |
d8a83dd3 | 2231 | register Pid_t pid; |
79072805 | 2232 | SV *sv; |
1738f5c4 | 2233 | I32 doexec = strNE(cmd,"-"); |
e446cec8 IZ |
2234 | I32 did_pipes = 0; |
2235 | int pp[2]; | |
a687059c | 2236 | |
45bc9206 | 2237 | PERL_FLUSHALL_FOR_CHILD; |
ddcf38b7 IZ |
2238 | #ifdef OS2 |
2239 | if (doexec) { | |
2240 | return my_syspopen(cmd,mode); | |
2241 | } | |
2242 | #endif | |
8ac85365 NIS |
2243 | This = (*mode == 'w'); |
2244 | that = !This; | |
3280af22 | 2245 | if (doexec && PL_tainting) { |
bbce6d69 | 2246 | taint_env(); |
2247 | taint_proper("Insecure %s%s", "EXEC"); | |
d48672a2 | 2248 | } |
c2267164 IZ |
2249 | if (PerlProc_pipe(p) < 0) |
2250 | return Nullfp; | |
e446cec8 IZ |
2251 | if (doexec && PerlProc_pipe(pp) >= 0) |
2252 | did_pipes = 1; | |
a687059c LW |
2253 | while ((pid = (doexec?vfork():fork())) < 0) { |
2254 | if (errno != EAGAIN) { | |
6ad3d225 | 2255 | PerlLIO_close(p[This]); |
e446cec8 IZ |
2256 | if (did_pipes) { |
2257 | PerlLIO_close(pp[0]); | |
2258 | PerlLIO_close(pp[1]); | |
2259 | } | |
a687059c | 2260 | if (!doexec) |
cea2e8a9 | 2261 | Perl_croak(aTHX_ "Can't fork"); |
a687059c LW |
2262 | return Nullfp; |
2263 | } | |
2264 | sleep(5); | |
2265 | } | |
2266 | if (pid == 0) { | |
79072805 LW |
2267 | GV* tmpgv; |
2268 | ||
30ac6d9b GS |
2269 | #undef THIS |
2270 | #undef THAT | |
a687059c | 2271 | #define THIS that |
8ac85365 | 2272 | #define THAT This |
6ad3d225 | 2273 | PerlLIO_close(p[THAT]); |
e446cec8 IZ |
2274 | if (did_pipes) { |
2275 | PerlLIO_close(pp[0]); | |
2276 | #if defined(HAS_FCNTL) && defined(F_SETFD) | |
2277 | fcntl(pp[1], F_SETFD, FD_CLOEXEC); | |
2278 | #endif | |
2279 | } | |
a687059c | 2280 | if (p[THIS] != (*mode == 'r')) { |
6ad3d225 GS |
2281 | PerlLIO_dup2(p[THIS], *mode == 'r'); |
2282 | PerlLIO_close(p[THIS]); | |
a687059c | 2283 | } |
4435c477 | 2284 | #ifndef OS2 |
a687059c | 2285 | if (doexec) { |
a0d0e21e | 2286 | #if !defined(HAS_FCNTL) || !defined(F_SETFD) |
ae986130 LW |
2287 | int fd; |
2288 | ||
2289 | #ifndef NOFILE | |
2290 | #define NOFILE 20 | |
2291 | #endif | |
6b88bc9c | 2292 | for (fd = PL_maxsysfd + 1; fd < NOFILE; fd++) |
e446cec8 IZ |
2293 | if (fd != pp[1]) |
2294 | PerlLIO_close(fd); | |
ae986130 | 2295 | #endif |
e446cec8 | 2296 | do_exec3(cmd,pp[1],did_pipes); /* may or may not use the shell */ |
6ad3d225 | 2297 | PerlProc__exit(1); |
a687059c | 2298 | } |
4435c477 | 2299 | #endif /* defined OS2 */ |
de3bb511 | 2300 | /*SUPPRESS 560*/ |
85e6fe83 | 2301 | if (tmpgv = gv_fetchpv("$",TRUE, SVt_PV)) |
d8a83dd3 | 2302 | sv_setiv(GvSV(tmpgv), getpid()); |
3280af22 NIS |
2303 | PL_forkprocess = 0; |
2304 | hv_clear(PL_pidstatus); /* we have no children */ | |
a687059c LW |
2305 | return Nullfp; |
2306 | #undef THIS | |
2307 | #undef THAT | |
2308 | } | |
62b28dd9 | 2309 | do_execfree(); /* free any memory malloced by child on vfork */ |
6ad3d225 | 2310 | PerlLIO_close(p[that]); |
e446cec8 IZ |
2311 | if (did_pipes) |
2312 | PerlLIO_close(pp[1]); | |
8ac85365 | 2313 | if (p[that] < p[This]) { |
6ad3d225 GS |
2314 | PerlLIO_dup2(p[This], p[that]); |
2315 | PerlLIO_close(p[This]); | |
8ac85365 | 2316 | p[This] = p[that]; |
62b28dd9 | 2317 | } |
3280af22 | 2318 | sv = *av_fetch(PL_fdpid,p[This],TRUE); |
a0d0e21e | 2319 | (void)SvUPGRADE(sv,SVt_IV); |
463ee0b2 | 2320 | SvIVX(sv) = pid; |
3280af22 | 2321 | PL_forkprocess = pid; |
e446cec8 IZ |
2322 | if (did_pipes && pid > 0) { |
2323 | int errkid; | |
2324 | int n = 0, n1; | |
2325 | ||
2326 | while (n < sizeof(int)) { | |
2327 | n1 = PerlLIO_read(pp[0], | |
2328 | (void*)(((char*)&errkid)+n), | |
2329 | (sizeof(int)) - n); | |
2330 | if (n1 <= 0) | |
2331 | break; | |
2332 | n += n1; | |
2333 | } | |
2f96c702 IZ |
2334 | PerlLIO_close(pp[0]); |
2335 | did_pipes = 0; | |
e446cec8 IZ |
2336 | if (n) { /* Error */ |
2337 | if (n != sizeof(int)) | |
cea2e8a9 | 2338 | Perl_croak(aTHX_ "panic: kid popen errno read"); |
e446cec8 IZ |
2339 | errno = errkid; /* Propagate errno from kid */ |
2340 | return Nullfp; | |
2341 | } | |
2342 | } | |
2343 | if (did_pipes) | |
2344 | PerlLIO_close(pp[0]); | |
8ac85365 | 2345 | return PerlIO_fdopen(p[This], mode); |
a687059c | 2346 | } |
7c0587c8 | 2347 | #else |
55497cff | 2348 | #if defined(atarist) || defined(DJGPP) |
7c0587c8 | 2349 | FILE *popen(); |
760ac839 | 2350 | PerlIO * |
864dbfa3 | 2351 | Perl_my_popen(pTHX_ char *cmd, char *mode) |
7c0587c8 | 2352 | { |
760ac839 | 2353 | /* Needs work for PerlIO ! */ |
55497cff | 2354 | /* used 0 for 2nd parameter to PerlIO-exportFILE; apparently not used */ |
45bc9206 | 2355 | PERL_FLUSHALL_FOR_CHILD; |
55497cff | 2356 | return popen(PerlIO_exportFILE(cmd, 0), mode); |
7c0587c8 LW |
2357 | } |
2358 | #endif | |
2359 | ||
2360 | #endif /* !DOSISH */ | |
a687059c | 2361 | |
748a9306 | 2362 | #ifdef DUMP_FDS |
35ff7856 | 2363 | void |
864dbfa3 | 2364 | Perl_dump_fds(pTHX_ char *s) |
ae986130 LW |
2365 | { |
2366 | int fd; | |
2367 | struct stat tmpstatbuf; | |
2368 | ||
bf49b057 | 2369 | PerlIO_printf(Perl_debug_log,"%s", s); |
ae986130 | 2370 | for (fd = 0; fd < 32; fd++) { |
6ad3d225 | 2371 | if (PerlLIO_fstat(fd,&tmpstatbuf) >= 0) |
bf49b057 | 2372 | PerlIO_printf(Perl_debug_log," %d",fd); |
ae986130 | 2373 | } |
bf49b057 | 2374 | PerlIO_printf(Perl_debug_log,"\n"); |
ae986130 | 2375 | } |
35ff7856 | 2376 | #endif /* DUMP_FDS */ |
ae986130 | 2377 | |
fe14fcc3 | 2378 | #ifndef HAS_DUP2 |
fec02dd3 | 2379 | int |
ba106d47 | 2380 | dup2(int oldfd, int newfd) |
a687059c | 2381 | { |
a0d0e21e | 2382 | #if defined(HAS_FCNTL) && defined(F_DUPFD) |
fec02dd3 AD |
2383 | if (oldfd == newfd) |
2384 | return oldfd; | |
6ad3d225 | 2385 | PerlLIO_close(newfd); |
fec02dd3 | 2386 | return fcntl(oldfd, F_DUPFD, newfd); |
62b28dd9 | 2387 | #else |
fc36a67e | 2388 | #define DUP2_MAX_FDS 256 |
2389 | int fdtmp[DUP2_MAX_FDS]; | |
79072805 | 2390 | I32 fdx = 0; |
ae986130 LW |
2391 | int fd; |
2392 | ||
fe14fcc3 | 2393 | if (oldfd == newfd) |
fec02dd3 | 2394 | return oldfd; |
6ad3d225 | 2395 | PerlLIO_close(newfd); |
fc36a67e | 2396 | /* good enough for low fd's... */ |
6ad3d225 | 2397 | while ((fd = PerlLIO_dup(oldfd)) != newfd && fd >= 0) { |
fc36a67e | 2398 | if (fdx >= DUP2_MAX_FDS) { |
6ad3d225 | 2399 | PerlLIO_close(fd); |
fc36a67e | 2400 | fd = -1; |
2401 | break; | |
2402 | } | |
ae986130 | 2403 | fdtmp[fdx++] = fd; |
fc36a67e | 2404 | } |
ae986130 | 2405 | while (fdx > 0) |
6ad3d225 | 2406 | PerlLIO_close(fdtmp[--fdx]); |
fec02dd3 | 2407 | return fd; |
62b28dd9 | 2408 | #endif |
a687059c LW |
2409 | } |
2410 | #endif | |
2411 | ||
ff68c719 | 2412 | |
2413 | #ifdef HAS_SIGACTION | |
2414 | ||
2415 | Sighandler_t | |
864dbfa3 | 2416 | Perl_rsignal(pTHX_ int signo, Sighandler_t handler) |
ff68c719 | 2417 | { |
2418 | struct sigaction act, oact; | |
2419 | ||
2420 | act.sa_handler = handler; | |
2421 | sigemptyset(&act.sa_mask); | |
2422 | act.sa_flags = 0; | |
2423 | #ifdef SA_RESTART | |
2424 | act.sa_flags |= SA_RESTART; /* SVR4, 4.3+BSD */ | |
2425 | #endif | |
85264bed CS |
2426 | #ifdef SA_NOCLDWAIT |
2427 | if (signo == SIGCHLD && handler == (Sighandler_t)SIG_IGN) | |
2428 | act.sa_flags |= SA_NOCLDWAIT; | |
2429 | #endif | |
ff68c719 | 2430 | if (sigaction(signo, &act, &oact) == -1) |
36477c24 | 2431 | return SIG_ERR; |
ff68c719 | 2432 | else |
36477c24 | 2433 | return oact.sa_handler; |
ff68c719 | 2434 | } |
2435 | ||
2436 | Sighandler_t | |
864dbfa3 | 2437 | Perl_rsignal_state(pTHX_ int signo) |
ff68c719 | 2438 | { |
2439 | struct sigaction oact; | |
2440 | ||
2441 | if (sigaction(signo, (struct sigaction *)NULL, &oact) == -1) | |
2442 | return SIG_ERR; | |
2443 | else | |
2444 | return oact.sa_handler; | |
2445 | } | |
2446 | ||
2447 | int | |
864dbfa3 | 2448 | Perl_rsignal_save(pTHX_ int signo, Sighandler_t handler, Sigsave_t *save) |
ff68c719 | 2449 | { |
2450 | struct sigaction act; | |
2451 | ||
2452 | act.sa_handler = handler; | |
2453 | sigemptyset(&act.sa_mask); | |
2454 | act.sa_flags = 0; | |
2455 | #ifdef SA_RESTART | |
2456 | act.sa_flags |= SA_RESTART; /* SVR4, 4.3+BSD */ | |
2457 | #endif | |
85264bed CS |
2458 | #ifdef SA_NOCLDWAIT |
2459 | if (signo == SIGCHLD && handler == (Sighandler_t)SIG_IGN) | |
2460 | act.sa_flags |= SA_NOCLDWAIT; | |
2461 | #endif | |
ff68c719 | 2462 | return sigaction(signo, &act, save); |
2463 | } | |
2464 | ||
2465 | int | |
864dbfa3 | 2466 | Perl_rsignal_restore(pTHX_ int signo, Sigsave_t *save) |
ff68c719 | 2467 | { |
2468 | return sigaction(signo, save, (struct sigaction *)NULL); | |
2469 | } | |
2470 | ||
2471 | #else /* !HAS_SIGACTION */ | |
2472 | ||
2473 | Sighandler_t | |
864dbfa3 | 2474 | Perl_rsignal(pTHX_ int signo, Sighandler_t handler) |
ff68c719 | 2475 | { |
6ad3d225 | 2476 | return PerlProc_signal(signo, handler); |
ff68c719 | 2477 | } |
2478 | ||
2479 | static int sig_trapped; | |
2480 | ||
2481 | static | |
2482 | Signal_t | |
4e35701f | 2483 | sig_trap(int signo) |
ff68c719 | 2484 | { |
2485 | sig_trapped++; | |
2486 | } | |
2487 | ||
2488 | Sighandler_t | |
864dbfa3 | 2489 | Perl_rsignal_state(pTHX_ int signo) |
ff68c719 | 2490 | { |
2491 | Sighandler_t oldsig; | |
2492 | ||
2493 | sig_trapped = 0; | |
6ad3d225 GS |
2494 | oldsig = PerlProc_signal(signo, sig_trap); |
2495 | PerlProc_signal(signo, oldsig); | |
ff68c719 | 2496 | if (sig_trapped) |
6ad3d225 | 2497 | PerlProc_kill(getpid(), signo); |
ff68c719 | 2498 | return oldsig; |
2499 | } | |
2500 | ||
2501 | int | |
864dbfa3 | 2502 | Perl_rsignal_save(pTHX_ int signo, Sighandler_t handler, Sigsave_t *save) |
ff68c719 | 2503 | { |
6ad3d225 | 2504 | *save = PerlProc_signal(signo, handler); |
ff68c719 | 2505 | return (*save == SIG_ERR) ? -1 : 0; |
2506 | } | |
2507 | ||
2508 | int | |
864dbfa3 | 2509 | Perl_rsignal_restore(pTHX_ int signo, Sigsave_t *save) |
ff68c719 | 2510 | { |
6ad3d225 | 2511 | return (PerlProc_signal(signo, *save) == SIG_ERR) ? -1 : 0; |
ff68c719 | 2512 | } |
2513 | ||
2514 | #endif /* !HAS_SIGACTION */ | |
2515 | ||
5f05dabc | 2516 | /* VMS' my_pclose() is in VMS.c; same with OS/2 */ |
4d2c4e07 | 2517 | #if (!defined(DOSISH) || defined(HAS_FORK) || defined(AMIGAOS)) && !defined(VMS) && !defined(__OPEN_VM) && !defined(EPOC) |
79072805 | 2518 | I32 |
864dbfa3 | 2519 | Perl_my_pclose(pTHX_ PerlIO *ptr) |
a687059c | 2520 | { |
ff68c719 | 2521 | Sigsave_t hstat, istat, qstat; |
a687059c | 2522 | int status; |
a0d0e21e | 2523 | SV **svp; |
d8a83dd3 JH |
2524 | Pid_t pid; |
2525 | Pid_t pid2; | |
03136e13 CS |
2526 | bool close_failed; |
2527 | int saved_errno; | |
2528 | #ifdef VMS | |
2529 | int saved_vaxc_errno; | |
2530 | #endif | |
22fae026 TM |
2531 | #ifdef WIN32 |
2532 | int saved_win32_errno; | |
2533 | #endif | |
a687059c | 2534 | |
3280af22 | 2535 | svp = av_fetch(PL_fdpid,PerlIO_fileno(ptr),TRUE); |
d8a83dd3 | 2536 | pid = SvIVX(*svp); |
a0d0e21e | 2537 | SvREFCNT_dec(*svp); |
3280af22 | 2538 | *svp = &PL_sv_undef; |
ddcf38b7 IZ |
2539 | #ifdef OS2 |
2540 | if (pid == -1) { /* Opened by popen. */ | |
2541 | return my_syspclose(ptr); | |
2542 | } | |
2543 | #endif | |
03136e13 CS |
2544 | if ((close_failed = (PerlIO_close(ptr) == EOF))) { |
2545 | saved_errno = errno; | |
2546 | #ifdef VMS | |
2547 | saved_vaxc_errno = vaxc$errno; | |
2548 | #endif | |
22fae026 TM |
2549 | #ifdef WIN32 |
2550 | saved_win32_errno = GetLastError(); | |
2551 | #endif | |
03136e13 | 2552 | } |
7c0587c8 | 2553 | #ifdef UTS |
6ad3d225 | 2554 | if(PerlProc_kill(pid, 0) < 0) { return(pid); } /* HOM 12/23/91 */ |
7c0587c8 | 2555 | #endif |
ff68c719 | 2556 | rsignal_save(SIGHUP, SIG_IGN, &hstat); |
2557 | rsignal_save(SIGINT, SIG_IGN, &istat); | |
2558 | rsignal_save(SIGQUIT, SIG_IGN, &qstat); | |
748a9306 | 2559 | do { |
1d3434b8 GS |
2560 | pid2 = wait4pid(pid, &status, 0); |
2561 | } while (pid2 == -1 && errno == EINTR); | |
ff68c719 | 2562 | rsignal_restore(SIGHUP, &hstat); |
2563 | rsignal_restore(SIGINT, &istat); | |
2564 | rsignal_restore(SIGQUIT, &qstat); | |
03136e13 CS |
2565 | if (close_failed) { |
2566 | SETERRNO(saved_errno, saved_vaxc_errno); | |
2567 | return -1; | |
2568 | } | |
1d3434b8 | 2569 | return(pid2 < 0 ? pid2 : status == 0 ? 0 : (errno = 0, status)); |
20188a90 | 2570 | } |
4633a7c4 LW |
2571 | #endif /* !DOSISH */ |
2572 | ||
2c2d71f5 | 2573 | #if !defined(DOSISH) || defined(OS2) || defined(WIN32) |
79072805 | 2574 | I32 |
d8a83dd3 | 2575 | Perl_wait4pid(pTHX_ Pid_t pid, int *statusp, int flags) |
20188a90 | 2576 | { |
79072805 LW |
2577 | SV *sv; |
2578 | SV** svp; | |
fc36a67e | 2579 | char spid[TYPE_CHARS(int)]; |
20188a90 LW |
2580 | |
2581 | if (!pid) | |
2582 | return -1; | |
20188a90 | 2583 | if (pid > 0) { |
7b0972df | 2584 | sprintf(spid, "%"IVdf, (IV)pid); |
3280af22 NIS |
2585 | svp = hv_fetch(PL_pidstatus,spid,strlen(spid),FALSE); |
2586 | if (svp && *svp != &PL_sv_undef) { | |
463ee0b2 | 2587 | *statusp = SvIVX(*svp); |
3280af22 | 2588 | (void)hv_delete(PL_pidstatus,spid,strlen(spid),G_DISCARD); |
20188a90 LW |
2589 | return pid; |
2590 | } | |
2591 | } | |
2592 | else { | |
79072805 | 2593 | HE *entry; |
20188a90 | 2594 | |
3280af22 NIS |
2595 | hv_iterinit(PL_pidstatus); |
2596 | if (entry = hv_iternext(PL_pidstatus)) { | |
a0d0e21e | 2597 | pid = atoi(hv_iterkey(entry,(I32*)statusp)); |
3280af22 | 2598 | sv = hv_iterval(PL_pidstatus,entry); |
463ee0b2 | 2599 | *statusp = SvIVX(sv); |
7b0972df | 2600 | sprintf(spid, "%"IVdf, (IV)pid); |
3280af22 | 2601 | (void)hv_delete(PL_pidstatus,spid,strlen(spid),G_DISCARD); |
20188a90 LW |
2602 | return pid; |
2603 | } | |
2604 | } | |
79072805 | 2605 | #ifdef HAS_WAITPID |
367f3c24 IZ |
2606 | # ifdef HAS_WAITPID_RUNTIME |
2607 | if (!HAS_WAITPID_RUNTIME) | |
2608 | goto hard_way; | |
2609 | # endif | |
f55ee38a | 2610 | return PerlProc_waitpid(pid,statusp,flags); |
367f3c24 IZ |
2611 | #endif |
2612 | #if !defined(HAS_WAITPID) && defined(HAS_WAIT4) | |
a0d0e21e | 2613 | return wait4((pid==-1)?0:pid,statusp,flags,Null(struct rusage *)); |
367f3c24 IZ |
2614 | #endif |
2615 | #if !defined(HAS_WAITPID) && !defined(HAS_WAIT4) || defined(HAS_WAITPID_RUNTIME) | |
2616 | hard_way: | |
a0d0e21e LW |
2617 | { |
2618 | I32 result; | |
2619 | if (flags) | |
cea2e8a9 | 2620 | Perl_croak(aTHX_ "Can't do waitpid with flags"); |
a0d0e21e | 2621 | else { |
76e3520e | 2622 | while ((result = PerlProc_wait(statusp)) != pid && pid > 0 && result >= 0) |
a0d0e21e LW |
2623 | pidgone(result,*statusp); |
2624 | if (result < 0) | |
2625 | *statusp = -1; | |
2626 | } | |
2627 | return result; | |
a687059c LW |
2628 | } |
2629 | #endif | |
a687059c | 2630 | } |
2d7a9237 | 2631 | #endif /* !DOSISH || OS2 || WIN32 */ |
a687059c | 2632 | |
7c0587c8 | 2633 | void |
de3bb511 | 2634 | /*SUPPRESS 590*/ |
d8a83dd3 | 2635 | Perl_pidgone(pTHX_ Pid_t pid, int status) |
a687059c | 2636 | { |
79072805 | 2637 | register SV *sv; |
fc36a67e | 2638 | char spid[TYPE_CHARS(int)]; |
a687059c | 2639 | |
7b0972df | 2640 | sprintf(spid, "%"IVdf, (IV)pid); |
3280af22 | 2641 | sv = *hv_fetch(PL_pidstatus,spid,strlen(spid),TRUE); |
a0d0e21e | 2642 | (void)SvUPGRADE(sv,SVt_IV); |
463ee0b2 | 2643 | SvIVX(sv) = status; |
20188a90 | 2644 | return; |
a687059c LW |
2645 | } |
2646 | ||
55497cff | 2647 | #if defined(atarist) || defined(OS2) || defined(DJGPP) |
7c0587c8 | 2648 | int pclose(); |
ddcf38b7 IZ |
2649 | #ifdef HAS_FORK |
2650 | int /* Cannot prototype with I32 | |
2651 | in os2ish.h. */ | |
ba106d47 | 2652 | my_syspclose(PerlIO *ptr) |
ddcf38b7 | 2653 | #else |
79072805 | 2654 | I32 |
864dbfa3 | 2655 | Perl_my_pclose(pTHX_ PerlIO *ptr) |
ddcf38b7 | 2656 | #endif |
a687059c | 2657 | { |
760ac839 LW |
2658 | /* Needs work for PerlIO ! */ |
2659 | FILE *f = PerlIO_findFILE(ptr); | |
2660 | I32 result = pclose(f); | |
933fea7f GS |
2661 | #if defined(DJGPP) |
2662 | result = (result << 8) & 0xff00; | |
2663 | #endif | |
760ac839 LW |
2664 | PerlIO_releaseFILE(ptr,f); |
2665 | return result; | |
a687059c | 2666 | } |
7c0587c8 | 2667 | #endif |
9f68db38 LW |
2668 | |
2669 | void | |
864dbfa3 | 2670 | Perl_repeatcpy(pTHX_ register char *to, register const char *from, I32 len, register I32 count) |
9f68db38 | 2671 | { |
79072805 | 2672 | register I32 todo; |
08105a92 | 2673 | register const char *frombase = from; |
9f68db38 LW |
2674 | |
2675 | if (len == 1) { | |
08105a92 | 2676 | register const char c = *from; |
9f68db38 | 2677 | while (count-- > 0) |
5926133d | 2678 | *to++ = c; |
9f68db38 LW |
2679 | return; |
2680 | } | |
2681 | while (count-- > 0) { | |
2682 | for (todo = len; todo > 0; todo--) { | |
2683 | *to++ = *from++; | |
2684 | } | |
2685 | from = frombase; | |
2686 | } | |
2687 | } | |
0f85fab0 | 2688 | |
463ee0b2 | 2689 | U32 |
65202027 | 2690 | Perl_cast_ulong(pTHX_ NV f) |
0f85fab0 LW |
2691 | { |
2692 | long along; | |
2693 | ||
27e2fb84 | 2694 | #if CASTFLAGS & 2 |
34de22dd LW |
2695 | # define BIGDOUBLE 2147483648.0 |
2696 | if (f >= BIGDOUBLE) | |
2697 | return (unsigned long)(f-(long)(f/BIGDOUBLE)*BIGDOUBLE)|0x80000000; | |
2698 | #endif | |
0f85fab0 LW |
2699 | if (f >= 0.0) |
2700 | return (unsigned long)f; | |
2701 | along = (long)f; | |
2702 | return (unsigned long)along; | |
2703 | } | |
ed6116ce | 2704 | # undef BIGDOUBLE |
5d94fbed | 2705 | |
5d94fbed AD |
2706 | /* Unfortunately, on some systems the cast_uv() function doesn't |
2707 | work with the system-supplied definition of ULONG_MAX. The | |
2708 | comparison (f >= ULONG_MAX) always comes out true. It must be a | |
2709 | problem with the compiler constant folding. | |
2710 | ||
2711 | In any case, this workaround should be fine on any two's complement | |
2712 | system. If it's not, supply a '-DMY_ULONG_MAX=whatever' in your | |
2713 | ccflags. | |
2714 | --Andy Dougherty <doughera@lafcol.lafayette.edu> | |
2715 | */ | |
1eb770ff | 2716 | |
2717 | /* Code modified to prefer proper named type ranges, I32, IV, or UV, instead | |
2718 | of LONG_(MIN/MAX). | |
2719 | -- Kenneth Albanowski <kjahds@kjahds.com> | |
2720 | */ | |
2721 | ||
20cec16a | 2722 | #ifndef MY_UV_MAX |
2723 | # define MY_UV_MAX ((UV)IV_MAX * (UV)2 + (UV)1) | |
5d94fbed AD |
2724 | #endif |
2725 | ||
ed6116ce | 2726 | I32 |
65202027 | 2727 | Perl_cast_i32(pTHX_ NV f) |
ed6116ce | 2728 | { |
20cec16a | 2729 | if (f >= I32_MAX) |
2730 | return (I32) I32_MAX; | |
2731 | if (f <= I32_MIN) | |
2732 | return (I32) I32_MIN; | |
ed6116ce LW |
2733 | return (I32) f; |
2734 | } | |
a0d0e21e LW |
2735 | |
2736 | IV | |
65202027 | 2737 | Perl_cast_iv(pTHX_ NV f) |
a0d0e21e | 2738 | { |
25da4f38 IZ |
2739 | if (f >= IV_MAX) { |
2740 | UV uv; | |
2741 | ||
65202027 | 2742 | if (f >= (NV)UV_MAX) |
25da4f38 IZ |
2743 | return (IV) UV_MAX; |
2744 | uv = (UV) f; | |
2745 | return (IV)uv; | |
2746 | } | |
20cec16a | 2747 | if (f <= IV_MIN) |
2748 | return (IV) IV_MIN; | |
a0d0e21e LW |
2749 | return (IV) f; |
2750 | } | |
5d94fbed AD |
2751 | |
2752 | UV | |
65202027 | 2753 | Perl_cast_uv(pTHX_ NV f) |
5d94fbed | 2754 | { |
20cec16a | 2755 | if (f >= MY_UV_MAX) |
2756 | return (UV) MY_UV_MAX; | |
25da4f38 IZ |
2757 | if (f < 0) { |
2758 | IV iv; | |
2759 | ||
2760 | if (f < IV_MIN) | |
2761 | return (UV)IV_MIN; | |
2762 | iv = (IV) f; | |
2763 | return (UV) iv; | |
2764 | } | |
5d94fbed AD |
2765 | return (UV) f; |
2766 | } | |
2767 | ||
fe14fcc3 | 2768 | #ifndef HAS_RENAME |
79072805 | 2769 | I32 |
864dbfa3 | 2770 | Perl_same_dirent(pTHX_ char *a, char *b) |
62b28dd9 | 2771 | { |
93a17b20 LW |
2772 | char *fa = strrchr(a,'/'); |
2773 | char *fb = strrchr(b,'/'); | |
62b28dd9 LW |
2774 | struct stat tmpstatbuf1; |
2775 | struct stat tmpstatbuf2; | |
46fc3d4c | 2776 | SV *tmpsv = sv_newmortal(); |
62b28dd9 LW |
2777 | |
2778 | if (fa) | |
2779 | fa++; | |
2780 | else | |
2781 | fa = a; | |
2782 | if (fb) | |
2783 | fb++; | |
2784 | else | |
2785 | fb = b; | |
2786 | if (strNE(a,b)) | |
2787 | return FALSE; | |
2788 | if (fa == a) | |
46fc3d4c | 2789 | sv_setpv(tmpsv, "."); |
62b28dd9 | 2790 | else |
46fc3d4c | 2791 | sv_setpvn(tmpsv, a, fa - a); |
c6ed36e1 | 2792 | if (PerlLIO_stat(SvPVX(tmpsv), &tmpstatbuf1) < 0) |
62b28dd9 LW |
2793 | return FALSE; |
2794 | if (fb == b) | |
46fc3d4c | 2795 | sv_setpv(tmpsv, "."); |
62b28dd9 | 2796 | else |
46fc3d4c | 2797 | sv_setpvn(tmpsv, b, fb - b); |
c6ed36e1 | 2798 | if (PerlLIO_stat(SvPVX(tmpsv), &tmpstatbuf2) < 0) |
62b28dd9 LW |
2799 | return FALSE; |
2800 | return tmpstatbuf1.st_dev == tmpstatbuf2.st_dev && | |
2801 | tmpstatbuf1.st_ino == tmpstatbuf2.st_ino; | |
2802 | } | |
fe14fcc3 LW |
2803 | #endif /* !HAS_RENAME */ |
2804 | ||
9e24b6e2 | 2805 | NV |
864dbfa3 | 2806 | Perl_scan_bin(pTHX_ char *start, I32 len, I32 *retlen) |
4f19785b WSI |
2807 | { |
2808 | register char *s = start; | |
9e24b6e2 JH |
2809 | register NV rnv = 0.0; |
2810 | register UV ruv = 0; | |
252aa082 | 2811 | register bool seenb = FALSE; |
9e24b6e2 | 2812 | register bool overflowed = FALSE; |
252aa082 JH |
2813 | |
2814 | for (; len-- && *s; s++) { | |
2815 | if (!(*s == '0' || *s == '1')) { | |
2816 | if (*s == '_') | |
9e24b6e2 | 2817 | continue; /* Note: does not check for __ and the like. */ |
2cc4c2dc | 2818 | if (seenb == FALSE && *s == 'b' && ruv == 0) { |
252aa082 JH |
2819 | /* Disallow 0bbb0b0bbb... */ |
2820 | seenb = TRUE; | |
252aa082 JH |
2821 | continue; |
2822 | } | |
2823 | else { | |
2824 | dTHR; | |
627300f0 JH |
2825 | if (ckWARN(WARN_DIGIT)) |
2826 | Perl_warner(aTHX_ WARN_DIGIT, | |
252aa082 JH |
2827 | "Illegal binary digit '%c' ignored", *s); |
2828 | break; | |
2829 | } | |
9e24b6e2 JH |
2830 | } |
2831 | if (!overflowed) { | |
2832 | register UV xuv = ruv << 1; | |
2833 | ||
2834 | if ((xuv >> 1) != ruv) { | |
2835 | dTHR; | |
2836 | overflowed = TRUE; | |
2837 | rnv = (NV) ruv; | |
627300f0 JH |
2838 | if (ckWARN_d(WARN_OVERFLOW)) |
2839 | Perl_warner(aTHX_ WARN_OVERFLOW, | |
9e24b6e2 JH |
2840 | "Integer overflow in binary number"); |
2841 | } else | |
2842 | ruv = xuv | (*s - '0'); | |
2843 | } | |
2844 | if (overflowed) { | |
2845 | rnv *= 2; | |
2846 | /* If an NV has not enough bits in its mantissa to | |
2847 | * represent an UV this summing of small low-order numbers | |
2848 | * is a waste of time (because the NV cannot preserve | |
2849 | * the low-order bits anyway): we could just remember when | |
2850 | * did we overflow and in the end just multiply rnv by the | |
2cc4c2dc | 2851 | * right amount. */ |
9e24b6e2 | 2852 | rnv += (*s - '0'); |
f248d071 | 2853 | } |
4f19785b | 2854 | } |
9e24b6e2 JH |
2855 | if (!overflowed) |
2856 | rnv = (NV) ruv; | |
893fe2c2 | 2857 | if ( ( overflowed && rnv > 4294967295.0) |
15041a67 | 2858 | #if UVSIZE > 4 |
893fe2c2 JH |
2859 | || (!overflowed && ruv > 0xffffffff ) |
2860 | #endif | |
2861 | ) { | |
252aa082 | 2862 | dTHR; |
627300f0 JH |
2863 | if (ckWARN(WARN_PORTABLE)) |
2864 | Perl_warner(aTHX_ WARN_PORTABLE, | |
252aa082 | 2865 | "Binary number > 0b11111111111111111111111111111111 non-portable"); |
4f19785b WSI |
2866 | } |
2867 | *retlen = s - start; | |
9e24b6e2 | 2868 | return rnv; |
4f19785b | 2869 | } |
9e24b6e2 JH |
2870 | |
2871 | NV | |
864dbfa3 | 2872 | Perl_scan_oct(pTHX_ char *start, I32 len, I32 *retlen) |
fe14fcc3 LW |
2873 | { |
2874 | register char *s = start; | |
9e24b6e2 JH |
2875 | register NV rnv = 0.0; |
2876 | register UV ruv = 0; | |
2877 | register bool overflowed = FALSE; | |
252aa082 JH |
2878 | |
2879 | for (; len-- && *s; s++) { | |
2880 | if (!(*s >= '0' && *s <= '7')) { | |
2881 | if (*s == '_') | |
9e24b6e2 | 2882 | continue; /* Note: does not check for __ and the like. */ |
252aa082 | 2883 | else { |
f84f607d JH |
2884 | /* Allow \octal to work the DWIM way (that is, stop scanning |
2885 | * as soon as non-octal characters are seen, complain only iff | |
2886 | * someone seems to want to use the digits eight and nine). */ | |
252aa082 JH |
2887 | if (*s == '8' || *s == '9') { |
2888 | dTHR; | |
627300f0 JH |
2889 | if (ckWARN(WARN_DIGIT)) |
2890 | Perl_warner(aTHX_ WARN_DIGIT, | |
252aa082 JH |
2891 | "Illegal octal digit '%c' ignored", *s); |
2892 | } | |
2893 | break; | |
2894 | } | |
55497cff | 2895 | } |
9e24b6e2 | 2896 | if (!overflowed) { |
893fe2c2 | 2897 | register UV xuv = ruv << 3; |
9e24b6e2 JH |
2898 | |
2899 | if ((xuv >> 3) != ruv) { | |
2900 | dTHR; | |
2901 | overflowed = TRUE; | |
2902 | rnv = (NV) ruv; | |
627300f0 JH |
2903 | if (ckWARN_d(WARN_OVERFLOW)) |
2904 | Perl_warner(aTHX_ WARN_OVERFLOW, | |
9e24b6e2 JH |
2905 | "Integer overflow in octal number"); |
2906 | } else | |
2907 | ruv = xuv | (*s - '0'); | |
2908 | } | |
2909 | if (overflowed) { | |
2910 | rnv *= 8.0; | |
2911 | /* If an NV has not enough bits in its mantissa to | |
2912 | * represent an UV this summing of small low-order numbers | |
2913 | * is a waste of time (because the NV cannot preserve | |
2914 | * the low-order bits anyway): we could just remember when | |
2915 | * did we overflow and in the end just multiply rnv by the | |
2916 | * right amount of 8-tuples. */ | |
2917 | rnv += (NV)(*s - '0'); | |
2918 | } | |
fe14fcc3 | 2919 | } |
9e24b6e2 JH |
2920 | if (!overflowed) |
2921 | rnv = (NV) ruv; | |
893fe2c2 | 2922 | if ( ( overflowed && rnv > 4294967295.0) |
15041a67 | 2923 | #if UVSIZE > 4 |
893fe2c2 JH |
2924 | || (!overflowed && ruv > 0xffffffff ) |
2925 | #endif | |
2926 | ) { | |
d008e5eb | 2927 | dTHR; |
627300f0 JH |
2928 | if (ckWARN(WARN_PORTABLE)) |
2929 | Perl_warner(aTHX_ WARN_PORTABLE, | |
252aa082 | 2930 | "Octal number > 037777777777 non-portable"); |
d008e5eb | 2931 | } |
fe14fcc3 | 2932 | *retlen = s - start; |
9e24b6e2 | 2933 | return rnv; |
fe14fcc3 LW |
2934 | } |
2935 | ||
9e24b6e2 | 2936 | NV |
864dbfa3 | 2937 | Perl_scan_hex(pTHX_ char *start, I32 len, I32 *retlen) |
fe14fcc3 LW |
2938 | { |
2939 | register char *s = start; | |
9e24b6e2 JH |
2940 | register NV rnv = 0.0; |
2941 | register UV ruv = 0; | |
252aa082 | 2942 | register bool seenx = FALSE; |
9e24b6e2 | 2943 | register bool overflowed = FALSE; |
9e24b6e2 | 2944 | char *hexdigit; |
fe14fcc3 | 2945 | |
9e24b6e2 JH |
2946 | for (; len-- && *s; s++) { |
2947 | hexdigit = strchr((char *) PL_hexdigit, *s); | |
2948 | if (!hexdigit) { | |
2949 | if (*s == '_') | |
2950 | continue; /* Note: does not check for __ and the like. */ | |
2cc4c2dc | 2951 | if (seenx == FALSE && *s == 'x' && ruv == 0) { |
252aa082 JH |
2952 | /* Disallow 0xxx0x0xxx... */ |
2953 | seenx = TRUE; | |
252aa082 JH |
2954 | continue; |
2955 | } | |
a0ed51b3 | 2956 | else { |
d008e5eb | 2957 | dTHR; |
627300f0 JH |
2958 | if (ckWARN(WARN_DIGIT)) |
2959 | Perl_warner(aTHX_ WARN_DIGIT, | |
252aa082 | 2960 | "Illegal hexadecimal digit '%c' ignored", *s); |
a0ed51b3 LW |
2961 | break; |
2962 | } | |
2963 | } | |
9e24b6e2 JH |
2964 | if (!overflowed) { |
2965 | register UV xuv = ruv << 4; | |
2966 | ||
2967 | if ((xuv >> 4) != ruv) { | |
2968 | dTHR; | |
2969 | overflowed = TRUE; | |
2970 | rnv = (NV) ruv; | |
627300f0 JH |
2971 | if (ckWARN_d(WARN_OVERFLOW)) |
2972 | Perl_warner(aTHX_ WARN_OVERFLOW, | |
9e24b6e2 JH |
2973 | "Integer overflow in hexadecimal number"); |
2974 | } else | |
2975 | ruv = xuv | ((hexdigit - PL_hexdigit) & 15); | |
2976 | } | |
2977 | if (overflowed) { | |
2978 | rnv *= 16.0; | |
2979 | /* If an NV has not enough bits in its mantissa to | |
2980 | * represent an UV this summing of small low-order numbers | |
2981 | * is a waste of time (because the NV cannot preserve | |
2982 | * the low-order bits anyway): we could just remember when | |
2983 | * did we overflow and in the end just multiply rnv by the | |
2984 | * right amount of 16-tuples. */ | |
2985 | rnv += (NV)((hexdigit - PL_hexdigit) & 15); | |
2986 | } | |
6ff81951 | 2987 | } |
9e24b6e2 JH |
2988 | if (!overflowed) |
2989 | rnv = (NV) ruv; | |
893fe2c2 | 2990 | if ( ( overflowed && rnv > 4294967295.0) |
15041a67 | 2991 | #if UVSIZE > 4 |
893fe2c2 JH |
2992 | || (!overflowed && ruv > 0xffffffff ) |
2993 | #endif | |
2994 | ) { | |
252aa082 | 2995 | dTHR; |
627300f0 JH |
2996 | if (ckWARN(WARN_PORTABLE)) |
2997 | Perl_warner(aTHX_ WARN_PORTABLE, | |
252aa082 JH |
2998 | "Hexadecimal number > 0xffffffff non-portable"); |
2999 | } | |
fe14fcc3 | 3000 | *retlen = s - start; |
9e24b6e2 | 3001 | return rnv; |
fe14fcc3 | 3002 | } |
760ac839 | 3003 | |
491527d0 | 3004 | char* |
864dbfa3 | 3005 | Perl_find_script(pTHX_ char *scriptname, bool dosearch, char **search_ext, I32 flags) |
491527d0 GS |
3006 | { |
3007 | dTHR; | |
3008 | char *xfound = Nullch; | |
3009 | char *xfailed = Nullch; | |
0f31cffe | 3010 | char tmpbuf[MAXPATHLEN]; |
491527d0 GS |
3011 | register char *s; |
3012 | I32 len; | |
3013 | int retval; | |
3014 | #if defined(DOSISH) && !defined(OS2) && !defined(atarist) | |
3015 | # define SEARCH_EXTS ".bat", ".cmd", NULL | |
3016 | # define MAX_EXT_LEN 4 | |
3017 | #endif | |
3018 | #ifdef OS2 | |
3019 | # define SEARCH_EXTS ".cmd", ".btm", ".bat", ".pl", NULL | |
3020 | # define MAX_EXT_LEN 4 | |
3021 | #endif | |
3022 | #ifdef VMS | |
3023 | # define SEARCH_EXTS ".pl", ".com", NULL | |
3024 | # define MAX_EXT_LEN 4 | |
3025 | #endif | |
3026 | /* additional extensions to try in each dir if scriptname not found */ | |
3027 | #ifdef SEARCH_EXTS | |
3028 | char *exts[] = { SEARCH_EXTS }; | |
3029 | char **ext = search_ext ? search_ext : exts; | |
3030 | int extidx = 0, i = 0; | |
3031 | char *curext = Nullch; | |
3032 | #else | |
3033 | # define MAX_EXT_LEN 0 | |
3034 | #endif | |
3035 | ||
3036 | /* | |
3037 | * If dosearch is true and if scriptname does not contain path | |
3038 | * delimiters, search the PATH for scriptname. | |
3039 | * | |
3040 | * If SEARCH_EXTS is also defined, will look for each | |
3041 | * scriptname{SEARCH_EXTS} whenever scriptname is not found | |
3042 | * while searching the PATH. | |
3043 | * | |
3044 | * Assuming SEARCH_EXTS is C<".foo",".bar",NULL>, PATH search | |
3045 | * proceeds as follows: | |
3046 | * If DOSISH or VMSISH: | |
3047 | * + look for ./scriptname{,.foo,.bar} | |
3048 | * + search the PATH for scriptname{,.foo,.bar} | |
3049 | * | |
3050 | * If !DOSISH: | |
3051 | * + look *only* in the PATH for scriptname{,.foo,.bar} (note | |
3052 | * this will not look in '.' if it's not in the PATH) | |
3053 | */ | |
84486fc6 | 3054 | tmpbuf[0] = '\0'; |
491527d0 GS |
3055 | |
3056 | #ifdef VMS | |
3057 | # ifdef ALWAYS_DEFTYPES | |
3058 | len = strlen(scriptname); | |
3059 | if (!(len == 1 && *scriptname == '-') && scriptname[len-1] != ':') { | |
3060 | int hasdir, idx = 0, deftypes = 1; | |
3061 | bool seen_dot = 1; | |
3062 | ||
3063 | hasdir = !dosearch || (strpbrk(scriptname,":[</") != Nullch) ; | |
3064 | # else | |
3065 | if (dosearch) { | |
3066 | int hasdir, idx = 0, deftypes = 1; | |
3067 | bool seen_dot = 1; | |
3068 | ||
3069 | hasdir = (strpbrk(scriptname,":[</") != Nullch) ; | |
3070 | # endif | |
3071 | /* The first time through, just add SEARCH_EXTS to whatever we | |
3072 | * already have, so we can check for default file types. */ | |
3073 | while (deftypes || | |
84486fc6 | 3074 | (!hasdir && my_trnlnm("DCL$PATH",tmpbuf,idx++)) ) |
491527d0 GS |
3075 | { |
3076 | if (deftypes) { | |
3077 | deftypes = 0; | |
84486fc6 | 3078 | *tmpbuf = '\0'; |
491527d0 | 3079 | } |
84486fc6 GS |
3080 | if ((strlen(tmpbuf) + strlen(scriptname) |
3081 | + MAX_EXT_LEN) >= sizeof tmpbuf) | |
491527d0 | 3082 | continue; /* don't search dir with too-long name */ |
84486fc6 | 3083 | strcat(tmpbuf, scriptname); |
491527d0 GS |
3084 | #else /* !VMS */ |
3085 | ||
3086 | #ifdef DOSISH | |
3087 | if (strEQ(scriptname, "-")) | |
3088 | dosearch = 0; | |
3089 | if (dosearch) { /* Look in '.' first. */ | |
3090 | char *cur = scriptname; | |
3091 | #ifdef SEARCH_EXTS | |
3092 | if ((curext = strrchr(scriptname,'.'))) /* possible current ext */ | |
3093 | while (ext[i]) | |
3094 | if (strEQ(ext[i++],curext)) { | |
3095 | extidx = -1; /* already has an ext */ | |
3096 | break; | |
3097 | } | |
3098 | do { | |
3099 | #endif | |
3100 | DEBUG_p(PerlIO_printf(Perl_debug_log, | |
3101 | "Looking for %s\n",cur)); | |
017f25f1 IZ |
3102 | if (PerlLIO_stat(cur,&PL_statbuf) >= 0 |
3103 | && !S_ISDIR(PL_statbuf.st_mode)) { | |
491527d0 GS |
3104 | dosearch = 0; |
3105 | scriptname = cur; | |
3106 | #ifdef SEARCH_EXTS | |
3107 | break; | |
3108 | #endif | |
3109 | } | |
3110 | #ifdef SEARCH_EXTS | |
3111 | if (cur == scriptname) { | |
3112 | len = strlen(scriptname); | |
84486fc6 | 3113 | if (len+MAX_EXT_LEN+1 >= sizeof(tmpbuf)) |
491527d0 | 3114 | break; |
84486fc6 | 3115 | cur = strcpy(tmpbuf, scriptname); |
491527d0 GS |
3116 | } |
3117 | } while (extidx >= 0 && ext[extidx] /* try an extension? */ | |
84486fc6 | 3118 | && strcpy(tmpbuf+len, ext[extidx++])); |
491527d0 GS |
3119 | #endif |
3120 | } | |
3121 | #endif | |
3122 | ||
3123 | if (dosearch && !strchr(scriptname, '/') | |
3124 | #ifdef DOSISH | |
3125 | && !strchr(scriptname, '\\') | |
3126 | #endif | |
3127 | && (s = PerlEnv_getenv("PATH"))) { | |
3128 | bool seen_dot = 0; | |
3129 | ||
3280af22 NIS |
3130 | PL_bufend = s + strlen(s); |
3131 | while (s < PL_bufend) { | |
491527d0 GS |
3132 | #if defined(atarist) || defined(DOSISH) |
3133 | for (len = 0; *s | |
3134 | # ifdef atarist | |
3135 | && *s != ',' | |
3136 | # endif | |
3137 | && *s != ';'; len++, s++) { | |
84486fc6 GS |
3138 | if (len < sizeof tmpbuf) |
3139 | tmpbuf[len] = *s; | |
491527d0 | 3140 | } |
84486fc6 GS |
3141 | if (len < sizeof tmpbuf) |
3142 | tmpbuf[len] = '\0'; | |
491527d0 | 3143 | #else /* ! (atarist || DOSISH) */ |
3280af22 | 3144 | s = delimcpy(tmpbuf, tmpbuf + sizeof tmpbuf, s, PL_bufend, |
491527d0 GS |
3145 | ':', |
3146 | &len); | |
3147 | #endif /* ! (atarist || DOSISH) */ | |
3280af22 | 3148 | if (s < PL_bufend) |
491527d0 | 3149 | s++; |
84486fc6 | 3150 | if (len + 1 + strlen(scriptname) + MAX_EXT_LEN >= sizeof tmpbuf) |
491527d0 GS |
3151 | continue; /* don't search dir with too-long name */ |
3152 | if (len | |
61ae2fbf | 3153 | #if defined(atarist) || defined(__MINT__) || defined(DOSISH) |
84486fc6 GS |
3154 | && tmpbuf[len - 1] != '/' |
3155 | && tmpbuf[len - 1] != '\\' | |
491527d0 GS |
3156 | #endif |
3157 | ) | |
84486fc6 GS |
3158 | tmpbuf[len++] = '/'; |
3159 | if (len == 2 && tmpbuf[0] == '.') | |
491527d0 | 3160 | seen_dot = 1; |
84486fc6 | 3161 | (void)strcpy(tmpbuf + len, scriptname); |
491527d0 GS |
3162 | #endif /* !VMS */ |
3163 | ||
3164 | #ifdef SEARCH_EXTS | |
84486fc6 | 3165 | len = strlen(tmpbuf); |
491527d0 GS |
3166 | if (extidx > 0) /* reset after previous loop */ |
3167 | extidx = 0; | |
3168 | do { | |
3169 | #endif | |
84486fc6 | 3170 | DEBUG_p(PerlIO_printf(Perl_debug_log, "Looking for %s\n",tmpbuf)); |
3280af22 | 3171 | retval = PerlLIO_stat(tmpbuf,&PL_statbuf); |
017f25f1 IZ |
3172 | if (S_ISDIR(PL_statbuf.st_mode)) { |
3173 | retval = -1; | |
3174 | } | |
491527d0 GS |
3175 | #ifdef SEARCH_EXTS |
3176 | } while ( retval < 0 /* not there */ | |
3177 | && extidx>=0 && ext[extidx] /* try an extension? */ | |
84486fc6 | 3178 | && strcpy(tmpbuf+len, ext[extidx++]) |
491527d0 GS |
3179 | ); |
3180 | #endif | |
3181 | if (retval < 0) | |
3182 | continue; | |
3280af22 NIS |
3183 | if (S_ISREG(PL_statbuf.st_mode) |
3184 | && cando(S_IRUSR,TRUE,&PL_statbuf) | |
491527d0 | 3185 | #ifndef DOSISH |
3280af22 | 3186 | && cando(S_IXUSR,TRUE,&PL_statbuf) |
491527d0 GS |
3187 | #endif |
3188 | ) | |
3189 | { | |
84486fc6 | 3190 | xfound = tmpbuf; /* bingo! */ |
491527d0 GS |
3191 | break; |
3192 | } | |
3193 | if (!xfailed) | |
84486fc6 | 3194 | xfailed = savepv(tmpbuf); |
491527d0 GS |
3195 | } |
3196 | #ifndef DOSISH | |
017f25f1 IZ |
3197 | if (!xfound && !seen_dot && !xfailed && |
3198 | (PerlLIO_stat(scriptname,&PL_statbuf) < 0 | |
3199 | || S_ISDIR(PL_statbuf.st_mode))) | |
491527d0 GS |
3200 | #endif |
3201 | seen_dot = 1; /* Disable message. */ | |
9ccb31f9 GS |
3202 | if (!xfound) { |
3203 | if (flags & 1) { /* do or die? */ | |
cea2e8a9 | 3204 | Perl_croak(aTHX_ "Can't %s %s%s%s", |
9ccb31f9 GS |
3205 | (xfailed ? "execute" : "find"), |
3206 | (xfailed ? xfailed : scriptname), | |
3207 | (xfailed ? "" : " on PATH"), | |
3208 | (xfailed || seen_dot) ? "" : ", '.' not in PATH"); | |
3209 | } | |
3210 | scriptname = Nullch; | |
3211 | } | |
491527d0 GS |
3212 | if (xfailed) |
3213 | Safefree(xfailed); | |
3214 | scriptname = xfound; | |
3215 | } | |
9ccb31f9 | 3216 | return (scriptname ? savepv(scriptname) : Nullch); |
491527d0 GS |
3217 | } |
3218 | ||
3219 | ||
11343788 | 3220 | #ifdef USE_THREADS |
12ca11f6 MB |
3221 | #ifdef FAKE_THREADS |
3222 | /* Very simplistic scheduler for now */ | |
3223 | void | |
3224 | schedule(void) | |
3225 | { | |
c7848ba1 | 3226 | thr = thr->i.next_run; |
12ca11f6 MB |
3227 | } |
3228 | ||
3229 | void | |
864dbfa3 | 3230 | Perl_cond_init(pTHX_ perl_cond *cp) |
12ca11f6 MB |
3231 | { |
3232 | *cp = 0; | |
3233 | } | |
3234 | ||
3235 | void | |
864dbfa3 | 3236 | Perl_cond_signal(pTHX_ perl_cond *cp) |
12ca11f6 | 3237 | { |
51dd5992 | 3238 | perl_os_thread t; |
12ca11f6 MB |
3239 | perl_cond cond = *cp; |
3240 | ||
3241 | if (!cond) | |
3242 | return; | |
3243 | t = cond->thread; | |
3244 | /* Insert t in the runnable queue just ahead of us */ | |
c7848ba1 MB |
3245 | t->i.next_run = thr->i.next_run; |
3246 | thr->i.next_run->i.prev_run = t; | |
3247 | t->i.prev_run = thr; | |
3248 | thr->i.next_run = t; | |
3249 | thr->i.wait_queue = 0; | |
12ca11f6 MB |
3250 | /* Remove from the wait queue */ |
3251 | *cp = cond->next; | |
3252 | Safefree(cond); | |
3253 | } | |
3254 | ||
3255 | void | |
864dbfa3 | 3256 | Perl_cond_broadcast(pTHX_ perl_cond *cp) |
12ca11f6 | 3257 | { |
51dd5992 | 3258 | perl_os_thread t; |
12ca11f6 MB |
3259 | perl_cond cond, cond_next; |
3260 | ||
3261 | for (cond = *cp; cond; cond = cond_next) { | |
3262 | t = cond->thread; | |
3263 | /* Insert t in the runnable queue just ahead of us */ | |
c7848ba1 MB |
3264 | t->i.next_run = thr->i.next_run; |
3265 | thr->i.next_run->i.prev_run = t; | |
3266 | t->i.prev_run = thr; | |
3267 | thr->i.next_run = t; | |
3268 | thr->i.wait_queue = 0; | |
12ca11f6 MB |
3269 | /* Remove from the wait queue */ |
3270 | cond_next = cond->next; | |
3271 | Safefree(cond); | |
3272 | } | |
3273 | *cp = 0; | |
3274 | } | |
3275 | ||
3276 | void | |
864dbfa3 | 3277 | Perl_cond_wait(pTHX_ perl_cond *cp) |
12ca11f6 MB |
3278 | { |
3279 | perl_cond cond; | |
3280 | ||
c7848ba1 | 3281 | if (thr->i.next_run == thr) |
cea2e8a9 | 3282 | Perl_croak(aTHX_ "panic: perl_cond_wait called by last runnable thread"); |
12ca11f6 | 3283 | |
0f15f207 | 3284 | New(666, cond, 1, struct perl_wait_queue); |
12ca11f6 MB |
3285 | cond->thread = thr; |
3286 | cond->next = *cp; | |
3287 | *cp = cond; | |
c7848ba1 | 3288 | thr->i.wait_queue = cond; |
12ca11f6 | 3289 | /* Remove ourselves from runnable queue */ |
c7848ba1 MB |
3290 | thr->i.next_run->i.prev_run = thr->i.prev_run; |
3291 | thr->i.prev_run->i.next_run = thr->i.next_run; | |
12ca11f6 MB |
3292 | } |
3293 | #endif /* FAKE_THREADS */ | |
3294 | ||
0d85d877 | 3295 | #ifdef PTHREAD_GETSPECIFIC_INT |
52e1cb5e | 3296 | struct perl_thread * |
864dbfa3 | 3297 | Perl_getTHR(pTHX) |
11343788 MB |
3298 | { |
3299 | pthread_addr_t t; | |
3300 | ||
6b88bc9c | 3301 | if (pthread_getspecific(PL_thr_key, &t)) |
cea2e8a9 | 3302 | Perl_croak(aTHX_ "panic: pthread_getspecific"); |
52e1cb5e | 3303 | return (struct perl_thread *) t; |
11343788 | 3304 | } |
0d85d877 | 3305 | #endif |
f93b4edd MB |
3306 | |
3307 | MAGIC * | |
864dbfa3 | 3308 | Perl_condpair_magic(pTHX_ SV *sv) |
f93b4edd MB |
3309 | { |
3310 | MAGIC *mg; | |
3311 | ||
3312 | SvUPGRADE(sv, SVt_PVMG); | |
3313 | mg = mg_find(sv, 'm'); | |
3314 | if (!mg) { | |
3315 | condpair_t *cp; | |
3316 | ||
3317 | New(53, cp, 1, condpair_t); | |
3318 | MUTEX_INIT(&cp->mutex); | |
3319 | COND_INIT(&cp->owner_cond); | |
3320 | COND_INIT(&cp->cond); | |
3321 | cp->owner = 0; | |
28a8573f | 3322 | MUTEX_LOCK(&PL_cred_mutex); /* XXX need separate mutex? */ |
f93b4edd MB |
3323 | mg = mg_find(sv, 'm'); |
3324 | if (mg) { | |
3325 | /* someone else beat us to initialising it */ | |
28a8573f | 3326 | MUTEX_UNLOCK(&PL_cred_mutex); /* XXX need separate mutex? */ |
f93b4edd MB |
3327 | MUTEX_DESTROY(&cp->mutex); |
3328 | COND_DESTROY(&cp->owner_cond); | |
3329 | COND_DESTROY(&cp->cond); | |
3330 | Safefree(cp); | |
3331 | } | |
3332 | else { | |
3333 | sv_magic(sv, Nullsv, 'm', 0, 0); | |
3334 | mg = SvMAGIC(sv); | |
3335 | mg->mg_ptr = (char *)cp; | |
565764a8 | 3336 | mg->mg_len = sizeof(cp); |
28a8573f | 3337 | MUTEX_UNLOCK(&PL_cred_mutex); /* XXX need separate mutex? */ |
bf49b057 | 3338 | DEBUG_S(WITH_THR(PerlIO_printf(Perl_debug_log, |
c7848ba1 | 3339 | "%p: condpair_magic %p\n", thr, sv));) |
f93b4edd MB |
3340 | } |
3341 | } | |
3342 | return mg; | |
3343 | } | |
a863c7d1 MB |
3344 | |
3345 | /* | |
199100c8 MB |
3346 | * Make a new perl thread structure using t as a prototype. Some of the |
3347 | * fields for the new thread are copied from the prototype thread, t, | |
3348 | * so t should not be running in perl at the time this function is | |
3349 | * called. The use by ext/Thread/Thread.xs in core perl (where t is the | |
3350 | * thread calling new_struct_thread) clearly satisfies this constraint. | |
a863c7d1 | 3351 | */ |
52e1cb5e | 3352 | struct perl_thread * |
864dbfa3 | 3353 | Perl_new_struct_thread(pTHX_ struct perl_thread *t) |
a863c7d1 | 3354 | { |
c5be433b | 3355 | #if !defined(PERL_IMPLICIT_CONTEXT) |
52e1cb5e | 3356 | struct perl_thread *thr; |
cea2e8a9 | 3357 | #endif |
a863c7d1 | 3358 | SV *sv; |
199100c8 MB |
3359 | SV **svp; |
3360 | I32 i; | |
3361 | ||
79cb57f6 | 3362 | sv = newSVpvn("", 0); |
52e1cb5e JH |
3363 | SvGROW(sv, sizeof(struct perl_thread) + 1); |
3364 | SvCUR_set(sv, sizeof(struct perl_thread)); | |
199100c8 | 3365 | thr = (Thread) SvPVX(sv); |
949ced2d | 3366 | #ifdef DEBUGGING |
52e1cb5e | 3367 | memset(thr, 0xab, sizeof(struct perl_thread)); |
533c011a NIS |
3368 | PL_markstack = 0; |
3369 | PL_scopestack = 0; | |
3370 | PL_savestack = 0; | |
3371 | PL_retstack = 0; | |
3372 | PL_dirty = 0; | |
3373 | PL_localizing = 0; | |
949ced2d GS |
3374 | Zero(&PL_hv_fetch_ent_mh, 1, HE); |
3375 | #else | |
3376 | Zero(thr, 1, struct perl_thread); | |
3377 | #endif | |
199100c8 | 3378 | |
0b94c7bb | 3379 | PL_protect = MEMBER_TO_FPTR(Perl_default_protect); |
312caa8e | 3380 | |
199100c8 | 3381 | thr->oursv = sv; |
cea2e8a9 | 3382 | init_stacks(); |
a863c7d1 | 3383 | |
533c011a | 3384 | PL_curcop = &PL_compiling; |
c5be433b | 3385 | thr->interp = t->interp; |
199100c8 | 3386 | thr->cvcache = newHV(); |
54b9620d | 3387 | thr->threadsv = newAV(); |
a863c7d1 | 3388 | thr->specific = newAV(); |
79cb57f6 | 3389 | thr->errsv = newSVpvn("", 0); |
a863c7d1 MB |
3390 | thr->flags = THRf_R_JOINABLE; |
3391 | MUTEX_INIT(&thr->mutex); | |
199100c8 | 3392 | |
d55594ae GS |
3393 | /* top_env needs to be non-zero. It points to an area |
3394 | in which longjmp() stuff is stored, as C callstack | |
3395 | info there at least is thread specific this has to | |
3396 | be per-thread. Otherwise a 'die' in a thread gives | |
3397 | that thread the C stack of last thread to do an eval {}! | |
3398 | See comments in scope.h | |
3399 | Initialize top entry (as in perl.c for main thread) | |
3400 | */ | |
533c011a NIS |
3401 | PL_start_env.je_prev = NULL; |
3402 | PL_start_env.je_ret = -1; | |
3403 | PL_start_env.je_mustcatch = TRUE; | |
3404 | PL_top_env = &PL_start_env; | |
3405 | ||
faef0170 | 3406 | PL_in_eval = EVAL_NULL; /* ~(EVAL_INEVAL|EVAL_WARNONLY|EVAL_KEEPERR) */ |
533c011a NIS |
3407 | PL_restartop = 0; |
3408 | ||
b099ddc0 | 3409 | PL_statname = NEWSV(66,0); |
5a844595 | 3410 | PL_errors = newSVpvn("", 0); |
b099ddc0 | 3411 | PL_maxscream = -1; |
0b94c7bb GS |
3412 | PL_regcompp = MEMBER_TO_FPTR(Perl_pregcomp); |
3413 | PL_regexecp = MEMBER_TO_FPTR(Perl_regexec_flags); | |
3414 | PL_regint_start = MEMBER_TO_FPTR(Perl_re_intuit_start); | |
3415 | PL_regint_string = MEMBER_TO_FPTR(Perl_re_intuit_string); | |
3416 | PL_regfree = MEMBER_TO_FPTR(Perl_pregfree); | |
b099ddc0 GS |
3417 | PL_regindent = 0; |
3418 | PL_reginterp_cnt = 0; | |
3419 | PL_lastscream = Nullsv; | |
3420 | PL_screamfirst = 0; | |
3421 | PL_screamnext = 0; | |
3422 | PL_reg_start_tmp = 0; | |
3423 | PL_reg_start_tmpl = 0; | |
14ed4b74 | 3424 | PL_reg_poscache = Nullch; |
b099ddc0 GS |
3425 | |
3426 | /* parent thread's data needs to be locked while we make copy */ | |
3427 | MUTEX_LOCK(&t->mutex); | |
3428 | ||
312caa8e CS |
3429 | PL_protect = t->Tprotect; |
3430 | ||
b099ddc0 GS |
3431 | PL_curcop = t->Tcurcop; /* XXX As good a guess as any? */ |
3432 | PL_defstash = t->Tdefstash; /* XXX maybe these should */ | |
3433 | PL_curstash = t->Tcurstash; /* always be set to main? */ | |
3434 | ||
6b88bc9c | 3435 | PL_tainted = t->Ttainted; |
84fee439 NIS |
3436 | PL_curpm = t->Tcurpm; /* XXX No PMOP ref count */ |
3437 | PL_nrs = newSVsv(t->Tnrs); | |
3438 | PL_rs = SvREFCNT_inc(PL_nrs); | |
3439 | PL_last_in_gv = Nullgv; | |
3440 | PL_ofslen = t->Tofslen; | |
3441 | PL_ofs = savepvn(t->Tofs, PL_ofslen); | |
3442 | PL_defoutgv = (GV*)SvREFCNT_inc(t->Tdefoutgv); | |
3443 | PL_chopset = t->Tchopset; | |
3444 | PL_formtarget = newSVsv(t->Tformtarget); | |
3445 | PL_bodytarget = newSVsv(t->Tbodytarget); | |
3446 | PL_toptarget = newSVsv(t->Ttoptarget); | |
533c011a | 3447 | |
54b9620d MB |
3448 | /* Initialise all per-thread SVs that the template thread used */ |
3449 | svp = AvARRAY(t->threadsv); | |
93965878 | 3450 | for (i = 0; i <= AvFILLp(t->threadsv); i++, svp++) { |
533c011a | 3451 | if (*svp && *svp != &PL_sv_undef) { |
199100c8 | 3452 | SV *sv = newSVsv(*svp); |
54b9620d | 3453 | av_store(thr->threadsv, i, sv); |
533c011a | 3454 | sv_magic(sv, 0, 0, &PL_threadsv_names[i], 1); |
bf49b057 | 3455 | DEBUG_S(PerlIO_printf(Perl_debug_log, |
54b9620d | 3456 | "new_struct_thread: copied threadsv %d %p->%p\n",i, t, thr)); |
199100c8 MB |
3457 | } |
3458 | } | |
940cb80d | 3459 | thr->threadsvp = AvARRAY(thr->threadsv); |
199100c8 | 3460 | |
533c011a NIS |
3461 | MUTEX_LOCK(&PL_threads_mutex); |
3462 | PL_nthreads++; | |
3463 | thr->tid = ++PL_threadnum; | |
199100c8 MB |
3464 | thr->next = t->next; |
3465 | thr->prev = t; | |
3466 | t->next = thr; | |
3467 | thr->next->prev = thr; | |
533c011a | 3468 | MUTEX_UNLOCK(&PL_threads_mutex); |
a863c7d1 | 3469 | |
b099ddc0 GS |
3470 | /* done copying parent's state */ |
3471 | MUTEX_UNLOCK(&t->mutex); | |
3472 | ||
a863c7d1 | 3473 | #ifdef HAVE_THREAD_INTERN |
4f63d024 | 3474 | Perl_init_thread_intern(thr); |
a863c7d1 | 3475 | #endif /* HAVE_THREAD_INTERN */ |
a863c7d1 MB |
3476 | return thr; |
3477 | } | |
11343788 | 3478 | #endif /* USE_THREADS */ |
760ac839 LW |
3479 | |
3480 | #ifdef HUGE_VAL | |
3481 | /* | |
3482 | * This hack is to force load of "huge" support from libm.a | |
3483 | * So it is in perl for (say) POSIX to use. | |
3484 | * Needed for SunOS with Sun's 'acc' for example. | |
3485 | */ | |
65202027 | 3486 | NV |
8ac85365 | 3487 | Perl_huge(void) |
760ac839 LW |
3488 | { |
3489 | return HUGE_VAL; | |
3490 | } | |
3491 | #endif | |
4e35701f | 3492 | |
22239a37 NIS |
3493 | #ifdef PERL_GLOBAL_STRUCT |
3494 | struct perl_vars * | |
864dbfa3 | 3495 | Perl_GetVars(pTHX) |
22239a37 | 3496 | { |
533c011a | 3497 | return &PL_Vars; |
22239a37 | 3498 | } |
31fb1209 NIS |
3499 | #endif |
3500 | ||
3501 | char ** | |
864dbfa3 | 3502 | Perl_get_op_names(pTHX) |
31fb1209 | 3503 | { |
22c35a8c | 3504 | return PL_op_name; |
31fb1209 NIS |
3505 | } |
3506 | ||
3507 | char ** | |
864dbfa3 | 3508 | Perl_get_op_descs(pTHX) |
31fb1209 | 3509 | { |
22c35a8c | 3510 | return PL_op_desc; |
31fb1209 | 3511 | } |
9e6b2b00 GS |
3512 | |
3513 | char * | |
864dbfa3 | 3514 | Perl_get_no_modify(pTHX) |
9e6b2b00 | 3515 | { |
22c35a8c | 3516 | return (char*)PL_no_modify; |
9e6b2b00 GS |
3517 | } |
3518 | ||
3519 | U32 * | |
864dbfa3 | 3520 | Perl_get_opargs(pTHX) |
9e6b2b00 | 3521 | { |
22c35a8c | 3522 | return PL_opargs; |
9e6b2b00 | 3523 | } |
51aa15f3 | 3524 | |
0cb96387 GS |
3525 | PPADDR_t* |
3526 | Perl_get_ppaddr(pTHX) | |
3527 | { | |
3528 | return &PL_ppaddr; | |
3529 | } | |
3530 | ||
a6c40364 GS |
3531 | #ifndef HAS_GETENV_LEN |
3532 | char * | |
864dbfa3 | 3533 | Perl_getenv_len(pTHX_ char *env_elem, unsigned long *len) |
a6c40364 GS |
3534 | { |
3535 | char *env_trans = PerlEnv_getenv(env_elem); | |
3536 | if (env_trans) | |
3537 | *len = strlen(env_trans); | |
3538 | return env_trans; | |
f675dbe5 CB |
3539 | } |
3540 | #endif | |
3541 | ||
dc9e4912 GS |
3542 | |
3543 | MGVTBL* | |
864dbfa3 | 3544 | Perl_get_vtbl(pTHX_ int vtbl_id) |
dc9e4912 GS |
3545 | { |
3546 | MGVTBL* result = Null(MGVTBL*); | |
3547 | ||
3548 | switch(vtbl_id) { | |
3549 | case want_vtbl_sv: | |
3550 | result = &PL_vtbl_sv; | |
3551 | break; | |
3552 | case want_vtbl_env: | |
3553 | result = &PL_vtbl_env; | |
3554 | break; | |
3555 | case want_vtbl_envelem: | |
3556 | result = &PL_vtbl_envelem; | |
3557 | break; | |
3558 | case want_vtbl_sig: | |
3559 | result = &PL_vtbl_sig; | |
3560 | break; | |
3561 | case want_vtbl_sigelem: | |
3562 | result = &PL_vtbl_sigelem; | |
3563 | break; | |
3564 | case want_vtbl_pack: | |
3565 | result = &PL_vtbl_pack; | |
3566 | break; | |
3567 | case want_vtbl_packelem: | |
3568 | result = &PL_vtbl_packelem; | |
3569 | break; | |
3570 | case want_vtbl_dbline: | |
3571 | result = &PL_vtbl_dbline; | |
3572 | break; | |
3573 | case want_vtbl_isa: | |
3574 | result = &PL_vtbl_isa; | |
3575 | break; | |
3576 | case want_vtbl_isaelem: | |
3577 | result = &PL_vtbl_isaelem; | |
3578 | break; | |
3579 | case want_vtbl_arylen: | |
3580 | result = &PL_vtbl_arylen; | |
3581 | break; | |
3582 | case want_vtbl_glob: | |
3583 | result = &PL_vtbl_glob; | |
3584 | break; | |
3585 | case want_vtbl_mglob: | |
3586 | result = &PL_vtbl_mglob; | |
3587 | break; | |
3588 | case want_vtbl_nkeys: | |
3589 | result = &PL_vtbl_nkeys; | |
3590 | break; | |
3591 | case want_vtbl_taint: | |
3592 | result = &PL_vtbl_taint; | |
3593 | break; | |
3594 | case want_vtbl_substr: | |
3595 | result = &PL_vtbl_substr; | |
3596 | break; | |
3597 | case want_vtbl_vec: | |
3598 | result = &PL_vtbl_vec; | |
3599 | break; | |
3600 | case want_vtbl_pos: | |
3601 | result = &PL_vtbl_pos; | |
3602 | break; | |
3603 | case want_vtbl_bm: | |
3604 | result = &PL_vtbl_bm; | |
3605 | break; | |
3606 | case want_vtbl_fm: | |
3607 | result = &PL_vtbl_fm; | |
3608 | break; | |
3609 | case want_vtbl_uvar: | |
3610 | result = &PL_vtbl_uvar; | |
3611 | break; | |
3612 | #ifdef USE_THREADS | |
3613 | case want_vtbl_mutex: | |
3614 | result = &PL_vtbl_mutex; | |
3615 | break; | |
3616 | #endif | |
3617 | case want_vtbl_defelem: | |
3618 | result = &PL_vtbl_defelem; | |
3619 | break; | |
3620 | case want_vtbl_regexp: | |
3621 | result = &PL_vtbl_regexp; | |
3622 | break; | |
3623 | case want_vtbl_regdata: | |
3624 | result = &PL_vtbl_regdata; | |
3625 | break; | |
3626 | case want_vtbl_regdatum: | |
3627 | result = &PL_vtbl_regdatum; | |
3628 | break; | |
3c90161d | 3629 | #ifdef USE_LOCALE_COLLATE |
dc9e4912 GS |
3630 | case want_vtbl_collxfrm: |
3631 | result = &PL_vtbl_collxfrm; | |
3632 | break; | |
3c90161d | 3633 | #endif |
dc9e4912 GS |
3634 | case want_vtbl_amagic: |
3635 | result = &PL_vtbl_amagic; | |
3636 | break; | |
3637 | case want_vtbl_amagicelem: | |
3638 | result = &PL_vtbl_amagicelem; | |
3639 | break; | |
810b8aa5 GS |
3640 | case want_vtbl_backref: |
3641 | result = &PL_vtbl_backref; | |
3642 | break; | |
dc9e4912 GS |
3643 | } |
3644 | return result; | |
3645 | } | |
3646 | ||