[go: up one dir, main page]

File: regex.c

package info (click to toggle)
cook 2.19-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 7,316 kB
  • ctags: 3,969
  • sloc: ansic: 50,331; sh: 13,190; makefile: 4,542; yacc: 3,174; perl: 224; awk: 154
file content (571 lines) | stat: -rw-r--r-- 10,605 bytes parent folder | download | duplicates (3)
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
/*
 *	cook - file construction tool
 *	Copyright (C) 1999, 2001 Peter Miller;
 *	All rights reserved.
 *
 *	This program is free software; you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License as published by
 *	the Free Software Foundation; either version 2 of the License, or
 *	(at your option) any later version.
 *
 *	This program is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU General Public License for more details.
 *
 *	You should have received a copy of the GNU General Public License
 *	along with this program; if not, write to the Free Software
 *	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
 *
 * MANIFEST: functions to manipulate regex pattern matching
 */

#include <ac/stddef.h>
#include <ac/string.h>
#include <ac/regex.h>

#include <error_intl.h>
#include <expr/position.h>
#include <match/private.h>
#include <match/regex.h>
#include <str.h>
#include <stracc.h>
#include <trace.h>


typedef struct match_regex_ty match_regex_ty;
struct match_regex_ty
{
	match_ty	inherited;

	/*
	 * This holds the compiled regular expression.
	 * It is internal to the regex implementation.
	 */
	regex_t		preg;

	/*
	 * The ``actual'' string points to the string which was matched.
	 * We have to keep it around, because the next field points into it.
	 */
	string_ty	*actual;

	/*
	 * The match field contains pointers into the ``actual'' string of
	 * the various matching items (well, indexes actually).
	 */
	regmatch_t	match[10];
};


static void report_regex_error _((const expr_position_ty *, int, regex_t *,
	string_ty *));

static void
report_regex_error(pp, err, preg, formal)
	const expr_position_ty *pp;
	int		err;
	regex_t		*preg;
	string_ty	*formal;
{
	sub_context_ty	*scp;
	char		buffer[200];

	regerror(err, preg, buffer, sizeof(buffer));

	scp = sub_context_new();
	sub_var_set(scp, "Pattern", "%S", formal);
	sub_var_set(scp, "MeSsaGe", "%s", buffer);
	error_with_position
	(
		pp,
		scp,
		i18n("pattern \"$pattern\" error: $message")
	);
	sub_context_delete(scp);
}


static void destructor _((match_ty *));

static void
destructor(mp)
	match_ty	*mp;
{
	match_regex_ty	*this;

	trace(("match_regex::destructor(mp = %08X)\n{\n"/*}*/, mp));
	this = (match_regex_ty *)mp;
	if (this->actual)
		str_free(this->actual);
	regfree(&this->preg);
	trace((/*{*/"}\n"));
}


static void constructor _((match_ty *));

static void
constructor(mp)
	match_ty	*mp;
{
	match_regex_ty	*this;

	trace(("match_regex::constructor(mp = %08X)\n{\n"/*}*/, mp));
	this = (match_regex_ty *)mp;
	this->actual = 0;
	/* this is not 100% portable */
	memset(&this->preg, 0, sizeof(this->preg));
	trace((/*{*/"}\n"));
}


static int compile _((match_ty *, string_ty *, const expr_position_ty *));

static int
compile(mp, formal, pp)
	match_ty	*mp;
	string_ty	*formal;
	const expr_position_ty *pp;
{
	match_regex_ty	*this;
	int		result;
	int		err;
	long		formal_start;
	long		formal_end;

	trace(("match_regex::complie(mp = %08lX, formal = %08lX)\n{\n",
		(long)mp, (long)formal));
	trace(("formal = \"%s\";\n", formal->str_text));
	this = (match_regex_ty *)mp;
	result = -1;

	/*
	 * Release resources held by previous compiles.
	 */
	regfree(&this->preg);

	/*
	 * Work formal over so that it has ^ at the beginning, and $
	 * at the end.	This ensures that the whole `actual' is matches,
	 * not just the bits the pattern likes.  I would much prefer a
	 * flag to regcomp (so that patterns with ``|'' work prperly,
	 * for instance) , but there isn't one.  Sigh.
	 */
	for
	(
		formal_start = 0;
		(
			formal_start < formal->str_length
		&&
			formal->str_text[formal_start] == '^'
		);
		++formal_start
	)
		;
	for
	(
		formal_end = formal->str_length;
		(
			formal_end >= 0
		&&
			formal->str_text[formal_end - 1] == '$'
		);
		--formal_end
	)
		;
	formal =
		str_format
		(
			"^%.*s$",
			(int)(formal_end - formal_start),
			formal->str_text + formal_start
		);

	/*
	 * compile the regular expression
	 */
	err = regcomp(&this->preg, formal->str_text, REG_BASIC);
	if (err != 0)
	{
		report_regex_error(pp, err, &this->preg, formal);
		result = -1;
	}
	else
		result = 0;

	/*
	 * release our worked-over formal expression
	 */
	str_free(formal);

	/*
	 * return result
	 */
	trace(("return %d;\n", result));
	trace(("}\n"));
	return result;
}


