Blame view

env/attr.c 6.68 KB
83d290c56   Tom Rini   SPDX: Convert all...
1
  // SPDX-License-Identifier: GPL-2.0+
170ab1107   Joe Hershberger   env: Add support ...
2
3
4
  /*
   * (C) Copyright 2012
   * Joe Hershberger, National Instruments, joe.hershberger@ni.com
170ab1107   Joe Hershberger   env: Add support ...
5
   */
30fd4fadb   Joe Hershberger   tools/env: Add en...
6
7
8
9
10
  #ifdef USE_HOSTCC /* Eliminate "ANSI does not permit..." warnings */
  #include <stdint.h>
  #include <stdio.h>
  #include <linux/linux_string.h>
  #else
170ab1107   Joe Hershberger   env: Add support ...
11
  #include <common.h>
bdf1fe4e6   Joe Hershberger   env: Add regex su...
12
  #include <slre.h>
30fd4fadb   Joe Hershberger   tools/env: Add en...
13
  #endif
170ab1107   Joe Hershberger   env: Add support ...
14
15
16
17
18
19
20
21
22
23
24
25
26
  #include <env_attr.h>
  #include <errno.h>
  #include <linux/string.h>
  #include <malloc.h>
  
  /*
   * Iterate through the whole list calling the callback for each found element.
   * "attr_list" takes the form:
   *	attributes = [^,:\s]*
   *	entry = name[:attributes]
   *	list = entry[,list]
   */
  int env_attr_walk(const char *attr_list,
cca98fd6a   Joe Hershberger   env: Allow env_at...
27
28
  	int (*callback)(const char *name, const char *attributes, void *priv),
  	void *priv)
170ab1107   Joe Hershberger   env: Add support ...
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
  {
  	const char *entry, *entry_end;
  	char *name, *attributes;
  
  	if (!attr_list)
  		/* list not found */
  		return 1;
  
  	entry = attr_list;
  	do {
  		char *entry_cpy = NULL;
  
  		entry_end = strchr(entry, ENV_ATTR_LIST_DELIM);
  		/* check if this is the last entry in the list */
  		if (entry_end == NULL) {
  			int entry_len = strlen(entry);
  
  			if (entry_len) {
  				/*
  				 * allocate memory to copy the entry into since
  				 * we will need to inject '\0' chars and squash
  				 * white-space before calling the callback
  				 */
  				entry_cpy = malloc(entry_len + 1);
  				if (entry_cpy)
  					/* copy the rest of the list */
  					strcpy(entry_cpy, entry);
  				else
  					return -ENOMEM;
  			}
  		} else {
  			int entry_len = entry_end - entry;
  
  			if (entry_len) {
  				/*
  				 * allocate memory to copy the entry into since
  				 * we will need to inject '\0' chars and squash
  				 * white-space before calling the callback
  				 */
  				entry_cpy = malloc(entry_len + 1);
  				if (entry_cpy) {
  					/* copy just this entry and null term */
  					strncpy(entry_cpy, entry, entry_len);
  					entry_cpy[entry_len] = '\0';
  				} else
  					return -ENOMEM;
  			}
  		}
  
  		/* check if there is anything to process (e.g. not ",,,") */
  		if (entry_cpy != NULL) {
  			attributes = strchr(entry_cpy, ENV_ATTR_SEP);
  			/* check if there is a ':' */
  			if (attributes != NULL) {
  				/* replace the ':' with '\0' to term name */
  				*attributes++ = '\0';
  				/* remove white-space from attributes */
  				attributes = strim(attributes);
  			}
  			/* remove white-space from name */
  			name = strim(entry_cpy);
  
  			/* only call the callback if there is a name */
  			if (strlen(name) != 0) {
  				int retval = 0;
cca98fd6a   Joe Hershberger   env: Allow env_at...
94
  				retval = callback(name, attributes, priv);
170ab1107   Joe Hershberger   env: Add support ...
95
96
97
98
99
100
101
102
103
104
105
106
107
  				if (retval) {
  					free(entry_cpy);
  					return retval;
  				}
  			}
  		}
  
  		free(entry_cpy);
  		entry = entry_end + 1;
  	} while (entry_end != NULL);
  
  	return 0;
  }
