xref: /freebsd/contrib/ncurses/form/fty_regex.c (revision 39beb93c3f8bdbf72a61fda42300b5ebed7390c8)
1 /****************************************************************************
2  * Copyright (c) 1998-2006,2007 Free Software Foundation, Inc.              *
3  *                                                                          *
4  * Permission is hereby granted, free of charge, to any person obtaining a  *
5  * copy of this software and associated documentation files (the            *
6  * "Software"), to deal in the Software without restriction, including      *
7  * without limitation the rights to use, copy, modify, merge, publish,      *
8  * distribute, distribute with modifications, sublicense, and/or sell       *
9  * copies of the Software, and to permit persons to whom the Software is    *
10  * furnished to do so, subject to the following conditions:                 *
11  *                                                                          *
12  * The above copyright notice and this permission notice shall be included  *
13  * in all copies or substantial portions of the Software.                   *
14  *                                                                          *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
16  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
18  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
19  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
20  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
21  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
22  *                                                                          *
23  * Except as contained in this notice, the name(s) of the above copyright   *
24  * holders shall not be used in advertising or otherwise to promote the     *
25  * sale, use or other dealings in this Software without prior written       *
26  * authorization.                                                           *
27  ****************************************************************************/
28 
29 /***************************************************************************
30 *                                                                          *
31 *  Author : Juergen Pfeifer                                                *
32 *                                                                          *
33 ***************************************************************************/
34 
35 #include "form.priv.h"
36 
37 MODULE_ID("$Id: fty_regex.c,v 1.21 2007/10/13 19:33:50 tom Exp $")
38 
39 #if HAVE_REGEX_H_FUNCS		/* We prefer POSIX regex */
40 #include <regex.h>
41 
42 typedef struct
43   {
44     regex_t *pRegExp;
45     unsigned long *refCount;
46   }
47 RegExp_Arg;
48 
49 #elif HAVE_REGEXP_H_FUNCS | HAVE_REGEXPR_H_FUNCS
50 #undef RETURN
51 static int reg_errno;
52 
53 static char *
54 RegEx_Init(char *instring)
55 {
56   reg_errno = 0;
57   return instring;
58 }
59 
60 static char *
61 RegEx_Error(int code)
62 {
63   reg_errno = code;
64   return 0;
65 }
66 
67 #define INIT 		register char *sp = RegEx_Init(instring);
68 #define GETC()		(*sp++)
69 #define PEEKC()		(*sp)
70 #define UNGETC(c)	(--sp)
71 #define RETURN(c)	return(c)
72 #define ERROR(c)	return RegEx_Error(c)
73 
74 #if HAVE_REGEXP_H_FUNCS
75 #include <regexp.h>
76 #else
77 #include <regexpr.h>
78 #endif
79 
80 typedef struct
81 {
82   char *compiled_expression;
83   unsigned long *refCount;
84 }
85 RegExp_Arg;
86 
87 /* Maximum Length we allow for a compiled regular expression */
88 #define MAX_RX_LEN   (2048)
89 #define RX_INCREMENT (256)
90 
91 #endif
92 
93 /*---------------------------------------------------------------------------
94 |   Facility      :  libnform
95 |   Function      :  static void *Make_RegularExpression_Type(va_list * ap)
96 |
97 |   Description   :  Allocate structure for regex type argument.
98 |
99 |   Return Values :  Pointer to argument structure or NULL on error
100 +--------------------------------------------------------------------------*/
101 static void *
102 Make_RegularExpression_Type(va_list *ap)
103 {
104 #if HAVE_REGEX_H_FUNCS
105   char *rx = va_arg(*ap, char *);
106   RegExp_Arg *preg;
107 
108   preg = typeMalloc(RegExp_Arg, 1);
109 
110   if (preg)
111     {
112       T((T_CREATE("RegExp_Arg %p"), preg));
113       if (((preg->pRegExp = typeMalloc(regex_t, 1)) != 0)
114 	  && !regcomp(preg->pRegExp, rx,
115 		      (REG_EXTENDED | REG_NOSUB | REG_NEWLINE)))
116 	{
117 	  T((T_CREATE("regex_t %p"), preg->pRegExp));
118 	  preg->refCount = typeMalloc(unsigned long, 1);
119 
120 	  *(preg->refCount) = 1;
121 	}
122       else
123 	{
124 	  if (preg->pRegExp)
125 	    free(preg->pRegExp);
126 	  free(preg);
127 	  preg = (RegExp_Arg *)0;
128 	}
129     }
130   return ((void *)preg);
131 #elif HAVE_REGEXP_H_FUNCS | HAVE_REGEXPR_H_FUNCS
132   char *rx = va_arg(*ap, char *);
133   RegExp_Arg *pArg;
134 
135   pArg = typeMalloc(RegExp_Arg, 1);
136 
137   if (pArg)
138     {
139       int blen = RX_INCREMENT;
140 
141       T((T_CREATE("RegExp_Arg %p"), pArg));
142       pArg->compiled_expression = NULL;
143       pArg->refCount = typeMalloc(unsigned long, 1);
144 
145       *(pArg->refCount) = 1;
146 
147       do
148 	{
149 	  char *buf = typeMalloc(char, blen);
150 
151 	  if (buf)
152 	    {
153 #if HAVE_REGEXP_H_FUNCS
154 	      char *last_pos = compile(rx, buf, &buf[blen], '\0');
155 
156 #else /* HAVE_REGEXPR_H_FUNCS */
157 	      char *last_pos = compile(rx, buf, &buf[blen]);
158 #endif
159 	      if (reg_errno)
160 		{
161 		  free(buf);
162 		  if (reg_errno == 50)
163 		    blen += RX_INCREMENT;
164 		  else
165 		    {
166 		      free(pArg);
167 		      pArg = NULL;
168 		      break;
169 		    }
170 		}
171 	      else
172 		{
173 		  pArg->compiled_expression = buf;
174 		  break;
175 		}
176 	    }
177 	}
178       while (blen <= MAX_RX_LEN);
179     }
180   if (pArg && !pArg->compiled_expression)
181     {
182       free(pArg);
183       pArg = NULL;
184     }
185   return (void *)pArg;
186 #else
187   return 0;
188 #endif
189 }
190 
191 /*---------------------------------------------------------------------------
192 |   Facility      :  libnform
193 |   Function      :  static void *Copy_RegularExpression_Type(
194 |                                      const void * argp)
195 |
196 |   Description   :  Copy structure for regex type argument.
197 |
198 |   Return Values :  Pointer to argument structure or NULL on error.
199 +--------------------------------------------------------------------------*/
200 static void *
201 Copy_RegularExpression_Type(const void *argp)
202 {
203 #if (HAVE_REGEX_H_FUNCS | HAVE_REGEXP_H_FUNCS | HAVE_REGEXPR_H_FUNCS)
204   const RegExp_Arg *ap = (const RegExp_Arg *)argp;
205   const RegExp_Arg *result = (const RegExp_Arg *)0;
206 
207   if (ap)
208     {
209       *(ap->refCount) += 1;
210       result = ap;
211     }
212   return (void *)result;
213 #else
214   return 0;
215 #endif
216 }
217 
218 /*---------------------------------------------------------------------------
219 |   Facility      :  libnform
220 |   Function      :  static void Free_RegularExpression_Type(void * argp)
221 |
222 |   Description   :  Free structure for regex type argument.
223 |
224 |   Return Values :  -
225 +--------------------------------------------------------------------------*/
226 static void
227 Free_RegularExpression_Type(void *argp)
228 {
229 #if HAVE_REGEX_H_FUNCS | HAVE_REGEXP_H_FUNCS | HAVE_REGEXPR_H_FUNCS
230   RegExp_Arg *ap = (RegExp_Arg *)argp;
231 
232   if (ap)
233     {
234       if (--(*(ap->refCount)) == 0)
235 	{
236 #if HAVE_REGEX_H_FUNCS
237 	  if (ap->pRegExp)
238 	    {
239 	      free(ap->refCount);
240 	      regfree(ap->pRegExp);
241 	    }
242 #elif HAVE_REGEXP_H_FUNCS | HAVE_REGEXPR_H_FUNCS
243 	  if (ap->compiled_expression)
244 	    {
245 	      free(ap->refCount);
246 	      free(ap->compiled_expression);
247 	    }
248 #endif
249 	  free(ap);
250 	}
251     }
252 #endif
253 }
254 
255 /*---------------------------------------------------------------------------
256 |   Facility      :  libnform
257 |   Function      :  static bool Check_RegularExpression_Field(
258 |                                      FIELD * field,
259 |                                      const void  * argp)
260 |
261 |   Description   :  Validate buffer content to be a valid regular expression
262 |
263 |   Return Values :  TRUE  - field is valid
264 |                    FALSE - field is invalid
265 +--------------------------------------------------------------------------*/
266 static bool
267 Check_RegularExpression_Field(FIELD *field, const void *argp)
268 {
269   bool match = FALSE;
270 
271 #if HAVE_REGEX_H_FUNCS
272   const RegExp_Arg *ap = (const RegExp_Arg *)argp;
273 
274   if (ap && ap->pRegExp)
275     match = (regexec(ap->pRegExp, field_buffer(field, 0), 0, NULL, 0)
276 	     ? FALSE
277 	     : TRUE);
278 #elif HAVE_REGEXP_H_FUNCS | HAVE_REGEXPR_H_FUNCS
279   RegExp_Arg *ap = (RegExp_Arg *)argp;
280 
281   if (ap && ap->compiled_expression)
282     match = (step(field_buffer(field, 0), ap->compiled_expression)
283 	     ? TRUE
284 	     : FALSE);
285 #endif
286   return match;
287 }
288 
289 static FIELDTYPE typeREGEXP =
290 {
291   _HAS_ARGS | _RESIDENT,
292   1,				/* this is mutable, so we can't be const */
293   (FIELDTYPE *)0,
294   (FIELDTYPE *)0,
295   Make_RegularExpression_Type,
296   Copy_RegularExpression_Type,
297   Free_RegularExpression_Type,
298   Check_RegularExpression_Field,
299   NULL,
300   NULL,
301   NULL
302 };
303 
304 NCURSES_EXPORT_VAR(FIELDTYPE*) TYPE_REGEXP = &typeREGEXP;
305 
306 /* fty_regex.c ends here */
307