Remove tailing spaces and TODO file
[src/app-framework-binder.git] / src / genskel / genskel.c
index 8afb70b..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>
 
@@ -71,6 +72,7 @@ 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;
 
@@ -93,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;
@@ -107,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)) {
@@ -167,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);
@@ -186,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 == '"') {
@@ -215,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;
@@ -235,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;
@@ -260,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)));
@@ -281,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);
@@ -483,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(
@@ -496,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(
@@ -561,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, "-"))
@@ -596,6 +628,9 @@ void process(char *filename)
        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(
@@ -604,7 +639,7 @@ void process(char *filename)
                "%s"
                ";\n"
                "\n"
-               , api, desc
+               , capi, desc
        );
        enum_verbs(declare_permissions);
        print_perms();
@@ -612,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(
@@ -624,6 +665,7 @@ 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"
@@ -633,10 +675,11 @@ void process(char *filename)
                "\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"
                , onevent ?: "NULL"
@@ -655,7 +698,7 @@ int main(int ac, char **av)
                process("-");
        else {
                do { process(*av); } while(*++av);
-       }       
+       }
        return 0;
 }