1 /* $NetBSD: type_alnum.c,v 1.10 2004/11/24 11:57:09 blymn Exp $ */
4 * Copyright (c) 1998-1999 Brett Lymn
5 * (blymn@baea.com.au, brett_lymn@yahoo.com.au)
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
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.
37 #include "internals.h"
40 * The alpha-numeric type handling.
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.
53 create_alnum_args(va_list *args
)
57 new = (alnum_args
*) malloc(sizeof(alnum_args
));
60 new->width
= va_arg(*args
, int);
66 * Copy the alnum argument structure.
69 copy_alnum_args(char *args
)
73 new = (alnum_args
*) malloc(sizeof(alnum_args
));
76 new->width
= ((alnum_args
*) (void *)args
)->width
;
78 return (char *) (void *) new;
82 * Free the allocated storage associated with the type arguments.
85 free_alnum_args(char *args
)
92 * Check the contents of the field buffer are alphanumeric only.
95 alnum_check_field(FIELD
*field
, char *args
)
97 int width
, start
, cur
, end
;
100 width
= ((alnum_args
*) (void *) field
->args
)->width
;
107 /* skip leading white space */
108 while ((buf
[start
] != '\0')
109 && ((buf
[start
] == ' ') || (buf
[start
] == '\t')))
112 /* no good if we have hit the end */
113 if (buf
[start
] == '\0')
116 /* find the end of the non-whitespace stuff */
118 while(isalnum((unsigned char)buf
[cur
]))
121 /* no good if it exceeds the width */
122 if ((cur
- start
) > width
)
127 /* check there is only trailing whitespace */
128 while ((buf
[cur
] != '\0')
129 && ((buf
[cur
] == ' ') || (buf
[cur
] == '\t')))
132 /* no good if we are not at the end of the string */
133 if (buf
[cur
] != '\0')
136 if ((new = (char *) malloc(sizeof(char) * (end
- start
))) == NULL
)
139 if ((end
- start
) >= 1) {
140 strncpy(new, &buf
[start
], (size_t) (end
- start
- 1));
145 set_field_buffer(field
, 0, new);
148 /* otherwise all was ok */
153 * Check the given character is alpha-numeric, return TRUE if it is.
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 */
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
;