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