Remove the enable_debugging member from the structure, and instead
[p5sagit/p5-mst-13.2.git] / ext / re / re.xs
1 #if defined(PERL_EXT_RE_DEBUG) && !defined(DEBUGGING)
2 #  define DEBUGGING
3 #endif
4
5 #define PERL_NO_GET_CONTEXT
6 #include "EXTERN.h"
7 #include "perl.h"
8 #include "XSUB.h"
9
10 START_EXTERN_C
11
12 extern regexp*  my_regcomp (pTHX_ char* exp, char* xend, PMOP* pm);
13 extern I32      my_regexec (pTHX_ regexp* prog, char* stringarg, char* strend,
14                             char* strbeg, I32 minend, SV* screamer,
15                             void* data, U32 flags);
16 extern void     my_regfree (pTHX_ struct regexp* r);
17 extern char*    my_re_intuit_start (pTHX_ regexp *prog, SV *sv, char *strpos,
18                                     char *strend, U32 flags,
19                                     struct re_scream_pos_data_s *data);
20 extern SV*      my_re_intuit_string (pTHX_ regexp *prog);
21
22 END_EXTERN_C
23
24 /* engine details need to be paired - non debugging, debuggin  */
25 #define NEEDS_DEBUGGING 0x01
26 struct regexp_engine {
27     regexp*     (*regcomp) (pTHX_ char* exp, char* xend, PMOP* pm);
28     I32         (*regexec) (pTHX_ regexp* prog, char* stringarg, char* strend,
29                             char* strbeg, I32 minend, SV* screamer,
30                             void* data, U32 flags);
31     char*       (*re_intuit_start) (pTHX_ regexp *prog, SV *sv, char *strpos,
32                                     char *strend, U32 flags,
33                                     struct re_scream_pos_data_s *data);
34     SV*         (*re_intuit_string) (pTHX_ regexp *prog);
35     void        (*regfree) (pTHX_ struct regexp* r);
36 };
37
38 struct regexp_engine engines[] = {
39     { Perl_pregcomp, Perl_regexec_flags, Perl_re_intuit_start,
40       Perl_re_intuit_string, Perl_pregfree },
41     { my_regcomp, my_regexec, my_re_intuit_start, my_re_intuit_string,
42       my_regfree }
43 };
44
45 #define MY_CXT_KEY "re::_guts" XS_VERSION
46
47 typedef struct {
48     int         x_oldflag;              /* debug flag */
49     unsigned int x_state;
50 } my_cxt_t;
51
52 START_MY_CXT
53
54 #define oldflag         (MY_CXT.x_oldflag)
55
56 static void
57 install(pTHX_ unsigned int new_state)
58 {
59     dMY_CXT;
60     const unsigned int states 
61         = sizeof(engines) / sizeof(struct regexp_engine) -1;
62     if(new_state == MY_CXT.x_state)
63         return;
64
65     if (new_state > states) {
66         Perl_croak(aTHX_ "panic: re::install state %u is illegal - max is %u",
67                    new_state, states);
68     }
69
70     PL_regexecp = engines[new_state].regexec;
71     PL_regcompp = engines[new_state].regcomp;
72     PL_regint_start = engines[new_state].re_intuit_start;
73     PL_regint_string = engines[new_state].re_intuit_string;
74     PL_regfree = engines[new_state].regfree;
75
76     if (new_state & NEEDS_DEBUGGING) {
77         PL_colorset = 0;        /* Allow reinspection of ENV. */
78         if (!(MY_CXT.x_state & NEEDS_DEBUGGING)) {
79             /* Debugging is turned on for the first time.  */
80             oldflag = PL_debug & DEBUG_r_FLAG;
81             PL_debug |= DEBUG_r_FLAG;
82         }
83     } else {
84         if (!(MY_CXT.x_state & NEEDS_DEBUGGING)) {
85             if (!oldflag)
86                 PL_debug &= ~DEBUG_r_FLAG;
87         }
88     }
89
90     MY_CXT.x_state = new_state;
91 }
92
93 MODULE = re     PACKAGE = re
94
95 BOOT:
96 {
97    MY_CXT_INIT;
98 }
99
100
101 void
102 install(new_state)
103   unsigned int new_state;
104   CODE:
105     install(aTHX_ new_state);