Skip to content

Commit

Permalink
Example cli pipe grep command quotes vertical bar for OR function
Browse files Browse the repository at this point in the history
  • Loading branch information
olofhagsand committed Aug 29, 2023
1 parent 1698c67 commit 4e79ac4
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
27 changes: 24 additions & 3 deletions apps/cli/cli_pipe.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -123,18 +126,22 @@ 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,
cvec *cvv,
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));
Expand All @@ -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;
}
/* 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, value);
retval = pipe_arg_fn(h, GREP_BIN, option, cbuf_get(cb));
done:
if (cb)
cbuf_free(cb);
return retval;
}

Expand Down
2 changes: 1 addition & 1 deletion test/test_cli_pipe.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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>"
Expand Down

0 comments on commit 4e79ac4

Please sign in to comment.