diff options
author | Renato Westphal <renato@opensourcerouting.org> | 2019-10-17 22:26:02 +0200 |
---|---|---|
committer | Renato Westphal <renato@opensourcerouting.org> | 2019-10-30 14:32:51 +0100 |
commit | f5f0a0e302da2e84d32c5accff28f0a2237483d4 (patch) | |
tree | b63689eb5fb2958ea15eee37dddcce8524466778 /tools/gen_northbound_callbacks.c | |
parent | bfdd: split northbound callbacks into multiple files (diff) | |
download | frr-f5f0a0e302da2e84d32c5accff28f0a2237483d4.tar.xz frr-f5f0a0e302da2e84d32c5accff28f0a2237483d4.zip |
tools: update the northbound callbacks generator
Add a new '-s' option which controls whether the generated northbound
callbacks are declared with the 'static' specifier or not. If not
(the default), a prototype is generated for each callback before
their declarations.
It's suggested that daemons shouldn't use the '-s' option so that
their northbound callbacks can be implemented in different files
according to their class (config, state, rpc or notification).
libfrr commands, on the other hand, can use the '-s' option when
their associated YANG module is too small and putting all callbacks
in the same file is desirable.
Signed-off-by: Renato Westphal <renato@opensourcerouting.org>
Diffstat (limited to 'tools/gen_northbound_callbacks.c')
-rw-r--r-- | tools/gen_northbound_callbacks.c | 55 |
1 files changed, 52 insertions, 3 deletions
diff --git a/tools/gen_northbound_callbacks.c b/tools/gen_northbound_callbacks.c index 14f648e8d..cbdf01e7b 100644 --- a/tools/gen_northbound_callbacks.c +++ b/tools/gen_northbound_callbacks.c @@ -26,10 +26,12 @@ #include "yang.h" #include "northbound.h" +static bool static_cbs; + static void __attribute__((noreturn)) usage(int status) { extern const char *__progname; - fprintf(stderr, "usage: %s [-h] [-p path] MODULE\n", __progname); + fprintf(stderr, "usage: %s [-h] [-s] [-p path] MODULE\n", __progname); exit(status); } @@ -153,10 +155,46 @@ static void generate_callback_name(struct lys_node *snode, replace_hyphens_by_underscores(buffer); } +static void generate_prototype(const struct nb_callback_info *ncinfo, + const char *cb_name) +{ + printf("%s%s(%s);\n", ncinfo->return_type, cb_name, ncinfo->arguments); +} + +static int generate_prototypes(const struct lys_node *snode, void *arg) +{ + switch (snode->nodetype) { + case LYS_CONTAINER: + case LYS_LEAF: + case LYS_LEAFLIST: + case LYS_LIST: + case LYS_NOTIF: + case LYS_RPC: + break; + default: + return YANG_ITER_CONTINUE; + } + + for (struct nb_callback_info *cb = &nb_callbacks[0]; + cb->operation != -1; cb++) { + char cb_name[BUFSIZ]; + + if (cb->optional + || !nb_operation_is_valid(cb->operation, snode)) + continue; + + generate_callback_name((struct lys_node *)snode, cb->operation, + cb_name, sizeof(cb_name)); + generate_prototype(cb, cb_name); + } + + return YANG_ITER_CONTINUE; +} + static void generate_callback(const struct nb_callback_info *ncinfo, const char *cb_name) { - printf("static %s%s(%s)\n{\n", + printf("%s%s%s(%s)\n{\n", static_cbs ? "static " : "", ncinfo->return_type, cb_name, ncinfo->arguments); switch (ncinfo->operation) { @@ -287,7 +325,7 @@ int main(int argc, char *argv[]) struct stat st; int opt; - while ((opt = getopt(argc, argv, "hp:")) != -1) { + while ((opt = getopt(argc, argv, "hp:s")) != -1) { switch (opt) { case 'h': usage(EXIT_SUCCESS); @@ -307,6 +345,9 @@ int main(int argc, char *argv[]) search_path = optarg; break; + case 's': + static_cbs = true; + break; default: usage(EXIT_FAILURE); /* NOTREACHED */ @@ -332,6 +373,14 @@ int main(int argc, char *argv[]) /* Create a nb_node for all YANG schema nodes. */ nb_nodes_create(); + /* Generate callback prototypes. */ + if (!static_cbs) { + printf("/* prototypes */\n"); + yang_snodes_iterate_module(module->info, generate_prototypes, 0, + NULL); + printf("\n"); + } + /* Generate callback functions. */ yang_snodes_iterate_module(module->info, generate_callbacks, 0, NULL); |