Fedora 30 packaging fix issu
[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                 { "settings",           afb_hook_flag_api_settings },
116                 { "start",              afb_hook_flag_api_start },
117                 { "vverbose",           afb_hook_flag_api_vverbose },
118 };
119
120 static struct flag evt_flags[] = { /* must be sorted by names */
121                 { "addref",             afb_hook_flag_evt_addref },
122                 { "all",                afb_hook_flags_evt_all },
123                 { "broadcast_after",    afb_hook_flag_evt_broadcast_after },
124                 { "broadcast_before",   afb_hook_flag_evt_broadcast_before },
125                 { "common",             afb_hook_flags_evt_common },
126                 { "create",             afb_hook_flag_evt_create },
127                 { "extra",              afb_hook_flags_evt_extra },
128                 { "name",               afb_hook_flag_evt_name },
129                 { "push_after",         afb_hook_flag_evt_push_after },
130                 { "push_before",        afb_hook_flag_evt_push_before },
131                 { "unref",              afb_hook_flag_evt_unref },
132 };
133
134 static struct flag session_flags[] = { /* must be sorted by names */
135                 { "addref",             afb_hook_flag_session_addref },
136                 { "all",                afb_hook_flags_session_all },
137                 { "close",              afb_hook_flag_session_close },
138                 { "common",             afb_hook_flags_session_common },
139                 { "create",             afb_hook_flag_session_create },
140                 { "destroy",            afb_hook_flag_session_destroy },
141                 { "renew",              afb_hook_flag_session_renew },
142                 { "unref",              afb_hook_flag_session_unref },
143 };
144
145 static struct flag global_flags[] = { /* must be sorted by names */
146                 { "all",                afb_hook_flags_global_all },
147                 { "vverbose",           afb_hook_flag_global_vverbose },
148 };
149
150 static int compare(const char *query, const char *value, size_t query_length)
151 {
152         size_t i;
153         char q, v;
154
155         for (i = 0 ; i < query_length ; i++) {
156                 v = value[i];
157                 q = query[i];
158                 if (!v)
159                         return -1;
160                 v = v == '_' ? '-' : (char)toupper(v);
161                 q = q == '_' ? '-' : (char)toupper(q);
162                 if (q != v)
163                         return (int)((unsigned)q - (unsigned)v);
164         }
165         return !!value[i];
166 }
167
168 /* get the value of the flag of 'name' in the array 'flags' of 'count elements */
169 static int get_flag(const char *name, struct flags flags, size_t length)
170 {
171         /* replace "*" by "all" */
172         if (length == 1 && *name == '*') {
173                 name = "all";
174                 length = 3;
175         }
176
177         /* dichotomic search */
178         int lower = 0, upper = flags.count;
179         while (lower < upper) {
180                 int mid = (lower + upper) >> 1;
181                 int cmp = compare(name, flags.flags[mid].name, length);
182                 if (!cmp)
183                         return flags.flags[mid].value;
184                 if (cmp < 0)
185                         upper = mid;
186                 else
187                         lower = mid + 1;
188         }
189
190         return -(compare(name, "no", length) && compare(name, "none", length));
191 }
192
193 static int from_text(const char *text, struct flags flags)
194 {
195         static const char sep[] = " \t,";
196         size_t s;
197         int result = 0, val;
198
199         if (text) {
200                 for (;;) {
201                         text += strspn(text, sep);
202                         if (!*text)
203                                 break;
204                         s = strcspn(text, sep);
205                         val = get_flag(text, flags, s);
206                         if (val == -1)
207                                 return val;
208                         result |= val;
209                         text += s;
210                 }
211         }
212         return result;
213 }
214
215 static char *to_text(int value, struct flags flags)
216 {
217         int borrow = 0, mask = 0, i, v, imask;
218         size_t s = 0;
219         char *result = NULL;
220
221         if (!value)
222                 return strdup("none");
223
224         do {
225                 if (s) {
226                         result = malloc(s + 1);
227                         if (!result)
228                                 break;
229                 }
230                 borrow = 0;
231                 while (borrow != value) {
232                         mask = imask = 0;
233                         i = flags.count;
234                         while (i) {
235                                 v = flags.flags[--i].value;
236                                 if ((mask & v) == mask && (borrow & v) == 0 && (value & v) == v) {
237                                         mask = v;
238                                         imask = i;
239                                 }
240                         }
241                         if (mask == 0)
242                                 borrow = value;
243                         else {
244                                 if (!result)
245                                         s += strlen(flags.flags[imask].name) + !!s;
246                                 else {
247                                         if (s)
248                                                 result[s++] = ',';
249                                         strcpy(&result[s], flags.flags[imask].name);
250                                         s += strlen(flags.flags[imask].name);
251                                 }
252                         }
253                 }
254         } while (!result);
255         return result;
256 }
257
258 int afb_hook_flags_xreq_from_text(const char *text)
259 {
260         return from_text(text, FLAGS(xreq_flags));
261 }
262
263 int afb_hook_flags_api_from_text(const char *text)
264 {
265         return from_text(text, FLAGS(api_flags));
266 }
267
268 int afb_hook_flags_evt_from_text(const char *text)
269 {
270         return from_text(text, FLAGS(evt_flags));
271 }
272
273 int afb_hook_flags_session_from_text(const char *text)
274 {
275         return from_text(text, FLAGS(session_flags));
276 }
277
278 int afb_hook_flags_global_from_text(const char *text)
279 {
280         return from_text(text, FLAGS(global_flags));
281 }
282
283 char *afb_hook_flags_xreq_to_text(int value)
284 {
285         return to_text(value, FLAGS(xreq_flags));
286 }
287
288 char *afb_hook_flags_api_to_text(int value)
289 {
290         return to_text(value, FLAGS(api_flags));
291 }
292
293 char *afb_hook_flags_evt_to_text(int value)
294 {
295         return to_text(value, FLAGS(evt_flags));
296 }
297
298 char *afb_hook_flags_session_to_text(int value)
299 {
300         return to_text(value, FLAGS(session_flags));
301 }
302
303 char *afb_hook_flags_global_to_text(int value)
304 {
305         return to_text(value, FLAGS(global_flags));
306 }
307
308 #if !defined(REMOVE_LEGACY_TRACE)
309 static struct flag legacy_ditf_flags[] = { /* must be sorted by names */
310                 { "all",                        afb_hook_flags_api_ditf_all },
311                 { "common",                     afb_hook_flags_api_ditf_common },
312                 { "event_broadcast_after",      afb_hook_flag_api_event_broadcast },
313                 { "event_broadcast_before",     afb_hook_flag_api_event_broadcast },
314                 { "event_make",                 afb_hook_flag_api_event_make },
315                 { "extra",                      afb_hook_flags_api_ditf_extra },
316                 { "get_event_loop",             afb_hook_flag_api_get_event_loop },
317                 { "get_system_bus",             afb_hook_flag_api_get_system_bus },
318                 { "get_user_bus",               afb_hook_flag_api_get_user_bus },
319                 { "queue_job",                  afb_hook_flag_api_queue_job },
320                 { "require_api",                afb_hook_flag_api_require_api },
321                 { "require_api_result",         afb_hook_flag_api_require_api },
322                 { "rootdir_get_fd",             afb_hook_flag_api_rootdir_get_fd },
323                 { "rootdir_open_locale",        afb_hook_flag_api_rootdir_open_locale },
324                 { "unstore_req",                afb_hook_flag_api_legacy_unstore_req },
325                 { "vverbose",                   afb_hook_flag_api_vverbose },
326 };
327
328 static struct flag legacy_svc_flags[] = { /* must be sorted by names */
329                 { "all",                afb_hook_flags_api_svc_all },
330                 { "call",               afb_hook_flag_api_call },
331                 { "call_result",        afb_hook_flag_api_call },
332                 { "callsync",           afb_hook_flag_api_callsync },
333                 { "callsync_result",    afb_hook_flag_api_callsync },
334                 { "on_event_after",     afb_hook_flag_api_on_event },
335                 { "on_event_before",    afb_hook_flag_api_on_event },
336                 { "start_after",        afb_hook_flag_api_start },
337                 { "start_before",       afb_hook_flag_api_start },
338 };
339
340 int afb_hook_flags_legacy_ditf_from_text(const char *text)
341 {
342         return from_text(text, FLAGS(legacy_ditf_flags));
343 }
344
345 int afb_hook_flags_legacy_svc_from_text(const char *text)
346 {
347         return from_text(text, FLAGS(legacy_svc_flags));
348 }
349
350 char *afb_hook_flags_legacy_ditf_to_text(int value)
351 {
352         return to_text(value, FLAGS(legacy_ditf_flags));
353 }
354
355 char *afb_hook_flags_legacy_svc_to_text(int value)
356 {
357         return to_text(value, FLAGS(legacy_svc_flags));
358 }
359 #endif
360