Kannel: Open Source WAP and SMS gateway  svn-r5335
gw-regex.h
Go to the documentation of this file.
1 /* ====================================================================
2  * The Kannel Software License, Version 1.0
3  *
4  * Copyright (c) 2001-2018 Kannel Group
5  * Copyright (c) 1998-2001 WapIT Ltd.
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  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Kannel Group (http://www.kannel.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Kannel" and "Kannel Group" must not be used to
28  * endorse or promote products derived from this software without
29  * prior written permission. For written permission, please
30  * contact org@kannel.org.
31  *
32  * 5. Products derived from this software may not be called "Kannel",
33  * nor may "Kannel" appear in their name, without prior written
34  * permission of the Kannel Group.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE KANNEL GROUP OR ITS CONTRIBUTORS
40  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
41  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
42  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
43  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
44  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
45  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
46  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of the Kannel Group. For more information on
51  * the Kannel Group, please see <http://www.kannel.org/>.
52  *
53  * Portions of this software are based upon software originally written at
54  * WapIT Ltd., Helsinki, Finland for the Kannel project.
55  */
56 
57 /*
58  * regex.h - POSIX regular expressions (REs)
59  *
60  * This modules implements wrapper functions to regcomp(3), regexec(3),
61  * et all functions from the POSIX compliance standard. Additinally
62  * it provides subexpression substitution routines in order to easily
63  * substitute strings arround regular expressions.
64  *
65  * See regex(3) man page for more details on POSIX regular expressions.
66  *
67  * PCRE allows wrapper functions for POSIX regex via an own API. So we
68  * use PCRE in favor, before falling back to POSIX regex.
69  *
70  * Stipe Tolj <stolj@kannel.org>
71  */
72 
73 #ifndef GW_REGEX_H
74 #define GW_REGEX_H
75 
76 #ifdef HAVE_PCRE
77 # include <pcreposix.h>
78 #elif HAVE_REGEX
79 # include <regex.h>
80 #endif
81 
82 #if defined(HAVE_REGEX) || defined(HAVE_PCRE)
83 
84 
85 /*
86  * We handle a maximum of 10 subexpression matches and
87  * substitution escape codes $0 to $9 in gw_regex_sub().
88  */
89 #define REGEX_MAX_SUB_MATCH 10
90 
91 
92 /*
93  * Destroy a previously compiled regular expression.
94  */
95 void gw_regex_destroy(regex_t *preg);
96 
97 
98 /*
99  * Compile a regular expression provided by pattern and return
100  * the regular expression type as function result.
101  * If the compilation fails, return NULL.
102  */
103 regex_t *gw_regex_comp_real(const Octstr *pattern, int cflags, const char *file,
104  long line, const char *func);
105 #define gw_regex_comp(pattern, cflags) \
106  gw_regex_comp_real(pattern, cflags, __FILE__, __LINE__, __func__)
107 
108 
109 /*
110  * Execute a previously compile regular expression on a given
111  * string and provide the matches via nmatch and pmatch[].
112  */
113 int gw_regex_exec_real(const regex_t *preg, const Octstr *string, size_t nmatch,
114  regmatch_t pmatch[], int eflags, const char *file, long line,
115  const char *func);
116 #define gw_regex_exec(preg, string, nmatch, pmatch, eflags) \
117  gw_regex_exec_real(preg, string, nmatch, pmatch, eflags, \
118  __FILE__, __LINE__, __func__)
119 
120 
121 /*
122  * Provide the error description string of an regex operation as
123  * Octstr instead of a char[].
124  */
125 Octstr *gw_regex_error(int errcode, const regex_t *preg);
126 
127 
128 /* This function substitutes for $0-$9, filling in regular expression
129  * submatches. Pass it the same nmatch and pmatch arguments that you
130  * passed gw_regexec(). pmatch should not be greater than the maximum number
131  * of subexpressions - i.e. one more than the re_nsub member of regex_t.
132  *
133  * input should be the string with the $-expressions, source should be the
134  * string that was matched against.
135  *
136  * It returns the substituted string, or NULL on error.
137  *
138  * Parts of this code are based on Henry Spencer's regsub(), from his
139  * AT&T V8 regexp package. Function borrowed from apache-1.3/src/main/util.c
140  */
141 char *gw_regex_sub(const char *input, const char *source,
142  size_t nmatch, regmatch_t pmatch[]);
143 
144 
145 /*
146  * Match directly a given regular expression and a source string. This assumes
147  * that the RE has not been pre-compiled and hence perform the compile and
148  * exec step in this matching step.
149  * Return 1 if the regular expression is successfully matching, 0 otherwise.
150  */
151 int gw_regex_match_real(const Octstr *re, const Octstr *os, const char *file,
152  long line, const char *func);
153 #define gw_regex_match(re, os) \
154  gw_regex_match_real(re, os, __FILE__, __LINE__, __func__)
155 
156 
157 /*
158  * Match directly a given source string against a previously pre-compiled
159  * regular expression.
160  * Return 1 if the regular expression is successfully matching, 0 otherwise.
161  */
162 int gw_regex_match_pre_real(const regex_t *preg, const Octstr *os, const char *file,
163  long line, const char *func);
164 #define gw_regex_match_pre(preg, os) \
165  gw_regex_match_pre_real(preg, os, __FILE__, __LINE__, __func__)
166 
167 
168 /*
169  * Match directly a given regular expression and a source string. RE has not
170  * been precompiled. Apply substitution rule accoding to Octstr 'rule' and
171  * return the substituted Ocstr as result. Return NULL if failed.
172  * Use \$0 up to \$9 as escape codes for subexpression matchings in the rule.
173  * Ie. os="+4914287756", re="^(00|\+)([0-9]{6,20})$" rule="\$2" would cause
174  * to return "4914287756" because the rule returns only the second regular
175  * expression atom that matched via the expression ([0-9]{6,20}).
176  */
177 Octstr *gw_regex_subst_real(const Octstr *re, const Octstr *os, const Octstr *rule,
178  const char *file, long line, const char *func);
179 #define gw_regex_subst(re, os, rule) \
180  gw_regex_subst_real(re, os, rule, __FILE__, __LINE__, __func__)
181 
182 /*
183  * Math directly a given source string against a previously pre-compiled
184  * regular expression. Apply substitution rule according to Ocstr 'rule' and
185  * return the substitued Octstr as result. Same as gw_regex_subst() but a
186  * pre-compiled RE is passed as first argument.
187  */
188 Octstr *gw_regex_subst_pre_real(const regex_t *preg, const Octstr *os, const Octstr *rule,
189  const char *file, long line, const char *func);
190 #define gw_regex_subst_pre(preg, os, rule) \
191  gw_regex_subst_pre_real(preg, os, rule, __FILE__, __LINE__, __func__)
192 
193 
194 #endif
195 #endif /* GW_REGEX_H */
196 
197 
FILE * file
Definition: log.c:169
Definition: octstr.c:118
See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.