xref: /titanic_41/usr/src/lib/libpkg/common/ckparam.c (revision dd49f125507979bb2ab505a8daf2a46d1be27051)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
29 
30 
31 
32 #include <ctype.h>
33 #include <string.h>
34 #include <sys/types.h>
35 #include "pkglib.h"
36 #include "pkglibmsgs.h"
37 #include "pkglocale.h"
38 
39 #define	MAXLEN 256
40 #define	TOKLEN 16
41 
42 static int	proc_name(char *param, char *value);
43 static int	proc_arch(char *param, char *value);
44 static int	proc_version(char *param, char *value);
45 static int	proc_category(char *param, char *value);
46 static int	bad_first_char(char *param, char *value);
47 static int	not_alnum(char *param, char *pt);
48 static int	not_ascii(char *param, char *pt);
49 static int	too_long(char *param, char *pt, int len);
50 static int	isnull(char *param, char *pt);
51 
52 int
53 ckparam(char *param, char *val)
54 {
55 	char *value = strdup(val);
56 	int ret_val = 0;	/* return value */
57 
58 	if (strcmp(param, "NAME") == 0)
59 		ret_val = proc_name(param, value);
60 
61 	else if (strcmp(param, "ARCH") == 0)
62 		ret_val = proc_arch(param, value);
63 
64 	else if (strcmp(param, "VERSION") == 0)
65 		ret_val = proc_version(param, value);
66 
67 	else if (strcmp(param, "CATEGORY") == 0)
68 		ret_val = proc_category(param, value);
69 
70 	/* param does not match existing parameters */
71 	free(value);
72 	return (ret_val);
73 }
74 
75 static int
76 proc_name(char *param, char *value)
77 {
78 	int ret_val;
79 
80 	if (!(ret_val = isnull(param, value))) {
81 		ret_val += too_long(param, value, MAXLEN);
82 		ret_val += not_ascii(param, value);
83 	}
84 
85 	return (ret_val);
86 }
87 
88 static int
89 proc_arch(char *param, char *value)
90 {
91 	int ret_val;
92 	char *token;
93 
94 	if (!(ret_val = isnull(param, value))) {
95 		token = strtok(value, ", ");
96 
97 		while (token) {
98 			ret_val += too_long(param, token, TOKLEN);
99 			ret_val += not_ascii(param, token);
100 			token = strtok(NULL, ", ");
101 		}
102 	}
103 
104 	return (ret_val);
105 }
106 
107 static int
108 proc_version(char *param, char *value)
109 {
110 	int ret_val;
111 
112 	if (!(ret_val = isnull(param, value))) {
113 		ret_val += bad_first_char(param, value);
114 		ret_val += too_long(param, value, MAXLEN);
115 		ret_val += not_ascii(param, value);
116 	}
117 
118 	return (ret_val);
119 }
120 
121 static int
122 proc_category(char *param, char *value)
123 {
124 	int ret_val;
125 	char *token;
126 
127 	if (!(ret_val = isnull(param, value))) {
128 		token = strtok(value, ", ");
129 
130 		while (token) {
131 			ret_val += too_long(param, token, TOKLEN);
132 			ret_val += not_alnum(param, token);
133 			token = strtok(NULL, ", ");
134 		}
135 	}
136 
137 	return (ret_val);
138 }
139 
140 static int
141 bad_first_char(char *param, char *value)
142 {
143 	if (*value == '(') {
144 		progerr(pkg_gt(ERR_CHAR), param);
145 		return (1);
146 	}
147 
148 	return (0);
149 }
150 
151 static int
152 isnull(char *param, char *pt)
153 {
154 	if (!pt || *pt == '\0') {
155 		progerr(pkg_gt(ERR_UNDEF), param);
156 		return (1);
157 	}
158 	return (0);
159 }
160 
161 static int
162 too_long(char *param, char *pt, int len)
163 {
164 	if (strlen(pt) > (size_t)len) {
165 		progerr(pkg_gt(ERR_LEN), pt);
166 		return (1);
167 	}
168 	return (0);
169 }
170 
171 static int
172 not_ascii(char *param, char *pt)
173 {
174 	while (*pt) {
175 		if (!(isascii(*pt))) {
176 			progerr(pkg_gt(ERR_ASCII), param);
177 			return (1);
178 		}
179 		pt++;
180 	}
181 	return (0);
182 }
183 
184 static int
185 not_alnum(char *param, char *pt)
186 {
187 	while (*pt) {
188 		if (!(isalnum(*pt))) {
189 			progerr(pkg_gt(ERR_ALNUM), param);
190 			return (1);
191 		}
192 		pt++;
193 	}
194 
195 	return (0);
196 }
197