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