diff --git a/lib/clixon/clixon_dispatcher.h b/lib/clixon/clixon_dispatcher.h index ff8bb0fd..c27e1881 100644 --- a/lib/clixon/clixon_dispatcher.h +++ b/lib/clixon/clixon_dispatcher.h @@ -105,6 +105,6 @@ struct _dispatcher_entry { int dispatcher_register_handler(dispatcher_entry_t **root, dispatcher_definition *x); int dispatcher_call_handlers(dispatcher_entry_t *root, void *handle, char *path, void *user_args); int dispatcher_free(dispatcher_entry_t *root); -int dispatcher_print(FILE *f, dispatcher_entry_t *root); +int dispatcher_print(FILE *f, int level, dispatcher_entry_t *root); #endif /* _CLIXON_DISPATCH_DISPATCHER_H */ diff --git a/lib/clixon/clixon_plugin.h b/lib/clixon/clixon_plugin.h index 62168e2d..ffebc982 100644 --- a/lib/clixon/clixon_plugin.h +++ b/lib/clixon/clixon_plugin.h @@ -50,7 +50,6 @@ /* * Types */ - /*! Registered RPC callback function * @param[in] h Clicon handle @@ -201,8 +200,8 @@ typedef int (plgreset_t)(clicon_handle h, const char *db); * A complete valid XML tree is created by the plugin and sent back via xtop, which is merged * into a complete state tree by the system. * The plugin should ensure that xpath is matched (using namspace context nsc) - * This callback may be replaced with a "dispatcher" type API in the future where the - * XPath binding is stricter, similar to the pagination API. + * XXX: This callback may be replaced with a "dispatcher" type API in the future where the + * XPath binding is stricter, similar to the pagination API. * * @param[in] h Clicon handle * @param[in] xpath Part of state requested diff --git a/lib/src/clixon_dispatcher.c b/lib/src/clixon_dispatcher.c index b38e78f9..05b3f911 100644 --- a/lib/src/clixon_dispatcher.c +++ b/lib/src/clixon_dispatcher.c @@ -1,5 +1,36 @@ /* - * Copyright 2021 Rubicon Communications LLC (Netgate) + * + ***** BEGIN LICENSE BLOCK ***** + + Copyright (C) 2021 Rubicon Communications, LLC(Netgate) + + This file is part of CLIXON. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + + Alternatively, the contents of this file may be used under the terms of + the GNU General Public License Version 3 or later (the "GPL"), + in which case the provisions of the GPL are applicable instead + of those above. If you wish to allow use of your version of this file only + under the terms of the GPL, and not to allow others to + use your version of this file under the terms of Apache License version 2, + indicate your decision by deleting the provisions above and replace them with + the notice and other provisions required by the GPL. If you do not delete + the provisions above, a recipient may use your version of this file under + the terms of any one of the Apache License version 2 or the GPL. + + ***** END LICENSE BLOCK ***** + * @see https://github.com/dcornejo/dispatcher */ @@ -285,11 +316,8 @@ get_entry(dispatcher_entry_t *root, /* some elements may have keys defined, strip them off */ for (int i = 0; i < split_path_len; i++) { - char *kptr = strchr(split_path_list[i], '='); - - if ((kptr != NULL) && (*kptr == '=')) { - *(kptr + 1) = 0; - } + char *kptr = split_path_list[i]; + strsep(&kptr, "=[]"); } /* search down the tree */ @@ -423,8 +451,12 @@ dispatcher_call_handlers(dispatcher_entry_t *root, void *user_args) { int ret = 0; - dispatcher_entry_t *best = get_entry(root, path); + dispatcher_entry_t *best; + if ((best = get_entry(root, path)) == NULL){ + errno = ENOENT; + return -1; + } if (best->children != NULL) { call_handler_helper(best->children, handle, path, user_args); } @@ -450,3 +482,24 @@ dispatcher_free(dispatcher_entry_t *root) free(root); return 0; } + +/*! Pretty-print dispatcher tree + */ +#define INDENT 3 +int +dispatcher_print(FILE *f, + int level, + dispatcher_entry_t *de) +{ + fprintf(f, "%*s%s", level*INDENT, "", de->node_name); + if (de->handler) + fprintf(f, " %p", de->handler); + if (de->arg) + fprintf(f, " (%p)", de->arg); + fprintf(f, "\n"); + if (de->children) + dispatcher_print(f, level+1, de->children); + if (de->peer) + dispatcher_print(f, level, de->peer); + return 0; +} diff --git a/lib/src/clixon_yang.c b/lib/src/clixon_yang.c index 7a588f26..6f3b45ab 100644 --- a/lib/src/clixon_yang.c +++ b/lib/src/clixon_yang.c @@ -3585,7 +3585,7 @@ yang_extension_value(yang_stmt *ys, clicon_err(OE_UNIX, errno, "cbuf_new"); goto done; } - yext = NULL; /* This loop gets complicated in trhe case the extension is augmented */ + yext = NULL; /* This loop gets complicated in the case the extension is augmented */ while ((yext = yn_each(ys, yext)) != NULL) { if (yang_keyword_get(yext) != Y_UNKNOWN) continue; diff --git a/util/Makefile.in b/util/Makefile.in index ed34e1ef..7e4d21ed 100644 --- a/util/Makefile.in +++ b/util/Makefile.in @@ -91,7 +91,8 @@ APPSRC += clixon_util_path.c APPSRC += clixon_util_datastore.c APPSRC += clixon_util_regexp.c APPSRC += clixon_util_socket.c -APPSRC += clixon_util_validate.c +APPSRC += clixon_util_validate.c +APPSRC += clixon_util_dispatcher.c APPSRC += clixon_netconf_ssh_callhome.c APPSRC += clixon_netconf_ssh_callhome_client.c ifdef with_restconf @@ -149,6 +150,9 @@ clixon_util_socket: clixon_util_socket.c $(LIBDEPS) clixon_util_validate: clixon_util_validate.c $(LIBDEPS) $(CC) $(INCLUDES) $(CPPFLAGS) @CFLAGS@ $(LDFLAGS) $^ -l clixon_backend -o $@ $(LIBS) +clixon_util_dispatcher: clixon_util_dispatcher.c $(LIBDEPS) + $(CC) $(INCLUDES) $(CPPFLAGS) @CFLAGS@ $(LDFLAGS) $^ -l clixon_backend -o $@ $(LIBS) + ifdef with_restconf clixon_util_stream: clixon_util_stream.c $(LIBDEPS) $(CC) $(INCLUDES) $(CPPFLAGS) @CFLAGS@ $(LDFLAGS) $^ $(LIBS) -lcurl -o $@ @@ -163,7 +167,7 @@ endif distclean: clean rm -f Makefile *~ .depend -install: +install: $(APPS) install -d -m 0755 $(DESTDIR)$(bindir) install -m 0755 $(INSTALLFLAGS) $(APPS) $(DESTDIR)$(bindir) diff --git a/util/clixon_util_dispatcher b/util/clixon_util_dispatcher deleted file mode 100755 index 4601c6a9..00000000 Binary files a/util/clixon_util_dispatcher and /dev/null differ diff --git a/util/clixon_util_xpath.c b/util/clixon_util_xpath.c index ec611f0a..878dcb39 100644 --- a/util/clixon_util_xpath.c +++ b/util/clixon_util_xpath.c @@ -126,7 +126,7 @@ main(int argc, int len; char *buf = NULL; int ret; - FILE *fp = stdin; /* unless overriden by argv[1] */ + FILE *fp = stdin; /* unless overriden by -f */ char *yang_file_dir = NULL; yang_stmt *yspec = NULL; char *xpath = NULL; @@ -169,7 +169,7 @@ main(int argc, case 'f': /* XML file */ filename = optarg; if ((fp = fopen(filename, "r")) == NULL){ - clicon_err(OE_UNIX, errno, "open(%s)", argv[1]); + clicon_err(OE_UNIX, errno, "fopen(%s)", optarg); goto done; } break;