2
0

ucimap.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922
  1. /*
  2. * ucimap.c - Library for the Unified Configuration Interface
  3. * Copyright (C) 2008-2009 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. /*
  15. * This file contains ucimap, an API for mapping UCI to C data structures
  16. */
  17. #include <strings.h>
  18. #include <stdbool.h>
  19. #include <string.h>
  20. #include <stdlib.h>
  21. #include <unistd.h>
  22. #include <limits.h>
  23. #include <stdio.h>
  24. #include <ctype.h>
  25. #include <errno.h>
  26. #include "ucimap.h"
  27. #include "uci_internal.h"
  28. struct ucimap_alloc {
  29. void *ptr;
  30. };
  31. struct ucimap_alloc_custom {
  32. void *section;
  33. struct uci_optmap *om;
  34. void *ptr;
  35. };
  36. struct ucimap_fixup {
  37. struct ucimap_fixup *next;
  38. struct uci_sectionmap *sm;
  39. const char *name;
  40. enum ucimap_type type;
  41. union ucimap_data *data;
  42. };
  43. #define ucimap_foreach_option(_sm, _o) \
  44. if (!(_sm)->options_size) \
  45. (_sm)->options_size = sizeof(struct uci_optmap); \
  46. for (_o = &(_sm)->options[0]; \
  47. ((char *)(_o)) < ((char *) &(_sm)->options[0] + \
  48. (_sm)->options_size * (_sm)->n_options); \
  49. _o = (struct uci_optmap *) ((char *)(_o) + \
  50. (_sm)->options_size))
  51. static inline bool
  52. ucimap_is_alloc(enum ucimap_type type)
  53. {
  54. return (type & UCIMAP_SUBTYPE) == UCIMAP_STRING;
  55. }
  56. static inline bool
  57. ucimap_is_fixup(enum ucimap_type type)
  58. {
  59. return (type & UCIMAP_SUBTYPE) == UCIMAP_SECTION;
  60. }
  61. static inline bool
  62. ucimap_is_simple(enum ucimap_type type)
  63. {
  64. return ((type & UCIMAP_TYPE) == UCIMAP_SIMPLE);
  65. }
  66. static inline bool
  67. ucimap_is_list(enum ucimap_type type)
  68. {
  69. return ((type & UCIMAP_TYPE) == UCIMAP_LIST);
  70. }
  71. static inline bool
  72. ucimap_is_list_auto(enum ucimap_type type)
  73. {
  74. return ucimap_is_list(type) && !!(type & UCIMAP_LIST_AUTO);
  75. }
  76. static inline bool
  77. ucimap_is_custom(enum ucimap_type type)
  78. {
  79. return ((type & UCIMAP_SUBTYPE) == UCIMAP_CUSTOM);
  80. }
  81. static inline void *
  82. ucimap_section_ptr(struct ucimap_section_data *sd)
  83. {
  84. return ((char *) sd - sd->sm->smap_offset);
  85. }
  86. static inline struct ucimap_section_data *
  87. ucimap_ptr_section(struct uci_sectionmap *sm, void *ptr) {
  88. ptr = (char *) ptr + sm->smap_offset;
  89. return ptr;
  90. }
  91. static inline union ucimap_data *
  92. ucimap_get_data(struct ucimap_section_data *sd, struct uci_optmap *om)
  93. {
  94. void *data;
  95. data = (char *) ucimap_section_ptr(sd) + om->offset;
  96. return data;
  97. }
  98. int
  99. ucimap_init(struct uci_map *map)
  100. {
  101. map->fixup = NULL;
  102. map->sdata = NULL;
  103. map->fixup_tail = &map->fixup;
  104. map->sdata_tail = &map->sdata;
  105. return 0;
  106. }
  107. static void
  108. ucimap_add_alloc(struct ucimap_section_data *sd, void *ptr)
  109. {
  110. struct ucimap_alloc *a = &sd->allocmap[sd->allocmap_len++];
  111. a->ptr = ptr;
  112. }
  113. void
  114. ucimap_free_section(struct uci_map *map, struct ucimap_section_data *sd)
  115. {
  116. void *section;
  117. int i;
  118. section = ucimap_section_ptr(sd);
  119. if (sd->ref)
  120. *sd->ref = sd->next;
  121. if (sd->sm->free)
  122. sd->sm->free(map, section);
  123. for (i = 0; i < sd->allocmap_len; i++) {
  124. free(sd->allocmap[i].ptr);
  125. }
  126. if (sd->alloc_custom) {
  127. for (i = 0; i < sd->alloc_custom_len; i++) {
  128. struct ucimap_alloc_custom *a = &sd->alloc_custom[i];
  129. a->om->free(a->section, a->om, a->ptr);
  130. }
  131. free(sd->alloc_custom);
  132. }
  133. free(sd->allocmap);
  134. free(sd);
  135. }
  136. void
  137. ucimap_cleanup(struct uci_map *map)
  138. {
  139. struct ucimap_section_data *sd, *sd_next;
  140. for (sd = map->sdata; sd; sd = sd_next) {
  141. sd_next = sd->next;
  142. ucimap_free_section(map, sd);
  143. }
  144. }
  145. static void *
  146. ucimap_find_section(struct uci_map *map, struct ucimap_fixup *f)
  147. {
  148. struct ucimap_section_data *sd;
  149. for (sd = map->sdata; sd; sd = sd->next) {
  150. if (sd->sm != f->sm)
  151. continue;
  152. if (strcmp(f->name, sd->section_name) != 0)
  153. continue;
  154. return ucimap_section_ptr(sd);
  155. }
  156. for (sd = map->pending; sd; sd = sd->next) {
  157. if (sd->sm != f->sm)
  158. continue;
  159. if (strcmp(f->name, sd->section_name) != 0)
  160. continue;
  161. return ucimap_section_ptr(sd);
  162. }
  163. return NULL;
  164. }
  165. static union ucimap_data *
  166. ucimap_list_append(struct ucimap_list *list)
  167. {
  168. if (unlikely(list->size <= list->n_items)) {
  169. /* should not happen */
  170. DPRINTF("ERROR: overflow while filling a list (size=%d)\n", list->size);
  171. return NULL;
  172. }
  173. return &list->item[list->n_items++];
  174. }
  175. static bool
  176. ucimap_handle_fixup(struct uci_map *map, struct ucimap_fixup *f)
  177. {
  178. void *ptr = ucimap_find_section(map, f);
  179. union ucimap_data *data;
  180. if (!ptr)
  181. return false;
  182. switch(f->type & UCIMAP_TYPE) {
  183. case UCIMAP_SIMPLE:
  184. f->data->ptr = ptr;
  185. break;
  186. case UCIMAP_LIST:
  187. data = ucimap_list_append(f->data->list);
  188. if (!data)
  189. return false;
  190. data->ptr = ptr;
  191. break;
  192. }
  193. return true;
  194. }
  195. void
  196. ucimap_free_item(struct ucimap_section_data *sd, void *item)
  197. {
  198. struct ucimap_alloc_custom *ac;
  199. struct ucimap_alloc *a;
  200. void *ptr = *((void **) item);
  201. int i;
  202. if (!ptr)
  203. return;
  204. *((void **)item) = NULL;
  205. for (i = 0, a = sd->allocmap; i < sd->allocmap_len; i++, a++) {
  206. if (a->ptr != ptr)
  207. continue;
  208. if (i != sd->allocmap_len - 1)
  209. a->ptr = sd->allocmap[sd->allocmap_len - 1].ptr;
  210. sd->allocmap_len--;
  211. return;
  212. }
  213. for (i = 0, ac = sd->alloc_custom; i < sd->alloc_custom_len; i++, ac++) {
  214. if (ac->ptr != ptr)
  215. continue;
  216. if (i != sd->alloc_custom_len - 1)
  217. memcpy(ac, &sd->alloc_custom[sd->alloc_custom_len - 1],
  218. sizeof(struct ucimap_alloc_custom));
  219. ac->om->free(ac->section, ac->om, ac->ptr);
  220. sd->alloc_custom_len--;
  221. return;
  222. }
  223. }
  224. int
  225. ucimap_resize_list(struct ucimap_section_data *sd, struct ucimap_list **list, int items)
  226. {
  227. struct ucimap_list *new;
  228. struct ucimap_alloc *a;
  229. int i, offset = 0;
  230. int size = sizeof(struct ucimap_list) + items * sizeof(union ucimap_data);
  231. if (!*list) {
  232. new = calloc(1, size);
  233. if (!new)
  234. return -ENOMEM;
  235. ucimap_add_alloc(sd, new);
  236. goto set;
  237. }
  238. for (i = 0, a = sd->allocmap; i < sd->allocmap_len; i++, a++) {
  239. if (a->ptr != *list)
  240. continue;
  241. goto realloc;
  242. }
  243. return -ENOENT;
  244. realloc:
  245. if (items > (*list)->size)
  246. offset = (items - (*list)->size) * sizeof(union ucimap_data);
  247. a->ptr = realloc(a->ptr, size);
  248. if (!a->ptr)
  249. return -ENOMEM;
  250. if (offset)
  251. memset((char *) a->ptr + offset, 0, size - offset);
  252. new = a->ptr;
  253. set:
  254. new->size = items;
  255. *list = new;
  256. return 0;
  257. }
  258. static void
  259. ucimap_add_fixup(struct ucimap_section_data *sd, union ucimap_data *data, struct uci_optmap *om, const char *str)
  260. {
  261. struct ucimap_fixup *f, tmp;
  262. struct uci_map *map = sd->map;
  263. tmp.next = NULL;
  264. tmp.sm = om->data.sm;
  265. tmp.name = str;
  266. tmp.type = om->type;
  267. tmp.data = data;
  268. if (ucimap_handle_fixup(map, &tmp))
  269. return;
  270. f = malloc(sizeof(struct ucimap_fixup));
  271. if (!f)
  272. return;
  273. memcpy(f, &tmp, sizeof(tmp));
  274. f->next = NULL;
  275. *map->fixup_tail = f;
  276. map->fixup_tail = &f->next;
  277. }
  278. static void
  279. ucimap_add_custom_alloc(struct ucimap_section_data *sd, struct uci_optmap *om, void *ptr)
  280. {
  281. struct ucimap_alloc_custom *a = &sd->alloc_custom[sd->alloc_custom_len++];
  282. a->section = ucimap_section_ptr(sd);
  283. a->om = om;
  284. a->ptr = ptr;
  285. }
  286. static void
  287. ucimap_add_value(union ucimap_data *data, struct uci_optmap *om, struct ucimap_section_data *sd, const char *str)
  288. {
  289. union ucimap_data tdata = *data;
  290. char *eptr = NULL;
  291. long lval;
  292. char *s;
  293. int val;
  294. if (ucimap_is_list(om->type) && !ucimap_is_fixup(om->type)) {
  295. data = ucimap_list_append(data->list);
  296. if (!data)
  297. return;
  298. }
  299. switch(om->type & UCIMAP_SUBTYPE) {
  300. case UCIMAP_STRING:
  301. if ((om->data.s.maxlen > 0) &&
  302. (strlen(str) > om->data.s.maxlen))
  303. return;
  304. s = strdup(str);
  305. tdata.s = s;
  306. ucimap_add_alloc(sd, s);
  307. break;
  308. case UCIMAP_BOOL:
  309. if (!strcmp(str, "on"))
  310. val = true;
  311. else if (!strcmp(str, "1"))
  312. val = true;
  313. else if (!strcmp(str, "enabled"))
  314. val = true;
  315. else if (!strcmp(str, "off"))
  316. val = false;
  317. else if (!strcmp(str, "0"))
  318. val = false;
  319. else if (!strcmp(str, "disabled"))
  320. val = false;
  321. else
  322. return;
  323. tdata.b = val;
  324. break;
  325. case UCIMAP_INT:
  326. lval = strtol(str, &eptr, om->data.i.base);
  327. if (lval < INT_MIN || lval > INT_MAX)
  328. return;
  329. if (!eptr || *eptr == '\0')
  330. tdata.i = (int) lval;
  331. else
  332. return;
  333. break;
  334. case UCIMAP_SECTION:
  335. ucimap_add_fixup(sd, data, om, str);
  336. return;
  337. case UCIMAP_CUSTOM:
  338. break;
  339. }
  340. if (om->parse) {
  341. if (om->parse(ucimap_section_ptr(sd), om, data, str) < 0)
  342. return;
  343. if (ucimap_is_custom(om->type) && om->free) {
  344. if (tdata.ptr != data->ptr)
  345. ucimap_add_custom_alloc(sd, om, data->ptr);
  346. }
  347. }
  348. if (ucimap_is_custom(om->type))
  349. return;
  350. memcpy(data, &tdata, sizeof(union ucimap_data));
  351. }
  352. static void
  353. ucimap_convert_list(union ucimap_data *data, struct uci_optmap *om, struct ucimap_section_data *sd, const char *str)
  354. {
  355. char *s, *p;
  356. s = strdup(str);
  357. if (!s)
  358. return;
  359. ucimap_add_alloc(sd, s);
  360. do {
  361. while (isspace(*s))
  362. s++;
  363. if (!*s)
  364. break;
  365. p = s;
  366. while (*s && !isspace(*s))
  367. s++;
  368. if (isspace(*s)) {
  369. *s = 0;
  370. s++;
  371. }
  372. ucimap_add_value(data, om, sd, p);
  373. } while (*s);
  374. }
  375. static int
  376. ucimap_parse_options(struct uci_map *map, struct uci_sectionmap *sm, struct ucimap_section_data *sd, struct uci_section *s)
  377. {
  378. struct uci_element *e, *l;
  379. struct uci_option *o;
  380. union ucimap_data *data;
  381. uci_foreach_element(&s->options, e) {
  382. struct uci_optmap *om = NULL, *tmp;
  383. ucimap_foreach_option(sm, tmp) {
  384. if (strcmp(e->name, tmp->name) == 0) {
  385. om = tmp;
  386. break;
  387. }
  388. }
  389. if (!om)
  390. continue;
  391. data = ucimap_get_data(sd, om);
  392. o = uci_to_option(e);
  393. if ((o->type == UCI_TYPE_STRING) && ucimap_is_simple(om->type)) {
  394. ucimap_add_value(data, om, sd, o->v.string);
  395. } else if ((o->type == UCI_TYPE_LIST) && ucimap_is_list(om->type)) {
  396. uci_foreach_element(&o->v.list, l) {
  397. ucimap_add_value(data, om, sd, l->name);
  398. }
  399. } else if ((o->type == UCI_TYPE_STRING) && ucimap_is_list_auto(om->type)) {
  400. ucimap_convert_list(data, om, sd, o->v.string);
  401. }
  402. }
  403. return 0;
  404. }
  405. static void
  406. ucimap_add_section_list(struct uci_map *map, struct ucimap_section_data *sd)
  407. {
  408. sd->ref = map->sdata_tail;
  409. *sd->ref = sd;
  410. map->sdata_tail = &sd->next;
  411. }
  412. static void
  413. ucimap_add_section(struct ucimap_section_data *sd)
  414. {
  415. struct uci_map *map = sd->map;
  416. sd->next = NULL;
  417. if (sd->sm->add(map, ucimap_section_ptr(sd)) < 0)
  418. ucimap_free_section(map, sd);
  419. else
  420. ucimap_add_section_list(map, sd);
  421. }
  422. #ifdef UCI_DEBUG
  423. static const char *ucimap_type_names[] = {
  424. [UCIMAP_STRING] = "string",
  425. [UCIMAP_INT] = "integer",
  426. [UCIMAP_BOOL] = "boolean",
  427. [UCIMAP_SECTION] = "section",
  428. [UCIMAP_LIST] = "list",
  429. };
  430. static const char *
  431. ucimap_get_type_name(int type)
  432. {
  433. static char buf[32];
  434. const char *name;
  435. if (ucimap_is_list(type))
  436. return ucimap_type_names[UCIMAP_LIST];
  437. name = ucimap_type_names[type & UCIMAP_SUBTYPE];
  438. if (!name) {
  439. sprintf(buf, "Unknown (%d)", type & UCIMAP_SUBTYPE);
  440. name = buf;
  441. }
  442. return name;
  443. }
  444. #endif
  445. static bool
  446. ucimap_check_optmap_type(struct uci_sectionmap *sm, struct uci_optmap *om)
  447. {
  448. unsigned int type;
  449. if (unlikely(sm->type_name != om->type_name) &&
  450. unlikely(strcmp(sm->type_name, om->type_name) != 0)) {
  451. DPRINTF("Option '%s' of section type '%s' refereces unknown "
  452. "section type '%s', should be '%s'.\n",
  453. om->name, sm->type, om->type_name, sm->type_name);
  454. return false;
  455. }
  456. if (om->detected_type < 0)
  457. return true;
  458. if (ucimap_is_custom(om->type))
  459. return true;
  460. if (ucimap_is_list(om->type) !=
  461. ucimap_is_list(om->detected_type))
  462. goto failed;
  463. if (ucimap_is_list(om->type))
  464. return true;
  465. type = om->type & UCIMAP_SUBTYPE;
  466. switch(type) {
  467. case UCIMAP_STRING:
  468. case UCIMAP_INT:
  469. case UCIMAP_BOOL:
  470. if (type != om->detected_type)
  471. goto failed;
  472. break;
  473. case UCIMAP_SECTION:
  474. goto failed;
  475. default:
  476. break;
  477. }
  478. return true;
  479. failed:
  480. DPRINTF("Invalid type in option '%s' of section type '%s', "
  481. "declared type is %s, detected type is %s\n",
  482. om->name, sm->type,
  483. ucimap_get_type_name(om->type),
  484. ucimap_get_type_name(om->detected_type));
  485. return false;
  486. }
  487. static void
  488. ucimap_count_alloc(struct uci_optmap *om, int *n_alloc, int *n_custom)
  489. {
  490. if (ucimap_is_alloc(om->type))
  491. (*n_alloc)++;
  492. else if (ucimap_is_custom(om->type) && om->free)
  493. (*n_custom)++;
  494. }
  495. int
  496. ucimap_parse_section(struct uci_map *map, struct uci_sectionmap *sm, struct ucimap_section_data *sd, struct uci_section *s)
  497. {
  498. struct uci_optmap *om;
  499. char *section_name;
  500. void *section;
  501. int n_alloc = 2;
  502. int n_alloc_custom = 0;
  503. int err;
  504. sd->map = map;
  505. sd->sm = sm;
  506. ucimap_foreach_option(sm, om) {
  507. if (!ucimap_check_optmap_type(sm, om))
  508. continue;
  509. if (ucimap_is_list(om->type)) {
  510. union ucimap_data *data;
  511. struct uci_element *e;
  512. int n_elements = 0;
  513. int n_elements_alloc = 0;
  514. int n_elements_custom = 0;
  515. int size;
  516. data = ucimap_get_data(sd, om);
  517. uci_foreach_element(&s->options, e) {
  518. struct uci_option *o = uci_to_option(e);
  519. struct uci_element *tmp;
  520. if (strcmp(e->name, om->name) != 0)
  521. continue;
  522. if (o->type == UCI_TYPE_LIST) {
  523. uci_foreach_element(&o->v.list, tmp) {
  524. ucimap_count_alloc(om, &n_elements_alloc, &n_elements_custom);
  525. n_elements++;
  526. }
  527. } else if ((o->type == UCI_TYPE_STRING) &&
  528. ucimap_is_list_auto(om->type)) {
  529. const char *data = o->v.string;
  530. do {
  531. while (isspace(*data))
  532. data++;
  533. if (!*data)
  534. break;
  535. n_elements++;
  536. ucimap_count_alloc(om, &n_elements_alloc, &n_elements_custom);
  537. while (*data && !isspace(*data))
  538. data++;
  539. } while (*data);
  540. /* for the duplicated data string */
  541. if (n_elements)
  542. n_alloc++;
  543. }
  544. break;
  545. }
  546. /* add one more for the ucimap_list */
  547. n_alloc += n_elements_alloc + 1;
  548. n_alloc_custom += n_elements_custom;
  549. size = sizeof(struct ucimap_list) +
  550. n_elements * sizeof(union ucimap_data);
  551. data->list = malloc(size);
  552. if (!data->list)
  553. goto error_mem;
  554. memset(data->list, 0, size);
  555. data->list->size = n_elements;
  556. } else {
  557. ucimap_count_alloc(om, &n_alloc, &n_alloc_custom);
  558. }
  559. }
  560. sd->allocmap = calloc(n_alloc, sizeof(struct ucimap_alloc));
  561. if (!sd->allocmap)
  562. goto error_mem;
  563. if (n_alloc_custom > 0) {
  564. sd->alloc_custom = calloc(n_alloc_custom, sizeof(struct ucimap_alloc_custom));
  565. if (!sd->alloc_custom)
  566. goto error_mem;
  567. }
  568. section_name = strdup(s->e.name);
  569. if (!section_name)
  570. goto error_mem;
  571. sd->section_name = section_name;
  572. sd->cmap = calloc(1, BITFIELD_SIZE(sm->n_options));
  573. if (!sd->cmap)
  574. goto error_mem;
  575. ucimap_add_alloc(sd, (void *)section_name);
  576. ucimap_add_alloc(sd, (void *)sd->cmap);
  577. ucimap_foreach_option(sm, om) {
  578. if (!ucimap_is_list(om->type))
  579. continue;
  580. ucimap_add_alloc(sd, ucimap_get_data(sd, om)->list);
  581. }
  582. section = ucimap_section_ptr(sd);
  583. err = sm->init(map, section, s);
  584. if (err)
  585. goto error;
  586. if (map->parsed) {
  587. ucimap_add_section(sd);
  588. } else {
  589. ucimap_add_section_list(map, sd);
  590. }
  591. err = ucimap_parse_options(map, sm, sd, s);
  592. if (err)
  593. goto error;
  594. return 0;
  595. error_mem:
  596. free(sd->alloc_custom);
  597. free(sd->allocmap);
  598. free(sd);
  599. return UCI_ERR_MEM;
  600. error:
  601. ucimap_free_section(map, sd);
  602. return err;
  603. }
  604. static int
  605. ucimap_fill_ptr(struct uci_ptr *ptr, struct uci_section *s, const char *option)
  606. {
  607. struct uci_package *p = s->package;
  608. memset(ptr, 0, sizeof(struct uci_ptr));
  609. ptr->package = p->e.name;
  610. ptr->p = p;
  611. ptr->section = s->e.name;
  612. ptr->s = s;
  613. ptr->option = option;
  614. return uci_lookup_ptr(p->ctx, ptr, NULL, false);
  615. }
  616. void
  617. ucimap_set_changed(struct ucimap_section_data *sd, void *field)
  618. {
  619. void *section = ucimap_section_ptr(sd);
  620. struct uci_sectionmap *sm = sd->sm;
  621. struct uci_optmap *om;
  622. int ofs = (char *)field - (char *)section;
  623. int i = 0;
  624. ucimap_foreach_option(sm, om) {
  625. if (om->offset == ofs) {
  626. SET_BIT(sd->cmap, i);
  627. break;
  628. }
  629. i++;
  630. }
  631. }
  632. static char *
  633. ucimap_data_to_string(struct ucimap_section_data *sd, struct uci_optmap *om, union ucimap_data *data)
  634. {
  635. static char buf[32];
  636. char *str = NULL;
  637. switch(om->type & UCIMAP_SUBTYPE) {
  638. case UCIMAP_STRING:
  639. str = data->s;
  640. break;
  641. case UCIMAP_INT:
  642. sprintf(buf, "%d", data->i);
  643. str = buf;
  644. break;
  645. case UCIMAP_BOOL:
  646. sprintf(buf, "%d", !!data->b);
  647. str = buf;
  648. break;
  649. case UCIMAP_SECTION:
  650. if (data->ptr)
  651. str = (char *) ucimap_ptr_section(om->data.sm, data->ptr)->section_name;
  652. else
  653. str = "";
  654. break;
  655. case UCIMAP_CUSTOM:
  656. break;
  657. default:
  658. return NULL;
  659. }
  660. if (om->format) {
  661. if (om->format(ucimap_section_ptr(sd), om, data, &str) < 0)
  662. return NULL;
  663. if (!str)
  664. str = "";
  665. }
  666. return str;
  667. }
  668. int
  669. ucimap_store_section(struct uci_map *map, struct uci_package *p, struct ucimap_section_data *sd)
  670. {
  671. struct uci_sectionmap *sm = sd->sm;
  672. struct uci_section *s = NULL;
  673. struct uci_optmap *om;
  674. struct uci_element *e;
  675. struct uci_ptr ptr;
  676. int i = 0;
  677. int ret;
  678. uci_foreach_element(&p->sections, e) {
  679. if (!strcmp(e->name, sd->section_name)) {
  680. s = uci_to_section(e);
  681. break;
  682. }
  683. }
  684. if (!s)
  685. return UCI_ERR_NOTFOUND;
  686. ucimap_foreach_option(sm, om) {
  687. union ucimap_data *data;
  688. i++;
  689. data = ucimap_get_data(sd, om);
  690. if (!TEST_BIT(sd->cmap, i - 1))
  691. continue;
  692. ucimap_fill_ptr(&ptr, s, om->name);
  693. if (ucimap_is_list(om->type)) {
  694. struct ucimap_list *list = data->list;
  695. bool first = true;
  696. int j;
  697. for (j = 0; j < list->n_items; j++) {
  698. ptr.value = ucimap_data_to_string(sd, om, &list->item[j]);
  699. if (!ptr.value)
  700. continue;
  701. if (first) {
  702. ret = uci_set(s->package->ctx, &ptr);
  703. first = false;
  704. } else {
  705. ret = uci_add_list(s->package->ctx, &ptr);
  706. }
  707. if (ret)
  708. return ret;
  709. }
  710. } else {
  711. ptr.value = ucimap_data_to_string(sd, om, data);
  712. if (!ptr.value)
  713. continue;
  714. ret = uci_set(s->package->ctx, &ptr);
  715. if (ret)
  716. return ret;
  717. }
  718. CLR_BIT(sd->cmap, i - 1);
  719. }
  720. return 0;
  721. }
  722. void
  723. ucimap_parse(struct uci_map *map, struct uci_package *pkg)
  724. {
  725. struct uci_element *e;
  726. struct ucimap_section_data *sd, **sd_tail;
  727. struct ucimap_fixup *f;
  728. int i;
  729. sd_tail = map->sdata_tail;
  730. map->parsed = false;
  731. map->sdata_tail = &map->pending;
  732. uci_foreach_element(&pkg->sections, e) {
  733. struct uci_section *s = uci_to_section(e);
  734. for (i = 0; i < map->n_sections; i++) {
  735. struct uci_sectionmap *sm = map->sections[i];
  736. struct ucimap_section_data *sd;
  737. if (strcmp(s->type, map->sections[i]->type) != 0)
  738. continue;
  739. if (sm->alloc) {
  740. sd = sm->alloc(map, sm, s);
  741. memset(sd, 0, sizeof(struct ucimap_section_data));
  742. } else {
  743. sd = malloc(sm->alloc_len);
  744. memset(sd, 0, sm->alloc_len);
  745. sd = ucimap_ptr_section(sm, sd);
  746. }
  747. if (!sd)
  748. continue;
  749. ucimap_parse_section(map, sm, sd, s);
  750. }
  751. }
  752. if (!map->parsed) {
  753. map->parsed = true;
  754. map->sdata_tail = sd_tail;
  755. }
  756. f = map->fixup;
  757. while (f) {
  758. struct ucimap_fixup *next = f->next;
  759. ucimap_handle_fixup(map, f);
  760. free(f);
  761. f = next;
  762. }
  763. map->fixup_tail = &map->fixup;
  764. map->fixup = NULL;
  765. sd = map->pending;
  766. while (sd) {
  767. struct ucimap_section_data *next = sd->next;
  768. ucimap_add_section(sd);
  769. sd = next;
  770. }
  771. map->pending = NULL;
  772. }