blob: f965b4bbb6a917fb5f27642bc88b66f73ac2da96 [file] [log] [blame]
Joe Hershberger60fd3ad2012-12-11 22:16:24 -06001/*
2 * (C) Copyright 2012
3 * Joe Hershberger, National Instruments, joe.hershberger@ni.com
4 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
Joe Hershberger60fd3ad2012-12-11 22:16:24 -06006 */
7
Joe Hershbergerdd7750a2012-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 Hershberger60fd3ad2012-12-11 22:16:24 -060013#include <common.h>
Joe Hershberger6db9fd42015-05-20 14:27:20 -050014#include <slre.h>
Joe Hershbergerdd7750a2012-12-11 22:16:32 -060015#endif
16
Joe Hershberger60fd3ad2012-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 Hershbergerd741f562015-05-20 14:27:19 -050030 int (*callback)(const char *name, const char *attributes, void *priv),
31 void *priv)
Joe Hershberger60fd3ad2012-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 Hershbergerd741f562015-05-20 14:27:19 -050098 retval = callback(name, attributes, priv);
Joe Hershberger60fd3ad2012-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 Hershberger6db9fd42015-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);
xypron.glpk@gmx.dee83b2222017-05-08 20:23:54 +0200135 if (!attributes) {
136 retval = -EINVAL;
137 goto done;
138 }
Joe Hershberger6db9fd42015-05-20 14:27:20 -0500139 cbp->regex = malloc(strlen(regex) + 1);
140 if (cbp->regex) {
141 strcpy(cbp->regex, regex);
142 } else {
143 retval = -ENOMEM;
144 goto done;
145 }
146
147 free(cbp->attributes);
148 cbp->attributes = malloc(strlen(attributes) + 1);
149 if (cbp->attributes) {
150 strcpy(cbp->attributes, attributes);
151 } else {
152 retval = -ENOMEM;
153 free(cbp->regex);
154 cbp->regex = NULL;
155 goto done;
156 }
157 }
158 } else {
159 printf("Error compiling regex: %s\n", slre.err_str);
xypron.glpk@gmx.de050fe9e2017-05-08 19:30:58 +0200160 retval = -EINVAL;
Joe Hershberger6db9fd42015-05-20 14:27:20 -0500161 }
162done:
163 return retval;
164}
165
166/*
167 * Retrieve the attributes string associated with a single name in the list
168 * There is no protection on attributes being too small for the value
169 */
170int env_attr_lookup(const char *attr_list, const char *name, char *attributes)
171{
172 if (!attributes)
173 /* bad parameter */
174 return -EINVAL;
175 if (!attr_list)
176 /* list not found */
177 return -EINVAL;
178
179 struct regex_callback_priv priv;
180 int retval;
181
182 priv.searched_for = name;
183 priv.regex = NULL;
184 priv.attributes = NULL;
185 retval = env_attr_walk(attr_list, regex_callback, &priv);
186 if (retval)
187 return retval; /* error */
188
189 if (priv.regex) {
190 strcpy(attributes, priv.attributes);
191 free(priv.attributes);
192 free(priv.regex);
193 /* success */
194 return 0;
195 }
196 return -ENOENT; /* not found in list */
197}
198#else
199
Joe Hershberger60fd3ad2012-12-11 22:16:24 -0600200/*
Joe Hershbergere163e1c2015-05-20 14:27:18 -0500201 * Search for the last exactly matching name in an attribute list
Joe Hershberger60fd3ad2012-12-11 22:16:24 -0600202 */
Joe Hershbergere163e1c2015-05-20 14:27:18 -0500203static int reverse_name_search(const char *searched, const char *search_for,
204 const char **result)
Joe Hershberger60fd3ad2012-12-11 22:16:24 -0600205{
Joe Hershbergere163e1c2015-05-20 14:27:18 -0500206 int result_size = 0;
207 const char *cur_searched = searched;
Joe Hershberger60fd3ad2012-12-11 22:16:24 -0600208
Joe Hershbergere163e1c2015-05-20 14:27:18 -0500209 if (result)
210 *result = NULL;
211
212 if (*search_for == '\0') {
213 if (result)
214 *result = searched;
215 return strlen(searched);
216 }
Joe Hershberger60fd3ad2012-12-11 22:16:24 -0600217
218 for (;;) {
Joe Hershbergere163e1c2015-05-20 14:27:18 -0500219 const char *match = strstr(cur_searched, search_for);
220 const char *prevch;
221 const char *nextch;
Joe Hershberger60fd3ad2012-12-11 22:16:24 -0600222
Joe Hershbergere163e1c2015-05-20 14:27:18 -0500223 /* Stop looking if no new match is found */
224 if (match == NULL)
Joe Hershberger60fd3ad2012-12-11 22:16:24 -0600225 break;
226
Joe Hershbergere163e1c2015-05-20 14:27:18 -0500227 prevch = match - 1;
228 nextch = match + strlen(search_for);
229
230 /* Skip spaces */
231 while (*prevch == ' ' && prevch >= searched)
232 prevch--;
233 while (*nextch == ' ')
234 nextch++;
235
236 /* Start looking past the current match so last is found */
237 cur_searched = match + 1;
238 /* Check for an exact match */
239 if (match != searched &&
240 *prevch != ENV_ATTR_LIST_DELIM &&
241 prevch != searched - 1)
242 continue;
243 if (*nextch != ENV_ATTR_SEP &&
244 *nextch != ENV_ATTR_LIST_DELIM &&
245 *nextch != '\0')
246 continue;
247
248 if (result)
249 *result = match;
250 result_size = strlen(search_for);
Joe Hershberger60fd3ad2012-12-11 22:16:24 -0600251 }
252
Joe Hershbergere163e1c2015-05-20 14:27:18 -0500253 return result_size;
Joe Hershberger60fd3ad2012-12-11 22:16:24 -0600254}
255
256/*
257 * Retrieve the attributes string associated with a single name in the list
258 * There is no protection on attributes being too small for the value
259 */
260int env_attr_lookup(const char *attr_list, const char *name, char *attributes)
261{
262 const char *entry = NULL;
Joe Hershbergere163e1c2015-05-20 14:27:18 -0500263 int entry_len;
Joe Hershberger60fd3ad2012-12-11 22:16:24 -0600264
265 if (!attributes)
266 /* bad parameter */
Joe Hershberger253cf4d2015-05-20 14:27:17 -0500267 return -EINVAL;
Joe Hershberger60fd3ad2012-12-11 22:16:24 -0600268 if (!attr_list)
269 /* list not found */
Joe Hershberger253cf4d2015-05-20 14:27:17 -0500270 return -EINVAL;
Joe Hershberger60fd3ad2012-12-11 22:16:24 -0600271
Joe Hershbergere163e1c2015-05-20 14:27:18 -0500272 entry_len = reverse_name_search(attr_list, name, &entry);
Joe Hershberger60fd3ad2012-12-11 22:16:24 -0600273 if (entry != NULL) {
274 int len;
275
276 /* skip the name */
Joe Hershbergere163e1c2015-05-20 14:27:18 -0500277 entry += entry_len;
Joe Hershberger60fd3ad2012-12-11 22:16:24 -0600278 /* skip spaces */
279 while (*entry == ' ')
280 entry++;
281 if (*entry != ENV_ATTR_SEP)
282 len = 0;
283 else {
284 const char *delim;
285 static const char delims[] = {
286 ENV_ATTR_LIST_DELIM, ' ', '\0'};
287
288 /* skip the attr sep */
289 entry += 1;
290 /* skip spaces */
291 while (*entry == ' ')
292 entry++;
293
294 delim = strpbrk(entry, delims);
295 if (delim == NULL)
296 len = strlen(entry);
297 else
298 len = delim - entry;
299 memcpy(attributes, entry, len);
300 }
301 attributes[len] = '\0';
302
303 /* success */
304 return 0;
305 }
306
307 /* not found in list */
Joe Hershberger253cf4d2015-05-20 14:27:17 -0500308 return -ENOENT;
Joe Hershberger60fd3ad2012-12-11 22:16:24 -0600309}
Joe Hershberger6db9fd42015-05-20 14:27:20 -0500310#endif