Sync usage with man page.
[netbsd-mini2440.git] / external / gpl2 / lvm2 / dist / libdm / regex / parse_rx.c
blobadb12379b037d8753bc3a9fadafafd87f27b4614
1 /* $NetBSD$ */
3 /*
4 * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
5 * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.
7 * This file is part of the device-mapper userspace tools.
9 * This copyrighted material is made available to anyone wishing to use,
10 * modify, copy, or redistribute it subject to the terms and conditions
11 * of the GNU Lesser General Public License v.2.1.
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program; if not, write to the Free Software Foundation,
15 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 #include "dmlib.h"
19 #include "parse_rx.h"
21 struct parse_sp { /* scratch pad for the parsing process */
22 struct dm_pool *mem;
23 int type; /* token type, 0 indicates a charset */
24 dm_bitset_t charset; /* The current charset */
25 const char *cursor; /* where we are in the regex */
26 const char *rx_end; /* 1pte for the expression being parsed */
29 static struct rx_node *_or_term(struct parse_sp *ps);
31 static void _single_char(struct parse_sp *ps, unsigned int c, const char *ptr)
33 ps->type = 0;
34 ps->cursor = ptr + 1;
35 dm_bit_clear_all(ps->charset);
36 dm_bit_set(ps->charset, c);
40 * Get the next token from the regular expression.
41 * Returns: 1 success, 0 end of input, -1 error.
43 static int _rx_get_token(struct parse_sp *ps)
45 int neg = 0, range = 0;
46 char c, lc = 0;
47 const char *ptr = ps->cursor;
48 if (ptr == ps->rx_end) { /* end of input ? */
49 ps->type = -1;
50 return 0;
53 switch (*ptr) {
54 /* charsets and ncharsets */
55 case '[':
56 ptr++;
57 if (*ptr == '^') {
58 dm_bit_set_all(ps->charset);
60 /* never transition on zero */
61 dm_bit_clear(ps->charset, 0);
62 neg = 1;
63 ptr++;
65 } else
66 dm_bit_clear_all(ps->charset);
68 while ((ptr < ps->rx_end) && (*ptr != ']')) {
69 if (*ptr == '\\') {
70 /* an escaped character */
71 ptr++;
72 switch (*ptr) {
73 case 'n':
74 c = '\n';
75 break;
76 case 'r':
77 c = '\r';
78 break;
79 case 't':
80 c = '\t';
81 break;
82 default:
83 c = *ptr;
85 } else if (*ptr == '-' && lc) {
86 /* we've got a range on our hands */
87 range = 1;
88 ptr++;
89 if (ptr == ps->rx_end) {
90 log_error("Incomplete range"
91 "specification");
92 return -1;
94 c = *ptr;
95 } else
96 c = *ptr;
98 if (range) {
99 /* add lc - c into the bitset */
100 if (lc > c) {
101 char tmp = c;
102 c = lc;
103 lc = tmp;
106 for (; lc <= c; lc++) {
107 if (neg)
108 dm_bit_clear(ps->charset, lc);
109 else
110 dm_bit_set(ps->charset, lc);
112 range = 0;
113 } else {
114 /* add c into the bitset */
115 if (neg)
116 dm_bit_clear(ps->charset, c);
117 else
118 dm_bit_set(ps->charset, c);
120 ptr++;
121 lc = c;
124 if (ptr >= ps->rx_end) {
125 ps->type = -1;
126 return -1;
129 ps->type = 0;
130 ps->cursor = ptr + 1;
131 break;
133 /* These characters are special, we just return their ASCII
134 codes as the type. Sorted into ascending order to help the
135 compiler */
136 case '(':
137 case ')':
138 case '*':
139 case '+':
140 case '?':
141 case '|':
142 ps->type = (int) *ptr;
143 ps->cursor = ptr + 1;
144 break;
146 case '^':
147 _single_char(ps, HAT_CHAR, ptr);
148 break;
150 case '$':
151 _single_char(ps, DOLLAR_CHAR, ptr);
152 break;
154 case '.':
155 /* The 'all but newline' character set */
156 ps->type = 0;
157 ps->cursor = ptr + 1;
158 dm_bit_set_all(ps->charset);
159 dm_bit_clear(ps->charset, (int) '\n');
160 dm_bit_clear(ps->charset, (int) '\r');
161 dm_bit_clear(ps->charset, 0);
162 break;
164 case '\\':
165 /* escaped character */
166 ptr++;
167 if (ptr >= ps->rx_end) {
168 log_error("Badly quoted character at end "
169 "of expression");
170 ps->type = -1;
171 return -1;
174 ps->type = 0;
175 ps->cursor = ptr + 1;
176 dm_bit_clear_all(ps->charset);
177 switch (*ptr) {
178 case 'n':
179 dm_bit_set(ps->charset, (int) '\n');
180 break;
181 case 'r':
182 dm_bit_set(ps->charset, (int) '\r');
183 break;
184 case 't':
185 dm_bit_set(ps->charset, (int) '\t');
186 break;
187 default:
188 dm_bit_set(ps->charset, (int) *ptr);
190 break;
192 default:
193 /* add a single character to the bitset */
194 ps->type = 0;
195 ps->cursor = ptr + 1;
196 dm_bit_clear_all(ps->charset);
197 dm_bit_set(ps->charset, (int) *ptr);
198 break;
201 return 1;
204 static struct rx_node *_node(struct dm_pool *mem, int type,
205 struct rx_node *l, struct rx_node *r)
207 struct rx_node *n = dm_pool_zalloc(mem, sizeof(*n));
209 if (n) {
210 if (!(n->charset = dm_bitset_create(mem, 256))) {
211 dm_pool_free(mem, n);
212 return NULL;
215 n->type = type;
216 n->left = l;
217 n->right = r;
220 return n;
223 static struct rx_node *_term(struct parse_sp *ps)
225 struct rx_node *n;
227 switch (ps->type) {
228 case 0:
229 if (!(n = _node(ps->mem, CHARSET, NULL, NULL))) {
230 stack;
231 return NULL;
234 dm_bit_copy(n->charset, ps->charset);
235 _rx_get_token(ps); /* match charset */
236 break;
238 case '(':
239 _rx_get_token(ps); /* match '(' */
240 n = _or_term(ps);
241 if (ps->type != ')') {
242 log_error("missing ')' in regular expression");
243 return 0;
245 _rx_get_token(ps); /* match ')' */
246 break;
248 default:
249 n = 0;
252 return n;
255 static struct rx_node *_closure_term(struct parse_sp *ps)
257 struct rx_node *l, *n;
259 if (!(l = _term(ps)))
260 return NULL;
262 for (;;) {
263 switch (ps->type) {
264 case '*':
265 n = _node(ps->mem, STAR, l, NULL);
266 break;
268 case '+':
269 n = _node(ps->mem, PLUS, l, NULL);
270 break;
272 case '?':
273 n = _node(ps->mem, QUEST, l, NULL);
274 break;
276 default:
277 return l;
280 if (!n) {
281 stack;
282 return NULL;
285 _rx_get_token(ps);
286 l = n;
289 return n;
292 static struct rx_node *_cat_term(struct parse_sp *ps)
294 struct rx_node *l, *r, *n;
296 if (!(l = _closure_term(ps)))
297 return NULL;
299 if (ps->type == '|')
300 return l;
302 if (!(r = _cat_term(ps)))
303 return l;
305 if (!(n = _node(ps->mem, CAT, l, r)))
306 stack;
308 return n;
311 static struct rx_node *_or_term(struct parse_sp *ps)
313 struct rx_node *l, *r, *n;
315 if (!(l = _cat_term(ps)))
316 return NULL;
318 if (ps->type != '|')
319 return l;
321 _rx_get_token(ps); /* match '|' */
323 if (!(r = _or_term(ps))) {
324 log_error("Badly formed 'or' expression");
325 return NULL;
328 if (!(n = _node(ps->mem, OR, l, r)))
329 stack;
331 return n;
334 struct rx_node *rx_parse_tok(struct dm_pool *mem,
335 const char *begin, const char *end)
337 struct rx_node *r;
338 struct parse_sp *ps = dm_pool_zalloc(mem, sizeof(*ps));
340 if (!ps) {
341 stack;
342 return NULL;
345 ps->mem = mem;
346 ps->charset = dm_bitset_create(mem, 256);
347 ps->cursor = begin;
348 ps->rx_end = end;
349 _rx_get_token(ps); /* load the first token */
351 if (!(r = _or_term(ps))) {
352 log_error("Parse error in regex");
353 dm_pool_free(mem, ps);
356 return r;
359 struct rx_node *rx_parse_str(struct dm_pool *mem, const char *str)
361 return rx_parse_tok(mem, str, str + strlen(str));