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