1 /* kwsearch.c - searching subroutines using kwset for grep.
2 Copyright 1992, 1998, 2000, 2007, 2009-2011 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
19 /* Written August 1992 by Mike Haertel. */
24 /* For -w, we also consider _ to be word constituent. */
25 #define WCHAR(C) (isalnum (C) || (C) == '_')
27 /* KWset compiled pattern. For Ecompile and Gcompile, we compile
28 a list of strings, at least one of which is known to occur in
29 any string matching the regexp. */
33 Fcompile (char const *pattern, size_t size)
35 char const *beg, *end, *lim, *err, *pat;
41 if (match_icase && MB_CUR_MAX > 1)
42 pat = mbtolower (pattern, &psize);
50 for (lim = beg;; ++lim)
53 if (lim >= pat + psize)
60 #if HAVE_DOS_FILE_CONTENTS
61 if (*lim == '\r' && lim + 1 < pat + psize && lim[1] == '\n')
69 if ((err = kwsincr (kwset, beg, end - beg)) != NULL)
70 error (EXIT_TROUBLE, 0, "%s", err);
73 while (beg < pat + psize);
75 if ((err = kwsprep (kwset)) != NULL)
76 error (EXIT_TROUBLE, 0, "%s", err);
80 Fexecute (char const *buf, size_t size, size_t *match_size,
81 char const *start_ptr)
83 char const *beg, *try, *end, *mb_start;
86 struct kwsmatch kwsmatch;
93 char *case_buf = mbtolower (buf, &size);
95 start_ptr = case_buf + (start_ptr - buf);
99 #endif /* MBS_SUPPORT */
101 for (mb_start = beg = start_ptr ? start_ptr : buf; beg <= buf + size; beg++)
103 size_t offset = kwsexec (kwset, beg, buf + size - beg, &kwsmatch);
104 if (offset == (size_t) -1)
106 len = kwsmatch.size[0];
108 if (MB_CUR_MAX > 1 && is_mb_middle (&mb_start, beg + offset, buf + size,
111 /* The match was a part of multibyte character, advance at least
112 one byte to ensure no infinite loop happens. */
114 memset (&s, 0, sizeof s);
115 size_t mb_len = mbrlen (mb_start, (buf + size) - (beg + offset), &s);
116 if (mb_len == (size_t) -2)
119 if (mb_len != (size_t) -1)
123 #endif /* MBS_SUPPORT */
125 if (start_ptr && !match_words)
126 goto success_in_beg_and_len;
129 if (beg > buf && beg[-1] != eol)
131 if (beg + len < buf + size && beg[len] != eol)
135 else if (match_words)
138 if (try > buf && WCHAR((unsigned char) try[-1]))
140 if (try + len < buf + size && WCHAR((unsigned char) try[len]))
144 offset = kwsexec (kwset, beg, --len, &kwsmatch);
145 if (offset == (size_t) -1)
148 len = kwsmatch.size[0];
153 goto success_in_beg_and_len;
157 } /* for (beg in buf) */
164 if ((end = memchr (beg + len, eol, (buf + size) - (beg + len))) != NULL)
168 while (buf < beg && beg[-1] != eol)
171 success_in_beg_and_len: