Example cli pipe grep command quotes vertical bar for OR function

This commit is contained in:
Olof hagsand 2023-08-29 08:39:22 +02:00
parent 1698c6717e
commit 4e79ac43fb
3 changed files with 26 additions and 4 deletions

View file

@ -51,6 +51,7 @@ Developers may need to change their code
### Minor features
* Example cli pipe grep command quotes vertical bar for OR function
* New command-line option for dumping configuration options for all clixon applications after load
* Syntax is `-C <format>`
* Example: `clixon_backend -1C json`

View file

@ -31,8 +31,11 @@
***** END LICENSE BLOCK *****
*
* Example cli pipe output functions.
* @note Paths to bins, such as GREP_BIN, are detected in configure.ac
* @note These functions are normally run in a forked sub-process as spawned in cligen_eval()
* A developer should probably revise these functions, since they are primarily intended for testing
* of the pipe functionality
*/
#ifdef HAVE_CONFIG_H
@ -123,6 +126,7 @@ pipe_arg_fn(clicon_handle h,
* @param[in] h Clicon handle
* @param[in] cvv Vector of cli string and instantiated variables
* @param[in] argv String vector of options. Format: <option> <value>
* @note Any vertical bar (|] in the patterns field is quoted for OR function
*/
int
pipe_grep_fn(clicon_handle h,
@ -130,11 +134,14 @@ pipe_grep_fn(clicon_handle h,
cvec *argv)
{
int retval = -1;
char *value = NULL;
char *pattern = NULL;
cg_var *cv;
char *str;
char *option = NULL;
char *argname = NULL;
cbuf *cb = NULL;
int i;
char c;
if (cvec_len(argv) != 2){
clicon_err(OE_PLUGIN, EINVAL, "Received %d arguments. Expected: <option> <argname>", cvec_len(argv));
@ -148,14 +155,28 @@ pipe_grep_fn(clicon_handle h,
(str = cv_string_get(cv)) != NULL &&
strlen(str))
argname = str;
if ((cb = cbuf_new()) == NULL){
clicon_err(OE_UNIX, errno, "cbuf_new");
goto done;
}
if (argname && strlen(argname)){
if ((cv = cvec_find_var(cvv, argname)) != NULL &&
(str = cv_string_get(cv)) != NULL &&
strlen(str))
value = str;
pattern = str;
}
retval = pipe_arg_fn(h, GREP_BIN, option, value);
/* quote | in pattern into cbuf */
for (i=0; i<strlen(pattern); i++){
c = pattern[i];
if (c == '|')
cprintf(cb, "\\|");
else
cprintf(cb, "%c", c);
}
retval = pipe_arg_fn(h, GREP_BIN, option, cbuf_get(cb));
done:
if (cb)
cbuf_free(cb);
return retval;
}

View file

@ -176,7 +176,7 @@ new "$mode show explicit | grep par"
expectpart "$($clixon_cli -1 -m $mode -f $cfg show explicit config \| grep par)" 0 "<parameter>" "</parameter>" --not-- "table" "value"
new "$mode show explicit | grep table|name"
expectpart "$($clixon_cli -1 -m $mode -f $cfg show explicit config \| grep 'table\\\|name')" 0 "<table xmlns=\"urn:example:clixon\">" "<name>x</name>" "<name>y</name>" "</table>" --not-- "parameter" "value"
expectpart "$($clixon_cli -1 -m $mode -f $cfg show explicit config \| grep 'table\|name')" 0 "<table xmlns=\"urn:example:clixon\">" "<name>x</name>" "<name>y</name>" "</table>" --not-- "parameter" "value"
new "$mode show explicit | tail 5"
expectpart "$($clixon_cli -1 -m $mode -f $cfg show explicit config \| tail 5)" 0 "<name>y</name>" --not-- "<name>x</name>"