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