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