1 /**********************************************************************
2 reggnu.c - Oniguruma (regular expression library)
3 **********************************************************************/
4 /*-
5 * Copyright (c) 2002-2008 K.Kosako <sndgk393 AT ybb DOT ne DOT jp>
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30 #include "regint.h"
31
32 #ifndef ONIGGNU_H
33 #include "oniggnu.h"
34 #endif
35
36 extern void
re_free_registers(OnigRegion * r)37 re_free_registers(OnigRegion* r)
38 {
39 /* 0: don't free self */
40 onig_region_free(r, 0);
41 }
42
43 extern int
re_adjust_startpos(regex_t * reg,const char * string,int size,int startpos,int range)44 re_adjust_startpos(regex_t* reg, const char* string, int size,
45 int startpos, int range)
46 {
47 if (startpos > 0 && ONIGENC_MBC_MAXLEN(reg->enc) != 1 && startpos < size) {
48 UChar *p;
49 UChar *s = (UChar* )string + startpos;
50
51 if (range > 0) {
52 p = onigenc_get_right_adjust_char_head(reg->enc, (UChar* )string, s);
53 }
54 else {
55 p = ONIGENC_LEFT_ADJUST_CHAR_HEAD(reg->enc, (UChar* )string, s);
56 }
57 return p - (UChar* )string;
58 }
59
60 return startpos;
61 }
62
63 extern int
re_match(regex_t * reg,const char * str,int size,int pos,struct re_registers * regs)64 re_match(regex_t* reg, const char* str, int size, int pos,
65 struct re_registers* regs)
66 {
67 return onig_match(reg, (UChar* )str, (UChar* )(str + size),
68 (UChar* )(str + pos), regs, ONIG_OPTION_NONE);
69 }
70
71 extern int
re_search(regex_t * bufp,const char * string,int size,int startpos,int range,struct re_registers * regs)72 re_search(regex_t* bufp, const char* string, int size, int startpos, int range,
73 struct re_registers* regs)
74 {
75 return onig_search(bufp, (UChar* )string, (UChar* )(string + size),
76 (UChar* )(string + startpos),
77 (UChar* )(string + startpos + range),
78 regs, ONIG_OPTION_NONE);
79 }
80
81 extern int
re_compile_pattern(const char * pattern,int size,regex_t * reg,char * ebuf)82 re_compile_pattern(const char* pattern, int size, regex_t* reg, char* ebuf)
83 {
84 int r;
85 OnigErrorInfo einfo;
86
87 r = onig_compile(reg, (UChar* )pattern, (UChar* )(pattern + size), &einfo);
88 if (r != ONIG_NORMAL) {
89 if (IS_NOT_NULL(ebuf))
90 (void )onig_error_code_to_str((UChar* )ebuf, r, &einfo);
91 }
92
93 return r;
94 }
95
96 extern void
re_free_pattern(regex_t * reg)97 re_free_pattern(regex_t* reg)
98 {
99 onig_free(reg);
100 }
101
102 extern int
re_alloc_pattern(regex_t ** reg)103 re_alloc_pattern(regex_t** reg)
104 {
105 *reg = (regex_t* )xmalloc(sizeof(regex_t));
106 if (IS_NULL(*reg)) return ONIGERR_MEMORY;
107
108 return onig_reg_init(*reg, ONIG_OPTION_DEFAULT,
109 ONIGENC_CASE_FOLD_DEFAULT,
110 OnigEncDefaultCharEncoding,
111 OnigDefaultSyntax);
112 }
113
114 extern void
re_set_casetable(const char * table)115 re_set_casetable(const char* table)
116 {
117 onigenc_set_default_caseconv_table((UChar* )table);
118 }
119
120 extern void
re_mbcinit(int mb_code)121 re_mbcinit(int mb_code)
122 {
123 OnigEncoding enc;
124
125 switch (mb_code) {
126 case RE_MBCTYPE_ASCII:
127 enc = ONIG_ENCODING_ASCII;
128 break;
129 case RE_MBCTYPE_EUC:
130 enc = ONIG_ENCODING_EUC_JP;
131 break;
132 case RE_MBCTYPE_SJIS:
133 enc = ONIG_ENCODING_SJIS;
134 break;
135 case RE_MBCTYPE_UTF8:
136 enc = ONIG_ENCODING_UTF8;
137 break;
138 default:
139 return ;
140 break;
141 }
142
143 onig_initialize(0, 0);
144 onig_initialize_encoding(enc);
145
146 onigenc_set_default_encoding(enc);
147 }
148