* cleanup with variable substitution. cleaner replacing function and string variables.
authorWilfried Göesgens <willi@citadel.org>
Tue, 13 Nov 2007 22:21:24 +0000 (22:21 +0000)
committerWilfried Göesgens <willi@citadel.org>
Tue, 13 Nov 2007 22:21:24 +0000 (22:21 +0000)
webcit/context_loop.c
webcit/subst.c
webcit/webcit.h

index f37fa66762f0ba1c5f6a6fe0ba7352e67ab0681e..a74ffb31e3fafd30bdc76092fa7f3069e16098f7 100644 (file)
@@ -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);
index 77c8a370d51cc8a1e122e6a956e88c4b408cb5b7..5d1ad726e72f4be4b0883eb808e31f644015b500 100644 (file)
 /**
  * \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;
 }
 
 
index f9b05e119f68afec620b29a211b54acd27b4d25e..35d9087cdd5ab0b5d2afa06bd6af4076440e861e 100644 (file)
@@ -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)() );