8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / lib / libpkg / common / ckparam.c
blob209d3b4d5d73d7d1fc19db89cd69f50b866ff17e
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
23 * Copyright (c) 2017 Peter Tribble.
27 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
28 * Use is subject to license terms.
31 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
32 /* All Rights Reserved */
36 #include <ctype.h>
37 #include <string.h>
38 #include <stdlib.h>
39 #include <sys/types.h>
40 #include "pkglib.h"
41 #include "pkglibmsgs.h"
42 #include "pkglocale.h"
44 #define MAXLEN 256
45 #define TOKLEN 16
47 static int proc_name(char *param, char *value);
48 static int proc_arch(char *param, char *value);
49 static int proc_version(char *param, char *value);
50 static int proc_category(char *param, char *value);
51 static int bad_first_char(char *param, char *value);
52 static int not_alnum(char *param, char *pt);
53 static int not_ascii(char *param, char *pt);
54 static int too_long(char *param, char *pt, int len);
55 static int isnull(char *param, char *pt);
57 int
58 ckparam(char *param, char *val)
60 char *value = strdup(val);
61 int ret_val = 0; /* return value */
63 if (strcmp(param, "NAME") == 0)
64 ret_val = proc_name(param, value);
66 else if (strcmp(param, "ARCH") == 0)
67 ret_val = proc_arch(param, value);
69 else if (strcmp(param, "VERSION") == 0)
70 ret_val = proc_version(param, value);
72 else if (strcmp(param, "CATEGORY") == 0)
73 ret_val = proc_category(param, value);
75 /* param does not match existing parameters */
76 free(value);
77 return (ret_val);
80 static int
81 proc_name(char *param, char *value)
83 int ret_val;
85 if (!(ret_val = isnull(param, value))) {
86 ret_val += too_long(param, value, MAXLEN);
87 ret_val += not_ascii(param, value);
90 return (ret_val);
93 static int
94 proc_arch(char *param, char *value)
96 int ret_val;
97 char *token;
99 if (!(ret_val = isnull(param, value))) {
100 token = strtok(value, ", ");
102 while (token) {
103 ret_val += too_long(param, token, TOKLEN);
104 ret_val += not_ascii(param, token);
105 token = strtok(NULL, ", ");
109 return (ret_val);
112 static int
113 proc_version(char *param, char *value)
115 int ret_val;
117 if (!(ret_val = isnull(param, value))) {
118 ret_val += bad_first_char(param, value);
119 ret_val += too_long(param, value, MAXLEN);
120 ret_val += not_ascii(param, value);
123 return (ret_val);
126 static int
127 proc_category(char *param, char *value)
129 int ret_val;
130 char *token;
132 if (!(ret_val = isnull(param, value))) {
133 token = strtok(value, ", ");
135 while (token) {
136 ret_val += too_long(param, token, TOKLEN);
137 ret_val += not_alnum(param, token);
138 token = strtok(NULL, ", ");
142 return (ret_val);
145 static int
146 bad_first_char(char *param, char *value)
148 if (*value == '(') {
149 progerr(pkg_gt(ERR_CHAR), param);
150 return (1);
153 return (0);
156 static int
157 isnull(char *param, char *pt)
159 if (!pt || *pt == '\0') {
160 progerr(pkg_gt(ERR_UNDEF), param);
161 return (1);
163 return (0);
166 static int
167 too_long(char *param, char *pt, int len)
169 if (strlen(pt) > (size_t)len) {
170 progerr(pkg_gt(ERR_LEN), param);
171 return (1);
173 return (0);
176 static int
177 not_ascii(char *param, char *pt)
179 while (*pt) {
180 if (!(isascii(*pt))) {
181 progerr(pkg_gt(ERR_ASCII), param);
182 return (1);
184 pt++;
186 return (0);
189 static int
190 not_alnum(char *param, char *pt)
192 while (*pt) {
193 if (!(isalnum(*pt))) {
194 progerr(pkg_gt(ERR_ALNUM), param);
195 return (1);
197 pt++;
200 return (0);