Home | History | Annotate | Line # | Download | only in libform
type_alpha.c revision 1.1
      1 /*	$NetBSD: type_alpha.c,v 1.1 2000/12/17 12:04:31 blymn Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998-1999 Brett Lymn
      5  *                         (blymn (at) baea.com.au, brett_lymn (at) 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 withough 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 <ctype.h>
     34 #include "form.h"
     35 #include "internals.h"
     36 
     37 /*
     38  * The alpha type handling.
     39  */
     40 
     41 typedef struct
     42 {
     43 	unsigned width;
     44 } alpha_args;
     45 
     46 /*
     47  * Create the alpha arguments structure from the given args.  Return NULL
     48  * if the call fails, otherwise return a pointer to the structure allocated.
     49  */
     50 static char *
     51 create_alpha_args(va_list *args)
     52 {
     53 	alpha_args *new;
     54 
     55 	new = (alpha_args *) malloc(sizeof(alpha_args));
     56 
     57 	if (new != NULL)
     58 		new->width = va_arg(*args, int);
     59 
     60 	return (void *) new;
     61 }
     62 
     63 /*
     64  * Copy the the alpha argument structure.
     65  */
     66 static char *
     67 copy_alpha_args(char *args)
     68 {
     69 	alpha_args *new;
     70 
     71 	new = (alpha_args *) malloc(sizeof(alpha_args));
     72 
     73 	if (new != NULL)
     74 		new->width = ((alpha_args *) (void *) args)->width;
     75 
     76 	return (void *) new;
     77 }
     78 
     79 /*
     80  * Free the allocated storage associated with the type arguments.
     81  */
     82 static void
     83 free_alpha_args(char *args)
     84 {
     85 	if (args != NULL)
     86 		free(args);
     87 }
     88 
     89 /*
     90  * Check the contents of the field buffer are alphanumeric only.
     91  */
     92 static int
     93 alpha_check_field(FIELD *field, char *args)
     94 {
     95 	int width, start, cur;
     96 	char *buf;
     97 
     98 	width = ((alpha_args *) (void *) args)->width;
     99 	buf = field->buffers[0].string;
    100 	start = 0;
    101 
    102 	  /* skip leading white space */
    103 	while ((buf[start] != '\0')
    104 	       && ((buf[start] == ' ') || (buf[start] == '\t')))
    105 		start++;
    106 
    107 	  /* no good if we have hit the end */
    108 	if (buf[start] == '\0')
    109 		return FALSE;
    110 
    111 	  /* find the end of the non-whitespace stuff */
    112 	cur = start;
    113 	while((buf[cur] != '\0') && isalpha(buf[cur]))
    114 		cur++;
    115 
    116 	  /* no good if it exceeds the width */
    117 	if ((cur - start) > width)
    118 		return FALSE;
    119 
    120 	  /* check there is only trailing whitespace */
    121 	while ((buf[cur] != '\0')
    122 	       && ((buf[cur] == ' ') || (buf[cur] == '\t')))
    123 		cur++;
    124 
    125 	  /* no good if we are not at the end of the string */
    126 	if (buf[cur] != '\0')
    127 		return FALSE;
    128 
    129 	  /* otherwise all was ok */
    130 	return TRUE;
    131 }
    132 
    133 /*
    134  * Check the given character is alphabetic, return TRUE if it is.
    135  */
    136 static int
    137 alpha_check_char(/* ARGSUSED1 */ int c, char *args)
    138 {
    139 	return (isalpha(c) ? TRUE : FALSE);
    140 }
    141 
    142 static FIELDTYPE builtin_alpha = {
    143 	_TYPE_HAS_ARGS | _TYPE_IS_BUILTIN,  /* flags */
    144 	0,                                  /* refcount */
    145 	NULL,                               /* link */
    146 	NULL,                               /* args */
    147 	create_alpha_args,                  /* make_args */
    148 	copy_alpha_args,                    /* copy_args */
    149 	free_alpha_args,                    /* free_args */
    150 	alpha_check_field,                  /* field_check */
    151 	alpha_check_char,                   /* char_check */
    152 	NULL,                               /* next_choice */
    153 	NULL                                /* prev_choice */
    154 };
    155 
    156 FIELDTYPE *TYPE_ALPHA = &builtin_alpha;
    157 
    158 
    159