ucimap.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917
  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. 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;
  140. for (sd = map->sdata; sd; sd = sd->next) {
  141. ucimap_free_section(map, sd);
  142. }
  143. }
  144. static void *
  145. ucimap_find_section(struct uci_map *map, struct ucimap_fixup *f)
  146. {
  147. struct ucimap_section_data *sd;
  148. for (sd = map->sdata; sd; sd = sd->next) {
  149. if (sd->sm != f->sm)
  150. continue;
  151. if (strcmp(f->name, sd->section_name) != 0)
  152. continue;
  153. return ucimap_section_ptr(sd);
  154. }
  155. for (sd = map->pending; sd; sd = sd->next) {
  156. if (sd->sm != f->sm)
  157. continue;
  158. if (strcmp(f->name, sd->section_name) != 0)
  159. continue;
  160. return ucimap_section_ptr(sd);
  161. }
  162. return NULL;
  163. }
  164. static union ucimap_data *
  165. ucimap_list_append(struct ucimap_list *list)
  166. {
  167. if (unlikely(list->size <= list->n_items)) {
  168. /* should not happen */
  169. DPRINTF("ERROR: overflow while filling a list (size=%d)\n", list->size);
  170. return NULL;
  171. }
  172. return &list->item[list->n_items++];
  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. 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. list = f->data->list;
  188. data = ucimap_list_append(f->data->list);
  189. if (!data)
  190. return false;
  191. data->ptr = ptr;
  192. break;
  193. }
  194. return true;
  195. }
  196. void
  197. ucimap_free_item(struct ucimap_section_data *sd, void *item)
  198. {
  199. struct ucimap_alloc_custom *ac;
  200. struct ucimap_alloc *a;
  201. void *ptr = *((void **) item);
  202. int i;
  203. if (!ptr)
  204. return;
  205. *((void **)item) = NULL;
  206. for (i = 0, a = sd->allocmap; i < sd->allocmap_len; i++, a++) {
  207. if (a->ptr != ptr)
  208. continue;
  209. if (i != sd->allocmap_len - 1)
  210. a->ptr = sd->allocmap[sd->allocmap_len - 1].ptr;
  211. sd->allocmap_len--;
  212. return;
  213. }
  214. for (i = 0, ac = sd->alloc_custom; i < sd->alloc_custom_len; i++, ac++) {
  215. if (ac->ptr != ptr)
  216. continue;
  217. if (i != sd->alloc_custom_len - 1)
  218. memcpy(ac, &sd->alloc_custom[sd->alloc_custom_len - 1],
  219. sizeof(struct ucimap_alloc_custom));
  220. ac->om->free(ac->section, ac->om, ac->ptr);
  221. sd->alloc_custom_len--;
  222. return;
  223. }
  224. }
  225. int
  226. ucimap_resize_list(struct ucimap_section_data *sd, struct ucimap_list **list, int items)
  227. {
  228. struct ucimap_list *new;
  229. struct ucimap_alloc *a;
  230. int i, offset = 0;
  231. int size = sizeof(struct ucimap_list) + items * sizeof(union ucimap_data);
  232. if (!*list) {
  233. new = calloc(1, size);
  234. ucimap_add_alloc(sd, new);
  235. goto set;
  236. }
  237. for (i = 0, a = sd->allocmap; i < sd->allocmap_len; i++, a++) {
  238. if (a->ptr != *list)
  239. continue;
  240. goto realloc;
  241. }
  242. return -ENOENT;
  243. realloc:
  244. if (items > (*list)->size)
  245. offset = (items - (*list)->size) * sizeof(union ucimap_data);
  246. a->ptr = realloc(a->ptr, size);
  247. if (offset)
  248. memset((char *) a->ptr + offset, 0, size - offset);
  249. new = a->ptr;
  250. set:
  251. new->size = items;
  252. *list = new;
  253. return 0;
  254. }
  255. static void
  256. ucimap_add_fixup(struct ucimap_section_data *sd, union ucimap_data *data, struct uci_optmap *om, const char *str)
  257. {
  258. struct ucimap_fixup *f, tmp;
  259. struct uci_map *map = sd->map;
  260. tmp.sm = om->data.sm;
  261. tmp.name = str;
  262. tmp.type = om->type;
  263. tmp.data = data;
  264. if (ucimap_handle_fixup(map, &tmp))
  265. return;
  266. f = malloc(sizeof(struct ucimap_fixup));
  267. if (!f)
  268. return;
  269. memcpy(f, &tmp, sizeof(tmp));
  270. f->next = NULL;
  271. *map->fixup_tail = f;
  272. map->fixup_tail = &f->next;
  273. }
  274. static void
  275. ucimap_add_custom_alloc(struct ucimap_section_data *sd, struct uci_optmap *om, void *ptr)
  276. {
  277. struct ucimap_alloc_custom *a = &sd->alloc_custom[sd->alloc_custom_len++];
  278. a->section = ucimap_section_ptr(sd);
  279. a->om = om;
  280. a->ptr = ptr;
  281. }
  282. static void
  283. ucimap_add_value(union ucimap_data *data, struct uci_optmap *om, struct ucimap_section_data *sd, const char *str)
  284. {
  285. union ucimap_data tdata = *data;
  286. char *eptr = NULL;
  287. long lval;
  288. char *s;
  289. int val;
  290. if (ucimap_is_list(om->type) && !ucimap_is_fixup(om->type)) {
  291. data = ucimap_list_append(data->list);
  292. if (!data)
  293. return;
  294. }
  295. switch(om->type & UCIMAP_SUBTYPE) {
  296. case UCIMAP_STRING:
  297. if ((om->data.s.maxlen > 0) &&
  298. (strlen(str) > om->data.s.maxlen))
  299. return;
  300. s = strdup(str);
  301. tdata.s = s;
  302. ucimap_add_alloc(sd, s);
  303. break;
  304. case UCIMAP_BOOL:
  305. if (!strcmp(str, "on"))
  306. val = true;
  307. else if (!strcmp(str, "1"))
  308. val = true;
  309. else if (!strcmp(str, "enabled"))
  310. val = true;
  311. else if (!strcmp(str, "off"))
  312. val = false;
  313. else if (!strcmp(str, "0"))
  314. val = false;
  315. else if (!strcmp(str, "disabled"))
  316. val = false;
  317. else
  318. return;
  319. tdata.b = val;
  320. break;
  321. case UCIMAP_INT:
  322. lval = strtol(str, &eptr, om->data.i.base);
  323. if (lval < INT_MIN || lval > INT_MAX)
  324. return;
  325. if (!eptr || *eptr == '\0')
  326. tdata.i = (int) lval;
  327. else
  328. return;
  329. break;
  330. case UCIMAP_SECTION:
  331. ucimap_add_fixup(sd, data, om, str);
  332. return;
  333. case UCIMAP_CUSTOM:
  334. break;
  335. }
  336. if (om->parse) {
  337. if (om->parse(ucimap_section_ptr(sd), om, data, 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_alloc = 0;
  510. int n_elements_custom = 0;
  511. int size;
  512. data = ucimap_get_data(sd, om);
  513. uci_foreach_element(&s->options, e) {
  514. struct uci_option *o = uci_to_option(e);
  515. struct uci_element *tmp;
  516. if (strcmp(e->name, om->name) != 0)
  517. continue;
  518. if (o->type == UCI_TYPE_LIST) {
  519. uci_foreach_element(&o->v.list, tmp) {
  520. ucimap_count_alloc(om, &n_elements_alloc, &n_elements_custom);
  521. n_elements++;
  522. }
  523. } else if ((o->type == UCI_TYPE_STRING) &&
  524. ucimap_is_list_auto(om->type)) {
  525. const char *data = o->v.string;
  526. do {
  527. while (isspace(*data))
  528. data++;
  529. if (!*data)
  530. break;
  531. n_elements++;
  532. ucimap_count_alloc(om, &n_elements_alloc, &n_elements_custom);
  533. while (*data && !isspace(*data))
  534. data++;
  535. } while (*data);
  536. /* for the duplicated data string */
  537. if (n_elements)
  538. n_alloc++;
  539. }
  540. break;
  541. }
  542. /* add one more for the ucimap_list */
  543. n_alloc += n_elements_alloc + 1;
  544. n_alloc_custom += n_elements_custom;
  545. size = sizeof(struct ucimap_list) +
  546. n_elements * sizeof(union ucimap_data);
  547. data->list = malloc(size);
  548. if (!data->list)
  549. goto error_mem;
  550. memset(data->list, 0, size);
  551. data->list->size = n_elements;
  552. } else {
  553. ucimap_count_alloc(om, &n_alloc, &n_alloc_custom);
  554. }
  555. }
  556. sd->allocmap = calloc(n_alloc, sizeof(struct ucimap_alloc));
  557. if (!sd->allocmap)
  558. goto error_mem;
  559. if (n_alloc_custom > 0) {
  560. sd->alloc_custom = calloc(n_alloc_custom, sizeof(struct ucimap_alloc_custom));
  561. if (!sd->alloc_custom)
  562. goto error_mem;
  563. }
  564. section_name = strdup(s->e.name);
  565. if (!section_name)
  566. goto error_mem;
  567. sd->section_name = section_name;
  568. sd->cmap = calloc(1, BITFIELD_SIZE(sm->n_options));
  569. if (!sd->cmap)
  570. goto error_mem;
  571. ucimap_add_alloc(sd, (void *)section_name);
  572. ucimap_add_alloc(sd, (void *)sd->cmap);
  573. ucimap_foreach_option(sm, om) {
  574. if (!ucimap_is_list(om->type))
  575. continue;
  576. ucimap_add_alloc(sd, ucimap_get_data(sd, om)->list);
  577. }
  578. section = ucimap_section_ptr(sd);
  579. err = sm->init(map, section, s);
  580. if (err)
  581. goto error;
  582. if (map->parsed) {
  583. ucimap_add_section(sd);
  584. } else {
  585. ucimap_add_section_list(map, sd);
  586. }
  587. err = ucimap_parse_options(map, sm, sd, s);
  588. if (err)
  589. goto error;
  590. return 0;
  591. error_mem:
  592. if (sd->allocmap)
  593. free(sd->allocmap);
  594. free(sd);
  595. return UCI_ERR_MEM;
  596. error:
  597. ucimap_free_section(map, sd);
  598. return err;
  599. }
  600. static int
  601. ucimap_fill_ptr(struct uci_ptr *ptr, struct uci_section *s, const char *option)
  602. {
  603. struct uci_package *p = s->package;
  604. memset(ptr, 0, sizeof(struct uci_ptr));
  605. ptr->package = p->e.name;
  606. ptr->p = p;
  607. ptr->section = s->e.name;
  608. ptr->s = s;
  609. ptr->option = option;
  610. return uci_lookup_ptr(p->ctx, ptr, NULL, false);
  611. }
  612. void
  613. ucimap_set_changed(struct ucimap_section_data *sd, void *field)
  614. {
  615. void *section = ucimap_section_ptr(sd);
  616. struct uci_sectionmap *sm = sd->sm;
  617. struct uci_optmap *om;
  618. int ofs = (char *)field - (char *)section;
  619. int i = 0;
  620. ucimap_foreach_option(sm, om) {
  621. if (om->offset == ofs) {
  622. SET_BIT(sd->cmap, i);
  623. break;
  624. }
  625. i++;
  626. }
  627. }
  628. static char *
  629. ucimap_data_to_string(struct ucimap_section_data *sd, struct uci_optmap *om, union ucimap_data *data)
  630. {
  631. static char buf[32];
  632. char *str = NULL;
  633. switch(om->type & UCIMAP_SUBTYPE) {
  634. case UCIMAP_STRING:
  635. str = data->s;
  636. break;
  637. case UCIMAP_INT:
  638. sprintf(buf, "%d", data->i);
  639. str = buf;
  640. break;
  641. case UCIMAP_BOOL:
  642. sprintf(buf, "%d", !!data->b);
  643. str = buf;
  644. break;
  645. case UCIMAP_SECTION:
  646. if (data->ptr)
  647. str = (char *) ucimap_ptr_section(om->data.sm, data->ptr)->section_name;
  648. else
  649. str = "";
  650. break;
  651. case UCIMAP_CUSTOM:
  652. break;
  653. default:
  654. return NULL;
  655. }
  656. if (om->format) {
  657. if (om->format(ucimap_section_ptr(sd), om, data, &str) < 0)
  658. return NULL;
  659. if (!str)
  660. str = "";
  661. }
  662. return str;
  663. }
  664. int
  665. ucimap_store_section(struct uci_map *map, struct uci_package *p, struct ucimap_section_data *sd)
  666. {
  667. struct uci_sectionmap *sm = sd->sm;
  668. struct uci_section *s = NULL;
  669. struct uci_optmap *om;
  670. struct uci_element *e;
  671. struct uci_ptr ptr;
  672. int i = 0;
  673. int ret;
  674. uci_foreach_element(&p->sections, e) {
  675. if (!strcmp(e->name, sd->section_name)) {
  676. s = uci_to_section(e);
  677. break;
  678. }
  679. }
  680. if (!s)
  681. return UCI_ERR_NOTFOUND;
  682. ucimap_foreach_option(sm, om) {
  683. union ucimap_data *data;
  684. i++;
  685. data = ucimap_get_data(sd, om);
  686. if (!TEST_BIT(sd->cmap, i - 1))
  687. continue;
  688. ucimap_fill_ptr(&ptr, s, om->name);
  689. if (ucimap_is_list(om->type)) {
  690. struct ucimap_list *list = data->list;
  691. bool first = true;
  692. int j;
  693. for (j = 0; j < list->n_items; j++) {
  694. ptr.value = ucimap_data_to_string(sd, om, &list->item[j]);
  695. if (!ptr.value)
  696. continue;
  697. if (first) {
  698. ret = uci_set(s->package->ctx, &ptr);
  699. first = false;
  700. } else {
  701. ret = uci_add_list(s->package->ctx, &ptr);
  702. }
  703. if (ret)
  704. return ret;
  705. }
  706. } else {
  707. ptr.value = ucimap_data_to_string(sd, om, data);
  708. if (!ptr.value)
  709. continue;
  710. ret = uci_set(s->package->ctx, &ptr);
  711. if (ret)
  712. return ret;
  713. }
  714. CLR_BIT(sd->cmap, i - 1);
  715. }
  716. return 0;
  717. }
  718. void
  719. ucimap_parse(struct uci_map *map, struct uci_package *pkg)
  720. {
  721. struct uci_element *e;
  722. struct ucimap_section_data *sd, **sd_tail;
  723. struct ucimap_fixup *f;
  724. int i;
  725. sd_tail = map->sdata_tail;
  726. map->parsed = false;
  727. map->sdata_tail = &map->pending;
  728. uci_foreach_element(&pkg->sections, e) {
  729. struct uci_section *s = uci_to_section(e);
  730. for (i = 0; i < map->n_sections; i++) {
  731. struct uci_sectionmap *sm = map->sections[i];
  732. struct ucimap_section_data *sd;
  733. if (strcmp(s->type, map->sections[i]->type) != 0)
  734. continue;
  735. if (sm->alloc) {
  736. sd = sm->alloc(map, sm, s);
  737. memset(sd, 0, sizeof(struct ucimap_section_data));
  738. } else {
  739. sd = malloc(sm->alloc_len);
  740. memset(sd, 0, sm->alloc_len);
  741. sd = ucimap_ptr_section(sm, sd);
  742. }
  743. if (!sd)
  744. continue;
  745. ucimap_parse_section(map, sm, sd, s);
  746. }
  747. }
  748. if (!map->parsed) {
  749. map->parsed = true;
  750. map->sdata_tail = sd_tail;
  751. }
  752. f = map->fixup;
  753. while (f) {
  754. struct ucimap_fixup *next = f->next;
  755. ucimap_handle_fixup(map, f);
  756. free(f);
  757. f = next;
  758. }
  759. map->fixup_tail = &map->fixup;
  760. map->fixup = NULL;
  761. sd = map->pending;
  762. while (sd) {
  763. struct ucimap_section_data *next = sd->next;
  764. ucimap_add_section(sd);
  765. sd = next;
  766. }
  767. map->pending = NULL;
  768. }