bdf1fe4e6   Joe Hershberger   env: Add regex su...
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
  #if defined(CONFIG_REGEX)
  struct regex_callback_priv {
  	const char *searched_for;
  	char *regex;
  	char *attributes;
  };
  
  static int regex_callback(const char *name, const char *attributes, void *priv)
  {
  	int retval = 0;
  	struct regex_callback_priv *cbp = (struct regex_callback_priv *)priv;
  	struct slre slre;
  	char regex[strlen(name) + 3];
  
  	/* Require the whole string to be described by the regex */
  	sprintf(regex, "^%s$", name);
  	if (slre_compile(&slre, regex)) {
  		struct cap caps[slre.num_caps + 2];
  
  		if (slre_match(&slre, cbp->searched_for,
  			       strlen(cbp->searched_for), caps)) {
  			free(cbp->regex);
902f5bcfb   xypron.glpk@gmx.de   env: avoid possib...
130
131
132
133
  			if (!attributes) {
  				retval = -EINVAL;
  				goto done;
  			}
bdf1fe4e6   Joe Hershberger   env: Add regex su...
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
  			cbp->regex = malloc(strlen(regex) + 1);
  			if (cbp->regex) {
  				strcpy(cbp->regex, regex);
  			} else {
  				retval = -ENOMEM;
  				goto done;
  			}
  
  			free(cbp->attributes);
  			cbp->attributes = malloc(strlen(attributes) + 1);
  			if (cbp->attributes) {
  				strcpy(cbp->attributes, attributes);
  			} else {
  				retval = -ENOMEM;
  				free(cbp->regex);
  				cbp->regex = NULL;
  				goto done;
  			}
  		}
  	} else {
  		printf("Error compiling regex: %s
  ", slre.err_str);
9b57e252f   xypron.glpk@gmx.de   env: correct sign...
156
  		retval = -EINVAL;
bdf1fe4e6   Joe Hershberger   env: Add regex su...
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
  	}
  done:
  	return retval;
  }
  
  /*
   * Retrieve the attributes string associated with a single name in the list
   * There is no protection on attributes being too small for the value
   */
  int env_attr_lookup(const char *attr_list, const char *name, char *attributes)
  {
  	if (!attributes)
  		/* bad parameter */
  		return -EINVAL;
  	if (!attr_list)
  		/* list not found */
  		return -EINVAL;
  
  	struct regex_callback_priv priv;
  	int retval;
  
  	priv.searched_for = name;
  	priv.regex = NULL;
  	priv.attributes = NULL;
  	retval = env_attr_walk(attr_list, regex_callback, &priv);
  	if (retval)
  		return retval; /* error */
  
  	if (priv.regex) {
  		strcpy(attributes, priv.attributes);
  		free(priv.attributes);
  		free(priv.regex);
  		/* success */
  		return 0;
  	}
  	return -ENOENT; /* not found in list */
  }
  #else
170ab1107   Joe Hershberger   env: Add support ...
195
  /*
032ea185d   Joe Hershberger   env: Simplify the...
196
   * Search for the last exactly matching name in an attribute list
170ab1107   Joe Hershberger   env: Add support ...
197
   */
032ea185d   Joe Hershberger   env: Simplify the...
198
199
  static int reverse_name_search(const char *searched, const char *search_for,
  	const char **result)
170ab1107   Joe Hershberger   env: Add support ...
200
  {
032ea185d   Joe Hershberger   env: Simplify the...
201
202
  	int result_size = 0;
  	const char *cur_searched = searched;
170ab1107   Joe Hershberger   env: Add support ...
203

032ea185d   Joe Hershberger   env: Simplify the...
204
205
206
207
208
209
210
211
  	if (result)
  		*result = NULL;
  
  	if (*search_for == '\0') {
  		if (result)
  			*result = searched;
  		return strlen(searched);
  	}
170ab1107   Joe Hershberger   env: Add support ...
212
213
  
  	for (;;) {
032ea185d   Joe Hershberger   env: Simplify the...
214
215
216
217
218
219
  		const char *match = strstr(cur_searched, search_for);
  		const char *prevch;
  		const char *nextch;
  
  		/* Stop looking if no new match is found */
  		if (match == NULL)
170ab1107   Joe Hershberger   env: Add support ...
220
  			break;
032ea185d   Joe Hershberger   env: Simplify the...
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
  		prevch = match - 1;
  		nextch = match + strlen(search_for);
  
  		/* Skip spaces */
  		while (*prevch == ' ' && prevch >= searched)
  			prevch--;
  		while (*nextch == ' ')
  			nextch++;
  
  		/* Start looking past the current match so last is found */
  		cur_searched = match + 1;
  		/* Check for an exact match */
  		if (match != searched &&
  		    *prevch != ENV_ATTR_LIST_DELIM &&
  		    prevch != searched - 1)
  			continue;
  		if (*nextch != ENV_ATTR_SEP &&
  		    *nextch != ENV_ATTR_LIST_DELIM &&
  		    *nextch != '\0')
  			continue;
  
  		if (result)
  			*result = match;
  		result_size = strlen(search_for);
170ab1107   Joe Hershberger   env: Add support ...
245
  	}
032ea185d   Joe Hershberger   env: Simplify the...
246
  	return result_size;
170ab1107   Joe Hershberger   env: Add support ...
247
248
249
250
251
252
253
254
255
  }
  
  /*
   * Retrieve the attributes string associated with a single name in the list
   * There is no protection on attributes being too small for the value
   */
  int env_attr_lookup(const char *attr_list, const char *name, char *attributes)
  {
  	const char *entry = NULL;
032ea185d   Joe Hershberger   env: Simplify the...
256
  	int entry_len;
170ab1107   Joe Hershberger   env: Add support ...
257
258
259
  
  	if (!attributes)
  		/* bad parameter */
7a0ad2cce   Joe Hershberger   env: Fix return v...
260
  		return -EINVAL;
170ab1107   Joe Hershberger   env: Add support ...
261
262
  	if (!attr_list)
  		/* list not found */
7a0ad2cce   Joe Hershberger   env: Fix return v...
263
  		return -EINVAL;
170ab1107   Joe Hershberger   env: Add support ...
264

032ea185d   Joe Hershberger   env: Simplify the...
265
  	entry_len = reverse_name_search(attr_list, name, &entry);
170ab1107   Joe Hershberger   env: Add support ...
266
267
268
269
  	if (entry != NULL) {
  		int len;
  
  		/* skip the name */
032ea185d   Joe Hershberger   env: Simplify the...
270
  		entry += entry_len;
170ab1107   Joe Hershberger   env: Add support ...
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
  		/* skip spaces */
  		while (*entry == ' ')
  			entry++;
  		if (*entry != ENV_ATTR_SEP)
  			len = 0;
  		else {
  			const char *delim;
  			static const char delims[] = {
  				ENV_ATTR_LIST_DELIM, ' ', '\0'};
  
  			/* skip the attr sep */
  			entry += 1;
  			/* skip spaces */
  			while (*entry == ' ')
  				entry++;
  
  			delim = strpbrk(entry, delims);
  			if (delim == NULL)
  				len = strlen(entry);
  			else
  				len = delim - entry;
  			memcpy(attributes, entry, len);
  		}
  		attributes[len] = '\0';
  
  		/* success */
  		return 0;
  	}
  
  	/* not found in list */
7a0ad2cce   Joe Hershberger   env: Fix return v...
301
  	return -ENOENT;
170ab1107   Joe Hershberger   env: Add support ...
302
  }
bdf1fe4e6   Joe Hershberger   env: Add regex su...
303
  #endif