aboutsummaryrefslogtreecommitdiffstats
path: root/regex.c
blob: 27ccdf114361ecb26d2a95250bb89ed4d9f51c0a (plain) (blame)
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/* 
 * regex: Regular expression pattern match library
 * 
 * by A.ITO, December 1989
 */

#ifdef REGEX_DEBUG
#include <sys/types.h>
#include <malloc.h>
#endif				/* REGEX_DEBUG */
#include <ctype.h>
#include <gc.h>
#include "fm.h"
#include "regex.h"

#ifdef JP_CHARSET
#define RE_KANJI(p)	(((unsigned char)*(p) << 8) | (unsigned char)*((p)+1))
#endif

#define RE_WHICH_RANGE	0xffff

static Regex DefaultRegex;
#define CompiledRegex DefaultRegex.re
#define Cstorage DefaultRegex.storage

static longchar *st_ptr;

static int regmatch(regexchar *, char *, int, int, char **);
static int regmatch1(regexchar *, longchar);
static int matchWhich(longchar *, longchar);


/* 
 * regexCompile: compile regular expression
 */
char *
regexCompile(char *ex, int igncase)
{
    char *msg;
    newRegex(ex, igncase, &DefaultRegex, &msg);
    return msg;
}

Regex *
newRegex(char *ex, int igncase, Regex * regex, char **msg)
{
    char *p;
    longchar *r;
    regexchar *re = regex->re - 1;
    int m;

    if (regex == 0)
	regex = (Regex *) GC_malloc_atomic(sizeof(Regex));
    st_ptr = regex->storage;
    for (p = ex; *p != '\0'; p++) {
	switch (*p) {
	case '.':
	    re++;
	    re->pattern = NULL;
	    re->mode = RE_ANY;
	    break;
	case '$':
	    re++;
	    re->pattern = NULL;
	    re->mode = RE_END;
	    break;
	case '^':
	    re++;
	    re->pattern = NULL;
	    re->mode = RE_BEGIN;
	    break;
	case '*':
	    if (!(re->mode & RE_ANY) && re->pattern == NULL) {
		if (msg)
		    *msg = "Invalid regular expression";
		return NULL;
	    }
	    re->mode |= RE_ANYTIME;
	    break;
	case '[':
	    r = st_ptr;
	    if (*++p == '^') {
		p++;
		m = RE_EXCEPT;
	    }
	    else
		m = RE_WHICH;
	    while (*p != ']') {
		if (*p == '\\') {
		    *(st_ptr++) = *(p + 1);
		    p += 2;
		}
		else if (*p == '-') {
		    *(st_ptr++) = RE_WHICH_RANGE;
		    p++;
		}
		else if (*p == '\0') {
		    if (msg)
			*msg = "Missing ]";
		    return NULL;
		}
#ifdef JP_CHARSET
		else if (IS_KANJI1(*p)) {
		    *(st_ptr++) = RE_KANJI(p);
		    p += 2;
		}
#endif
		else
		    *(st_ptr++) = (unsigned char)*(p++);
	    }
	    *(st_ptr++) = '\0';
	    re++;
	    re->pattern = r;
	    re->mode = m;
	    break;
	case '\\':
	    p++;
	default:
	    re++;
#ifdef JP_CHARSET
	    if (IS_KANJI1(*p)) {
		*(st_ptr) = RE_KANJI(p);
		p++;
	    }
	    else
#endif
		*st_ptr = (unsigned char)*p;
	    re->pattern = st_ptr;
	    st_ptr++;
	    re->mode = RE_NORMAL;
	    if (igncase)
		re->mode |= RE_IGNCASE;
	}
	if (st_ptr >= &Cstorage[STORAGE_MAX] ||
	    re >= &CompiledRegex[REGEX_MAX]) {
	    if (msg)
		*msg = "Regular expression too long";
	    return NULL;
	}
    }
    re++;
    re->mode = RE_ENDMARK;
    if (msg)
	*msg = NULL;
    return regex;
}

/* 
 * regexMatch: match regular expression
 */
int
regexMatch(char *str, int len, int firstp)
{
    return RegexMatch(&DefaultRegex, str, len, firstp);
}

int
RegexMatch(Regex * re, char *str, int len, int firstp)
{
    char *p, *ep;

    if (str == NULL)
	return 0;
    re->position = NULL;
    ep = str + ((len == 0) ? strlen(str) : len);
    for (p = str; p < ep; p++) {
	switch (regmatch(re->re, p, ep - p, firstp && (p == str), &re->lposition)) {
	case 1:
	    re->position = p;
	    return 1;
	case -1:
	    re->position = NULL;
	    return -1;
	}
#ifdef JP_CHARSET
	if (IS_KANJI1(*p))
	    p++;
#endif
    }
    return 0;
}

/* 
 * matchedPosition: last matched position
 */
void
MatchedPosition(Regex * re, char **first, char **last)
{
    *first = re->position;
    *last = re->lposition;
}

void
matchedPosition(char **first, char **last)
{
    *first = DefaultRegex.position;
    *last = DefaultRegex.lposition;
}

/* 
 * Intermal routines
 */
