]>
Commit | Line | Data |
---|---|---|
a6826fbc WD |
1 | /* |
2 | * This implementation is based on code from uClibc-0.9.30.3 but was | |
3 | * modified and extended for use within U-Boot. | |
4 | * | |
ea009d47 | 5 | * Copyright (C) 2010-2013 Wolfgang Denk <wd@denx.de> |
a6826fbc WD |
6 | * |
7 | * Original license header: | |
8 | * | |
9 | * Copyright (C) 1993, 1995, 1996, 1997, 2002 Free Software Foundation, Inc. | |
10 | * This file is part of the GNU C Library. | |
11 | * Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1993. | |
12 | * | |
13 | * The GNU C Library is free software; you can redistribute it and/or | |
14 | * modify it under the terms of the GNU Lesser General Public | |
15 | * License as published by the Free Software Foundation; either | |
16 | * version 2.1 of the License, or (at your option) any later version. | |
17 | * | |
18 | * The GNU C Library is distributed in the hope that it will be useful, | |
19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
21 | * Lesser General Public License for more details. | |
22 | * | |
23 | * You should have received a copy of the GNU Lesser General Public | |
24 | * License along with the GNU C Library; if not, write to the Free | |
25 | * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA | |
26 | * 02111-1307 USA. | |
27 | */ | |
28 | ||
29 | #include <errno.h> | |
30 | #include <malloc.h> | |
31 | ||
32 | #ifdef USE_HOSTCC /* HOST build */ | |
33 | # include <string.h> | |
34 | # include <assert.h> | |
4d91a6ec | 35 | # include <ctype.h> |
a6826fbc WD |
36 | |
37 | # ifndef debug | |
38 | # ifdef DEBUG | |
39 | # define debug(fmt,args...) printf(fmt ,##args) | |
40 | # else | |
41 | # define debug(fmt,args...) | |
42 | # endif | |
43 | # endif | |
44 | #else /* U-Boot build */ | |
45 | # include <common.h> | |
46 | # include <linux/string.h> | |
4d91a6ec | 47 | # include <linux/ctype.h> |
a6826fbc WD |
48 | #endif |
49 | ||
fc5fc76b AB |
50 | #ifndef CONFIG_ENV_MIN_ENTRIES /* minimum number of entries */ |
51 | #define CONFIG_ENV_MIN_ENTRIES 64 | |
52 | #endif | |
ea882baf WD |
53 | #ifndef CONFIG_ENV_MAX_ENTRIES /* maximum number of entries */ |
54 | #define CONFIG_ENV_MAX_ENTRIES 512 | |
55 | #endif | |
56 | ||
170ab110 | 57 | #include <env_callback.h> |
2598090b | 58 | #include <env_flags.h> |
170ab110 | 59 | #include <search.h> |
a6826fbc WD |
60 | |
61 | /* | |
62 | * [Aho,Sethi,Ullman] Compilers: Principles, Techniques and Tools, 1986 | |
071bc923 | 63 | * [Knuth] The Art of Computer Programming, part 3 (6.4) |
a6826fbc WD |
64 | */ |
65 | ||
a6826fbc WD |
66 | /* |
67 | * The reentrant version has no static variables to maintain the state. | |
68 | * Instead the interface of all functions is extended to take an argument | |
69 | * which describes the current status. | |
70 | */ | |
7afcf3a5 | 71 | |
a6826fbc | 72 | typedef struct _ENTRY { |
c81c1222 | 73 | int used; |
a6826fbc WD |
74 | ENTRY entry; |
75 | } _ENTRY; | |
76 | ||
77 | ||
7afcf3a5 JH |
78 | static void _hdelete(const char *key, struct hsearch_data *htab, ENTRY *ep, |
79 | int idx); | |
80 | ||
a6826fbc WD |
81 | /* |
82 | * hcreate() | |
83 | */ | |
84 | ||
85 | /* | |
86 | * For the used double hash method the table size has to be a prime. To | |
87 | * correct the user given table size we need a prime test. This trivial | |
88 | * algorithm is adequate because | |
89 | * a) the code is (most probably) called a few times per program run and | |
90 | * b) the number is small because the table must fit in the core | |
91 | * */ | |
92 | static int isprime(unsigned int number) | |
93 | { | |
94 | /* no even number will be passed */ | |
95 | unsigned int div = 3; | |
96 | ||
97 | while (div * div < number && number % div != 0) | |
98 | div += 2; | |
99 | ||
100 | return number % div != 0; | |
101 | } | |
102 | ||
a6826fbc WD |
103 | /* |
104 | * Before using the hash table we must allocate memory for it. | |
105 | * Test for an existing table are done. We allocate one element | |
106 | * more as the found prime number says. This is done for more effective | |
107 | * indexing as explained in the comment for the hsearch function. | |
108 | * The contents of the table is zeroed, especially the field used | |
109 | * becomes zero. | |
110 | */ | |
2eb1573f | 111 | |
a6826fbc WD |
112 | int hcreate_r(size_t nel, struct hsearch_data *htab) |
113 | { | |
114 | /* Test for correct arguments. */ | |
115 | if (htab == NULL) { | |
116 | __set_errno(EINVAL); | |
117 | return 0; | |
118 | } | |
119 | ||
120 | /* There is still another table active. Return with error. */ | |
121 | if (htab->table != NULL) | |
122 | return 0; | |
123 | ||
124 | /* Change nel to the first prime number not smaller as nel. */ | |
125 | nel |= 1; /* make odd */ | |
126 | while (!isprime(nel)) | |
127 | nel += 2; | |
128 | ||
129 | htab->size = nel; | |
130 | htab->filled = 0; | |
131 | ||
132 | /* allocate memory and zero out */ | |
133 | htab->table = (_ENTRY *) calloc(htab->size + 1, sizeof(_ENTRY)); | |
134 | if (htab->table == NULL) | |
135 | return 0; | |
136 | ||
137 | /* everything went alright */ | |
138 | return 1; | |
139 | } | |
140 | ||
141 | ||
142 | /* | |
143 | * hdestroy() | |
144 | */ | |
a6826fbc WD |
145 | |
146 | /* | |
147 | * After using the hash table it has to be destroyed. The used memory can | |
148 | * be freed and the local static variable can be marked as not used. | |
149 | */ | |
2eb1573f | 150 | |
c4e0057f | 151 | void hdestroy_r(struct hsearch_data *htab) |
a6826fbc WD |
152 | { |
153 | int i; | |
154 | ||
155 | /* Test for correct arguments. */ | |
156 | if (htab == NULL) { | |
157 | __set_errno(EINVAL); | |
158 | return; | |
159 | } | |
160 | ||
161 | /* free used memory */ | |
162 | for (i = 1; i <= htab->size; ++i) { | |
c81c1222 | 163 | if (htab->table[i].used > 0) { |
a6826fbc | 164 | ENTRY *ep = &htab->table[i].entry; |
c4e0057f | 165 | |
84b5e802 | 166 | free((void *)ep->key); |
a6826fbc WD |
167 | free(ep->data); |
168 | } | |
169 | } | |
170 | free(htab->table); | |
171 | ||
172 | /* the sign for an existing table is an value != NULL in htable */ | |
173 | htab->table = NULL; | |
174 | } | |
175 | ||
176 | /* | |
177 | * hsearch() | |
178 | */ | |
179 | ||
180 | /* | |
181 | * This is the search function. It uses double hashing with open addressing. | |
182 | * The argument item.key has to be a pointer to an zero terminated, most | |
183 | * probably strings of chars. The function for generating a number of the | |
184 | * strings is simple but fast. It can be replaced by a more complex function | |
185 | * like ajw (see [Aho,Sethi,Ullman]) if the needs are shown. | |
186 | * | |
187 | * We use an trick to speed up the lookup. The table is created by hcreate | |
188 | * with one more element available. This enables us to use the index zero | |
189 | * special. This index will never be used because we store the first hash | |
190 | * index in the field used where zero means not used. Every other value | |
191 | * means used. The used field can be used as a first fast comparison for | |
192 | * equality of the stored and the parameter value. This helps to prevent | |
193 | * unnecessary expensive calls of strcmp. | |
194 | * | |
195 | * This implementation differs from the standard library version of | |
196 | * this function in a number of ways: | |
197 | * | |
198 | * - While the standard version does not make any assumptions about | |
199 | * the type of the stored data objects at all, this implementation | |
200 | * works with NUL terminated strings only. | |
201 | * - Instead of storing just pointers to the original objects, we | |
202 | * create local copies so the caller does not need to care about the | |
203 | * data any more. | |
204 | * - The standard implementation does not provide a way to update an | |
205 | * existing entry. This version will create a new entry or update an | |
206 | * existing one when both "action == ENTER" and "item.data != NULL". | |
207 | * - Instead of returning 1 on success, we return the index into the | |
208 | * internal hash table, which is also guaranteed to be positive. | |
209 | * This allows us direct access to the found hash table slot for | |
210 | * example for functions like hdelete(). | |
211 | */ | |
212 | ||
560d424b MF |
213 | int hmatch_r(const char *match, int last_idx, ENTRY ** retval, |
214 | struct hsearch_data *htab) | |
215 | { | |
216 | unsigned int idx; | |
217 | size_t key_len = strlen(match); | |
218 | ||
219 | for (idx = last_idx + 1; idx < htab->size; ++idx) { | |
af4d9074 | 220 | if (htab->table[idx].used <= 0) |
560d424b MF |
221 | continue; |
222 | if (!strncmp(match, htab->table[idx].entry.key, key_len)) { | |
223 | *retval = &htab->table[idx].entry; | |
224 | return idx; | |
225 | } | |
226 | } | |
227 | ||
228 | __set_errno(ESRCH); | |
229 | *retval = NULL; | |
230 | return 0; | |
231 | } | |
232 | ||
3d3b52f2 JH |
233 | /* |
234 | * Compare an existing entry with the desired key, and overwrite if the action | |
235 | * is ENTER. This is simply a helper function for hsearch_r(). | |
236 | */ | |
237 | static inline int _compare_and_overwrite_entry(ENTRY item, ACTION action, | |
238 | ENTRY **retval, struct hsearch_data *htab, int flag, | |
239 | unsigned int hval, unsigned int idx) | |
240 | { | |
241 | if (htab->table[idx].used == hval | |
242 | && strcmp(item.key, htab->table[idx].entry.key) == 0) { | |
243 | /* Overwrite existing value? */ | |
244 | if ((action == ENTER) && (item.data != NULL)) { | |
7afcf3a5 JH |
245 | /* check for permission */ |
246 | if (htab->change_ok != NULL && htab->change_ok( | |
247 | &htab->table[idx].entry, item.data, | |
248 | env_op_overwrite, flag)) { | |
249 | debug("change_ok() rejected setting variable " | |
250 | "%s, skipping it!\n", item.key); | |
251 | __set_errno(EPERM); | |
252 | *retval = NULL; | |
253 | return 0; | |
254 | } | |
255 | ||
170ab110 JH |
256 | /* If there is a callback, call it */ |
257 | if (htab->table[idx].entry.callback && | |
258 | htab->table[idx].entry.callback(item.key, | |
259 | item.data, env_op_overwrite, flag)) { | |
260 | debug("callback() rejected setting variable " | |
261 | "%s, skipping it!\n", item.key); | |
262 | __set_errno(EINVAL); | |
263 | *retval = NULL; | |
264 | return 0; | |
265 | } | |
266 | ||
3d3b52f2 JH |
267 | free(htab->table[idx].entry.data); |
268 | htab->table[idx].entry.data = strdup(item.data); | |
269 | if (!htab->table[idx].entry.data) { | |
270 | __set_errno(ENOMEM); | |
271 | *retval = NULL; | |
272 | return 0; | |
273 | } | |
274 | } | |
275 | /* return found entry */ | |
276 | *retval = &htab->table[idx].entry; | |
277 | return idx; | |
278 | } | |
279 | /* keep searching */ | |
280 | return -1; | |
281 | } | |
282 | ||
a6826fbc | 283 | int hsearch_r(ENTRY item, ACTION action, ENTRY ** retval, |
c4e0057f | 284 | struct hsearch_data *htab, int flag) |
a6826fbc WD |
285 | { |
286 | unsigned int hval; | |
287 | unsigned int count; | |
288 | unsigned int len = strlen(item.key); | |
289 | unsigned int idx; | |
c81c1222 | 290 | unsigned int first_deleted = 0; |
3d3b52f2 | 291 | int ret; |
a6826fbc WD |
292 | |
293 | /* Compute an value for the given string. Perhaps use a better method. */ | |
294 | hval = len; | |
295 | count = len; | |
296 | while (count-- > 0) { | |
297 | hval <<= 4; | |
298 | hval += item.key[count]; | |
299 | } | |
300 | ||
301 | /* | |
302 | * First hash function: | |
303 | * simply take the modul but prevent zero. | |
304 | */ | |
305 | hval %= htab->size; | |
306 | if (hval == 0) | |
307 | ++hval; | |
308 | ||
309 | /* The first index tried. */ | |
310 | idx = hval; | |
311 | ||
312 | if (htab->table[idx].used) { | |
313 | /* | |
071bc923 | 314 | * Further action might be required according to the |
a6826fbc WD |
315 | * action value. |
316 | */ | |
317 | unsigned hval2; | |
318 | ||
c81c1222 PB |
319 | if (htab->table[idx].used == -1 |
320 | && !first_deleted) | |
321 | first_deleted = idx; | |
322 | ||
3d3b52f2 JH |
323 | ret = _compare_and_overwrite_entry(item, action, retval, htab, |
324 | flag, hval, idx); | |
325 | if (ret != -1) | |
326 | return ret; | |
a6826fbc WD |
327 | |
328 | /* | |
329 | * Second hash function: | |
330 | * as suggested in [Knuth] | |
331 | */ | |
332 | hval2 = 1 + hval % (htab->size - 2); | |
333 | ||
334 | do { | |
335 | /* | |
071bc923 WD |
336 | * Because SIZE is prime this guarantees to |
337 | * step through all available indices. | |
a6826fbc WD |
338 | */ |
339 | if (idx <= hval2) | |
340 | idx = htab->size + idx - hval2; | |
341 | else | |
342 | idx -= hval2; | |
343 | ||
344 | /* | |
345 | * If we visited all entries leave the loop | |
346 | * unsuccessfully. | |
347 | */ | |
348 | if (idx == hval) | |
349 | break; | |
350 | ||
351 | /* If entry is found use it. */ | |
3d3b52f2 JH |
352 | ret = _compare_and_overwrite_entry(item, action, retval, |
353 | htab, flag, hval, idx); | |
354 | if (ret != -1) | |
355 | return ret; | |
a6826fbc WD |
356 | } |
357 | while (htab->table[idx].used); | |
358 | } | |
359 | ||
360 | /* An empty bucket has been found. */ | |
361 | if (action == ENTER) { | |
362 | /* | |
071bc923 WD |
363 | * If table is full and another entry should be |
364 | * entered return with error. | |
a6826fbc WD |
365 | */ |
366 | if (htab->filled == htab->size) { | |
367 | __set_errno(ENOMEM); | |
368 | *retval = NULL; | |
369 | return 0; | |
370 | } | |
371 | ||
372 | /* | |
373 | * Create new entry; | |
374 | * create copies of item.key and item.data | |
375 | */ | |
c81c1222 PB |
376 | if (first_deleted) |
377 | idx = first_deleted; | |
378 | ||
a6826fbc WD |
379 | htab->table[idx].used = hval; |
380 | htab->table[idx].entry.key = strdup(item.key); | |
381 | htab->table[idx].entry.data = strdup(item.data); | |
382 | if (!htab->table[idx].entry.key || | |
383 | !htab->table[idx].entry.data) { | |
384 | __set_errno(ENOMEM); | |
385 | *retval = NULL; | |
386 | return 0; | |
387 | } | |
388 | ||
389 | ++htab->filled; | |
390 | ||
170ab110 JH |
391 | /* This is a new entry, so look up a possible callback */ |
392 | env_callback_init(&htab->table[idx].entry); | |
2598090b JH |
393 | /* Also look for flags */ |
394 | env_flags_init(&htab->table[idx].entry); | |
170ab110 | 395 | |
7afcf3a5 JH |
396 | /* check for permission */ |
397 | if (htab->change_ok != NULL && htab->change_ok( | |
398 | &htab->table[idx].entry, item.data, env_op_create, flag)) { | |
399 | debug("change_ok() rejected setting variable " | |
400 | "%s, skipping it!\n", item.key); | |
401 | _hdelete(item.key, htab, &htab->table[idx].entry, idx); | |
402 | __set_errno(EPERM); | |
403 | *retval = NULL; | |
404 | return 0; | |
405 | } | |
406 | ||
170ab110 JH |
407 | /* If there is a callback, call it */ |
408 | if (htab->table[idx].entry.callback && | |
409 | htab->table[idx].entry.callback(item.key, item.data, | |
410 | env_op_create, flag)) { | |
411 | debug("callback() rejected setting variable " | |
412 | "%s, skipping it!\n", item.key); | |
413 | _hdelete(item.key, htab, &htab->table[idx].entry, idx); | |
414 | __set_errno(EINVAL); | |
415 | *retval = NULL; | |
416 | return 0; | |
417 | } | |
418 | ||
a6826fbc WD |
419 | /* return new entry */ |
420 | *retval = &htab->table[idx].entry; | |
421 | return 1; | |
422 | } | |
423 | ||
424 | __set_errno(ESRCH); | |
425 | *retval = NULL; | |
426 | return 0; | |
427 | } | |
428 | ||
429 | ||
430 | /* | |
431 | * hdelete() | |
432 | */ | |
433 | ||
434 | /* | |
435 | * The standard implementation of hsearch(3) does not provide any way | |
436 | * to delete any entries from the hash table. We extend the code to | |
437 | * do that. | |
438 | */ | |
439 | ||
7afcf3a5 JH |
440 | static void _hdelete(const char *key, struct hsearch_data *htab, ENTRY *ep, |
441 | int idx) | |
442 | { | |
443 | /* free used ENTRY */ | |
444 | debug("hdelete: DELETING key \"%s\"\n", key); | |
445 | free((void *)ep->key); | |
446 | free(ep->data); | |
170ab110 | 447 | ep->callback = NULL; |
2598090b | 448 | ep->flags = 0; |
7afcf3a5 JH |
449 | htab->table[idx].used = -1; |
450 | ||
451 | --htab->filled; | |
452 | } | |
453 | ||
c4e0057f | 454 | int hdelete_r(const char *key, struct hsearch_data *htab, int flag) |
a6826fbc WD |
455 | { |
456 | ENTRY e, *ep; | |
457 | int idx; | |
458 | ||
459 | debug("hdelete: DELETE key \"%s\"\n", key); | |
460 | ||
461 | e.key = (char *)key; | |
462 | ||
c4e0057f JH |
463 | idx = hsearch_r(e, FIND, &ep, htab, 0); |
464 | if (idx == 0) { | |
a6826fbc WD |
465 | __set_errno(ESRCH); |
466 | return 0; /* not found */ | |
467 | } | |
468 | ||
c4e0057f | 469 | /* Check for permission */ |
7afcf3a5 JH |
470 | if (htab->change_ok != NULL && |
471 | htab->change_ok(ep, NULL, env_op_delete, flag)) { | |
472 | debug("change_ok() rejected deleting variable " | |
473 | "%s, skipping it!\n", key); | |
c4e0057f JH |
474 | __set_errno(EPERM); |
475 | return 0; | |
476 | } | |
477 | ||
170ab110 JH |
478 | /* If there is a callback, call it */ |
479 | if (htab->table[idx].entry.callback && | |
480 | htab->table[idx].entry.callback(key, NULL, env_op_delete, flag)) { | |
481 | debug("callback() rejected deleting variable " | |
482 | "%s, skipping it!\n", key); | |
483 | __set_errno(EINVAL); | |
484 | return 0; | |
485 | } | |
486 | ||
7afcf3a5 | 487 | _hdelete(key, htab, ep, idx); |
a6826fbc WD |
488 | |
489 | return 1; | |
490 | } | |
491 | ||
492 | /* | |
493 | * hexport() | |
494 | */ | |
495 | ||
7ac2fe2d | 496 | #ifndef CONFIG_SPL_BUILD |
a6826fbc WD |
497 | /* |
498 | * Export the data stored in the hash table in linearized form. | |
499 | * | |
500 | * Entries are exported as "name=value" strings, separated by an | |
501 | * arbitrary (non-NUL, of course) separator character. This allows to | |
502 | * use this function both when formatting the U-Boot environment for | |
503 | * external storage (using '\0' as separator), but also when using it | |
504 | * for the "printenv" command to print all variables, simply by using | |
505 | * as '\n" as separator. This can also be used for new features like | |
506 | * exporting the environment data as text file, including the option | |
507 | * for later re-import. | |
508 | * | |
509 | * The entries in the result list will be sorted by ascending key | |
510 | * values. | |
511 | * | |
512 | * If the separator character is different from NUL, then any | |
513 | * separator characters and backslash characters in the values will | |
514 | * be escaped by a preceeding backslash in output. This is needed for | |
515 | * example to enable multi-line values, especially when the output | |
516 | * shall later be parsed (for example, for re-import). | |
517 | * | |
518 | * There are several options how the result buffer is handled: | |
519 | * | |
520 | * *resp size | |
521 | * ----------- | |
522 | * NULL 0 A string of sufficient length will be allocated. | |
523 | * NULL >0 A string of the size given will be | |
524 | * allocated. An error will be returned if the size is | |
525 | * not sufficient. Any unused bytes in the string will | |
526 | * be '\0'-padded. | |
527 | * !NULL 0 The user-supplied buffer will be used. No length | |
528 | * checking will be performed, i. e. it is assumed that | |
529 | * the buffer size will always be big enough. DANGEROUS. | |
530 | * !NULL >0 The user-supplied buffer will be used. An error will | |
531 | * be returned if the size is not sufficient. Any unused | |
532 | * bytes in the string will be '\0'-padded. | |
533 | */ | |
534 | ||
a6826fbc WD |
535 | static int cmpkey(const void *p1, const void *p2) |
536 | { | |
537 | ENTRY *e1 = *(ENTRY **) p1; | |
538 | ENTRY *e2 = *(ENTRY **) p2; | |
539 | ||
540 | return (strcmp(e1->key, e2->key)); | |
541 | } | |
542 | ||
5a31ea04 WD |
543 | static int match_string(int flag, const char *str, const char *pat) |
544 | { | |
545 | switch (flag & H_MATCH_METHOD) { | |
546 | case H_MATCH_IDENT: | |
547 | if (strcmp(str, pat) == 0) | |
548 | return 1; | |
549 | break; | |
550 | case H_MATCH_SUBSTR: | |
551 | if (strstr(str, pat)) | |
552 | return 1; | |
553 | break; | |
554 | default: | |
555 | printf("## ERROR: unsupported match method: 0x%02x\n", | |
556 | flag & H_MATCH_METHOD); | |
557 | break; | |
558 | } | |
559 | return 0; | |
560 | } | |
561 | ||
562 | static int match_entry(ENTRY *ep, int flag, | |
ea009d47 WD |
563 | int argc, char * const argv[]) |
564 | { | |
565 | int arg; | |
566 | ||
5a31ea04 | 567 | for (arg = 1; arg < argc; ++arg) { |
ea009d47 | 568 | if (flag & H_MATCH_KEY) { |
5a31ea04 WD |
569 | if (match_string(flag, ep->key, argv[arg])) |
570 | return 1; | |
571 | } | |
572 | if (flag & H_MATCH_DATA) { | |
573 | if (match_string(flag, ep->data, argv[arg])) | |
574 | return 1; | |
ea009d47 WD |
575 | } |
576 | } | |
577 | return 0; | |
578 | } | |
579 | ||
be11235a | 580 | ssize_t hexport_r(struct hsearch_data *htab, const char sep, int flag, |
37f2fe74 WD |
581 | char **resp, size_t size, |
582 | int argc, char * const argv[]) | |
a6826fbc WD |
583 | { |
584 | ENTRY *list[htab->size]; | |
585 | char *res, *p; | |
586 | size_t totlen; | |
587 | int i, n; | |
588 | ||
589 | /* Test for correct arguments. */ | |
590 | if ((resp == NULL) || (htab == NULL)) { | |
591 | __set_errno(EINVAL); | |
592 | return (-1); | |
593 | } | |
594 | ||
ff856286 SG |
595 | debug("EXPORT table = %p, htab.size = %d, htab.filled = %d, " |
596 | "size = %zu\n", htab, htab->size, htab->filled, size); | |
a6826fbc WD |
597 | /* |
598 | * Pass 1: | |
599 | * search used entries, | |
600 | * save addresses and compute total length | |
601 | */ | |
602 | for (i = 1, n = 0, totlen = 0; i <= htab->size; ++i) { | |
603 | ||
c81c1222 | 604 | if (htab->table[i].used > 0) { |
a6826fbc | 605 | ENTRY *ep = &htab->table[i].entry; |
5a31ea04 | 606 | int found = match_entry(ep, flag, argc, argv); |
37f2fe74 | 607 | |
37f2fe74 WD |
608 | if ((argc > 0) && (found == 0)) |
609 | continue; | |
a6826fbc | 610 | |
be11235a JH |
611 | if ((flag & H_HIDE_DOT) && ep->key[0] == '.') |
612 | continue; | |
613 | ||
a6826fbc WD |
614 | list[n++] = ep; |
615 | ||
616 | totlen += strlen(ep->key) + 2; | |
617 | ||
618 | if (sep == '\0') { | |
619 | totlen += strlen(ep->data); | |
620 | } else { /* check if escapes are needed */ | |
621 | char *s = ep->data; | |
622 | ||
623 | while (*s) { | |
624 | ++totlen; | |
625 | /* add room for needed escape chars */ | |
626 | if ((*s == sep) || (*s == '\\')) | |
627 | ++totlen; | |
628 | ++s; | |
629 | } | |
630 | } | |
631 | totlen += 2; /* for '=' and 'sep' char */ | |
632 | } | |
633 | } | |
634 | ||
635 | #ifdef DEBUG | |
636 | /* Pass 1a: print unsorted list */ | |
637 | printf("Unsorted: n=%d\n", n); | |
638 | for (i = 0; i < n; ++i) { | |
639 | printf("\t%3d: %p ==> %-10s => %s\n", | |
640 | i, list[i], list[i]->key, list[i]->data); | |
641 | } | |
642 | #endif | |
643 | ||
644 | /* Sort list by keys */ | |
645 | qsort(list, n, sizeof(ENTRY *), cmpkey); | |
646 | ||
647 | /* Check if the user supplied buffer size is sufficient */ | |
648 | if (size) { | |
649 | if (size < totlen + 1) { /* provided buffer too small */ | |
ff856286 SG |
650 | printf("Env export buffer too small: %zu, " |
651 | "but need %zu\n", size, totlen + 1); | |
a6826fbc WD |
652 | __set_errno(ENOMEM); |
653 | return (-1); | |
654 | } | |
655 | } else { | |
656 | size = totlen + 1; | |
657 | } | |
658 | ||
659 | /* Check if the user provided a buffer */ | |
660 | if (*resp) { | |
661 | /* yes; clear it */ | |
662 | res = *resp; | |
663 | memset(res, '\0', size); | |
664 | } else { | |
665 | /* no, allocate and clear one */ | |
666 | *resp = res = calloc(1, size); | |
667 | if (res == NULL) { | |
668 | __set_errno(ENOMEM); | |
669 | return (-1); | |
670 | } | |
671 | } | |
672 | /* | |
673 | * Pass 2: | |
674 | * export sorted list of result data | |
675 | */ | |
676 | for (i = 0, p = res; i < n; ++i) { | |
84b5e802 | 677 | const char *s; |
a6826fbc WD |
678 | |
679 | s = list[i]->key; | |
680 | while (*s) | |
681 | *p++ = *s++; | |
682 | *p++ = '='; | |
683 | ||
684 | s = list[i]->data; | |
685 | ||
686 | while (*s) { | |
687 | if ((*s == sep) || (*s == '\\')) | |
688 | *p++ = '\\'; /* escape */ | |
689 | *p++ = *s++; | |
690 | } | |
691 | *p++ = sep; | |
692 | } | |
693 | *p = '\0'; /* terminate result */ | |
694 | ||
695 | return size; | |
696 | } | |
7ac2fe2d | 697 | #endif |
a6826fbc WD |
698 | |
699 | ||
700 | /* | |
701 | * himport() | |
702 | */ | |
703 | ||
d5370feb GF |
704 | /* |
705 | * Check whether variable 'name' is amongst vars[], | |
706 | * and remove all instances by setting the pointer to NULL | |
707 | */ | |
708 | static int drop_var_from_set(const char *name, int nvars, char * vars[]) | |
348b1f1c GF |
709 | { |
710 | int i = 0; | |
d5370feb | 711 | int res = 0; |
348b1f1c GF |
712 | |
713 | /* No variables specified means process all of them */ | |
714 | if (nvars == 0) | |
715 | return 1; | |
716 | ||
717 | for (i = 0; i < nvars; i++) { | |
d5370feb GF |
718 | if (vars[i] == NULL) |
719 | continue; | |
720 | /* If we found it, delete all of them */ | |
721 | if (!strcmp(name, vars[i])) { | |
722 | vars[i] = NULL; | |
723 | res = 1; | |
724 | } | |
348b1f1c | 725 | } |
d5370feb GF |
726 | if (!res) |
727 | debug("Skipping non-listed variable %s\n", name); | |
348b1f1c | 728 | |
d5370feb | 729 | return res; |
348b1f1c GF |
730 | } |
731 | ||
a6826fbc WD |
732 | /* |
733 | * Import linearized data into hash table. | |
734 | * | |
735 | * This is the inverse function to hexport(): it takes a linear list | |
736 | * of "name=value" pairs and creates hash table entries from it. | |
737 | * | |
738 | * Entries without "value", i. e. consisting of only "name" or | |
739 | * "name=", will cause this entry to be deleted from the hash table. | |
740 | * | |
741 | * The "flag" argument can be used to control the behaviour: when the | |
742 | * H_NOCLEAR bit is set, then an existing hash table will kept, i. e. | |
743 | * new data will be added to an existing hash table; otherwise, old | |
744 | * data will be discarded and a new hash table will be created. | |
745 | * | |
746 | * The separator character for the "name=value" pairs can be selected, | |
747 | * so we both support importing from externally stored environment | |
748 | * data (separated by NUL characters) and from plain text files | |
749 | * (entries separated by newline characters). | |
750 | * | |
751 | * To allow for nicely formatted text input, leading white space | |
752 | * (sequences of SPACE and TAB chars) is ignored, and entries starting | |
753 | * (after removal of any leading white space) with a '#' character are | |
754 | * considered comments and ignored. | |
755 | * | |
756 | * [NOTE: this means that a variable name cannot start with a '#' | |
757 | * character.] | |
758 | * | |
759 | * When using a non-NUL separator character, backslash is used as | |
760 | * escape character in the value part, allowing for example for | |
761 | * multi-line values. | |
762 | * | |
763 | * In theory, arbitrary separator characters can be used, but only | |
764 | * '\0' and '\n' have really been tested. | |
765 | */ | |
766 | ||
a6826fbc | 767 | int himport_r(struct hsearch_data *htab, |
348b1f1c | 768 | const char *env, size_t size, const char sep, int flag, |
c4e0057f | 769 | int nvars, char * const vars[]) |
a6826fbc WD |
770 | { |
771 | char *data, *sp, *dp, *name, *value; | |
d5370feb GF |
772 | char *localvars[nvars]; |
773 | int i; | |
a6826fbc WD |
774 | |
775 | /* Test for correct arguments. */ | |
776 | if (htab == NULL) { | |
777 | __set_errno(EINVAL); | |
778 | return 0; | |
779 | } | |
780 | ||
781 | /* we allocate new space to make sure we can write to the array */ | |
782 | if ((data = malloc(size)) == NULL) { | |
ff856286 | 783 | debug("himport_r: can't malloc %zu bytes\n", size); |
a6826fbc WD |
784 | __set_errno(ENOMEM); |
785 | return 0; | |
786 | } | |
787 | memcpy(data, env, size); | |
788 | dp = data; | |
789 | ||
d5370feb GF |
790 | /* make a local copy of the list of variables */ |
791 | if (nvars) | |
792 | memcpy(localvars, vars, sizeof(vars[0]) * nvars); | |
793 | ||
a6826fbc WD |
794 | if ((flag & H_NOCLEAR) == 0) { |
795 | /* Destroy old hash table if one exists */ | |
796 | debug("Destroy Hash Table: %p table = %p\n", htab, | |
797 | htab->table); | |
798 | if (htab->table) | |
c4e0057f | 799 | hdestroy_r(htab); |
a6826fbc WD |
800 | } |
801 | ||
802 | /* | |
803 | * Create new hash table (if needed). The computation of the hash | |
804 | * table size is based on heuristics: in a sample of some 70+ | |
805 | * existing systems we found an average size of 39+ bytes per entry | |
806 | * in the environment (for the whole key=value pair). Assuming a | |
ea882baf WD |
807 | * size of 8 per entry (= safety factor of ~5) should provide enough |
808 | * safety margin for any existing environment definitions and still | |
a6826fbc WD |
809 | * allow for more than enough dynamic additions. Note that the |
810 | * "size" argument is supposed to give the maximum enviroment size | |
ea882baf WD |
811 | * (CONFIG_ENV_SIZE). This heuristics will result in |
812 | * unreasonably large numbers (and thus memory footprint) for | |
813 | * big flash environments (>8,000 entries for 64 KB | |
fc5fc76b AB |
814 | * envrionment size), so we clip it to a reasonable value. |
815 | * On the other hand we need to add some more entries for free | |
816 | * space when importing very small buffers. Both boundaries can | |
817 | * be overwritten in the board config file if needed. | |
a6826fbc WD |
818 | */ |
819 | ||
820 | if (!htab->table) { | |
fc5fc76b | 821 | int nent = CONFIG_ENV_MIN_ENTRIES + size / 8; |
ea882baf WD |
822 | |
823 | if (nent > CONFIG_ENV_MAX_ENTRIES) | |
824 | nent = CONFIG_ENV_MAX_ENTRIES; | |
a6826fbc WD |
825 | |
826 | debug("Create Hash Table: N=%d\n", nent); | |
827 | ||
828 | if (hcreate_r(nent, htab) == 0) { | |
829 | free(data); | |
830 | return 0; | |
831 | } | |
832 | } | |
833 | ||
834 | /* Parse environment; allow for '\0' and 'sep' as separators */ | |
835 | do { | |
836 | ENTRY e, *rv; | |
837 | ||
838 | /* skip leading white space */ | |
4d91a6ec | 839 | while (isblank(*dp)) |
a6826fbc WD |
840 | ++dp; |
841 | ||
842 | /* skip comment lines */ | |
843 | if (*dp == '#') { | |
844 | while (*dp && (*dp != sep)) | |
845 | ++dp; | |
846 | ++dp; | |
847 | continue; | |
848 | } | |
849 | ||
850 | /* parse name */ | |
851 | for (name = dp; *dp != '=' && *dp && *dp != sep; ++dp) | |
852 | ; | |
853 | ||
854 | /* deal with "name" and "name=" entries (delete var) */ | |
855 | if (*dp == '\0' || *(dp + 1) == '\0' || | |
856 | *dp == sep || *(dp + 1) == sep) { | |
857 | if (*dp == '=') | |
858 | *dp++ = '\0'; | |
859 | *dp++ = '\0'; /* terminate name */ | |
860 | ||
861 | debug("DELETE CANDIDATE: \"%s\"\n", name); | |
d5370feb | 862 | if (!drop_var_from_set(name, nvars, localvars)) |
348b1f1c | 863 | continue; |
a6826fbc | 864 | |
c4e0057f | 865 | if (hdelete_r(name, htab, flag) == 0) |
a6826fbc WD |
866 | debug("DELETE ERROR ##############################\n"); |
867 | ||
868 | continue; | |
869 | } | |
870 | *dp++ = '\0'; /* terminate name */ | |
871 | ||
872 | /* parse value; deal with escapes */ | |
873 | for (value = sp = dp; *dp && (*dp != sep); ++dp) { | |
874 | if ((*dp == '\\') && *(dp + 1)) | |
875 | ++dp; | |
876 | *sp++ = *dp; | |
877 | } | |
878 | *sp++ = '\0'; /* terminate value */ | |
879 | ++dp; | |
880 | ||
348b1f1c | 881 | /* Skip variables which are not supposed to be processed */ |
d5370feb | 882 | if (!drop_var_from_set(name, nvars, localvars)) |
348b1f1c GF |
883 | continue; |
884 | ||
a6826fbc WD |
885 | /* enter into hash table */ |
886 | e.key = name; | |
887 | e.data = value; | |
888 | ||
c4e0057f | 889 | hsearch_r(e, ENTER, &rv, htab, flag); |
170ab110 | 890 | if (rv == NULL) |
ea882baf WD |
891 | printf("himport_r: can't insert \"%s=%s\" into hash table\n", |
892 | name, value); | |
a6826fbc | 893 | |
ea882baf WD |
894 | debug("INSERT: table %p, filled %d/%d rv %p ==> name=\"%s\" value=\"%s\"\n", |
895 | htab, htab->filled, htab->size, | |
896 | rv, name, value); | |
a6826fbc WD |
897 | } while ((dp < data + size) && *dp); /* size check needed for text */ |
898 | /* without '\0' termination */ | |
ea882baf | 899 | debug("INSERT: free(data = %p)\n", data); |
a6826fbc WD |
900 | free(data); |
901 | ||
d5370feb GF |
902 | /* process variables which were not considered */ |
903 | for (i = 0; i < nvars; i++) { | |
904 | if (localvars[i] == NULL) | |
905 | continue; | |
906 | /* | |
907 | * All variables which were not deleted from the variable list | |
908 | * were not present in the imported env | |
909 | * This could mean two things: | |
910 | * a) if the variable was present in current env, we delete it | |
911 | * b) if the variable was not present in current env, we notify | |
912 | * it might be a typo | |
913 | */ | |
c4e0057f | 914 | if (hdelete_r(localvars[i], htab, flag) == 0) |
d5370feb GF |
915 | printf("WARNING: '%s' neither in running nor in imported env!\n", localvars[i]); |
916 | else | |
917 | printf("WARNING: '%s' not in imported env, deleting it!\n", localvars[i]); | |
918 | } | |
919 | ||
ea882baf | 920 | debug("INSERT: done\n"); |
a6826fbc WD |
921 | return 1; /* everything OK */ |
922 | } | |
170ab110 JH |
923 | |
924 | /* | |
925 | * hwalk_r() | |
926 | */ | |
927 | ||
928 | /* | |
929 | * Walk all of the entries in the hash, calling the callback for each one. | |
930 | * this allows some generic operation to be performed on each element. | |
931 | */ | |
932 | int hwalk_r(struct hsearch_data *htab, int (*callback)(ENTRY *)) | |
933 | { | |
934 | int i; | |
935 | int retval; | |
936 | ||
937 | for (i = 1; i <= htab->size; ++i) { | |
938 | if (htab->table[i].used > 0) { | |
939 | retval = callback(&htab->table[i].entry); | |
940 | if (retval) | |
941 | return retval; | |
942 | } | |
943 | } | |
944 | ||
945 | return 0; | |
946 | } |