]> git.vpit.fr Git - perl/modules/indirect.git/blob - indirect.xs
Brr. Those strstr() really were fugly
[perl/modules/indirect.git] / indirect.xs
1 /* This file is part of the indirect Perl module.
2  * See http://search.cpan.org/dist/indirect/ */
3
4 #define PERL_NO_GET_CONTEXT
5 #include "EXTERN.h"
6 #include "perl.h"
7 #include "XSUB.h"
8
9 #ifndef SvPV_const
10 # define SvPV_const SvPV
11 #endif
12
13 #ifndef SvPV_nolen_const
14 # define SvPV_nolen_const SvPV_nolen
15 #endif
16
17 #ifndef SvPVX_const
18 # define SvPVX_const SvPVX
19 #endif
20
21 #ifndef sv_catpvn_nomg
22 # define sv_catpvn_nomg sv_catpvn
23 #endif
24
25 /* ... Hints ............................................................... */
26
27 STATIC U32 indirect_hash = 0;
28
29 STATIC IV indirect_hint(pTHX) {
30 #define indirect_hint() indirect_hint(aTHX)
31  SV *id = Perl_refcounted_he_fetch(aTHX_ PL_curcop->cop_hints_hash,
32                                          NULL,
33                                          "indirect", 8,
34                                          0,
35                                          indirect_hash);
36  return (id && SvOK(id) && SvIOK(id)) ? SvIV(id) : 0;
37 }
38
39 /* ... op -> source position ............................................... */
40
41 STATIC HV *indirect_map = NULL;
42 STATIC const char *indirect_linestr = NULL;
43
44 #define OP2STR(O) (sprintf(buf, "%u", PTR2UV(o)))
45
46 STATIC void indirect_map_store(pTHX_ const OP *o, const char *src, SV *sv) {
47 #define indirect_map_store(O, S, N) indirect_map_store(aTHX_ (O), (S), (N))
48  char buf[32];
49  const char *pl_linestr;
50  SV *val;
51
52  /* When lex_inwhat is set, we're in a quotelike environment (qq, qr, but not q)
53   * In this case the linestr has temporarly changed, but the old buffer should
54   * still be alive somewhere. */
55
56  if (!PL_parser->lex_inwhat) {
57   pl_linestr = SvPVX_const(PL_parser->linestr);
58   if (indirect_linestr != pl_linestr) {
59    hv_clear(indirect_map);
60    indirect_linestr = pl_linestr;
61   }
62  }
63
64  val = newSVsv(sv);
65  SvUPGRADE(val, SVt_PVIV);
66  SvUVX(val) = PTR2UV(src);
67  SvIOK_on(val);
68  SvIsUV_on(val);
69  if (!hv_store(indirect_map, buf, OP2STR(o), val, 0)) SvREFCNT_dec(val);
70 }
71
72 STATIC const char *indirect_map_fetch(pTHX_ const OP *o, SV ** const name) {
73 #define indirect_map_fetch(O, S) indirect_map_fetch(aTHX_ (O), (S))
74  char buf[32];
75  SV **val;
76
77  if (indirect_linestr != SvPVX_const(PL_parser->linestr))
78   return NULL;
79
80  val = hv_fetch(indirect_map, buf, OP2STR(o), 0);
81  if (!val) {
82   *name = NULL;
83   return NULL;
84  }
85
86  *name = *val;
87  return INT2PTR(const char *, SvUVX(*val));
88 }
89
90 STATIC void indirect_map_delete(pTHX_ const OP *o) {
91 #define indirect_map_delete(O) indirect_map_delete(aTHX_ (O))
92  char buf[32];
93
94  hv_delete(indirect_map, buf, OP2STR(o), G_DISCARD);
95 }
96
97 STATIC void indirect_map_clean(pTHX_ const OP *o) {
98 #define indirect_map_clean(O) indirect_map_clean(aTHX_ (O))
99  if (o->op_flags & OPf_KIDS) {
100   const OP *kid = cUNOPo->op_first;
101   for (; kid; kid = kid->op_sibling) {
102    indirect_map_delete(kid);
103    indirect_map_clean(kid);
104   }
105  } else {
106   indirect_map_delete(o);
107  }
108 }
109
110 STATIC const char *indirect_find(pTHX_ SV *sv, const char *s) {
111 #define indirect_find(N, S) indirect_find(aTHX_ (N), (S))
112  STRLEN len;
113  const char *p = NULL, *r = SvPV_const(sv, len);
114
115  if (len >= 1 && *r == '$') {
116   ++r;
117   --len;
118   s = strchr(s, '$');
119   if (!s)
120    return NULL;
121  }
122
123  p = strstr(s, r);
124  while (p) {
125   p += len;
126   if (!isALNUM(*p))
127    break;
128   p = strstr(p + 1, r);
129  }
130
131  return p;
132 }
133
134 /* ... ck_const ............................................................ */
135
136 STATIC OP *(*indirect_old_ck_const)(pTHX_ OP *) = 0;
137
138 STATIC OP *indirect_ck_const(pTHX_ OP *o) {
139  o = CALL_FPTR(indirect_old_ck_const)(aTHX_ o);
140
141  if (indirect_hint()) {
142   SV *sv = cSVOPo_sv;
143   if (SvPOK(sv) && (SvTYPE(sv) >= SVt_PV))
144    indirect_map_store(o, indirect_find(sv, PL_parser->oldbufptr), sv);
145  }
146
147  return o;
148 }
149
150 /* ... ck_rv2sv ............................................................ */
151
152 STATIC OP *(*indirect_old_ck_rv2sv)(pTHX_ OP *) = 0;
153
154 STATIC OP *indirect_ck_rv2sv(pTHX_ OP *o) {
155  if (indirect_hint()) {
156   OP *op = cUNOPo->op_first;
157   SV *sv;
158   const char *name = NULL, *s;
159   STRLEN len;
160
161   switch (op->op_type) {
162    case OP_GV:
163    case OP_GVSV: {
164     GV *gv = cGVOPx_gv(op);
165     name = GvNAME(gv);
166     len  = GvNAMELEN(gv);
167     break;
168    }
169    default: {
170     SV *sv = cSVOPx_sv(op);
171     if (SvPOK(sv) && (SvTYPE(sv) >= SVt_PV))
172      name = SvPV_const(sv, len);
173     break;
174    }
175   }
176   if (!name)
177    goto done;
178
179   sv = sv_2mortal(newSVpvn("$", 1));
180   sv_catpvn_nomg(sv, name, len);
181   s = indirect_find(sv, PL_parser->oldbufptr);
182   if (!s) { /* If it failed, retry without the current stash */
183    const char *stash = HvNAME_get(PL_curstash);
184    STRLEN stashlen = HvNAMELEN_get(PL_curstash);
185
186    if ((len < stashlen + 2) || strnNE(name, stash, stashlen)) {
187     /* Failed again ? Try to remove main */
188     stash = "main";
189     stashlen = 4;
190     if ((len < stashlen + 2) || strnNE(name, stash, stashlen))
191      goto done;
192    }
193    if (name[stashlen] != ':' || name[stashlen+1] != ':')
194     goto done;
195
196    sv_setpvn(sv, "$", 1);
197    stashlen += 2;
198    sv_catpvn_nomg(sv, name + stashlen, len - stashlen);
199    s = indirect_find(sv, PL_parser->oldbufptr);
200    if (!s)
201     goto done;
202   }
203
204   o = CALL_FPTR(indirect_old_ck_rv2sv)(aTHX_ o);
205   indirect_map_store(o, s, sv);
206   return o;
207  }
208
209 done:
210  return CALL_FPTR(indirect_old_ck_rv2sv)(aTHX_ o);
211 }
212
213 /* ... ck_padany ........................................................... */
214
215 STATIC OP *(*indirect_old_ck_padany)(pTHX_ OP *) = 0;
216
217 STATIC OP *indirect_ck_padany(pTHX_ OP *o) {
218  o = CALL_FPTR(indirect_old_ck_padany)(aTHX_ o);
219
220  if (indirect_hint()) {
221   SV *sv;
222   const char *s = PL_parser->oldbufptr, *t = PL_parser->bufptr - 1;
223
224   while (s < t && isSPACE(*s)) ++s;
225   if (*s == '$' && ++s <= t) {
226    while (s < t && isSPACE(*s)) ++s;
227    while (s < t && isSPACE(*t)) --t;
228    sv = sv_2mortal(newSVpvn("$", 1));
229    sv_catpvn_nomg(sv, s, t - s + 1);
230    indirect_map_store(o, s, sv);
231   }
232  }
233
234  return o;
235 }
236
237 /* ... ck_method ........................................................... */
238
239 STATIC OP *(*indirect_old_ck_method)(pTHX_ OP *) = 0;
240
241 STATIC OP *indirect_ck_method(pTHX_ OP *o) {
242  if (indirect_hint()) {
243   OP *op = cUNOPo->op_first;
244   SV *sv;
245   const char *s = indirect_map_fetch(op, &sv);
246   if (!s) {
247    sv = cSVOPx_sv(op);
248    if (!SvPOK(sv) || (SvTYPE(sv) < SVt_PV))
249     goto done;
250    sv = sv_mortalcopy(sv);
251    s  = indirect_find(sv, PL_parser->oldbufptr);
252   }
253   o = CALL_FPTR(indirect_old_ck_method)(aTHX_ o);
254   /* o may now be a method_named */
255   indirect_map_store(o, s, sv);
256   return o;
257  }
258
259 done:
260  return CALL_FPTR(indirect_old_ck_method)(aTHX_ o);
261 }
262
263 /* ... ck_entersub ......................................................... */
264
265 STATIC const char indirect_msg[] = "Indirect call of method \"%s\" on object \"%s\"";
266
267 STATIC OP *(*indirect_old_ck_entersub)(pTHX_ OP *) = 0;
268
269 STATIC OP *indirect_ck_entersub(pTHX_ OP *o) {
270  LISTOP *op;
271  OP *om, *oo;
272  IV hint = indirect_hint();
273
274  o = CALL_FPTR(indirect_old_ck_entersub)(aTHX_ o);
275
276  if (hint) {
277   const char *pm, *po;
278   SV *svm, *svo;
279   oo = o;
280   do {
281    op = (LISTOP *) oo;
282    if (!op->op_flags & OPf_KIDS)
283     goto done;
284    oo = op->op_first;
285   } while (oo->op_type != OP_PUSHMARK);
286   oo = oo->op_sibling;
287   om = op->op_last;
288   if (om->op_type == OP_METHOD)
289    om = cUNOPx(om)->op_first;
290   else if (om->op_type != OP_METHOD_NAMED)
291    goto done;
292   pm = indirect_map_fetch(om, &svm);
293   po = indirect_map_fetch(oo, &svo);
294   if (pm && po && pm < po) {
295    const char *psvm = SvPV_nolen_const(svm), *psvo = SvPV_nolen_const(svo);
296    if (hint == 2)
297     croak(indirect_msg, psvm, psvo);
298    else
299     warn(indirect_msg, psvm, psvo);
300   }
301 done:
302   indirect_map_clean(o);
303  }
304
305  return o;
306 }
307
308 STATIC U32 indirect_initialized = 0;
309
310 /* --- XS ------------------------------------------------------------------ */
311
312 MODULE = indirect      PACKAGE = indirect
313
314 PROTOTYPES: DISABLE
315
316 BOOT:
317 {
318  if (!indirect_initialized++) {
319   PERL_HASH(indirect_hash, "indirect", 8);
320   indirect_map = newHV();
321   indirect_old_ck_const    = PL_check[OP_CONST];
322   PL_check[OP_CONST]       = MEMBER_TO_FPTR(indirect_ck_const);
323   indirect_old_ck_rv2sv    = PL_check[OP_RV2SV];
324   PL_check[OP_RV2SV]       = MEMBER_TO_FPTR(indirect_ck_rv2sv);
325   indirect_old_ck_padany   = PL_check[OP_PADANY];
326   PL_check[OP_PADANY]      = MEMBER_TO_FPTR(indirect_ck_padany);
327   indirect_old_ck_method   = PL_check[OP_METHOD];
328   PL_check[OP_METHOD]      = MEMBER_TO_FPTR(indirect_ck_method);
329   indirect_old_ck_entersub = PL_check[OP_ENTERSUB];
330   PL_check[OP_ENTERSUB]    = MEMBER_TO_FPTR(indirect_ck_entersub);
331  }
332 }