Remove tailing spaces and TODO file
[src/app-framework-binder.git] / src / genskel / genskel.c
index 7b7dafa..62dfe3f 100644 (file)
@@ -34,7 +34,7 @@
  *      "b": "int"
  *    }
  *  }
- * 
+ *
  * Invocation:   program  [file|-]...
  *
  * without arguments, it reads the input.
@@ -44,6 +44,7 @@
 #include <string.h>
 #include <stdio.h>
 #include <unistd.h>
+#include <ctype.h>
 
 #include <json-c/json.h>
 
@@ -64,14 +65,16 @@ struct path
 struct json_object *root = NULL;
 struct json_object *d_perms = NULL;
 struct json_object *a_perms = NULL;
+const char *preinit = NULL;
 const char *init = NULL;
-const char *start = NULL;
 const char *onevent = NULL;
 const char *api = NULL;
 const char *scope = NULL;
 const char *prefix = NULL;
 const char *postfix = NULL;
+char *capi = NULL;
 int priv = -1;
+int noconc = -1;
 
 /**
  * Search for a reference of type "#/a/b/c" int the
@@ -92,7 +95,7 @@ struct json_object *search(const char *path)
        d = strtok(d, "/");
        while(i && d) {
                if (!json_object_object_get_ex(i, d, &i))
-                       return NULL; 
+                       return NULL;
                d = strtok(NULL, "/");
        }
        return i;
@@ -106,7 +109,7 @@ struct json_object *expand_$ref(struct path path)
        struct path *p;
        struct json_object *o, *x;
        int n, i;
-       struct json_object_iterator ji, jn; 
+       struct json_object_iterator ji, jn;
 
        /* expansion depends of the type of the node */
        switch (json_object_get_type(path.object)) {
@@ -166,15 +169,27 @@ struct json_object *expand_$ref(struct path path)
        return path.object;
 }
 
+char *cify(const char *str)
+{
+       char *r = strdup(str);
+       int i = 0;
+       while (r && r[i]) {
+               if (!isalnum(r[i]))
+                       r[i] = '_';
+               i++;
+       }
+       return r;
+}
 
