xref: /netbsd-src/external/gpl2/gettext/dist/gettext-tools/libgrep/m-fgrep.c (revision 946379e7b37692fc43f68eb0d1c10daa0a7f3b6c)
1*946379e7Schristos /* Pattern Matcher for Fixed String search.
2*946379e7Schristos    Copyright (C) 1992, 1998, 2000, 2005 Free Software Foundation, Inc.
3*946379e7Schristos 
4*946379e7Schristos    This program is free software; you can redistribute it and/or modify
5*946379e7Schristos    it under the terms of the GNU General Public License as published by
6*946379e7Schristos    the Free Software Foundation; either version 2, or (at your option)
7*946379e7Schristos    any later version.
8*946379e7Schristos 
9*946379e7Schristos    This program is distributed in the hope that it will be useful,
10*946379e7Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
11*946379e7Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12*946379e7Schristos    GNU General Public License for more details.
13*946379e7Schristos 
14*946379e7Schristos    You should have received a copy of the GNU General Public License
15*946379e7Schristos    along with this program; if not, write to the Free Software Foundation,
16*946379e7Schristos    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
17*946379e7Schristos 
18*946379e7Schristos #ifdef HAVE_CONFIG_H
19*946379e7Schristos # include <config.h>
20*946379e7Schristos #endif
21*946379e7Schristos 
22*946379e7Schristos /* Specification.  */
23*946379e7Schristos #include "libgrep.h"
24*946379e7Schristos 
25*946379e7Schristos #include <ctype.h>
26*946379e7Schristos #include <stdlib.h>
27*946379e7Schristos #include <string.h>
28*946379e7Schristos 
29*946379e7Schristos #include "error.h"
30*946379e7Schristos #include "exitfail.h"
31*946379e7Schristos #include "xalloc.h"
32*946379e7Schristos #include "m-common.h"
33*946379e7Schristos 
34*946379e7Schristos #if defined (STDC_HEADERS) || (!defined (isascii) && !defined (HAVE_ISASCII))
35*946379e7Schristos # define IN_CTYPE_DOMAIN(c) 1
36*946379e7Schristos #else
37*946379e7Schristos # define IN_CTYPE_DOMAIN(c) isascii(c)
38*946379e7Schristos #endif
39*946379e7Schristos #define ISALNUM(C) (IN_CTYPE_DOMAIN (C) && isalnum (C))
40*946379e7Schristos 
41*946379e7Schristos static void *
Fcompile(const char * pattern,size_t pattern_size,bool match_icase,bool match_words,bool match_lines,char eolbyte)42*946379e7Schristos Fcompile (const char *pattern, size_t pattern_size,
43*946379e7Schristos 	  bool match_icase, bool match_words, bool match_lines,
44*946379e7Schristos 	  char eolbyte)
45*946379e7Schristos {
46*946379e7Schristos   struct compiled_kwset *ckwset;
47*946379e7Schristos   const char *beg, *lim, *err;
48*946379e7Schristos 
49*946379e7Schristos   ckwset = (struct compiled_kwset *) xmalloc (sizeof (struct compiled_kwset));
50*946379e7Schristos   kwsinit (ckwset, match_icase, match_words, match_lines, eolbyte);
51*946379e7Schristos 
52*946379e7Schristos   beg = pattern;
53*946379e7Schristos   do
54*946379e7Schristos     {
55*946379e7Schristos       for (lim = beg; lim < pattern + pattern_size && *lim != '\n'; ++lim)
56*946379e7Schristos 	;
57*946379e7Schristos       if ((err = kwsincr (ckwset->kwset, beg, lim - beg)) != NULL)
58*946379e7Schristos 	error (exit_failure, 0, err);
59*946379e7Schristos       if (lim < pattern + pattern_size)
60*946379e7Schristos 	++lim;
61*946379e7Schristos       beg = lim;
62*946379e7Schristos     }
63*946379e7Schristos   while (beg < pattern + pattern_size);
64*946379e7Schristos 
65*946379e7Schristos   if ((err = kwsprep (ckwset->kwset)) != NULL)
66*946379e7Schristos     error (exit_failure, 0, err);
67*946379e7Schristos   return ckwset;
68*946379e7Schristos }
69*946379e7Schristos 
70*946379e7Schristos static size_t
Fexecute(const void * compiled_pattern,const char * buf,size_t buf_size,size_t * match_size,bool exact)71*946379e7Schristos Fexecute (const void *compiled_pattern, const char *buf, size_t buf_size,
72*946379e7Schristos 	  size_t *match_size, bool exact)
73*946379e7Schristos {
74*946379e7Schristos   struct compiled_kwset *ckwset = (struct compiled_kwset *) compiled_pattern;
75*946379e7Schristos   register const char *beg, *try, *end;
76*946379e7Schristos   register size_t len;
77*946379e7Schristos   char eol = ckwset->eolbyte;
78*946379e7Schristos   struct kwsmatch kwsmatch;
79*946379e7Schristos #ifdef MBS_SUPPORT
80*946379e7Schristos   char *mb_properties;
81*946379e7Schristos   if (MB_CUR_MAX > 1)
82*946379e7Schristos     mb_properties = check_multibyte_string (buf, buf_size);
83*946379e7Schristos #endif /* MBS_SUPPORT */
84*946379e7Schristos 
85*946379e7Schristos   for (beg = buf; beg <= buf + buf_size; ++beg)
86*946379e7Schristos     {
87*946379e7Schristos       size_t offset =
88*946379e7Schristos 	kwsexec (ckwset->kwset, beg, buf + buf_size - beg, &kwsmatch);
89*946379e7Schristos       if (offset == (size_t) -1)
90*946379e7Schristos 	{
91*946379e7Schristos #ifdef MBS_SUPPORT
92*946379e7Schristos 	  if (MB_CUR_MAX > 1)
93*946379e7Schristos 	    free (mb_properties);
94*946379e7Schristos #endif /* MBS_SUPPORT */
95*946379e7Schristos 	  return offset;
96*946379e7Schristos 	}
97*946379e7Schristos #ifdef MBS_SUPPORT
98*946379e7Schristos       if (MB_CUR_MAX > 1 && mb_properties[offset+beg-buf] == 0)
99*946379e7Schristos 	continue; /* It is a part of multibyte character.  */
100*946379e7Schristos #endif /* MBS_SUPPORT */
101*946379e7Schristos       beg += offset;
102*946379e7Schristos       len = kwsmatch.size[0];
103*946379e7Schristos       if (exact)
104*946379e7Schristos 	{
105*946379e7Schristos 	  *match_size = len;
106*946379e7Schristos #ifdef MBS_SUPPORT
107*946379e7Schristos 	  if (MB_CUR_MAX > 1)
108*946379e7Schristos 	    free (mb_properties);
109*946379e7Schristos #endif /* MBS_SUPPORT */
110*946379e7Schristos 	  return beg - buf;
111*946379e7Schristos 	}
112*946379e7Schristos       if (ckwset->match_lines)
113*946379e7Schristos 	{
114*946379e7Schristos 	  if (beg > buf && beg[-1] != eol)
115*946379e7Schristos 	    continue;
116*946379e7Schristos 	  if (beg + len < buf + buf_size && beg[len] != eol)
117*946379e7Schristos 	    continue;
118*946379e7Schristos 	  goto success;
119*946379e7Schristos 	}
120*946379e7Schristos       else if (ckwset->match_words)
121*946379e7Schristos 	for (try = beg; len; )
122*946379e7Schristos 	  {
123*946379e7Schristos 	    if (try > buf && IS_WORD_CONSTITUENT ((unsigned char) try[-1]))
124*946379e7Schristos 	      break;
125*946379e7Schristos 	    if (try + len < buf + buf_size
126*946379e7Schristos 		&& IS_WORD_CONSTITUENT ((unsigned char) try[len]))
127*946379e7Schristos 	      {
128*946379e7Schristos 		offset = kwsexec (ckwset->kwset, beg, --len, &kwsmatch);
129*946379e7Schristos 		if (offset == (size_t) -1)
130*946379e7Schristos 		  {
131*946379e7Schristos #ifdef MBS_SUPPORT
132*946379e7Schristos 		    if (MB_CUR_MAX > 1)
133*946379e7Schristos 		      free (mb_properties);
134*946379e7Schristos #endif /* MBS_SUPPORT */
135*946379e7Schristos 		    return offset;
136*946379e7Schristos 		  }
137*946379e7Schristos 		try = beg + offset;
138*946379e7Schristos 		len = kwsmatch.size[0];
139*946379e7Schristos 	      }
140*946379e7Schristos 	    else
141*946379e7Schristos 	      goto success;
142*946379e7Schristos 	  }
143*946379e7Schristos       else
144*946379e7Schristos 	goto success;
145*946379e7Schristos     }
146*946379e7Schristos 
147*946379e7Schristos #ifdef MBS_SUPPORT
148*946379e7Schristos   if (MB_CUR_MAX > 1)
149*946379e7Schristos     free (mb_properties);
150*946379e7Schristos #endif /* MBS_SUPPORT */
151*946379e7Schristos   return -1;
152*946379e7Schristos 
153*946379e7Schristos  success:
154*946379e7Schristos   end = memchr (beg + len, eol, (buf + buf_size) - (beg + len));
155*946379e7Schristos   end++;
156*946379e7Schristos   while (buf < beg && beg[-1] != eol)
157*946379e7Schristos     --beg;
158*946379e7Schristos   *match_size = end - beg;
159*946379e7Schristos #ifdef MBS_SUPPORT
160*946379e7Schristos   if (MB_CUR_MAX > 1)
161*946379e7Schristos     free (mb_properties);
162*946379e7Schristos #endif /* MBS_SUPPORT */
163*946379e7Schristos   return beg - buf;
164*946379e7Schristos }
165*946379e7Schristos 
166*946379e7Schristos static void
Ffree(void * compiled_pattern)167*946379e7Schristos Ffree (void *compiled_pattern)
168*946379e7Schristos {
169*946379e7Schristos   struct compiled_kwset *ckwset = (struct compiled_kwset *) compiled_pattern;
170*946379e7Schristos 
171*946379e7Schristos   free (ckwset->trans);
172*946379e7Schristos   free (ckwset);
173*946379e7Schristos }
174*946379e7Schristos 
175*946379e7Schristos matcher_t matcher_fgrep =
176*946379e7Schristos   {
177*946379e7Schristos     Fcompile,
178*946379e7Schristos     Fexecute,
179*946379e7Schristos     Ffree
180*946379e7Schristos   };
181*946379e7Schristos 
182