5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 2007 - 2008 Pekka Riikonen
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; version 2 of the License.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
20 /****h* silcutil/SILC Regular Expression Interface
24 * SILC regular expression interface provides Unix and POSIX compliant
25 * regular expression compilation and matching.
27 * The interface also provides many convenience functions to make the use
28 * of regular expressions easier. Especially the silc_regex allows very
29 * simple way to match strings against regular expressions and get the
30 * exact match or matches as a return. The silc_subst provides simple and
31 * familiar way to match and substitute strings (Sed syntax).
33 * The regex syntax follows POSIX regex syntax:
36 * ^ Match start of line/string
37 * '^a' matches 'ab' but not 'ba'
38 * $ Match end of line/string
39 * 'a$' matches 'ba' but not 'ab'
40 * . Match any single character (except new line (\n))
41 * '.a' matches 'ba' but not 'a'
42 * + Preceding item is matched one or more times
43 * 'a+b' matches 'aaab' but not 'b'
44 * * Preceding item is matched zero or more times
45 * 'a*b' matches 'ab', 'aab' and 'b'
46 * ? Preceding item is matched zero or one time
47 * 'ca?b' matches 'cb' and 'cab' but not 'caab'
48 * | Joins two expressions and matches either of them (OR)
49 * 'foo|bar' matches 'foo' or 'bar'
50 * {n} Preceding item is matched exactly n times (n can be 0-255)
51 * 'a{2}' matches 'aa' but not 'aaa'
52 * {n,} Preceding item is matched n or more times
53 * 'a{2,} matches 'aa' and 'aaaa' but not 'a'
54 * {n,m} Preceding item is matched at least n times and at most m times
55 * 'a{2,4}' matches 'aa', 'aaa' and 'aaaa' but not 'aaaaa'
56 * [ ] Match any single character in the character list inside [ ]
57 * '[0123]' matches only '0', '1', '2' or '3'
58 * [ - ] Match any single character in the specified range
59 * '[0-5]' matches digits 0-5.
60 * [^ ] Match any character not in the character list or range
61 * '[^09]]' matches any other character except '0' and '9'
62 * ( ) Subexpression, grouping
64 * Escaping (C-language style, '\' is written as '\\'):
65 * \\ Considers following character literal ('\\{' is '{')
66 * \\\\ Matches literal \
67 * \a Matches bell (BEL)
68 * \t Matches horizontal tab (HT)
69 * \n Matches new line (LF)
70 * \v Matches vertical tab (VT)
71 * \f Matches form feed (FF)
72 * \r Matches carriage ret (CR)
73 * \\< Match null string at the start of a word
74 * \\> Match null string at the end of a word
75 * \\b Match null string at the edge of a wrod
76 * \\B Match null string when not at the edge of a word
80 * SilcRegexStruct reg;
82 * // Compile regular expression
83 * if (!silc_regex_compile(®, "foo[0-9]*", 0))
86 * // Match string against the compiled regex
87 * if (!silc_regex_match(®, "foo20", 0, NULL, 0))
90 * // Free the compiled regular expression
91 * silc_regex_free(®);
98 /****s* silcutil/SilcRegexAPI/SilcRegex
102 * typedef struct { ... } *SilcRegex, SilcRegexStruct;
106 * The regular expression context. This context is given as argument
107 * to all silc_regex_* functions. It is usually statically allocated
108 * but can be dynamically allocated by silc_malloc.
111 typedef struct SilcRegexObject {
112 SilcStack rstack; /* Stack for fast allocations */
113 unsigned char *buffer; /* compiled pattern */
114 char *fastmap; /* fastmap[ch] is true if ch can start pattern */
115 char *translate; /* translation to apply during comp/match */
116 int allocated; /* allocated size of compiled pattern */
117 int used; /* actual length of compiled pattern */
118 int num_registers; /* number of registers used */
119 char fastmap_accurate; /* true if fastmap is valid */
120 char can_be_null; /* true if can match empty string */
121 char uses_registers; /* registers used and need to be initialized */
122 char anchor; /* anchor: 0=none 1=begline 2=begbuf */
123 } *SilcRegex, SilcRegexStruct;
125 /****s* silcutil/SilcRegexAPI/SilcRegexMatch
129 * typedef struct { ... } *SilcRegexMatch, SilcRegexMatchStruct;
133 * The regular expression match context that provides information on the
134 * found match. It provides the start offset and end offset of the
139 typedef struct SilcRegexMatchObject {
140 int start; /* Start offset of region */
141 int end; /* End offset of region */
142 } *SilcRegexMatch, SilcRegexMatchStruct;
145 /****d* silcutil/SilcRegexAPI/SilcRegexFlags
149 * typedef enum { ... } SilcRegexFlags;
153 * Regular expression feature flags.
158 SILC_REGEX_DEFAULT = 0x00000000,
160 /* The following flags can be used with silc_regex_match */
162 /* The beginning-of-line (^) always fails to match. This can be useful
163 when beginning of a string should not be interpreted as the beginning
165 SILC_REGEX_NOTBOL = 0x00010000,
167 /* The end-of-line ($) always fails to match. */
168 SILC_REGEX_NOTEOL = 0x00020000,
172 /****f* silcutil/SilcRegexAPI/silc_regex_compile
176 * SilcBool silc_regex_compile(SilcRegex regexp, const char *regex,
177 * SilcRegexFlags flags);
181 * Compiles the regular expression string `regex'. The `regexp' is a
182 * pre-allocated regular expression context. The `flags' define
183 * various feature flags. This function must be called before the
184 * silc_regex_match can be used to find matches.
186 * Returns TRUE after the compilation is completed. Returns FALSE on
187 * error and sets silc_errno.
190 SilcBool silc_regex_compile(SilcRegex regexp, const char *regex,
191 SilcRegexFlags flags);
193 /****f* silcutil/SilcRegexAPI/silc_regex_compile
197 * SilcBool silc_regex_match(SilcRegex regexp, const char *string,
198 * SilcUInt32 string_len, SilcUInt32 num_match,
199 * SilcRegexMatch match, SilcRegexFlags flags);
203 * Finds one or more matches from the `string' using the pre-compiled
204 * regular expression `regexp'. It must be compiled by calling the
205 * silc_regex_compile before calling this function. The `flags' defines
206 * various feature flags.
208 * If only one match is needed the `num_match' may be set to 0 and the
209 * `match' is set to NULL. If multiple matches (substrings) are needed the
210 * `num_match' defines the size of the `match' array, where each of the
211 * matches (with parenthesized regular expression) will be stored. The
212 * `match' provides information on where the match was found in `string',
213 * providing the start offset and end offset of the match. Unused entires
214 * in the array will have -1 as the offset values.
216 * Returns TRUE if the string matched the regular expression or FALSE
217 * if it did not match or error occurred. The silc_errno will indicate
218 * the error. The silc_errno is set to SILC_ERR_NOT_FOUND if the regular
219 * expression did not match.
223 * // Find first match (check if string matches)
224 * if (!silc_regex_match(®, "foo20", 5, 0, NULL, 0))
227 * // Find multiple matches, one by one
228 * SilcRegexMatchStruct match;
230 * while (silc_regex_match(®, string, len, 1, &match, 0)) {
231 * match_string = silc_memdup(string + match.start,
232 * match.end - match.start);
233 * string += match.end;
236 * // Parse URI into its components, available in the match[] array
237 * SilcRegexStruct reg;
238 * SilcRegexMatchStruct match[7];
240 * silc_regex_compile(®, "^(([^:]+)://)?([^:/]+)(:([0-9]+))?(/.*)", 0);
241 * silc_regex_match(®, "http://example.com/page.html", len, 7, match, 0);
244 SilcBool silc_regex_match(SilcRegex regexp, const char *string,
245 SilcUInt32 string_len, SilcUInt32 num_match,
246 SilcRegexMatch match, SilcRegexFlags flags);
248 /****f* silcutil/SilcRegexAPI/silc_regex_free
252 * void silc_regex_free(SilcRegex regexp);
256 * Free's the compiled regular expression context `regexp'. This must
257 * be called even if `regexp' is statically allocated. If the
258 * silc_regex_compile has been called this function must be called.
261 void silc_regex_free(SilcRegex regexp);
263 /****f* silcutil/SilcRegexAPI/silc_regex
267 * SilcBool silc_regex(const char *string, const char *regex,
268 * SilcBuffer match, ...);
272 * Matches the `string' to the regular expression `regex'. Returns TRUE
273 * if the `string' matches the regular expression or FALSE if it does not
274 * match. The silc_errno is also set to SILC_ERR_NOT_FOUND.
276 * The first (whole) match is returned to `match' buffer if it is non-NULL.
277 * The variable argument list are buffers where multiple matches are
278 * returned in case of group (parenthesized) regular expression. The caller
279 * needs to know how many pointers to provide in order to get all matches.
280 * If a particular group is optional, a buffer pointer still must be given
281 * as argument for it, however, if it did not match the returned buffer
282 * length is 0 and data pointer is NULL.
284 * If `match' is non-NULL the variable argument list must be ended with
285 * NULL. The data in the `match' and in any other buffer is from `string'
286 * and must not be freed by the caller.
291 * if (!silc_regex("foobar", "foo.", NULL))
294 * // Get the pointer to the first match
295 * if (!silc_regex("foobar", ".bar", &match, NULL))
299 * SilcBufferStruct match, sub1, sub2;
301 * if (!silc_regex("Hello World", "(H..).(o..)", &match, &sub1, &sub2, NULL))
305 SilcBool silc_regex(const char *string, const char *regex,
306 SilcBuffer match, ...);
308 /****f* silcutil/SilcRegexAPI/silc_regex_buffer
312 * SilcBool silc_regex_buffer(SilcBuffer buffer, const char *regex,
313 * SilcBuffer match, ...);
317 * Same as silc_regex but the string to match is in `buffer'. Returns
318 * TRUE if the string matches and FALSE if it doesn't. See examples and
319 * other information in silc_regex. The `buffer' and `match' may be the
323 SilcBool silc_regex_buffer(SilcBuffer buffer, const char *regex,
324 SilcBuffer match, ...);
326 /****f* silcutil/SilcRegexAPI/silc_subst
330 * SilcBool silc_subst(SilcBuffer buffer, const char *subst);
334 * Regular expression matching and substitution in `buffer' according
335 * to the substitution expression `subst'. This function provides
336 * Sed (Stream Editor) style substitution interface. The `subst' may
337 * be of following formats:
339 * 's/REGEXP/REPLACEMENT/FLAGS'
341 * Matches regular expression REGEXP in each line in the buffer and
342 * substitutes the match with REPLACEMENT.
344 * 'ADDRs/REGEXP/REPLACEMENT/FLAGS'
346 * Selects lines in the buffer matching the address ADDR and matches the
347 * regular expression REGEXP in the line and substitutes the match with
350 * The ADDR may be of following format:
352 * /REGEXP/ Matches only lines matching the regular expression
353 * NUMBER Matches only the specified line number (1-n)
354 * $ Matches only the last line
356 * The FLAGS may be of following format:
358 * no FLAGS Finds first match in the line and replaces that
359 * g Finds and replaces all matches in the line
361 * An '!' may precede the 's'. In that case the ADDR is not matched.
363 * Returns TRUE if the match and replacement was done, FALSE in case
364 * of error, and sets the silc_errno.
366 * If you need to match and/or replace '/' characters, they must be
367 * escaped with '\' (C-style escaping for '\' is '\\').
369 * If you need more versatile ways to modify the buffer you may consider
370 * using the SILC_STR_REGEX in SILC Buffer Format API directly. This
371 * function only provides basic matching and substitution.
375 * // Replace all foos with bar on all lines in the buffer
376 * silc_subst(buffer, "s/foo/bar/g");
379 SilcBool silc_subst(SilcBuffer buffer, const char *subst);
381 /* Backwards support */
382 #define silc_string_regex_match(regex, string) silc_regex(string, regex, NULL)
384 #endif /* SILCREGEX_H */