1 /****************************************************************************
2 * Copyright 2020,2021 Thomas E. Dickey *
3 * Copyright 1998-2009,2010 Free Software Foundation, Inc. *
5 * Permission is hereby granted, free of charge, to any person obtaining a *
6 * copy of this software and associated documentation files (the *
7 * "Software"), to deal in the Software without restriction, including *
8 * without limitation the rights to use, copy, modify, merge, publish, *
9 * distribute, distribute with modifications, sublicense, and/or sell *
10 * copies of the Software, and to permit persons to whom the Software is *
11 * furnished to do so, subject to the following conditions: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
24 * Except as contained in this notice, the name(s) of the above copyright *
25 * holders shall not be used in advertising or otherwise to promote the *
26 * sale, use or other dealings in this Software without prior written *
28 ****************************************************************************/
30 /***************************************************************************
32 * Author : Juergen Pfeifer *
34 ***************************************************************************/
36 #include "form.priv.h"
38 MODULE_ID("$Id: fty_enum.c,v 1.33 2021/06/17 21:11:08 tom Exp $")
57 /*---------------------------------------------------------------------------
59 | Function : static void *Generic_Enum_Type(void * arg)
61 | Description : Allocate structure for enumeration type argument.
63 | Return Values : Pointer to argument structure or NULL on error
64 +--------------------------------------------------------------------------*/
66 Generic_Enum_Type(void *arg)
68 enumARG *argp = (enumARG *)0;
69 enumParams *params = (enumParams *)arg;
73 argp = typeMalloc(enumARG, 1);
78 char **kp = (char **)0;
79 char **kwds = (char **)0;
82 T((T_CREATE("enumARG %p"), (void *)argp));
84 ccase = params->ccase;
85 cunique = params->cunique;
87 argp->checkcase = ccase ? TRUE : FALSE;
88 argp->checkunique = cunique ? TRUE : FALSE;
89 argp->kwds = (char **)0;
100 /* We copy the keywords, because we can't rely on the fact
101 that the caller doesn't relocate or free the memory used
102 for the keywords (maybe he has GC)
104 argp->kwds = typeMalloc(char *, cnt + 1);
107 if ((kptarget = argp->kwds) != 0)
111 (*kptarget++) = strdup(*kp++);
113 *kptarget = (char *)0;
121 /*---------------------------------------------------------------------------
122 | Facility : libnform
123 | Function : static void *Make_Enum_Type( va_list * ap )
125 | Description : Allocate structure for enumeration type argument.
127 | Return Values : Pointer to argument structure or NULL on error
128 +--------------------------------------------------------------------------*/
130 Make_Enum_Type(va_list *ap)
134 params.kwds = va_arg(*ap, char **);
135 params.ccase = va_arg(*ap, int);
136 params.cunique = va_arg(*ap, int);
138 return Generic_Enum_Type((void *)¶ms);
141 /*---------------------------------------------------------------------------
142 | Facility : libnform
143 | Function : static void *Copy_Enum_Type( const void * argp )
145 | Description : Copy structure for enumeration type argument.
147 | Return Values : Pointer to argument structure or NULL on error.
148 +--------------------------------------------------------------------------*/
150 Copy_Enum_Type(const void *argp)
152 enumARG *result = (enumARG *)0;
156 const enumARG *ap = (const enumARG *)argp;
158 result = typeMalloc(enumARG, 1);
162 T((T_CREATE("enumARG %p"), (void *)result));
168 char **kp = ap->kwds;
169 result->kwds = typeMalloc(char *, 1 + ap->count);
171 if ((kptarget = result->kwds) != 0)
175 (*kptarget++) = strdup(*kp++);
177 *kptarget = (char *)0;
182 return (void *)result;
185 /*---------------------------------------------------------------------------
186 | Facility : libnform
187 | Function : static void Free_Enum_Type( void * argp )
189 | Description : Free structure for enumeration type argument.
192 +--------------------------------------------------------------------------*/
194 Free_Enum_Type(void *argp)
198 const enumARG *ap = (const enumARG *)argp;
200 if (ap->kwds && ap->count > 0)
202 char **kp = ap->kwds;
210 assert(cnt == ap->count);
217 #define SKIP_SPACE(x) while(((*(x))!='\0') && (is_blank(*(x)))) (x)++
222 /*---------------------------------------------------------------------------
223 | Facility : libnform
224 | Function : static int Compare(const unsigned char * s,
225 | const unsigned char * buf,
228 | Description : Check whether or not the text in 'buf' matches the
229 | text in 's', at least partial.
231 | Return Values : NOMATCH - buffer doesn't match
232 | PARTIAL - buffer matches partially
233 | EXACT - buffer matches exactly
234 +--------------------------------------------------------------------------*/
236 Compare(const unsigned char *s, const unsigned char *buf,
239 SKIP_SPACE(buf); /* Skip leading spaces in both texts */
244 return (((*s) != '\0') ? NOMATCH : EXACT);
258 while (toupper(*s++) == toupper(*buf))
265 /* At this location buf points to the first character where it no longer
266 matches with s. So if only blanks are following, we have a partial
267 match otherwise there is no match */
272 /* If it happens that the reference buffer is at its end, the partial
273 match is actually an exact match. */
274 return ((s[-1] != '\0') ? PARTIAL : EXACT);
277 /*---------------------------------------------------------------------------
278 | Facility : libnform
279 | Function : static bool Check_Enum_Field(
283 | Description : Validate buffer content to be a valid enumeration value
285 | Return Values : TRUE - field is valid
286 | FALSE - field is invalid
287 +--------------------------------------------------------------------------*/
289 Check_Enum_Field(FIELD *field, const void *argp)
291 char **kwds = ((const enumARG *)argp)->kwds;
292 bool ccase = ((const enumARG *)argp)->checkcase;
293 bool unique = ((const enumARG *)argp)->checkunique;
294 unsigned char *bp = (unsigned char *)field_buffer(field, 0);
297 while (kwds && (s = (*kwds++)))
301 if ((res = Compare((unsigned char *)s, bp, ccase)) != NOMATCH)
303 p = t = s; /* t is at least a partial match */
304 if ((unique && res != EXACT))
306 while (kwds && (p = *kwds++))
308 if ((res = Compare((unsigned char *)p, bp, ccase)) != NOMATCH)
322 set_field_buffer(field, 0, t);
332 static const char *dummy[] =
335 /*---------------------------------------------------------------------------
336 | Facility : libnform
337 | Function : static bool Next_Enum(FIELD * field,
340 | Description : Check for the next enumeration value
342 | Return Values : TRUE - next value found and loaded
343 | FALSE - no next value loaded
344 +--------------------------------------------------------------------------*/
346 Next_Enum(FIELD *field, const void *argp)
348 const enumARG *args = (const enumARG *)argp;
349 char **kwds = args->kwds;
350 bool ccase = args->checkcase;
351 int cnt = args->count;
352 unsigned char *bp = (unsigned char *)field_buffer(field, 0);
358 if (Compare((unsigned char *)(*kwds++), bp, ccase) == EXACT)
363 if ((cnt >= 0) || (Compare((const unsigned char *)dummy, bp, ccase) == EXACT))
365 set_field_buffer(field, 0, *kwds);
372 /*---------------------------------------------------------------------------
373 | Facility : libnform
374 | Function : static bool Previous_Enum(
378 | Description : Check for the previous enumeration value
380 | Return Values : TRUE - previous value found and loaded
381 | FALSE - no previous value loaded
382 +--------------------------------------------------------------------------*/
384 Previous_Enum(FIELD *field, const void *argp)
386 const enumARG *args = (const enumARG *)argp;
387 int cnt = args->count;
388 char **kwds = &args->kwds[cnt - 1];
389 bool ccase = args->checkcase;
390 unsigned char *bp = (unsigned char *)field_buffer(field, 0);
396 if (Compare((unsigned char *)(*kwds--), bp, ccase) == EXACT)
401 kwds = &args->kwds[args->count - 1];
403 if ((cnt >= 0) || (Compare((const unsigned char *)dummy, bp, ccase) == EXACT))
405 set_field_buffer(field, 0, *kwds);
412 static FIELDTYPE typeENUM =
414 _HAS_ARGS | _HAS_CHOICE | _RESIDENT,
415 1, /* this is mutable, so we can't be const */
421 INIT_FT_FUNC(Check_Enum_Field),
423 INIT_FT_FUNC(Next_Enum),
424 INIT_FT_FUNC(Previous_Enum),
425 #if NCURSES_INTEROP_FUNCS
430 FORM_EXPORT_VAR(FIELDTYPE *) TYPE_ENUM = &typeENUM;
432 #if NCURSES_INTEROP_FUNCS
433 /* The next routines are to simplify the use of ncurses from
434 programming languages with restrictions on interop with C level
435 constructs (e.g. variable access or va_list + ellipsis constructs)
437 FORM_EXPORT(FIELDTYPE *)
444 /* fty_enum.c ends here */