65fb7d34cb1fb00a8804de0a2197ef5fb4a01725
[project/uci.git] / uci.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 #define UCI_CONFDIR "/etc/config"
22
23 enum
24 {
25         UCI_OK = 0,
26         UCI_ERR_MEM,
27         UCI_ERR_INVAL,
28         UCI_ERR_NOTFOUND,
29         UCI_ERR_IO,
30         UCI_ERR_PARSE,
31         UCI_ERR_UNKNOWN,
32         UCI_ERR_LAST
33 };
34
35 struct uci_list
36 {
37         void *next;
38         void *prev;
39 };
40
41 struct uci_config;
42 struct uci_section;
43 struct uci_option;
44 struct uci_parse_context;
45
46
47 /**
48  * uci_alloc: Allocate a new uci context
49  */
50 extern struct uci_context *uci_alloc(void);
51
52 /**
53  * uci_free: Free the uci context including all of its data
54  */
55 extern void uci_free(struct uci_context *ctx);
56
57 /**
58  * uci_perror: Print the last uci error that occured
59  * @ctx: uci context
60  * @str: string to print before the error message
61  */
62 extern void uci_perror(struct uci_context *ctx, const char *str);
63
64 /**
65  * uci_load: Parse an uci config file and store it in the uci context
66  *
67  * @ctx: uci context
68  * @name: name of the config file (relative to the config directory)
69  */
70 extern int uci_load(struct uci_context *ctx, const char *name, struct uci_config **cfg);
71
72 /**
73  * uci_unload: Unload a config file from the uci context
74  *
75  * @ctx: uci context
76  * @name: name of the config file
77  */
78 extern int uci_unload(struct uci_context *ctx, const char *name);
79
80 /**
81  * uci_cleanup: Clean up after an error
82  *
83  * @ctx: uci context
84  */
85 extern int uci_cleanup(struct uci_context *ctx);
86
87 /**
88  * uci_list_configs: List available uci config files
89  *
90  * @ctx: uci context
91  */
92 extern char **uci_list_configs();
93
94 /* UCI data structures */
95
96 struct uci_context
97 {
98         struct uci_list root;
99
100         /* for error handling only */
101         struct uci_parse_context *pctx;
102
103         /* private: */
104         int errno;
105         jmp_buf trap;
106 };
107
108 struct uci_parse_context
109 {
110         int line;
111         int byte;
112
113         /* private: */
114         struct uci_config *cfg;
115         struct uci_section *section;
116         FILE *file;
117         char *buf;
118         char *reason;
119         int bufsz;
120 };
121
122 struct uci_config
123 {
124         struct uci_list list;
125         struct uci_list sections;
126         struct uci_context *ctx;
127         char *name;
128 };
129
130 struct uci_section
131 {
132         struct uci_list list;
133         struct uci_list options;
134         struct uci_config *config;
135         char *type;
136         char *name;
137 };
138
139 struct uci_option
140 {
141         struct uci_list list;
142         struct uci_section *section;
143         char *name;
144         char *value;
145 };
146
147 /* linked list handling */
148 #ifndef offsetof
149 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
150 #endif
151
152 #define uci_list_empty(list) ((list)->next == (list))
153 #define uci_list_entry(_type, _ptr) \
154         ((struct uci_ ## _type *) ((char *)(_ptr) - offsetof(struct uci_ ## _type,list)))
155
156 #define uci_foreach_entry(_type, _list, _ptr)           \
157         for(_ptr = uci_list_entry(_type, (_list)->next);        \
158                 &_ptr->list != (_list);                 \
159                 _ptr = uci_list_entry(_type, _ptr->list.next))
160
161 #endif