From fbd6e20ae50852dc89429b299ea1bbc3b84dc73a Mon Sep 17 00:00:00 2001 From: Olof hagsand Date: Mon, 9 Dec 2024 22:49:08 +0100 Subject: [PATCH] Add socket pipe interface to keep track of when a function is called in a pipe --- cligen_handle.c | 37 +++++++++++++++++++++++++++++++++++-- cligen_handle.h | 3 +++ cligen_handle_internal.h | 3 ++- cligen_read.c | 6 +++++- 4 files changed, 45 insertions(+), 4 deletions(-) diff --git a/cligen_handle.c b/cligen_handle.c index 0183332..4835946 100644 --- a/cligen_handle.c +++ b/cligen_handle.c @@ -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 */ @@ -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) @@ -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, @@ -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 diff --git a/cligen_handle.h b/cligen_handle.h index a77338c..b09afd3 100644 --- a/cligen_handle.h +++ b/cligen_handle.h @@ -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); diff --git a/cligen_handle_internal.h b/cligen_handle_internal.h index 6ffff14..909006a 100644 --- a/cligen_handle_internal.h +++ b/cligen_handle_internal.h @@ -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 */ diff --git a/cligen_read.c b/cligen_read.c index b139cb5..ee6a5a6 100644 --- a/cligen_read.c +++ b/cligen_read.c @@ -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)){ @@ -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)