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