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