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