xref: /openbsd-src/lib/libform/fty_alpha.c (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1 /*	$OpenBSD: fty_alpha.c,v 1.7 2007/03/20 03:40:05 tedu Exp $	*/
2 
3 
4 /*
5  * THIS CODE IS SPECIFICALLY EXEMPTED FROM THE NCURSES PACKAGE COPYRIGHT.
6  * You may freely copy it for use as a template for your own field types.
7  * If you develop a field type that might be of general use, please send
8  * it back to the ncurses maintainers for inclusion in the next version.
9  */
10 /***************************************************************************
11 *                                                                          *
12 *  Author : Juergen Pfeifer, juergen.pfeifer@gmx.net                       *
13 *                                                                          *
14 ***************************************************************************/
15 
16 #include "form.priv.h"
17 
18 MODULE_ID("$From: fty_alpha.c,v 1.10 2000/12/09 23:46:12 tom Exp $")
19 
20 typedef struct {
21   int width;
22 } alphaARG;
23 
24 /*---------------------------------------------------------------------------
25 |   Facility      :  libnform
26 |   Function      :  static void *Make_Alpha_Type(va_list *ap)
27 |
28 |   Description   :  Allocate structure for alpha type argument.
29 |
30 |   Return Values :  Pointer to argument structure or NULL on error
31 +--------------------------------------------------------------------------*/
32 static void *Make_Alpha_Type(va_list * ap)
33 {
34   alphaARG *argp = (alphaARG *)malloc(sizeof(alphaARG));
35   if (argp)
36     {
37       argp->width = va_arg(*ap,int);
38     }
39   return ((void *)argp);
40 }
41 
42 /*---------------------------------------------------------------------------
43 |   Facility      :  libnform
44 |   Function      :  static void *Copy_Alpha_Type(const void * argp)
45 |
46 |   Description   :  Copy structure for alpha type argument.
47 |
48 |   Return Values :  Pointer to argument structure or NULL on error.
49 +--------------------------------------------------------------------------*/
50 static void *Copy_Alpha_Type(const void * argp)
51 {
52   const alphaARG *ap = (const alphaARG *)argp;
53   alphaARG *result = (alphaARG *)malloc(sizeof(alphaARG));
54 
55   if (result)
56     {
57       *result = *ap;
58     }
59   return ((void *)result);
60 }
61 
62 /*---------------------------------------------------------------------------
63 |   Facility      :  libnform
64 |   Function      :  static void Free_Alpha_Type( void * argp )
65 |
66 |   Description   :  Free structure for alpha type argument.
67 |
68 |   Return Values :  -
69 +--------------------------------------------------------------------------*/
70 static void Free_Alpha_Type(void * argp)
71 {
72   if (argp)
73     free(argp);
74 }
75 
76 /*---------------------------------------------------------------------------
77 |   Facility      :  libnform
78 |   Function      :  static bool Check_Alpha_Field(
79 |                                      FIELD * field,
80 |                                      const void * argp)
81 |
82 |   Description   :  Validate buffer content to be a valid alpha value
83 |
84 |   Return Values :  TRUE  - field is valid
85 |                    FALSE - field is invalid
86 +--------------------------------------------------------------------------*/
87 static bool Check_Alpha_Field(FIELD * field, const void * argp)
88 {
89   int width = ((const alphaARG *)argp)->width;
90   unsigned char *bp  = (unsigned char *)field_buffer(field,0);
91   int  l = -1;
92   unsigned char *s;
93 
94   while(*bp==' ')
95     bp++;
96   if (*bp)
97     {
98       s = bp;
99       while(isalpha(*bp))
100 	bp++;
101       l = (int)(bp-s);
102       while(*bp==' ')
103 	bp++;
104     }
105   return ((*bp || (l < width)) ? FALSE : TRUE);
106 }
107 
108 /*---------------------------------------------------------------------------
109 |   Facility      :  libnform
110 |   Function      :  static bool Check_Alpha_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 Check_Alpha_Character(int c, const void * argp GCC_UNUSED)
120 {
121   return (isalpha(c) ? TRUE : FALSE);
122 }
123 
124 static FIELDTYPE typeALPHA = {
125   _HAS_ARGS | _RESIDENT,
126   1,                           /* this is mutable, so we can't be const */
127   (FIELDTYPE *)0,
128   (FIELDTYPE *)0,
129   Make_Alpha_Type,
130   Copy_Alpha_Type,
131   Free_Alpha_Type,
132   Check_Alpha_Field,
133   Check_Alpha_Character,
134   NULL,
135   NULL
136 };
137 
138 NCURSES_EXPORT_VAR(FIELDTYPE*) TYPE_ALPHA = &typeALPHA;
139 
140 /* fty_alpha.c ends here */
141