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