xref: /openbsd-src/lib/libform/fty_alnum.c (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1 /*	$OpenBSD: fty_alnum.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_alnum.c,v 1.10 2000/12/09 23:46:12 tom Exp $")
19 
20 typedef struct {
21   int width;
22 } alnumARG;
23 
24 /*---------------------------------------------------------------------------
25 |   Facility      :  libnform
26 |   Function      :  static void *Make_AlphaNumeric_Type(va_list *ap)
27 |
28 |   Description   :  Allocate structure for alphanumeric type argument.
29 |
30 |   Return Values :  Pointer to argument structure or NULL on error
31 +--------------------------------------------------------------------------*/
32 static void *Make_AlphaNumeric_Type(va_list * ap)
33 {
34   alnumARG *argp = (alnumARG *)malloc(sizeof(alnumARG));
35 
36   if (argp)
37     argp->width = va_arg(*ap,int);
38 
39   return ((void *)argp);
40 }
41 
42 /*---------------------------------------------------------------------------
43 |   Facility      :  libnform
44 |   Function      :  static void *Copy_AlphaNumericType(const void *argp)
45 |
46 |   Description   :  Copy structure for alphanumeric type argument.
47 |
48 |   Return Values :  Pointer to argument structure or NULL on error.
49 +--------------------------------------------------------------------------*/
50 static void *Copy_AlphaNumeric_Type(const void *argp)
51 {
52   const alnumARG *ap = (const alnumARG *)argp;
53   alnumARG *result = (alnumARG *)malloc(sizeof(alnumARG));
54 
55   if (result)
56     *result = *ap;
57 
58   return ((void *)result);
59 }
60 
61 /*---------------------------------------------------------------------------
62 |   Facility      :  libnform
63 |   Function      :  static void Free_AlphaNumeric_Type(void *argp)
64 |
65 |   Description   :  Free structure for alphanumeric type argument.
66 |
67 |   Return Values :  -
68 +--------------------------------------------------------------------------*/
69 static void Free_AlphaNumeric_Type(void * argp)
70 {
71   if (argp)
72     free(argp);
73 }
74 
75 /*---------------------------------------------------------------------------
76 |   Facility      :  libnform
77 |   Function      :  static bool Check_AlphaNumeric_Field(
78 |                                      FIELD *field,
79 |                                      const void *argp)
80 |
81 |   Description   :  Validate buffer content to be a valid alphanumeric value
82 |
83 |   Return Values :  TRUE  - field is valid
84 |                    FALSE - field is invalid
85 +--------------------------------------------------------------------------*/
86 static bool Check_AlphaNumeric_Field(FIELD * field, const void * argp)
87 {
88   int width = ((const alnumARG *)argp)->width;
89   unsigned char *bp  = (unsigned char *)field_buffer(field,0);
90   int  l = -1;
91   unsigned char *s;
92 
93   while(*bp==' ')
94     bp++;
95   if (*bp)
96     {
97       s = bp;
98       while(isalnum(*bp))
99 	bp++;
100       l = (int)(bp-s);
101       while(*bp==' ')
102 	bp++;
103     }
104   return ((*bp || (l < width)) ? FALSE : TRUE);
105 }
106 
107 /*---------------------------------------------------------------------------
108 |   Facility      :  libnform
109 |   Function      :  static bool Check_AlphaNumeric_Character(
110 |                                      int c,
111 |                                      const void *argp )
112 |
113 |   Description   :  Check a character for the alphanumeric type.
114 |
115 |   Return Values :  TRUE  - character is valid
116 |                    FALSE - character is invalid
117 +--------------------------------------------------------------------------*/
118 static bool Check_AlphaNumeric_Character(int c, const void * argp GCC_UNUSED)
119 {
120   return (isalnum(c) ? TRUE : FALSE);
121 }
122 
123 static FIELDTYPE typeALNUM = {
124   _HAS_ARGS | _RESIDENT,
125   1,                           /* this is mutable, so we can't be const */
126   (FIELDTYPE *)0,
127   (FIELDTYPE *)0,
128   Make_AlphaNumeric_Type,
129   Copy_AlphaNumeric_Type,
130   Free_AlphaNumeric_Type,
131   Check_AlphaNumeric_Field,
132   Check_AlphaNumeric_Character,
133   NULL,
134   NULL
135 };
136 
137 NCURSES_EXPORT_VAR(FIELDTYPE*) TYPE_ALNUM = &typeALNUM;
138 
139 /* fty_alnum.c ends here */
140