uci.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License as published by
  4. * the Free Software Foundation; either version 2 of the License, or
  5. * (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software
  14. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
  15. *
  16. * Provided by fon.com
  17. * Copyright (C) 2008 John Crispin <blogic@openwrt.org>
  18. */
  19. #include <string.h>
  20. #include <stdlib.h>
  21. #include "include/log.h"
  22. #include <uci.h>
  23. struct uci_package *p = NULL;
  24. struct uci_context* uci_init(char *config_file)
  25. {
  26. struct uci_context *ctx = uci_alloc_context();
  27. uci_add_delta_path(ctx, "/var/state");
  28. if(uci_load(ctx, config_file, &p) != UCI_OK)
  29. {
  30. log_printf("/etc/config/%s is missing or corrupt\n", config_file);
  31. exit(-1);
  32. }
  33. return ctx;
  34. }
  35. void uci_cleanup(struct uci_context *ctx)
  36. {
  37. uci_unload(ctx, p);
  38. uci_free_context(ctx);
  39. }
  40. void uci_save_state(struct uci_context *ctx)
  41. {
  42. uci_save(ctx, p);
  43. }
  44. char* uci_get_option(struct uci_context *ctx, char *section, char *option)
  45. {
  46. struct uci_element *e = NULL;
  47. char *value = NULL;
  48. struct uci_ptr ptr;
  49. memset(&ptr, 0, sizeof(ptr));
  50. ptr.package = p->e.name;
  51. ptr.section = section;
  52. ptr.option = option;
  53. if (uci_lookup_ptr(ctx, &ptr, NULL, true) != UCI_OK)
  54. return NULL;
  55. if (!(ptr.flags & UCI_LOOKUP_COMPLETE))
  56. return NULL;
  57. e = ptr.last;
  58. switch (e->type)
  59. {
  60. case UCI_TYPE_SECTION:
  61. value = uci_to_section(e)->type;
  62. break;
  63. case UCI_TYPE_OPTION:
  64. switch(ptr.o->type) {
  65. case UCI_TYPE_STRING:
  66. value = ptr.o->v.string;
  67. break;
  68. default:
  69. value = NULL;
  70. break;
  71. }
  72. break;
  73. default:
  74. return 0;
  75. }
  76. return value;
  77. }
  78. int uci_get_option_int(struct uci_context *ctx, char *section, char *option, int def)
  79. {
  80. char *tmp = uci_get_option(ctx, section, option);
  81. int ret = def;
  82. if (tmp)
  83. ret = atoi(tmp);
  84. return ret;
  85. }
  86. void uci_for_each_section_type(char *type, void (*cb)(char*, void*), void *priv)
  87. {
  88. struct uci_element *e;
  89. uci_foreach_element(&p->sections, e)
  90. if (!strcmp(type, uci_to_section(e)->type))
  91. cb(e->name, priv);
  92. }