static int execute _((match_ty *, string_ty *, const expr_position_ty *));

static int
execute(mp, actual, pp)
	match_ty	*mp;
	string_ty	*actual;
	const expr_position_ty *pp;
{
	match_regex_ty	*this;
	int		result;
	int		err;

	trace(("match_regex::execute(mp = %08lX, actual = %08lX)\n{\n",
		(long)mp, (long)actual));
	trace(("actual = \"%s\";\n", actual->str_text));
	this = (match_regex_ty *)mp;
	result = -1;

	/*
	 * Release resources held by previous executes.
	 */
	if (this->actual)
	{
		str_free(this->actual);
		this->actual = 0;
	}

	/*
	 * execute the regular expression
	 */
	err =
		regexec
		(
			&this->preg,
			actual->str_text,
			SIZEOF(this->match),
			this->match,
			0
		);
	switch (err)
	{
	case 0:
		this->actual = str_copy(actual);
		result = 1;
		break;

	case REG_NOMATCH:
		result = 0;
		break;

	default:
		report_regex_error(pp, err, &this->preg, actual);
		result = -1;
		break;
	}

	trace(("return %d;\n", result));
	trace(("}\n"));
	return result;
}


static void illegal_sub_expr _((const expr_position_ty *, string_ty *, int));

static void
illegal_sub_expr(pp, s, why)
	const expr_position_ty *pp;
	string_ty	*s;
	int		why;
{
	sub_context_ty	*scp;

	scp = sub_context_new();
	sub_var_set(scp, "Name", "\\%d", why);
	sub_var_set(scp, "Pattern", "%S", s);
	error_with_position
	(
		pp,
		scp,
		i18n("illegal use of '$name' in \"$pattern\" pattern")
	);
	sub_context_delete(scp);
}


static string_ty *reconstruct_lhs _((const match_ty *, string_ty *,
	const expr_position_ty *));

static string_ty *
reconstruct_lhs(mp, lhs, pp)
	const match_ty	*mp;
	string_ty	*lhs;
	const expr_position_ty *pp;
{
	const match_regex_ty *this;
	static stracc	buffer;
	int		depth;
	int		n;
	char		*cp;
	char		*ip;
	string_ty	*s;
	int		j;

	trace(("match_regex::reconstruct_lhs(mp = %08lX, lhs = %08X)\n{\n",
		(long)mp, (long)lhs));
	this = (const match_regex_ty *)mp;
	trace_string(lhs->str_text);

	assert(this->actual);
	if (!this->actual)
	{
		/* this_is_a_bug(); */
		trace(("}\n"));
		return 0;
	}
	ip = this->actual->str_text;

	sa_open(&buffer);

	depth = 0;
	n = 0;
	for (cp = lhs->str_text; *cp; ++cp)
	{
		const regmatch_t *rm;

		switch (*cp)
		{
		default:
			if (depth == 0)
				sa_char(&buffer, *cp);
			break;

		case '\\':
			switch (*++cp)
			{
			default:
				if (depth == 0)
					sa_char(&buffer, *cp);
				break;

			case '(':
				++depth;
				++n;
				if (depth == 1)
				{
					rm = &this->match[n];
					if (rm->rm_so < 0)
					{
						illegal_sub_expr(pp, lhs, n);
						trace(("return NULL;\n"));
						trace(("}\n"));
						return 0;
					}
					sa_chars
					(
						&buffer,
						ip + rm->rm_so,
						rm->rm_eo - rm->rm_so
					);
				}
				break;

			case ')':
				if (depth > 0)
					--depth;
				break;

			case '0': case '1': case '2': case '3': case '4':
			case '5': case '6': case '7': case '8': case '9':
				if (depth == 0)
				{
					j = *cp - '0';
					rm = &this->match[j];
					if (rm->rm_so < 0)
					{
						illegal_sub_expr(pp, lhs, j);
						trace(("return NULL;\n"));
						trace(("}\n"));
						return 0;
					}
					sa_chars
					(
						&buffer,
						ip + rm->rm_so,
						rm->rm_eo - rm->rm_so
					);
				}
				break;

			case 0:
				--cp;
				break;
			}
			break;
		}
	}

	s = sa_close(&buffer);
	trace_string(s->str_text);
	trace(("return %08lX;\n", s));
	trace(("}\n"));
	return s;
}


