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