Avoid potentially empty struct.
[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     int dummy; /* just in case */
672 } REENTR;
673
674 /* The wrappers. */
675
676 @wrap
677 #endif /* USE_REENTRANT_API */
678  
679 #endif
680
681 EOF
682
683 close(H);
684
685 die "reentr.c: $!" unless open(C, ">reentr.c");
686 select C;
687 print <<EOF;
688 /*
689  *    reentr.c
690  *
691  *    Copyright (c) 1997-2002, Larry Wall
692  *
693  *    You may distribute under the terms of either the GNU General Public
694  *    License or the Artistic License, as specified in the README file.
695  *
696  *  !!!!!!!   DO NOT EDIT THIS FILE   !!!!!!!
697  *  This file is built by reentrl.pl from data in reentr.pl.
698  *
699  * "Saruman," I said, standing away from him, "only one hand at a time can
700  *  wield the One, and you know that well, so do not trouble to say we!"
701  *
702  */
703
704 #include "EXTERN.h"
705 #define PERL_IN_REENTR_C
706 #include "perl.h"
707 #include "reentr.h"
708
709 void
710 Perl_reentrant_size(pTHX) {
711 #ifdef USE_REENTRANT_API
712 #define REENTRANTSMALLSIZE       256    /* Make something up. */
713 #define REENTRANTUSUALSIZE      4096    /* Make something up. */
714 @size
715 #endif /* USE_REENTRANT_API */
716 }
717
718 void
719 Perl_reentrant_init(pTHX) {
720 #ifdef USE_REENTRANT_API
721         New(31337, PL_reentrant_buffer, 1, REENTR);
722         Perl_reentrant_size(aTHX);
723 @init
724 #endif /* USE_REENTRANT_API */
725 }
726
727 void
728 Perl_reentrant_free(pTHX) {
729 #ifdef USE_REENTRANT_API
730 @free
731         Safefree(PL_reentrant_buffer);
732 #endif /* USE_REENTRANT_API */
733 }
734
735 void*
736 Perl_reentrant_retry(const char *f, ...)
737 {
738     dTHX;
739     void *retptr = NULL;
740 #ifdef USE_REENTRANT_API
741 #  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)
742     void *p0;
743 #  endif
744 #  if defined(USE_SERVENT_BUFFER)
745     void *p1;
746 #  endif
747 #  if defined(USE_HOSTENT_BUFFER)
748     size_t asize;
749 #  endif
750 #  if defined(USE_HOSTENT_BUFFER) || defined(USE_NETENT_BUFFER) || defined(USE_PROTOENT_BUFFER) || defined(USE_SERVENT_BUFFER)
751     int anint;
752 #  endif
753     va_list ap;
754
755     va_start(ap, f);
756
757 #define REENTRANTHALFMAXSIZE 32768 /* The maximum may end up twice this. */
758
759     switch (PL_op->op_type) {
760 #ifdef USE_HOSTENT_BUFFER
761     case OP_GHBYADDR:
762     case OP_GHBYNAME:
763     case OP_GHOSTENT:
764         {
765             if (PL_reentrant_buffer->_hostent_size <= REENTRANTHALFMAXSIZE) {
766                 PL_reentrant_buffer->_hostent_size *= 2;
767                 Renew(PL_reentrant_buffer->_hostent_buffer,
768                       PL_reentrant_buffer->_hostent_size, char);
769                 switch (PL_op->op_type) {
770                 case OP_GHBYADDR:
771                     p0    = va_arg(ap, void *);
772                     asize = va_arg(ap, size_t);
773                     anint  = va_arg(ap, int);
774                     retptr = gethostbyaddr(p0, asize, anint); break;
775                 case OP_GHBYNAME:
776                     p0 = va_arg(ap, void *);
777                     retptr = gethostbyname(p0); break;
778                 case OP_GHOSTENT:
779                     retptr = gethostent(); break;
780                 default:
781                     break;
782                 }
783             }
784         }
785         break;
786 #endif
787 #ifdef USE_GRENT_BUFFER
788     case OP_GGRNAM:
789     case OP_GGRGID:
790     case OP_GGRENT:
791         {
792             if (PL_reentrant_buffer->_grent_size <= REENTRANTHALFMAXSIZE) {
793                 Gid_t gid;
794                 PL_reentrant_buffer->_grent_size *= 2;
795                 Renew(PL_reentrant_buffer->_grent_buffer,
796                       PL_reentrant_buffer->_grent_size, char);
797                 switch (PL_op->op_type) {
798                 case OP_GGRNAM:
799                     p0 = va_arg(ap, void *);
800                     retptr = getgrnam(p0); break;
801                 case OP_GGRGID:
802                     gid = va_arg(ap, Gid_t);
803                     retptr = getgrgid(gid); break;
804                 case OP_GGRENT:
805                     retptr = getgrent(); break;
806                 default:
807                     break;
808                 }
809             }
810         }
811         break;
812 #endif
813 #ifdef USE_NETENT_BUFFER
814     case OP_GNBYADDR:
815     case OP_GNBYNAME:
816     case OP_GNETENT:
817         {
818             if (PL_reentrant_buffer->_netent_size <= REENTRANTHALFMAXSIZE) {
819                 Netdb_net_t net;
820                 PL_reentrant_buffer->_netent_size *= 2;
821                 Renew(PL_reentrant_buffer->_netent_buffer,
822                       PL_reentrant_buffer->_netent_size, char);
823                 switch (PL_op->op_type) {
824                 case OP_GNBYADDR:
825                     net = va_arg(ap, Netdb_net_t);
826                     anint = va_arg(ap, int);
827                     retptr = getnetbyaddr(net, anint); break;
828                 case OP_GNBYNAME:
829                     p0 = va_arg(ap, void *);
830                     retptr = getnetbyname(p0); break;
831                 case OP_GNETENT:
832                     retptr = getnetent(); break;
833                 default:
834                     break;
835                 }
836             }
837         }
838         break;
839 #endif
840 #ifdef USE_PWENT_BUFFER
841     case OP_GPWNAM:
842     case OP_GPWUID:
843     case OP_GPWENT:
844         {
845             if (PL_reentrant_buffer->_pwent_size <= REENTRANTHALFMAXSIZE) {
846                 Uid_t uid;
847                 PL_reentrant_buffer->_pwent_size *= 2;
848                 Renew(PL_reentrant_buffer->_pwent_buffer,
849                       PL_reentrant_buffer->_pwent_size, char);
850                 switch (PL_op->op_type) {
851                 case OP_GPWNAM:
852                     p0 = va_arg(ap, void *);
853                     retptr = getpwnam(p0); break;
854                 case OP_GPWUID:
855                     uid = va_arg(ap, Uid_t);
856                     retptr = getpwuid(uid); break;
857                 case OP_GPWENT:
858                     retptr = getpwent(); break;
859                 default:
860                     break;
861                 }
862             }
863         }
864         break;
865 #endif
866 #ifdef USE_PROTOENT_BUFFER
867     case OP_GPBYNAME:
868     case OP_GPBYNUMBER:
869     case OP_GPROTOENT:
870         {
871             if (PL_reentrant_buffer->_protoent_size <= REENTRANTHALFMAXSIZE) {
872                 PL_reentrant_buffer->_protoent_size *= 2;
873                 Renew(PL_reentrant_buffer->_protoent_buffer,
874                       PL_reentrant_buffer->_protoent_size, char);
875                 switch (PL_op->op_type) {
876                 case OP_GPBYNAME:
877                     p0 = va_arg(ap, void *);
878                     retptr = getprotobyname(p0); break;
879                 case OP_GPBYNUMBER:
880                     anint = va_arg(ap, int);
881                     retptr = getprotobynumber(anint); break;
882                 case OP_GPROTOENT:
883                     retptr = getprotoent(); break;
884                 default:
885                     break;
886                 }
887             }
888         }
889         break;
890 #endif
891 #ifdef USE_SERVENT_BUFFER
892     case OP_GSBYNAME:
893     case OP_GSBYPORT:
894     case OP_GSERVENT:
895         {
896             if (PL_reentrant_buffer->_servent_size <= REENTRANTHALFMAXSIZE) {
897                 PL_reentrant_buffer->_servent_size *= 2;
898                 Renew(PL_reentrant_buffer->_servent_buffer,
899                       PL_reentrant_buffer->_servent_size, char);
900                 switch (PL_op->op_type) {
901                 case OP_GSBYNAME:
902                     p0 = va_arg(ap, void *);
903                     p1 = va_arg(ap, void *);
904                     retptr = getservbyname(p0, p1); break;
905                 case OP_GSBYPORT:
906                     anint = va_arg(ap, int);
907                     p0 = va_arg(ap, void *);
908                     retptr = getservbyport(anint, p0); break;
909                 case OP_GSERVENT:
910                     retptr = getservent(); break;
911                 default:
912                     break;
913                 }
914             }
915         }
916         break;
917 #endif
918     default:
919         /* Not known how to retry, so just fail. */
920         break;
921     }
922
923     va_end(ap);
924 #endif
925     return retptr;
926 }
927
928 EOF
929
930 __DATA__
931 asctime S       |time   |const struct tm|B_SB|B_SBI|I_SB|I_SBI
932 crypt CC        |crypt  |struct crypt_data|B_CCS|B_CCD|D=CRYPTD*
933 ctermid B       |stdio  |               |B_B
934 ctime S         |time   |const time_t   |B_SB|B_SBI|I_SB|I_SBI
935 drand48         |stdlib |struct drand48_data    |I_ST|T=double*
936 endgrent        |grp    |               |I_H|V_H
937 endhostent      |netdb  |               |I_D|V_D|D=struct hostent_data*
938 endnetent       |netdb  |               |I_D|V_D|D=struct netent_data*
939 endprotoent     |netdb  |               |I_D|V_D|D=struct protoent_data*
940 endpwent        |pwd    |               |I_H|V_H
941 endservent      |netdb  |               |I_D|V_D|D=struct servent_data*
942 getgrent        |grp    |struct group   |I_SBWR|I_SBIR|S_SBW|S_SBI|I_SBI|I_SBIH
943 getgrgid T      |grp    |struct group   |I_TSBWR|I_TSBIR|I_TSBI|S_TSBI|T=gid_t
944 getgrnam C      |grp    |struct group   |I_CSBWR|I_CSBIR|S_CBI|I_CSBI|S_CSBI
945 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*
946 gethostbyname C |netdb  |struct hostent |I_CSBWRE|S_CSBIE|I_CSD|D=struct hostent_data*
947 gethostent      |netdb  |struct hostent |I_SBWRE|I_SBIE|S_SBIE|S_SBI|I_SBI|I_SD|D=struct hostent_data*
948 getlogin        |unistd |               |I_BW|I_BI|B_BW|B_BI
949 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
950 getnetbyname C  |netdb  |struct netent  |I_CSBWRE|I_CSBI|S_CSBI|I_CSD|D=struct netent_data*
951 getnetent       |netdb  |struct netent  |I_SBWRE|I_SBIE|S_SBIE|S_SBI|I_SBI|I_SD|D=struct netent_data*
952 getprotobyname C|netdb  |struct protoent|I_CSBWR|S_CSBI|I_CSD|D=struct protoent_data*
953 getprotobynumber I      |netdb  |struct protoent|I_ISBWR|S_ISBI|I_ISD|D=struct protoent_data*
954 getprotoent     |netdb  |struct protoent|I_SBWR|I_SBI|S_SBI|I_SD|D=struct protoent_data*
955 getpwent        |pwd    |struct passwd  |I_SBWR|I_SBIR|S_SBW|S_SBI|I_SBI|I_SBIH
956 getpwnam C      |pwd    |struct passwd  |I_CSBWR|I_CSBIR|S_CSBI|I_CSBI
957 getpwuid T      |pwd    |struct passwd  |I_TSBWR|I_TSBIR|I_TSBI|S_TSBI|T=uid_t
958 getservbyname CC|netdb  |struct servent |I_CCSBWR|S_CCSBI|I_CCSD|D=struct servent_data*
959 getservbyport IC|netdb  |struct servent |I_ICSBWR|S_ICSBI|I_ICSD|D=struct servent_data*
960 getservent      |netdb  |struct servent |I_SBWR|I_SBI|S_SBI|I_SD|D=struct servent_data*
961 getspnam C      |shadow |struct spwd    |I_CSBWR|S_CSBI
962 gmtime T        |time   |struct tm      |S_TS|I_TS|T=const time_t*
963 localtime T     |time   |struct tm      |S_TS|I_TS|T=const time_t*
964 random          |stdlib |struct random_data|I_TS|T=int*
965 readdir T       |dirent |struct dirent  |I_TSR|I_TS|T=DIR*
966 readdir64 T     |dirent |struct dirent64|I_TSR|I_TS|T=DIR*
967 setgrent        |grp    |               |I_H|V_H
968 sethostent I    |netdb  |               |I_ID|V_ID|D=struct hostent_data*
969 setlocale IC    |locale |               |I_ICBI
970 setnetent I     |netdb  |               |I_ID|V_ID|D=struct netent_data*
971 setprotoent I   |netdb  |               |I_ID|V_ID|D=struct protoent_data*
972 setpwent        |pwd    |               |I_H|V_H
973 setservent I    |netdb  |               |I_ID|V_ID|D=struct servent_data*
974 srand48 L       |stdlib |struct drand48_data    |I_LS
975 srandom T       |stdlib |struct random_data|I_TS|T=unsigned int
976 strerror I      |string |               |I_IBW|I_IBI|B_IBW
977 tmpnam B        |stdio  |               |B_B
978 ttyname I       |unistd |               |I_IBW|I_IBI|B_IBI