Moving charon to libcharon.
[strongswan.git] / src / libcharon / 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
17 #include "uci_parser.h"
18
19 #include <stdarg.h>
20
21 #include <library.h>
22 #include <uci.h>
23
24 typedef struct private_uci_parser_t private_uci_parser_t;
25
26 /**
27 * Private data of an uci_parser_t object
28 */
29 struct private_uci_parser_t {
30
31 /**
32 * Public part
33 */
34 uci_parser_t public;
35
36 /**
37 * UCI package name this parser reads
38 */
39 char *package;
40 };
41
42 /**
43 * enumerator implementation create_section_enumerator
44 */
45 typedef struct {
46 /** implements enumerator */
47 enumerator_t public;
48 /** currently enumerated uci section */
49 struct uci_element *current;
50 /** all uci ipsec config sections */
51 struct uci_list *list;
52 /** uci conntext */
53 struct uci_context *ctx;
54 /** ipsec uci package */
55 struct uci_package *package;
56 /** NULL terminated list of keywords */
57 char *keywords[];
58 } section_enumerator_t;
59
60 /**
61 * Implementation of section_enumerator_t.enumerate
62 */
63 static bool section_enumerator_enumerate(section_enumerator_t *this, ...)
64 {
65 struct uci_element *element;
66 char **value;
67 va_list args;
68 int i;
69
70 if (&this->current->list == this->list)
71 {
72 return FALSE;
73 }
74
75 va_start(args, this);
76
77 value = va_arg(args, char**);
78 if (value)
79 {
80 if (uci_lookup(this->ctx, &element, this->package,
81 this->current->name, "name") == UCI_OK)
82 { /* use "name" attribute as config name if available ... */
83 *value = uci_to_option(element)->value;
84 }
85 else
86 { /* ... or the section name becomes config name */
87 *value = uci_to_section(this->current)->type;
88 }
89 }
90
91 /* followed by keyword parameters */
92 for (i = 0; this->keywords[i]; i++)
93 {
94 value = va_arg(args, char**);
95 if (value && uci_lookup(this->ctx, &element, this->package,
96 this->current->name, this->keywords[i]) == UCI_OK)
97 {
98 *value = uci_to_option(element)->value;
99 }
100 }
101 va_end(args);
102
103 this->current = list_to_element(this->current->list.next);
104 return TRUE;
105 }
106
107 /**
108 * Implementation of section_enumerator_t.public.destroy
109 */
110 static void section_enumerator_destroy(section_enumerator_t *this)
111 {
112 uci_free_context(this->ctx);
113 free(this);
114 }
115
116 /**
117 * Implementation of backend_t.create_section_enumerator.
118 */
119 static enumerator_t* create_section_enumerator(private_uci_parser_t *this, ...)
120 {
121 section_enumerator_t *e;
122 va_list args;
123 int i;
124
125 /* allocate enumerator large enought to hold keyword pointers */
126 i = 1;
127 va_start(args, this);
128 while (va_arg(args, char*))
129 {
130 i++;
131 }
132 va_end(args);
133 e = malloc(sizeof(section_enumerator_t) + sizeof(char*) * i);
134 i = 0;
135 va_start(args, this);
136 do
137 {
138 e->keywords[i] = va_arg(args, char*);
139 }
140 while (e->keywords[i++]);
141 va_end(args);
142
143 e->public.enumerate = (void*)section_enumerator_enumerate;
144 e->public.destroy = (void*)section_enumerator_destroy;
145
146 /* load uci context */
147 e->ctx = uci_alloc_context();
148 if (uci_load(e->ctx, this->package, &e->package) != UCI_OK)
149 {
150 section_enumerator_destroy(e);
151 return NULL;
152 }
153 e->list = &e->package->sections;
154 e->current = list_to_element(e->list->next);
155 if (e->current->type != UCI_TYPE_SECTION)
156 {
157 section_enumerator_destroy(e);
158 return NULL;
159 }
160 return &e->public;
161 }
162
163 /**
164 * Implementation of uci_parser_t.destroy.
165 */
166 static void destroy(private_uci_parser_t *this)
167 {
168 free(this->package);
169 free(this);
170 }
171
172 /**
173 * Described in header.
174 */
175 uci_parser_t *uci_parser_create(char *package)
176 {
177 private_uci_parser_t *this = malloc_thing(private_uci_parser_t);
178
179 this->public.create_section_enumerator = (enumerator_t*(*)(uci_parser_t*, ...))create_section_enumerator;
180 this->public.destroy = (void(*)(uci_parser_t*))destroy;
181
182 this->package = strdup(package);
183
184 return &this->public;
185 }
186