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