1 /* $OpenBSD: fty_int.c,v 1.10 2023/10/17 09:52:10 nicm Exp $ */
2 /****************************************************************************
3 * Copyright 2020,2021 Thomas E. Dickey *
4 * Copyright 1998-2010,2012 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_int.c,v 1.10 2023/10/17 09:52:10 nicm Exp $")
40
41 #if USE_WIDEC_SUPPORT
42 #define isDigit(c) (iswdigit((wint_t)(c)) || isdigit(UChar(c)))
43 #else
44 #define isDigit(c) isdigit(UChar(c))
45 #endif
46
47 #define thisARG integerARG
48
49 typedef struct
50 {
51 int precision;
52 long low;
53 long high;
54 }
55 thisARG;
56
57 typedef struct
58 {
59 int precision;
60 long low;
61 long high;
62 }
63 integerPARM;
64
65 /*---------------------------------------------------------------------------
66 | Facility : libnform
67 | Function : static void *Generic_This_Type( void * arg )
68 |
69 | Description : Allocate structure for integer type argument.
70 |
71 | Return Values : Pointer to argument structure or NULL on error
72 +--------------------------------------------------------------------------*/
73 static void *
Generic_This_Type(void * arg)74 Generic_This_Type(void *arg)
75 {
76 thisARG *argp = (thisARG *)0;
77 thisARG *param = (thisARG *)arg;
78
79 if (param)
80 {
81 argp = typeMalloc(thisARG, 1);
82
83 if (argp)
84 {
85 T((T_CREATE("thisARG %p"), (void *)argp));
86 *argp = *param;
87 }
88 }
89 return (void *)argp;
90 }
91
92 /*---------------------------------------------------------------------------
93 | Facility : libnform
94 | Function : static void *Make_This_Type( va_list * ap )
95 |
96 | Description : Allocate structure for integer type argument.
97 |
98 | Return Values : Pointer to argument structure or NULL on error
99 +--------------------------------------------------------------------------*/
100 static void *
Make_This_Type(va_list * ap)101 Make_This_Type(va_list *ap)
102 {
103 thisARG arg;
104
105 arg.precision = va_arg(*ap, int);
106 arg.low = va_arg(*ap, long);
107 arg.high = va_arg(*ap, long);
108
109 return Generic_This_Type((void *)&arg);
110 }
111
112 /*---------------------------------------------------------------------------
113 | Facility : libnform
114 | Function : static void *Copy_This_Type(const void * argp)
115 |
116 | Description : Copy structure for integer type argument.
117 |
118 | Return Values : Pointer to argument structure or NULL on error.
119 +--------------------------------------------------------------------------*/
120 static void *
Copy_This_Type(const void * argp)121 Copy_This_Type(const void *argp)
122 {
123 const thisARG *ap = (const thisARG *)argp;
124 thisARG *result = (thisARG *)0;
125
126 if (argp)
127 {
128 result = typeMalloc(thisARG, 1);
129
130 if (result)
131 {
132 T((T_CREATE("thisARG %p"), (void *)result));
133 *result = *ap;
134 }
135 }
136 return (void *)result;
137 }
138
139 /*---------------------------------------------------------------------------
140 | Facility : libnform
141 | Function : static void Free_This_Type(void * argp)
142 |
143 | Description : Free structure for integer type argument.
144 |
145 | Return Values : -
146 +--------------------------------------------------------------------------*/
147 static void
Free_This_Type(void * argp)148 Free_This_Type(void *argp)
149 {
150 if (argp)
151 free(argp);
152 }
153
154 /*---------------------------------------------------------------------------
155 | Facility : libnform
156 | Function : static bool Check_This_Field(
157 | FIELD * field,
158 | const void * argp)
159 |
160 | Description : Validate buffer content to be a valid integer value
161 |
162 | Return Values : TRUE - field is valid
163 | FALSE - field is invalid
164 +--------------------------------------------------------------------------*/
165 static bool
Check_This_Field(FIELD * field,const void * argp)166 Check_This_Field(FIELD *field, const void *argp)
167 {
168 const thisARG *argi = (const thisARG *)argp;
169 long low = argi->low;
170 long high = argi->high;
171 int prec = argi->precision;
172 unsigned char *bp = (unsigned char *)field_buffer(field, 0);
173 char *s = (char *)bp;
174 bool result = FALSE;
175
176 while (*bp == ' ')
177 bp++;
178 if (*bp)
179 {
180 if (*bp == '-')
181 bp++;
182 #if USE_WIDEC_SUPPORT
183 if (*bp)
184 {
185 int len;
186 wchar_t *list = _nc_Widen_String((char *)bp, &len);
187
188 if (list != 0)
189 {
190 bool blank = FALSE;
191 int n;
192
193 result = TRUE;
194 for (n = 0; n < len; ++n)
195 {
196 if (blank)
197 {
198 if (list[n] != ' ')
199 {
200 result = FALSE;
201 break;
202 }
203 }
204 else if (list[n] == ' ')
205 {
206 blank = TRUE;
207 }
208 else if (!isDigit(list[n]))
209 {
210 result = FALSE;
211 break;
212 }
213 }
214 free(list);
215 }
216 }
217 #else
218 while (*bp)
219 {
220 if (!isdigit(UChar(*bp)))
221 break;
222 bp++;
223 }
224 while (*bp && *bp == ' ')
225 bp++;
226 result = (*bp == '\0');
227 #endif
228 if (result)
229 {
230 long val = atol(s);
231
232 if (low < high)
233 {
234 if (val < low || val > high)
235 result = FALSE;
236 }
237 if (result)
238 {
239 char buf[100];
240
241 _nc_SPRINTF(buf, _nc_SLIMIT(sizeof(buf))
242 "%.*ld", (prec > 0 ? prec : 0), val);
243 set_field_buffer(field, 0, buf);
244 }
245 }
246 }
247 return (result);
248 }
249
250 /*---------------------------------------------------------------------------
251 | Facility : libnform
252 | Function : static bool Check_This_Character(
253 | int c,
254 | const void * argp)
255 |
256 | Description : Check a character for the integer type.
257 |
258 | Return Values : TRUE - character is valid
259 | FALSE - character is invalid
260 +--------------------------------------------------------------------------*/
261 static bool
Check_This_Character(int c,const void * argp GCC_UNUSED)262 Check_This_Character(int c, const void *argp GCC_UNUSED)
263 {
264 return ((isDigit(UChar(c)) || (c == '-')) ? TRUE : FALSE);
265 }
266
267 static FIELDTYPE typeTHIS =
268 {
269 _HAS_ARGS | _RESIDENT,
270 1, /* this is mutable, so we can't be const */
271 (FIELDTYPE *)0,
272 (FIELDTYPE *)0,
273 Make_This_Type,
274 Copy_This_Type,
275 Free_This_Type,
276 INIT_FT_FUNC(Check_This_Field),
277 INIT_FT_FUNC(Check_This_Character),
278 INIT_FT_FUNC(NULL),
279 INIT_FT_FUNC(NULL),
280 #if NCURSES_INTEROP_FUNCS
281 Generic_This_Type
282 #endif
283 };
284
285 FORM_EXPORT_VAR(FIELDTYPE *) TYPE_INTEGER = &typeTHIS;
286
287 #if NCURSES_INTEROP_FUNCS
288 /* The next routines are to simplify the use of ncurses from
289 programming languages with restrictions on interop with C level
290 constructs (e.g. variable access or va_list + ellipsis constructs)
291 */
292 FORM_EXPORT(FIELDTYPE *)
_nc_TYPE_INTEGER(void)293 _nc_TYPE_INTEGER(void)
294 {
295 return TYPE_INTEGER;
296 }
297 #endif
298
299 /* fty_int.c ends here */
300