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