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