1 /* $NetBSD: type_alnum.c,v 1.10 2004/11/24 11:57:09 blymn Exp $ */
2
3 /*-
4 * Copyright (c) 1998-1999 Brett Lymn
5 * (blymn@baea.com.au, brett_lymn@yahoo.com.au)
6 * All rights reserved.
7 *
8 * This code has been donated to The NetBSD Foundation by the Author.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *
29 *
30 */
31
32 #include <stdlib.h>
33 #include <stdarg.h>
34 #include <string.h>
35 #include <ctype.h>
36 #include "form.h"
37 #include "internals.h"
38
39 /*
40 * The alpha-numeric type handling.
41 */
42
43 typedef struct
44 {
45 unsigned width;
46 } alnum_args;
47
48 /*
49 * Create the alnum arguments structure from the given args. Return NULL
50 * if the call fails, otherwise return a pointer to the structure allocated.
51 */
52 static char *
create_alnum_args(va_list * args)53 create_alnum_args(va_list *args)
54 {
55 alnum_args *new;
56
57 new = (alnum_args *) malloc(sizeof(alnum_args));
58
59 if (new != NULL)
60 new->width = va_arg(*args, int);
61
62 return (void *) new;
63 }
64
65 /*
66 * Copy the alnum argument structure.
67 */
68 static char *
copy_alnum_args(char * args)69 copy_alnum_args(char *args)
70 {
71 alnum_args *new;
72
73 new = (alnum_args *) malloc(sizeof(alnum_args));
74
75 if (new != NULL)
76 new->width = ((alnum_args *) (void *)args)->width;
77
78 return (char *) (void *) new;
79 }
80
81 /*
82 * Free the allocated storage associated with the type arguments.
83 */
84 static void
free_alnum_args(char * args)85 free_alnum_args(char *args)
86 {
87 if (args != NULL)
88 free(args);
89 }
90
91 /*
92 * Check the contents of the field buffer are alphanumeric only.
93 */
94 static int
alnum_check_field(FIELD * field,char * args)95 alnum_check_field(FIELD *field, char *args)
96 {
97 int width, start, cur, end;
98 char *buf, *new;
99
100 width = ((alnum_args *) (void *) field->args)->width;
101 buf = args;
102 start = 0;
103
104 if (buf == NULL)
105 return FALSE;
106
107 /* skip leading white space */
108 while ((buf[start] != '\0')
109 && ((buf[start] == ' ') || (buf[start] == '\t')))
110 start++;
111
112 /* no good if we have hit the end */
113 if (buf[start] == '\0')
114 return FALSE;
115
116 /* find the end of the non-whitespace stuff */
117 cur = start;
118 while(isalnum((unsigned char)buf[cur]))
119 cur++;
120
121 /* no good if it exceeds the width */
122 if ((cur - start) > width)
123 return FALSE;
124
125 end = cur;
126
127 /* check there is only trailing whitespace */
128 while ((buf[cur] != '\0')
129 && ((buf[cur] == ' ') || (buf[cur] == '\t')))
130 cur++;
131
132 /* no good if we are not at the end of the string */
133 if (buf[cur] != '\0')
134 return FALSE;
135
136 if ((new = (char *) malloc(sizeof(char) * (end - start))) == NULL)
137 return FALSE;
138
139 if ((end - start) >= 1) {
140 strncpy(new, &buf[start], (size_t) (end - start - 1));
141 new[end] = '\0';
142 } else
143 new[0]= '\0';
144
145 set_field_buffer(field, 0, new);
146 free(new);
147
148 /* otherwise all was ok */
149 return TRUE;
150 }
151
152 /*
153 * Check the given character is alpha-numeric, return TRUE if it is.
154 */
155 static int
alnum_check_char(int c,char * args)156 alnum_check_char(/* ARGSUSED1 */ int c, char *args)
157 {
158 return (isalnum(c) ? TRUE : FALSE);
159 }
160
161 static FIELDTYPE builtin_alnum = {
162 _TYPE_HAS_ARGS | _TYPE_IS_BUILTIN, /* flags */
163 0, /* refcount */
164 NULL, /* link */
165 create_alnum_args, /* make_args */
166 copy_alnum_args, /* copy_args */
167 free_alnum_args, /* free_args */
168 alnum_check_field, /* field_check */
169 alnum_check_char, /* char_check */
170 NULL, /* next_choice */
171 NULL /* prev_choice */
172 };
173
174 FIELDTYPE *TYPE_ALNUM = &builtin_alnum;
175
176
177