first simple prototype of a UCI configuration plugin for OpenWRT
[strongswan.git] / src / charon / plugins / uci / uci_parser.c
1 /*
2 * Copyright (C) 2008 Martin Willi
3 * Copyright (C) 2008 Thomas Kallenberg
4 * Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 *
16 * $Id$
17 */
18
19 #include "uci_parser.h"
20
21 #include <stdarg.h>
22
23 #include <library.h>
24 #include <uci.h>
25
26 typedef struct private_uci_parser_t private_uci_parser_t;
27
28 /**
29 * Private data of an uci_parser_t object
30 */
31 struct private_uci_parser_t {
32
33 /**
34 * Public part
35 */
36 uci_parser_t public;
37
38 /**
39 * UCI package name this parser reads
40 */
41 char *package;
42 };
43
44 /**
45 * enumerator implementation create_section_enumerator
46 */
47 typedef struct {
48 /** implements enumerator */
49 enumerator_t public;
50 /** currently enumerated uci section */
51 struct uci_element *current;
52 /** all uci ipsec config sections */
53 struct uci_list *list;
54 /** uci conntext */
55 struct uci_context *ctx;
56 /** ipsec uci package */
57 struct uci_package *package;
58 /** NULL terminated list of keywords */
59 char *keywords[];
60 } section_enumerator_t;
61
62 /**
63 * Implementation of section_enumerator_t.enumerate
64 */
65 static bool section_enumerator_enumerate(section_enumerator_t *this, ...)
66 {
67 struct uci_element *element;
68 char **value;
69 va_list args;
70 int i;
71
72 if (&this->current->list == this->list)
73 {
74 return FALSE;
75 }
76
77 va_start(args, this);
78
79 /* name is first parameter */
80 value = va_arg(args, char**);
81 if (value)
82 {
83 *value = uci_to_section(this->current)->type;
84 }
85
86 /* followed by keyword parameters */
87 for (i = 0; this->keywords[i]; i++)
88 {
89 value = va_arg(args, char**);
90 if (value && uci_lookup(this->ctx, &element, this->package,
91 this->current->name, this->keywords[i]) == UCI_OK)
92 {
93 *value = uci_to_option(element)->value;
94 }
95 }
96 va_end(args);
97
98 this->current = list_to_element(this->current->list.next);
99 return TRUE;
100 }
101
102 /**
103 * Implementation of section_enumerator_t.public.destroy
104 */
105 static void section_enumerator_destroy(section_enumerator_t *this)
106 {
107 uci_free_context(this->ctx);
108 free(this);
109 }
110
111 /**
112 * Implementation of backend_t.create_section_enumerator.
113 */
114 static enumerator_t* create_section_enumerator(private_uci_parser_t *this, ...)
115 {
116 section_enumerator_t *e;
117 va_list args;
118 int i;
119
120 /* allocate enumerator large enought to hold keyword pointers */
121 i = 1;
122 va_start(args, this);
123 while (va_arg(args, char*))
124 {
125 i++;
126 }
127 va_end(args);
128 e = malloc(sizeof(section_enumerator_t) + sizeof(char*) * i);
129 i = 0;
130 va_start(args, this);
131 do
132 {
133 e->keywords[i] = va_arg(args, char*);
134 }
135 while (e->keywords[i++]);
136 va_end(args);
137
138 e->public.enumerate = (void*)section_enumerator_enumerate;
139 e->public.destroy = (void*)section_enumerator_destroy;
140
141 /* load uci context */
142 e->ctx = uci_alloc_context();
143 if (uci_load(e->ctx, this->package, &e->package) != UCI_OK)
144 {
145 section_enumerator_destroy(e);
146 return NULL;
147 }
148 e->list = &e->package->sections;
149 e->current = list_to_element(e->list->next);
150 if (e->current->type != UCI_TYPE_SECTION)
151 {
152 section_enumerator_destroy(e);
153 return NULL;
154 }
155 return &e->public;
156 }
157
158 /**
159 * Implementation of uci_parser_t.destroy.
160 */
161 static void destroy(private_uci_parser_t *this)
162 {
163 free(this->package);
164 free(this);
165 }
166
167 /**
168 * Described in header.
169 */
170 uci_parser_t *uci_parser_create(char *package)
171 {
172 private_uci_parser_t *this = malloc_thing(private_uci_parser_t);
173
174 this->public.create_section_enumerator = (enumerator_t*(*)(uci_parser_t*, ...))create_section_enumerator;
175 this->public.destroy = (void(*)(uci_parser_t*))destroy;
176
177 this->package = strdup(package);
178
179 return &this->public;
180 }
181