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