Skip to content

Commit

Permalink
Add socket pipe interface to keep track of when a function is called …
Browse files Browse the repository at this point in the history
…in a pipe
  • Loading branch information
olofhagsand committed Dec 9, 2024
1 parent 6a0e731 commit fbd6e20
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 4 deletions.
37 changes: 35 additions & 2 deletions cligen_handle.c
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ cligen_init(void)
ch->ch_magic = CLIGEN_MAGIC;
ch->ch_tabmode = 0x0; /* see CLIGEN_TABMODE_* */
ch->ch_delimiter = ' ';
ch->ch_spipe = -1;
h = (cligen_handle)ch;
cligen_prompt_set(h, CLIGEN_PROMPT_DEFAULT);
/* Only if stdin and stdout refers to a terminal make win size check */
Expand Down Expand Up @@ -465,7 +466,8 @@ cligen_callback_arguments_set(cligen_handle h,
* return 0;
* }
* @endcode
* @param[in] h CLIgen handle
* @param[in] h CLIgen handle
* @retval fn Name of function that was called in currently active callback
*/
char *
cligen_fn_str_get(cligen_handle h)
Expand All @@ -478,7 +480,9 @@ cligen_fn_str_get(cligen_handle h)
/*! Set callback function name string
*
* @param[in] h CLIgen handle
* @param[in] fn_str Name of function that was called in this callback
* @param[in] fn_str Name of function that was called in currently active callback
* @retval 0 OK
* @retval -1 Error
*/
int
cligen_fn_str_set(cligen_handle h,
Expand All @@ -497,6 +501,35 @@ cligen_fn_str_set(cligen_handle h,
return 0;
}

/*! Get socket of active output pipe
*
* @param[in] h CLIgen handle
* @retval s Socket, -1 means not set
*/
int
cligen_spipe_get(cligen_handle h)
{
struct cligen_handle *ch = handle(h);

return ch->ch_spipe;
}

/*! Set socket of active output pipe
*
* @param[in] h CLIgen handle
* @param[in] s Socket, -1 means not set
* @retval 0 OK
*/
int
cligen_spipe_set(cligen_handle h,
int s)
{
struct cligen_handle *ch = handle(h);

ch->ch_spipe = s;
return 0;
}

/*! Get number of displayed terminal rows.
*
* @param[in] h CLIgen handle
Expand Down
3 changes: 3 additions & 0 deletions cligen_handle.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ int cligen_callback_arguments_set(cligen_handle h, cvec *args);
char *cligen_fn_str_get(cligen_handle h);
int cligen_fn_str_set(cligen_handle h, char *fn_str);

int cligen_spipe_get(cligen_handle h);
int cligen_spipe_set(cligen_handle h, int s);

int cligen_terminal_rows(cligen_handle h);
int cligen_terminal_rows_set(cligen_handle h, int rows);

Expand Down
3 changes: 2 additions & 1 deletion cligen_handle_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ struct cligen_handle{
char *ch_treename_keyword; /* Name of treename parsing keyword */
cg_obj *ch_co_match; /* Matching object in latest evaluation */
cvec *ch_callback_arguments; /* Callback arguments */
char *ch_fn_str; /* Name of active callback function */
char *ch_fn_str; /* Name of active callback function (useful in callbacks) */
int ch_spipe; /* Socket of active output pipe if != -1 (useful in callbacks) */
int ch_completion; /* completion mode */
char *ch_nomatch; /* Why did a string not match an evaluation? */
int ch_tabmode; /* short or long output mode on TAB */
Expand Down
6 changes: 5 additions & 1 deletion cligen_read.c
Original file line number Diff line number Diff line change
Expand Up @@ -1029,6 +1029,8 @@ cligen_eval(cligen_handle h,
continue;
if (cligen_eval_pipe_pre(h, cc, cvv1, &spipe, &childpid) < 0)
goto done;
cligen_spipe_set(h, spipe);
break; /* Just one pipe for now */
}
/* Second round, call regular callbacks */
for (cc = co->co_callbacks; cc; cc=co_callback_next(cc)){
Expand Down Expand Up @@ -1063,8 +1065,10 @@ cligen_eval(cligen_handle h,
}
retval = 0;
done:
if (spipe != -1)
if (spipe != -1){
cligen_spipe_set(h, -1);
close(spipe);
}
if (wh)
free(wh);
if (cvv1)
Expand Down

0 comments on commit fbd6e20

Please sign in to comment.