This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
z/OS update: not all Storable was clear yet.
[perl5.git] / reentr.pl
CommitLineData
10bc17b6
JH
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
8use strict;
9use Getopt::Std;
10my %opts;
11getopts('U', \%opts);
12
13my %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
38die "reentr.h: $!" unless open(H, ">reentr.h");
39select H;
40print <<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
efa45b01
JH
67# undef HAS_ENDGRENT_R
68# undef HAS_ENDPWENT_R
10bc17b6
JH
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
129EOF
130
131my %seenh;
132my %seena;
133my @seenf;
134my %seenp;
135my %seent;
136my %seens;
137my %seend;
138my %seenu;
139
140while (<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 }
31ee0cb7
JH
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 }
d63eadf0 172 if ($opts{U}) {
1fdb6f84
JH
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 }
d63eadf0 180 print <<EOF;
10bc17b6
JH
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:
1fdb6f84 190?MAKE:d_${f}_r ${f}_r_proto: @prereq
10bc17b6
JH
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:.
a48ec845 210?T:try hdrs d_${f}_r_proto
10bc17b6
JH
211?LINT:set d_${f}_r
212?LINT:set ${f}_r_proto
213: see if ${f}_r exists
214set ${f}_r d_${f}_r
215eval \$inlibc
216case "\$d_${f}_r" in
217"\$define")
d63eadf0 218EOF
d63eadf0
JH
219 print <<EOF;
220 hdrs="$hdrs"
c18e646a
JH
221 case "\$d_${f}_r_proto:\$usethreads" in
222 ":define") d_${f}_r_proto=define
a48ec845
JH
223 set d_${f}_r_proto ${f}_r \$hdrs
224 eval \$hasproto ;;
225 *) ;;
226 esac
227 case "\$d_${f}_r_proto" in
228 define)
10bc17b6 229EOF
d63eadf0
JH
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 ;
10bc17b6
JH
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
240EOF
d63eadf0
JH
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;
10bc17b6 251 case "\$${f}_r_proto" in
90e831dc 252 ''|0) d_${f}_r=undef
10bc17b6 253 ${f}_r_proto=0
a48ec845 254 echo "Disabling ${f}_r, cannot determine prototype." >&4 ;;
10bc17b6
JH
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 ;;
c18e646a
JH
262 *) case "\$usethreads" in
263 define) echo "${f}_r has no prototype, not using it." >&4 ;;
264 esac
90e831dc
SB
265 d_${f}_r=undef
266 ${f}_r_proto=0
c18e646a 267 ;;
a48ec845
JH
268 esac
269 ;;
10bc17b6
JH
270*) ${f}_r_proto=0
271 ;;
272esac
273
274EOF
275 close(U);
276 }
277}
278
279close DATA;
280
281select 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
291sub ifprotomatch {
292 my $F = shift;
293 join " || ", map { "${F}_R_PROTO == REENTRANT_PROTO_$_" } @_;
294}
295
296my @struct;
297my @size;
298my @init;
299my @free;
300my @wrap;
301my @define;
302
303sub pushssif {
304 push @struct, @_;
305 push @size, @_;
306 push @init, @_;
307 push @free, @_;
308}
309
310sub pushinitfree {
311 my $f = shift;
312 push @init, <<EOF;
313 New(31338, PL_reentrant_buffer->_${f}_buffer, PL_reentrant_buffer->_${f}_size, char);
314EOF
315 push @free, <<EOF;
316 Safefree(PL_reentrant_buffer->_${f}_buffer);
317EOF
318}
319
320sub 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
327EOF
f7937171 328 my $G;
10bc17b6
JH
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}};
f7937171
JH
334 unless (defined $G) {
335 $G = $F;
336 $G =~ s/^GET//;
337 }
10bc17b6 338 if (@h) {
09310450 339 push @define, "#if defined(HAS_${F}_R) && (" . join(" || ", map { "${F}_R_PROTO == REENTRANT_PROTO_$_" } @h) . ")\n";
10bc17b6
JH
340
341 push @define, <<EOF;
342# define $h
343#else
344# undef $h
345#endif
346EOF
347 }
348 }
349 push @define, <<EOF;
350
351/* Any of the @F using \L$n? */
352
353EOF
354 push @define, "#if (" . join(" || ", map { "defined($_)" } @H) . ")\n";
355 push @define, <<EOF;
f7937171 356# define USE_${G}_$n
10bc17b6 357#else
f7937171 358# undef USE_${G}_$n
10bc17b6
JH
359#endif
360
361EOF
362}
363
edd309b7
JH
364define('BUFFER', 'B',
365 qw(getgrent getgrgid getgrnam));
366
10bc17b6
JH
367define('PTR', 'R',
368 qw(getgrent getgrgid getgrnam));
369define('PTR', 'R',
370 qw(getpwent getpwnam getpwuid));
371define('PTR', 'R',
372 qw(getspent getspnam));
373
374define('FPTR', 'H',
f7937171 375 qw(getgrent getgrgid getgrnam setgrent endgrent));
10bc17b6 376define('FPTR', 'H',
f7937171 377 qw(getpwent getpwnam getpwuid setpwent endpwent));
10bc17b6 378
edd309b7
JH
379define('BUFFER', 'B',
380 qw(getpwent getpwgid getpwnam));
381
10bc17b6
JH
382define('PTR', 'R',
383 qw(gethostent gethostbyaddr gethostbyname));
384define('PTR', 'R',
385 qw(getnetent getnetbyaddr getnetbyname));
386define('PTR', 'R',
387 qw(getprotoent getprotobyname getprotobynumber));
388define('PTR', 'R',
389 qw(getservent getservbyname getservbyport));
390
edd309b7
JH
391define('BUFFER', 'B',
392 qw(gethostent gethostbyaddr gethostbyname));
393define('BUFFER', 'B',
394 qw(getnetent getnetbyaddr getnetbyname));
395define('BUFFER', 'B',
396 qw(getprotoent getprotobyname getprotobynumber));
397define('BUFFER', 'B',
398 qw(getservent getservbyname getservbyport));
399
10bc17b6
JH
400define('ERRNO', 'E',
401 qw(gethostent gethostbyaddr gethostbyname));
402define('ERRNO', 'E',
403 qw(getnetent getnetbyaddr getnetbyname));
404
405for 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;
416EOF
417 push @size, <<EOF;
8695fa85 418 PL_reentrant_buffer->_${f}_size = REENTRANTSMALLSIZE;
10bc17b6
JH
419EOF
420 pushinitfree $f;
421 pushssif $endif;
422 }
b430fd04 423 elsif ($f =~ /^(crypt)$/) {
10bc17b6
JH
424 pushssif $ifdef;
425 push @struct, <<EOF;
b430fd04
JH
426#if CRYPT_R_PROTO == REENTRANT_PROTO_B_CCD
427 $seend{$f} _${f}_data;
428#else
10bc17b6 429 $seent{$f} _${f}_struct;
b430fd04 430#endif
10bc17b6 431EOF
b430fd04 432 push @init, <<EOF;
10bc17b6
JH
433#ifdef __GLIBC__
434 PL_reentrant_buffer->_${f}_struct.initialized = 0;
435#endif
436EOF
b430fd04
JH
437 pushssif $endif;
438 }
439 elsif ($f =~ /^(drand48|gmtime|localtime|random)$/) {
440 pushssif $ifdef;
441 push @struct, <<EOF;
442 $seent{$f} _${f}_struct;
443EOF
10bc17b6
JH
444 if ($1 eq 'drand48') {
445 push @struct, <<EOF;
446 double _${f}_double;
447EOF
448 }
449 pushssif $endif;
450 }
451 elsif ($f =~ /^(getgrnam|getpwnam|getspnam)$/) {
452 pushssif $ifdef;
453 my $g = $f;
454 $g =~ s/nam/ent/g;
f7937171 455 $g =~ s/^get//;
10bc17b6
JH
456 my $G = uc $g;
457 push @struct, <<EOF;
458 $seent{$f} _${g}_struct;
459 char* _${g}_buffer;
460 size_t _${g}_size;
461EOF
462 push @struct, <<EOF;
463# ifdef USE_${G}_PTR
464 $seent{$f}* _${g}_ptr;
465# endif
466EOF
467 if ($g eq 'getspent') {
468 push @size, <<EOF;
469 PL_reentrant_buffer->_${g}_size = 1024;
470EOF
471 } else {
472 push @struct, <<EOF;
473# ifdef USE_${G}_FPTR
474 FILE* _${g}_fptr;
475# endif
476EOF
477 push @init, <<EOF;
478# ifdef USE_${G}_FPTR
479 PL_reentrant_buffer->_${g}_fptr = NULL;
480# endif
481EOF
482 my $sc = $g eq 'getgrent' ?
483 '_SC_GETGR_R_SIZE_MAX' : '_SC_GETPW_R_SIZE_MAX';
e3410746 484 my $sz = $g eq 'getgrent' ?
f7937171 485 '_grent_size' : '_pwent_size';
10bc17b6
JH
486 push @size, <<EOF;
487# if defined(HAS_SYSCONF) && defined($sc) && !defined(__GLIBC__)
488 PL_reentrant_buffer->_${g}_size = sysconf($sc);
e3410746
SR
489 if (PL_reentrant_buffer->$sz == -1)
490 PL_reentrant_buffer->$sz = REENTRANTUSUALSIZE;
10bc17b6
JH
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
8695fa85 498 PL_reentrant_buffer->_${g}_size = REENTRANTUSUALSIZE;
10bc17b6
JH
499# endif
500# endif
501# endif
502EOF
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/;
f7937171 511 $g =~ s/^get//;
10bc17b6
JH
512 my $G = uc $g;
513 my $D = ifprotomatch($F, grep {/D/} @p);
514 my $d = $seend{$f};
31ee0cb7 515 $d =~ s/\*$//; # snip: we need need the base type.
10bc17b6
JH
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
527EOF
528 push @struct, <<EOF;
529# ifdef USE_${G}_ERRNO
530 int _${g}_errno;
531# endif
532EOF
533 push @size, <<EOF;
534#if !($D)
8695fa85 535 PL_reentrant_buffer->_${g}_size = REENTRANTUSUALSIZE;
10bc17b6
JH
536#endif
537EOF
538 push @init, <<EOF;
539#if !($D)
540 New(31338, PL_reentrant_buffer->_${g}_buffer, PL_reentrant_buffer->_${g}_size, char);
541#endif
542EOF
543 push @free, <<EOF;
544#if !($D)
545 Safefree(PL_reentrant_buffer->_${g}_buffer);
546#endif
547EOF
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
559EOF
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;
564EOF
565 push @init, <<EOF;
566 PL_reentrant_buffer->_${f}_struct = ($seent{$f}*)safemalloc(PL_reentrant_buffer->_${f}_size);
567EOF
568 push @free, <<EOF;
569 Safefree(PL_reentrant_buffer->_${f}_struct);
570EOF
571 pushssif $endif;
572 }
573
574 push @wrap, $ifdef;
575
10bc17b6
JH
576 push @wrap, <<EOF;
577# undef $f
578EOF
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;
10bc17b6
JH
585 my $g = $f;
586 if ($g =~ /^(?:get|set|end)(pw|gr|host|net|proto|serv|sp)/) {
f7937171 587 $g = "$1ent";
10bc17b6
JH
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
636EOF
637 if ($r eq 'V' || $r eq 'B') {
638 push @wrap, <<EOF;
639# define $f($v) $call
640EOF
641 } else {
edd309b7
JH
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))
10bc17b6 646EOF
edd309b7
JH
647 } else {
648 push @wrap, <<EOF;
649# define $f($v) ($call$test ? $true : 0)
650EOF
651 }
10bc17b6
JH
652 }
653 push @wrap, <<EOF;
654# endif
655EOF
656 }
657
658 push @wrap, $endif, "\n";
659 }
660}
661
662local $" = '';
663
664print <<EOF;
665
666/* Defines for indicating which special features are supported. */
667
668@define
669typedef struct {
670@struct
158b3652 671 int dummy; /* just in case */
10bc17b6
JH
672} REENTR;
673
674/* The wrappers. */
675
676@wrap
677#endif /* USE_REENTRANT_API */
678
679#endif
680
681EOF
682
683close(H);
684
685die "reentr.c: $!" unless open(C, ">reentr.c");
686select C;
687print <<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
709void
710Perl_reentrant_size(pTHX) {
711#ifdef USE_REENTRANT_API
8695fa85
SR
712#define REENTRANTSMALLSIZE 256 /* Make something up. */
713#define REENTRANTUSUALSIZE 4096 /* Make something up. */
10bc17b6
JH
714@size
715#endif /* USE_REENTRANT_API */
716}
717
718void
719Perl_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
727void
728Perl_reentrant_free(pTHX) {
729#ifdef USE_REENTRANT_API
730@free
731 Safefree(PL_reentrant_buffer);
732#endif /* USE_REENTRANT_API */
733}
734
edd309b7
JH
735void*
736Perl_reentrant_retry(const char *f, ...)
737{
738 dTHX;
739 void *retptr = NULL;
740#ifdef USE_REENTRANT_API
f7937171 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)
e3410746
SR
742 void *p0;
743# endif
f7937171 744# if defined(USE_SERVENT_BUFFER)
e3410746
SR
745 void *p1;
746# endif
f7937171 747# if defined(USE_HOSTENT_BUFFER)
edd309b7 748 size_t asize;
e3410746 749# endif
f7937171 750# if defined(USE_HOSTENT_BUFFER) || defined(USE_NETENT_BUFFER) || defined(USE_PROTOENT_BUFFER) || defined(USE_SERVENT_BUFFER)
edd309b7 751 int anint;
e3410746 752# endif
edd309b7
JH
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) {
f7937171 760#ifdef USE_HOSTENT_BUFFER
edd309b7
JH
761 case OP_GHBYADDR:
762 case OP_GHBYNAME:
763 case OP_GHOSTENT:
764 {
f7937171
JH
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);
edd309b7
JH
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
f7937171 787#ifdef USE_GRENT_BUFFER
edd309b7
JH
788 case OP_GGRNAM:
789 case OP_GGRGID:
790 case OP_GGRENT:
791 {
f7937171 792 if (PL_reentrant_buffer->_grent_size <= REENTRANTHALFMAXSIZE) {
edd309b7 793 Gid_t gid;
f7937171
JH
794 PL_reentrant_buffer->_grent_size *= 2;
795 Renew(PL_reentrant_buffer->_grent_buffer,
796 PL_reentrant_buffer->_grent_size, char);
edd309b7
JH
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
f7937171 813#ifdef USE_NETENT_BUFFER
edd309b7
JH
814 case OP_GNBYADDR:
815 case OP_GNBYNAME:
816 case OP_GNETENT:
817 {
f7937171 818 if (PL_reentrant_buffer->_netent_size <= REENTRANTHALFMAXSIZE) {
edd309b7 819 Netdb_net_t net;
f7937171
JH
820 PL_reentrant_buffer->_netent_size *= 2;
821 Renew(PL_reentrant_buffer->_netent_buffer,
822 PL_reentrant_buffer->_netent_size, char);
edd309b7
JH
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
f7937171 840#ifdef USE_PWENT_BUFFER
edd309b7
JH
841 case OP_GPWNAM:
842 case OP_GPWUID:
843 case OP_GPWENT:
844 {
f7937171 845 if (PL_reentrant_buffer->_pwent_size <= REENTRANTHALFMAXSIZE) {
edd309b7 846 Uid_t uid;
f7937171
JH
847 PL_reentrant_buffer->_pwent_size *= 2;
848 Renew(PL_reentrant_buffer->_pwent_buffer,
849 PL_reentrant_buffer->_pwent_size, char);
edd309b7
JH
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
f7937171 866#ifdef USE_PROTOENT_BUFFER
edd309b7
JH
867 case OP_GPBYNAME:
868 case OP_GPBYNUMBER:
869 case OP_GPROTOENT:
870 {
f7937171
JH
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);
edd309b7
JH
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
f7937171 891#ifdef USE_SERVENT_BUFFER
edd309b7
JH
892 case OP_GSBYNAME:
893 case OP_GSBYPORT:
894 case OP_GSERVENT:
895 {
f7937171
JH
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);
edd309b7
JH
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
10bc17b6
JH
928EOF
929
930__DATA__
931asctime S |time |const struct tm|B_SB|B_SBI|I_SB|I_SBI
b430fd04 932crypt CC |crypt |struct crypt_data|B_CCS|B_CCD|D=CRYPTD*
10bc17b6
JH
933ctermid B |stdio | |B_B
934ctime S |time |const time_t |B_SB|B_SBI|I_SB|I_SBI
935drand48 |stdlib |struct drand48_data |I_ST|T=double*
936endgrent |grp | |I_H|V_H
31ee0cb7
JH
937endhostent |netdb | |I_D|V_D|D=struct hostent_data*
938endnetent |netdb | |I_D|V_D|D=struct netent_data*
939endprotoent |netdb | |I_D|V_D|D=struct protoent_data*
10bc17b6 940endpwent |pwd | |I_H|V_H
31ee0cb7 941endservent |netdb | |I_D|V_D|D=struct servent_data*
10bc17b6
JH
942getgrent |grp |struct group |I_SBWR|I_SBIR|S_SBW|S_SBI|I_SBI|I_SBIH
943getgrgid T |grp |struct group |I_TSBWR|I_TSBIR|I_TSBI|S_TSBI|T=gid_t
944getgrnam C |grp |struct group |I_CSBWR|I_CSBIR|S_CBI|I_CSBI|S_CSBI
945gethostbyaddr 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*
946gethostbyname C |netdb |struct hostent |I_CSBWRE|S_CSBIE|I_CSD|D=struct hostent_data*
947gethostent |netdb |struct hostent |I_SBWRE|I_SBIE|S_SBIE|S_SBI|I_SBI|I_SD|D=struct hostent_data*
948getlogin |unistd | |I_BW|I_BI|B_BW|B_BI
949getnetbyaddr 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
950getnetbyname C |netdb |struct netent |I_CSBWRE|I_CSBI|S_CSBI|I_CSD|D=struct netent_data*
951getnetent |netdb |struct netent |I_SBWRE|I_SBIE|S_SBIE|S_SBI|I_SBI|I_SD|D=struct netent_data*
952getprotobyname C|netdb |struct protoent|I_CSBWR|S_CSBI|I_CSD|D=struct protoent_data*
953getprotobynumber I |netdb |struct protoent|I_ISBWR|S_ISBI|I_ISD|D=struct protoent_data*
954getprotoent |netdb |struct protoent|I_SBWR|I_SBI|S_SBI|I_SD|D=struct protoent_data*
955getpwent |pwd |struct passwd |I_SBWR|I_SBIR|S_SBW|S_SBI|I_SBI|I_SBIH
956getpwnam C |pwd |struct passwd |I_CSBWR|I_CSBIR|S_CSBI|I_CSBI
957getpwuid T |pwd |struct passwd |I_TSBWR|I_TSBIR|I_TSBI|S_TSBI|T=uid_t
958getservbyname CC|netdb |struct servent |I_CCSBWR|S_CCSBI|I_CCSD|D=struct servent_data*
959getservbyport IC|netdb |struct servent |I_ICSBWR|S_ICSBI|I_ICSD|D=struct servent_data*
960getservent |netdb |struct servent |I_SBWR|I_SBI|S_SBI|I_SD|D=struct servent_data*
961getspnam C |shadow |struct spwd |I_CSBWR|S_CSBI
962gmtime T |time |struct tm |S_TS|I_TS|T=const time_t*
963localtime T |time |struct tm |S_TS|I_TS|T=const time_t*
964random |stdlib |struct random_data|I_TS|T=int*
965readdir T |dirent |struct dirent |I_TSR|I_TS|T=DIR*
966readdir64 T |dirent |struct dirent64|I_TSR|I_TS|T=DIR*
967setgrent |grp | |I_H|V_H
968sethostent I |netdb | |I_ID|V_ID|D=struct hostent_data*
969setlocale IC |locale | |I_ICBI
970setnetent I |netdb | |I_ID|V_ID|D=struct netent_data*
971setprotoent I |netdb | |I_ID|V_ID|D=struct protoent_data*
972setpwent |pwd | |I_H|V_H
973setservent I |netdb | |I_ID|V_ID|D=struct servent_data*
974srand48 L |stdlib |struct drand48_data |I_LS
975srandom T |stdlib |struct random_data|I_TS|T=unsigned int
976strerror I |string | |I_IBW|I_IBI|B_IBW
977tmpnam B |stdio | |B_B
978ttyname I |unistd | |I_IBW|I_IBI|B_IBI