From 56cbf1afa3230653826c4e19b942ebccf330ab0b Mon Sep 17 00:00:00 2001 From: =?utf8?q?Wilfried=20G=C3=B6esgens?= Date: Tue, 13 Nov 2007 22:21:24 +0000 Subject: [PATCH] * cleanup with variable substitution. cleaner replacing function and string variables. --- webcit/context_loop.c | 1 + webcit/subst.c | 57 +++++++++++++++++++++++++++++++------------ webcit/webcit.h | 1 + 3 files changed, 44 insertions(+), 15 deletions(-) diff --git a/webcit/context_loop.c b/webcit/context_loop.c index f37fa6676..a74ffb31e 100644 --- a/webcit/context_loop.c +++ b/webcit/context_loop.c @@ -106,6 +106,7 @@ void do_housekeeping(void) } free_attachments(sessions_to_kill); free_march_list(sessions_to_kill); + clear_substs(sessions_to_kill); pthread_mutex_unlock(&sessions_to_kill->SessionMutex); sptr = sessions_to_kill->next; free(sessions_to_kill); diff --git a/webcit/subst.c b/webcit/subst.c index 77c8a370d..5d1ad726e 100644 --- a/webcit/subst.c +++ b/webcit/subst.c @@ -18,22 +18,29 @@ /** * \brief Clear out the list of substitution variables local to this session */ -void clear_local_substs(void) { +void clear_substs(struct wcsession *wc) { struct wcsubst *ptr; - while (WC->vars != NULL) { - ptr = WC->vars->next; + while (wc->vars != NULL) { + ptr = wc->vars->next; - if ((WC->vars->wcs_type == WCS_STRING) - || (WC->vars->wcs_type == WCS_SERVCMD)) { - free(WC->vars->wcs_value); + if ((wc->vars->wcs_type == WCS_STRING) + || (wc->vars->wcs_type == WCS_SERVCMD)) { + free(wc->vars->wcs_value); } - free(WC->vars); - WC->vars = ptr; + free(wc->vars); + wc->vars = ptr; } - WC->vars = NULL; + wc->vars = NULL; +} + +/** + * \brief Clear out the list of substitution variables local to this session + */ +void clear_local_substs(void) { + clear_substs (WC); } @@ -58,15 +65,16 @@ void svprintf(char *keyname, int keytype, const char *format,...) for (scan=WC->vars; scan!=NULL; scan=scan->next) { if (!strcasecmp(scan->wcs_key, keyname)) { ptr = scan; - free(ptr->wcs_value); + if (ptr->wcs_value != NULL) + free(ptr->wcs_value); } } /** Otherwise allocate a new one */ if (ptr == NULL) { ptr = (struct wcsubst *) malloc(sizeof(struct wcsubst)); - ptr->next = WC->vars; safestrncpy(ptr->wcs_key, keyname, sizeof ptr->wcs_key); + ptr->next = WC->vars; WC->vars = ptr; } @@ -76,6 +84,7 @@ void svprintf(char *keyname, int keytype, const char *format,...) vsnprintf(wbuf, sizeof wbuf, format, arg_ptr); va_end(arg_ptr); + ptr->wcs_function = NULL; ptr->wcs_type = keytype; ptr->wcs_value = strdup(wbuf); } @@ -87,14 +96,32 @@ void svprintf(char *keyname, int keytype, const char *format,...) */ void svcallback(char *keyname, void (*fcn_ptr)() ) { + struct wcsubst *scan; struct wcsubst *ptr; - ptr = (struct wcsubst *) malloc(sizeof(struct wcsubst)); - ptr->next = WC->vars; + /** + * First scan through to see if we're doing a replacement of + * an existing key + */ + ptr = NULL; + for (scan=WC->vars; scan!=NULL; scan=scan->next) { + if (!strcasecmp(scan->wcs_key, keyname)) { + ptr = scan; + if (ptr->wcs_value != NULL) + free(ptr->wcs_value); + } + } + + /** Otherwise allocate a new one */ + if (ptr == NULL) { + ptr = (struct wcsubst *) malloc(sizeof(struct wcsubst)); + safestrncpy(ptr->wcs_key, keyname, sizeof ptr->wcs_key); + ptr->next = WC->vars; + WC->vars = ptr; + } + ptr->wcs_value = NULL; ptr->wcs_type = WCS_FUNCTION; - strcpy(ptr->wcs_key, keyname); ptr->wcs_function = fcn_ptr; - WC->vars = ptr; } diff --git a/webcit/webcit.h b/webcit/webcit.h index f9b05e119..35d9087cd 100644 --- a/webcit/webcit.h +++ b/webcit/webcit.h @@ -591,6 +591,7 @@ void page_popup(void); void chat_recv(void); void chat_send(void); void http_redirect(char *); +void clear_substs(struct wcsession *wc); void clear_local_substs(void); void svprintf(char *keyname, int keytype, const char *format,...); void svcallback(char *keyname, void (*fcn_ptr)() ); -- 2.39.2