1 /*
2 * sql.c
3 */
4 #include <stdio.h>
5 #include <string.h>
6 #include "oniguruma.h"
7
main(int argc,char * argv[])8 extern int main(int argc, char* argv[])
9 {
10 static OnigSyntaxType SQLSyntax;
11
12 int r;
13 unsigned char *start, *range, *end;
14 regex_t* reg;
15 OnigErrorInfo einfo;
16 OnigRegion *region;
17 OnigEncoding use_encs[1];
18
19 static UChar* pattern = (UChar* )"\\_%\\\\__zz";
20 static UChar* str = (UChar* )"a_abcabcabc\\ppzz";
21
22 use_encs[0] = ONIG_ENCODING_ASCII;
23 onig_initialize(use_encs, sizeof(use_encs)/sizeof(use_encs[0]));
24
25 onig_set_syntax_op (&SQLSyntax, ONIG_SYN_OP_VARIABLE_META_CHARACTERS);
26 onig_set_syntax_op2 (&SQLSyntax, 0);
27 onig_set_syntax_behavior(&SQLSyntax, 0);
28 onig_set_syntax_options (&SQLSyntax, ONIG_OPTION_MULTILINE);
29 onig_set_meta_char(&SQLSyntax, ONIG_META_CHAR_ESCAPE, (OnigCodePoint )'\\');
30 onig_set_meta_char(&SQLSyntax, ONIG_META_CHAR_ANYCHAR, (OnigCodePoint )'_');
31 onig_set_meta_char(&SQLSyntax, ONIG_META_CHAR_ANYTIME,
32 ONIG_INEFFECTIVE_META_CHAR);
33 onig_set_meta_char(&SQLSyntax, ONIG_META_CHAR_ZERO_OR_ONE_TIME,
34 ONIG_INEFFECTIVE_META_CHAR);
35 onig_set_meta_char(&SQLSyntax, ONIG_META_CHAR_ONE_OR_MORE_TIME,
36 ONIG_INEFFECTIVE_META_CHAR);
37 onig_set_meta_char(&SQLSyntax, ONIG_META_CHAR_ANYCHAR_ANYTIME,
38 (OnigCodePoint )'%');
39
40 r = onig_new(®, pattern, pattern + strlen((char* )pattern),
41 ONIG_OPTION_DEFAULT, ONIG_ENCODING_ASCII, &SQLSyntax, &einfo);
42 if (r != ONIG_NORMAL) {
43 char s[ONIG_MAX_ERROR_MESSAGE_LEN];
44 onig_error_code_to_str((UChar* )s, r, &einfo);
45 fprintf(stderr, "ERROR: %s\n", s);
46 onig_end();
47 return -1;
48 }
49
50 region = onig_region_new();
51
52 end = str + strlen((char* )str);
53 start = str;
54 range = end;
55 r = onig_search(reg, str, end, start, range, region, ONIG_OPTION_NONE);
56 if (r >= 0) {
57 int i;
58
59 fprintf(stderr, "match at %d\n", r);
60 for (i = 0; i < region->num_regs; i++) {
61 fprintf(stderr, "%d: (%d-%d)\n", i, region->beg[i], region->end[i]);
62 }
63 }
64 else if (r == ONIG_MISMATCH) {
65 fprintf(stderr, "search fail\n");
66 }
67 else { /* error */
68 char s[ONIG_MAX_ERROR_MESSAGE_LEN];
69 onig_error_code_to_str((UChar* )s, r);
70 fprintf(stderr, "ERROR: %s\n", s);
71 onig_region_free(region, 1 /* 1:free self, 0:free contents only */);
72 onig_free(reg);
73 onig_end();
74 return -1;
75 }
76
77 onig_region_free(region, 1 /* 1:free self, 0:free contents only */);
78 onig_free(reg);
79 onig_end();
80 return 0;
81 }
82