xref: /openbsd/lib/libform/fty_alpha.c (revision e5dd7070)
1 /*	$OpenBSD: fty_alpha.c,v 1.9 2015/01/23 22:48:51 krw Exp $	*/
2 /****************************************************************************
3  * Copyright (c) 1998-2006,2007 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.9 2015/01/23 22:48:51 krw 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 *Make_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 Make_This_Type(va_list *ap)
58 {
59   thisARG *argp = typeMalloc(thisARG, 1);
60 
61   if (argp)
62     {
63       T((T_CREATE("thisARG %p"), argp));
64       argp->width = va_arg(*ap, int);
65     }
66 
67   return ((void *)argp);
68 }
69 
70 /*---------------------------------------------------------------------------
71 |   Facility      :  libnform
72 |   Function      :  static void *Copy_This_Type(const void * argp)
73 |
74 |   Description   :  Copy structure for alpha type argument.
75 |
76 |   Return Values :  Pointer to argument structure or NULL on error.
77 +--------------------------------------------------------------------------*/
78 static void *
79 Copy_This_Type(const void *argp)
80 {
81   const thisARG *ap = (const thisARG *)argp;
82   thisARG *result = typeMalloc(thisARG, 1);
83 
84   if (result)
85     {
86       T((T_CREATE("thisARG %p"), result));
87       *result = *ap;
88     }
89 
90   return ((void *)result);
91 }
92 
93 /*---------------------------------------------------------------------------
94 |   Facility      :  libnform
95 |   Function      :  static void Free_This_Type(void *argp)
96 |
97 |   Description   :  Free structure for alpha type argument.
98 |
99 |   Return Values :  -
100 +--------------------------------------------------------------------------*/
101 static void
102 Free_This_Type(void *argp)
103 {
104   if (argp)
105     free(argp);
106 }
107 
108 /*---------------------------------------------------------------------------
109 |   Facility      :  libnform
110 |   Function      :  static bool Check_This_Character(
111 |                                      int c,
112 |                                      const void *argp)
113 |
114 |   Description   :  Check a character for the alpha type.
115 |
116 |   Return Values :  TRUE  - character is valid
117 |                    FALSE - character is invalid
118 +--------------------------------------------------------------------------*/
119 static bool
120 Check_This_Character(int c, const void *argp GCC_UNUSED)
121 {
122 #if USE_WIDEC_SUPPORT
123   if (iswalpha((wint_t) c))
124     return TRUE;
125 #endif
126   return (isalpha(UChar(c)) ? TRUE : FALSE);
127 }
128 
129 /*---------------------------------------------------------------------------
130 |   Facility      :  libnform
131 |   Function      :  static bool Check_This_Field(
132 |                                      FIELD *field,
133 |                                      const void *argp)
134 |
135 |   Description   :  Validate buffer content to be a valid alpha value
136 |
137 |   Return Values :  TRUE  - field is valid
138 |                    FALSE - field is invalid
139 +--------------------------------------------------------------------------*/
140 static bool
141 Check_This_Field(FIELD *field, const void *argp)
142 {
143   int width = ((const thisARG *)argp)->width;
144   unsigned char *bp = (unsigned char *)field_buffer(field, 0);
145   bool result = (width < 0);
146 
147   Check_CTYPE_Field(result, bp, width, Check_This_Character);
148   return (result);
149 }
150 
151 static FIELDTYPE typeTHIS =
152 {
153   _HAS_ARGS | _RESIDENT,
154   1,				/* this is mutable, so we can't be const */
155   (FIELDTYPE *)0,
156   (FIELDTYPE *)0,
157   Make_This_Type,
158   Copy_This_Type,
159   Free_This_Type,
160   Check_This_Field,
161   Check_This_Character,
162   NULL,
163   NULL
164 };
165 
166 NCURSES_EXPORT_VAR(FIELDTYPE*) TYPE_ALPHA = &typeTHIS;
167 
168 /* fty_alpha.c ends here */
169