1 
2 #ifndef _P_P_PORTABILITY_H_
3 #define _P_P_PORTABILITY_H_
4 
5 /* Perl/Pollution/Portability Version 1.0007 */
6 
7 /* Copyright (C) 1999, Kenneth Albanowski. This code may be used and
8    distributed under the same license as any version of Perl. */
9 
10 /* For the latest version of this code, please retreive the Devel::PPPort
11    module from CPAN, contact the author at <kjahds@kjahds.com>, or check
12    with the Perl maintainers. */
13 
14 /* If you needed to customize this file for your project, please mention
15    your changes, and visible alter the version number. */
16 
17 
18 /*
19    In order for a Perl extension module to be as portable as possible
20    across differing versions of Perl itself, certain steps need to be taken.
21    Including this header is the first major one, then using dTHR is all the
22    appropriate places and using a PL_ prefix to refer to global Perl
23    variables is the second.
24 */
25 
26 
27 /* If you use one of a few functions that were not present in earlier
28    versions of Perl, please add a define before the inclusion of ppport.h
29    for a static include, or use the GLOBAL request in a single module to
30    produce a global definition that can be referenced from the other
31    modules.
32 
33    Function:            Static define:           Extern define:
34    newCONSTSUB()        NEED_newCONSTSUB         NEED_newCONSTSUB_GLOBAL
35 
36 */
37 
38 
39 /* To verify whether ppport.h is needed for your module, and whether any
40    special defines should be used, ppport.h can be run through Perl to check
41    your source code. Simply say:
42 
43    	perl -x ppport.h *.c *.h *.xs foo/*.c [etc]
44 
45    The result will be a list of patches suggesting changes that should at
46    least be acceptable, if not necessarily the most efficient solution, or a
47    fix for all possible problems. It won't catch where dTHR is needed, and
48    doesn't attempt to account for global macro or function definitions,
49    nested includes, typemaps, etc.
50 
51    In order to test for the need of dTHR, please try your module under a
52    recent version of Perl that has threading compiled-in.
53 
54 */
55 
56 
57 /*
58 #!/usr/bin/perl
59 @ARGV = ("*.xs") if !@ARGV;
60 %badmacros = %funcs = %macros = (); $replace = 0;
61 foreach (<DATA>) {
62 	$funcs{$1} = 1 if /Provide:\s+(\S+)/;
63 	$macros{$1} = 1 if /^#\s*define\s+([a-zA-Z0-9_]+)/;
64 	$replace = $1 if /Replace:\s+(\d+)/;
65 	$badmacros{$2}=$1 if $replace and /^#\s*define\s+([a-zA-Z0-9_]+).*?\s+([a-zA-Z0-9_]+)/;
66 	$badmacros{$1}=$2 if /Replace (\S+) with (\S+)/;
67 }
68 foreach $filename (map(glob($_),@ARGV)) {
69 	unless (open(IN, "<$filename")) {
70 		warn "Unable to read from $file: $!\n";
71 		next;
72 	}
73 	print "Scanning $filename...\n";
74 	$c = ""; while (<IN>) { $c .= $_; } close(IN);
75 	$need_include = 0; %add_func = (); $changes = 0;
76 	$has_include = ($c =~ /#.*include.*ppport/m);
77 
78 	foreach $func (keys %funcs) {
79 		if ($c =~ /#.*define.*\bNEED_$func(_GLOBAL)?\b/m) {
80 			if ($c !~ /\b$func\b/m) {
81 				print "If $func isn't needed, you don't need to request it.\n" if
82 				$changes += ($c =~ s/^.*#.*define.*\bNEED_$func\b.*\n//m);
83 			} else {
84 				print "Uses $func\n";
85 				$need_include = 1;
86 			}
87 		} else {
88 			if ($c =~ /\b$func\b/m) {
89 				$add_func{$func} =1 ;
90 				print "Uses $func\n";
91 				$need_include = 1;
92 			}
93 		}
94 	}
95 
96 	if (not $need_include) {
97 		foreach $macro (keys %macros) {
98 			if ($c =~ /\b$macro\b/m) {
99 				print "Uses $macro\n";
100 				$need_include = 1;
101 			}
102 		}
103 	}
104 
105 	foreach $badmacro (keys %badmacros) {
106 		if ($c =~ /\b$badmacro\b/m) {
107 			$changes += ($c =~ s/\b$badmacro\b/$badmacros{$badmacro}/gm);
108 			print "Uses $badmacros{$badmacro} (instead of $badmacro)\n";
109 			$need_include = 1;
110 		}
111 	}
112 
113 	if (scalar(keys %add_func) or $need_include != $has_include) {
114 		if (!$has_include) {
115 			$inc = join('',map("#define NEED_$_\n", sort keys %add_func)).
116 			       "#include \"ppport.h\"\n";
117 			$c = "$inc$c" unless $c =~ s/#.*include.*XSUB.*\n/$&$inc/m;
118 		} elsif (keys %add_func) {
119 			$inc = join('',map("#define NEED_$_\n", sort keys %add_func));
120 			$c = "$inc$c" unless $c =~ s/^.*#.*include.*ppport.*$/$inc$&/m;
121 		}
122 		if (!$need_include) {
123 			print "Doesn't seem to need ppport.h.\n";
124 			$c =~ s/^.*#.*include.*ppport.*\n//m;
125 		}
126 		$changes++;
127 	}
128 
129 	if ($changes) {
130 		open(OUT,">/tmp/ppport.h.$$");
131 		print OUT $c;
132 		close(OUT);
133 		open(DIFF, "diff -u $filename /tmp/ppport.h.$$|");
134 		while (<DIFF>) { s!/tmp/ppport\.h\.$$!$filename.patched!; print STDOUT; }
135 		close(DIFF);
136 		unlink("/tmp/ppport.h.$$");
137 	} else {
138 		print "Looks OK\n";
139 	}
140 }
141 __DATA__
142 */
143 
144 #ifndef PERL_REVISION
145 #   ifndef __PATCHLEVEL_H_INCLUDED__
146 #       include "patchlevel.h"
147 #   endif
148 #   ifndef PERL_REVISION
149 #	define PERL_REVISION	(5)
150         /* Replace: 1 */
151 #       define PERL_VERSION	PATCHLEVEL
152 #       define PERL_SUBVERSION	SUBVERSION
153         /* Replace PERL_PATCHLEVEL with PERL_VERSION */
154         /* Replace: 0 */
155 #   endif
156 #endif
157 
158 #define PERL_BCDVERSION ((PERL_REVISION * 0x1000000L) + (PERL_VERSION * 0x1000L) + PERL_SUBVERSION)
159 
160 #ifndef ERRSV
161 #	define ERRSV perl_get_sv("@",FALSE)
162 #endif
163 
164 #if (PERL_VERSION < 4) || ((PERL_VERSION == 4) && (PERL_SUBVERSION <= 5))
165 /* Replace: 1 */
166 #	define PL_sv_undef	sv_undef
167 #	define PL_sv_yes	sv_yes
168 #	define PL_sv_no		sv_no
169 #	define PL_na		na
170 #	define PL_stdingv	stdingv
171 #	define PL_hints		hints
172 #	define PL_curcop	curcop
173 #	define PL_curstash	curstash
174 #	define PL_copline	copline
175 #	define PL_Sv		Sv
176 /* Replace: 0 */
177 #endif
178 
179 #ifndef dTHR
180 #  ifdef WIN32
181 #	define dTHR extern int Perl___notused
182 #  else
183 #	define dTHR extern int errno
184 #  endif
185 #endif
186 
187 #ifndef boolSV
188 #	define boolSV(b) ((b) ? &PL_sv_yes : &PL_sv_no)
189 #endif
190 
191 #ifndef gv_stashpvn
192 #	define gv_stashpvn(str,len,flags) gv_stashpv(str,flags)
193 #endif
194 
195 #ifndef newSVpvn
196 #	define newSVpvn(data,len) ((len) ? newSVpv ((data), (len)) : newSVpv ("", 0))
197 #endif
198 
199 #ifndef newRV_inc
200 /* Replace: 1 */
201 #	define newRV_inc(sv) newRV(sv)
202 /* Replace: 0 */
203 #endif
204 
205 #ifndef newRV_noinc
206 #  ifdef __GNUC__
207 #    define newRV_noinc(sv)               \
208       ({                                  \
209           SV *nsv = (SV*)newRV(sv);       \
210           SvREFCNT_dec(sv);               \
211           nsv;                            \
212       })
213 #  else
214 #    if defined(CRIPPLED_CC) || defined(USE_THREADS)
newRV_noinc(SV * sv)215 static SV * newRV_noinc (SV * sv)
216 {
217           SV *nsv = (SV*)newRV(sv);
218           SvREFCNT_dec(sv);
219           return nsv;
220 }
221 #    else
222 #      define newRV_noinc(sv)    \
223         ((PL_Sv=(SV*)newRV(sv), SvREFCNT_dec(sv), (SV*)PL_Sv)
224 #    endif
225 #  endif
226 #endif
227 
228 /* Provide: newCONSTSUB */
229 
230 /* newCONSTSUB from IO.xs is in the core starting with 5.004_63 */
231 #if (PERL_VERSION < 4) || ((PERL_VERSION == 4) && (PERL_SUBVERSION < 63))
232 
233 #if defined(NEED_newCONSTSUB)
234 static
235 #else
236 extern void newCONSTSUB _((HV * stash, char * name, SV *sv));
237 #endif
238 
239 #if defined(NEED_newCONSTSUB) || defined(NEED_newCONSTSUB_GLOBAL)
240 void
newCONSTSUB(stash,name,sv)241 newCONSTSUB(stash,name,sv)
242 HV *stash;
243 char *name;
244 SV *sv;
245 {
246 	U32 oldhints = PL_hints;
247 	HV *old_cop_stash = PL_curcop->cop_stash;
248 	HV *old_curstash = PL_curstash;
249 	line_t oldline = PL_curcop->cop_line;
250 	PL_curcop->cop_line = PL_copline;
251 
252 	PL_hints &= ~HINT_BLOCK_SCOPE;
253 	if (stash)
254 		PL_curstash = PL_curcop->cop_stash = stash;
255 
256 	newSUB(
257 
258 #if (PERL_VERSION < 3) || ((PERL_VERSION == 3) && (PERL_SUBVERSION < 22))
259      /* before 5.003_22 */
260 		start_subparse(),
261 #else
262 #  if (PERL_VERSION == 3) && (PERL_SUBVERSION == 22)
263      /* 5.003_22 */
264      		start_subparse(0),
265 #  else
266      /* 5.003_23  onwards */
267      		start_subparse(FALSE, 0),
268 #  endif
269 #endif
270 
271 		newSVOP(OP_CONST, 0, newSVpv(name,0)),
272 		newSVOP(OP_CONST, 0, &PL_sv_no),   /* SvPV(&PL_sv_no) == "" -- GMB */
273 		newSTATEOP(0, Nullch, newSVOP(OP_CONST, 0, sv))
274 	);
275 
276 	PL_hints = oldhints;
277 	PL_curcop->cop_stash = old_cop_stash;
278 	PL_curstash = old_curstash;
279 	PL_curcop->cop_line = oldline;
280 }
281 #endif
282 
283 #endif /* newCONSTSUB */
284 
285 
286 #endif /* _P_P_PORTABILITY_H_ */
287