static int
regmatch(regexchar * re, char *str, int len, int firstp, char **lastpos)
{
    char *p = str, *ep = str + len;
    char *lpos, *llpos = NULL;
    longchar k;

    *lastpos = NULL;
#ifdef REGEX_DEBUG
    debugre(re, str);
#endif				/* REGEX_DEBUG */
    while ((re->mode & RE_ENDMARK) == 0) {
	if (re->mode & RE_BEGIN) {
	    if (!firstp)
		return 0;
	    re++;
	}
	else if (re->mode & RE_ANYTIME) {
           short matched, ok = 0;
           for (;;) {
        matched = 0;
		if (regmatch(re + 1, p, ep - p, firstp, &lpos) == 1) {
		    llpos = lpos;
		    matched = 1;
		    ok = 1;
		}
        if (p >= ep)
		    break;
#ifdef JP_CHARSET
		if (IS_KANJI1(*p)) {
		    k = RE_KANJI(p);
		    if (regmatch1(re, k)) {
			if (lastpos != NULL)
			    *lastpos = llpos;
			p += 2;
		    }
		    else
			break;
		}
		else
#endif
		{
		    k = (unsigned char)*p;
		    if (regmatch1(re, k)) {
			p++;
			if (lastpos != NULL)
			    *lastpos = llpos;
		    }
		    else
              break;
		}
           } 
	    if (lastpos != NULL)
		*lastpos = llpos;
	    return ok;
	}
	else if (re->mode & RE_END) {
	    if (lastpos != NULL)
		*lastpos = p;
	    return (p >= ep);
	}
	else {
	    int a;
#ifdef JP_CHARSET
	    if (IS_KANJI1(*p)) {
		k = RE_KANJI(p);
		p += 2;
		a = regmatch1(re, k);
	    }
	    else 
#endif
	    {
		k = (unsigned char)*(p++);
		a = regmatch1(re, k);
	    }
	    if (!a)
		return 0;
	    else
		re++;
	}
    }
    if (lastpos != NULL)
	*lastpos = p;
    return 1;
}

static int
regmatch1(regexchar * re, longchar c)
{
    switch (re->mode & RE_MATCHMODE) {
    case RE_ANY:
#ifdef REGEX_DEBUG
	printf("%c vs any. -> 1\n", c);
#endif				/* REGEX_DEBUG */
	return 1;
    case RE_NORMAL:
#ifdef REGEX_DEBUG
	printf("RE=%c vs %c -> %d\n", *re->pattern, c, *re->pattern == c);
#endif				/* REGEX_DEBUG */
	if (re->mode & RE_IGNCASE) {
	    if (*re->pattern < 127 && c < 127 &&
		IS_ALPHA(*re->pattern) && IS_ALPHA(c))
		return tolower(*re->pattern) == tolower(c);
	    else
		return *re->pattern == c;
	}
	else
	    return (*re->pattern == c);
    case RE_WHICH:
	return matchWhich(re->pattern, c);
    case RE_EXCEPT:
	return !matchWhich(re->pattern, c);
    }
    return 0;
}

static int
matchWhich(longchar * pattern, longchar c)
{
    longchar *p = pattern;
    int ans = 0;

#ifdef REGEX_DEBUG
    printf("RE pattern = %s char=%c", pattern, c);
#endif				/* REGEX_DEBUG */
    while (*p != '\0') {
	if (*(p + 1) == RE_WHICH_RANGE && *(p + 2) != '\0') {	/* Char  * 
								 * 
								 * *  * *
								 * * * * * 
								 * * * * 
								 * * *  *
								 * *  * *
								 * *  * *
								 * * *  *
								 * * *  *
								 * * * * * 
								 * * * * * 
								 * * *
								 * class. 
								 * * * *  * 
								 * * * * *
								 * *  * * * 
								 */
	    if (*p <= c && c <= *(p + 2)) {
		ans = 1;
		break;
	    }
	    p += 3;
	}
	else {
	    if (*p == c) {
		ans = 1;
		break;
	    }
	    p++;
	}
    }
#ifdef REGEX_DEBUG
    printf(" -> %d\n", ans);
#endif				/* REGEX_DEBUG */
    return ans;
}

#ifdef REGEX_DEBUG
char *
lc2c(longchar * x)
{
    static char y[100];
    int i = 0;

    while (x[i]) {
	if (x[i] == RE_WHICH_RANGE)
	    y[i] = '-';
	else
	    y[i] = x[i];
	i++;
    }
    y[i] = '\0';
    return y;
}

void
debugre(re, s)
    regexchar *re;
    char *s;
{
    for (; !(re->mode & RE_ENDMARK); re++) {
	if (re->mode & RE_BEGIN) {
	    printf("Begin ");
	    continue;
	}
	else if (re->mode & RE_END) {
	    printf("End ");
	    continue;
	}
	if (re->mode & RE_ANYTIME)
	    printf("Anytime-");

	switch (re->mode & RE_MATCHMODE) {
	case RE_ANY:
	    printf("Any ");
	    break;
	case RE_NORMAL:
	    printf("Match-to'%c' ", *re->pattern);
	    break;
	case RE_WHICH:
	    printf("One-of\"%s\" ", lc2c(re->pattern));
	    break;
	case RE_EXCEPT:
	    printf("Other-than\"%s\" ", lc2c(re->pattern));
	    break;
	default:
	    printf("Unknown ");
	}
    }
    putchar('\n');
}

#endif				/* REGEX_DEBUG */