list.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  1. /*
  2. * libuci - Library for the Unified Configuration Interface
  3. * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License version 2.1
  7. * as published by the Free Software Foundation
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU Lesser General Public License for more details.
  13. */
  14. static void uci_list_set_pos(struct uci_list *head, struct uci_list *ptr, int pos)
  15. {
  16. struct uci_list *new_head = head;
  17. struct uci_element *p = NULL;
  18. uci_list_del(ptr);
  19. uci_foreach_element(head, p) {
  20. if (pos-- <= 0)
  21. break;
  22. new_head = &p->list;
  23. }
  24. uci_list_add(new_head->next, ptr);
  25. }
  26. static inline void uci_list_fixup(struct uci_list *ptr)
  27. {
  28. ptr->prev->next = ptr;
  29. ptr->next->prev = ptr;
  30. }
  31. /*
  32. * uci_alloc_generic allocates a new uci_element with payload
  33. * payload is appended to the struct to save memory and reduce fragmentation
  34. */
  35. __private struct uci_element *
  36. uci_alloc_generic(struct uci_context *ctx, int type, const char *name, int size)
  37. {
  38. struct uci_element *e;
  39. int datalen = size;
  40. void *ptr;
  41. ptr = uci_malloc(ctx, datalen);
  42. e = (struct uci_element *) ptr;
  43. e->type = type;
  44. if (name) {
  45. UCI_TRAP_SAVE(ctx, error);
  46. e->name = uci_strdup(ctx, name);
  47. UCI_TRAP_RESTORE(ctx);
  48. }
  49. uci_list_init(&e->list);
  50. goto done;
  51. error:
  52. free(ptr);
  53. UCI_THROW(ctx, ctx->err);
  54. done:
  55. return e;
  56. }
  57. __private void
  58. uci_free_element(struct uci_element *e)
  59. {
  60. free(e->name);
  61. if (!uci_list_empty(&e->list))
  62. uci_list_del(&e->list);
  63. free(e);
  64. }
  65. static struct uci_option *
  66. uci_alloc_option(struct uci_section *s, const char *name, const char *value)
  67. {
  68. struct uci_package *p = s->package;
  69. struct uci_context *ctx = p->ctx;
  70. struct uci_option *o;
  71. o = uci_alloc_element(ctx, option, name, strlen(value) + 1);
  72. o->type = UCI_TYPE_STRING;
  73. o->v.string = uci_dataptr(o);
  74. o->section = s;
  75. strcpy(o->v.string, value);
  76. uci_list_add(&s->options, &o->e.list);
  77. return o;
  78. }
  79. static inline void
  80. uci_free_option(struct uci_option *o)
  81. {
  82. struct uci_element *e, *tmp;
  83. switch(o->type) {
  84. case UCI_TYPE_STRING:
  85. if ((o->v.string != uci_dataptr(o)) &&
  86. (o->v.string != NULL))
  87. free(o->v.string);
  88. break;
  89. case UCI_TYPE_LIST:
  90. uci_foreach_element_safe(&o->v.list, tmp, e) {
  91. uci_free_element(e);
  92. }
  93. break;
  94. default:
  95. break;
  96. }
  97. uci_free_element(&o->e);
  98. }
  99. static struct uci_option *
  100. uci_alloc_list(struct uci_section *s, const char *name)
  101. {
  102. struct uci_package *p = s->package;
  103. struct uci_context *ctx = p->ctx;
  104. struct uci_option *o;
  105. o = uci_alloc_element(ctx, option, name, 0);
  106. o->type = UCI_TYPE_LIST;
  107. o->section = s;
  108. uci_list_init(&o->v.list);
  109. uci_list_add(&s->options, &o->e.list);
  110. return o;
  111. }
  112. /* Based on an efficient hash function published by D. J. Bernstein */
  113. static unsigned int djbhash(unsigned int hash, char *str)
  114. {
  115. int len = strlen(str);
  116. int i;
  117. /* initial value */
  118. if (hash == ~0)
  119. hash = 5381;
  120. for(i = 0; i < len; i++) {
  121. hash = ((hash << 5) + hash) + str[i];
  122. }
  123. return (hash & 0x7FFFFFFF);
  124. }
  125. /* fix up an unnamed section, e.g. after adding options to it */
  126. static void uci_fixup_section(struct uci_context *ctx, struct uci_section *s)
  127. {
  128. unsigned int hash = ~0;
  129. struct uci_element *e;
  130. char buf[16];
  131. if (!s || s->e.name)
  132. return;
  133. /*
  134. * Generate a name for unnamed sections. This is used as reference
  135. * when locating or updating the section from apps/scripts.
  136. * To make multiple concurrent versions somewhat safe for updating,
  137. * the name is generated from a hash of its type and name/value
  138. * pairs of its option, and it is prefixed by a counter value.
  139. * If the order of the unnamed sections changes for some reason,
  140. * updates to them will be rejected.
  141. */
  142. hash = djbhash(hash, s->type);
  143. uci_foreach_element(&s->options, e) {
  144. struct uci_option *o;
  145. hash = djbhash(hash, e->name);
  146. o = uci_to_option(e);
  147. switch(o->type) {
  148. case UCI_TYPE_STRING:
  149. hash = djbhash(hash, o->v.string);
  150. break;
  151. default:
  152. break;
  153. }
  154. }
  155. sprintf(buf, "cfg%02x%04x", ++s->package->n_section, hash % (1 << 16));
  156. s->e.name = uci_strdup(ctx, buf);
  157. }
  158. static struct uci_section *
  159. uci_alloc_section(struct uci_package *p, const char *type, const char *name)
  160. {
  161. struct uci_context *ctx = p->ctx;
  162. struct uci_section *s;
  163. if (name && !name[0])
  164. name = NULL;
  165. s = uci_alloc_element(ctx, section, name, strlen(type) + 1);
  166. uci_list_init(&s->options);
  167. s->type = uci_dataptr(s);
  168. s->package = p;
  169. strcpy(s->type, type);
  170. if (name == NULL)
  171. s->anonymous = true;
  172. p->n_section++;
  173. uci_list_add(&p->sections, &s->e.list);
  174. return s;
  175. }
  176. static void
  177. uci_free_section(struct uci_section *s)
  178. {
  179. struct uci_element *o, *tmp;
  180. uci_foreach_element_safe(&s->options, tmp, o) {
  181. uci_free_option(uci_to_option(o));
  182. }
  183. if ((s->type != uci_dataptr(s)) &&
  184. (s->type != NULL))
  185. free(s->type);
  186. uci_free_element(&s->e);
  187. }
  188. __private struct uci_package *
  189. uci_alloc_package(struct uci_context *ctx, const char *name)
  190. {
  191. struct uci_package *p;
  192. p = uci_alloc_element(ctx, package, name, 0);
  193. p->ctx = ctx;
  194. uci_list_init(&p->sections);
  195. uci_list_init(&p->delta);
  196. uci_list_init(&p->saved_delta);
  197. return p;
  198. }
  199. __private void
  200. uci_free_package(struct uci_package **package)
  201. {
  202. struct uci_element *e, *tmp;
  203. struct uci_package *p = *package;
  204. if(!p)
  205. return;
  206. free(p->path);
  207. uci_foreach_element_safe(&p->sections, tmp, e) {
  208. uci_free_section(uci_to_section(e));
  209. }
  210. uci_foreach_element_safe(&p->delta, tmp, e) {
  211. uci_free_delta(uci_to_delta(e));
  212. }
  213. uci_foreach_element_safe(&p->saved_delta, tmp, e) {
  214. uci_free_delta(uci_to_delta(e));
  215. }
  216. uci_free_element(&p->e);
  217. *package = NULL;
  218. }
  219. static void
  220. uci_free_any(struct uci_element **e)
  221. {
  222. switch((*e)->type) {
  223. case UCI_TYPE_SECTION:
  224. uci_free_section(uci_to_section(*e));
  225. break;
  226. case UCI_TYPE_OPTION:
  227. uci_free_option(uci_to_option(*e));
  228. break;
  229. default:
  230. break;
  231. }
  232. *e = NULL;
  233. }
  234. __private struct uci_element *
  235. uci_lookup_list(struct uci_list *list, const char *name)
  236. {
  237. struct uci_element *e;
  238. uci_foreach_element(list, e) {
  239. if (!strcmp(e->name, name))
  240. return e;
  241. }
  242. return NULL;
  243. }
  244. static struct uci_element *
  245. uci_lookup_ext_section(struct uci_context *ctx, struct uci_ptr *ptr)
  246. {
  247. char *idxstr, *t, *section, *name;
  248. struct uci_element *e = NULL;
  249. struct uci_section *s;
  250. int idx, c;
  251. section = uci_strdup(ctx, ptr->section);
  252. name = idxstr = section + 1;
  253. if (section[0] != '@')
  254. goto error;
  255. /* parse the section index part */
  256. idxstr = strchr(idxstr, '[');
  257. if (!idxstr)
  258. goto error;
  259. *idxstr = 0;
  260. idxstr++;
  261. t = strchr(idxstr, ']');
  262. if (!t)
  263. goto error;
  264. if (t[1] != 0)
  265. goto error;
  266. *t = 0;
  267. t = NULL;
  268. idx = strtol(idxstr, &t, 10);
  269. if (t && *t)
  270. goto error;
  271. if (!*name)
  272. name = NULL;
  273. else if (!uci_validate_type(name))
  274. goto error;
  275. /* if the given index is negative, it specifies the section number from
  276. * the end of the list */
  277. if (idx < 0) {
  278. c = 0;
  279. uci_foreach_element(&ptr->p->sections, e) {
  280. s = uci_to_section(e);
  281. if (name && (strcmp(s->type, name) != 0))
  282. continue;
  283. c++;
  284. }
  285. idx += c;
  286. }
  287. c = 0;
  288. uci_foreach_element(&ptr->p->sections, e) {
  289. s = uci_to_section(e);
  290. if (name && (strcmp(s->type, name) != 0))
  291. continue;
  292. if (idx == c)
  293. goto done;
  294. c++;
  295. }
  296. e = NULL;
  297. goto done;
  298. error:
  299. free(section);
  300. memset(ptr, 0, sizeof(struct uci_ptr));
  301. UCI_THROW(ctx, UCI_ERR_INVAL);
  302. done:
  303. free(section);
  304. if (e)
  305. ptr->section = e->name;
  306. return e;
  307. }
  308. int
  309. uci_lookup_next(struct uci_context *ctx, struct uci_element **e, struct uci_list *list, const char *name)
  310. {
  311. UCI_HANDLE_ERR(ctx);
  312. *e = uci_lookup_list(list, name);
  313. if (!*e)
  314. UCI_THROW(ctx, UCI_ERR_NOTFOUND);
  315. return 0;
  316. }
  317. int
  318. uci_lookup_ptr(struct uci_context *ctx, struct uci_ptr *ptr, char *str, bool extended)
  319. {
  320. struct uci_element *e;
  321. UCI_HANDLE_ERR(ctx);
  322. UCI_ASSERT(ctx, ptr != NULL);
  323. if (str)
  324. UCI_INTERNAL(uci_parse_ptr, ctx, ptr, str);
  325. ptr->flags |= UCI_LOOKUP_DONE;
  326. /* look up the package first */
  327. if (ptr->p)
  328. e = &ptr->p->e;
  329. else
  330. e = uci_lookup_list(&ctx->root, ptr->package);
  331. if (!e) {
  332. UCI_INTERNAL(uci_load, ctx, ptr->package, &ptr->p);
  333. if (!ptr->p)
  334. goto notfound;
  335. ptr->last = &ptr->p->e;
  336. } else {
  337. ptr->p = uci_to_package(e);
  338. ptr->last = e;
  339. }
  340. if (!ptr->section && !ptr->s)
  341. goto complete;
  342. /* if the section name validates as a regular name, pass through
  343. * to the regular uci_lookup function call */
  344. if (ptr->s) {
  345. e = &ptr->s->e;
  346. } else if (ptr->flags & UCI_LOOKUP_EXTENDED) {
  347. if (extended)
  348. e = uci_lookup_ext_section(ctx, ptr);
  349. else
  350. UCI_THROW(ctx, UCI_ERR_INVAL);
  351. } else {
  352. e = uci_lookup_list(&ptr->p->sections, ptr->section);
  353. }
  354. if (!e)
  355. goto abort;
  356. ptr->last = e;
  357. ptr->s = uci_to_section(e);
  358. if (ptr->option) {
  359. e = uci_lookup_list(&ptr->s->options, ptr->option);
  360. if (!e)
  361. goto abort;
  362. ptr->o = uci_to_option(e);
  363. ptr->last = e;
  364. }
  365. complete:
  366. ptr->flags |= UCI_LOOKUP_COMPLETE;
  367. abort:
  368. return UCI_OK;
  369. notfound:
  370. UCI_THROW(ctx, UCI_ERR_NOTFOUND);
  371. /* not a chance here */
  372. return UCI_ERR_NOTFOUND;
  373. }
  374. __private struct uci_element *
  375. uci_expand_ptr(struct uci_context *ctx, struct uci_ptr *ptr, bool complete)
  376. {
  377. UCI_ASSERT(ctx, ptr != NULL);
  378. if (!(ptr->flags & UCI_LOOKUP_DONE))
  379. UCI_INTERNAL(uci_lookup_ptr, ctx, ptr, NULL, 1);
  380. if (complete && !(ptr->flags & UCI_LOOKUP_COMPLETE))
  381. UCI_THROW(ctx, UCI_ERR_NOTFOUND);
  382. UCI_ASSERT(ctx, ptr->p != NULL);
  383. /* fill in missing string info */
  384. if (ptr->p && !ptr->package)
  385. ptr->package = ptr->p->e.name;
  386. if (ptr->s && !ptr->section)
  387. ptr->section = ptr->s->e.name;
  388. if (ptr->o && !ptr->option)
  389. ptr->option = ptr->o->e.name;
  390. if (ptr->o)
  391. return &ptr->o->e;
  392. if (ptr->s)
  393. return &ptr->s->e;
  394. if (ptr->p)
  395. return &ptr->p->e;
  396. else
  397. return NULL;
  398. }
  399. static void uci_add_element_list(struct uci_context *ctx, struct uci_ptr *ptr, bool internal)
  400. {
  401. struct uci_element *e;
  402. struct uci_package *p;
  403. p = ptr->p;
  404. if (!internal && p->has_delta)
  405. uci_add_delta(ctx, &p->delta, UCI_CMD_LIST_ADD, ptr->section, ptr->option, ptr->value);
  406. e = uci_alloc_generic(ctx, UCI_TYPE_ITEM, ptr->value, sizeof(struct uci_option));
  407. uci_list_add(&ptr->o->v.list, &e->list);
  408. }
  409. int uci_rename(struct uci_context *ctx, struct uci_ptr *ptr)
  410. {
  411. /* NB: UCI_INTERNAL use means without delta tracking */
  412. bool internal = ctx && ctx->internal;
  413. struct uci_element *e;
  414. struct uci_package *p;
  415. char *n;
  416. UCI_HANDLE_ERR(ctx);
  417. e = uci_expand_ptr(ctx, ptr, true);
  418. p = ptr->p;
  419. UCI_ASSERT(ctx, ptr->s);
  420. UCI_ASSERT(ctx, ptr->value);
  421. if (!internal && p->has_delta)
  422. uci_add_delta(ctx, &p->delta, UCI_CMD_RENAME, ptr->section, ptr->option, ptr->value);
  423. n = uci_strdup(ctx, ptr->value);
  424. free(e->name);
  425. e->name = n;
  426. if (e->type == UCI_TYPE_SECTION)
  427. uci_to_section(e)->anonymous = false;
  428. return 0;
  429. }
  430. int uci_reorder_section(struct uci_context *ctx, struct uci_section *s, int pos)
  431. {
  432. struct uci_package *p = s->package;
  433. bool internal = ctx && ctx->internal;
  434. char order[32];
  435. UCI_HANDLE_ERR(ctx);
  436. uci_list_set_pos(&s->package->sections, &s->e.list, pos);
  437. if (!internal && p->has_delta) {
  438. sprintf(order, "%d", pos);
  439. uci_add_delta(ctx, &p->delta, UCI_CMD_REORDER, s->e.name, NULL, order);
  440. }
  441. return 0;
  442. }
  443. int uci_add_section(struct uci_context *ctx, struct uci_package *p, const char *type, struct uci_section **res)
  444. {
  445. bool internal = ctx && ctx->internal;
  446. struct uci_section *s;
  447. UCI_HANDLE_ERR(ctx);
  448. UCI_ASSERT(ctx, p != NULL);
  449. s = uci_alloc_section(p, type, NULL);
  450. if (s && s->anonymous)
  451. uci_fixup_section(ctx, s);
  452. *res = s;
  453. if (!internal && p->has_delta)
  454. uci_add_delta(ctx, &p->delta, UCI_CMD_ADD, s->e.name, NULL, type);
  455. return 0;
  456. }
  457. int uci_delete(struct uci_context *ctx, struct uci_ptr *ptr)
  458. {
  459. /* NB: pass on internal flag to uci_del_element */
  460. bool internal = ctx && ctx->internal;
  461. struct uci_package *p;
  462. struct uci_element *e1, *e2, *tmp;
  463. int index;
  464. UCI_HANDLE_ERR(ctx);
  465. e1 = uci_expand_ptr(ctx, ptr, true);
  466. p = ptr->p;
  467. UCI_ASSERT(ctx, ptr->s);
  468. if (ptr->o && ptr->o->type == UCI_TYPE_LIST && ptr->value && *ptr->value) {
  469. if (!sscanf(ptr->value, "%d", &index))
  470. return 1;
  471. uci_foreach_element_safe(&ptr->o->v.list, tmp, e2) {
  472. if (index == 0) {
  473. if (!internal && p->has_delta)
  474. uci_add_delta(ctx, &p->delta, UCI_CMD_REMOVE, ptr->section, ptr->option, ptr->value);
  475. uci_free_option(uci_to_option(e2));
  476. return 0;
  477. }
  478. index--;
  479. }
  480. return 0;
  481. }
  482. if (!internal && p->has_delta)
  483. uci_add_delta(ctx, &p->delta, UCI_CMD_REMOVE, ptr->section, ptr->option, NULL);
  484. uci_free_any(&e1);
  485. if (ptr->option)
  486. ptr->o = NULL;
  487. else if (ptr->section)
  488. ptr->s = NULL;
  489. return 0;
  490. }
  491. int uci_add_list(struct uci_context *ctx, struct uci_ptr *ptr)
  492. {
  493. /* NB: UCI_INTERNAL use means without delta tracking */
  494. bool internal = ctx && ctx->internal;
  495. struct uci_option *prev = NULL;
  496. const char *value2 = NULL;
  497. UCI_HANDLE_ERR(ctx);
  498. uci_expand_ptr(ctx, ptr, false);
  499. UCI_ASSERT(ctx, ptr->s);
  500. UCI_ASSERT(ctx, ptr->value);
  501. if (ptr->o) {
  502. switch (ptr->o->type) {
  503. case UCI_TYPE_STRING:
  504. /* we already have a string value, convert that to a list */
  505. prev = ptr->o;
  506. value2 = ptr->value;
  507. ptr->value = ptr->o->v.string;
  508. break;
  509. case UCI_TYPE_LIST:
  510. uci_add_element_list(ctx, ptr, internal);
  511. return 0;
  512. default:
  513. UCI_THROW(ctx, UCI_ERR_INVAL);
  514. break;
  515. }
  516. }
  517. ptr->o = uci_alloc_list(ptr->s, ptr->option);
  518. if (prev) {
  519. uci_add_element_list(ctx, ptr, true);
  520. uci_free_option(prev);
  521. ptr->value = value2;
  522. }
  523. uci_add_element_list(ctx, ptr, internal);
  524. return 0;
  525. }
  526. int uci_del_list(struct uci_context *ctx, struct uci_ptr *ptr)
  527. {
  528. /* NB: pass on internal flag to uci_del_element */
  529. bool internal = ctx && ctx->internal;
  530. struct uci_element *e, *tmp;
  531. struct uci_package *p;
  532. UCI_HANDLE_ERR(ctx);
  533. uci_expand_ptr(ctx, ptr, false);
  534. UCI_ASSERT(ctx, ptr->s);
  535. UCI_ASSERT(ctx, ptr->value);
  536. if (!(ptr->o && ptr->option))
  537. return 0;
  538. if ((ptr->o->type != UCI_TYPE_LIST))
  539. return 0;
  540. p = ptr->p;
  541. if (!internal && p->has_delta)
  542. uci_add_delta(ctx, &p->delta, UCI_CMD_LIST_DEL, ptr->section, ptr->option, ptr->value);
  543. uci_foreach_element_safe(&ptr->o->v.list, tmp, e) {
  544. if (!strcmp(ptr->value, uci_to_option(e)->e.name)) {
  545. uci_free_option(uci_to_option(e));
  546. }
  547. }
  548. return 0;
  549. }
  550. int uci_set(struct uci_context *ctx, struct uci_ptr *ptr)
  551. {
  552. /* NB: UCI_INTERNAL use means without delta tracking */
  553. bool internal = ctx && ctx->internal;
  554. UCI_HANDLE_ERR(ctx);
  555. uci_expand_ptr(ctx, ptr, false);
  556. UCI_ASSERT(ctx, ptr->value);
  557. UCI_ASSERT(ctx, ptr->s || (!ptr->option && ptr->section));
  558. if (!ptr->option && ptr->value[0]) {
  559. UCI_ASSERT(ctx, uci_validate_type(ptr->value));
  560. }
  561. if (!ptr->o && ptr->s && ptr->option) {
  562. struct uci_element *e;
  563. e = uci_lookup_list(&ptr->s->options, ptr->option);
  564. if (e)
  565. ptr->o = uci_to_option(e);
  566. }
  567. if (!ptr->value[0]) {
  568. /* if setting a nonexistant option/section to a nonexistant value,
  569. * exit without errors */
  570. if (!(ptr->flags & UCI_LOOKUP_COMPLETE))
  571. return 0;
  572. return uci_delete(ctx, ptr);
  573. } else if (!ptr->o && ptr->option) { /* new option */
  574. ptr->o = uci_alloc_option(ptr->s, ptr->option, ptr->value);
  575. ptr->last = &ptr->o->e;
  576. } else if (!ptr->s && ptr->section) { /* new section */
  577. ptr->s = uci_alloc_section(ptr->p, ptr->value, ptr->section);
  578. ptr->last = &ptr->s->e;
  579. } else if (ptr->o && ptr->option) { /* update option */
  580. if ((ptr->o->type == UCI_TYPE_STRING) &&
  581. !strcmp(ptr->o->v.string, ptr->value))
  582. return 0;
  583. uci_free_option(ptr->o);
  584. ptr->o = uci_alloc_option(ptr->s, ptr->option, ptr->value);
  585. ptr->last = &ptr->o->e;
  586. } else if (ptr->s && ptr->section) { /* update section */
  587. char *s = uci_strdup(ctx, ptr->value);
  588. if (ptr->s->type == uci_dataptr(ptr->s)) {
  589. ptr->last = NULL;
  590. ptr->last = uci_realloc(ctx, ptr->s, sizeof(struct uci_section));
  591. ptr->s = uci_to_section(ptr->last);
  592. uci_list_fixup(&ptr->s->e.list);
  593. } else {
  594. free(ptr->s->type);
  595. }
  596. ptr->s->type = s;
  597. } else {
  598. UCI_THROW(ctx, UCI_ERR_INVAL);
  599. }
  600. if (!internal && ptr->p->has_delta)
  601. uci_add_delta(ctx, &ptr->p->delta, UCI_CMD_CHANGE, ptr->section, ptr->option, ptr->value);
  602. return 0;
  603. }
  604. int uci_unload(struct uci_context *ctx, struct uci_package *p)
  605. {
  606. UCI_HANDLE_ERR(ctx);
  607. UCI_ASSERT(ctx, p != NULL);
  608. uci_free_package(&p);
  609. return 0;
  610. }