4 # Generate the reentr.c and reentr.h,
5 # and optionally also the relevant metaconfig units (-U option).
15 A => "char*", # as an input argument
16 B => "char*", # as an output argument
17 C => "const char*", # as a read-only input argument
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".)
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".
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)
38 die "reentr.h: $!" unless open(H, ">reentr.h");
44 * Copyright (c) 1997-2002, Larry Wall
46 * You may distribute under the terms of either the GNU General Public
47 * License or the Artistic License, as specified in the README file.
49 * !!!!!!! DO NOT EDIT THIS FILE !!!!!!!
50 * This file is built by reentrl.pl from data in reentr.pl.
56 #ifdef USE_REENTRANT_API
58 /* Deprecations: some platforms have the said reentrant interfaces
59 * but they are declared obsolete and are not to be used. Often this
60 * means that the platform has threadsafed the interfaces (hopefully).
61 * All this is OS version dependent, so we are of course fooling ourselves.
62 * If you know of more deprecations on some platforms, please add your own. */
67 # undef HAS_ENDGRENT_R
68 # undef HAS_ENDPWENT_R
69 # undef HAS_GETGRENT_R
70 # undef HAS_GETPWENT_R
71 # undef HAS_SETLOCALE_R
73 # undef HAS_STRERROR_R
74 # define NETDB_R_OBSOLETE
77 #if defined(__osf__) && defined(__alpha) /* Tru64 aka Digital UNIX */
79 # undef HAS_STRERROR_R
80 # define NETDB_R_OBSOLETE
83 #ifdef NETDB_R_OBSOLETE
84 # undef HAS_ENDHOSTENT_R
85 # undef HAS_ENDNETENT_R
86 # undef HAS_ENDPROTOENT_R
87 # undef HAS_ENDSERVENT_R
88 # undef HAS_GETHOSTBYADDR_R
89 # undef HAS_GETHOSTBYNAME_R
90 # undef HAS_GETHOSTENT_R
91 # undef HAS_GETNETBYADDR_R
92 # undef HAS_GETNETBYNAME_R
93 # undef HAS_GETNETENT_R
94 # undef HAS_GETPROTOBYNAME_R
95 # undef HAS_GETPROTOBYNUMBER_R
96 # undef HAS_GETPROTOENT_R
97 # undef HAS_GETSERVBYNAME_R
98 # undef HAS_GETSERVBYPORT_R
99 # undef HAS_GETSERVENT_R
100 # undef HAS_SETHOSTENT_R
101 # undef HAS_SETNETENT_R
102 # undef HAS_SETPROTOENT_R
103 # undef HAS_SETSERVENT_R
116 # include <stdlib.h> /* drand48_data */
123 #ifdef HAS_GETSPNAM_R
143 my ($f, $h, $t, @p) = split(/\s*\|\s*/, $_, -1);
145 ($f, $u) = split(' ', $f);
146 $seenu{$f} = defined $u ? length $u : 0;
155 while ($p[-1] =~ /=/) {
156 my ($k, $v) = ($p[-1] =~ /^([A-Za-z])\s*=\s*(.*)/);
161 if ($opts{U} && open(U, ">d_${f}_r.U")) {
164 my $prereqh = $h eq 'stdio' ? '' : "i_$h"; # There's no i_stdio.
165 print <<EOF if $opts{U};
166 ?RCS: \$Id: d_${f}_r.U,v $
168 ?RCS: Copyright (c) 2002 Jarkko Hietaniemi
170 ?RCS: You may distribute under the terms of either the GNU General Public
171 ?RCS: License or the Artistic License, as specified in the README file.
173 ?RCS: Generated by the reentr.pl from the Perl 5.8 distribution.
175 ?MAKE:d_${f}_r ${f}_r_proto: Inlibc Protochk Hasproto i_systypes $prereqh usethreads
176 ?MAKE: -pick add \$@ %<
178 ?S: This variable conditionally defines the HAS_${F}_R symbol,
179 ?S: which indicates to the C program that the ${f}_r()
180 ?S: routine is available.
183 ?S: This variable encodes the prototype of ${f}_r.
186 ?C: This symbol, if defined, indicates that the ${f}_r routine
187 ?C: is available to ${f} re-entrantly.
190 ?C: This symbol encodes the prototype of ${f}_r.
192 ?H:#\$d_${f}_r HAS_${F}_R /**/
193 ?H:#define ${F}_R_PROTO \$${f}_r_proto /**/
195 ?T:try hdrs d_${f}_r_proto
197 ?LINT:set ${f}_r_proto
198 : see if ${f}_r exists
203 hdrs="\$i_systypes sys/types.h define stdio.h \$i_${h} $h.h"
204 case "\$d_${f}_r_proto:\$usethreads" in
205 ":define") d_${f}_r_proto=define
206 set d_${f}_r_proto ${f}_r \$hdrs
210 case "\$d_${f}_r_proto" in
214 my ($r, $a) = ($p =~ /^(.)_(.+)/);
215 my $v = join(", ", map { $m{$_} } split '', $a);
218 case "\$${f}_r_proto" in
219 ''|0) try='$m{$r} ${f}_r($v);'
220 ./protochk "extern \$try" \$hdrs && ${f}_r_proto=$p ;;
225 push @{$seena{$f}}, $p;
233 case "\$${f}_r_proto" in
236 echo "Disabling ${f}_r, cannot determine prototype." >&4 ;;
237 * ) case "\$${f}_r_proto" in
239 *) ${f}_r_proto="REENTRANT_PROTO_\$${f}_r_proto" ;;
241 echo "Prototype: \$try" ;;
244 *) case "\$usethreads" in
245 define) echo "${f}_r has no prototype, not using it." >&4 ;;
265 for my $p (sort keys %seenp) {
266 print "#define REENTRANT_PROTO_${p} ${i}\n";
273 join " || ", map { "${F}_R_PROTO == REENTRANT_PROTO_$_" } @_;
293 New(31338, PL_reentrant_buffer->_${f}_buffer, PL_reentrant_buffer->_${f}_size, char);
296 Safefree(PL_reentrant_buffer->_${f}_buffer);
301 my ($n, $p, @F) = @_;
305 /* The @F using \L$n? */
310 my $h = "${F}_R_HAS_$n";
312 my @h = grep { /$p/ } @{$seena{$f}};
314 push @define, "#if defined(HAS_${F}_R) && (" . join(" || ", map { "${F}_R_PROTO == REENTRANT_PROTO_$_" } @h) . ")\n";
326 /* Any of the @F using \L$n? */
329 push @define, "#if (" . join(" || ", map { "defined($_)" } @H) . ")\n";
339 define('BUFFER', 'B',
340 qw(getgrent getgrgid getgrnam));
343 qw(getgrent getgrgid getgrnam));
345 qw(getpwent getpwnam getpwuid));
347 qw(getspent getspnam));
350 qw(getgrent getgrgid getgrnam));
352 qw(getpwent getpwnam getpwuid));
354 define('BUFFER', 'B',
355 qw(getpwent getpwgid getpwnam));
358 qw(gethostent gethostbyaddr gethostbyname));
360 qw(getnetent getnetbyaddr getnetbyname));
362 qw(getprotoent getprotobyname getprotobynumber));
364 qw(getservent getservbyname getservbyport));
366 define('BUFFER', 'B',
367 qw(gethostent gethostbyaddr gethostbyname));
368 define('BUFFER', 'B',
369 qw(getnetent getnetbyaddr getnetbyname));
370 define('BUFFER', 'B',
371 qw(getprotoent getprotobyname getprotobynumber));
372 define('BUFFER', 'B',
373 qw(getservent getservbyname getservbyport));
376 qw(gethostent gethostbyaddr gethostbyname));
378 qw(getnetent getnetbyaddr getnetbyname));
382 my $ifdef = "#ifdef HAS_${F}_R\n";
383 my $endif = "#endif /* HAS_${F}_R */\n";
384 if (exists $seena{$f}) {
385 my @p = @{$seena{$f}};
386 if ($f =~ /^(asctime|ctime|getlogin|setlocale|strerror|ttyname)$/) {
393 PL_reentrant_buffer->_${f}_size = 256; /* Make something up. */
398 elsif ($f =~ /^(crypt|drand48|gmtime|localtime|random)$/) {
401 $seent{$f} _${f}_struct;
406 PL_reentrant_buffer->_${f}_struct.initialized = 0;
410 if ($1 eq 'drand48') {
417 elsif ($f =~ /^(getgrnam|getpwnam|getspnam)$/) {
423 $seent{$f} _${g}_struct;
429 $seent{$f}* _${g}_ptr;
432 if ($g eq 'getspent') {
434 PL_reentrant_buffer->_${g}_size = 1024;
438 # ifdef USE_${G}_FPTR
443 # ifdef USE_${G}_FPTR
444 PL_reentrant_buffer->_${g}_fptr = NULL;
447 my $sc = $g eq 'getgrent' ?
448 '_SC_GETGR_R_SIZE_MAX' : '_SC_GETPW_R_SIZE_MAX';
450 # if defined(HAS_SYSCONF) && defined($sc) && !defined(__GLIBC__)
451 PL_reentrant_buffer->_${g}_size = sysconf($sc);
453 # if defined(__osf__) && defined(__alpha) && defined(SIABUFSIZ)
454 PL_reentrant_buffer->_${g}_size = SIABUFSIZ;
457 PL_reentrant_buffer->_${g}_size = BUFSIZ;
459 PL_reentrant_buffer->_${g}_size = 256;
468 elsif ($f =~ /^(gethostbyname|getnetbyname|getservbyname|getprotobyname)$/) {
473 my $D = ifprotomatch($F, grep {/D/} @p);
476 $seent{$f} _${g}_struct;
484 $seent{$f}* _${g}_ptr;
488 # ifdef USE_${G}_ERRNO
494 PL_reentrant_buffer->_${g}_size = 2048; /* Any better ideas? */
499 New(31338, PL_reentrant_buffer->_${g}_buffer, PL_reentrant_buffer->_${g}_size, char);
504 Safefree(PL_reentrant_buffer->_${g}_buffer);
509 elsif ($f =~ /^(readdir|readdir64)$/) {
511 my $R = ifprotomatch($F, grep {/R/} @p);
513 $seent{$f}* _${f}_struct;
516 $seent{$f}* _${f}_ptr;
520 /* This is the size Solaris recommends.
521 * (though we go static, should use pathconf() instead) */
522 PL_reentrant_buffer->_${f}_size = sizeof($seent{$f}) + MAXPATHLEN + 1;
525 PL_reentrant_buffer->_${f}_struct = ($seent{$f}*)safemalloc(PL_reentrant_buffer->_${f}_size);
528 Safefree(PL_reentrant_buffer->_${f}_struct);
535 # Doesn't implement the buffer growth loop for glibc gethostby*().
540 my $v = join(", ", @v[0..$seenu{$f}-1]);
542 my ($r, $a) = split '_', $p;
543 my $test = $r eq 'I' ? ' == 0' : '';
546 if ($g =~ /^(?:get|set|end)(pw|gr|host|net|proto|serv|sp)/) {
548 } elsif ($g eq 'srand48') {
553 substr($b, 0, $seenu{$f}) = '';
555 $true = "PL_reentrant_buffer->_${g}_ptr";
556 } elsif ($b =~ /T/ && $f eq 'drand48') {
557 $true = "PL_reentrant_buffer->_${g}_double";
558 } elsif ($b =~ /S/) {
559 if ($f =~ /^readdir/) {
560 $true = "PL_reentrant_buffer->_${g}_struct";
562 $true = "&PL_reentrant_buffer->_${g}_struct";
564 } elsif ($b =~ /B/) {
565 $true = "PL_reentrant_buffer->_${g}_buffer";
571 "&PL_reentrant_buffer->_${g}_ptr" :
573 "&PL_reentrant_buffer->_${g}_errno" :
575 "PL_reentrant_buffer->_${g}_buffer" :
577 "PL_reentrant_buffer->_${g}_size" :
579 "&PL_reentrant_buffer->_${g}_fptr" :
581 "&PL_reentrant_buffer->_${g}_data" :
584 "PL_reentrant_buffer->_${g}_struct" :
585 "&PL_reentrant_buffer->_${g}_struct" ) :
586 $_ eq 'T' && $f eq 'drand48' ?
587 "&PL_reentrant_buffer->_${g}_double" :
590 $w = ", $w" if length $v;
592 my $call = "${f}_r($v$w)";
593 $call = "((errno = $call))" if $r eq 'I';
595 # if !defined($f) && ${F}_R_PROTO == REENTRANT_PROTO_$p
597 if ($r eq 'V' || $r eq 'B') {
599 # define $f($v) $call
603 my $rv = $v ? ", $v" : "";
605 # define $f($v) ($call$test ? $true : (errno == ERANGE ? Perl_reentrant_retry("$f"$rv) : 0))
609 # define $f($v) ($call$test ? $true : 0)
618 push @wrap, $endif, "\n";
626 /* Defines for indicating which special features are supported. */
636 #endif /* USE_REENTRANT_API */
644 die "reentr.c: $!" unless open(C, ">reentr.c");
650 * Copyright (c) 1997-2002, Larry Wall
652 * You may distribute under the terms of either the GNU General Public
653 * License or the Artistic License, as specified in the README file.
655 * !!!!!!! DO NOT EDIT THIS FILE !!!!!!!
656 * This file is built by reentrl.pl from data in reentr.pl.
658 * "Saruman," I said, standing away from him, "only one hand at a time can
659 * wield the One, and you know that well, so do not trouble to say we!"
664 #define PERL_IN_REENTR_C
669 Perl_reentrant_size(pTHX) {
670 #ifdef USE_REENTRANT_API
672 #endif /* USE_REENTRANT_API */
676 Perl_reentrant_init(pTHX) {
677 #ifdef USE_REENTRANT_API
678 New(31337, PL_reentrant_buffer, 1, REENTR);
679 Perl_reentrant_size(aTHX);
681 #endif /* USE_REENTRANT_API */
685 Perl_reentrant_free(pTHX) {
686 #ifdef USE_REENTRANT_API
688 Safefree(PL_reentrant_buffer);
689 #endif /* USE_REENTRANT_API */
693 Perl_reentrant_retry(const char *f, ...)
697 #ifdef USE_REENTRANT_API
705 #define REENTRANTHALFMAXSIZE 32768 /* The maximum may end up twice this. */
707 switch (PL_op->op_type) {
708 #ifdef USE_GETHOSTENT_BUFFER
713 if (PL_reentrant_buffer->_gethostent_size <= REENTRANTHALFMAXSIZE) {
714 PL_reentrant_buffer->_gethostent_size *= 2;
715 Renew(PL_reentrant_buffer->_gethostent_buffer,
716 PL_reentrant_buffer->_gethostent_size, char);
717 switch (PL_op->op_type) {
719 p0 = va_arg(ap, void *);
720 asize = va_arg(ap, size_t);
721 anint = va_arg(ap, int);
722 retptr = gethostbyaddr(p0, asize, anint); break;
724 p0 = va_arg(ap, void *);
725 retptr = gethostbyname(p0); break;
727 retptr = gethostent(); break;
735 #ifdef USE_GETGRENT_BUFFER
740 if (PL_reentrant_buffer->_getgrent_size <= REENTRANTHALFMAXSIZE) {
742 PL_reentrant_buffer->_getgrent_size *= 2;
743 Renew(PL_reentrant_buffer->_getgrent_buffer,
744 PL_reentrant_buffer->_getgrent_size, char);
745 switch (PL_op->op_type) {
747 p0 = va_arg(ap, void *);
748 retptr = getgrnam(p0); break;
750 gid = va_arg(ap, Gid_t);
751 retptr = getgrgid(gid); break;
753 retptr = getgrent(); break;
761 #ifdef USE_GETNETENT_BUFFER
766 if (PL_reentrant_buffer->_getnetent_size <= REENTRANTHALFMAXSIZE) {
768 PL_reentrant_buffer->_getnetent_size *= 2;
769 Renew(PL_reentrant_buffer->_getnetent_buffer,
770 PL_reentrant_buffer->_getnetent_size, char);
771 switch (PL_op->op_type) {
773 net = va_arg(ap, Netdb_net_t);
774 anint = va_arg(ap, int);
775 retptr = getnetbyaddr(net, anint); break;
777 p0 = va_arg(ap, void *);
778 retptr = getnetbyname(p0); break;
780 retptr = getnetent(); break;
788 #ifdef USE_GETPWENT_BUFFER
793 if (PL_reentrant_buffer->_getpwent_size <= REENTRANTHALFMAXSIZE) {
795 PL_reentrant_buffer->_getpwent_size *= 2;
796 Renew(PL_reentrant_buffer->_getpwent_buffer,
797 PL_reentrant_buffer->_getpwent_size, char);
798 switch (PL_op->op_type) {
800 p0 = va_arg(ap, void *);
801 retptr = getpwnam(p0); break;
803 uid = va_arg(ap, Uid_t);
804 retptr = getpwuid(uid); break;
806 retptr = getpwent(); break;
814 #ifdef USE_GETPROTOENT_BUFFER
819 if (PL_reentrant_buffer->_getprotoent_size <= REENTRANTHALFMAXSIZE) {
820 PL_reentrant_buffer->_getprotoent_size *= 2;
821 Renew(PL_reentrant_buffer->_getprotoent_buffer,
822 PL_reentrant_buffer->_getprotoent_size, char);
823 switch (PL_op->op_type) {
825 p0 = va_arg(ap, void *);
826 retptr = getprotobyname(p0); break;
828 anint = va_arg(ap, int);
829 retptr = getprotobynumber(anint); break;
831 retptr = getprotoent(); break;
839 #ifdef USE_GETSERVENT_BUFFER
844 if (PL_reentrant_buffer->_getservent_size <= REENTRANTHALFMAXSIZE) {
845 PL_reentrant_buffer->_getservent_size *= 2;
846 Renew(PL_reentrant_buffer->_getservent_buffer,
847 PL_reentrant_buffer->_getservent_size, char);
848 switch (PL_op->op_type) {
850 p0 = va_arg(ap, void *);
851 p1 = va_arg(ap, void *);
852 retptr = getservbyname(p0, p1); break;
854 anint = va_arg(ap, int);
855 p0 = va_arg(ap, void *);
856 retptr = getservbyport(anint, p0); break;
858 retptr = getservent(); break;
867 /* Not known how to retry, so just fail. */
879 asctime S |time |const struct tm|B_SB|B_SBI|I_SB|I_SBI
880 crypt CC |crypt |struct crypt_data|B_CCS
881 ctermid B |stdio | |B_B
882 ctime S |time |const time_t |B_SB|B_SBI|I_SB|I_SBI
883 drand48 |stdlib |struct drand48_data |I_ST|T=double*
884 endgrent |grp | |I_H|V_H
885 endhostent |netdb |struct hostent_data |I_S|V_S
886 endnetent |netdb |struct netent_data |I_S|V_S
887 endprotoent |netdb |struct protoent_data |I_S|V_S
888 endpwent |pwd | |I_H|V_H
889 endservent |netdb |struct servent_data |I_S|V_S
890 getgrent |grp |struct group |I_SBWR|I_SBIR|S_SBW|S_SBI|I_SBI|I_SBIH
891 getgrgid T |grp |struct group |I_TSBWR|I_TSBIR|I_TSBI|S_TSBI|T=gid_t
892 getgrnam C |grp |struct group |I_CSBWR|I_CSBIR|S_CBI|I_CSBI|S_CSBI
893 gethostbyaddr CWI |netdb |struct hostent |I_CWISBWRE|S_CWISBWIE|S_CWISBIE|S_TWISBIE|S_CIISBIE|S_CSBIE|S_TSBIE|I_CWISD|I_CIISD|I_CII|D=struct hostent_data*|T=const void*
894 gethostbyname C |netdb |struct hostent |I_CSBWRE|S_CSBIE|I_CSD|D=struct hostent_data*
895 gethostent |netdb |struct hostent |I_SBWRE|I_SBIE|S_SBIE|S_SBI|I_SBI|I_SD|D=struct hostent_data*
896 getlogin |unistd | |I_BW|I_BI|B_BW|B_BI
897 getnetbyaddr LI |netdb |struct netent |I_UISBWRE|I_LISBI|S_TISBI|S_LISBI|I_TISD|I_LISD|I_IISD|D=struct netent_data*|T=in_addr_t|U=unsigned long
898 getnetbyname C |netdb |struct netent |I_CSBWRE|I_CSBI|S_CSBI|I_CSD|D=struct netent_data*
899 getnetent |netdb |struct netent |I_SBWRE|I_SBIE|S_SBIE|S_SBI|I_SBI|I_SD|D=struct netent_data*
900 getprotobyname C|netdb |struct protoent|I_CSBWR|S_CSBI|I_CSD|D=struct protoent_data*
901 getprotobynumber I |netdb |struct protoent|I_ISBWR|S_ISBI|I_ISD|D=struct protoent_data*
902 getprotoent |netdb |struct protoent|I_SBWR|I_SBI|S_SBI|I_SD|D=struct protoent_data*
903 getpwent |pwd |struct passwd |I_SBWR|I_SBIR|S_SBW|S_SBI|I_SBI|I_SBIH
904 getpwnam C |pwd |struct passwd |I_CSBWR|I_CSBIR|S_CSBI|I_CSBI
905 getpwuid T |pwd |struct passwd |I_TSBWR|I_TSBIR|I_TSBI|S_TSBI|T=uid_t
906 getservbyname CC|netdb |struct servent |I_CCSBWR|S_CCSBI|I_CCSD|D=struct servent_data*
907 getservbyport IC|netdb |struct servent |I_ICSBWR|S_ICSBI|I_ICSD|D=struct servent_data*
908 getservent |netdb |struct servent |I_SBWR|I_SBI|S_SBI|I_SD|D=struct servent_data*
909 getspnam C |shadow |struct spwd |I_CSBWR|S_CSBI
910 gmtime T |time |struct tm |S_TS|I_TS|T=const time_t*
911 localtime T |time |struct tm |S_TS|I_TS|T=const time_t*
912 random |stdlib |struct random_data|I_TS|T=int*
913 readdir T |dirent |struct dirent |I_TSR|I_TS|T=DIR*
914 readdir64 T |dirent |struct dirent64|I_TSR|I_TS|T=DIR*
915 setgrent |grp | |I_H|V_H
916 sethostent I |netdb | |I_ID|V_ID|D=struct hostent_data*
917 setlocale IC |locale | |I_ICBI
918 setnetent I |netdb | |I_ID|V_ID|D=struct netent_data*
919 setprotoent I |netdb | |I_ID|V_ID|D=struct protoent_data*
920 setpwent |pwd | |I_H|V_H
921 setservent I |netdb | |I_ID|V_ID|D=struct servent_data*
922 srand48 L |stdlib |struct drand48_data |I_LS
923 srandom T |stdlib |struct random_data|I_TS|T=unsigned int
924 strerror I |string | |I_IBW|I_IBI|B_IBW
925 tmpnam B |stdio | |B_B
926 ttyname I |unistd | |I_IBW|I_IBI|B_IBI