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:
parent
e498e09570
commit
b33603107d
10 changed files with 547 additions and 11 deletions
123
apps/cli/cli_pipe.c
Normal file
123
apps/cli/cli_pipe.c
Normal 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;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue