Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 1 | /**************************************************************************** |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 2 | * Copyright 2020,2021 Thomas E. Dickey * |
| 3 | * Copyright 1998-2010,2012 Free Software Foundation, Inc. * |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 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 | |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 38 | MODULE_ID("$Id: fty_int.c,v 1.33 2021/06/17 21:11:08 tom Exp $") |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 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 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 56 | typedef struct |
| 57 | { |
| 58 | int precision; |
| 59 | long low; |
| 60 | long high; |
| 61 | } |
| 62 | integerPARM; |
| 63 | |
| 64 | /*--------------------------------------------------------------------------- |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 65 | | Facility : libnform |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 66 | | Function : static void *Generic_This_Type( void * arg ) |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 67 | | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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 | { |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 75 | thisARG *argp = (thisARG *)0; |
| 76 | thisARG *param = (thisARG *)arg; |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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 | |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 91 | /*--------------------------------------------------------------------------- |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 92 | | Facility : libnform |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 93 | | Function : static void *Make_This_Type( va_list * ap ) |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 94 | | |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 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 | { |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 102 | thisARG arg; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 103 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | /*--------------------------------------------------------------------------- |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 112 | | Facility : libnform |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 113 | | Function : static void *Copy_This_Type(const void * argp) |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 114 | | |
| 115 | | Description : Copy structure for integer type argument. |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 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; |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 123 | thisARG *result = (thisARG *)0; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 124 | |
| 125 | if (argp) |
| 126 | { |
| 127 | result = typeMalloc(thisARG, 1); |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 128 | |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 129 | if (result) |
| 130 | { |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 131 | T((T_CREATE("thisARG %p"), (void *)result)); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 132 | *result = *ap; |
| 133 | } |
| 134 | } |
| 135 | return (void *)result; |
| 136 | } |
| 137 | |
| 138 | /*--------------------------------------------------------------------------- |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 139 | | Facility : libnform |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 140 | | Function : static void Free_This_Type(void * argp) |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 141 | | |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 142 | | Description : Free structure for integer type argument. |
| 143 | | |
| 144 | | Return Values : - |
| 145 | +--------------------------------------------------------------------------*/ |
| 146 | static void |
| 147 | Free_This_Type(void *argp) |
| 148 | { |
| 149 | if (argp) |
| 150 | free(argp); |
| 151 | } |
| 152 | |
| 153 | /*--------------------------------------------------------------------------- |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 154 | | Facility : libnform |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 155 | | Function : static bool Check_This_Field( |
| 156 | | FIELD * field, |
| 157 | | const void * argp) |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 158 | | |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 159 | | Description : Validate buffer content to be a valid integer value |
| 160 | | |
| 161 | | Return Values : TRUE - field is valid |
| 162 | | FALSE - field is invalid |
| 163 | +--------------------------------------------------------------------------*/ |
| 164 | static bool |
| 165 | Check_This_Field(FIELD *field, const void *argp) |
| 166 | { |
| 167 | const thisARG *argi = (const thisARG *)argp; |
| 168 | long low = argi->low; |
| 169 | long high = argi->high; |
| 170 | int prec = argi->precision; |
| 171 | unsigned char *bp = (unsigned char *)field_buffer(field, 0); |
| 172 | char *s = (char *)bp; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 173 | bool result = FALSE; |
| 174 | |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 175 | while (*bp == ' ') |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 176 | bp++; |
| 177 | if (*bp) |
| 178 | { |
| 179 | if (*bp == '-') |
| 180 | bp++; |
| 181 | #if USE_WIDEC_SUPPORT |
| 182 | if (*bp) |
| 183 | { |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 184 | int len; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 185 | wchar_t *list = _nc_Widen_String((char *)bp, &len); |
| 186 | |
| 187 | if (list != 0) |
| 188 | { |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 189 | bool blank = FALSE; |
| 190 | int n; |
| 191 | |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 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 | { |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 229 | long val = atol(s); |
| 230 | |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 231 | if (low < high) |
| 232 | { |
| 233 | if (val < low || val > high) |
| 234 | result = FALSE; |
| 235 | } |
| 236 | if (result) |
| 237 | { |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 238 | char buf[100]; |
| 239 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 240 | _nc_SPRINTF(buf, _nc_SLIMIT(sizeof(buf)) |
| 241 | "%.*ld", (prec > 0 ? prec : 0), val); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 242 | set_field_buffer(field, 0, buf); |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | return (result); |
| 247 | } |
| 248 | |
| 249 | /*--------------------------------------------------------------------------- |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 250 | | Facility : libnform |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 251 | | Function : static bool Check_This_Character( |
| 252 | | int c, |
| 253 | | const void * argp) |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 254 | | |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 255 | | Description : Check a character for the integer type. |
| 256 | | |
| 257 | | Return Values : TRUE - character is valid |
| 258 | | FALSE - character is invalid |
| 259 | +--------------------------------------------------------------------------*/ |
| 260 | static bool |
| 261 | Check_This_Character(int c, const void *argp GCC_UNUSED) |
| 262 | { |
| 263 | return ((isDigit(UChar(c)) || (c == '-')) ? TRUE : FALSE); |
| 264 | } |
| 265 | |
| 266 | static FIELDTYPE typeTHIS = |
| 267 | { |
| 268 | _HAS_ARGS | _RESIDENT, |
| 269 | 1, /* this is mutable, so we can't be const */ |
| 270 | (FIELDTYPE *)0, |
| 271 | (FIELDTYPE *)0, |
| 272 | Make_This_Type, |
| 273 | Copy_This_Type, |
| 274 | Free_This_Type, |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 275 | INIT_FT_FUNC(Check_This_Field), |
| 276 | INIT_FT_FUNC(Check_This_Character), |
| 277 | INIT_FT_FUNC(NULL), |
| 278 | INIT_FT_FUNC(NULL), |
| 279 | #if NCURSES_INTEROP_FUNCS |
| 280 | Generic_This_Type |
| 281 | #endif |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 282 | }; |
| 283 | |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 284 | FORM_EXPORT_VAR(FIELDTYPE *) TYPE_INTEGER = &typeTHIS; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 285 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 286 | #if NCURSES_INTEROP_FUNCS |
| 287 | /* The next routines are to simplify the use of ncurses from |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 288 | programming languages with restrictions on interop with C level |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 289 | constructs (e.g. variable access or va_list + ellipsis constructs) |
| 290 | */ |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 291 | FORM_EXPORT(FIELDTYPE *) |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 292 | _nc_TYPE_INTEGER(void) |
| 293 | { |
| 294 | return TYPE_INTEGER; |
| 295 | } |
| 296 | #endif |
| 297 | |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 298 | /* fty_int.c ends here */ |