2 * Copyright (c) 2004-2005 Sergey Lyubka <valenok@gmail.com>
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.
12 * Downloaded Sat Nov 5 17:43:06 CET 2011 at
13 * http://slre.sourceforge.net/1.0/slre.c
25 #include <linux/ctype.h>
26 #endif /* SLRE_TEST */
32 enum {END, BRANCH, ANY, EXACT, ANYOF, ANYBUT, OPEN, CLOSE, BOL, EOL,
33 STAR, PLUS, STARQ, PLUSQ, QUEST, SPACE, NONSPACE, DIGIT};
41 {"END", 0, ""}, /* End of code block or program */
42 {"BRANCH", 2, "oo"}, /* Alternative operator, "|" */
43 {"ANY", 0, ""}, /* Match any character, "." */
44 {"EXACT", 2, "d"}, /* Match exact string */
45 {"ANYOF", 2, "D"}, /* Match any from set, "[]" */
46 {"ANYBUT", 2, "D"}, /* Match any but from set, "[^]"*/
47 {"OPEN ", 1, "i"}, /* Capture start, "(" */
48 {"CLOSE", 1, "i"}, /* Capture end, ")" */
49 {"BOL", 0, ""}, /* Beginning of string, "^" */
50 {"EOL", 0, ""}, /* End of string, "$" */
51 {"STAR", 1, "o"}, /* Match zero or more times "*" */
52 {"PLUS", 1, "o"}, /* Match one or more times, "+" */
53 {"STARQ", 1, "o"}, /* Non-greedy STAR, "*?" */
54 {"PLUSQ", 1, "o"}, /* Non-greedy PLUS, "+?" */
55 {"QUEST", 1, "o"}, /* Match zero or one time, "?" */
56 {"SPACE", 0, ""}, /* Match whitespace, "\s" */
57 {"NONSPACE", 0, ""}, /* Match non-space, "\S" */
58 {"DIGIT", 0, ""} /* Match digit, "\d" */
60 #endif /* SLRE_TEST */
63 * Commands and operands are all unsigned char (1 byte long). All code offsets
64 * are relative to current address, and positive (always point forward). Data
65 * offsets are absolute. Commands with operands:
67 * BRANCH offset1 offset2
68 * Try to match the code block that follows the BRANCH instruction
69 * (code block ends with END). If no match, try to match code block that
70 * starts at offset1. If either of these match, jump to offset2.
72 * EXACT data_offset data_length
73 * Try to match exact string. String is recorded in data section from
74 * data_offset, and has length data_length.
77 * CLOSE capture_number
78 * If the user have passed 'struct cap' array for captures, OPEN
79 * records the beginning of the matched substring (cap->ptr), CLOSE
80 * sets the length (cap->len) for respective capture_number.
85 * *, +, ?, respectively. Try to gobble as much as possible from the
86 * matched buffer, until code block that follows these instructions
87 * matches. When the longest possible string is matched,
90 * STARQ, PLUSQ are non-greedy versions of STAR and PLUS.
93 static const char *meta_chars = "|.^$*+?()[\\";
98 print_character_set(FILE *fp, const unsigned char *p, int len)
102 for (i = 0; i < len; i++) {
104 (void) fputc(',', fp);
108 (void) fprintf(fp, "\\x%02x", p[i]);
110 (void) fprintf(fp, "%s", opcodes[p[i]].name);
111 } else if (isprint(p[i])) {
112 (void) fputc(p[i], fp);
114 (void) fprintf(fp, "\\x%02x", p[i]);
120 slre_dump(const struct slre *r, FILE *fp)
122 int i, j, ch, op, pc;
124 for (pc = 0; pc < r->code_size; pc++) {
127 (void) fprintf(fp, "%3d %s ", pc, opcodes[op].name);
129 for (i = 0; opcodes[op].flags[i] != '\0'; i++)
130 switch (opcodes[op].flags[i]) {
132 (void) fprintf(fp, "%d ", r->code[pc + 1]);
136 (void) fprintf(fp, "%d ",
137 pc + r->code[pc + 1] - i);
141 print_character_set(fp, r->data +
142 r->code[pc + 1], r->code[pc + 2]);
146 (void) fputc('"', fp);
147 for (j = 0; j < r->code[pc + 2]; j++) {
148 ch = r->data[r->code[pc + 1] + j];
150 (void) fputc(ch, fp);
156 (void) fputc('"', fp);
161 (void) fputc('\n', fp);
164 #endif /* SLRE_TEST */
167 set_jump_offset(struct slre *r, int pc, int offset)
169 assert(offset < r->code_size);
171 if (r->code_size - offset > 0xff)
172 r->err_str = "Jump offset is too big";
174 r->code[pc] = (unsigned char) (r->code_size - offset);
178 emit(struct slre *r, int code)
180 if (r->code_size >= (int) (sizeof(r->code) / sizeof(r->code[0])))
181 r->err_str = "RE is too long (code overflow)";
183 r->code[r->code_size++] = (unsigned char) code;
187 store_char_in_data(struct slre *r, int ch)
189 if (r->data_size >= (int) sizeof(r->data))
190 r->err_str = "RE is too long (data overflow)";
192 r->data[r->data_size++] = ch;
196 exact(struct slre *r, const char **re)
198 int old_data_size = r->data_size;
200 while (**re != '\0' && (strchr(meta_chars, **re)) == NULL)
201 store_char_in_data(r, *(*re)++);
204 emit(r, old_data_size);
205 emit(r, r->data_size - old_data_size);
209 get_escape_char(const char **re)
244 anyof(struct slre *r, const char **re)
246 int esc, old_data_size = r->data_size, op = ANYOF;
258 emit(r, old_data_size);
259 emit(r, r->data_size - old_data_size);
264 esc = get_escape_char(re);
265 if ((esc & 0xff) == 0) {
266 store_char_in_data(r, 0);
267 store_char_in_data(r, esc >> 8);
269 store_char_in_data(r, esc);
273 store_char_in_data(r, (*re)[-1]);
277 r->err_str = "No closing ']' bracket";
281 relocate(struct slre *r, int begin, int shift)
284 memmove(r->code + begin + shift, r->code + begin, r->code_size - begin);
285 r->code_size += shift;
289 quantifier(struct slre *r, int prev, int op)
291 if (r->code[prev] == EXACT && r->code[prev + 2] > 1) {
294 emit(r, r->code[prev + 1] + r->code[prev + 2]);
296 prev = r->code_size - 3;
298 relocate(r, prev, 2);
300 set_jump_offset(r, prev + 1, prev);
304 exact_one_char(struct slre *r, int ch)
307 emit(r, r->data_size);
309 store_char_in_data(r, ch);
313 fixup_branch(struct slre *r, int fixup)
317 set_jump_offset(r, fixup, fixup - 2);
322 compile(struct slre *r, const char **re)
324 int op, esc, branch_start, last_op, fixup, cap_no, level;
328 branch_start = last_op = r->code_size;
344 last_op = r->code_size;
348 last_op = r->code_size;
352 last_op = r->code_size;
353 esc = get_escape_char(re);
357 exact_one_char(r, esc);
360 last_op = r->code_size;
361 cap_no = ++r->num_caps;
366 if (*(*re)++ != ')') {
367 r->err_str = "No closing bracket";
376 fixup_branch(r, fixup);
378 r->err_str = "Unbalanced brackets";
386 op = (*re)[-1] == '*' ? STAR : PLUS;
389 op = op == STAR ? STARQ : PLUSQ;
391 quantifier(r, last_op, op);
394 quantifier(r, last_op, QUEST);
397 fixup_branch(r, fixup);
398 relocate(r, branch_start, 3);
399 r->code[branch_start] = BRANCH;
400 set_jump_offset(r, branch_start + 1, branch_start);
401 fixup = branch_start + 2;
402 r->code[fixup] = 0xff;
406 last_op = r->code_size;
413 slre_compile(struct slre *r, const char *re)
416 r->code_size = r->data_size = r->num_caps = r->anchored = 0;
421 emit(r, OPEN); /* This will capture what matches full RE */
427 if (r->code[2] == BRANCH)
434 return (r->err_str == NULL ? 1 : 0);
437 static int match(const struct slre *, int,
438 const char *, int, int *, struct cap *);
441 loop_greedy(const struct slre *r, int pc, const char *s, int len, int *ofs)
443 int saved_offset, matched_offset;
445 matched_offset = *ofs;
447 while (match(r, pc + 2, s, len, ofs, NULL)) {
449 if (match(r, pc + r->code[pc + 1], s, len, ofs, NULL))
450 matched_offset = saved_offset;
454 *ofs = matched_offset;
458 loop_non_greedy(const struct slre *r, int pc, const char *s, int len, int *ofs)
460 int saved_offset = *ofs;
462 while (match(r, pc + 2, s, len, ofs, NULL)) {
464 if (match(r, pc + r->code[pc + 1], s, len, ofs, NULL))
472 is_any_of(const unsigned char *p, int len, const char *s, int *ofs)
478 for (i = 0; i < len; i++)
488 is_any_but(const unsigned char *p, int len, const char *s, int *ofs)
494 for (i = 0; i < len; i++) {
504 match(const struct slre *r, int pc, const char *s, int len,
505 int *ofs, struct cap *caps)
507 int n, saved_offset, res = 1;
509 while (res && r->code[pc] != END) {
511 assert(pc < r->code_size);
512 assert(pc < (int) (sizeof(r->code) / sizeof(r->code[0])));
514 switch (r->code[pc]) {
517 res = match(r, pc + 3, s, len, ofs, caps);
520 res = match(r, pc + r->code[pc + 1],
523 pc += r->code[pc + 2];
527 n = r->code[pc + 2]; /* String length */
528 if (n <= len - *ofs && !memcmp(s + *ofs, r->data +
529 r->code[pc + 1], n)) {
538 if (!match(r, pc + 2, s, len, ofs, caps))
540 pc += r->code[pc + 1];
544 loop_greedy(r, pc, s, len, ofs);
545 pc += r->code[pc + 1];
549 loop_non_greedy(r, pc, s, len, ofs);
550 pc += r->code[pc + 1];
553 res = match(r, pc + 2, s, len, ofs, caps);
557 loop_greedy(r, pc, s, len, ofs);
558 pc += r->code[pc + 1];
561 res = match(r, pc + 2, s, len, ofs, caps);
565 loop_non_greedy(r, pc, s, len, ofs);
566 pc += r->code[pc + 1];
570 if (*ofs < len && isspace(((unsigned char *)s)[*ofs])) {
579 !isspace(((unsigned char *)s)[*ofs])) {
587 if (*ofs < len && isdigit(((unsigned char *)s)[*ofs])) {
604 res = is_any_of(r->data + r->code[pc + 1],
605 r->code[pc + 2], s, ofs);
611 res = is_any_but(r->data + r->code[pc + 1],
612 r->code[pc + 2], s, ofs);
616 res = *ofs == 0 ? 1 : 0;
620 res = *ofs == len ? 1 : 0;
625 caps[r->code[pc + 1]].ptr = s + *ofs;
630 caps[r->code[pc + 1]].len = (s + *ofs) -
631 caps[r->code[pc + 1]].ptr;
638 printf("unknown cmd (%d) at %d\n", r->code[pc], pc);
648 slre_match(const struct slre *r, const char *buf, int len,
651 int i, ofs = 0, res = 0;
654 res = match(r, 0, buf, len, &ofs, caps);
656 for (i = 0; i < len && res == 0; i++) {
658 res = match(r, 0, buf, len, &ofs, caps);
668 int main(int argc, char *argv[])
671 struct cap caps[N_CAPS];
672 unsigned char data[1 * 1024 * 1024];
677 fprintf(stderr, "Usage: %s 'slre' <file>\n", argv[0]);
681 fp = fopen(argv[2], "rb");
683 fprintf(stderr, "Error: cannot open %s:%s\n",
684 argv[2], strerror(errno));
688 if (!slre_compile(&slre, argv[1])) {
689 fprintf(stderr, "Error compiling slre: %s\n", slre.err_str);
693 slre_dump(&slre, stderr);
695 while (fgets(data, sizeof(data), fp) != NULL) {
698 if ((len > 0) && (data[len-1] == '\n')) {
703 printf("Data = \"%s\"\n", data);
705 (void) memset(caps, 0, sizeof(caps));
707 res = slre_match(&slre, data, len, caps);
708 printf("Result [%d]: %d\n", i, res);
710 for (i = 0; i < N_CAPS; i++) {
711 if (caps[i].len > 0) {
712 printf("Substring %d: len=%d [%.*s]\n", i,
714 caps[i].len, caps[i].ptr);
717 printf("----------------------------------------------------\n");
723 #endif /* SLRE_TEST */