This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
add the 5.17 feature bundle
[perl5.git] / regen / reentr.pl
... / ...
CommitLineData
1#!/usr/bin/perl -w
2#
3# Regenerate (overwriting only if changed):
4#
5# reentr.h
6# reentr.c
7#
8# from information stored in the DATA section of this file.
9#
10# With the -U option, it also unconditionally regenerates the relevant
11# metaconfig units:
12#
13# d_${func}_r.U
14#
15# Also accepts the standard regen_lib -q and -v args.
16#
17# This script is normally invoked from regen.pl.
18
19BEGIN {
20 # Get function prototypes
21 require 'regen/regen_lib.pl';
22}
23
24use strict;
25use Getopt::Std;
26my %opts;
27getopts('Uv', \%opts);
28
29my %map = (
30 V => "void",
31 A => "char*", # as an input argument
32 B => "char*", # as an output argument
33 C => "const char*", # as a read-only input argument
34 I => "int",
35 L => "long",
36 W => "size_t",
37 H => "FILE**",
38 E => "int*",
39 );
40
41# (See the definitions after __DATA__.)
42# In func|inc|type|... a "S" means "type*", and a "R" means "type**".
43# (The "types" are often structs, such as "struct passwd".)
44#
45# After the prototypes one can have |X=...|Y=... to define more types.
46# A commonly used extra type is to define D to be equal to "type_data",
47# for example "struct_hostent_data to" go with "struct hostent".
48#
49# Example #1: I_XSBWR means int func_r(X, type, char*, size_t, type**)
50# Example #2: S_SBIE means type func_r(type, char*, int, int*)
51# Example #3: S_CBI means type func_r(const char*, char*, int)
52
53sub open_print_header {
54 my ($file, $quote) = @_;
55 return open_new($file, '>',
56 { by => 'regen/reentr.pl',
57 from => 'data in regen/reentr.pl',
58 file => $file, style => '*',
59 copyright => [2002, 2003, 2005 .. 2007],
60 quote => $quote });
61}
62
63my $h = open_print_header('reentr.h');
64print $h <<EOF;
65#ifndef REENTR_H
66#define REENTR_H
67
68/* If compiling for a threaded perl, we will macro-wrap the system/library
69 * interfaces (e.g. getpwent()) which have threaded versions
70 * (e.g. getpwent_r()), which will handle things correctly for
71 * the Perl interpreter, but otherwise (for XS) the wrapping does
72 * not take place. See L<perlxs/Thread-aware system interfaces>.
73 */
74
75#ifndef PERL_REENTR_API
76# if defined(PERL_CORE) || defined(PERL_EXT)
77# define PERL_REENTR_API 1
78# else
79# define PERL_REENTR_API 0
80# endif
81#endif
82
83#ifdef USE_REENTRANT_API
84
85/* Deprecations: some platforms have the said reentrant interfaces
86 * but they are declared obsolete and are not to be used. Often this
87 * means that the platform has threadsafed the interfaces (hopefully).
88 * All this is OS version dependent, so we are of course fooling ourselves.
89 * If you know of more deprecations on some platforms, please add your own
90 * (by editing reentr.pl, mind!) */
91
92#ifdef __hpux
93# undef HAS_CRYPT_R
94# undef HAS_DRAND48_R
95# undef HAS_ENDGRENT_R
96# undef HAS_ENDPWENT_R
97# undef HAS_GETGRENT_R
98# undef HAS_GETPWENT_R
99# undef HAS_SETLOCALE_R
100# undef HAS_SRAND48_R
101# undef HAS_STRERROR_R
102# define NETDB_R_OBSOLETE
103#endif
104
105#if defined(__osf__) && defined(__alpha) /* Tru64 aka Digital UNIX */
106# undef HAS_CRYPT_R
107# undef HAS_STRERROR_R
108# define NETDB_R_OBSOLETE
109#endif
110
111/*
112 * As of OpenBSD 3.7, reentrant functions are now working, they just are
113 * incompatible with everyone else. To make OpenBSD happy, we have to
114 * memzero out certain structures before calling the functions.
115 */
116#if defined(__OpenBSD__)
117# define REENTR_MEMZERO(a,b) memzero(a,b)
118#else
119# define REENTR_MEMZERO(a,b) 0
120#endif
121
122#ifdef NETDB_R_OBSOLETE
123# undef HAS_ENDHOSTENT_R
124# undef HAS_ENDNETENT_R
125# undef HAS_ENDPROTOENT_R
126# undef HAS_ENDSERVENT_R
127# undef HAS_GETHOSTBYADDR_R
128# undef HAS_GETHOSTBYNAME_R
129# undef HAS_GETHOSTENT_R
130# undef HAS_GETNETBYADDR_R
131# undef HAS_GETNETBYNAME_R
132# undef HAS_GETNETENT_R
133# undef HAS_GETPROTOBYNAME_R
134# undef HAS_GETPROTOBYNUMBER_R
135# undef HAS_GETPROTOENT_R
136# undef HAS_GETSERVBYNAME_R
137# undef HAS_GETSERVBYPORT_R
138# undef HAS_GETSERVENT_R
139# undef HAS_SETHOSTENT_R
140# undef HAS_SETNETENT_R
141# undef HAS_SETPROTOENT_R
142# undef HAS_SETSERVENT_R
143#endif
144
145#ifdef I_PWD
146# include <pwd.h>
147#endif
148#ifdef I_GRP
149# include <grp.h>
150#endif
151#ifdef I_NETDB
152# include <netdb.h>
153#endif
154#ifdef I_STDLIB
155# include <stdlib.h> /* drand48_data */
156#endif
157#ifdef I_CRYPT
158# ifdef I_CRYPT
159# include <crypt.h>
160# endif
161#endif
162#ifdef HAS_GETSPNAM_R
163# ifdef I_SHADOW
164# include <shadow.h>
165# endif
166#endif
167
168EOF
169
170my %seenh; # the different prototypes signatures for this function
171my %seena; # the different prototypes signatures for this function in order
172my @seenf; # all the seen functions
173my %seenp; # the different prototype signatures for all functions
174my %seent; # the return type of this function
175my %seens; # the type of this function's "S"
176my %seend; # the type of this function's "D"
177my %seenm; # all the types
178my %seenu; # the length of the argument list of this function
179
180while (<DATA>) { # Read in the protypes.
181 next if /^\s+$/;
182 chomp;
183 my ($func, $hdr, $type, @p) = split(/\s*\|\s*/, $_, -1);
184 my $u;
185 # Split off the real function name and the argument list.
186 ($func, $u) = split(' ', $func);
187 $seenu{$func} = defined $u ? length $u : 0;
188 my $FUNC = uc $func; # for output.
189 push @seenf, $func;
190 my %m = %map;
191 if ($type) {
192 $m{S} = "$type*";
193 $m{R} = "$type**";
194 }
195
196 # Set any special mapping variables (like X=x_t)
197 if (@p) {
198 while ($p[-1] =~ /=/) {
199 my ($k, $v) = ($p[-1] =~ /^([A-Za-z])\s*=\s*(.*)/);
200 $m{$k} = $v;
201 pop @p;
202 }
203 }
204
205 # If given the -U option open up the metaconfig unit for this function.
206 if ($opts{U} && open(U, ">d_${func}_r.U")) {
207 binmode U;
208 }
209
210 if ($opts{U}) {
211 # The metaconfig units needs prerequisite dependencies.
212 my $prereqs = '';
213 my $prereqh = '';
214 my $prereqsh = '';
215 if ($hdr ne 'stdio') { # There's no i_stdio.
216 $prereqs = "i_$hdr";
217 $prereqh = "$hdr.h";
218 $prereqsh = "\$$prereqs $prereqh";
219 }
220 my @prereq = qw(Inlibc Protochk Hasproto i_systypes usethreads);
221 push @prereq, $prereqs;
222 my $hdrs = "\$i_systypes sys/types.h define stdio.h $prereqsh";
223 if ($hdr eq 'time') {
224 $hdrs .= " \$i_systime sys/time.h";
225 push @prereq, 'i_systime';
226 }
227 # Output the metaconfig unit header.
228 print U <<"EOF";
229?RCS: \$Id: d_${func}_r.U,v $
230?RCS:
231?RCS: Copyright (c) 2002,2003 Jarkko Hietaniemi
232?RCS:
233?RCS: You may distribute under the terms of either the GNU General Public
234?RCS: License or the Artistic License, as specified in the README file.
235?RCS:
236?RCS: Generated by the reentr.pl from the Perl 5.8 distribution.
237?RCS:
238?MAKE:d_${func}_r ${func}_r_proto: @prereq
239?MAKE: -pick add \$@ %<
240?S:d_${func}_r:
241?S: This variable conditionally defines the HAS_${FUNC}_R symbol,
242?S: which indicates to the C program that the ${func}_r()
243?S: routine is available.
244?S:.
245?S:${func}_r_proto:
246?S: This variable encodes the prototype of ${func}_r.
247?S: It is zero if d_${func}_r is undef, and one of the
248?S: REENTRANT_PROTO_T_ABC macros of reentr.h if d_${func}_r
249?S: is defined.
250?S:.
251?C:HAS_${FUNC}_R:
252?C: This symbol, if defined, indicates that the ${func}_r routine
253?C: is available to ${func} re-entrantly.
254?C:.
255?C:${FUNC}_R_PROTO:
256?C: This symbol encodes the prototype of ${func}_r.
257?C: It is zero if d_${func}_r is undef, and one of the
258?C: REENTRANT_PROTO_T_ABC macros of reentr.h if d_${func}_r
259?C: is defined.
260?C:.
261?H:#\$d_${func}_r HAS_${FUNC}_R /**/
262?H:#define ${FUNC}_R_PROTO \$${func}_r_proto /**/
263?H:.
264?T:try hdrs d_${func}_r_proto
265?LINT:set d_${func}_r
266?LINT:set ${func}_r_proto
267: see if ${func}_r exists
268set ${func}_r d_${func}_r
269eval \$inlibc
270case "\$d_${func}_r" in
271"\$define")
272EOF
273 print U <<"EOF";
274 hdrs="$hdrs"
275 case "\$d_${func}_r_proto:\$usethreads" in
276 ":define") d_${func}_r_proto=define
277 set d_${func}_r_proto ${func}_r \$hdrs
278 eval \$hasproto ;;
279 *) ;;
280 esac
281 case "\$d_${func}_r_proto" in
282 define)
283EOF
284 }
285 for my $p (@p) {
286 my ($r, $a) = ($p =~ /^(.)_(.+)/);
287 my $v = join(", ", map { $m{$_} } split '', $a);
288 if ($opts{U}) {
289 print U <<"EOF";
290 case "\$${func}_r_proto" in
291 ''|0) try='$m{$r} ${func}_r($v);'
292 ./protochk "extern \$try" \$hdrs && ${func}_r_proto=$p ;;
293 esac
294EOF
295 }
296 $seenh{$func}->{$p}++;
297 push @{$seena{$func}}, $p;
298 $seenp{$p}++;
299 $seent{$func} = $type;
300 $seens{$func} = $m{S};
301 $seend{$func} = $m{D};
302 $seenm{$func} = \%m;
303 }
304 if ($opts{U}) {
305 print U <<"EOF";
306 case "\$${func}_r_proto" in
307 ''|0) d_${func}_r=undef
308 ${func}_r_proto=0
309 echo "Disabling ${func}_r, cannot determine prototype." >&4 ;;
310 * ) case "\$${func}_r_proto" in
311 REENTRANT_PROTO*) ;;
312 *) ${func}_r_proto="REENTRANT_PROTO_\$${func}_r_proto" ;;
313 esac
314 echo "Prototype: \$try" ;;
315 esac
316 ;;
317 *) case "\$usethreads" in
318 define) echo "${func}_r has no prototype, not using it." >&4 ;;
319 esac
320 d_${func}_r=undef
321 ${func}_r_proto=0
322 ;;
323 esac
324 ;;
325*) ${func}_r_proto=0
326 ;;
327esac
328
329EOF
330 close(U);
331 }
332}
333
334close DATA;
335
336{
337 # Write out all the known prototype signatures.
338 my $i = 1;
339 for my $p (sort keys %seenp) {
340 print $h "#define REENTRANT_PROTO_${p} ${i}\n";
341 $i++;
342 }
343}
344
345my @struct; # REENTR struct members
346my @size; # struct member buffer size initialization code
347my @init; # struct member buffer initialization (malloc) code
348my @free; # struct member buffer release (free) code
349my @wrap; # the wrapper (foo(a) -> foo_r(a,...)) cpp code
350my @define; # defines for optional features
351
352sub ifprotomatch {
353 my $FUNC = shift;
354 join " || ", map { "${FUNC}_R_PROTO == REENTRANT_PROTO_$_" } @_;
355}
356
357sub pushssif {
358 push @struct, @_;
359 push @size, @_;
360 push @init, @_;
361 push @free, @_;
362}
363
364sub pushinitfree {
365 my $func = shift;
366 push @init, <<EOF;
367 Newx(PL_reentrant_buffer->_${func}_buffer, PL_reentrant_buffer->_${func}_size, char);
368EOF
369 push @free, <<EOF;
370 Safefree(PL_reentrant_buffer->_${func}_buffer);
371EOF
372}
373
374sub define {
375 my ($n, $p, @F) = @_;
376 my @H;
377 my $H = uc $F[0];
378 push @define, <<EOF;
379/* The @F using \L$n? */
380
381EOF
382 my $GENFUNC;
383 for my $func (@F) {
384 my $FUNC = uc $func;
385 my $HAS = "${FUNC}_R_HAS_$n";
386 push @H, $HAS;
387 my @h = grep { /$p/ } @{$seena{$func}};
388 unless (defined $GENFUNC) {
389 $GENFUNC = $FUNC;
390 $GENFUNC =~ s/^GET//;
391 }
392 if (@h) {
393 push @define, "#if defined(HAS_${FUNC}_R) && (" . join(" || ", map { "${FUNC}_R_PROTO == REENTRANT_PROTO_$_" } @h) . ")\n";
394
395 push @define, <<EOF;
396# define $HAS
397#else
398# undef $HAS
399#endif
400EOF
401 }
402 }
403 return if @F == 1;
404 push @define, <<EOF;
405
406/* Any of the @F using \L$n? */
407
408EOF
409 push @define, "#if (" . join(" || ", map { "defined($_)" } @H) . ")\n";
410 push @define, <<EOF;
411# define USE_${GENFUNC}_$n
412#else
413# undef USE_${GENFUNC}_$n
414#endif
415
416EOF
417}
418
419define('BUFFER', 'B',
420 qw(getgrent getgrgid getgrnam));
421
422define('PTR', 'R',
423 qw(getgrent getgrgid getgrnam));
424define('PTR', 'R',
425 qw(getpwent getpwnam getpwuid));
426define('PTR', 'R',
427 qw(getspent getspnam));
428
429define('FPTR', 'H',
430 qw(getgrent getgrgid getgrnam setgrent endgrent));
431define('FPTR', 'H',
432 qw(getpwent getpwnam getpwuid setpwent endpwent));
433
434define('BUFFER', 'B',
435 qw(getpwent getpwgid getpwnam));
436
437define('PTR', 'R',
438 qw(gethostent gethostbyaddr gethostbyname));
439define('PTR', 'R',
440 qw(getnetent getnetbyaddr getnetbyname));
441define('PTR', 'R',
442 qw(getprotoent getprotobyname getprotobynumber));
443define('PTR', 'R',
444 qw(getservent getservbyname getservbyport));
445
446define('BUFFER', 'B',
447 qw(gethostent gethostbyaddr gethostbyname));
448define('BUFFER', 'B',
449 qw(getnetent getnetbyaddr getnetbyname));
450define('BUFFER', 'B',
451 qw(getprotoent getprotobyname getprotobynumber));
452define('BUFFER', 'B',
453 qw(getservent getservbyname getservbyport));
454
455define('ERRNO', 'E',
456 qw(gethostent gethostbyaddr gethostbyname));
457define('ERRNO', 'E',
458 qw(getnetent getnetbyaddr getnetbyname));
459
460# The following loop accumulates the "ssif" (struct, size, init, free)
461# sections that declare the struct members (in reentr.h), and the buffer
462# size initialization, buffer initialization (malloc), and buffer
463# release (free) code (in reentr.c).
464#
465# The loop also contains a lot of intrinsic logic about groups of
466# functions (since functions of certain kind operate the same way).
467
468for my $func (@seenf) {
469 my $FUNC = uc $func;
470 my $ifdef = "#ifdef HAS_${FUNC}_R\n";
471 my $endif = "#endif /* HAS_${FUNC}_R */\n";
472 if (exists $seena{$func}) {
473 my @p = @{$seena{$func}};
474 if ($func =~ /^(asctime|ctime|getlogin|setlocale|strerror|ttyname)$/) {
475 pushssif $ifdef;
476 push @struct, <<EOF;
477 char* _${func}_buffer;
478 size_t _${func}_size;
479EOF
480 push @size, <<EOF;
481 PL_reentrant_buffer->_${func}_size = REENTRANTSMALLSIZE;
482EOF
483 pushinitfree $func;
484 pushssif $endif;
485 }
486 elsif ($func =~ /^(crypt)$/) {
487 pushssif $ifdef;
488 push @struct, <<EOF;
489#if CRYPT_R_PROTO == REENTRANT_PROTO_B_CCD
490 $seend{$func} _${func}_data;
491#else
492 $seent{$func} *_${func}_struct_buffer;
493#endif
494EOF
495 push @init, <<EOF;
496#if CRYPT_R_PROTO != REENTRANT_PROTO_B_CCD
497 PL_reentrant_buffer->_${func}_struct_buffer = 0;
498#endif
499EOF
500 push @free, <<EOF;
501#if CRYPT_R_PROTO != REENTRANT_PROTO_B_CCD
502 Safefree(PL_reentrant_buffer->_${func}_struct_buffer);
503#endif
504EOF
505 pushssif $endif;
506 }
507 elsif ($func =~ /^(drand48|random|srandom)$/) {
508 pushssif $ifdef;
509 push @struct, <<EOF;
510 $seent{$func} _${func}_struct;
511EOF
512 if ($1 eq 'drand48') {
513 push @struct, <<EOF;
514 double _${func}_double;
515EOF
516 } elsif ($1 eq 'random') {
517 push @struct, <<EOF;
518# if RANDOM_R_PROTO == REENTRANT_PROTO_I_iS
519 int _${func}_retval;
520# endif
521# if RANDOM_R_PROTO == REENTRANT_PROTO_I_lS
522 long _${func}_retval;
523# endif
524# if RANDOM_R_PROTO == REENTRANT_PROTO_I_St
525 int32_t _${func}_retval;
526# endif
527EOF
528 }
529 pushssif $endif;
530 }
531 elsif ($func =~ /^(getgrnam|getpwnam|getspnam)$/) {
532 pushssif $ifdef;
533 # 'genfunc' can be read either as 'generic' or 'genre',
534 # it represents a group of functions.
535 my $genfunc = $func;
536 $genfunc =~ s/nam/ent/g;
537 $genfunc =~ s/^get//;
538 my $GENFUNC = uc $genfunc;
539 push @struct, <<EOF;
540 $seent{$func} _${genfunc}_struct;
541 char* _${genfunc}_buffer;
542 size_t _${genfunc}_size;
543EOF
544 push @struct, <<EOF;
545# ifdef USE_${GENFUNC}_PTR
546 $seent{$func}* _${genfunc}_ptr;
547# endif
548EOF
549 push @struct, <<EOF;
550# ifdef USE_${GENFUNC}_FPTR
551 FILE* _${genfunc}_fptr;
552# endif
553EOF
554 push @init, <<EOF;
555# ifdef USE_${GENFUNC}_FPTR
556 PL_reentrant_buffer->_${genfunc}_fptr = NULL;
557# endif
558EOF
559 my $sc = $genfunc eq 'grent' ?
560 '_SC_GETGR_R_SIZE_MAX' : '_SC_GETPW_R_SIZE_MAX';
561 my $sz = "_${genfunc}_size";
562 push @size, <<EOF;
563# if defined(HAS_SYSCONF) && defined($sc) && !defined(__GLIBC__)
564 PL_reentrant_buffer->$sz = sysconf($sc);
565 if (PL_reentrant_buffer->$sz == (size_t) -1)
566 PL_reentrant_buffer->$sz = REENTRANTUSUALSIZE;
567# else
568# if defined(__osf__) && defined(__alpha) && defined(SIABUFSIZ)
569 PL_reentrant_buffer->$sz = SIABUFSIZ;
570# else
571# ifdef __sgi
572 PL_reentrant_buffer->$sz = BUFSIZ;
573# else
574 PL_reentrant_buffer->$sz = REENTRANTUSUALSIZE;
575# endif
576# endif
577# endif
578EOF
579 pushinitfree $genfunc;
580 pushssif $endif;
581 }
582 elsif ($func =~ /^(gethostbyname|getnetbyname|getservbyname|getprotobyname)$/) {
583 pushssif $ifdef;
584 my $genfunc = $func;
585 $genfunc =~ s/byname/ent/;
586 $genfunc =~ s/^get//;
587 my $GENFUNC = uc $genfunc;
588 my $D = ifprotomatch($FUNC, grep {/D/} @p);
589 my $d = $seend{$func};
590 $d =~ s/\*$//; # snip: we need need the base type.
591 push @struct, <<EOF;
592 $seent{$func} _${genfunc}_struct;
593# if $D
594 $d _${genfunc}_data;
595# else
596 char* _${genfunc}_buffer;
597 size_t _${genfunc}_size;
598# endif
599# ifdef USE_${GENFUNC}_PTR
600 $seent{$func}* _${genfunc}_ptr;
601# endif
602EOF
603 push @struct, <<EOF;
604# ifdef USE_${GENFUNC}_ERRNO
605 int _${genfunc}_errno;
606# endif
607EOF
608 push @size, <<EOF;
609#if !($D)
610 PL_reentrant_buffer->_${genfunc}_size = REENTRANTUSUALSIZE;
611#endif
612EOF
613 push @init, <<EOF;
614#if !($D)
615 Newx(PL_reentrant_buffer->_${genfunc}_buffer, PL_reentrant_buffer->_${genfunc}_size, char);
616#endif
617EOF
618 push @free, <<EOF;
619#if !($D)
620 Safefree(PL_reentrant_buffer->_${genfunc}_buffer);
621#endif
622EOF
623 pushssif $endif;
624 }
625 elsif ($func =~ /^(readdir|readdir64)$/) {
626 pushssif $ifdef;
627 my $R = ifprotomatch($FUNC, grep {/R/} @p);
628 push @struct, <<EOF;
629 $seent{$func}* _${func}_struct;
630 size_t _${func}_size;
631# if $R
632 $seent{$func}* _${func}_ptr;
633# endif
634EOF
635 push @size, <<EOF;
636 /* This is the size Solaris recommends.
637 * (though we go static, should use pathconf() instead) */
638 PL_reentrant_buffer->_${func}_size = sizeof($seent{$func}) + MAXPATHLEN + 1;
639EOF
640 push @init, <<EOF;
641 PL_reentrant_buffer->_${func}_struct = ($seent{$func}*)safemalloc(PL_reentrant_buffer->_${func}_size);
642EOF
643 push @free, <<EOF;
644 Safefree(PL_reentrant_buffer->_${func}_struct);
645EOF
646 pushssif $endif;
647 }
648
649 push @wrap, $ifdef;
650
651 push @wrap, <<EOF;
652# if defined(PERL_REENTR_API) && (PERL_REENTR_API+0 == 1)
653# undef $func
654EOF
655
656 # Write out what we have learned.
657
658 my @v = 'a'..'z';
659 my $v = join(", ", @v[0..$seenu{$func}-1]);
660 for my $p (@p) {
661 my ($r, $a) = split '_', $p;
662 my $test = $r eq 'I' ? ' == 0' : '';
663 my $true = 1;
664 my $genfunc = $func;
665 if ($genfunc =~ /^(?:get|set|end)(pw|gr|host|net|proto|serv|sp)/) {
666 $genfunc = "${1}ent";
667 } elsif ($genfunc eq 'srand48') {
668 $genfunc = "drand48";
669 }
670 my $b = $a;
671 my $w = '';
672 substr($b, 0, $seenu{$func}) = '';
673 if ($func =~ /^random$/) {
674 $true = "PL_reentrant_buffer->_random_retval";
675 } elsif ($b =~ /R/) {
676 $true = "PL_reentrant_buffer->_${genfunc}_ptr";
677 } elsif ($b =~ /T/ && $func eq 'drand48') {
678 $true = "PL_reentrant_buffer->_${genfunc}_double";
679 } elsif ($b =~ /S/) {
680 if ($func =~ /^readdir/) {
681 $true = "PL_reentrant_buffer->_${genfunc}_struct";
682 } else {
683 $true = "&PL_reentrant_buffer->_${genfunc}_struct";
684 }
685 } elsif ($b =~ /B/) {
686 $true = "PL_reentrant_buffer->_${genfunc}_buffer";
687 }
688 if (length $b) {
689 $w = join ", ",
690 map {
691 $_ eq 'R' ?
692 "&PL_reentrant_buffer->_${genfunc}_ptr" :
693 $_ eq 'E' ?
694 "&PL_reentrant_buffer->_${genfunc}_errno" :
695 $_ eq 'B' ?
696 "PL_reentrant_buffer->_${genfunc}_buffer" :
697 $_ =~ /^[WI]$/ ?
698 "PL_reentrant_buffer->_${genfunc}_size" :
699 $_ eq 'H' ?
700 "&PL_reentrant_buffer->_${genfunc}_fptr" :
701 $_ eq 'D' ?
702 "&PL_reentrant_buffer->_${genfunc}_data" :
703 $_ eq 'S' ?
704 ($func =~ /^readdir\d*$/ ?
705 "PL_reentrant_buffer->_${genfunc}_struct" :
706 $func =~ /^crypt$/ ?
707 "PL_reentrant_buffer->_${genfunc}_struct_buffer" :
708 "&PL_reentrant_buffer->_${genfunc}_struct") :
709 $_ eq 'T' && $func eq 'drand48' ?
710 "&PL_reentrant_buffer->_${genfunc}_double" :
711 $_ =~ /^[ilt]$/ && $func eq 'random' ?
712 "&PL_reentrant_buffer->_random_retval" :
713 $_
714 } split '', $b;
715 $w = ", $w" if length $v;
716 }
717
718 my $call = "${func}_r($v$w)";
719
720 # Must make OpenBSD happy
721 my $memzero = '';
722 if($p =~ /D$/ &&
723 ($genfunc eq 'protoent' || $genfunc eq 'servent')) {
724 $memzero = 'REENTR_MEMZERO(&PL_reentrant_buffer->_' . $genfunc . '_data, sizeof(PL_reentrant_buffer->_' . $genfunc . '_data)),';
725 }
726 push @wrap, <<EOF;
727# if !defined($func) && ${FUNC}_R_PROTO == REENTRANT_PROTO_$p
728EOF
729 if ($r eq 'V' || $r eq 'B') {
730 push @wrap, <<EOF;
731# define $func($v) $call
732EOF
733 } else {
734 if ($func =~ /^get/) {
735 my $rv = $v ? ", $v" : "";
736 if ($r eq 'I') {
737 push @wrap, <<EOF;
738# define $func($v) ($memzero(PL_reentrant_retint = $call)$test ? $true : ((PL_reentrant_retint == ERANGE) ? ($seent{$func} *) Perl_reentrant_retry("$func"$rv) : 0))
739EOF
740 } else {
741 push @wrap, <<EOF;
742# define $func($v) ($call$test ? $true : ((errno == ERANGE) ? ($seent{$func} *) Perl_reentrant_retry("$func"$rv) : 0))
743EOF
744 }
745 } else {
746 push @wrap, <<EOF;
747# define $func($v) ($call$test ? $true : 0)
748EOF
749 }
750 }
751 push @wrap, <<EOF; # !defined(xxx) && XXX_R_PROTO == REENTRANT_PROTO_Y_TS
752# endif
753EOF
754 }
755
756 push @wrap, <<EOF; # defined(PERL_REENTR_API) && (PERL_REENTR_API+0 == 1)
757# endif
758EOF
759
760 push @wrap, $endif, "\n";
761 }
762}
763
764local $" = '';
765
766print $h <<EOF;
767
768/* Defines for indicating which special features are supported. */
769
770@define
771typedef struct {
772@struct
773 int dummy; /* cannot have empty structs */
774} REENTR;
775
776/* The wrappers. */
777
778@wrap
779
780#endif /* USE_REENTRANT_API */
781
782#endif
783EOF
784
785read_only_bottom_close_and_rename($h);
786
787# Prepare to write the reentr.c.
788
789my $c = open_print_header('reentr.c', <<'EOQ');
790 *
791 * "Saruman," I said, standing away from him, "only one hand at a time can
792 * wield the One, and you know that well, so do not trouble to say we!"
793 *
794 * This file contains a collection of automatically created wrappers
795 * (created by running reentr.pl) for reentrant (thread-safe) versions of
796 * various library calls, such as getpwent_r. The wrapping is done so
797 * that other files like pp_sys.c calling those library functions need not
798 * care about the differences between various platforms' idiosyncrasies
799 * regarding these reentrant interfaces.
800 */
801EOQ
802
803print $c <<"EOF";
804#include "EXTERN.h"
805#define PERL_IN_REENTR_C
806#include "perl.h"
807#include "reentr.h"
808
809void
810Perl_reentrant_size(pTHX) {
811#ifdef USE_REENTRANT_API
812#define REENTRANTSMALLSIZE 256 /* Make something up. */
813#define REENTRANTUSUALSIZE 4096 /* Make something up. */
814@size
815#endif /* USE_REENTRANT_API */
816}
817
818void
819Perl_reentrant_init(pTHX) {
820#ifdef USE_REENTRANT_API
821 Newx(PL_reentrant_buffer, 1, REENTR);
822 Perl_reentrant_size(aTHX);
823@init
824#endif /* USE_REENTRANT_API */
825}
826
827void
828Perl_reentrant_free(pTHX) {
829#ifdef USE_REENTRANT_API
830@free
831 Safefree(PL_reentrant_buffer);
832#endif /* USE_REENTRANT_API */
833}
834
835void*
836Perl_reentrant_retry(const char *f, ...)
837{
838 dTHX;
839 void *retptr = NULL;
840 va_list ap;
841#ifdef USE_REENTRANT_API
842 /* Easier to special case this here than in embed.pl. (Look at what it
843 generates for proto.h) */
844 PERL_ARGS_ASSERT_REENTRANT_RETRY;
845#endif
846 va_start(ap, f);
847 {
848#ifdef USE_REENTRANT_API
849# if defined(USE_HOSTENT_BUFFER) || defined(USE_GRENT_BUFFER) || defined(USE_NETENT_BUFFER) || defined(USE_PWENT_BUFFER) || defined(USE_PROTOENT_BUFFER) || defined(USE_SERVENT_BUFFER)
850 void *p0;
851# endif
852# if defined(USE_SERVENT_BUFFER)
853 void *p1;
854# endif
855# if defined(USE_HOSTENT_BUFFER)
856 size_t asize;
857# endif
858# if defined(USE_HOSTENT_BUFFER) || defined(USE_NETENT_BUFFER) || defined(USE_PROTOENT_BUFFER) || defined(USE_SERVENT_BUFFER)
859 int anint;
860# endif
861
862 switch (PL_op->op_type) {
863#ifdef USE_HOSTENT_BUFFER
864 case OP_GHBYADDR:
865 case OP_GHBYNAME:
866 case OP_GHOSTENT:
867 {
868#ifdef PERL_REENTRANT_MAXSIZE
869 if (PL_reentrant_buffer->_hostent_size <=
870 PERL_REENTRANT_MAXSIZE / 2)
871#endif
872 {
873 PL_reentrant_buffer->_hostent_size *= 2;
874 Renew(PL_reentrant_buffer->_hostent_buffer,
875 PL_reentrant_buffer->_hostent_size, char);
876 switch (PL_op->op_type) {
877 case OP_GHBYADDR:
878 p0 = va_arg(ap, void *);
879 asize = va_arg(ap, size_t);
880 anint = va_arg(ap, int);
881 retptr = gethostbyaddr(p0, asize, anint); break;
882 case OP_GHBYNAME:
883 p0 = va_arg(ap, void *);
884 retptr = gethostbyname((char *)p0); break;
885 case OP_GHOSTENT:
886 retptr = gethostent(); break;
887 default:
888 SETERRNO(ERANGE, LIB_INVARG);
889 break;
890 }
891 }
892 }
893 break;
894#endif
895#ifdef USE_GRENT_BUFFER
896 case OP_GGRNAM:
897 case OP_GGRGID:
898 case OP_GGRENT:
899 {
900#ifdef PERL_REENTRANT_MAXSIZE
901 if (PL_reentrant_buffer->_grent_size <=
902 PERL_REENTRANT_MAXSIZE / 2)
903#endif
904 {
905 Gid_t gid;
906 PL_reentrant_buffer->_grent_size *= 2;
907 Renew(PL_reentrant_buffer->_grent_buffer,
908 PL_reentrant_buffer->_grent_size, char);
909 switch (PL_op->op_type) {
910 case OP_GGRNAM:
911 p0 = va_arg(ap, void *);
912 retptr = getgrnam((char *)p0); break;
913 case OP_GGRGID:
914#if Gid_t_size < INTSIZE
915 gid = (Gid_t)va_arg(ap, int);
916#else
917 gid = va_arg(ap, Gid_t);
918#endif
919 retptr = getgrgid(gid); break;
920 case OP_GGRENT:
921 retptr = getgrent(); break;
922 default:
923 SETERRNO(ERANGE, LIB_INVARG);
924 break;
925 }
926 }
927 }
928 break;
929#endif
930#ifdef USE_NETENT_BUFFER
931 case OP_GNBYADDR:
932 case OP_GNBYNAME:
933 case OP_GNETENT:
934 {
935#ifdef PERL_REENTRANT_MAXSIZE
936 if (PL_reentrant_buffer->_netent_size <=
937 PERL_REENTRANT_MAXSIZE / 2)
938#endif
939 {
940 Netdb_net_t net;
941 PL_reentrant_buffer->_netent_size *= 2;
942 Renew(PL_reentrant_buffer->_netent_buffer,
943 PL_reentrant_buffer->_netent_size, char);
944 switch (PL_op->op_type) {
945 case OP_GNBYADDR:
946 net = va_arg(ap, Netdb_net_t);
947 anint = va_arg(ap, int);
948 retptr = getnetbyaddr(net, anint); break;
949 case OP_GNBYNAME:
950 p0 = va_arg(ap, void *);
951 retptr = getnetbyname((char *)p0); break;
952 case OP_GNETENT:
953 retptr = getnetent(); break;
954 default:
955 SETERRNO(ERANGE, LIB_INVARG);
956 break;
957 }
958 }
959 }
960 break;
961#endif
962#ifdef USE_PWENT_BUFFER
963 case OP_GPWNAM:
964 case OP_GPWUID:
965 case OP_GPWENT:
966 {
967#ifdef PERL_REENTRANT_MAXSIZE
968 if (PL_reentrant_buffer->_pwent_size <=
969 PERL_REENTRANT_MAXSIZE / 2)
970#endif
971 {
972 Uid_t uid;
973 PL_reentrant_buffer->_pwent_size *= 2;
974 Renew(PL_reentrant_buffer->_pwent_buffer,
975 PL_reentrant_buffer->_pwent_size, char);
976 switch (PL_op->op_type) {
977 case OP_GPWNAM:
978 p0 = va_arg(ap, void *);
979 retptr = getpwnam((char *)p0); break;
980 case OP_GPWUID:
981#if Uid_t_size < INTSIZE
982 uid = (Uid_t)va_arg(ap, int);
983#else
984 uid = va_arg(ap, Uid_t);
985#endif
986 retptr = getpwuid(uid); break;
987 case OP_GPWENT:
988 retptr = getpwent(); break;
989 default:
990 SETERRNO(ERANGE, LIB_INVARG);
991 break;
992 }
993 }
994 }
995 break;
996#endif
997#ifdef USE_PROTOENT_BUFFER
998 case OP_GPBYNAME:
999 case OP_GPBYNUMBER:
1000 case OP_GPROTOENT:
1001 {
1002#ifdef PERL_REENTRANT_MAXSIZE
1003 if (PL_reentrant_buffer->_protoent_size <=
1004 PERL_REENTRANT_MAXSIZE / 2)
1005#endif
1006 {
1007 PL_reentrant_buffer->_protoent_size *= 2;
1008 Renew(PL_reentrant_buffer->_protoent_buffer,
1009 PL_reentrant_buffer->_protoent_size, char);
1010 switch (PL_op->op_type) {
1011 case OP_GPBYNAME:
1012 p0 = va_arg(ap, void *);
1013 retptr = getprotobyname((char *)p0); break;
1014 case OP_GPBYNUMBER:
1015 anint = va_arg(ap, int);
1016 retptr = getprotobynumber(anint); break;
1017 case OP_GPROTOENT:
1018 retptr = getprotoent(); break;
1019 default:
1020 SETERRNO(ERANGE, LIB_INVARG);
1021 break;
1022 }
1023 }
1024 }
1025 break;
1026#endif
1027#ifdef USE_SERVENT_BUFFER
1028 case OP_GSBYNAME:
1029 case OP_GSBYPORT:
1030 case OP_GSERVENT:
1031 {
1032#ifdef PERL_REENTRANT_MAXSIZE
1033 if (PL_reentrant_buffer->_servent_size <=
1034 PERL_REENTRANT_MAXSIZE / 2)
1035#endif
1036 {
1037 PL_reentrant_buffer->_servent_size *= 2;
1038 Renew(PL_reentrant_buffer->_servent_buffer,
1039 PL_reentrant_buffer->_servent_size, char);
1040 switch (PL_op->op_type) {
1041 case OP_GSBYNAME:
1042 p0 = va_arg(ap, void *);
1043 p1 = va_arg(ap, void *);
1044 retptr = getservbyname((char *)p0, (char *)p1); break;
1045 case OP_GSBYPORT:
1046 anint = va_arg(ap, int);
1047 p0 = va_arg(ap, void *);
1048 retptr = getservbyport(anint, (char *)p0); break;
1049 case OP_GSERVENT:
1050 retptr = getservent(); break;
1051 default:
1052 SETERRNO(ERANGE, LIB_INVARG);
1053 break;
1054 }
1055 }
1056 }
1057 break;
1058#endif
1059 default:
1060 /* Not known how to retry, so just fail. */
1061 break;
1062 }
1063#else
1064 PERL_UNUSED_ARG(f);
1065#endif
1066 }
1067 va_end(ap);
1068 return retptr;
1069}
1070EOF
1071
1072read_only_bottom_close_and_rename($c);
1073
1074__DATA__
1075asctime S |time |const struct tm|B_SB|B_SBI|I_SB|I_SBI
1076crypt CC |crypt |struct crypt_data|B_CCS|B_CCD|D=CRYPTD*
1077ctermid B |stdio | |B_B
1078ctime S |time |const time_t |B_SB|B_SBI|I_SB|I_SBI
1079drand48 |stdlib |struct drand48_data |I_ST|T=double*
1080endgrent |grp | |I_H|V_H
1081endhostent |netdb | |I_D|V_D|D=struct hostent_data*
1082endnetent |netdb | |I_D|V_D|D=struct netent_data*
1083endprotoent |netdb | |I_D|V_D|D=struct protoent_data*
1084endpwent |pwd | |I_H|V_H
1085endservent |netdb | |I_D|V_D|D=struct servent_data*
1086getgrent |grp |struct group |I_SBWR|I_SBIR|S_SBW|S_SBI|I_SBI|I_SBIH
1087getgrgid T |grp |struct group |I_TSBWR|I_TSBIR|I_TSBI|S_TSBI|T=gid_t
1088getgrnam C |grp |struct group |I_CSBWR|I_CSBIR|S_CBI|I_CSBI|S_CSBI
1089gethostbyaddr CWI |netdb |struct hostent |I_CWISBWRE|S_CWISBWIE|S_CWISBIE|S_TWISBIE|S_CIISBIE|S_CSBIE|S_TSBIE|I_CWISD|I_CIISD|I_CII|I_TsISBWRE|D=struct hostent_data*|T=const void*|s=socklen_t
1090gethostbyname C |netdb |struct hostent |I_CSBWRE|S_CSBIE|I_CSD|D=struct hostent_data*
1091gethostent |netdb |struct hostent |I_SBWRE|I_SBIE|S_SBIE|S_SBI|I_SBI|I_SD|D=struct hostent_data*
1092getlogin |unistd |char |I_BW|I_BI|B_BW|B_BI
1093getnetbyaddr LI |netdb |struct netent |I_UISBWRE|I_LISBI|S_TISBI|S_LISBI|I_TISD|I_LISD|I_IISD|I_uISBWRE|D=struct netent_data*|T=in_addr_t|U=unsigned long|u=uint32_t
1094getnetbyname C |netdb |struct netent |I_CSBWRE|I_CSBI|S_CSBI|I_CSD|D=struct netent_data*
1095getnetent |netdb |struct netent |I_SBWRE|I_SBIE|S_SBIE|S_SBI|I_SBI|I_SD|D=struct netent_data*
1096getprotobyname C|netdb |struct protoent|I_CSBWR|S_CSBI|I_CSD|D=struct protoent_data*
1097getprotobynumber I |netdb |struct protoent|I_ISBWR|S_ISBI|I_ISD|D=struct protoent_data*
1098getprotoent |netdb |struct protoent|I_SBWR|I_SBI|S_SBI|I_SD|D=struct protoent_data*
1099getpwent |pwd |struct passwd |I_SBWR|I_SBIR|S_SBW|S_SBI|I_SBI|I_SBIH
1100getpwnam C |pwd |struct passwd |I_CSBWR|I_CSBIR|S_CSBI|I_CSBI
1101getpwuid T |pwd |struct passwd |I_TSBWR|I_TSBIR|I_TSBI|S_TSBI|T=uid_t
1102getservbyname CC|netdb |struct servent |I_CCSBWR|S_CCSBI|I_CCSD|D=struct servent_data*
1103getservbyport IC|netdb |struct servent |I_ICSBWR|S_ICSBI|I_ICSD|D=struct servent_data*
1104getservent |netdb |struct servent |I_SBWR|I_SBI|S_SBI|I_SD|D=struct servent_data*
1105getspnam C |shadow |struct spwd |I_CSBWR|S_CSBI
1106random |stdlib |struct random_data|I_iS|I_lS|I_St|i=int*|l=long*|t=int32_t*
1107readdir T |dirent |struct dirent |I_TSR|I_TS|T=DIR*
1108readdir64 T |dirent |struct dirent64|I_TSR|I_TS|T=DIR*
1109setgrent |grp | |I_H|V_H
1110sethostent I |netdb | |I_ID|V_ID|D=struct hostent_data*
1111setlocale IC |locale | |I_ICBI
1112setnetent I |netdb | |I_ID|V_ID|D=struct netent_data*
1113setprotoent I |netdb | |I_ID|V_ID|D=struct protoent_data*
1114setpwent |pwd | |I_H|V_H
1115setservent I |netdb | |I_ID|V_ID|D=struct servent_data*
1116srand48 L |stdlib |struct drand48_data |I_LS
1117srandom T |stdlib |struct random_data|I_TS|T=unsigned int
1118strerror I |string | |I_IBW|I_IBI|B_IBW
1119tmpnam B |stdio | |B_B
1120ttyname I |unistd | |I_IBW|I_IBI|B_IBI