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 * 5 * Copyright (C) 2010-2013 Wolfgang Denk <wd@denx.de> 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> 35 # include <ctype.h> 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> 47 # include <linux/ctype.h> 48 #endif 49 50 #ifndef CONFIG_ENV_MIN_ENTRIES /* minimum number of entries */ 51 #define CONFIG_ENV_MIN_ENTRIES 64 52 #endif 53 #ifndef CONFIG_ENV_MAX_ENTRIES /* maximum number of entries */ 54 #define CONFIG_ENV_MAX_ENTRIES 512 55 #endif 56 57 #include <env_callback.h> 58 #include <env_flags.h> 59 #include <search.h> 60 61 /* 62 * [Aho,Sethi,Ullman] Compilers: Principles, Techniques and Tools, 1986 63 * [Knuth] The Art of Computer Programming, part 3 (6.4) 64 */ 65 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 */ 71 72 typedef struct _ENTRY { 73 int used; 74 ENTRY entry; 75 } _ENTRY; 76 77 78 static void _hdelete(const char *key, struct hsearch_data *htab, ENTRY *ep, 79 int idx); 80 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 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 */ 111 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 */ 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 */ 150 151 void hdestroy_r(struct hsearch_data *htab) 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) { 163 if (htab->table[i].used > 0) { 164 ENTRY *ep = &htab->table[i].entry; 165 166 free((void *)ep->key); 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 213 /* 214 * hstrstr_r - return index to entry whose key and/or data contains match 215 */ 216 int hstrstr_r(const char *match, int last_idx, ENTRY ** retval, 217 struct hsearch_data *htab) 218 { 219 unsigned int idx; 220 221 for (idx = last_idx + 1; idx < htab->size; ++idx) { 222 if (htab->table[idx].used <= 0) 223 continue; 224 if (strstr(htab->table[idx].entry.key, match) || 225 strstr(htab->table[idx].entry.data, match)) { 226 *retval = &htab->table[idx].entry; 227 return idx; 228 } 229 } 230 231 __set_errno(ESRCH); 232 *retval = NULL; 233 return 0; 234 } 235 236 int hmatch_r(const char *match, int last_idx, ENTRY ** retval, 237 struct hsearch_data *htab) 238 { 239 unsigned int idx; 240 size_t key_len = strlen(match); 241 242 for (idx = last_idx + 1; idx < htab->size; ++idx) { 243 if (htab->table[idx].used <= 0) 244 continue; 245 if (!strncmp(match, htab->table[idx].entry.key, key_len)) { 246 *retval = &htab->table[idx].entry; 247 return idx; 248 } 249 } 250 251 __set_errno(ESRCH); 252 *retval = NULL; 253 return 0; 254 } 255 256 /* 257 * Compare an existing entry with the desired key, and overwrite if the action 258 * is ENTER. This is simply a helper function for hsearch_r(). 259 */ 260 static inline int _compare_and_overwrite_entry(ENTRY item, ACTION action, 261 ENTRY **retval, struct hsearch_data *htab, int flag, 262 unsigned int hval, unsigned int idx) 263 { 264 if (htab->table[idx].used == hval 265 && strcmp(item.key, htab->table[idx].entry.key) == 0) { 266 /* Overwrite existing value? */ 267 if ((action == ENTER) && (item.data != NULL)) { 268 /* check for permission */ 269 if (htab->change_ok != NULL && htab->change_ok( 270 &htab->table[idx].entry, item.data, 271 env_op_overwrite, flag)) { 272 debug("change_ok() rejected setting variable " 273 "%s, skipping it!\n", item.key); 274 __set_errno(EPERM); 275 *retval = NULL; 276 return 0; 277 } 278 279 /* If there is a callback, call it */ 280 if (htab->table[idx].entry.callback && 281 htab->table[idx].entry.callback(item.key, 282 item.data, env_op_overwrite, flag)) { 283 debug("callback() rejected setting variable " 284 "%s, skipping it!\n", item.key); 285 __set_errno(EINVAL); 286 *retval = NULL; 287 return 0; 288 } 289 290 free(htab->table[idx].entry.data); 291 htab->table[idx].entry.data = strdup(item.data); 292 if (!htab->table[idx].entry.data) { 293 __set_errno(ENOMEM); 294 *retval = NULL; 295 return 0; 296 } 297 } 298 /* return found entry */ 299 *retval = &htab->table[idx].entry; 300 return idx; 301 } 302 /* keep searching */ 303 return -1; 304 } 305 306 int hsearch_r(ENTRY item, ACTION action, ENTRY ** retval, 307 struct hsearch_data *htab, int flag) 308 { 309 unsigned int hval; 310 unsigned int count; 311 unsigned int len = strlen(item.key); 312 unsigned int idx; 313 unsigned int first_deleted = 0; 314 int ret; 315 316 /* Compute an value for the given string. Perhaps use a better method. */ 317 hval = len; 318 count = len; 319 while (count-- > 0) { 320 hval <<= 4; 321 hval += item.key[count]; 322 } 323 324 /* 325 * First hash function: 326 * simply take the modul but prevent zero. 327 */ 328 hval %= htab->size; 329 if (hval == 0) 330 ++hval; 331 332 /* The first index tried. */ 333 idx = hval; 334 335 if (htab->table[idx].used) { 336 /* 337 * Further action might be required according to the 338 * action value. 339 */ 340 unsigned hval2; 341 342 if (htab->table[idx].used == -1 343 && !first_deleted) 344 first_deleted = idx; 345 346 ret = _compare_and_overwrite_entry(item, action, retval, htab, 347 flag, hval, idx); 348 if (ret != -1) 349 return ret; 350 351 /* 352 * Second hash function: 353 * as suggested in [Knuth] 354 */ 355 hval2 = 1 + hval % (htab->size - 2); 356 357 do { 358 /* 359 * Because SIZE is prime this guarantees to 360 * step through all available indices. 361 */ 362 if (idx <= hval2) 363 idx = htab->size + idx - hval2; 364 else 365 idx -= hval2; 366 367 /* 368 * If we visited all entries leave the loop 369 * unsuccessfully. 370 */ 371 if (idx == hval) 372 break; 373 374 /* If entry is found use it. */ 375 ret = _compare_and_overwrite_entry(item, action, retval, 376 htab, flag, hval, idx); 377 if (ret != -1) 378 return ret; 379 } 380 while (htab->table[idx].used); 381 } 382 383 /* An empty bucket has been found. */ 384 if (action == ENTER) { 385 /* 386 * If table is full and another entry should be 387 * entered return with error. 388 */ 389 if (htab->filled == htab->size) { 390 __set_errno(ENOMEM); 391 *retval = NULL; 392 return 0; 393 } 394 395 /* 396 * Create new entry; 397 * create copies of item.key and item.data 398 */ 399 if (first_deleted) 400 idx = first_deleted; 401 402 htab->table[idx].used = hval; 403 htab->table[idx].entry.key = strdup(item.key); 404 htab->table[idx].entry.data = strdup(item.data); 405 if (!htab->table[idx].entry.key || 406 !htab->table[idx].entry.data) { 407 __set_errno(ENOMEM); 408 *retval = NULL; 409 return 0; 410 } 411 412 ++htab->filled; 413 414 /* This is a new entry, so look up a possible callback */ 415 env_callback_init(&htab->table[idx].entry); 416 /* Also look for flags */ 417 env_flags_init(&htab->table[idx].entry); 418 419 /* check for permission */ 420 if (htab->change_ok != NULL && htab->change_ok( 421 &htab->table[idx].entry, item.data, env_op_create, flag)) { 422 debug("change_ok() rejected setting variable " 423 "%s, skipping it!\n", item.key); 424 _hdelete(item.key, htab, &htab->table[idx].entry, idx); 425 __set_errno(EPERM); 426 *retval = NULL; 427 return 0; 428 } 429 430 /* If there is a callback, call it */ 431 if (htab->table[idx].entry.callback && 432 htab->table[idx].entry.callback(item.key, item.data, 433 env_op_create, flag)) { 434 debug("callback() rejected setting variable " 435 "%s, skipping it!\n", item.key); 436 _hdelete(item.key, htab, &htab->table[idx].entry, idx); 437 __set_errno(EINVAL); 438 *retval = NULL; 439 return 0; 440 } 441 442 /* return new entry */ 443 *retval = &htab->table[idx].entry; 444 return 1; 445 } 446 447 __set_errno(ESRCH); 448 *retval = NULL; 449 return 0; 450 } 451 452 453 /* 454 * hdelete() 455 */ 456 457 /* 458 * The standard implementation of hsearch(3) does not provide any way 459 * to delete any entries from the hash table. We extend the code to 460 * do that. 461 */ 462 463 static void _hdelete(const char *key, struct hsearch_data *htab, ENTRY *ep, 464 int idx) 465 { 466 /* free used ENTRY */ 467 debug("hdelete: DELETING key \"%s\"\n", key); 468 free((void *)ep->key); 469 free(ep->data); 470 ep->callback = NULL; 471 ep->flags = 0; 472 htab->table[idx].used = -1; 473 474 --htab->filled; 475 } 476 477 int hdelete_r(const char *key, struct hsearch_data *htab, int flag) 478 { 479 ENTRY e, *ep; 480 int idx; 481 482 debug("hdelete: DELETE key \"%s\"\n", key); 483 484 e.key = (char *)key; 485 486 idx = hsearch_r(e, FIND, &ep, htab, 0); 487 if (idx == 0) { 488 __set_errno(ESRCH); 489 return 0; /* not found */ 490 } 491 492 /* Check for permission */ 493 if (htab->change_ok != NULL && 494 htab->change_ok(ep, NULL, env_op_delete, flag)) { 495 debug("change_ok() rejected deleting variable " 496 "%s, skipping it!\n", key); 497 __set_errno(EPERM); 498 return 0; 499 } 500 501 /* If there is a callback, call it */ 502 if (htab->table[idx].entry.callback && 503 htab->table[idx].entry.callback(key, NULL, env_op_delete, flag)) { 504 debug("callback() rejected deleting variable " 505 "%s, skipping it!\n", key); 506 __set_errno(EINVAL); 507 return 0; 508 } 509 510 _hdelete(key, htab, ep, idx); 511 512 return 1; 513 } 514 515 /* 516 * hexport() 517 */ 518 519 #ifndef CONFIG_SPL_BUILD 520 /* 521 * Export the data stored in the hash table in linearized form. 522 * 523 * Entries are exported as "name=value" strings, separated by an 524 * arbitrary (non-NUL, of course) separator character. This allows to 525 * use this function both when formatting the U-Boot environment for 526 * external storage (using '\0' as separator), but also when using it 527 * for the "printenv" command to print all variables, simply by using 528 * as '\n" as separator. This can also be used for new features like 529 * exporting the environment data as text file, including the option 530 * for later re-import. 531 * 532 * The entries in the result list will be sorted by ascending key 533 * values. 534 * 535 * If the separator character is different from NUL, then any 536 * separator characters and backslash characters in the values will 537 * be escaped by a preceeding backslash in output. This is needed for 538 * example to enable multi-line values, especially when the output 539 * shall later be parsed (for example, for re-import). 540 * 541 * There are several options how the result buffer is handled: 542 * 543 * *resp size 544 * ----------- 545 * NULL 0 A string of sufficient length will be allocated. 546 * NULL >0 A string of the size given will be 547 * allocated. An error will be returned if the size is 548 * not sufficient. Any unused bytes in the string will 549 * be '\0'-padded. 550 * !NULL 0 The user-supplied buffer will be used. No length 551 * checking will be performed, i. e. it is assumed that 552 * the buffer size will always be big enough. DANGEROUS. 553 * !NULL >0 The user-supplied buffer will be used. An error will 554 * be returned if the size is not sufficient. Any unused 555 * bytes in the string will be '\0'-padded. 556 */ 557 558 static int cmpkey(const void *p1, const void *p2) 559 { 560 ENTRY *e1 = *(ENTRY **) p1; 561 ENTRY *e2 = *(ENTRY **) p2; 562 563 return (strcmp(e1->key, e2->key)); 564 } 565 566 static int match_strings(ENTRY *ep, int flag, 567 int argc, char * const argv[]) 568 { 569 int arg; 570 571 for (arg = 0; arg < argc; ++arg) { 572 if (flag & H_MATCH_KEY) { 573 switch (flag & H_MATCH_METHOD) { 574 case H_MATCH_IDENT: 575 if (strcmp(argv[arg], ep->key) == 0) 576 return 1; 577 break; 578 default: 579 printf("## ERROR: unsupported match method: 0x%02x\n", 580 flag & H_MATCH_METHOD); 581 break; 582 } 583 } 584 } 585 return 0; 586 } 587 588 ssize_t hexport_r(struct hsearch_data *htab, const char sep, int flag, 589 char **resp, size_t size, 590 int argc, char * const argv[]) 591 { 592 ENTRY *list[htab->size]; 593 char *res, *p; 594 size_t totlen; 595 int i, n; 596 597 /* Test for correct arguments. */ 598 if ((resp == NULL) || (htab == NULL)) { 599 __set_errno(EINVAL); 600 return (-1); 601 } 602 603 debug("EXPORT table = %p, htab.size = %d, htab.filled = %d, " 604 "size = %zu\n", htab, htab->size, htab->filled, size); 605 /* 606 * Pass 1: 607 * search used entries, 608 * save addresses and compute total length 609 */ 610 for (i = 1, n = 0, totlen = 0; i <= htab->size; ++i) { 611 612 if (htab->table[i].used > 0) { 613 ENTRY *ep = &htab->table[i].entry; 614 int found = match_strings(ep, flag, argc, argv); 615 616 if ((argc > 0) && (found == 0)) 617 continue; 618 619 if ((flag & H_HIDE_DOT) && ep->key[0] == '.') 620 continue; 621 622 list[n++] = ep; 623 624 totlen += strlen(ep->key) + 2; 625 626 if (sep == '\0') { 627 totlen += strlen(ep->data); 628 } else { /* check if escapes are needed */ 629 char *s = ep->data; 630 631 while (*s) { 632 ++totlen; 633 /* add room for needed escape chars */ 634 if ((*s == sep) || (*s == '\\')) 635 ++totlen; 636 ++s; 637 } 638 } 639 totlen += 2; /* for '=' and 'sep' char */ 640 } 641 } 642 643 #ifdef DEBUG 644 /* Pass 1a: print unsorted list */ 645 printf("Unsorted: n=%d\n", n); 646 for (i = 0; i < n; ++i) { 647 printf("\t%3d: %p ==> %-10s => %s\n", 648 i, list[i], list[i]->key, list[i]->data); 649 } 650 #endif 651 652 /* Sort list by keys */ 653 qsort(list, n, sizeof(ENTRY *), cmpkey); 654 655 /* Check if the user supplied buffer size is sufficient */ 656 if (size) { 657 if (size < totlen + 1) { /* provided buffer too small */ 658 printf("Env export buffer too small: %zu, " 659 "but need %zu\n", size, totlen + 1); 660 __set_errno(ENOMEM); 661 return (-1); 662 } 663 } else { 664 size = totlen + 1; 665 } 666 667 /* Check if the user provided a buffer */ 668 if (*resp) { 669 /* yes; clear it */ 670 res = *resp; 671 memset(res, '\0', size); 672 } else { 673 /* no, allocate and clear one */ 674 *resp = res = calloc(1, size); 675 if (res == NULL) { 676 __set_errno(ENOMEM); 677 return (-1); 678 } 679 } 680 /* 681 * Pass 2: 682 * export sorted list of result data 683 */ 684 for (i = 0, p = res; i < n; ++i) { 685 const char *s; 686 687 s = list[i]->key; 688 while (*s) 689 *p++ = *s++; 690 *p++ = '='; 691 692 s = list[i]->data; 693 694 while (*s) { 695 if ((*s == sep) || (*s == '\\')) 696 *p++ = '\\'; /* escape */ 697 *p++ = *s++; 698 } 699 *p++ = sep; 700 } 701 *p = '\0'; /* terminate result */ 702 703 return size; 704 } 705 #endif 706 707 708 /* 709 * himport() 710 */ 711 712 /* 713 * Check whether variable 'name' is amongst vars[], 714 * and remove all instances by setting the pointer to NULL 715 */ 716 static int drop_var_from_set(const char *name, int nvars, char * vars[]) 717 { 718 int i = 0; 719 int res = 0; 720 721 /* No variables specified means process all of them */ 722 if (nvars == 0) 723 return 1; 724 725 for (i = 0; i < nvars; i++) { 726 if (vars[i] == NULL) 727 continue; 728 /* If we found it, delete all of them */ 729 if (!strcmp(name, vars[i])) { 730 vars[i] = NULL; 731 res = 1; 732 } 733 } 734 if (!res) 735 debug("Skipping non-listed variable %s\n", name); 736 737 return res; 738 } 739 740 /* 741 * Import linearized data into hash table. 742 * 743 * This is the inverse function to hexport(): it takes a linear list 744 * of "name=value" pairs and creates hash table entries from it. 745 * 746 * Entries without "value", i. e. consisting of only "name" or 747 * "name=", will cause this entry to be deleted from the hash table. 748 * 749 * The "flag" argument can be used to control the behaviour: when the 750 * H_NOCLEAR bit is set, then an existing hash table will kept, i. e. 751 * new data will be added to an existing hash table; otherwise, old 752 * data will be discarded and a new hash table will be created. 753 * 754 * The separator character for the "name=value" pairs can be selected, 755 * so we both support importing from externally stored environment 756 * data (separated by NUL characters) and from plain text files 757 * (entries separated by newline characters). 758 * 759 * To allow for nicely formatted text input, leading white space 760 * (sequences of SPACE and TAB chars) is ignored, and entries starting 761 * (after removal of any leading white space) with a '#' character are 762 * considered comments and ignored. 763 * 764 * [NOTE: this means that a variable name cannot start with a '#' 765 * character.] 766 * 767 * When using a non-NUL separator character, backslash is used as 768 * escape character in the value part, allowing for example for 769 * multi-line values. 770 * 771 * In theory, arbitrary separator characters can be used, but only 772 * '\0' and '\n' have really been tested. 773 */ 774 775 int himport_r(struct hsearch_data *htab, 776 const char *env, size_t size, const char sep, int flag, 777 int nvars, char * const vars[]) 778 { 779 char *data, *sp, *dp, *name, *value; 780 char *localvars[nvars]; 781 int i; 782 783 /* Test for correct arguments. */ 784 if (htab == NULL) { 785 __set_errno(EINVAL); 786 return 0; 787 } 788 789 /* we allocate new space to make sure we can write to the array */ 790 if ((data = malloc(size)) == NULL) { 791 debug("himport_r: can't malloc %zu bytes\n", size); 792 __set_errno(ENOMEM); 793 return 0; 794 } 795 memcpy(data, env, size); 796 dp = data; 797 798 /* make a local copy of the list of variables */ 799 if (nvars) 800 memcpy(localvars, vars, sizeof(vars[0]) * nvars); 801 802 if ((flag & H_NOCLEAR) == 0) { 803 /* Destroy old hash table if one exists */ 804 debug("Destroy Hash Table: %p table = %p\n", htab, 805 htab->table); 806 if (htab->table) 807 hdestroy_r(htab); 808 } 809 810 /* 811 * Create new hash table (if needed). The computation of the hash 812 * table size is based on heuristics: in a sample of some 70+ 813 * existing systems we found an average size of 39+ bytes per entry 814 * in the environment (for the whole key=value pair). Assuming a 815 * size of 8 per entry (= safety factor of ~5) should provide enough 816 * safety margin for any existing environment definitions and still 817 * allow for more than enough dynamic additions. Note that the 818 * "size" argument is supposed to give the maximum enviroment size 819 * (CONFIG_ENV_SIZE). This heuristics will result in 820 * unreasonably large numbers (and thus memory footprint) for 821 * big flash environments (>8,000 entries for 64 KB 822 * envrionment size), so we clip it to a reasonable value. 823 * On the other hand we need to add some more entries for free 824 * space when importing very small buffers. Both boundaries can 825 * be overwritten in the board config file if needed. 826 */ 827 828 if (!htab->table) { 829 int nent = CONFIG_ENV_MIN_ENTRIES + size / 8; 830 831 if (nent > CONFIG_ENV_MAX_ENTRIES) 832 nent = CONFIG_ENV_MAX_ENTRIES; 833 834 debug("Create Hash Table: N=%d\n", nent); 835 836 if (hcreate_r(nent, htab) == 0) { 837 free(data); 838 return 0; 839 } 840 } 841 842 /* Parse environment; allow for '\0' and 'sep' as separators */ 843 do { 844 ENTRY e, *rv; 845 846 /* skip leading white space */ 847 while (isblank(*dp)) 848 ++dp; 849 850 /* skip comment lines */ 851 if (*dp == '#') { 852 while (*dp && (*dp != sep)) 853 ++dp; 854 ++dp; 855 continue; 856 } 857 858 /* parse name */ 859 for (name = dp; *dp != '=' && *dp && *dp != sep; ++dp) 860 ; 861 862 /* deal with "name" and "name=" entries (delete var) */ 863 if (*dp == '\0' || *(dp + 1) == '\0' || 864 *dp == sep || *(dp + 1) == sep) { 865 if (*dp == '=') 866 *dp++ = '\0'; 867 *dp++ = '\0'; /* terminate name */ 868 869 debug("DELETE CANDIDATE: \"%s\"\n", name); 870 if (!drop_var_from_set(name, nvars, localvars)) 871 continue; 872 873 if (hdelete_r(name, htab, flag) == 0) 874 debug("DELETE ERROR ##############################\n"); 875 876 continue; 877 } 878 *dp++ = '\0'; /* terminate name */ 879 880 /* parse value; deal with escapes */ 881 for (value = sp = dp; *dp && (*dp != sep); ++dp) { 882 if ((*dp == '\\') && *(dp + 1)) 883 ++dp; 884 *sp++ = *dp; 885 } 886 *sp++ = '\0'; /* terminate value */ 887 ++dp; 888 889 /* Skip variables which are not supposed to be processed */ 890 if (!drop_var_from_set(name, nvars, localvars)) 891 continue; 892 893 /* enter into hash table */ 894 e.key = name; 895 e.data = value; 896 897 hsearch_r(e, ENTER, &rv, htab, flag); 898 if (rv == NULL) 899 printf("himport_r: can't insert \"%s=%s\" into hash table\n", 900 name, value); 901 902 debug("INSERT: table %p, filled %d/%d rv %p ==> name=\"%s\" value=\"%s\"\n", 903 htab, htab->filled, htab->size, 904 rv, name, value); 905 } while ((dp < data + size) && *dp); /* size check needed for text */ 906 /* without '\0' termination */ 907 debug("INSERT: free(data = %p)\n", data); 908 free(data); 909 910 /* process variables which were not considered */ 911 for (i = 0; i < nvars; i++) { 912 if (localvars[i] == NULL) 913 continue; 914 /* 915 * All variables which were not deleted from the variable list 916 * were not present in the imported env 917 * This could mean two things: 918 * a) if the variable was present in current env, we delete it 919 * b) if the variable was not present in current env, we notify 920 * it might be a typo 921 */ 922 if (hdelete_r(localvars[i], htab, flag) == 0) 923 printf("WARNING: '%s' neither in running nor in imported env!\n", localvars[i]); 924 else 925 printf("WARNING: '%s' not in imported env, deleting it!\n", localvars[i]); 926 } 927 928 debug("INSERT: done\n"); 929 return 1; /* everything OK */ 930 } 931 932 /* 933 * hwalk_r() 934 */ 935 936 /* 937 * Walk all of the entries in the hash, calling the callback for each one. 938 * this allows some generic operation to be performed on each element. 939 */ 940 int hwalk_r(struct hsearch_data *htab, int (*callback)(ENTRY *)) 941 { 942 int i; 943 int retval; 944 945 for (i = 1; i <= htab->size; ++i) { 946 if (htab->table[i].used > 0) { 947 retval = callback(&htab->table[i].entry); 948 if (retval) 949 return retval; 950 } 951 } 952 953 return 0; 954 } 955