This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Make use of *at functions dependent upon HAS_LINKAT.
[perl5.git] / regen / reentr.pl
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
19 BEGIN {
20     # Get function prototypes
21     require './regen/regen_lib.pl';
22 }
23
24 use strict;
25 use Getopt::Std;
26 my %opts;
27 getopts('Uv', \%opts);
28
29 my %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
53 sub 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
63 my $h = open_print_header('reentr.h');
64 print $h <<EOF;
65 #ifndef PERL_REENTR_H_
66 #define PERL_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_ENDGRENT_R
95 #   undef HAS_ENDPWENT_R
96 #   undef HAS_GETGRENT_R
97 #   undef HAS_GETPWENT_R
98 #   undef HAS_SETLOCALE_R
99 #   undef HAS_STRERROR_R
100 #   define NETDB_R_OBSOLETE
101 #endif
102
103 #if defined(__osf__) && defined(__alpha) /* Tru64 aka Digital UNIX */
104 #   undef HAS_CRYPT_R
105 #   undef HAS_STRERROR_R
106 #   define NETDB_R_OBSOLETE
107 #endif
108
109 #if defined(__GLIBC__) && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 24))
110 #   undef HAS_READDIR_R
111 #   undef HAS_READDIR64_R
112 #endif
113
114 /*
115  * As of OpenBSD 3.7, reentrant functions are now working, they just are
116  * incompatible with everyone else.  To make OpenBSD happy, we have to
117  * memzero out certain structures before calling the functions.
118  */
119 #if defined(__OpenBSD__)
120 #    define REENTR_MEMZERO(a,b) memzero(a,b)
121 #else
122 #    define REENTR_MEMZERO(a,b) 0
123 #endif 
124
125 #ifdef NETDB_R_OBSOLETE
126 #   undef HAS_ENDHOSTENT_R
127 #   undef HAS_ENDNETENT_R
128 #   undef HAS_ENDPROTOENT_R
129 #   undef HAS_ENDSERVENT_R
130 #   undef HAS_GETHOSTBYADDR_R
131 #   undef HAS_GETHOSTBYNAME_R
132 #   undef HAS_GETHOSTENT_R
133 #   undef HAS_GETNETBYADDR_R
134 #   undef HAS_GETNETBYNAME_R
135 #   undef HAS_GETNETENT_R
136 #   undef HAS_GETPROTOBYNAME_R
137 #   undef HAS_GETPROTOBYNUMBER_R
138 #   undef HAS_GETPROTOENT_R
139 #   undef HAS_GETSERVBYNAME_R
140 #   undef HAS_GETSERVBYPORT_R
141 #   undef HAS_GETSERVENT_R
142 #   undef HAS_SETHOSTENT_R
143 #   undef HAS_SETNETENT_R
144 #   undef HAS_SETPROTOENT_R
145 #   undef HAS_SETSERVENT_R
146 #endif
147
148 #ifdef I_PWD
149 #   include <pwd.h>
150 #endif
151 #ifdef I_GRP
152 #   include <grp.h>
153 #endif
154 #ifdef I_NETDB
155 #   include <netdb.h>
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
168 EOF
169
170 my %seenh; # the different prototypes signatures for this function
171 my %seena; # the different prototypes signatures for this function in order
172 my @seenf; # all the seen functions
173 my %seenp; # the different prototype signatures for all functions
174 my %seent; # the return type of this function
175 my %seens; # the type of this function's "S"
176 my %seend; # the type of this function's "D"
177 my %seenm; # all the types
178 my %seenu; # the length of the argument list of this function
179
180 while (<DATA>) { # Read in the protoypes.
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
268 set ${func}_r d_${func}_r
269 eval \$inlibc
270 case "\$d_${func}_r" in
271 "\$define")
272 EOF
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)
283 EOF
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
294 EOF
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         ;;
327 esac
328
329 EOF
330         close(U);                   
331     }
332 }
333
334 close 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
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 =~ /^(getgrnam|getpwnam|getspnam)$/) {
508             pushssif $ifdef;
509             # 'genfunc' can be read either as 'generic' or 'genre',
510             # it represents a group of functions.
511             my $genfunc = $func;
512             $genfunc =~ s/nam/ent/g;
513             $genfunc =~ s/^get//;
514             my $GENFUNC = uc $genfunc;
515             push @struct, <<EOF;
516         $seent{$func}   _${genfunc}_struct;
517         char*   _${genfunc}_buffer;
518         size_t  _${genfunc}_size;
519 EOF
520             push @struct, <<EOF;
521 #   ifdef USE_${GENFUNC}_PTR
522         $seent{$func}*  _${genfunc}_ptr;
523 #   endif
524 EOF
525             push @struct, <<EOF;
526 #   ifdef USE_${GENFUNC}_FPTR
527         FILE*   _${genfunc}_fptr;
528 #   endif
529 EOF
530             push @init, <<EOF;
531 #   ifdef USE_${GENFUNC}_FPTR
532         PL_reentrant_buffer->_${genfunc}_fptr = NULL;
533 #   endif
534 EOF
535             my $sc = $genfunc eq 'grent' ?
536                     '_SC_GETGR_R_SIZE_MAX' : '_SC_GETPW_R_SIZE_MAX';
537             my $sz = "_${genfunc}_size";
538             push @size, <<EOF;
539 #   if defined(HAS_SYSCONF) && defined($sc) && !defined(__GLIBC__)
540         PL_reentrant_buffer->$sz = sysconf($sc);
541         if (PL_reentrant_buffer->$sz == (size_t) -1)
542                 PL_reentrant_buffer->$sz = REENTRANTUSUALSIZE;
543 #   else
544 #       if defined(__osf__) && defined(__alpha) && defined(SIABUFSIZ)
545         PL_reentrant_buffer->$sz = SIABUFSIZ;
546 #       else
547 #           ifdef __sgi
548         PL_reentrant_buffer->$sz = BUFSIZ;
549 #           else
550         PL_reentrant_buffer->$sz = REENTRANTUSUALSIZE;
551 #           endif
552 #       endif
553 #   endif 
554 EOF
555             pushinitfree $genfunc;
556             pushssif $endif;
557         }
558         elsif ($func =~ /^(gethostbyname|getnetbyname|getservbyname|getprotobyname)$/) {
559             pushssif $ifdef;
560             my $genfunc = $func;
561             $genfunc =~ s/byname/ent/;
562             $genfunc =~ s/^get//;
563             my $GENFUNC = uc $genfunc;
564             my $D = ifprotomatch($FUNC, grep {/D/} @p);
565             my $d = $seend{$func};
566             $d =~ s/\*$//; # snip: we need the base type.
567             push @struct, <<EOF;
568         $seent{$func}   _${genfunc}_struct;
569 #   if $D
570         $d      _${genfunc}_data;
571 #   else
572         char*   _${genfunc}_buffer;
573         size_t  _${genfunc}_size;
574 #   endif
575 #   ifdef USE_${GENFUNC}_PTR
576         $seent{$func}*  _${genfunc}_ptr;
577 #   endif
578 EOF
579             push @struct, <<EOF;
580 #   ifdef USE_${GENFUNC}_ERRNO
581         int     _${genfunc}_errno;
582 #   endif 
583 EOF
584             push @size, <<EOF;
585 #if   !($D)
586         PL_reentrant_buffer->_${genfunc}_size = REENTRANTUSUALSIZE;
587 #endif
588 EOF
589             push @init, <<EOF;
590 #if   !($D)
591         Newx(PL_reentrant_buffer->_${genfunc}_buffer, PL_reentrant_buffer->_${genfunc}_size, char);
592 #endif
593 EOF
594             push @free, <<EOF;
595 #if   !($D)
596         Safefree(PL_reentrant_buffer->_${genfunc}_buffer);
597 #endif
598 EOF
599             pushssif $endif;
600         }
601         elsif ($func =~ /^(readdir|readdir64)$/) {
602             pushssif $ifdef;
603             my $R = ifprotomatch($FUNC, grep {/R/} @p);
604             push @struct, <<EOF;
605         $seent{$func}*  _${func}_struct;
606         size_t  _${func}_size;
607 #   if $R
608         $seent{$func}*  _${func}_ptr;
609 #   endif
610 EOF
611             push @size, <<EOF;
612         /* This is the size Solaris recommends.
613          * (though we go static, should use pathconf() instead) */
614         PL_reentrant_buffer->_${func}_size = sizeof($seent{$func}) + MAXPATHLEN + 1;
615 EOF
616             push @init, <<EOF;
617         PL_reentrant_buffer->_${func}_struct = ($seent{$func}*)safemalloc(PL_reentrant_buffer->_${func}_size);
618 EOF
619             push @free, <<EOF;
620         Safefree(PL_reentrant_buffer->_${func}_struct);
621 EOF
622             pushssif $endif;
623         }
624
625         push @wrap, $ifdef;
626
627         push @wrap, <<EOF;
628 #  if defined(PERL_REENTR_API) && (PERL_REENTR_API+0 == 1)
629 #   undef $func
630 EOF
631
632         # Write out what we have learned.
633         
634         my @v = 'a'..'z';
635         my $v = join(", ", @v[0..$seenu{$func}-1]);
636         for my $p (@p) {
637             my ($r, $a) = split '_', $p;
638             my $test = $r eq 'I' ? ' == 0' : '';
639             my $true  = 1;
640             my $genfunc = $func;
641             if ($genfunc =~ /^(?:get|set|end)(pw|gr|host|net|proto|serv|sp)/) {
642                 $genfunc = "${1}ent";
643             }
644             my $b = $a;
645             my $w = '';
646             substr($b, 0, $seenu{$func}) = '';
647             if ($b =~ /R/) {
648                 $true = "PL_reentrant_buffer->_${genfunc}_ptr";
649             } elsif ($b =~ /S/) {
650                 if ($func =~ /^readdir/) {
651                     $true = "PL_reentrant_buffer->_${genfunc}_struct";
652                 } else {
653                     $true = "&PL_reentrant_buffer->_${genfunc}_struct";
654                 }
655             } elsif ($b =~ /B/) {
656                 $true = "PL_reentrant_buffer->_${genfunc}_buffer";
657             }
658             if (length $b) {
659                 $w = join ", ",
660                          map {
661                              $_ eq 'R' ?
662                                  "&PL_reentrant_buffer->_${genfunc}_ptr" :
663                              $_ eq 'E' ?
664                                  "&PL_reentrant_buffer->_${genfunc}_errno" :
665                              $_ eq 'B' ?
666                                  "PL_reentrant_buffer->_${genfunc}_buffer" :
667                              $_ =~ /^[WI]$/ ?
668                                  "PL_reentrant_buffer->_${genfunc}_size" :
669                              $_ eq 'H' ?
670                                  "&PL_reentrant_buffer->_${genfunc}_fptr" :
671                              $_ eq 'D' ?
672                                  "&PL_reentrant_buffer->_${genfunc}_data" :
673                              $_ eq 'S' ?
674                                  ($func =~ /^readdir\d*$/ ?
675                                   "PL_reentrant_buffer->_${genfunc}_struct" :
676                                   $func =~ /^crypt$/ ?
677                                   "PL_reentrant_buffer->_${genfunc}_struct_buffer" :
678                                   "&PL_reentrant_buffer->_${genfunc}_struct") :
679                                  $_
680                          } split '', $b;
681                 $w = ", $w" if length $v;
682             }
683
684             my $call = "${func}_r($v$w)";
685
686             # Must make OpenBSD happy
687             my $memzero = '';
688             if($p =~ /D$/ &&
689                 ($genfunc eq 'protoent' || $genfunc eq 'servent')) {
690                 $memzero = 'REENTR_MEMZERO(&PL_reentrant_buffer->_' . $genfunc . '_data, sizeof(PL_reentrant_buffer->_' . $genfunc . '_data)),';
691             }
692             push @wrap, <<EOF;
693 #   if !defined($func) && ${FUNC}_R_PROTO == REENTRANT_PROTO_$p
694 EOF
695             if ($r eq 'V' || $r eq 'B') {
696                 push @wrap, <<EOF;
697 #       define $func($v) $call
698 EOF
699             } else {
700                 if ($func =~ /^get/) {
701                     my $rv = $v ? ", $v" : "";
702                     if ($r eq 'I') {
703                         push @wrap, <<EOF;
704 #       define $func($v) ($memzero(PL_reentrant_retint = $call)$test ? $true : ((PL_reentrant_retint == ERANGE) ? ($seent{$func} *) Perl_reentrant_retry("$func"$rv) : 0))
705 EOF
706                     } else {
707                         push @wrap, <<EOF;
708 #       define $func($v) ($call$test ? $true : ((errno == ERANGE) ? ($seent{$func} *) Perl_reentrant_retry("$func"$rv) : 0))
709 EOF
710                     }
711                 } else {
712                     push @wrap, <<EOF;
713 #       define $func($v) ($call$test ? $true : 0)
714 EOF
715                 }
716             }
717             push @wrap, <<EOF;  # !defined(xxx) && XXX_R_PROTO == REENTRANT_PROTO_Y_TS
718 #   endif
719 EOF
720         }
721
722             push @wrap, <<EOF;  # defined(PERL_REENTR_API) && (PERL_REENTR_API+0 == 1)
723 #  endif
724 EOF
725
726         push @wrap, $endif, "\n";
727     }
728 }
729
730 local $" = '';
731
732 print $h <<EOF;
733
734 /* Defines for indicating which special features are supported. */
735
736 @define
737 typedef struct {
738 @struct
739     int dummy; /* cannot have empty structs */
740 } REENTR;
741
742 /* The wrappers. */
743
744 @wrap
745
746 #endif /* USE_REENTRANT_API */
747  
748 #endif
749 EOF
750
751 read_only_bottom_close_and_rename($h);
752
753 # Prepare to write the reentr.c.
754
755 my $c = open_print_header('reentr.c', <<'EOQ');
756  */
757
758 /*
759  * "Saruman," I said, standing away from him, "only one hand at a time can
760  *  wield the One, and you know that well, so do not trouble to say we!"
761  *
762  *     [p.260 of _The Lord of the Rings_, II/ii: "The Council of Elrond"]
763  */
764
765 /*
766  * This file contains a collection of automatically created wrappers
767  * (created by running reentr.pl) for reentrant (thread-safe) versions of
768  * various library calls, such as getpwent_r.  The wrapping is done so
769  * that other files like pp_sys.c calling those library functions need not
770  * care about the differences between various platforms' idiosyncrasies
771  * regarding these reentrant interfaces.  
772  */
773 EOQ
774
775 print $c <<"EOF";
776 #include "EXTERN.h"
777 #define PERL_IN_REENTR_C
778 #include "perl.h"
779 #include "reentr.h"
780
781 #define RenewDouble(data_pointer, size_pointer, type) \\
782     STMT_START { \\
783         const size_t size = *(size_pointer) * 2; \\
784         Renew((data_pointer), (size), type); \\
785         *(size_pointer) = size; \\
786     } STMT_END
787
788 void
789 Perl_reentrant_size(pTHX) {
790         PERL_UNUSED_CONTEXT;
791 #ifdef USE_REENTRANT_API
792 #define REENTRANTSMALLSIZE       256    /* Make something up. */
793 #define REENTRANTUSUALSIZE      4096    /* Make something up. */
794 @size
795 #endif /* USE_REENTRANT_API */
796 }
797
798 void
799 Perl_reentrant_init(pTHX) {
800         PERL_UNUSED_CONTEXT;
801 #ifdef USE_REENTRANT_API
802         Newx(PL_reentrant_buffer, 1, REENTR);
803         Perl_reentrant_size(aTHX);
804 @init
805 #endif /* USE_REENTRANT_API */
806 }
807
808 void
809 Perl_reentrant_free(pTHX) {
810         PERL_UNUSED_CONTEXT;
811 #ifdef USE_REENTRANT_API
812 @free
813         Safefree(PL_reentrant_buffer);
814 #endif /* USE_REENTRANT_API */
815 }
816
817 void*
818 Perl_reentrant_retry(const char *f, ...)
819 {
820     void *retptr = NULL;
821     va_list ap;
822 #ifdef USE_REENTRANT_API
823     dTHX;
824     /* Easier to special case this here than in embed.pl. (Look at what it
825        generates for proto.h) */
826     PERL_ARGS_ASSERT_REENTRANT_RETRY;
827 #endif
828     va_start(ap, f);
829     {
830 #ifdef USE_REENTRANT_API
831 #  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)
832     void *p0;
833 #  endif
834 #  if defined(USE_SERVENT_BUFFER)
835     void *p1;
836 #  endif
837 #  if defined(USE_HOSTENT_BUFFER)
838     size_t asize;
839 #  endif
840 #  if defined(USE_HOSTENT_BUFFER) || defined(USE_NETENT_BUFFER) || defined(USE_PROTOENT_BUFFER) || defined(USE_SERVENT_BUFFER)
841     int anint;
842 #  endif
843
844     switch (PL_op->op_type) {
845 #ifdef USE_HOSTENT_BUFFER
846     case OP_GHBYADDR:
847     case OP_GHBYNAME:
848     case OP_GHOSTENT:
849         {
850 #ifdef PERL_REENTRANT_MAXSIZE
851             if (PL_reentrant_buffer->_hostent_size <=
852                 PERL_REENTRANT_MAXSIZE / 2)
853 #endif
854             {
855                 RenewDouble(PL_reentrant_buffer->_hostent_buffer,
856                         &PL_reentrant_buffer->_hostent_size, char);
857                 switch (PL_op->op_type) {
858                 case OP_GHBYADDR:
859                     p0    = va_arg(ap, void *);
860                     asize = va_arg(ap, size_t);
861                     anint  = va_arg(ap, int);
862                     retptr = gethostbyaddr(p0, asize, anint); break;
863                 case OP_GHBYNAME:
864                     p0 = va_arg(ap, void *);
865                     retptr = gethostbyname((char *)p0); break;
866                 case OP_GHOSTENT:
867                     retptr = gethostent(); break;
868                 default:
869                     SETERRNO(ERANGE, LIB_INVARG);
870                     break;
871                 }
872             }
873         }
874         break;
875 #endif
876 #ifdef USE_GRENT_BUFFER
877     case OP_GGRNAM:
878     case OP_GGRGID:
879     case OP_GGRENT:
880         {
881 #ifdef PERL_REENTRANT_MAXSIZE
882             if (PL_reentrant_buffer->_grent_size <=
883                 PERL_REENTRANT_MAXSIZE / 2)
884 #endif
885             {
886                 Gid_t gid;
887                 RenewDouble(PL_reentrant_buffer->_grent_buffer,
888                       &PL_reentrant_buffer->_grent_size, char);
889                 switch (PL_op->op_type) {
890                 case OP_GGRNAM:
891                     p0 = va_arg(ap, void *);
892                     retptr = getgrnam((char *)p0); break;
893                 case OP_GGRGID:
894 #if Gid_t_size < INTSIZE
895                     gid = (Gid_t)va_arg(ap, int);
896 #else
897                     gid = va_arg(ap, Gid_t);
898 #endif
899                     retptr = getgrgid(gid); break;
900                 case OP_GGRENT:
901                     retptr = getgrent(); break;
902                 default:
903                     SETERRNO(ERANGE, LIB_INVARG);
904                     break;
905                 }
906             }
907         }
908         break;
909 #endif
910 #ifdef USE_NETENT_BUFFER
911     case OP_GNBYADDR:
912     case OP_GNBYNAME:
913     case OP_GNETENT:
914         {
915 #ifdef PERL_REENTRANT_MAXSIZE
916             if (PL_reentrant_buffer->_netent_size <=
917                 PERL_REENTRANT_MAXSIZE / 2)
918 #endif
919             {
920                 Netdb_net_t net;
921                 RenewDouble(PL_reentrant_buffer->_netent_buffer,
922                       &PL_reentrant_buffer->_netent_size, char);
923                 switch (PL_op->op_type) {
924                 case OP_GNBYADDR:
925                     net = va_arg(ap, Netdb_net_t);
926                     anint = va_arg(ap, int);
927                     retptr = getnetbyaddr(net, anint); break;
928                 case OP_GNBYNAME:
929                     p0 = va_arg(ap, void *);
930                     retptr = getnetbyname((char *)p0); break;
931                 case OP_GNETENT:
932                     retptr = getnetent(); break;
933                 default:
934                     SETERRNO(ERANGE, LIB_INVARG);
935                     break;
936                 }
937             }
938         }
939         break;
940 #endif
941 #ifdef USE_PWENT_BUFFER
942     case OP_GPWNAM:
943     case OP_GPWUID:
944     case OP_GPWENT:
945         {
946 #ifdef PERL_REENTRANT_MAXSIZE
947             if (PL_reentrant_buffer->_pwent_size <=
948                 PERL_REENTRANT_MAXSIZE / 2)
949 #endif
950             {
951                 Uid_t uid;
952                 RenewDouble(PL_reentrant_buffer->_pwent_buffer,
953                       &PL_reentrant_buffer->_pwent_size, char);
954                 switch (PL_op->op_type) {
955                 case OP_GPWNAM:
956                     p0 = va_arg(ap, void *);
957                     retptr = getpwnam((char *)p0); break;
958                 case OP_GPWUID:
959 #if Uid_t_size < INTSIZE
960                     uid = (Uid_t)va_arg(ap, int);
961 #else
962                     uid = va_arg(ap, Uid_t);
963 #endif
964                     retptr = getpwuid(uid); break;
965 #if defined(HAS_GETPWENT) || defined(HAS_GETPWENT_R)
966                 case OP_GPWENT:
967                     retptr = getpwent(); break;
968 #endif
969                 default:
970                     SETERRNO(ERANGE, LIB_INVARG);
971                     break;
972                 }
973             }
974         }
975         break;
976 #endif
977 #ifdef USE_PROTOENT_BUFFER
978     case OP_GPBYNAME:
979     case OP_GPBYNUMBER:
980     case OP_GPROTOENT:
981         {
982 #ifdef PERL_REENTRANT_MAXSIZE
983             if (PL_reentrant_buffer->_protoent_size <=
984                 PERL_REENTRANT_MAXSIZE / 2)
985 #endif
986             {
987                 RenewDouble(PL_reentrant_buffer->_protoent_buffer,
988                       &PL_reentrant_buffer->_protoent_size, char);
989                 switch (PL_op->op_type) {
990                 case OP_GPBYNAME:
991                     p0 = va_arg(ap, void *);
992                     retptr = getprotobyname((char *)p0); break;
993                 case OP_GPBYNUMBER:
994                     anint = va_arg(ap, int);
995                     retptr = getprotobynumber(anint); break;
996                 case OP_GPROTOENT:
997                     retptr = getprotoent(); break;
998                 default:
999                     SETERRNO(ERANGE, LIB_INVARG);
1000                     break;
1001                 }
1002             }
1003         }
1004         break;
1005 #endif
1006 #ifdef USE_SERVENT_BUFFER
1007     case OP_GSBYNAME:
1008     case OP_GSBYPORT:
1009     case OP_GSERVENT:
1010         {
1011 #ifdef PERL_REENTRANT_MAXSIZE
1012             if (PL_reentrant_buffer->_servent_size <=
1013                 PERL_REENTRANT_MAXSIZE / 2)
1014 #endif
1015             {
1016                 RenewDouble(PL_reentrant_buffer->_servent_buffer,
1017                       &PL_reentrant_buffer->_servent_size, char);
1018                 switch (PL_op->op_type) {
1019                 case OP_GSBYNAME:
1020                     p0 = va_arg(ap, void *);
1021                     p1 = va_arg(ap, void *);
1022                     retptr = getservbyname((char *)p0, (char *)p1); break;
1023                 case OP_GSBYPORT:
1024                     anint = va_arg(ap, int);
1025                     p0 = va_arg(ap, void *);
1026                     retptr = getservbyport(anint, (char *)p0); break;
1027                 case OP_GSERVENT:
1028                     retptr = getservent(); break;
1029                 default:
1030                     SETERRNO(ERANGE, LIB_INVARG);
1031                     break;
1032                 }
1033             }
1034         }
1035         break;
1036 #endif
1037     default:
1038         /* Not known how to retry, so just fail. */
1039         break;
1040     }
1041 #else
1042     PERL_UNUSED_ARG(f);
1043 #endif
1044     }
1045     va_end(ap);
1046     return retptr;
1047 }
1048 EOF
1049
1050 read_only_bottom_close_and_rename($c);
1051
1052 __DATA__
1053 asctime S       |time   |const struct tm|B_SB|B_SBI|I_SB|I_SBI
1054 crypt CC        |crypt  |struct crypt_data|B_CCS|B_CCD|D=CRYPTD*
1055 ctermid B       |stdio  |               |B_B
1056 ctime S         |time   |const time_t   |B_SB|B_SBI|I_SB|I_SBI
1057 endgrent        |grp    |               |I_H|V_H
1058 endhostent      |netdb  |               |I_D|V_D|D=struct hostent_data*
1059 endnetent       |netdb  |               |I_D|V_D|D=struct netent_data*
1060 endprotoent     |netdb  |               |I_D|V_D|D=struct protoent_data*
1061 endpwent        |pwd    |               |I_H|V_H
1062 endservent      |netdb  |               |I_D|V_D|D=struct servent_data*
1063 getgrent        |grp    |struct group   |I_SBWR|I_SBIR|S_SBW|S_SBI|I_SBI|I_SBIH
1064 getgrgid T      |grp    |struct group   |I_TSBWR|I_TSBIR|I_TSBI|S_TSBI|T=gid_t
1065 getgrnam C      |grp    |struct group   |I_CSBWR|I_CSBIR|S_CBI|I_CSBI|S_CSBI
1066 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
1067 gethostbyname C |netdb  |struct hostent |I_CSBWRE|S_CSBIE|I_CSD|D=struct hostent_data*
1068 gethostent      |netdb  |struct hostent |I_SBWRE|I_SBIE|S_SBIE|S_SBI|I_SBI|I_SD|D=struct hostent_data*
1069 getlogin        |unistd |char           |I_BW|I_BI|B_BW|B_BI
1070 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
1071 getnetbyname C  |netdb  |struct netent  |I_CSBWRE|I_CSBI|S_CSBI|I_CSD|D=struct netent_data*
1072 getnetent       |netdb  |struct netent  |I_SBWRE|I_SBIE|S_SBIE|S_SBI|I_SBI|I_SD|D=struct netent_data*
1073 getprotobyname C|netdb  |struct protoent|I_CSBWR|S_CSBI|I_CSD|D=struct protoent_data*
1074 getprotobynumber I      |netdb  |struct protoent|I_ISBWR|S_ISBI|I_ISD|D=struct protoent_data*
1075 getprotoent     |netdb  |struct protoent|I_SBWR|I_SBI|S_SBI|I_SD|D=struct protoent_data*
1076 getpwent        |pwd    |struct passwd  |I_SBWR|I_SBIR|S_SBW|S_SBI|I_SBI|I_SBIH
1077 getpwnam C      |pwd    |struct passwd  |I_CSBWR|I_CSBIR|S_CSBI|I_CSBI
1078 getpwuid T      |pwd    |struct passwd  |I_TSBWR|I_TSBIR|I_TSBI|S_TSBI|T=uid_t
1079 getservbyname CC|netdb  |struct servent |I_CCSBWR|S_CCSBI|I_CCSD|D=struct servent_data*
1080 getservbyport IC|netdb  |struct servent |I_ICSBWR|S_ICSBI|I_ICSD|D=struct servent_data*
1081 getservent      |netdb  |struct servent |I_SBWR|I_SBI|S_SBI|I_SD|D=struct servent_data*
1082 getspnam C      |shadow |struct spwd    |I_CSBWR|S_CSBI
1083 readdir T       |dirent |struct dirent  |I_TSR|I_TS|T=DIR*
1084 readdir64 T     |dirent |struct dirent64|I_TSR|I_TS|T=DIR*
1085 setgrent        |grp    |               |I_H|V_H
1086 sethostent I    |netdb  |               |I_ID|V_ID|D=struct hostent_data*
1087 setlocale IC    |locale |               |I_ICBI
1088 setnetent I     |netdb  |               |I_ID|V_ID|D=struct netent_data*
1089 setprotoent I   |netdb  |               |I_ID|V_ID|D=struct protoent_data*
1090 setpwent        |pwd    |               |I_H|V_H
1091 setservent I    |netdb  |               |I_ID|V_ID|D=struct servent_data*
1092 strerror I      |string |               |I_IBW|I_IBI|B_IBW
1093 tmpnam B        |stdio  |               |B_B
1094 ttyname I       |unistd |               |I_IBW|I_IBI|B_IBI