aba435936bc11476ecaa4d84e0be19ca8889d14b
[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 struct uci_config *cfg;
93 FILE *file;
94 char *buf;
95 int bufsz;
96 };
97
98 struct uci_config
99 {
100 struct uci_list list;
101 struct uci_list sections;
102 struct uci_context *ctx;
103 char *name;
104 };
105
106 struct uci_section
107 {
108 struct uci_list list;
109 struct uci_list options;
110 struct uci_config *config;
111 char *type;
112 char *name;
113 };
114
115 struct uci_option
116 {
117 struct uci_list list;
118 struct uci_section *section;
119 char *name;
120 char *value;
121 };
122
123 /* linked list handling */
124 #ifndef offsetof
125 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
126 #endif
127
128 #define uci_list_entry(type, ptr) \
129 ((struct uci_#type *) ((char *)(ptr) - offsetof(struct uci_#type,list)))
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