1 /* $OpenBSD: fty_alnum.c,v 1.10 2023/10/17 09:52:10 nicm Exp $ */
2 /****************************************************************************
3 * Copyright 2020 Thomas E. Dickey *
4 * Copyright 1998-2009,2010 Free Software Foundation, Inc. *
5 * *
6 * Permission is hereby granted, free of charge, to any person obtaining a *
7 * copy of this software and associated documentation files (the *
8 * "Software"), to deal in the Software without restriction, including *
9 * without limitation the rights to use, copy, modify, merge, publish, *
10 * distribute, distribute with modifications, sublicense, and/or sell *
11 * copies of the Software, and to permit persons to whom the Software is *
12 * furnished to do so, subject to the following conditions: *
13 * *
14 * The above copyright notice and this permission notice shall be included *
15 * in all copies or substantial portions of the Software. *
16 * *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
20 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
21 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
22 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
23 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
24 * *
25 * Except as contained in this notice, the name(s) of the above copyright *
26 * holders shall not be used in advertising or otherwise to promote the *
27 * sale, use or other dealings in this Software without prior written *
28 * authorization. *
29 ****************************************************************************/
30
31 /***************************************************************************
32 * *
33 * Author : Juergen Pfeifer *
34 * *
35 ***************************************************************************/
36
37 #include "form.priv.h"
38
39 MODULE_ID("$Id: fty_alnum.c,v 1.10 2023/10/17 09:52:10 nicm Exp $")
40
41 #define thisARG alnumARG
42
43 typedef struct
44 {
45 int width;
46 }
47 thisARG;
48
49 /*---------------------------------------------------------------------------
50 | Facility : libnform
51 | Function : static void *Generic_This_Type(void *arg)
52 |
53 | Description : Allocate structure for alphanumeric type argument.
54 |
55 | Return Values : Pointer to argument structure or NULL on error
56 +--------------------------------------------------------------------------*/
57 static void *
Generic_This_Type(void * arg)58 Generic_This_Type(void *arg)
59 {
60 thisARG *argp = (thisARG *)0;
61
62 if (arg)
63 {
64 argp = typeMalloc(thisARG, 1);
65
66 if (argp)
67 {
68 T((T_CREATE("thisARG %p"), (void *)argp));
69 argp->width = *((int *)arg);
70 }
71 }
72 return ((void *)argp);
73 }
74
75 /*---------------------------------------------------------------------------
76 | Facility : libnform
77 | Function : static void *Make_This_Type(va_list *ap)
78 |
79 | Description : Allocate structure for alphanumeric type argument.
80 |
81 | Return Values : Pointer to argument structure or NULL on error
82 +--------------------------------------------------------------------------*/
83 static void *
Make_This_Type(va_list * ap)84 Make_This_Type(va_list *ap)
85 {
86 int w = va_arg(*ap, int);
87
88 return Generic_This_Type((void *)&w);
89 }
90
91 /*---------------------------------------------------------------------------
92 | Facility : libnform
93 | Function : static void *Copy_ThisType(const void *argp)
94 |
95 | Description : Copy structure for alphanumeric type argument.
96 |
97 | Return Values : Pointer to argument structure or NULL on error.
98 +--------------------------------------------------------------------------*/
99 static void *
Copy_This_Type(const void * argp)100 Copy_This_Type(const void *argp)
101 {
102 const thisARG *ap = (const thisARG *)argp;
103 thisARG *result = typeMalloc(thisARG, 1);
104
105 if (result)
106 {
107 T((T_CREATE("thisARG %p"), (void *)result));
108 *result = *ap;
109 }
110
111 return ((void *)result);
112 }
113
114 /*---------------------------------------------------------------------------
115 | Facility : libnform
116 | Function : static void Free_This_Type(void *argp)
117 |
118 | Description : Free structure for alphanumeric type argument.
119 |
120 | Return Values : -
121 +--------------------------------------------------------------------------*/
122 static void
Free_This_Type(void * argp)123 Free_This_Type(void *argp)
124 {
125 if (argp)
126 free(argp);
127 }
128
129 /*---------------------------------------------------------------------------
130 | Facility : libnform
131 | Function : static bool Check_This_Character(
132 | int c,
133 | const void *argp)
134 |
135 | Description : Check a character for the alphanumeric type.
136 |
137 | Return Values : TRUE - character is valid
138 | FALSE - character is invalid
139 +--------------------------------------------------------------------------*/
140 static bool
Check_This_Character(int c,const void * argp GCC_UNUSED)141 Check_This_Character(int c, const void *argp GCC_UNUSED)
142 {
143 #if USE_WIDEC_SUPPORT
144 if (iswalnum((wint_t)c))
145 return TRUE;
146 #endif
147 return (isalnum(UChar(c)) ? TRUE : FALSE);
148 }
149
150 /*---------------------------------------------------------------------------
151 | Facility : libnform
152 | Function : static bool Check_This_Field(
153 | FIELD *field,
154 | const void *argp)
155 |
156 | Description : Validate buffer content to be a valid alphanumeric value
157 |
158 | Return Values : TRUE - field is valid
159 | FALSE - field is invalid
160 +--------------------------------------------------------------------------*/
161 static bool
Check_This_Field(FIELD * field,const void * argp)162 Check_This_Field(FIELD *field, const void *argp)
163 {
164 int width = ((const thisARG *)argp)->width;
165 unsigned char *bp = (unsigned char *)field_buffer(field, 0);
166 bool result = (width < 0);
167
168 Check_CTYPE_Field(result, bp, width, Check_This_Character);
169 return (result);
170 }
171
172 static FIELDTYPE typeTHIS =
173 {
174 _HAS_ARGS | _RESIDENT,
175 1, /* this is mutable, so we can't be const */
176 (FIELDTYPE *)0,
177 (FIELDTYPE *)0,
178 Make_This_Type,
179 Copy_This_Type,
180 Free_This_Type,
181 INIT_FT_FUNC(Check_This_Field),
182 INIT_FT_FUNC(Check_This_Character),
183 INIT_FT_FUNC(NULL),
184 INIT_FT_FUNC(NULL),
185 #if NCURSES_INTEROP_FUNCS
186 Generic_This_Type
187 #endif
188 };
189
190 FORM_EXPORT_VAR(FIELDTYPE *) TYPE_ALNUM = &typeTHIS;
191
192 #if NCURSES_INTEROP_FUNCS
193 /* The next routines are to simplify the use of ncurses from
194 programming languages with restrictions on interop with C level
195 constructs (e.g. variable access or va_list + ellipsis constructs)
196 */
197 FORM_EXPORT(FIELDTYPE *)
_nc_TYPE_ALNUM(void)198 _nc_TYPE_ALNUM(void)
199 {
200 return TYPE_ALNUM;
201 }
202 #endif
203
204 /* fty_alnum.c ends here */
205