Output pipe functionality

Based on output pipe code in CLIgen
Clixon adaptions include `CLICON_PIPETREE=` variable and a new cli_pipe.c callback file
This commit is contained in:
Olof hagsand 2023-06-28 14:40:10 +02:00
parent e498e09570
commit b33603107d
10 changed files with 547 additions and 11 deletions

View file

@ -107,6 +107,7 @@ LIBSRC += cli_plugin.c
LIBSRC += cli_auto.c
LIBSRC += cli_generate.c
LIBSRC += cli_autocli.c
LIBSRC += cli_pipe.c
LIBOBJ = $(LIBSRC:.c=.o)
# Name of lib

View file

@ -308,7 +308,6 @@ autocli_trees_default(clicon_handle h)
goto done;
if (cligen_ph_parsetree_set(ph, pt) < 0)
goto done;
/* Create backward compatible tree: @datamodelstate */
if ((ph = cligen_ph_add(cli_cligen(h), "datamodelstate")) == NULL)
goto done;
@ -403,7 +402,7 @@ autocli_start(clicon_handle h)
if (yang2cli_init(h) < 0)
goto done;
yspec = clicon_dbspec_yang(h);
/* The actual generating call from yang to clispec for the complete yang spec */
/* The actual generating call from yang to clispec for the complete yang spec, @basemodel */
if (yang2cli_yspec(h, yspec, AUTOCLI_TREENAME) < 0)
goto done;
/* XXX Create pre-5.5 tree-refs for backward compatibility */

123
apps/cli/cli_pipe.c Normal file
View file

@ -0,0 +1,123 @@
/*
*
***** BEGIN LICENSE BLOCK *****
Copyright (C) 2023 Olof Hagsand
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 *****
*
*/
#ifdef HAVE_CONFIG_H
#include "clixon_config.h" /* generated by config & autoconf */
#endif
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <errno.h>
#include <stdarg.h>
#include <time.h>
#include <ctype.h>
#include <unistd.h>
#include <dirent.h>
#include <syslog.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <sys/param.h>
#include <sys/mount.h>
#include <pwd.h>
/* cligen */
#include <cligen/cligen.h>
/* clicon */
#include <clixon/clixon.h>
#include "clixon_cli_api.h"
/* Grep pipe output function
*/
int
grep_fn(cligen_handle h,
cvec *cvv,
cvec *argv)
{
int retval = -1;
cbuf *cb = NULL;
cg_var *av;
cg_var *cv;
char *name;
if ((cb = cbuf_new()) == NULL){
perror("cbuf_new");
goto done;
}
if (argv && (av = cvec_i(argv, 0)) != NULL){
/* First arg is command */
// cprintf(cb, "%s", cv_string_get(av));
/* Rest are names of parameters from cvv */
av = NULL;
while ((av = cvec_each1(argv, av)) != NULL){
name = cv_string_get(av);
if ((cv = cvec_find_var(cvv, name)) != NULL)
cprintf(cb, "%s", cv_string_get(cv));
}
retval = execl("/usr/bin/grep", "grep", "-e", cbuf_get(cb), (char *) NULL);
}
done:
if (cb)
cbuf_free(cb);
return retval;
}
/*! Test cli callback calls cligen_output with output lines as given by function arguments
*
* This is to generate output to eg cligen_output scrolling
* Example:
* a, printlines_fn("line1 abc", "line2 def");
*/
int
output_fn(cligen_handle handle,
cvec *cvv,
cvec *argv)
{
cg_var *cv;
cv = NULL;
while ((cv = cvec_each(argv, cv)) != NULL){
cligen_output(stdout, "%s\n", cv_string_get(cv));
}
return 0;
}

View file

@ -32,7 +32,7 @@
the terms of any one of the Apache License version 2 or the GPL.
***** END LICENSE BLOCK *****
*
*/
#ifdef HAVE_CONFIG_H
@ -88,6 +88,7 @@ static int
gen_parse_tree(clicon_handle h,
char *name,
parse_tree *pt,
char *pipetree,
pt_head **php)
{
int retval = -1;
@ -101,6 +102,9 @@ gen_parse_tree(clicon_handle h,
clicon_err(OE_UNIX, errno, "cligen_ph_prompt_set");
goto done;
}
if (pipetree &&
cligen_ph_pipe_set(ph, pipetree) < 0)
goto done;
*php = ph;
retval = 0;
done:
@ -163,6 +167,25 @@ clixon_str2fn(char *name,
return NULL;
}
/*! Set output pipe flag in all callbacks
*
* @param[in] co CLIgen parse-tree object
* @param[in] arg Argument, cast to application-specific info
* @retval 1 OK and return (abort iteration)
* @retval 0 OK and continue
* @retval -1 Error: break and return
*/
static int
cli_mark_output_pipes(cg_obj *co,
void *arg)
{
cg_callback *cc;
for (cc = co->co_callbacks; cc; cc = co_callback_next(cc))
cc->cc_flags |= CC_FLAGS_PIPE_FUNCTION;
return 0;
}
/*! Load a file containing clispec syntax and append to specified modes, also load C plugin
*
* First load CLIgen file,
@ -195,6 +218,7 @@ clispec_load_file(clicon_handle h,
int i;
int nvec;
char *plgnam;
char *pipetree;
pt_head *ph;
#ifndef CLIXON_STATIC_PLUGINS
clixon_plugin_t *cp;
@ -238,6 +262,7 @@ clispec_load_file(clicon_handle h,
mode = cvec_find_str(cvv, "CLICON_MODE");
prompt = cvec_find_str(cvv, "CLICON_PROMPT");
plgnam = cvec_find_str(cvv, "CLICON_PLUGIN");
pipetree = cvec_find_str(cvv, "CLICON_PIPETREE");
#ifndef CLIXON_STATIC_PLUGINS
if (plgnam != NULL) { /* Find plugin for callback resolving */
@ -252,6 +277,13 @@ clispec_load_file(clicon_handle h,
}
#endif
/* Algorithm to find output pipe trees: first mode is on the form |<treename>
*/
if (mode && strlen(mode) && IS_PIPE_TREE(mode)){
if (pt_apply(pt, cli_mark_output_pipes, -1, NULL) < 0)
goto done;
}
/* Resolve callback names to function pointers. */
if (cligen_callbackv_str2fn(pt, (cgv_str2fn_t*)clixon_str2fn, handle) < 0){
clicon_err(OE_PLUGIN, 0, "Mismatch between CLIgen file '%s' and CLI plugin file '%s'. Some possible errors:\n\t1. A function given in the CLIgen file does not exist in the plugin (ie link error)\n\t2. The CLIgen spec does not point to the correct plugin .so file (CLICON_PLUGIN=\"%s\" is wrong)",
@ -296,7 +328,7 @@ clispec_load_file(clicon_handle h,
clicon_err(OE_UNIX, errno, "pt_new");
goto done;
}
if (gen_parse_tree(h, name, ptnew, &ph) < 0)
if (gen_parse_tree(h, name, ptnew, pipetree, &ph) < 0)
goto done;
if (ph == NULL)
goto done;