pattern.c (009e81b16465ea457c0e63fd49fe77f47cc27a5a) pattern.c (1ea316270f1f75922ac53976d5d8808a41442f46)
1/*
2 * Copyright (C) 1984-2015 Mark Nudelman
3 *
4 * You may distribute under the terms of either the GNU General Public
5 * License or the Less License, as specified in the README file.
6 *
7 * For more information, see the README file.
8 */

--- 6 unchanged lines hidden (view full) ---

15#include "pattern.h"
16
17extern int caseless;
18
19/*
20 * Compile a search pattern, for future use by match_pattern.
21 */
22 static int
1/*
2 * Copyright (C) 1984-2015 Mark Nudelman
3 *
4 * You may distribute under the terms of either the GNU General Public
5 * License or the Less License, as specified in the README file.
6 *
7 * For more information, see the README file.
8 */

--- 6 unchanged lines hidden (view full) ---

15#include "pattern.h"
16
17extern int caseless;
18
19/*
20 * Compile a search pattern, for future use by match_pattern.
21 */
22 static int
23compile_pattern2(pattern, search_type, comp_pattern, show_error)
24 char *pattern;
25 int search_type;
26 void **comp_pattern;
27 int show_error;
23compile_pattern2(char *pattern, int search_type, void **comp_pattern, int show_error)
28{
29 if (search_type & SRCH_NO_REGEX)
30 return (0);
31 {
32#if HAVE_GNU_REGEX
33 struct re_pattern_buffer *comp = (struct re_pattern_buffer *)
34 ecalloc(1, sizeof(struct re_pattern_buffer));
35 struct re_pattern_buffer **pcomp =

--- 86 unchanged lines hidden (view full) ---

122 }
123 return (0);
124}
125
126/*
127 * Like compile_pattern2, but convert the pattern to lowercase if necessary.
128 */
129 public int
24{
25 if (search_type & SRCH_NO_REGEX)
26 return (0);
27 {
28#if HAVE_GNU_REGEX
29 struct re_pattern_buffer *comp = (struct re_pattern_buffer *)
30 ecalloc(1, sizeof(struct re_pattern_buffer));
31 struct re_pattern_buffer **pcomp =

--- 86 unchanged lines hidden (view full) ---

118 }
119 return (0);
120}
121
122/*
123 * Like compile_pattern2, but convert the pattern to lowercase if necessary.
124 */
125 public int
130compile_pattern(pattern, search_type, comp_pattern)
131 char *pattern;
132 int search_type;
133 void **comp_pattern;
126compile_pattern(char *pattern, int search_type, void **comp_pattern)
134{
135 char *cvt_pattern;
136 int result;
137
138 if (caseless != OPT_ONPLUS)
139 cvt_pattern = pattern;
140 else
141 {

--- 5 unchanged lines hidden (view full) ---

147 free(cvt_pattern);
148 return (result);
149}
150
151/*
152 * Forget that we have a compiled pattern.
153 */
154 public void
127{
128 char *cvt_pattern;
129 int result;
130
131 if (caseless != OPT_ONPLUS)
132 cvt_pattern = pattern;
133 else
134 {

--- 5 unchanged lines hidden (view full) ---

140 free(cvt_pattern);
141 return (result);
142}
143
144/*
145 * Forget that we have a compiled pattern.
146 */
147 public void
155uncompile_pattern(pattern)
156 void **pattern;
148uncompile_pattern(void **pattern)
157{
158#if HAVE_GNU_REGEX
159 struct re_pattern_buffer **pcomp = (struct re_pattern_buffer **) pattern;
160 if (*pcomp != NULL)
161 regfree(*pcomp);
162 *pcomp = NULL;
163#endif
164#if HAVE_POSIX_REGCOMP

--- 25 unchanged lines hidden (view full) ---

190 *pcomp = NULL;
191#endif
192}
193
194/*
195 * Can a pattern be successfully compiled?
196 */
197 public int
149{
150#if HAVE_GNU_REGEX
151 struct re_pattern_buffer **pcomp = (struct re_pattern_buffer **) pattern;
152 if (*pcomp != NULL)
153 regfree(*pcomp);
154 *pcomp = NULL;
155#endif
156#if HAVE_POSIX_REGCOMP

--- 25 unchanged lines hidden (view full) ---

182 *pcomp = NULL;
183#endif
184}
185
186/*
187 * Can a pattern be successfully compiled?
188 */
189 public int
198valid_pattern(pattern)
199 char *pattern;
190valid_pattern(char *pattern)
200{
201 void *comp_pattern;
202 int result;
203
204 CLEAR_PATTERN(comp_pattern);
205 result = compile_pattern2(pattern, 0, &comp_pattern, 0);
206 if (result != 0)
207 return (0);
208 uncompile_pattern(&comp_pattern);
209 return (1);
210}
211
212/*
213 * Is a compiled pattern null?
214 */
215 public int
191{
192 void *comp_pattern;
193 int result;
194
195 CLEAR_PATTERN(comp_pattern);
196 result = compile_pattern2(pattern, 0, &comp_pattern, 0);
197 if (result != 0)
198 return (0);
199 uncompile_pattern(&comp_pattern);
200 return (1);
201}
202
203/*
204 * Is a compiled pattern null?
205 */
206 public int
216is_null_pattern(pattern)
217 void *pattern;
207is_null_pattern(void *pattern)
218{
219#if HAVE_GNU_REGEX
220 return (pattern == NULL);
221#endif
222#if HAVE_POSIX_REGCOMP
223 return (pattern == NULL);
224#endif
225#if HAVE_PCRE

--- 13 unchanged lines hidden (view full) ---

239#endif
240}
241
242/*
243 * Simple pattern matching function.
244 * It supports no metacharacters like *, etc.
245 */
246 static int
208{
209#if HAVE_GNU_REGEX
210 return (pattern == NULL);
211#endif
212#if HAVE_POSIX_REGCOMP
213 return (pattern == NULL);
214#endif
215#if HAVE_PCRE

--- 13 unchanged lines hidden (view full) ---

229#endif
230}
231
232/*
233 * Simple pattern matching function.
234 * It supports no metacharacters like *, etc.
235 */
236 static int
247match(pattern, pattern_len, buf, buf_len, pfound, pend)
248 char *pattern;
249 int pattern_len;
250 char *buf;
251 int buf_len;
252 char **pfound, **pend;
237match(char *pattern, int pattern_len, char *buf, int buf_len, char **pfound, char **pend)
253{
238{
254 register char *pp, *lp;
255 register char *pattern_end = pattern + pattern_len;
256 register char *buf_end = buf + buf_len;
239 char *pp, *lp;
240 char *pattern_end = pattern + pattern_len;
241 char *buf_end = buf + buf_len;
257
258 for ( ; buf < buf_end; buf++)
259 {
260 for (pp = pattern, lp = buf; ; pp++, lp++)
261 {
262 char cp = *pp;
263 char cl = *lp;
264 if (caseless == OPT_ONPLUS && ASCII_IS_UPPER(cp))

--- 15 unchanged lines hidden (view full) ---

280 return (0);
281}
282
283/*
284 * Perform a pattern match with the previously compiled pattern.
285 * Set sp and ep to the start and end of the matched string.
286 */
287 public int
242
243 for ( ; buf < buf_end; buf++)
244 {
245 for (pp = pattern, lp = buf; ; pp++, lp++)
246 {
247 char cp = *pp;
248 char cl = *lp;
249 if (caseless == OPT_ONPLUS && ASCII_IS_UPPER(cp))

--- 15 unchanged lines hidden (view full) ---

265 return (0);
266}
267
268/*
269 * Perform a pattern match with the previously compiled pattern.
270 * Set sp and ep to the start and end of the matched string.
271 */
272 public int
288match_pattern(pattern, tpattern, line, line_len, sp, ep, notbol, search_type)
289 void *pattern;
290 char *tpattern;
291 char *line;
292 int line_len;
293 char **sp;
294 char **ep;
295 int notbol;
296 int search_type;
273match_pattern(void *pattern, char *tpattern, char *line, int line_len, char **sp, char **ep, int notbol, int search_type)
297{
298 int matched;
299#if HAVE_GNU_REGEX
300 struct re_pattern_buffer *spattern = (struct re_pattern_buffer *) pattern;
301#endif
302#if HAVE_POSIX_REGCOMP
303 regex_t *spattern = (regex_t *) pattern;
304#endif

--- 100 unchanged lines hidden ---
274{
275 int matched;
276#if HAVE_GNU_REGEX
277 struct re_pattern_buffer *spattern = (struct re_pattern_buffer *) pattern;
278#endif
279#if HAVE_POSIX_REGCOMP
280 regex_t *spattern = (regex_t *) pattern;
281#endif

--- 100 unchanged lines hidden ---