ucimap.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930
  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. unsigned 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. unsigned 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. unsigned int i;
  230. int offset = 0;
  231. int size = sizeof(struct ucimap_list) + items * sizeof(union ucimap_data);
  232. if (!*list) {
  233. new = calloc(1, size);
  234. if (!new)
  235. return -ENOMEM;
  236. ucimap_add_alloc(sd, new);
  237. goto set;
  238. }
  239. for (i = 0, a = sd->allocmap; i < sd->allocmap_len; i++, a++) {
  240. if (a->ptr != *list)
  241. continue;
  242. goto realloc;
  243. }
  244. return -ENOENT;
  245. realloc:
  246. if (items > (*list)->size)
  247. offset = (items - (*list)->size) * sizeof(union ucimap_data);
  248. a->ptr = realloc(a->ptr, size);
  249. if (!a->ptr)
  250. return -ENOMEM;
  251. if (offset)
  252. memset((char *) a->ptr + offset, 0, size - offset);
  253. new = a->ptr;
  254. set:
  255. new->size = items;
  256. *list = new;
  257. return 0;
  258. }
  259. static void
  260. ucimap_add_fixup(struct ucimap_section_data *sd, union ucimap_data *data, struct uci_optmap *om, const char *str)
  261. {
  262. struct ucimap_fixup *f, tmp;
  263. struct uci_map *map = sd->map;
  264. tmp.next = NULL;
  265. tmp.sm = om->data.sm;
  266. tmp.name = str;
  267. tmp.type = om->type;
  268. tmp.data = data;
  269. if (ucimap_handle_fixup(map, &tmp))
  270. return;
  271. f = malloc(sizeof(struct ucimap_fixup));
  272. if (!f)
  273. return;
  274. memcpy(f, &tmp, sizeof(tmp));
  275. f->next = NULL;
  276. *map->fixup_tail = f;
  277. map->fixup_tail = &f->next;
  278. }
  279. static void
  280. ucimap_add_custom_alloc(struct ucimap_section_data *sd, struct uci_optmap *om, void *ptr)
  281. {
  282. struct ucimap_alloc_custom *a = &sd->alloc_custom[sd->alloc_custom_len++];
  283. a->section = ucimap_section_ptr(sd);
  284. a->om = om;
  285. a->ptr = ptr;
  286. }
  287. static void
  288. ucimap_add_value(union ucimap_data *data, struct uci_optmap *om, struct ucimap_section_data *sd, const char *str)
  289. {
  290. union ucimap_data tdata = *data;
  291. char *eptr = NULL;
  292. long lval;
  293. char *s;
  294. int val;
  295. if (ucimap_is_list(om->type) && !ucimap_is_fixup(om->type)) {
  296. data = ucimap_list_append(data->list);
  297. if (!data)
  298. return;
  299. }
  300. switch(om->type & UCIMAP_SUBTYPE) {
  301. case UCIMAP_STRING:
  302. if ((om->data.s.maxlen > 0) &&
  303. (strlen(str) > (unsigned) om->data.s.maxlen))
  304. return;
  305. s = strdup(str);
  306. tdata.s = s;
  307. ucimap_add_alloc(sd, s);
  308. break;
  309. case UCIMAP_BOOL:
  310. if (!strcmp(str, "on"))
  311. val = true;
  312. else if (!strcmp(str, "1"))
  313. val = true;
  314. else if (!strcmp(str, "enabled"))
  315. val = true;
  316. else if (!strcmp(str, "off"))
  317. val = false;
  318. else if (!strcmp(str, "0"))
  319. val = false;
  320. else if (!strcmp(str, "disabled"))
  321. val = false;
  322. else
  323. return;
  324. tdata.b = val;
  325. break;
  326. case UCIMAP_INT:
  327. lval = strtol(str, &eptr, om->data.i.base);
  328. if (lval < INT_MIN || lval > INT_MAX)
  329. return;
  330. if (!eptr || *eptr == '\0')
  331. tdata.i = (int) lval;
  332. else
  333. return;
  334. break;
  335. case UCIMAP_SECTION:
  336. ucimap_add_fixup(sd, data, om, str);
  337. return;
  338. case UCIMAP_CUSTOM:
  339. break;
  340. }
  341. if (om->parse) {
  342. if (om->parse(ucimap_section_ptr(sd), om, data, str) < 0)
  343. return;
  344. if (ucimap_is_custom(om->type) && om->free) {
  345. if (tdata.ptr != data->ptr)
  346. ucimap_add_custom_alloc(sd, om, data->ptr);
  347. }
  348. }
  349. if (ucimap_is_custom(om->type))
  350. return;
  351. memcpy(data, &tdata, sizeof(union ucimap_data));
  352. }
  353. static void
  354. ucimap_convert_list(union ucimap_data *data, struct uci_optmap *om, struct ucimap_section_data *sd, const char *str)
  355. {
  356. char *s, *p;
  357. s = strdup(str);
  358. if (!s)
  359. return;
  360. ucimap_add_alloc(sd, s);
  361. do {
  362. while (isspace(*s))
  363. s++;
  364. if (!*s)
  365. break;
  366. p = s;
  367. while (*s && !isspace(*s))
  368. s++;
  369. if (isspace(*s)) {
  370. *s = 0;
  371. s++;
  372. }
  373. ucimap_add_value(data, om, sd, p);
  374. } while (*s);
  375. }
  376. static int
  377. ucimap_parse_options(struct uci_map *map, struct uci_sectionmap *sm, struct ucimap_section_data *sd, struct uci_section *s)
  378. {
  379. struct uci_element *e, *l;
  380. struct uci_option *o;
  381. union ucimap_data *data;
  382. uci_foreach_element(&s->options, e) {
  383. struct uci_optmap *om = NULL, *tmp;
  384. ucimap_foreach_option(sm, tmp) {
  385. if (strcmp(e->name, tmp->name) == 0) {
  386. om = tmp;
  387. break;
  388. }
  389. }
  390. if (!om)
  391. continue;
  392. data = ucimap_get_data(sd, om);
  393. o = uci_to_option(e);
  394. if ((o->type == UCI_TYPE_STRING) && ucimap_is_simple(om->type)) {
  395. ucimap_add_value(data, om, sd, o->v.string);
  396. } else if ((o->type == UCI_TYPE_LIST) && ucimap_is_list(om->type)) {
  397. uci_foreach_element(&o->v.list, l) {
  398. ucimap_add_value(data, om, sd, l->name);
  399. }
  400. } else if ((o->type == UCI_TYPE_STRING) && ucimap_is_list_auto(om->type)) {
  401. ucimap_convert_list(data, om, sd, o->v.string);
  402. }
  403. }
  404. return 0;
  405. }
  406. static void
  407. ucimap_add_section_list(struct uci_map *map, struct ucimap_section_data *sd)
  408. {
  409. sd->ref = map->sdata_tail;
  410. *sd->ref = sd;
  411. map->sdata_tail = &sd->next;
  412. }
  413. static int
  414. ucimap_add_section(struct ucimap_section_data *sd)
  415. {
  416. int r;
  417. struct uci_map *map = sd->map;
  418. sd->next = NULL;
  419. r = sd->sm->add(map, ucimap_section_ptr(sd));
  420. if (r < 0) {
  421. ucimap_free_section(map, sd);
  422. return r;
  423. } else
  424. ucimap_add_section_list(map, sd);
  425. return 0;
  426. }
  427. #ifdef UCI_DEBUG
  428. static const char *ucimap_type_names[] = {
  429. [UCIMAP_STRING] = "string",
  430. [UCIMAP_INT] = "integer",
  431. [UCIMAP_BOOL] = "boolean",
  432. [UCIMAP_SECTION] = "section",
  433. [UCIMAP_LIST] = "list",
  434. };
  435. static const char *
  436. ucimap_get_type_name(int type)
  437. {
  438. static char buf[32];
  439. const char *name;
  440. if (ucimap_is_list(type))
  441. return ucimap_type_names[UCIMAP_LIST];
  442. name = ucimap_type_names[type & UCIMAP_SUBTYPE];
  443. if (!name) {
  444. sprintf(buf, "Unknown (%d)", type & UCIMAP_SUBTYPE);
  445. name = buf;
  446. }
  447. return name;
  448. }
  449. #endif
  450. static bool
  451. ucimap_check_optmap_type(struct uci_sectionmap *sm, struct uci_optmap *om)
  452. {
  453. int type;
  454. if (unlikely(sm->type_name != om->type_name) &&
  455. unlikely(strcmp(sm->type_name, om->type_name) != 0)) {
  456. DPRINTF("Option '%s' of section type '%s' refereces unknown "
  457. "section type '%s', should be '%s'.\n",
  458. om->name, sm->type, om->type_name, sm->type_name);
  459. return false;
  460. }
  461. if (om->detected_type < 0)
  462. return true;
  463. if (ucimap_is_custom(om->type))
  464. return true;
  465. if (ucimap_is_list(om->type) !=
  466. ucimap_is_list(om->detected_type))
  467. goto failed;
  468. if (ucimap_is_list(om->type))
  469. return true;
  470. type = om->type & UCIMAP_SUBTYPE;
  471. switch(type) {
  472. case UCIMAP_STRING:
  473. case UCIMAP_INT:
  474. case UCIMAP_BOOL:
  475. if (type != om->detected_type)
  476. goto failed;
  477. break;
  478. case UCIMAP_SECTION:
  479. goto failed;
  480. default:
  481. break;
  482. }
  483. return true;
  484. failed:
  485. DPRINTF("Invalid type in option '%s' of section type '%s', "
  486. "declared type is %s, detected type is %s\n",
  487. om->name, sm->type,
  488. ucimap_get_type_name(om->type),
  489. ucimap_get_type_name(om->detected_type));
  490. return false;
  491. }
  492. static void
  493. ucimap_count_alloc(struct uci_optmap *om, int *n_alloc, int *n_custom)
  494. {
  495. if (ucimap_is_alloc(om->type))
  496. (*n_alloc)++;
  497. else if (ucimap_is_custom(om->type) && om->free)
  498. (*n_custom)++;
  499. }
  500. int
  501. ucimap_parse_section(struct uci_map *map, struct uci_sectionmap *sm, struct ucimap_section_data *sd, struct uci_section *s)
  502. {
  503. struct uci_optmap *om;
  504. char *section_name;
  505. void *section;
  506. int n_alloc = 2;
  507. int n_alloc_custom = 0;
  508. int err;
  509. sd->map = map;
  510. sd->sm = sm;
  511. ucimap_foreach_option(sm, om) {
  512. if (!ucimap_check_optmap_type(sm, om))
  513. continue;
  514. if (ucimap_is_list(om->type)) {
  515. union ucimap_data *data;
  516. struct uci_element *e;
  517. int n_elements = 0;
  518. int n_elements_alloc = 0;
  519. int n_elements_custom = 0;
  520. int size;
  521. data = ucimap_get_data(sd, om);
  522. uci_foreach_element(&s->options, e) {
  523. struct uci_option *o = uci_to_option(e);
  524. struct uci_element *tmp;
  525. if (strcmp(e->name, om->name) != 0)
  526. continue;
  527. if (o->type == UCI_TYPE_LIST) {
  528. uci_foreach_element(&o->v.list, tmp) {
  529. ucimap_count_alloc(om, &n_elements_alloc, &n_elements_custom);
  530. n_elements++;
  531. }
  532. } else if ((o->type == UCI_TYPE_STRING) &&
  533. ucimap_is_list_auto(om->type)) {
  534. const char *data = o->v.string;
  535. do {
  536. while (isspace(*data))
  537. data++;
  538. if (!*data)
  539. break;
  540. n_elements++;
  541. ucimap_count_alloc(om, &n_elements_alloc, &n_elements_custom);
  542. while (*data && !isspace(*data))
  543. data++;
  544. } while (*data);
  545. /* for the duplicated data string */
  546. if (n_elements)
  547. n_alloc++;
  548. }
  549. break;
  550. }
  551. /* add one more for the ucimap_list */
  552. n_alloc += n_elements_alloc + 1;
  553. n_alloc_custom += n_elements_custom;
  554. size = sizeof(struct ucimap_list) +
  555. n_elements * sizeof(union ucimap_data);
  556. data->list = calloc(1, size);
  557. if (!data->list)
  558. goto error_mem;
  559. data->list->size = n_elements;
  560. } else {
  561. ucimap_count_alloc(om, &n_alloc, &n_alloc_custom);
  562. }
  563. }
  564. sd->allocmap = calloc(n_alloc, sizeof(struct ucimap_alloc));
  565. if (!sd->allocmap)
  566. goto error_mem;
  567. if (n_alloc_custom > 0) {
  568. sd->alloc_custom = calloc(n_alloc_custom, sizeof(struct ucimap_alloc_custom));
  569. if (!sd->alloc_custom)
  570. goto error_mem;
  571. }
  572. section_name = strdup(s->e.name);
  573. if (!section_name)
  574. goto error_mem;
  575. sd->section_name = section_name;
  576. sd->cmap = calloc(1, BITFIELD_SIZE(sm->n_options));
  577. if (!sd->cmap)
  578. goto error_mem;
  579. ucimap_add_alloc(sd, (void *)section_name);
  580. ucimap_add_alloc(sd, (void *)sd->cmap);
  581. ucimap_foreach_option(sm, om) {
  582. if (!ucimap_is_list(om->type))
  583. continue;
  584. ucimap_add_alloc(sd, ucimap_get_data(sd, om)->list);
  585. }
  586. section = ucimap_section_ptr(sd);
  587. err = sm->init(map, section, s);
  588. if (err)
  589. goto error;
  590. if (map->parsed) {
  591. err = ucimap_add_section(sd);
  592. if (err)
  593. return err;
  594. } else {
  595. ucimap_add_section_list(map, sd);
  596. }
  597. err = ucimap_parse_options(map, sm, sd, s);
  598. if (err)
  599. goto error;
  600. return 0;
  601. error_mem:
  602. free(sd->alloc_custom);
  603. free(sd->allocmap);
  604. free(sd);
  605. return UCI_ERR_MEM;
  606. error:
  607. ucimap_free_section(map, sd);
  608. return err;
  609. }
  610. static int
  611. ucimap_fill_ptr(struct uci_ptr *ptr, struct uci_section *s, const char *option)
  612. {
  613. struct uci_package *p = s->package;
  614. memset(ptr, 0, sizeof(struct uci_ptr));
  615. ptr->package = p->e.name;
  616. ptr->p = p;
  617. ptr->section = s->e.name;
  618. ptr->s = s;
  619. ptr->option = option;
  620. return uci_lookup_ptr(p->ctx, ptr, NULL, false);
  621. }
  622. void
  623. ucimap_set_changed(struct ucimap_section_data *sd, void *field)
  624. {
  625. void *section = ucimap_section_ptr(sd);
  626. struct uci_sectionmap *sm = sd->sm;
  627. struct uci_optmap *om;
  628. unsigned int ofs = (char *)field - (char *)section;
  629. int i = 0;
  630. ucimap_foreach_option(sm, om) {
  631. if (om->offset == ofs) {
  632. SET_BIT(sd->cmap, i);
  633. break;
  634. }
  635. i++;
  636. }
  637. }
  638. static char *
  639. ucimap_data_to_string(struct ucimap_section_data *sd, struct uci_optmap *om, union ucimap_data *data)
  640. {
  641. static char buf[32];
  642. char *str = NULL;
  643. switch(om->type & UCIMAP_SUBTYPE) {
  644. case UCIMAP_STRING:
  645. str = data->s;
  646. break;
  647. case UCIMAP_INT:
  648. sprintf(buf, "%d", data->i);
  649. str = buf;
  650. break;
  651. case UCIMAP_BOOL:
  652. sprintf(buf, "%d", !!data->b);
  653. str = buf;
  654. break;
  655. case UCIMAP_SECTION:
  656. if (data->ptr)
  657. str = (char *) ucimap_ptr_section(om->data.sm, data->ptr)->section_name;
  658. else
  659. str = "";
  660. break;
  661. case UCIMAP_CUSTOM:
  662. break;
  663. default:
  664. return NULL;
  665. }
  666. if (om->format) {
  667. if (om->format(ucimap_section_ptr(sd), om, data, &str) < 0)
  668. return NULL;
  669. if (!str)
  670. str = "";
  671. }
  672. return str;
  673. }
  674. int
  675. ucimap_store_section(struct uci_map *map, struct uci_package *p, struct ucimap_section_data *sd)
  676. {
  677. struct uci_sectionmap *sm = sd->sm;
  678. struct uci_section *s = NULL;
  679. struct uci_optmap *om;
  680. struct uci_element *e;
  681. struct uci_ptr ptr;
  682. int i = 0;
  683. int ret;
  684. uci_foreach_element(&p->sections, e) {
  685. if (!strcmp(e->name, sd->section_name)) {
  686. s = uci_to_section(e);
  687. break;
  688. }
  689. }
  690. if (!s)
  691. return UCI_ERR_NOTFOUND;
  692. ucimap_foreach_option(sm, om) {
  693. union ucimap_data *data;
  694. i++;
  695. data = ucimap_get_data(sd, om);
  696. if (!TEST_BIT(sd->cmap, i - 1))
  697. continue;
  698. ucimap_fill_ptr(&ptr, s, om->name);
  699. if (ucimap_is_list(om->type)) {
  700. struct ucimap_list *list = data->list;
  701. bool first = true;
  702. int j;
  703. for (j = 0; j < list->n_items; j++) {
  704. ptr.value = ucimap_data_to_string(sd, om, &list->item[j]);
  705. if (!ptr.value)
  706. continue;
  707. if (first) {
  708. ret = uci_set(s->package->ctx, &ptr);
  709. first = false;
  710. } else {
  711. ret = uci_add_list(s->package->ctx, &ptr);
  712. }
  713. if (ret)
  714. return ret;
  715. }
  716. } else {
  717. ptr.value = ucimap_data_to_string(sd, om, data);
  718. if (!ptr.value)
  719. continue;
  720. ret = uci_set(s->package->ctx, &ptr);
  721. if (ret)
  722. return ret;
  723. }
  724. CLR_BIT(sd->cmap, i - 1);
  725. }
  726. return 0;
  727. }
  728. void
  729. ucimap_parse(struct uci_map *map, struct uci_package *pkg)
  730. {
  731. struct uci_element *e;
  732. struct ucimap_section_data *sd, **sd_tail;
  733. struct ucimap_fixup *f;
  734. unsigned int i;
  735. sd_tail = map->sdata_tail;
  736. map->parsed = false;
  737. map->sdata_tail = &map->pending;
  738. uci_foreach_element(&pkg->sections, e) {
  739. struct uci_section *s = uci_to_section(e);
  740. for (i = 0; i < map->n_sections; i++) {
  741. struct uci_sectionmap *sm = map->sections[i];
  742. struct ucimap_section_data *sd;
  743. if (strcmp(s->type, map->sections[i]->type) != 0)
  744. continue;
  745. if (sm->alloc) {
  746. sd = sm->alloc(map, sm, s);
  747. if (!sd)
  748. continue;
  749. memset(sd, 0, sizeof(struct ucimap_section_data));
  750. } else {
  751. sd = calloc(1, sm->alloc_len);
  752. if (!sd)
  753. continue;
  754. sd = ucimap_ptr_section(sm, sd);
  755. }
  756. ucimap_parse_section(map, sm, sd, s);
  757. }
  758. }
  759. if (!map->parsed) {
  760. map->parsed = true;
  761. map->sdata_tail = sd_tail;
  762. }
  763. f = map->fixup;
  764. while (f) {
  765. struct ucimap_fixup *next = f->next;
  766. ucimap_handle_fixup(map, f);
  767. free(f);
  768. f = next;
  769. }
  770. map->fixup_tail = &map->fixup;
  771. map->fixup = NULL;
  772. sd = map->pending;
  773. while (sd) {
  774. struct ucimap_section_data *next = sd->next;
  775. ucimap_add_section(sd);
  776. sd = next;
  777. }
  778. map->pending = NULL;
  779. }