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