]> git.ipfire.org Git - thirdparty/strongswan.git/blame - src/libcharon/plugins/uci/uci_parser.c
Fixed some typos, courtesy of codespell
[thirdparty/strongswan.git] / src / libcharon / plugins / uci / uci_parser.c
CommitLineData
61c46386
MW
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.
61c46386
MW
15 */
16
17#include "uci_parser.h"
18
19#include <stdarg.h>
20
21#include <library.h>
22#include <uci.h>
23
24typedef struct private_uci_parser_t private_uci_parser_t;
25
26/**
27 * Private data of an uci_parser_t object
28 */
29struct private_uci_parser_t {
30
31 /**
32 * Public part
33 */
34 uci_parser_t public;
7daf5226 35
61c46386
MW
36 /**
37 * UCI package name this parser reads
38 */
39 char *package;
40};
41
42/**
43 * enumerator implementation create_section_enumerator
44 */
45typedef 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
d3e4e92e 60METHOD(enumerator_t, section_enumerator_enumerate, bool,
95a63bf2 61 section_enumerator_t *this, va_list args)
61c46386
MW
62{
63 struct uci_element *element;
64 char **value;
61c46386 65 int i;
7daf5226 66
61c46386
MW
67 if (&this->current->list == this->list)
68 {
69 return FALSE;
70 }
7daf5226 71
61c46386
MW
72 value = va_arg(args, char**);
73 if (value)
74 {
12b1c1a1
MW
75 if (uci_lookup(this->ctx, &element, this->package,
76 this->current->name, "name") == UCI_OK)
77 { /* use "name" attribute as config name if available ... */
78 *value = uci_to_option(element)->value;
79 }
80 else
81 { /* ... or the section name becomes config name */
82 *value = uci_to_section(this->current)->type;
83 }
61c46386 84 }
7daf5226 85
61c46386
MW
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,
b9b8a98f 91 this->current->name, this->keywords[i]) == UCI_OK)
61c46386
MW
92 {
93 *value = uci_to_option(element)->value;
94 }
95 }
7daf5226 96
61c46386
MW
97 this->current = list_to_element(this->current->list.next);
98 return TRUE;
99}
100
d3e4e92e
TB
101METHOD(enumerator_t, section_enumerator_destroy, void,
102 section_enumerator_t *this)
61c46386
MW
103{
104 uci_free_context(this->ctx);
105 free(this);
106}
107
d3e4e92e
TB
108METHOD(uci_parser_t, create_section_enumerator, enumerator_t*,
109 private_uci_parser_t *this, ...)
61c46386
MW
110{
111 section_enumerator_t *e;
112 va_list args;
113 int i;
7daf5226 114
2db6d5b8 115 /* allocate enumerator large enough to hold keyword pointers */
61c46386
MW
116 i = 1;
117 va_start(args, this);
118 while (va_arg(args, char*))
119 {
120 i++;
121 }
122 va_end(args);
95a63bf2
TB
123 INIT_EXTRA(e, sizeof(char*) * i,
124 .public = {
125 .enumerate = enumerator_enumerate_default,
126 .venumerate = _section_enumerator_enumerate,
127 .destroy = _section_enumerator_destroy,
128 },
129 );
61c46386
MW
130 i = 0;
131 va_start(args, this);
7daf5226 132 do
61c46386
MW
133 {
134 e->keywords[i] = va_arg(args, char*);
135 }
136 while (e->keywords[i++]);
137 va_end(args);
7daf5226 138
61c46386
MW
139 /* load uci context */
140 e->ctx = uci_alloc_context();
141 if (uci_load(e->ctx, this->package, &e->package) != UCI_OK)
142 {
143 section_enumerator_destroy(e);
144 return NULL;
145 }
146 e->list = &e->package->sections;
147 e->current = list_to_element(e->list->next);
148 if (e->current->type != UCI_TYPE_SECTION)
149 {
150 section_enumerator_destroy(e);
151 return NULL;
152 }
153 return &e->public;
154}
155
d3e4e92e
TB
156METHOD(uci_parser_t, destroy, void,
157 private_uci_parser_t *this)
61c46386
MW
158{
159 free(this->package);
160 free(this);
161}
162
163/**
164 * Described in header.
165 */
166uci_parser_t *uci_parser_create(char *package)
167{
d3e4e92e
TB
168 private_uci_parser_t *this;
169
170 INIT(this,
171 .public = {
172 .create_section_enumerator = _create_section_enumerator,
173 .destroy = _destroy,
174 },
175 .package = strdup(package),
176 );
7daf5226 177
61c46386
MW
178 return &this->public;
179}
180