1 
2 #ifndef PPPORT_H
3 #define PPPORT_H 1
4 
5 /* Perl/Pollution/Portability Version 1.0003 */
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 contact the author at
11    <kjahds@kjahds.com>, or check with the Perl maintainers. */
12 
13 /* If you needed to customize this file for your project, please mention
14    your changes. */
15 
16 /*
17    Modified for Perl 5.6.0 by Russ Allbery (use PERL_VERSION instead of
18    PERL_PATCHLEVEL).
19 */
20 
21 
22 /*
23    In order for a Perl extension module to be as portable as possible
24    across differing versions of Perl itself, certain steps need to be taken.
25    Including this header is the first major one, then using dTHR is all the
26    appropriate places and using a PL_ prefix to refer to global Perl
27    variables is the second.
28 */
29 
30 
31 /* If you use one of a few functions that were not present in earlier
32    versions of Perl, please add a define before the inclusion of ppport.h
33    for a static include, or use the GLOBAL request in a single module to
34    produce a global definition that can be referenced from the other
35    modules.
36 
37    Function:            Static define:           Extern define:
38    newCONSTSUB()        NEED_newCONSTSUB         NEED_newCONSTSUB_GLOBAL
39 
40 */
41 
42 
43 /* To verify whether ppport.h is needed for your module, and whether any
44    special defines should be used, ppport.h can be run through Perl to check
45    your source code. Simply say:
46 
47         perl -x ppport.h *.c *.h *.xs foo/perl.c [etc]
48 
49    The result will be a list of patches suggesting changes that should at
50    least be acceptable, if not necessarily the most efficient solution, or a
51    fix for all possible problems. It won't catch where dTHR is needed, and
52    doesn't attempt to account for global macro or function definitions,
53    nested includes, typemaps, etc.
54 
55    In order to test for the need of dTHR, please try your module under a
56    recent version of Perl that has threading compiled-in.
57 
58 */
59 
60 /* clang-format off */
61 /*
62 #!/usr/bin/perl
63 @ARGV = ("*.xs") if !@ARGV;
64 %badmacros = %funcs = %macros = ();
65 foreach (<DATA>) {
66     $funcs{$1} = 1 if /Provide:\s+(\S+)/;
67     $macros{$1} = 1 if /^#\s*define\s+([a-zA-Z0-9_]+)/;
68     $badmacros{$2}=$1 if /^#\s*define\s+(PL_\S+)\s+(\S+)/;
69 }
70 foreach $filename (map(glob($_),@ARGV)) {
71     unless (open(IN, "<$filename")) {
72         warn "Unable to read from $file: $!\n";
73         next;
74     }
75     print "Scanning $filename...\n";
76     $c = ""; while (<IN>) { $c .= $_; } close(IN);
77     $need_include = 0; %add_func = (); $changes = 0;
78     $has_include = ($c =~ /#.*include.*ppport/m);
79 
80     foreach $func (keys %funcs) {
81         if ($c =~ /#.*define.*\bNEED_$func(_GLOBAL)?\b/m) {
82             if ($c !~ /\b$func\b/m) {
83                 print "If $func isn't needed, you don't need to request it.\n" if
84                 $changes += ($c =~ s/^.*#.*define.*\bNEED_$func\b.*\n//m);
85             } else {
86                 print "Uses $func\n";
87                 $need_include = 1;
88             }
89         } else {
90             if ($c =~ /\b$func\b/m) {
91                 $add_func{$func} =1 ;
92                 print "Uses $func\n";
93                 $need_include = 1;
94             }
95         }
96     }
97 
98     if (not $need_include) {
99         foreach $macro (keys %macros) {
100             if ($c =~ /\b$macro\b/m) {
101                 print "Uses $macro\n";
102                 $need_include = 1;
103             }
104         }
105     }
106 
107     foreach $badmacro (keys %badmacros) {
108         if ($c =~ /\b$badmacro\b/m) {
109             $changes += ($c =~ s/\b$badmacro\b/$badmacros{$badmacro}/gm);
110             print "Uses $badmacros{$badmacro} (instead of $badmacro)\n";
111             $need_include = 1;
112         }
113     }
114 
115     if (scalar(keys %add_func) or $need_include != $has_include) {
116         if (!$has_include) {
117             $inc = join('',map("#define NEED_$_\n", sort keys %add_func)).
118                    "#include \"ppport.h\"\n";
119             $c = "$inc$c" unless $c =~ s/#.*include.*XSUB.*\n/$&$inc/m;
120         } elsif (keys %add_func) {
121             $inc = join('',map("#define NEED_$_\n", sort keys %add_func));
122             $c = "$inc$c" unless $c =~ s/^.*#.*include.*ppport.*$/$inc$&/m;
123         }
124         if (!$need_include) {
125             print "Doesn't seem to need ppport.h.\n";
126             $c =~ s/^.*#.*include.*ppport.*\n//m;
127         }
128         $changes++;
129     }
130 
131     if ($changes) {
132         open(OUT,">/tmp/ppport.h.$$");
133         print OUT $c;
134         close(OUT);
135         open(DIFF, "diff -u $filename /tmp/ppport.h.$$|");
136         while (<DIFF>) {
137             s!/tmp/ppport\.h\.$$!$filename.patched!; print STDOUT;
138         }
139         close(DIFF);
140         unlink("/tmp/ppport.h.$$");
141     } else {
142         print "Looks OK\n";
143     }
144 }
145 __DATA__
146 */
147 /* clang-format on */
148 
149 
150 #if !defined(PERL_VERSION) && !defined(PERL_PATCHLEVEL)
151 #    ifndef __PATCHLEVEL_H_INCLUDED__
152 #        include <patchlevel.h>
153 #    endif
154 #endif
155 #ifndef PERL_VERSION
156 #    define PERL_REVISION (5)
157 #    ifdef PERL_PATCHLEVEL
158 #        define PERL_VERSION PERL_PATCHLEVEL
159 #    else
160 #        define PERL_VERSION    PATCHLEVEL
161 #        define PERL_SUBVERSION SUBVERSION
162 #    endif
163 #endif
164 
165 #ifndef ERRSV
166 #    define ERRSV perl_get_sv("@", false)
167 #endif
168 
169 #if (PERL_REVISION == 5)   \
170     && ((PERL_VERSION < 4) \
171         || ((PERL_VERSION == 4) && (PERL_SUBVERSION <= 4)))
172 #    define PL_sv_undef sv_undef
173 #    define PL_sv_yes   sv_yes
174 #    define PL_sv_no    sv_no
175 #    define PL_na       na
176 #    define PL_stdingv  stdingv
177 #    define PL_hints    hints
178 #    define PL_curcop   curcop
179 #    define PL_curstash curstash
180 #    define PL_copline  copline
181 #endif
182 
183 #if (PERL_REVISION == 5) && (PERL_VERSION < 5)
184 #    undef dTHR
185 #    ifdef WIN32
186 #        define dTHR extern int Perl___notused
187 #    else
188 #        define dTHR extern int errno
189 #    endif
190 #endif
191 
192 #ifndef boolSV
193 #    define boolSV(b) ((b) ? &PL_sv_yes : &PL_sv_no)
194 #endif
195 
196 /* Perl tries to export a bunch of its own functions.  Mutter. */
197 #undef die
198 #undef list
199 #undef warn
200 
201 #endif /* !PPPORT_H */
202