more cleanup/list fixes
[project/uci.git] / libuci.h
index 9411b54..de96f45 100644 (file)
--- a/libuci.h
+++ b/libuci.h
@@ -29,31 +29,16 @@ enum
        UCI_ERR_LAST
 };
 
-struct uci_config;
-struct uci_parse_context;
-
-struct uci_context
+struct uci_list
 {
-       struct uci_config *root;
-       
-       /* for error handling only */
-       struct uci_parse_context *pctx;
-
-       /* private: */
-       int errno;
-       jmp_buf trap;
+       void *next;
+       void *prev;
 };
 
-struct uci_parse_context
-{
-       int line;
-       int byte;
-
-       /* private: */
-       FILE *file;
-       char *buf;
-       int bufsz;
-};
+struct uci_config;
+struct uci_section;
+struct uci_option;
+struct uci_parse_context;
 
 
 /**
@@ -65,7 +50,7 @@ extern struct uci_context *uci_alloc(void);
  * uci_perror: Print the last uci error that occured
  * @ctx: uci context
  * @str: string to print before the error message
- */ 
+ */
 extern void uci_perror(struct uci_context *ctx, const char *str);
 
 /**
@@ -83,4 +68,72 @@ int uci_parse(struct uci_context *ctx, const char *name);
  */
 int uci_cleanup(struct uci_context *ctx);
 
+
+/* UCI data structures */
+
+struct uci_context
+{
+       struct uci_list root;
+
+       /* for error handling only */
+       struct uci_parse_context *pctx;
+
+       /* private: */
+       int errno;
+       jmp_buf trap;
+       jmp_buf trap_saved;
+       int saved;
+};
+
+struct uci_parse_context
+{
+       int line;
+       int byte;
+
+       /* private: */
+       struct uci_config *cfg;
+       FILE *file;
+       char *buf;
+       int bufsz;
+};
+
+struct uci_config
+{
+       struct uci_list list;
+       struct uci_list sections;
+       struct uci_context *ctx;
+       char *name;
+};
+
+struct uci_section
+{
+       struct uci_list list;
+       struct uci_list options;
+       struct uci_config *config;
+       char *type;
+       char *name;
+};
+
+struct uci_option
+{
+       struct uci_list list;
+       struct uci_section *section;
+       char *name;
+       char *value;
+};
+
+/* linked list handling */
+#ifndef offsetof
+#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
+#endif
+
+#define uci_list_empty(list) (list->next == ptr)
+#define uci_list_entry(_type, _ptr) \
+       ((struct uci_ ## _type *) ((char *)(_ptr) - offsetof(struct uci_ ## _type,list)))
+
+#define uci_foreach_entry(_type, _list, _ptr)          \
+       for(_ptr = uci_list_entry(_type, (_list)->next);        \
+               &_ptr->list != (_list);                 \
+               _ptr = uci_list_entry(_type, _ptr->list.next))
+
 #endif