Done with doxygenizing
[citadel.git] / webcit / subst.c
index 095e414a169769cff408e12d16ff40b2d68a877f..6bcaf0fb395741e7d138e7cd78fe569f5d36852e 100644 (file)
@@ -1,39 +1,18 @@
 /*
  * $Id$
- *
- * Variable substitution type stuff
+ */
+/**
+ * \defgroup Subst Variable substitution type stuff
  *
  */
 
+/*@{*/
 
-
-#include <ctype.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <stdio.h>
-#include <fcntl.h>
-#include <signal.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;
 
-
-/*
- * Clear out the list of substitution variables local to this session
+/**
+ * \brief Clear out the list of substitution variables local to this session
  */
 void clear_local_substs(void) {
        struct wcsubst *ptr;
@@ -49,33 +28,58 @@ void clear_local_substs(void) {
                free(WC->vars);
                WC->vars = ptr;
        }
+
+       WC->vars = NULL;
 }
 
 
 /*
- * Add a substitution variable (local to this session)
+ * \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, int keytype, const char *format,...)
 {
        va_list arg_ptr;
-       char wbuf[1024];
-       struct wcsubst *ptr;
+       char wbuf[SIZ];
+       struct wcsubst *ptr = NULL;
+       struct wcsubst *scan;
+
+       /**
+        * First scan through to see 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;
+                       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);
+               WC->vars = ptr;
+       }
+
+       /** 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);
 
-       ptr = (struct wcsubst *) malloc(sizeof(struct wcsubst));
-       ptr->next = WC->vars;
        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(wbuf);
 }
 
-/*
- * 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)() )
 {
@@ -91,14 +95,15 @@ void svcallback(char *keyname, void (*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':
@@ -107,7 +112,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]);
@@ -118,14 +123,59 @@ 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 *fcn();
 
-       for (ptr = WC->vars; ptr != NULL; ptr = ptr->next) {
+       if (keyname[0] == '=') {
+               do_template(&keyname[1]);
+       }
+
+       if (!strcasecmp(keyname, "SERV_PID")) {
+               wprintf("%d", WC->ctdl_pid);
+       }
+
+       else if (!strcasecmp(keyname, "SERV_NODENAME")) {
+               escputs(serv_info.serv_nodename);
+       }
+
+       else if (!strcasecmp(keyname, "SERV_HUMANNODE")) {
+               escputs(serv_info.serv_humannode);
+       }
+
+       else if (!strcasecmp(keyname, "SERV_FQDN")) {
+               escputs(serv_info.serv_fqdn);
+       }
+
+       else if (!strcasecmp(keyname, "SERV_SOFTWARE")) {
+               escputs(serv_info.serv_software);
+       }
+
+       else if (!strcasecmp(keyname, "SERV_REV_LEVEL")) {
+               wprintf("%d.%02d",
+                       serv_info.serv_rev_level / 100,
+                       serv_info.serv_rev_level % 100
+               );
+       }
+
+       else if (!strcasecmp(keyname, "SERV_BBS_CITY")) {
+               escputs(serv_info.serv_bbs_city);
+       }
+
+       else if (!strcasecmp(keyname, "CURRENT_USER")) {
+               escputs(WC->wc_fullname);
+       }
+
+       else if (!strcasecmp(keyname, "CURRENT_ROOM")) {
+               escputs(WC->wc_roomname);
+       }
+
+       /** 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);
@@ -142,8 +192,9 @@ void print_value_of(char *keyname) {
 
 
 
-/*
- * Display a variable-substituted template
+/**
+ * \brief Display a variable-substituted template
+ * \param templatename template file to load
  */
 void do_template(void *templatename) {
        char filename[PATH_MAX];
@@ -162,8 +213,8 @@ void do_template(void *templatename) {
        
        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;
        }
@@ -201,3 +252,7 @@ void do_template(void *templatename) {
 
        fclose(fp);
 }
+
+
+
+/*@}*/