env: Add regex support to env_attrs
[platform/kernel/u-boot.git] / common / env_attr.c
1 /*
2  * (C) Copyright 2012
3  * Joe Hershberger, National Instruments, joe.hershberger@ni.com
4  *
5  * SPDX-License-Identifier:     GPL-2.0+
6  */
7
8 #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
13 #include <common.h>
14 #include <slre.h>
15 #endif
16
17 #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  */
29 int env_attr_walk(const char *attr_list,
30         int (*callback)(const char *name, const char *attributes, void *priv),
31         void *priv)
32 {
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
98                                 retval = callback(name, attributes, priv);
99                                 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
113 #if defined(CONFIG_REGEX)
114 struct regex_callback_priv {
115         const char *searched_for;
116         char *regex;
117         char *attributes;
118 };
119
120 static 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         }
158 done:
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  */
166 int 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
196 /*
197  * Search for the last exactly matching name in an attribute list
198  */
199 static int reverse_name_search(const char *searched, const char *search_for,
200         const char **result)
201 {
202         int result_size = 0;
203         const char *cur_searched = searched;
204
205         if (result)
206                 *result = NULL;
207
208         if (*search_for == '\0') {
209                 if (result)
210                         *result = searched;
211                 return strlen(searched);
212         }
213
214         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;
250 }
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  */
256 int env_attr_lookup(const char *attr_list, const char *name, char *attributes)
257 {
258         const char *entry = NULL;
259         int entry_len;
260
261         if (!attributes)
262                 /* bad parameter */
263                 return -EINVAL;
264         if (!attr_list)
265                 /* list not found */
266                 return -EINVAL;
267
268         entry_len = reverse_name_search(attr_list, name, &entry);
269         if (entry != NULL) {
270                 int len;
271
272                 /* skip the name */
273                 entry += entry_len;
274                 /* 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 */
304         return -ENOENT;
305 }
306 #endif