afb-config: Rewrite the config as JSON object
[src/app-framework-binder.git] / src / afb-hook-flags.c
1 /*
2  * Copyright (C) 2018 "IoT.bzh"
3  * Author José Bollo <jose.bollo@iot.bzh>
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *   http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #define _GNU_SOURCE
19
20 #include <stdlib.h>
21 #include <string.h>
22 #include <ctype.h>
23
24 #include "afb-hook.h"
25 #include "afb-hook-flags.h"
26
27 /* structure for searching flags by names */
28 struct flag
29 {
30         const char *name;       /** the name */
31         int value;              /** the value */
32 };
33
34 struct flags
35 {
36         struct flag *flags;
37         int count;
38 };
39
40 #define FLAGS(x)   ((struct flags){ .flags = x, .count = (int)(sizeof x / sizeof * x) })
41
42 static struct flag xreq_flags[] = { /* must be sorted by names */
43                 { "addref",             afb_hook_flag_req_addref },
44                 { "all",                afb_hook_flags_req_all },
45                 { "args",               afb_hook_flags_req_args },
46                 { "begin",              afb_hook_flag_req_begin },
47                 { "common",             afb_hook_flags_req_common },
48                 { "context",            afb_hook_flags_req_context },
49                 { "context_get",        afb_hook_flag_req_legacy_context_get },
50                 { "context_make",       afb_hook_flag_req_context_make },
51                 { "context_set",        afb_hook_flag_req_legacy_context_set },
52                 { "end",                afb_hook_flag_req_end },
53                 { "event",              afb_hook_flags_req_event },
54                 { "extra",              afb_hook_flags_req_extra },
55                 { "get",                afb_hook_flag_req_get },
56                 { "get_application_id", afb_hook_flag_req_get_application_id },
57                 { "get_client_info",    afb_hook_flag_req_get_client_info },
58                 { "get_uid",            afb_hook_flag_req_get_uid },
59                 { "has_permission",     afb_hook_flag_req_has_permission },
60                 { "json",               afb_hook_flag_req_json },
61                 { "life",               afb_hook_flags_req_life },
62                 { "ref",                afb_hook_flags_req_ref },
63                 { "reply",              afb_hook_flag_req_reply },
64                 { "security",           afb_hook_flags_req_security },
65                 { "session",            afb_hook_flags_req_session },
66                 { "session_close",      afb_hook_flag_req_session_close },
67                 { "session_set_LOA",    afb_hook_flag_req_session_set_LOA },
68                 { "store",              afb_hook_flag_req_legacy_store },
69                 { "stores",             afb_hook_flags_req_stores },
70                 { "subcall",            afb_hook_flag_req_subcall },
71                 { "subcall_result",     afb_hook_flag_req_subcall_result },
72                 { "subcalls",           afb_hook_flags_req_subcalls },
73                 { "subcallsync",        afb_hook_flag_req_subcallsync },
74                 { "subcallsync_result", afb_hook_flag_req_subcallsync_result },
75                 { "subscribe",          afb_hook_flag_req_subscribe },
76                 { "unref",              afb_hook_flag_req_unref },
77                 { "unstore",            afb_hook_flag_req_legacy_unstore },
78                 { "unsubscribe",        afb_hook_flag_req_unsubscribe },
79                 { "vverbose",           afb_hook_flag_req_vverbose },
80 };
81
82 static struct flag api_flags[] = { /* must be sorted by names */
83                 { "add_alias",          afb_hook_flag_api_add_alias },
84                 { "all",                afb_hook_flags_api_all },
85                 { "api_add_verb",       afb_hook_flag_api_api_add_verb },
86                 { "api",                afb_hook_flags_api_api },
87                 { "api_del_verb",       afb_hook_flag_api_api_del_verb },
88                 { "api_seal",           afb_hook_flag_api_api_seal },
89                 { "api_set_on_event",   afb_hook_flag_api_api_set_on_event },
90                 { "api_set_on_init",    afb_hook_flag_api_api_set_on_init },
91                 { "api_set_verbs",      afb_hook_flag_api_api_set_verbs },
92                 { "call",               afb_hook_flag_api_call },
93                 { "callsync",           afb_hook_flag_api_callsync },
94                 { "class_provide",      afb_hook_flag_api_class_provide },
95                 { "class_require",      afb_hook_flag_api_class_require },
96                 { "common",             afb_hook_flags_api_common },
97                 { "delete_api",         afb_hook_flag_api_delete_api },
98                 { "event",              afb_hook_flags_api_event },
99                 { "event_broadcast",    afb_hook_flag_api_event_broadcast },
100                 { "event_handler_add",  afb_hook_flag_api_event_handler_add },
101                 { "event_handler_del",  afb_hook_flag_api_event_handler_del },
102                 { "event_make",         afb_hook_flag_api_event_make },
103                 { "extra",              afb_hook_flags_api_extra },
104                 { "get_event_loop",     afb_hook_flag_api_get_event_loop },
105                 { "get_system_bus",     afb_hook_flag_api_get_system_bus },
106                 { "get_user_bus",       afb_hook_flag_api_get_user_bus },
107                 { "legacy_unstore_req", afb_hook_flag_api_legacy_unstore_req },
108                 { "new_api",            afb_hook_flag_api_new_api },
109                 { "on_event",           afb_hook_flag_api_on_event },
110                 { "on_event_handler",   afb_hook_flag_api_on_event_handler },
111                 { "queue_job",          afb_hook_flag_api_queue_job },
112                 { "require_api",        afb_hook_flag_api_require_api },
113                 { "rootdir_get_fd",     afb_hook_flag_api_rootdir_get_fd },
114                 { "rootdir_open_locale",afb_hook_flag_api_rootdir_open_locale },
115                 { "start",              afb_hook_flag_api_start },
116                 { "vverbose",           afb_hook_flag_api_vverbose },
117 };
118
119 static struct flag evt_flags[] = { /* must be sorted by names */
120                 { "addref",             afb_hook_flag_evt_addref },
121                 { "all",                afb_hook_flags_evt_all },
122                 { "broadcast_after",    afb_hook_flag_evt_broadcast_after },
123                 { "broadcast_before",   afb_hook_flag_evt_broadcast_before },
124                 { "common",             afb_hook_flags_evt_common },
125                 { "create",             afb_hook_flag_evt_create },
126                 { "extra",              afb_hook_flags_evt_extra },
127                 { "name",               afb_hook_flag_evt_name },
128                 { "push_after",         afb_hook_flag_evt_push_after },
129                 { "push_before",        afb_hook_flag_evt_push_before },
130                 { "unref",              afb_hook_flag_evt_unref },
131 };
132
133 static struct flag session_flags[] = { /* must be sorted by names */
134                 { "addref",             afb_hook_flag_session_addref },
135                 { "all",                afb_hook_flags_session_all },
136                 { "close",              afb_hook_flag_session_close },
137                 { "common",             afb_hook_flags_session_common },
138                 { "create",             afb_hook_flag_session_create },
139                 { "destroy",            afb_hook_flag_session_destroy },
140                 { "renew",              afb_hook_flag_session_renew },
141                 { "unref",              afb_hook_flag_session_unref },
142 };
143
144 static struct flag global_flags[] = { /* must be sorted by names */
145                 { "all",                afb_hook_flags_global_all },
146                 { "vverbose",           afb_hook_flag_global_vverbose },
147 };
148
149 static int compare(const char *query, const char *value, size_t query_length)
150 {
151         size_t i;
152         char q, v;
153
154         for (i = 0 ; i < query_length ; i++) {
155                 v = value[i];
156                 q = query[i];
157                 if (!v)
158                         return -1;
159                 v = v == '_' ? '-' : (char)toupper(v);
160                 q = q == '_' ? '-' : (char)toupper(q);
161                 if (q != v)
162                         return (int)((unsigned)q - (unsigned)v);
163         }
164         return !!value[i];
165 }
166
167 /* get the value of the flag of 'name' in the array 'flags' of 'count elements */
168 static int get_flag(const char *name, struct flags flags, size_t length)
169 {
170         /* replace "*" by "all" */
171         if (length == 1 && *name == '*') {
172                 name = "all";
173                 length = 3;
174         }
175
176         /* dichotomic search */
177         int lower = 0, upper = flags.count;
178         while (lower < upper) {
179                 int mid = (lower + upper) >> 1;
180                 int cmp = compare(name, flags.flags[mid].name, length);
181                 if (!cmp)
182                         return flags.flags[mid].value;
183                 if (cmp < 0)
184                         upper = mid;
185                 else
186                         lower = mid + 1;
187         }
188
189         return -(compare(name, "no", length) && compare(name, "none", length));
190 }
191
192 static int from_text(const char *text, struct flags flags)
193 {
194         static const char sep[] = " \t,";
195         size_t s;
196         int result = 0, val;
197
198         if (text) {
199                 for (;;) {
200                         text += strspn(text, sep);
201                         if (!*text)
202                                 break;
203                         s = strcspn(text, sep);
204                         val = get_flag(text, flags, s);
205                         if (val == -1)
206                                 return val;
207                         result |= val;
208                         text += s;
209                 }
210         }
211         return result;
212 }
213
214 static char *to_text(int value, struct flags flags)
215 {
216         int borrow = 0, mask = 0, i, v, imask;
217         size_t s = 0;
218         char *result;
219
220         if (!value)
221                 return strdup("none");
222
223         do {
224                 if (s) {
225                         result = malloc(s + 1);
226                         if (!result)
227                                 break;
228                 }
229                 borrow = 0;
230                 while (borrow != value) {
231                         mask = 0;
232                         i = flags.count;
233                         while (i) {
234                                 v = flags.flags[--i].value;
235                                 if ((mask & v) == mask && (borrow & v) == 0 && (value & v) == v) {
236                                         mask = v;
237                                         imask = i;
238                                 }
239                         }
240                         if (mask == 0)
241                                 borrow = value;
242                         else {
243                                 if (!result)
244                                         s += strlen(flags.flags[imask].name) + !!s;
245                                 else {
246                                         if (s)
247                                                 result[s++] = ',';
248                                         strcpy(&result[s], flags.flags[imask].name);
249                                         s += strlen(flags.flags[imask].name);
250                                 }
251                         }
252                 }
253         } while (!result);
254         return result;
255 }
256
257 int afb_hook_flags_xreq_from_text(const char *text)
258 {
259         return from_text(text, FLAGS(xreq_flags));
260 }
261
262 int afb_hook_flags_api_from_text(const char *text)
263 {
264         return from_text(text, FLAGS(api_flags));
265 }
266
267 int afb_hook_flags_evt_from_text(const char *text)
268 {
269         return from_text(text, FLAGS(evt_flags));
270 }
271
272 int afb_hook_flags_session_from_text(const char *text)
273 {
274         return from_text(text, FLAGS(session_flags));
275 }
276
277 int afb_hook_flags_global_from_text(const char *text)
278 {
279         return from_text(text, FLAGS(global_flags));
280 }
281
282 char *afb_hook_flags_xreq_to_text(int value)
283 {
284         return to_text(value, FLAGS(xreq_flags));
285 }
286
287 char *afb_hook_flags_api_to_text(int value)
288 {
289         return to_text(value, FLAGS(api_flags));
290 }
291
292 char *afb_hook_flags_evt_to_text(int value)
293 {
294         return to_text(value, FLAGS(evt_flags));
295 }
296
297 char *afb_hook_flags_session_to_text(int value)
298 {
299         return to_text(value, FLAGS(session_flags));
300 }
301
302 char *afb_hook_flags_global_to_text(int value)
303 {
304         return to_text(value, FLAGS(global_flags));
305 }
306
307 #if !defined(REMOVE_LEGACY_TRACE)
308 static struct flag legacy_ditf_flags[] = { /* must be sorted by names */
309                 { "all",                        afb_hook_flags_api_ditf_all },
310                 { "common",                     afb_hook_flags_api_ditf_common },
311                 { "event_broadcast_after",      afb_hook_flag_api_event_broadcast },
312                 { "event_broadcast_before",     afb_hook_flag_api_event_broadcast },
313                 { "event_make",                 afb_hook_flag_api_event_make },
314                 { "extra",                      afb_hook_flags_api_ditf_extra },
315                 { "get_event_loop",             afb_hook_flag_api_get_event_loop },
316                 { "get_system_bus",             afb_hook_flag_api_get_system_bus },
317                 { "get_user_bus",               afb_hook_flag_api_get_user_bus },
318                 { "queue_job",                  afb_hook_flag_api_queue_job },
319                 { "require_api",                afb_hook_flag_api_require_api },
320                 { "require_api_result",         afb_hook_flag_api_require_api },
321                 { "rootdir_get_fd",             afb_hook_flag_api_rootdir_get_fd },
322                 { "rootdir_open_locale",        afb_hook_flag_api_rootdir_open_locale },
323                 { "unstore_req",                afb_hook_flag_api_legacy_unstore_req },
324                 { "vverbose",                   afb_hook_flag_api_vverbose },
325 };
326
327 static struct flag legacy_svc_flags[] = { /* must be sorted by names */
328                 { "all",                afb_hook_flags_api_svc_all },
329                 { "call",               afb_hook_flag_api_call },
330                 { "call_result",        afb_hook_flag_api_call },
331                 { "callsync",           afb_hook_flag_api_callsync },
332                 { "callsync_result",    afb_hook_flag_api_callsync },
333                 { "on_event_after",     afb_hook_flag_api_on_event },
334                 { "on_event_before",    afb_hook_flag_api_on_event },
335                 { "start_after",        afb_hook_flag_api_start },
336                 { "start_before",       afb_hook_flag_api_start },
337 };
338
339 int afb_hook_flags_legacy_ditf_from_text(const char *text)
340 {
341         return from_text(text, FLAGS(legacy_ditf_flags));
342 }
343
344 int afb_hook_flags_legacy_svc_from_text(const char *text)
345 {
346         return from_text(text, FLAGS(legacy_svc_flags));
347 }
348
349 char *afb_hook_flags_legacy_ditf_to_text(int value)
350 {
351         return to_text(value, FLAGS(legacy_ditf_flags));
352 }
353
354 char *afb_hook_flags_legacy_svc_to_text(int value)
355 {
356         return to_text(value, FLAGS(legacy_svc_flags));
357 }
358 #endif
359