Added silc_subst, regex matching and substitution with the
[silc.git] / lib / silcutil / silcregex.h
1 /*
2
3   silcregex.h
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 2007 - 2008 Pekka Riikonen
8
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.
12
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.
17
18 */
19
20 /****h* silcutil/SILC Regular Expression Interface
21  *
22  * DESCRIPTION
23  *
24  * SILC regular expression interface provides Unix and POSIX compliant
25  * regular expression compilation and matching.
26  *
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).
32  *
33  * The regex syntax follows POSIX regex syntax:
34  *
35  * Expressions:
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
63  *
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
77  *
78  * EXAMPLE
79  *
80  * SilcRegexStruct reg;
81  *
82  * // Compile regular expression
83  * if (!silc_regex_compile(&reg, "foo[0-9]*", 0))
84  *   error;
85  *
86  * // Match string against the compiled regex
87  * if (!silc_regex_match(&reg, "foo20", 0, NULL, 0))
88  *   no_match;
89  *
90  * // Free the compiled regular expression
91  * silc_regex_free(&reg);
92  *
93  ***/
94
95 #ifndef SILCREGEX_H
96 #define SILCREGEX_H
97
98 /****s* silcutil/SilcRegexAPI/SilcRegex
99  *
100  * NAME
101  *
102  *    typedef struct { ... } *SilcRegex, SilcRegexStruct;
103  *
104  * DESCRIPTION
105  *
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.
109  *
110  ***/
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;
124
125 /****s* silcutil/SilcRegexAPI/SilcRegexMatch
126  *
127  * NAME
128  *
129  *    typedef struct { ... } *SilcRegexMatch, SilcRegexMatchStruct;
130  *
131  * DESCRIPTION
132  *
133  *    The regular expression match context that provides information on the
134  *    found match.  It provides the start offset and end offset of the
135  *    found match.
136  *
137  * SOURCE
138  */
139 typedef struct SilcRegexMatchObject {
140   int start;                   /* Start offset of region */
141   int end;                     /* End offset of region */
142 } *SilcRegexMatch, SilcRegexMatchStruct;
143 /***/
144
145 /****d* silcutil/SilcRegexAPI/SilcRegexFlags
146  *
147  * NAME
148  *
149  *    typedef enum { ... } SilcRegexFlags;
150  *
151  * DESCRIPTION
152  *
153  *    Regular expression feature flags.
154  *
155  * SOURCE
156  */
157 typedef enum {
158   SILC_REGEX_DEFAULT   = 0x00000000,
159
160   /* The following flags can be used with silc_regex_match */
161
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
164      of line. */
165   SILC_REGEX_NOTBOL    = 0x00010000,
166
167   /* The end-of-line ($) always fails to match. */
168   SILC_REGEX_NOTEOL    = 0x00020000,
169 } SilcRegexFlags;
170 /***/
171
172 /****f* silcutil/SilcRegexAPI/silc_regex_compile
173  *
174  * SYNOPSIS
175  *
176  *    SilcBool silc_regex_compile(SilcRegex regexp, const char *regex,
177  *                                SilcRegexFlags flags);
178  *
179  * DESCRIPTION
180  *
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.
185  *
186  *    Returns TRUE after the compilation is completed.  Returns FALSE on
187  *    error and sets silc_errno.
188  *
189  ***/
190 SilcBool silc_regex_compile(SilcRegex regexp, const char *regex,
191                             SilcRegexFlags flags);
192
193 /****f* silcutil/SilcRegexAPI/silc_regex_compile
194  *
195  * SYNOPSIS
196  *
197  *    SilcBool silc_regex_match(SilcRegex regexp, const char *string,
198  *                              SilcUInt32 string_len, SilcUInt32 num_match,
199  *                              SilcRegexMatch match, SilcRegexFlags flags);
200  *
201  * DESCRIPTION
202  *
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.
207  *
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.
215  *
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.
220  *
221  * EXAMPLE
222  *
223  *    // Find first match (check if string matches)
224  *    if (!silc_regex_match(&reg, "foo20", 5, 0, NULL, 0))
225  *      no_match;
226  *
227  *    // Find multiple matches, one by one
228  *    SilcRegexMatchStruct match;
229  *
230  *    while (silc_regex_match(&reg, string, len, 1, &match, 0)) {
231  *      match_string = silc_memdup(string + match.start,
232  *                                 match.end - match.start);
233  *      string += match.end;
234  *    }
235  *
236  *    // Parse URI into its components, available in the match[] array
237  *    SilcRegexStruct reg;
238  *    SilcRegexMatchStruct match[7];
239  *
240  *    silc_regex_compile(&reg, "^(([^:]+)://)?([^:/]+)(:([0-9]+))?(/.*)", 0);
241  *    silc_regex_match(&reg, "http://example.com/page.html", len, 7, match, 0);
242  *
243  ***/
244 SilcBool silc_regex_match(SilcRegex regexp, const char *string,
245                           SilcUInt32 string_len, SilcUInt32 num_match,
246                           SilcRegexMatch match, SilcRegexFlags flags);
247
248 /****f* silcutil/SilcRegexAPI/silc_regex_free
249  *
250  * SYNOPSIS
251  *
252  *    void silc_regex_free(SilcRegex regexp);
253  *
254  * DESCRIPTION
255  *
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.
259  *
260  ***/
261 void silc_regex_free(SilcRegex regexp);
262
263 /****f* silcutil/SilcRegexAPI/silc_regex
264  *
265  * SYNOPSIS
266  *
267  *    SilcBool silc_regex(const char *string, const char *regex,
268  *                        SilcBuffer match, ...);
269  *
270  * DESCRIPTION
271  *
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.
275  *
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.
283  *
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.
287  *
288  * EXAMPLE
289  *
290  *    // Simple match
291  *    if (!silc_regex("foobar", "foo.", NULL))
292  *      no_match;
293  *
294  *    // Get the pointer to the first match
295  *    if (!silc_regex("foobar", ".bar", &match, NULL))
296  *      no_match;
297  *
298  *    // Group match
299  *    SilcBufferStruct match, sub1, sub2;
300  *
301  *    if (!silc_regex("Hello World", "(H..).(o..)", &match, &sub1, &sub2, NULL))
302  *      no_match;
303  *
304  ***/
305 SilcBool silc_regex(const char *string, const char *regex,
306                     SilcBuffer match, ...);
307
308 /****f* silcutil/SilcRegexAPI/silc_regex_buffer
309  *
310  * SYNOPSIS
311  *
312  *    SilcBool silc_regex_buffer(SilcBuffer buffer, const char *regex,
313  *                               SilcBuffer match, ...);
314  *
315  * DESCRIPTION
316  *
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
320  *    same buffer.
321  *
322  ***/
323 SilcBool silc_regex_buffer(SilcBuffer buffer, const char *regex,
324                            SilcBuffer match, ...);
325
326 /****f* silcutil/SilcRegexAPI/silc_subst
327  *
328  * SYNOPSIS
329  *
330  *    SilcBool silc_subst(SilcBuffer buffer, const char *subst);
331  *
332  * DESCRIPTION
333  *
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:
338  *
339  *    's/REGEXP/REPLACEMENT/FLAGS'
340  *
341  *    Matches regular expression REGEXP in each line in the buffer and
342  *    substitutes the match with REPLACEMENT.
343  *
344  *    'ADDRs/REGEXP/REPLACEMENT/FLAGS'
345  *
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
348  *    REPLACEMENT.
349  *
350  *    The ADDR may be of following format:
351  *
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
355  *
356  *    The FLAGS may be of following format:
357  *
358  *    no FLAGS       Finds first match in the line and replaces that
359  *    g              Finds and replaces all matches in the line
360  *
361  *    An '!' may precede the 's'.  In that case the ADDR is not matched.
362  *
363  *    Returns TRUE if the match and replacement was done, FALSE in case
364  *    of error, and sets the silc_errno.
365  *
366  *    If you need to match and/or replace '/' characters, they must be
367  *    escaped with '\' (C-style escaping for '\' is '\\').
368  *
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.
372  *
373  ***/
374 SilcBool silc_subst(SilcBuffer buffer, const char *subst);
375
376 /* Backwards support */
377 #define silc_string_regex_match(regex, string) silc_regex(string, regex, NULL)
378
379 #endif /* SILCREGEX_H */