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