avoid memory leak in toke_scan_str
[p5sagit/Devel-Declare.git] / Declare.xs
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4 #include "hook_op_check.h"
5 #undef printf
6 #include "stolen_chunk_of_toke.c"
7 #include <stdio.h>
8 #include <string.h>
9
10 #define PERL_VERSION_DECIMAL(r,v,s) (r*1000000 + v*1000 + s)
11 #define PERL_DECIMAL_VERSION \
12   PERL_VERSION_DECIMAL(PERL_REVISION,PERL_VERSION,PERL_SUBVERSION)
13 #define PERL_VERSION_GE(r,v,s) \
14   (PERL_DECIMAL_VERSION >= PERL_VERSION_DECIMAL(r,v,s))
15
16 #ifndef Newx
17 # define Newx(v,n,t) New(0,v,n,t)
18 #endif /* !Newx */
19
20 #define DD_DEBUGf_UPDATED_LINESTR 1
21 #define DD_DEBUGf_TRACE 2
22
23 #define DD_DEBUG_UPDATED_LINESTR (dd_debug & DD_DEBUGf_UPDATED_LINESTR)
24 #define DD_DEBUG_TRACE (dd_debug & DD_DEBUGf_TRACE)
25 static int dd_debug = 0;
26
27 #define DD_CONST_VIA_RV2CV PERL_VERSION_GE(5,11,2)
28
29 #define DD_GROW_VIA_BLOCKHOOK PERL_VERSION_GE(5,13,3)
30
31 #define LEX_NORMAL    10
32 #define LEX_INTERPNORMAL   9
33
34 /* please try not to have a line longer than this :) */
35
36 #define DD_PREFERRED_LINESTR_SIZE 16384
37
38 /* flag to trigger removal of temporary declaree sub */
39
40 static int in_declare = 0;
41
42 /* in 5.10, PL_parser will be NULL if we aren't parsing, and PL_lex_stuff
43    is a lookup into it - so if anything else we can use to tell, so we
44    need to be a bit more careful if PL_parser exists */
45
46 #define DD_AM_LEXING_CHECK (PL_lex_state == LEX_NORMAL || PL_lex_state == LEX_INTERPNORMAL)
47
48 #if defined(PL_parser) || defined(PERL_5_9_PLUS)
49 #define DD_HAVE_PARSER PL_parser
50 #define DD_HAVE_LEX_STUFF (PL_parser && PL_lex_stuff)
51 #define DD_AM_LEXING (PL_parser && DD_AM_LEXING_CHECK)
52 #else
53 #define DD_HAVE_PARSER 1
54 #define DD_HAVE_LEX_STUFF PL_lex_stuff
55 #define DD_AM_LEXING DD_AM_LEXING_CHECK
56 #endif
57
58 /* thing that decides whether we're dealing with a declarator */
59
60 int dd_is_declarator(pTHX_ char* name) {
61   HV* is_declarator;
62   SV** is_declarator_pack_ref;
63   HV* is_declarator_pack_hash;
64   SV** is_declarator_flag_ref;
65   int dd_flags;
66
67   is_declarator = get_hv("Devel::Declare::declarators", FALSE);
68
69   if (!is_declarator)
70     return -1;
71
72   /* $declarators{$current_package_name} */
73
74   if (!HvNAME(PL_curstash))
75     return -1;
76
77   is_declarator_pack_ref = hv_fetch(is_declarator, HvNAME(PL_curstash),
78                              strlen(HvNAME(PL_curstash)), FALSE);
79
80   if (!is_declarator_pack_ref || !SvROK(*is_declarator_pack_ref))
81     return -1; /* not a hashref */
82
83   is_declarator_pack_hash = (HV*) SvRV(*is_declarator_pack_ref);
84
85   /* $declarators{$current_package_name}{$name} */
86
87   is_declarator_flag_ref = hv_fetch(
88     is_declarator_pack_hash, name,
89     strlen(name), FALSE
90   );
91
92   /* requires SvIOK as well as TRUE since flags not being an int is useless */
93
94   if (!is_declarator_flag_ref
95         || !SvIOK(*is_declarator_flag_ref)
96         || !SvTRUE(*is_declarator_flag_ref))
97     return -1;
98
99   dd_flags = SvIVX(*is_declarator_flag_ref);
100
101   return dd_flags;
102 }
103
104 /* callback thingy */
105
106 void dd_linestr_callback (pTHX_ char* type, char* name) {
107
108   char* linestr = SvPVX(PL_linestr);
109   int offset = PL_bufptr - linestr;
110
111   dSP;
112
113   ENTER;
114   SAVETMPS;
115
116   PUSHMARK(SP);
117   XPUSHs(sv_2mortal(newSVpv(type, 0)));
118   XPUSHs(sv_2mortal(newSVpv(name, 0)));
119   XPUSHs(sv_2mortal(newSViv(offset)));
120   PUTBACK;
121
122   call_pv("Devel::Declare::linestr_callback", G_VOID|G_DISCARD);
123
124   FREETMPS;
125   LEAVE;
126 }
127
128 char* dd_get_linestr(pTHX) {
129   if (!DD_HAVE_PARSER) {
130     return NULL;
131   }
132   return SvPVX(PL_linestr);
133 }
134
135 void dd_set_linestr(pTHX_ char* new_value) {
136   unsigned int new_len = strlen(new_value);
137
138   if (SvLEN(PL_linestr) < new_len) {
139     croak("PL_linestr not long enough, was Devel::Declare loaded soon enough in %s",
140       CopFILE(&PL_compiling)
141     );
142   }
143
144
145   memcpy(SvPVX(PL_linestr), new_value, new_len+1);
146
147   SvCUR_set(PL_linestr, new_len);
148
149   PL_bufend = SvPVX(PL_linestr) + new_len;
150
151   if ( DD_DEBUG_UPDATED_LINESTR && PERLDB_LINE && PL_curstash != PL_debstash) {
152     // Cribbed from toke.c
153     SV * const sv = NEWSV(85,0);
154
155     sv_upgrade(sv, SVt_PVMG);
156     sv_setpvn(sv,PL_bufptr,PL_bufend-PL_bufptr);
157     (void)SvIOK_on(sv);
158     SvIV_set(sv, 0);
159     av_store(CopFILEAV(&PL_compiling),(I32)CopLINE(&PL_compiling),sv);
160   }
161 }
162
163 char* dd_get_lex_stuff(pTHX) {
164   return (DD_HAVE_LEX_STUFF ? SvPVX(PL_lex_stuff) : "");
165 }
166
167 void dd_clear_lex_stuff(pTHX) {
168   if (DD_HAVE_PARSER)
169     PL_lex_stuff = (SV*)NULL;
170 }
171
172 char* dd_get_curstash_name(pTHX) {
173   return HvNAME(PL_curstash);
174 }
175
176 int dd_get_linestr_offset(pTHX) {
177   char* linestr;
178   if (!DD_HAVE_PARSER) {
179     return -1;
180   }
181   linestr = SvPVX(PL_linestr);
182   return PL_bufptr - linestr;
183 }
184
185 char* dd_move_past_token (pTHX_ char* s) {
186
187   /*
188    *   buffer will be at the beginning of the declarator, -unless- the
189    *   declarator is at EOL in which case it'll be the next useful line
190    *   so we don't short-circuit out if we don't find the declarator
191    */
192
193   while (s < PL_bufend && isSPACE(*s)) s++;
194   if (memEQ(s, PL_tokenbuf, strlen(PL_tokenbuf)))
195     s += strlen(PL_tokenbuf);
196   return s;
197 }
198
199 int dd_toke_move_past_token (pTHX_ int offset) {
200   char* base_s = SvPVX(PL_linestr) + offset;
201   char* s = dd_move_past_token(aTHX_ base_s);
202   return s - base_s;
203 }
204
205 int dd_toke_scan_word(pTHX_ int offset, int handle_package) {
206   char tmpbuf[sizeof PL_tokenbuf];
207   char* base_s = SvPVX(PL_linestr) + offset;
208   STRLEN len;
209   char* s = scan_word(base_s, tmpbuf, sizeof tmpbuf, handle_package, &len);
210   return s - base_s;
211 }
212
213 int dd_toke_scan_ident(pTHX_ int offset) {
214     char tmpbuf[sizeof PL_tokenbuf];
215     char* base_s = SvPVX(PL_linestr) + offset;
216     char* s = scan_ident(base_s, PL_bufend, tmpbuf, sizeof tmpbuf, 0);
217     return s - base_s;
218 }
219
220 int dd_toke_scan_str(pTHX_ int offset) {
221   char* old_pvx = SvPVX(PL_linestr);
222   STRLEN remaining = sv_len(PL_linestr) - offset;
223   SV* line_copy = sv_2mortal(newSVsv(PL_linestr));
224   char* base_s = SvPVX(PL_linestr) + offset;
225   char* s = scan_str(base_s, FALSE, FALSE);
226   if(SvPVX(PL_linestr) != old_pvx)
227     croak("PL_linestr reallocated during scan_str, "
228       "Devel::Declare can't continue");
229   if (s != base_s && sv_len(PL_lex_stuff) > remaining) {
230     int ret = (s - SvPVX(PL_linestr)) + remaining;
231     sv_catsv(line_copy, PL_linestr);
232     dd_set_linestr(aTHX_ SvPV_nolen(line_copy));
233     return ret;
234   }
235   return s - base_s;
236 }
237
238 int dd_toke_skipspace(pTHX_ int offset) {
239   char* old_pvx = SvPVX(PL_linestr);
240   char* base_s = SvPVX(PL_linestr) + offset;
241   char* s = skipspace_force(base_s);
242   if(SvPVX(PL_linestr) != old_pvx)
243     croak("PL_linestr reallocated during skipspace, "
244       "Devel::Declare can't continue");
245   return s - base_s;
246 }
247
248 static void call_done_declare(pTHX) {
249   dSP;
250
251   if (DD_DEBUG_TRACE) {
252     printf("Deconstructing declare\n");
253     printf("PL_bufptr: %s\n", PL_bufptr);
254     printf("bufend at: %i\n", PL_bufend - PL_bufptr);
255     printf("linestr: %s\n", SvPVX(PL_linestr));
256     printf("linestr len: %i\n", PL_bufend - SvPVX(PL_linestr));
257   }
258
259   ENTER;
260   SAVETMPS;
261
262   PUSHMARK(SP);
263
264   call_pv("Devel::Declare::done_declare", G_VOID|G_DISCARD);
265
266   FREETMPS;
267   LEAVE;
268
269   if (DD_DEBUG_TRACE) {
270     printf("PL_bufptr: %s\n", PL_bufptr);
271     printf("bufend at: %i\n", PL_bufend - PL_bufptr);
272     printf("linestr: %s\n", SvPVX(PL_linestr));
273     printf("linestr len: %i\n", PL_bufend - SvPVX(PL_linestr));
274     printf("actual len: %i\n", strlen(PL_bufptr));
275   }
276 }
277
278 static int dd_handle_const(pTHX_ char *name);
279
280 /* replacement PL_check rv2cv entry */
281
282 STATIC OP *dd_ck_rv2cv(pTHX_ OP *o, void *user_data) {
283   OP* kid;
284   int dd_flags;
285
286   PERL_UNUSED_VAR(user_data);
287
288   if (!DD_AM_LEXING)
289     return o; /* not lexing? */
290
291   if (in_declare) {
292     call_done_declare(aTHX);
293     return o;
294   }
295
296   kid = cUNOPo->op_first;
297
298   if (kid->op_type != OP_GV) /* not a GV so ignore */
299     return o;
300
301   if (DD_DEBUG_TRACE) {
302     printf("Checking GV %s -> %s\n", HvNAME(GvSTASH(kGVOP_gv)), GvNAME(kGVOP_gv));
303   }
304
305   dd_flags = dd_is_declarator(aTHX_ GvNAME(kGVOP_gv));
306
307   if (dd_flags == -1)
308     return o;
309
310   if (DD_DEBUG_TRACE) {
311     printf("dd_flags are: %i\n", dd_flags);
312     printf("PL_tokenbuf: %s\n", PL_tokenbuf);
313   }
314
315 #if DD_CONST_VIA_RV2CV
316   if (PL_expect != XOPERATOR) {
317     if (!dd_handle_const(aTHX_ GvNAME(kGVOP_gv)))
318       return o;
319     CopLINE(PL_curcop) = PL_copline;
320     /* The parser behaviour that we're simulating depends on what comes
321        after the declarator. */
322     if (*skipspace(PL_bufptr + strlen(GvNAME(kGVOP_gv))) != '(') {
323       if (in_declare) {
324         call_done_declare(aTHX);
325       } else {
326         dd_linestr_callback(aTHX_ "rv2cv", GvNAME(kGVOP_gv));
327       }
328     }
329     return o;
330   }
331 #endif /* DD_CONST_VIA_RV2CV */
332
333   dd_linestr_callback(aTHX_ "rv2cv", GvNAME(kGVOP_gv));
334
335   return o;
336 }
337
338 #if DD_GROW_VIA_BLOCKHOOK
339
340 static void dd_block_start(pTHX_ int full)
341 {
342   PERL_UNUSED_VAR(full);
343   if (SvLEN(PL_linestr) < DD_PREFERRED_LINESTR_SIZE)
344     (void) lex_grow_linestr(DD_PREFERRED_LINESTR_SIZE);
345 }
346
347 #else /* !DD_GROW_VIA_BLOCKHOOK */
348
349 OP* dd_pp_entereval(pTHX) {
350   dSP;
351   STRLEN len;
352   const char* s;
353   SV *sv;
354 #ifdef PERL_5_9_PLUS
355   SV *saved_hh;
356   if (PL_op->op_private & OPpEVAL_HAS_HH) {
357     saved_hh = POPs;
358   }
359 #endif
360   sv = POPs;
361   if (SvPOK(sv)) {
362     if (DD_DEBUG_TRACE) {
363       printf("mangling eval sv\n");
364     }
365     if (SvREADONLY(sv))
366       sv = sv_2mortal(newSVsv(sv));
367     s = SvPVX(sv);
368     len = SvCUR(sv);
369     if (!len || s[len-1] != ';') {
370       if (!(SvFLAGS(sv) & SVs_TEMP))
371         sv = sv_2mortal(newSVsv(sv));
372       sv_catpvn(sv, "\n;", 2);
373     }
374     SvGROW(sv, DD_PREFERRED_LINESTR_SIZE);
375   }
376   PUSHs(sv);
377 #ifdef PERL_5_9_PLUS
378   if (PL_op->op_private & OPpEVAL_HAS_HH) {
379     PUSHs(saved_hh);
380   }
381 #endif
382   return PL_ppaddr[OP_ENTEREVAL](aTHX);
383 }
384
385 STATIC OP *dd_ck_entereval(pTHX_ OP *o, void *user_data) {
386   PERL_UNUSED_VAR(user_data);
387
388   if (o->op_ppaddr == PL_ppaddr[OP_ENTEREVAL])
389     o->op_ppaddr = dd_pp_entereval;
390   return o;
391 }
392
393 #endif /* !DD_GROW_VIA_BLOCKHOOK */
394
395 static I32 dd_filter_realloc(pTHX_ int idx, SV *sv, int maxlen)
396 {
397   const I32 count = FILTER_READ(idx+1, sv, maxlen);
398   SvGROW(sv, DD_PREFERRED_LINESTR_SIZE);
399   /* filter_del(dd_filter_realloc); */
400   return count;
401 }
402
403 static int dd_handle_const(pTHX_ char *name) {
404   switch (PL_lex_inwhat) {
405     case OP_QR:
406     case OP_MATCH:
407     case OP_SUBST:
408     case OP_TRANS:
409     case OP_BACKTICK:
410     case OP_STRINGIFY:
411       return 0;
412       break;
413     default:
414       break;
415   }
416
417   if (strnEQ(PL_bufptr, "->", 2)) {
418     return 0;
419   }
420
421   {
422     char buf[256];
423     STRLEN len;
424     char *s = PL_bufptr;
425     STRLEN old_offset = PL_bufptr - SvPVX(PL_linestr);
426
427     s = scan_word(s, buf, sizeof buf, FALSE, &len);
428     if (strnEQ(buf, name, len)) {
429       char *d;
430       SV *inject = newSVpvn(SvPVX(PL_linestr), PL_bufptr - SvPVX(PL_linestr));
431       sv_catpvn(inject, buf, len);
432
433       d = peekspace(s);
434       sv_catpvn(inject, s, d - s);
435
436       if ((PL_bufend - d) >= 2 && strnEQ(d, "=>", 2)) {
437         return 0;
438       }
439
440       sv_catpv(inject, d);
441       dd_set_linestr(aTHX_ SvPV_nolen(inject));
442       PL_bufptr = SvPVX(PL_linestr) + old_offset;
443       SvREFCNT_dec (inject);
444     }
445   }
446
447   dd_linestr_callback(aTHX_ "const", name);
448
449   return 1;
450 }
451
452 #if !DD_CONST_VIA_RV2CV
453
454 STATIC OP *dd_ck_const(pTHX_ OP *o, void *user_data) {
455   int dd_flags;
456   char* name;
457
458   PERL_UNUSED_VAR(user_data);
459
460   if (DD_HAVE_PARSER && PL_expect == XOPERATOR) {
461     return o;
462   }
463
464   /* if this is set, we just grabbed a delimited string or something,
465      not a bareword, so NO TOUCHY */
466
467   if (DD_HAVE_LEX_STUFF)
468     return o;
469
470   /* don't try and look this up if it's not a string const */
471   if (!SvPOK(cSVOPo->op_sv))
472     return o;
473
474   name = SvPVX(cSVOPo->op_sv);
475
476   dd_flags = dd_is_declarator(aTHX_ name);
477
478   if (dd_flags == -1)
479     return o;
480
481   dd_handle_const(aTHX_ name);
482
483   return o;
484 }
485
486 #endif /* !DD_CONST_VIA_RV2CV */
487
488 static int initialized = 0;
489
490 MODULE = Devel::Declare  PACKAGE = Devel::Declare
491
492 PROTOTYPES: DISABLE
493
494 void
495 setup()
496   CODE:
497   if (!initialized++) {
498 #if DD_GROW_VIA_BLOCKHOOK
499     static BHK bhk;
500 #if PERL_VERSION_GE(5,13,6)
501     BhkENTRY_set(&bhk, bhk_start, dd_block_start);
502 #else /* <5.13.6 */
503     BhkENTRY_set(&bhk, start, dd_block_start);
504 #endif /* <5.13.6 */
505     Perl_blockhook_register(aTHX_ &bhk);
506 #else /* !DD_GROW_VIA_BLOCKHOOK */
507     hook_op_check(OP_ENTEREVAL, dd_ck_entereval, NULL);
508 #endif /* !DD_GROW_VIA_BLOCKHOOK */
509     hook_op_check(OP_RV2CV, dd_ck_rv2cv, NULL);
510 #if !DD_CONST_VIA_RV2CV
511     hook_op_check(OP_CONST, dd_ck_const, NULL);
512 #endif /* !DD_CONST_VIA_RV2CV */
513   }
514   filter_add(dd_filter_realloc, NULL);
515
516 char*
517 get_linestr()
518   CODE:
519     RETVAL = dd_get_linestr(aTHX);
520   OUTPUT:
521     RETVAL
522
523 void
524 set_linestr(char* new_value)
525   CODE:
526     dd_set_linestr(aTHX_ new_value);
527
528 char*
529 get_lex_stuff()
530   CODE:
531     RETVAL = dd_get_lex_stuff(aTHX);
532   OUTPUT:
533     RETVAL
534
535 void
536 clear_lex_stuff()
537   CODE:
538     dd_clear_lex_stuff(aTHX);
539
540 char*
541 get_curstash_name()
542   CODE:
543     RETVAL = dd_get_curstash_name(aTHX);
544   OUTPUT:
545     RETVAL
546
547 int
548 get_linestr_offset()
549   CODE:
550     RETVAL = dd_get_linestr_offset(aTHX);
551   OUTPUT:
552     RETVAL
553
554 int
555 toke_scan_word(int offset, int handle_package)
556   CODE:
557     RETVAL = dd_toke_scan_word(aTHX_ offset, handle_package);
558   OUTPUT:
559     RETVAL
560
561 int
562 toke_move_past_token(int offset);
563   CODE:
564     RETVAL = dd_toke_move_past_token(aTHX_ offset);
565   OUTPUT:
566     RETVAL
567
568 int
569 toke_scan_str(int offset);
570   CODE:
571     RETVAL = dd_toke_scan_str(aTHX_ offset);
572   OUTPUT:
573     RETVAL
574
575 int
576 toke_scan_ident(int offset)
577   CODE:
578     RETVAL = dd_toke_scan_ident(aTHX_ offset);
579   OUTPUT:
580     RETVAL
581
582 int
583 toke_skipspace(int offset)
584   CODE:
585     RETVAL = dd_toke_skipspace(aTHX_ offset);
586   OUTPUT:
587     RETVAL
588
589 int
590 get_in_declare()
591   CODE:
592     RETVAL = in_declare;
593   OUTPUT:
594     RETVAL
595
596 void
597 set_in_declare(int value)
598   CODE:
599     in_declare = value;
600
601 BOOT:
602 {
603   char *endptr;
604   char *debug_str = getenv ("DD_DEBUG");
605   if (debug_str) {
606     dd_debug = strtol (debug_str, &endptr, 10);
607     if (*endptr != '\0') {
608       dd_debug = 0;
609     }
610   }
611 }