xref: /illumos-gate/usr/src/lib/libeti/form/common/ty_alnum.c (revision 69a119caa6570c7077699161b7c28b6ee9f8b0f4)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*	Copyright (c) 1988 AT&T	*/
23 /*	  All Rights Reserved  	*/
24 
25 
26 /*
27  *      Copyright (c) 1997, by Sun Microsystems, Inc.
28  *      All rights reserved.
29  */
30 
31 #pragma ident	"%Z%%M%	%I%	%E% SMI" /* SVr4.0 1.1 */
32 
33 /*LINTLIBRARY*/
34 
35 #include <sys/types.h>
36 #include <stdlib.h>
37 #include "utility.h"
38 
39 /*
40  *	TYPE_ALNUM
41  *
42  *	usage:
43  *		set_field_type(f, TYPE_ALNUM, width);
44  *
45  *		int width;	minimum token width
46  */
47 static char * make_alnum(va_list *);
48 static char * copy_alnum(char *);
49 static void free_alnum(char *);
50 static int fcheck_alnum(FIELD *, char *);
51 static int ccheck_alnum(int, char *);
52 
53 static FIELDTYPE typeALNUM =
54 {
55 				ARGS,			/* status	*/
56 				1,			/* ref		*/
57 				(FIELDTYPE *) 0,	/* left		*/
58 				(FIELDTYPE *) 0,	/* right	*/
59 				make_alnum,		/* makearg	*/
60 				copy_alnum,		/* copyarg	*/
61 				free_alnum,		/* freearg	*/
62 				fcheck_alnum,		/* fcheck	*/
63 				ccheck_alnum,		/* ccheck	*/
64 				(PTF_int) 0,		/* next		*/
65 				(PTF_int) 0,		/* prev		*/
66 };
67 
68 FIELDTYPE * TYPE_ALNUM = &typeALNUM;
69 
70 static char *
71 make_alnum(va_list *ap)
72 {
73 	int * width;
74 
75 	if (Alloc(width, int))
76 		*width = va_arg(*ap, int);
77 	return ((char *) width);
78 }
79 
80 static char *
81 copy_alnum(char *arg)
82 {
83 	int * width;
84 
85 	if (Alloc(width, int))
86 		*width = *((int *) arg);
87 	return ((char *) width);
88 }
89 
90 static void
91 free_alnum(char *arg)
92 {
93 	Free(arg);
94 }
95 
96 static int
97 fcheck_alnum(FIELD *f, char *arg)
98 {
99 	int	width	= *((int *) arg);
100 	int	n	= 0;
101 	char 	*v	= field_buffer(f, 0);
102 
103 	while (*v && *v == ' ')
104 		++v;
105 	if (*v) {
106 		char * vbeg = v;
107 		while (*v && isalnum(*v))
108 			++v;
109 		n = (int) (v - vbeg);
110 		while (*v && *v == ' ')
111 			++v;
112 	}
113 	return (*v || n < width ? FALSE : TRUE);
114 }
115 
116 /*ARGSUSED*/
117 static int
118 ccheck_alnum(int c, char *arg)
119 {
120 	return (isalnum(c));
121 }
122