]> git.ipfire.org Git - people/ms/u-boot.git/blame - common/env_attr.c
env: Simplify the reverse_strstr() interface
[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 *
1a459660 5 * SPDX-License-Identifier: GPL-2.0+
170ab110
JH
6 */
7
30fd4fad
JH
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
170ab110 13#include <common.h>
30fd4fad
JH
14#endif
15
170ab110
JH
16#include <env_attr.h>
17#include <errno.h>
18#include <linux/string.h>
19#include <malloc.h>
20
21/*
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]
26 * list = entry[,list]
27 */
28int env_attr_walk(const char *attr_list,
29 int (*callback)(const char *name, const char *attributes))
30{
31 const char *entry, *entry_end;
32 char *name, *attributes;
33
34 if (!attr_list)
35 /* list not found */
36 return 1;
37
38 entry = attr_list;
39 do {
40 char *entry_cpy = NULL;
41
42 entry_end = strchr(entry, ENV_ATTR_LIST_DELIM);
43 /* check if this is the last entry in the list */
44 if (entry_end == NULL) {
45 int entry_len = strlen(entry);
46
47 if (entry_len) {
48 /*
49 * allocate memory to copy the entry into since
50 * we will need to inject '\0' chars and squash
51 * white-space before calling the callback
52 */
53 entry_cpy = malloc(entry_len + 1);
54 if (entry_cpy)
55 /* copy the rest of the list */
56 strcpy(entry_cpy, entry);
57 else
58 return -ENOMEM;
59 }
60 } else {
61 int entry_len = entry_end - 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 just this entry and null term */
72 strncpy(entry_cpy, entry, entry_len);
73 entry_cpy[entry_len] = '\0';
74 } else
75 return -ENOMEM;
76 }
77 }
78
79 /* check if there is anything to process (e.g. not ",,,") */
80 if (entry_cpy != NULL) {
81 attributes = strchr(entry_cpy, ENV_ATTR_SEP);
82 /* check if there is a ':' */
83 if (attributes != NULL) {
84 /* replace the ':' with '\0' to term name */
85 *attributes++ = '\0';
86 /* remove white-space from attributes */
87 attributes = strim(attributes);
88 }
89 /* remove white-space from name */
90 name = strim(entry_cpy);
91
92 /* only call the callback if there is a name */
93 if (strlen(name) != 0) {
94 int retval = 0;
95
96 retval = callback(name, attributes);
97 if (retval) {
98 free(entry_cpy);
99 return retval;
100 }
101 }
102 }
103
104 free(entry_cpy);
105 entry = entry_end + 1;
106 } while (entry_end != NULL);
107
108 return 0;
109}
110
111/*
032ea185 112 * Search for the last exactly matching name in an attribute list
170ab110 113 */
032ea185
JH
114static int reverse_name_search(const char *searched, const char *search_for,
115 const char **result)
170ab110 116{
032ea185
JH
117 int result_size = 0;
118 const char *cur_searched = searched;
170ab110 119
032ea185
JH
120 if (result)
121 *result = NULL;
122
123 if (*search_for == '\0') {
124 if (result)
125 *result = searched;
126 return strlen(searched);
127 }
170ab110
JH
128
129 for (;;) {
032ea185
JH
130 const char *match = strstr(cur_searched, search_for);
131 const char *prevch;
132 const char *nextch;
133
134 /* Stop looking if no new match is found */
135 if (match == NULL)
170ab110
JH
136 break;
137
032ea185
JH
138 prevch = match - 1;
139 nextch = match + strlen(search_for);
140
141 /* Skip spaces */
142 while (*prevch == ' ' && prevch >= searched)
143 prevch--;
144 while (*nextch == ' ')
145 nextch++;
146
147 /* Start looking past the current match so last is found */
148 cur_searched = match + 1;
149 /* Check for an exact match */
150 if (match != searched &&
151 *prevch != ENV_ATTR_LIST_DELIM &&
152 prevch != searched - 1)
153 continue;
154 if (*nextch != ENV_ATTR_SEP &&
155 *nextch != ENV_ATTR_LIST_DELIM &&
156 *nextch != '\0')
157 continue;
158
159 if (result)
160 *result = match;
161 result_size = strlen(search_for);
170ab110
JH
162 }
163
032ea185 164 return result_size;
170ab110
JH
165}
166
167/*
168 * Retrieve the attributes string associated with a single name in the list
169 * There is no protection on attributes being too small for the value
170 */
171int env_attr_lookup(const char *attr_list, const char *name, char *attributes)
172{
173 const char *entry = NULL;
032ea185 174 int entry_len;
170ab110
JH
175
176 if (!attributes)
177 /* bad parameter */
7a0ad2cc 178 return -EINVAL;
170ab110
JH
179 if (!attr_list)
180 /* list not found */
7a0ad2cc 181 return -EINVAL;
170ab110 182
032ea185 183 entry_len = reverse_name_search(attr_list, name, &entry);
170ab110
JH
184 if (entry != NULL) {
185 int len;
186
187 /* skip the name */
032ea185 188 entry += entry_len;
170ab110
JH
189 /* skip spaces */
190 while (*entry == ' ')
191 entry++;
192 if (*entry != ENV_ATTR_SEP)
193 len = 0;
194 else {
195 const char *delim;
196 static const char delims[] = {
197 ENV_ATTR_LIST_DELIM, ' ', '\0'};
198
199 /* skip the attr sep */
200 entry += 1;
201 /* skip spaces */
202 while (*entry == ' ')
203 entry++;
204
205 delim = strpbrk(entry, delims);
206 if (delim == NULL)
207 len = strlen(entry);
208 else
209 len = delim - entry;
210 memcpy(attributes, entry, len);
211 }
212 attributes[len] = '\0';
213
214 /* success */
215 return 0;
216 }
217
218 /* not found in list */
7a0ad2cc 219 return -ENOENT;
170ab110 220}