-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathlib_mysqludf_preg_capture.c
308 lines (278 loc) · 11 KB
/
lib_mysqludf_preg_capture.c
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
/*
* Copyright (C) 2007-2013 Rich Waters <[email protected]>
*
* This file is part of lib_mysqludf_preg.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
/**
* @file lib_mysqludf_preg_capture.c
*
* @brief Implements the PREG_CAPTURE mysql udf
*
*/
/**
* @page PREG_CAPTURE PREG_CAPTURE
*
* @brief capture a parenthesized subexpression from a PCRE pattern
*
* @par Function Installation
* CREATE FUNCTION preg_capture RETURNS STRING SONAME 'lib_mysqludf_preg.so';
*
* @par Synopsis
* PREG_CAPTURE( pattern , subject [, group] [, occurence] )
*
* @par
* @param pattern - is a string that is a perl compatible regular
* expression as documented at:
* http://us.php.net/manual/en/ref.pcre.php This expression passed to
* this function should have delimiters and can contain the standard
* perl modifiers after the ending delimiter.
*
* @param subject -is the data to perform the match & capture on
*
* @param group - is the capture group that should be
* returned. This can be a numeric capture group or a named capture group.
* Numeric groups should be passed in as integers while named groups should be
* strings. If not speficied, this defaults to 0, which will capture the
* entire matching regular expression.
*
* @param occurence - which match of the regex to perform capture on.
* This is useful for subjects that have multiple matches of the pattern. If
* not speficied, this defaults to 1, which will capture the requested group,
* from the first matching occurence of the pattern.
*
* @return - string that was captured - if there was a match and the desired
* capture group is valid
* @return - string that is the entire portion of subject which matches the
* pattern - if 0 is passed in as the group and pattern matches subject
* @return - NULL - if pattern does not match the subject or group is not a
* valid capture group for the given pattern and subject.
*
* @details
* preg_capture is a udf that captures parenthesized sub-expressions
* from a pcre pattern.
*
* @par Examples:
*
*
* SELECT PREG_CAPTURE('/(.*?)(fox)/' , 'the quick brown fox' ,2 );
*
* @b Yields:
* @verbatim
+----------------------------------------------------------+
| PREG_CAPTURE('/(.*?)(fox)/' , 'the quick brown fox' ,2 ) |
+----------------------------------------------------------+
| fox |
+----------------------------------------------------------+
@endverbatim
*
* SELECT PREG_CAPTURE( '/"([^"]+)"/' , 'the "quick" brown fox "jumped" over the "lazy" dog' , 1,2 );
*
* @verbatim
+--------------------------------------------------------------------------------------------+
| PREG_CAPTURE( '/"([^"]+)"/' , 'the "quick" brown fox "jumped" over the "lazy" dog' , 1,2 ) |
+--------------------------------------------------------------------------------------------+
| jumped |
+--------------------------------------------------------------------------------------------+
@endverbatim
*
* SELECT PREG_CAPTURE( '/b[^\\s]+/' , 'the quick brown fox jumped over' )
*
* @b Yields:
*
* @verbatim
+------------------------------------------------------------------+
| PREG_CAPTURE( '/b[^\\s]+/' , 'the quick brown fox jumped over' ) |
+------------------------------------------------------------------+
| brown |
+------------------------------------------------------------------+
@endverbatim
*
* @note
* Remember to add a backslash to escape patterns that use \ notation
*/
#include "ghmysql.h"
#include "preg.h"
/*
* Public function declarations:
*/
bool preg_capture_init(UDF_INIT *initid, UDF_ARGS *args, char *message);
char *preg_capture( UDF_INIT *initid __attribute__((unused)),
UDF_ARGS *args, char *result, unsigned long *length,
char *is_null __attribute__((unused)),
char *error __attribute__((unused)));
void preg_capture_deinit( UDF_INIT* initid );
/**
* @fn bool preg_capture_init(UDF_INIT *initid, UDF_ARGS *args,
* char *message)
*
* @brief
* Perform the per-query initializations for PREG_CAPTURE
*
* @param initid - various info supplied by mysql api - read mode at
* http://dev.mysql.com/doc/refman/5.0/en/adding-udf.html
*
* @param args - array of information about arguments from the SQL call
* See file documentation for the description of the SQL arguments
*
* @param message - for error messages. Should be <80 but can be 255.
*
* @return 0 - on success
* @return 1 - on error
*
* @details This function calls pregInit to handle the common init taskes.
* It also checks to make sure there are at least arguments, and checks
* the type of the 'group' and 'occurence' arguments.
*/
bool preg_capture_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
{
if (args->arg_count < 2)
{
strncpy(message,"PREG_CAPTURE: requires at least 2 arguments", MYSQL_ERRMSG_SIZE);
return 1;
}
if( args->arg_count > 3 && args->arg_type[3] != INT_RESULT ) {
strncpy(message,"PREG_CAPTURE: optional occurence argument must be an integer", MYSQL_ERRMSG_SIZE);
return 1;
}
// If occurence isn't an int, then it is a named capture group
if( args->arg_count > 2 && args->arg_type[2] != INT_RESULT )
args->arg_type[2] != STRING_RESULT ;
// preg_capture can return NULL
initid->maybe_null=1;
// Default value of max_length should be sufficient
return ( pregInit( initid , args , message ) ) ;
}
/**
* @fn char *preg_capture(UDF_INIT *initid , UDF_ARGS *args, char *result,
* unsigned long *length, char *is_null , char *error )
*
* @brief
* The main routine for the PREG_CAPTURE udf.
*
* @param initid - various info supplied by mysql api - read more at
* http://dev.mysql.com/doc/refman/5.0/en/adding-udf.html
*
* @param args - array of information about arguments from the SQL call
* See file documentation for the description of the SQL arguments
*
* @param result - a place the captured string can be placed (255 len max)
* @param length - put the length of the captured item here.
* @param is_null - set this if return value is null
* @param error - to be set if an error occurs
*
* @return - string - if the numeric or named group can be captured
* @return - NULL - if no match or some other problem
*
* @details This function uses the pregSkipToOccurence function to call
* pcre_ex repeatedly until the requested occurence is found.
* It then uses pcre_get_substring (or copy) to retrieve any
* capture groups from the pattern. It then returns the appropriate
* string or NULL.
*/
char *preg_capture(UDF_INIT *initid , UDF_ARGS *args, char *result,
unsigned long *length, char *is_null , char *error )
{
char *ex_subject ; /* part subject starting after occurence*/
int groupnum ; /* numeric group - found or from args */
int l ; /* length of captured info */
char msg[255] ; /* to store errors from regex compile */
int occurence ; /* occurence of the pattern to capture from */
int oveccount ; /* number of items captures */
int *ovector; /* for offsets of captures */
struct preg_s *ptr ; /* local holder of initid->ptr */
int rc ; /* number of regex's matched by pattern */
pcre *re ; /* the compiled pattern */
const char *res2 ; /* for pcre_get_substring to alloc */
char *subject ; /* args[1] */
ptr = (struct preg_s *) initid->ptr ;
*is_null = 1 ; /* default to NULL return */
*error = 0 ; /* default to no error */
*ptr->return_buffer = '\0'; /* clear return value */
*length = 0 ; /* just to be safe */
#ifndef GH_1_0_NULL_HANDLING
if( ghargIsNullConstant( args , 0 ) || ghargIsNullConstant( args , 1 )
|| ghargIsNullConstant( args , 2 ) )
{
//fprintf( stderr , "null subject. Returning NULL\n" ) ;
*is_null = 1 ;
return NULL ;
}
#endif
// compile the regex if necessary
if( ptr->constant_pattern )
re = ptr->re ;
else
{
re = pregCompileRegexArg( args , msg , sizeof(msg)) ;
if( !re )
{
ghlogprintf( "PREG_CAPTURE: compile failed: %s\n", msg );
*error = 1 ;
return NULL ;
}
}
// create vector to hold offsets for pcre
ovector = pregCreateOffsetsVector( re,NULL, &oveccount ,msg,sizeof(msg)) ;
if( !ovector )
{
ghlogprintf( "PREG_CAPTURE: can't create offset vector :%s\n", msg );
*error = 1 ;
if( !ptr->constant_pattern )
pcre_free( re ) ;
return NULL ;
}
if( args->arg_count > 3 )
occurence = (int)(*(longlong *)args->args[3]) ;
else
occurence = 1 ;
subject = ghargdup( args , 1 ) ;
if( subject )
{
ex_subject = pregSkipToOccurence( re , subject , args->lengths[1] ,
ovector , oveccount , occurence,&rc);
groupnum = -1 ;
if( rc > 0 )
groupnum = pregGetGroupNum( re , args , 2 ) ;
// If groupnum found, get the substring and prepare for return
if( groupnum >= 0 && groupnum < (oveccount/3) )
{
l = pcre_get_substring( ex_subject, ovector,rc,groupnum, &res2 ) ;
result = pregMoveToReturnValues( initid,length,is_null , error,
(char *)res2 , l );
}
free( subject ) ;
}
free( ovector ) ;
if( !ptr->constant_pattern )
pcre_free( re ) ;
return result ;
}
/**
* @fn void preg_capture_deinit(UDF_INIT *initid)
*
* @brief cleanup after PREG_CAPTURE
*
* @param initid - pointer to struct to be cleaned.
*/
void preg_capture_deinit(UDF_INIT *initid)
{
pregDeInit(initid);
}