xref: /netbsd-src/sys/kern/subr_optstr.c (revision 4e4f3181fe6ba46dafc1139dd372fa4720330ceb)
1 /*	$NetBSD: subr_optstr.c,v 1.8 2023/04/20 09:04:45 skrll Exp $	*/
2 
3 /*-
4  * Copyright (c) 2006 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Julio M. Merino Vidal.
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. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: subr_optstr.c,v 1.8 2023/04/20 09:04:45 skrll Exp $");
34 
35 #include <sys/param.h>
36 #include <sys/optstr.h>
37 
38 /* --------------------------------------------------------------------- */
39 
40 /*
41  * Given an options string of the form 'a=b c=d ... y=z' and a key,
42  * looks for the given key's value in the string and returns it in buf
43  * with a maximum of bufsize bytes.  If the key is found, returns true;
44  * otherwise FALSE.
45  */
46 
47 
48 static bool
optstr_get_pointer(const char * optstr,const char * key,const char ** result)49 optstr_get_pointer(const char *optstr, const char *key, const char **result)
50 {
51 	bool found = false;
52 
53 	/* Skip any initial spaces or tabs until we find a word. */
54 	while (*optstr == ' ' || *optstr == '\t')
55 		optstr++;
56 
57 	/* Search for the given key within the option string. */
58 	while (!found && *optstr != '\0') {
59 		const char *keyp;
60 
61 		/* Check if the next word matches the key. */
62 		keyp = key;
63 		while (*optstr == *keyp) {
64 			optstr++;
65 			keyp++;
66 		}
67 
68 		if (*optstr == '=' && *keyp == '\0')
69 			found = true;
70 		else {
71 			/* Key not found; skip until next space. */
72 			while (*optstr != ' ' && *optstr != '\0')
73 				optstr++;
74 
75 			/* And now skip until next word. */
76 			while (*optstr == ' ')
77 				optstr++;
78 		}
79 	}
80 
81 	if (found) {
82 		optstr++; /* Skip '='. */
83 		*result = optstr;
84 	}
85 
86 	return found;
87 }
88 
89 bool
optstr_get(const char * optstr,const char * key,char * buf,size_t bufsize)90 optstr_get(const char *optstr, const char *key, char *buf, size_t bufsize)
91 {
92 	const char *data;
93 	bool found = optstr_get_pointer(optstr, key, &data);
94 
95 	/* If the key was found; copy its value to the target buffer. */
96 	if (found) {
97 		const char *lastbuf = buf + (bufsize - 1);
98 
99 		while (buf != lastbuf && *data != ' ' && *data != '\0')
100 			*buf++ = *data++;
101 		*buf = '\0';
102 	}
103 	return found;
104 }
105 
106 
107 bool
optstr_get_string(const char * optstr,const char * key,const char ** result)108 optstr_get_string(const char *optstr, const char *key, const char **result)
109 {
110 	const char *data;
111 	const bool found = optstr_get_pointer(optstr, key, &data);
112 
113 	/* If the key was found; copy its value to the target buffer. */
114 	if (found) {
115 		*result = data;
116 	}
117 	return found;
118 }
119 
120 bool
optstr_get_number(const char * optstr,const char * key,unsigned long * result)121 optstr_get_number(const char *optstr, const char *key, unsigned long *result)
122 {
123 	const char *data;
124 	const bool found = optstr_get_pointer(optstr, key, &data);
125 
126 	/* If the key was found; copy its value to the target buffer. */
127 	if (found) {
128 		char *ep;
129 		const unsigned long ulval = strtoul(data, &ep, 10);
130 		if (ep == data)
131 			return false;
132 		*result = ulval;
133 		return true;
134 	}
135 	return false;
136 }
137 
138 bool
optstr_get_number_hex(const char * optstr,const char * key,unsigned long * result)139 optstr_get_number_hex(const char *optstr, const char *key,
140     unsigned long *result)
141 {
142 	const char *data;
143 	const bool found = optstr_get_pointer(optstr, key, &data);
144 
145 	/* If the key was found; copy its value to the target buffer. */
146 	if (found) {
147 		char *ep;
148 		const unsigned long ulval = strtoul(data, &ep, 16);
149 		if (ep == data)
150 			return false;
151 		*result = ulval;
152 		return true;
153 	}
154 	return false;
155 }
156 
157 bool
optstr_get_number_binary(const char * optstr,const char * key,unsigned long * result)158 optstr_get_number_binary(const char *optstr, const char *key,
159     unsigned long *result)
160 {
161 	const char *data;
162 	const bool found = optstr_get_pointer(optstr, key, &data);
163 
164 	/* If the key was found; copy its value to the target buffer. */
165 	if (found) {
166 		char *ep;
167 		const unsigned long ulval = strtoul(data, &ep, 2);
168 		if (ep == data)
169 			return false;
170 		*result = ulval;
171 		return true;
172 	}
173 	return false;
174 }
175 
176 
177 #if NETHER > 0
178 bool
optstr_get_macaddr(const char * optstr,const char * key,uint8_t result[ETHER_ADDR_LEN])179 optstr_get_macaddr(const char *optstr, const char *key,
180     uint8_t result[ETHER_ADDR_LEN])
181 {
182 	const char *data;
183 	const bool found = optstr_get_pointer(optstr, key, &data);
184 
185 	/* If the key was found; copy its value to the target buffer. */
186 	if (found) {
187 		uint8_t temp[ETHER_ADDR_LEN];
188 		int error = ether_aton_r(temp, sizeof(temp), data);
189 		if (error)
190 			return false;
191 		memcpy(result, temp, sizeof(temp));
192 	}
193 	return found;
194 }
195 #endif
196