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.
49 #include <json-c/json.h>
51 #define oom(x) do{if(!(x)){fprintf(stderr,"out of memory\n");exit(1);}}while(0)
54 * records path to the expanded node
58 struct json_object *object; /**< node being expanded */
59 struct path *upper; /**< link to upper expanded nodes */
63 * root of the JSON being parsed
65 struct json_object *root = NULL;
66 struct json_object *d_perms = NULL;
67 struct json_object *a_perms = NULL;
68 const char *preinit = NULL;
69 const char *init = NULL;
70 const char *onevent = NULL;
71 const char *api = NULL;
72 const char *scope = NULL;
73 const char *prefix = NULL;
74 const char *postfix = NULL;
80 * Search for a reference of type "#/a/b/c" int the
83 struct json_object *search(const char *path)
86 struct json_object *i;
88 /* does it match #/ at the beginning? */
89 if (path[0] != '#' || (path[0] && path[1] != '/'))
92 /* search from root to target */
97 if (!json_object_object_get_ex(i, d, &i))
99 d = strtok(NULL, "/");
105 * Expands the node designated by path and returns its expanded form
107 struct json_object *expand_$ref(struct path path)
110 struct json_object *o, *x;
112 struct json_object_iterator ji, jn;
114 /* expansion depends of the type of the node */
115 switch (json_object_get_type(path.object)) {
116 case json_type_object:
117 /* for object, look if it contains a property "$ref" */
118 if (json_object_object_get_ex(path.object, "$ref", &o)) {
119 /* yes, reference, try to substitute its target */
120 if (!json_object_is_type(o, json_type_string)) {
121 fprintf(stderr, "found a $ref not being string. Is: %s\n", json_object_get_string(o));
124 x = search(json_object_get_string(o));
126 fprintf(stderr, "$ref not found. Was: %s\n", json_object_get_string(o));
131 if (x == p->object) {
132 fprintf(stderr, "$ref recursive. Was: %s\n", json_object_get_string(o));
137 /* cool found, return a new instance of the target */
138 return json_object_get(x);
140 /* no, expand the values */
141 ji = json_object_iter_begin(path.object);
142 jn = json_object_iter_end(path.object);
143 while (!json_object_iter_equal(&ji, &jn)) {
144 o = json_object_iter_peek_value(&ji);
145 x = expand_$ref((struct path){ .object = o, .upper = &path });
147 json_object_object_add(path.object, json_object_iter_peek_name(&ji), x);
148 json_object_iter_next(&ji);
151 case json_type_array:
152 /* expand the values of arrays */
154 n = json_object_array_length(path.object);
156 o = json_object_array_get_idx(path.object, i);
157 x = expand_$ref((struct path){ .object = o, .upper = &path });
159 json_object_array_put_idx(path.object, i, x);
164 /* otherwise no expansion */
168 /* return the given node */
172 char *cify(const char *str)
174 char *r = strdup(str);
184 char *make_info(const char *text, int split)
187 char *desc, c, buf[3];
191 /* estimated length */
195 len += 1 + ('"' == c);
198 len += 7 * (1 + len / 72);
212 else if (c == '\\') {
213 switch ((c = *b++)) {
235 if (pos >= 77 && !e) {
262 char *make_desc(struct json_object *o)
264 return make_info(json_object_to_json_string_ext(root, 0), 1);
267 struct json_object *permissions_of_verb(struct json_object *obj)
269 struct json_object *x, *y;
271 if (json_object_object_get_ex(obj, "x-permissions", &x))
274 if (json_object_object_get_ex(obj, "get", &x))
275 if (json_object_object_get_ex(x, "x-permissions", &y))
285 n = a_perms ? json_object_array_length(a_perms) : 0;
287 printf("static const struct afb_auth _afb_auths_v2_%s[] = {\n" , capi);
290 printf("\t{ %s }", json_object_get_string(json_object_array_get_idx(a_perms, i)));
291 printf(",\n"+(++i == n));
297 struct json_object *new_perm(struct json_object *obj, const char *desc)
301 struct json_object *x, *y;
303 tag = obj ? json_object_to_json_string_ext(obj, 0) : desc;
304 if (!json_object_object_get_ex(d_perms, tag, &y)) {
306 d_perms = json_object_new_object();
307 a_perms = json_object_new_array();
310 asprintf(&b, "&_afb_auths_v2_%s[%d]", capi, json_object_array_length(a_perms));
311 x = json_object_new_string(desc);
312 y = json_object_new_string(b);
313 json_object_array_add(a_perms, x);
314 json_object_object_add(d_perms, tag, y);
320 struct json_object *decl_perm(struct json_object *obj);
322 struct json_object *decl_perm_a(const char *op, struct json_object *obj)
326 struct json_object *x, *y;
329 i = n = obj ? json_object_array_length(obj) : 0;
331 y = decl_perm(json_object_array_get_idx(obj, --i));
337 asprintf(&a, ".type = afb_auth_%s, .first = %s, .next = %s",
338 op, json_object_get_string(y), json_object_get_string(x));
339 x = new_perm(NULL, a);
346 struct json_object *decl_perm(struct json_object *obj)
349 struct json_object *x, *y;
351 if (json_object_object_get_ex(d_perms, json_object_to_json_string_ext(obj, 0), &x))
354 if (json_object_object_get_ex(obj, "permission", &x)) {
355 asprintf(&a, ".type = afb_auth_Permission, .text = \"%s\"", json_object_get_string(x));
356 y = new_perm(obj, a);
359 else if (json_object_object_get_ex(obj, "anyOf", &x)) {
360 y = decl_perm_a("Or", x);
362 else if (json_object_object_get_ex(obj, "allOf", &x)) {
363 y = decl_perm_a("And", x);
365 else if (json_object_object_get_ex(obj, "not", &x)) {
367 asprintf(&a, ".type = afb_auth_Not, .first = %s", json_object_get_string(x));
368 y = new_perm(obj, a);
371 else if (json_object_object_get_ex(obj, "LOA", &x))
373 else if (json_object_object_get_ex(obj, "session", &x))
381 void declare_permissions(const char *name, struct json_object *obj)
383 struct json_object *p;
385 p = permissions_of_verb(obj);
391 #define SESSION_CLOSE 0x000001
392 #define SESSION_RENEW 0x000010
393 #define SESSION_CHECK 0x000100
394 #define SESSION_LOA_1 0x001000
395 #define SESSION_LOA_2 0x011000
396 #define SESSION_LOA_3 0x111000
397 #define SESSION_MASK 0x111111
400 int get_session(struct json_object *obj);
402 int get_session_a(int and, struct json_object *obj)
406 n = obj ? json_object_array_length(obj) : 0;
411 x = get_session(json_object_array_get_idx(obj, --i));
413 y = get_session(json_object_array_get_idx(obj, --i));
422 int get_session(struct json_object *obj)
426 struct json_object *x;
429 if (json_object_object_get_ex(obj, "anyOf", &x)) {
430 y = get_session_a(1, x);
432 else if (json_object_object_get_ex(obj, "allOf", &x)) {
433 y = get_session_a(0, x);
435 else if (json_object_object_get_ex(obj, "not", &x)) {
436 y = ~get_session(x) & SESSION_MASK;
438 else if (json_object_object_get_ex(obj, "LOA", &x)) {
439 switch (json_object_get_int(x)) {
440 case 3: y = SESSION_LOA_3; break;
441 case 2: y = SESSION_LOA_2; break;
442 case 1: y = SESSION_LOA_1; break;
446 else if (json_object_object_get_ex(obj, "session", &x)) {
447 a = json_object_get_string(x);
448 if (!strcmp(a, "check"))
450 else if (!strcmp(a, "close"))
453 else if (json_object_object_get_ex(obj, "token", &x)) {
454 a = json_object_get_string(x);
455 if (!strcmp(a, "refresh"))
462 void print_session(struct json_object *p)
466 s = p ? get_session(p) : 0;
468 if (s & SESSION_CHECK) {
469 printf("%s", "|AFB_SESSION_CHECK_V2" + c);
472 if (s & SESSION_LOA_3 & ~SESSION_LOA_2)
474 else if (s & SESSION_LOA_2 & ~SESSION_LOA_1)
476 else if (s & SESSION_LOA_1)
481 printf("%s%d_V2", "|AFB_SESSION_LOA_" + c, l);
484 if (s & SESSION_CLOSE) {
485 printf("%s", "|AFB_SESSION_CLOSE_V2" + c);
488 if (s & SESSION_RENEW) {
489 printf("%s", "|AFB_SESSION_REFRESH_V2" + c);
493 printf("AFB_SESSION_NONE_V2");
496 void print_verb(const char *name)
498 printf("%s%s%s" , prefix, name, postfix);
501 void print_declare_verb(const char *name, struct json_object *obj)
503 printf("%s void ", scope);
505 printf("(struct afb_req req);\n");
508 void print_struct_verb(const char *name, struct json_object *obj)
510 struct json_object *p, *i;
514 if (json_object_object_get_ex(obj, "description", &i))
515 info = json_object_get_string(i);
517 p = permissions_of_verb(obj);
530 , p && decl_perm(p) ? json_object_get_string(decl_perm(p)) : "NULL"
531 , info ? make_info(info, 0) : "NULL"
540 void enum_verbs(void (*func)(const char *name, struct json_object *obj))
542 struct json_object_iterator ji, jn;
543 struct json_object *paths, *obj;
546 /* search the verbs */
547 paths = search("#/paths");
551 /* list the verbs and sort it */
552 ji = json_object_iter_begin(paths);
553 jn = json_object_iter_end(paths);
554 while (!json_object_iter_equal(&ji, &jn)) {
555 name = json_object_iter_peek_name(&ji);
556 obj = json_object_iter_peek_value(&ji);
557 name += (*name == '/');
559 json_object_iter_next(&ji);
563 void getvarbool(int *var, const char *path, int defval)
565 struct json_object *o;
567 if (*var != 0 && *var != 1) {
569 if (o && json_object_is_type(o, json_type_boolean))
570 *var = json_object_get_boolean(o);
576 void getvar(const char **var, const char *path, const char *defval)
578 struct json_object *o;
582 if (o && json_object_is_type(o, json_type_string))
583 *var = json_object_get_string(o);
590 * process a file and prints its expansion on stdout
592 void process(char *filename)
598 if (!strcmp(filename, "-"))
599 filename = "/dev/stdin";
602 if (access(filename, R_OK)) {
603 fprintf(stderr, "can't access file %s\n", filename);
608 root = json_object_from_file(filename);
610 fprintf(stderr, "reading file %s produced null\n", filename);
614 /* create the description */
615 desc = make_desc(root);
617 /* expand references */
618 root = expand_$ref((struct path){ .object = root, .upper = NULL });
621 getvar(&api, "#/info/x-binding-c-generator/api", NULL);
622 getvar(&preinit, "#/info/x-binding-c-generator/preinit", NULL);
623 getvar(&init, "#/info/x-binding-c-generator/init", NULL);
624 getvar(&onevent, "#/info/x-binding-c-generator/onevent", NULL);
625 getvar(&scope, "#/info/x-binding-c-generator/scope", "static");
626 getvar(&prefix, "#/info/x-binding-c-generator/prefix", "afb_verb_");
627 getvar(&postfix, "#/info/x-binding-c-generator/postfix", "_cb");
628 getvarbool(&priv, "#/info/x-binding-c-generator/private", 0);
629 getvarbool(&noconc, "#/info/x-binding-c-generator/noconcurrency", 0);
630 getvar(&api, "#/info/title", "?");
632 getvar(&info, "#/info/description", NULL);
635 /* get the API name */
638 "static const char _afb_description_v2_%s[] =\n"
644 enum_verbs(declare_permissions);
646 enum_verbs(print_declare_verb);
649 "static const struct afb_verb_v2 _afb_verbs_v2_%s[] = {\n"
652 enum_verbs(print_struct_verb);
656 " .callback = NULL,\n"
665 "%sconst struct afb_binding_v2 %s%s = {\n"
667 " .specification = _afb_description_v2_%s,\n"
669 " .verbs = _afb_verbs_v2_%s,\n"
673 " .noconcurrency = %d\n"
676 , priv ? "static " : ""
677 , priv ? "_afb_binding_v2_" : "afbBindingV2"
681 , info ? make_info(info, 0) : "NULL"
690 json_object_put(root);
694 /** process the list of files or stdin if none */
695 int main(int ac, char **av)
700 do { process(*av); } while(*++av);