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