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