-
Notifications
You must be signed in to change notification settings - Fork 3
/
echo.c
136 lines (112 loc) · 3.18 KB
/
echo.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
* echo.c
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "oniguruma.h"
static int
echo(OnigCalloutArgs* args, void* user_data)
{
int r;
OnigCalloutIn in;
OnigType type;
OnigValue val;
FILE* fp;
fp = stdout;
in = onig_get_callout_in_by_callout_args(args);
r = onig_get_arg_by_callout_args(args, 1, &type, &val);
if (r != ONIG_NORMAL) return r;
if (in == ONIG_CALLOUT_IN_PROGRESS) {
if (val.c == '<')
return ONIG_CALLOUT_SUCCESS;
}
else {
if (val.c != 'X' && val.c != '<')
return ONIG_CALLOUT_SUCCESS;
}
r = onig_get_arg_by_callout_args(args, 0, &type, &val);
if (r != ONIG_NORMAL) return r;
fprintf(fp, "%s %s\n",
(in == ONIG_CALLOUT_IN_PROGRESS ? "=>" : "<="),
val.s.start);
fflush(fp);
return ONIG_CALLOUT_SUCCESS;
}
static int
test(OnigEncoding enc, char* in_pattern, char* in_str)
{
int r;
unsigned char *start, *range, *end;
regex_t* reg;
OnigErrorInfo einfo;
OnigRegion *region;
UChar* pattern;
UChar* str;
pattern = (UChar* )in_pattern;
str = (UChar* )in_str;
r = onig_new(®, pattern, pattern + strlen((char* )pattern),
ONIG_OPTION_DEFAULT, enc, ONIG_SYNTAX_DEFAULT, &einfo);
if (r != ONIG_NORMAL) {
char s[ONIG_MAX_ERROR_MESSAGE_LEN];
onig_error_code_to_str((UChar* )s, r, &einfo);
fprintf(stderr, "COMPILE ERROR: %d: %s\n", r, s);
return -1;
}
region = onig_region_new();
end = str + strlen((char* )str);
start = str;
range = end;
r = onig_search(reg, str, end, start, range, region, ONIG_OPTION_NONE);
if (r >= 0) {
int i;
fprintf(stderr, "match at %d\n", r);
for (i = 0; i < region->num_regs; i++) {
fprintf(stderr, "%d: (%d-%d)\n", i, region->beg[i], region->end[i]);
}
}
else if (r == ONIG_MISMATCH) {
fprintf(stderr, "search fail\n");
}
else { /* error */
char s[ONIG_MAX_ERROR_MESSAGE_LEN];
onig_error_code_to_str((UChar* )s, r);
fprintf(stderr, "SEARCH ERROR: %d: %s\n", r, s);
}
onig_region_free(region, 1 /* 1:free self, 0:free contents only */);
onig_free(reg);
return r;
}
extern int main(int argc, char* argv[])
{
int r;
int id;
UChar* name;
OnigEncoding use_encs[1];
OnigType arg_types[4];
OnigValue opt_defaults[4];
OnigEncoding enc;
enc = ONIG_ENCODING_UTF8;
use_encs[0] = enc;
r = onig_initialize(use_encs, sizeof(use_encs)/sizeof(use_encs[0]));
if (r != ONIG_NORMAL) return -1;
name = (UChar* )"echo";
arg_types[0] = ONIG_TYPE_STRING;
arg_types[1] = ONIG_TYPE_CHAR;
opt_defaults[0].s.start = (UChar* )"echo";
opt_defaults[0].s.end = opt_defaults[0].s.start +
strlen((char* )opt_defaults[0].s.start);
opt_defaults[1].c = '>';
id = onig_set_callout_of_name(enc, ONIG_CALLOUT_TYPE_SINGLE,
name, name + strlen((char* )name),
ONIG_CALLOUT_IN_BOTH, echo, 0,
2, arg_types, 2, opt_defaults);
if (id < 0) {
fprintf(stderr, "ERROR: fail to set callout of name: %s\n", name);
return -1;
}
test(enc, "(?:(*echo{abc!!!})a|b)*", "abba");
test(enc, "(?:(*echo{xyz,X})a|b)*", "abba");
onig_end();
return 0;
}