]> code.citadel.org Git - citadel.git/blobdiff - webcit/subst.c
* migrated SUBST stuff to hash
[citadel.git] / webcit / subst.c
index 083f8e4e03719c48ebfdbad99aac56b9fbc35922..e98001904489bad3dd50f0f5af971926b23c70cb 100644 (file)
 /*
  * $Id$
- *
- * Variable substitution type stuff
- *
+ */
+/**
+ * \defgroup Subst Variable substitution type stuff
+ * \ingroup CitadelConfig
  */
 
+/*@{*/
 
-
-#include <ctype.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <stdio.h>
-#include <fcntl.h>
-#include <signal.h>
+#include "sysdep.h"
 #include <sys/types.h>
-#include <sys/wait.h>
-#include <sys/socket.h>
-#include <sys/time.h>
 #include <sys/stat.h>
-#include <limits.h>
-#include <netinet/in.h>
-#include <netdb.h>
-#include <string.h>
-#include <pwd.h>
-#include <errno.h>
-#include <stdarg.h>
-#include <pthread.h>
-#include <signal.h>
-#include "webcit.h"
-
-struct wcsubst *global_subst = NULL;
+#include <unistd.h>
 
+#include "webcit.h"
+#include "webserver.h"
 
-/*
- * Clear out the list of substitution variables local to this session
+/**
+ * \brief debugging function to print array to log
  */
-void clear_local_substs(void) {
-       struct wcsubst *ptr;
+void VarPrintTransition(void *vVar1, void *vVar2, int odd){}
+/**
+ * \brief debugging function to print array to log
+ */
+void VarPrintEntry(const char *Key, void *vSubst, int odd)
+{
+       wcsubst *ptr;
+       lprintf(1,"Subst[%s] : ", Key);
+       ptr = (wcsubst*) vSubst;
+
+       switch(ptr->wcs_type) {
+       case WCS_STRING:
+               lprintf(1, "  -> %s\n", ptr->wcs_value);
+               break;
+       case WCS_SERVCMD:
+               lprintf(1, "  -> Server [%s]\n", ptr->wcs_value);
+               break;
+       case WCS_FUNCTION:
+               lprintf(1, "  -> function at [%0xd]\n", ptr->wcs_function);
+               break;
+       default:
+               lprintf(1,"  WARNING: invalid type: [%ld]!\n", ptr->wcs_type);
+       }
+}
 
-       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);
-               }
 
-               free(WC->vars);
-               WC->vars = ptr;
+/**
+ * \brief Clear out the list of substitution variables local to this session
+ */
+void clear_substs(struct wcsession *wc) {
+
+       if (wc->vars != NULL) {
+       
+               DeleteHash(&wc->vars);
        }
 }
 
+/**
+ * \brief Clear out the list of substitution variables local to this session
+ */
+void clear_local_substs(void) {
+       clear_substs (WC);
+}
 
-/*
- * Add a substitution variable (local to this session)
+/**
+ * \brief destructor; kill one entry.
+ */
+void deletevar(void *data)
+{
+       wcsubst *ptr = (wcsubst*)data;
+//             if ((wc->vars->wcs_type == WCS_STRING)
+//                || (wc->vars->wcs_type == WCS_SERVCMD)) {
+       if (ptr->wcs_type != WCS_FUNCTION)
+               free(ptr->wcs_value);
+       free(ptr);      
+}
+
+/**
+ * \brief Add a substitution variable (local to this session) (strlen version...)
+ * \param keyname the replacementstring to substitute
+ * \param keytype the kind of the key
+ * \param format the format string ala printf
+ * \param ... the arguments to substitute in the formatstring
  */
