1 /****************************************************************************
2 * Copyright 2020 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_alnum.c,v 1.29 2020/12/12 01:15:37 tom Exp $")
40 #define thisARG alnumARG
48 /*---------------------------------------------------------------------------
50 | Function : static void *Generic_This_Type(void *arg)
52 | Description : Allocate structure for alphanumeric type argument.
54 | Return Values : Pointer to argument structure or NULL on error
55 +--------------------------------------------------------------------------*/
57 Generic_This_Type(void *arg)
59 thisARG *argp = (thisARG *)0;
63 argp = typeMalloc(thisARG, 1);
67 T((T_CREATE("thisARG %p"), (void *)argp));
68 argp->width = *((int *)arg);
71 return ((void *)argp);
74 /*---------------------------------------------------------------------------
76 | Function : static void *Make_This_Type(va_list *ap)
78 | Description : Allocate structure for alphanumeric type argument.
80 | Return Values : Pointer to argument structure or NULL on error
81 +--------------------------------------------------------------------------*/
83 Make_This_Type(va_list *ap)
85 int w = va_arg(*ap, int);
87 return Generic_This_Type((void *)&w);
90 /*---------------------------------------------------------------------------
92 | Function : static void *Copy_ThisType(const void *argp)
94 | Description : Copy structure for alphanumeric type argument.
96 | Return Values : Pointer to argument structure or NULL on error.
97 +--------------------------------------------------------------------------*/
99 Copy_This_Type(const void *argp)
101 const thisARG *ap = (const thisARG *)argp;
102 thisARG *result = typeMalloc(thisARG, 1);
106 T((T_CREATE("thisARG %p"), (void *)result));
110 return ((void *)result);
113 /*---------------------------------------------------------------------------
114 | Facility : libnform
115 | Function : static void Free_This_Type(void *argp)
117 | Description : Free structure for alphanumeric type argument.
120 +--------------------------------------------------------------------------*/
122 Free_This_Type(void *argp)
128 /*---------------------------------------------------------------------------
129 | Facility : libnform
130 | Function : static bool Check_This_Character(
134 | Description : Check a character for the alphanumeric type.
136 | Return Values : TRUE - character is valid
137 | FALSE - character is invalid
138 +--------------------------------------------------------------------------*/
140 Check_This_Character(int c, const void *argp GCC_UNUSED)
142 #if USE_WIDEC_SUPPORT
143 if (iswalnum((wint_t)c))
146 return (isalnum(UChar(c)) ? TRUE : FALSE);
149 /*---------------------------------------------------------------------------
150 | Facility : libnform
151 | Function : static bool Check_This_Field(
155 | Description : Validate buffer content to be a valid alphanumeric value
157 | Return Values : TRUE - field is valid
158 | FALSE - field is invalid
159 +--------------------------------------------------------------------------*/
161 Check_This_Field(FIELD *field, const void *argp)
163 int width = ((const thisARG *)argp)->width;
164 unsigned char *bp = (unsigned char *)field_buffer(field, 0);
165 bool result = (width < 0);
167 Check_CTYPE_Field(result, bp, width, Check_This_Character);
171 static FIELDTYPE typeTHIS =
173 _HAS_ARGS | _RESIDENT,
174 1, /* this is mutable, so we can't be const */
180 INIT_FT_FUNC(Check_This_Field),
181 INIT_FT_FUNC(Check_This_Character),
184 #if NCURSES_INTEROP_FUNCS
189 FORM_EXPORT_VAR(FIELDTYPE *) TYPE_ALNUM = &typeTHIS;
191 #if NCURSES_INTEROP_FUNCS
192 /* The next routines are to simplify the use of ncurses from
193 programming languages with restrictions on interop with C level
194 constructs (e.g. variable access or va_list + ellipsis constructs)
196 FORM_EXPORT(FIELDTYPE *)
203 /* fty_alnum.c ends here */