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