export PerlIOBuf_get_base for win32 extension linkage
[p5sagit/p5-mst-13.2.git] / reentr.pl
1 #!/usr/bin/perl -w
2
3 #
4 # Generate the reentr.c and reentr.h,
5 # and optionally also the relevant metaconfig units (-U option).
6
7
8 use strict;
9 use Getopt::Std;
10 my %opts;
11 getopts('U', \%opts);
12
13 my %map = (
14            V => "void",
15            A => "char*",        # as an input argument
16            B => "char*",        # as an output argument 
17            C => "const char*",  # as a read-only input argument
18            I => "int",
19            L => "long",
20            W => "size_t",
21            H => "FILE**",
22            E => "int*",
23           );
24
25 # (See the definitions after __DATA__.)
26 # In func|inc|type|... a "S" means "type*", and a "R" means "type**".
27 # (The "types" are often structs, such as "struct passwd".)
28 #
29 # After the prototypes one can have |X=...|Y=... to define more types.
30 # A commonly used extra type is to define D to be equal to "type_data",
31 # for example "struct_hostent_data to" go with "struct hostent".
32 #
33 # Example #1: I_XSBWR means int  func_r(X, type, char*, size_t, type**)
34 # Example #2: S_SBIE  means type func_r(type, char*, int, int*)
35 # Example #3: S_CBI   means type func_r(const char*, char*, int)
36
37
38 die "reentr.h: $!" unless open(H, ">reentr.h");
39 select H;
40 print <<EOF;
41 /*
42  *    reentr.h
43  *
44  *    Copyright (c) 1997-2002, Larry Wall
45  *
46  *    You may distribute under the terms of either the GNU General Public
47  *    License or the Artistic License, as specified in the README file.
48  *
49  *  !!!!!!!   DO NOT EDIT THIS FILE   !!!!!!!
50  *  This file is built by reentrl.pl from data in reentr.pl.
51  */
52
53 #ifndef REENTR_H
54 #define REENTR_H 
55
56 #ifdef USE_REENTRANT_API
57  
58 /* Deprecations: some platforms have the said reentrant interfaces
59  * but they are declared obsolete and are not to be used.  Often this
60  * means that the platform has threadsafed the interfaces (hopefully).
61  * All this is OS version dependent, so we are of course fooling ourselves.
62  * If you know of more deprecations on some platforms, please add your own. */
63
64 #ifdef __hpux
65 #   undef HAS_CRYPT_R
66 #   undef HAS_DRAND48_R
67 #   undef HAS_ENDGRENT_R
68 #   undef HAS_ENDPWENT_R
69 #   undef HAS_GETGRENT_R
70 #   undef HAS_GETPWENT_R
71 #   undef HAS_SETLOCALE_R
72 #   undef HAS_SRAND48_R
73 #   undef HAS_STRERROR_R
74 #   define NETDB_R_OBSOLETE
75 #endif
76
77 #if defined(__osf__) && defined(__alpha) /* Tru64 aka Digital UNIX */
78 #   undef HAS_CRYPT_R
79 #   undef HAS_STRERROR_R
80 #   define NETDB_R_OBSOLETE
81 #endif
82
83 #ifdef NETDB_R_OBSOLETE
84 #   undef HAS_ENDHOSTENT_R
85 #   undef HAS_ENDNETENT_R
86 #   undef HAS_ENDPROTOENT_R
87 #   undef HAS_ENDSERVENT_R
88 #   undef HAS_GETHOSTBYADDR_R
89 #   undef HAS_GETHOSTBYNAME_R
90 #   undef HAS_GETHOSTENT_R
91 #   undef HAS_GETNETBYADDR_R
92 #   undef HAS_GETNETBYNAME_R
93 #   undef HAS_GETNETENT_R
94 #   undef HAS_GETPROTOBYNAME_R
95 #   undef HAS_GETPROTOBYNUMBER_R
96 #   undef HAS_GETPROTOENT_R
97 #   undef HAS_GETSERVBYNAME_R
98 #   undef HAS_GETSERVBYPORT_R
99 #   undef HAS_GETSERVENT_R
100 #   undef HAS_SETHOSTENT_R
101 #   undef HAS_SETNETENT_R
102 #   undef HAS_SETPROTOENT_R
103 #   undef HAS_SETSERVENT_R
104 #endif
105
106 #ifdef I_PWD
107 #   include <pwd.h>
108 #endif
109 #ifdef I_GRP
110 #   include <grp.h>
111 #endif
112 #ifdef I_NETDB
113 #   include <netdb.h>
114 #endif
115 #ifdef I_STDLIB
116 #   include <stdlib.h>  /* drand48_data */
117 #endif
118 #ifdef I_CRYPT
119 #   ifdef I_CRYPT
120 #       include <crypt.h>
121 #   endif
122 #endif
123 #ifdef HAS_GETSPNAM_R
124 #   ifdef I_SHADOW
125 #       include <shadow.h>
126 #   endif
127 #endif
128
129 EOF
130
131 my %seenh;
132 my %seena;
133 my @seenf;
134 my %seenp;
135 my %seent;
136 my %seens;
137 my %seend;
138 my %seenu;
139
140 while (<DATA>) {
141     next if /^\s+$/;
142     chomp;
143     my ($f, $h, $t, @p) = split(/\s*\|\s*/, $_, -1);
144     my $u;
145     ($f, $u) = split(' ', $f);
146     $seenu{$f} = defined $u ? length $u : 0;
147     my $F = uc $f;
148     push @seenf, $f;
149     my %m = %map;
150     if ($t) {
151         $m{S} = "$t*";
152         $m{R} = "$t**";
153     }
154     if (@p) {
155         while ($p[-1] =~ /=/) {
156             my ($k, $v) = ($p[-1] =~ /^([A-Za-z])\s*=\s*(.*)/);
157             $m{$k} = $v;
158             pop @p;
159         }
160     }
161     if ($opts{U} && open(U, ">d_${f}_r.U"))  {
162         select U;
163     }
164     my $prereqs  = '';
165     my $prereqh  = '';
166     my $prereqsh = '';
167     if ($h ne 'stdio') { # There's no i_stdio.
168         $prereqs  = "i_$h";
169         $prereqh  = "$h.h";
170         $prereqsh = "\$$prereqs $prereqh";
171     }
172     if ($opts{U}) {
173         my @prereq = qw(Inlibc Protochk Hasproto i_systypes usethreads);
174         push @prereq, $prereqs;
175         my $hdrs = "\$i_systypes sys/types.h define stdio.h $prereqsh";
176         if ($h eq 'time') {
177             $hdrs .= " \$i_systime sys/time.h";
178             push @prereq, 'i_systime';
179         }
180         print <<EOF;
181 ?RCS: \$Id: d_${f}_r.U,v $
182 ?RCS:
183 ?RCS: Copyright (c) 2002 Jarkko Hietaniemi
184 ?RCS:
185 ?RCS: You may distribute under the terms of either the GNU General Public
186 ?RCS: License or the Artistic License, as specified in the README file.
187 ?RCS:
188 ?RCS: Generated by the reentr.pl from the Perl 5.8 distribution.
189 ?RCS:
190 ?MAKE:d_${f}_r ${f}_r_proto: @prereq
191 ?MAKE:  -pick add \$@ %<
192 ?S:d_${f}_r:
193 ?S:     This variable conditionally defines the HAS_${F}_R symbol,
194 ?S:     which indicates to the C program that the ${f}_r()
195 ?S:     routine is available.
196 ?S:.
197 ?S:${f}_r_proto:
198 ?S:     This variable encodes the prototype of ${f}_r.
199 ?S:.
200 ?C:HAS_${F}_R:
201 ?C:     This symbol, if defined, indicates that the ${f}_r routine
202 ?C:     is available to ${f} re-entrantly.
203 ?C:.
204 ?C:${F}_R_PROTO:
205 ?C:     This symbol encodes the prototype of ${f}_r.
206 ?C:.
207 ?H:#\$d_${f}_r HAS_${F}_R          /**/
208 ?H:#define ${F}_R_PROTO \$${f}_r_proto     /**/
209 ?H:.
210 ?T:try hdrs d_${f}_r_proto
211 ?LINT:set d_${f}_r
212 ?LINT:set ${f}_r_proto
213 : see if ${f}_r exists
214 set ${f}_r d_${f}_r
215 eval \$inlibc
216 case "\$d_${f}_r" in
217 "\$define")
218 EOF
219         print <<EOF;
220         hdrs="$hdrs"
221         case "\$d_${f}_r_proto:\$usethreads" in
222         ":define")      d_${f}_r_proto=define
223                 set d_${f}_r_proto ${f}_r \$hdrs
224                 eval \$hasproto ;;
225         *)      ;;
226         esac
227         case "\$d_${f}_r_proto" in
228         define)
229 EOF
230     }
231     for my $p (@p) {
232         my ($r, $a) = ($p =~ /^(.)_(.+)/);
233         my $v = join(", ", map { $m{$_} } split '', $a);
234         if ($opts{U}) {
235             print <<EOF ;
236         case "\$${f}_r_proto" in
237         ''|0) try='$m{$r} ${f}_r($v);'
238         ./protochk "extern \$try" \$hdrs && ${f}_r_proto=$p ;;
239         esac
240 EOF
241         }
242         $seenh{$f}->{$p}++;
243         push @{$seena{$f}}, $p;
244         $seenp{$p}++;
245         $seent{$f} = $t;
246         $seens{$f} = $m{S};
247         $seend{$f} = $m{D};
248     }
249     if ($opts{U}) {
250         print <<EOF;
251         case "\$${f}_r_proto" in
252         ''|0)   d_${f}_r=undef
253                 ${f}_r_proto=0
254                 echo "Disabling ${f}_r, cannot determine prototype." >&4 ;;
255         * )     case "\$${f}_r_proto" in
256                 REENTRANT_PROTO*) ;;
257                 *) ${f}_r_proto="REENTRANT_PROTO_\$${f}_r_proto" ;;
258                 esac
259                 echo "Prototype: \$try" ;;
260         esac
261         ;;
262         *)      case "\$usethreads" in
263                 define) echo "${f}_r has no prototype, not using it." >&4 ;;
264                 esac
265                 d_${f}_r=undef
266                 ${f}_r_proto=0
267                 ;;
268         esac
269         ;;
270 *)      ${f}_r_proto=0
271         ;;
272 esac
273
274 EOF
275         close(U);                   
276     }
277 }
278
279 close DATA;
280
281 select H;
282
283 {
284     my $i = 1;
285     for my $p (sort keys %seenp) {
286         print "#define REENTRANT_PROTO_${p}     ${i}\n";
287         $i++;
288     }
289 }
290
291 sub ifprotomatch {
292     my $F = shift;
293     join " || ", map { "${F}_R_PROTO == REENTRANT_PROTO_$_" } @_;
294 }
295
296 my @struct;
297 my @size;
298 my @init;
299 my @free;
300 my @wrap;
301 my @define;
302
303 sub pushssif {
304     push @struct, @_;
305     push @size, @_;
306     push @init, @_;
307     push @free, @_;
308 }
309
310 sub pushinitfree {
311     my $f = shift;
312     push @init, <<EOF;
313         New(31338, PL_reentrant_buffer->_${f}_buffer, PL_reentrant_buffer->_${f}_size, char);
314 EOF
315     push @free, <<EOF;
316         Safefree(PL_reentrant_buffer->_${f}_buffer);
317 EOF
318 }
319
320 sub define {
321     my ($n, $p, @F) = @_;
322     my @H;
323     my $H = uc $F[0];
324     push @define, <<EOF;
325 /* The @F using \L$n? */
326
327 EOF
328     my $G;
329     for my $f (@F) {
330         my $F = uc $f;
331         my $h = "${F}_R_HAS_$n";
332         push @H, $h;
333         my @h = grep { /$p/ } @{$seena{$f}};
334         unless (defined $G) {
335             $G = $F;
336             $G =~ s/^GET//;
337         }
338         if (@h) {
339             push @define, "#if defined(HAS_${F}_R) && (" . join(" || ", map { "${F}_R_PROTO == REENTRANT_PROTO_$_" } @h) . ")\n";
340
341             push @define, <<EOF;
342 #   define $h
343 #else
344 #   undef  $h
345 #endif
346 EOF
347         }
348     }
349     push @define, <<EOF;
350
351 /* Any of the @F using \L$n? */
352
353 EOF
354     push @define, "#if (" . join(" || ", map { "defined($_)" } @H) . ")\n";
355     push @define, <<EOF;
356 #   define USE_${G}_$n
357 #else
358 #   undef  USE_${G}_$n
359 #endif
360
361 EOF
362 }
363
364 define('BUFFER',  'B',
365        qw(getgrent getgrgid getgrnam));
366
367 define('PTR',  'R',
368        qw(getgrent getgrgid getgrnam));
369 define('PTR',  'R',
370        qw(getpwent getpwnam getpwuid));
371 define('PTR',  'R',
372        qw(getspent getspnam));
373
374 define('FPTR', 'H',
375        qw(getgrent getgrgid getgrnam setgrent endgrent));
376 define('FPTR', 'H',
377        qw(getpwent getpwnam getpwuid setpwent endpwent));
378
379 define('BUFFER',  'B',
380        qw(getpwent getpwgid getpwnam));
381
382 define('PTR', 'R',
383        qw(gethostent gethostbyaddr gethostbyname));
384 define('PTR', 'R',
385        qw(getnetent getnetbyaddr getnetbyname));
386 define('PTR', 'R',
387        qw(getprotoent getprotobyname getprotobynumber));
388 define('PTR', 'R',
389        qw(getservent getservbyname getservbyport));
390
391 define('BUFFER', 'B',
392        qw(gethostent gethostbyaddr gethostbyname));
393 define('BUFFER', 'B',
394        qw(getnetent getnetbyaddr getnetbyname));
395 define('BUFFER', 'B',
396        qw(getprotoent getprotobyname getprotobynumber));
397 define('BUFFER', 'B',
398        qw(getservent getservbyname getservbyport));
399
400 define('ERRNO', 'E',
401        qw(gethostent gethostbyaddr gethostbyname));
402 define('ERRNO', 'E',
403        qw(getnetent getnetbyaddr getnetbyname));
404
405 for my $f (@seenf) {
406     my $F = uc $f;
407     my $ifdef = "#ifdef HAS_${F}_R\n";
408     my $endif = "#endif /* HAS_${F}_R */\n";
409     if (exists $seena{$f}) {
410         my @p = @{$seena{$f}};
411         if ($f =~ /^(asctime|ctime|getlogin|setlocale|strerror|ttyname)$/) {
412             pushssif $ifdef;
413             push @struct, <<EOF;
414         char*   _${f}_buffer;
415         size_t  _${f}_size;
416 EOF
417             push @size, <<EOF;
418         PL_reentrant_buffer->_${f}_size = REENTRANTSMALLSIZE;
419 EOF
420             pushinitfree $f;
421             pushssif $endif;
422         }
423         elsif ($f =~ /^(crypt)$/) {
424             pushssif $ifdef;
425             push @struct, <<EOF;
426 #if CRYPT_R_PROTO == REENTRANT_PROTO_B_CCD
427         $seend{$f} _${f}_data;
428 #else
429         $seent{$f} _${f}_struct;
430 #endif
431 EOF
432             push @init, <<EOF;
433 #ifdef __GLIBC__
434         PL_reentrant_buffer->_${f}_struct.initialized = 0;
435 #endif
436 EOF
437             pushssif $endif;
438         }
439         elsif ($f =~ /^(drand48|gmtime|localtime|random)$/) {
440             pushssif $ifdef;
441             push @struct, <<EOF;
442         $seent{$f} _${f}_struct;
443 EOF
444             if ($1 eq 'drand48') {
445                 push @struct, <<EOF;
446         double  _${f}_double;
447 EOF
448             }
449             pushssif $endif;
450         }
451         elsif ($f =~ /^(getgrnam|getpwnam|getspnam)$/) {
452             pushssif $ifdef;
453             my $g = $f;
454             $g =~ s/nam/ent/g;
455             $g =~ s/^get//;
456             my $G = uc $g;
457             push @struct, <<EOF;
458         $seent{$f}      _${g}_struct;
459         char*   _${g}_buffer;
460         size_t  _${g}_size;
461 EOF
462             push @struct, <<EOF;
463 #   ifdef USE_${G}_PTR
464         $seent{$f}*     _${g}_ptr;
465 #   endif
466 EOF
467             if ($g eq 'getspent') {
468                 push @size, <<EOF;
469         PL_reentrant_buffer->_${g}_size = 1024;
470 EOF
471             } else {
472                 push @struct, <<EOF;
473 #   ifdef USE_${G}_FPTR
474         FILE*   _${g}_fptr;
475 #   endif
476 EOF
477                     push @init, <<EOF;
478 #   ifdef USE_${G}_FPTR
479         PL_reentrant_buffer->_${g}_fptr = NULL;
480 #   endif
481 EOF
482                 my $sc = $g eq 'getgrent' ?
483                     '_SC_GETGR_R_SIZE_MAX' : '_SC_GETPW_R_SIZE_MAX';
484                 my $sz = $g eq 'getgrent' ?
485                     '_grent_size' : '_pwent_size';
486                 push @size, <<EOF;
487 #   if defined(HAS_SYSCONF) && defined($sc) && !defined(__GLIBC__)
488         PL_reentrant_buffer->_${g}_size = sysconf($sc);
489         if (PL_reentrant_buffer->$sz == -1)
490                 PL_reentrant_buffer->$sz = REENTRANTUSUALSIZE;
491 #   else
492 #       if defined(__osf__) && defined(__alpha) && defined(SIABUFSIZ)
493         PL_reentrant_buffer->_${g}_size = SIABUFSIZ;
494 #       else
495 #           ifdef __sgi
496         PL_reentrant_buffer->_${g}_size = BUFSIZ;
497 #           else
498         PL_reentrant_buffer->_${g}_size = REENTRANTUSUALSIZE;
499 #           endif
500 #       endif
501 #   endif 
502 EOF
503             }
504             pushinitfree $g;
505             pushssif $endif;
506         }
507         elsif ($f =~ /^(gethostbyname|getnetbyname|getservbyname|getprotobyname)$/) {
508             pushssif $ifdef;
509             my $g = $f;
510             $g =~ s/byname/ent/;
511             $g =~ s/^get//;
512             my $G = uc $g;
513             my $D = ifprotomatch($F, grep {/D/} @p);
514             my $d = $seend{$f};
515             $d =~ s/\*$//; # snip: we need need the base type.
516             push @struct, <<EOF;
517         $seent{$f}      _${g}_struct;
518 #   if $D
519         $d      _${g}_data;
520 #   else
521         char*   _${g}_buffer;
522         size_t  _${g}_size;
523 #   endif
524 #   ifdef USE_${G}_PTR
525         $seent{$f}*     _${g}_ptr;
526 #   endif
527 EOF
528             push @struct, <<EOF;
529 #   ifdef USE_${G}_ERRNO
530         int     _${g}_errno;
531 #   endif 
532 EOF
533             push @size, <<EOF;
534 #if   !($D)
535         PL_reentrant_buffer->_${g}_size = REENTRANTUSUALSIZE;
536 #endif
537 EOF
538             push @init, <<EOF;
539 #if   !($D)
540         New(31338, PL_reentrant_buffer->_${g}_buffer, PL_reentrant_buffer->_${g}_size, char);
541 #endif
542 EOF
543             push @free, <<EOF;
544 #if   !($D)
545         Safefree(PL_reentrant_buffer->_${g}_buffer);
546 #endif
547 EOF
548             pushssif $endif;
549         }
550         elsif ($f =~ /^(readdir|readdir64)$/) {
551             pushssif $ifdef;
552             my $R = ifprotomatch($F, grep {/R/} @p);
553             push @struct, <<EOF;
554         $seent{$f}*     _${f}_struct;
555         size_t  _${f}_size;
556 #   if $R
557         $seent{$f}*     _${f}_ptr;
558 #   endif
559 EOF
560             push @size, <<EOF;
561         /* This is the size Solaris recommends.
562          * (though we go static, should use pathconf() instead) */
563         PL_reentrant_buffer->_${f}_size = sizeof($seent{$f}) + MAXPATHLEN + 1;
564 EOF
565             push @init, <<EOF;
566         PL_reentrant_buffer->_${f}_struct = ($seent{$f}*)safemalloc(PL_reentrant_buffer->_${f}_size);
567 EOF
568             push @free, <<EOF;
569         Safefree(PL_reentrant_buffer->_${f}_struct);
570 EOF
571             pushssif $endif;
572         }
573
574         push @wrap, $ifdef;
575
576         push @wrap, <<EOF;
577 #   undef $f
578 EOF
579         my @v = 'a'..'z';
580         my $v = join(", ", @v[0..$seenu{$f}-1]);
581         for my $p (@p) {
582             my ($r, $a) = split '_', $p;
583             my $test = $r eq 'I' ? ' == 0' : '';
584             my $true  = 1;
585             my $g = $f;
586             if ($g =~ /^(?:get|set|end)(pw|gr|host|net|proto|serv|sp)/) {
587                 $g = "$1ent";
588             } elsif ($g eq 'srand48') {
589                 $g = "drand48";
590             }
591             my $b = $a;
592             my $w = '';
593             substr($b, 0, $seenu{$f}) = '';
594             if ($b =~ /R/) {
595                 $true = "PL_reentrant_buffer->_${g}_ptr";
596             } elsif ($b =~ /T/ && $f eq 'drand48') {
597                 $true = "PL_reentrant_buffer->_${g}_double";
598             } elsif ($b =~ /S/) {
599                 if ($f =~ /^readdir/) {
600                     $true = "PL_reentrant_buffer->_${g}_struct";
601                 } else {
602                     $true = "&PL_reentrant_buffer->_${g}_struct";
603                 }
604             } elsif ($b =~ /B/) {
605                 $true = "PL_reentrant_buffer->_${g}_buffer";
606             }
607             if (length $b) {
608                 $w = join ", ",
609                          map {
610                              $_ eq 'R' ?
611                                  "&PL_reentrant_buffer->_${g}_ptr" :
612                              $_ eq 'E' ?
613                                  "&PL_reentrant_buffer->_${g}_errno" :
614                              $_ eq 'B' ?
615                                  "PL_reentrant_buffer->_${g}_buffer" :
616                              $_ =~ /^[WI]$/ ?
617                                  "PL_reentrant_buffer->_${g}_size" :
618                              $_ eq 'H' ?
619                                  "&PL_reentrant_buffer->_${g}_fptr" :
620                              $_ eq 'D' ?
621                                  "&PL_reentrant_buffer->_${g}_data" :
622                              $_ eq 'S' ?
623                                  ($f =~ /^readdir/ ?
624                                   "PL_reentrant_buffer->_${g}_struct" :
625                                   "&PL_reentrant_buffer->_${g}_struct" ) :
626                              $_ eq 'T' && $f eq 'drand48' ?
627                                  "&PL_reentrant_buffer->_${g}_double" :
628                                  $_
629                          } split '', $b;
630                 $w = ", $w" if length $v;
631             }
632             my $call = "${f}_r($v$w)";
633             $call = "((errno = $call))" if $r eq 'I';
634             push @wrap, <<EOF;
635 #   if !defined($f) && ${F}_R_PROTO == REENTRANT_PROTO_$p
636 EOF
637             if ($r eq 'V' || $r eq 'B') {
638                 push @wrap, <<EOF;
639 #       define $f($v) $call
640 EOF
641             } else {
642                 if ($f =~ /^get/) {
643                     my $rv = $v ? ", $v" : "";
644                     push @wrap, <<EOF;
645 #       define $f($v) ($call$test ? $true : (errno == ERANGE ? Perl_reentrant_retry("$f"$rv) : 0))
646 EOF
647                 } else {
648                     push @wrap, <<EOF;
649 #       define $f($v) ($call$test ? $true : 0)
650 EOF
651                 }
652             }
653             push @wrap, <<EOF;
654 #   endif
655 EOF
656         }
657
658         push @wrap, $endif, "\n";
659     }
660 }
661
662 local $" = '';
663
664 print <<EOF;
665
666 /* Defines for indicating which special features are supported. */
667
668 @define
669 typedef struct {
670 @struct
671 } REENTR;
672
673 /* The wrappers. */
674
675 @wrap
676 #endif /* USE_REENTRANT_API */
677  
678 #endif
679
680 EOF
681
682 close(H);
683
684 die "reentr.c: $!" unless open(C, ">reentr.c");
685 select C;
686 print <<EOF;
687 /*
688  *    reentr.c
689  *
690  *    Copyright (c) 1997-2002, Larry Wall
691  *
692  *    You may distribute under the terms of either the GNU General Public
693  *    License or the Artistic License, as specified in the README file.
694  *
695  *  !!!!!!!   DO NOT EDIT THIS FILE   !!!!!!!
696  *  This file is built by reentrl.pl from data in reentr.pl.
697  *
698  * "Saruman," I said, standing away from him, "only one hand at a time can
699  *  wield the One, and you know that well, so do not trouble to say we!"
700  *
701  */
702
703 #include "EXTERN.h"
704 #define PERL_IN_REENTR_C
705 #include "perl.h"
706 #include "reentr.h"
707
708 void
709 Perl_reentrant_size(pTHX) {
710 #ifdef USE_REENTRANT_API
711 #define REENTRANTSMALLSIZE       256    /* Make something up. */
712 #define REENTRANTUSUALSIZE      4096    /* Make something up. */
713 @size
714 #endif /* USE_REENTRANT_API */
715 }
716
717 void
718 Perl_reentrant_init(pTHX) {
719 #ifdef USE_REENTRANT_API
720         New(31337, PL_reentrant_buffer, 1, REENTR);
721         Perl_reentrant_size(aTHX);
722 @init
723 #endif /* USE_REENTRANT_API */
724 }
725
726 void
727 Perl_reentrant_free(pTHX) {
728 #ifdef USE_REENTRANT_API
729 @free
730         Safefree(PL_reentrant_buffer);
731 #endif /* USE_REENTRANT_API */
732 }
733
734 void*
735 Perl_reentrant_retry(const char *f, ...)
736 {
737     dTHX;
738     void *retptr = NULL;
739 #ifdef USE_REENTRANT_API
740 #  if defined(USE_HOSTENT_BUFFER) || defined(USE_GRENT_BUFFER) || defined(USE_NETENT_BUFFER) || defined(USE_PWENT_BUFFER) || defined(USE_PROTOENT_BUFFER) || defined(USE_SRVENT_BUFFER)
741     void *p0;
742 #  endif
743 #  if defined(USE_SERVENT_BUFFER)
744     void *p1;
745 #  endif
746 #  if defined(USE_HOSTENT_BUFFER)
747     size_t asize;
748 #  endif
749 #  if defined(USE_HOSTENT_BUFFER) || defined(USE_NETENT_BUFFER) || defined(USE_PROTOENT_BUFFER) || defined(USE_SERVENT_BUFFER)
750     int anint;
751 #  endif
752     va_list ap;
753
754     va_start(ap, f);
755
756 #define REENTRANTHALFMAXSIZE 32768 /* The maximum may end up twice this. */
757
758     switch (PL_op->op_type) {
759 #ifdef USE_HOSTENT_BUFFER
760     case OP_GHBYADDR:
761     case OP_GHBYNAME:
762     case OP_GHOSTENT:
763         {
764             if (PL_reentrant_buffer->_hostent_size <= REENTRANTHALFMAXSIZE) {
765                 PL_reentrant_buffer->_hostent_size *= 2;
766                 Renew(PL_reentrant_buffer->_hostent_buffer,
767                       PL_reentrant_buffer->_hostent_size, char);
768                 switch (PL_op->op_type) {
769                 case OP_GHBYADDR:
770                     p0    = va_arg(ap, void *);
771                     asize = va_arg(ap, size_t);
772                     anint  = va_arg(ap, int);
773                     retptr = gethostbyaddr(p0, asize, anint); break;
774                 case OP_GHBYNAME:
775                     p0 = va_arg(ap, void *);
776                     retptr = gethostbyname(p0); break;
777                 case OP_GHOSTENT:
778                     retptr = gethostent(); break;
779                 default:
780                     break;
781                 }
782             }
783         }
784         break;
785 #endif
786 #ifdef USE_GRENT_BUFFER
787     case OP_GGRNAM:
788     case OP_GGRGID:
789     case OP_GGRENT:
790         {
791             if (PL_reentrant_buffer->_grent_size <= REENTRANTHALFMAXSIZE) {
792                 Gid_t gid;
793                 PL_reentrant_buffer->_grent_size *= 2;
794                 Renew(PL_reentrant_buffer->_grent_buffer,
795                       PL_reentrant_buffer->_grent_size, char);
796                 switch (PL_op->op_type) {
797                 case OP_GGRNAM:
798                     p0 = va_arg(ap, void *);
799                     retptr = getgrnam(p0); break;
800                 case OP_GGRGID:
801                     gid = va_arg(ap, Gid_t);
802                     retptr = getgrgid(gid); break;
803                 case OP_GGRENT:
804                     retptr = getgrent(); break;
805                 default:
806                     break;
807                 }
808             }
809         }
810         break;
811 #endif
812 #ifdef USE_NETENT_BUFFER
813     case OP_GNBYADDR:
814     case OP_GNBYNAME:
815     case OP_GNETENT:
816         {
817             if (PL_reentrant_buffer->_netent_size <= REENTRANTHALFMAXSIZE) {
818                 Netdb_net_t net;
819                 PL_reentrant_buffer->_netent_size *= 2;
820                 Renew(PL_reentrant_buffer->_netent_buffer,
821                       PL_reentrant_buffer->_netent_size, char);
822                 switch (PL_op->op_type) {
823                 case OP_GNBYADDR:
824                     net = va_arg(ap, Netdb_net_t);
825                     anint = va_arg(ap, int);
826                     retptr = getnetbyaddr(net, anint); break;
827                 case OP_GNBYNAME:
828                     p0 = va_arg(ap, void *);
829                     retptr = getnetbyname(p0); break;
830                 case OP_GNETENT:
831                     retptr = getnetent(); break;
832                 default:
833                     break;
834                 }
835             }
836         }
837         break;
838 #endif
839 #ifdef USE_PWENT_BUFFER
840     case OP_GPWNAM:
841     case OP_GPWUID:
842     case OP_GPWENT:
843         {
844             if (PL_reentrant_buffer->_pwent_size <= REENTRANTHALFMAXSIZE) {
845                 Uid_t uid;
846                 PL_reentrant_buffer->_pwent_size *= 2;
847                 Renew(PL_reentrant_buffer->_pwent_buffer,
848                       PL_reentrant_buffer->_pwent_size, char);
849                 switch (PL_op->op_type) {
850                 case OP_GPWNAM:
851                     p0 = va_arg(ap, void *);
852                     retptr = getpwnam(p0); break;
853                 case OP_GPWUID:
854                     uid = va_arg(ap, Uid_t);
855                     retptr = getpwuid(uid); break;
856                 case OP_GPWENT:
857                     retptr = getpwent(); break;
858                 default:
859                     break;
860                 }
861             }
862         }
863         break;
864 #endif
865 #ifdef USE_PROTOENT_BUFFER
866     case OP_GPBYNAME:
867     case OP_GPBYNUMBER:
868     case OP_GPROTOENT:
869         {
870             if (PL_reentrant_buffer->_protoent_size <= REENTRANTHALFMAXSIZE) {
871                 PL_reentrant_buffer->_protoent_size *= 2;
872                 Renew(PL_reentrant_buffer->_protoent_buffer,
873                       PL_reentrant_buffer->_protoent_size, char);
874                 switch (PL_op->op_type) {
875                 case OP_GPBYNAME:
876                     p0 = va_arg(ap, void *);
877                     retptr = getprotobyname(p0); break;
878                 case OP_GPBYNUMBER:
879                     anint = va_arg(ap, int);
880                     retptr = getprotobynumber(anint); break;
881                 case OP_GPROTOENT:
882                     retptr = getprotoent(); break;
883                 default:
884                     break;
885                 }
886             }
887         }
888         break;
889 #endif
890 #ifdef USE_SERVENT_BUFFER
891     case OP_GSBYNAME:
892     case OP_GSBYPORT:
893     case OP_GSERVENT:
894         {
895             if (PL_reentrant_buffer->_servent_size <= REENTRANTHALFMAXSIZE) {
896                 PL_reentrant_buffer->_servent_size *= 2;
897                 Renew(PL_reentrant_buffer->_servent_buffer,
898                       PL_reentrant_buffer->_servent_size, char);
899                 switch (PL_op->op_type) {
900                 case OP_GSBYNAME:
901                     p0 = va_arg(ap, void *);
902                     p1 = va_arg(ap, void *);
903                     retptr = getservbyname(p0, p1); break;
904                 case OP_GSBYPORT:
905                     anint = va_arg(ap, int);
906                     p0 = va_arg(ap, void *);
907                     retptr = getservbyport(anint, p0); break;
908                 case OP_GSERVENT:
909                     retptr = getservent(); break;
910                 default:
911                     break;
912                 }
913             }
914         }
915         break;
916 #endif
917     default:
918         /* Not known how to retry, so just fail. */
919         break;
920     }
921
922     va_end(ap);
923 #endif
924     return retptr;
925 }
926
927 EOF
928
929 __DATA__
930 asctime S       |time   |const struct tm|B_SB|B_SBI|I_SB|I_SBI
931 crypt CC        |crypt  |struct crypt_data|B_CCS|B_CCD|D=CRYPTD*
932 ctermid B       |stdio  |               |B_B
933 ctime S         |time   |const time_t   |B_SB|B_SBI|I_SB|I_SBI
934 drand48         |stdlib |struct drand48_data    |I_ST|T=double*
935 endgrent        |grp    |               |I_H|V_H
936 endhostent      |netdb  |               |I_D|V_D|D=struct hostent_data*
937 endnetent       |netdb  |               |I_D|V_D|D=struct netent_data*
938 endprotoent     |netdb  |               |I_D|V_D|D=struct protoent_data*
939 endpwent        |pwd    |               |I_H|V_H
940 endservent      |netdb  |               |I_D|V_D|D=struct servent_data*
941 getgrent        |grp    |struct group   |I_SBWR|I_SBIR|S_SBW|S_SBI|I_SBI|I_SBIH
942 getgrgid T      |grp    |struct group   |I_TSBWR|I_TSBIR|I_TSBI|S_TSBI|T=gid_t
943 getgrnam C      |grp    |struct group   |I_CSBWR|I_CSBIR|S_CBI|I_CSBI|S_CSBI
944 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|D=struct hostent_data*|T=const void*
945 gethostbyname C |netdb  |struct hostent |I_CSBWRE|S_CSBIE|I_CSD|D=struct hostent_data*
946 gethostent      |netdb  |struct hostent |I_SBWRE|I_SBIE|S_SBIE|S_SBI|I_SBI|I_SD|D=struct hostent_data*
947 getlogin        |unistd |               |I_BW|I_BI|B_BW|B_BI
948 getnetbyaddr LI |netdb  |struct netent  |I_UISBWRE|I_LISBI|S_TISBI|S_LISBI|I_TISD|I_LISD|I_IISD|D=struct netent_data*|T=in_addr_t|U=unsigned long
949 getnetbyname C  |netdb  |struct netent  |I_CSBWRE|I_CSBI|S_CSBI|I_CSD|D=struct netent_data*
950 getnetent       |netdb  |struct netent  |I_SBWRE|I_SBIE|S_SBIE|S_SBI|I_SBI|I_SD|D=struct netent_data*
951 getprotobyname C|netdb  |struct protoent|I_CSBWR|S_CSBI|I_CSD|D=struct protoent_data*
952 getprotobynumber I      |netdb  |struct protoent|I_ISBWR|S_ISBI|I_ISD|D=struct protoent_data*
953 getprotoent     |netdb  |struct protoent|I_SBWR|I_SBI|S_SBI|I_SD|D=struct protoent_data*
954 getpwent        |pwd    |struct passwd  |I_SBWR|I_SBIR|S_SBW|S_SBI|I_SBI|I_SBIH
955 getpwnam C      |pwd    |struct passwd  |I_CSBWR|I_CSBIR|S_CSBI|I_CSBI
956 getpwuid T      |pwd    |struct passwd  |I_TSBWR|I_TSBIR|I_TSBI|S_TSBI|T=uid_t
957 getservbyname CC|netdb  |struct servent |I_CCSBWR|S_CCSBI|I_CCSD|D=struct servent_data*
958 getservbyport IC|netdb  |struct servent |I_ICSBWR|S_ICSBI|I_ICSD|D=struct servent_data*
959 getservent      |netdb  |struct servent |I_SBWR|I_SBI|S_SBI|I_SD|D=struct servent_data*
960 getspnam C      |shadow |struct spwd    |I_CSBWR|S_CSBI
961 gmtime T        |time   |struct tm      |S_TS|I_TS|T=const time_t*
962 localtime T     |time   |struct tm      |S_TS|I_TS|T=const time_t*
963 random          |stdlib |struct random_data|I_TS|T=int*
964 readdir T       |dirent |struct dirent  |I_TSR|I_TS|T=DIR*
965 readdir64 T     |dirent |struct dirent64|I_TSR|I_TS|T=DIR*
966 setgrent        |grp    |               |I_H|V_H
967 sethostent I    |netdb  |               |I_ID|V_ID|D=struct hostent_data*
968 setlocale IC    |locale |               |I_ICBI
969 setnetent I     |netdb  |               |I_ID|V_ID|D=struct netent_data*
970 setprotoent I   |netdb  |               |I_ID|V_ID|D=struct protoent_data*
971 setpwent        |pwd    |               |I_H|V_H
972 setservent I    |netdb  |               |I_ID|V_ID|D=struct servent_data*
973 srand48 L       |stdlib |struct drand48_data    |I_LS
974 srandom T       |stdlib |struct random_data|I_TS|T=unsigned int
975 strerror I      |string |               |I_IBW|I_IBI|B_IBW
976 tmpnam B        |stdio  |               |B_B
977 ttyname I       |unistd |               |I_IBW|I_IBI|B_IBI