1 |
#ifndef SCHMORP_PERL_H_ |
2 |
#define SCHMORP_PERL_H_ |
3 |
|
4 |
/* WARNING |
5 |
* This header file is a shared resource between many modules. |
6 |
*/ |
7 |
|
8 |
/* useful stuff, used by schmorp mostly */ |
9 |
|
10 |
#include "patchlevel.h" |
11 |
|
12 |
#define PERL_VERSION_ATLEAST(a,b,c) \ |
13 |
(PERL_REVISION > (a) \ |
14 |
|| (PERL_REVISION == (a) \ |
15 |
&& (PERL_VERSION > (b) \ |
16 |
|| (PERL_VERSION == (b) && PERL_SUBVERSION >= (c))))) |
17 |
|
18 |
#if !PERL_VERSION_ATLEAST (5,6,0) |
19 |
# ifndef PL_ppaddr |
20 |
# define PL_ppaddr ppaddr |
21 |
# endif |
22 |
# ifndef call_sv |
23 |
# define call_sv perl_call_sv |
24 |
# endif |
25 |
# ifndef get_sv |
26 |
# define get_sv perl_get_sv |
27 |
# endif |
28 |
# ifndef get_cv |
29 |
# define get_cv perl_get_cv |
30 |
# endif |
31 |
# ifndef IS_PADGV |
32 |
# define IS_PADGV(v) 0 |
33 |
# endif |
34 |
# ifndef IS_PADCONST |
35 |
# define IS_PADCONST(v) 0 |
36 |
# endif |
37 |
#endif |
38 |
|
39 |
/* 5.11 */ |
40 |
#ifndef CxHASARGS |
41 |
# define CxHASARGS(cx) (cx)->blk_sub.hasargs |
42 |
#endif |
43 |
|
44 |
/* 5.10.0 */ |
45 |
#ifndef SvREFCNT_inc_NN |
46 |
# define SvREFCNT_inc_NN(sv) SvREFCNT_inc (sv) |
47 |
#endif |
48 |
|
49 |
/* 5.8.8 */ |
50 |
#ifndef GV_NOTQUAL |
51 |
# define GV_NOTQUAL 0 |
52 |
#endif |
53 |
#ifndef newSV |
54 |
# define newSV(l) NEWSV(0,l) |
55 |
#endif |
56 |
#ifndef CvISXSUB_on |
57 |
# define CvISXSUB_on(cv) (void)cv |
58 |
#endif |
59 |
#ifndef CvISXSUB |
60 |
# define CvISXSUB(cv) (CvXSUB (cv) ? TRUE : FALSE) |
61 |
#endif |
62 |
#ifndef Newx |
63 |
# define Newx(ptr,nitems,type) New (0,ptr,nitems,type) |
64 |
#endif |
65 |
|
66 |
/* 5.8.7 */ |
67 |
#ifndef SvRV_set |
68 |
# define SvRV_set(s,v) SvRV(s) = (v) |
69 |
#endif |
70 |
|
71 |
static int |
72 |
s_signum (SV *sig) |
73 |
{ |
74 |
#ifndef SIG_SIZE |
75 |
/* kudos to Slaven Rezic for the idea */ |
76 |
static char sig_size [] = { SIG_NUM }; |
77 |
# define SIG_SIZE (sizeof (sig_size) + 1) |
78 |
#endif |
79 |
dTHX; |
80 |
int signum; |
81 |
|
82 |
SvGETMAGIC (sig); |
83 |
|
84 |
for (signum = 1; signum < SIG_SIZE; ++signum) |
85 |
if (strEQ (SvPV_nolen (sig), PL_sig_name [signum])) |
86 |
return signum; |
87 |
|
88 |
signum = SvIV (sig); |
89 |
|
90 |
if (signum > 0 && signum < SIG_SIZE) |
91 |
return signum; |
92 |
|
93 |
return -1; |
94 |
} |
95 |
|
96 |
static int |
97 |
s_signum_croak (SV *sig) |
98 |
{ |
99 |
int signum = s_signum (sig); |
100 |
|
101 |
if (signum < 0) |
102 |
{ |
103 |
dTHX; |
104 |
croak ("%s: invalid signal name or number", SvPV_nolen (sig)); |
105 |
} |
106 |
|
107 |
return signum; |
108 |
} |
109 |
|
110 |
static int |
111 |
s_fileno (SV *fh, int wr) |
112 |
{ |
113 |
dTHX; |
114 |
SvGETMAGIC (fh); |
115 |
|
116 |
if (SvROK (fh)) |
117 |
{ |
118 |
fh = SvRV (fh); |
119 |
SvGETMAGIC (fh); |
120 |
} |
121 |
|
122 |
if (SvTYPE (fh) == SVt_PVGV) |
123 |
return PerlIO_fileno (wr ? IoOFP (sv_2io (fh)) : IoIFP (sv_2io (fh))); |
124 |
|
125 |
if (SvOK (fh) && (SvIV (fh) >= 0) && (SvIV (fh) < 0x7fffffffL)) |
126 |
return SvIV (fh); |
127 |
|
128 |
return -1; |
129 |
} |
130 |
|
131 |
static int |
132 |
s_fileno_croak (SV *fh, int wr) |
133 |
{ |
134 |
int fd = s_fileno (fh, wr); |
135 |
|
136 |
if (fd < 0) |
137 |
{ |
138 |
dTHX; |
139 |
croak ("%s: illegal fh argument, either not an OS file or read/write mode mismatch", SvPV_nolen (fh)); |
140 |
} |
141 |
|
142 |
return fd; |
143 |
} |
144 |
|
145 |
static SV * |
146 |
s_get_cv (SV *cb_sv) |
147 |
{ |
148 |
dTHX; |
149 |
HV *st; |
150 |
GV *gvp; |
151 |
|
152 |
return (SV *)sv_2cv (cb_sv, &st, &gvp, 0); |
153 |
} |
154 |
|
155 |
static SV * |
156 |
s_get_cv_croak (SV *cb_sv) |
157 |
{ |
158 |
SV *cv = s_get_cv (cb_sv); |
159 |
|
160 |
if (!cv) |
161 |
{ |
162 |
dTHX; |
163 |
croak ("%s: callback must be a CODE reference or another callable object", SvPV_nolen (cb_sv)); |
164 |
} |
165 |
|
166 |
return cv; |
167 |
} |
168 |
|
169 |
/*****************************************************************************/ |
170 |
/* gensub: simple closure generation utility */ |
171 |
|
172 |
#define S_GENSUB_ARG CvXSUBANY (cv).any_ptr |
173 |
|
174 |
/* create a closure from XS, returns a code reference */ |
175 |
/* the arg can be accessed via GENSUB_ARG from the callback */ |
176 |
/* the callback must use dXSARGS/XSRETURN */ |
177 |
static SV * |
178 |
s_gensub (pTHX_ void (*xsub)(pTHX_ CV *), void *arg) |
179 |
{ |
180 |
CV *cv = (CV *)newSV (0); |
181 |
|
182 |
sv_upgrade ((SV *)cv, SVt_PVCV); |
183 |
|
184 |
CvANON_on (cv); |
185 |
CvISXSUB_on (cv); |
186 |
CvXSUB (cv) = xsub; |
187 |
S_GENSUB_ARG = arg; |
188 |
|
189 |
return newRV_noinc ((SV *)cv); |
190 |
} |
191 |
|
192 |
#endif |
193 |
|