static string_ty *reconstruct_rhs _((const match_ty *, string_ty *,
	const expr_position_ty *));

static string_ty *
reconstruct_rhs(mp, rhs, pp)
	const match_ty	*mp;
	string_ty	*rhs;
	const expr_position_ty *pp;
{
	const match_regex_ty *this;
	static stracc	buffer;
	char		*cp;
	char		*ip;
	string_ty	*s;
	int		j;

	trace(("match_regex::reconstruct_rhs(mp = %08lX, rhs = %08X)\n{\n",
		(long)mp, (long)rhs));
	this = (const match_regex_ty *)mp;
	trace_string(rhs->str_text);
	assert(this->actual);
	if (!this->actual)
	{
		/* this_is_a_bug(); */
		trace(("}\n"));
		return 0;
	}
	ip = this->actual->str_text;

	sa_open(&buffer);

	/*
	 * replace the matched portion with the right hand side
	 */
	for (cp = rhs->str_text; *cp; ++cp)
	{
		const regmatch_t *rm;

		switch (*cp)
		{
		default:
			sa_char(&buffer, *cp);
			break;

		case '&':
			rm = &this->match[0];
			sa_chars
			(
				&buffer,
				ip + rm->rm_so,
				rm->rm_eo - rm->rm_so
			);
			break;

		case '\\':
			switch (*++cp)
			{
			default:
				sa_char(&buffer, *cp);
				break;

			case '0': case '1': case '2': case '3':
			case '4': case '5': case '6': case '7':
			case '8': case '9':
				j = *cp - '0';
				rm = &this->match[j];
				if (rm->rm_so < 0)
				{
					illegal_sub_expr(pp, rhs, j);
					trace(("return NULL;\n"));
					trace(("}\n"));
					return 0;
				}
				sa_chars
				(
					&buffer,
					ip + rm->rm_so,
					rm->rm_eo - rm->rm_so
				);
				break;

			case 0:
				--cp;
				break;
			}
			break;
		}
	}

	/*
	 * Build the result and return.
	 */
	s = sa_close(&buffer);
	trace_string(s->str_text);
	trace(("return %08lX;\n", s));
	trace(("}\n"));
	return s;
}


static int usage_mask _((const match_ty *, string_ty *,
	const expr_position_ty *));

static int
usage_mask(mp, s, pp)
	const match_ty	*mp;
	string_ty	*s;
	const expr_position_ty *pp;
{
	int		n;
	const char	*cp;

	/*
	 * This is just plain horrible.  We are trying to see if
	 * we have been given a regex pattern *and* it has one or
	 * more sub-expressions.  (One that has some problems is OK,
	 * because the real regcomp will (hopefully) report them.)
	 * Since there is no nice way to ask POSIX regex this question,
	 * this less-than-perfect method is used.
	 *
	 * Unfortunately, different rx implementations give different
	 * re_nsub results for the same string (literal strings give 0
	 * on some, and 1 on others) so it can't be used.
	 */
	n = 0;
	for (cp = s->str_text; *cp; ++cp)
	{
		if (cp[0] == '\\' && cp[1] == '(')
		{
			++n;
			++cp;
		}
	}
	return (n ? ((1 << n) - 1) : 0);
}


static match_method_ty vtbl =
{
	"regex",
	sizeof(match_regex_ty),
	destructor,
	constructor,
	compile,
	execute,
	reconstruct_lhs,
	reconstruct_rhs,
	usage_mask,
};


match_ty *
match_regex_new()
{
	return match_private_new(&vtbl);
}