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