1 // SPDX-License-Identifier: GPL-2.0+
4 * Joe Hershberger, National Instruments, joe.hershberger@ni.com
7 #ifdef USE_HOSTCC /* Eliminate "ANSI does not permit..." warnings */
10 #include <linux/linux_string.h>
18 #include <linux/string.h>
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]
28 int env_attr_walk(const char *attr_list,
29 int (*callback)(const char *name, const char *attributes, void *priv),
32 const char *entry, *entry_end;
33 char *name, *attributes;
41 char *entry_cpy = NULL;
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);
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
54 entry_cpy = malloc(entry_len + 1);
56 /* copy the rest of the list */
57 strcpy(entry_cpy, entry);
62 int entry_len = entry_end - entry;
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
70 entry_cpy = malloc(entry_len + 1);
72 /* copy just this entry and null term */
73 strncpy(entry_cpy, entry, entry_len);
74 entry_cpy[entry_len] = '\0';
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 */
87 /* remove white-space from attributes */
88 attributes = strim(attributes);
90 /* remove white-space from name */
91 name = strim(entry_cpy);
93 /* only call the callback if there is a name */
94 if (strlen(name) != 0) {
97 retval = callback(name, attributes, priv);
106 entry = entry_end + 1;
107 } while (entry_end != NULL);
112 #if defined(CONFIG_REGEX)
113 struct regex_callback_priv {
114 const char *searched_for;
119 static int regex_callback(const char *name, const char *attributes, void *priv)
122 struct regex_callback_priv *cbp = (struct regex_callback_priv *)priv;
124 char regex[strlen(name) + 3];
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];
131 if (slre_match(&slre, cbp->searched_for,
132 strlen(cbp->searched_for), caps)) {
138 cbp->regex = malloc(strlen(regex) + 1);
140 strcpy(cbp->regex, regex);
146 free(cbp->attributes);
147 cbp->attributes = malloc(strlen(attributes) + 1);
148 if (cbp->attributes) {
149 strcpy(cbp->attributes, attributes);
158 printf("Error compiling regex: %s\n", slre.err_str);
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
169 int env_attr_lookup(const char *attr_list, const char *name, char *attributes)
178 struct regex_callback_priv priv;
181 priv.searched_for = name;
183 priv.attributes = NULL;
184 retval = env_attr_walk(attr_list, regex_callback, &priv);
186 return retval; /* error */
189 strcpy(attributes, priv.attributes);
190 free(priv.attributes);
195 return -ENOENT; /* not found in list */
200 * Search for the last exactly matching name in an attribute list
202 static int reverse_name_search(const char *searched, const char *search_for,
206 const char *cur_searched = searched;
211 if (*search_for == '\0') {
214 return strlen(searched);
218 const char *match = strstr(cur_searched, search_for);
222 /* Stop looking if no new match is found */
227 nextch = match + strlen(search_for);
230 while (*prevch == ' ' && prevch >= searched)
232 while (*nextch == ' ')
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)
242 if (*nextch != ENV_ATTR_SEP &&
243 *nextch != ENV_ATTR_LIST_DELIM &&
249 result_size = strlen(search_for);
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
259 int env_attr_lookup(const char *attr_list, const char *name, char *attributes)
261 const char *entry = NULL;
271 entry_len = reverse_name_search(attr_list, name, &entry);
278 while (*entry == ' ')
280 if (*entry != ENV_ATTR_SEP)
284 static const char delims[] = {
285 ENV_ATTR_LIST_DELIM, ' ', '\0'};
287 /* skip the attr sep */
290 while (*entry == ' ')
293 delim = strpbrk(entry, delims);
298 memcpy(attributes, entry, len);
300 attributes[len] = '\0';
306 /* not found in list */