blob: 4b41a4b276f8297358c44d1d512584628c304a29 [file] [log] [blame]
Wolfgang Denk2aceea12013-03-23 23:50:31 +00001/*
2 * Copyright (c) 2004-2005 Sergey Lyubka <valenok@gmail.com>
3 * All rights reserved
4 *
5 * "THE BEER-WARE LICENSE" (Revision 42):
6 * Sergey Lyubka wrote this file. As long as you retain this notice you
7 * can do whatever you want with this stuff. If we meet some day, and you think
8 * this stuff is worth it, you can buy me a beer in return.
9 */
10
11/*
12 * Downloaded Sat Nov 5 17:42:08 CET 2011 at
13 * http://slre.sourceforge.net/1.0/slre.h
14 */
15
16/*
17 * This is a regular expression library that implements a subset of Perl RE.
18 * Please refer to http://slre.sourceforge.net for detailed description.
19 *
20 * Usage example (parsing HTTP request):
21 *
22 * struct slre slre;
23 * struct cap captures[4 + 1]; // Number of braket pairs + 1
24 * ...
25 *
26 * slre_compile(&slre,"^(GET|POST) (\S+) HTTP/(\S+?)\r\n");
27 *
28 * if (slre_match(&slre, buf, len, captures)) {
29 * printf("Request line length: %d\n", captures[0].len);
30 * printf("Method: %.*s\n", captures[1].len, captures[1].ptr);
31 * printf("URI: %.*s\n", captures[2].len, captures[2].ptr);
32 * }
33 *
34 * Supported syntax:
35 * ^ Match beginning of a buffer
36 * $ Match end of a buffer
37 * () Grouping and substring capturing
38 * [...] Match any character from set
39 * [^...] Match any character but ones from set
40 * \s Match whitespace
41 * \S Match non-whitespace
42 * \d Match decimal digit
43 * \r Match carriage return
44 * \n Match newline
45 * + Match one or more times (greedy)
46 * +? Match one or more times (non-greedy)
47 * * Match zero or more times (greedy)
48 * *? Match zero or more times (non-greedy)
49 * ? Match zero or once
50 * \xDD Match byte with hex value 0xDD
51 * \meta Match one of the meta character: ^$().[*+?\
52 */
53
54#ifndef SLRE_HEADER_DEFINED
55#define SLRE_HEADER_DEFINED
56
57/*
58 * Compiled regular expression
59 */
60struct slre {
61 unsigned char code[256];
62 unsigned char data[256];
63 int code_size;
64 int data_size;
65 int num_caps; /* Number of bracket pairs */
66 int anchored; /* Must match from string start */
67 const char *err_str; /* Error string */
68};
69
70/*
71 * Captured substring
72 */
73struct cap {
74 const char *ptr; /* Pointer to the substring */
75 int len; /* Substring length */
76};
77
78/*
79 * Compile regular expression. If success, 1 is returned.
80 * If error, 0 is returned and slre.err_str points to the error message.
81 */
82int slre_compile(struct slre *, const char *re);
83
84/*
85 * Return 1 if match, 0 if no match.
86 * If `captured_substrings' array is not NULL, then it is filled with the
87 * values of captured substrings. captured_substrings[0] element is always
88 * a full matched substring. The round bracket captures start from
89 * captured_substrings[1].
90 * It is assumed that the size of captured_substrings array is enough to
91 * hold all captures. The caller function must make sure it is! So, the
92 * array_size = number_of_round_bracket_pairs + 1
93 */
94int slre_match(const struct slre *, const char *buf, int buf_len,
95 struct cap *captured_substrings);
96
97#ifdef SLRE_TEST
98void slre_dump(const struct slre *r, FILE *fp);
99#endif /* SLRE_TEST */
100#endif /* SLRE_HEADER_DEFINED */