-char *make_desc(struct json_object *o)
+char *make_info(const char *text, int split)
 {
        const char *a, *b;
        char *desc, c, buf[3];
        size_t len;
        int i, pos, e;
 
-       a = b = json_object_to_json_string_ext(root, 0);
+       /* estimated length */
+       a = b = text;
        len = 1;
        while((c = *b++)) {
                len += 1 + ('"' == c);
@@ -185,6 +200,8 @@ char *make_desc(struct json_object *o)
        oom(desc);
 
        len = pos = 0;
+       if (!split)
+               desc[len++] = '"';
        b = a;
        while((c = *b++)) {
                if (c == '"') {
@@ -214,18 +231,20 @@ char *make_desc(struct json_object *o)
                }
                i = e = 0;
                while (buf[i]) {
-                       if (pos >= 77 && !e) {
-                               desc[len++] = '"';
-                               desc[len++] = '\n';
-                               pos = 0;
-                       }
-                       if (pos == 0) {
-                               desc[len++] = ' ';
-                               desc[len++] = ' ';
-                               desc[len++] = ' ';
-                               desc[len++] = ' ';
-                               desc[len++] = '"';
-                               pos = 5;
+                       if (split) {
+                               if (pos >= 77 && !e) {
+                                       desc[len++] = '"';
+                                       desc[len++] = '\n';
+                                       pos = 0;
+                               }
+                               if (pos == 0) {
+                                       desc[len++] = ' ';
+                                       desc[len++] = ' ';
+                                       desc[len++] = ' ';
+                                       desc[len++] = ' ';
+                                       desc[len++] = '"';
+                                       pos = 5;
+                               }
                        }
                        c = buf[i++];
                        desc[len++] = c;
@@ -234,11 +253,17 @@ char *make_desc(struct json_object *o)
                }
        }
        desc[len++] = '"';
-       desc[len++] = '\n';
+       if (split)
+               desc[len++] = '\n';
        desc[len] = 0;
        return desc;
 }
 
+char *make_desc(struct json_object *o)
+{
+       return make_info(json_object_to_json_string_ext(root, 0), 1);
+}
+
 struct json_object *permissions_of_verb(struct json_object *obj)
 {
        struct json_object *x, *y;
@@ -259,7 +284,7 @@ void print_perms()
 
        n = a_perms ? json_object_array_length(a_perms) : 0;
        if (n) {
-               printf("static const struct afb_auth _afb_auths_v2_%s[] = {\n" , api);
+               printf("static const struct afb_auth _afb_auths_v2_%s[] = {\n" , capi);
                i = 0;
                while (i < n) {
                        printf("\t{ %s }", json_object_get_string(json_object_array_get_idx(a_perms, i)));
@@ -280,9 +305,9 @@ struct json_object *new_perm(struct json_object *obj, const char *desc)
                if (!d_perms) {
                        d_perms = json_object_new_object();
                        a_perms = json_object_new_array();
-               }       
+               }
 
-               asprintf(&b, "&_afb_auths_v2_%s[%d]", api, json_object_array_length(a_perms));
+               asprintf(&b, "&_afb_auths_v2_%s[%d]", capi, 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);
@@ -441,7 +466,7 @@ void print_session(struct json_object *p)
        s = p ? get_session(p) : 0;
        c = 1;
        if (s & SESSION_CHECK) {
-               printf("%s", "|AFB_SESSION_CHECK" + c);
+               printf("%s", "|AFB_SESSION_CHECK_V2" + c);
                c = 0;
        }
        if (s & SESSION_LOA_3 & ~SESSION_LOA_2)
@@ -453,19 +478,19 @@ void print_session(struct json_object *p)
        else
                l = 0;
        if (l) {
-               printf("%s%d", "|AFB_SESSION_LOA_GE_" + c, l);
+               printf("%s%d_V2", "|AFB_SESSION_LOA_" + c, l);
                c = 0;
        }
        if (s & SESSION_CLOSE) {
-               printf("%s", "|AFB_SESSION_CLOSE" + c);
+               printf("%s", "|AFB_SESSION_CLOSE_V2" + c);
                c = 0;
        }
        if (s & SESSION_RENEW) {
-               printf("%s", "|AFB_SESSION_RENEW" + c);
+               printf("%s", "|AFB_SESSION_REFRESH_V2" + c);
                c = 0;
        }
        if (c)
-               printf("AFB_SESSION_NONE");
+               printf("AFB_SESSION_NONE_V2");
 }
 
 void print_verb(const char *name)
@@ -482,7 +507,12 @@ void print_declare_verb(const char *name, struct json_object *obj)
 
 void print_struct_verb(const char *name, struct json_object *obj)
 {
-       struct json_object *p;
+       struct json_object *p, *i;
+       const char *info;
+
+       info = NULL;
+       if (json_object_object_get_ex(obj, "description", &i))
+               info = json_object_get_string(i);
 
        p = permissions_of_verb(obj);
        printf(
@@ -495,8 +525,10 @@ void print_struct_verb(const char *name, struct json_object *obj)
        printf(
                ",\n"
                "        .auth = %s,\n"
+               "        .info = %s,\n"
                "        .session = "
-               , p ? json_object_get_string(decl_perm(p)) : "NULL"
+               , p && decl_perm(p) ? json_object_get_string(decl_perm(p)) : "NULL"
+               , info ? make_info(info, 0) : "NULL"
        );
        print_session(p);
        printf(
@@ -560,6 +592,7 @@ void getvar(const char **var, const char *path, const char *defval)
 void process(char *filename)
 {
        char *desc;
+       const char *info;
 
        /* translate - */
        if (!strcmp(filename, "-"))
@@ -586,14 +619,18 @@ void process(char *filename)
 
        /* get some names */
        getvar(&api, "#/info/x-binding-c-generator/api", NULL);
+       getvar(&preinit, "#/info/x-binding-c-generator/preinit", NULL);
        getvar(&init, "#/info/x-binding-c-generator/init", NULL);
-       getvar(&start, "#/info/x-binding-c-generator/start", NULL);
        getvar(&onevent, "#/info/x-binding-c-generator/onevent", NULL);
        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");
        getvarbool(&priv, "#/info/x-binding-c-generator/private", 0);
+       getvarbool(&noconc, "#/info/x-binding-c-generator/noconcurrency", 0);
        getvar(&api, "#/info/title", "?");
+       info = NULL;
+       getvar(&info, "#/info/description", NULL);
+       capi = cify(api);
 
        /* get the API name */
        printf(
@@ -602,7 +639,7 @@ void process(char *filename)
                "%s"
                ";\n"
                "\n"
-               , api, desc
+               , capi, desc
        );
        enum_verbs(declare_permissions);
        print_perms();
@@ -610,11 +647,17 @@ void process(char *filename)
        printf(
                "\n"
                "static const struct afb_verb_v2 _afb_verbs_v2_%s[] = {\n"
-                , api
+                , capi
        );
        enum_verbs(print_struct_verb);
        printf(
-               "    { .verb = NULL }\n"
+               "    {\n"
+               "        .verb = NULL,\n"
+               "        .callback = NULL,\n"
+               "        .auth = NULL,\n"
+               "        .info = NULL,\n"
+               "        .session = 0\n"
+               "       }\n"
                "};\n"
        );
        printf(
@@ -622,21 +665,25 @@ void process(char *filename)
                "%sconst struct afb_binding_v2 %s%s = {\n"
                "    .api = \"%s\",\n"
                "    .specification = _afb_description_v2_%s,\n"
+               "    .info = %s,\n"
                "    .verbs = _afb_verbs_v2_%s,\n"
+               "    .preinit = %s,\n"
                "    .init = %s,\n"
-               "    .start = %s,\n"
                "    .onevent = %s,\n"
+               "    .noconcurrency = %d\n"
                "};\n"
                "\n"
                , priv ? "static " : ""
                , priv ? "_afb_binding_v2_" : "afbBindingV2"
-               , priv ? api : ""
-               , api
-               , api
+               , priv ? capi : ""
                , api
+               , capi
+               , info ? make_info(info, 0) : "NULL"
+               , capi
+               , preinit ?: "NULL"
                , init ?: "NULL"
-               , start ?: "NULL"
                , onevent ?: "NULL"
+               , !!noconc
        );
 
        /* clean up */
@@ -651,7 +698,7 @@ int main(int ac, char **av)
                process("-");
        else {
                do { process(*av); } while(*++av);
-       }       
+       }
        return 0;
 }