xref: /freebsd/contrib/ncurses/form/fty_alnum.c (revision f39bffc62c1395bde25d152c7f68fdf7cbaab414)
1 /****************************************************************************
2  * Copyright (c) 1998-2009,2010 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.24 2010/01/23 21:14:36 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 *Generic_This_Type(void *arg)
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 Generic_This_Type(void *arg)
57 {
58   thisARG *argp = (thisARG *) 0;
59 
60   if (arg)
61     {
62       argp = typeMalloc(thisARG, 1);
63 
64       if (argp)
65 	{
66 	  T((T_CREATE("thisARG %p"), (void *)argp));
67 	  argp->width = *((int *)arg);
68 	}
69     }
70   return ((void *)argp);
71 }
72 
73 /*---------------------------------------------------------------------------
74 |   Facility      :  libnform
75 |   Function      :  static void *Make_This_Type(va_list *ap)
76 |
77 |   Description   :  Allocate structure for alphanumeric type argument.
78 |
79 |   Return Values :  Pointer to argument structure or NULL on error
80 +--------------------------------------------------------------------------*/
81 static void *
82 Make_This_Type(va_list *ap)
83 {
84   int w = va_arg(*ap, int);
85 
86   return Generic_This_Type((void *)&w);
87 }
88 
89 /*---------------------------------------------------------------------------
90 |   Facility      :  libnform
91 |   Function      :  static void *Copy_ThisType(const void *argp)
92 |
93 |   Description   :  Copy structure for alphanumeric type argument.
94 |
95 |   Return Values :  Pointer to argument structure or NULL on error.
96 +--------------------------------------------------------------------------*/
97 static void *
98 Copy_This_Type(const void *argp)
99 {
100   const thisARG *ap = (const thisARG *)argp;
101   thisARG *result = typeMalloc(thisARG, 1);
102 
103   if (result)
104     {
105       T((T_CREATE("thisARG %p"), (void *)result));
106       *result = *ap;
107     }
108 
109   return ((void *)result);
110 }
111 
112 /*---------------------------------------------------------------------------
113 |   Facility      :  libnform
114 |   Function      :  static void Free_This_Type(void *argp)
115 |
116 |   Description   :  Free structure for alphanumeric type argument.
117 |
118 |   Return Values :  -
119 +--------------------------------------------------------------------------*/
120 static void
121 Free_This_Type(void *argp)
122 {
123   if (argp)
124     free(argp);
125 }
126 
127 /*---------------------------------------------------------------------------
128 |   Facility      :  libnform
129 |   Function      :  static bool Check_This_Character(
130 |                                      int c,
131 |                                      const void *argp)
132 |
133 |   Description   :  Check a character for the alphanumeric type.
134 |
135 |   Return Values :  TRUE  - character is valid
136 |                    FALSE - character is invalid
137 +--------------------------------------------------------------------------*/
138 static bool
139 Check_This_Character(int c, const void *argp GCC_UNUSED)
140 {
141 #if USE_WIDEC_SUPPORT
142   if (iswalnum((wint_t) c))
143     return TRUE;
144 #endif
145   return (isalnum(UChar(c)) ? TRUE : FALSE);
146 }
147 
148 /*---------------------------------------------------------------------------
149 |   Facility      :  libnform
150 |   Function      :  static bool Check_This_Field(
151 |                                      FIELD *field,
152 |                                      const void *argp)
153 |
154 |   Description   :  Validate buffer content to be a valid alphanumeric value
155 |
156 |   Return Values :  TRUE  - field is valid
157 |                    FALSE - field is invalid
158 +--------------------------------------------------------------------------*/
159 static bool
160 Check_This_Field(FIELD *field, const void *argp)
161 {
162   int width = ((const thisARG *)argp)->width;
163   unsigned char *bp = (unsigned char *)field_buffer(field, 0);
164   bool result = (width < 0);
165 
166   Check_CTYPE_Field(result, bp, width, Check_This_Character);
167   return (result);
168 }
169 
170 static FIELDTYPE typeTHIS =
171 {
172   _HAS_ARGS | _RESIDENT,
173   1,				/* this is mutable, so we can't be const */
174   (FIELDTYPE *)0,
175   (FIELDTYPE *)0,
176   Make_This_Type,
177   Copy_This_Type,
178   Free_This_Type,
179   INIT_FT_FUNC(Check_This_Field),
180   INIT_FT_FUNC(Check_This_Character),
181   INIT_FT_FUNC(NULL),
182   INIT_FT_FUNC(NULL),
183 #if NCURSES_INTEROP_FUNCS
184   Generic_This_Type
185 #endif
186 };
187 
188 NCURSES_EXPORT_VAR(FIELDTYPE*) TYPE_ALNUM = &typeTHIS;
189 
190 #if NCURSES_INTEROP_FUNCS
191 /* The next routines are to simplify the use of ncurses from
192    programming languages with restictions on interop with C level
193    constructs (e.g. variable access or va_list + ellipsis constructs)
194 */
195 NCURSES_EXPORT(FIELDTYPE *)
196 _nc_TYPE_ALNUM(void)
197 {
198   return TYPE_ALNUM;
199 }
200 #endif
201 
202 /* fty_alnum.c ends here */
203