1 |
root |
1.1 |
#ifndef SCHMORP_PERL_H_ |
2 |
|
|
#define SCHMORP_PERL_H_ |
3 |
|
|
|
4 |
root |
1.2 |
/* WARNING |
5 |
|
|
* This header file is a shared resource between many modules. |
6 |
|
|
*/ |
7 |
|
|
|
8 |
root |
1.1 |
/* useful stuff, used by schmorp mostly */ |
9 |
|
|
|
10 |
root |
1.4 |
#include "patchlevel.h" |
11 |
|
|
|
12 |
root |
1.1 |
#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 |
|
|
int signum; |
80 |
|
|
|
81 |
|
|
SvGETMAGIC (sig); |
82 |
|
|
|
83 |
|
|
for (signum = 1; signum < SIG_SIZE; ++signum) |
84 |
|
|
if (strEQ (SvPV_nolen (sig), PL_sig_name [signum])) |
85 |
|
|
return signum; |
86 |
|
|
|
87 |
|
|
signum = SvIV (sig); |
88 |
|
|
|
89 |
|
|
if (signum > 0 && signum < SIG_SIZE) |
90 |
|
|
return signum; |
91 |
|
|
|
92 |
|
|
return -1; |
93 |
|
|
} |
94 |
|
|
|
95 |
|
|
static int |
96 |
|
|
s_signum_croak (SV *sig) |
97 |
|
|
{ |
98 |
|
|
int signum = s_signum (sig); |
99 |
|
|
|
100 |
|
|
if (signum < 0) |
101 |
|
|
croak ("%s: invalid signal name or number", SvPV_nolen (sig)); |
102 |
|
|
|
103 |
|
|
return signum; |
104 |
|
|
} |
105 |
|
|
|
106 |
|
|
static int |
107 |
|
|
s_fileno (SV *fh, int wr) |
108 |
|
|
{ |
109 |
|
|
SvGETMAGIC (fh); |
110 |
|
|
|
111 |
|
|
if (SvROK (fh)) |
112 |
|
|
{ |
113 |
|
|
fh = SvRV (fh); |
114 |
|
|
SvGETMAGIC (fh); |
115 |
|
|
} |
116 |
|
|
|
117 |
|
|
if (SvTYPE (fh) == SVt_PVGV) |
118 |
|
|
return PerlIO_fileno (wr ? IoOFP (sv_2io (fh)) : IoIFP (sv_2io (fh))); |
119 |
|
|
|
120 |
|
|
if (SvOK (fh) && (SvIV (fh) >= 0) && (SvIV (fh) < 0x7fffffffL)) |
121 |
|
|
return SvIV (fh); |
122 |
|
|
|
123 |
|
|
return -1; |
124 |
|
|
} |
125 |
|
|
|
126 |
|
|
static int |
127 |
|
|
s_fileno_croak (SV *fh, int wr) |
128 |
|
|
{ |
129 |
|
|
int fd = s_fileno (fh, wr); |
130 |
|
|
|
131 |
|
|
if (fd < 0) |
132 |
|
|
croak ("%s: illegal fh argument, either not an OS file or read/write mode mismatch", SvPV_nolen (fh)); |
133 |
|
|
|
134 |
|
|
return fd; |
135 |
|
|
} |
136 |
|
|
|
137 |
|
|
static SV * |
138 |
|
|
s_get_cv (SV *cb_sv) |
139 |
|
|
{ |
140 |
|
|
HV *st; |
141 |
|
|
GV *gvp; |
142 |
|
|
|
143 |
root |
1.4 |
return (SV *)sv_2cv (cb_sv, &st, &gvp, 0); |
144 |
root |
1.1 |
} |
145 |
|
|
|
146 |
|
|
static SV * |
147 |
|
|
s_get_cv_croak (SV *cb_sv) |
148 |
|
|
{ |
149 |
root |
1.3 |
SV *cv = s_get_cv (cb_sv); |
150 |
root |
1.1 |
|
151 |
root |
1.3 |
if (!cv) |
152 |
root |
1.1 |
croak ("%s: callback must be a CODE reference or another callable object", SvPV_nolen (cb_sv)); |
153 |
|
|
|
154 |
root |
1.3 |
return cv; |
155 |
root |
1.1 |
} |
156 |
|
|
|
157 |
|
|
/*****************************************************************************/ |
158 |
|
|
/* gensub: simple closure generation utility */ |
159 |
|
|
|
160 |
|
|
#define S_GENSUB_ARG CvXSUBANY (cv).any_ptr |
161 |
|
|
|
162 |
|
|
/* create a closure from XS, returns a code reference */ |
163 |
|
|
/* the arg can be accessed via GENSUB_ARG from the callback */ |
164 |
|
|
/* the callback must use dXSARGS/XSRETURN */ |
165 |
|
|
static SV * |
166 |
|
|
s_gensub (pTHX_ void (*xsub)(pTHX_ CV *), void *arg) |
167 |
|
|
{ |
168 |
|
|
CV *cv = (CV *)newSV (0); |
169 |
|
|
|
170 |
|
|
sv_upgrade ((SV *)cv, SVt_PVCV); |
171 |
|
|
|
172 |
|
|
CvANON_on (cv); |
173 |
|
|
CvISXSUB_on (cv); |
174 |
|
|
CvXSUB (cv) = xsub; |
175 |
|
|
S_GENSUB_ARG = arg; |
176 |
|
|
|
177 |
|
|
return newRV_noinc ((SV *)cv); |
178 |
|
|
} |
179 |
|
|
|
180 |
|
|
#endif |
181 |
|
|
|