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 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 ssize_t hexport_r(struct hsearch_data *htab, const char sep, int flag, 567 char **resp, size_t size, 568 int argc, char * const argv[]) 569 { 570 ENTRY *list[htab->size]; 571 char *res, *p; 572 size_t totlen; 573 int i, n; 574 575 /* Test for correct arguments. */ 576 if ((resp == NULL) || (htab == NULL)) { 577 __set_errno(EINVAL); 578 return (-1); 579 } 580 581 debug("EXPORT table = %p, htab.size = %d, htab.filled = %d, " 582 "size = %zu\n", htab, htab->size, htab->filled, size); 583 /* 584 * Pass 1: 585 * search used entries, 586 * save addresses and compute total length 587 */ 588 for (i = 1, n = 0, totlen = 0; i <= htab->size; ++i) { 589 590 if (htab->table[i].used > 0) { 591 ENTRY *ep = &htab->table[i].entry; 592 int arg, found = 0; 593 594 for (arg = 0; arg < argc; ++arg) { 595 if (strcmp(argv[arg], ep->key) == 0) { 596 found = 1; 597 break; 598 } 599 } 600 if ((argc > 0) && (found == 0)) 601 continue; 602 603 if ((flag & H_HIDE_DOT) && ep->key[0] == '.') 604 continue; 605 606 list[n++] = ep; 607 608 totlen += strlen(ep->key) + 2; 609 610 if (sep == '\0') { 611 totlen += strlen(ep->data); 612 } else { /* check if escapes are needed */ 613 char *s = ep->data; 614 615 while (*s) { 616 ++totlen; 617 /* add room for needed escape chars */ 618 if ((*s == sep) || (*s == '\\')) 619 ++totlen; 620 ++s; 621 } 622 } 623 totlen += 2; /* for '=' and 'sep' char */ 624 } 625 } 626 627 #ifdef DEBUG 628 /* Pass 1a: print unsorted list */ 629 printf("Unsorted: n=%d\n", n); 630 for (i = 0; i < n; ++i) { 631 printf("\t%3d: %p ==> %-10s => %s\n", 632 i, list[i], list[i]->key, list[i]->data); 633 } 634 #endif 635 636 /* Sort list by keys */ 637 qsort(list, n, sizeof(ENTRY *), cmpkey); 638 639 /* Check if the user supplied buffer size is sufficient */ 640 if (size) { 641 if (size < totlen + 1) { /* provided buffer too small */ 642 printf("Env export buffer too small: %zu, " 643 "but need %zu\n", size, totlen + 1); 644 __set_errno(ENOMEM); 645 return (-1); 646 } 647 } else { 648 size = totlen + 1; 649 } 650 651 /* Check if the user provided a buffer */ 652 if (*resp) { 653 /* yes; clear it */ 654 res = *resp; 655 memset(res, '\0', size); 656 } else { 657 /* no, allocate and clear one */ 658 *resp = res = calloc(1, size); 659 if (res == NULL) { 660 __set_errno(ENOMEM); 661 return (-1); 662 } 663 } 664 /* 665 * Pass 2: 666 * export sorted list of result data 667 */ 668 for (i = 0, p = res; i < n; ++i) { 669 const char *s; 670 671 s = list[i]->key; 672 while (*s) 673 *p++ = *s++; 674 *p++ = '='; 675 676 s = list[i]->data; 677 678 while (*s) { 679 if ((*s == sep) || (*s == '\\')) 680 *p++ = '\\'; /* escape */ 681 *p++ = *s++; 682 } 683 *p++ = sep; 684 } 685 *p = '\0'; /* terminate result */ 686 687 return size; 688 } 689 #endif 690 691 692 /* 693 * himport() 694 */ 695 696 /* 697 * Check whether variable 'name' is amongst vars[], 698 * and remove all instances by setting the pointer to NULL 699 */ 700 static int drop_var_from_set(const char *name, int nvars, char * vars[]) 701 { 702 int i = 0; 703 int res = 0; 704 705 /* No variables specified means process all of them */ 706 if (nvars == 0) 707 return 1; 708 709 for (i = 0; i < nvars; i++) { 710 if (vars[i] == NULL) 711 continue; 712 /* If we found it, delete all of them */ 713 if (!strcmp(name, vars[i])) { 714 vars[i] = NULL; 715 res = 1; 716 } 717 } 718 if (!res) 719 debug("Skipping non-listed variable %s\n", name); 720 721 return res; 722 } 723 724 /* 725 * Import linearized data into hash table. 726 * 727 * This is the inverse function to hexport(): it takes a linear list 728 * of "name=value" pairs and creates hash table entries from it. 729 * 730 * Entries without "value", i. e. consisting of only "name" or 731 * "name=", will cause this entry to be deleted from the hash table. 732 * 733 * The "flag" argument can be used to control the behaviour: when the 734 * H_NOCLEAR bit is set, then an existing hash table will kept, i. e. 735 * new data will be added to an existing hash table; otherwise, old 736 * data will be discarded and a new hash table will be created. 737 * 738 * The separator character for the "name=value" pairs can be selected, 739 * so we both support importing from externally stored environment 740 * data (separated by NUL characters) and from plain text files 741 * (entries separated by newline characters). 742 * 743 * To allow for nicely formatted text input, leading white space 744 * (sequences of SPACE and TAB chars) is ignored, and entries starting 745 * (after removal of any leading white space) with a '#' character are 746 * considered comments and ignored. 747 * 748 * [NOTE: this means that a variable name cannot start with a '#' 749 * character.] 750 * 751 * When using a non-NUL separator character, backslash is used as 752 * escape character in the value part, allowing for example for 753 * multi-line values. 754 * 755 * In theory, arbitrary separator characters can be used, but only 756 * '\0' and '\n' have really been tested. 757 */ 758 759 int himport_r(struct hsearch_data *htab, 760 const char *env, size_t size, const char sep, int flag, 761 int nvars, char * const vars[]) 762 { 763 char *data, *sp, *dp, *name, *value; 764 char *localvars[nvars]; 765 int i; 766 767 /* Test for correct arguments. */ 768 if (htab == NULL) { 769 __set_errno(EINVAL); 770 return 0; 771 } 772 773 /* we allocate new space to make sure we can write to the array */ 774 if ((data = malloc(size)) == NULL) { 775 debug("himport_r: can't malloc %zu bytes\n", size); 776 __set_errno(ENOMEM); 777 return 0; 778 } 779 memcpy(data, env, size); 780 dp = data; 781 782 /* make a local copy of the list of variables */ 783 if (nvars) 784 memcpy(localvars, vars, sizeof(vars[0]) * nvars); 785 786 if ((flag & H_NOCLEAR) == 0) { 787 /* Destroy old hash table if one exists */ 788 debug("Destroy Hash Table: %p table = %p\n", htab, 789 htab->table); 790 if (htab->table) 791 hdestroy_r(htab); 792 } 793 794 /* 795 * Create new hash table (if needed). The computation of the hash 796 * table size is based on heuristics: in a sample of some 70+ 797 * existing systems we found an average size of 39+ bytes per entry 798 * in the environment (for the whole key=value pair). Assuming a 799 * size of 8 per entry (= safety factor of ~5) should provide enough 800 * safety margin for any existing environment definitions and still 801 * allow for more than enough dynamic additions. Note that the 802 * "size" argument is supposed to give the maximum enviroment size 803 * (CONFIG_ENV_SIZE). This heuristics will result in 804 * unreasonably large numbers (and thus memory footprint) for 805 * big flash environments (>8,000 entries for 64 KB 806 * envrionment size), so we clip it to a reasonable value. 807 * On the other hand we need to add some more entries for free 808 * space when importing very small buffers. Both boundaries can 809 * be overwritten in the board config file if needed. 810 */ 811 812 if (!htab->table) { 813 int nent = CONFIG_ENV_MIN_ENTRIES + size / 8; 814 815 if (nent > CONFIG_ENV_MAX_ENTRIES) 816 nent = CONFIG_ENV_MAX_ENTRIES; 817 818 debug("Create Hash Table: N=%d\n", nent); 819 820 if (hcreate_r(nent, htab) == 0) { 821 free(data); 822 return 0; 823 } 824 } 825 826 /* Parse environment; allow for '\0' and 'sep' as separators */ 827 do { 828 ENTRY e, *rv; 829 830 /* skip leading white space */ 831 while (isblank(*dp)) 832 ++dp; 833 834 /* skip comment lines */ 835 if (*dp == '#') { 836 while (*dp && (*dp != sep)) 837 ++dp; 838 ++dp; 839 continue; 840 } 841 842 /* parse name */ 843 for (name = dp; *dp != '=' && *dp && *dp != sep; ++dp) 844 ; 845 846 /* deal with "name" and "name=" entries (delete var) */ 847 if (*dp == '\0' || *(dp + 1) == '\0' || 848 *dp == sep || *(dp + 1) == sep) { 849 if (*dp == '=') 850 *dp++ = '\0'; 851 *dp++ = '\0'; /* terminate name */ 852 853 debug("DELETE CANDIDATE: \"%s\"\n", name); 854 if (!drop_var_from_set(name, nvars, localvars)) 855 continue; 856 857 if (hdelete_r(name, htab, flag) == 0) 858 debug("DELETE ERROR ##############################\n"); 859 860 continue; 861 } 862 *dp++ = '\0'; /* terminate name */ 863 864 /* parse value; deal with escapes */ 865 for (value = sp = dp; *dp && (*dp != sep); ++dp) { 866 if ((*dp == '\\') && *(dp + 1)) 867 ++dp; 868 *sp++ = *dp; 869 } 870 *sp++ = '\0'; /* terminate value */ 871 ++dp; 872 873 /* Skip variables which are not supposed to be processed */ 874 if (!drop_var_from_set(name, nvars, localvars)) 875 continue; 876 877 /* enter into hash table */ 878 e.key = name; 879 e.data = value; 880 881 hsearch_r(e, ENTER, &rv, htab, flag); 882 if (rv == NULL) 883 printf("himport_r: can't insert \"%s=%s\" into hash table\n", 884 name, value); 885 886 debug("INSERT: table %p, filled %d/%d rv %p ==> name=\"%s\" value=\"%s\"\n", 887 htab, htab->filled, htab->size, 888 rv, name, value); 889 } while ((dp < data + size) && *dp); /* size check needed for text */ 890 /* without '\0' termination */ 891 debug("INSERT: free(data = %p)\n", data); 892 free(data); 893 894 /* process variables which were not considered */ 895 for (i = 0; i < nvars; i++) { 896 if (localvars[i] == NULL) 897 continue; 898 /* 899 * All variables which were not deleted from the variable list 900 * were not present in the imported env 901 * This could mean two things: 902 * a) if the variable was present in current env, we delete it 903 * b) if the variable was not present in current env, we notify 904 * it might be a typo 905 */ 906 if (hdelete_r(localvars[i], htab, flag) == 0) 907 printf("WARNING: '%s' neither in running nor in imported env!\n", localvars[i]); 908 else 909 printf("WARNING: '%s' not in imported env, deleting it!\n", localvars[i]); 910 } 911 912 debug("INSERT: done\n"); 913 return 1; /* everything OK */ 914 } 915 916 /* 917 * hwalk_r() 918 */ 919 920 /* 921 * Walk all of the entries in the hash, calling the callback for each one. 922 * this allows some generic operation to be performed on each element. 923 */ 924 int hwalk_r(struct hsearch_data *htab, int (*callback)(ENTRY *)) 925 { 926 int i; 927 int retval; 928 929 for (i = 1; i <= htab->size; ++i) { 930 if (htab->table[i].used > 0) { 931 retval = callback(&htab->table[i].entry); 932 if (retval) 933 return retval; 934 } 935 } 936 937 return 0; 938 } 939