Initial import
[project/libubox.git] / ucix.h
1 /*
2  *   ucix
3  *   Copyright (C) 2010 John Crispin <blogic@openwrt.org>
4  *   Copyright (C) 2010 Steven Barth <steven@midlink.org>
5  *
6  *   This program is free software; you can redistribute it and/or modify
7  *   it under the terms of the GNU General Public License as published by
8  *   the Free Software Foundation; either version 2 of the License, or
9  *   (at your option) any later version.
10  *
11  *   This program is distributed in the hope that it will be useful,
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *   GNU General Public License for more details.
15  *
16  *   You should have received a copy of the GNU General Public License
17  *   along with this program; if not, write to the Free Software
18  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
19  *
20  */
21
22 #ifndef _UCI_H__
23 #define _UCI_H__
24 #include <uci.h>
25 #include "list.h"
26
27 struct ucilist {
28         struct list_head list;
29         char *val;
30 };
31
32 extern struct uci_ptr uci_ptr;
33
34 int ucix_get_ptr(struct uci_context *ctx, const char *p,
35         const char *s, const char *o, const char *t);
36 struct uci_context* ucix_init(const char *config_file, int state);
37 struct uci_context* ucix_init_path(const char *vpath, const char *config_file, int state);
38 int ucix_save_state(struct uci_context *ctx, const char *p);
39 char* ucix_get_option(struct uci_context *ctx,
40         const char *p, const char *s, const char *o);
41 int ucix_get_option_list(struct uci_context *ctx, const char *p,
42         const char *s, const char *o, struct list_head *l);
43 void ucix_for_each_section_type(struct uci_context *ctx,
44         const char *p, const char *t,
45         void (*cb)(const char*, void*), void *priv);
46 void ucix_for_each_section_option(struct uci_context *ctx,
47         const char *p, const char *s,
48         void (*cb)(const char*, const char*, void*), void *priv);
49 void ucix_add_list(struct uci_context *ctx, const char *p,
50         const char *s, const char *o, struct list_head *vals);
51
52 static inline void ucix_del(struct uci_context *ctx, const char *p, const char *s, const char *o)
53 {
54         if (!ucix_get_ptr(ctx, p, s, o, NULL))
55                 uci_delete(ctx, &uci_ptr);
56 }
57
58 static inline void ucix_revert(struct uci_context *ctx, const char *p, const char *s, const char *o)
59 {
60         if (!ucix_get_ptr(ctx, p, s, o, NULL))
61                 uci_revert(ctx, &uci_ptr);
62 }
63
64 static inline void ucix_add_list_single(struct uci_context *ctx, const char *p, const char *s, const char *o, const char *t)
65 {
66         if (ucix_get_ptr(ctx, p, s, o, t))
67                 return;
68         uci_add_list(ctx, &uci_ptr);
69 }
70
71 static inline void ucix_add_option(struct uci_context *ctx, const char *p, const char *s, const char *o, const char *t)
72 {
73         if (ucix_get_ptr(ctx, p, s, o, t))
74                 return;
75         uci_set(ctx, &uci_ptr);
76 }
77
78 static inline void ucix_add_section(struct uci_context *ctx, const char *p, const char *s, const char *t)
79 {
80         if(ucix_get_ptr(ctx, p, s, NULL, t))
81                 return;
82         uci_set(ctx, &uci_ptr);
83 }
84
85 static inline void ucix_add_option_int(struct uci_context *ctx, const char *p, const char *s, const char *o, int t)
86 {
87         char tmp[64];
88         snprintf(tmp, 64, "%d", t);
89         ucix_add_option(ctx, p, s, o, tmp);
90 }
91
92 static inline void ucix_add_list_single_int(struct uci_context *ctx, const char *p, const char *s, const char *o, const int t)
93 {
94         char tmp[64];
95         snprintf(tmp, 64, "%d", t);
96         ucix_add_list_single(ctx, p, s, o, tmp);
97 }
98
99 static inline int ucix_get_option_int(struct uci_context *ctx, const char *p, const char *s, const char *o, int def)
100 {
101         char *tmp = ucix_get_option(ctx, p, s, o);
102         int ret = def;
103
104         if (tmp)
105         {
106                 ret = atoi(tmp);
107                 free(tmp);
108         }
109         return ret;
110 }
111
112 static inline int ucix_save(struct uci_context *ctx, const char *p)
113 {
114         if(ucix_get_ptr(ctx, p, NULL, NULL, NULL))
115                 return 1;
116         uci_save(ctx, uci_ptr.p);
117         return 0;
118 }
119
120 static inline int ucix_commit(struct uci_context *ctx, const char *p)
121 {
122         if(ucix_get_ptr(ctx, p, NULL, NULL, NULL))
123                 return 1;
124         return uci_commit(ctx, &uci_ptr.p, false);
125 }
126
127 static inline void ucix_cleanup(struct uci_context *ctx)
128 {
129         uci_free_context(ctx);
130 }
131
132
133 #endif