more config functions
[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         jmp_buf trap_saved;
85         int saved;
86 };
87
88 struct uci_parse_context
89 {
90         int line;
91         int byte;
92
93         /* private: */
94         struct uci_config *cfg;
95         FILE *file;
96         char *buf;
97         int bufsz;
98 };
99
100 struct uci_config
101 {
102         struct uci_list list;
103         struct uci_list sections;
104         struct uci_context *ctx;
105         char *name;
106 };
107
108 struct uci_section
109 {
110         struct uci_list list;
111         struct uci_list options;
112         struct uci_config *config;
113         char *type;
114         char *name;
115 };
116
117 struct uci_option
118 {
119         struct uci_list list;
120         struct uci_section *section;
121         char *name;
122         char *value;
123 };
124
125 /* linked list handling */
126 #ifndef offsetof
127 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
128 #endif
129
130 #define uci_list_empty(list) (list->next == ptr)
131 #define uci_list_entry(_type, _ptr) \
132         ((struct uci_ ## _type *) ((char *)(_ptr) - offsetof(struct uci_ ## _type,list)))
133
134 #define uci_foreach_entry(_type, _list, _ptr)           \
135         for(_ptr = uci_list_entry(_type, (_list)->next);        \
136                 &_ptr->list != (_list);                 \
137                 _ptr = uci_list_entry(_type, _ptr->list.next))
138
139 #endif