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