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>
This commit is contained in:
Renato Westphal 2019-10-17 17:26:02 -03:00
parent 6c57402944
commit f5f0a0e302

View File

@ -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);