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