-void svprintf(char *keyname, int keytype, const char *format,...)
+void SVPRINTF(char *keyname, int keytype, const char *format,...)
 {
        va_list arg_ptr;
-       char wbuf[1024];
-       struct wcsubst *ptr = NULL;
-       struct wcsubst *scan;
+       char wbuf[SIZ];
+       void *vPtr;
+       wcsubst *ptr = NULL;
+       size_t keylen;
+       struct wcsession *WCC = WC;
+       
+       keylen = strlen(keyname);
+       /**
+        * First look if we're doing a replacement of
+        * an existing key
+        */
+       /*PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
+       if (GetHash(WCC->vars, keyname, keylen, &vPtr)) {
+               ptr = (wcsubst*)vPtr;
+               if (ptr->wcs_value != NULL)
+                       free(ptr->wcs_value);
+       }
+       else    /** Otherwise allocate a new one */
+       {
+               ptr = (wcsubst *) malloc(sizeof(wcsubst));
+               safestrncpy(ptr->wcs_key, keyname, sizeof ptr->wcs_key);
+               Put(WCC->vars, keyname, keylen, ptr,  deletevar);
+       }
+
+       /** Format the string and save it */
 
        va_start(arg_ptr, format);
-       vsprintf(wbuf, format, arg_ptr);
+       vsnprintf(wbuf, sizeof wbuf, format, arg_ptr);
        va_end(arg_ptr);
 
-       /* First scan through to see if we're doing a replacement of
+       ptr->wcs_function = NULL;
+       ptr->wcs_type = keytype;
+       ptr->wcs_value = strdup(wbuf);
+}
+
+/**
+ * \brief Add a substitution variable (local to this session)
+ * \param keyname the replacementstring to substitute
+ * \param keytype the kind of the key
+ * \param format the format string ala printf
+ * \param ... the arguments to substitute in the formatstring
+ */
+void svprintf(char *keyname, size_t keylen, int keytype, const char *format,...)
+{
+       va_list arg_ptr;
+       char wbuf[SIZ];
+       void *vPtr;
+       wcsubst *ptr = NULL;
+       struct wcsession *WCC = WC;
+       size_t len;
+       
+       /**
+        * First look if we're doing a replacement of
         * an existing key
         */
-       for (scan=WC->vars; scan!=NULL; scan=scan->next) {
-               if (!strcasecmp(scan->wcs_key, keyname)) {
-                       ptr = scan;
+       /*PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
+       if (GetHash(WCC->vars, keyname, keylen, &vPtr)) {
+               ptr = (wcsubst*)vPtr;
+               if (ptr->wcs_value != NULL)
                        free(ptr->wcs_value);
-               }
+       }
+       else    /** Otherwise allocate a new one */
+       {
+               ptr = (wcsubst *) malloc(sizeof(wcsubst));
+               safestrncpy(ptr->wcs_key, keyname, sizeof ptr->wcs_key);
+               Put(WCC->vars, keyname, keylen, ptr,  deletevar);
        }
 
-       /* Otherwise allocate a new one */
-       if (ptr == NULL) {
-               ptr = (struct wcsubst *) malloc(sizeof(struct wcsubst));
-               ptr->next = WC->vars;
+       /** Format the string and save it */
+
+       va_start(arg_ptr, format);
+       len = vsnprintf(wbuf, sizeof wbuf, format, arg_ptr);
+       va_end(arg_ptr);
+
+       ptr->wcs_value = (char*) malloc(len + 1);
+       memcpy(ptr->wcs_value, wbuf, len + 1);
+       ptr->wcs_function = NULL;
+       ptr->wcs_type = keytype;
+}
+
+/**
+ * \brief Add a substitution variable (local to this session)
+ * \param keyname the replacementstring to substitute
+ * \param keytype the kind of the key
+ * \param format the format string ala printf
+ * \param ... the arguments to substitute in the formatstring
+ */
+void SVPut(char *keyname, size_t keylen, int keytype, char *Data)
+{
+       void *vPtr;
+       wcsubst *ptr = NULL;
+       struct wcsession *WCC = WC;
+
+       
+       /**
+        * First look if we're doing a replacement of
+        * an existing key
+        */
+       /*PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
+       if (GetHash(WCC->vars, keyname, keylen, &vPtr)) {
+               ptr = (wcsubst*)vPtr;
+               if (ptr->wcs_value != NULL)
+                       free(ptr->wcs_value);
+       }
+       else    /** Otherwise allocate a new one */
+       {
+               ptr = (wcsubst *) malloc(sizeof(wcsubst));
+               safestrncpy(ptr->wcs_key, keyname, sizeof ptr->wcs_key);
+               Put(WCC->vars, keyname, keylen, ptr,  deletevar);
        }
 
+       ptr->wcs_function = NULL;
        ptr->wcs_type = keytype;
-       strcpy(ptr->wcs_key, keyname);
-       ptr->wcs_value = malloc(strlen(wbuf)+1);
-       strcpy(ptr->wcs_value, wbuf);
-       WC->vars = ptr;
+       ptr->wcs_value = strdup(Data);
 }
 
-/*
- * Add a substitution variable (local to this session) that does a callback
+/**
+ * \brief Add a substitution variable (local to this session) that does a callback
+ * \param keyname the keystring to substitute
+ * \param fcn_ptr the function callback to give the substitution string
  */
-void svcallback(char *keyname, void (*fcn_ptr)() )
+void SVCallback(char *keyname, size_t keylen, var_callback_fptr fcn_ptr)
 {
-       struct wcsubst *ptr;
+       wcsubst *ptr;
+       void *vPtr;
+       struct wcsession *WCC = WC;
 
-       ptr = (struct wcsubst *) malloc(sizeof(struct wcsubst));
-       ptr->next = WC->vars;
+       /**
+        * First look if we're doing a replacement of
+        * an existing key
+        */
+       /*PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
+       if (GetHash(WCC->vars, keyname, keylen, &vPtr)) {
+               ptr = (wcsubst*)vPtr;
+               if (ptr->wcs_value != NULL)
+                       free(ptr->wcs_value);
+       }
+       else    /** Otherwise allocate a new one */
+       {
+               ptr = (wcsubst *) malloc(sizeof(wcsubst));
+               safestrncpy(ptr->wcs_key, keyname, sizeof ptr->wcs_key);
+               Put(WCC->vars, keyname, keylen, ptr,  deletevar);
+       }
+
+       ptr->wcs_value = NULL;
        ptr->wcs_type = WCS_FUNCTION;
-       strcpy(ptr->wcs_key, keyname);
        ptr->wcs_function = fcn_ptr;
-       WC->vars = ptr;
+}
+inline void SVCALLBACK(char *keyname, var_callback_fptr fcn_ptr)
+{
+       SVCallback(keyname, strlen(keyname), fcn_ptr);
 }
 
 
 
-/*
- * back end for print_value_of() ... does a server command
+/**
+ * \brief back end for print_value_of() ... does a server command
+ * \param servcmd server command to execute on the citadel server
  */
 void pvo_do_cmd(char *servcmd) {
        char buf[SIZ];
 
        serv_puts(servcmd);
-       serv_gets(buf);
+       serv_getln(buf, sizeof buf);
 
        switch(buf[0]) {
                case '2':
@@ -122,7 +259,7 @@ void pvo_do_cmd(char *servcmd) {
                        wprintf("%s\n", &buf[4]);
                        break;
                case '1':
-                       fmout(NULL);
+                       fmout("CENTER");
                        break;
                case '4':
                        wprintf("%s\n", &buf[4]);
@@ -131,33 +268,55 @@ void pvo_do_cmd(char *servcmd) {
        }
 }
 
-
-
-/*
- * Print the value of a variable
+/**
+ * \brief Print the value of a variable
+ * \param keyname get a key to print
  */
-void print_value_of(char *keyname) {
-       struct wcsubst *ptr;
+void print_value_of(char *keyname, size_t keylen) {
+       struct wcsession *WCC = WC;
+       wcsubst *ptr;
        void *fcn();
+       void *vVar;
 
-       if (!strcasecmp(keyname, "SERV_PID")) {
-               wprintf("%d", serv_info.serv_pid);
+       /*if (WCC->vars != NULL) PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
+       if (keyname[0] == '=') {
+               do_template(&keyname[1]);
+       }
+       /** Page-local variables */
+       if ((WCC->vars!= NULL) && GetHash(WCC->vars, keyname, keylen, &vVar)) {
+               ptr = (wcsubst*) vVar;
+               switch(ptr->wcs_type) {
+               case WCS_STRING:
+                       wprintf("%s", ptr->wcs_value);
+                       break;
+               case WCS_SERVCMD:
+                       pvo_do_cmd(ptr->wcs_value);
+                       break;
+               case WCS_FUNCTION:
+                       (*ptr->wcs_function) ();
+                       break;
+               default:
+                       lprintf(1,"WARNING: invalid value in SV-Hash at %s!", keyname);
+               }
+       }
+       else if (!strcasecmp(keyname, "SERV_PID")) {
+               wprintf("%d", WCC->ctdl_pid);
        }
 
        else if (!strcasecmp(keyname, "SERV_NODENAME")) {
-               wprintf("%s", serv_info.serv_nodename);
+               escputs(serv_info.serv_nodename);
        }
 
        else if (!strcasecmp(keyname, "SERV_HUMANNODE")) {
-               wprintf("%s", serv_info.serv_humannode);
+               escputs(serv_info.serv_humannode);
        }
 
        else if (!strcasecmp(keyname, "SERV_FQDN")) {
-               wprintf("%s", serv_info.serv_fqdn);
+               escputs(serv_info.serv_fqdn);
        }
 
        else if (!strcasecmp(keyname, "SERV_SOFTWARE")) {
-               wprintf("%s", serv_info.serv_software);
+               escputs(serv_info.serv_software);
        }
 
        else if (!strcasecmp(keyname, "SERV_REV_LEVEL")) {
@@ -168,49 +327,53 @@ void print_value_of(char *keyname) {
        }
 
        else if (!strcasecmp(keyname, "SERV_BBS_CITY")) {
-               wprintf("%s", serv_info.serv_bbs_city);
+               escputs(serv_info.serv_bbs_city);
        }
 
-       /* Page-local variables */
-       else for (ptr = WC->vars; ptr != NULL; ptr = ptr->next) {
-               if (!strcasecmp(ptr->wcs_key, keyname)) {
-                       if (ptr->wcs_type == WCS_STRING) {
-                               wprintf("%s", ptr->wcs_value);
-                       }
-                       else if (ptr->wcs_type == WCS_SERVCMD) {
-                               pvo_do_cmd(ptr->wcs_value);
-                       }
-                       else if (ptr->wcs_type == WCS_FUNCTION) {
-                               (*ptr->wcs_function) ();
-                       }
-               }
+       else if (!strcasecmp(keyname, "CURRENT_USER")) {
+               escputs(WCC->wc_fullname);
        }
-}
 
+       else if (!strcasecmp(keyname, "CURRENT_ROOM")) {
+               escputs(WCC->wc_roomname);
+       }
 
+}
 
-/*
- * Display a variable-substituted template
+extern char *static_dirs[PATH_MAX];  /**< Disk representation */
+
+/**
+ * \brief Display a variable-substituted template
+ * \param templatename template file to load
  */
 void do_template(void *templatename) {
+       char flat_filename[PATH_MAX];
        char filename[PATH_MAX];
        FILE *fp;
        char inbuf[1024];
        char outbuf[sizeof inbuf];
        char key[sizeof inbuf];
        int i, pos;
+       struct stat mystat;
 
-       strcpy(filename, "static/");
-       strcat(filename, templatename);
+       strcpy(flat_filename, templatename);
        if (WC->is_wap)
-               strcat(filename, ".wml");
+               strcat(flat_filename, ".wml");
        else
-               strcat(filename, ".html");
+               strcat(flat_filename, ".html");
        
+       strcpy(filename, static_dirs[1]);
+       strcat(filename, flat_filename);
+       if (stat(filename, &mystat) == -1)
+       {
+               strcpy(filename, static_dirs[0]);
+               strcat(filename, flat_filename);
+       }
+
        fp = fopen(filename, "r");
        if (fp == NULL) {
-               wprintf("<BLINK>ERROR</BLINK> - could not open template ");
-               wprintf("'%s' - %s<BR>\n",
+               wprintf(_("ERROR: could not open template "));
+               wprintf("'%s' - %s<br />\n",
                        templatename, strerror(errno));
                return;
        }
@@ -218,33 +381,43 @@ void do_template(void *templatename) {
        strcpy(inbuf, "");
 
        while (fgets(inbuf, sizeof inbuf, fp) != NULL) {
-               strcpy(outbuf, "");
+               int len;
 
-               while (strlen(inbuf) > 0) {
+               strcpy(outbuf, "");
+               len = strlen(inbuf);
+               while (len > 0) {
                        pos = (-1);
-                       for (i=strlen(inbuf); i>=0; --i) {
+                       for (i=len; i>=0; --i) {
                                if ((inbuf[i]=='<')&&(inbuf[i+1]=='?')) pos = i;
                        }
                        if (pos < 0) {
                                wprintf("%s", inbuf);
                                strcpy(inbuf, "");
+                               len = 0;
                        }
                        else {
                                strncpy(outbuf, inbuf, pos);
                                outbuf[pos] = 0;
                                wprintf("%s", outbuf);
-                               strcpy(inbuf, &inbuf[pos]);
+                               memmove(inbuf, &inbuf[pos], len - pos +1);
+                               len -= pos;
                                pos = 1;
-                               for (i=strlen(inbuf); i>=0; --i) {
+                               for (i=len; i>=0; --i) {
                                        if (inbuf[i]=='>') pos = i;
                                }
                                strncpy(key, &inbuf[2], pos-2);
                                key[pos-2] = 0;
-                               print_value_of(key);
-                               strcpy(inbuf, &inbuf[pos+1]);
+                               print_value_of(key, pos-2);
+                               pos++;
+                               memmove(inbuf, &inbuf[pos], len - pos + 1);
+                               len -= pos;
                        }
                }
        }
 
        fclose(fp);
 }
+
+
+
+/*@}*/