blob: a958c714828315ad069b831a84992d152abe1a9f [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Joe Hershberger170ab112012-12-11 22:16:24 -06002/*
3 * (C) Copyright 2012
4 * Joe Hershberger, National Instruments, joe.hershberger@ni.com
Joe Hershberger170ab112012-12-11 22:16:24 -06005 */
6
Joe Hershberger30fd4fad2012-12-11 22:16:32 -06007#ifdef USE_HOSTCC /* Eliminate "ANSI does not permit..." warnings */
8#include <stdint.h>
9#include <stdio.h>
10#include <linux/linux_string.h>
11#else
Joe Hershberger170ab112012-12-11 22:16:24 -060012#include <common.h>
Joe Hershbergerbdf1fe42015-05-20 14:27:20 -050013#include <slre.h>
Joe Hershberger30fd4fad2012-12-11 22:16:32 -060014#endif
15
Joe Hershberger170ab112012-12-11 22:16:24 -060016#include <env_attr.h>
17#include <errno.h>
18#include <linux/string.h>
19#include <malloc.h>
20
21/*
22 * Iterate through the whole list calling the callback for each found element.
23 * "attr_list" takes the form:
24 * attributes = [^,:\s]*
25 * entry = name[:attributes]
26 * list = entry[,list]
27 */
28int env_attr_walk(const char *attr_list,
Joe Hershbergercca98fd2015-05-20 14:27:19 -050029 int (*callback)(const char *name, const char *attributes, void *priv),
30 void *priv)
Joe Hershberger170ab112012-12-11 22:16:24 -060031{
32 const char *entry, *entry_end;
33 char *name, *attributes;
34
35 if (!attr_list)
36 /* list not found */
37 return 1;
38
39 entry = attr_list;
40 do {
41 char *entry_cpy = NULL;
42
43 entry_end = strchr(entry, ENV_ATTR_LIST_DELIM);
44 /* check if this is the last entry in the list */
45 if (entry_end == NULL) {
46 int entry_len = strlen(entry);
47
48 if (entry_len) {
49 /*
50 * allocate memory to copy the entry into since
51 * we will need to inject '\0' chars and squash
52 * white-space before calling the callback
53 */
54 entry_cpy = malloc(entry_len + 1);
55 if (entry_cpy)
56 /* copy the rest of the list */
57 strcpy(entry_cpy, entry);
58 else
59 return -ENOMEM;
60 }
61 } else {
62 int entry_len = entry_end - entry;
63
64 if (entry_len) {
65 /*
66 * allocate memory to copy the entry into since
67 * we will need to inject '\0' chars and squash
68 * white-space before calling the callback
69 */
70 entry_cpy = malloc(entry_len + 1);
71 if (entry_cpy) {
72 /* copy just this entry and null term */
73 strncpy(entry_cpy, entry, entry_len);
74 entry_cpy[entry_len] = '\0';
75 } else
76 return -ENOMEM;
77 }
78 }
79
80 /* check if there is anything to process (e.g. not ",,,") */
81 if (entry_cpy != NULL) {
82 attributes = strchr(entry_cpy, ENV_ATTR_SEP);
83 /* check if there is a ':' */
84 if (attributes != NULL) {
85 /* replace the ':' with '\0' to term name */
86 *attributes++ = '\0';
87 /* remove white-space from attributes */
88 attributes = strim(attributes);
89 }
90 /* remove white-space from name */
91 name = strim(entry_cpy);
92
93 /* only call the callback if there is a name */
94 if (strlen(name) != 0) {
95 int retval = 0;
96
Joe Hershbergercca98fd2015-05-20 14:27:19 -050097 retval = callback(name, attributes, priv);
Joe Hershberger170ab112012-12-11 22:16:24 -060098 if (retval) {
99 free(entry_cpy);
100 return retval;
101 }
102 }
103 }
104
105 free(entry_cpy);
106 entry = entry_end + 1;
107 } while (entry_end != NULL);
108
109 return 0;
110}
111
Joe Hershbergerbdf1fe42015-05-20 14:27:20 -0500112#if defined(CONFIG_REGEX)
113struct regex_callback_priv {
114 const char *searched_for;
115 char *regex;
116 char *attributes;
117};
118
119static int regex_callback(const char *name, const char *attributes, void *priv)
120{
121 int retval = 0;
122 struct regex_callback_priv *cbp = (struct regex_callback_priv *)priv;
123 struct slre slre;
124 char regex[strlen(name) + 3];
125
126 /* Require the whole string to be described by the regex */
127 sprintf(regex, "^%s$", name);
128 if (slre_compile(&slre, regex)) {
129 struct cap caps[slre.num_caps + 2];
130
131 if (slre_match(&slre, cbp->searched_for,
132 strlen(cbp->searched_for), caps)) {
133 free(cbp->regex);
xypron.glpk@gmx.de902f5bc2017-05-08 20:23:54 +0200134 if (!attributes) {
135 retval = -EINVAL;
136 goto done;
137 }
Joe Hershbergerbdf1fe42015-05-20 14:27:20 -0500138 cbp->regex = malloc(strlen(regex) + 1);
139 if (cbp->regex) {
140 strcpy(cbp->regex, regex);
141 } else {
142 retval = -ENOMEM;
143 goto done;
144 }
145
146 free(cbp->attributes);
147 cbp->attributes = malloc(strlen(attributes) + 1);
148 if (cbp->attributes) {
149 strcpy(cbp->attributes, attributes);
150 } else {
151 retval = -ENOMEM;
152 free(cbp->regex);
153 cbp->regex = NULL;
154 goto done;
155 }
156 }
157 } else {
158 printf("Error compiling regex: %s\n", slre.err_str);
xypron.glpk@gmx.de9b57e252017-05-08 19:30:58 +0200159 retval = -EINVAL;
Joe Hershbergerbdf1fe42015-05-20 14:27:20 -0500160 }
161done:
162 return retval;
163}
164
165/*
166 * Retrieve the attributes string associated with a single name in the list
167 * There is no protection on attributes being too small for the value
168 */
169int env_attr_lookup(const char *attr_list, const char *name, char *attributes)
170{
171 if (!attributes)
172 /* bad parameter */
173 return -EINVAL;
174 if (!attr_list)
175 /* list not found */
176 return -EINVAL;
177
178 struct regex_callback_priv priv;
179 int retval;
180
181 priv.searched_for = name;
182 priv.regex = NULL;
183 priv.attributes = NULL;
184 retval = env_attr_walk(attr_list, regex_callback, &priv);
185 if (retval)
186 return retval; /* error */
187
188 if (priv.regex) {
189 strcpy(attributes, priv.attributes);
190 free(priv.attributes);
191 free(priv.regex);
192 /* success */
193 return 0;
194 }
195 return -ENOENT; /* not found in list */
196}
197#else
198
Joe Hershberger170ab112012-12-11 22:16:24 -0600199/*
Joe Hershberger032ea182015-05-20 14:27:18 -0500200 * Search for the last exactly matching name in an attribute list
Joe Hershberger170ab112012-12-11 22:16:24 -0600201 */
Joe Hershberger032ea182015-05-20 14:27:18 -0500202static int reverse_name_search(const char *searched, const char *search_for,
203 const char **result)
Joe Hershberger170ab112012-12-11 22:16:24 -0600204{
Joe Hershberger032ea182015-05-20 14:27:18 -0500205 int result_size = 0;
206 const char *cur_searched = searched;
Joe Hershberger170ab112012-12-11 22:16:24 -0600207
Joe Hershberger032ea182015-05-20 14:27:18 -0500208 if (result)
209 *result = NULL;
Joe Hershberger170ab112012-12-11 22:16:24 -0600210
Joe Hershberger032ea182015-05-20 14:27:18 -0500211 if (*search_for == '\0') {
212 if (result)
213 *result = searched;
214 return strlen(searched);
Joe Hershberger170ab112012-12-11 22:16:24 -0600215 }
216
Joe Hershberger032ea182015-05-20 14:27:18 -0500217 for (;;) {
218 const char *match = strstr(cur_searched, search_for);
219 const char *prevch;
220 const char *nextch;
221
222 /* Stop looking if no new match is found */
223 if (match == NULL)
224 break;
225
226 prevch = match - 1;
227 nextch = match + strlen(search_for);
228
229 /* Skip spaces */
230 while (*prevch == ' ' && prevch >= searched)
231 prevch--;
232 while (*nextch == ' ')
233 nextch++;
234
235 /* Start looking past the current match so last is found */
236 cur_searched = match + 1;
237 /* Check for an exact match */
238 if (match != searched &&
239 *prevch != ENV_ATTR_LIST_DELIM &&
240 prevch != searched - 1)
241 continue;
242 if (*nextch != ENV_ATTR_SEP &&
243 *nextch != ENV_ATTR_LIST_DELIM &&
244 *nextch != '\0')
245 continue;
246
247 if (result)
248 *result = match;
249 result_size = strlen(search_for);
250 }
251
252 return result_size;
Joe Hershberger170ab112012-12-11 22:16:24 -0600253}
254
255/*
256 * Retrieve the attributes string associated with a single name in the list
257 * There is no protection on attributes being too small for the value
258 */
259int env_attr_lookup(const char *attr_list, const char *name, char *attributes)
260{
261 const char *entry = NULL;
Joe Hershberger032ea182015-05-20 14:27:18 -0500262 int entry_len;
Joe Hershberger170ab112012-12-11 22:16:24 -0600263
264 if (!attributes)
265 /* bad parameter */
Joe Hershberger7a0ad2c2015-05-20 14:27:17 -0500266 return -EINVAL;
Joe Hershberger170ab112012-12-11 22:16:24 -0600267 if (!attr_list)
268 /* list not found */
Joe Hershberger7a0ad2c2015-05-20 14:27:17 -0500269 return -EINVAL;
Joe Hershberger170ab112012-12-11 22:16:24 -0600270
Joe Hershberger032ea182015-05-20 14:27:18 -0500271 entry_len = reverse_name_search(attr_list, name, &entry);
Joe Hershberger170ab112012-12-11 22:16:24 -0600272 if (entry != NULL) {
273 int len;
274
275 /* skip the name */
Joe Hershberger032ea182015-05-20 14:27:18 -0500276 entry += entry_len;
Joe Hershberger170ab112012-12-11 22:16:24 -0600277 /* skip spaces */
278 while (*entry == ' ')
279 entry++;
280 if (*entry != ENV_ATTR_SEP)
281 len = 0;
282 else {
283 const char *delim;
284 static const char delims[] = {
285 ENV_ATTR_LIST_DELIM, ' ', '\0'};
286
287 /* skip the attr sep */
288 entry += 1;
289 /* skip spaces */
290 while (*entry == ' ')
291 entry++;
292
293 delim = strpbrk(entry, delims);
294 if (delim == NULL)
295 len = strlen(entry);
296 else
297 len = delim - entry;
298 memcpy(attributes, entry, len);
299 }
300 attributes[len] = '\0';
301
302 /* success */
303 return 0;
304 }
305
306 /* not found in list */
Joe Hershberger7a0ad2c2015-05-20 14:27:17 -0500307 return -ENOENT;
Joe Hershberger170ab112012-12-11 22:16:24 -0600308}
Joe Hershbergerbdf1fe42015-05-20 14:27:20 -0500309#endif