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