|
1 | 1 | #include "config/init_tals.h"
|
2 | 2 |
|
3 |
| -#include <getopt.h> |
4 |
| -#include <stdlib.h> |
5 |
| -#include <string.h> |
6 |
| -#include <sys/queue.h> |
7 |
| -#include "log.h" |
8 |
| - |
9 |
| -#define JSON_MEMBER_URL "url" |
10 |
| -#define JSON_MEMBER_MESSAGE "accept-message" |
11 |
| - |
12 |
| -static int |
13 |
| -init_location_create(char const *url, struct init_location **result) |
14 |
| -{ |
15 |
| - struct init_location *tmp; |
16 |
| - |
17 |
| - tmp = malloc(sizeof(struct init_location)); |
18 |
| - if (tmp == NULL) |
19 |
| - return pr_enomem(); |
20 |
| - |
21 |
| - tmp->url = strdup(url); |
22 |
| - if (tmp->url == NULL) { |
23 |
| - free(tmp); |
24 |
| - return pr_enomem(); |
25 |
| - } |
26 |
| - |
27 |
| - tmp->accept_message = NULL; |
28 |
| - |
29 |
| - *result = tmp; |
30 |
| - return 0; |
31 |
| -} |
32 |
| - |
33 |
| -static void |
34 |
| -init_location_destroy(struct init_location *location) |
35 |
| -{ |
36 |
| - if (location->accept_message != NULL) |
37 |
| - free(location->accept_message); |
38 |
| - free(location->url); |
39 |
| - free(location); |
40 |
| -} |
41 |
| - |
42 |
| -void |
43 |
| -init_locations_cleanup(struct init_locations *locations) |
44 |
| -{ |
45 |
| - struct init_location *tmp; |
46 |
| - |
47 |
| - while (!SLIST_EMPTY(locations)) { |
48 |
| - tmp = locations->slh_first; |
49 |
| - SLIST_REMOVE_HEAD(locations, next); |
50 |
| - init_location_destroy(tmp); |
51 |
| - } |
52 |
| -} |
53 |
| - |
54 |
| -void |
55 |
| -__init_locations_cleanup(void *arg) |
56 |
| -{ |
57 |
| - init_locations_cleanup(arg); |
58 |
| -} |
59 |
| - |
60 |
| -static int |
61 |
| -init_locations_add_n_msg(struct init_locations *locations, char const *url) |
62 |
| -{ |
63 |
| - struct init_location *tmp; |
64 |
| - int error; |
65 |
| - |
66 |
| - tmp = NULL; |
67 |
| - error = init_location_create(url, &tmp); |
68 |
| - if (error) |
69 |
| - return error; |
70 |
| - |
71 |
| - SLIST_INSERT_HEAD(locations, tmp, next); |
72 |
| - return 0; |
73 |
| -} |
74 |
| - |
75 |
| -static int |
76 |
| -init_locations_add_w_msg(struct init_locations *locations, char const *url, |
77 |
| - char const *message) |
78 |
| -{ |
79 |
| - struct init_location *tmp; |
80 |
| - int error; |
81 |
| - |
82 |
| - tmp = NULL; |
83 |
| - error = init_location_create(url, &tmp); |
84 |
| - if (error) |
85 |
| - return error; |
86 |
| - |
87 |
| - tmp->accept_message = strdup(message); |
88 |
| - if (tmp->accept_message == NULL) { |
89 |
| - init_location_destroy(tmp); |
90 |
| - return pr_enomem(); |
91 |
| - } |
92 |
| - |
93 |
| - SLIST_INSERT_HEAD(locations, tmp, next); |
94 |
| - return 0; |
95 |
| -} |
96 |
| - |
97 |
| -int |
98 |
| -init_locations_init(struct init_locations *locations, |
99 |
| - char const *const *non_message, size_t non_message_len, |
100 |
| - char const *const *with_message, size_t with_message_len) |
101 |
| -{ |
102 |
| - size_t i; |
103 |
| - int error; |
104 |
| - |
105 |
| - SLIST_INIT(locations); |
106 |
| - |
107 |
| - for (i = 0; i < non_message_len; i++) { |
108 |
| - error = init_locations_add_n_msg(locations, non_message[i]); |
109 |
| - if (error) |
110 |
| - goto cleanup; |
111 |
| - } |
112 |
| - |
113 |
| - for (i = 0; i < with_message_len; i+=2) { |
114 |
| - error = init_locations_add_w_msg(locations, with_message[i], |
115 |
| - with_message[i + 1]); |
116 |
| - if (error) |
117 |
| - goto cleanup; |
118 |
| - } |
119 |
| - |
120 |
| - return 0; |
121 |
| -cleanup: |
122 |
| - init_locations_cleanup(locations); |
123 |
| - return error; |
124 |
| -} |
125 |
| - |
126 |
| -int |
127 |
| -init_locations_foreach(struct init_locations *locations, |
128 |
| - init_locations_foreach_cb cb, void *arg) |
129 |
| -{ |
130 |
| - struct init_location *ptr; |
131 |
| - int error; |
132 |
| - |
133 |
| - SLIST_FOREACH(ptr, locations, next) { |
134 |
| - error = cb(ptr->url, ptr->accept_message, arg); |
135 |
| - if (error) |
136 |
| - return error; |
137 |
| - } |
138 |
| - |
139 |
| - return 0; |
140 |
| -} |
141 |
| - |
142 |
| -static int |
143 |
| -parse_location(char const *name, size_t pos, json_t *json, char const **url, |
144 |
| - char const **message) |
145 |
| -{ |
146 |
| - json_t *member; |
147 |
| - |
148 |
| - member = json_object_get(json, JSON_MEMBER_URL); |
149 |
| - if (member == NULL) |
150 |
| - return pr_op_err("'%s' array element #%zu requires the member '%s'.", |
151 |
| - name, pos, JSON_MEMBER_URL); |
152 |
| - |
153 |
| - if (!json_is_string(member)) |
154 |
| - return pr_op_err("'%s' array element #%zu '%s' member must be a string", |
155 |
| - name, pos, JSON_MEMBER_URL); |
156 |
| - |
157 |
| - *url = json_string_value(member); |
158 |
| - |
159 |
| - /* Optional */ |
160 |
| - member = json_object_get(json, JSON_MEMBER_MESSAGE); |
161 |
| - if (member == NULL) { |
162 |
| - *message = NULL; |
163 |
| - return 0; |
164 |
| - } |
165 |
| - |
166 |
| - if (!json_is_string(member)) |
167 |
| - return pr_op_err("'%s' array element #%zu '%s' member must be a string", |
168 |
| - name, pos, JSON_MEMBER_MESSAGE); |
169 |
| - |
170 |
| - *message = json_string_value(member); |
171 |
| - |
172 |
| - return 0; |
173 |
| -} |
174 |
| - |
175 | 3 | static int
|
176 | 4 | init_tals_parse_json(struct option_field const *opt, json_t *json, void *result)
|
177 | 5 | {
|
178 |
| - struct init_locations *ptr; |
179 |
| - json_t *elem; |
180 |
| - size_t len; |
181 |
| - size_t i; |
182 |
| - char const *url; |
183 |
| - char const *message; |
184 |
| - int error; |
185 |
| - |
186 |
| - if (!json_is_array(json)) { |
187 |
| - return pr_op_err("The '%s' element is not a JSON array", |
188 |
| - opt->name); |
189 |
| - } |
190 |
| - |
191 |
| - len = json_array_size(json); |
192 |
| - |
193 |
| - if (len == 0) { |
194 |
| - /* Cleanup default value */ |
195 |
| - init_locations_cleanup(result); |
196 |
| - return 0; |
197 |
| - } |
198 |
| - |
199 |
| - ptr = result; |
200 |
| - |
201 |
| - /* Remove the previous value (usually the default). */ |
202 |
| - init_locations_cleanup(ptr); |
203 |
| - |
204 |
| - for (i = 0; i < len; i++) { |
205 |
| - elem = json_array_get(json, i); |
206 |
| - if (!json_is_object(elem)) |
207 |
| - return pr_op_err("'%s' array element #%zu is not an object", |
208 |
| - opt->name, i); |
209 |
| - |
210 |
| - url = NULL; |
211 |
| - message = NULL; |
212 |
| - error = parse_location(opt->name, i, elem, &url, &message); |
213 |
| - if (error) |
214 |
| - goto cleanup; |
215 |
| - |
216 |
| - if (message == NULL) |
217 |
| - error = init_locations_add_n_msg(ptr, url); |
218 |
| - else |
219 |
| - error = init_locations_add_w_msg(ptr, url, message); |
220 |
| - |
221 |
| - if (error) |
222 |
| - goto cleanup; |
223 |
| - } |
| 6 | + /* This is deprecated. Please delete it in the future. */ |
224 | 7 | return 0;
|
225 |
| -cleanup: |
226 |
| - init_locations_cleanup(ptr); |
227 |
| - return error; |
228 | 8 | }
|
229 | 9 |
|
230 | 10 | const struct global_type gt_init_tals_locations = {
|
231 | 11 | .print = NULL,
|
232 | 12 | .parse.json = init_tals_parse_json,
|
233 |
| - .free = __init_locations_cleanup, |
234 | 13 | };
|
0 commit comments