ucimap.c 19 KB

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