From c24c38dbb5aaab826f069ffc30da5daaa1753aa8 Mon Sep 17 00:00:00 2001 From: Olof hagsand Date: Mon, 2 Dec 2024 16:24:34 +0100 Subject: [PATCH] CLI generic pipe callbacks --- CHANGELOG.md | 3 + apps/cli/cli_auto.c | 2 - apps/cli/cli_common.c | 6 +- apps/cli/cli_handle.c | 1 - apps/cli/cli_main.c | 1 - apps/cli/cli_pipe.c | 82 ++++++++++++++++++++--- apps/cli/cli_pipe.h | 48 +++++++++++++ apps/cli/cli_show.c | 69 ++++++++++++++++++- apps/cli/clixon_cli_api.h | 2 +- test/test_cli_pipe.sh | 28 +++++++- yang/clixon/clixon-config@2024-11-01.yang | 12 ++++ 11 files changed, 234 insertions(+), 20 deletions(-) create mode 100644 apps/cli/cli_pipe.h diff --git a/CHANGELOG.md b/CHANGELOG.md index b5bcaaad..b5d72f14 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,8 @@ Expected: January 2025 ### Features +* New: CLI generic pipe callbacks + * Add scripts in `CLICON_CLI_PIPE_DIR` * New: [feature request: support xpath functions for strings](https://github.com/clicon/clixon/issues/556) * Added: re-match, substring, string, string-length, translate, substring-before, substring-after, starts-with * Added support for system-only-config data @@ -26,6 +28,7 @@ Expected: January 2025 * New `ca_system_only` backend callback for reading system-only data * New `clixon-config@2024-11-01.yang` revision * Changed: `CLICON_NETCONF_DUPLICATE_ALLOW` to not only check but remove duplicates + * Added: `CLICON_CLI_PIPE_DIR` * Added: `CLICON_XMLDB_SYSTEM_ONLY_CONFIG` ### C/CLI-API changes on existing features diff --git a/apps/cli/cli_auto.c b/apps/cli/cli_auto.c index 7d1cbc3d..594951e4 100644 --- a/apps/cli/cli_auto.c +++ b/apps/cli/cli_auto.c @@ -52,9 +52,7 @@ #include #include #include - #include -#include #include #include #include diff --git a/apps/cli/cli_common.c b/apps/cli/cli_common.c index 590a8b85..22a7c249 100644 --- a/apps/cli/cli_common.c +++ b/apps/cli/cli_common.c @@ -48,7 +48,6 @@ #include #include #include -#include #include #include #include @@ -807,7 +806,8 @@ cli_set_mode(clixon_handle h, * @param[in] argv Function arguments * @retval 0 OK, returns the exit code of the program * @retval -1 Error - * + * @note utility, should probably not be in lib + * @see cli_start_shell */ int cli_start_program(clixon_handle h, @@ -926,6 +926,8 @@ done: * @param[in] argv [], defaults to "sh" * @retval 0 OK * @retval -1 Error + * @note utility, should probably not be in lib + * @see cli_start_program */ int cli_start_shell(clixon_handle h, diff --git a/apps/cli/cli_handle.c b/apps/cli/cli_handle.c index c72637a7..fec17dfb 100644 --- a/apps/cli/cli_handle.c +++ b/apps/cli/cli_handle.c @@ -49,7 +49,6 @@ #include #include #include -#include #include #include #include diff --git a/apps/cli/cli_main.c b/apps/cli/cli_main.c index 623ccd21..0bc3b7f0 100644 --- a/apps/cli/cli_main.c +++ b/apps/cli/cli_main.c @@ -45,7 +45,6 @@ #include #include #include -#include #include #include #include diff --git a/apps/cli/cli_pipe.c b/apps/cli/cli_pipe.c index 7e35b812..304cee83 100644 --- a/apps/cli/cli_pipe.c +++ b/apps/cli/cli_pipe.c @@ -50,9 +50,7 @@ #include #include #include - #include -#include #include #include #include @@ -71,24 +69,25 @@ #include #include "clixon_cli_api.h" +#include "cli_pipe.h" /* General-purpose pipe output function * * @param[in] h Clixon handle - * @param[in] cmd Command to exec + * @param[in] cmd Command/file to exec * @param[in] option Option to command (or NULL) * @param[in] value Command argument value (or NULL) */ -int +static int pipe_arg_fn(clixon_handle h, char *cmd, char *option, char *value) { - int retval = -1; - struct stat fstat; - char **argv = NULL; - int i; + int retval = -1; + struct stat fstat; + char **argv = NULL; + int i; if (cmd == NULL || strlen(cmd) == 0){ clixon_err(OE_PLUGIN, EINVAL, "cmd '%s' NULL or empty", cmd); @@ -108,8 +107,10 @@ pipe_arg_fn(clixon_handle h, } i = 0; argv[i++] = cmd; - argv[i++] = option; - argv[i++] = value; + if (option) { + argv[i++] = option; + argv[i++] = value; + } argv[i++] = NULL; retval = execv(cmd, argv); done: @@ -198,6 +199,7 @@ pipe_wc_fn(clixon_handle h, if (cvec_len(argv) != 1){ clixon_err(OE_PLUGIN, EINVAL, "Received %d arguments. Expected: ", cvec_len(argv)); goto done; + } if ((cv = cvec_i(argv, 0)) != NULL && (str = cv_string_get(cv)) != NULL && @@ -392,6 +394,66 @@ pipe_save_file(clixon_handle h, return retval; } +/*! pipe function: start script + * + * @param[in] h Clixon handle + * @param[in] cvv Vector of cli string and instantiated variables + * @param[in] argv String vector of options. Format: + * name Name of cv containing script filename + * @retval 0 OK + * @retval -1 Error + * @code + * format("Generic format by callback") ("Name of generic format"), + * pipe_generic_callback("callback"); + * @endcode + */ +int +pipe_generic(clixon_handle h, + cvec *cvv, + cvec *argv) +{ + int retval = -1; + char *cvname; + cg_var *cv; + char *dir; + char *filename; + char *script = NULL; + cbuf *cb = NULL; + + if (cvec_len(argv) != 1) { + clixon_err(OE_PLUGIN, EINVAL, "Received %d arguments. Expected: