Remove building with NOCRYPTO option
[minix.git] / lib / libform / type_alnum.c
blob14bc6546fab8b9c435fb8f00dfd6316b750806fb
1 /* $NetBSD: type_alnum.c,v 1.10 2004/11/24 11:57:09 blymn Exp $ */
3 /*-
4 * Copyright (c) 1998-1999 Brett Lymn
5 * (blymn@baea.com.au, brett_lymn@yahoo.com.au)
6 * All rights reserved.
8 * This code has been donated to The NetBSD Foundation by the Author.
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
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.
32 #include <stdlib.h>
33 #include <stdarg.h>
34 #include <string.h>
35 #include <ctype.h>
36 #include "form.h"
37 #include "internals.h"
40 * The alpha-numeric type handling.
43 typedef struct
45 unsigned width;
46 } alnum_args;
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.
52 static char *
53 create_alnum_args(va_list *args)
55 alnum_args *new;
57 new = (alnum_args *) malloc(sizeof(alnum_args));
59 if (new != NULL)
60 new->width = va_arg(*args, int);
62 return (void *) new;
66 * Copy the alnum argument structure.
68 static char *
69 copy_alnum_args(char *args)
71 alnum_args *new;
73 new = (alnum_args *) malloc(sizeof(alnum_args));
75 if (new != NULL)
76 new->width = ((alnum_args *) (void *)args)->width;
78 return (char *) (void *) new;
82 * Free the allocated storage associated with the type arguments.
84 static void
85 free_alnum_args(char *args)
87 if (args != NULL)
88 free(args);
92 * Check the contents of the field buffer are alphanumeric only.
94 static int
95 alnum_check_field(FIELD *field, char *args)
97 int width, start, cur, end;
98 char *buf, *new;
100 width = ((alnum_args *) (void *) field->args)->width;
101 buf = args;
102 start = 0;
104 if (buf == NULL)
105 return FALSE;
107 /* skip leading white space */
108 while ((buf[start] != '\0')
109 && ((buf[start] == ' ') || (buf[start] == '\t')))
110 start++;
112 /* no good if we have hit the end */
113 if (buf[start] == '\0')
114 return FALSE;
116 /* find the end of the non-whitespace stuff */
117 cur = start;
118 while(isalnum((unsigned char)buf[cur]))
119 cur++;
121 /* no good if it exceeds the width */
122 if ((cur - start) > width)
123 return FALSE;
125 end = cur;
127 /* check there is only trailing whitespace */
128 while ((buf[cur] != '\0')
129 && ((buf[cur] == ' ') || (buf[cur] == '\t')))
130 cur++;
132 /* no good if we are not at the end of the string */
133 if (buf[cur] != '\0')
134 return FALSE;
136 if ((new = (char *) malloc(sizeof(char) * (end - start))) == NULL)
137 return FALSE;
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';
145 set_field_buffer(field, 0, new);
146 free(new);
148 /* otherwise all was ok */
149 return TRUE;
153 * Check the given character is alpha-numeric, return TRUE if it is.
155 static int
156 alnum_check_char(/* ARGSUSED1 */ int c, char *args)
158 return (isalnum(c) ? TRUE : FALSE);
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 */
174 FIELDTYPE *TYPE_ALNUM = &builtin_alnum;