1 /* $NetBSD: type_alpha.c,v 1.10 2004/10/28 21:14:52 dsl 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.
32 #include <sys/cdefs.h>
33 __RCSID("$NetBSD: type_alpha.c,v 1.10 2004/10/28 21:14:52 dsl Exp $");
39 #include "internals.h"
42 * The alpha type handling.
51 * Create the alpha arguments structure from the given args. Return NULL
52 * if the call fails, otherwise return a pointer to the structure allocated.
55 create_alpha_args(va_list *args
)
59 new = (alpha_args
*) malloc(sizeof(alpha_args
));
62 new->width
= va_arg(*args
, int);
68 * Copy the alpha argument structure.
71 copy_alpha_args(char *args
)
75 new = (alpha_args
*) malloc(sizeof(alpha_args
));
78 new->width
= ((alpha_args
*) (void *) args
)->width
;
84 * Free the allocated storage associated with the type arguments.
87 free_alpha_args(char *args
)
94 * Check the contents of the field buffer are alphanumeric only.
97 alpha_check_field(FIELD
*field
, char *args
)
99 int width
, start
, cur
, end
;
102 width
= ((alpha_args
*) (void *) field
->args
)->width
;
109 /* skip leading white space */
110 while ((buf
[start
] != '\0')
111 && ((buf
[start
] == ' ') || (buf
[start
] == '\t')))
114 /* no good if we have hit the end */
115 if (buf
[start
] == '\0')
118 /* find the end of the non-whitespace stuff */
120 while(isalpha((unsigned char)buf
[cur
]))
123 /* no good if it exceeds the width */
124 if ((cur
- start
) > width
)
129 /* check there is only trailing whitespace */
130 while ((buf
[cur
] != '\0')
131 && ((buf
[cur
] == ' ') || (buf
[cur
] == '\t')))
134 /* no good if we are not at the end of the string */
135 if (buf
[cur
] != '\0')
138 /* set buffer 0 to the new string */
139 if ((new = (char *) malloc(sizeof(char) * (end
- start
))) == NULL
)
142 if ((end
- start
) >= 1) {
143 strncpy(new, &buf
[start
], (size_t) (end
- start
- 1));
149 set_field_buffer(field
, 0, new);
152 /* otherwise all was ok */
157 * Check the given character is alphabetic, return TRUE if it is.
160 alpha_check_char(/* ARGSUSED1 */ int c
, char *args
)
162 return (isalpha(c
) ? TRUE
: FALSE
);
165 static FIELDTYPE builtin_alpha
= {
166 _TYPE_HAS_ARGS
| _TYPE_IS_BUILTIN
, /* flags */
169 create_alpha_args
, /* make_args */
170 copy_alpha_args
, /* copy_args */
171 free_alpha_args
, /* free_args */
172 alpha_check_field
, /* field_check */
173 alpha_check_char
, /* char_check */
174 NULL
, /* next_choice */
175 NULL
/* prev_choice */
178 FIELDTYPE
*TYPE_ALPHA
= &builtin_alpha
;