/*
- * Copyright (C) 2016, 2017, 2018 "IoT.bzh"
+ * Copyright (C) 2016-2019 "IoT.bzh"
* Author José Bollo <jose.bollo@iot.bzh>
*
* Licensed under the Apache License, Version 2.0 (the "License");
#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)
/**
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;
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 });
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;
switch ((c = *b++)) {
case 0:
b--;
+ buf[0] = 0;
break;
case '/':
buf[0] = '/';
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;
}
/* 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);
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)
{
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;
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)
",\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(
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", "?");
" .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(
" }\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"
" .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"
, 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
);