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