xref: /openbsd-src/lib/libform/fty_alnum.c (revision a4afd6dad3fba28f80e70208181c06c482259988)
1 
2 /*
3  * THIS CODE IS SPECIFICALLY EXEMPTED FROM THE NCURSES PACKAGE COPYRIGHT.
4  * You may freely copy it for use as a template for your own field types.
5  * If you develop a field type that might be of general use, please send
6  * it back to the ncurses maintainers for inclusion in the next version.
7  */
8 
9 #include "form.priv.h"
10 
11 typedef struct {
12   int width;
13 } alnumARG;
14 
15 /*---------------------------------------------------------------------------
16 |   Facility      :  libnform
17 |   Function      :  static void *Make_AlphaNumeric_Type(va_list *ap)
18 |
19 |   Description   :  Allocate structure for alphanumeric type argument.
20 |
21 |   Return Values :  Pointer to argument structure or NULL on error
22 +--------------------------------------------------------------------------*/
23 static void *Make_AlphaNumeric_Type(va_list * ap)
24 {
25   alnumARG *argp = (alnumARG *)malloc(sizeof(alnumARG));
26 
27   if (argp)
28     argp->width = va_arg(*ap,int);
29 
30   return ((void *)argp);
31 }
32 
33 /*---------------------------------------------------------------------------
34 |   Facility      :  libnform
35 |   Function      :  static void *Copy_AlphaNumericType(const void *argp)
36 |
37 |   Description   :  Copy structure for alphanumeric type argument.
38 |
39 |   Return Values :  Pointer to argument structure or NULL on error.
40 +--------------------------------------------------------------------------*/
41 static void *Copy_AlphaNumeric_Type(const void *argp)
42 {
43   alnumARG *ap  = (alnumARG *)argp;
44   alnumARG *new = (alnumARG *)malloc(sizeof(alnumARG));
45 
46   if (new)
47     *new = *ap;
48 
49   return ((void *)new);
50 }
51 
52 /*---------------------------------------------------------------------------
53 |   Facility      :  libnform
54 |   Function      :  static void Free_AlphaNumeric_Type(void *argp)
55 |
56 |   Description   :  Free structure for alphanumeric type argument.
57 |
58 |   Return Values :  -
59 +--------------------------------------------------------------------------*/
60 static void Free_AlphaNumeric_Type(void * argp)
61 {
62   if (argp)
63     free(argp);
64 }
65 
66 /*---------------------------------------------------------------------------
67 |   Facility      :  libnform
68 |   Function      :  static bool Check_AlphaNumeric_Field(
69 |                                      FIELD *field,
70 |                                      const void *argp)
71 |
72 |   Description   :  Validate buffer content to be a valid alphanumeric value
73 |
74 |   Return Values :  TRUE  - field is valid
75 |                    FALSE - field is invalid
76 +--------------------------------------------------------------------------*/
77 static bool Check_AlphaNumeric_Field(FIELD * field, const void * argp)
78 {
79   int width = ((alnumARG *)argp)->width;
80   unsigned char *bp  = (unsigned char *)field_buffer(field,0);
81   int  l = -1;
82   unsigned char *s;
83 
84   while(*bp && *bp==' ')
85     bp++;
86   if (*bp)
87     {
88       s = bp;
89       while(*bp && isalnum(*bp))
90 	bp++;
91       l = (int)(bp-s);
92       while(*bp && *bp==' ')
93 	bp++;
94     }
95   return ((*bp || (l < width)) ? FALSE : TRUE);
96 }
97 
98 /*---------------------------------------------------------------------------
99 |   Facility      :  libnform
100 |   Function      :  static bool Check_AlphaNumeric_Character(
101 |                                      int c,
102 |                                      const void *argp )
103 |
104 |   Description   :  Check a character for the alphanumeric type.
105 |
106 |   Return Values :  TRUE  - character is valid
107 |                    FALSE - character is invalid
108 +--------------------------------------------------------------------------*/
109 static bool Check_AlphaNumeric_Character(int c, const void * argp)
110 {
111   return (isalnum(c) ? TRUE : FALSE);
112 }
113 
114 static FIELDTYPE typeALNUM = {
115   _HAS_ARGS | _RESIDENT,
116   1,                           /* this is mutable, so we can't be const */
117   (FIELDTYPE *)0,
118   (FIELDTYPE *)0,
119   Make_AlphaNumeric_Type,
120   Copy_AlphaNumeric_Type,
121   Free_AlphaNumeric_Type,
122   Check_AlphaNumeric_Field,
123   Check_AlphaNumeric_Character,
124   NULL,
125   NULL
126 };
127 
128 FIELDTYPE* TYPE_ALNUM = &typeALNUM;
129 
130 /* fty_alnum.c ends here */
131