move export functionality to libuci
[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_import: Import uci config data from a stream
66 * @ctx: uci context
67 * @stream: file stream to import from
68 * @name: (optional) assume the config has the given name
69 * @cfg: (optional) store the last parsed config package in this variable
70 *
71 * the name parameter is for config files that don't explicitly use the 'package <...>' keyword
72 */
73 extern int uci_import(struct uci_context *ctx, FILE *stream, const char *name, struct uci_config **cfg);
74
75 /**
76 * uci_export: Export one or all uci config packages
77 * @ctx: uci context
78 * @stream: output stream
79 * @cfg: (optional) uci config package to export
80 */
81 extern int uci_export(struct uci_context *ctx, FILE *stream, struct uci_config *cfg);
82
83 /**
84 * uci_load: Parse an uci config file and store it in the uci context
85 *
86 * @ctx: uci context
87 * @name: name of the config file (relative to the config directory)
88 */
89 extern int uci_load(struct uci_context *ctx, const char *name, struct uci_config **cfg);
90
91 /**
92 * uci_unload: Unload a config file from the uci context
93 *
94 * @ctx: uci context
95 * @name: name of the config file
96 */
97 extern int uci_unload(struct uci_context *ctx, const char *name);
98
99 /**
100 * uci_cleanup: Clean up after an error
101 *
102 * @ctx: uci context
103 */
104 extern int uci_cleanup(struct uci_context *ctx);
105
106 /**
107 * uci_list_configs: List available uci config files
108 *
109 * @ctx: uci context
110 */
111 extern char **uci_list_configs();
112
113 /* UCI data structures */
114
115 struct uci_context
116 {
117 /* list of config packages */
118 struct uci_list root;
119
120 /* parser context, use for error handling only */
121 struct uci_parse_context *pctx;
122
123 /* private: */
124 int errno;
125 jmp_buf trap;
126 char *buf;
127 int bufsz;
128 };
129
130 struct uci_parse_context
131 {
132 /* error context */
133 const char *reason;
134 int line;
135 int byte;
136
137 /* private: */
138 struct uci_config *cfg;
139 struct uci_section *section;
140 FILE *file;
141 const char *name;
142 char *buf;
143 int bufsz;
144 };
145
146 struct uci_config
147 {
148 struct uci_list list;
149 struct uci_list sections;
150 struct uci_context *ctx;
151 char *name;
152 /* private: */
153 int n_section;
154 };
155
156 struct uci_section
157 {
158 struct uci_list list;
159 struct uci_list options;
160 struct uci_config *config;
161 char *type;
162 char *name;
163 };
164
165 struct uci_option
166 {
167 struct uci_list list;
168 struct uci_section *section;
169 char *name;
170 char *value;
171 };
172
173 /* linked list handling */
174 #ifndef offsetof
175 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
176 #endif
177
178 /* returns true if a list is empty */
179 #define uci_list_empty(list) ((list)->next == (list))
180
181 /**
182 * uci_list_entry: casts an uci_list pointer to the containing struct.
183 * @_type: config, section or option
184 * @_ptr: pointer to the uci_list struct
185 */
186 #define uci_list_entry(_type, _ptr) \
187 ((struct uci_ ## _type *) ((char *)(_ptr) - offsetof(struct uci_ ## _type,list)))
188
189 /**
190 * uci_foreach_entry: loop through a list of configs, sections or options
191 * @_type: see uci_list_entry
192 * @_list: pointer to the uci_list struct
193 * @_ptr: iteration variable
194 *
195 * use like a for loop, e.g:
196 * uci_foreach(section, &list, p) {
197 * ...
198 * }
199 */
200 #define uci_foreach_entry(_type, _list, _ptr) \
201 for(_ptr = uci_list_entry(_type, (_list)->next); \
202 &_ptr->list != (_list); \
203 _ptr = uci_list_entry(_type, _ptr->list.next))
204
205 #endif