Wolfgang Denk | c9ae986 | 2010-06-20 13:17:12 +0200 | [diff] [blame] | 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 | * |
Wolfgang Denk | 565af27 | 2013-03-23 23:50:28 +0000 | [diff] [blame^] | 5 | * Copyright (C) 2010-2013 Wolfgang Denk <wd@denx.de> |
Wolfgang Denk | c9ae986 | 2010-06-20 13:17:12 +0200 | [diff] [blame] | 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> |
Jason Hobbs | cafa1aa | 2011-08-23 11:06:54 +0000 | [diff] [blame] | 35 | # include <ctype.h> |
Wolfgang Denk | c9ae986 | 2010-06-20 13:17:12 +0200 | [diff] [blame] | 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> |
Jason Hobbs | cafa1aa | 2011-08-23 11:06:54 +0000 | [diff] [blame] | 47 | # include <linux/ctype.h> |
Wolfgang Denk | c9ae986 | 2010-06-20 13:17:12 +0200 | [diff] [blame] | 48 | #endif |
| 49 | |
Andreas Bießmann | 2638b2b | 2010-10-01 22:51:02 +0200 | [diff] [blame] | 50 | #ifndef CONFIG_ENV_MIN_ENTRIES /* minimum number of entries */ |
| 51 | #define CONFIG_ENV_MIN_ENTRIES 64 |
| 52 | #endif |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 53 | #ifndef CONFIG_ENV_MAX_ENTRIES /* maximum number of entries */ |
| 54 | #define CONFIG_ENV_MAX_ENTRIES 512 |
| 55 | #endif |
| 56 | |
Joe Hershberger | 60fd3ad | 2012-12-11 22:16:24 -0600 | [diff] [blame] | 57 | #include <env_callback.h> |
Joe Hershberger | 71497d0 | 2012-12-11 22:16:31 -0600 | [diff] [blame] | 58 | #include <env_flags.h> |
Joe Hershberger | 60fd3ad | 2012-12-11 22:16:24 -0600 | [diff] [blame] | 59 | #include <search.h> |
Wolfgang Denk | c9ae986 | 2010-06-20 13:17:12 +0200 | [diff] [blame] | 60 | |
| 61 | /* |
| 62 | * [Aho,Sethi,Ullman] Compilers: Principles, Techniques and Tools, 1986 |
Wolfgang Denk | 1136f69 | 2010-10-27 22:48:30 +0200 | [diff] [blame] | 63 | * [Knuth] The Art of Computer Programming, part 3 (6.4) |
Wolfgang Denk | c9ae986 | 2010-06-20 13:17:12 +0200 | [diff] [blame] | 64 | */ |
| 65 | |
| 66 | /* |
Wolfgang Denk | c9ae986 | 2010-06-20 13:17:12 +0200 | [diff] [blame] | 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 | */ |
Joe Hershberger | 638e659 | 2012-12-11 22:16:21 -0600 | [diff] [blame] | 71 | |
Wolfgang Denk | c9ae986 | 2010-06-20 13:17:12 +0200 | [diff] [blame] | 72 | typedef struct _ENTRY { |
Peter Barada | 305775b | 2011-03-21 19:05:20 -0400 | [diff] [blame] | 73 | int used; |
Wolfgang Denk | c9ae986 | 2010-06-20 13:17:12 +0200 | [diff] [blame] | 74 | ENTRY entry; |
| 75 | } _ENTRY; |
| 76 | |
| 77 | |
Joe Hershberger | 638e659 | 2012-12-11 22:16:21 -0600 | [diff] [blame] | 78 | static void _hdelete(const char *key, struct hsearch_data *htab, ENTRY *ep, |
| 79 | int idx); |
| 80 | |
Wolfgang Denk | c9ae986 | 2010-06-20 13:17:12 +0200 | [diff] [blame] | 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 | |
Wolfgang Denk | c9ae986 | 2010-06-20 13:17:12 +0200 | [diff] [blame] | 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 | */ |
Mike Frysinger | 92d7a99 | 2010-12-08 06:26:04 -0500 | [diff] [blame] | 111 | |
Wolfgang Denk | c9ae986 | 2010-06-20 13:17:12 +0200 | [diff] [blame] | 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 | */ |
Wolfgang Denk | c9ae986 | 2010-06-20 13:17:12 +0200 | [diff] [blame] | 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 | */ |
Mike Frysinger | 92d7a99 | 2010-12-08 06:26:04 -0500 | [diff] [blame] | 150 | |
Joe Hershberger | a46f770 | 2012-12-11 22:16:19 -0600 | [diff] [blame] | 151 | void hdestroy_r(struct hsearch_data *htab) |
Wolfgang Denk | c9ae986 | 2010-06-20 13:17:12 +0200 | [diff] [blame] | 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) { |
Peter Barada | 305775b | 2011-03-21 19:05:20 -0400 | [diff] [blame] | 163 | if (htab->table[i].used > 0) { |
Wolfgang Denk | c9ae986 | 2010-06-20 13:17:12 +0200 | [diff] [blame] | 164 | ENTRY *ep = &htab->table[i].entry; |
Joe Hershberger | a46f770 | 2012-12-11 22:16:19 -0600 | [diff] [blame] | 165 | |
Wolfgang Denk | d67abff | 2011-07-29 14:42:18 +0200 | [diff] [blame] | 166 | free((void *)ep->key); |
Wolfgang Denk | c9ae986 | 2010-06-20 13:17:12 +0200 | [diff] [blame] | 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 | |
Kim Phillips | f0c9d53 | 2011-04-05 07:15:14 +0000 | [diff] [blame] | 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 | |
Mike Frysinger | 8d88232 | 2010-12-17 16:51:59 -0500 | [diff] [blame] | 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) { |
Kim Phillips | f73d935 | 2011-04-04 15:17:45 +0000 | [diff] [blame] | 243 | if (htab->table[idx].used <= 0) |
Mike Frysinger | 8d88232 | 2010-12-17 16:51:59 -0500 | [diff] [blame] | 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 | |
Joe Hershberger | e5611c3 | 2012-12-11 22:16:20 -0600 | [diff] [blame] | 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)) { |
Joe Hershberger | 638e659 | 2012-12-11 22:16:21 -0600 | [diff] [blame] | 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 | |
Joe Hershberger | 60fd3ad | 2012-12-11 22:16:24 -0600 | [diff] [blame] | 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 | |
Joe Hershberger | e5611c3 | 2012-12-11 22:16:20 -0600 | [diff] [blame] | 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 | |
Wolfgang Denk | c9ae986 | 2010-06-20 13:17:12 +0200 | [diff] [blame] | 306 | int hsearch_r(ENTRY item, ACTION action, ENTRY ** retval, |
Joe Hershberger | a46f770 | 2012-12-11 22:16:19 -0600 | [diff] [blame] | 307 | struct hsearch_data *htab, int flag) |
Wolfgang Denk | c9ae986 | 2010-06-20 13:17:12 +0200 | [diff] [blame] | 308 | { |
| 309 | unsigned int hval; |
| 310 | unsigned int count; |
| 311 | unsigned int len = strlen(item.key); |
| 312 | unsigned int idx; |
Peter Barada | 305775b | 2011-03-21 19:05:20 -0400 | [diff] [blame] | 313 | unsigned int first_deleted = 0; |
Joe Hershberger | e5611c3 | 2012-12-11 22:16:20 -0600 | [diff] [blame] | 314 | int ret; |
Wolfgang Denk | c9ae986 | 2010-06-20 13:17:12 +0200 | [diff] [blame] | 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 | /* |
Wolfgang Denk | 1136f69 | 2010-10-27 22:48:30 +0200 | [diff] [blame] | 337 | * Further action might be required according to the |
Wolfgang Denk | c9ae986 | 2010-06-20 13:17:12 +0200 | [diff] [blame] | 338 | * action value. |
| 339 | */ |
| 340 | unsigned hval2; |
| 341 | |
Peter Barada | 305775b | 2011-03-21 19:05:20 -0400 | [diff] [blame] | 342 | if (htab->table[idx].used == -1 |
| 343 | && !first_deleted) |
| 344 | first_deleted = idx; |
| 345 | |
Joe Hershberger | e5611c3 | 2012-12-11 22:16:20 -0600 | [diff] [blame] | 346 | ret = _compare_and_overwrite_entry(item, action, retval, htab, |
| 347 | flag, hval, idx); |
| 348 | if (ret != -1) |
| 349 | return ret; |
Wolfgang Denk | c9ae986 | 2010-06-20 13:17:12 +0200 | [diff] [blame] | 350 | |
| 351 | /* |
| 352 | * Second hash function: |
| 353 | * as suggested in [Knuth] |
| 354 | */ |
| 355 | hval2 = 1 + hval % (htab->size - 2); |
| 356 | |
| 357 | do { |
| 358 | /* |
Wolfgang Denk | 1136f69 | 2010-10-27 22:48:30 +0200 | [diff] [blame] | 359 | * Because SIZE is prime this guarantees to |
| 360 | * step through all available indices. |
Wolfgang Denk | c9ae986 | 2010-06-20 13:17:12 +0200 | [diff] [blame] | 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. */ |
Joe Hershberger | e5611c3 | 2012-12-11 22:16:20 -0600 | [diff] [blame] | 375 | ret = _compare_and_overwrite_entry(item, action, retval, |
| 376 | htab, flag, hval, idx); |
| 377 | if (ret != -1) |
| 378 | return ret; |
Wolfgang Denk | c9ae986 | 2010-06-20 13:17:12 +0200 | [diff] [blame] | 379 | } |
| 380 | while (htab->table[idx].used); |
| 381 | } |
| 382 | |
| 383 | /* An empty bucket has been found. */ |
| 384 | if (action == ENTER) { |
| 385 | /* |
Wolfgang Denk | 1136f69 | 2010-10-27 22:48:30 +0200 | [diff] [blame] | 386 | * If table is full and another entry should be |
| 387 | * entered return with error. |
Wolfgang Denk | c9ae986 | 2010-06-20 13:17:12 +0200 | [diff] [blame] | 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 | */ |
Peter Barada | 305775b | 2011-03-21 19:05:20 -0400 | [diff] [blame] | 399 | if (first_deleted) |
| 400 | idx = first_deleted; |
| 401 | |
Wolfgang Denk | c9ae986 | 2010-06-20 13:17:12 +0200 | [diff] [blame] | 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 | |
Joe Hershberger | 60fd3ad | 2012-12-11 22:16:24 -0600 | [diff] [blame] | 414 | /* This is a new entry, so look up a possible callback */ |
| 415 | env_callback_init(&htab->table[idx].entry); |
Joe Hershberger | 71497d0 | 2012-12-11 22:16:31 -0600 | [diff] [blame] | 416 | /* Also look for flags */ |
| 417 | env_flags_init(&htab->table[idx].entry); |
Joe Hershberger | 60fd3ad | 2012-12-11 22:16:24 -0600 | [diff] [blame] | 418 | |
Joe Hershberger | 638e659 | 2012-12-11 22:16:21 -0600 | [diff] [blame] | 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 | |
Joe Hershberger | 60fd3ad | 2012-12-11 22:16:24 -0600 | [diff] [blame] | 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 | |
Wolfgang Denk | c9ae986 | 2010-06-20 13:17:12 +0200 | [diff] [blame] | 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 | |
Joe Hershberger | 638e659 | 2012-12-11 22:16:21 -0600 | [diff] [blame] | 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); |
Joe Hershberger | 60fd3ad | 2012-12-11 22:16:24 -0600 | [diff] [blame] | 470 | ep->callback = NULL; |
Joe Hershberger | 71497d0 | 2012-12-11 22:16:31 -0600 | [diff] [blame] | 471 | ep->flags = 0; |
Joe Hershberger | 638e659 | 2012-12-11 22:16:21 -0600 | [diff] [blame] | 472 | htab->table[idx].used = -1; |
| 473 | |
| 474 | --htab->filled; |
| 475 | } |
| 476 | |
Joe Hershberger | a46f770 | 2012-12-11 22:16:19 -0600 | [diff] [blame] | 477 | int hdelete_r(const char *key, struct hsearch_data *htab, int flag) |
Wolfgang Denk | c9ae986 | 2010-06-20 13:17:12 +0200 | [diff] [blame] | 478 | { |
| 479 | ENTRY e, *ep; |
| 480 | int idx; |
| 481 | |
| 482 | debug("hdelete: DELETE key \"%s\"\n", key); |
| 483 | |
| 484 | e.key = (char *)key; |
| 485 | |
Joe Hershberger | a46f770 | 2012-12-11 22:16:19 -0600 | [diff] [blame] | 486 | idx = hsearch_r(e, FIND, &ep, htab, 0); |
| 487 | if (idx == 0) { |
Wolfgang Denk | c9ae986 | 2010-06-20 13:17:12 +0200 | [diff] [blame] | 488 | __set_errno(ESRCH); |
| 489 | return 0; /* not found */ |
| 490 | } |
| 491 | |
Joe Hershberger | a46f770 | 2012-12-11 22:16:19 -0600 | [diff] [blame] | 492 | /* Check for permission */ |
Joe Hershberger | 638e659 | 2012-12-11 22:16:21 -0600 | [diff] [blame] | 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); |
Joe Hershberger | a46f770 | 2012-12-11 22:16:19 -0600 | [diff] [blame] | 497 | __set_errno(EPERM); |
| 498 | return 0; |
| 499 | } |
| 500 | |
Joe Hershberger | 60fd3ad | 2012-12-11 22:16:24 -0600 | [diff] [blame] | 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 | |
Joe Hershberger | 638e659 | 2012-12-11 22:16:21 -0600 | [diff] [blame] | 510 | _hdelete(key, htab, ep, idx); |
Wolfgang Denk | c9ae986 | 2010-06-20 13:17:12 +0200 | [diff] [blame] | 511 | |
| 512 | return 1; |
| 513 | } |
| 514 | |
| 515 | /* |
| 516 | * hexport() |
| 517 | */ |
| 518 | |
Ilya Yanok | f7a2c55 | 2012-09-18 00:22:50 +0000 | [diff] [blame] | 519 | #ifndef CONFIG_SPL_BUILD |
Wolfgang Denk | c9ae986 | 2010-06-20 13:17:12 +0200 | [diff] [blame] | 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 | |
Wolfgang Denk | c9ae986 | 2010-06-20 13:17:12 +0200 | [diff] [blame] | 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 | |
Wolfgang Denk | 565af27 | 2013-03-23 23:50:28 +0000 | [diff] [blame^] | 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 | |
Joe Hershberger | 79a905e | 2012-12-11 22:16:23 -0600 | [diff] [blame] | 588 | ssize_t hexport_r(struct hsearch_data *htab, const char sep, int flag, |
Wolfgang Denk | 1e3cdf3 | 2011-11-06 22:49:44 +0100 | [diff] [blame] | 589 | char **resp, size_t size, |
| 590 | int argc, char * const argv[]) |
Wolfgang Denk | c9ae986 | 2010-06-20 13:17:12 +0200 | [diff] [blame] | 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 | |
Simon Glass | 60b07a1 | 2011-11-21 19:04:23 +0000 | [diff] [blame] | 603 | debug("EXPORT table = %p, htab.size = %d, htab.filled = %d, " |
| 604 | "size = %zu\n", htab, htab->size, htab->filled, size); |
Wolfgang Denk | c9ae986 | 2010-06-20 13:17:12 +0200 | [diff] [blame] | 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 | |
Peter Barada | 305775b | 2011-03-21 19:05:20 -0400 | [diff] [blame] | 612 | if (htab->table[i].used > 0) { |
Wolfgang Denk | c9ae986 | 2010-06-20 13:17:12 +0200 | [diff] [blame] | 613 | ENTRY *ep = &htab->table[i].entry; |
Wolfgang Denk | 565af27 | 2013-03-23 23:50:28 +0000 | [diff] [blame^] | 614 | int found = match_strings(ep, flag, argc, argv); |
Wolfgang Denk | 1e3cdf3 | 2011-11-06 22:49:44 +0100 | [diff] [blame] | 615 | |
Wolfgang Denk | 1e3cdf3 | 2011-11-06 22:49:44 +0100 | [diff] [blame] | 616 | if ((argc > 0) && (found == 0)) |
| 617 | continue; |
Wolfgang Denk | c9ae986 | 2010-06-20 13:17:12 +0200 | [diff] [blame] | 618 | |
Joe Hershberger | 79a905e | 2012-12-11 22:16:23 -0600 | [diff] [blame] | 619 | if ((flag & H_HIDE_DOT) && ep->key[0] == '.') |
| 620 | continue; |
| 621 | |
Wolfgang Denk | c9ae986 | 2010-06-20 13:17:12 +0200 | [diff] [blame] | 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 */ |
Simon Glass | 60b07a1 | 2011-11-21 19:04:23 +0000 | [diff] [blame] | 658 | printf("Env export buffer too small: %zu, " |
| 659 | "but need %zu\n", size, totlen + 1); |
Wolfgang Denk | c9ae986 | 2010-06-20 13:17:12 +0200 | [diff] [blame] | 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) { |
Wolfgang Denk | d67abff | 2011-07-29 14:42:18 +0200 | [diff] [blame] | 685 | const char *s; |
Wolfgang Denk | c9ae986 | 2010-06-20 13:17:12 +0200 | [diff] [blame] | 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 | } |
Ilya Yanok | f7a2c55 | 2012-09-18 00:22:50 +0000 | [diff] [blame] | 705 | #endif |
Wolfgang Denk | c9ae986 | 2010-06-20 13:17:12 +0200 | [diff] [blame] | 706 | |
| 707 | |
| 708 | /* |
| 709 | * himport() |
| 710 | */ |
| 711 | |
Gerlando Falauto | 6765eb1 | 2012-08-26 21:53:00 +0000 | [diff] [blame] | 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[]) |
Gerlando Falauto | 9b0e1ff | 2012-08-24 00:11:38 +0000 | [diff] [blame] | 717 | { |
| 718 | int i = 0; |
Gerlando Falauto | 6765eb1 | 2012-08-26 21:53:00 +0000 | [diff] [blame] | 719 | int res = 0; |
Gerlando Falauto | 9b0e1ff | 2012-08-24 00:11:38 +0000 | [diff] [blame] | 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++) { |
Gerlando Falauto | 6765eb1 | 2012-08-26 21:53:00 +0000 | [diff] [blame] | 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 | } |
Gerlando Falauto | 9b0e1ff | 2012-08-24 00:11:38 +0000 | [diff] [blame] | 733 | } |
Gerlando Falauto | 6765eb1 | 2012-08-26 21:53:00 +0000 | [diff] [blame] | 734 | if (!res) |
| 735 | debug("Skipping non-listed variable %s\n", name); |
Gerlando Falauto | 9b0e1ff | 2012-08-24 00:11:38 +0000 | [diff] [blame] | 736 | |
Gerlando Falauto | 6765eb1 | 2012-08-26 21:53:00 +0000 | [diff] [blame] | 737 | return res; |
Gerlando Falauto | 9b0e1ff | 2012-08-24 00:11:38 +0000 | [diff] [blame] | 738 | } |
| 739 | |
Wolfgang Denk | c9ae986 | 2010-06-20 13:17:12 +0200 | [diff] [blame] | 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 | |
Wolfgang Denk | c9ae986 | 2010-06-20 13:17:12 +0200 | [diff] [blame] | 775 | int himport_r(struct hsearch_data *htab, |
Gerlando Falauto | 9b0e1ff | 2012-08-24 00:11:38 +0000 | [diff] [blame] | 776 | const char *env, size_t size, const char sep, int flag, |
Joe Hershberger | a46f770 | 2012-12-11 22:16:19 -0600 | [diff] [blame] | 777 | int nvars, char * const vars[]) |
Wolfgang Denk | c9ae986 | 2010-06-20 13:17:12 +0200 | [diff] [blame] | 778 | { |
| 779 | char *data, *sp, *dp, *name, *value; |
Gerlando Falauto | 6765eb1 | 2012-08-26 21:53:00 +0000 | [diff] [blame] | 780 | char *localvars[nvars]; |
| 781 | int i; |
Wolfgang Denk | c9ae986 | 2010-06-20 13:17:12 +0200 | [diff] [blame] | 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) { |
Simon Glass | 60b07a1 | 2011-11-21 19:04:23 +0000 | [diff] [blame] | 791 | debug("himport_r: can't malloc %zu bytes\n", size); |
Wolfgang Denk | c9ae986 | 2010-06-20 13:17:12 +0200 | [diff] [blame] | 792 | __set_errno(ENOMEM); |
| 793 | return 0; |
| 794 | } |
| 795 | memcpy(data, env, size); |
| 796 | dp = data; |
| 797 | |
Gerlando Falauto | 6765eb1 | 2012-08-26 21:53:00 +0000 | [diff] [blame] | 798 | /* make a local copy of the list of variables */ |
| 799 | if (nvars) |
| 800 | memcpy(localvars, vars, sizeof(vars[0]) * nvars); |
| 801 | |
Wolfgang Denk | c9ae986 | 2010-06-20 13:17:12 +0200 | [diff] [blame] | 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) |
Joe Hershberger | a46f770 | 2012-12-11 22:16:19 -0600 | [diff] [blame] | 807 | hdestroy_r(htab); |
Wolfgang Denk | c9ae986 | 2010-06-20 13:17:12 +0200 | [diff] [blame] | 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 |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 815 | * size of 8 per entry (= safety factor of ~5) should provide enough |
| 816 | * safety margin for any existing environment definitions and still |
Wolfgang Denk | c9ae986 | 2010-06-20 13:17:12 +0200 | [diff] [blame] | 817 | * allow for more than enough dynamic additions. Note that the |
| 818 | * "size" argument is supposed to give the maximum enviroment size |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 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 |
Andreas Bießmann | 2638b2b | 2010-10-01 22:51:02 +0200 | [diff] [blame] | 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. |
Wolfgang Denk | c9ae986 | 2010-06-20 13:17:12 +0200 | [diff] [blame] | 826 | */ |
| 827 | |
| 828 | if (!htab->table) { |
Andreas Bießmann | 2638b2b | 2010-10-01 22:51:02 +0200 | [diff] [blame] | 829 | int nent = CONFIG_ENV_MIN_ENTRIES + size / 8; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 830 | |
| 831 | if (nent > CONFIG_ENV_MAX_ENTRIES) |
| 832 | nent = CONFIG_ENV_MAX_ENTRIES; |
Wolfgang Denk | c9ae986 | 2010-06-20 13:17:12 +0200 | [diff] [blame] | 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 */ |
Jason Hobbs | cafa1aa | 2011-08-23 11:06:54 +0000 | [diff] [blame] | 847 | while (isblank(*dp)) |
Wolfgang Denk | c9ae986 | 2010-06-20 13:17:12 +0200 | [diff] [blame] | 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); |
Gerlando Falauto | 6765eb1 | 2012-08-26 21:53:00 +0000 | [diff] [blame] | 870 | if (!drop_var_from_set(name, nvars, localvars)) |
Gerlando Falauto | 9b0e1ff | 2012-08-24 00:11:38 +0000 | [diff] [blame] | 871 | continue; |
Wolfgang Denk | c9ae986 | 2010-06-20 13:17:12 +0200 | [diff] [blame] | 872 | |
Joe Hershberger | a46f770 | 2012-12-11 22:16:19 -0600 | [diff] [blame] | 873 | if (hdelete_r(name, htab, flag) == 0) |
Wolfgang Denk | c9ae986 | 2010-06-20 13:17:12 +0200 | [diff] [blame] | 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 | |
Gerlando Falauto | 9b0e1ff | 2012-08-24 00:11:38 +0000 | [diff] [blame] | 889 | /* Skip variables which are not supposed to be processed */ |
Gerlando Falauto | 6765eb1 | 2012-08-26 21:53:00 +0000 | [diff] [blame] | 890 | if (!drop_var_from_set(name, nvars, localvars)) |
Gerlando Falauto | 9b0e1ff | 2012-08-24 00:11:38 +0000 | [diff] [blame] | 891 | continue; |
| 892 | |
Wolfgang Denk | c9ae986 | 2010-06-20 13:17:12 +0200 | [diff] [blame] | 893 | /* enter into hash table */ |
| 894 | e.key = name; |
| 895 | e.data = value; |
| 896 | |
Joe Hershberger | a46f770 | 2012-12-11 22:16:19 -0600 | [diff] [blame] | 897 | hsearch_r(e, ENTER, &rv, htab, flag); |
Joe Hershberger | 60fd3ad | 2012-12-11 22:16:24 -0600 | [diff] [blame] | 898 | if (rv == NULL) |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 899 | printf("himport_r: can't insert \"%s=%s\" into hash table\n", |
| 900 | name, value); |
Wolfgang Denk | c9ae986 | 2010-06-20 13:17:12 +0200 | [diff] [blame] | 901 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 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); |
Wolfgang Denk | c9ae986 | 2010-06-20 13:17:12 +0200 | [diff] [blame] | 905 | } while ((dp < data + size) && *dp); /* size check needed for text */ |
| 906 | /* without '\0' termination */ |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 907 | debug("INSERT: free(data = %p)\n", data); |
Wolfgang Denk | c9ae986 | 2010-06-20 13:17:12 +0200 | [diff] [blame] | 908 | free(data); |
| 909 | |
Gerlando Falauto | 6765eb1 | 2012-08-26 21:53:00 +0000 | [diff] [blame] | 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 | */ |
Joe Hershberger | a46f770 | 2012-12-11 22:16:19 -0600 | [diff] [blame] | 922 | if (hdelete_r(localvars[i], htab, flag) == 0) |
Gerlando Falauto | 6765eb1 | 2012-08-26 21:53:00 +0000 | [diff] [blame] | 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 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 928 | debug("INSERT: done\n"); |
Wolfgang Denk | c9ae986 | 2010-06-20 13:17:12 +0200 | [diff] [blame] | 929 | return 1; /* everything OK */ |
| 930 | } |
Joe Hershberger | 60fd3ad | 2012-12-11 22:16:24 -0600 | [diff] [blame] | 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 | } |