]> code.citadel.org Git - citadel.git/blobdiff - webcit/subst.c
* calculate dirs right
[citadel.git] / webcit / subst.c
index 43593069811c85863792511d51f8cdcf05ffca37..77c8a370d51cc8a1e122e6a956e88c4b408cb5b7 100644 (file)
@@ -8,8 +8,12 @@
 
 /*@{*/
 
-#include "webcit.h"
+#include "sysdep.h"
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
 
+#include "webcit.h"
 
 /**
  * \brief Clear out the list of substitution variables local to this session
@@ -190,27 +194,36 @@ void print_value_of(char *keyname) {
        }
 }
 
-
+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(_("ERROR: could not open template "));
@@ -222,30 +235,36 @@ 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]);
+                               pos++;
+                               memmove(inbuf, &inbuf[pos], len - pos + 1);
+                               len -= pos;
                        }
                }
        }