1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
|
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#define NEED_PL_parser
#include "ppport.h"
/* this should go into ppport */
#if PERL_BCDVERSION >= 0x5009005
#define PL_oldbufptr D_PPP_my_PL_parser_var(oldbufptr)
#endif
#if PERL_REVISION == 5 && PERL_VERSION >= 10
#define HAS_HINTS_HASH
#endif
#include "hook_op_check.h"
#include "hook_op_ppaddr.h"
#include "hook_parser.h"
typedef struct userdata_St {
char *f_class;
SV *class;
hook_op_check_id eval_hook;
hook_op_check_id parser_id;
} userdata_t;
STATIC void
call_to_perl (SV *class, UV offset, char *proto) {
dSP;
ENTER;
SAVETMPS;
PUSHMARK (SP);
EXTEND (SP, 3);
PUSHs (class);
mPUSHu (offset);
mPUSHp (proto, strlen (proto));
PUTBACK;
call_method ("callback", G_VOID|G_DISCARD);
FREETMPS;
LEAVE;
}
STATIC SV *
qualify_func_name (const char *s) {
SV *ret = newSVpvs ("");
if (strstr (s, ":") == NULL) {
sv_catpv (ret, SvPVX (PL_curstname));
sv_catpvs (ret, "::");
}
sv_catpv (ret, s);
return ret;
}
STATIC int
enabled (SV *class) {
STRLEN len;
char *key;
HV *hints = GvHV (PL_hintgv);
SV **sv, *tmp = newSVsv (class);
sv_catpv (tmp, "::enabled");
key = SvPV (tmp, len);
if (!hints) {
return 0;
}
sv = hv_fetch (hints, key, len, 0);
SvREFCNT_dec (tmp);
if (!sv || !*sv) {
return 0;
}
return SvOK (*sv);
}
STATIC OP *
handle_proto (pTHX_ OP *op, void *user_data) {
OP *ret;
SV *op_sv, *name;
char *s, *tmp, *tmp2;
char tmpbuf[sizeof (PL_tokenbuf)], proto[sizeof (PL_tokenbuf)];
STRLEN retlen = 0;
userdata_t *ud = (userdata_t *)user_data;
if (strNE (ud->f_class, SvPVX (PL_curstname))) {
return op;
}
if (!enabled (ud->class)) {
return op;
}
if (!PL_parser) {
return op;
}
if (!PL_lex_stuff) {
return op;
}
op_sv = cSVOPx (op)->op_sv;
if (!SvPOK (op_sv)) {
return op;
}
/* sub $name */
s = PL_oldbufptr;
s = hook_toke_skipspace (aTHX_ s);
if (strnNE (s, "sub", 3)) {
return op;
}
if (!isSPACE (s[3])) {
return op;
}
s = hook_toke_skipspace (aTHX_ s + 4);
if (strNE (SvPVX (PL_subname), "?")) {
(void)hook_toke_scan_word (aTHX_ (s - SvPVX (PL_linestr)), 1, tmpbuf, sizeof (tmpbuf), &retlen);
if (retlen < 1) {
return op;
}
name = qualify_func_name (tmpbuf);
if (!sv_eq (PL_subname, name)) {
SvREFCNT_dec (name);
return op;
}
SvREFCNT_dec (name);
}
/* ($proto) */
s = hook_toke_skipspace (aTHX_ s + retlen);
if (s[0] != '(') {
return op;
}
tmp = hook_toke_scan_str (aTHX_ s);
tmp2 = hook_parser_get_lex_stuff (aTHX);
hook_parser_clear_lex_stuff (aTHX);
if (s == tmp || !tmp2) {
return op;
}
strncpy (proto, s + 1, tmp - s - 2);
proto[tmp - s - 2] = '\0';
s++;
while (tmp > s + 1) {
if (isSPACE (s[0])) {
s++;
continue;
}
if (*tmp2 != *s) {
return op;
}
tmp2++;
s++;
}
ret = NULL;
s = hook_toke_skipspace (aTHX_ s + 1);
if (s[0] == ':') {
s++;
while (s[0] != '{') {
char *attr_start;
s = hook_toke_skipspace (aTHX_ s);
attr_start = s;
(void)hook_toke_scan_word (aTHX_ (s - SvPVX (PL_linestr)), 0, tmpbuf, sizeof (tmpbuf), &retlen);
if (retlen < 1) {
return op;
}
s += retlen;
if (s[0] == '(') {
tmp = hook_toke_scan_str (aTHX_ s);
tmp2 = hook_parser_get_lex_stuff (aTHX);
hook_parser_clear_lex_stuff (aTHX);
if (s == tmp) {
return op;
}
s += strlen (tmp2);
if (strEQ (tmpbuf, "proto")) {
while (attr_start < tmp) {
*attr_start = ' ';
attr_start++;
}
ret = op;
sv_setpv (op_sv, tmp2);
}
}
else if (strEQ (tmpbuf, "proto")) {
croak ("proto attribute requires argument");
}
s = hook_toke_skipspace (aTHX_ s);
if (s[0] == ':') {
s++;
}
}
}
if (s[0] != '{') {
/* croak as we already messed with op when :proto is given? */
return op;
}
call_to_perl (ud->class, s - hook_parser_get_linestr (aTHX), proto);
if (!ret) {
op_free (op);
}
return ret;
}
#if PERL_BCDVERSION >= 0x5013006
STATIC void
block_start (pTHX_ int full) {
PERL_UNUSED_VAR (full);
if (SvLEN (PL_linestr) < 16384)
lex_grow_linestr (16384);
}
#endif
STATIC OP *
before_eval (pTHX_ OP *op, void *user_data) {
dSP;
SV *sv, **stack;
SV *class = (SV *)user_data;
#ifdef HAS_HINTS_HASH
if (PL_op->op_private & OPpEVAL_HAS_HH) {
stack = &SP[-1];
}
else {
stack = &SP[0];
}
#else
stack = &SP[0];
#endif
sv = *stack;
if (SvPOK (sv)) {
/* FIXME: this leaks the new scalar */
SV *new = newSVpvs ("use ");
sv_catsv (new, class);
sv_catpvs (new, ";");
sv_catsv (new, sv);
*stack = new;
}
return op;
}
STATIC OP *
handle_eval (pTHX_ OP *op, void *user_data) {
userdata_t *ud = (userdata_t *)user_data;
if (enabled (ud->class)) {
hook_op_ppaddr_around (op, before_eval, NULL, newSVsv (ud->class));
}
return op;
}
MODULE = signatures PACKAGE = signatures
PROTOTYPES: DISABLE
UV
setup (class, f_class)
SV *class
char *f_class
PREINIT:
userdata_t *ud;
#if PERL_BCDVERSION >= 0x5013006
static BHK bhk;
#endif
INIT:
Newx (ud, 1, userdata_t);
ud->class = newSVsv (class);
ud->f_class = f_class;
CODE:
ud->parser_id = hook_parser_setup ();
#if PERL_BCDVERSION >= 0x5013006
BhkENTRY_set (&bhk, bhk_start, block_start);
Perl_blockhook_register (aTHX_ &bhk);
#endif
ud->eval_hook = hook_op_check (OP_ENTEREVAL, handle_eval, ud);
RETVAL = (UV)hook_op_check (OP_CONST, handle_proto, ud);
OUTPUT:
RETVAL
void
teardown (class, id)
UV id
PREINIT:
userdata_t *ud;
CODE:
ud = (userdata_t *)hook_op_check_remove (OP_CONST, id);
if (ud) {
hook_op_check_remove (OP_ENTEREVAL, ud->eval_hook);
hook_parser_teardown (ud->parser_id);
SvREFCNT_dec (ud->class);
Safefree (ud);
}
|