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