]>
git.ipfire.org Git - people/ms/u-boot.git/blob - common/env_attr.c
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
)) {
135 cbp
->regex
= malloc(strlen(regex
) + 1);
137 strcpy(cbp
->regex
, regex
);
143 free(cbp
->attributes
);
144 cbp
->attributes
= malloc(strlen(attributes
) + 1);
145 if (cbp
->attributes
) {
146 strcpy(cbp
->attributes
, attributes
);
155 printf("Error compiling regex: %s\n", slre
.err_str
);
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
166 int env_attr_lookup(const char *attr_list
, const char *name
, char *attributes
)
175 struct regex_callback_priv priv
;
178 priv
.searched_for
= name
;
180 priv
.attributes
= NULL
;
181 retval
= env_attr_walk(attr_list
, regex_callback
, &priv
);
183 return retval
; /* error */
186 strcpy(attributes
, priv
.attributes
);
187 free(priv
.attributes
);
192 return -ENOENT
; /* not found in list */
197 * Search for the last exactly matching name in an attribute list
199 static int reverse_name_search(const char *searched
, const char *search_for
,
203 const char *cur_searched
= searched
;
208 if (*search_for
== '\0') {
211 return strlen(searched
);
215 const char *match
= strstr(cur_searched
, search_for
);
219 /* Stop looking if no new match is found */
224 nextch
= match
+ strlen(search_for
);
227 while (*prevch
== ' ' && prevch
>= searched
)
229 while (*nextch
== ' ')
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)
239 if (*nextch
!= ENV_ATTR_SEP
&&
240 *nextch
!= ENV_ATTR_LIST_DELIM
&&
246 result_size
= strlen(search_for
);
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
256 int env_attr_lookup(const char *attr_list
, const char *name
, char *attributes
)
258 const char *entry
= NULL
;
268 entry_len
= reverse_name_search(attr_list
, name
, &entry
);
275 while (*entry
== ' ')
277 if (*entry
!= ENV_ATTR_SEP
)
281 static const char delims
[] = {
282 ENV_ATTR_LIST_DELIM
, ' ', '\0'};
284 /* skip the attr sep */
287 while (*entry
== ' ')
290 delim
= strpbrk(entry
, delims
);
295 memcpy(attributes
, entry
, len
);
297 attributes
[len
] = '\0';
303 /* not found in list */