2 * Copyright (C) 2016, 2017 "IoT.bzh"
3 * Author José Bollo <jose.bollo@iot.bzh>
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * This simple program expands the object { "$ref": "#/path/to/a/target" }
25 * "b": { "$ref": "#/type/a" }
29 * will be exapanded to
38 * Invocation: program [file|-]...
40 * without arguments, it reads the input.
48 #include <json-c/json.h>
50 #define oom(x) do{if(!(x)){fprintf(stderr,"out of memory\n");exit(1);}}while(0)
53 * records path to the expanded node
57 struct json_object *object; /**< node being expanded */
58 struct path *upper; /**< link to upper expanded nodes */
62 * root of the JSON being parsed
64 struct json_object *root = NULL;
65 struct json_object *d_perms = NULL;
66 struct json_object *a_perms = NULL;
67 const char *preinit = NULL;
68 const char *init = NULL;
69 const char *onevent = NULL;
70 const char *api = NULL;
71 const char *scope = NULL;
72 const char *prefix = NULL;
73 const char *postfix = NULL;
78 * Search for a reference of type "#/a/b/c" int the
81 struct json_object *search(const char *path)
84 struct json_object *i;
86 /* does it match #/ at the beginning? */
87 if (path[0] != '#' || (path[0] && path[1] != '/'))
90 /* search from root to target */
95 if (!json_object_object_get_ex(i, d, &i))
97 d = strtok(NULL, "/");
103 * Expands the node designated by path and returns its expanded form
105 struct json_object *expand_$ref(struct path path)
108 struct json_object *o, *x;
110 struct json_object_iterator ji, jn;
112 /* expansion depends of the type of the node */
113 switch (json_object_get_type(path.object)) {
114 case json_type_object:
115 /* for object, look if it contains a property "$ref" */
116 if (json_object_object_get_ex(path.object, "$ref", &o)) {
117 /* yes, reference, try to substitute its target */
118 if (!json_object_is_type(o, json_type_string)) {
119 fprintf(stderr, "found a $ref not being string. Is: %s\n", json_object_get_string(o));
122 x = search(json_object_get_string(o));
124 fprintf(stderr, "$ref not found. Was: %s\n", json_object_get_string(o));
129 if (x == p->object) {
130 fprintf(stderr, "$ref recursive. Was: %s\n", json_object_get_string(o));
135 /* cool found, return a new instance of the target */
136 return json_object_get(x);
138 /* no, expand the values */
139 ji = json_object_iter_begin(path.object);
140 jn = json_object_iter_end(path.object);
141 while (!json_object_iter_equal(&ji, &jn)) {
142 o = json_object_iter_peek_value(&ji);
143 x = expand_$ref((struct path){ .object = o, .upper = &path });
145 json_object_object_add(path.object, json_object_iter_peek_name(&ji), x);
146 json_object_iter_next(&ji);
149 case json_type_array:
150 /* expand the values of arrays */
152 n = json_object_array_length(path.object);
154 o = json_object_array_get_idx(path.object, i);
155 x = expand_$ref((struct path){ .object = o, .upper = &path });
157 json_object_array_put_idx(path.object, i, x);
162 /* otherwise no expansion */
166 /* return the given node */
171 char *make_desc(struct json_object *o)
174 char *desc, c, buf[3];
178 a = b = json_object_to_json_string_ext(root, 0);
181 len += 1 + ('"' == c);
184 len += 7 * (1 + len / 72);
196 else if (c == '\\') {
197 switch ((c = *b++)) {
218 if (pos >= 77 && !e) {
243 struct json_object *permissions_of_verb(struct json_object *obj)
245 struct json_object *x, *y;
247 if (json_object_object_get_ex(obj, "x-permissions", &x))
250 if (json_object_object_get_ex(obj, "get", &x))
251 if (json_object_object_get_ex(x, "x-permissions", &y))
261 n = a_perms ? json_object_array_length(a_perms) : 0;
263 printf("static const struct afb_auth _afb_auths_v2_%s[] = {\n" , api);
266 printf("\t{ %s }", json_object_get_string(json_object_array_get_idx(a_perms, i)));
267 printf(",\n"+(++i == n));
273 struct json_object *new_perm(struct json_object *obj, const char *desc)
277 struct json_object *x, *y;
279 tag = obj ? json_object_to_json_string_ext(obj, 0) : desc;
280 if (!json_object_object_get_ex(d_perms, tag, &y)) {
282 d_perms = json_object_new_object();
283 a_perms = json_object_new_array();
286 asprintf(&b, "&_afb_auths_v2_%s[%d]", api, json_object_array_length(a_perms));
287 x = json_object_new_string(desc);
288 y = json_object_new_string(b);
289 json_object_array_add(a_perms, x);
290 json_object_object_add(d_perms, tag, y);
296 struct json_object *decl_perm(struct json_object *obj);
298 struct json_object *decl_perm_a(const char *op, struct json_object *obj)
302 struct json_object *x, *y;
305 i = n = obj ? json_object_array_length(obj) : 0;
307 y = decl_perm(json_object_array_get_idx(obj, --i));
313 asprintf(&a, ".type = afb_auth_%s, .first = %s, .next = %s",
314 op, json_object_get_string(y), json_object_get_string(x));
315 x = new_perm(NULL, a);
322 struct json_object *decl_perm(struct json_object *obj)
325 struct json_object *x, *y;
327 if (json_object_object_get_ex(d_perms, json_object_to_json_string_ext(obj, 0), &x))
330 if (json_object_object_get_ex(obj, "permission", &x)) {
331 asprintf(&a, ".type = afb_auth_Permission, .text = \"%s\"", json_object_get_string(x));
332 y = new_perm(obj, a);
335 else if (json_object_object_get_ex(obj, "anyOf", &x)) {
336 y = decl_perm_a("Or", x);
338 else if (json_object_object_get_ex(obj, "allOf", &x)) {
339 y = decl_perm_a("And", x);
341 else if (json_object_object_get_ex(obj, "not", &x)) {
343 asprintf(&a, ".type = afb_auth_Not, .first = %s", json_object_get_string(x));
344 y = new_perm(obj, a);
347 else if (json_object_object_get_ex(obj, "LOA", &x))
349 else if (json_object_object_get_ex(obj, "session", &x))
357 void declare_permissions(const char *name, struct json_object *obj)
359 struct json_object *p;
361 p = permissions_of_verb(obj);
367 #define SESSION_CLOSE 0x000001
368 #define SESSION_RENEW 0x000010
369 #define SESSION_CHECK 0x000100
370 #define SESSION_LOA_1 0x001000
371 #define SESSION_LOA_2 0x011000
372 #define SESSION_LOA_3 0x111000
373 #define SESSION_MASK 0x111111
376 int get_session(struct json_object *obj);
378 int get_session_a(int and, struct json_object *obj)
382 n = obj ? json_object_array_length(obj) : 0;
387 x = get_session(json_object_array_get_idx(obj, --i));
389 y = get_session(json_object_array_get_idx(obj, --i));
398 int get_session(struct json_object *obj)
402 struct json_object *x;
405 if (json_object_object_get_ex(obj, "anyOf", &x)) {
406 y = get_session_a(1, x);
408 else if (json_object_object_get_ex(obj, "allOf", &x)) {
409 y = get_session_a(0, x);
411 else if (json_object_object_get_ex(obj, "not", &x)) {
412 y = ~get_session(x) & SESSION_MASK;
414 else if (json_object_object_get_ex(obj, "LOA", &x)) {
415 switch (json_object_get_int(x)) {
416 case 3: y = SESSION_LOA_3; break;
417 case 2: y = SESSION_LOA_2; break;
418 case 1: y = SESSION_LOA_1; break;
422 else if (json_object_object_get_ex(obj, "session", &x)) {
423 a = json_object_get_string(x);
424 if (!strcmp(a, "check"))
426 else if (!strcmp(a, "close"))
429 else if (json_object_object_get_ex(obj, "token", &x)) {
430 a = json_object_get_string(x);
431 if (!strcmp(a, "refresh"))
438 void print_session(struct json_object *p)
442 s = p ? get_session(p) : 0;
444 if (s & SESSION_CHECK) {
445 printf("%s", "|AFB_SESSION_CHECK_V2" + c);
448 if (s & SESSION_LOA_3 & ~SESSION_LOA_2)
450 else if (s & SESSION_LOA_2 & ~SESSION_LOA_1)
452 else if (s & SESSION_LOA_1)
457 printf("%s%d_V2", "|AFB_SESSION_LOA_" + c, l);
460 if (s & SESSION_CLOSE) {
461 printf("%s", "|AFB_SESSION_CLOSE_V2" + c);
464 if (s & SESSION_RENEW) {
465 printf("%s", "|AFB_SESSION_REFRESH_V2" + c);
469 printf("AFB_SESSION_NONE_V2");
472 void print_verb(const char *name)
474 printf("%s%s%s" , prefix, name, postfix);
477 void print_declare_verb(const char *name, struct json_object *obj)
479 printf("%s void ", scope);
481 printf("(struct afb_req req);\n");
484 void print_struct_verb(const char *name, struct json_object *obj)
486 struct json_object *p;
488 p = permissions_of_verb(obj);
501 , p ? json_object_get_string(decl_perm(p)) : "NULL"
510 void enum_verbs(void (*func)(const char *name, struct json_object *obj))
512 struct json_object_iterator ji, jn;
513 struct json_object *paths, *obj;
516 /* search the verbs */
517 paths = search("#/paths");
521 /* list the verbs and sort it */
522 ji = json_object_iter_begin(paths);
523 jn = json_object_iter_end(paths);
524 while (!json_object_iter_equal(&ji, &jn)) {
525 name = json_object_iter_peek_name(&ji);
526 obj = json_object_iter_peek_value(&ji);
527 name += (*name == '/');
529 json_object_iter_next(&ji);
533 void getvarbool(int *var, const char *path, int defval)
535 struct json_object *o;
537 if (*var != 0 && *var != 1) {
539 if (o && json_object_is_type(o, json_type_boolean))
540 *var = json_object_get_boolean(o);
546 void getvar(const char **var, const char *path, const char *defval)
548 struct json_object *o;
552 if (o && json_object_is_type(o, json_type_string))
553 *var = json_object_get_string(o);
560 * process a file and prints its expansion on stdout
562 void process(char *filename)
567 if (!strcmp(filename, "-"))
568 filename = "/dev/stdin";
571 if (access(filename, R_OK)) {
572 fprintf(stderr, "can't access file %s\n", filename);
577 root = json_object_from_file(filename);
579 fprintf(stderr, "reading file %s produced null\n", filename);
583 /* create the description */
584 desc = make_desc(root);
586 /* expand references */
587 root = expand_$ref((struct path){ .object = root, .upper = NULL });
590 getvar(&api, "#/info/x-binding-c-generator/api", NULL);
591 getvar(&preinit, "#/info/x-binding-c-generator/preinit", NULL);
592 getvar(&init, "#/info/x-binding-c-generator/init", NULL);
593 getvar(&onevent, "#/info/x-binding-c-generator/onevent", NULL);
594 getvar(&scope, "#/info/x-binding-c-generator/scope", "static");
595 getvar(&prefix, "#/info/x-binding-c-generator/prefix", "afb_verb_");
596 getvar(&postfix, "#/info/x-binding-c-generator/postfix", "_cb");
597 getvarbool(&priv, "#/info/x-binding-c-generator/private", 0);
598 getvarbool(&noconc, "#/info/x-binding-c-generator/noconcurrency", 0);
599 getvar(&api, "#/info/title", "?");
601 /* get the API name */
604 "static const char _afb_description_v2_%s[] =\n"
610 enum_verbs(declare_permissions);
612 enum_verbs(print_declare_verb);
615 "static const struct afb_verb_v2 _afb_verbs_v2_%s[] = {\n"
618 enum_verbs(print_struct_verb);
620 " { .verb = NULL }\n"
625 "%sconst struct afb_binding_v2 %s%s = {\n"
627 " .specification = _afb_description_v2_%s,\n"
629 " .verbs = _afb_verbs_v2_%s,\n"
633 " .noconcurrency = %d\n"
636 , priv ? "static " : ""
637 , priv ? "_afb_binding_v2_" : "afbBindingV2"
649 json_object_put(root);
653 /** process the list of files or stdin if none */
654 int main(int ac, char **av)
659 do { process(*av); } while(*++av);