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