-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathregexp_system.c
More file actions
214 lines (181 loc) · 3.95 KB
/
regexp_system.c
File metadata and controls
214 lines (181 loc) · 3.95 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
* $MawkId: regexp_system.c,v 1.10 2009/12/14 09:26:50 tom Exp $
*/
#include <sys/types.h>
#include <stdio.h>
#include <regex.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include "regexp.h"
typedef struct {
regex_t re;
char *regexp;
} mawk_re_t;
static mawk_re_t *last_used_regexp = NULL;
static int err_code = 0;
/*#define MAWK_EXTRACT_REGEXP_DEBUG*/
static void
prepare_regexp(char *regexp)
{
#ifdef MAWK_EXTRACT_REGEXP_DEBUG
char *begin = regexp;
#endif
int bs = 0;
char *tail = regexp;
char ch;
#ifdef MAWK_EXTRACT_REGEXP_DEBUG
fprintf(stderr, "in: %s\n", begin);
#endif
while ((ch = *regexp++) != 0) {
if (bs) {
switch (ch) {
case 'n':
*tail++ = '\n';
break;
case 't':
*tail++ = '\t';
break;
case 'f':
*tail++ = '\f';
break;
case 'b':
*tail++ = '\b';
break;
case 'r':
*tail++ = '\r';
break;
case 'a':
*tail++ = '\07';
break;
case 'v':
*tail++ = '\013';
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
*tail = (char) (ch - '0');
ch = *regexp++;
if (ch >= '0' && ch <= '7') {
*tail = (char) (((unsigned char) *tail) * 8 + (ch - '0'));
ch = *regexp++;
if (ch >= '0' && ch <= '7') {
*tail = (char) (((unsigned char) *tail) * 8 + (ch - '0'));
} else {
--regexp;
}
} else {
--regexp;
}
++tail;
break;
default:
/* pass \<unknown_char> to regcomp */
#ifdef MAWK_EXTRACT_REGEXP_DEBUG
fprintf(stderr, "passing %c%c\n", '\\', ch);
#endif
*tail++ = '\\';
*tail++ = ch;
}
bs = 0;
} else {
if (ch == '\\') {
bs = 1;
} else {
*tail++ = ch;
}
}
}
*tail = 0;
#ifdef MAWK_EXTRACT_REGEXP_DEBUG
fprintf(stderr, "out: %s\n", begin);
#endif
}
void *
REcompile(char *regexp, size_t len)
{
mawk_re_t *re = (mawk_re_t *) malloc(sizeof(mawk_re_t));
char *new_regexp = (char *) malloc(len + 3);
if (!re || !new_regexp)
return NULL;
new_regexp[0] = '(';
memcpy(new_regexp + 1, regexp, len);
new_regexp[len + 1] = ')';
new_regexp[len + 2] = 0;
prepare_regexp(new_regexp);
last_used_regexp = re;
memset(re, 0, sizeof(mawk_re_t));
re->regexp = strdup(new_regexp);
err_code = regcomp(&re->re, new_regexp, REG_EXTENDED);
free(new_regexp);
if (err_code)
return NULL;
return re;
}
/*
* Test the regular expression in 'q' against the string 'str'.
* The 'len' parameter is ignored since POSIX regular expressions assume 'str'
* is a null-terminated string.
*/
int
REtest(char *str, unsigned str_len GCC_UNUSED, PTR q)
{
mawk_re_t *re = (mawk_re_t *) q;
/* fprintf (stderr, "REtest: \"%s\" ~ /%s/", str, re -> regexp); */
last_used_regexp = re;
if (regexec(&re->re, str, 0, NULL, 0)) {
/* fprintf (stderr, "=1\n"); */
return 0;
} else {
/* fprintf (stderr, "=0\n"); */
return 1;
}
}
#define MAX_MATCHES 100
char *
REmatch(char *str, unsigned str_len GCC_UNUSED, PTR q, unsigned *lenp)
{
mawk_re_t *re = (mawk_re_t *) q;
regmatch_t match[MAX_MATCHES];
/* fprintf (stderr, "REmatch: \"%s\" ~ /%s/", str, re -> regexp); */
last_used_regexp = re;
if (!regexec(&re->re, str, MAX_MATCHES, match, 0)) {
*lenp = match[0].rm_eo - match[0].rm_so;
/* fprintf (stderr, "=%i/%i\n", match [0].rm_so, *lenp); */
return str + match[0].rm_so;
} else {
/* fprintf (stderr, "=0\n"); */
return NULL;
}
}
void
REmprint(void *m, FILE *f)
{
(void) m;
(void) f;
/* no debugging code available */
abort();
}
static char error_buffer[2048];
const char *
REerror(void)
{
size_t len;
if (last_used_regexp) {
len = regerror(err_code, &last_used_regexp->re,
error_buffer, sizeof(error_buffer));
} else {
char *msg = strerror(errno);
const char *fmt = "malloc failed: %.*s";
sprintf(error_buffer,
fmt,
(int) (sizeof(error_buffer) - strlen(msg)),
msg);
}
return error_buffer;
}