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