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