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