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