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