uci.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * libuci - Library for the Unified Configuration Interface
  3. * Copyright (C) 2008 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. #ifndef __LIBUCI_H
  15. #define __LIBUCI_H
  16. #include <setjmp.h>
  17. #include <stdio.h>
  18. #define UCI_CONFDIR "/etc/config"
  19. enum
  20. {
  21. UCI_OK = 0,
  22. UCI_ERR_MEM,
  23. UCI_ERR_INVAL,
  24. UCI_ERR_NOTFOUND,
  25. UCI_ERR_IO,
  26. UCI_ERR_PARSE,
  27. UCI_ERR_UNKNOWN,
  28. UCI_ERR_LAST
  29. };
  30. struct uci_list
  31. {
  32. void *next;
  33. void *prev;
  34. };
  35. struct uci_config;
  36. struct uci_section;
  37. struct uci_option;
  38. struct uci_parse_context;
  39. /**
  40. * uci_alloc: Allocate a new uci context
  41. */
  42. extern struct uci_context *uci_alloc(void);
  43. /**
  44. * uci_perror: Print the last uci error that occured
  45. * @ctx: uci context
  46. * @str: string to print before the error message
  47. */
  48. extern void uci_perror(struct uci_context *ctx, const char *str);
  49. /**
  50. * uci_load: Parse an uci config file and store it in the uci context
  51. *
  52. * @ctx: uci context
  53. * @name: name of the config file (relative to the config directory)
  54. */
  55. int uci_load(struct uci_context *ctx, const char *name);
  56. /**
  57. * uci_cleanup: Clean up after an error
  58. *
  59. * @ctx: uci context
  60. */
  61. int uci_cleanup(struct uci_context *ctx);
  62. /* UCI data structures */
  63. struct uci_context
  64. {
  65. struct uci_list root;
  66. /* for error handling only */
  67. struct uci_parse_context *pctx;
  68. /* private: */
  69. int errno;
  70. jmp_buf trap;
  71. jmp_buf trap_saved;
  72. int saved;
  73. };
  74. struct uci_parse_context
  75. {
  76. int line;
  77. int byte;
  78. /* private: */
  79. struct uci_config *cfg;
  80. FILE *file;
  81. char *buf;
  82. int bufsz;
  83. };
  84. struct uci_config
  85. {
  86. struct uci_list list;
  87. struct uci_list sections;
  88. struct uci_context *ctx;
  89. char *name;
  90. };
  91. struct uci_section
  92. {
  93. struct uci_list list;
  94. struct uci_list options;
  95. struct uci_config *config;
  96. char *type;
  97. char *name;
  98. };
  99. struct uci_option
  100. {
  101. struct uci_list list;
  102. struct uci_section *section;
  103. char *name;
  104. char *value;
  105. };
  106. /* linked list handling */
  107. #ifndef offsetof
  108. #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
  109. #endif
  110. #define uci_list_empty(list) (list->next == ptr)
  111. #define uci_list_entry(_type, _ptr) \
  112. ((struct uci_ ## _type *) ((char *)(_ptr) - offsetof(struct uci_ ## _type,list)))
  113. #define uci_foreach_entry(_type, _list, _ptr) \
  114. for(_ptr = uci_list_entry(_type, (_list)->next); \
  115. &_ptr->list != (_list); \
  116. _ptr = uci_list_entry(_type, _ptr->list.next))
  117. #endif