Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ucix.h - git.openwrt.org/project/libubox.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/ucix.h
blob: 80f0f625acad9806338a07f2adfb7c1cc41e4f5b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
 *   ucix
 *   Copyright (C) 2010 John Crispin <blogic@openwrt.org>
 *   Copyright (C) 2010 Steven Barth <steven@midlink.org>
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
 *
 */

#ifndef _UCI_H__
#define _UCI_H__
#include <uci.h>
#include "list.h"

struct ucilist {
	struct list_head list;
	char *val;
};

extern struct uci_ptr uci_ptr;

int ucix_get_ptr(struct uci_context *ctx, const char *p,
	const char *s, const char *o, const char *t);
struct uci_context* ucix_init(const char *config_file, int state);
struct uci_context* ucix_init_path(const char *vpath, const char *config_file, int state);
int ucix_save_state(struct uci_context *ctx, const char *p);
char* ucix_get_option(struct uci_context *ctx,
	const char *p, const char *s, const char *o);
int ucix_get_option_list(struct uci_context *ctx, const char *p,
	const char *s, const char *o, struct list_head *l);
void ucix_for_each_section_type(struct uci_context *ctx,
	const char *p, const char *t,
	void (*cb)(const char*, void*), void *priv);
void ucix_for_each_section_option(struct uci_context *ctx,
	const char *p, const char *s,
	void (*cb)(const char*, const char*, void*), void *priv);
void ucix_add_list(struct uci_context *ctx, const char *p,
	const char *s, const char *o, struct list_head *vals);

static inline void ucix_del(struct uci_context *ctx, const char *p, const char *s, const char *o)
{
	if (!ucix_get_ptr(ctx, p, s, o, NULL))
		uci_delete(ctx, &uci_ptr);
}

static inline void ucix_revert(struct uci_context *ctx, const char *p, const char *s, const char *o)
{
	if (!ucix_get_ptr(ctx, p, s, o, NULL))
		uci_revert(ctx, &uci_ptr);
}

static inline void ucix_add_list_single(struct uci_context *ctx, const char *p, const char *s, const char *o, const char *t)
{
	if (ucix_get_ptr(ctx, p, s, o, t))
		return;
	uci_add_list(ctx, &uci_ptr);
}

static inline void ucix_add_option(struct uci_context *ctx, const char *p, const char *s, const char *o, const char *t)
{
	if (ucix_get_ptr(ctx, p, s, o, t))
		return;
	uci_set(ctx, &uci_ptr);
}

static inline void ucix_add_section(struct uci_context *ctx, const char *p, const char *s, const char *t)
{
	if(ucix_get_ptr(ctx, p, s, NULL, t))
		return;
	uci_set(ctx, &uci_ptr);
}

static inline void ucix_add_option_int(struct uci_context *ctx, const char *p, const char *s, const char *o, int t)
{
	char tmp[64];
	snprintf(tmp, 64, "%d", t);
	ucix_add_option(ctx, p, s, o, tmp);
}

static inline void ucix_add_list_single_int(struct uci_context *ctx, const char *p, const char *s, const char *o, const int t)
{
	char tmp[64];
	snprintf(tmp, 64, "%d", t);
	ucix_add_list_single(ctx, p, s, o, tmp);
}

static inline int ucix_get_option_int(struct uci_context *ctx, const char *p, const char *s, const char *o, int def)
{
	char *tmp = ucix_get_option(ctx, p, s, o);
	int ret = def;

	if (tmp)
	{
		ret = atoi(tmp);
		free(tmp);
	}
	return ret;
}

static inline int ucix_save(struct uci_context *ctx, const char *p)
{
	if(ucix_get_ptr(ctx, p, NULL, NULL, NULL))
		return 1;
	uci_save(ctx, uci_ptr.p);
	return 0;
}

static inline int ucix_commit(struct uci_context *ctx, const char *p)
{
	if(ucix_get_ptr(ctx, p, NULL, NULL, NULL))
		return 1;
	return uci_commit(ctx, &uci_ptr.p, false);
}

static inline void ucix_cleanup(struct uci_context *ctx)
{
	uci_free_context(ctx);
}


#endif