fix bignum test failures
[p5sagit/p5-mst-13.2.git] / reentr.pl
CommitLineData
10bc17b6 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 67# undef HAS_ENDGRENT_R
68# undef HAS_ENDPWENT_R
10bc17b6 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
aa418cf1 131my %seenh; # the different prototypes signatures for this function
132my %seena; # the different prototypes signatures for this function in order
133my @seenf; # all the seen functions
134my %seenp; # the different prototype signatures for all functions
135my %seent; # the return type of this function
136my %seens; # the type of this function's "S"
137my %seend; # the type of this function's "D"
138my %seenu; # the length of the argument list of this function
139
140while (<DATA>) { # Read in the protypes.
10bc17b6 141 next if /^\s+$/;
142 chomp;
aa418cf1 143 my ($func, $hdr, $type, @p) = split(/\s*\|\s*/, $_, -1);
10bc17b6 144 my $u;
aa418cf1 145 # Split off the real function name and the argument list.
146 ($func, $u) = split(' ', $func);
147 $seenu{$func} = defined $u ? length $u : 0;
148 my $FUNC = uc $func; # for output.
149 push @seenf, $func;
10bc17b6 150 my %m = %map;
aa418cf1 151 if ($type) {
152 $m{S} = "$type*";
153 $m{R} = "$type**";
10bc17b6 154 }
aa418cf1 155
156 # Set any special mapping variables (like X=x_t)
10bc17b6 157 if (@p) {
158 while ($p[-1] =~ /=/) {
159 my ($k, $v) = ($p[-1] =~ /^([A-Za-z])\s*=\s*(.*)/);
160 $m{$k} = $v;
161 pop @p;
162 }
163 }
aa418cf1 164
165 # If given the -U option open up the metaconfig unit for this function.
166 if ($opts{U} && open(U, ">d_${func}_r.U")) {
10bc17b6 167 select U;
168 }
aa418cf1 169
d63eadf0 170 if ($opts{U}) {
aa418cf1 171 # The metaconfig units needs prerequisite dependencies.
172 my $prereqs = '';
173 my $prereqh = '';
174 my $prereqsh = '';
175 if ($hdr ne 'stdio') { # There's no i_stdio.
176 $prereqs = "i_$hdr";
177 $prereqh = "$hdr.h";
178 $prereqsh = "\$$prereqs $prereqh";
179 }
1fdb6f84 180 my @prereq = qw(Inlibc Protochk Hasproto i_systypes usethreads);
181 push @prereq, $prereqs;
182 my $hdrs = "\$i_systypes sys/types.h define stdio.h $prereqsh";
aa418cf1 183 if ($hdr eq 'time') {
1fdb6f84 184 $hdrs .= " \$i_systime sys/time.h";
185 push @prereq, 'i_systime';
186 }
aa418cf1 187 # Output the metaconfig unit header.
d63eadf0 188 print <<EOF;
aa418cf1 189?RCS: \$Id: d_${func}_r.U,v $
10bc17b6 190?RCS:
191?RCS: Copyright (c) 2002 Jarkko Hietaniemi
192?RCS:
193?RCS: You may distribute under the terms of either the GNU General Public
194?RCS: License or the Artistic License, as specified in the README file.
195?RCS:
196?RCS: Generated by the reentr.pl from the Perl 5.8 distribution.
197?RCS:
aa418cf1 198?MAKE:d_${func}_r ${func}_r_proto: @prereq
10bc17b6 199?MAKE: -pick add \$@ %<
aa418cf1 200?S:d_${func}_r:
201?S: This variable conditionally defines the HAS_${FUNC}_R symbol,
202?S: which indicates to the C program that the ${func}_r()
10bc17b6 203?S: routine is available.
204?S:.
aa418cf1 205?S:${func}_r_proto:
206?S: This variable encodes the prototype of ${func}_r.
39183afa 207?S: It is zero if d_${func}_r is undef, and one of the
208?S: REENTRANT_PROTO_T_ABC macros of reentr.h if d_${func}_r
209?S: is defined.
10bc17b6 210?S:.
aa418cf1 211?C:HAS_${FUNC}_R:
212?C: This symbol, if defined, indicates that the ${func}_r routine
213?C: is available to ${func} re-entrantly.
10bc17b6 214?C:.
aa418cf1 215?C:${FUNC}_R_PROTO:
216?C: This symbol encodes the prototype of ${func}_r.
39183afa 217?C: It is zero if d_${func}_r is undef, and one of the
218?C: REENTRANT_PROTO_T_ABC macros of reentr.h if d_${func}_r
219?C: is defined.
10bc17b6 220?C:.
aa418cf1 221?H:#\$d_${func}_r HAS_${FUNC}_R /**/
222?H:#define ${FUNC}_R_PROTO \$${func}_r_proto /**/
10bc17b6 223?H:.
aa418cf1 224?T:try hdrs d_${func}_r_proto
225?LINT:set d_${func}_r
226?LINT:set ${func}_r_proto
227: see if ${func}_r exists
228set ${func}_r d_${func}_r
10bc17b6 229eval \$inlibc
aa418cf1 230case "\$d_${func}_r" in
10bc17b6 231"\$define")
d63eadf0 232EOF
d63eadf0 233 print <<EOF;
234 hdrs="$hdrs"
aa418cf1 235 case "\$d_${func}_r_proto:\$usethreads" in
236 ":define") d_${func}_r_proto=define
237 set d_${func}_r_proto ${func}_r \$hdrs
a48ec845 238 eval \$hasproto ;;
239 *) ;;
240 esac
aa418cf1 241 case "\$d_${func}_r_proto" in
a48ec845 242 define)
10bc17b6 243EOF
d63eadf0 244 }
245 for my $p (@p) {
246 my ($r, $a) = ($p =~ /^(.)_(.+)/);
247 my $v = join(", ", map { $m{$_} } split '', $a);
248 if ($opts{U}) {
249 print <<EOF ;
aa418cf1 250 case "\$${func}_r_proto" in
251 ''|0) try='$m{$r} ${func}_r($v);'
252 ./protochk "extern \$try" \$hdrs && ${func}_r_proto=$p ;;
10bc17b6 253 esac
254EOF
d63eadf0 255 }
aa418cf1 256 $seenh{$func}->{$p}++;
257 push @{$seena{$func}}, $p;
d63eadf0 258 $seenp{$p}++;
aa418cf1 259 $seent{$func} = $type;
260 $seens{$func} = $m{S};
261 $seend{$func} = $m{D};
d63eadf0 262 }
263 if ($opts{U}) {
264 print <<EOF;
aa418cf1 265 case "\$${func}_r_proto" in
266 ''|0) d_${func}_r=undef
267 ${func}_r_proto=0
268 echo "Disabling ${func}_r, cannot determine prototype." >&4 ;;
269 * ) case "\$${func}_r_proto" in
10bc17b6 270 REENTRANT_PROTO*) ;;
aa418cf1 271 *) ${func}_r_proto="REENTRANT_PROTO_\$${func}_r_proto" ;;
10bc17b6 272 esac
273 echo "Prototype: \$try" ;;
274 esac
275 ;;
c18e646a 276 *) case "\$usethreads" in
aa418cf1 277 define) echo "${func}_r has no prototype, not using it." >&4 ;;
c18e646a 278 esac
aa418cf1 279 d_${func}_r=undef
280 ${func}_r_proto=0
c18e646a 281 ;;
a48ec845 282 esac
283 ;;
aa418cf1 284*) ${func}_r_proto=0
10bc17b6 285 ;;
286esac
287
288EOF
289 close(U);
290 }
291}
292
293close DATA;
294
aa418cf1 295# Prepare to continue writing the reentr.h.
296
10bc17b6 297select H;
298
299{
aa418cf1 300 # Write out all the known prototype signatures.
10bc17b6 301 my $i = 1;
302 for my $p (sort keys %seenp) {
303 print "#define REENTRANT_PROTO_${p} ${i}\n";
304 $i++;
305 }
306}
307
aa418cf1 308my @struct; # REENTR struct members
309my @size; # struct member buffer size initialization code
310my @init; # struct member buffer initialization (malloc) code
311my @free; # struct member buffer release (free) code
312my @wrap; # the wrapper (foo(a) -> foo_r(a,...)) cpp code
313my @define; # defines for optional features
314
10bc17b6 315sub ifprotomatch {
aa418cf1 316 my $FUNC = shift;
317 join " || ", map { "${FUNC}_R_PROTO == REENTRANT_PROTO_$_" } @_;
10bc17b6 318}
319
10bc17b6 320sub pushssif {
321 push @struct, @_;
322 push @size, @_;
323 push @init, @_;
324 push @free, @_;
325}
326
327sub pushinitfree {
aa418cf1 328 my $func = shift;
10bc17b6 329 push @init, <<EOF;
aa418cf1 330 New(31338, PL_reentrant_buffer->_${func}_buffer, PL_reentrant_buffer->_${func}_size, char);
10bc17b6 331EOF
332 push @free, <<EOF;
aa418cf1 333 Safefree(PL_reentrant_buffer->_${func}_buffer);
10bc17b6 334EOF
335}
336
337sub define {
338 my ($n, $p, @F) = @_;
339 my @H;
340 my $H = uc $F[0];
341 push @define, <<EOF;
342/* The @F using \L$n? */
343
344EOF
aa418cf1 345 my $GENFUNC;
346 for my $func (@F) {
347 my $FUNC = uc $func;
348 my $HAS = "${FUNC}_R_HAS_$n";
349 push @H, $HAS;
350 my @h = grep { /$p/ } @{$seena{$func}};
351 unless (defined $GENFUNC) {
352 $GENFUNC = $FUNC;
353 $GENFUNC =~ s/^GET//;
f7937171 354 }
10bc17b6 355 if (@h) {
aa418cf1 356 push @define, "#if defined(HAS_${FUNC}_R) && (" . join(" || ", map { "${FUNC}_R_PROTO == REENTRANT_PROTO_$_" } @h) . ")\n";
10bc17b6 357
358 push @define, <<EOF;
aa418cf1 359# define $HAS
10bc17b6 360#else
aa418cf1 361# undef $HAS
10bc17b6 362#endif
363EOF
364 }
365 }
366 push @define, <<EOF;
367
368/* Any of the @F using \L$n? */
369
370EOF
371 push @define, "#if (" . join(" || ", map { "defined($_)" } @H) . ")\n";
372 push @define, <<EOF;
aa418cf1 373# define USE_${GENFUNC}_$n
10bc17b6 374#else
aa418cf1 375# undef USE_${GENFUNC}_$n
10bc17b6 376#endif
377
378EOF
379}
380
edd309b7 381define('BUFFER', 'B',
382 qw(getgrent getgrgid getgrnam));
383
10bc17b6 384define('PTR', 'R',
385 qw(getgrent getgrgid getgrnam));
386define('PTR', 'R',
387 qw(getpwent getpwnam getpwuid));
388define('PTR', 'R',
389 qw(getspent getspnam));
390
391define('FPTR', 'H',
f7937171 392 qw(getgrent getgrgid getgrnam setgrent endgrent));
10bc17b6 393define('FPTR', 'H',
f7937171 394 qw(getpwent getpwnam getpwuid setpwent endpwent));
10bc17b6 395
edd309b7 396define('BUFFER', 'B',
397 qw(getpwent getpwgid getpwnam));
398
10bc17b6 399define('PTR', 'R',
400 qw(gethostent gethostbyaddr gethostbyname));
401define('PTR', 'R',
402 qw(getnetent getnetbyaddr getnetbyname));
403define('PTR', 'R',
404 qw(getprotoent getprotobyname getprotobynumber));
405define('PTR', 'R',
406 qw(getservent getservbyname getservbyport));
407
edd309b7 408define('BUFFER', 'B',
409 qw(gethostent gethostbyaddr gethostbyname));
410define('BUFFER', 'B',
411 qw(getnetent getnetbyaddr getnetbyname));
412define('BUFFER', 'B',
413 qw(getprotoent getprotobyname getprotobynumber));
414define('BUFFER', 'B',
415 qw(getservent getservbyname getservbyport));
416
10bc17b6 417define('ERRNO', 'E',
418 qw(gethostent gethostbyaddr gethostbyname));
419define('ERRNO', 'E',
420 qw(getnetent getnetbyaddr getnetbyname));
421
aa418cf1 422# The following loop accumulates the "ssif" (struct, size, init, free)
423# sections that declare the struct members (in reentr.h), and the buffer
424# size initialization, buffer initialization (malloc), and buffer
425# release (free) code (in reentr.c).
426#
427# The loop also contains a lot of intrinsic logic about groups of
428# functions (since functions of certain kind operate the same way).
429
430for my $func (@seenf) {
431 my $FUNC = uc $func;
432 my $ifdef = "#ifdef HAS_${FUNC}_R\n";
433 my $endif = "#endif /* HAS_${FUNC}_R */\n";
434 if (exists $seena{$func}) {
435 my @p = @{$seena{$func}};
436 if ($func =~ /^(asctime|ctime|getlogin|setlocale|strerror|ttyname)$/) {
10bc17b6 437 pushssif $ifdef;
438 push @struct, <<EOF;
aa418cf1 439 char* _${func}_buffer;
440 size_t _${func}_size;
10bc17b6 441EOF
442 push @size, <<EOF;
aa418cf1 443 PL_reentrant_buffer->_${func}_size = REENTRANTSMALLSIZE;
10bc17b6 444EOF
aa418cf1 445 pushinitfree $func;
10bc17b6 446 pushssif $endif;
447 }
aa418cf1 448 elsif ($func =~ /^(crypt)$/) {
10bc17b6 449 pushssif $ifdef;
450 push @struct, <<EOF;
b430fd04 451#if CRYPT_R_PROTO == REENTRANT_PROTO_B_CCD
aa418cf1 452 $seend{$func} _${func}_data;
b430fd04 453#else
aa418cf1 454 $seent{$func} _${func}_struct;
b430fd04 455#endif
10bc17b6 456EOF
b430fd04 457 push @init, <<EOF;
10bc17b6 458#ifdef __GLIBC__
aa418cf1 459 PL_reentrant_buffer->_${func}_struct.initialized = 0;
10bc17b6 460#endif
461EOF
b430fd04 462 pushssif $endif;
463 }
aa418cf1 464 elsif ($func =~ /^(drand48|gmtime|localtime|random)$/) {
b430fd04 465 pushssif $ifdef;
466 push @struct, <<EOF;
aa418cf1 467 $seent{$func} _${func}_struct;
b430fd04 468EOF
10bc17b6 469 if ($1 eq 'drand48') {
470 push @struct, <<EOF;
aa418cf1 471 double _${func}_double;
10bc17b6 472EOF
473 }
474 pushssif $endif;
475 }
aa418cf1 476 elsif ($func =~ /^(getgrnam|getpwnam|getspnam)$/) {
10bc17b6 477 pushssif $ifdef;
aa418cf1 478 # 'genfunc' can be read either as 'generic' or 'genre',
479 # it represents a group of functions.
480 my $genfunc = $func;
481 $genfunc =~ s/nam/ent/g;
482 $genfunc =~ s/^get//;
483 my $GENFUNC = uc $genfunc;
10bc17b6 484 push @struct, <<EOF;
aa418cf1 485 $seent{$func} _${genfunc}_struct;
486 char* _${genfunc}_buffer;
487 size_t _${genfunc}_size;
10bc17b6 488EOF
489 push @struct, <<EOF;
aa418cf1 490# ifdef USE_${GENFUNC}_PTR
491 $seent{$func}* _${genfunc}_ptr;
10bc17b6 492# endif
493EOF
aa418cf1 494 if ($genfunc eq 'getspent') {
10bc17b6 495 push @size, <<EOF;
aa418cf1 496 PL_reentrant_buffer->_${genfunc}_size = 1024;
10bc17b6 497EOF
498 } else {
499 push @struct, <<EOF;
aa418cf1 500# ifdef USE_${GENFUNC}_FPTR
501 FILE* _${genfunc}_fptr;
10bc17b6 502# endif
503EOF
504 push @init, <<EOF;
aa418cf1 505# ifdef USE_${GENFUNC}_FPTR
506 PL_reentrant_buffer->_${genfunc}_fptr = NULL;
10bc17b6 507# endif
508EOF
aa418cf1 509 my $sc = $genfunc eq 'getgrent' ?
10bc17b6 510 '_SC_GETGR_R_SIZE_MAX' : '_SC_GETPW_R_SIZE_MAX';
aa418cf1 511 my $sz = $genfunc eq 'getgrent' ?
f7937171 512 '_grent_size' : '_pwent_size';
10bc17b6 513 push @size, <<EOF;
514# if defined(HAS_SYSCONF) && defined($sc) && !defined(__GLIBC__)
aa418cf1 515 PL_reentrant_buffer->_${genfunc}_size = sysconf($sc);
e3410746 516 if (PL_reentrant_buffer->$sz == -1)
517 PL_reentrant_buffer->$sz = REENTRANTUSUALSIZE;
10bc17b6 518# else
519# if defined(__osf__) && defined(__alpha) && defined(SIABUFSIZ)
aa418cf1 520 PL_reentrant_buffer->_${genfunc}_size = SIABUFSIZ;
10bc17b6 521# else
522# ifdef __sgi
aa418cf1 523 PL_reentrant_buffer->_${genfunc}_size = BUFSIZ;
10bc17b6 524# else
aa418cf1 525 PL_reentrant_buffer->_${genfunc}_size = REENTRANTUSUALSIZE;
10bc17b6 526# endif
527# endif
528# endif
529EOF
530 }
aa418cf1 531 pushinitfree $genfunc;
10bc17b6 532 pushssif $endif;
533 }
aa418cf1 534 elsif ($func =~ /^(gethostbyname|getnetbyname|getservbyname|getprotobyname)$/) {
10bc17b6 535 pushssif $ifdef;
aa418cf1 536 my $genfunc = $func;
537 $genfunc =~ s/byname/ent/;
538 $genfunc =~ s/^get//;
539 my $GENFUNC = uc $genfunc;
540 my $D = ifprotomatch($FUNC, grep {/D/} @p);
541 my $d = $seend{$func};
31ee0cb7 542 $d =~ s/\*$//; # snip: we need need the base type.
10bc17b6 543 push @struct, <<EOF;
aa418cf1 544 $seent{$func} _${genfunc}_struct;
10bc17b6 545# if $D
aa418cf1 546 $d _${genfunc}_data;
10bc17b6 547# else
aa418cf1 548 char* _${genfunc}_buffer;
549 size_t _${genfunc}_size;
10bc17b6 550# endif
aa418cf1 551# ifdef USE_${GENFUNC}_PTR
552 $seent{$func}* _${genfunc}_ptr;
10bc17b6 553# endif
554EOF
555 push @struct, <<EOF;
aa418cf1 556# ifdef USE_${GENFUNC}_ERRNO
557 int _${genfunc}_errno;
10bc17b6 558# endif
559EOF
560 push @size, <<EOF;
561#if !($D)
aa418cf1 562 PL_reentrant_buffer->_${genfunc}_size = REENTRANTUSUALSIZE;
10bc17b6 563#endif
564EOF
565 push @init, <<EOF;
566#if !($D)
aa418cf1 567 New(31338, PL_reentrant_buffer->_${genfunc}_buffer, PL_reentrant_buffer->_${genfunc}_size, char);
10bc17b6 568#endif
569EOF
570 push @free, <<EOF;
571#if !($D)
aa418cf1 572 Safefree(PL_reentrant_buffer->_${genfunc}_buffer);
10bc17b6 573#endif
574EOF
575 pushssif $endif;
576 }
aa418cf1 577 elsif ($func =~ /^(readdir|readdir64)$/) {
10bc17b6 578 pushssif $ifdef;
aa418cf1 579 my $R = ifprotomatch($FUNC, grep {/R/} @p);
10bc17b6 580 push @struct, <<EOF;
aa418cf1 581 $seent{$func}* _${func}_struct;
582 size_t _${func}_size;
10bc17b6 583# if $R
aa418cf1 584 $seent{$func}* _${func}_ptr;
10bc17b6 585# endif
586EOF
587 push @size, <<EOF;
588 /* This is the size Solaris recommends.
589 * (though we go static, should use pathconf() instead) */
aa418cf1 590 PL_reentrant_buffer->_${func}_size = sizeof($seent{$func}) + MAXPATHLEN + 1;
10bc17b6 591EOF
592 push @init, <<EOF;
aa418cf1 593 PL_reentrant_buffer->_${func}_struct = ($seent{$func}*)safemalloc(PL_reentrant_buffer->_${func}_size);
10bc17b6 594EOF
595 push @free, <<EOF;
aa418cf1 596 Safefree(PL_reentrant_buffer->_${func}_struct);
10bc17b6 597EOF
598 pushssif $endif;
599 }
600
601 push @wrap, $ifdef;
602
10bc17b6 603 push @wrap, <<EOF;
aa418cf1 604# undef $func
10bc17b6 605EOF
aa418cf1 606
607 # Write out what we have learned.
608
10bc17b6 609 my @v = 'a'..'z';
aa418cf1 610 my $v = join(", ", @v[0..$seenu{$func}-1]);
10bc17b6 611 for my $p (@p) {
612 my ($r, $a) = split '_', $p;
613 my $test = $r eq 'I' ? ' == 0' : '';
614 my $true = 1;
aa418cf1 615 my $genfunc = $func;
616 if ($genfunc =~ /^(?:get|set|end)(pw|gr|host|net|proto|serv|sp)/) {
617 $genfunc = "${1}ent";
618 } elsif ($genfunc eq 'srand48') {
619 $genfunc = "drand48";
10bc17b6 620 }
621 my $b = $a;
622 my $w = '';
aa418cf1 623 substr($b, 0, $seenu{$func}) = '';
10bc17b6 624 if ($b =~ /R/) {
aa418cf1 625 $true = "PL_reentrant_buffer->_${genfunc}_ptr";
626 } elsif ($b =~ /T/ && $func eq 'drand48') {
627 $true = "PL_reentrant_buffer->_${genfunc}_double";
10bc17b6 628 } elsif ($b =~ /S/) {
aa418cf1 629 if ($func =~ /^readdir/) {
630 $true = "PL_reentrant_buffer->_${genfunc}_struct";
10bc17b6 631 } else {
aa418cf1 632 $true = "&PL_reentrant_buffer->_${genfunc}_struct";
10bc17b6 633 }
634 } elsif ($b =~ /B/) {
aa418cf1 635 $true = "PL_reentrant_buffer->_${genfunc}_buffer";
10bc17b6 636 }
637 if (length $b) {
638 $w = join ", ",
639 map {
640 $_ eq 'R' ?
aa418cf1 641 "&PL_reentrant_buffer->_${genfunc}_ptr" :
10bc17b6 642 $_ eq 'E' ?
aa418cf1 643 "&PL_reentrant_buffer->_${genfunc}_errno" :
10bc17b6 644 $_ eq 'B' ?
aa418cf1 645 "PL_reentrant_buffer->_${genfunc}_buffer" :
10bc17b6 646 $_ =~ /^[WI]$/ ?
aa418cf1 647 "PL_reentrant_buffer->_${genfunc}_size" :
10bc17b6 648 $_ eq 'H' ?
aa418cf1 649 "&PL_reentrant_buffer->_${genfunc}_fptr" :
10bc17b6 650 $_ eq 'D' ?
aa418cf1 651 "&PL_reentrant_buffer->_${genfunc}_data" :
10bc17b6 652 $_ eq 'S' ?
aa418cf1 653 ($func =~ /^readdir/ ?
654 "PL_reentrant_buffer->_${genfunc}_struct" :
655 "&PL_reentrant_buffer->_${genfunc}_struct" ) :
656 $_ eq 'T' && $func eq 'drand48' ?
657 "&PL_reentrant_buffer->_${genfunc}_double" :
10bc17b6 658 $_
659 } split '', $b;
660 $w = ", $w" if length $v;
661 }
aa418cf1 662 my $call = "${func}_r($v$w)";
10bc17b6 663 $call = "((errno = $call))" if $r eq 'I';
664 push @wrap, <<EOF;
aa418cf1 665# if !defined($func) && ${FUNC}_R_PROTO == REENTRANT_PROTO_$p
10bc17b6 666EOF
667 if ($r eq 'V' || $r eq 'B') {
668 push @wrap, <<EOF;
aa418cf1 669# define $func($v) $call
10bc17b6 670EOF
671 } else {
aa418cf1 672 if ($func =~ /^get/) {
edd309b7 673 my $rv = $v ? ", $v" : "";
674 push @wrap, <<EOF;
aa418cf1 675# define $func($v) ($call$test ? $true : (errno == ERANGE ? Perl_reentrant_retry("$func"$rv) : 0))
10bc17b6 676EOF
edd309b7 677 } else {
678 push @wrap, <<EOF;
aa418cf1 679# define $func($v) ($call$test ? $true : 0)
edd309b7 680EOF
681 }
10bc17b6 682 }
683 push @wrap, <<EOF;
684# endif
685EOF
686 }
687
688 push @wrap, $endif, "\n";
689 }
690}
691
692local $" = '';
693
694print <<EOF;
695
696/* Defines for indicating which special features are supported. */
697
698@define
699typedef struct {
700@struct
aa418cf1 701 int dummy; /* cannot have empty structs */
10bc17b6 702} REENTR;
703
704/* The wrappers. */
705
706@wrap
707#endif /* USE_REENTRANT_API */
708
709#endif
710
711EOF
712
713close(H);
714
aa418cf1 715# Prepare to write the reentr.c.
716
10bc17b6 717die "reentr.c: $!" unless open(C, ">reentr.c");
718select C;
719print <<EOF;
720/*
721 * reentr.c
722 *
723 * Copyright (c) 1997-2002, Larry Wall
724 *
725 * You may distribute under the terms of either the GNU General Public
726 * License or the Artistic License, as specified in the README file.
727 *
728 * !!!!!!! DO NOT EDIT THIS FILE !!!!!!!
729 * This file is built by reentrl.pl from data in reentr.pl.
730 *
731 * "Saruman," I said, standing away from him, "only one hand at a time can
732 * wield the One, and you know that well, so do not trouble to say we!"
733 *
734 */
735
736#include "EXTERN.h"
737#define PERL_IN_REENTR_C
738#include "perl.h"
739#include "reentr.h"
740
741void
742Perl_reentrant_size(pTHX) {
743#ifdef USE_REENTRANT_API
8695fa85 744#define REENTRANTSMALLSIZE 256 /* Make something up. */
745#define REENTRANTUSUALSIZE 4096 /* Make something up. */
10bc17b6 746@size
747#endif /* USE_REENTRANT_API */
748}
749
750void
751Perl_reentrant_init(pTHX) {
752#ifdef USE_REENTRANT_API
753 New(31337, PL_reentrant_buffer, 1, REENTR);
754 Perl_reentrant_size(aTHX);
755@init
756#endif /* USE_REENTRANT_API */
757}
758
759void
760Perl_reentrant_free(pTHX) {
761#ifdef USE_REENTRANT_API
762@free
763 Safefree(PL_reentrant_buffer);
764#endif /* USE_REENTRANT_API */
765}
766
edd309b7 767void*
768Perl_reentrant_retry(const char *f, ...)
769{
770 dTHX;
771 void *retptr = NULL;
772#ifdef USE_REENTRANT_API
f7937171 773# 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 774 void *p0;
775# endif
f7937171 776# if defined(USE_SERVENT_BUFFER)
e3410746 777 void *p1;
778# endif
f7937171 779# if defined(USE_HOSTENT_BUFFER)
edd309b7 780 size_t asize;
e3410746 781# endif
f7937171 782# if defined(USE_HOSTENT_BUFFER) || defined(USE_NETENT_BUFFER) || defined(USE_PROTOENT_BUFFER) || defined(USE_SERVENT_BUFFER)
edd309b7 783 int anint;
e3410746 784# endif
edd309b7 785 va_list ap;
786
787 va_start(ap, f);
788
789#define REENTRANTHALFMAXSIZE 32768 /* The maximum may end up twice this. */
790
791 switch (PL_op->op_type) {
f7937171 792#ifdef USE_HOSTENT_BUFFER
edd309b7 793 case OP_GHBYADDR:
794 case OP_GHBYNAME:
795 case OP_GHOSTENT:
796 {
f7937171 797 if (PL_reentrant_buffer->_hostent_size <= REENTRANTHALFMAXSIZE) {
798 PL_reentrant_buffer->_hostent_size *= 2;
799 Renew(PL_reentrant_buffer->_hostent_buffer,
800 PL_reentrant_buffer->_hostent_size, char);
edd309b7 801 switch (PL_op->op_type) {
802 case OP_GHBYADDR:
803 p0 = va_arg(ap, void *);
804 asize = va_arg(ap, size_t);
805 anint = va_arg(ap, int);
806 retptr = gethostbyaddr(p0, asize, anint); break;
807 case OP_GHBYNAME:
808 p0 = va_arg(ap, void *);
809 retptr = gethostbyname(p0); break;
810 case OP_GHOSTENT:
811 retptr = gethostent(); break;
812 default:
813 break;
814 }
815 }
816 }
817 break;
818#endif
f7937171 819#ifdef USE_GRENT_BUFFER
edd309b7 820 case OP_GGRNAM:
821 case OP_GGRGID:
822 case OP_GGRENT:
823 {
f7937171 824 if (PL_reentrant_buffer->_grent_size <= REENTRANTHALFMAXSIZE) {
edd309b7 825 Gid_t gid;
f7937171 826 PL_reentrant_buffer->_grent_size *= 2;
827 Renew(PL_reentrant_buffer->_grent_buffer,
828 PL_reentrant_buffer->_grent_size, char);
edd309b7 829 switch (PL_op->op_type) {
830 case OP_GGRNAM:
831 p0 = va_arg(ap, void *);
832 retptr = getgrnam(p0); break;
833 case OP_GGRGID:
834 gid = va_arg(ap, Gid_t);
835 retptr = getgrgid(gid); break;
836 case OP_GGRENT:
837 retptr = getgrent(); break;
838 default:
839 break;
840 }
841 }
842 }
843 break;
844#endif
f7937171 845#ifdef USE_NETENT_BUFFER
edd309b7 846 case OP_GNBYADDR:
847 case OP_GNBYNAME:
848 case OP_GNETENT:
849 {
f7937171 850 if (PL_reentrant_buffer->_netent_size <= REENTRANTHALFMAXSIZE) {
edd309b7 851 Netdb_net_t net;
f7937171 852 PL_reentrant_buffer->_netent_size *= 2;
853 Renew(PL_reentrant_buffer->_netent_buffer,
854 PL_reentrant_buffer->_netent_size, char);
edd309b7 855 switch (PL_op->op_type) {
856 case OP_GNBYADDR:
857 net = va_arg(ap, Netdb_net_t);
858 anint = va_arg(ap, int);
859 retptr = getnetbyaddr(net, anint); break;
860 case OP_GNBYNAME:
861 p0 = va_arg(ap, void *);
862 retptr = getnetbyname(p0); break;
863 case OP_GNETENT:
864 retptr = getnetent(); break;
865 default:
866 break;
867 }
868 }
869 }
870 break;
871#endif
f7937171 872#ifdef USE_PWENT_BUFFER
edd309b7 873 case OP_GPWNAM:
874 case OP_GPWUID:
875 case OP_GPWENT:
876 {
f7937171 877 if (PL_reentrant_buffer->_pwent_size <= REENTRANTHALFMAXSIZE) {
edd309b7 878 Uid_t uid;
f7937171 879 PL_reentrant_buffer->_pwent_size *= 2;
880 Renew(PL_reentrant_buffer->_pwent_buffer,
881 PL_reentrant_buffer->_pwent_size, char);
edd309b7 882 switch (PL_op->op_type) {
883 case OP_GPWNAM:
884 p0 = va_arg(ap, void *);
885 retptr = getpwnam(p0); break;
886 case OP_GPWUID:
887 uid = va_arg(ap, Uid_t);
888 retptr = getpwuid(uid); break;
889 case OP_GPWENT:
890 retptr = getpwent(); break;
891 default:
892 break;
893 }
894 }
895 }
896 break;
897#endif
f7937171 898#ifdef USE_PROTOENT_BUFFER
edd309b7 899 case OP_GPBYNAME:
900 case OP_GPBYNUMBER:
901 case OP_GPROTOENT:
902 {
f7937171 903 if (PL_reentrant_buffer->_protoent_size <= REENTRANTHALFMAXSIZE) {
904 PL_reentrant_buffer->_protoent_size *= 2;
905 Renew(PL_reentrant_buffer->_protoent_buffer,
906 PL_reentrant_buffer->_protoent_size, char);
edd309b7 907 switch (PL_op->op_type) {
908 case OP_GPBYNAME:
909 p0 = va_arg(ap, void *);
910 retptr = getprotobyname(p0); break;
911 case OP_GPBYNUMBER:
912 anint = va_arg(ap, int);
913 retptr = getprotobynumber(anint); break;
914 case OP_GPROTOENT:
915 retptr = getprotoent(); break;
916 default:
917 break;
918 }
919 }
920 }
921 break;
922#endif
f7937171 923#ifdef USE_SERVENT_BUFFER
edd309b7 924 case OP_GSBYNAME:
925 case OP_GSBYPORT:
926 case OP_GSERVENT:
927 {
f7937171 928 if (PL_reentrant_buffer->_servent_size <= REENTRANTHALFMAXSIZE) {
929 PL_reentrant_buffer->_servent_size *= 2;
930 Renew(PL_reentrant_buffer->_servent_buffer,
931 PL_reentrant_buffer->_servent_size, char);
edd309b7 932 switch (PL_op->op_type) {
933 case OP_GSBYNAME:
934 p0 = va_arg(ap, void *);
935 p1 = va_arg(ap, void *);
936 retptr = getservbyname(p0, p1); break;
937 case OP_GSBYPORT:
938 anint = va_arg(ap, int);
939 p0 = va_arg(ap, void *);
940 retptr = getservbyport(anint, p0); break;
941 case OP_GSERVENT:
942 retptr = getservent(); break;
943 default:
944 break;
945 }
946 }
947 }
948 break;
949#endif
950 default:
951 /* Not known how to retry, so just fail. */
952 break;
953 }
954
955 va_end(ap);
956#endif
957 return retptr;
958}
959
10bc17b6 960EOF
961
962__DATA__
963asctime S |time |const struct tm|B_SB|B_SBI|I_SB|I_SBI
b430fd04 964crypt CC |crypt |struct crypt_data|B_CCS|B_CCD|D=CRYPTD*
10bc17b6 965ctermid B |stdio | |B_B
966ctime S |time |const time_t |B_SB|B_SBI|I_SB|I_SBI
967drand48 |stdlib |struct drand48_data |I_ST|T=double*
968endgrent |grp | |I_H|V_H
31ee0cb7 969endhostent |netdb | |I_D|V_D|D=struct hostent_data*
970endnetent |netdb | |I_D|V_D|D=struct netent_data*
971endprotoent |netdb | |I_D|V_D|D=struct protoent_data*
10bc17b6 972endpwent |pwd | |I_H|V_H
31ee0cb7 973endservent |netdb | |I_D|V_D|D=struct servent_data*
10bc17b6 974getgrent |grp |struct group |I_SBWR|I_SBIR|S_SBW|S_SBI|I_SBI|I_SBIH
975getgrgid T |grp |struct group |I_TSBWR|I_TSBIR|I_TSBI|S_TSBI|T=gid_t
976getgrnam C |grp |struct group |I_CSBWR|I_CSBIR|S_CBI|I_CSBI|S_CSBI
977gethostbyaddr 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*
978gethostbyname C |netdb |struct hostent |I_CSBWRE|S_CSBIE|I_CSD|D=struct hostent_data*
979gethostent |netdb |struct hostent |I_SBWRE|I_SBIE|S_SBIE|S_SBI|I_SBI|I_SD|D=struct hostent_data*
980getlogin |unistd | |I_BW|I_BI|B_BW|B_BI
981getnetbyaddr 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
982getnetbyname C |netdb |struct netent |I_CSBWRE|I_CSBI|S_CSBI|I_CSD|D=struct netent_data*
983getnetent |netdb |struct netent |I_SBWRE|I_SBIE|S_SBIE|S_SBI|I_SBI|I_SD|D=struct netent_data*
984getprotobyname C|netdb |struct protoent|I_CSBWR|S_CSBI|I_CSD|D=struct protoent_data*
985getprotobynumber I |netdb |struct protoent|I_ISBWR|S_ISBI|I_ISD|D=struct protoent_data*
986getprotoent |netdb |struct protoent|I_SBWR|I_SBI|S_SBI|I_SD|D=struct protoent_data*
987getpwent |pwd |struct passwd |I_SBWR|I_SBIR|S_SBW|S_SBI|I_SBI|I_SBIH
988getpwnam C |pwd |struct passwd |I_CSBWR|I_CSBIR|S_CSBI|I_CSBI
989getpwuid T |pwd |struct passwd |I_TSBWR|I_TSBIR|I_TSBI|S_TSBI|T=uid_t
990getservbyname CC|netdb |struct servent |I_CCSBWR|S_CCSBI|I_CCSD|D=struct servent_data*
991getservbyport IC|netdb |struct servent |I_ICSBWR|S_ICSBI|I_ICSD|D=struct servent_data*
992getservent |netdb |struct servent |I_SBWR|I_SBI|S_SBI|I_SD|D=struct servent_data*
993getspnam C |shadow |struct spwd |I_CSBWR|S_CSBI
994gmtime T |time |struct tm |S_TS|I_TS|T=const time_t*
995localtime T |time |struct tm |S_TS|I_TS|T=const time_t*
996random |stdlib |struct random_data|I_TS|T=int*
997readdir T |dirent |struct dirent |I_TSR|I_TS|T=DIR*
998readdir64 T |dirent |struct dirent64|I_TSR|I_TS|T=DIR*
999setgrent |grp | |I_H|V_H
1000sethostent I |netdb | |I_ID|V_ID|D=struct hostent_data*
1001setlocale IC |locale | |I_ICBI
1002setnetent I |netdb | |I_ID|V_ID|D=struct netent_data*
1003setprotoent I |netdb | |I_ID|V_ID|D=struct protoent_data*
1004setpwent |pwd | |I_H|V_H
1005setservent I |netdb | |I_ID|V_ID|D=struct servent_data*
1006srand48 L |stdlib |struct drand48_data |I_LS
1007srandom T |stdlib |struct random_data|I_TS|T=unsigned int
1008strerror I |string | |I_IBW|I_IBI|B_IBW
1009tmpnam B |stdio | |B_B
1010ttyname I |unistd | |I_IBW|I_IBI|B_IBI