]> git.ipfire.org Git - people/ms/u-boot.git/blame - common/env_attr.c
Licenses: introduce SPDX Unique Lincense Identifiers
[people/ms/u-boot.git] / common / env_attr.c
CommitLineData
170ab110
JH
1/*
2 * (C) Copyright 2012
3 * Joe Hershberger, National Instruments, joe.hershberger@ni.com
4 *
5 * See file CREDITS for list of people who contributed to this
6 * project.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 * MA 02111-1307 USA
22 */
23
30fd4fad
JH
24#ifdef USE_HOSTCC /* Eliminate "ANSI does not permit..." warnings */
25#include <stdint.h>
26#include <stdio.h>
27#include <linux/linux_string.h>
28#else
170ab110 29#include <common.h>
30fd4fad
JH
30#endif
31
170ab110
JH
32#include <env_attr.h>
33#include <errno.h>
34#include <linux/string.h>
35#include <malloc.h>
36
37/*
38 * Iterate through the whole list calling the callback for each found element.
39 * "attr_list" takes the form:
40 * attributes = [^,:\s]*
41 * entry = name[:attributes]
42 * list = entry[,list]
43 */
44int env_attr_walk(const char *attr_list,
45 int (*callback)(const char *name, const char *attributes))
46{
47 const char *entry, *entry_end;
48 char *name, *attributes;
49
50 if (!attr_list)
51 /* list not found */
52 return 1;
53
54 entry = attr_list;
55 do {
56 char *entry_cpy = NULL;
57
58 entry_end = strchr(entry, ENV_ATTR_LIST_DELIM);
59 /* check if this is the last entry in the list */
60 if (entry_end == NULL) {
61 int entry_len = strlen(entry);
62
63 if (entry_len) {
64 /*
65 * allocate memory to copy the entry into since
66 * we will need to inject '\0' chars and squash
67 * white-space before calling the callback
68 */
69 entry_cpy = malloc(entry_len + 1);
70 if (entry_cpy)
71 /* copy the rest of the list */
72 strcpy(entry_cpy, entry);
73 else
74 return -ENOMEM;
75 }
76 } else {
77 int entry_len = entry_end - entry;
78
79 if (entry_len) {
80 /*
81 * allocate memory to copy the entry into since
82 * we will need to inject '\0' chars and squash
83 * white-space before calling the callback
84 */
85 entry_cpy = malloc(entry_len + 1);
86 if (entry_cpy) {
87 /* copy just this entry and null term */
88 strncpy(entry_cpy, entry, entry_len);
89 entry_cpy[entry_len] = '\0';
90 } else
91 return -ENOMEM;
92 }
93 }
94
95 /* check if there is anything to process (e.g. not ",,,") */
96 if (entry_cpy != NULL) {
97 attributes = strchr(entry_cpy, ENV_ATTR_SEP);
98 /* check if there is a ':' */
99 if (attributes != NULL) {
100 /* replace the ':' with '\0' to term name */
101 *attributes++ = '\0';
102 /* remove white-space from attributes */
103 attributes = strim(attributes);
104 }
105 /* remove white-space from name */
106 name = strim(entry_cpy);
107
108 /* only call the callback if there is a name */
109 if (strlen(name) != 0) {
110 int retval = 0;
111
112 retval = callback(name, attributes);
113 if (retval) {
114 free(entry_cpy);
115 return retval;
116 }
117 }
118 }
119
120 free(entry_cpy);
121 entry = entry_end + 1;
122 } while (entry_end != NULL);
123
124 return 0;
125}
126
127/*
128 * Search for the last matching string in another string with the option to
129 * start looking at a certain point (i.e. ignore anything beyond that point).
130 */
131static char *reverse_strstr(const char *searched, const char *search_for,
132 const char *searched_start)
133{
134 char *result = NULL;
135
136 if (*search_for == '\0')
137 return (char *)searched;
138
139 for (;;) {
140 char *match = strstr(searched, search_for);
141
142 /*
143 * Stop looking if no new match is found or looking past the
144 * searched_start pointer
145 */
146 if (match == NULL || (searched_start != NULL &&
147 match + strlen(search_for) > searched_start))
148 break;
149
150 result = match;
151 searched = match + 1;
152 }
153
154 return result;
155}
156
157/*
158 * Retrieve the attributes string associated with a single name in the list
159 * There is no protection on attributes being too small for the value
160 */
161int env_attr_lookup(const char *attr_list, const char *name, char *attributes)
162{
163 const char *entry = NULL;
164
165 if (!attributes)
166 /* bad parameter */
167 return -1;
168 if (!attr_list)
169 /* list not found */
170 return 1;
171
172 entry = reverse_strstr(attr_list, name, NULL);
173 while (entry != NULL) {
174 const char *prevch = entry - 1;
175 const char *nextch = entry + strlen(name);
176
177 /* Skip spaces */
178 while (*prevch == ' ')
179 prevch--;
180 while (*nextch == ' ')
181 nextch++;
182
183 /* check for an exact match */
184 if ((entry == attr_list ||
185 *prevch == ENV_ATTR_LIST_DELIM) &&
186 (*nextch == ENV_ATTR_SEP ||
187 *nextch == ENV_ATTR_LIST_DELIM ||
188 *nextch == '\0'))
189 break;
190
191 entry = reverse_strstr(attr_list, name, entry);
192 }
193 if (entry != NULL) {
194 int len;
195
196 /* skip the name */
197 entry += strlen(name);
198 /* skip spaces */
199 while (*entry == ' ')
200 entry++;
201 if (*entry != ENV_ATTR_SEP)
202 len = 0;
203 else {
204 const char *delim;
205 static const char delims[] = {
206 ENV_ATTR_LIST_DELIM, ' ', '\0'};
207
208 /* skip the attr sep */
209 entry += 1;
210 /* skip spaces */
211 while (*entry == ' ')
212 entry++;
213
214 delim = strpbrk(entry, delims);
215 if (delim == NULL)
216 len = strlen(entry);
217 else
218 len = delim - entry;
219 memcpy(attributes, entry, len);
220 }
221 attributes[len] = '\0';
222
223 /* success */
224 return 0;
225 }
226
227 /* not found in list */
228 return 2;
229}