xref: /titanic_52/usr/src/lib/libeti/form/common/ty_int.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
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.2 */
32 
33 /*LINTLIBRARY*/
34 
35 #include <sys/types.h>
36 #include <stdlib.h>
37 #include "utility.h"
38 
39 /*
40  *	TYPE_INTEGER standard type
41  *
42  *	usage:
43  *		set_field_type(f, TYPE_INTEGER, precision, vmin, vmax);
44  *
45  *		int precision;	for padding with leading zeros
46  *		double vmin;	minimum acceptable value
47  *		double vmax;	maximum acceptable value
48  */
49 static char *make_int(va_list *);
50 static char *copy_int(char *);
51 static void free_int(char *);
52 static int fcheck_int(FIELD *, char *);
53 static int ccheck_int(int, char *);
54 
55 typedef struct {
56 
57 	int	prec;
58 	long	vmin;
59 	long	vmax;
60 } INTEGER;
61 
62 static FIELDTYPE typeINTEGER =
63 {
64 				ARGS,			/* status	*/
65 				1,			/* ref		*/
66 				(FIELDTYPE *) 0,	/* left		*/
67 				(FIELDTYPE *) 0,	/* right	*/
68 				make_int,		/* makearg	*/
69 				copy_int,		/* copyarg	*/
70 				free_int,		/* freearg	*/
71 				fcheck_int,		/* fcheck	*/
72 				ccheck_int,		/* ccheck	*/
73 				(PTF_int) 0,		/* next		*/
74 				(PTF_int) 0,		/* prev		*/
75 };
76 
77 FIELDTYPE * TYPE_INTEGER = &typeINTEGER;
78 
79 static char *
80 make_int(va_list *ap)
81 {
82 	INTEGER * n;
83 
84 	if (Alloc(n, INTEGER)) {
85 		n -> prec = va_arg(*ap, int);
86 		n -> vmin = va_arg(*ap, long);
87 		n -> vmax = va_arg(*ap, long);
88 	}
89 	return ((char *) n);
90 }
91 
92 static char *
93 copy_int(char *arg)
94 {
95 	INTEGER *n;
96 
97 	if (Alloc(n, INTEGER))
98 		*n = *((INTEGER *) arg);
99 	return ((char *) n);
100 }
101 
102 static void
103 free_int(char *arg)
104 {
105 	Free(arg);
106 }
107 
108 static int
109 fcheck_int(FIELD *f, char *arg)
110 {
111 	INTEGER *	n = (INTEGER *) arg;
112 	long		vmin = n -> vmin;
113 	long		vmax = n -> vmax;
114 	int		prec = n -> prec;
115 	char *		x = field_buffer(f, 0);
116 	char		buf[80];
117 
118 	while (*x && *x == ' ')
119 		++x;
120 	if (*x) {
121 		char * t = x;
122 
123 		if (*x == '-')
124 			++x;
125 		while (*x && isdigit(*x))
126 			++x;
127 		while (*x && *x == ' ')
128 			++x;
129 		if (! *x) {
130 			long v = atol(t);
131 
132 			if (vmin >= vmax || (v >= vmin && v <= vmax)) {
133 				(void) sprintf(buf, "%.*ld", prec, v);
134 				(void) set_field_buffer(f, 0, buf);
135 				return (TRUE);
136 			}
137 		}
138 	}
139 	return (FALSE);
140 }
141 
142 #define	charok(c)	(isdigit(c) || c == '-')
143 
144 /*ARGSUSED*/
145 
146 static int
147 ccheck_int(int c, char *arg)
148 {
149 	return (charok(c));
150 }
151