[go: up one dir, main page]

File: re.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 (238 lines) | stat: -rw-r--r-- 4,465 bytes parent folder | download | duplicates (4)
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
/*
 *	cook - file construction tool
 *	Copyright (C) 1999 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 regular expressions
 */

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

#include <str/re.h>
#include <stracc.h>


int
str_re_match(formal, actual, error_callback)
	string_ty	*formal;
	string_ty	*actual;
	void		(*error_callback)_((const char *));
{
#ifdef HAVE_REGCOMP
	regex_t		preg;
	int		err;

	/*
	 * compile the regular expression
	 */
	err = regcomp(&preg, formal->str_text, REG_BASIC | REG_NOSUB);
	if (err != 0)
	{
		bomb:
		if (error_callback)
		{
			char		buffer[100];

			regerror(err, &preg, buffer, sizeof(buffer));
			error_callback(buffer);
		}
		return -1;
	}

	/*
	 * execute the regular expression
	 */
	err = regexec(&preg, actual->str_text, (size_t)0, NULL, 0);
	regfree(&preg);
	if (err == REG_NOMATCH)
		return 0;
	if (err != 0)
		goto bomb;
	return 1;
#else
	if (error_callback)
		error_callback("Regular expressions not available.");
	return -1;
#endif
}


string_ty *
str_re_substitute(lhs, rhs, actual, error_callback, how_many_times)
	string_ty	*lhs;
	string_ty	*rhs;
	string_ty	*actual;
	void		(*error_callback)_((const char *));
	int		how_many_times;
{
#ifdef HAVE_REGCOMP
	static stracc	sa;
	regex_t		preg;
	int		err;
	const char	*ip;
	const char	*ip_end;
	int		suppress_on_zero;

	/*
	 * compile the regular expression
	 */
	err = regcomp(&preg, lhs->str_text, REG_BASIC);
	if (err != 0)
	{
		bomb:
		if (error_callback)
		{
			char		buffer[100];

			regerror(err, &preg, buffer, sizeof(buffer));
			error_callback(buffer);
		}
		return NULL;
	}

	/*
	 * execute the regular expression
	 */
	if (how_many_times <= 0)
		how_many_times = actual->str_length + 1;
	ip = actual->str_text;
	ip_end = actual->str_text + actual->str_length;
	sa_open(&sa);
	suppress_on_zero = 0;
	while (ip < ip_end)
	{
		regmatch_t	match[10];
		char		*cp;

		err =
			regexec
			(
				&preg,
				ip,
				SIZEOF(match),
				match,
				(ip == actual->str_text ? 0 : REG_NOTBOL)
			);
		if (err == REG_NOMATCH)
			break;
		if (err)
			goto bomb;

		if (match[0].rm_eo == 0 && suppress_on_zero)
			goto yuck;

		/*
		 * copy the fixed portion
		 */
		sa_chars(&sa, ip, match[0].rm_so);

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

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

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

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

				case '0': case '1': case '2': case '3':
				case '4': case '5': case '6': case '7':
				case '8': case '9':
					rm = &match[*cp - '0'];
					if (rm->rm_so < 0)
						break;
					sa_chars
					(
						&sa,
						ip + rm->rm_so,
						rm->rm_eo - rm->rm_so
					);
					break;

				case 0:
					--cp;
					break;
				}
				break;
			}
		}
		suppress_on_zero = 1;

		/*
		 * Move past the matched portion.
		 */
		ip += match[0].rm_eo;

		/*
		 * There is a nasty boundary condition: we need to
		 * move past the infinite loop of a zero-length match.
		 * (This can happen for non-trivial patterns.)
		 */
		if (match[0].rm_so == match[0].rm_eo)
		{
			yuck:
			if (ip >= ip_end)
				break;
			sa_char(&sa, *ip++);
			suppress_on_zero = 0;
		}

		/*
		 * Limit how many times we go through this loop.
		 */
		if (--how_many_times <= 0)
			break;
	}

	/*
	 * Collect the tail-end of the input.
	 */
	sa_chars(&sa, ip, ip_end - ip);
	regfree(&preg);

	/*
	 * Build ther answer.
	 */
	return sa_close(&sa);
#else
	if (error_callback)
		error_callback("Regular expressions not available.");
	return NULL;
#endif
}