Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Kitware
GitHub Repository: Kitware/CMake
Path: blob/master/Source/CursesDialog/form/fty_regex.c
5017 views
1
2
/*
3
* THIS CODE IS SPECIFICALLY EXEMPTED FROM THE NCURSES PACKAGE COPYRIGHT.
4
* You may freely copy it for use as a template for your own field types.
5
* If you develop a field type that might be of general use, please send
6
* it back to the ncurses maintainers for inclusion in the next version.
7
*/
8
/***************************************************************************
9
* *
10
* Author : Juergen Pfeifer, [email protected] *
11
* *
12
***************************************************************************/
13
14
#include "form.priv.h"
15
16
MODULE_ID("$Id$")
17
18
#if HAVE_REGEX_H_FUNCS /* We prefer POSIX regex */
19
#include <regex.h>
20
21
typedef struct
22
{
23
regex_t *pRegExp;
24
unsigned long *refCount;
25
} RegExp_Arg;
26
27
#elif HAVE_REGEXP_H_FUNCS | HAVE_REGEXPR_H_FUNCS
28
#undef RETURN
29
static int reg_errno;
30
31
static char *RegEx_Init(char *instring)
32
{
33
reg_errno = 0;
34
return instring;
35
}
36
37
static char *RegEx_Error(int code)
38
{
39
reg_errno = code;
40
return 0;
41
}
42
43
#define INIT char *sp = RegEx_Init(instring);
44
#define GETC() (*sp++)
45
#define PEEKC() (*sp)
46
#define UNGETC(c) (--sp)
47
#define RETURN(c) return(c)
48
#define ERROR(c) return RegEx_Error(c)
49
50
#if HAVE_REGEXP_H_FUNCS
51
#include <regexp.h>
52
#else
53
#include <regexpr.h>
54
#endif
55
56
typedef struct
57
{
58
char *compiled_expression;
59
unsigned long *refCount;
60
} RegExp_Arg;
61
62
/* Maximum Length we allow for a compiled regular expression */
63
#define MAX_RX_LEN (2048)
64
#define RX_INCREMENT (256)
65
66
#endif
67
68
/*---------------------------------------------------------------------------
69
| Facility : libnform
70
| Function : static void *Make_RegularExpression_Type(va_list * ap)
71
|
72
| Description : Allocate structure for regex type argument.
73
|
74
| Return Values : Pointer to argument structure or NULL on error
75
+--------------------------------------------------------------------------*/
76
static void *Make_RegularExpression_Type(va_list * ap)
77
{
78
#if HAVE_REGEX_H_FUNCS
79
char *rx = va_arg(*ap,char *);
80
RegExp_Arg *preg;
81
82
preg = (RegExp_Arg*)malloc(sizeof(RegExp_Arg));
83
if (preg)
84
{
85
if (((preg->pRegExp = (regex_t*)malloc(sizeof(regex_t))) != (regex_t*)0)
86
&& !regcomp(preg->pRegExp,rx,
87
(REG_EXTENDED | REG_NOSUB | REG_NEWLINE) ))
88
{
89
preg->refCount = (unsigned long *)malloc(sizeof(unsigned long));
90
*(preg->refCount) = 1;
91
}
92
else
93
{
94
if (preg->pRegExp)
95
free(preg->pRegExp);
96
free(preg);
97
preg = (RegExp_Arg*)0;
98
}
99
}
100
return((void *)preg);
101
#elif HAVE_REGEXP_H_FUNCS | HAVE_REGEXPR_H_FUNCS
102
char *rx = va_arg(*ap,char *);
103
RegExp_Arg *pArg;
104
105
pArg = (RegExp_Arg *)malloc(sizeof(RegExp_Arg));
106
107
if (pArg)
108
{
109
int blen = RX_INCREMENT;
110
pArg->compiled_expression = NULL;
111
pArg->refCount = (unsigned long *)malloc(sizeof(unsigned long));
112
*(pArg->refCount) = 1;
113
114
do {
115
char *buf = (char *)malloc(blen);
116
if (buf)
117
{
118
#if HAVE_REGEXP_H_FUNCS
119
char *last_pos = compile (rx, buf, &buf[blen], '\0');
120
#else /* HAVE_REGEXPR_H_FUNCS */
121
char *last_pos = compile (rx, buf, &buf[blen]);
122
#endif
123
if (reg_errno)
124
{
125
free(buf);
126
if (reg_errno==50)
127
blen += RX_INCREMENT;
128
else
129
{
130
free(pArg);
131
pArg = NULL;
132
break;
133
}
134
}
135
else
136
{
137
pArg->compiled_expression = buf;
138
break;
139
}
140
}
141
} while( blen <= MAX_RX_LEN );
142
}
143
if (pArg && !pArg->compiled_expression)
144
{
145
free(pArg);
146
pArg = NULL;
147
}
148
return (void *)pArg;
149
#else
150
ap=0; /* Silence unused parameter warning. */
151
return 0;
152
#endif
153
}
154
155
/*---------------------------------------------------------------------------
156
| Facility : libnform
157
| Function : static void *Copy_RegularExpression_Type(
158
| const void * argp)
159
|
160
| Description : Copy structure for regex type argument.
161
|
162
| Return Values : Pointer to argument structure or NULL on error.
163
+--------------------------------------------------------------------------*/
164
static void *Copy_RegularExpression_Type(const void * argp)
165
{
166
#if (HAVE_REGEX_H_FUNCS | HAVE_REGEXP_H_FUNCS | HAVE_REGEXPR_H_FUNCS)
167
const RegExp_Arg *ap = (const RegExp_Arg *)argp;
168
const RegExp_Arg *result = (const RegExp_Arg *)0;
169
170
if (ap)
171
{
172
*(ap->refCount) += 1;
173
result = ap;
174
}
175
return (void *)result;
176
#else
177
argp=0; /* Silence unused parameter warning. */
178
return 0;
179
#endif
180
}
181
182
/*---------------------------------------------------------------------------
183
| Facility : libnform
184
| Function : static void Free_RegularExpression_Type(void * argp)
185
|
186
| Description : Free structure for regex type argument.
187
|
188
| Return Values : -
189
+--------------------------------------------------------------------------*/
190
static void Free_RegularExpression_Type(void * argp)
191
{
192
#if HAVE_REGEX_H_FUNCS | HAVE_REGEXP_H_FUNCS | HAVE_REGEXPR_H_FUNCS
193
RegExp_Arg *ap = (RegExp_Arg *)argp;
194
if (ap)
195
{
196
if (--(*(ap->refCount)) == 0)
197
{
198
#if HAVE_REGEX_H_FUNCS
199
if (ap->pRegExp)
200
{
201
free(ap->refCount);
202
regfree(ap->pRegExp);
203
}
204
#elif HAVE_REGEXP_H_FUNCS | HAVE_REGEXPR_H_FUNCS
205
if (ap->compiled_expression)
206
{
207
free(ap->refCount);
208
free(ap->compiled_expression);
209
}
210
#endif
211
free(ap);
212
}
213
}
214
#else
215
argp=0; /* Silence unused parameter warning. */
216
#endif
217
}
218
219
/*---------------------------------------------------------------------------
220
| Facility : libnform
221
| Function : static bool Check_RegularExpression_Field(
222
| FIELD * field,
223
| const void * argp)
224
|
225
| Description : Validate buffer content to be a valid regular expression
226
|
227
| Return Values : TRUE - field is valid
228
| FALSE - field is invalid
229
+--------------------------------------------------------------------------*/
230
static bool Check_RegularExpression_Field(FIELD * field, const void * argp)
231
{
232
bool match = FALSE;
233
#if HAVE_REGEX_H_FUNCS
234
const RegExp_Arg *ap = (const RegExp_Arg*)argp;
235
if (ap && ap->pRegExp)
236
match = (regexec(ap->pRegExp,field_buffer(field,0),0,NULL,0) ? FALSE:TRUE);
237
#elif HAVE_REGEXP_H_FUNCS | HAVE_REGEXPR_H_FUNCS
238
RegExp_Arg *ap = (RegExp_Arg *)argp;
239
if (ap && ap->compiled_expression)
240
match = (step(field_buffer(field,0),ap->compiled_expression) ? TRUE:FALSE);
241
#else
242
argp=0; /* Silence unused parameter warning. */
243
field=0; /* Silence unused parameter warning. */
244
#endif
245
return match;
246
}
247
248
static FIELDTYPE typeREGEXP = {
249
_HAS_ARGS | _RESIDENT,
250
1, /* this is mutable, so we can't be const */
251
(FIELDTYPE *)0,
252
(FIELDTYPE *)0,
253
Make_RegularExpression_Type,
254
Copy_RegularExpression_Type,
255
Free_RegularExpression_Type,
256
Check_RegularExpression_Field,
257
NULL,
258
NULL,
259
NULL
260
};
261
262
FIELDTYPE* TYPE_REGEXP = &typeREGEXP;
263
264
/* fty_regex.c ends here */
265
266