Update copyright dates
[src/app-framework-binder.git] / src / devtools / genskel.c
index 55dd3c1..aab2e56 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2016, 2017, 2018 "IoT.bzh"
+ * Copyright (C) 2015-2020 "IoT.bzh"
  * Author José Bollo <jose.bollo@iot.bzh>
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
@@ -48,6 +48,7 @@
 
 #include <json-c/json.h>
 
+#define T(x)   ((x) && *(x))
 #define oom(x) do{if(!(x)){fprintf(stderr,"out of memory\n");exit(1);}}while(0)
 
 /**
@@ -73,6 +74,9 @@ const char *api = NULL;
 const char *scope = NULL;
 const char *prefix = NULL;
 const char *postfix = NULL;
+const char *provideclass = NULL;
+const char *requireclass = NULL;
+const char *requireapi = NULL;
 char *capi = NULL;
 int priv = -1;
 int noconc = -1;
@@ -153,7 +157,7 @@ struct json_object *expand_$ref(struct path path)
        case json_type_array:
                /* expand the values of arrays */
                i = 0;
-               n = json_object_array_length(path.object);
+               n = (int)json_object_array_length(path.object);
                while (i != n) {
                        o = json_object_array_get_idx(path.object, i);
                        x = expand_$ref((struct path){ .object = o, .upper = &path });
@@ -188,7 +192,7 @@ char *cify(const char *str)
 char *make_info(const char *text, int split)
 {
        const char *a, *b;
-       char *desc, c, buf[3];
+       char *desc, c, buf[3] = {0};
        size_t len;
        int i, pos, e;
 
@@ -217,6 +221,7 @@ char *make_info(const char *text, int split)
                        switch ((c = *b++)) {
                        case 0:
                                b--;
+                               buf[0] = 0;
                                break;
                        case '/':
                                buf[0] = '/';
@@ -290,7 +295,7 @@ void print_perms()
        int i, n;
        const char *fmtstr = cpp ? "\t%s" : "\t{ %s }";
 
-       n = a_perms ? json_object_array_length(a_perms) : 0;
+       n = a_perms ? (int)json_object_array_length(a_perms) : 0;
        if (n) {
                printf("static const struct afb_auth _afb_auths_%s[] = {\n" , capi);
                i = 0;
@@ -322,7 +327,7 @@ struct json_object *new_perm(struct json_object *obj, const char *desc)
                }
 
                /* creates the reference in the structure */
-               asprintf(&b, "&_afb_auths_%s[%d]", capi, json_object_array_length(a_perms));
+               asprintf(&b, "&_afb_auths_%s[%d]", capi, (int)json_object_array_length(a_perms));
                x = json_object_new_string(desc);
                y = json_object_new_string(b);
                json_object_array_add(a_perms, x);
@@ -352,7 +357,7 @@ struct json_object *decl_perm_a(enum optype op, struct json_object *obj)
                opstr = op==And ? "And" : "Or";
        }
        x = NULL;
-       i = n = obj ? json_object_array_length(obj) : 0;
+       i = n = obj ? (int)json_object_array_length(obj) : 0;
        while (i) {
                y = decl_perm(json_object_array_get_idx(obj, --i));
                if (!y)
@@ -438,7 +443,7 @@ int get_session_a(int and, struct json_object *obj)
 {
        int i, n, x, y;
 
-       n = obj ? json_object_array_length(obj) : 0;
+       n = obj ? (int)json_object_array_length(obj) : 0;
        if (n == 0)
                return 0;
 
@@ -535,9 +540,11 @@ void print_verb(const char *name)
 
 void print_declare_verb(const char *name, struct json_object *obj)
 {
-       printf("%s void ", scope);
+       if (T(scope))
+               printf("%s ", scope);
+       printf("void ");
        print_verb(name);
-       printf("(afb_req req);\n");
+       printf("(afb_req_t req);\n");
 }
 
 void print_struct_verb(const char *name, struct json_object *obj)
@@ -561,15 +568,20 @@ void print_struct_verb(const char *name, struct json_object *obj)
                ",\n"
                "        .auth = %s,\n"
                "        .info = %s,\n"
-               "        .session = "
                , p && decl_perm(p) ? json_object_get_string(decl_perm(p)) : "NULL"
                , info ? make_info(info, 0) : "NULL"
        );
+       if (version == 3)
+               printf(
+                       "        .vcbdata = NULL,\n"
+               );
+       printf(
+               "        .session = "
+       );
        print_session(p);
        if (version == 3)
                printf(
                        ",\n"
-                       "        .vcbdata = NULL,\n"
                        "        .glob = 0"
                );
        printf(
@@ -666,6 +678,9 @@ void process(char *filename)
        getvar(&scope, "#/info/x-binding-c-generator/scope", "static");
        getvar(&prefix, "#/info/x-binding-c-generator/prefix", "afb_verb_");
        getvar(&postfix, "#/info/x-binding-c-generator/postfix", "_cb");
+       getvar(&provideclass, "#/info/x-binding-c-generator/provide-class", NULL);
+       getvar(&requireclass, "#/info/x-binding-c-generator/require-class", NULL);
+       getvar(&requireapi, "#/info/x-binding-c-generator/require-api", NULL);
        getvarbool(&priv, "#/info/x-binding-c-generator/private", 0);
        getvarbool(&noconc, "#/info/x-binding-c-generator/noconcurrency", 0);
        getvar(&api, "#/info/title", "?");
@@ -697,12 +712,17 @@ void process(char *filename)
                "        .callback = NULL,\n"
                "        .auth = NULL,\n"
                "        .info = NULL,\n"
+       );
+       if (version == 3)
+               printf(
+                       "        .vcbdata = NULL,\n"
+               );
+       printf(
                "        .session = 0"
        );
        if (version == 3)
                printf(
                        ",\n"
-                       "        .vcbdata = NULL,\n"
                        "        .glob = 0"
                );
        printf(
@@ -710,6 +730,24 @@ void process(char *filename)
                "       }\n"
                "};\n"
        );
+
+       if (T(preinit) || T(init) || T(onevent)) {
+               printf("\n");
+               if (T(preinit)) {
+                       if (T(scope)) printf("%s ", scope);
+                       printf("int %s(%s);\n", preinit, version==3 ? "afb_api_t api" : "");
+               }
+               if (T(init)) {
+                       if (T(scope)) printf("%s ", scope);
+                       printf("int %s(%s);\n", init, version==3 ? "afb_api_t api" : "");
+               }
+               if (T(onevent)) {
+                       if (T(scope)) printf("%s ", scope);
+                       printf("void %s(%sconst char *event, struct json_object *object);\n",
+                                       onevent, version==3 ? "afb_api_t api, " : "");
+               }
+       }
+
        printf(
                "\n"
                "%sconst struct afb_binding_v%d %s%s = {\n"
@@ -720,10 +758,6 @@ void process(char *filename)
                "    .preinit = %s,\n"
                "    .init = %s,\n"
                "    .onevent = %s,\n"
-               "%s"
-               "    .noconcurrency = %d\n"
-               "};\n"
-               "\n"
                , priv ? "static " : ""
                , version
                , priv ? "_afb_binding_" : version==3 ? "afbBindingV3" : "afbBindingV2"
@@ -732,10 +766,28 @@ void process(char *filename)
                , capi
                , info ? make_info(info, 0) : "NULL"
                , capi
-               , preinit ?: "NULL"
-               , init ?: "NULL"
-               , onevent ?: "NULL"
-               , version==3 ? "    .userdata = NULL,\n" : ""
+               , T(preinit) ? preinit : "NULL"
+               , T(init) ? init : "NULL"
+               , T(onevent) ? onevent : "NULL"
+       );
+
+
+       if (version == 3)
+               printf(
+                       "    .userdata = NULL,\n"
+                       "    .provide_class = %s%s%s,\n"
+                       "    .require_class = %s%s%s,\n"
+                       "    .require_api = %s%s%s,\n"
+                       , T(provideclass) ? "\"" : "", T(provideclass) ? provideclass : "NULL", T(provideclass) ? "\"" : ""
+                       , T(requireclass) ? "\"" : "", T(requireclass) ? requireclass : "NULL", T(requireclass) ? "\"" : ""
+                       , T(requireapi) ? "\"" : "", T(requireapi) ? requireapi : "NULL", T(requireapi) ? "\"" : ""
+               );
+
+
+       printf(
+               "    .noconcurrency = %d\n"
+               "};\n"
+               "\n"
                , !!noconc
        );