Commit | Line | Data |
---|---|---|
a0d0e21e | 1 | /* util.c |
a687059c | 2 | * |
1129b882 NC |
3 | * Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, |
4 | * 2002, 2003, 2004, 2005, 2006, 2007, 2008 by Larry Wall and others | |
a687059c | 5 | * |
d48672a2 LW |
6 | * You may distribute under the terms of either the GNU General Public |
7 | * License or the Artistic License, as specified in the README file. | |
8d063cd8 | 8 | * |
8d063cd8 | 9 | */ |
a0d0e21e LW |
10 | |
11 | /* | |
4ac71550 TC |
12 | * 'Very useful, no doubt, that was to Saruman; yet it seems that he was |
13 | * not content.' --Gandalf to Pippin | |
14 | * | |
cdad3b53 | 15 | * [p.598 of _The Lord of the Rings_, III/xi: "The PalantÃr"] |
a0d0e21e | 16 | */ |
8d063cd8 | 17 | |
166f8a29 DM |
18 | /* This file contains assorted utility routines. |
19 | * Which is a polite way of saying any stuff that people couldn't think of | |
20 | * a better place for. Amongst other things, it includes the warning and | |
21 | * dieing stuff, plus wrappers for malloc code. | |
22 | */ | |
23 | ||
8d063cd8 | 24 | #include "EXTERN.h" |
864dbfa3 | 25 | #define PERL_IN_UTIL_C |
8d063cd8 | 26 | #include "perl.h" |
7dc86639 | 27 | #include "reentr.h" |
62b28dd9 | 28 | |
97cb92d6 | 29 | #if defined(USE_PERLIO) |
2e0cfa16 | 30 | #include "perliol.h" /* For PerlIOUnix_refcnt */ |
6f408c34 | 31 | #endif |
2e0cfa16 | 32 | |
64ca3a65 | 33 | #ifndef PERL_MICRO |
a687059c | 34 | #include <signal.h> |
36477c24 | 35 | #ifndef SIG_ERR |
36 | # define SIG_ERR ((Sighandler_t) -1) | |
37 | #endif | |
64ca3a65 | 38 | #endif |
36477c24 | 39 | |
3be8f094 TC |
40 | #include <math.h> |
41 | #include <stdlib.h> | |
42 | ||
172d2248 OS |
43 | #ifdef __Lynx__ |
44 | /* Missing protos on LynxOS */ | |
45 | int putenv(char *); | |
46 | #endif | |
47 | ||
40262ff4 AB |
48 | #ifdef __amigaos__ |
49 | # include "amigaos4/amigaio.h" | |
50 | #endif | |
51 | ||
868439a2 JH |
52 | #ifdef HAS_SELECT |
53 | # ifdef I_SYS_SELECT | |
54 | # include <sys/select.h> | |
55 | # endif | |
56 | #endif | |
57 | ||
470dd224 | 58 | #ifdef USE_C_BACKTRACE |
0762e42f JH |
59 | # ifdef I_BFD |
60 | # define USE_BFD | |
61 | # ifdef PERL_DARWIN | |
62 | # undef USE_BFD /* BFD is useless in OS X. */ | |
63 | # endif | |
64 | # ifdef USE_BFD | |
65 | # include <bfd.h> | |
66 | # endif | |
67 | # endif | |
470dd224 JH |
68 | # ifdef I_DLFCN |
69 | # include <dlfcn.h> | |
70 | # endif | |
71 | # ifdef I_EXECINFO | |
72 | # include <execinfo.h> | |
73 | # endif | |
74 | #endif | |
75 | ||
b001a0d1 FC |
76 | #ifdef PERL_DEBUG_READONLY_COW |
77 | # include <sys/mman.h> | |
78 | #endif | |
79 | ||
8d063cd8 | 80 | #define FLUSH |
8d063cd8 | 81 | |
a687059c LW |
82 | /* NOTE: Do not call the next three routines directly. Use the macros |
83 | * in handy.h, so that we can easily redefine everything to do tracking of | |
84 | * allocated hunks back to the original New to track down any memory leaks. | |
20cec16a | 85 | * XXX This advice seems to be widely ignored :-( --AD August 1996. |
a687059c LW |
86 | */ |
87 | ||
79a92154 | 88 | #if defined (DEBUGGING) || defined(PERL_IMPLICIT_SYS) || defined (PERL_TRACK_MEMPOOL) |
1f4d2d4e NC |
89 | # define ALWAYS_NEED_THX |
90 | #endif | |
91 | ||
b001a0d1 FC |
92 | #if defined(PERL_TRACK_MEMPOOL) && defined(PERL_DEBUG_READONLY_COW) |
93 | static void | |
94 | S_maybe_protect_rw(pTHX_ struct perl_memory_debug_header *header) | |
95 | { | |
96 | if (header->readonly | |
97 | && mprotect(header, header->size, PROT_READ|PROT_WRITE)) | |
98 | Perl_warn(aTHX_ "mprotect for COW string %p %lu failed with %d", | |
99 | header, header->size, errno); | |
100 | } | |
101 | ||
102 | static void | |
103 | S_maybe_protect_ro(pTHX_ struct perl_memory_debug_header *header) | |
104 | { | |
105 | if (header->readonly | |
106 | && mprotect(header, header->size, PROT_READ)) | |
107 | Perl_warn(aTHX_ "mprotect RW for COW string %p %lu failed with %d", | |
108 | header, header->size, errno); | |
109 | } | |
110 | # define maybe_protect_rw(foo) S_maybe_protect_rw(aTHX_ foo) | |
111 | # define maybe_protect_ro(foo) S_maybe_protect_ro(aTHX_ foo) | |
112 | #else | |
113 | # define maybe_protect_rw(foo) NOOP | |
114 | # define maybe_protect_ro(foo) NOOP | |
115 | #endif | |
116 | ||
3f07c2bc FC |
117 | #if defined(PERL_TRACK_MEMPOOL) || defined(PERL_DEBUG_READONLY_COW) |
118 | /* Use memory_debug_header */ | |
119 | # define USE_MDH | |
120 | # if (defined(PERL_POISON) && defined(PERL_TRACK_MEMPOOL)) \ | |
121 | || defined(PERL_DEBUG_READONLY_COW) | |
122 | # define MDH_HAS_SIZE | |
123 | # endif | |
124 | #endif | |
125 | ||
26fa51c3 AMS |
126 | /* paranoid version of system's malloc() */ |
127 | ||
bd4080b3 | 128 | Malloc_t |
4f63d024 | 129 | Perl_safesysmalloc(MEM_SIZE size) |
8d063cd8 | 130 | { |
1f4d2d4e | 131 | #ifdef ALWAYS_NEED_THX |
54aff467 | 132 | dTHX; |
0cb20dae | 133 | #endif |
bd4080b3 | 134 | Malloc_t ptr; |
9efda33a TC |
135 | |
136 | #ifdef USE_MDH | |
137 | if (size + PERL_MEMORY_DEBUG_HEADER_SIZE < size) | |
138 | goto out_of_memory; | |
a78adc84 | 139 | size += PERL_MEMORY_DEBUG_HEADER_SIZE; |
9efda33a | 140 | #endif |
34de22dd | 141 | #ifdef DEBUGGING |
03c5309f | 142 | if ((SSize_t)size < 0) |
5637ef5b | 143 | Perl_croak_nocontext("panic: malloc, size=%"UVuf, (UV) size); |
34de22dd | 144 | #endif |
b001a0d1 FC |
145 | if (!size) size = 1; /* malloc(0) is NASTY on our system */ |
146 | #ifdef PERL_DEBUG_READONLY_COW | |
147 | if ((ptr = mmap(0, size, PROT_READ|PROT_WRITE, | |
148 | MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED) { | |
149 | perror("mmap failed"); | |
150 | abort(); | |
151 | } | |
152 | #else | |
153 | ptr = (Malloc_t)PerlMem_malloc(size?size:1); | |
154 | #endif | |
da927450 | 155 | PERL_ALLOC_CHECK(ptr); |
bd61b366 | 156 | if (ptr != NULL) { |
3f07c2bc | 157 | #ifdef USE_MDH |
7cb608b5 NC |
158 | struct perl_memory_debug_header *const header |
159 | = (struct perl_memory_debug_header *)ptr; | |
9a083ecf NC |
160 | #endif |
161 | ||
162 | #ifdef PERL_POISON | |
7e337ee0 | 163 | PoisonNew(((char *)ptr), size, char); |
9a083ecf | 164 | #endif |
7cb608b5 | 165 | |
9a083ecf | 166 | #ifdef PERL_TRACK_MEMPOOL |
7cb608b5 NC |
167 | header->interpreter = aTHX; |
168 | /* Link us into the list. */ | |
169 | header->prev = &PL_memory_debug_header; | |
170 | header->next = PL_memory_debug_header.next; | |
171 | PL_memory_debug_header.next = header; | |
b001a0d1 | 172 | maybe_protect_rw(header->next); |
7cb608b5 | 173 | header->next->prev = header; |
b001a0d1 FC |
174 | maybe_protect_ro(header->next); |
175 | # ifdef PERL_DEBUG_READONLY_COW | |
176 | header->readonly = 0; | |
cd1541b2 | 177 | # endif |
e8dda941 | 178 | #endif |
3f07c2bc | 179 | #ifdef MDH_HAS_SIZE |
b001a0d1 FC |
180 | header->size = size; |
181 | #endif | |
b033d668 | 182 | ptr = (Malloc_t)((char*)ptr+PERL_MEMORY_DEBUG_HEADER_SIZE); |
5dfff8f3 | 183 | DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%"UVxf": (%05ld) malloc %ld bytes\n",PTR2UV(ptr),(long)PL_an++,(long)size)); |
b033d668 DD |
184 | |
185 | } | |
8d063cd8 | 186 | else { |
296f0d56 | 187 | #ifdef USE_MDH |
9efda33a | 188 | out_of_memory: |
296f0d56 TC |
189 | #endif |
190 | { | |
191 | #ifndef ALWAYS_NEED_THX | |
192 | dTHX; | |
193 | #endif | |
194 | if (PL_nomemok) | |
195 | ptr = NULL; | |
196 | else | |
197 | croak_no_mem(); | |
198 | } | |
8d063cd8 | 199 | } |
b033d668 | 200 | return ptr; |
8d063cd8 LW |
201 | } |
202 | ||
f2517201 | 203 | /* paranoid version of system's realloc() */ |
8d063cd8 | 204 | |
bd4080b3 | 205 | Malloc_t |
4f63d024 | 206 | Perl_safesysrealloc(Malloc_t where,MEM_SIZE size) |
8d063cd8 | 207 | { |
1f4d2d4e | 208 | #ifdef ALWAYS_NEED_THX |
54aff467 | 209 | dTHX; |
0cb20dae | 210 | #endif |
bd4080b3 | 211 | Malloc_t ptr; |
b001a0d1 FC |
212 | #ifdef PERL_DEBUG_READONLY_COW |
213 | const MEM_SIZE oldsize = where | |
a78adc84 | 214 | ? ((struct perl_memory_debug_header *)((char *)where - PERL_MEMORY_DEBUG_HEADER_SIZE))->size |
b001a0d1 FC |
215 | : 0; |
216 | #endif | |
9a34ef1d | 217 | #if !defined(STANDARD_C) && !defined(HAS_REALLOC_PROTOTYPE) && !defined(PERL_MICRO) |
6ad3d225 | 218 | Malloc_t PerlMem_realloc(); |
ecfc5424 | 219 | #endif /* !defined(STANDARD_C) && !defined(HAS_REALLOC_PROTOTYPE) */ |
8d063cd8 | 220 | |
7614df0c | 221 | if (!size) { |
f2517201 | 222 | safesysfree(where); |
b033d668 | 223 | ptr = NULL; |
7614df0c | 224 | } |
b033d668 DD |
225 | else if (!where) { |
226 | ptr = safesysmalloc(size); | |
227 | } | |
228 | else { | |
3f07c2bc | 229 | #ifdef USE_MDH |
b033d668 | 230 | where = (Malloc_t)((char*)where-PERL_MEMORY_DEBUG_HEADER_SIZE); |
9efda33a TC |
231 | if (size + PERL_MEMORY_DEBUG_HEADER_SIZE < size) |
232 | goto out_of_memory; | |
b033d668 DD |
233 | size += PERL_MEMORY_DEBUG_HEADER_SIZE; |
234 | { | |
235 | struct perl_memory_debug_header *const header | |
236 | = (struct perl_memory_debug_header *)where; | |
7cb608b5 | 237 | |
b001a0d1 | 238 | # ifdef PERL_TRACK_MEMPOOL |
b033d668 DD |
239 | if (header->interpreter != aTHX) { |
240 | Perl_croak_nocontext("panic: realloc from wrong pool, %p!=%p", | |
241 | header->interpreter, aTHX); | |
242 | } | |
243 | assert(header->next->prev == header); | |
244 | assert(header->prev->next == header); | |
cd1541b2 | 245 | # ifdef PERL_POISON |
b033d668 DD |
246 | if (header->size > size) { |
247 | const MEM_SIZE freed_up = header->size - size; | |
248 | char *start_of_freed = ((char *)where) + size; | |
249 | PoisonFree(start_of_freed, freed_up, char); | |
250 | } | |
cd1541b2 | 251 | # endif |
b001a0d1 | 252 | # endif |
3f07c2bc | 253 | # ifdef MDH_HAS_SIZE |
b033d668 | 254 | header->size = size; |
b001a0d1 | 255 | # endif |
b033d668 | 256 | } |
e8dda941 | 257 | #endif |
34de22dd | 258 | #ifdef DEBUGGING |
b033d668 DD |
259 | if ((SSize_t)size < 0) |
260 | Perl_croak_nocontext("panic: realloc, size=%"UVuf, (UV)size); | |
34de22dd | 261 | #endif |
b001a0d1 | 262 | #ifdef PERL_DEBUG_READONLY_COW |
b033d668 DD |
263 | if ((ptr = mmap(0, size, PROT_READ|PROT_WRITE, |
264 | MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED) { | |
265 | perror("mmap failed"); | |
266 | abort(); | |
267 | } | |
268 | Copy(where,ptr,oldsize < size ? oldsize : size,char); | |
269 | if (munmap(where, oldsize)) { | |
270 | perror("munmap failed"); | |
271 | abort(); | |
272 | } | |
b001a0d1 | 273 | #else |
b033d668 | 274 | ptr = (Malloc_t)PerlMem_realloc(where,size); |
b001a0d1 | 275 | #endif |
b033d668 | 276 | PERL_ALLOC_CHECK(ptr); |
a1d180c4 | 277 | |
4fd0a9b8 NC |
278 | /* MUST do this fixup first, before doing ANYTHING else, as anything else |
279 | might allocate memory/free/move memory, and until we do the fixup, it | |
280 | may well be chasing (and writing to) free memory. */ | |
b033d668 | 281 | if (ptr != NULL) { |
b001a0d1 | 282 | #ifdef PERL_TRACK_MEMPOOL |
b033d668 DD |
283 | struct perl_memory_debug_header *const header |
284 | = (struct perl_memory_debug_header *)ptr; | |
7cb608b5 | 285 | |
9a083ecf | 286 | # ifdef PERL_POISON |
b033d668 DD |
287 | if (header->size < size) { |
288 | const MEM_SIZE fresh = size - header->size; | |
289 | char *start_of_fresh = ((char *)ptr) + size; | |
290 | PoisonNew(start_of_fresh, fresh, char); | |
291 | } | |
9a083ecf NC |
292 | # endif |
293 | ||
b033d668 DD |
294 | maybe_protect_rw(header->next); |
295 | header->next->prev = header; | |
296 | maybe_protect_ro(header->next); | |
297 | maybe_protect_rw(header->prev); | |
298 | header->prev->next = header; | |
299 | maybe_protect_ro(header->prev); | |
b001a0d1 | 300 | #endif |
b033d668 DD |
301 | ptr = (Malloc_t)((char*)ptr+PERL_MEMORY_DEBUG_HEADER_SIZE); |
302 | } | |
4fd0a9b8 NC |
303 | |
304 | /* In particular, must do that fixup above before logging anything via | |
305 | *printf(), as it can reallocate memory, which can cause SEGVs. */ | |
306 | ||
b033d668 DD |
307 | DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%"UVxf": (%05ld) rfree\n",PTR2UV(where),(long)PL_an++)); |
308 | DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%"UVxf": (%05ld) realloc %ld bytes\n",PTR2UV(ptr),(long)PL_an++,(long)size)); | |
4fd0a9b8 | 309 | |
b033d668 | 310 | if (ptr == NULL) { |
296f0d56 | 311 | #ifdef USE_MDH |
9efda33a | 312 | out_of_memory: |
296f0d56 TC |
313 | #endif |
314 | { | |
315 | #ifndef ALWAYS_NEED_THX | |
316 | dTHX; | |
317 | #endif | |
318 | if (PL_nomemok) | |
319 | ptr = NULL; | |
320 | else | |
321 | croak_no_mem(); | |
322 | } | |
0cb20dae | 323 | } |
8d063cd8 | 324 | } |
b033d668 | 325 | return ptr; |
8d063cd8 LW |
326 | } |
327 | ||
f2517201 | 328 | /* safe version of system's free() */ |
8d063cd8 | 329 | |
54310121 | 330 | Free_t |
4f63d024 | 331 | Perl_safesysfree(Malloc_t where) |
8d063cd8 | 332 | { |
79a92154 | 333 | #ifdef ALWAYS_NEED_THX |
54aff467 | 334 | dTHX; |
155aba94 | 335 | #endif |
97835f67 | 336 | DEBUG_m( PerlIO_printf(Perl_debug_log, "0x%"UVxf": (%05ld) free\n",PTR2UV(where),(long)PL_an++)); |
378cc40b | 337 | if (where) { |
3f07c2bc | 338 | #ifdef USE_MDH |
6edcbed6 | 339 | Malloc_t where_intrn = (Malloc_t)((char*)where-PERL_MEMORY_DEBUG_HEADER_SIZE); |
cd1541b2 | 340 | { |
7cb608b5 | 341 | struct perl_memory_debug_header *const header |
6edcbed6 | 342 | = (struct perl_memory_debug_header *)where_intrn; |
7cb608b5 | 343 | |
3f07c2bc | 344 | # ifdef MDH_HAS_SIZE |
b001a0d1 FC |
345 | const MEM_SIZE size = header->size; |
346 | # endif | |
347 | # ifdef PERL_TRACK_MEMPOOL | |
7cb608b5 | 348 | if (header->interpreter != aTHX) { |
5637ef5b NC |
349 | Perl_croak_nocontext("panic: free from wrong pool, %p!=%p", |
350 | header->interpreter, aTHX); | |
7cb608b5 NC |
351 | } |
352 | if (!header->prev) { | |
cd1541b2 NC |
353 | Perl_croak_nocontext("panic: duplicate free"); |
354 | } | |
5637ef5b NC |
355 | if (!(header->next)) |
356 | Perl_croak_nocontext("panic: bad free, header->next==NULL"); | |
357 | if (header->next->prev != header || header->prev->next != header) { | |
358 | Perl_croak_nocontext("panic: bad free, ->next->prev=%p, " | |
359 | "header=%p, ->prev->next=%p", | |
360 | header->next->prev, header, | |
361 | header->prev->next); | |
cd1541b2 | 362 | } |
7cb608b5 | 363 | /* Unlink us from the chain. */ |
b001a0d1 | 364 | maybe_protect_rw(header->next); |
7cb608b5 | 365 | header->next->prev = header->prev; |
b001a0d1 FC |
366 | maybe_protect_ro(header->next); |
367 | maybe_protect_rw(header->prev); | |
7cb608b5 | 368 | header->prev->next = header->next; |
b001a0d1 FC |
369 | maybe_protect_ro(header->prev); |
370 | maybe_protect_rw(header); | |
7cb608b5 | 371 | # ifdef PERL_POISON |
6edcbed6 | 372 | PoisonNew(where_intrn, size, char); |
cd1541b2 | 373 | # endif |
7cb608b5 NC |
374 | /* Trigger the duplicate free warning. */ |
375 | header->next = NULL; | |
b001a0d1 FC |
376 | # endif |
377 | # ifdef PERL_DEBUG_READONLY_COW | |
6edcbed6 | 378 | if (munmap(where_intrn, size)) { |
b001a0d1 FC |
379 | perror("munmap failed"); |
380 | abort(); | |
381 | } | |
382 | # endif | |
7cb608b5 | 383 | } |
6edcbed6 DD |
384 | #else |
385 | Malloc_t where_intrn = where; | |
386 | #endif /* USE_MDH */ | |
b001a0d1 | 387 | #ifndef PERL_DEBUG_READONLY_COW |
6edcbed6 | 388 | PerlMem_free(where_intrn); |
b001a0d1 | 389 | #endif |
378cc40b | 390 | } |
8d063cd8 LW |
391 | } |
392 | ||
f2517201 | 393 | /* safe version of system's calloc() */ |
1050c9ca | 394 | |
bd4080b3 | 395 | Malloc_t |
4f63d024 | 396 | Perl_safesyscalloc(MEM_SIZE count, MEM_SIZE size) |
1050c9ca | 397 | { |
1f4d2d4e | 398 | #ifdef ALWAYS_NEED_THX |
54aff467 | 399 | dTHX; |
0cb20dae | 400 | #endif |
bd4080b3 | 401 | Malloc_t ptr; |
3f07c2bc | 402 | #if defined(USE_MDH) || defined(DEBUGGING) |
ad7244db | 403 | MEM_SIZE total_size = 0; |
4b1123b9 | 404 | #endif |
1050c9ca | 405 | |
ad7244db | 406 | /* Even though calloc() for zero bytes is strange, be robust. */ |
4b1123b9 | 407 | if (size && (count <= MEM_SIZE_MAX / size)) { |
3f07c2bc | 408 | #if defined(USE_MDH) || defined(DEBUGGING) |
ad7244db | 409 | total_size = size * count; |
4b1123b9 NC |
410 | #endif |
411 | } | |
ad7244db | 412 | else |
d1decf2b | 413 | croak_memory_wrap(); |
3f07c2bc | 414 | #ifdef USE_MDH |
a78adc84 DM |
415 | if (PERL_MEMORY_DEBUG_HEADER_SIZE <= MEM_SIZE_MAX - (MEM_SIZE)total_size) |
416 | total_size += PERL_MEMORY_DEBUG_HEADER_SIZE; | |
ad7244db | 417 | else |
d1decf2b | 418 | croak_memory_wrap(); |
ad7244db | 419 | #endif |
1050c9ca | 420 | #ifdef DEBUGGING |
03c5309f | 421 | if ((SSize_t)size < 0 || (SSize_t)count < 0) |
5637ef5b NC |
422 | Perl_croak_nocontext("panic: calloc, size=%"UVuf", count=%"UVuf, |
423 | (UV)size, (UV)count); | |
1050c9ca | 424 | #endif |
b001a0d1 FC |
425 | #ifdef PERL_DEBUG_READONLY_COW |
426 | if ((ptr = mmap(0, total_size ? total_size : 1, PROT_READ|PROT_WRITE, | |
427 | MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED) { | |
428 | perror("mmap failed"); | |
429 | abort(); | |
430 | } | |
431 | #elif defined(PERL_TRACK_MEMPOOL) | |
e1a95402 NC |
432 | /* Have to use malloc() because we've added some space for our tracking |
433 | header. */ | |
ad7244db JH |
434 | /* malloc(0) is non-portable. */ |
435 | ptr = (Malloc_t)PerlMem_malloc(total_size ? total_size : 1); | |
e1a95402 NC |
436 | #else |
437 | /* Use calloc() because it might save a memset() if the memory is fresh | |
438 | and clean from the OS. */ | |
ad7244db JH |
439 | if (count && size) |
440 | ptr = (Malloc_t)PerlMem_calloc(count, size); | |
441 | else /* calloc(0) is non-portable. */ | |
442 | ptr = (Malloc_t)PerlMem_calloc(count ? count : 1, size ? size : 1); | |
e8dda941 | 443 | #endif |
da927450 | 444 | PERL_ALLOC_CHECK(ptr); |
e1a95402 | 445 | DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%"UVxf": (%05ld) calloc %ld x %ld bytes\n",PTR2UV(ptr),(long)PL_an++,(long)count,(long)total_size)); |
bd61b366 | 446 | if (ptr != NULL) { |
3f07c2bc | 447 | #ifdef USE_MDH |
7cb608b5 NC |
448 | { |
449 | struct perl_memory_debug_header *const header | |
450 | = (struct perl_memory_debug_header *)ptr; | |
451 | ||
b001a0d1 | 452 | # ifndef PERL_DEBUG_READONLY_COW |
e1a95402 | 453 | memset((void*)ptr, 0, total_size); |
b001a0d1 FC |
454 | # endif |
455 | # ifdef PERL_TRACK_MEMPOOL | |
7cb608b5 NC |
456 | header->interpreter = aTHX; |
457 | /* Link us into the list. */ | |
458 | header->prev = &PL_memory_debug_header; | |
459 | header->next = PL_memory_debug_header.next; | |
460 | PL_memory_debug_header.next = header; | |
b001a0d1 | 461 | maybe_protect_rw(header->next); |
7cb608b5 | 462 | header->next->prev = header; |
b001a0d1 FC |
463 | maybe_protect_ro(header->next); |
464 | # ifdef PERL_DEBUG_READONLY_COW | |
465 | header->readonly = 0; | |
466 | # endif | |
467 | # endif | |
3f07c2bc | 468 | # ifdef MDH_HAS_SIZE |
e1a95402 | 469 | header->size = total_size; |
cd1541b2 | 470 | # endif |
a78adc84 | 471 | ptr = (Malloc_t)((char*)ptr+PERL_MEMORY_DEBUG_HEADER_SIZE); |
7cb608b5 | 472 | } |
e8dda941 | 473 | #endif |
1050c9ca | 474 | return ptr; |
475 | } | |
0cb20dae | 476 | else { |
1f4d2d4e | 477 | #ifndef ALWAYS_NEED_THX |
0cb20dae NC |
478 | dTHX; |
479 | #endif | |
480 | if (PL_nomemok) | |
481 | return NULL; | |
4cbe3a7d | 482 | croak_no_mem(); |
0cb20dae | 483 | } |
1050c9ca | 484 | } |
485 | ||
cae6d0e5 GS |
486 | /* These must be defined when not using Perl's malloc for binary |
487 | * compatibility */ | |
488 | ||
489 | #ifndef MYMALLOC | |
490 | ||
491 | Malloc_t Perl_malloc (MEM_SIZE nbytes) | |
492 | { | |
20b7effb JH |
493 | #ifdef PERL_IMPLICIT_SYS |
494 | dTHX; | |
495 | #endif | |
077a72a9 | 496 | return (Malloc_t)PerlMem_malloc(nbytes); |
cae6d0e5 GS |
497 | } |
498 | ||
499 | Malloc_t Perl_calloc (MEM_SIZE elements, MEM_SIZE size) | |
500 | { | |
20b7effb JH |
501 | #ifdef PERL_IMPLICIT_SYS |
502 | dTHX; | |
503 | #endif | |
077a72a9 | 504 | return (Malloc_t)PerlMem_calloc(elements, size); |
cae6d0e5 GS |
505 | } |
506 | ||
507 | Malloc_t Perl_realloc (Malloc_t where, MEM_SIZE nbytes) | |
508 | { | |
20b7effb JH |
509 | #ifdef PERL_IMPLICIT_SYS |
510 | dTHX; | |
511 | #endif | |
077a72a9 | 512 | return (Malloc_t)PerlMem_realloc(where, nbytes); |
cae6d0e5 GS |
513 | } |
514 | ||
515 | Free_t Perl_mfree (Malloc_t where) | |
516 | { | |
20b7effb JH |
517 | #ifdef PERL_IMPLICIT_SYS |
518 | dTHX; | |
519 | #endif | |
cae6d0e5 GS |
520 | PerlMem_free(where); |
521 | } | |
522 | ||
523 | #endif | |
524 | ||
8d063cd8 LW |
525 | /* copy a string up to some (non-backslashed) delimiter, if any */ |
526 | ||
527 | char * | |
5aaab254 | 528 | Perl_delimcpy(char *to, const char *toend, const char *from, const char *fromend, int delim, I32 *retlen) |
8d063cd8 | 529 | { |
eb578fdb | 530 | I32 tolen; |
35da51f7 | 531 | |
7918f24d NC |
532 | PERL_ARGS_ASSERT_DELIMCPY; |
533 | ||
fc36a67e | 534 | for (tolen = 0; from < fromend; from++, tolen++) { |
378cc40b | 535 | if (*from == '\\') { |
35da51f7 | 536 | if (from[1] != delim) { |
fc36a67e | 537 | if (to < toend) |
538 | *to++ = *from; | |
539 | tolen++; | |
fc36a67e | 540 | } |
35da51f7 | 541 | from++; |
378cc40b | 542 | } |
bedebaa5 | 543 | else if (*from == delim) |
8d063cd8 | 544 | break; |
fc36a67e | 545 | if (to < toend) |
546 | *to++ = *from; | |
8d063cd8 | 547 | } |
bedebaa5 CS |
548 | if (to < toend) |
549 | *to = '\0'; | |
fc36a67e | 550 | *retlen = tolen; |
73d840c0 | 551 | return (char *)from; |
8d063cd8 LW |
552 | } |
553 | ||
554 | /* return ptr to little string in big string, NULL if not found */ | |
378cc40b | 555 | /* This routine was donated by Corey Satten. */ |
8d063cd8 LW |
556 | |
557 | char * | |
5aaab254 | 558 | Perl_instr(const char *big, const char *little) |
378cc40b | 559 | { |
378cc40b | 560 | |
7918f24d NC |
561 | PERL_ARGS_ASSERT_INSTR; |
562 | ||
5d1d68e2 | 563 | return strstr((char*)big, (char*)little); |
378cc40b | 564 | } |
8d063cd8 | 565 | |
fcfc5a27 KW |
566 | /* |
567 | =head1 Miscellaneous Functions | |
568 | ||
569 | =for apidoc Am|char *|ninstr|char * big|char * bigend|char * little|char * little_end | |
570 | ||
571 | Find the first (leftmost) occurrence of a sequence of bytes within another | |
572 | sequence. This is the Perl version of C<strstr()>, extended to handle | |
573 | arbitrary sequences, potentially containing embedded C<NUL> characters (C<NUL> | |
574 | is what the initial C<n> in the function name stands for; some systems have an | |
575 | equivalent, C<memmem()>, but with a somewhat different API). | |
576 | ||
577 | Another way of thinking about this function is finding a needle in a haystack. | |
578 | C<big> points to the first byte in the haystack. C<big_end> points to one byte | |
579 | beyond the final byte in the haystack. C<little> points to the first byte in | |
580 | the needle. C<little_end> points to one byte beyond the final byte in the | |
581 | needle. All the parameters must be non-C<NULL>. | |
582 | ||
583 | The function returns C<NULL> if there is no occurrence of C<little> within | |
584 | C<big>. If C<little> is the empty string, C<big> is returned. | |
585 | ||
586 | Because this function operates at the byte level, and because of the inherent | |
587 | characteristics of UTF-8 (or UTF-EBCDIC), it will work properly if both the | |
588 | needle and the haystack are strings with the same UTF-8ness, but not if the | |
589 | UTF-8ness differs. | |
590 | ||
591 | =cut | |
592 | ||
593 | */ | |
a687059c LW |
594 | |
595 | char * | |
04c9e624 | 596 | Perl_ninstr(const char *big, const char *bigend, const char *little, const char *lend) |
8d063cd8 | 597 | { |
7918f24d | 598 | PERL_ARGS_ASSERT_NINSTR; |
4c8626be GA |
599 | if (little >= lend) |
600 | return (char*)big; | |
601 | { | |
8ba22ff4 | 602 | const char first = *little; |
4c8626be | 603 | const char *s, *x; |
8ba22ff4 | 604 | bigend -= lend - little++; |
4c8626be GA |
605 | OUTER: |
606 | while (big <= bigend) { | |
b0ca24ee JH |
607 | if (*big++ == first) { |
608 | for (x=big,s=little; s < lend; x++,s++) { | |
609 | if (*s != *x) | |
610 | goto OUTER; | |
611 | } | |
612 | return (char*)(big-1); | |
4c8626be | 613 | } |
4c8626be | 614 | } |
378cc40b | 615 | } |
bd61b366 | 616 | return NULL; |
a687059c LW |
617 | } |
618 | ||
fcfc5a27 KW |
619 | /* |
620 | =head1 Miscellaneous Functions | |
621 | ||
622 | =for apidoc Am|char *|rninstr|char * big|char * bigend|char * little|char * little_end | |
623 | ||
624 | Like C<L</ninstr>>, but instead finds the final (rightmost) occurrence of a | |
625 | sequence of bytes within another sequence, returning C<NULL> if there is no | |
626 | such occurrence. | |
627 | ||
628 | =cut | |
629 | ||
630 | */ | |
a687059c LW |
631 | |
632 | char * | |
5aaab254 | 633 | Perl_rninstr(const char *big, const char *bigend, const char *little, const char *lend) |
a687059c | 634 | { |
eb578fdb KW |
635 | const char *bigbeg; |
636 | const I32 first = *little; | |
637 | const char * const littleend = lend; | |
a687059c | 638 | |
7918f24d NC |
639 | PERL_ARGS_ASSERT_RNINSTR; |
640 | ||
260d78c9 | 641 | if (little >= littleend) |
08105a92 | 642 | return (char*)bigend; |
a687059c LW |
643 | bigbeg = big; |
644 | big = bigend - (littleend - little++); | |
645 | while (big >= bigbeg) { | |
eb578fdb | 646 | const char *s, *x; |
a687059c LW |
647 | if (*big-- != first) |
648 | continue; | |
649 | for (x=big+2,s=little; s < littleend; /**/ ) { | |
4fc877ac | 650 | if (*s != *x) |
a687059c | 651 | break; |
4fc877ac AL |
652 | else { |
653 | x++; | |
654 | s++; | |
a687059c LW |
655 | } |
656 | } | |
657 | if (s >= littleend) | |
08105a92 | 658 | return (char*)(big+1); |
378cc40b | 659 | } |
bd61b366 | 660 | return NULL; |
378cc40b | 661 | } |
a687059c | 662 | |
cf93c79d IZ |
663 | /* As a space optimization, we do not compile tables for strings of length |
664 | 0 and 1, and for strings of length 2 unless FBMcf_TAIL. These are | |
665 | special-cased in fbm_instr(). | |
666 | ||
667 | If FBMcf_TAIL, the table is created as if the string has a trailing \n. */ | |
668 | ||
954c1994 | 669 | /* |
ccfc67b7 JH |
670 | =head1 Miscellaneous Functions |
671 | ||
954c1994 GS |
672 | =for apidoc fbm_compile |
673 | ||
796b6530 | 674 | Analyses the string in order to make fast searches on it using C<fbm_instr()> |
954c1994 GS |
675 | -- the Boyer-Moore algorithm. |
676 | ||
677 | =cut | |
678 | */ | |
679 | ||
378cc40b | 680 | void |
7506f9c3 | 681 | Perl_fbm_compile(pTHX_ SV *sv, U32 flags) |
378cc40b | 682 | { |
eb578fdb | 683 | const U8 *s; |
ea725ce6 | 684 | STRLEN i; |
0b71040e | 685 | STRLEN len; |
79072805 | 686 | U32 frequency = 256; |
2bda37ba | 687 | MAGIC *mg; |
00cccd05 | 688 | PERL_DEB( STRLEN rarest = 0 ); |
79072805 | 689 | |
7918f24d NC |
690 | PERL_ARGS_ASSERT_FBM_COMPILE; |
691 | ||
948d2370 | 692 | if (isGV_with_GP(sv) || SvROK(sv)) |
4265b45d NC |
693 | return; |
694 | ||
9402563a NC |
695 | if (SvVALID(sv)) |
696 | return; | |
697 | ||
c517dc2b | 698 | if (flags & FBMcf_TAIL) { |
890ce7af | 699 | MAGIC * const mg = SvUTF8(sv) && SvMAGICAL(sv) ? mg_find(sv, PERL_MAGIC_utf8) : NULL; |
396482e1 | 700 | sv_catpvs(sv, "\n"); /* Taken into account in fbm_instr() */ |
c517dc2b JH |
701 | if (mg && mg->mg_len >= 0) |
702 | mg->mg_len++; | |
703 | } | |
11609d9c | 704 | if (!SvPOK(sv) || SvNIOKp(sv)) |
66379c06 FC |
705 | s = (U8*)SvPV_force_mutable(sv, len); |
706 | else s = (U8 *)SvPV_mutable(sv, len); | |
d1be9408 | 707 | if (len == 0) /* TAIL might be on a zero-length string. */ |
cf93c79d | 708 | return; |
c13a5c80 | 709 | SvUPGRADE(sv, SVt_PVMG); |
78d0cf80 | 710 | SvIOK_off(sv); |
8eeaf79a NC |
711 | SvNOK_off(sv); |
712 | SvVALID_on(sv); | |
2bda37ba NC |
713 | |
714 | /* "deep magic", the comment used to add. The use of MAGIC itself isn't | |
715 | really. MAGIC was originally added in 79072805bf63abe5 (perl 5.0 alpha 2) | |
716 | to call SvVALID_off() if the scalar was assigned to. | |
717 | ||
718 | The comment itself (and "deeper magic" below) date back to | |
719 | 378cc40b38293ffc (perl 2.0). "deep magic" was an annotation on | |
720 | str->str_pok |= 2; | |
721 | where the magic (presumably) was that the scalar had a BM table hidden | |
722 | inside itself. | |
723 | ||
724 | As MAGIC is always present on BMs [in Perl 5 :-)], we can use it to store | |
725 | the table instead of the previous (somewhat hacky) approach of co-opting | |
726 | the string buffer and storing it after the string. */ | |
727 | ||
728 | assert(!mg_find(sv, PERL_MAGIC_bm)); | |
729 | mg = sv_magicext(sv, NULL, PERL_MAGIC_bm, &PL_vtbl_bm, NULL, 0); | |
730 | assert(mg); | |
731 | ||
02128f11 | 732 | if (len > 2) { |
21aeb718 NC |
733 | /* Shorter strings are special-cased in Perl_fbm_instr(), and don't use |
734 | the BM table. */ | |
66a1b24b | 735 | const U8 mlen = (len>255) ? 255 : (U8)len; |
2bda37ba | 736 | const unsigned char *const sb = s + len - mlen; /* first char (maybe) */ |
eb578fdb | 737 | U8 *table; |
cf93c79d | 738 | |
2bda37ba | 739 | Newx(table, 256, U8); |
7506f9c3 | 740 | memset((void*)table, mlen, 256); |
2bda37ba NC |
741 | mg->mg_ptr = (char *)table; |
742 | mg->mg_len = 256; | |
743 | ||
744 | s += len - 1; /* last char */ | |
02128f11 | 745 | i = 0; |
cf93c79d IZ |
746 | while (s >= sb) { |
747 | if (table[*s] == mlen) | |
7506f9c3 | 748 | table[*s] = (U8)i; |
cf93c79d IZ |
749 | s--, i++; |
750 | } | |
378cc40b | 751 | } |
378cc40b | 752 | |
9cbe880b | 753 | s = (const unsigned char*)(SvPVX_const(sv)); /* deeper magic */ |
bbce6d69 | 754 | for (i = 0; i < len; i++) { |
22c35a8c | 755 | if (PL_freq[s[i]] < frequency) { |
00cccd05 | 756 | PERL_DEB( rarest = i ); |
22c35a8c | 757 | frequency = PL_freq[s[i]]; |
378cc40b LW |
758 | } |
759 | } | |
cf93c79d IZ |
760 | BmUSEFUL(sv) = 100; /* Initial value */ |
761 | if (flags & FBMcf_TAIL) | |
762 | SvTAIL_on(sv); | |
ea725ce6 | 763 | DEBUG_r(PerlIO_printf(Perl_debug_log, "rarest char %c at %"UVuf"\n", |
d80cf470 | 764 | s[rarest], (UV)rarest)); |
378cc40b LW |
765 | } |
766 | ||
cf93c79d | 767 | |
954c1994 GS |
768 | /* |
769 | =for apidoc fbm_instr | |
770 | ||
3f4963df | 771 | Returns the location of the SV in the string delimited by C<big> and |
41c8d07a DM |
772 | C<bigend> (C<bigend>) is the char following the last char). |
773 | It returns C<NULL> if the string can't be found. The C<sv> | |
796b6530 | 774 | does not have to be C<fbm_compiled>, but the search will not be as fast |
954c1994 GS |
775 | then. |
776 | ||
777 | =cut | |
41c8d07a DM |
778 | |
779 | If SvTAIL(littlestr) is true, a fake "\n" was appended to to the string | |
780 | during FBM compilation due to FBMcf_TAIL in flags. It indicates that | |
781 | the littlestr must be anchored to the end of bigstr (or to any \n if | |
782 | FBMrf_MULTILINE). | |
783 | ||
784 | E.g. The regex compiler would compile /abc/ to a littlestr of "abc", | |
785 | while /abc$/ compiles to "abc\n" with SvTAIL() true. | |
786 | ||
787 | A littlestr of "abc", !SvTAIL matches as /abc/; | |
788 | a littlestr of "ab\n", SvTAIL matches as: | |
789 | without FBMrf_MULTILINE: /ab\n?\z/ | |
790 | with FBMrf_MULTILINE: /ab\n/ || /ab\z/; | |
791 | ||
792 | (According to Ilya from 1999; I don't know if this is still true, DAPM 2015): | |
793 | "If SvTAIL is actually due to \Z or \z, this gives false positives | |
794 | if multiline". | |
954c1994 GS |
795 | */ |
796 | ||
41c8d07a | 797 | |
378cc40b | 798 | char * |
5aaab254 | 799 | Perl_fbm_instr(pTHX_ unsigned char *big, unsigned char *bigend, SV *littlestr, U32 flags) |
378cc40b | 800 | { |
eb578fdb | 801 | unsigned char *s; |
cf93c79d | 802 | STRLEN l; |
eb578fdb KW |
803 | const unsigned char *little = (const unsigned char *)SvPV_const(littlestr,l); |
804 | STRLEN littlelen = l; | |
805 | const I32 multiline = flags & FBMrf_MULTILINE; | |
cf93c79d | 806 | |
7918f24d NC |
807 | PERL_ARGS_ASSERT_FBM_INSTR; |
808 | ||
eb160463 | 809 | if ((STRLEN)(bigend - big) < littlelen) { |
a1d180c4 | 810 | if ( SvTAIL(littlestr) |
eb160463 | 811 | && ((STRLEN)(bigend - big) == littlelen - 1) |
a1d180c4 | 812 | && (littlelen == 1 |
12ae5dfc | 813 | || (*big == *little && |
27da23d5 | 814 | memEQ((char *)big, (char *)little, littlelen - 1)))) |
cf93c79d | 815 | return (char*)big; |
bd61b366 | 816 | return NULL; |
cf93c79d | 817 | } |
378cc40b | 818 | |
21aeb718 NC |
819 | switch (littlelen) { /* Special cases for 0, 1 and 2 */ |
820 | case 0: | |
821 | return (char*)big; /* Cannot be SvTAIL! */ | |
41c8d07a | 822 | |
21aeb718 | 823 | case 1: |
147f21b5 DM |
824 | if (SvTAIL(littlestr) && !multiline) /* Anchor only! */ |
825 | /* [-1] is safe because we know that bigend != big. */ | |
826 | return (char *) (bigend - (bigend[-1] == '\n')); | |
827 | ||
828 | s = (unsigned char *)memchr((void*)big, *little, bigend-big); | |
829 | if (s) | |
830 | return (char *)s; | |
cf93c79d IZ |
831 | if (SvTAIL(littlestr)) |
832 | return (char *) bigend; | |
bd61b366 | 833 | return NULL; |
41c8d07a | 834 | |
21aeb718 | 835 | case 2: |
cf93c79d | 836 | if (SvTAIL(littlestr) && !multiline) { |
147f21b5 DM |
837 | /* a littlestr with SvTAIL must be of the form "X\n" (where X |
838 | * is a single char). It is anchored, and can only match | |
839 | * "....X\n" or "....X" */ | |
840 | if (bigend[-2] == *little && bigend[-1] == '\n') | |
cf93c79d IZ |
841 | return (char*)bigend - 2; |
842 | if (bigend[-1] == *little) | |
843 | return (char*)bigend - 1; | |
bd61b366 | 844 | return NULL; |
cf93c79d | 845 | } |
147f21b5 | 846 | |
cf93c79d | 847 | { |
147f21b5 DM |
848 | /* memchr() is likely to be very fast, possibly using whatever |
849 | * hardware support is available, such as checking a whole | |
850 | * cache line in one instruction. | |
851 | * So for a 2 char pattern, calling memchr() is likely to be | |
852 | * faster than running FBM, or rolling our own. The previous | |
853 | * version of this code was roll-your-own which typically | |
854 | * only needed to read every 2nd char, which was good back in | |
855 | * the day, but no longer. | |
856 | */ | |
857 | unsigned char c1 = little[0]; | |
858 | unsigned char c2 = little[1]; | |
859 | ||
860 | /* *** for all this case, bigend points to the last char, | |
861 | * not the trailing \0: this makes the conditions slightly | |
862 | * simpler */ | |
863 | bigend--; | |
864 | s = big; | |
865 | if (c1 != c2) { | |
866 | while (s < bigend) { | |
867 | /* do a quick test for c1 before calling memchr(); | |
868 | * this avoids the expensive fn call overhead when | |
869 | * there are lots of c1's */ | |
870 | if (LIKELY(*s != c1)) { | |
871 | s++; | |
872 | s = (unsigned char *)memchr((void*)s, c1, bigend - s); | |
873 | if (!s) | |
874 | break; | |
875 | } | |
876 | if (s[1] == c2) | |
877 | return (char*)s; | |
878 | ||
879 | /* failed; try searching for c2 this time; that way | |
880 | * we don't go pathologically slow when the string | |
881 | * consists mostly of c1's or vice versa. | |
882 | */ | |
883 | s += 2; | |
884 | if (s > bigend) | |
885 | break; | |
886 | s = (unsigned char *)memchr((void*)s, c2, bigend - s + 1); | |
887 | if (!s) | |
888 | break; | |
889 | if (s[-1] == c1) | |
890 | return (char*)s - 1; | |
891 | } | |
892 | } | |
893 | else { | |
894 | /* c1, c2 the same */ | |
895 | while (s < bigend) { | |
896 | if (s[0] == c1) { | |
897 | got_1char: | |
898 | if (s[1] == c1) | |
899 | return (char*)s; | |
900 | s += 2; | |
901 | } | |
902 | else { | |
903 | s++; | |
904 | s = (unsigned char *)memchr((void*)s, c1, bigend - s); | |
905 | if (!s || s >= bigend) | |
906 | break; | |
907 | goto got_1char; | |
908 | } | |
909 | } | |
910 | } | |
911 | ||
912 | /* failed to find 2 chars; try anchored match at end without | |
913 | * the \n */ | |
914 | if (SvTAIL(littlestr) && bigend[0] == little[0]) | |
915 | return (char *)bigend; | |
916 | return NULL; | |
917 | } | |
41c8d07a | 918 | |
21aeb718 NC |
919 | default: |
920 | break; /* Only lengths 0 1 and 2 have special-case code. */ | |
d48672a2 | 921 | } |
21aeb718 | 922 | |
cf93c79d | 923 | if (SvTAIL(littlestr) && !multiline) { /* tail anchored? */ |
bbce6d69 | 924 | s = bigend - littlelen; |
a1d180c4 | 925 | if (s >= big && bigend[-1] == '\n' && *s == *little |
cf93c79d IZ |
926 | /* Automatically of length > 2 */ |
927 | && memEQ((char*)s + 1, (char*)little + 1, littlelen - 2)) | |
7506f9c3 | 928 | { |
bbce6d69 | 929 | return (char*)s; /* how sweet it is */ |
7506f9c3 GS |
930 | } |
931 | if (s[1] == *little | |
932 | && memEQ((char*)s + 2, (char*)little + 1, littlelen - 2)) | |
933 | { | |
cf93c79d | 934 | return (char*)s + 1; /* how sweet it is */ |
7506f9c3 | 935 | } |
bd61b366 | 936 | return NULL; |
02128f11 | 937 | } |
41c8d07a | 938 | |
cecf5685 | 939 | if (!SvVALID(littlestr)) { |
147f21b5 | 940 | /* not compiled; use Perl_ninstr() instead */ |
c4420975 | 941 | char * const b = ninstr((char*)big,(char*)bigend, |
cf93c79d IZ |
942 | (char*)little, (char*)little + littlelen); |
943 | ||
944 | if (!b && SvTAIL(littlestr)) { /* Automatically multiline! */ | |
945 | /* Chop \n from littlestr: */ | |
946 | s = bigend - littlelen + 1; | |
7506f9c3 GS |
947 | if (*s == *little |
948 | && memEQ((char*)s + 1, (char*)little + 1, littlelen - 2)) | |
949 | { | |
3fe6f2dc | 950 | return (char*)s; |
7506f9c3 | 951 | } |
bd61b366 | 952 | return NULL; |
a687059c | 953 | } |
cf93c79d | 954 | return b; |
a687059c | 955 | } |
a1d180c4 | 956 | |
3566a07d NC |
957 | /* Do actual FBM. */ |
958 | if (littlelen > (STRLEN)(bigend - big)) | |
959 | return NULL; | |
960 | ||
961 | { | |
2bda37ba | 962 | const MAGIC *const mg = mg_find(littlestr, PERL_MAGIC_bm); |
eb578fdb | 963 | const unsigned char *oldlittle; |
cf93c79d | 964 | |
316ebaf2 JH |
965 | assert(mg); |
966 | ||
cf93c79d IZ |
967 | --littlelen; /* Last char found by table lookup */ |
968 | ||
969 | s = big + littlelen; | |
970 | little += littlelen; /* last char */ | |
971 | oldlittle = little; | |
972 | if (s < bigend) { | |
316ebaf2 | 973 | const unsigned char * const table = (const unsigned char *) mg->mg_ptr; |
147f21b5 | 974 | const unsigned char lastc = *little; |
eb578fdb | 975 | I32 tmp; |
cf93c79d IZ |
976 | |
977 | top2: | |
7506f9c3 | 978 | if ((tmp = table[*s])) { |
147f21b5 DM |
979 | /* *s != lastc; earliest position it could match now is |
980 | * tmp slots further on */ | |
981 | if ((s += tmp) >= bigend) | |
982 | goto check_end; | |
983 | if (LIKELY(*s != lastc)) { | |
984 | s++; | |
985 | s = (unsigned char *)memchr((void*)s, lastc, bigend - s); | |
986 | if (!s) { | |
987 | s = bigend; | |
988 | goto check_end; | |
989 | } | |
990 | goto top2; | |
991 | } | |
cf93c79d | 992 | } |
147f21b5 DM |
993 | |
994 | ||
995 | /* hand-rolled strncmp(): less expensive than calling the | |
996 | * real function (maybe???) */ | |
997 | { | |
eb578fdb | 998 | unsigned char * const olds = s; |
cf93c79d IZ |
999 | |
1000 | tmp = littlelen; | |
1001 | ||
1002 | while (tmp--) { | |
1003 | if (*--s == *--little) | |
1004 | continue; | |
cf93c79d IZ |
1005 | s = olds + 1; /* here we pay the price for failure */ |
1006 | little = oldlittle; | |
1007 | if (s < bigend) /* fake up continue to outer loop */ | |
1008 | goto top2; | |
1009 | goto check_end; | |
1010 | } | |
1011 | return (char *)s; | |
a687059c | 1012 | } |
378cc40b | 1013 | } |
cf93c79d | 1014 | check_end: |
c8029a41 | 1015 | if ( s == bigend |
cffe132d | 1016 | && SvTAIL(littlestr) |
12ae5dfc JH |
1017 | && memEQ((char *)(bigend - littlelen), |
1018 | (char *)(oldlittle - littlelen), littlelen) ) | |
cf93c79d | 1019 | return (char*)bigend - littlelen; |
bd61b366 | 1020 | return NULL; |
378cc40b | 1021 | } |
378cc40b LW |
1022 | } |
1023 | ||
41c8d07a | 1024 | |
e6226b18 KW |
1025 | /* |
1026 | =for apidoc foldEQ | |
1027 | ||
796b6530 KW |
1028 | Returns true if the leading C<len> bytes of the strings C<s1> and C<s2> are the |
1029 | same | |
e6226b18 KW |
1030 | case-insensitively; false otherwise. Uppercase and lowercase ASCII range bytes |
1031 | match themselves and their opposite case counterparts. Non-cased and non-ASCII | |
1032 | range bytes match only themselves. | |
1033 | ||
1034 | =cut | |
1035 | */ | |
1036 | ||
1037 | ||
79072805 | 1038 | I32 |
5aaab254 | 1039 | Perl_foldEQ(const char *s1, const char *s2, I32 len) |
79072805 | 1040 | { |
eb578fdb KW |
1041 | const U8 *a = (const U8 *)s1; |
1042 | const U8 *b = (const U8 *)s2; | |
96a5add6 | 1043 | |
e6226b18 | 1044 | PERL_ARGS_ASSERT_FOLDEQ; |
7918f24d | 1045 | |
223f01db KW |
1046 | assert(len >= 0); |
1047 | ||
79072805 | 1048 | while (len--) { |
22c35a8c | 1049 | if (*a != *b && *a != PL_fold[*b]) |
e6226b18 | 1050 | return 0; |
bbce6d69 | 1051 | a++,b++; |
1052 | } | |
e6226b18 | 1053 | return 1; |
bbce6d69 | 1054 | } |
1b9f127b | 1055 | I32 |
5aaab254 | 1056 | Perl_foldEQ_latin1(const char *s1, const char *s2, I32 len) |
1b9f127b KW |
1057 | { |
1058 | /* Compare non-utf8 using Unicode (Latin1) semantics. Does not work on | |
1059 | * MICRO_SIGN, LATIN_SMALL_LETTER_SHARP_S, nor | |
1060 | * LATIN_SMALL_LETTER_Y_WITH_DIAERESIS, and does not check for these. Nor | |
1061 | * does it check that the strings each have at least 'len' characters */ | |
1062 | ||
eb578fdb KW |
1063 | const U8 *a = (const U8 *)s1; |
1064 | const U8 *b = (const U8 *)s2; | |
1b9f127b KW |
1065 | |
1066 | PERL_ARGS_ASSERT_FOLDEQ_LATIN1; | |
1067 | ||
223f01db KW |
1068 | assert(len >= 0); |
1069 | ||
1b9f127b KW |
1070 | while (len--) { |
1071 | if (*a != *b && *a != PL_fold_latin1[*b]) { | |
1072 | return 0; | |
1073 | } | |
1074 | a++, b++; | |
1075 | } | |
1076 | return 1; | |
1077 | } | |
bbce6d69 | 1078 | |
e6226b18 KW |
1079 | /* |
1080 | =for apidoc foldEQ_locale | |
1081 | ||
796b6530 KW |
1082 | Returns true if the leading C<len> bytes of the strings C<s1> and C<s2> are the |
1083 | same case-insensitively in the current locale; false otherwise. | |
e6226b18 KW |
1084 | |
1085 | =cut | |
1086 | */ | |
1087 | ||
bbce6d69 | 1088 | I32 |
5aaab254 | 1089 | Perl_foldEQ_locale(const char *s1, const char *s2, I32 len) |
bbce6d69 | 1090 | { |
27da23d5 | 1091 | dVAR; |
eb578fdb KW |
1092 | const U8 *a = (const U8 *)s1; |
1093 | const U8 *b = (const U8 *)s2; | |
96a5add6 | 1094 | |
e6226b18 | 1095 | PERL_ARGS_ASSERT_FOLDEQ_LOCALE; |
7918f24d | 1096 | |
223f01db KW |
1097 | assert(len >= 0); |
1098 | ||
bbce6d69 | 1099 | while (len--) { |
22c35a8c | 1100 | if (*a != *b && *a != PL_fold_locale[*b]) |
e6226b18 | 1101 | return 0; |
bbce6d69 | 1102 | a++,b++; |
79072805 | 1103 | } |
e6226b18 | 1104 | return 1; |
79072805 LW |
1105 | } |
1106 | ||
8d063cd8 LW |
1107 | /* copy a string to a safe spot */ |
1108 | ||
954c1994 | 1109 | /* |
ccfc67b7 JH |
1110 | =head1 Memory Management |
1111 | ||
954c1994 GS |
1112 | =for apidoc savepv |
1113 | ||
72d33970 FC |
1114 | Perl's version of C<strdup()>. Returns a pointer to a newly allocated |
1115 | string which is a duplicate of C<pv>. The size of the string is | |
30a15352 KW |
1116 | determined by C<strlen()>, which means it may not contain embedded C<NUL> |
1117 | characters and must have a trailing C<NUL>. The memory allocated for the new | |
1118 | string can be freed with the C<Safefree()> function. | |
954c1994 | 1119 | |
0358c255 KW |
1120 | On some platforms, Windows for example, all allocated memory owned by a thread |
1121 | is deallocated when that thread ends. So if you need that not to happen, you | |
1122 | need to use the shared memory functions, such as C<L</savesharedpv>>. | |
1123 | ||
954c1994 GS |
1124 | =cut |
1125 | */ | |
1126 | ||
8d063cd8 | 1127 | char * |
efdfce31 | 1128 | Perl_savepv(pTHX_ const char *pv) |
8d063cd8 | 1129 | { |
96a5add6 | 1130 | PERL_UNUSED_CONTEXT; |
e90e2364 | 1131 | if (!pv) |
bd61b366 | 1132 | return NULL; |
66a1b24b AL |
1133 | else { |
1134 | char *newaddr; | |
1135 | const STRLEN pvlen = strlen(pv)+1; | |
10edeb5d JH |
1136 | Newx(newaddr, pvlen, char); |
1137 | return (char*)memcpy(newaddr, pv, pvlen); | |
66a1b24b | 1138 | } |
8d063cd8 LW |
1139 | } |
1140 | ||
a687059c LW |
1141 | /* same thing but with a known length */ |
1142 | ||
954c1994 GS |
1143 | /* |
1144 | =for apidoc savepvn | |
1145 | ||
72d33970 | 1146 | Perl's version of what C<strndup()> would be if it existed. Returns a |
61a925ed | 1147 | pointer to a newly allocated string which is a duplicate of the first |
72d33970 | 1148 | C<len> bytes from C<pv>, plus a trailing |
6602b933 | 1149 | C<NUL> byte. The memory allocated for |
cbf82dd0 | 1150 | the new string can be freed with the C<Safefree()> function. |
954c1994 | 1151 | |
0358c255 KW |
1152 | On some platforms, Windows for example, all allocated memory owned by a thread |
1153 | is deallocated when that thread ends. So if you need that not to happen, you | |
1154 | need to use the shared memory functions, such as C<L</savesharedpvn>>. | |
1155 | ||
954c1994 GS |
1156 | =cut |
1157 | */ | |
1158 | ||
a687059c | 1159 | char * |
5aaab254 | 1160 | Perl_savepvn(pTHX_ const char *pv, I32 len) |
a687059c | 1161 | { |
eb578fdb | 1162 | char *newaddr; |
96a5add6 | 1163 | PERL_UNUSED_CONTEXT; |
a687059c | 1164 | |
223f01db KW |
1165 | assert(len >= 0); |
1166 | ||
a02a5408 | 1167 | Newx(newaddr,len+1,char); |
92110913 | 1168 | /* Give a meaning to NULL pointer mainly for the use in sv_magic() */ |
efdfce31 | 1169 | if (pv) { |
e90e2364 NC |
1170 | /* might not be null terminated */ |
1171 | newaddr[len] = '\0'; | |
07409e01 | 1172 | return (char *) CopyD(pv,newaddr,len,char); |
92110913 NIS |
1173 | } |
1174 | else { | |
07409e01 | 1175 | return (char *) ZeroD(newaddr,len+1,char); |
92110913 | 1176 | } |
a687059c LW |
1177 | } |
1178 | ||
05ec9bb3 NIS |
1179 | /* |
1180 | =for apidoc savesharedpv | |
1181 | ||
61a925ed AMS |
1182 | A version of C<savepv()> which allocates the duplicate string in memory |
1183 | which is shared between threads. | |
05ec9bb3 NIS |
1184 | |
1185 | =cut | |
1186 | */ | |
1187 | char * | |
efdfce31 | 1188 | Perl_savesharedpv(pTHX_ const char *pv) |
05ec9bb3 | 1189 | { |
eb578fdb | 1190 | char *newaddr; |
490a0e98 | 1191 | STRLEN pvlen; |
dc3bf405 BF |
1192 | |
1193 | PERL_UNUSED_CONTEXT; | |
1194 | ||
e90e2364 | 1195 | if (!pv) |
bd61b366 | 1196 | return NULL; |
e90e2364 | 1197 | |
490a0e98 NC |
1198 | pvlen = strlen(pv)+1; |
1199 | newaddr = (char*)PerlMemShared_malloc(pvlen); | |
e90e2364 | 1200 | if (!newaddr) { |
4cbe3a7d | 1201 | croak_no_mem(); |
05ec9bb3 | 1202 | } |
10edeb5d | 1203 | return (char*)memcpy(newaddr, pv, pvlen); |
05ec9bb3 NIS |
1204 | } |
1205 | ||
2e0de35c | 1206 | /* |
d9095cec NC |
1207 | =for apidoc savesharedpvn |
1208 | ||
1209 | A version of C<savepvn()> which allocates the duplicate string in memory | |
796b6530 | 1210 | which is shared between threads. (With the specific difference that a C<NULL> |
d9095cec NC |
1211 | pointer is not acceptable) |
1212 | ||
1213 | =cut | |
1214 | */ | |
1215 | char * | |
1216 | Perl_savesharedpvn(pTHX_ const char *const pv, const STRLEN len) | |
1217 | { | |
1218 | char *const newaddr = (char*)PerlMemShared_malloc(len + 1); | |
7918f24d | 1219 | |
dc3bf405 | 1220 | PERL_UNUSED_CONTEXT; |
6379d4a9 | 1221 | /* PERL_ARGS_ASSERT_SAVESHAREDPVN; */ |
7918f24d | 1222 | |
d9095cec | 1223 | if (!newaddr) { |
4cbe3a7d | 1224 | croak_no_mem(); |
d9095cec NC |
1225 | } |
1226 | newaddr[len] = '\0'; | |
1227 | return (char*)memcpy(newaddr, pv, len); | |
1228 | } | |
1229 | ||
1230 | /* | |
2e0de35c NC |
1231 | =for apidoc savesvpv |
1232 | ||
6832267f | 1233 | A version of C<savepv()>/C<savepvn()> which gets the string to duplicate from |
2e0de35c NC |
1234 | the passed in SV using C<SvPV()> |
1235 | ||
0358c255 KW |
1236 | On some platforms, Windows for example, all allocated memory owned by a thread |
1237 | is deallocated when that thread ends. So if you need that not to happen, you | |
1238 | need to use the shared memory functions, such as C<L</savesharedsvpv>>. | |
1239 | ||
2e0de35c NC |
1240 | =cut |
1241 | */ | |
1242 | ||
1243 | char * | |
1244 | Perl_savesvpv(pTHX_ SV *sv) | |
1245 | { | |
1246 | STRLEN len; | |
7452cf6a | 1247 | const char * const pv = SvPV_const(sv, len); |
eb578fdb | 1248 | char *newaddr; |
2e0de35c | 1249 | |
7918f24d NC |
1250 | PERL_ARGS_ASSERT_SAVESVPV; |
1251 | ||
26866f99 | 1252 | ++len; |
a02a5408 | 1253 | Newx(newaddr,len,char); |
07409e01 | 1254 | return (char *) CopyD(pv,newaddr,len,char); |
2e0de35c | 1255 | } |
05ec9bb3 | 1256 | |
9dcc53ea Z |
1257 | /* |
1258 | =for apidoc savesharedsvpv | |
1259 | ||
1260 | A version of C<savesharedpv()> which allocates the duplicate string in | |
1261 | memory which is shared between threads. | |
1262 | ||
1263 | =cut | |
1264 | */ | |
1265 | ||
1266 | char * | |
1267 | Perl_savesharedsvpv(pTHX_ SV *sv) | |
1268 | { | |
1269 | STRLEN len; | |
1270 | const char * const pv = SvPV_const(sv, len); | |
1271 | ||
1272 | PERL_ARGS_ASSERT_SAVESHAREDSVPV; | |
1273 | ||
1274 | return savesharedpvn(pv, len); | |
1275 | } | |
05ec9bb3 | 1276 | |
cea2e8a9 | 1277 | /* the SV for Perl_form() and mess() is not kept in an arena */ |
fc36a67e | 1278 | |
76e3520e | 1279 | STATIC SV * |
cea2e8a9 | 1280 | S_mess_alloc(pTHX) |
fc36a67e | 1281 | { |
1282 | SV *sv; | |
1283 | XPVMG *any; | |
1284 | ||
627364f1 | 1285 | if (PL_phase != PERL_PHASE_DESTRUCT) |
84bafc02 | 1286 | return newSVpvs_flags("", SVs_TEMP); |
e72dc28c | 1287 | |
0372dbb6 GS |
1288 | if (PL_mess_sv) |
1289 | return PL_mess_sv; | |
1290 | ||
fc36a67e | 1291 | /* Create as PVMG now, to avoid any upgrading later */ |
a02a5408 JC |
1292 | Newx(sv, 1, SV); |
1293 | Newxz(any, 1, XPVMG); | |
fc36a67e | 1294 | SvFLAGS(sv) = SVt_PVMG; |
1295 | SvANY(sv) = (void*)any; | |
6136c704 | 1296 | SvPV_set(sv, NULL); |
fc36a67e | 1297 | SvREFCNT(sv) = 1 << 30; /* practically infinite */ |
e72dc28c | 1298 | PL_mess_sv = sv; |
fc36a67e | 1299 | return sv; |
1300 | } | |
1301 | ||
c5be433b | 1302 | #if defined(PERL_IMPLICIT_CONTEXT) |
cea2e8a9 GS |
1303 | char * |
1304 | Perl_form_nocontext(const char* pat, ...) | |
1305 | { | |
1306 | dTHX; | |
c5be433b | 1307 | char *retval; |
cea2e8a9 | 1308 | va_list args; |
7918f24d | 1309 | PERL_ARGS_ASSERT_FORM_NOCONTEXT; |
cea2e8a9 | 1310 | va_start(args, pat); |
c5be433b | 1311 | retval = vform(pat, &args); |
cea2e8a9 | 1312 | va_end(args); |
c5be433b | 1313 | return retval; |
cea2e8a9 | 1314 | } |
c5be433b | 1315 | #endif /* PERL_IMPLICIT_CONTEXT */ |
cea2e8a9 | 1316 | |
7c9e965c | 1317 | /* |
ccfc67b7 | 1318 | =head1 Miscellaneous Functions |
7c9e965c JP |
1319 | =for apidoc form |
1320 | ||
1321 | Takes a sprintf-style format pattern and conventional | |
1322 | (non-SV) arguments and returns the formatted string. | |
1323 | ||
1324 | (char *) Perl_form(pTHX_ const char* pat, ...) | |
1325 | ||
1326 | can be used any place a string (char *) is required: | |
1327 | ||
1328 | char * s = Perl_form("%d.%d",major,minor); | |
1329 | ||
1330 | Uses a single private buffer so if you want to format several strings you | |
1331 | must explicitly copy the earlier strings away (and free the copies when you | |
1332 | are done). | |
1333 | ||
1334 | =cut | |
1335 | */ | |
1336 | ||
8990e307 | 1337 | char * |
864dbfa3 | 1338 | Perl_form(pTHX_ const char* pat, ...) |
8990e307 | 1339 | { |
c5be433b | 1340 | char *retval; |
46fc3d4c | 1341 | va_list args; |
7918f24d | 1342 | PERL_ARGS_ASSERT_FORM; |
46fc3d4c | 1343 | va_start(args, pat); |
c5be433b | 1344 | retval = vform(pat, &args); |
46fc3d4c | 1345 | va_end(args); |
c5be433b GS |
1346 | return retval; |
1347 | } | |
1348 | ||
1349 | char * | |
1350 | Perl_vform(pTHX_ const char *pat, va_list *args) | |
1351 | { | |
2d03de9c | 1352 | SV * const sv = mess_alloc(); |
7918f24d | 1353 | PERL_ARGS_ASSERT_VFORM; |
4608196e | 1354 | sv_vsetpvfn(sv, pat, strlen(pat), args, NULL, 0, NULL); |
e72dc28c | 1355 | return SvPVX(sv); |
46fc3d4c | 1356 | } |
a687059c | 1357 | |
c5df3096 Z |
1358 | /* |
1359 | =for apidoc Am|SV *|mess|const char *pat|... | |
1360 | ||
1361 | Take a sprintf-style format pattern and argument list. These are used to | |
1362 | generate a string message. If the message does not end with a newline, | |
1363 | then it will be extended with some indication of the current location | |
1364 | in the code, as described for L</mess_sv>. | |
1365 | ||
1366 | Normally, the resulting message is returned in a new mortal SV. | |
1367 | During global destruction a single SV may be shared between uses of | |
1368 | this function. | |
1369 | ||
1370 | =cut | |
1371 | */ | |
1372 | ||
5a844595 GS |
1373 | #if defined(PERL_IMPLICIT_CONTEXT) |
1374 | SV * | |
1375 | Perl_mess_nocontext(const char *pat, ...) | |
1376 | { | |
1377 | dTHX; | |
1378 | SV *retval; | |
1379 | va_list args; | |
7918f24d | 1380 | PERL_ARGS_ASSERT_MESS_NOCONTEXT; |
5a844595 GS |
1381 | va_start(args, pat); |
1382 | retval = vmess(pat, &args); | |
1383 | va_end(args); | |
1384 | return retval; | |
1385 | } | |
1386 | #endif /* PERL_IMPLICIT_CONTEXT */ | |
1387 | ||
06bf62c7 | 1388 | SV * |
5a844595 GS |
1389 | Perl_mess(pTHX_ const char *pat, ...) |
1390 | { | |
1391 | SV *retval; | |
1392 | va_list args; | |
7918f24d | 1393 | PERL_ARGS_ASSERT_MESS; |
5a844595 GS |
1394 | va_start(args, pat); |
1395 | retval = vmess(pat, &args); | |
1396 | va_end(args); | |
1397 | return retval; | |
1398 | } | |
1399 | ||
25502127 FC |
1400 | const COP* |
1401 | Perl_closest_cop(pTHX_ const COP *cop, const OP *o, const OP *curop, | |
1402 | bool opnext) | |
ae7d165c | 1403 | { |
25502127 FC |
1404 | /* Look for curop starting from o. cop is the last COP we've seen. */ |
1405 | /* opnext means that curop is actually the ->op_next of the op we are | |
1406 | seeking. */ | |
ae7d165c | 1407 | |
7918f24d NC |
1408 | PERL_ARGS_ASSERT_CLOSEST_COP; |
1409 | ||
25502127 FC |
1410 | if (!o || !curop || ( |
1411 | opnext ? o->op_next == curop && o->op_type != OP_SCOPE : o == curop | |
1412 | )) | |
fabdb6c0 | 1413 | return cop; |
ae7d165c PJ |
1414 | |
1415 | if (o->op_flags & OPf_KIDS) { | |
5f66b61c | 1416 | const OP *kid; |
e6dae479 | 1417 | for (kid = cUNOPo->op_first; kid; kid = OpSIBLING(kid)) { |
5f66b61c | 1418 | const COP *new_cop; |
ae7d165c PJ |
1419 | |
1420 | /* If the OP_NEXTSTATE has been optimised away we can still use it | |
1421 | * the get the file and line number. */ | |
1422 | ||
1423 | if (kid->op_type == OP_NULL && kid->op_targ == OP_NEXTSTATE) | |
5f66b61c | 1424 | cop = (const COP *)kid; |
ae7d165c PJ |
1425 | |
1426 | /* Keep searching, and return when we've found something. */ | |
1427 | ||
25502127 | 1428 | new_cop = closest_cop(cop, kid, curop, opnext); |
fabdb6c0 AL |
1429 | if (new_cop) |
1430 | return new_cop; | |
ae7d165c PJ |
1431 | } |
1432 | } | |
1433 | ||
1434 | /* Nothing found. */ | |
1435 | ||
5f66b61c | 1436 | return NULL; |
ae7d165c PJ |
1437 | } |
1438 | ||
c5df3096 Z |
1439 | /* |
1440 | =for apidoc Am|SV *|mess_sv|SV *basemsg|bool consume | |
1441 | ||
1442 | Expands a message, intended for the user, to include an indication of | |
1443 | the current location in the code, if the message does not already appear | |
1444 | to be complete. | |
1445 | ||
1446 | C<basemsg> is the initial message or object. If it is a reference, it | |
1447 | will be used as-is and will be the result of this function. Otherwise it | |
1448 | is used as a string, and if it already ends with a newline, it is taken | |
1449 | to be complete, and the result of this function will be the same string. | |
1450 | If the message does not end with a newline, then a segment such as C<at | |
1451 | foo.pl line 37> will be appended, and possibly other clauses indicating | |
1452 | the current state of execution. The resulting message will end with a | |
1453 | dot and a newline. | |
1454 | ||
1455 | Normally, the resulting message is returned in a new mortal SV. | |
1456 | During global destruction a single SV may be shared between uses of this | |
1457 | function. If C<consume> is true, then the function is permitted (but not | |
1458 | required) to modify and return C<basemsg> instead of allocating a new SV. | |
1459 | ||
1460 | =cut | |
1461 | */ | |
1462 | ||
5a844595 | 1463 | SV * |
c5df3096 | 1464 | Perl_mess_sv(pTHX_ SV *basemsg, bool consume) |
46fc3d4c | 1465 | { |
c5df3096 | 1466 | SV *sv; |
46fc3d4c | 1467 | |
0762e42f | 1468 | #if defined(USE_C_BACKTRACE) && defined(USE_C_BACKTRACE_ON_ERROR) |
470dd224 JH |
1469 | { |
1470 | char *ws; | |
22ff3130 | 1471 | UV wi; |
470dd224 | 1472 | /* The PERL_C_BACKTRACE_ON_WARN must be an integer of one or more. */ |
22ff3130 HS |
1473 | if ((ws = PerlEnv_getenv("PERL_C_BACKTRACE_ON_ERROR")) |
1474 | && grok_atoUV(ws, &wi, NULL) | |
1475 | && wi <= PERL_INT_MAX | |
1476 | ) { | |
1477 | Perl_dump_c_backtrace(aTHX_ Perl_debug_log, (int)wi, 1); | |
470dd224 JH |
1478 | } |
1479 | } | |
1480 | #endif | |
1481 | ||
c5df3096 Z |
1482 | PERL_ARGS_ASSERT_MESS_SV; |
1483 | ||
1484 | if (SvROK(basemsg)) { | |
1485 | if (consume) { | |
1486 | sv = basemsg; | |
1487 | } | |
1488 | else { | |
1489 | sv = mess_alloc(); | |
1490 | sv_setsv(sv, basemsg); | |
1491 | } | |
1492 | return sv; | |
1493 | } | |
1494 | ||
1495 | if (SvPOK(basemsg) && consume) { | |
1496 | sv = basemsg; | |
1497 | } | |
1498 | else { | |
1499 | sv = mess_alloc(); | |
1500 | sv_copypv(sv, basemsg); | |
1501 | } | |
7918f24d | 1502 | |
46fc3d4c | 1503 | if (!SvCUR(sv) || *(SvEND(sv) - 1) != '\n') { |
ae7d165c PJ |
1504 | /* |
1505 | * Try and find the file and line for PL_op. This will usually be | |
1506 | * PL_curcop, but it might be a cop that has been optimised away. We | |
1507 | * can try to find such a cop by searching through the optree starting | |
1508 | * from the sibling of PL_curcop. | |
1509 | */ | |
1510 | ||
25502127 | 1511 | const COP *cop = |
e6dae479 | 1512 | closest_cop(PL_curcop, OpSIBLING(PL_curcop), PL_op, FALSE); |
5f66b61c AL |
1513 | if (!cop) |
1514 | cop = PL_curcop; | |
ae7d165c PJ |
1515 | |
1516 | if (CopLINE(cop)) | |
ed094faf | 1517 | Perl_sv_catpvf(aTHX_ sv, " at %s line %"IVdf, |
3aed30dc | 1518 | OutCopFILE(cop), (IV)CopLINE(cop)); |
191f87d5 DH |
1519 | /* Seems that GvIO() can be untrustworthy during global destruction. */ |
1520 | if (GvIO(PL_last_in_gv) && (SvTYPE(GvIOp(PL_last_in_gv)) == SVt_PVIO) | |
1521 | && IoLINES(GvIOp(PL_last_in_gv))) | |
1522 | { | |
2748e602 | 1523 | STRLEN l; |
e1ec3a88 | 1524 | const bool line_mode = (RsSIMPLE(PL_rs) && |
2748e602 | 1525 | *SvPV_const(PL_rs,l) == '\n' && l == 1); |
3b46b707 BF |
1526 | Perl_sv_catpvf(aTHX_ sv, ", <%"SVf"> %s %"IVdf, |
1527 | SVfARG(PL_last_in_gv == PL_argvgv | |
1528 | ? &PL_sv_no | |
1529 | : sv_2mortal(newSVhek(GvNAME_HEK(PL_last_in_gv)))), | |
edc2eac3 JH |
1530 | line_mode ? "line" : "chunk", |
1531 | (IV)IoLINES(GvIOp(PL_last_in_gv))); | |
a687059c | 1532 | } |
627364f1 | 1533 | if (PL_phase == PERL_PHASE_DESTRUCT) |
5f66b61c AL |
1534 | sv_catpvs(sv, " during global destruction"); |
1535 | sv_catpvs(sv, ".\n"); | |
a687059c | 1536 | } |
06bf62c7 | 1537 | return sv; |
a687059c LW |
1538 | } |
1539 | ||
c5df3096 Z |
1540 | /* |
1541 | =for apidoc Am|SV *|vmess|const char *pat|va_list *args | |
1542 | ||
1543 | C<pat> and C<args> are a sprintf-style format pattern and encapsulated | |
801caa78 KW |
1544 | argument list, respectively. These are used to generate a string message. If |
1545 | the | |
c5df3096 Z |
1546 | message does not end with a newline, then it will be extended with |
1547 | some indication of the current location in the code, as described for | |
1548 | L</mess_sv>. | |
1549 | ||
1550 | Normally, the resulting message is returned in a new mortal SV. | |
1551 | During global destruction a single SV may be shared between uses of | |
1552 | this function. | |
1553 | ||
1554 | =cut | |
1555 | */ | |
1556 | ||
1557 | SV * | |
1558 | Perl_vmess(pTHX_ const char *pat, va_list *args) | |
1559 | { | |
c5df3096 Z |
1560 | SV * const sv = mess_alloc(); |
1561 | ||
1562 | PERL_ARGS_ASSERT_VMESS; | |
1563 | ||
1564 | sv_vsetpvfn(sv, pat, strlen(pat), args, NULL, 0, NULL); | |
1565 | return mess_sv(sv, 1); | |
1566 | } | |
1567 | ||
7ff03255 | 1568 | void |
7d0994e0 | 1569 | Perl_write_to_stderr(pTHX_ SV* msv) |
7ff03255 SG |
1570 | { |
1571 | IO *io; | |
1572 | MAGIC *mg; | |
1573 | ||
7918f24d NC |
1574 | PERL_ARGS_ASSERT_WRITE_TO_STDERR; |
1575 | ||
7ff03255 SG |
1576 | if (PL_stderrgv && SvREFCNT(PL_stderrgv) |
1577 | && (io = GvIO(PL_stderrgv)) | |
daba3364 | 1578 | && (mg = SvTIED_mg((const SV *)io, PERL_MAGIC_tiedscalar))) |
36925d9e | 1579 | Perl_magic_methcall(aTHX_ MUTABLE_SV(io), mg, SV_CONST(PRINT), |
d1d7a15d | 1580 | G_SCALAR | G_DISCARD | G_WRITING_TO_STDERR, 1, msv); |
7ff03255 | 1581 | else { |
53c1dcc0 | 1582 | PerlIO * const serr = Perl_error_log; |
7ff03255 | 1583 | |
83c55556 | 1584 | do_print(msv, serr); |
7ff03255 | 1585 | (void)PerlIO_flush(serr); |
7ff03255 SG |
1586 | } |
1587 | } | |
1588 | ||
c5df3096 Z |
1589 | /* |
1590 | =head1 Warning and Dieing | |
1591 | */ | |
1592 | ||
1593 | /* Common code used in dieing and warning */ | |
1594 | ||
1595 | STATIC SV * | |
1596 | S_with_queued_errors(pTHX_ SV *ex) | |
1597 | { | |
1598 | PERL_ARGS_ASSERT_WITH_QUEUED_ERRORS; | |
1599 | if (PL_errors && SvCUR(PL_errors) && !SvROK(ex)) { | |
1600 | sv_catsv(PL_errors, ex); | |
1601 | ex = sv_mortalcopy(PL_errors); | |
1602 | SvCUR_set(PL_errors, 0); | |
1603 | } | |
1604 | return ex; | |
1605 | } | |
3ab1ac99 | 1606 | |
46d9c920 | 1607 | STATIC bool |
c5df3096 | 1608 | S_invoke_exception_hook(pTHX_ SV *ex, bool warn) |
63315e18 NC |
1609 | { |
1610 | HV *stash; | |
1611 | GV *gv; | |
1612 | CV *cv; | |
46d9c920 NC |
1613 | SV **const hook = warn ? &PL_warnhook : &PL_diehook; |
1614 | /* sv_2cv might call Perl_croak() or Perl_warner() */ | |
1615 | SV * const oldhook = *hook; | |
1616 | ||
c5df3096 Z |
1617 | if (!oldhook) |
1618 | return FALSE; | |
63315e18 | 1619 | |
63315e18 | 1620 | ENTER; |
46d9c920 NC |
1621 | SAVESPTR(*hook); |
1622 | *hook = NULL; | |
1623 | cv = sv_2cv(oldhook, &stash, &gv, 0); | |
63315e18 NC |
1624 | LEAVE; |
1625 | if (cv && !CvDEPTH(cv) && (CvROOT(cv) || CvXSUB(cv))) { | |
1626 | dSP; | |
c5df3096 | 1627 | SV *exarg; |
63315e18 NC |
1628 | |
1629 | ENTER; | |
2782061f | 1630 | save_re_context(); |
46d9c920 NC |
1631 | if (warn) { |
1632 | SAVESPTR(*hook); | |
1633 | *hook = NULL; | |
1634 | } | |
c5df3096 Z |
1635 | exarg = newSVsv(ex); |
1636 | SvREADONLY_on(exarg); | |
1637 | SAVEFREESV(exarg); | |
63315e18 | 1638 | |
46d9c920 | 1639 | PUSHSTACKi(warn ? PERLSI_WARNHOOK : PERLSI_DIEHOOK); |
63315e18 | 1640 | PUSHMARK(SP); |
c5df3096 | 1641 | XPUSHs(exarg); |
63315e18 | 1642 | PUTBACK; |
daba3364 | 1643 | call_sv(MUTABLE_SV(cv), G_DISCARD); |
63315e18 NC |
1644 | POPSTACK; |
1645 | LEAVE; | |
46d9c920 | 1646 | return TRUE; |
63315e18 | 1647 | } |
46d9c920 | 1648 | return FALSE; |
63315e18 NC |
1649 | } |
1650 | ||
c5df3096 Z |
1651 | /* |
1652 | =for apidoc Am|OP *|die_sv|SV *baseex | |
e07360fa | 1653 | |
c5df3096 Z |
1654 | Behaves the same as L</croak_sv>, except for the return type. |
1655 | It should be used only where the C<OP *> return type is required. | |
1656 | The function never actually returns. | |
e07360fa | 1657 | |
c5df3096 Z |
1658 | =cut |
1659 | */ | |
e07360fa | 1660 | |
f8d5a522 DD |
1661 | #ifdef _MSC_VER |
1662 | # pragma warning( push ) | |
1663 | # pragma warning( disable : 4646 ) /* warning C4646: function declared with | |
1664 | __declspec(noreturn) has non-void return type */ | |
1665 | # pragma warning( disable : 4645 ) /* warning C4645: function declared with | |
1666 | __declspec(noreturn) has a return statement */ | |
1667 | #endif | |
c5df3096 Z |
1668 | OP * |
1669 | Perl_die_sv(pTHX_ SV *baseex) | |
36477c24 | 1670 | { |
c5df3096 Z |
1671 | PERL_ARGS_ASSERT_DIE_SV; |
1672 | croak_sv(baseex); | |
e5964223 | 1673 | /* NOTREACHED */ |
117af67d | 1674 | NORETURN_FUNCTION_END; |
36477c24 | 1675 | } |
f8d5a522 DD |
1676 | #ifdef _MSC_VER |
1677 | # pragma warning( pop ) | |
1678 | #endif | |
36477c24 | 1679 | |
c5df3096 Z |
1680 | /* |
1681 | =for apidoc Am|OP *|die|const char *pat|... | |
1682 | ||
1683 | Behaves the same as L</croak>, except for the return type. | |
1684 | It should be used only where the C<OP *> return type is required. | |
1685 | The function never actually returns. | |
1686 | ||
1687 | =cut | |
1688 | */ | |
1689 | ||
c5be433b | 1690 | #if defined(PERL_IMPLICIT_CONTEXT) |
f8d5a522 DD |
1691 | #ifdef _MSC_VER |
1692 | # pragma warning( push ) | |
1693 | # pragma warning( disable : 4646 ) /* warning C4646: function declared with | |
1694 | __declspec(noreturn) has non-void return type */ | |
1695 | # pragma warning( disable : 4645 ) /* warning C4645: function declared with | |
1696 | __declspec(noreturn) has a return statement */ | |
1697 | #endif | |
cea2e8a9 GS |
1698 | OP * |
1699 | Perl_die_nocontext(const char* pat, ...) | |
a687059c | 1700 | { |
cea2e8a9 | 1701 | dTHX; |
a687059c | 1702 | va_list args; |
cea2e8a9 | 1703 | va_start(args, pat); |
c5df3096 | 1704 | vcroak(pat, &args); |
e5964223 | 1705 | NOT_REACHED; /* NOTREACHED */ |
cea2e8a9 | 1706 | va_end(args); |
117af67d | 1707 | NORETURN_FUNCTION_END; |
cea2e8a9 | 1708 | } |
f8d5a522 DD |
1709 | #ifdef _MSC_VER |
1710 | # pragma warning( pop ) | |
1711 | #endif | |
c5be433b | 1712 | #endif /* PERL_IMPLICIT_CONTEXT */ |
cea2e8a9 | 1713 | |
f8d5a522 DD |
1714 | #ifdef _MSC_VER |
1715 | # pragma warning( push ) | |
1716 | # pragma warning( disable : 4646 ) /* warning C4646: function declared with | |
1717 | __declspec(noreturn) has non-void return type */ | |
1718 | # pragma warning( disable : 4645 ) /* warning C4645: function declared with | |
1719 | __declspec(noreturn) has a return statement */ | |
1720 | #endif | |
cea2e8a9 GS |
1721 | OP * |
1722 | Perl_die(pTHX_ const char* pat, ...) | |
1723 | { | |
cea2e8a9 GS |
1724 | va_list args; |
1725 | va_start(args, pat); | |
c5df3096 | 1726 | vcroak(pat, &args); |
e5964223 | 1727 | NOT_REACHED; /* NOTREACHED */ |
cea2e8a9 | 1728 | va_end(args); |
117af67d | 1729 | NORETURN_FUNCTION_END; |
cea2e8a9 | 1730 | } |
f8d5a522 DD |
1731 | #ifdef _MSC_VER |
1732 | # pragma warning( pop ) | |
1733 | #endif | |
cea2e8a9 | 1734 | |
c5df3096 Z |
1735 | /* |
1736 | =for apidoc Am|void|croak_sv|SV *baseex | |
1737 | ||
1738 | This is an XS interface to Perl's C<die> function. | |
1739 | ||
1740 | C<baseex> is the error message or object. If it is a reference, it | |
1741 | will be used as-is. Otherwise it is used as a string, and if it does | |
1742 | not end with a newline then it will be extended with some indication of | |
1743 | the current location in the code, as described for L</mess_sv>. | |
1744 | ||
1745 | The error message or object will be used as an exception, by default | |
1746 | returning control to the nearest enclosing C<eval>, but subject to | |
1747 | modification by a C<$SIG{__DIE__}> handler. In any case, the C<croak_sv> | |
1748 | function never returns normally. | |
1749 | ||
1750 | To die with a simple string message, the L</croak> function may be | |
1751 | more convenient. | |
1752 | ||
1753 | =cut | |
1754 | */ | |
1755 | ||
c5be433b | 1756 | void |
c5df3096 | 1757 | Perl_croak_sv(pTHX_ SV *baseex) |
cea2e8a9 | 1758 | { |
c5df3096 Z |
1759 | SV *ex = with_queued_errors(mess_sv(baseex, 0)); |
1760 | PERL_ARGS_ASSERT_CROAK_SV; | |
1761 | invoke_exception_hook(ex, FALSE); | |
1762 | die_unwind(ex); | |
1763 | } | |
1764 | ||
1765 | /* | |
1766 | =for apidoc Am|void|vcroak|const char *pat|va_list *args | |
1767 | ||
1768 | This is an XS interface to Perl's C<die> function. | |
1769 | ||
1770 | C<pat> and C<args> are a sprintf-style format pattern and encapsulated | |
1771 | argument list. These are used to generate a string message. If the | |
1772 | message does not end with a newline, then it will be extended with | |
1773 | some indication of the current location in the code, as described for | |
1774 | L</mess_sv>. | |
1775 | ||
1776 | The error message will be used as an exception, by default | |
1777 | returning control to the nearest enclosing C<eval>, but subject to | |
1778 | modification by a C<$SIG{__DIE__}> handler. In any case, the C<croak> | |
1779 | function never returns normally. | |
a687059c | 1780 | |
c5df3096 Z |
1781 | For historical reasons, if C<pat> is null then the contents of C<ERRSV> |
1782 | (C<$@>) will be used as an error message or object instead of building an | |
1783 | error message from arguments. If you want to throw a non-string object, | |
1784 | or build an error message in an SV yourself, it is preferable to use | |
1785 | the L</croak_sv> function, which does not involve clobbering C<ERRSV>. | |
5a844595 | 1786 | |
c5df3096 Z |
1787 | =cut |
1788 | */ | |
1789 | ||
1790 | void | |
1791 | Perl_vcroak(pTHX_ const char* pat, va_list *args) | |
1792 | { | |
1793 | SV *ex = with_queued_errors(pat ? vmess(pat, args) : mess_sv(ERRSV, 0)); | |
1794 | invoke_exception_hook(ex, FALSE); | |
1795 | die_unwind(ex); | |
a687059c LW |
1796 | } |
1797 | ||
c5df3096 Z |
1798 | /* |
1799 | =for apidoc Am|void|croak|const char *pat|... | |
1800 | ||
1801 | This is an XS interface to Perl's C<die> function. | |
1802 | ||
1803 | Take a sprintf-style format pattern and argument list. These are used to | |
1804 | generate a string message. If the message does not end with a newline, | |
1805 | then it will be extended with some indication of the current location | |
1806 | in the code, as described for L</mess_sv>. | |
1807 | ||
1808 | The error message will be used as an exception, by default | |
1809 | returning control to the nearest enclosing C<eval>, but subject to | |
1810 | modification by a C<$SIG{__DIE__}> handler. In any case, the C<croak> | |
1811 | function never returns normally. | |
1812 | ||
1813 | For historical reasons, if C<pat> is null then the contents of C<ERRSV> | |
1814 | (C<$@>) will be used as an error message or object instead of building an | |
1815 | error message from arguments. If you want to throw a non-string object, | |
1816 | or build an error message in an SV yourself, it is preferable to use | |
1817 | the L</croak_sv> function, which does not involve clobbering C<ERRSV>. | |
1818 | ||
1819 | =cut | |
1820 | */ | |
1821 | ||
c5be433b | 1822 | #if defined(PERL_IMPLICIT_CONTEXT) |
8990e307 | 1823 | void |
cea2e8a9 | 1824 | Perl_croak_nocontext(const char *pat, ...) |
a687059c | 1825 | { |
cea2e8a9 | 1826 | dTHX; |
a687059c | 1827 | va_list args; |
cea2e8a9 | 1828 | va_start(args, pat); |
c5be433b | 1829 | vcroak(pat, &args); |
e5964223 | 1830 | NOT_REACHED; /* NOTREACHED */ |
cea2e8a9 GS |
1831 | va_end(args); |
1832 | } | |
1833 | #endif /* PERL_IMPLICIT_CONTEXT */ | |
1834 | ||
c5df3096 Z |
1835 | void |
1836 | Perl_croak(pTHX_ const char *pat, ...) | |
1837 | { | |
1838 | va_list args; | |
1839 | va_start(args, pat); | |
1840 | vcroak(pat, &args); | |
e5964223 | 1841 | NOT_REACHED; /* NOTREACHED */ |
c5df3096 Z |
1842 | va_end(args); |
1843 | } | |
1844 | ||
954c1994 | 1845 | /* |
6ad8f254 NC |
1846 | =for apidoc Am|void|croak_no_modify |
1847 | ||
1848 | Exactly equivalent to C<Perl_croak(aTHX_ "%s", PL_no_modify)>, but generates | |
72d33970 | 1849 | terser object code than using C<Perl_croak>. Less code used on exception code |
6ad8f254 NC |
1850 | paths reduces CPU cache pressure. |
1851 | ||
d8e47b5c | 1852 | =cut |
6ad8f254 NC |
1853 | */ |
1854 | ||
1855 | void | |
88772978 | 1856 | Perl_croak_no_modify(void) |
6ad8f254 | 1857 | { |
cb077ed2 | 1858 | Perl_croak_nocontext( "%s", PL_no_modify); |
6ad8f254 NC |
1859 | } |
1860 | ||
4cbe3a7d DD |
1861 | /* does not return, used in util.c perlio.c and win32.c |
1862 | This is typically called when malloc returns NULL. | |
1863 | */ | |
1864 | void | |
88772978 | 1865 | Perl_croak_no_mem(void) |
4cbe3a7d DD |
1866 | { |
1867 | dTHX; | |
77c1c05b | 1868 | |
375ed12a JH |
1869 | int fd = PerlIO_fileno(Perl_error_log); |
1870 | if (fd < 0) | |
1871 | SETERRNO(EBADF,RMS_IFI); | |
1872 | else { | |
1873 | /* Can't use PerlIO to write as it allocates memory */ | |
b469f1e0 | 1874 | PERL_UNUSED_RESULT(PerlLIO_write(fd, PL_no_mem, sizeof(PL_no_mem)-1)); |
375ed12a | 1875 | } |
4cbe3a7d DD |
1876 | my_exit(1); |
1877 | } | |
1878 | ||
3d04513d DD |
1879 | /* does not return, used only in POPSTACK */ |
1880 | void | |
1881 | Perl_croak_popstack(void) | |
1882 | { | |
1883 | dTHX; | |
1884 | PerlIO_printf(Perl_error_log, "panic: POPSTACK\n"); | |
1885 | my_exit(1); | |
1886 | } | |
1887 | ||
6ad8f254 | 1888 | /* |
c5df3096 | 1889 | =for apidoc Am|void|warn_sv|SV *baseex |
ccfc67b7 | 1890 | |
c5df3096 | 1891 | This is an XS interface to Perl's C<warn> function. |
954c1994 | 1892 | |
c5df3096 Z |
1893 | C<baseex> is the error message or object. If it is a reference, it |
1894 | will be used as-is. Otherwise it is used as a string, and if it does | |
1895 | not end with a newline then it will be extended with some indication of | |
1896 | the current location in the code, as described for L</mess_sv>. | |
9983fa3c | 1897 | |
c5df3096 Z |
1898 | The error message or object will by default be written to standard error, |
1899 | but this is subject to modification by a C<$SIG{__WARN__}> handler. | |
9983fa3c | 1900 | |
c5df3096 Z |
1901 | To warn with a simple string message, the L</warn> function may be |
1902 | more convenient. | |
954c1994 GS |
1903 | |
1904 | =cut | |
1905 | */ | |
1906 | ||
cea2e8a9 | 1907 | void |
c5df3096 | 1908 | Perl_warn_sv(pTHX_ SV *baseex) |
cea2e8a9 | 1909 | { |
c5df3096 Z |
1910 | SV *ex = mess_sv(baseex, 0); |
1911 | PERL_ARGS_ASSERT_WARN_SV; | |
1912 | if (!invoke_exception_hook(ex, TRUE)) | |
1913 | write_to_stderr(ex); | |
cea2e8a9 GS |
1914 | } |
1915 | ||
c5df3096 Z |
1916 | /* |
1917 | =for apidoc Am|void|vwarn|const char *pat|va_list *args | |
1918 | ||
1919 | This is an XS interface to Perl's C<warn> function. | |
1920 | ||
1921 | C<pat> and C<args> are a sprintf-style format pattern and encapsulated | |
1922 | argument list. These are used to generate a string message. If the | |
1923 | message does not end with a newline, then it will be extended with | |
1924 | some indication of the current location in the code, as described for | |
1925 | L</mess_sv>. | |
1926 | ||
1927 | The error message or object will by default be written to standard error, | |
1928 | but this is subject to modification by a C<$SIG{__WARN__}> handler. | |
1929 | ||
1930 | Unlike with L</vcroak>, C<pat> is not permitted to be null. | |
1931 | ||
1932 | =cut | |
1933 | */ | |
1934 | ||
c5be433b GS |
1935 | void |
1936 | Perl_vwarn(pTHX_ const char* pat, va_list *args) | |
cea2e8a9 | 1937 | { |
c5df3096 | 1938 | SV *ex = vmess(pat, args); |
7918f24d | 1939 | PERL_ARGS_ASSERT_VWARN; |
c5df3096 Z |
1940 | if (!invoke_exception_hook(ex, TRUE)) |
1941 | write_to_stderr(ex); | |
1942 | } | |
7918f24d | 1943 | |
c5df3096 Z |
1944 | /* |
1945 | =for apidoc Am|void|warn|const char *pat|... | |
87582a92 | 1946 | |
c5df3096 Z |
1947 | This is an XS interface to Perl's C<warn> function. |
1948 | ||
1949 | Take a sprintf-style format pattern and argument list. These are used to | |
1950 | generate a string message. If the message does not end with a newline, | |
1951 | then it will be extended with some indication of the current location | |
1952 | in the code, as described for L</mess_sv>. | |
1953 | ||
1954 | The error message or object will by default be written to standard error, | |
1955 | but this is subject to modification by a C<$SIG{__WARN__}> handler. | |
1956 | ||
1957 | Unlike with L</croak>, C<pat> is not permitted to be null. | |
1958 | ||
1959 | =cut | |
1960 | */ | |
8d063cd8 | 1961 | |
c5be433b | 1962 | #if defined(PERL_IMPLICIT_CONTEXT) |
cea2e8a9 GS |
1963 | void |
1964 | Perl_warn_nocontext(const char *pat, ...) | |
1965 | { | |
1966 | dTHX; | |
1967 | va_list args; | |
7918f24d | 1968 | PERL_ARGS_ASSERT_WARN_NOCONTEXT; |
cea2e8a9 | 1969 | va_start(args, pat); |
c5be433b | 1970 | vwarn(pat, &args); |
cea2e8a9 GS |
1971 | va_end(args); |
1972 | } | |
1973 | #endif /* PERL_IMPLICIT_CONTEXT */ | |
1974 | ||
1975 | void | |
1976 | Perl_warn(pTHX_ const char *pat, ...) | |
1977 | { | |
1978 | va_list args; | |
7918f24d | 1979 | PERL_ARGS_ASSERT_WARN; |
cea2e8a9 | 1980 | va_start(args, pat); |
c5be433b | 1981 | vwarn(pat, &args); |
cea2e8a9 GS |
1982 | va_end(args); |
1983 | } | |
1984 | ||
c5be433b GS |
1985 | #if defined(PERL_IMPLICIT_CONTEXT) |
1986 | void | |
1987 | Perl_warner_nocontext(U32 err, const char *pat, ...) | |
1988 | { | |
27da23d5 | 1989 | dTHX; |
c5be433b | 1990 | va_list args; |
7918f24d | 1991 | PERL_ARGS_ASSERT_WARNER_NOCONTEXT; |
c5be433b GS |
1992 | va_start(args, pat); |
1993 | vwarner(err, pat, &args); | |
1994 | va_end(args); | |
1995 | } | |
1996 | #endif /* PERL_IMPLICIT_CONTEXT */ | |
1997 | ||
599cee73 | 1998 | void |
9b387841 NC |
1999 | Perl_ck_warner_d(pTHX_ U32 err, const char* pat, ...) |
2000 | { | |
2001 | PERL_ARGS_ASSERT_CK_WARNER_D; | |
2002 | ||
2003 | if (Perl_ckwarn_d(aTHX_ err)) { | |
2004 | va_list args; | |
2005 | va_start(args, pat); | |
2006 | vwarner(err, pat, &args); | |
2007 | va_end(args); | |
2008 | } | |
2009 | } | |
2010 | ||
2011 | void | |
a2a5de95 NC |
2012 | Perl_ck_warner(pTHX_ U32 err, const char* pat, ...) |
2013 | { | |
2014 | PERL_ARGS_ASSERT_CK_WARNER; | |
2015 | ||
2016 | if (Perl_ckwarn(aTHX_ err)) { | |
2017 | va_list args; | |
2018 | va_start(args, pat); | |
2019 | vwarner(err, pat, &args); | |
2020 | va_end(args); | |
2021 | } | |
2022 | } | |
2023 | ||
2024 | void | |
864dbfa3 | 2025 | Perl_warner(pTHX_ U32 err, const char* pat,...) |
599cee73 PM |
2026 | { |
2027 | va_list args; | |
7918f24d | 2028 | PERL_ARGS_ASSERT_WARNER; |
c5be433b GS |
2029 | va_start(args, pat); |
2030 | vwarner(err, pat, &args); | |
2031 | va_end(args); | |
2032 | } | |
2033 | ||
2034 | void | |
2035 | Perl_vwarner(pTHX_ U32 err, const char* pat, va_list* args) | |
2036 | { | |
27da23d5 | 2037 | dVAR; |
7918f24d | 2038 | PERL_ARGS_ASSERT_VWARNER; |
46b27d2f LM |
2039 | if ( |
2040 | (PL_warnhook == PERL_WARNHOOK_FATAL || ckDEAD(err)) && | |
2041 | !(PL_in_eval & EVAL_KEEPERR) | |
2042 | ) { | |
a3b680e6 | 2043 | SV * const msv = vmess(pat, args); |
599cee73 | 2044 | |
594b6fac LM |
2045 | if (PL_parser && PL_parser->error_count) { |
2046 | qerror(msv); | |
2047 | } | |
2048 | else { | |
2049 | invoke_exception_hook(msv, FALSE); | |
2050 | die_unwind(msv); | |
2051 | } | |
599cee73 PM |
2052 | } |
2053 | else { | |
d13b0d77 | 2054 | Perl_vwarn(aTHX_ pat, args); |
599cee73 PM |
2055 | } |
2056 | } | |
2057 | ||
f54ba1c2 DM |
2058 | /* implements the ckWARN? macros */ |
2059 | ||
2060 | bool | |
2061 | Perl_ckwarn(pTHX_ U32 w) | |
2062 | { | |
ad287e37 | 2063 | /* If lexical warnings have not been set, use $^W. */ |
3c3f8cd6 AB |
2064 | if (isLEXWARN_off) |
2065 | return PL_dowarn & G_WARN_ON; | |
ad287e37 | 2066 | |
26c7b074 | 2067 | return ckwarn_common(w); |
f54ba1c2 DM |
2068 | } |
2069 | ||
2070 | /* implements the ckWARN?_d macro */ | |
2071 | ||
2072 | bool | |
2073 | Perl_ckwarn_d(pTHX_ U32 w) | |
2074 | { | |
ad287e37 | 2075 | /* If lexical warnings have not been set then default classes warn. */ |
3c3f8cd6 AB |
2076 | if (isLEXWARN_off) |
2077 | return TRUE; | |
ad287e37 | 2078 | |
26c7b074 NC |
2079 | return ckwarn_common(w); |
2080 | } | |
2081 | ||
2082 | static bool | |
2083 | S_ckwarn_common(pTHX_ U32 w) | |
2084 | { | |
3c3f8cd6 AB |
2085 | if (PL_curcop->cop_warnings == pWARN_ALL) |
2086 | return TRUE; | |
ad287e37 NC |
2087 | |
2088 | if (PL_curcop->cop_warnings == pWARN_NONE) | |
2089 | return FALSE; | |
2090 | ||
98fe6610 NC |
2091 | /* Check the assumption that at least the first slot is non-zero. */ |
2092 | assert(unpackWARN1(w)); | |
2093 | ||
2094 | /* Check the assumption that it is valid to stop as soon as a zero slot is | |
2095 | seen. */ | |
2096 | if (!unpackWARN2(w)) { | |
2097 | assert(!unpackWARN3(w)); | |
2098 | assert(!unpackWARN4(w)); | |
2099 | } else if (!unpackWARN3(w)) { | |
2100 | assert(!unpackWARN4(w)); | |
2101 | } | |
2102 | ||
26c7b074 NC |
2103 | /* Right, dealt with all the special cases, which are implemented as non- |
2104 | pointers, so there is a pointer to a real warnings mask. */ | |
98fe6610 NC |
2105 | do { |
2106 | if (isWARN_on(PL_curcop->cop_warnings, unpackWARN1(w))) | |
2107 | return TRUE; | |
2108 | } while (w >>= WARNshift); | |
2109 | ||
2110 | return FALSE; | |
f54ba1c2 DM |
2111 | } |
2112 | ||
72dc9ed5 NC |
2113 | /* Set buffer=NULL to get a new one. */ |
2114 | STRLEN * | |
8ee4cf24 | 2115 | Perl_new_warnings_bitfield(pTHX_ STRLEN *buffer, const char *const bits, |
72dc9ed5 | 2116 | STRLEN size) { |
5af88345 FC |
2117 | const MEM_SIZE len_wanted = |
2118 | sizeof(STRLEN) + (size > WARNsize ? size : WARNsize); | |
35da51f7 | 2119 | PERL_UNUSED_CONTEXT; |
7918f24d | 2120 | PERL_ARGS_ASSERT_NEW_WARNINGS_BITFIELD; |
72dc9ed5 | 2121 | |
10edeb5d JH |
2122 | buffer = (STRLEN*) |
2123 | (specialWARN(buffer) ? | |
2124 | PerlMemShared_malloc(len_wanted) : | |
2125 | PerlMemShared_realloc(buffer, len_wanted)); | |
72dc9ed5 NC |
2126 | buffer[0] = size; |
2127 | Copy(bits, (buffer + 1), size, char); | |
5af88345 FC |
2128 | if (size < WARNsize) |
2129 | Zero((char *)(buffer + 1) + size, WARNsize - size, char); | |
72dc9ed5 NC |
2130 | return buffer; |
2131 | } | |
f54ba1c2 | 2132 | |
e6587932 DM |
2133 | /* since we've already done strlen() for both nam and val |
2134 | * we can use that info to make things faster than | |
2135 | * sprintf(s, "%s=%s", nam, val) | |
2136 | */ | |
2137 | #define my_setenv_format(s, nam, nlen, val, vlen) \ | |
2138 | Copy(nam, s, nlen, char); \ | |
2139 | *(s+nlen) = '='; \ | |
2140 | Copy(val, s+(nlen+1), vlen, char); \ | |
2141 | *(s+(nlen+1+vlen)) = '\0' | |
2142 | ||
c5d12488 JH |
2143 | #ifdef USE_ENVIRON_ARRAY |
2144 | /* VMS' my_setenv() is in vms.c */ | |
2145 | #if !defined(WIN32) && !defined(NETWARE) | |
8d063cd8 | 2146 | void |
e1ec3a88 | 2147 | Perl_my_setenv(pTHX_ const char *nam, const char *val) |
8d063cd8 | 2148 | { |
27da23d5 | 2149 | dVAR; |
ea34f6bd | 2150 | #ifdef __amigaos4__ |
6e3136a6 AB |
2151 | amigaos4_obtain_environ(__FUNCTION__); |
2152 | #endif | |
4efc5df6 GS |
2153 | #ifdef USE_ITHREADS |
2154 | /* only parent thread can modify process environment */ | |
2155 | if (PL_curinterp == aTHX) | |
2156 | #endif | |
2157 | { | |
f2517201 | 2158 | #ifndef PERL_USE_SAFE_PUTENV |
50acdf95 | 2159 | if (!PL_use_safe_putenv) { |
b7d87861 JH |
2160 | /* most putenv()s leak, so we manipulate environ directly */ |
2161 | I32 i; | |
2162 | const I32 len = strlen(nam); | |
2163 | int nlen, vlen; | |
2164 | ||
2165 | /* where does it go? */ | |
2166 | for (i = 0; environ[i]; i++) { | |
2167 | if (strnEQ(environ[i],nam,len) && environ[i][len] == '=') | |
2168 | break; | |
2169 | } | |
c5d12488 | 2170 | |
b7d87861 JH |
2171 | if (environ == PL_origenviron) { /* need we copy environment? */ |
2172 | I32 j; | |
2173 | I32 max; | |
2174 | char **tmpenv; | |
2175 | ||
2176 | max = i; | |
2177 | while (environ[max]) | |
2178 | max++; | |
2179 | tmpenv = (char**)safesysmalloc((max+2) * sizeof(char*)); | |
2180 | for (j=0; j<max; j++) { /* copy environment */ | |
2181 | const int len = strlen(environ[j]); | |
2182 | tmpenv[j] = (char*)safesysmalloc((len+1)*sizeof(char)); | |
2183 | Copy(environ[j], tmpenv[j], len+1, char); | |
2184 | } | |
2185 | tmpenv[max] = NULL; | |
2186 | environ = tmpenv; /* tell exec where it is now */ | |
2187 | } | |
2188 | if (!val) { | |
2189 | safesysfree(environ[i]); | |
2190 | while (environ[i]) { | |
2191 | environ[i] = environ[i+1]; | |
2192 | i++; | |
2193 | } | |
ea34f6bd | 2194 | #ifdef __amigaos4__ |
6e3136a6 AB |
2195 | goto my_setenv_out; |
2196 | #else | |
b7d87861 | 2197 | return; |
6e3136a6 | 2198 | #endif |
b7d87861 JH |
2199 | } |
2200 | if (!environ[i]) { /* does not exist yet */ | |
2201 | environ = (char**)safesysrealloc(environ, (i+2) * sizeof(char*)); | |
2202 | environ[i+1] = NULL; /* make sure it's null terminated */ | |
2203 | } | |
2204 | else | |
2205 | safesysfree(environ[i]); | |
2206 | nlen = strlen(nam); | |
2207 | vlen = strlen(val); | |
2208 | ||
2209 | environ[i] = (char*)safesysmalloc((nlen+vlen+2) * sizeof(char)); | |
2210 | /* all that work just for this */ | |
2211 | my_setenv_format(environ[i], nam, nlen, val, vlen); | |
50acdf95 | 2212 | } else { |
c5d12488 | 2213 | # endif |
235c1d5f AD |
2214 | /* This next branch should only be called #if defined(HAS_SETENV), but |
2215 | Configure doesn't test for that yet. For Solaris, setenv() and unsetenv() | |
2216 | were introduced in Solaris 9, so testing for HAS UNSETENV is sufficient. | |
2217 | */ | |
12ffbb10 | 2218 | # if defined(__CYGWIN__)|| defined(__SYMBIAN32__) || defined(__riscos__) || (defined(__sun) && defined(HAS_UNSETENV)) || defined(PERL_DARWIN) |
88f5bc07 AB |
2219 | # if defined(HAS_UNSETENV) |
2220 | if (val == NULL) { | |
2221 | (void)unsetenv(nam); | |
2222 | } else { | |
2223 | (void)setenv(nam, val, 1); | |
2224 | } | |
2225 | # else /* ! HAS_UNSETENV */ | |
2226 | (void)setenv(nam, val, 1); | |
2227 | # endif /* HAS_UNSETENV */ | |
47dafe4d | 2228 | # else |
88f5bc07 AB |
2229 | # if defined(HAS_UNSETENV) |
2230 | if (val == NULL) { | |
ba88ff58 MJ |
2231 | if (environ) /* old glibc can crash with null environ */ |
2232 | (void)unsetenv(nam); | |
88f5bc07 | 2233 | } else { |
c4420975 AL |
2234 | const int nlen = strlen(nam); |
2235 | const int vlen = strlen(val); | |
2236 | char * const new_env = | |
88f5bc07 AB |
2237 | (char*)safesysmalloc((nlen + vlen + 2) * sizeof(char)); |
2238 | my_setenv_format(new_env, nam, nlen, val, vlen); | |
2239 | (void)putenv(new_env); | |
2240 | } | |
2241 | # else /* ! HAS_UNSETENV */ | |
2242 | char *new_env; | |
c4420975 AL |
2243 | const int nlen = strlen(nam); |
2244 | int vlen; | |
88f5bc07 AB |
2245 | if (!val) { |
2246 | val = ""; | |
2247 | } | |
2248 | vlen = strlen(val); | |
2249 | new_env = (char*)safesysmalloc((nlen + vlen + 2) * sizeof(char)); | |
2250 | /* all that work just for this */ | |
2251 | my_setenv_format(new_env, nam, nlen, val, vlen); | |
2252 | (void)putenv(new_env); | |
2253 | # endif /* HAS_UNSETENV */ | |
47dafe4d | 2254 | # endif /* __CYGWIN__ */ |
50acdf95 MS |
2255 | #ifndef PERL_USE_SAFE_PUTENV |
2256 | } | |
2257 | #endif | |
4efc5df6 | 2258 | } |
ea34f6bd | 2259 | #ifdef __amigaos4__ |
6e3136a6 AB |
2260 | my_setenv_out: |
2261 | amigaos4_release_environ(__FUNCTION__); | |
2262 | #endif | |
8d063cd8 LW |
2263 | } |
2264 | ||
c5d12488 | 2265 | #else /* WIN32 || NETWARE */ |
68dc0745 | 2266 | |
2267 | void | |
72229eff | 2268 | Perl_my_setenv(pTHX_ const char *nam, const char *val) |
68dc0745 | 2269 | { |
27da23d5 | 2270 | dVAR; |
eb578fdb | 2271 | char *envstr; |
c5d12488 JH |
2272 | const int nlen = strlen(nam); |
2273 | int vlen; | |
e6587932 | 2274 | |
c5d12488 JH |
2275 | if (!val) { |
2276 | val = ""; | |
ac5c734f | 2277 | } |
c5d12488 JH |
2278 | vlen = strlen(val); |
2279 | Newx(envstr, nlen+vlen+2, char); | |
2280 | my_setenv_format(envstr, nam, nlen, val, vlen); | |
2281 | (void)PerlEnv_putenv(envstr); | |
2282 | Safefree(envstr); | |
3e3baf6d TB |
2283 | } |
2284 | ||
c5d12488 | 2285 | #endif /* WIN32 || NETWARE */ |
3e3baf6d | 2286 | |
739a0b84 | 2287 | #endif /* !VMS */ |
378cc40b | 2288 | |
16d20bd9 | 2289 | #ifdef UNLINK_ALL_VERSIONS |
79072805 | 2290 | I32 |
6e732051 | 2291 | Perl_unlnk(pTHX_ const char *f) /* unlink all versions of a file */ |
378cc40b | 2292 | { |
35da51f7 | 2293 | I32 retries = 0; |
378cc40b | 2294 | |
7918f24d NC |
2295 | PERL_ARGS_ASSERT_UNLNK; |
2296 | ||
35da51f7 AL |
2297 | while (PerlLIO_unlink(f) >= 0) |
2298 | retries++; | |
2299 | return retries ? 0 : -1; | |
378cc40b LW |
2300 | } |
2301 | #endif | |
2302 | ||
99fff99d LM |
2303 | /* this is a drop-in replacement for bcopy(), except for the return |
2304 | * value, which we need to be able to emulate memcpy() */ | |
af4291a8 | 2305 | #if !defined(HAS_MEMCPY) || (!defined(HAS_MEMMOVE) && !defined(HAS_SAFE_MEMCPY)) |
99fff99d LM |
2306 | void * |
2307 | Perl_my_bcopy(const void *vfrom, void *vto, size_t len) | |
378cc40b | 2308 | { |
af4291a8 LM |
2309 | #if defined(HAS_BCOPY) && defined(HAS_SAFE_BCOPY) |
2310 | bcopy(vfrom, vto, len); | |
2311 | #else | |
99fff99d LM |
2312 | const unsigned char *from = (const unsigned char *)vfrom; |
2313 | unsigned char *to = (unsigned char *)vto; | |
378cc40b | 2314 | |
7918f24d NC |
2315 | PERL_ARGS_ASSERT_MY_BCOPY; |
2316 | ||
7c0587c8 LW |
2317 | if (from - to >= 0) { |
2318 | while (len--) | |
2319 | *to++ = *from++; | |
2320 | } | |
2321 | else { | |
2322 | to += len; | |
2323 | from += len; | |
2324 | while (len--) | |
faf8582f | 2325 | *(--to) = *(--from); |
7c0587c8 | 2326 | } |
af4291a8 LM |
2327 | #endif |
2328 | ||
99fff99d | 2329 | return vto; |
378cc40b | 2330 | } |
ffed7fef | 2331 | #endif |
378cc40b | 2332 | |
7a3f2258 | 2333 | /* this is a drop-in replacement for memset() */ |
fc36a67e | 2334 | #ifndef HAS_MEMSET |
2335 | void * | |
99fff99d | 2336 | Perl_my_memset(void *vloc, int ch, size_t len) |
fc36a67e | 2337 | { |
99fff99d | 2338 | unsigned char *loc = (unsigned char *)vloc; |
fc36a67e | 2339 | |
7918f24d NC |
2340 | PERL_ARGS_ASSERT_MY_MEMSET; |
2341 | ||
fc36a67e | 2342 | while (len--) |
2343 | *loc++ = ch; | |
99fff99d | 2344 | return vloc; |
fc36a67e | 2345 | } |
2346 | #endif | |
2347 | ||
7a3f2258 | 2348 | /* this is a drop-in replacement for bzero() */ |
7c0587c8 | 2349 | #if !defined(HAS_BZERO) && !defined(HAS_MEMSET) |
99fff99d LM |
2350 | void * |
2351 | Perl_my_bzero(void *vloc, size_t len) | |
378cc40b | 2352 | { |
99fff99d | 2353 | unsigned char *loc = (unsigned char *)vloc; |
378cc40b | 2354 | |
7918f24d NC |
2355 | PERL_ARGS_ASSERT_MY_BZERO; |
2356 | ||
378cc40b LW |
2357 | while (len--) |
2358 | *loc++ = 0; | |
99fff99d | 2359 | return vloc; |
378cc40b LW |
2360 | } |
2361 | #endif | |
7c0587c8 | 2362 | |
7a3f2258 | 2363 | /* this is a drop-in replacement for memcmp() */ |
36477c24 | 2364 | #if !defined(HAS_MEMCMP) || !defined(HAS_SANE_MEMCMP) |
99fff99d LM |
2365 | int |
2366 | Perl_my_memcmp(const void *vs1, const void *vs2, size_t len) | |
7c0587c8 | 2367 | { |
99fff99d LM |
2368 | const U8 *a = (const U8 *)vs1; |
2369 | const U8 *b = (const U8 *)vs2; | |
2370 | int tmp; | |
7c0587c8 | 2371 | |
7918f24d NC |
2372 | PERL_ARGS_ASSERT_MY_MEMCMP; |
2373 | ||
7c0587c8 | 2374 | while (len--) { |
27da23d5 | 2375 | if ((tmp = *a++ - *b++)) |
7c0587c8 LW |
2376 | return tmp; |
2377 | } | |
2378 | return 0; | |
2379 | } | |
36477c24 | 2380 | #endif /* !HAS_MEMCMP || !HAS_SANE_MEMCMP */ |
a687059c | 2381 | |
fe14fcc3 | 2382 | #ifndef HAS_VPRINTF |
d05d9be5 AD |
2383 | /* This vsprintf replacement should generally never get used, since |
2384 | vsprintf was available in both System V and BSD 2.11. (There may | |
2385 | be some cross-compilation or embedded set-ups where it is needed, | |
2386 | however.) | |
2387 | ||
2388 | If you encounter a problem in this function, it's probably a symptom | |
2389 | that Configure failed to detect your system's vprintf() function. | |
2390 | See the section on "item vsprintf" in the INSTALL file. | |
2391 | ||
2392 | This version may compile on systems with BSD-ish <stdio.h>, | |
2393 | but probably won't on others. | |
2394 | */ | |
a687059c | 2395 | |
85e6fe83 | 2396 | #ifdef USE_CHAR_VSPRINTF |
a687059c LW |
2397 | char * |
2398 | #else | |
2399 | int | |
2400 | #endif | |
d05d9be5 | 2401 | vsprintf(char *dest, const char *pat, void *args) |
a687059c LW |
2402 | { |
2403 | FILE fakebuf; | |
2404 | ||
d05d9be5 AD |
2405 | #if defined(STDIO_PTR_LVALUE) && defined(STDIO_CNT_LVALUE) |
2406 | FILE_ptr(&fakebuf) = (STDCHAR *) dest; | |
2407 | FILE_cnt(&fakebuf) = 32767; | |
2408 | #else | |
2409 | /* These probably won't compile -- If you really need | |
2410 | this, you'll have to figure out some other method. */ | |
a687059c LW |
2411 | fakebuf._ptr = dest; |
2412 | fakebuf._cnt = 32767; | |
d05d9be5 | 2413 | #endif |
35c8bce7 LW |
2414 | #ifndef _IOSTRG |
2415 | #define _IOSTRG 0 | |
2416 | #endif | |
a687059c LW |
2417 | fakebuf._flag = _IOWRT|_IOSTRG; |
2418 | _doprnt(pat, args, &fakebuf); /* what a kludge */ | |
d05d9be5 AD |
2419 | #if defined(STDIO_PTR_LVALUE) |
2420 | *(FILE_ptr(&fakebuf)++) = '\0'; | |
2421 | #else | |
2422 | /* PerlIO has probably #defined away fputc, but we want it here. */ | |
2423 | # ifdef fputc | |
2424 | # undef fputc /* XXX Should really restore it later */ | |
2425 | # endif | |
2426 | (void)fputc('\0', &fakebuf); | |
2427 | #endif | |
85e6fe83 | 2428 | #ifdef USE_CHAR_VSPRINTF |
a687059c LW |
2429 | return(dest); |
2430 | #else | |
2431 | return 0; /* perl doesn't use return value */ | |
2432 | #endif | |
2433 | } | |
2434 | ||
fe14fcc3 | 2435 | #endif /* HAS_VPRINTF */ |
a687059c | 2436 | |
4a7d1889 | 2437 | PerlIO * |
c9289b7b | 2438 | Perl_my_popen_list(pTHX_ const char *mode, int n, SV **args) |
4a7d1889 | 2439 | { |
f6fb4e44 | 2440 | #if (!defined(DOSISH) || defined(HAS_FORK)) && !defined(OS2) && !defined(VMS) && !defined(NETWARE) && !defined(__LIBCATAMOUNT__) && !defined(__amigaos4__) |
1f852d0d | 2441 | int p[2]; |
eb578fdb KW |
2442 | I32 This, that; |
2443 | Pid_t pid; | |
1f852d0d NIS |
2444 | SV *sv; |
2445 | I32 did_pipes = 0; | |
2446 | int pp[2]; | |
2447 | ||
7918f24d NC |
2448 | PERL_ARGS_ASSERT_MY_POPEN_LIST; |
2449 | ||
1f852d0d NIS |
2450 | PERL_FLUSHALL_FOR_CHILD; |
2451 | This = (*mode == 'w'); | |
2452 | that = !This; | |
284167a5 | 2453 | if (TAINTING_get) { |
1f852d0d NIS |
2454 | taint_env(); |
2455 | taint_proper("Insecure %s%s", "EXEC"); | |
2456 | } | |
2457 | if (PerlProc_pipe(p) < 0) | |
4608196e | 2458 | return NULL; |
1f852d0d NIS |
2459 | /* Try for another pipe pair for error return */ |
2460 | if (PerlProc_pipe(pp) >= 0) | |
2461 | did_pipes = 1; | |
52e18b1f | 2462 | while ((pid = PerlProc_fork()) < 0) { |
1f852d0d NIS |
2463 | if (errno != EAGAIN) { |
2464 | PerlLIO_close(p[This]); | |
4e6dfe71 | 2465 | PerlLIO_close(p[that]); |
1f852d0d NIS |
2466 | if (did_pipes) { |
2467 | PerlLIO_close(pp[0]); | |
2468 | PerlLIO_close(pp[1]); | |
2469 | } | |
4608196e | 2470 | return NULL; |
1f852d0d | 2471 | } |
a2a5de95 | 2472 | Perl_ck_warner(aTHX_ packWARN(WARN_PIPE), "Can't fork, trying again in 5 seconds"); |
1f852d0d NIS |
2473 | sleep(5); |
2474 | } | |
2475 | if (pid == 0) { | |
2476 | /* Child */ | |
1f852d0d NIS |
2477 | #undef THIS |
2478 | #undef THAT | |
2479 | #define THIS that | |
2480 | #define THAT This | |
1f852d0d NIS |
2481 | /* Close parent's end of error status pipe (if any) */ |
2482 | if (did_pipes) { | |
2483 | PerlLIO_close(pp[0]); | |
131d45a9 | 2484 | #if defined(HAS_FCNTL) && defined(F_SETFD) && defined(FD_CLOEXEC) |
1f852d0d | 2485 | /* Close error pipe automatically if exec works */ |
375ed12a JH |
2486 | if (fcntl(pp[1], F_SETFD, FD_CLOEXEC) < 0) |
2487 | return NULL; | |
1f852d0d NIS |
2488 | #endif |
2489 | } | |
2490 | /* Now dup our end of _the_ pipe to right position */ | |
2491 | if (p[THIS] != (*mode == 'r')) { | |
2492 | PerlLIO_dup2(p[THIS], *mode == 'r'); | |
2493 | PerlLIO_close(p[THIS]); | |
4e6dfe71 GS |
2494 | if (p[THAT] != (*mode == 'r')) /* if dup2() didn't close it */ |
2495 | PerlLIO_close(p[THAT]); /* close parent's end of _the_ pipe */ | |
1f852d0d | 2496 | } |
4e6dfe71 GS |
2497 | else |
2498 | PerlLIO_close(p[THAT]); /* close parent's end of _the_ pipe */ | |
1f852d0d NIS |
2499 | #if !defined(HAS_FCNTL) || !defined(F_SETFD) |
2500 | /* No automatic close - do it by hand */ | |
b7953727 JH |
2501 | # ifndef NOFILE |
2502 | # define NOFILE 20 | |
2503 | # endif | |
a080fe3d NIS |
2504 | { |
2505 | int fd; | |
2506 | ||
2507 | for (fd = PL_maxsysfd + 1; fd < NOFILE; fd++) { | |
3aed30dc | 2508 | if (fd != pp[1]) |
a080fe3d NIS |
2509 | PerlLIO_close(fd); |
2510 | } | |
1f852d0d NIS |
2511 | } |
2512 | #endif | |
a0714e2c | 2513 | do_aexec5(NULL, args-1, args-1+n, pp[1], did_pipes); |
1f852d0d NIS |
2514 | PerlProc__exit(1); |
2515 | #undef THIS | |
2516 | #undef THAT | |
2517 | } | |
2518 | /* Parent */ | |
52e18b1f | 2519 | do_execfree(); /* free any memory malloced by child on fork */ |
1f852d0d NIS |
2520 | if (did_pipes) |
2521 | PerlLIO_close(pp[1]); | |
2522 | /* Keep the lower of the two fd numbers */ | |
2523 | if (p[that] < p[This]) { | |
2524 | PerlLIO_dup2(p[This], p[that]); | |
2525 | PerlLIO_close(p[This]); | |
2526 | p[This] = p[that]; | |
2527 | } | |
4e6dfe71 GS |
2528 | else |
2529 | PerlLIO_close(p[that]); /* close child's end of pipe */ | |
2530 | ||
1f852d0d | 2531 | sv = *av_fetch(PL_fdpid,p[This],TRUE); |
862a34c6 | 2532 | SvUPGRADE(sv,SVt_IV); |
45977657 | 2533 | SvIV_set(sv, pid); |
1f852d0d NIS |
2534 | PL_forkprocess = pid; |
2535 | /* If we managed to get status pipe check for exec fail */ | |
2536 | if (did_pipes && pid > 0) { | |
2537 | int errkid; | |
bb7a0f54 MHM |
2538 | unsigned n = 0; |
2539 | SSize_t n1; | |
1f852d0d NIS |
2540 | |
2541 | while (n < sizeof(int)) { | |
2542 | n1 = PerlLIO_read(pp[0], | |
2543 | (void*)(((char*)&errkid)+n), | |
2544 | (sizeof(int)) - n); | |
2545 | if (n1 <= 0) | |
2546 | break; | |
2547 | n += n1; | |
2548 | } | |
2549 | PerlLIO_close(pp[0]); | |
2550 | did_pipes = 0; | |
2551 | if (n) { /* Error */ | |
2552 | int pid2, status; | |
8c51524e | 2553 | PerlLIO_close(p[This]); |
1f852d0d | 2554 | if (n != sizeof(int)) |
5637ef5b | 2555 | Perl_croak(aTHX_ "panic: kid popen errno read, n=%u", n); |
1f852d0d NIS |
2556 | do { |
2557 | pid2 = wait4pid(pid, &status, 0); | |
2558 | } while (pid2 == -1 && errno == EINTR); | |
2559 | errno = errkid; /* Propagate errno from kid */ | |
4608196e | 2560 | return NULL; |
1f852d0d NIS |
2561 | } |
2562 | } | |
2563 | if (did_pipes) | |
2564 | PerlLIO_close(pp[0]); | |
2565 | return PerlIO_fdopen(p[This], mode); | |
2566 | #else | |
8492b23f | 2567 | # if defined(OS2) /* Same, without fork()ing and all extra overhead... */ |
4e205ed6 | 2568 | return my_syspopen4(aTHX_ NULL, mode, n, args); |
8492b23f TC |
2569 | # elif defined(WIN32) |
2570 | return win32_popenlist(mode, n, args); | |
9d419b5f | 2571 | # else |
4a7d1889 NIS |
2572 | Perl_croak(aTHX_ "List form of piped open not implemented"); |
2573 | return (PerlIO *) NULL; | |
9d419b5f | 2574 | # endif |
1f852d0d | 2575 | #endif |
4a7d1889 NIS |
2576 | } |
2577 | ||
4dd5370d AB |
2578 | /* VMS' my_popen() is in VMS.c, same with OS/2 and AmigaOS 4. */ |
2579 | #if (!defined(DOSISH) || defined(HAS_FORK)) && !defined(VMS) && !defined(__LIBCATAMOUNT__) && !defined(__amigaos4__) | |
760ac839 | 2580 | PerlIO * |
3dd43144 | 2581 | Perl_my_popen(pTHX_ const char *cmd, const char *mode) |
a687059c LW |
2582 | { |
2583 | int p[2]; | |
eb578fdb KW |
2584 | I32 This, that; |
2585 | Pid_t pid; | |
79072805 | 2586 | SV *sv; |
bfce84ec | 2587 | const I32 doexec = !(*cmd == '-' && cmd[1] == '\0'); |
e446cec8 IZ |
2588 | I32 did_pipes = 0; |
2589 | int pp[2]; | |
a687059c | 2590 | |
7918f24d NC |
2591 | PERL_ARGS_ASSERT_MY_POPEN; |
2592 | ||
45bc9206 | 2593 | PERL_FLUSHALL_FOR_CHILD; |
ddcf38b7 IZ |
2594 | #ifdef OS2 |
2595 | if (doexec) { | |
23da6c43 | 2596 | return my_syspopen(aTHX_ cmd,mode); |
ddcf38b7 | 2597 | } |
a1d180c4 | 2598 | #endif |
8ac85365 NIS |
2599 | This = (*mode == 'w'); |
2600 | that = !This; | |
284167a5 | 2601 | if (doexec && TAINTING_get) { |
bbce6d69 | 2602 | taint_env(); |
2603 | taint_proper("Insecure %s%s", "EXEC"); | |
d48672a2 | 2604 | } |
c2267164 | 2605 | if (PerlProc_pipe(p) < 0) |
4608196e | 2606 | return NULL; |
e446cec8 IZ |
2607 | if (doexec && PerlProc_pipe(pp) >= 0) |
2608 | did_pipes = 1; | |
52e18b1f | 2609 | while ((pid = PerlProc_fork()) < 0) { |
a687059c | 2610 | if (errno != EAGAIN) { |
6ad3d225 | 2611 | PerlLIO_close(p[This]); |
b5ac89c3 | 2612 | PerlLIO_close(p[that]); |
e446cec8 IZ |
2613 | if (did_pipes) { |
2614 | PerlLIO_close(pp[0]); | |
2615 | PerlLIO_close(pp[1]); | |
2616 | } | |
a687059c | 2617 | if (!doexec) |
b3647a36 | 2618 | Perl_croak(aTHX_ "Can't fork: %s", Strerror(errno)); |
4608196e | 2619 | return NULL; |
a687059c | 2620 | } |
a2a5de95 | 2621 | Perl_ck_warner(aTHX_ packWARN(WARN_PIPE), "Can't fork, trying again in 5 seconds"); |
a687059c LW |
2622 | sleep(5); |
2623 | } | |
2624 | if (pid == 0) { | |
79072805 | 2625 | |
30ac6d9b GS |
2626 | #undef THIS |
2627 | #undef THAT | |
a687059c | 2628 | #define THIS that |
8ac85365 | 2629 | #define THAT This |
e446cec8 IZ |
2630 | if (did_pipes) { |
2631 | PerlLIO_close(pp[0]); | |
2632 | #if defined(HAS_FCNTL) && defined(F_SETFD) | |
375ed12a JH |
2633 | if (fcntl(pp[1], F_SETFD, FD_CLOEXEC) < 0) |
2634 | return NULL; | |
e446cec8 IZ |
2635 | #endif |
2636 | } | |
a687059c | 2637 | if (p[THIS] != (*mode == 'r')) { |
6ad3d225 GS |
2638 | PerlLIO_dup2(p[THIS], *mode == 'r'); |
2639 | PerlLIO_close(p[THIS]); | |
b5ac89c3 NIS |
2640 | if (p[THAT] != (*mode == 'r')) /* if dup2() didn't close it */ |
2641 | PerlLIO_close(p[THAT]); | |
a687059c | 2642 | } |
b5ac89c3 NIS |
2643 | else |
2644 | PerlLIO_close(p[THAT]); | |
4435c477 | 2645 | #ifndef OS2 |
a687059c | 2646 | if (doexec) { |
a0d0e21e | 2647 | #if !defined(HAS_FCNTL) || !defined(F_SETFD) |
ae986130 LW |
2648 | #ifndef NOFILE |
2649 | #define NOFILE 20 | |
2650 | #endif | |
a080fe3d | 2651 | { |
3aed30dc | 2652 | int fd; |
a080fe3d NIS |
2653 | |
2654 | for (fd = PL_maxsysfd + 1; fd < NOFILE; fd++) | |
2655 | if (fd != pp[1]) | |
3aed30dc | 2656 | PerlLIO_close(fd); |
a080fe3d | 2657 | } |
ae986130 | 2658 | #endif |
a080fe3d NIS |
2659 | /* may or may not use the shell */ |
2660 | do_exec3(cmd, pp[1], did_pipes); | |
6ad3d225 | 2661 | PerlProc__exit(1); |
a687059c | 2662 | } |
4435c477 | 2663 | #endif /* defined OS2 */ |
713cef20 IZ |
2664 | |
2665 | #ifdef PERLIO_USING_CRLF | |
2666 | /* Since we circumvent IO layers when we manipulate low-level | |
2667 | filedescriptors directly, need to manually switch to the | |
2668 | default, binary, low-level mode; see PerlIOBuf_open(). */ | |
2669 | PerlLIO_setmode((*mode == 'r'), O_BINARY); | |
2670 | #endif | |
3280af22 | 2671 | PL_forkprocess = 0; |
ca0c25f6 | 2672 | #ifdef PERL_USES_PL_PIDSTATUS |
3280af22 | 2673 | hv_clear(PL_pidstatus); /* we have no children */ |
ca0c25f6 | 2674 | #endif |
4608196e | 2675 | return NULL; |
a687059c LW |
2676 | #undef THIS |
2677 | #undef THAT | |
2678 | } | |
b5ac89c3 | 2679 | do_execfree(); /* free any memory malloced by child on vfork */ |
e446cec8 IZ |
2680 | if (did_pipes) |
2681 | PerlLIO_close(pp[1]); | |
8ac85365 | 2682 | if (p[that] < p[This]) { |
6ad3d225 GS |
2683 | PerlLIO_dup2(p[This], p[that]); |
2684 | PerlLIO_close(p[This]); | |
8ac85365 | 2685 | p[This] = p[that]; |
62b28dd9 | 2686 | } |
b5ac89c3 NIS |
2687 | else |
2688 | PerlLIO_close(p[that]); | |
2689 | ||
3280af22 | 2690 | sv = *av_fetch(PL_fdpid,p[This],TRUE); |
862a34c6 | 2691 | SvUPGRADE(sv,SVt_IV); |
45977657 | 2692 | SvIV_set(sv, pid); |
3280af22 | 2693 | PL_forkprocess = pid; |
e446cec8 IZ |
2694 | if (did_pipes && pid > 0) { |
2695 | int errkid; | |
bb7a0f54 MHM |
2696 | unsigned n = 0; |
2697 | SSize_t n1; | |
e446cec8 IZ |
2698 | |
2699 | while (n < sizeof(int)) { | |
2700 | n1 = PerlLIO_read(pp[0], | |
2701 | (void*)(((char*)&errkid)+n), | |
2702 | (sizeof(int)) - n); | |
2703 | if (n1 <= 0) | |
2704 | break; | |
2705 | n += n1; | |
2706 | } | |
2f96c702 IZ |
2707 | PerlLIO_close(pp[0]); |
2708 | did_pipes = 0; | |
e446cec8 | 2709 | if (n) { /* Error */ |
faa466a7 | 2710 | int pid2, status; |
8c51524e | 2711 | PerlLIO_close(p[This]); |
e446cec8 | 2712 | if (n != sizeof(int)) |
5637ef5b | 2713 | Perl_croak(aTHX_ "panic: kid popen errno read, n=%u", n); |
faa466a7 RG |
2714 | do { |
2715 | pid2 = wait4pid(pid, &status, 0); | |
2716 | } while (pid2 == -1 && errno == EINTR); | |
e446cec8 | 2717 | errno = errkid; /* Propagate errno from kid */ |
4608196e | 2718 | return NULL; |
e446cec8 IZ |
2719 | } |
2720 | } | |
2721 | if (did_pipes) | |
2722 | PerlLIO_close(pp[0]); | |
8ac85365 | 2723 | return PerlIO_fdopen(p[This], mode); |
a687059c | 2724 | } |
7c0587c8 | 2725 | #else |
2b96b0a5 JH |
2726 | #if defined(DJGPP) |
2727 | FILE *djgpp_popen(); | |
2728 | PerlIO * | |
cef6ea9d | 2729 | Perl_my_popen(pTHX_ const char *cmd, const char *mode) |
2b96b0a5 JH |
2730 | { |
2731 | PERL_FLUSHALL_FOR_CHILD; | |
2732 | /* Call system's popen() to get a FILE *, then import it. | |
2733 | used 0 for 2nd parameter to PerlIO_importFILE; | |
2734 | apparently not used | |
2735 | */ | |
2736 | return PerlIO_importFILE(djgpp_popen(cmd, mode), 0); | |
2737 | } | |
9c12f1e5 RGS |
2738 | #else |
2739 | #if defined(__LIBCATAMOUNT__) | |
2740 | PerlIO * | |
2741 | Perl_my_popen(pTHX_ const char *cmd, const char *mode) | |
2742 | { | |
2743 | return NULL; | |
2744 | } | |
2745 | #endif | |
2b96b0a5 | 2746 | #endif |
7c0587c8 LW |
2747 | |
2748 | #endif /* !DOSISH */ | |
a687059c | 2749 | |
52e18b1f GS |
2750 | /* this is called in parent before the fork() */ |
2751 | void | |
2752 | Perl_atfork_lock(void) | |
80b94025 JH |
2753 | #if defined(USE_ITHREADS) |
2754 | # ifdef USE_PERLIO | |
2755 | PERL_TSA_ACQUIRE(PL_perlio_mutex) | |
2756 | # endif | |
2757 | # ifdef MYMALLOC | |
2758 | PERL_TSA_ACQUIRE(PL_malloc_mutex) | |
2759 | # endif | |
2760 | PERL_TSA_ACQUIRE(PL_op_mutex) | |
2761 | #endif | |
52e18b1f | 2762 | { |
3db8f154 | 2763 | #if defined(USE_ITHREADS) |
20b7effb | 2764 | dVAR; |
52e18b1f | 2765 | /* locks must be held in locking order (if any) */ |
4da80956 P |
2766 | # ifdef USE_PERLIO |
2767 | MUTEX_LOCK(&PL_perlio_mutex); | |
2768 | # endif | |
52e18b1f GS |
2769 | # ifdef MYMALLOC |
2770 | MUTEX_LOCK(&PL_malloc_mutex); | |
2771 | # endif | |
2772 | OP_REFCNT_LOCK; | |
2773 | #endif | |
2774 | } | |
2775 | ||
2776 | /* this is called in both parent and child after the fork() */ | |
2777 | void | |
2778 | Perl_atfork_unlock(void) | |
80b94025 JH |
2779 | #if defined(USE_ITHREADS) |
2780 | # ifdef USE_PERLIO | |
2781 | PERL_TSA_RELEASE(PL_perlio_mutex) | |
2782 | # endif | |
2783 | # ifdef MYMALLOC | |
2784 | PERL_TSA_RELEASE(PL_malloc_mutex) | |
2785 | # endif | |
2786 | PERL_TSA_RELEASE(PL_op_mutex) | |
2787 | #endif | |
52e18b1f | 2788 | { |
3db8f154 | 2789 | #if defined(USE_ITHREADS) |
20b7effb | 2790 | dVAR; |
52e18b1f | 2791 | /* locks must be released in same order as in atfork_lock() */ |
4da80956 P |
2792 | # ifdef USE_PERLIO |
2793 | MUTEX_UNLOCK(&PL_perlio_mutex); | |
2794 | # endif | |
52e18b1f GS |
2795 | # ifdef MYMALLOC |
2796 | MUTEX_UNLOCK(&PL_malloc_mutex); | |
2797 | # endif | |
2798 | OP_REFCNT_UNLOCK; | |
2799 | #endif | |
2800 | } | |
2801 | ||
2802 | Pid_t | |
2803 | Perl_my_fork(void) | |
2804 | { | |
2805 | #if defined(HAS_FORK) | |
2806 | Pid_t pid; | |
3db8f154 | 2807 | #if defined(USE_ITHREADS) && !defined(HAS_PTHREAD_ATFORK) |
52e18b1f GS |
2808 | atfork_lock(); |
2809 | pid = fork(); | |
2810 | atfork_unlock(); | |
2811 | #else | |
2812 | /* atfork_lock() and atfork_unlock() are installed as pthread_atfork() | |
2813 | * handlers elsewhere in the code */ | |
2814 | pid = fork(); | |
2815 | #endif | |
2816 | return pid; | |
40262ff4 AB |
2817 | #elif defined(__amigaos4__) |
2818 | return amigaos_fork(); | |
52e18b1f GS |
2819 | #else |
2820 | /* this "canna happen" since nothing should be calling here if !HAS_FORK */ | |
2821 | Perl_croak_nocontext("fork() not available"); | |
b961a566 | 2822 | return 0; |
52e18b1f GS |
2823 | #endif /* HAS_FORK */ |
2824 | } | |
2825 | ||
fe14fcc3 | 2826 | #ifndef HAS_DUP2 |
fec02dd3 | 2827 | int |
ba106d47 | 2828 | dup2(int oldfd, int newfd) |
a687059c | 2829 | { |
a0d0e21e | 2830 | #if defined(HAS_FCNTL) && defined(F_DUPFD) |
fec02dd3 AD |
2831 | if (oldfd == newfd) |
2832 | return oldfd; | |
6ad3d225 | 2833 | PerlLIO_close(newfd); |
fec02dd3 | 2834 | return fcntl(oldfd, F_DUPFD, newfd); |
62b28dd9 | 2835 | #else |
fc36a67e | 2836 | #define DUP2_MAX_FDS 256 |
2837 | int fdtmp[DUP2_MAX_FDS]; | |
79072805 | 2838 | I32 fdx = 0; |
ae986130 LW |
2839 | int fd; |
2840 | ||
fe14fcc3 | 2841 | if (oldfd == newfd) |
fec02dd3 | 2842 | return oldfd; |
6ad3d225 | 2843 | PerlLIO_close(newfd); |
fc36a67e | 2844 | /* good enough for low fd's... */ |
6ad3d225 | 2845 | while ((fd = PerlLIO_dup(oldfd)) != newfd && fd >= 0) { |
fc36a67e | 2846 | if (fdx >= DUP2_MAX_FDS) { |
6ad3d225 | 2847 | PerlLIO_close(fd); |
fc36a67e | 2848 | fd = -1; |
2849 | break; | |
2850 | } | |
ae986130 | 2851 | fdtmp[fdx++] = fd; |
fc36a67e | 2852 | } |
ae986130 | 2853 | while (fdx > 0) |
6ad3d225 | 2854 | PerlLIO_close(fdtmp[--fdx]); |
fec02dd3 | 2855 | return fd; |
62b28dd9 | 2856 | #endif |
a687059c LW |
2857 | } |
2858 | #endif | |
2859 | ||
64ca3a65 | 2860 | #ifndef PERL_MICRO |
ff68c719 | 2861 | #ifdef HAS_SIGACTION |
2862 | ||
2863 | Sighandler_t | |
864dbfa3 | 2864 | Perl_rsignal(pTHX_ int signo, Sighandler_t handler) |
ff68c719 | 2865 | { |
2866 | struct sigaction act, oact; | |
2867 | ||
a10b1e10 | 2868 | #ifdef USE_ITHREADS |
20b7effb | 2869 | dVAR; |
a10b1e10 JH |
2870 | /* only "parent" interpreter can diddle signals */ |
2871 | if (PL_curinterp != aTHX) | |
8aad04aa | 2872 | return (Sighandler_t) SIG_ERR; |
a10b1e10 JH |
2873 | #endif |
2874 | ||
8aad04aa | 2875 | act.sa_handler = (void(*)(int))handler; |
ff68c719 | 2876 | sigemptyset(&act.sa_mask); |
2877 | act.sa_flags = 0; | |
2878 | #ifdef SA_RESTART | |
4ffa73a3 JH |
2879 | if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG) |
2880 | act.sa_flags |= SA_RESTART; /* SVR4, 4.3+BSD */ | |
0a8e0eff | 2881 | #endif |
358837b8 | 2882 | #if defined(SA_NOCLDWAIT) && !defined(BSDish) /* See [perl #18849] */ |
8aad04aa | 2883 | if (signo == SIGCHLD && handler == (Sighandler_t) SIG_IGN) |
85264bed CS |
2884 | act.sa_flags |= SA_NOCLDWAIT; |
2885 | #endif | |
ff68c719 | 2886 | if (sigaction(signo, &act, &oact) == -1) |
8aad04aa | 2887 | return (Sighandler_t) SIG_ERR; |
ff68c719 | 2888 | else |
8aad04aa | 2889 | return (Sighandler_t) oact.sa_handler; |
ff68c719 | 2890 | } |
2891 | ||
2892 | Sighandler_t | |
864dbfa3 | 2893 | Perl_rsignal_state(pTHX_ int signo) |
ff68c719 | 2894 | { |
2895 | struct sigaction oact; | |
96a5add6 | 2896 | PERL_UNUSED_CONTEXT; |
ff68c719 | 2897 | |
2898 | if (sigaction(signo, (struct sigaction *)NULL, &oact) == -1) | |
8aad04aa | 2899 | return (Sighandler_t) SIG_ERR; |
ff68c719 | 2900 | else |
8aad04aa | 2901 | return (Sighandler_t) oact.sa_handler; |
ff68c719 | 2902 | } |
2903 | ||
2904 | int | |
864dbfa3 | 2905 | Perl_rsignal_save(pTHX_ int signo, Sighandler_t handler, Sigsave_t *save) |
ff68c719 | 2906 | { |
20b7effb | 2907 | #ifdef USE_ITHREADS |
27da23d5 | 2908 | dVAR; |
20b7effb | 2909 | #endif |
ff68c719 | 2910 | struct sigaction act; |
2911 | ||
7918f24d NC |
2912 | PERL_ARGS_ASSERT_RSIGNAL_SAVE; |
2913 | ||
a10b1e10 JH |
2914 | #ifdef USE_ITHREADS |
2915 | /* only "parent" interpreter can diddle signals */ | |
2916 | if (PL_curinterp != aTHX) | |
2917 | return -1; | |
2918 | #endif | |
2919 | ||
8aad04aa | 2920 | act.sa_handler = (void(*)(int))handler; |
ff68c719 | 2921 | sigemptyset(&act.sa_mask); |
2922 | act.sa_flags = 0; | |
2923 | #ifdef SA_RESTART | |
4ffa73a3 JH |
2924 | if (PL_signals & PERL_SIGNALS_UNSAFE_FLAG) |
2925 | act.sa_flags |= SA_RESTART; /* SVR4, 4.3+BSD */ | |
0a8e0eff | 2926 | #endif |
36b5d377 | 2927 | #if defined(SA_NOCLDWAIT) && !defined(BSDish) /* See [perl #18849] */ |
8aad04aa | 2928 | if (signo == SIGCHLD && handler == (Sighandler_t) SIG_IGN) |
85264bed CS |
2929 | act.sa_flags |= SA_NOCLDWAIT; |
2930 | #endif | |
ff68c719 | 2931 | return sigaction(signo, &act, save); |
2932 | } | |
2933 | ||
2934 | int | |
864dbfa3 | 2935 | Perl_rsignal_restore(pTHX_ int signo, Sigsave_t *save) |
ff68c719 | 2936 | { |
20b7effb | 2937 | #ifdef USE_ITHREADS |
27da23d5 | 2938 | dVAR; |
20b7effb JH |
2939 | #endif |
2940 | PERL_UNUSED_CONTEXT; | |
a10b1e10 JH |
2941 | #ifdef USE_ITHREADS |
2942 | /* only "parent" interpreter can diddle signals */ | |
2943 | if (PL_curinterp != aTHX) | |
2944 | return -1; | |
2945 | #endif | |
2946 | ||
ff68c719 | 2947 | return sigaction(signo, save, (struct sigaction *)NULL); |
2948 | } | |
2949 | ||
2950 | #else /* !HAS_SIGACTION */ | |
2951 | ||
2952 | Sighandler_t | |
864dbfa3 | 2953 | Perl_rsignal(pTHX_ int signo, Sighandler_t handler) |
ff68c719 | 2954 | { |
39f1703b | 2955 | #if defined(USE_ITHREADS) && !defined(WIN32) |
a10b1e10 JH |
2956 | /* only "parent" interpreter can diddle signals */ |
2957 | if (PL_curinterp != aTHX) | |
8aad04aa | 2958 | return (Sighandler_t) SIG_ERR; |
a10b1e10 JH |
2959 | #endif |
2960 | ||
6ad3d225 | 2961 | return PerlProc_signal(signo, handler); |
ff68c719 | 2962 | } |
2963 | ||
fabdb6c0 | 2964 | static Signal_t |
4e35701f | 2965 | sig_trap(int signo) |
ff68c719 | 2966 | { |
27da23d5 JH |
2967 | dVAR; |
2968 | PL_sig_trapped++; | |
ff68c719 | 2969 | } |
2970 | ||
2971 | Sighandler_t | |
864dbfa3 | 2972 | Perl_rsignal_state(pTHX_ int signo) |
ff68c719 | 2973 | { |
27da23d5 | 2974 | dVAR; |
ff68c719 | 2975 | Sighandler_t oldsig; |
2976 | ||
39f1703b | 2977 | #if defined(USE_ITHREADS) && !defined(WIN32) |
a10b1e10 JH |
2978 | /* only "parent" interpreter can diddle signals */ |
2979 | if (PL_curinterp != aTHX) | |
8aad04aa | 2980 | return (Sighandler_t) SIG_ERR; |
a10b1e10 JH |
2981 | #endif |
2982 | ||
27da23d5 | 2983 | PL_sig_trapped = 0; |
6ad3d225 GS |
2984 | oldsig = PerlProc_signal(signo, sig_trap); |
2985 | PerlProc_signal(signo, oldsig); | |
27da23d5 | 2986 | if (PL_sig_trapped) |
3aed30dc | 2987 | PerlProc_kill(PerlProc_getpid(), signo); |
ff68c719 | 2988 | return oldsig; |
2989 | } | |
2990 | ||
2991 | int | |
864dbfa3 | 2992 | Perl_rsignal_save(pTHX_ int signo, Sighandler_t handler, Sigsave_t *save) |
ff68c719 | 2993 | { |
39f1703b | 2994 | #if defined(USE_ITHREADS) && !defined(WIN32) |
a10b1e10 JH |
2995 | /* only "parent" interpreter can diddle signals */ |
2996 | if (PL_curinterp != aTHX) | |
2997 | return -1; | |
2998 | #endif | |
6ad3d225 | 2999 | *save = PerlProc_signal(signo, handler); |
8aad04aa | 3000 | return (*save == (Sighandler_t) SIG_ERR) ? -1 : 0; |
ff68c719 | 3001 | } |
3002 | ||
3003 | int | |
864dbfa3 | 3004 | Perl_rsignal_restore(pTHX_ int signo, Sigsave_t *save) |
ff68c719 | 3005 | { |
39f1703b | 3006 | #if defined(USE_ITHREADS) && !defined(WIN32) |
a10b1e10 JH |
3007 | /* only "parent" interpreter can diddle signals */ |
3008 | if (PL_curinterp != aTHX) | |
3009 | return -1; | |
3010 | #endif | |
8aad04aa | 3011 | return (PerlProc_signal(signo, *save) == (Sighandler_t) SIG_ERR) ? -1 : 0; |
ff68c719 | 3012 | } |
3013 | ||
3014 | #endif /* !HAS_SIGACTION */ | |
64ca3a65 | 3015 | #endif /* !PERL_MICRO */ |
ff68c719 | 3016 | |
5f05dabc | 3017 | /* VMS' my_pclose() is in VMS.c; same with OS/2 */ |
53f73940 | 3018 | #if (!defined(DOSISH) || defined(HAS_FORK)) && !defined(VMS) && !defined(__LIBCATAMOUNT__) && !defined(__amigaos4__) |
79072805 | 3019 | I32 |
864dbfa3 | 3020 | Perl_my_pclose(pTHX_ PerlIO *ptr) |
a687059c | 3021 | { |
a687059c | 3022 | int status; |
a0d0e21e | 3023 | SV **svp; |
d8a83dd3 | 3024 | Pid_t pid; |
2e0cfa16 | 3025 | Pid_t pid2 = 0; |
03136e13 | 3026 | bool close_failed; |
4ee39169 | 3027 | dSAVEDERRNO; |
2e0cfa16 | 3028 | const int fd = PerlIO_fileno(ptr); |
e9d373c4 TC |
3029 | bool should_wait; |
3030 | ||
3031 | svp = av_fetch(PL_fdpid,fd,TRUE); | |
3032 | pid = (SvTYPE(*svp) == SVt_IV) ? SvIVX(*svp) : -1; | |
3033 | SvREFCNT_dec(*svp); | |
3034 | *svp = NULL; | |
2e0cfa16 | 3035 | |
97cb92d6 | 3036 | #if defined(USE_PERLIO) |
2e0cfa16 FC |
3037 | /* Find out whether the refcount is low enough for us to wait for the |
3038 | child proc without blocking. */ | |
e9d373c4 | 3039 | should_wait = PerlIOUnix_refcnt(fd) == 1 && pid > 0; |
b6ae43b7 | 3040 | #else |
e9d373c4 | 3041 | should_wait = pid > 0; |
b6ae43b7 | 3042 | #endif |
a687059c | 3043 | |
ddcf38b7 IZ |
3044 | #ifdef OS2 |
3045 | if (pid == -1) { /* Opened by popen. */ | |
3046 | return my_syspclose(ptr); | |
3047 | } | |
a1d180c4 | 3048 | #endif |
f1618b10 CS |
3049 | close_failed = (PerlIO_close(ptr) == EOF); |
3050 | SAVE_ERRNO; | |
2e0cfa16 | 3051 | if (should_wait) do { |
1d3434b8 GS |
3052 | pid2 = wait4pid(pid, &status, 0); |
3053 | } while (pid2 == -1 && errno == EINTR); | |
03136e13 | 3054 | if (close_failed) { |
4ee39169 | 3055 | RESTORE_ERRNO; |
03136e13 CS |
3056 | return -1; |
3057 | } | |
2e0cfa16 FC |
3058 | return( |
3059 | should_wait | |
3060 | ? pid2 < 0 ? pid2 : status == 0 ? 0 : (errno = 0, status) | |
3061 | : 0 | |
3062 | ); | |
20188a90 | 3063 | } |
9c12f1e5 RGS |
3064 | #else |
3065 | #if defined(__LIBCATAMOUNT__) | |
3066 | I32 | |
3067 | Perl_my_pclose(pTHX_ PerlIO *ptr) | |
3068 | { | |
3069 | return -1; | |
3070 | } | |
3071 | #endif | |
4633a7c4 LW |
3072 | #endif /* !DOSISH */ |
3073 | ||
e37778c2 | 3074 | #if (!defined(DOSISH) || defined(OS2) || defined(WIN32) || defined(NETWARE)) && !defined(__LIBCATAMOUNT__) |
79072805 | 3075 | I32 |
d8a83dd3 | 3076 | Perl_wait4pid(pTHX_ Pid_t pid, int *statusp, int flags) |
20188a90 | 3077 | { |
27da23d5 | 3078 | I32 result = 0; |
7918f24d | 3079 | PERL_ARGS_ASSERT_WAIT4PID; |
ca0c25f6 | 3080 | #ifdef PERL_USES_PL_PIDSTATUS |
d4c02743 TC |
3081 | if (!pid) { |
3082 | /* PERL_USES_PL_PIDSTATUS is only defined when neither | |
3083 | waitpid() nor wait4() is available, or on OS/2, which | |
3084 | doesn't appear to support waiting for a progress group | |
3085 | member, so we can only treat a 0 pid as an unknown child. | |
3086 | */ | |
3087 | errno = ECHILD; | |
3088 | return -1; | |
3089 | } | |
b7953727 | 3090 | { |
3aed30dc | 3091 | if (pid > 0) { |
12072db5 NC |
3092 | /* The keys in PL_pidstatus are now the raw 4 (or 8) bytes of the |
3093 | pid, rather than a string form. */ | |
c4420975 | 3094 | SV * const * const svp = hv_fetch(PL_pidstatus,(const char*) &pid,sizeof(Pid_t),FALSE); |
3aed30dc HS |
3095 | if (svp && *svp != &PL_sv_undef) { |
3096 | *statusp = SvIVX(*svp); | |
12072db5 NC |
3097 | (void)hv_delete(PL_pidstatus,(const char*) &pid,sizeof(Pid_t), |
3098 | G_DISCARD); | |
3aed30dc HS |
3099 | return pid; |
3100 | } | |
3101 | } | |
3102 | else { | |
3103 | HE *entry; | |
3104 | ||
3105 | hv_iterinit(PL_pidstatus); | |
3106 | if ((entry = hv_iternext(PL_pidstatus))) { | |
c4420975 | 3107 | SV * const sv = hv_iterval(PL_pidstatus,entry); |
7ea75b61 | 3108 | I32 len; |
0bcc34c2 | 3109 | const char * const spid = hv_iterkey(entry,&len); |
27da23d5 | 3110 | |
12072db5 NC |
3111 | assert (len == sizeof(Pid_t)); |
3112 | memcpy((char *)&pid, spid, len); | |
3aed30dc | 3113 | *statusp = SvIVX(sv); |
7b9a3241 NC |
3114 | /* The hash iterator is currently on this entry, so simply |
3115 | calling hv_delete would trigger the lazy delete, which on | |
f6bab5f6 | 3116 | aggregate does more work, because next call to hv_iterinit() |
7b9a3241 NC |
3117 | would spot the flag, and have to call the delete routine, |
3118 | while in the meantime any new entries can't re-use that | |
3119 | memory. */ | |
3120 | hv_iterinit(PL_pidstatus); | |
7ea75b61 | 3121 | (void)hv_delete(PL_pidstatus,spid,len,G_DISCARD); |
3aed30dc HS |
3122 | return pid; |
3123 | } | |
20188a90 LW |
3124 | } |
3125 | } | |
68a29c53 | 3126 | #endif |
79072805 | 3127 | #ifdef HAS_WAITPID |
367f3c24 IZ |
3128 | # ifdef HAS_WAITPID_RUNTIME |
3129 | if (!HAS_WAITPID_RUNTIME) | |
3130 | goto hard_way; | |
3131 | # endif | |
cddd4526 | 3132 | result = PerlProc_waitpid(pid,statusp,flags); |
dfcfdb64 | 3133 | goto finish; |
367f3c24 IZ |
3134 | #endif |
3135 | #if !defined(HAS_WAITPID) && defined(HAS_WAIT4) | |
d4c02743 | 3136 | result = wait4(pid,statusp,flags,NULL); |
dfcfdb64 | 3137 | goto finish; |
367f3c24 | 3138 | #endif |
ca0c25f6 | 3139 | #ifdef PERL_USES_PL_PIDSTATUS |
27da23d5 | 3140 | #if defined(HAS_WAITPID) && defined(HAS_WAITPID_RUNTIME) |
367f3c24 | 3141 | hard_way: |
27da23d5 | 3142 | #endif |
a0d0e21e | 3143 | { |
a0d0e21e | 3144 | if (flags) |
cea2e8a9 | 3145 | Perl_croak(aTHX_ "Can't do waitpid with flags"); |
a0d0e21e | 3146 | else { |
76e3520e | 3147 | while ((result = PerlProc_wait(statusp)) != pid && pid > 0 && result >= 0) |
a0d0e21e LW |
3148 | pidgone(result,*statusp); |
3149 | if (result < 0) | |
3150 | *statusp = -1; | |
3151 | } | |
a687059c LW |
3152 | } |
3153 | #endif | |
27da23d5 | 3154 | #if defined(HAS_WAITPID) || defined(HAS_WAIT4) |
dfcfdb64 | 3155 | finish: |
27da23d5 | 3156 | #endif |
cddd4526 NIS |
3157 | if (result < 0 && errno == EINTR) { |
3158 | PERL_ASYNC_CHECK(); | |
48dbb59e | 3159 | errno = EINTR; /* reset in case a signal handler changed $! */ |
cddd4526 NIS |
3160 | } |
3161 | return result; | |
a687059c | 3162 | } |
2986a63f | 3163 | #endif /* !DOSISH || OS2 || WIN32 || NETWARE */ |
a687059c | 3164 | |
ca0c25f6 | 3165 | #ifdef PERL_USES_PL_PIDSTATUS |
7c0587c8 | 3166 | void |
ed4173ef | 3167 | S_pidgone(pTHX_ Pid_t pid, int status) |
a687059c | 3168 | { |
eb578fdb | 3169 | SV *sv; |
a687059c | 3170 | |
12072db5 | 3171 | sv = *hv_fetch(PL_pidstatus,(const char*)&pid,sizeof(Pid_t),TRUE); |
862a34c6 | 3172 | SvUPGRADE(sv,SVt_IV); |
45977657 | 3173 | SvIV_set(sv, status); |
20188a90 | 3174 | return; |
a687059c | 3175 | } |
ca0c25f6 | 3176 | #endif |
a687059c | 3177 | |
6de23f80 | 3178 | #if defined(OS2) |
7c0587c8 | 3179 | int pclose(); |
ddcf38b7 IZ |
3180 | #ifdef HAS_FORK |
3181 | int /* Cannot prototype with I32 | |
3182 | in os2ish.h. */ | |
ba106d47 | 3183 | my_syspclose(PerlIO *ptr) |
ddcf38b7 | 3184 | #else |
79072805 | 3185 | I32 |
864dbfa3 | 3186 | Perl_my_pclose(pTHX_ PerlIO *ptr) |
a1d180c4 | 3187 | #endif |
a687059c | 3188 | { |
760ac839 | 3189 | /* Needs work for PerlIO ! */ |
c4420975 | 3190 | FILE * const f = PerlIO_findFILE(ptr); |
7452cf6a | 3191 | const I32 result = pclose(f); |
2b96b0a5 JH |
3192 | PerlIO_releaseFILE(ptr,f); |
3193 | return result; | |
3194 | } | |
3195 | #endif | |
3196 | ||
933fea7f | 3197 | #if defined(DJGPP) |
2b96b0a5 JH |
3198 | int djgpp_pclose(); |
3199 | I32 | |
3200 | Perl_my_pclose(pTHX_ PerlIO *ptr) | |
3201 | { | |
3202 | /* Needs work for PerlIO ! */ | |
c4420975 | 3203 | FILE * const f = PerlIO_findFILE(ptr); |
2b96b0a5 | 3204 | I32 result = djgpp_pclose(f); |
933fea7f | 3205 | result = (result << 8) & 0xff00; |
760ac839 LW |
3206 | PerlIO_releaseFILE(ptr,f); |
3207 | return result; | |
a687059c | 3208 | } |
7c0587c8 | 3209 | #endif |
9f68db38 | 3210 | |
16fa5c11 | 3211 | #define PERL_REPEATCPY_LINEAR 4 |
9f68db38 | 3212 | void |
5aaab254 | 3213 | Perl_repeatcpy(char *to, const char *from, I32 len, IV count) |
9f68db38 | 3214 | { |
7918f24d NC |
3215 | PERL_ARGS_ASSERT_REPEATCPY; |
3216 | ||
223f01db KW |
3217 | assert(len >= 0); |
3218 | ||
2709980d | 3219 | if (count < 0) |
d1decf2b | 3220 | croak_memory_wrap(); |
2709980d | 3221 | |
16fa5c11 VP |
3222 | if (len == 1) |
3223 | memset(to, *from, count); | |
3224 | else if (count) { | |
eb578fdb | 3225 | char *p = to; |
26e1303d | 3226 | IV items, linear, half; |
16fa5c11 VP |
3227 | |
3228 | linear = count < PERL_REPEATCPY_LINEAR ? count : PERL_REPEATCPY_LINEAR; | |
3229 | for (items = 0; items < linear; ++items) { | |
eb578fdb | 3230 | const char *q = from; |
26e1303d | 3231 | IV todo; |
16fa5c11 VP |
3232 | for (todo = len; todo > 0; todo--) |
3233 | *p++ = *q++; | |
3234 | } | |
3235 | ||
3236 | half = count / 2; | |
3237 | while (items <= half) { | |
26e1303d | 3238 | IV size = items * len; |
16fa5c11 VP |
3239 | memcpy(p, to, size); |
3240 | p += size; | |
3241 | items *= 2; | |
9f68db38 | 3242 | } |
16fa5c11 VP |
3243 | |
3244 | if (count > items) | |
3245 | memcpy(p, to, (count - items) * len); | |
9f68db38 LW |
3246 | } |
3247 | } | |
0f85fab0 | 3248 | |
fe14fcc3 | 3249 | #ifndef HAS_RENAME |
79072805 | 3250 | I32 |
4373e329 | 3251 | Perl_same_dirent(pTHX_ const char *a, const char *b) |
62b28dd9 | 3252 | { |
93a17b20 LW |
3253 | char *fa = strrchr(a,'/'); |
3254 | char *fb = strrchr(b,'/'); | |
c623ac67 GS |
3255 | Stat_t tmpstatbuf1; |
3256 | Stat_t tmpstatbuf2; | |
c4420975 | 3257 | SV * const tmpsv = sv_newmortal(); |
62b28dd9 | 3258 | |
7918f24d NC |
3259 | PERL_ARGS_ASSERT_SAME_DIRENT; |
3260 | ||
62b28dd9 LW |
3261 | if (fa) |
3262 | fa++; | |
3263 | else | |
3264 | fa = a; | |
3265 | if (fb) | |
3266 | fb++; | |
3267 | else | |
3268 | fb = b; | |
3269 | if (strNE(a,b)) | |
3270 | return FALSE; | |
3271 | if (fa == a) | |
76f68e9b | 3272 | sv_setpvs(tmpsv, "."); |
62b28dd9 | 3273 | else |
46fc3d4c | 3274 | sv_setpvn(tmpsv, a, fa - a); |
95a20fc0 | 3275 | if (PerlLIO_stat(SvPVX_const(tmpsv), &tmpstatbuf1) < 0) |
62b28dd9 LW |
3276 | return FALSE; |
3277 | if (fb == b) | |
76f68e9b | 3278 | sv_setpvs(tmpsv, "."); |
62b28dd9 | 3279 | else |
46fc3d4c | 3280 | sv_setpvn(tmpsv, b, fb - b); |
95a20fc0 | 3281 | if (PerlLIO_stat(SvPVX_const(tmpsv), &tmpstatbuf2) < 0) |
62b28dd9 LW |
3282 | return FALSE; |
3283 | return tmpstatbuf1.st_dev == tmpstatbuf2.st_dev && | |
3284 | tmpstatbuf1.st_ino == tmpstatbuf2.st_ino; | |
3285 | } | |
fe14fcc3 LW |
3286 | #endif /* !HAS_RENAME */ |
3287 | ||
491527d0 | 3288 | char* |
7f315aed NC |
3289 | Perl_find_script(pTHX_ const char *scriptname, bool dosearch, |
3290 | const char *const *const search_ext, I32 flags) | |
491527d0 | 3291 | { |
bd61b366 SS |
3292 | const char *xfound = NULL; |
3293 | char *xfailed = NULL; | |
0f31cffe | 3294 | char tmpbuf[MAXPATHLEN]; |
eb578fdb | 3295 | char *s; |
5f74f29c | 3296 | I32 len = 0; |
491527d0 | 3297 | int retval; |
39a02377 | 3298 | char *bufend; |
7c458fae | 3299 | #if defined(DOSISH) && !defined(OS2) |
491527d0 GS |
3300 | # define SEARCH_EXTS ".bat", ".cmd", NULL |
3301 | # define MAX_EXT_LEN 4 | |
3302 | #endif | |
3303 | #ifdef OS2 | |
3304 | # define SEARCH_EXTS ".cmd", ".btm", ".bat", ".pl", NULL | |
3305 | # define MAX_EXT_LEN 4 | |
3306 | #endif | |
3307 | #ifdef VMS | |
3308 | # define SEARCH_EXTS ".pl", ".com", NULL | |
3309 | # define MAX_EXT_LEN 4 | |
3310 | #endif | |
3311 | /* additional extensions to try in each dir if scriptname not found */ | |
3312 | #ifdef SEARCH_EXTS | |
0bcc34c2 | 3313 | static const char *const exts[] = { SEARCH_EXTS }; |
7f315aed | 3314 | const char *const *const ext = search_ext ? search_ext : exts; |
491527d0 | 3315 | int extidx = 0, i = 0; |
bd61b366 | 3316 | const char *curext = NULL; |
491527d0 | 3317 | #else |
53c1dcc0 | 3318 | PERL_UNUSED_ARG(search_ext); |
491527d0 GS |
3319 | # define MAX_EXT_LEN 0 |
3320 | #endif | |
3321 | ||
7918f24d NC |
3322 | PERL_ARGS_ASSERT_FIND_SCRIPT; |
3323 | ||
491527d0 GS |
3324 | /* |
3325 | * If dosearch is true and if scriptname does not contain path | |
3326 | * delimiters, search the PATH for scriptname. | |
3327 | * | |
3328 | * If SEARCH_EXTS is also defined, will look for each | |
3329 | * scriptname{SEARCH_EXTS} whenever scriptname is not found | |
3330 | * while searching the PATH. | |
3331 | * | |
3332 | * Assuming SEARCH_EXTS is C<".foo",".bar",NULL>, PATH search | |
3333 | * proceeds as follows: | |
3334 | * If DOSISH or VMSISH: | |
3335 | * + look for ./scriptname{,.foo,.bar} | |
3336 | * + search the PATH for scriptname{,.foo,.bar} | |
3337 | * | |
3338 | * If !DOSISH: | |
3339 | * + look *only* in the PATH for scriptname{,.foo,.bar} (note | |
3340 | * this will not look in '.' if it's not in the PATH) | |
3341 | */ | |
84486fc6 | 3342 | tmpbuf[0] = '\0'; |
491527d0 GS |
3343 | |
3344 | #ifdef VMS | |
3345 | # ifdef ALWAYS_DEFTYPES | |
3346 | len = strlen(scriptname); | |
3347 | if (!(len == 1 && *scriptname == '-') && scriptname[len-1] != ':') { | |
c4420975 | 3348 | int idx = 0, deftypes = 1; |
491527d0 GS |
3349 | bool seen_dot = 1; |
3350 | ||
bd61b366 | 3351 | const int hasdir = !dosearch || (strpbrk(scriptname,":[</") != NULL); |
491527d0 GS |
3352 | # else |
3353 | if (dosearch) { | |
c4420975 | 3354 | int idx = 0, deftypes = 1; |
491527d0 GS |
3355 | bool seen_dot = 1; |
3356 | ||
bd61b366 | 3357 | const int hasdir = (strpbrk(scriptname,":[</") != NULL); |
491527d0 GS |
3358 | # endif |
3359 | /* The first time through, just add SEARCH_EXTS to whatever we | |
3360 | * already have, so we can check for default file types. */ | |
3361 | while (deftypes || | |
84486fc6 | 3362 | (!hasdir && my_trnlnm("DCL$PATH",tmpbuf,idx++)) ) |
491527d0 | 3363 | { |
2aa28b86 | 3364 | Stat_t statbuf; |
491527d0 GS |
3365 | if (deftypes) { |
3366 | deftypes = 0; | |
84486fc6 | 3367 | *tmpbuf = '\0'; |
491527d0 | 3368 | } |
84486fc6 GS |
3369 | if ((strlen(tmpbuf) + strlen(scriptname) |
3370 | + MAX_EXT_LEN) >= sizeof tmpbuf) | |
491527d0 | 3371 | continue; /* don't search dir with too-long name */ |
6fca0082 | 3372 | my_strlcat(tmpbuf, scriptname, sizeof(tmpbuf)); |
491527d0 GS |
3373 | #else /* !VMS */ |
3374 | ||
3375 | #ifdef DOSISH | |
3376 | if (strEQ(scriptname, "-")) | |
3377 | dosearch = 0; | |
3378 | if (dosearch) { /* Look in '.' first. */ | |
fe2774ed | 3379 | const char *cur = scriptname; |
491527d0 GS |
3380 | #ifdef SEARCH_EXTS |
3381 | if ((curext = strrchr(scriptname,'.'))) /* possible current ext */ | |
3382 | while (ext[i]) | |
3383 | if (strEQ(ext[i++],curext)) { | |
3384 | extidx = -1; /* already has an ext */ | |
3385 | break; | |
3386 | } | |
3387 | do { | |
3388 | #endif | |
3389 | DEBUG_p(PerlIO_printf(Perl_debug_log, | |
3390 | "Looking for %s\n",cur)); | |
45a23732 | 3391 | { |
0cc19a43 | 3392 | Stat_t statbuf; |
45a23732 DD |
3393 | if (PerlLIO_stat(cur,&statbuf) >= 0 |
3394 | && !S_ISDIR(statbuf.st_mode)) { | |
3395 | dosearch = 0; | |
3396 | scriptname = cur; | |
491527d0 | 3397 | #ifdef SEARCH_EXTS |
45a23732 | 3398 | break; |
491527d0 | 3399 | #endif |
45a23732 | 3400 | } |
491527d0 GS |
3401 | } |
3402 | #ifdef SEARCH_EXTS | |
3403 | if (cur == scriptname) { | |
3404 | len = strlen(scriptname); | |
84486fc6 | 3405 | if (len+MAX_EXT_LEN+1 >= sizeof(tmpbuf)) |
491527d0 | 3406 | break; |
9e4425f7 SH |
3407 | my_strlcpy(tmpbuf, scriptname, sizeof(tmpbuf)); |
3408 | cur = tmpbuf; | |
491527d0 GS |
3409 | } |
3410 | } while (extidx >= 0 && ext[extidx] /* try an extension? */ | |
6fca0082 | 3411 | && my_strlcpy(tmpbuf+len, ext[extidx++], sizeof(tmpbuf) - len)); |
491527d0 GS |
3412 | #endif |
3413 | } | |
3414 | #endif | |
3415 | ||
3416 | if (dosearch && !strchr(scriptname, '/') | |
3417 | #ifdef DOSISH | |
3418 | && !strchr(scriptname, '\\') | |
3419 | #endif | |
cd39f2b6 | 3420 | && (s = PerlEnv_getenv("PATH"))) |
cd39f2b6 | 3421 | { |
491527d0 | 3422 | bool seen_dot = 0; |
92f0c265 | 3423 | |
39a02377 DM |
3424 | bufend = s + strlen(s); |
3425 | while (s < bufend) { | |
45a23732 | 3426 | Stat_t statbuf; |
7c458fae | 3427 | # ifdef DOSISH |
491527d0 | 3428 | for (len = 0; *s |
491527d0 | 3429 | && *s != ';'; len++, s++) { |
84486fc6 GS |
3430 | if (len < sizeof tmpbuf) |
3431 | tmpbuf[len] = *s; | |
491527d0 | 3432 | } |
84486fc6 GS |
3433 | if (len < sizeof tmpbuf) |
3434 | tmpbuf[len] = '\0'; | |
7c458fae | 3435 | # else |
39a02377 | 3436 | s = delimcpy(tmpbuf, tmpbuf + sizeof tmpbuf, s, bufend, |
491527d0 GS |
3437 | ':', |
3438 | &len); | |
7c458fae | 3439 | # endif |
39a02377 | 3440 | if (s < bufend) |
491527d0 | 3441 | s++; |
84486fc6 | 3442 | if (len + 1 + strlen(scriptname) + MAX_EXT_LEN >= sizeof tmpbuf) |
491527d0 GS |
3443 | continue; /* don't search dir with too-long name */ |
3444 | if (len | |
7c458fae | 3445 | # ifdef DOSISH |
84486fc6 GS |
3446 | && tmpbuf[len - 1] != '/' |
3447 | && tmpbuf[len - 1] != '\\' | |
490a0e98 | 3448 | # endif |
491527d0 | 3449 | ) |
84486fc6 GS |
3450 | tmpbuf[len++] = '/'; |
3451 | if (len == 2 && tmpbuf[0] == '.') | |
491527d0 | 3452 | seen_dot = 1; |
28f0d0ec | 3453 | (void)my_strlcpy(tmpbuf + len, scriptname, sizeof(tmpbuf) - len); |
491527d0 GS |
3454 | #endif /* !VMS */ |
3455 | ||
3456 | #ifdef SEARCH_EXTS | |
84486fc6 | 3457 | len = strlen(tmpbuf); |
491527d0 GS |
3458 | if (extidx > 0) /* reset after previous loop */ |
3459 | extidx = 0; | |
3460 | do { | |
3461 | #endif | |
84486fc6 | 3462 | DEBUG_p(PerlIO_printf(Perl_debug_log, "Looking for %s\n",tmpbuf)); |
45a23732 DD |
3463 | retval = PerlLIO_stat(tmpbuf,&statbuf); |
3464 | if (S_ISDIR(statbuf.st_mode)) { | |
017f25f1 IZ |
3465 | retval = -1; |
3466 | } | |
491527d0 GS |
3467 | #ifdef SEARCH_EXTS |
3468 | } while ( retval < 0 /* not there */ | |
3469 | && extidx>=0 && ext[extidx] /* try an extension? */ | |
6fca0082 | 3470 | && my_strlcpy(tmpbuf+len, ext[extidx++], sizeof(tmpbuf) - len) |
491527d0 GS |
3471 | ); |
3472 | #endif | |
3473 | if (retval < 0) | |
3474 | continue; | |
45a23732 DD |
3475 | if (S_ISREG(statbuf.st_mode) |
3476 | && cando(S_IRUSR,TRUE,&statbuf) | |
e37778c2 | 3477 | #if !defined(DOSISH) |
45a23732 | 3478 | && cando(S_IXUSR,TRUE,&statbuf) |
491527d0 GS |
3479 | #endif |
3480 | ) | |
3481 | { | |
3aed30dc | 3482 | xfound = tmpbuf; /* bingo! */ |
491527d0 GS |
3483 | break; |
3484 | } | |
3485 | if (!xfailed) | |
84486fc6 | 3486 | xfailed = savepv(tmpbuf); |
491527d0 GS |
3487 | } |
3488 | #ifndef DOSISH | |
45a23732 DD |
3489 | { |
3490 | Stat_t statbuf; | |
3491 | if (!xfound && !seen_dot && !xfailed && | |
3492 | (PerlLIO_stat(scriptname,&statbuf) < 0 | |
3493 | || S_ISDIR(statbuf.st_mode))) | |
3494 | #endif | |
3495 | seen_dot = 1; /* Disable message. */ | |
3496 | #ifndef DOSISH | |
3497 | } | |
491527d0 | 3498 | #endif |
9ccb31f9 GS |
3499 | if (!xfound) { |
3500 | if (flags & 1) { /* do or die? */ | |
6ad282c7 | 3501 | /* diag_listed_as: Can't execute %s */ |
3aed30dc | 3502 | Perl_croak(aTHX_ "Can't %s %s%s%s", |
9ccb31f9 GS |
3503 | (xfailed ? "execute" : "find"), |
3504 | (xfailed ? xfailed : scriptname), | |
3505 | (xfailed ? "" : " on PATH"), | |
3506 | (xfailed || seen_dot) ? "" : ", '.' not in PATH"); | |
3507 | } | |
bd61b366 | 3508 | scriptname = NULL; |
9ccb31f9 | 3509 | } |
43c5f42d | 3510 | Safefree(xfailed); |
491527d0 GS |
3511 | scriptname = xfound; |
3512 | } | |
bd61b366 | 3513 | return (scriptname ? savepv(scriptname) : NULL); |
491527d0 GS |
3514 | } |
3515 | ||
ba869deb GS |
3516 | #ifndef PERL_GET_CONTEXT_DEFINED |
3517 | ||
3518 | void * | |
3519 | Perl_get_context(void) | |
3520 | { | |
3db8f154 | 3521 | #if defined(USE_ITHREADS) |
20b7effb | 3522 | dVAR; |
ba869deb GS |
3523 | # ifdef OLD_PTHREADS_API |
3524 | pthread_addr_t t; | |
5637ef5b NC |
3525 | int error = pthread_getspecific(PL_thr_key, &t) |
3526 | if (error) | |
3527 | Perl_croak_nocontext("panic: pthread_getspecific, error=%d", error); | |
ba869deb GS |
3528 | return (void*)t; |
3529 | # else | |
bce813aa | 3530 | # ifdef I_MACH_CTHREADS |
8b8b35ab | 3531 | return (void*)cthread_data(cthread_self()); |
bce813aa | 3532 | # else |
8b8b35ab JH |
3533 | return (void*)PTHREAD_GETSPECIFIC(PL_thr_key); |
3534 | # endif | |
c44d3fdb | 3535 | # endif |
ba869deb GS |
3536 | #else |
3537 | return (void*)NULL; | |
3538 | #endif | |
3539 | } | |
3540 | ||
3541 | void | |
3542 | Perl_set_context(void *t) | |
3543 | { | |
20b7effb | 3544 | #if defined(USE_ITHREADS) |
8772537c | 3545 | dVAR; |
20b7effb | 3546 | #endif |
7918f24d | 3547 | PERL_ARGS_ASSERT_SET_CONTEXT; |
3db8f154 | 3548 | #if defined(USE_ITHREADS) |
c44d3fdb GS |
3549 | # ifdef I_MACH_CTHREADS |
3550 | cthread_set_data(cthread_self(), t); | |
3551 | # else | |
5637ef5b NC |
3552 | { |
3553 | const int error = pthread_setspecific(PL_thr_key, t); | |
3554 | if (error) | |
3555 | Perl_croak_nocontext("panic: pthread_setspecific, error=%d", error); | |
3556 | } | |
c44d3fdb | 3557 | # endif |
b464bac0 | 3558 | #else |
8772537c | 3559 | PERL_UNUSED_ARG(t); |
ba869deb GS |
3560 | #endif |
3561 | } | |
3562 | ||
3563 | #endif /* !PERL_GET_CONTEXT_DEFINED */ | |
491527d0 | 3564 | |
27da23d5 | 3565 | #if defined(PERL_GLOBAL_STRUCT) && !defined(PERL_GLOBAL_STRUCT_PRIVATE) |
22239a37 | 3566 | struct perl_vars * |
864dbfa3 | 3567 | Perl_GetVars(pTHX) |
22239a37 | 3568 | { |
23491f1d JH |
3569 | PERL_UNUSED_CONTEXT; |
3570 | return &PL_Vars; | |
22239a37 | 3571 | } |
31fb1209 NIS |
3572 | #endif |
3573 | ||
1cb0ed9b | 3574 | char ** |
864dbfa3 | 3575 | Perl_get_op_names(pTHX) |
31fb1209 | 3576 | { |
96a5add6 AL |
3577 | PERL_UNUSED_CONTEXT; |
3578 | return (char **)PL_op_name; | |
31fb1209 NIS |
3579 | } |
3580 | ||
1cb0ed9b | 3581 | char ** |
864dbfa3 | 3582 | Perl_get_op_descs(pTHX) |
31fb1209 | 3583 | { |
96a5add6 AL |
3584 | PERL_UNUSED_CONTEXT; |
3585 | return (char **)PL_op_desc; | |
31fb1209 | 3586 | } |
9e6b2b00 | 3587 | |
e1ec3a88 | 3588 | const char * |
864dbfa3 | 3589 | Perl_get_no_modify(pTHX) |
9e6b2b00 | 3590 | { |
96a5add6 AL |
3591 | PERL_UNUSED_CONTEXT; |
3592 | return PL_no_modify; | |
9e6b2b00 GS |
3593 | } |
3594 | ||
3595 | U32 * | |
864dbfa3 | 3596 | Perl_get_opargs(pTHX) |
9e6b2b00 | 3597 | { |
96a5add6 AL |
3598 | PERL_UNUSED_CONTEXT; |
3599 | return (U32 *)PL_opargs; | |
9e6b2b00 | 3600 | } |
51aa15f3 | 3601 | |
0cb96387 GS |
3602 | PPADDR_t* |
3603 | Perl_get_ppaddr(pTHX) | |
3604 | { | |
96a5add6 AL |
3605 | dVAR; |
3606 | PERL_UNUSED_CONTEXT; | |
3607 | return (PPADDR_t*)PL_ppaddr; | |
0cb96387 GS |
3608 | } |
3609 | ||
a6c40364 GS |
3610 | #ifndef HAS_GETENV_LEN |
3611 | char * | |
bf4acbe4 | 3612 | Perl_getenv_len(pTHX_ const char *env_elem, unsigned long *len) |
a6c40364 | 3613 | { |
8772537c | 3614 | char * const env_trans = PerlEnv_getenv(env_elem); |
96a5add6 | 3615 | PERL_UNUSED_CONTEXT; |
7918f24d | 3616 | PERL_ARGS_ASSERT_GETENV_LEN; |
a6c40364 GS |
3617 | if (env_trans) |
3618 | *len = strlen(env_trans); | |
3619 | return env_trans; | |
f675dbe5 CB |
3620 | } |
3621 | #endif | |
3622 | ||
dc9e4912 GS |
3623 | |
3624 | MGVTBL* | |
864dbfa3 | 3625 | Perl_get_vtbl(pTHX_ int vtbl_id) |
dc9e4912 | 3626 | { |
96a5add6 | 3627 | PERL_UNUSED_CONTEXT; |
dc9e4912 | 3628 | |
c7fdacb9 | 3629 | return (vtbl_id < 0 || vtbl_id >= magic_vtable_max) |
31114fe9 | 3630 | ? NULL : (MGVTBL*)PL_magic_vtables + vtbl_id; |
dc9e4912 GS |
3631 | } |
3632 | ||
767df6a1 | 3633 | I32 |
864dbfa3 | 3634 | Perl_my_fflush_all(pTHX) |
767df6a1 | 3635 | { |
97cb92d6 | 3636 | #if defined(USE_PERLIO) || defined(FFLUSH_NULL) |
ce720889 | 3637 | return PerlIO_flush(NULL); |
767df6a1 | 3638 | #else |
8fbdfb7c | 3639 | # if defined(HAS__FWALK) |
f13a2bc0 | 3640 | extern int fflush(FILE *); |
74cac757 JH |
3641 | /* undocumented, unprototyped, but very useful BSDism */ |
3642 | extern void _fwalk(int (*)(FILE *)); | |
8fbdfb7c | 3643 | _fwalk(&fflush); |
74cac757 | 3644 | return 0; |
8fa7f367 | 3645 | # else |
8fbdfb7c | 3646 | # if defined(FFLUSH_ALL) && defined(HAS_STDIO_STREAM_ARRAY) |
8fa7f367 | 3647 | long open_max = -1; |
8fbdfb7c | 3648 | # ifdef PERL_FFLUSH_ALL_FOPEN_MAX |
d2201af2 | 3649 | open_max = PERL_FFLUSH_ALL_FOPEN_MAX; |
8fbdfb7c | 3650 | # else |
8fa7f367 | 3651 | # if defined(HAS_SYSCONF) && defined(_SC_OPEN_MAX) |
767df6a1 | 3652 | open_max = sysconf(_SC_OPEN_MAX); |
8fa7f367 JH |
3653 | # else |
3654 | # ifdef FOPEN_MAX | |
74cac757 | 3655 | open_max = FOPEN_MAX; |
8fa7f367 JH |
3656 | # else |
3657 | # ifdef OPEN_MAX | |
74cac757 | 3658 | open_max = OPEN_MAX; |
8fa7f367 JH |
3659 | # else |
3660 | # ifdef _NFILE | |
d2201af2 | 3661 | open_max = _NFILE; |
8fa7f367 JH |
3662 | # endif |
3663 | # endif | |
74cac757 | 3664 | # endif |
767df6a1 JH |
3665 | # endif |
3666 | # endif | |
767df6a1 JH |
3667 | if (open_max > 0) { |
3668 | long i; | |
3669 | for (i = 0; i < open_max; i++) | |
d2201af2 AD |
3670 | if (STDIO_STREAM_ARRAY[i]._file >= 0 && |
3671 | STDIO_STREAM_ARRAY[i]._file < open_max && | |
3672 | STDIO_STREAM_ARRAY[i]._flag) | |
3673 | PerlIO_flush(&STDIO_STREAM_ARRAY[i]); | |
767df6a1 JH |
3674 | return 0; |
3675 | } | |
8fbdfb7c | 3676 | # endif |
93189314 | 3677 | SETERRNO(EBADF,RMS_IFI); |
767df6a1 | 3678 | return EOF; |
74cac757 | 3679 | # endif |
767df6a1 JH |
3680 | #endif |
3681 | } | |
097ee67d | 3682 | |
69282e91 | 3683 | void |
45219de6 | 3684 | Perl_report_wrongway_fh(pTHX_ const GV *gv, const char have) |
a5390457 NC |
3685 | { |
3686 | if (ckWARN(WARN_IO)) { | |
0223a801 | 3687 | HEK * const name |
c6e4ff34 | 3688 | = gv && (isGV_with_GP(gv)) |
0223a801 | 3689 | ? GvENAME_HEK((gv)) |
3b46b707 | 3690 | : NULL; |
a5390457 NC |
3691 | const char * const direction = have == '>' ? "out" : "in"; |
3692 | ||
b3c81598 | 3693 | if (name && HEK_LEN(name)) |
a5390457 | 3694 | Perl_warner(aTHX_ packWARN(WARN_IO), |
0223a801 | 3695 | "Filehandle %"HEKf" opened only for %sput", |
10bafe90 | 3696 | HEKfARG(name), direction); |
a5390457 NC |
3697 | else |
3698 | Perl_warner(aTHX_ packWARN(WARN_IO), | |
3699 | "Filehandle opened only for %sput", direction); | |
3700 | } | |
3701 | } | |
3702 | ||
3703 | void | |
831e4cc3 | 3704 | Perl_report_evil_fh(pTHX_ const GV *gv) |
bc37a18f | 3705 | { |
65820a28 | 3706 | const IO *io = gv ? GvIO(gv) : NULL; |
831e4cc3 | 3707 | const PERL_BITFIELD16 op = PL_op->op_type; |
a5390457 NC |
3708 | const char *vile; |
3709 | I32 warn_type; | |
3710 | ||
65820a28 | 3711 | if (io && IoTYPE(io) == IoTYPE_CLOSED) { |
a5390457 NC |
3712 | vile = "closed"; |
3713 | warn_type = WARN_CLOSED; | |
2dd78f96 JH |
3714 | } |
3715 | else { | |
a5390457 NC |
3716 | vile = "unopened"; |
3717 | warn_type = WARN_UNOPENED; | |
3718 | } | |
3719 | ||
3720 | if (ckWARN(warn_type)) { | |
3b46b707 | 3721 | SV * const name |
5c5c5f45 | 3722 | = gv && isGV_with_GP(gv) && GvENAMELEN(gv) ? |
3b46b707 | 3723 | sv_2mortal(newSVhek(GvENAME_HEK(gv))) : NULL; |
a5390457 NC |
3724 | const char * const pars = |
3725 | (const char *)(OP_IS_FILETEST(op) ? "" : "()"); | |
3726 | const char * const func = | |
3727 | (const char *) | |
d955f84c FC |
3728 | (op == OP_READLINE || op == OP_RCATLINE |
3729 | ? "readline" : /* "<HANDLE>" not nice */ | |
a5390457 | 3730 | op == OP_LEAVEWRITE ? "write" : /* "write exit" not nice */ |
a5390457 NC |
3731 | PL_op_desc[op]); |
3732 | const char * const type = | |
3733 | (const char *) | |
65820a28 | 3734 | (OP_IS_SOCKET(op) || (io && IoTYPE(io) == IoTYPE_SOCKET) |
a5390457 | 3735 | ? "socket" : "filehandle"); |
1e00d6e9 | 3736 | const bool have_name = name && SvCUR(name); |
65d99836 FC |
3737 | Perl_warner(aTHX_ packWARN(warn_type), |
3738 | "%s%s on %s %s%s%"SVf, func, pars, vile, type, | |
3739 | have_name ? " " : "", | |
3740 | SVfARG(have_name ? name : &PL_sv_no)); | |
3741 | if (io && IoDIRP(io) && !(IoFLAGS(io) & IOf_FAKE_DIRP)) | |
a5390457 NC |
3742 | Perl_warner( |
3743 | aTHX_ packWARN(warn_type), | |
65d99836 FC |
3744 | "\t(Are you trying to call %s%s on dirhandle%s%"SVf"?)\n", |
3745 | func, pars, have_name ? " " : "", | |
3746 | SVfARG(have_name ? name : &PL_sv_no) | |
a5390457 | 3747 | ); |
bc37a18f | 3748 | } |
69282e91 | 3749 | } |
a926ef6b | 3750 | |
f6adc668 | 3751 | /* To workaround core dumps from the uninitialised tm_zone we get the |
e72cf795 JH |
3752 | * system to give us a reasonable struct to copy. This fix means that |
3753 | * strftime uses the tm_zone and tm_gmtoff values returned by | |
3754 | * localtime(time()). That should give the desired result most of the | |
3755 | * time. But probably not always! | |
3756 | * | |
f6adc668 JH |
3757 | * This does not address tzname aspects of NETaa14816. |
3758 | * | |
e72cf795 | 3759 | */ |
f6adc668 | 3760 | |
61b27c87 | 3761 | #ifdef __GLIBC__ |
e72cf795 JH |
3762 | # ifndef STRUCT_TM_HASZONE |
3763 | # define STRUCT_TM_HASZONE | |
3764 | # endif | |
3765 | #endif | |
3766 | ||
f6adc668 JH |
3767 | #ifdef STRUCT_TM_HASZONE /* Backward compat */ |
3768 | # ifndef HAS_TM_TM_ZONE | |
3769 | # define HAS_TM_TM_ZONE | |
3770 | # endif | |
3771 | #endif | |
3772 | ||
e72cf795 | 3773 | void |
f1208910 | 3774 | Perl_init_tm(pTHX_ struct tm *ptm) /* see mktime, strftime and asctime */ |
e72cf795 | 3775 | { |
f6adc668 | 3776 | #ifdef HAS_TM_TM_ZONE |
e72cf795 | 3777 | Time_t now; |
1b6737cc | 3778 | const struct tm* my_tm; |
dc3bf405 | 3779 | PERL_UNUSED_CONTEXT; |
7918f24d | 3780 | PERL_ARGS_ASSERT_INIT_TM; |
e72cf795 | 3781 | (void)time(&now); |
82c57498 | 3782 | my_tm = localtime(&now); |
ca46b8ee SP |
3783 | if (my_tm) |
3784 | Copy(my_tm, ptm, 1, struct tm); | |
1b6737cc | 3785 | #else |
dc3bf405 | 3786 | PERL_UNUSED_CONTEXT; |
7918f24d | 3787 | PERL_ARGS_ASSERT_INIT_TM; |
1b6737cc | 3788 | PERL_UNUSED_ARG(ptm); |
e72cf795 JH |
3789 | #endif |
3790 | } | |
3791 | ||
3792 | /* | |
3793 | * mini_mktime - normalise struct tm values without the localtime() | |
3794 | * semantics (and overhead) of mktime(). | |
3795 | */ | |
3796 | void | |
ddeaf645 | 3797 | Perl_mini_mktime(struct tm *ptm) |
e72cf795 JH |
3798 | { |
3799 | int yearday; | |
3800 | int secs; | |
3801 | int month, mday, year, jday; | |
3802 | int odd_cent, odd_year; | |
3803 | ||
7918f24d NC |
3804 | PERL_ARGS_ASSERT_MINI_MKTIME; |
3805 | ||
e72cf795 JH |
3806 | #define DAYS_PER_YEAR 365 |
3807 | #define DAYS_PER_QYEAR (4*DAYS_PER_YEAR+1) | |
3808 | #define DAYS_PER_CENT (25*DAYS_PER_QYEAR-1) | |
3809 | #define DAYS_PER_QCENT (4*DAYS_PER_CENT+1) | |
3810 | #define SECS_PER_HOUR (60*60) | |
3811 | #define SECS_PER_DAY (24*SECS_PER_HOUR) | |
3812 | /* parentheses deliberately absent on these two, otherwise they don't work */ | |
3813 | #define MONTH_TO_DAYS 153/5 | |
3814 | #define DAYS_TO_MONTH 5/153 | |
3815 | /* offset to bias by March (month 4) 1st between month/mday & year finding */ | |
3816 | #define YEAR_ADJUST (4*MONTH_TO_DAYS+1) | |
3817 | /* as used here, the algorithm leaves Sunday as day 1 unless we adjust it */ | |
3818 | #define WEEKDAY_BIAS 6 /* (1+6)%7 makes Sunday 0 again */ | |
3819 | ||
3820 | /* | |
3821 | * Year/day algorithm notes: | |
3822 | * | |
3823 | * With a suitable offset for numeric value of the month, one can find | |
3824 | * an offset into the year by considering months to have 30.6 (153/5) days, | |
3825 | * using integer arithmetic (i.e., with truncation). To avoid too much | |
3826 | * messing about with leap days, we consider January and February to be | |
3827 | * the 13th and 14th month of the previous year. After that transformation, | |
3828 | * we need the month index we use to be high by 1 from 'normal human' usage, | |
3829 | * so the month index values we use run from 4 through 15. | |
3830 | * | |
3831 | * Given that, and the rules for the Gregorian calendar (leap years are those | |
3832 | * divisible by 4 unless also divisible by 100, when they must be divisible | |
3833 | * by 400 instead), we can simply calculate the number of days since some | |
3834 | * arbitrary 'beginning of time' by futzing with the (adjusted) year number, | |
3835 | * the days we derive from our month index, and adding in the day of the | |
3836 | * month. The value used here is not adjusted for the actual origin which | |
3837 | * it normally would use (1 January A.D. 1), since we're not exposing it. | |
3838 | * We're only building the value so we can turn around and get the | |
3839 | * normalised values for the year, month, day-of-month, and day-of-year. | |
3840 | * | |
3841 | * For going backward, we need to bias the value we're using so that we find | |
3842 | * the right year value. (Basically, we don't want the contribution of | |
3843 | * March 1st to the number to apply while deriving the year). Having done | |
3844 | * that, we 'count up' the contribution to the year number by accounting for | |
3845 | * full quadracenturies (400-year periods) with their extra leap days, plus | |
3846 | * the contribution from full centuries (to avoid counting in the lost leap | |
3847 | * days), plus the contribution from full quad-years (to count in the normal | |
3848 | * leap days), plus the leftover contribution from any non-leap years. | |
3849 | * At this point, if we were working with an actual leap day, we'll have 0 | |
3850 | * days left over. This is also true for March 1st, however. So, we have | |
3851 | * to special-case that result, and (earlier) keep track of the 'odd' | |
3852 | * century and year contributions. If we got 4 extra centuries in a qcent, | |
3853 | * or 4 extra years in a qyear, then it's a leap day and we call it 29 Feb. | |
3854 | * Otherwise, we add back in the earlier bias we removed (the 123 from | |
3855 | * figuring in March 1st), find the month index (integer division by 30.6), | |
3856 | * and the remainder is the day-of-month. We then have to convert back to | |
3857 | * 'real' months (including fixing January and February from being 14/15 in | |
3858 | * the previous year to being in the proper year). After that, to get | |
3859 | * tm_yday, we work with the normalised year and get a new yearday value for | |
3860 | * January 1st, which we subtract from the yearday value we had earlier, | |
3861 | * representing the date we've re-built. This is done from January 1 | |
3862 | * because tm_yday is 0-origin. | |
3863 | * | |
3864 | * Since POSIX time routines are only guaranteed to work for times since the | |
3865 | * UNIX epoch (00:00:00 1 Jan 1970 UTC), the fact that this algorithm | |
3866 | * applies Gregorian calendar rules even to dates before the 16th century | |
3867 | * doesn't bother me. Besides, you'd need cultural context for a given | |
3868 | * date to know whether it was Julian or Gregorian calendar, and that's | |
3869 | * outside the scope for this routine. Since we convert back based on the | |
3870 | * same rules we used to build the yearday, you'll only get strange results | |
3871 | * for input which needed normalising, or for the 'odd' century years which | |
486ec47a | 3872 | * were leap years in the Julian calendar but not in the Gregorian one. |
e72cf795 JH |
3873 | * I can live with that. |
3874 | * | |
3875 | * This algorithm also fails to handle years before A.D. 1 gracefully, but | |
3876 | * that's still outside the scope for POSIX time manipulation, so I don't | |
3877 | * care. | |
3878 | */ | |
3879 | ||
3880 | year = 1900 + ptm->tm_year; | |
3881 | month = ptm->tm_mon; | |
3882 | mday = ptm->tm_mday; | |
a64f08cb | 3883 | jday = 0; |
e72cf795 JH |
3884 | if (month >= 2) |
3885 | month+=2; | |
3886 | else | |
3887 | month+=14, year--; | |
3888 | yearday = DAYS_PER_YEAR * year + year/4 - year/100 + year/400; | |
3889 | yearday += month*MONTH_TO_DAYS + mday + jday; | |
3890 | /* | |
3891 | * Note that we don't know when leap-seconds were or will be, | |
3892 | * so we have to trust the user if we get something which looks | |
3893 | * like a sensible leap-second. Wild values for seconds will | |
3894 | * be rationalised, however. | |
3895 | */ | |
3896 | if ((unsigned) ptm->tm_sec <= 60) { | |
3897 | secs = 0; | |
3898 | } | |
3899 | else { | |
3900 | secs = ptm->tm_sec; | |
3901 | ptm->tm_sec = 0; | |
3902 | } | |
3903 | secs += 60 * ptm->tm_min; | |
3904 | secs += SECS_PER_HOUR * ptm->tm_hour; | |
3905 | if (secs < 0) { | |
3906 | if (secs-(secs/SECS_PER_DAY*SECS_PER_DAY) < 0) { | |
3907 | /* got negative remainder, but need positive time */ | |
3908 | /* back off an extra day to compensate */ | |
3909 | yearday += (secs/SECS_PER_DAY)-1; | |
3910 | secs -= SECS_PER_DAY * (secs/SECS_PER_DAY - 1); | |
3911 | } | |
3912 | else { | |
3913 | yearday += (secs/SECS_PER_DAY); | |
3914 | secs -= SECS_PER_DAY * (secs/SECS_PER_DAY); | |
3915 | } | |
3916 | } | |
3917 | else if (secs >= SECS_PER_DAY) { | |
3918 | yearday += (secs/SECS_PER_DAY); | |
3919 | secs %= SECS_PER_DAY; | |
3920 | } | |
3921 | ptm->tm_hour = secs/SECS_PER_HOUR; | |
3922 | secs %= SECS_PER_HOUR; | |
3923 | ptm->tm_min = secs/60; | |
3924 | secs %= 60; | |
3925 | ptm->tm_sec += secs; | |
3926 | /* done with time of day effects */ | |
3927 | /* | |
3928 | * The algorithm for yearday has (so far) left it high by 428. | |
3929 | * To avoid mistaking a legitimate Feb 29 as Mar 1, we need to | |
3930 | * bias it by 123 while trying to figure out what year it | |
3931 | * really represents. Even with this tweak, the reverse | |
3932 | * translation fails for years before A.D. 0001. | |
3933 | * It would still fail for Feb 29, but we catch that one below. | |
3934 | */ | |
3935 | jday = yearday; /* save for later fixup vis-a-vis Jan 1 */ | |
3936 | yearday -= YEAR_ADJUST; | |
3937 | year = (yearday / DAYS_PER_QCENT) * 400; | |
3938 | yearday %= DAYS_PER_QCENT; | |
3939 | odd_cent = yearday / DAYS_PER_CENT; | |
3940 | year += odd_cent * 100; | |
3941 | yearday %= DAYS_PER_CENT; | |
3942 | year += (yearday / DAYS_PER_QYEAR) * 4; | |
3943 | yearday %= DAYS_PER_QYEAR; | |
3944 | odd_year = yearday / DAYS_PER_YEAR; | |
3945 | year += odd_year; | |
3946 | yearday %= DAYS_PER_YEAR; | |
3947 | if (!yearday && (odd_cent==4 || odd_year==4)) { /* catch Feb 29 */ | |
3948 | month = 1; | |
3949 | yearday = 29; | |
3950 | } | |
3951 | else { | |
3952 | yearday += YEAR_ADJUST; /* recover March 1st crock */ | |
3953 | month = yearday*DAYS_TO_MONTH; | |
3954 | yearday -= month*MONTH_TO_DAYS; | |
3955 | /* recover other leap-year adjustment */ | |
3956 | if (month > 13) { | |
3957 | month-=14; | |
3958 | year++; | |
3959 | } | |
3960 | else { | |
3961 | month-=2; | |
3962 | } | |
3963 | } | |
3964 | ptm->tm_year = year - 1900; | |
3965 | if (yearday) { | |
3966 | ptm->tm_mday = yearday; | |
3967 | ptm->tm_mon = month; | |
3968 | } | |
3969 | else { | |
3970 | ptm->tm_mday = 31; | |
3971 | ptm->tm_mon = month - 1; | |
3972 | } | |
3973 | /* re-build yearday based on Jan 1 to get tm_yday */ | |
3974 | year--; | |
3975 | yearday = year*DAYS_PER_YEAR + year/4 - year/100 + year/400; | |
3976 | yearday += 14*MONTH_TO_DAYS + 1; | |
3977 | ptm->tm_yday = jday - yearday; | |
a64f08cb | 3978 | ptm->tm_wday = (jday + WEEKDAY_BIAS) % 7; |
e72cf795 | 3979 | } |
b3c85772 JH |
3980 | |
3981 | char * | |
e1ec3a88 | 3982 | Perl_my_strftime(pTHX_ const char *fmt, int sec, int min, int hour, int mday, int mon, int year, int wday, int yday, int isdst) |
b3c85772 JH |
3983 | { |
3984 | #ifdef HAS_STRFTIME | |
4c17e999 KW |
3985 | |
3986 | /* Note that yday and wday effectively are ignored by this function, as mini_mktime() overwrites them */ | |
3987 | ||
b3c85772 JH |
3988 | char *buf; |
3989 | int buflen; | |
3990 | struct tm mytm; | |
3991 | int len; | |
3992 | ||
7918f24d NC |
3993 | PERL_ARGS_ASSERT_MY_STRFTIME; |
3994 | ||
b3c85772 JH |
3995 | init_tm(&mytm); /* XXX workaround - see init_tm() above */ |
3996 | mytm.tm_sec = sec; | |
3997 | mytm.tm_min = min; | |
3998 | mytm.tm_hour = hour; | |
3999 | mytm.tm_mday = mday; | |
4000 | mytm.tm_mon = mon; | |
4001 | mytm.tm_year = year; | |
4002 | mytm.tm_wday = wday; | |
4003 | mytm.tm_yday = yday; | |
4004 | mytm.tm_isdst = isdst; | |
4005 | mini_mktime(&mytm); | |
c473feec SR |
4006 | /* use libc to get the values for tm_gmtoff and tm_zone [perl #18238] */ |
4007 | #if defined(HAS_MKTIME) && (defined(HAS_TM_TM_GMTOFF) || defined(HAS_TM_TM_ZONE)) | |
4008 | STMT_START { | |
4009 | struct tm mytm2; | |
4010 | mytm2 = mytm; | |
4011 | mktime(&mytm2); | |
4012 | #ifdef HAS_TM_TM_GMTOFF | |
4013 | mytm.tm_gmtoff = mytm2.tm_gmtoff; | |
4014 | #endif | |
4015 | #ifdef HAS_TM_TM_ZONE | |
4016 | mytm.tm_zone = mytm2.tm_zone; | |
4017 | #endif | |
4018 | } STMT_END; | |
4019 | #endif | |
b3c85772 | 4020 | buflen = 64; |
a02a5408 | 4021 | Newx(buf, buflen, char); |
5d37acd6 DM |
4022 | |
4023 | GCC_DIAG_IGNORE(-Wformat-nonliteral); /* fmt checked by caller */ | |
b3c85772 | 4024 | len = strftime(buf, buflen, fmt, &mytm); |
5d37acd6 DM |
4025 | GCC_DIAG_RESTORE; |
4026 | ||
b3c85772 | 4027 | /* |
877f6a72 | 4028 | ** The following is needed to handle to the situation where |
b3c85772 JH |
4029 | ** tmpbuf overflows. Basically we want to allocate a buffer |
4030 | ** and try repeatedly. The reason why it is so complicated | |
4031 | ** is that getting a return value of 0 from strftime can indicate | |
4032 | ** one of the following: | |
4033 | ** 1. buffer overflowed, | |
4034 | ** 2. illegal conversion specifier, or | |
4035 | ** 3. the format string specifies nothing to be returned(not | |
4036 | ** an error). This could be because format is an empty string | |
4037 | ** or it specifies %p that yields an empty string in some locale. | |
4038 | ** If there is a better way to make it portable, go ahead by | |
4039 | ** all means. | |
4040 | */ | |
4041 | if ((len > 0 && len < buflen) || (len == 0 && *fmt == '\0')) | |
4042 | return buf; | |
4043 | else { | |
4044 | /* Possibly buf overflowed - try again with a bigger buf */ | |
e1ec3a88 | 4045 | const int fmtlen = strlen(fmt); |
7743c307 | 4046 | int bufsize = fmtlen + buflen; |
877f6a72 | 4047 | |
c4bc4aaa | 4048 | Renew(buf, bufsize, char); |
b3c85772 | 4049 | while (buf) { |
5d37acd6 DM |
4050 | |
4051 | GCC_DIAG_IGNORE(-Wformat-nonliteral); /* fmt checked by caller */ | |
b3c85772 | 4052 | buflen = strftime(buf, bufsize, fmt, &mytm); |
5d37acd6 DM |
4053 | GCC_DIAG_RESTORE; |
4054 | ||
b3c85772 JH |
4055 | if (buflen > 0 && buflen < bufsize) |
4056 | break; | |
4057 | /* heuristic to prevent out-of-memory errors */ | |
4058 | if (bufsize > 100*fmtlen) { | |
4059 | Safefree(buf); | |
4060 | buf = NULL; | |
4061 | break; | |
4062 | } | |
7743c307 SH |
4063 | bufsize *= 2; |
4064 | Renew(buf, bufsize, char); | |
b3c85772 JH |
4065 | } |
4066 | return buf; | |
4067 | } | |
4068 | #else | |
4069 | Perl_croak(aTHX_ "panic: no strftime"); | |
27da23d5 | 4070 | return NULL; |
b3c85772 JH |
4071 | #endif |
4072 | } | |
4073 | ||
877f6a72 NIS |
4074 | |
4075 | #define SV_CWD_RETURN_UNDEF \ | |
4076 | sv_setsv(sv, &PL_sv_undef); \ | |
4077 | return FALSE | |
4078 | ||
4079 | #define SV_CWD_ISDOT(dp) \ | |
4080 | (dp->d_name[0] == '.' && (dp->d_name[1] == '\0' || \ | |
3aed30dc | 4081 | (dp->d_name[1] == '.' && dp->d_name[2] == '\0'))) |
877f6a72 NIS |
4082 | |
4083 | /* | |
ccfc67b7 JH |
4084 | =head1 Miscellaneous Functions |
4085 | ||
89423764 | 4086 | =for apidoc getcwd_sv |
877f6a72 | 4087 | |
796b6530 | 4088 | Fill C<sv> with current working directory |
877f6a72 NIS |
4089 | |
4090 | =cut | |
4091 | */ | |
4092 | ||
4093 | /* Originally written in Perl by John Bazik; rewritten in C by Ben Sugars. | |
4094 | * rewritten again by dougm, optimized for use with xs TARG, and to prefer | |
4095 | * getcwd(3) if available | |
f6bab5f6 | 4096 | * Comments from the original: |
877f6a72 NIS |
4097 | * This is a faster version of getcwd. It's also more dangerous |
4098 | * because you might chdir out of a directory that you can't chdir | |
4099 | * back into. */ | |
4100 | ||
877f6a72 | 4101 | int |
5aaab254 | 4102 | Perl_getcwd_sv(pTHX_ SV *sv) |
877f6a72 NIS |
4103 | { |
4104 | #ifndef PERL_MICRO | |
ea715489 | 4105 | SvTAINTED_on(sv); |
ea715489 | 4106 | |
7918f24d NC |
4107 | PERL_ARGS_ASSERT_GETCWD_SV; |
4108 | ||
8f95b30d JH |
4109 | #ifdef HAS_GETCWD |
4110 | { | |
60e110a8 DM |
4111 | char buf[MAXPATHLEN]; |
4112 | ||
3aed30dc | 4113 | /* Some getcwd()s automatically allocate a buffer of the given |
60e110a8 DM |
4114 | * size from the heap if they are given a NULL buffer pointer. |
4115 | * The problem is that this behaviour is not portable. */ | |
3aed30dc | 4116 | if (getcwd(buf, sizeof(buf) - 1)) { |
42d9b98d | 4117 | sv_setpv(sv, buf); |
3aed30dc HS |
4118 | return TRUE; |
4119 | } | |
4120 | else { | |
4121 | sv_setsv(sv, &PL_sv_undef); | |
4122 | return FALSE; | |
4123 | } | |
8f95b30d JH |
4124 | } |
4125 | ||
4126 | #else | |
4127 | ||
c623ac67 | 4128 | Stat_t statbuf; |
877f6a72 | 4129 | int orig_cdev, orig_cino, cdev, cino, odev, oino, tdev, tino; |
4373e329 | 4130 | int pathlen=0; |
877f6a72 | 4131 | Direntry_t *dp; |
877f6a72 | 4132 | |
862a34c6 | 4133 | SvUPGRADE(sv, SVt_PV); |
877f6a72 | 4134 | |
877f6a72 | 4135 | if (PerlLIO_lstat(".", &statbuf) < 0) { |
3aed30dc | 4136 | SV_CWD_RETURN_UNDEF; |
877f6a72 NIS |
4137 | } |
4138 | ||
4139 | orig_cdev = statbuf.st_dev; | |
4140 | orig_cino = statbuf.st_ino; | |
4141 | cdev = orig_cdev; | |
4142 | cino = orig_cino; | |
4143 | ||
4144 | for (;;) { | |
4373e329 | 4145 | DIR *dir; |
f56ed502 | 4146 | int namelen; |
3aed30dc HS |
4147 | odev = cdev; |
4148 | oino = cino; | |
4149 | ||
4150 | if (PerlDir_chdir("..") < 0) { | |
4151 | SV_CWD_RETURN_UNDEF; | |
4152 | } | |
4153 | if (PerlLIO_stat(".", &statbuf) < 0) { | |
4154 | SV_CWD_RETURN_UNDEF; | |
4155 | } | |
4156 | ||
4157 | cdev = statbuf.st_dev; | |
4158 | cino = statbuf.st_ino; | |
4159 | ||
4160 | if (odev == cdev && oino == cino) { | |
4161 | break; | |
4162 | } | |
4163 | if (!(dir = PerlDir_open("."))) { | |
4164 | SV_CWD_RETURN_UNDEF; | |
4165 | } | |
4166 | ||
4167 | while ((dp = PerlDir_read(dir)) != NULL) { | |
877f6a72 | 4168 | #ifdef DIRNAMLEN |
f56ed502 | 4169 | namelen = dp->d_namlen; |
877f6a72 | 4170 | #else |
f56ed502 | 4171 | namelen = strlen(dp->d_name); |
877f6a72 | 4172 | #endif |
3aed30dc HS |
4173 | /* skip . and .. */ |
4174 | if (SV_CWD_ISDOT(dp)) { | |
4175 | continue; | |
4176 | } | |
4177 | ||
4178 | if (PerlLIO_lstat(dp->d_name, &statbuf) < 0) { | |
4179 | SV_CWD_RETURN_UNDEF; | |
4180 | } | |
4181 | ||
4182 | tdev = statbuf.st_dev; | |
4183 | tino = statbuf.st_ino; | |
4184 | if (tino == oino && tdev == odev) { | |
4185 | break; | |
4186 | } | |
cb5953d6 JH |
4187 | } |
4188 | ||
3aed30dc HS |
4189 | if (!dp) { |
4190 | SV_CWD_RETURN_UNDEF; | |
4191 | } | |
4192 | ||
4193 | if (pathlen + namelen + 1 >= MAXPATHLEN) { | |
4194 | SV_CWD_RETURN_UNDEF; | |
4195 | } | |
877f6a72 | 4196 | |
3aed30dc HS |
4197 | SvGROW(sv, pathlen + namelen + 1); |
4198 | ||
4199 | if (pathlen) { | |
4200 | /* shift down */ | |
95a20fc0 | 4201 | Move(SvPVX_const(sv), SvPVX(sv) + namelen + 1, pathlen, char); |
3aed30dc | 4202 | } |
877f6a72 | 4203 | |
3aed30dc HS |
4204 | /* prepend current directory to the front */ |
4205 | *SvPVX(sv) = '/'; | |
4206 | Move(dp->d_name, SvPVX(sv)+1, namelen, char); | |
4207 | pathlen += (namelen + 1); | |
877f6a72 NIS |
4208 | |
4209 | #ifdef VOID_CLOSEDIR | |
3aed30dc | 4210 | PerlDir_close(dir); |
877f6a72 | 4211 | #else |
3aed30dc HS |
4212 | if (PerlDir_close(dir) < 0) { |
4213 | SV_CWD_RETURN_UNDEF; | |
4214 | } | |
877f6a72 NIS |
4215 | #endif |
4216 | } | |
4217 | ||
60e110a8 | 4218 | if (pathlen) { |
3aed30dc HS |
4219 | SvCUR_set(sv, pathlen); |
4220 | *SvEND(sv) = '\0'; | |
4221 | SvPOK_only(sv); | |
877f6a72 | 4222 | |
95a20fc0 | 4223 | if (PerlDir_chdir(SvPVX_const(sv)) < 0) { |
3aed30dc HS |
4224 | SV_CWD_RETURN_UNDEF; |
4225 | } | |
877f6a72 NIS |
4226 | } |
4227 | if (PerlLIO_stat(".", &statbuf) < 0) { | |
3aed30dc | 4228 | SV_CWD_RETURN_UNDEF; |
877f6a72 NIS |
4229 | } |
4230 | ||
4231 | cdev = statbuf.st_dev; | |
4232 | cino = statbuf.st_ino; | |
4233 | ||
4234 | if (cdev != orig_cdev || cino != orig_cino) { | |
3aed30dc HS |
4235 | Perl_croak(aTHX_ "Unstable directory path, " |
4236 | "current directory changed unexpectedly"); | |
877f6a72 | 4237 | } |
877f6a72 NIS |
4238 | |
4239 | return TRUE; | |
793b8d8e JH |
4240 | #endif |
4241 | ||
877f6a72 NIS |
4242 | #else |
4243 | return FALSE; | |
4244 | #endif | |
4245 | } | |
4246 | ||
abc6d738 | 4247 | #include "vutil.c" |
ad63d80f | 4248 | |
c95c94b1 | 4249 | #if !defined(HAS_SOCKETPAIR) && defined(HAS_SOCKET) && defined(AF_INET) && defined(PF_INET) && defined(SOCK_DGRAM) && defined(HAS_SELECT) |
2bc69dc4 NIS |
4250 | # define EMULATE_SOCKETPAIR_UDP |
4251 | #endif | |
4252 | ||
4253 | #ifdef EMULATE_SOCKETPAIR_UDP | |
02fc2eee NC |
4254 | static int |
4255 | S_socketpair_udp (int fd[2]) { | |
e10bb1e9 | 4256 | dTHX; |
02fc2eee NC |
4257 | /* Fake a datagram socketpair using UDP to localhost. */ |
4258 | int sockets[2] = {-1, -1}; | |
4259 | struct sockaddr_in addresses[2]; | |
4260 | int i; | |
3aed30dc | 4261 | Sock_size_t size = sizeof(struct sockaddr_in); |
ae92b34e | 4262 | unsigned short port; |
02fc2eee NC |
4263 | int got; |
4264 | ||
3aed30dc | 4265 | memset(&addresses, 0, sizeof(addresses)); |
02fc2eee NC |
4266 | i = 1; |
4267 | do { | |
3aed30dc HS |
4268 | sockets[i] = PerlSock_socket(AF_INET, SOCK_DGRAM, PF_INET); |
4269 | if (sockets[i] == -1) | |
4270 | goto tidy_up_and_fail; | |
4271 | ||
4272 | addresses[i].sin_family = AF_INET; | |
4273 | addresses[i].sin_addr.s_addr = htonl(INADDR_LOOPBACK); | |
4274 | addresses[i].sin_port = 0; /* kernel choses port. */ | |
4275 | if (PerlSock_bind(sockets[i], (struct sockaddr *) &addresses[i], | |
4276 | sizeof(struct sockaddr_in)) == -1) | |
4277 | goto tidy_up_and_fail; | |
02fc2eee NC |
4278 | } while (i--); |
4279 | ||
4280 | /* Now have 2 UDP sockets. Find out which port each is connected to, and | |
4281 | for each connect the other socket to it. */ | |
4282 | i = 1; | |
4283 | do { | |
3aed30dc HS |
4284 | if (PerlSock_getsockname(sockets[i], (struct sockaddr *) &addresses[i], |
4285 | &size) == -1) | |
4286 | goto tidy_up_and_fail; | |
4287 | if (size != sizeof(struct sockaddr_in)) | |
4288 | goto abort_tidy_up_and_fail; | |
4289 | /* !1 is 0, !0 is 1 */ | |
4290 | if (PerlSock_connect(sockets[!i], (struct sockaddr *) &addresses[i], | |
4291 | sizeof(struct sockaddr_in)) == -1) | |
4292 | goto tidy_up_and_fail; | |
02fc2eee NC |
4293 | } while (i--); |
4294 | ||
4295 | /* Now we have 2 sockets connected to each other. I don't trust some other | |
4296 | process not to have already sent a packet to us (by random) so send | |
4297 | a packet from each to the other. */ | |
4298 | i = 1; | |
4299 | do { | |
3aed30dc HS |
4300 | /* I'm going to send my own port number. As a short. |
4301 | (Who knows if someone somewhere has sin_port as a bitfield and needs | |
4302 | this routine. (I'm assuming crays have socketpair)) */ | |
4303 | port = addresses[i].sin_port; | |
4304 | got = PerlLIO_write(sockets[i], &port, sizeof(port)); | |
4305 | if (got != sizeof(port)) { | |
4306 | if (got == -1) | |
4307 | goto tidy_up_and_fail; | |
4308 | goto abort_tidy_up_and_fail; | |
4309 | } | |
02fc2eee NC |
4310 | } while (i--); |
4311 | ||
4312 | /* Packets sent. I don't trust them to have arrived though. | |
4313 | (As I understand it Solaris TCP stack is multithreaded. Non-blocking | |
4314 | connect to localhost will use a second kernel thread. In 2.6 the | |
4315 | first thread running the connect() returns before the second completes, | |
4316 | so EINPROGRESS> In 2.7 the improved stack is faster and connect() | |
4317 | returns 0. Poor programs have tripped up. One poor program's authors' | |
4318 | had a 50-1 reverse stock split. Not sure how connected these were.) | |
4319 | So I don't trust someone not to have an unpredictable UDP stack. | |
4320 | */ | |
4321 | ||
4322 | { | |
3aed30dc HS |
4323 | struct timeval waitfor = {0, 100000}; /* You have 0.1 seconds */ |
4324 | int max = sockets[1] > sockets[0] ? sockets[1] : sockets[0]; | |
4325 | fd_set rset; | |
4326 | ||
4327 | FD_ZERO(&rset); | |
ea407a0c NC |
4328 | FD_SET((unsigned int)sockets[0], &rset); |
4329 | FD_SET((unsigned int)sockets[1], &rset); | |
3aed30dc HS |
4330 | |
4331 | got = PerlSock_select(max + 1, &rset, NULL, NULL, &waitfor); | |
4332 | if (got != 2 || !FD_ISSET(sockets[0], &rset) | |
4333 | || !FD_ISSET(sockets[1], &rset)) { | |
4334 | /* I hope this is portable and appropriate. */ | |
4335 | if (got == -1) | |
4336 | goto tidy_up_and_fail; | |
4337 | goto abort_tidy_up_and_fail; | |
4338 | } | |
02fc2eee | 4339 | } |
f4758303 | 4340 | |
02fc2eee NC |
4341 | /* And the paranoia department even now doesn't trust it to have arrive |
4342 | (hence MSG_DONTWAIT). Or that what arrives was sent by us. */ | |
4343 | { | |
3aed30dc HS |
4344 | struct sockaddr_in readfrom; |
4345 | unsigned short buffer[2]; | |
02fc2eee | 4346 | |
3aed30dc HS |
4347 | i = 1; |
4348 | do { | |
02fc2eee | 4349 | #ifdef MSG_DONTWAIT |
3aed30dc HS |
4350 | got = PerlSock_recvfrom(sockets[i], (char *) &buffer, |
4351 | sizeof(buffer), MSG_DONTWAIT, | |
4352 | (struct sockaddr *) &readfrom, &size); | |
02fc2eee | 4353 | #else |
3aed30dc HS |
4354 | got = PerlSock_recvfrom(sockets[i], (char *) &buffer, |
4355 | sizeof(buffer), 0, | |
4356 | (struct sockaddr *) &readfrom, &size); | |
e10bb1e9 | 4357 | #endif |
02fc2eee | 4358 | |
3aed30dc HS |
4359 | if (got == -1) |
4360 | goto tidy_up_and_fail; | |
4361 | if (got != sizeof(port) | |
4362 | || size != sizeof(struct sockaddr_in) | |
4363 | /* Check other socket sent us its port. */ | |
4364 | || buffer[0] != (unsigned short) addresses[!i].sin_port | |
4365 | /* Check kernel says we got the datagram from that socket */ | |
4366 | || readfrom.sin_family != addresses[!i].sin_family | |
4367 | || readfrom.sin_addr.s_addr != addresses[!i].sin_addr.s_addr | |
4368 | || readfrom.sin_port != addresses[!i].sin_port) | |
4369 | goto abort_tidy_up_and_fail; | |
4370 | } while (i--); | |
02fc2eee NC |
4371 | } |
4372 | /* My caller (my_socketpair) has validated that this is non-NULL */ | |
4373 | fd[0] = sockets[0]; | |
4374 | fd[1] = sockets[1]; | |
4375 | /* I hereby declare this connection open. May God bless all who cross | |
4376 | her. */ | |
4377 | return 0; | |
4378 | ||
4379 | abort_tidy_up_and_fail: | |
4380 | errno = ECONNABORTED; | |
4381 | tidy_up_and_fail: | |
4382 | { | |
4ee39169 | 4383 | dSAVE_ERRNO; |
3aed30dc HS |
4384 | if (sockets[0] != -1) |
4385 | PerlLIO_close(sockets[0]); | |
4386 | if (sockets[1] != -1) | |
4387 | PerlLIO_close(sockets[1]); | |
4ee39169 | 4388 | RESTORE_ERRNO; |
3aed30dc | 4389 | return -1; |
02fc2eee NC |
4390 | } |
4391 | } | |
85ca448a | 4392 | #endif /* EMULATE_SOCKETPAIR_UDP */ |
02fc2eee | 4393 | |
b5ac89c3 | 4394 | #if !defined(HAS_SOCKETPAIR) && defined(HAS_SOCKET) && defined(AF_INET) && defined(PF_INET) |
02fc2eee NC |
4395 | int |
4396 | Perl_my_socketpair (int family, int type, int protocol, int fd[2]) { | |
4397 | /* Stevens says that family must be AF_LOCAL, protocol 0. | |
2948e0bd | 4398 | I'm going to enforce that, then ignore it, and use TCP (or UDP). */ |
2bcd6579 | 4399 | dTHXa(NULL); |
02fc2eee NC |
4400 | int listener = -1; |
4401 | int connector = -1; | |
4402 | int acceptor = -1; | |
4403 | struct sockaddr_in listen_addr; | |
4404 | struct sockaddr_in connect_addr; | |
4405 | Sock_size_t size; | |
4406 | ||
50458334 JH |
4407 | if (protocol |
4408 | #ifdef AF_UNIX | |
4409 | || family != AF_UNIX | |
4410 | #endif | |
3aed30dc HS |
4411 | ) { |
4412 | errno = EAFNOSUPPORT; | |
4413 | return -1; | |
02fc2eee | 4414 | } |
2948e0bd | 4415 | if (!fd) { |
3aed30dc HS |
4416 | errno = EINVAL; |
4417 | return -1; | |
2948e0bd | 4418 | } |
02fc2eee | 4419 | |
2bc69dc4 | 4420 | #ifdef EMULATE_SOCKETPAIR_UDP |
02fc2eee | 4421 | if (type == SOCK_DGRAM) |
3aed30dc | 4422 | return S_socketpair_udp(fd); |
2bc69dc4 | 4423 | #endif |
02fc2eee | 4424 | |
2bcd6579 | 4425 | aTHXa(PERL_GET_THX); |
3aed30dc | 4426 | listener = PerlSock_socket(AF_INET, type, 0); |
02fc2eee | 4427 | if (listener == -1) |
3aed30dc HS |
4428 | return -1; |
4429 | memset(&listen_addr, 0, sizeof(listen_addr)); | |
02fc2eee | 4430 | listen_addr.sin_family = AF_INET; |
3aed30dc | 4431 | listen_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); |
02fc2eee | 4432 | listen_addr.sin_port = 0; /* kernel choses port. */ |
3aed30dc HS |
4433 | if (PerlSock_bind(listener, (struct sockaddr *) &listen_addr, |
4434 | sizeof(listen_addr)) == -1) | |
4435 | goto tidy_up_and_fail; | |
e10bb1e9 | 4436 | if (PerlSock_listen(listener, 1) == -1) |
3aed30dc | 4437 | goto tidy_up_and_fail; |
02fc2eee | 4438 | |
3aed30dc | 4439 | connector = PerlSock_socket(AF_INET, type, 0); |
02fc2eee | 4440 | if (connector == -1) |
3aed30dc | 4441 | goto tidy_up_and_fail; |
02fc2eee | 4442 | /* We want to find out the port number to connect to. */ |
3aed30dc HS |
4443 | size = sizeof(connect_addr); |
4444 | if (PerlSock_getsockname(listener, (struct sockaddr *) &connect_addr, | |
4445 | &size) == -1) | |
4446 | goto tidy_up_and_fail; | |
4447 | if (size != sizeof(connect_addr)) | |
4448 | goto abort_tidy_up_and_fail; | |
e10bb1e9 | 4449 | if (PerlSock_connect(connector, (struct sockaddr *) &connect_addr, |
3aed30dc HS |
4450 | sizeof(connect_addr)) == -1) |
4451 | goto tidy_up_and_fail; | |
02fc2eee | 4452 | |
3aed30dc HS |
4453 | size = sizeof(listen_addr); |
4454 | acceptor = PerlSock_accept(listener, (struct sockaddr *) &listen_addr, | |
4455 | &size); | |
02fc2eee | 4456 | if (acceptor == -1) |
3aed30dc HS |
4457 | goto tidy_up_and_fail; |
4458 | if (size != sizeof(listen_addr)) | |
4459 | goto abort_tidy_up_and_fail; | |
4460 | PerlLIO_close(listener); | |
02fc2eee NC |
4461 | /* Now check we are talking to ourself by matching port and host on the |
4462 | two sockets. */ | |
3aed30dc HS |
4463 | if (PerlSock_getsockname(connector, (struct sockaddr *) &connect_addr, |
4464 | &size) == -1) | |
4465 | goto tidy_up_and_fail; | |
4466 | if (size != sizeof(connect_addr) | |
4467 | || listen_addr.sin_family != connect_addr.sin_family | |
4468 | || listen_addr.sin_addr.s_addr != connect_addr.sin_addr.s_addr | |
4469 | || listen_addr.sin_port != connect_addr.sin_port) { | |
4470 | goto abort_tidy_up_and_fail; | |
02fc2eee NC |
4471 | } |
4472 | fd[0] = connector; | |
4473 | fd[1] = acceptor; | |
4474 | return 0; | |
4475 | ||
4476 | abort_tidy_up_and_fail: | |
27da23d5 JH |
4477 | #ifdef ECONNABORTED |
4478 | errno = ECONNABORTED; /* This would be the standard thing to do. */ | |
4479 | #else | |
4480 | # ifdef ECONNREFUSED | |
4481 | errno = ECONNREFUSED; /* E.g. Symbian does not have ECONNABORTED. */ | |
4482 | # else | |
4483 | errno = ETIMEDOUT; /* Desperation time. */ | |
4484 | # endif | |
4485 | #endif | |
02fc2eee NC |
4486 | tidy_up_and_fail: |
4487 | { | |
4ee39169 | 4488 | dSAVE_ERRNO; |
3aed30dc HS |
4489 | if (listener != -1) |
4490 | PerlLIO_close(listener); | |
4491 | if (connector != -1) | |
4492 | PerlLIO_close(connector); | |
4493 | if (acceptor != -1) | |
4494 | PerlLIO_close(acceptor); | |
4ee39169 | 4495 | RESTORE_ERRNO; |
3aed30dc | 4496 | return -1; |
02fc2eee NC |
4497 | } |
4498 | } | |
85ca448a | 4499 | #else |
48ea76d1 | 4500 | /* In any case have a stub so that there's code corresponding |
d500e60d | 4501 | * to the my_socketpair in embed.fnc. */ |
48ea76d1 JH |
4502 | int |
4503 | Perl_my_socketpair (int family, int type, int protocol, int fd[2]) { | |
daf16542 | 4504 | #ifdef HAS_SOCKETPAIR |
48ea76d1 | 4505 | return socketpair(family, type, protocol, fd); |
daf16542 JH |
4506 | #else |
4507 | return -1; | |
4508 | #endif | |
48ea76d1 JH |
4509 | } |
4510 | #endif | |
4511 | ||
68795e93 NIS |
4512 | /* |
4513 | ||
4514 | =for apidoc sv_nosharing | |
4515 | ||
4516 | Dummy routine which "shares" an SV when there is no sharing module present. | |
72d33970 FC |
4517 | Or "locks" it. Or "unlocks" it. In other |
4518 | words, ignores its single SV argument. | |
796b6530 | 4519 | Exists to avoid test for a C<NULL> function pointer and because it could |
d5b2b27b | 4520 | potentially warn under some level of strict-ness. |
68795e93 NIS |
4521 | |
4522 | =cut | |
4523 | */ | |
4524 | ||
4525 | void | |
4526 | Perl_sv_nosharing(pTHX_ SV *sv) | |
4527 | { | |
96a5add6 | 4528 | PERL_UNUSED_CONTEXT; |
53c1dcc0 | 4529 | PERL_UNUSED_ARG(sv); |
68795e93 NIS |
4530 | } |
4531 | ||
eba16661 JH |
4532 | /* |
4533 | ||
4534 | =for apidoc sv_destroyable | |
4535 | ||
4536 | Dummy routine which reports that object can be destroyed when there is no | |
4537 | sharing module present. It ignores its single SV argument, and returns | |
796b6530 | 4538 | 'true'. Exists to avoid test for a C<NULL> function pointer and because it |
eba16661 JH |
4539 | could potentially warn under some level of strict-ness. |
4540 | ||
4541 | =cut | |
4542 | */ | |
4543 | ||
4544 | bool | |
4545 | Perl_sv_destroyable(pTHX_ SV *sv) | |
4546 | { | |
4547 | PERL_UNUSED_CONTEXT; | |
4548 | PERL_UNUSED_ARG(sv); | |
4549 | return TRUE; | |
4550 | } | |
4551 | ||
a05d7ebb | 4552 | U32 |
e1ec3a88 | 4553 | Perl_parse_unicode_opts(pTHX_ const char **popt) |
a05d7ebb | 4554 | { |
e1ec3a88 | 4555 | const char *p = *popt; |
a05d7ebb JH |
4556 | U32 opt = 0; |
4557 | ||
7918f24d NC |
4558 | PERL_ARGS_ASSERT_PARSE_UNICODE_OPTS; |
4559 | ||
a05d7ebb JH |
4560 | if (*p) { |
4561 | if (isDIGIT(*p)) { | |
96e440d2 | 4562 | const char* endptr; |
22ff3130 | 4563 | UV uv; |
89d84ff9 | 4564 | if (grok_atoUV(p, &uv, &endptr) && uv <= U32_MAX) { |
22ff3130 | 4565 | opt = (U32)uv; |
89d84ff9 HS |
4566 | p = endptr; |
4567 | if (p && *p && *p != '\n' && *p != '\r') { | |
4568 | if (isSPACE(*p)) | |
4569 | goto the_end_of_the_opts_parser; | |
4570 | else | |
4571 | Perl_croak(aTHX_ "Unknown Unicode option letter '%c'", *p); | |
4572 | } | |
22ff3130 | 4573 | } |
817e3e2c TC |
4574 | else { |
4575 | Perl_croak(aTHX_ "Invalid number '%s' for -C option.\n", p); | |
4576 | } | |
22ff3130 HS |
4577 | } |
4578 | else { | |
a05d7ebb JH |
4579 | for (; *p; p++) { |
4580 | switch (*p) { | |
4581 | case PERL_UNICODE_STDIN: | |
4582 | opt |= PERL_UNICODE_STDIN_FLAG; break; | |
4583 | case PERL_UNICODE_STDOUT: | |
4584 | opt |= PERL_UNICODE_STDOUT_FLAG; break; | |
4585 | case PERL_UNICODE_STDERR: | |
4586 | opt |= PERL_UNICODE_STDERR_FLAG; break; | |
4587 | case PERL_UNICODE_STD: | |
4588 | opt |= PERL_UNICODE_STD_FLAG; break; | |
4589 | case PERL_UNICODE_IN: | |
4590 | opt |= PERL_UNICODE_IN_FLAG; break; | |
4591 | case PERL_UNICODE_OUT: | |
4592 | opt |= PERL_UNICODE_OUT_FLAG; break; | |
4593 | case PERL_UNICODE_INOUT: | |
4594 | opt |= PERL_UNICODE_INOUT_FLAG; break; | |
4595 | case PERL_UNICODE_LOCALE: | |
4596 | opt |= PERL_UNICODE_LOCALE_FLAG; break; | |
4597 | case PERL_UNICODE_ARGV: | |
4598 | opt |= PERL_UNICODE_ARGV_FLAG; break; | |
5a22a2bb NC |
4599 | case PERL_UNICODE_UTF8CACHEASSERT: |
4600 | opt |= PERL_UNICODE_UTF8CACHEASSERT_FLAG; break; | |
a05d7ebb | 4601 | default: |
d4a59e54 FC |
4602 | if (*p != '\n' && *p != '\r') { |
4603 | if(isSPACE(*p)) goto the_end_of_the_opts_parser; | |
4604 | else | |
7c91f477 JH |
4605 | Perl_croak(aTHX_ |
4606 | "Unknown Unicode option letter '%c'", *p); | |
d4a59e54 | 4607 | } |
a05d7ebb JH |
4608 | } |
4609 | } | |
4610 | } | |
4611 | } | |
4612 | else | |
4613 | opt = PERL_UNICODE_DEFAULT_FLAGS; | |
4614 | ||
d4a59e54 FC |
4615 | the_end_of_the_opts_parser: |
4616 | ||
a05d7ebb | 4617 | if (opt & ~PERL_UNICODE_ALL_FLAGS) |
06e66572 | 4618 | Perl_croak(aTHX_ "Unknown Unicode option value %"UVuf, |
a05d7ebb JH |
4619 | (UV) (opt & ~PERL_UNICODE_ALL_FLAGS)); |
4620 | ||
4621 | *popt = p; | |
4622 | ||
4623 | return opt; | |
4624 | } | |
4625 | ||
25bbd826 CB |
4626 | #ifdef VMS |
4627 | # include <starlet.h> | |
4628 | #endif | |
4629 | ||
132efe8b JH |
4630 | U32 |
4631 | Perl_seed(pTHX) | |
4632 | { | |
4633 | /* | |
4634 | * This is really just a quick hack which grabs various garbage | |
4635 | * values. It really should be a real hash algorithm which | |
4636 | * spreads the effect of every input bit onto every output bit, | |
4637 | * if someone who knows about such things would bother to write it. | |
4638 | * Might be a good idea to add that function to CORE as well. | |
4639 | * No numbers below come from careful analysis or anything here, | |
4640 | * except they are primes and SEED_C1 > 1E6 to get a full-width | |
4641 | * value from (tv_sec * SEED_C1 + tv_usec). The multipliers should | |
4642 | * probably be bigger too. | |
4643 | */ | |
4644 | #if RANDBITS > 16 | |
4645 | # define SEED_C1 1000003 | |
4646 | #define SEED_C4 73819 | |
4647 | #else | |
4648 | # define SEED_C1 25747 | |
4649 | #define SEED_C4 20639 | |
4650 | #endif | |
4651 | #define SEED_C2 3 | |
4652 | #define SEED_C3 269 | |
4653 | #define SEED_C5 26107 | |
4654 | ||
4655 | #ifndef PERL_NO_DEV_RANDOM | |
4656 | int fd; | |
4657 | #endif | |
4658 | U32 u; | |
95a8bf05 | 4659 | #ifdef HAS_GETTIMEOFDAY |
132efe8b | 4660 | struct timeval when; |
95a8bf05 | 4661 | #else |
132efe8b | 4662 | Time_t when; |
132efe8b JH |
4663 | #endif |
4664 | ||
4665 | /* This test is an escape hatch, this symbol isn't set by Configure. */ | |
4666 | #ifndef PERL_NO_DEV_RANDOM | |
4667 | #ifndef PERL_RANDOM_DEVICE | |
4668 | /* /dev/random isn't used by default because reads from it will block | |
4669 | * if there isn't enough entropy available. You can compile with | |
4670 | * PERL_RANDOM_DEVICE to it if you'd prefer Perl to block until there | |
4671 | * is enough real entropy to fill the seed. */ | |
afa49a03 AB |
4672 | # ifdef __amigaos4__ |
4673 | # define PERL_RANDOM_DEVICE "RANDOM:SIZE=4" | |
4674 | # else | |
4675 | # define PERL_RANDOM_DEVICE "/dev/urandom" | |
4676 | # endif | |
132efe8b JH |
4677 | #endif |
4678 | fd = PerlLIO_open(PERL_RANDOM_DEVICE, 0); | |
4679 | if (fd != -1) { | |
27da23d5 | 4680 | if (PerlLIO_read(fd, (void*)&u, sizeof u) != sizeof u) |
132efe8b JH |
4681 | u = 0; |
4682 | PerlLIO_close(fd); | |
4683 | if (u) | |
4684 | return u; | |
4685 | } | |
4686 | #endif | |
4687 | ||
95a8bf05 | 4688 | #ifdef HAS_GETTIMEOFDAY |
132efe8b JH |
4689 | PerlProc_gettimeofday(&when,NULL); |
4690 | u = (U32)SEED_C1 * when.tv_sec + (U32)SEED_C2 * when.tv_usec; | |
95a8bf05 | 4691 | #else |
132efe8b JH |
4692 | (void)time(&when); |
4693 | u = (U32)SEED_C1 * when; | |
132efe8b JH |
4694 | #endif |
4695 | u += SEED_C3 * (U32)PerlProc_getpid(); | |
4696 | u += SEED_C4 * (U32)PTR2UV(PL_stack_sp); | |
4697 | #ifndef PLAN9 /* XXX Plan9 assembler chokes on this; fix needed */ | |
4698 | u += SEED_C5 * (U32)PTR2UV(&when); | |
4699 | #endif | |
4700 | return u; | |
4701 | } | |
4702 | ||
7dc86639 | 4703 | void |
a2098e20 | 4704 | Perl_get_hash_seed(pTHX_ unsigned char * const seed_buffer) |
bed60192 | 4705 | { |
a2098e20 YO |
4706 | const char *env_pv; |
4707 | unsigned long i; | |
7dc86639 YO |
4708 | |
4709 | PERL_ARGS_ASSERT_GET_HASH_SEED; | |
bed60192 | 4710 | |
a2098e20 | 4711 | env_pv= PerlEnv_getenv("PERL_HASH_SEED"); |
7dc86639 | 4712 | |
a2098e20 | 4713 | if ( env_pv ) |
7dc86639 YO |
4714 | #ifndef USE_HASH_SEED_EXPLICIT |
4715 | { | |
a2098e20 YO |
4716 | /* ignore leading spaces */ |
4717 | while (isSPACE(*env_pv)) | |
4718 | env_pv++; | |
6a5b4183 | 4719 | #ifdef USE_PERL_PERTURB_KEYS |
a2098e20 YO |
4720 | /* if they set it to "0" we disable key traversal randomization completely */ |
4721 | if (strEQ(env_pv,"0")) { | |
6a5b4183 YO |
4722 | PL_hash_rand_bits_enabled= 0; |
4723 | } else { | |
a2098e20 | 4724 | /* otherwise switch to deterministic mode */ |
6a5b4183 YO |
4725 | PL_hash_rand_bits_enabled= 2; |
4726 | } | |
4727 | #endif | |
a2098e20 YO |
4728 | /* ignore a leading 0x... if it is there */ |
4729 | if (env_pv[0] == '0' && env_pv[1] == 'x') | |
4730 | env_pv += 2; | |
bed60192 | 4731 | |
a2098e20 YO |
4732 | for( i = 0; isXDIGIT(*env_pv) && i < PERL_HASH_SEED_BYTES; i++ ) { |
4733 | seed_buffer[i] = READ_XDIGIT(env_pv) << 4; | |
4734 | if ( isXDIGIT(*env_pv)) { | |
4735 | seed_buffer[i] |= READ_XDIGIT(env_pv); | |
7dc86639 | 4736 | } |
7dc86639 | 4737 | } |
a2098e20 YO |
4738 | while (isSPACE(*env_pv)) |
4739 | env_pv++; | |
4740 | ||
4741 | if (*env_pv && !isXDIGIT(*env_pv)) { | |
aac486f1 | 4742 | Perl_warn(aTHX_ "perl: warning: Non hex character in '$ENV{PERL_HASH_SEED}', seed only partially set\n"); |
73cf895c | 4743 | } |
7dc86639 | 4744 | /* should we check for unparsed crap? */ |
a2098e20 YO |
4745 | /* should we warn about unused hex? */ |
4746 | /* should we warn about insufficient hex? */ | |
7dc86639 YO |
4747 | } |
4748 | else | |
4749 | #endif | |
4750 | { | |
4751 | (void)seedDrand01((Rand_seed_t)seed()); | |
4752 | ||
a2098e20 YO |
4753 | for( i = 0; i < PERL_HASH_SEED_BYTES; i++ ) { |
4754 | seed_buffer[i] = (unsigned char)(Drand01() * (U8_MAX+1)); | |
7dc86639 | 4755 | } |
0e0ab621 | 4756 | } |
6a5b4183 | 4757 | #ifdef USE_PERL_PERTURB_KEYS |
0e0ab621 YO |
4758 | { /* initialize PL_hash_rand_bits from the hash seed. |
4759 | * This value is highly volatile, it is updated every | |
4760 | * hash insert, and is used as part of hash bucket chain | |
4761 | * randomization and hash iterator randomization. */ | |
a2098e20 | 4762 | PL_hash_rand_bits= 0xbe49d17f; /* I just picked a number */ |
0e0ab621 | 4763 | for( i = 0; i < sizeof(UV) ; i++ ) { |
6a5b4183 YO |
4764 | PL_hash_rand_bits += seed_buffer[i % PERL_HASH_SEED_BYTES]; |
4765 | PL_hash_rand_bits = ROTL_UV(PL_hash_rand_bits,8); | |
0e0ab621 YO |
4766 | } |
4767 | } | |
a2098e20 YO |
4768 | env_pv= PerlEnv_getenv("PERL_PERTURB_KEYS"); |
4769 | if (env_pv) { | |
4770 | if (strEQ(env_pv,"0") || strEQ(env_pv,"NO")) { | |
6a5b4183 | 4771 | PL_hash_rand_bits_enabled= 0; |
a2098e20 | 4772 | } else if (strEQ(env_pv,"1") || strEQ(env_pv,"RANDOM")) { |
6a5b4183 | 4773 | PL_hash_rand_bits_enabled= 1; |
a2098e20 | 4774 | } else if (strEQ(env_pv,"2") || strEQ(env_pv,"DETERMINISTIC")) { |
6a5b4183 YO |
4775 | PL_hash_rand_bits_enabled= 2; |
4776 | } else { | |
a2098e20 | 4777 | Perl_warn(aTHX_ "perl: warning: strange setting in '$ENV{PERL_PERTURB_KEYS}': '%s'\n", env_pv); |
6a5b4183 YO |
4778 | } |
4779 | } | |
4780 | #endif | |
bed60192 | 4781 | } |
27da23d5 JH |
4782 | |
4783 | #ifdef PERL_GLOBAL_STRUCT | |
4784 | ||
bae1192d JH |
4785 | #define PERL_GLOBAL_STRUCT_INIT |
4786 | #include "opcode.h" /* the ppaddr and check */ | |
4787 | ||
27da23d5 JH |
4788 | struct perl_vars * |
4789 | Perl_init_global_struct(pTHX) | |
4790 | { | |
4791 | struct perl_vars *plvarsp = NULL; | |
bae1192d | 4792 | # ifdef PERL_GLOBAL_STRUCT |
c3caa5c3 JH |
4793 | const IV nppaddr = C_ARRAY_LENGTH(Gppaddr); |
4794 | const IV ncheck = C_ARRAY_LENGTH(Gcheck); | |
23491f1d | 4795 | PERL_UNUSED_CONTEXT; |
27da23d5 JH |
4796 | # ifdef PERL_GLOBAL_STRUCT_PRIVATE |
4797 | /* PerlMem_malloc() because can't use even safesysmalloc() this early. */ | |
4798 | plvarsp = (struct perl_vars*)PerlMem_malloc(sizeof(struct perl_vars)); | |
4799 | if (!plvarsp) | |
4800 | exit(1); | |
4801 | # else | |
4802 | plvarsp = PL_VarsPtr; | |
4803 | # endif /* PERL_GLOBAL_STRUCT_PRIVATE */ | |
aadb217d JH |
4804 | # undef PERLVAR |
4805 | # undef PERLVARA | |
4806 | # undef PERLVARI | |
4807 | # undef PERLVARIC | |
115ff745 NC |
4808 | # define PERLVAR(prefix,var,type) /**/ |
4809 | # define PERLVARA(prefix,var,n,type) /**/ | |
4810 | # define PERLVARI(prefix,var,type,init) plvarsp->prefix##var = init; | |
4811 | # define PERLVARIC(prefix,var,type,init) plvarsp->prefix##var = init; | |
27da23d5 JH |
4812 | # include "perlvars.h" |
4813 | # undef PERLVAR | |
4814 | # undef PERLVARA | |
4815 | # undef PERLVARI | |
4816 | # undef PERLVARIC | |
27da23d5 | 4817 | # ifdef PERL_GLOBAL_STRUCT |
bae1192d JH |
4818 | plvarsp->Gppaddr = |
4819 | (Perl_ppaddr_t*) | |
4820 | PerlMem_malloc(nppaddr * sizeof(Perl_ppaddr_t)); | |
27da23d5 JH |
4821 | if (!plvarsp->Gppaddr) |
4822 | exit(1); | |
bae1192d JH |
4823 | plvarsp->Gcheck = |
4824 | (Perl_check_t*) | |
4825 | PerlMem_malloc(ncheck * sizeof(Perl_check_t)); | |
27da23d5 JH |
4826 | if (!plvarsp->Gcheck) |
4827 | exit(1); | |
4828 | Copy(Gppaddr, plvarsp->Gppaddr, nppaddr, Perl_ppaddr_t); | |
4829 | Copy(Gcheck, plvarsp->Gcheck, ncheck, Perl_check_t); | |
4830 | # endif | |
4831 | # ifdef PERL_SET_VARS | |
4832 | PERL_SET_VARS(plvarsp); | |
4833 | # endif | |
5c64bffd NC |
4834 | # ifdef PERL_GLOBAL_STRUCT_PRIVATE |
4835 | plvarsp->Gsv_placeholder.sv_flags = 0; | |
4836 | memset(plvarsp->Ghash_seed, 0, sizeof(plvarsp->Ghash_seed)); | |
4837 | # endif | |
bae1192d JH |
4838 | # undef PERL_GLOBAL_STRUCT_INIT |
4839 | # endif | |
27da23d5 JH |
4840 | return plvarsp; |
4841 | } | |
4842 | ||
4843 | #endif /* PERL_GLOBAL_STRUCT */ | |
4844 | ||
4845 | #ifdef PERL_GLOBAL_STRUCT | |
4846 | ||
4847 | void | |
4848 | Perl_free_global_struct(pTHX_ struct perl_vars *plvarsp) | |
4849 | { | |
c1181d2b DM |
4850 | int veto = plvarsp->Gveto_cleanup; |
4851 | ||
7918f24d | 4852 | PERL_ARGS_ASSERT_FREE_GLOBAL_STRUCT; |
23491f1d | 4853 | PERL_UNUSED_CONTEXT; |
bae1192d | 4854 | # ifdef PERL_GLOBAL_STRUCT |
27da23d5 JH |
4855 | # ifdef PERL_UNSET_VARS |
4856 | PERL_UNSET_VARS(plvarsp); | |
4857 | # endif | |
c1181d2b DM |
4858 | if (veto) |
4859 | return; | |
27da23d5 JH |
4860 | free(plvarsp->Gppaddr); |
4861 | free(plvarsp->Gcheck); | |
bae1192d | 4862 | # ifdef PERL_GLOBAL_STRUCT_PRIVATE |
27da23d5 | 4863 | free(plvarsp); |
bae1192d JH |
4864 | # endif |
4865 | # endif | |
27da23d5 JH |
4866 | } |
4867 | ||
4868 | #endif /* PERL_GLOBAL_STRUCT */ | |
4869 | ||
fe4f188c JH |
4870 | #ifdef PERL_MEM_LOG |
4871 | ||
22ff3130 | 4872 | /* -DPERL_MEM_LOG: the Perl_mem_log_..() is compiled, including |
73d1d973 JC |
4873 | * the default implementation, unless -DPERL_MEM_LOG_NOIMPL is also |
4874 | * given, and you supply your own implementation. | |
65ceff02 | 4875 | * |
2e5b5004 | 4876 | * The default implementation reads a single env var, PERL_MEM_LOG, |
1cd8acb5 JC |
4877 | * expecting one or more of the following: |
4878 | * | |
22ff3130 | 4879 | * \d+ - fd fd to write to : must be 1st (grok_atoUV) |
2e5b5004 | 4880 | * 'm' - memlog was PERL_MEM_LOG=1 |
1cd8acb5 JC |
4881 | * 's' - svlog was PERL_SV_LOG=1 |
4882 | * 't' - timestamp was PERL_MEM_LOG_TIMESTAMP=1 | |
0b0ab801 | 4883 | * |
1cd8acb5 JC |
4884 | * This makes the logger controllable enough that it can reasonably be |
4885 | * added to the system perl. | |
65ceff02 JH |
4886 | */ |
4887 | ||
1cd8acb5 | 4888 | /* -DPERL_MEM_LOG_SPRINTF_BUF_SIZE=X: size of a (stack-allocated) buffer |
65ceff02 JH |
4889 | * the Perl_mem_log_...() will use (either via sprintf or snprintf). |
4890 | */ | |
e352bcff JH |
4891 | #define PERL_MEM_LOG_SPRINTF_BUF_SIZE 128 |
4892 | ||
1cd8acb5 JC |
4893 | /* -DPERL_MEM_LOG_FD=N: the file descriptor the Perl_mem_log_...() |
4894 | * writes to. In the default logger, this is settable at runtime. | |
65ceff02 JH |
4895 | */ |
4896 | #ifndef PERL_MEM_LOG_FD | |
4897 | # define PERL_MEM_LOG_FD 2 /* If STDERR is too boring for you. */ | |
4898 | #endif | |
4899 | ||
73d1d973 | 4900 | #ifndef PERL_MEM_LOG_NOIMPL |
d7a2c63c MHM |
4901 | |
4902 | # ifdef DEBUG_LEAKING_SCALARS | |
4903 | # define SV_LOG_SERIAL_FMT " [%lu]" | |
4904 | # define _SV_LOG_SERIAL_ARG(sv) , (unsigned long) (sv)->sv_debug_serial | |
4905 | # else | |
4906 | # define SV_LOG_SERIAL_FMT | |
4907 | # define _SV_LOG_SERIAL_ARG(sv) | |
4908 | # endif | |
4909 | ||
0b0ab801 | 4910 | static void |
73d1d973 JC |
4911 | S_mem_log_common(enum mem_log_type mlt, const UV n, |
4912 | const UV typesize, const char *type_name, const SV *sv, | |
4913 | Malloc_t oldalloc, Malloc_t newalloc, | |
4914 | const char *filename, const int linenumber, | |
4915 | const char *funcname) | |
0b0ab801 | 4916 | { |
1cd8acb5 | 4917 | const char *pmlenv; |
4ca7bcef | 4918 | |
1cd8acb5 | 4919 | PERL_ARGS_ASSERT_MEM_LOG_COMMON; |
4ca7bcef | 4920 | |
1cd8acb5 JC |
4921 | pmlenv = PerlEnv_getenv("PERL_MEM_LOG"); |
4922 | if (!pmlenv) | |
4923 | return; | |
4924 | if (mlt < MLT_NEW_SV ? strchr(pmlenv,'m') : strchr(pmlenv,'s')) | |
65ceff02 JH |
4925 | { |
4926 | /* We can't use SVs or PerlIO for obvious reasons, | |
4927 | * so we'll use stdio and low-level IO instead. */ | |
4928 | char buf[PERL_MEM_LOG_SPRINTF_BUF_SIZE]; | |
1cd8acb5 | 4929 | |
5b692037 | 4930 | # ifdef HAS_GETTIMEOFDAY |
0b0ab801 MHM |
4931 | # define MEM_LOG_TIME_FMT "%10d.%06d: " |
4932 | # define MEM_LOG_TIME_ARG (int)tv.tv_sec, (int)tv.tv_usec | |
4933 | struct timeval tv; | |
65ceff02 | 4934 | gettimeofday(&tv, 0); |
0b0ab801 MHM |
4935 | # else |
4936 | # define MEM_LOG_TIME_FMT "%10d: " | |
4937 | # define MEM_LOG_TIME_ARG (int)when | |
4938 | Time_t when; | |
4939 | (void)time(&when); | |
5b692037 JH |
4940 | # endif |
4941 | /* If there are other OS specific ways of hires time than | |
d0b0e707 | 4942 | * gettimeofday() (see dist/Time-HiRes), the easiest way is |
5b692037 JH |
4943 | * probably that they would be used to fill in the struct |
4944 | * timeval. */ | |
65ceff02 | 4945 | { |
0b0ab801 | 4946 | STRLEN len; |
96e440d2 | 4947 | const char* endptr; |
22ff3130 HS |
4948 | int fd; |
4949 | UV uv; | |
4950 | if (grok_atoUV(pmlenv, &uv, &endptr) /* Ignore endptr. */ | |
4951 | && uv && uv <= PERL_INT_MAX | |
4952 | ) { | |
4953 | fd = (int)uv; | |
4954 | } else { | |
1cd8acb5 | 4955 | fd = PERL_MEM_LOG_FD; |
22ff3130 | 4956 | } |
0b0ab801 | 4957 | |
1cd8acb5 | 4958 | if (strchr(pmlenv, 't')) { |
0b0ab801 MHM |
4959 | len = my_snprintf(buf, sizeof(buf), |
4960 | MEM_LOG_TIME_FMT, MEM_LOG_TIME_ARG); | |
acfd4d8e | 4961 | PERL_UNUSED_RESULT(PerlLIO_write(fd, buf, len)); |
0b0ab801 | 4962 | } |
0b0ab801 MHM |
4963 | switch (mlt) { |
4964 | case MLT_ALLOC: | |
4965 | len = my_snprintf(buf, sizeof(buf), | |
4966 | "alloc: %s:%d:%s: %"IVdf" %"UVuf | |
4967 | " %s = %"IVdf": %"UVxf"\n", | |
4968 | filename, linenumber, funcname, n, typesize, | |
bef8a128 | 4969 | type_name, n * typesize, PTR2UV(newalloc)); |
0b0ab801 MHM |
4970 | break; |
4971 | case MLT_REALLOC: | |
4972 | len = my_snprintf(buf, sizeof(buf), | |
4973 | "realloc: %s:%d:%s: %"IVdf" %"UVuf | |
4974 | " %s = %"IVdf": %"UVxf" -> %"UVxf"\n", | |
4975 | filename, linenumber, funcname, n, typesize, | |
bef8a128 | 4976 | type_name, n * typesize, PTR2UV(oldalloc), |
0b0ab801 MHM |
4977 | PTR2UV(newalloc)); |
4978 | break; | |
4979 | case MLT_FREE: | |
4980 | len = my_snprintf(buf, sizeof(buf), | |
4981 | "free: %s:%d:%s: %"UVxf"\n", | |
4982 | filename, linenumber, funcname, | |
4983 | PTR2UV(oldalloc)); | |
4984 | break; | |
d7a2c63c MHM |
4985 | case MLT_NEW_SV: |
4986 | case MLT_DEL_SV: | |
4987 | len = my_snprintf(buf, sizeof(buf), | |
4988 | "%s_SV: %s:%d:%s: %"UVxf SV_LOG_SERIAL_FMT "\n", | |
4989 | mlt == MLT_NEW_SV ? "new" : "del", | |
4990 | filename, linenumber, funcname, | |
4991 | PTR2UV(sv) _SV_LOG_SERIAL_ARG(sv)); | |
4992 | break; | |
73d1d973 JC |
4993 | default: |
4994 | len = 0; | |
0b0ab801 | 4995 | } |
acfd4d8e | 4996 | PERL_UNUSED_RESULT(PerlLIO_write(fd, buf, len)); |
65ceff02 JH |
4997 | } |
4998 | } | |
0b0ab801 | 4999 | } |
73d1d973 JC |
5000 | #endif /* !PERL_MEM_LOG_NOIMPL */ |
5001 | ||
5002 | #ifndef PERL_MEM_LOG_NOIMPL | |
5003 | # define \ | |
5004 | mem_log_common_if(alty, num, tysz, tynm, sv, oal, nal, flnm, ln, fnnm) \ | |
5005 | mem_log_common (alty, num, tysz, tynm, sv, oal, nal, flnm, ln, fnnm) | |
5006 | #else | |
5007 | /* this is suboptimal, but bug compatible. User is providing their | |
486ec47a | 5008 | own implementation, but is getting these functions anyway, and they |
73d1d973 JC |
5009 | do nothing. But _NOIMPL users should be able to cope or fix */ |
5010 | # define \ | |
5011 | mem_log_common_if(alty, num, tysz, tynm, u, oal, nal, flnm, ln, fnnm) \ | |
5012 | /* mem_log_common_if_PERL_MEM_LOG_NOIMPL */ | |
0b0ab801 MHM |
5013 | #endif |
5014 | ||
5015 | Malloc_t | |
73d1d973 JC |
5016 | Perl_mem_log_alloc(const UV n, const UV typesize, const char *type_name, |
5017 | Malloc_t newalloc, | |
5018 | const char *filename, const int linenumber, | |
5019 | const char *funcname) | |
5020 | { | |
6fb87544 MH |
5021 | PERL_ARGS_ASSERT_MEM_LOG_ALLOC; |
5022 | ||
73d1d973 JC |
5023 | mem_log_common_if(MLT_ALLOC, n, typesize, type_name, |
5024 | NULL, NULL, newalloc, | |
5025 | filename, linenumber, funcname); | |
fe4f188c JH |
5026 | return newalloc; |
5027 | } | |
5028 | ||
5029 | Malloc_t | |
73d1d973 JC |
5030 | Perl_mem_log_realloc(const UV n, const UV typesize, const char *type_name, |
5031 | Malloc_t oldalloc, Malloc_t newalloc, | |
5032 | const char *filename, const int linenumber, | |
5033 | const char *funcname) | |
5034 | { | |
6fb87544 MH |
5035 | PERL_ARGS_ASSERT_MEM_LOG_REALLOC; |
5036 | ||
73d1d973 JC |
5037 | mem_log_common_if(MLT_REALLOC, n, typesize, type_name, |
5038 | NULL, oldalloc, newalloc, | |
5039 | filename, linenumber, funcname); | |
fe4f188c JH |
5040 | return newalloc; |
5041 | } | |
5042 | ||
5043 | Malloc_t | |
73d1d973 JC |
5044 | Perl_mem_log_free(Malloc_t oldalloc, |
5045 | const char *filename, const int linenumber, | |
5046 | const char *funcname) | |
fe4f188c | 5047 | { |
6fb87544 MH |
5048 | PERL_ARGS_ASSERT_MEM_LOG_FREE; |
5049 | ||
73d1d973 JC |
5050 | mem_log_common_if(MLT_FREE, 0, 0, "", NULL, oldalloc, NULL, |
5051 | filename, linenumber, funcname); | |
fe4f188c JH |
5052 | return oldalloc; |
5053 | } | |
5054 | ||
d7a2c63c | 5055 | void |
73d1d973 JC |
5056 | Perl_mem_log_new_sv(const SV *sv, |
5057 | const char *filename, const int linenumber, | |
5058 | const char *funcname) | |
d7a2c63c | 5059 | { |
73d1d973 JC |
5060 | mem_log_common_if(MLT_NEW_SV, 0, 0, "", sv, NULL, NULL, |
5061 | filename, linenumber, funcname); | |
d7a2c63c MHM |
5062 | } |
5063 | ||
5064 | void | |
73d1d973 JC |
5065 | Perl_mem_log_del_sv(const SV *sv, |
5066 | const char *filename, const int linenumber, | |
5067 | const char *funcname) | |
d7a2c63c | 5068 | { |
73d1d973 JC |
5069 | mem_log_common_if(MLT_DEL_SV, 0, 0, "", sv, NULL, NULL, |
5070 | filename, linenumber, funcname); | |
d7a2c63c MHM |
5071 | } |
5072 | ||
fe4f188c JH |
5073 | #endif /* PERL_MEM_LOG */ |
5074 | ||
66610fdd | 5075 | /* |
ce582cee NC |
5076 | =for apidoc my_sprintf |
5077 | ||
5078 | The C library C<sprintf>, wrapped if necessary, to ensure that it will return | |
72d33970 | 5079 | the length of the string written to the buffer. Only rare pre-ANSI systems |
ce582cee NC |
5080 | need the wrapper function - usually this is a direct call to C<sprintf>. |
5081 | ||
5082 | =cut | |
5083 | */ | |
5084 | #ifndef SPRINTF_RETURNS_STRLEN | |
5085 | int | |
5086 | Perl_my_sprintf(char *buffer, const char* pat, ...) | |
5087 | { | |
5088 | va_list args; | |
7918f24d | 5089 | PERL_ARGS_ASSERT_MY_SPRINTF; |
ce582cee NC |
5090 | va_start(args, pat); |
5091 | vsprintf(buffer, pat, args); | |
5092 | va_end(args); | |
5093 | return strlen(buffer); | |
5094 | } | |
5095 | #endif | |
5096 | ||
d9fad198 | 5097 | /* |
a4eca1d4 JH |
5098 | =for apidoc quadmath_format_single |
5099 | ||
796b6530 | 5100 | C<quadmath_snprintf()> is very strict about its C<format> string and will |
801caa78 | 5101 | fail, returning -1, if the format is invalid. It accepts exactly |
a4eca1d4 JH |
5102 | one format spec. |
5103 | ||
796b6530 | 5104 | C<quadmath_format_single()> checks that the intended single spec looks |
a4eca1d4 JH |
5105 | sane: begins with C<%>, has only one C<%>, ends with C<[efgaEFGA]>, |
5106 | and has C<Q> before it. This is not a full "printf syntax check", | |
5107 | just the basics. | |
5108 | ||
5109 | Returns the format if it is valid, NULL if not. | |
5110 | ||
796b6530 | 5111 | C<quadmath_format_single()> can and will actually patch in the missing |
a4eca1d4 JH |
5112 | C<Q>, if necessary. In this case it will return the modified copy of |
5113 | the format, B<which the caller will need to free.> | |
5114 | ||
5115 | See also L</quadmath_format_needed>. | |
5116 | ||
5117 | =cut | |
5118 | */ | |
5119 | #ifdef USE_QUADMATH | |
5120 | const char* | |
5121 | Perl_quadmath_format_single(const char* format) | |
5122 | { | |
5123 | STRLEN len; | |
5124 | ||
5125 | PERL_ARGS_ASSERT_QUADMATH_FORMAT_SINGLE; | |
5126 | ||
5127 | if (format[0] != '%' || strchr(format + 1, '%')) | |
5128 | return NULL; | |
5129 | len = strlen(format); | |
5130 | /* minimum length three: %Qg */ | |
5131 | if (len < 3 || strchr("efgaEFGA", format[len - 1]) == NULL) | |
5132 | return NULL; | |
5133 | if (format[len - 2] != 'Q') { | |
5134 | char* fixed; | |
5135 | Newx(fixed, len + 1, char); | |
5136 | memcpy(fixed, format, len - 1); | |
5137 | fixed[len - 1] = 'Q'; | |
5138 | fixed[len ] = format[len - 1]; | |
5139 | fixed[len + 1] = 0; | |
5140 | return (const char*)fixed; | |
5141 | } | |
5142 | return format; | |
5143 | } | |
5144 | #endif | |
5145 | ||
5146 | /* | |
5147 | =for apidoc quadmath_format_needed | |
5148 | ||
796b6530 KW |
5149 | C<quadmath_format_needed()> returns true if the C<format> string seems to |
5150 | contain at least one non-Q-prefixed C<%[efgaEFGA]> format specifier, | |
a4eca1d4 JH |
5151 | or returns false otherwise. |
5152 | ||
5153 | The format specifier detection is not complete printf-syntax detection, | |
5154 | but it should catch most common cases. | |
5155 | ||
5156 | If true is returned, those arguments B<should> in theory be processed | |
796b6530 | 5157 | with C<quadmath_snprintf()>, but in case there is more than one such |
a4eca1d4 JH |
5158 | format specifier (see L</quadmath_format_single>), and if there is |
5159 | anything else beyond that one (even just a single byte), they | |
796b6530 | 5160 | B<cannot> be processed because C<quadmath_snprintf()> is very strict, |
a4eca1d4 JH |
5161 | accepting only one format spec, and nothing else. |
5162 | In this case, the code should probably fail. | |
5163 | ||
5164 | =cut | |
5165 | */ | |
5166 | #ifdef USE_QUADMATH | |
5167 | bool | |
5168 | Perl_quadmath_format_needed(const char* format) | |
5169 | { | |
5170 | const char *p = format; | |
5171 | const char *q; | |
5172 | ||
5173 | PERL_ARGS_ASSERT_QUADMATH_FORMAT_NEEDED; | |
5174 | ||
5175 | while ((q = strchr(p, '%'))) { | |
5176 | q++; | |
5177 | if (*q == '+') /* plus */ | |
5178 | q++; | |
5179 | if (*q == '#') /* alt */ | |
5180 | q++; | |
5181 | if (*q == '*') /* width */ | |
5182 | q++; | |
5183 | else { | |
5184 | if (isDIGIT(*q)) { | |
5185 | while (isDIGIT(*q)) q++; | |
5186 | } | |
5187 | } | |
5188 | if (*q == '.' && (q[1] == '*' || isDIGIT(q[1]))) { /* prec */ | |
5189 | q++; | |
5190 | if (*q == '*') | |
5191 | q++; | |
5192 | else | |
5193 | while (isDIGIT(*q)) q++; | |
5194 | } | |
5195 | if (strchr("efgaEFGA", *q)) /* Would have needed 'Q' in front. */ | |
5196 | return TRUE; | |
5197 | p = q + 1; | |
5198 | } | |
5199 | return FALSE; | |
5200 | } | |
5201 | #endif | |
5202 | ||
5203 | /* | |
d9fad198 JH |
5204 | =for apidoc my_snprintf |
5205 | ||
5206 | The C library C<snprintf> functionality, if available and | |
5b692037 | 5207 | standards-compliant (uses C<vsnprintf>, actually). However, if the |
d9fad198 | 5208 | C<vsnprintf> is not available, will unfortunately use the unsafe |
5b692037 JH |
5209 | C<vsprintf> which can overrun the buffer (there is an overrun check, |
5210 | but that may be too late). Consider using C<sv_vcatpvf> instead, or | |
5211 | getting C<vsnprintf>. | |
d9fad198 JH |
5212 | |
5213 | =cut | |
5214 | */ | |
5215 | int | |
5216 | Perl_my_snprintf(char *buffer, const Size_t len, const char *format, ...) | |
d9fad198 | 5217 | { |
a4eca1d4 | 5218 | int retval = -1; |
d9fad198 | 5219 | va_list ap; |
7918f24d | 5220 | PERL_ARGS_ASSERT_MY_SNPRINTF; |
f9adac4b JH |
5221 | #ifndef HAS_VSNPRINTF |
5222 | PERL_UNUSED_VAR(len); | |
5223 | #endif | |
d9fad198 | 5224 | va_start(ap, format); |
a4eca1d4 JH |
5225 | #ifdef USE_QUADMATH |
5226 | { | |
5227 | const char* qfmt = quadmath_format_single(format); | |
5228 | bool quadmath_valid = FALSE; | |
5229 | if (qfmt) { | |
5230 | /* If the format looked promising, use it as quadmath. */ | |
5231 | retval = quadmath_snprintf(buffer, len, qfmt, va_arg(ap, NV)); | |
5232 | if (retval == -1) | |
5233 | Perl_croak_nocontext("panic: quadmath_snprintf failed, format \"%s\"", qfmt); | |
5234 | quadmath_valid = TRUE; | |
5235 | if (qfmt != format) | |
5236 | Safefree(qfmt); | |
5237 | qfmt = NULL; | |
5238 | } | |
5239 | assert(qfmt == NULL); | |
5240 | /* quadmath_format_single() will return false for example for | |
5241 | * "foo = %g", or simply "%g". We could handle the %g by | |
5242 | * using quadmath for the NV args. More complex cases of | |
5243 | * course exist: "foo = %g, bar = %g", or "foo=%Qg" (otherwise | |
5244 | * quadmath-valid but has stuff in front). | |
5245 | * | |
5246 | * Handling the "Q-less" cases right would require walking | |
5247 | * through the va_list and rewriting the format, calling | |
5248 | * quadmath for the NVs, building a new va_list, and then | |
5249 | * letting vsnprintf/vsprintf to take care of the other | |
5250 | * arguments. This may be doable. | |
5251 | * | |
5252 | * We do not attempt that now. But for paranoia, we here try | |
5253 | * to detect some common (but not all) cases where the | |
5254 | * "Q-less" %[efgaEFGA] formats are present, and die if | |
5255 | * detected. This doesn't fix the problem, but it stops the | |
5256 | * vsnprintf/vsprintf pulling doubles off the va_list when | |
5257 | * __float128 NVs should be pulled off instead. | |
5258 | * | |
5259 | * If quadmath_format_needed() returns false, we are reasonably | |
5260 | * certain that we can call vnsprintf() or vsprintf() safely. */ | |
5261 | if (!quadmath_valid && quadmath_format_needed(format)) | |
5262 | Perl_croak_nocontext("panic: quadmath_snprintf failed, format \"%s\"", format); | |
5263 | ||
5264 | } | |
5265 | #endif | |
5266 | if (retval == -1) | |
5b692037 | 5267 | #ifdef HAS_VSNPRINTF |
a4eca1d4 | 5268 | retval = vsnprintf(buffer, len, format, ap); |
d9fad198 | 5269 | #else |
a4eca1d4 | 5270 | retval = vsprintf(buffer, format, ap); |
d9fad198 JH |
5271 | #endif |
5272 | va_end(ap); | |
7dac5c64 RB |
5273 | /* vsprintf() shows failure with < 0 */ |
5274 | if (retval < 0 | |
5275 | #ifdef HAS_VSNPRINTF | |
5276 | /* vsnprintf() shows failure with >= len */ | |
5277 | || | |
5278 | (len > 0 && (Size_t)retval >= len) | |
5279 | #endif | |
5280 | ) | |
dbf7dff6 | 5281 | Perl_croak_nocontext("panic: my_snprintf buffer overflow"); |
d9fad198 JH |
5282 | return retval; |
5283 | } | |
5284 | ||
5285 | /* | |
5286 | =for apidoc my_vsnprintf | |
5287 | ||
5b692037 JH |
5288 | The C library C<vsnprintf> if available and standards-compliant. |
5289 | However, if if the C<vsnprintf> is not available, will unfortunately | |
5290 | use the unsafe C<vsprintf> which can overrun the buffer (there is an | |
5291 | overrun check, but that may be too late). Consider using | |
5292 | C<sv_vcatpvf> instead, or getting C<vsnprintf>. | |
d9fad198 JH |
5293 | |
5294 | =cut | |
5295 | */ | |
5296 | int | |
5297 | Perl_my_vsnprintf(char *buffer, const Size_t len, const char *format, va_list ap) | |
d9fad198 | 5298 | { |
a4eca1d4 JH |
5299 | #ifdef USE_QUADMATH |
5300 | PERL_UNUSED_ARG(buffer); | |
5301 | PERL_UNUSED_ARG(len); | |
5302 | PERL_UNUSED_ARG(format); | |
5303 | PERL_UNUSED_ARG(ap); | |
5304 | Perl_croak_nocontext("panic: my_vsnprintf not available with quadmath"); | |
5305 | return 0; | |
5306 | #else | |
d9fad198 | 5307 | int retval; |
d9fad198 JH |
5308 | #ifdef NEED_VA_COPY |
5309 | va_list apc; | |
7918f24d NC |
5310 | |
5311 | PERL_ARGS_ASSERT_MY_VSNPRINTF; | |
239fec62 | 5312 | Perl_va_copy(ap, apc); |
5b692037 | 5313 | # ifdef HAS_VSNPRINTF |
d9fad198 JH |
5314 | retval = vsnprintf(buffer, len, format, apc); |
5315 | # else | |
52039d8f | 5316 | PERL_UNUSED_ARG(len); |
d9fad198 JH |
5317 | retval = vsprintf(buffer, format, apc); |
5318 | # endif | |
d4825b27 | 5319 | va_end(apc); |
d9fad198 | 5320 | #else |
5b692037 | 5321 | # ifdef HAS_VSNPRINTF |
d9fad198 JH |
5322 | retval = vsnprintf(buffer, len, format, ap); |
5323 | # else | |
52039d8f | 5324 | PERL_UNUSED_ARG(len); |
d9fad198 JH |
5325 | retval = vsprintf(buffer, format, ap); |
5326 | # endif | |
5b692037 | 5327 | #endif /* #ifdef NEED_VA_COPY */ |
7dac5c64 RB |
5328 | /* vsprintf() shows failure with < 0 */ |
5329 | if (retval < 0 | |
5330 | #ifdef HAS_VSNPRINTF | |
5331 | /* vsnprintf() shows failure with >= len */ | |
5332 | || | |
5333 | (len > 0 && (Size_t)retval >= len) | |
5334 | #endif | |
5335 | ) | |
dbf7dff6 | 5336 | Perl_croak_nocontext("panic: my_vsnprintf buffer overflow"); |
d9fad198 | 5337 | return retval; |
a4eca1d4 | 5338 | #endif |
d9fad198 JH |
5339 | } |
5340 | ||
b0269e46 AB |
5341 | void |
5342 | Perl_my_clearenv(pTHX) | |
5343 | { | |
5344 | dVAR; | |
5345 | #if ! defined(PERL_MICRO) | |
5346 | # if defined(PERL_IMPLICIT_SYS) || defined(WIN32) | |
5347 | PerlEnv_clearenv(); | |
5348 | # else /* ! (PERL_IMPLICIT_SYS || WIN32) */ | |
5349 | # if defined(USE_ENVIRON_ARRAY) | |
5350 | # if defined(USE_ITHREADS) | |
5351 | /* only the parent thread can clobber the process environment */ | |
5352 | if (PL_curinterp == aTHX) | |
5353 | # endif /* USE_ITHREADS */ | |
5354 | { | |
5355 | # if ! defined(PERL_USE_SAFE_PUTENV) | |
5356 | if ( !PL_use_safe_putenv) { | |
5357 | I32 i; | |
5358 | if (environ == PL_origenviron) | |
5359 | environ = (char**)safesysmalloc(sizeof(char*)); | |
5360 | else | |
5361 | for (i = 0; environ[i]; i++) | |
5362 | (void)safesysfree(environ[i]); | |
5363 | } | |
5364 | environ[0] = NULL; | |
5365 | # else /* PERL_USE_SAFE_PUTENV */ | |
5366 | # if defined(HAS_CLEARENV) | |
5367 | (void)clearenv(); | |
5368 | # elif defined(HAS_UNSETENV) | |
5369 | int bsiz = 80; /* Most envvar names will be shorter than this. */ | |
a96bc635 | 5370 | char *buf = (char*)safesysmalloc(bsiz); |
b0269e46 AB |
5371 | while (*environ != NULL) { |
5372 | char *e = strchr(*environ, '='); | |
b57a0404 | 5373 | int l = e ? e - *environ : (int)strlen(*environ); |
b0269e46 AB |
5374 | if (bsiz < l + 1) { |
5375 | (void)safesysfree(buf); | |
1bdfa2de | 5376 | bsiz = l + 1; /* + 1 for the \0. */ |
a96bc635 | 5377 | buf = (char*)safesysmalloc(bsiz); |
b0269e46 | 5378 | } |
82d8bb49 NC |
5379 | memcpy(buf, *environ, l); |
5380 | buf[l] = '\0'; | |
b0269e46 AB |
5381 | (void)unsetenv(buf); |
5382 | } | |
5383 | (void)safesysfree(buf); | |
5384 | # else /* ! HAS_CLEARENV && ! HAS_UNSETENV */ | |
5385 | /* Just null environ and accept the leakage. */ | |
5386 | *environ = NULL; | |
5387 | # endif /* HAS_CLEARENV || HAS_UNSETENV */ | |
5388 | # endif /* ! PERL_USE_SAFE_PUTENV */ | |
5389 | } | |
5390 | # endif /* USE_ENVIRON_ARRAY */ | |
5391 | # endif /* PERL_IMPLICIT_SYS || WIN32 */ | |
5392 | #endif /* PERL_MICRO */ | |
5393 | } | |
5394 | ||
f16dd614 DM |
5395 | #ifdef PERL_IMPLICIT_CONTEXT |
5396 | ||
53d44271 | 5397 | /* Implements the MY_CXT_INIT macro. The first time a module is loaded, |
f16dd614 DM |
5398 | the global PL_my_cxt_index is incremented, and that value is assigned to |
5399 | that module's static my_cxt_index (who's address is passed as an arg). | |
5400 | Then, for each interpreter this function is called for, it makes sure a | |
5401 | void* slot is available to hang the static data off, by allocating or | |
5402 | extending the interpreter's PL_my_cxt_list array */ | |
5403 | ||
53d44271 | 5404 | #ifndef PERL_GLOBAL_STRUCT_PRIVATE |
f16dd614 DM |
5405 | void * |
5406 | Perl_my_cxt_init(pTHX_ int *index, size_t size) | |
5407 | { | |
97aff369 | 5408 | dVAR; |
f16dd614 | 5409 | void *p; |
7918f24d | 5410 | PERL_ARGS_ASSERT_MY_CXT_INIT; |
f16dd614 DM |
5411 | if (*index == -1) { |
5412 | /* this module hasn't been allocated an index yet */ | |
8703a9a4 | 5413 | #if defined(USE_ITHREADS) |
f16dd614 | 5414 | MUTEX_LOCK(&PL_my_ctx_mutex); |
8703a9a4 | 5415 | #endif |
f16dd614 | 5416 | *index = PL_my_cxt_index++; |
8703a9a4 | 5417 | #if defined(USE_ITHREADS) |
f16dd614 | 5418 | MUTEX_UNLOCK(&PL_my_ctx_mutex); |
8703a9a4 | 5419 | #endif |
f16dd614 DM |
5420 | } |
5421 | ||
5422 | /* make sure the array is big enough */ | |
4c901e72 DM |
5423 | if (PL_my_cxt_size <= *index) { |
5424 | if (PL_my_cxt_size) { | |
5425 | while (PL_my_cxt_size <= *index) | |
f16dd614 DM |
5426 | PL_my_cxt_size *= 2; |
5427 | Renew(PL_my_cxt_list, PL_my_cxt_size, void *); | |
5428 | } | |
5429 | else { | |
5430 | PL_my_cxt_size = 16; | |
5431 | Newx(PL_my_cxt_list, PL_my_cxt_size, void *); | |
5432 | } | |
5433 | } | |
5434 | /* newSV() allocates one more than needed */ | |
5435 | p = (void*)SvPVX(newSV(size-1)); | |
5436 | PL_my_cxt_list[*index] = p; | |
5437 | Zero(p, size, char); | |
5438 | return p; | |
5439 | } | |
53d44271 JH |
5440 | |
5441 | #else /* #ifndef PERL_GLOBAL_STRUCT_PRIVATE */ | |
5442 | ||
5443 | int | |
5444 | Perl_my_cxt_index(pTHX_ const char *my_cxt_key) | |
5445 | { | |
5446 | dVAR; | |
5447 | int index; | |
5448 | ||
7918f24d NC |
5449 | PERL_ARGS_ASSERT_MY_CXT_INDEX; |
5450 | ||
53d44271 JH |
5451 | for (index = 0; index < PL_my_cxt_index; index++) { |
5452 | const char *key = PL_my_cxt_keys[index]; | |
5453 | /* try direct pointer compare first - there are chances to success, | |
5454 | * and it's much faster. | |
5455 | */ | |
5456 | if ((key == my_cxt_key) || strEQ(key, my_cxt_key)) | |
5457 | return index; | |
5458 | } | |
5459 | return -1; | |
5460 | } | |
5461 | ||
5462 | void * | |
5463 | Perl_my_cxt_init(pTHX_ const char *my_cxt_key, size_t size) | |
5464 | { | |
5465 | dVAR; | |
5466 | void *p; | |
5467 | int index; | |
5468 | ||
7918f24d NC |
5469 | PERL_ARGS_ASSERT_MY_CXT_INIT; |
5470 | ||
53d44271 JH |
5471 | index = Perl_my_cxt_index(aTHX_ my_cxt_key); |
5472 | if (index == -1) { | |
5473 | /* this module hasn't been allocated an index yet */ | |
8703a9a4 | 5474 | #if defined(USE_ITHREADS) |
53d44271 | 5475 | MUTEX_LOCK(&PL_my_ctx_mutex); |
8703a9a4 | 5476 | #endif |
53d44271 | 5477 | index = PL_my_cxt_index++; |
8703a9a4 | 5478 | #if defined(USE_ITHREADS) |
53d44271 | 5479 | MUTEX_UNLOCK(&PL_my_ctx_mutex); |
8703a9a4 | 5480 | #endif |
53d44271 JH |
5481 | } |
5482 | ||
5483 | /* make sure the array is big enough */ | |
5484 | if (PL_my_cxt_size <= index) { | |
5485 | int old_size = PL_my_cxt_size; | |
5486 | int i; | |
5487 | if (PL_my_cxt_size) { | |
5488 | while (PL_my_cxt_size <= index) | |
5489 | PL_my_cxt_size *= 2; | |
5490 | Renew(PL_my_cxt_list, PL_my_cxt_size, void *); | |
5491 | Renew(PL_my_cxt_keys, PL_my_cxt_size, const char *); | |
5492 | } | |
5493 | else { | |
5494 | PL_my_cxt_size = 16; | |
5495 | Newx(PL_my_cxt_list, PL_my_cxt_size, void *); | |
5496 | Newx(PL_my_cxt_keys, PL_my_cxt_size, const char *); | |
5497 | } | |
5498 | for (i = old_size; i < PL_my_cxt_size; i++) { | |
5499 | PL_my_cxt_keys[i] = 0; | |
5500 | PL_my_cxt_list[i] = 0; | |
5501 | } | |
5502 | } | |
5503 | PL_my_cxt_keys[index] = my_cxt_key; | |
5504 | /* newSV() allocates one more than needed */ | |
5505 | p = (void*)SvPVX(newSV(size-1)); | |
5506 | PL_my_cxt_list[index] = p; | |
5507 | Zero(p, size, char); | |
5508 | return p; | |
5509 | } | |
5510 | #endif /* #ifndef PERL_GLOBAL_STRUCT_PRIVATE */ | |
5511 | #endif /* PERL_IMPLICIT_CONTEXT */ | |
f16dd614 | 5512 | |
db6e00bd | 5513 | |
5ec05c96 DM |
5514 | /* Perl_xs_handshake(): |
5515 | implement the various XS_*_BOOTCHECK macros, which are added to .c | |
5516 | files by ExtUtils::ParseXS, to check that the perl the module was built | |
5517 | with is binary compatible with the running perl. | |
5518 | ||
5519 | usage: | |
5520 | Perl_xs_handshake(U32 key, void * v_my_perl, const char * file, | |
5521 | [U32 items, U32 ax], [char * api_version], [char * xs_version]) | |
5522 | ||
5523 | The meaning of the varargs is determined the U32 key arg (which is not | |
5524 | a format string). The fields of key are assembled by using HS_KEY(). | |
5525 | ||
5526 | Under PERL_IMPLICIT_CONTEX, the v_my_perl arg is of type | |
5527 | "PerlInterpreter *" and represents the callers context; otherwise it is | |
5528 | of type "CV *", and is the boot xsub's CV. | |
5529 | ||
5530 | v_my_perl will catch where a threaded future perl526.dll calling IO.dll | |
5531 | for example, and IO.dll was linked with threaded perl524.dll, and both | |
5532 | perl526.dll and perl524.dll are in %PATH and the Win32 DLL loader | |
5533 | successfully can load IO.dll into the process but simultaneously it | |
5534 | loaded an interpreter of a different version into the process, and XS | |
5535 | code will naturally pass SV*s created by perl524.dll for perl526.dll to | |
5536 | use through perl526.dll's my_perl->Istack_base. | |
5537 | ||
5538 | v_my_perl cannot be the first arg, since then 'key' will be out of | |
5539 | place in a threaded vs non-threaded mixup; and analyzing the key | |
5540 | number's bitfields won't reveal the problem, since it will be a valid | |
5541 | key (unthreaded perl) on interp side, but croak will report the XS mod's | |
5542 | key as gibberish (it is really a my_perl ptr) (threaded XS mod); or if | |
5543 | it's a threaded perl and an unthreaded XS module, threaded perl will | |
5544 | look at an uninit C stack or an uninit register to get 'key' | |
5545 | (remember that it assumes that the 1st arg is the interp cxt). | |
5546 | ||
5547 | 'file' is the source filename of the caller. | |
5548 | */ | |
5549 | ||
db6e00bd | 5550 | I32 |
9a189793 | 5551 | Perl_xs_handshake(const U32 key, void * v_my_perl, const char * file, ...) |
db6e00bd DD |
5552 | { |
5553 | va_list args; | |
5554 | U32 items, ax; | |
9a189793 DD |
5555 | void * got; |
5556 | void * need; | |
db6e00bd DD |
5557 | #ifdef PERL_IMPLICIT_CONTEXT |
5558 | dTHX; | |
9a189793 DD |
5559 | tTHX xs_interp; |
5560 | #else | |
5561 | CV* cv; | |
5562 | SV *** xs_spp; | |
db6e00bd DD |
5563 | #endif |
5564 | PERL_ARGS_ASSERT_XS_HANDSHAKE; | |
9a189793 | 5565 | va_start(args, file); |
db6e00bd | 5566 | |
3ef6b8e9 | 5567 | got = INT2PTR(void*, (UV)(key & HSm_KEY_MATCH)); |
9a189793 | 5568 | need = (void *)(HS_KEY(FALSE, FALSE, "", "") & HSm_KEY_MATCH); |
3ef6b8e9 | 5569 | if (UNLIKELY(got != need)) |
9a189793 | 5570 | goto bad_handshake; |
db6e00bd DD |
5571 | /* try to catch where a 2nd threaded perl interp DLL is loaded into a process |
5572 | by a XS DLL compiled against the wrong interl DLL b/c of bad @INC, and the | |
5573 | 2nd threaded perl interp DLL never initialized its TLS/PERL_SYS_INIT3 so | |
5574 | dTHX call from 2nd interp DLL can't return the my_perl that pp_entersub | |
5575 | passed to the XS DLL */ | |
db6e00bd | 5576 | #ifdef PERL_IMPLICIT_CONTEXT |
9a189793 DD |
5577 | xs_interp = (tTHX)v_my_perl; |
5578 | got = xs_interp; | |
5579 | need = my_perl; | |
db6e00bd DD |
5580 | #else |
5581 | /* try to catch where an unthreaded perl interp DLL (for ex. perl522.dll) is | |
5582 | loaded into a process by a XS DLL built by an unthreaded perl522.dll perl, | |
5583 | but the DynaLoder/Perl that started the process and loaded the XS DLL is | |
5584 | unthreaded perl524.dll, since unthreadeds don't pass my_perl (a unique *) | |
5585 | through pp_entersub, use a unique value (which is a pointer to PL_stack_sp's | |
5586 | location in the unthreaded perl binary) stored in CV * to figure out if this | |
5587 | Perl_xs_handshake was called by the same pp_entersub */ | |
9a189793 DD |
5588 | cv = (CV*)v_my_perl; |
5589 | xs_spp = (SV***)CvHSCXT(cv); | |
5590 | got = xs_spp; | |
5591 | need = &PL_stack_sp; | |
5592 | #endif | |
5593 | if(UNLIKELY(got != need)) { | |
5594 | bad_handshake:/* recycle branch and string from above */ | |
5595 | if(got != (void *)HSf_NOCHK) | |
fe60b4f6 DM |
5596 | noperl_die("%s: loadable library and perl binaries are mismatched" |
5597 | " (got handshake key %p, needed %p)\n", | |
9a189793 DD |
5598 | file, got, need); |
5599 | } | |
5600 | ||
5601 | if(key & HSf_SETXSUBFN) { /* this might be called from a module bootstrap */ | |
5602 | SAVEPPTR(PL_xsubfilename);/* which was require'd from a XSUB BEGIN */ | |
5603 | PL_xsubfilename = file; /* so the old name must be restored for | |
5604 | additional XSUBs to register themselves */ | |
9b669ea1 DD |
5605 | /* XSUBs can't be perl lang/perl5db.pl debugged |
5606 | if (PERLDB_LINE_OR_SAVESRC) | |
5607 | (void)gv_fetchfile(file); */ | |
db6e00bd DD |
5608 | } |
5609 | ||
5610 | if(key & HSf_POPMARK) { | |
5611 | ax = POPMARK; | |
5612 | { SV **mark = PL_stack_base + ax++; | |
5613 | { dSP; | |
5614 | items = (I32)(SP - MARK); | |
5615 | } | |
5616 | } | |
5617 | } else { | |
5618 | items = va_arg(args, U32); | |
5619 | ax = va_arg(args, U32); | |
5620 | } | |
5621 | { | |
5622 | U32 apiverlen; | |
5623 | assert(HS_GETAPIVERLEN(key) <= UCHAR_MAX); | |
ea91b243 | 5624 | if((apiverlen = HS_GETAPIVERLEN(key))) { |
db6e00bd DD |
5625 | char * api_p = va_arg(args, char*); |
5626 | if(apiverlen != sizeof("v" PERL_API_VERSION_STRING)-1 | |
5627 | || memNE(api_p, "v" PERL_API_VERSION_STRING, | |
5628 | sizeof("v" PERL_API_VERSION_STRING)-1)) | |
5629 | Perl_croak_nocontext("Perl API version %s of %"SVf" does not match %s", | |
5630 | api_p, SVfARG(PL_stack_base[ax + 0]), | |
5631 | "v" PERL_API_VERSION_STRING); | |
5632 | } | |
5633 | } | |
5634 | { | |
5635 | U32 xsverlen; | |
5636 | assert(HS_GETXSVERLEN(key) <= UCHAR_MAX && HS_GETXSVERLEN(key) <= HS_APIVERLEN_MAX); | |
ea91b243 | 5637 | if((xsverlen = HS_GETXSVERLEN(key))) |
672cbd15 | 5638 | S_xs_version_bootcheck(aTHX_ |
db6e00bd DD |
5639 | items, ax, va_arg(args, char*), xsverlen); |
5640 | } | |
5641 | va_end(args); | |
5642 | return ax; | |
5643 | } | |
5644 | ||
5ec05c96 | 5645 | |
672cbd15 DD |
5646 | STATIC void |
5647 | S_xs_version_bootcheck(pTHX_ U32 items, U32 ax, const char *xs_p, | |
e9b067d9 NC |
5648 | STRLEN xs_len) |
5649 | { | |
5650 | SV *sv; | |
5651 | const char *vn = NULL; | |
a2f871a2 | 5652 | SV *const module = PL_stack_base[ax]; |
e9b067d9 NC |
5653 | |
5654 | PERL_ARGS_ASSERT_XS_VERSION_BOOTCHECK; | |
5655 | ||
5656 | if (items >= 2) /* version supplied as bootstrap arg */ | |
5657 | sv = PL_stack_base[ax + 1]; | |
5658 | else { | |
5659 | /* XXX GV_ADDWARN */ | |
a2f871a2 | 5660 | vn = "XS_VERSION"; |
c1f6cd39 | 5661 | sv = get_sv(Perl_form(aTHX_ "%"SVf"::%s", SVfARG(module), vn), 0); |
a2f871a2 NC |
5662 | if (!sv || !SvOK(sv)) { |
5663 | vn = "VERSION"; | |
c1f6cd39 | 5664 | sv = get_sv(Perl_form(aTHX_ "%"SVf"::%s", SVfARG(module), vn), 0); |
a2f871a2 | 5665 | } |
e9b067d9 NC |
5666 | } |
5667 | if (sv) { | |
f9cc56fa | 5668 | SV *xssv = Perl_newSVpvn_flags(aTHX_ xs_p, xs_len, SVs_TEMP); |
573a19fb | 5669 | SV *pmsv = sv_isobject(sv) && sv_derived_from(sv, "version") |
f9cc56fa | 5670 | ? sv : sv_2mortal(new_version(sv)); |
e9b067d9 NC |
5671 | xssv = upg_version(xssv, 0); |
5672 | if ( vcmp(pmsv,xssv) ) { | |
a2f871a2 NC |
5673 | SV *string = vstringify(xssv); |
5674 | SV *xpt = Perl_newSVpvf(aTHX_ "%"SVf" object version %"SVf | |
c1f6cd39 | 5675 | " does not match ", SVfARG(module), SVfARG(string)); |
a2f871a2 NC |
5676 | |
5677 | SvREFCNT_dec(string); | |
5678 | string = vstringify(pmsv); | |
5679 | ||
5680 | if (vn) { | |
c1f6cd39 BF |
5681 | Perl_sv_catpvf(aTHX_ xpt, "$%"SVf"::%s %"SVf, SVfARG(module), vn, |
5682 | SVfARG(string)); | |
a2f871a2 | 5683 | } else { |
c1f6cd39 | 5684 | Perl_sv_catpvf(aTHX_ xpt, "bootstrap parameter %"SVf, SVfARG(string)); |
a2f871a2 NC |
5685 | } |
5686 | SvREFCNT_dec(string); | |
5687 | ||
e9b067d9 | 5688 | Perl_sv_2mortal(aTHX_ xpt); |
e9b067d9 | 5689 | Perl_croak_sv(aTHX_ xpt); |
f9cc56fa | 5690 | } |
e9b067d9 NC |
5691 | } |
5692 | } | |
5693 | ||
f46a3253 KW |
5694 | /* |
5695 | =for apidoc my_strlcat | |
5696 | ||
5697 | The C library C<strlcat> if available, or a Perl implementation of it. | |
6602b933 | 5698 | This operates on C C<NUL>-terminated strings. |
f46a3253 KW |
5699 | |
5700 | C<my_strlcat()> appends string C<src> to the end of C<dst>. It will append at | |
6602b933 | 5701 | most S<C<size - strlen(dst) - 1>> characters. It will then C<NUL>-terminate, |
f46a3253 KW |
5702 | unless C<size> is 0 or the original C<dst> string was longer than C<size> (in |
5703 | practice this should not happen as it means that either C<size> is incorrect or | |
6602b933 | 5704 | that C<dst> is not a proper C<NUL>-terminated string). |
f46a3253 KW |
5705 | |
5706 | Note that C<size> is the full size of the destination buffer and | |
6602b933 KW |
5707 | the result is guaranteed to be C<NUL>-terminated if there is room. Note that |
5708 | room for the C<NUL> should be included in C<size>. | |
f46a3253 KW |
5709 | |
5710 | =cut | |
5711 | ||
5712 | Description stolen from http://www.openbsd.org/cgi-bin/man.cgi?query=strlcat | |
5713 | */ | |
a6cc4119 SP |
5714 | #ifndef HAS_STRLCAT |
5715 | Size_t | |
5716 | Perl_my_strlcat(char *dst, const char *src, Size_t size) | |
5717 | { | |
5718 | Size_t used, length, copy; | |
5719 | ||
5720 | used = strlen(dst); | |
5721 | length = strlen(src); | |
5722 | if (size > 0 && used < size - 1) { | |
5723 | copy = (length >= size - used) ? size - used - 1 : length; | |
5724 | memcpy(dst + used, src, copy); | |
5725 | dst[used + copy] = '\0'; | |
5726 | } | |
5727 | return used + length; | |
5728 | } | |
5729 | #endif | |
5730 | ||
f46a3253 KW |
5731 | |
5732 | /* | |
5733 | =for apidoc my_strlcpy | |
5734 | ||
5735 | The C library C<strlcpy> if available, or a Perl implementation of it. | |
6602b933 | 5736 | This operates on C C<NUL>-terminated strings. |
f46a3253 KW |
5737 | |
5738 | C<my_strlcpy()> copies up to S<C<size - 1>> characters from the string C<src> | |
6602b933 | 5739 | to C<dst>, C<NUL>-terminating the result if C<size> is not 0. |
f46a3253 KW |
5740 | |
5741 | =cut | |
5742 | ||
5743 | Description stolen from http://www.openbsd.org/cgi-bin/man.cgi?query=strlcpy | |
5744 | */ | |
a6cc4119 SP |
5745 | #ifndef HAS_STRLCPY |
5746 | Size_t | |
5747 | Perl_my_strlcpy(char *dst, const char *src, Size_t size) | |
5748 | { | |
5749 | Size_t length, copy; | |
5750 | ||
5751 | length = strlen(src); | |
5752 | if (size > 0) { | |
5753 | copy = (length >= size) ? size - 1 : length; | |
5754 | memcpy(dst, src, copy); | |
5755 | dst[copy] = '\0'; | |
5756 | } | |
5757 | return length; | |
5758 | } | |
5759 | #endif | |
5760 | ||
17dd9954 JH |
5761 | #if defined(_MSC_VER) && (_MSC_VER >= 1300) && (_MSC_VER < 1400) && (WINVER < 0x0500) |
5762 | /* VC7 or 7.1, building with pre-VC7 runtime libraries. */ | |
5763 | long _ftol( double ); /* Defined by VC6 C libs. */ | |
5764 | long _ftol2( double dblSource ) { return _ftol( dblSource ); } | |
5765 | #endif | |
5766 | ||
a7999c08 FC |
5767 | PERL_STATIC_INLINE bool |
5768 | S_gv_has_usable_name(pTHX_ GV *gv) | |
5769 | { | |
5770 | GV **gvp; | |
5771 | return GvSTASH(gv) | |
5772 | && HvENAME(GvSTASH(gv)) | |
edf4dbd2 FC |
5773 | && (gvp = (GV **)hv_fetchhek( |
5774 | GvSTASH(gv), GvNAME_HEK(gv), 0 | |
a7999c08 FC |
5775 | )) |
5776 | && *gvp == gv; | |
5777 | } | |
5778 | ||
c51f309c NC |
5779 | void |
5780 | Perl_get_db_sub(pTHX_ SV **svp, CV *cv) | |
5781 | { | |
c51f309c | 5782 | SV * const dbsv = GvSVn(PL_DBsub); |
9a9b5ec9 | 5783 | const bool save_taint = TAINT_get; |
07004ebb | 5784 | |
107c452c FC |
5785 | /* When we are called from pp_goto (svp is null), |
5786 | * we do not care about using dbsv to call CV; | |
c51f309c NC |
5787 | * it's for informational purposes only. |
5788 | */ | |
5789 | ||
7918f24d NC |
5790 | PERL_ARGS_ASSERT_GET_DB_SUB; |
5791 | ||
284167a5 | 5792 | TAINT_set(FALSE); |
c51f309c NC |
5793 | save_item(dbsv); |
5794 | if (!PERLDB_SUB_NN) { | |
be1cc451 | 5795 | GV *gv = CvGV(cv); |
c51f309c | 5796 | |
e0a18850 | 5797 | if (!svp && !CvLEXICAL(cv)) { |
7d8b4ed3 FC |
5798 | gv_efullname3(dbsv, gv, NULL); |
5799 | } | |
e0a18850 | 5800 | else if ( (CvFLAGS(cv) & (CVf_ANON | CVf_CLONED)) || CvLEXICAL(cv) |
c51f309c | 5801 | || strEQ(GvNAME(gv), "END") |
a7999c08 FC |
5802 | || ( /* Could be imported, and old sub redefined. */ |
5803 | (GvCV(gv) != cv || !S_gv_has_usable_name(aTHX_ gv)) | |
5804 | && | |
159b6efe | 5805 | !( (SvTYPE(*svp) == SVt_PVGV) |
be1cc451 | 5806 | && (GvCV((const GV *)*svp) == cv) |
a7999c08 | 5807 | /* Use GV from the stack as a fallback. */ |
4aaab439 | 5808 | && S_gv_has_usable_name(aTHX_ gv = (GV *)*svp) |
be1cc451 FC |
5809 | ) |
5810 | ) | |
7d8b4ed3 | 5811 | ) { |
c51f309c | 5812 | /* GV is potentially non-unique, or contain different CV. */ |
daba3364 | 5813 | SV * const tmp = newRV(MUTABLE_SV(cv)); |
c51f309c NC |
5814 | sv_setsv(dbsv, tmp); |
5815 | SvREFCNT_dec(tmp); | |
5816 | } | |
5817 | else { | |
a7999c08 FC |
5818 | sv_sethek(dbsv, HvENAME_HEK(GvSTASH(gv))); |
5819 | sv_catpvs(dbsv, "::"); | |
f34d8cdd | 5820 | sv_cathek(dbsv, GvNAME_HEK(gv)); |
c51f309c NC |
5821 | } |
5822 | } | |
5823 | else { | |
5824 | const int type = SvTYPE(dbsv); | |
5825 | if (type < SVt_PVIV && type != SVt_IV) | |
5826 | sv_upgrade(dbsv, SVt_PVIV); | |
5827 | (void)SvIOK_on(dbsv); | |
5828 | SvIV_set(dbsv, PTR2IV(cv)); /* Do it the quickest way */ | |
5829 | } | |
90a04aed | 5830 | SvSETMAGIC(dbsv); |
07004ebb | 5831 | TAINT_IF(save_taint); |
9a9b5ec9 DM |
5832 | #ifdef NO_TAINT_SUPPORT |
5833 | PERL_UNUSED_VAR(save_taint); | |
5834 | #endif | |
c51f309c NC |
5835 | } |
5836 | ||
3497a01f | 5837 | int |
ddeaf645 | 5838 | Perl_my_dirfd(DIR * dir) { |
3497a01f SP |
5839 | |
5840 | /* Most dirfd implementations have problems when passed NULL. */ | |
5841 | if(!dir) | |
5842 | return -1; | |
5843 | #ifdef HAS_DIRFD | |
5844 | return dirfd(dir); | |
5845 | #elif defined(HAS_DIR_DD_FD) | |
5846 | return dir->dd_fd; | |
5847 | #else | |
ddeaf645 | 5848 | Perl_croak_nocontext(PL_no_func, "dirfd"); |
661d43c4 | 5849 | NOT_REACHED; /* NOTREACHED */ |
3497a01f SP |
5850 | return 0; |
5851 | #endif | |
5852 | } | |
5853 | ||
f7e71195 AB |
5854 | REGEXP * |
5855 | Perl_get_re_arg(pTHX_ SV *sv) { | |
f7e71195 AB |
5856 | |
5857 | if (sv) { | |
5858 | if (SvMAGICAL(sv)) | |
5859 | mg_get(sv); | |
df052ff8 BM |
5860 | if (SvROK(sv)) |
5861 | sv = MUTABLE_SV(SvRV(sv)); | |
5862 | if (SvTYPE(sv) == SVt_REGEXP) | |
5863 | return (REGEXP*) sv; | |
f7e71195 AB |
5864 | } |
5865 | ||
5866 | return NULL; | |
5867 | } | |
5868 | ||
ce582cee | 5869 | /* |
3be8f094 TC |
5870 | * This code is derived from drand48() implementation from FreeBSD, |
5871 | * found in lib/libc/gen/_rand48.c. | |
5872 | * | |
5873 | * The U64 implementation is original, based on the POSIX | |
5874 | * specification for drand48(). | |
5875 | */ | |
5876 | ||
5877 | /* | |
5878 | * Copyright (c) 1993 Martin Birgmeier | |
5879 | * All rights reserved. | |
5880 | * | |
5881 | * You may redistribute unmodified or modified versions of this source | |
5882 | * code provided that the above copyright notice and this and the | |
5883 | * following conditions are retained. | |
5884 | * | |
5885 | * This software is provided ``as is'', and comes with no warranties | |
5886 | * of any kind. I shall in no event be liable for anything that happens | |
5887 | * to anyone/anything when using this software. | |
5888 | */ | |
5889 | ||
5890 | #define FREEBSD_DRAND48_SEED_0 (0x330e) | |
5891 | ||
5892 | #ifdef PERL_DRAND48_QUAD | |
5893 | ||
7ace1b59 | 5894 | #define DRAND48_MULT U64_CONST(0x5deece66d) |
3be8f094 | 5895 | #define DRAND48_ADD 0xb |
7ace1b59 | 5896 | #define DRAND48_MASK U64_CONST(0xffffffffffff) |
3be8f094 TC |
5897 | |
5898 | #else | |
5899 | ||
5900 | #define FREEBSD_DRAND48_SEED_1 (0xabcd) | |
5901 | #define FREEBSD_DRAND48_SEED_2 (0x1234) | |
5902 | #define FREEBSD_DRAND48_MULT_0 (0xe66d) | |
5903 | #define FREEBSD_DRAND48_MULT_1 (0xdeec) | |
5904 | #define FREEBSD_DRAND48_MULT_2 (0x0005) | |
5905 | #define FREEBSD_DRAND48_ADD (0x000b) | |
5906 | ||
5907 | const unsigned short _rand48_mult[3] = { | |
5908 | FREEBSD_DRAND48_MULT_0, | |
5909 | FREEBSD_DRAND48_MULT_1, | |
5910 | FREEBSD_DRAND48_MULT_2 | |
5911 | }; | |
5912 | const unsigned short _rand48_add = FREEBSD_DRAND48_ADD; | |
5913 | ||
5914 | #endif | |
5915 | ||
5916 | void | |
5917 | Perl_drand48_init_r(perl_drand48_t *random_state, U32 seed) | |
5918 | { | |
5919 | PERL_ARGS_ASSERT_DRAND48_INIT_R; | |
5920 | ||
5921 | #ifdef PERL_DRAND48_QUAD | |
702c92eb | 5922 | *random_state = FREEBSD_DRAND48_SEED_0 + ((U64)seed << 16); |
3be8f094 TC |
5923 | #else |
5924 | random_state->seed[0] = FREEBSD_DRAND48_SEED_0; | |
5925 | random_state->seed[1] = (U16) seed; | |
5926 | random_state->seed[2] = (U16) (seed >> 16); | |
5927 | #endif | |
5928 | } | |
5929 | ||
5930 | double | |
5931 | Perl_drand48_r(perl_drand48_t *random_state) | |
5932 | { | |
5933 | PERL_ARGS_ASSERT_DRAND48_R; | |
5934 | ||
5935 | #ifdef PERL_DRAND48_QUAD | |
5936 | *random_state = (*random_state * DRAND48_MULT + DRAND48_ADD) | |
5937 | & DRAND48_MASK; | |
5938 | ||
0f246720 | 5939 | return ldexp((double)*random_state, -48); |
3be8f094 | 5940 | #else |
63835f79 | 5941 | { |
3be8f094 TC |
5942 | U32 accu; |
5943 | U16 temp[2]; | |
5944 | ||
5945 | accu = (U32) _rand48_mult[0] * (U32) random_state->seed[0] | |
5946 | + (U32) _rand48_add; | |
5947 | temp[0] = (U16) accu; /* lower 16 bits */ | |
5948 | accu >>= sizeof(U16) * 8; | |
5949 | accu += (U32) _rand48_mult[0] * (U32) random_state->seed[1] | |
5950 | + (U32) _rand48_mult[1] * (U32) random_state->seed[0]; | |
5951 | temp[1] = (U16) accu; /* middle 16 bits */ | |
5952 | accu >>= sizeof(U16) * 8; | |
5953 | accu += _rand48_mult[0] * random_state->seed[2] | |
5954 | + _rand48_mult[1] * random_state->seed[1] | |
5955 | + _rand48_mult[2] * random_state->seed[0]; | |
5956 | random_state->seed[0] = temp[0]; | |
5957 | random_state->seed[1] = temp[1]; | |
5958 | random_state->seed[2] = (U16) accu; | |
5959 | ||
5960 | return ldexp((double) random_state->seed[0], -48) + | |
5961 | ldexp((double) random_state->seed[1], -32) + | |
5962 | ldexp((double) random_state->seed[2], -16); | |
63835f79 | 5963 | } |
3be8f094 TC |
5964 | #endif |
5965 | } | |
2c6ee1a7 | 5966 | |
470dd224 JH |
5967 | #ifdef USE_C_BACKTRACE |
5968 | ||
5969 | /* Possibly move all this USE_C_BACKTRACE code into a new file. */ | |
5970 | ||
5971 | #ifdef USE_BFD | |
5972 | ||
5973 | typedef struct { | |
0762e42f | 5974 | /* abfd is the BFD handle. */ |
470dd224 | 5975 | bfd* abfd; |
0762e42f | 5976 | /* bfd_syms is the BFD symbol table. */ |
470dd224 | 5977 | asymbol** bfd_syms; |
0762e42f | 5978 | /* bfd_text is handle to the the ".text" section of the object file. */ |
470dd224 JH |
5979 | asection* bfd_text; |
5980 | /* Since opening the executable and scanning its symbols is quite | |
5981 | * heavy operation, we remember the filename we used the last time, | |
5982 | * and do the opening and scanning only if the filename changes. | |
5983 | * This removes most (but not all) open+scan cycles. */ | |
5984 | const char* fname_prev; | |
5985 | } bfd_context; | |
5986 | ||
5987 | /* Given a dl_info, update the BFD context if necessary. */ | |
5988 | static void bfd_update(bfd_context* ctx, Dl_info* dl_info) | |
5989 | { | |
5990 | /* BFD open and scan only if the filename changed. */ | |
5991 | if (ctx->fname_prev == NULL || | |
5992 | strNE(dl_info->dli_fname, ctx->fname_prev)) { | |
a1684041 JH |
5993 | if (ctx->abfd) { |
5994 | bfd_close(ctx->abfd); | |
5995 | } | |
470dd224 JH |
5996 | ctx->abfd = bfd_openr(dl_info->dli_fname, 0); |
5997 | if (ctx->abfd) { | |
5998 | if (bfd_check_format(ctx->abfd, bfd_object)) { | |
5999 | IV symbol_size = bfd_get_symtab_upper_bound(ctx->abfd); | |
6000 | if (symbol_size > 0) { | |
6001 | Safefree(ctx->bfd_syms); | |
6002 | Newx(ctx->bfd_syms, symbol_size, asymbol*); | |
6003 | ctx->bfd_text = | |
6004 | bfd_get_section_by_name(ctx->abfd, ".text"); | |
6005 | } | |
6006 | else | |
6007 | ctx->abfd = NULL; | |
6008 | } | |
6009 | else | |
6010 | ctx->abfd = NULL; | |
6011 | } | |
6012 | ctx->fname_prev = dl_info->dli_fname; | |
6013 | } | |
6014 | } | |
6015 | ||
6016 | /* Given a raw frame, try to symbolize it and store | |
6017 | * symbol information (source file, line number) away. */ | |
6018 | static void bfd_symbolize(bfd_context* ctx, | |
6019 | void* raw_frame, | |
6020 | char** symbol_name, | |
6021 | STRLEN* symbol_name_size, | |
6022 | char** source_name, | |
6023 | STRLEN* source_name_size, | |
6024 | STRLEN* source_line) | |
6025 | { | |
6026 | *symbol_name = NULL; | |
6027 | *symbol_name_size = 0; | |
6028 | if (ctx->abfd) { | |
6029 | IV offset = PTR2IV(raw_frame) - PTR2IV(ctx->bfd_text->vma); | |
6030 | if (offset > 0 && | |
6031 | bfd_canonicalize_symtab(ctx->abfd, ctx->bfd_syms) > 0) { | |
6032 | const char *file; | |
6033 | const char *func; | |
6034 | unsigned int line = 0; | |
6035 | if (bfd_find_nearest_line(ctx->abfd, ctx->bfd_text, | |
6036 | ctx->bfd_syms, offset, | |
6037 | &file, &func, &line) && | |
6038 | file && func && line > 0) { | |
6039 | /* Size and copy the source file, use only | |
6040 | * the basename of the source file. | |
6041 | * | |
6042 | * NOTE: the basenames are fine for the | |
6043 | * Perl source files, but may not always | |
6044 | * be the best idea for XS files. */ | |
6045 | const char *p, *b = NULL; | |
6046 | /* Look for the last slash. */ | |
6047 | for (p = file; *p; p++) { | |
6048 | if (*p == '/') | |
6049 | b = p + 1; | |
6050 | } | |
6051 | if (b == NULL || *b == 0) { | |
6052 | b = file; | |
6053 | } | |
6054 | *source_name_size = p - b + 1; | |
6055 | Newx(*source_name, *source_name_size + 1, char); | |
6056 | Copy(b, *source_name, *source_name_size + 1, char); | |
6057 | ||
6058 | *symbol_name_size = strlen(func); | |
6059 | Newx(*symbol_name, *symbol_name_size + 1, char); | |
6060 | Copy(func, *symbol_name, *symbol_name_size + 1, char); | |
6061 | ||
6062 | *source_line = line; | |
6063 | } | |
6064 | } | |
6065 | } | |
6066 | } | |
6067 | ||
6068 | #endif /* #ifdef USE_BFD */ | |
6069 | ||
6070 | #ifdef PERL_DARWIN | |
6071 | ||
6072 | /* OS X has no public API for for 'symbolicating' (Apple official term) | |
6073 | * stack addresses to {function_name, source_file, line_number}. | |
6074 | * Good news: there is command line utility atos(1) which does that. | |
6075 | * Bad news 1: it's a command line utility. | |
6076 | * Bad news 2: one needs to have the Developer Tools installed. | |
6077 | * Bad news 3: in newer releases it needs to be run as 'xcrun atos'. | |
6078 | * | |
6079 | * To recap: we need to open a pipe for reading for a utility which | |
6080 | * might not exist, or exists in different locations, and then parse | |
6081 | * the output. And since this is all for a low-level API, we cannot | |
6082 | * use high-level stuff. Thanks, Apple. */ | |
6083 | ||
6084 | typedef struct { | |
0762e42f JH |
6085 | /* tool is set to the absolute pathname of the tool to use: |
6086 | * xcrun or atos. */ | |
470dd224 | 6087 | const char* tool; |
0762e42f JH |
6088 | /* format is set to a printf format string used for building |
6089 | * the external command to run. */ | |
470dd224 | 6090 | const char* format; |
0762e42f JH |
6091 | /* unavail is set if e.g. xcrun cannot be found, or something |
6092 | * else happens that makes getting the backtrace dubious. Note, | |
6093 | * however, that the context isn't persistent, the next call to | |
6094 | * get_c_backtrace() will start from scratch. */ | |
470dd224 | 6095 | bool unavail; |
0762e42f | 6096 | /* fname is the current object file name. */ |
470dd224 | 6097 | const char* fname; |
0762e42f | 6098 | /* object_base_addr is the base address of the shared object. */ |
470dd224 JH |
6099 | void* object_base_addr; |
6100 | } atos_context; | |
6101 | ||
6102 | /* Given |dl_info|, updates the context. If the context has been | |
6103 | * marked unavailable, return immediately. If not but the tool has | |
6104 | * not been set, set it to either "xcrun atos" or "atos" (also set the | |
6105 | * format to use for creating commands for piping), or if neither is | |
6106 | * unavailable (one needs the Developer Tools installed), mark the context | |
6107 | * an unavailable. Finally, update the filename (object name), | |
6108 | * and its base address. */ | |
6109 | ||
6110 | static void atos_update(atos_context* ctx, | |
6111 | Dl_info* dl_info) | |
6112 | { | |
6113 | if (ctx->unavail) | |
6114 | return; | |
6115 | if (ctx->tool == NULL) { | |
6116 | const char* tools[] = { | |
6117 | "/usr/bin/xcrun", | |
6118 | "/usr/bin/atos" | |
6119 | }; | |
6120 | const char* formats[] = { | |
6121 | "/usr/bin/xcrun atos -o '%s' -l %08x %08x 2>&1", | |
6122 | "/usr/bin/atos -d -o '%s' -l %08x %08x 2>&1" | |
6123 | }; | |
6124 | struct stat st; | |
6125 | UV i; | |
6126 | for (i = 0; i < C_ARRAY_LENGTH(tools); i++) { | |
6127 | if (stat(tools[i], &st) == 0 && S_ISREG(st.st_mode)) { | |
6128 | ctx->tool = tools[i]; | |
6129 | ctx->format = formats[i]; | |
6130 | break; | |
6131 | } | |
6132 | } | |
6133 | if (ctx->tool == NULL) { | |
6134 | ctx->unavail = TRUE; | |
6135 | return; | |
6136 | } | |
6137 | } | |
6138 | if (ctx->fname == NULL || | |
6139 | strNE(dl_info->dli_fname, ctx->fname)) { | |
6140 | ctx->fname = dl_info->dli_fname; | |
6141 | ctx->object_base_addr = dl_info->dli_fbase; | |
6142 | } | |
6143 | } | |
6144 | ||
6145 | /* Given an output buffer end |p| and its |start|, matches | |
6146 | * for the atos output, extracting the source code location | |
96e440d2 | 6147 | * and returning non-NULL if possible, returning NULL otherwise. */ |
470dd224 JH |
6148 | static const char* atos_parse(const char* p, |
6149 | const char* start, | |
6150 | STRLEN* source_name_size, | |
6151 | STRLEN* source_line) { | |
96e440d2 | 6152 | /* atos() output is something like: |
470dd224 JH |
6153 | * perl_parse (in miniperl) (perl.c:2314)\n\n". |
6154 | * We cannot use Perl regular expressions, because we need to | |
6155 | * stay low-level. Therefore here we have a rolled-out version | |
6156 | * of a state machine which matches _backwards_from_the_end_ and | |
6157 | * if there's a success, returns the starts of the filename, | |
6158 | * also setting the filename size and the source line number. | |
6159 | * The matched regular expression is roughly "\(.*:\d+\)\s*$" */ | |
6160 | const char* source_number_start; | |
6161 | const char* source_name_end; | |
96e440d2 JH |
6162 | const char* source_line_end; |
6163 | const char* close_paren; | |
22ff3130 HS |
6164 | UV uv; |
6165 | ||
470dd224 JH |
6166 | /* Skip trailing whitespace. */ |
6167 | while (p > start && isspace(*p)) p--; | |
6168 | /* Now we should be at the close paren. */ | |
6169 | if (p == start || *p != ')') | |
6170 | return NULL; | |
96e440d2 | 6171 | close_paren = p; |
470dd224 JH |
6172 | p--; |
6173 | /* Now we should be in the line number. */ | |
6174 | if (p == start || !isdigit(*p)) | |
6175 | return NULL; | |
6176 | /* Skip over the digits. */ | |
6177 | while (p > start && isdigit(*p)) | |
6178 | p--; | |
6179 | /* Now we should be at the colon. */ | |
6180 | if (p == start || *p != ':') | |
6181 | return NULL; | |
6182 | source_number_start = p + 1; | |
6183 | source_name_end = p; /* Just beyond the end. */ | |
6184 | p--; | |
6185 | /* Look for the open paren. */ | |
6186 | while (p > start && *p != '(') | |
6187 | p--; | |
6188 | if (p == start) | |
6189 | return NULL; | |
6190 | p++; | |
6191 | *source_name_size = source_name_end - p; | |
22ff3130 HS |
6192 | if (grok_atoUV(source_number_start, &uv, &source_line_end) |
6193 | && source_line_end == close_paren | |
99315af8 | 6194 | && uv <= PERL_INT_MAX |
22ff3130 HS |
6195 | ) { |
6196 | *source_line = (STRLEN)uv; | |
6197 | return p; | |
6198 | } | |
6199 | return NULL; | |
470dd224 JH |
6200 | } |
6201 | ||
6202 | /* Given a raw frame, read a pipe from the symbolicator (that's the | |
6203 | * technical term) atos, reads the result, and parses the source code | |
6204 | * location. We must stay low-level, so we use snprintf(), pipe(), | |
6205 | * and fread(), and then also parse the output ourselves. */ | |
6206 | static void atos_symbolize(atos_context* ctx, | |
6207 | void* raw_frame, | |
6208 | char** source_name, | |
6209 | STRLEN* source_name_size, | |
6210 | STRLEN* source_line) | |
6211 | { | |
6212 | char cmd[1024]; | |
6213 | const char* p; | |
6214 | Size_t cnt; | |
6215 | ||
6216 | if (ctx->unavail) | |
6217 | return; | |
6218 | /* Simple security measure: if there's any funny business with | |
6219 | * the object name (used as "-o '%s'" ), leave since at least | |
6220 | * partially the user controls it. */ | |
6221 | for (p = ctx->fname; *p; p++) { | |
6222 | if (*p == '\'' || iscntrl(*p)) { | |
6223 | ctx->unavail = TRUE; | |
6224 | return; | |
6225 | } | |
6226 | } | |
6227 | cnt = snprintf(cmd, sizeof(cmd), ctx->format, | |
6228 | ctx->fname, ctx->object_base_addr, raw_frame); | |
6229 | if (cnt < sizeof(cmd)) { | |
6230 | /* Undo nostdio.h #defines that disable stdio. | |
6231 | * This is somewhat naughty, but is used elsewhere | |
6232 | * in the core, and affects only OS X. */ | |
6233 | #undef FILE | |
6234 | #undef popen | |
6235 | #undef fread | |
6236 | #undef pclose | |
6237 | FILE* fp = popen(cmd, "r"); | |
6238 | /* At the moment we open a new pipe for each stack frame. | |
6239 | * This is naturally somewhat slow, but hopefully generating | |
6240 | * stack traces is never going to in a performance critical path. | |
6241 | * | |
6242 | * We could play tricks with atos by batching the stack | |
6243 | * addresses to be resolved: atos can either take multiple | |
6244 | * addresses from the command line, or read addresses from | |
470dd224 JH |
6245 | * a file (though the mess of creating temporary files would |
6246 | * probably negate much of any possible speedup). | |
6247 | * | |
6248 | * Normally there are only two objects present in the backtrace: | |
6249 | * perl itself, and the libdyld.dylib. (Note that the object | |
6250 | * filenames contain the full pathname, so perl may not always | |
6251 | * be in the same place.) Whenever the object in the | |
6252 | * backtrace changes, the base address also changes. | |
6253 | * | |
6254 | * The problem with batching the addresses, though, would be | |
6255 | * matching the results with the addresses: the parsing of | |
6256 | * the results is already painful enough with a single address. */ | |
6257 | if (fp) { | |
6258 | char out[1024]; | |
6259 | UV cnt = fread(out, 1, sizeof(out), fp); | |
6260 | if (cnt < sizeof(out)) { | |
70ead873 | 6261 | const char* p = atos_parse(out + cnt - 1, out, |
470dd224 JH |
6262 | source_name_size, |
6263 | source_line); | |
6264 | if (p) { | |
6265 | Newx(*source_name, | |
70ead873 | 6266 | *source_name_size, char); |
470dd224 | 6267 | Copy(p, *source_name, |
70ead873 | 6268 | *source_name_size, char); |
470dd224 JH |
6269 | } |
6270 | } | |
6271 | pclose(fp); | |
6272 | } | |
6273 | } | |
6274 | } | |
6275 | ||
6276 | #endif /* #ifdef PERL_DARWIN */ | |
6277 | ||
6278 | /* | |
6279 | =for apidoc get_c_backtrace | |
6280 | ||
6281 | Collects the backtrace (aka "stacktrace") into a single linear | |
796b6530 | 6282 | malloced buffer, which the caller B<must> C<Perl_free_c_backtrace()>. |
470dd224 | 6283 | |
796b6530 KW |
6284 | Scans the frames back by S<C<depth + skip>>, then drops the C<skip> innermost, |
6285 | returning at most C<depth> frames. | |
470dd224 JH |
6286 | |
6287 | =cut | |
6288 | */ | |
6289 | ||
6290 | Perl_c_backtrace* | |
6291 | Perl_get_c_backtrace(pTHX_ int depth, int skip) | |
6292 | { | |
6293 | /* Note that here we must stay as low-level as possible: Newx(), | |
6294 | * Copy(), Safefree(); since we may be called from anywhere, | |
6295 | * so we should avoid higher level constructs like SVs or AVs. | |
6296 | * | |
6297 | * Since we are using safesysmalloc() via Newx(), don't try | |
6298 | * getting backtrace() there, unless you like deep recursion. */ | |
6299 | ||
6300 | /* Currently only implemented with backtrace() and dladdr(), | |
6301 | * for other platforms NULL is returned. */ | |
6302 | ||
6303 | #if defined(HAS_BACKTRACE) && defined(HAS_DLADDR) | |
6304 | /* backtrace() is available via <execinfo.h> in glibc and in most | |
6305 | * modern BSDs; dladdr() is available via <dlfcn.h>. */ | |
6306 | ||
6307 | /* We try fetching this many frames total, but then discard | |
6308 | * the |skip| first ones. For the remaining ones we will try | |
6309 | * retrieving more information with dladdr(). */ | |
6310 | int try_depth = skip + depth; | |
6311 | ||
6312 | /* The addresses (program counters) returned by backtrace(). */ | |
6313 | void** raw_frames; | |
6314 | ||
6315 | /* Retrieved with dladdr() from the addresses returned by backtrace(). */ | |
6316 | Dl_info* dl_infos; | |
6317 | ||
6318 | /* Sizes _including_ the terminating \0 of the object name | |
6319 | * and symbol name strings. */ | |
6320 | STRLEN* object_name_sizes; | |
6321 | STRLEN* symbol_name_sizes; | |
6322 | ||
6323 | #ifdef USE_BFD | |
6324 | /* The symbol names comes either from dli_sname, | |
6325 | * or if using BFD, they can come from BFD. */ | |
6326 | char** symbol_names; | |
6327 | #endif | |
6328 | ||
6329 | /* The source code location information. Dug out with e.g. BFD. */ | |
6330 | char** source_names; | |
6331 | STRLEN* source_name_sizes; | |
6332 | STRLEN* source_lines; | |
6333 | ||
6334 | Perl_c_backtrace* bt = NULL; /* This is what will be returned. */ | |
6335 | int got_depth; /* How many frames were returned from backtrace(). */ | |
6336 | UV frame_count = 0; /* How many frames we return. */ | |
6337 | UV total_bytes = 0; /* The size of the whole returned backtrace. */ | |
6338 | ||
6339 | #ifdef USE_BFD | |
6340 | bfd_context bfd_ctx; | |
6341 | #endif | |
6342 | #ifdef PERL_DARWIN | |
6343 | atos_context atos_ctx; | |
6344 | #endif | |
6345 | ||
6346 | /* Here are probably possibilities for optimizing. We could for | |
6347 | * example have a struct that contains most of these and then | |
6348 | * allocate |try_depth| of them, saving a bunch of malloc calls. | |
6349 | * Note, however, that |frames| could not be part of that struct | |
6350 | * because backtrace() will want an array of just them. Also be | |
6351 | * careful about the name strings. */ | |
6352 | Newx(raw_frames, try_depth, void*); | |
6353 | Newx(dl_infos, try_depth, Dl_info); | |
6354 | Newx(object_name_sizes, try_depth, STRLEN); | |
6355 | Newx(symbol_name_sizes, try_depth, STRLEN); | |
6356 | Newx(source_names, try_depth, char*); | |
6357 | Newx(source_name_sizes, try_depth, STRLEN); | |
6358 | Newx(source_lines, try_depth, STRLEN); | |
6359 | #ifdef USE_BFD | |
6360 | Newx(symbol_names, try_depth, char*); | |
6361 | #endif | |
6362 | ||
6363 | /* Get the raw frames. */ | |
6364 | got_depth = (int)backtrace(raw_frames, try_depth); | |
6365 | ||
6366 | /* We use dladdr() instead of backtrace_symbols() because we want | |
6367 | * the full details instead of opaque strings. This is useful for | |
6368 | * two reasons: () the details are needed for further symbolic | |
0762e42f JH |
6369 | * digging, for example in OS X (2) by having the details we fully |
6370 | * control the output, which in turn is useful when more platforms | |
6371 | * are added: we can keep out output "portable". */ | |
470dd224 JH |
6372 | |
6373 | /* We want a single linear allocation, which can then be freed | |
6374 | * with a single swoop. We will do the usual trick of first | |
6375 | * walking over the structure and seeing how much we need to | |
6376 | * allocate, then allocating, and then walking over the structure | |
6377 | * the second time and populating it. */ | |
6378 | ||
6379 | /* First we must compute the total size of the buffer. */ | |
6380 | total_bytes = sizeof(Perl_c_backtrace_header); | |
6381 | if (got_depth > skip) { | |
6382 | int i; | |
6383 | #ifdef USE_BFD | |
6384 | bfd_init(); /* Is this safe to call multiple times? */ | |
6385 | Zero(&bfd_ctx, 1, bfd_context); | |
6386 | #endif | |
6387 | #ifdef PERL_DARWIN | |
6388 | Zero(&atos_ctx, 1, atos_context); | |
6389 | #endif | |
6390 | for (i = skip; i < try_depth; i++) { | |
6391 | Dl_info* dl_info = &dl_infos[i]; | |
6392 | ||
4d00a319 JH |
6393 | object_name_sizes[i] = 0; |
6394 | source_names[i] = NULL; | |
6395 | source_name_sizes[i] = 0; | |
6396 | source_lines[i] = 0; | |
6397 | ||
470dd224 JH |
6398 | /* Yes, zero from dladdr() is failure. */ |
6399 | if (dladdr(raw_frames[i], dl_info)) { | |
70ead873 VT |
6400 | total_bytes += sizeof(Perl_c_backtrace_frame); |
6401 | ||
470dd224 JH |
6402 | object_name_sizes[i] = |
6403 | dl_info->dli_fname ? strlen(dl_info->dli_fname) : 0; | |
6404 | symbol_name_sizes[i] = | |
6405 | dl_info->dli_sname ? strlen(dl_info->dli_sname) : 0; | |
6406 | #ifdef USE_BFD | |
6407 | bfd_update(&bfd_ctx, dl_info); | |
6408 | bfd_symbolize(&bfd_ctx, raw_frames[i], | |
6409 | &symbol_names[i], | |
6410 | &symbol_name_sizes[i], | |
6411 | &source_names[i], | |
6412 | &source_name_sizes[i], | |
6413 | &source_lines[i]); | |
6414 | #endif | |
6415 | #if PERL_DARWIN | |
6416 | atos_update(&atos_ctx, dl_info); | |
6417 | atos_symbolize(&atos_ctx, | |
6418 | raw_frames[i], | |
6419 | &source_names[i], | |
6420 | &source_name_sizes[i], | |
6421 | &source_lines[i]); | |
6422 | #endif | |
6423 | ||
6424 | /* Plus ones for the terminating \0. */ | |
6425 | total_bytes += object_name_sizes[i] + 1; | |
6426 | total_bytes += symbol_name_sizes[i] + 1; | |
6427 | total_bytes += source_name_sizes[i] + 1; | |
6428 | ||
6429 | frame_count++; | |
6430 | } else { | |
6431 | break; | |
6432 | } | |
6433 | } | |
6434 | #ifdef USE_BFD | |
6435 | Safefree(bfd_ctx.bfd_syms); | |
6436 | #endif | |
6437 | } | |
6438 | ||
6439 | /* Now we can allocate and populate the result buffer. */ | |
6440 | Newxc(bt, total_bytes, char, Perl_c_backtrace); | |
6441 | Zero(bt, total_bytes, char); | |
6442 | bt->header.frame_count = frame_count; | |
6443 | bt->header.total_bytes = total_bytes; | |
6444 | if (frame_count > 0) { | |
6445 | Perl_c_backtrace_frame* frame = bt->frame_info; | |
6446 | char* name_base = (char *)(frame + frame_count); | |
6447 | char* name_curr = name_base; /* Outputting the name strings here. */ | |
6448 | UV i; | |
6449 | for (i = skip; i < skip + frame_count; i++) { | |
6450 | Dl_info* dl_info = &dl_infos[i]; | |
6451 | ||
6452 | frame->addr = raw_frames[i]; | |
6453 | frame->object_base_addr = dl_info->dli_fbase; | |
6454 | frame->symbol_addr = dl_info->dli_saddr; | |
6455 | ||
6456 | /* Copies a string, including the \0, and advances the name_curr. | |
6457 | * Also copies the start and the size to the frame. */ | |
6458 | #define PERL_C_BACKTRACE_STRCPY(frame, doffset, src, dsize, size) \ | |
6459 | if (size && src) \ | |
6460 | Copy(src, name_curr, size, char); \ | |
6461 | frame->doffset = name_curr - (char*)bt; \ | |
6462 | frame->dsize = size; \ | |
6463 | name_curr += size; \ | |
6464 | *name_curr++ = 0; | |
6465 | ||
6466 | PERL_C_BACKTRACE_STRCPY(frame, object_name_offset, | |
6467 | dl_info->dli_fname, | |
6468 | object_name_size, object_name_sizes[i]); | |
6469 | ||
6470 | #ifdef USE_BFD | |
6471 | PERL_C_BACKTRACE_STRCPY(frame, symbol_name_offset, | |
6472 | symbol_names[i], | |
6473 | symbol_name_size, symbol_name_sizes[i]); | |
6474 | Safefree(symbol_names[i]); | |
6475 | #else | |
6476 | PERL_C_BACKTRACE_STRCPY(frame, symbol_name_offset, | |
6477 | dl_info->dli_sname, | |
6478 | symbol_name_size, symbol_name_sizes[i]); | |
6479 | #endif | |
6480 | ||
6481 | PERL_C_BACKTRACE_STRCPY(frame, source_name_offset, | |
6482 | source_names[i], | |
6483 | source_name_size, source_name_sizes[i]); | |
6484 | Safefree(source_names[i]); | |
6485 | ||
6486 | #undef PERL_C_BACKTRACE_STRCPY | |
6487 | ||
6488 | frame->source_line_number = source_lines[i]; | |
6489 | ||
6490 | frame++; | |
6491 | } | |
6492 | assert(total_bytes == | |
6493 | (UV)(sizeof(Perl_c_backtrace_header) + | |
6494 | frame_count * sizeof(Perl_c_backtrace_frame) + | |
6495 | name_curr - name_base)); | |
6496 | } | |
6497 | #ifdef USE_BFD | |
6498 | Safefree(symbol_names); | |
a1684041 JH |
6499 | if (bfd_ctx.abfd) { |
6500 | bfd_close(bfd_ctx.abfd); | |
6501 | } | |
470dd224 JH |
6502 | #endif |
6503 | Safefree(source_lines); | |
6504 | Safefree(source_name_sizes); | |
6505 | Safefree(source_names); | |
6506 | Safefree(symbol_name_sizes); | |
6507 | Safefree(object_name_sizes); | |
6508 | /* Assuming the strings returned by dladdr() are pointers | |
6509 | * to read-only static memory (the object file), so that | |
6510 | * they do not need freeing (and cannot be). */ | |
6511 | Safefree(dl_infos); | |
6512 | Safefree(raw_frames); | |
6513 | return bt; | |
6514 | #else | |
6515 | PERL_UNUSED_ARGV(depth); | |
6516 | PERL_UNUSED_ARGV(skip); | |
6517 | return NULL; | |
6518 | #endif | |
6519 | } | |
6520 | ||
6521 | /* | |
6522 | =for apidoc free_c_backtrace | |
6523 | ||
6524 | Deallocates a backtrace received from get_c_bracktrace. | |
6525 | ||
6526 | =cut | |
6527 | */ | |
6528 | ||
6529 | /* | |
6530 | =for apidoc get_c_backtrace_dump | |
6531 | ||
796b6530 KW |
6532 | Returns a SV containing a dump of C<depth> frames of the call stack, skipping |
6533 | the C<skip> innermost ones. C<depth> of 20 is usually enough. | |
470dd224 JH |
6534 | |
6535 | The appended output looks like: | |
6536 | ||
6537 | ... | |
6538 | 1 10e004812:0082 Perl_croak util.c:1716 /usr/bin/perl | |
6539 | 2 10df8d6d2:1d72 perl_parse perl.c:3975 /usr/bin/perl | |
6540 | ... | |
6541 | ||
6542 | The fields are tab-separated. The first column is the depth (zero | |
6543 | being the innermost non-skipped frame). In the hex:offset, the hex is | |
796b6530 KW |
6544 | where the program counter was in C<S_parse_body>, and the :offset (might |
6545 | be missing) tells how much inside the C<S_parse_body> the program counter was. | |
470dd224 | 6546 | |
796b6530 | 6547 | The C<util.c:1716> is the source code file and line number. |
470dd224 | 6548 | |
75af9d73 | 6549 | The F</usr/bin/perl> is obvious (hopefully). |
470dd224 JH |
6550 | |
6551 | Unknowns are C<"-">. Unknowns can happen unfortunately quite easily: | |
6552 | if the platform doesn't support retrieving the information; | |
6553 | if the binary is missing the debug information; | |
6554 | if the optimizer has transformed the code by for example inlining. | |
6555 | ||
6556 | =cut | |
6557 | */ | |
6558 | ||
6559 | SV* | |
6560 | Perl_get_c_backtrace_dump(pTHX_ int depth, int skip) | |
6561 | { | |
6562 | Perl_c_backtrace* bt; | |
6563 | ||
6564 | bt = get_c_backtrace(depth, skip + 1 /* Hide ourselves. */); | |
6565 | if (bt) { | |
6566 | Perl_c_backtrace_frame* frame; | |
6567 | SV* dsv = newSVpvs(""); | |
6568 | UV i; | |
6569 | for (i = 0, frame = bt->frame_info; | |
6570 | i < bt->header.frame_count; i++, frame++) { | |
6571 | Perl_sv_catpvf(aTHX_ dsv, "%d", (int)i); | |
6572 | Perl_sv_catpvf(aTHX_ dsv, "\t%p", frame->addr ? frame->addr : "-"); | |
6573 | /* Symbol (function) names might disappear without debug info. | |
6574 | * | |
6575 | * The source code location might disappear in case of the | |
6576 | * optimizer inlining or otherwise rearranging the code. */ | |
6577 | if (frame->symbol_addr) { | |
6578 | Perl_sv_catpvf(aTHX_ dsv, ":%04x", | |
6579 | (int) | |
6580 | ((char*)frame->addr - (char*)frame->symbol_addr)); | |
6581 | } | |
6582 | Perl_sv_catpvf(aTHX_ dsv, "\t%s", | |
6583 | frame->symbol_name_size && | |
6584 | frame->symbol_name_offset ? | |
6585 | (char*)bt + frame->symbol_name_offset : "-"); | |
6586 | if (frame->source_name_size && | |
6587 | frame->source_name_offset && | |
6588 | frame->source_line_number) { | |
6589 | Perl_sv_catpvf(aTHX_ dsv, "\t%s:%"UVuf, | |
6590 | (char*)bt + frame->source_name_offset, | |
6591 | (UV)frame->source_line_number); | |
6592 | } else { | |
6593 | Perl_sv_catpvf(aTHX_ dsv, "\t-"); | |
6594 | } | |
6595 | Perl_sv_catpvf(aTHX_ dsv, "\t%s", | |
6596 | frame->object_name_size && | |
6597 | frame->object_name_offset ? | |
6598 | (char*)bt + frame->object_name_offset : "-"); | |
6599 | /* The frame->object_base_addr is not output, | |
6600 | * but it is used for symbolizing/symbolicating. */ | |
6601 | sv_catpvs(dsv, "\n"); | |
6602 | } | |
6603 | ||
6604 | Perl_free_c_backtrace(aTHX_ bt); | |
6605 | ||
6606 | return dsv; | |
6607 | } | |
6608 | ||
6609 | return NULL; | |
6610 | } | |
6611 | ||
6612 | /* | |
6613 | =for apidoc dump_c_backtrace | |
6614 | ||
796b6530 | 6615 | Dumps the C backtrace to the given C<fp>. |
470dd224 JH |
6616 | |
6617 | Returns true if a backtrace could be retrieved, false if not. | |
6618 | ||
6619 | =cut | |
6620 | */ | |
6621 | ||
6622 | bool | |
6623 | Perl_dump_c_backtrace(pTHX_ PerlIO* fp, int depth, int skip) | |
6624 | { | |
6625 | SV* sv; | |
6626 | ||
6627 | PERL_ARGS_ASSERT_DUMP_C_BACKTRACE; | |
6628 | ||
6629 | sv = Perl_get_c_backtrace_dump(aTHX_ depth, skip); | |
6630 | if (sv) { | |
6631 | sv_2mortal(sv); | |
6632 | PerlIO_printf(fp, "%s", SvPV_nolen(sv)); | |
6633 | return TRUE; | |
6634 | } | |
6635 | return FALSE; | |
6636 | } | |
6637 | ||
6638 | #endif /* #ifdef USE_C_BACKTRACE */ | |
3be8f094 | 6639 | |
3baee7cc JH |
6640 | #ifdef PERL_TSA_ACTIVE |
6641 | ||
6642 | /* pthread_mutex_t and perl_mutex are typedef equivalent | |
6643 | * so casting the pointers is fine. */ | |
6644 | ||
6645 | int perl_tsa_mutex_lock(perl_mutex* mutex) | |
6646 | { | |
6647 | return pthread_mutex_lock((pthread_mutex_t *) mutex); | |
6648 | } | |
6649 | ||
6650 | int perl_tsa_mutex_unlock(perl_mutex* mutex) | |
6651 | { | |
6652 | return pthread_mutex_unlock((pthread_mutex_t *) mutex); | |
6653 | } | |
6654 | ||
6655 | int perl_tsa_mutex_destroy(perl_mutex* mutex) | |
6656 | { | |
6657 | return pthread_mutex_destroy((pthread_mutex_t *) mutex); | |
6658 | } | |
6659 | ||
6660 | #endif | |
6661 | ||
3f6bd23a DM |
6662 | |
6663 | #ifdef USE_DTRACE | |
6664 | ||
6665 | /* log a sub call or return */ | |
6666 | ||
6667 | void | |
6668 | Perl_dtrace_probe_call(pTHX_ CV *cv, bool is_call) | |
6669 | { | |
6670 | const char *func; | |
6671 | const char *file; | |
6672 | const char *stash; | |
6673 | const COP *start; | |
6674 | line_t line; | |
6675 | ||
6676 | PERL_ARGS_ASSERT_DTRACE_PROBE_CALL; | |
6677 | ||
6678 | if (CvNAMED(cv)) { | |
6679 | HEK *hek = CvNAME_HEK(cv); | |
6680 | func = HEK_KEY(hek); | |
6681 | } | |
6682 | else { | |
6683 | GV *gv = CvGV(cv); | |
6684 | func = GvENAME(gv); | |
6685 | } | |
6686 | start = (const COP *)CvSTART(cv); | |
6687 | file = CopFILE(start); | |
6688 | line = CopLINE(start); | |
6689 | stash = CopSTASHPV(start); | |
6690 | ||
6691 | if (is_call) { | |
6692 | PERL_SUB_ENTRY(func, file, line, stash); | |
6693 | } | |
6694 | else { | |
6695 | PERL_SUB_RETURN(func, file, line, stash); | |
6696 | } | |
6697 | } | |
6698 | ||
6699 | ||
6700 | /* log a require file loading/loaded */ | |
6701 | ||
6702 | void | |
6703 | Perl_dtrace_probe_load(pTHX_ const char *name, bool is_loading) | |
6704 | { | |
6705 | PERL_ARGS_ASSERT_DTRACE_PROBE_LOAD; | |
6706 | ||
6707 | if (is_loading) { | |
6708 | PERL_LOADING_FILE(name); | |
6709 | } | |
6710 | else { | |
6711 | PERL_LOADED_FILE(name); | |
6712 | } | |
6713 | } | |
6714 | ||
6715 | ||
6716 | /* log an op execution */ | |
6717 | ||
6718 | void | |
6719 | Perl_dtrace_probe_op(pTHX_ const OP *op) | |
6720 | { | |
6721 | PERL_ARGS_ASSERT_DTRACE_PROBE_OP; | |
6722 | ||
6723 | PERL_OP_ENTRY(OP_NAME(op)); | |
6724 | } | |
6725 | ||
6726 | ||
6727 | /* log a compile/run phase change */ | |
6728 | ||
6729 | void | |
6730 | Perl_dtrace_probe_phase(pTHX_ enum perl_phase phase) | |
6731 | { | |
6732 | const char *ph_old = PL_phase_names[PL_phase]; | |
6733 | const char *ph_new = PL_phase_names[phase]; | |
6734 | ||
6735 | PERL_PHASE_CHANGE(ph_new, ph_old); | |
6736 | } | |
6737 | ||
6738 | #endif | |
6739 | ||
3be8f094 | 6740 | /* |
14d04a33 | 6741 | * ex: set ts=8 sts=4 sw=4 et: |
37442d52 | 6742 | */ |