3 * Joe Hershberger, National Instruments, joe.hershberger@ni.com
5 * SPDX-License-Identifier: GPL-2.0+
8 #ifdef USE_HOSTCC /* Eliminate "ANSI does not permit..." warnings */
11 #include <linux/linux_string.h>
19 #include <linux/string.h>
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]
29 int env_attr_walk(const char *attr_list,
30 int (*callback)(const char *name, const char *attributes, void *priv),
33 const char *entry, *entry_end;
34 char *name, *attributes;
42 char *entry_cpy = NULL;
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);
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
55 entry_cpy = malloc(entry_len + 1);
57 /* copy the rest of the list */
58 strcpy(entry_cpy, entry);
63 int entry_len = entry_end - entry;
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
71 entry_cpy = malloc(entry_len + 1);
73 /* copy just this entry and null term */
74 strncpy(entry_cpy, entry, entry_len);
75 entry_cpy[entry_len] = '\0';
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 */
88 /* remove white-space from attributes */
89 attributes = strim(attributes);
91 /* remove white-space from name */
92 name = strim(entry_cpy);
94 /* only call the callback if there is a name */
95 if (strlen(name) != 0) {
98 retval = callback(name, attributes, priv);
107 entry = entry_end + 1;
108 } while (entry_end != NULL);
113 #if defined(CONFIG_REGEX)
114 struct regex_callback_priv {
115 const char *searched_for;
120 static int regex_callback(const char *name, const char *attributes, void *priv)
123 struct regex_callback_priv *cbp = (struct regex_callback_priv *)priv;
125 char regex[strlen(name) + 3];
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];
132 if (slre_match(&slre, cbp->searched_for,
133 strlen(cbp->searched_for), caps)) {
139 cbp->regex = malloc(strlen(regex) + 1);
141 strcpy(cbp->regex, regex);
147 free(cbp->attributes);
148 cbp->attributes = malloc(strlen(attributes) + 1);
149 if (cbp->attributes) {
150 strcpy(cbp->attributes, attributes);
159 printf("Error compiling regex: %s\n", slre.err_str);
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
170 int env_attr_lookup(const char *attr_list, const char *name, char *attributes)
179 struct regex_callback_priv priv;
182 priv.searched_for = name;
184 priv.attributes = NULL;
185 retval = env_attr_walk(attr_list, regex_callback, &priv);
187 return retval; /* error */
190 strcpy(attributes, priv.attributes);
191 free(priv.attributes);
196 return -ENOENT; /* not found in list */
201 * Search for the last exactly matching name in an attribute list
203 static int reverse_name_search(const char *searched, const char *search_for,
207 const char *cur_searched = searched;
212 if (*search_for == '\0') {
215 return strlen(searched);
219 const char *match = strstr(cur_searched, search_for);
223 /* Stop looking if no new match is found */
228 nextch = match + strlen(search_for);
231 while (*prevch == ' ' && prevch >= searched)
233 while (*nextch == ' ')
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)
243 if (*nextch != ENV_ATTR_SEP &&
244 *nextch != ENV_ATTR_LIST_DELIM &&
250 result_size = strlen(search_for);
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
260 int env_attr_lookup(const char *attr_list, const char *name, char *attributes)
262 const char *entry = NULL;
272 entry_len = reverse_name_search(attr_list, name, &entry);
279 while (*entry == ' ')
281 if (*entry != ENV_ATTR_SEP)
285 static const char delims[] = {
286 ENV_ATTR_LIST_DELIM, ' ', '\0'};
288 /* skip the attr sep */
291 while (*entry == ' ')
294 delim = strpbrk(entry, delims);
299 memcpy(attributes, entry, len);
301 attributes[len] = '\0';
307 /* not found in list */