fm/ipmitopo: fix 64-bit compilation
[unleashed.git] / contrib / ncurses / form / fty_enum.c
blobd3351644ef1849f2aa9de6dfd7ab31674cc08e8c
1 /****************************************************************************
2 * Copyright (c) 1998-2009,2010 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 ****************************************************************************/
29 /***************************************************************************
30 * *
31 * Author : Juergen Pfeifer *
32 * *
33 ***************************************************************************/
35 #include "form.priv.h"
37 MODULE_ID("$Id: fty_enum.c,v 1.26 2010/05/01 21:11:07 tom Exp $")
39 typedef struct
41 char **kwds;
42 int count;
43 bool checkcase;
44 bool checkunique;
46 enumARG;
48 typedef struct
50 char **kwds;
51 int ccase;
52 int cunique;
54 enumParams;
56 /*---------------------------------------------------------------------------
57 | Facility : libnform
58 | Function : static void *Generic_Enum_Type(void * arg)
60 | Description : Allocate structure for enumeration type argument.
62 | Return Values : Pointer to argument structure or NULL on error
63 +--------------------------------------------------------------------------*/
64 static void *
65 Generic_Enum_Type(void *arg)
67 enumARG *argp = (enumARG *)0;
68 enumParams *params = (enumParams *) arg;
70 if (params)
72 argp = typeMalloc(enumARG, 1);
74 if (argp)
76 int cnt = 0;
77 char **kp = (char **)0;
78 char **kwds = (char **)0;
79 char **kptarget;
80 int ccase, cunique;
82 T((T_CREATE("enumARG %p"), (void *)argp));
83 kwds = params->kwds;
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;
91 kp = kwds;
92 while (kp && (*kp++))
93 cnt++;
94 argp->count = cnt;
96 if (cnt > 0)
98 /* We copy the keywords, because we can't rely on the fact
99 that the caller doesn't relocate or free the memory used
100 for the keywords (maybe he has GC)
102 argp->kwds = typeMalloc(char *, cnt + 1);
104 kp = kwds;
105 if ((kptarget = argp->kwds) != 0)
107 while (kp && (*kp))
109 (*kptarget++) = strdup(*kp++);
111 *kptarget = (char *)0;
116 return (void *)argp;
119 /*---------------------------------------------------------------------------
120 | Facility : libnform
121 | Function : static void *Make_Enum_Type( va_list * ap )
123 | Description : Allocate structure for enumeration type argument.
125 | Return Values : Pointer to argument structure or NULL on error
126 +--------------------------------------------------------------------------*/
127 static void *
128 Make_Enum_Type(va_list *ap)
130 enumParams params;
132 params.kwds = va_arg(*ap, char **);
133 params.ccase = va_arg(*ap, int);
134 params.cunique = va_arg(*ap, int);
136 return Generic_Enum_Type((void *)&params);
139 /*---------------------------------------------------------------------------
140 | Facility : libnform
141 | Function : static void *Copy_Enum_Type( const void * argp )
143 | Description : Copy structure for enumeration type argument.
145 | Return Values : Pointer to argument structure or NULL on error.
146 +--------------------------------------------------------------------------*/
147 static void *
148 Copy_Enum_Type(const void *argp)
150 enumARG *result = (enumARG *)0;
152 if (argp)
154 const enumARG *ap = (const enumARG *)argp;
156 result = typeMalloc(enumARG, 1);
158 if (result)
160 T((T_CREATE("enumARG %p"), (void *)result));
161 *result = *ap;
163 if (ap->count > 0)
165 char **kptarget;
166 char **kp = ap->kwds;
167 result->kwds = typeMalloc(char *, 1 + ap->count);
169 if ((kptarget = result->kwds) != 0)
171 while (kp && (*kp))
173 (*kptarget++) = strdup(*kp++);
175 *kptarget = (char *)0;
180 return (void *)result;
183 /*---------------------------------------------------------------------------
184 | Facility : libnform
185 | Function : static void Free_Enum_Type( void * argp )
187 | Description : Free structure for enumeration type argument.
189 | Return Values : -
190 +--------------------------------------------------------------------------*/
191 static void
192 Free_Enum_Type(void *argp)
194 if (argp)
196 const enumARG *ap = (const enumARG *)argp;
198 if (ap->kwds && ap->count > 0)
200 char **kp = ap->kwds;
201 int cnt = 0;
203 while (kp && (*kp))
205 free(*kp++);
206 cnt++;
208 assert(cnt == ap->count);
209 free(ap->kwds);
211 free(argp);
215 #define SKIP_SPACE(x) while(((*(x))!='\0') && (is_blank(*(x)))) (x)++
216 #define NOMATCH 0
217 #define PARTIAL 1
218 #define EXACT 2
220 /*---------------------------------------------------------------------------
221 | Facility : libnform
222 | Function : static int Compare(const unsigned char * s,
223 | const unsigned char * buf,
224 | bool ccase )
226 | Description : Check whether or not the text in 'buf' matches the
227 | text in 's', at least partial.
229 | Return Values : NOMATCH - buffer doesn't match
230 | PARTIAL - buffer matches partially
231 | EXACT - buffer matches exactly
232 +--------------------------------------------------------------------------*/
233 static int
234 Compare(const unsigned char *s, const unsigned char *buf,
235 bool ccase)
237 SKIP_SPACE(buf); /* Skip leading spaces in both texts */
238 SKIP_SPACE(s);
240 if (*buf == '\0')
242 return (((*s) != '\0') ? NOMATCH : EXACT);
244 else
246 if (ccase)
248 while (*s++ == *buf)
250 if (*buf++ == '\0')
251 return EXACT;
254 else
256 while (toupper(*s++) == toupper(*buf))
258 if (*buf++ == '\0')
259 return EXACT;
263 /* At this location buf points to the first character where it no longer
264 matches with s. So if only blanks are following, we have a partial
265 match otherwise there is no match */
266 SKIP_SPACE(buf);
267 if (*buf)
268 return NOMATCH;
270 /* If it happens that the reference buffer is at its end, the partial
271 match is actually an exact match. */
272 return ((s[-1] != '\0') ? PARTIAL : EXACT);
275 /*---------------------------------------------------------------------------
276 | Facility : libnform
277 | Function : static bool Check_Enum_Field(
278 | FIELD * field,
279 | const void * argp)
281 | Description : Validate buffer content to be a valid enumeration value
283 | Return Values : TRUE - field is valid
284 | FALSE - field is invalid
285 +--------------------------------------------------------------------------*/
286 static bool
287 Check_Enum_Field(FIELD *field, const void *argp)
289 char **kwds = ((const enumARG *)argp)->kwds;
290 bool ccase = ((const enumARG *)argp)->checkcase;
291 bool unique = ((const enumARG *)argp)->checkunique;
292 unsigned char *bp = (unsigned char *)field_buffer(field, 0);
293 char *s, *t, *p;
294 int res;
296 while (kwds && (s = (*kwds++)))
298 if ((res = Compare((unsigned char *)s, bp, ccase)) != NOMATCH)
300 p = t = s; /* t is at least a partial match */
301 if ((unique && res != EXACT))
303 while (kwds && (p = *kwds++))
305 if ((res = Compare((unsigned char *)p, bp, ccase)) != NOMATCH)
307 if (res == EXACT)
309 t = p;
310 break;
312 else
313 t = (char *)0;
317 if (t)
319 set_field_buffer(field, 0, t);
320 return TRUE;
322 if (!p)
323 break;
326 return FALSE;
329 static const char *dummy[] =
330 {(char *)0};
332 /*---------------------------------------------------------------------------
333 | Facility : libnform
334 | Function : static bool Next_Enum(FIELD * field,
335 | const void * argp)
337 | Description : Check for the next enumeration value
339 | Return Values : TRUE - next value found and loaded
340 | FALSE - no next value loaded
341 +--------------------------------------------------------------------------*/
342 static bool
343 Next_Enum(FIELD *field, const void *argp)
345 const enumARG *args = (const enumARG *)argp;
346 char **kwds = args->kwds;
347 bool ccase = args->checkcase;
348 int cnt = args->count;
349 unsigned char *bp = (unsigned char *)field_buffer(field, 0);
351 if (kwds)
353 while (cnt--)
355 if (Compare((unsigned char *)(*kwds++), bp, ccase) == EXACT)
356 break;
358 if (cnt <= 0)
359 kwds = args->kwds;
360 if ((cnt >= 0) || (Compare((const unsigned char *)dummy, bp, ccase) == EXACT))
362 set_field_buffer(field, 0, *kwds);
363 return TRUE;
366 return FALSE;
369 /*---------------------------------------------------------------------------
370 | Facility : libnform
371 | Function : static bool Previous_Enum(
372 | FIELD * field,
373 | const void * argp)
375 | Description : Check for the previous enumeration value
377 | Return Values : TRUE - previous value found and loaded
378 | FALSE - no previous value loaded
379 +--------------------------------------------------------------------------*/
380 static bool
381 Previous_Enum(FIELD *field, const void *argp)
383 const enumARG *args = (const enumARG *)argp;
384 int cnt = args->count;
385 char **kwds = &args->kwds[cnt - 1];
386 bool ccase = args->checkcase;
387 unsigned char *bp = (unsigned char *)field_buffer(field, 0);
389 if (kwds)
391 while (cnt--)
393 if (Compare((unsigned char *)(*kwds--), bp, ccase) == EXACT)
394 break;
397 if (cnt <= 0)
398 kwds = &args->kwds[args->count - 1];
400 if ((cnt >= 0) || (Compare((const unsigned char *)dummy, bp, ccase) == EXACT))
402 set_field_buffer(field, 0, *kwds);
403 return TRUE;
406 return FALSE;
409 static FIELDTYPE typeENUM =
411 _HAS_ARGS | _HAS_CHOICE | _RESIDENT,
412 1, /* this is mutable, so we can't be const */
413 (FIELDTYPE *)0,
414 (FIELDTYPE *)0,
415 Make_Enum_Type,
416 Copy_Enum_Type,
417 Free_Enum_Type,
418 INIT_FT_FUNC(Check_Enum_Field),
419 INIT_FT_FUNC(NULL),
420 INIT_FT_FUNC(Next_Enum),
421 INIT_FT_FUNC(Previous_Enum),
422 #if NCURSES_INTEROP_FUNCS
423 Generic_Enum_Type
424 #endif
427 NCURSES_EXPORT_VAR(FIELDTYPE *)
428 TYPE_ENUM = &typeENUM;
430 #if NCURSES_INTEROP_FUNCS
431 /* The next routines are to simplify the use of ncurses from
432 programming languages with restictions on interop with C level
433 constructs (e.g. variable access or va_list + ellipsis constructs)
435 NCURSES_EXPORT(FIELDTYPE *)
436 _nc_TYPE_ENUM(void)
438 return TYPE_ENUM;
440 #endif
442 /* fty_enum.c ends here */