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