]> andersk Git - test.git/blobdiff - shellinabox/usercss.c
Internet Explorer still doesn't properly support CSS. In particular, it has
[test.git] / shellinabox / usercss.c
index 5ab733a81486c8251c92f332b2e6b94bf5215bea..e94be7440c593f7cc67b077d50213d9a81b5aa1d 100644 (file)
 
 #include "logging/logging.h"
 #include "shellinabox/usercss.h"
+#include "libhttp/hashmap.h"
 
+static struct HashMap *defines;
 
-static void readStylesheet(const char *filename, char **style, size_t *len) {
-  int fd               = open(filename, O_RDONLY);
+static void definesDestructor(void *arg, char *key, char *value) {
+  free(key);
+}
+
+static void readStylesheet(struct UserCSS *userCSS, const char *filename,
+                           char **style, size_t *len) {
+  int fd                  = open(filename, O_RDONLY);
   struct stat st;
   if (fd < 0 || fstat(fd, &st)) {
     fatal("Cannot access style sheet \"%s\"", filename);
   }
   FILE *fp;
-  check(fp             = fdopen(fd, "r"));
-  check(*style         = malloc(st.st_size + 1));
+  check(fp                = fdopen(fd, "r"));
+  check(*style            = malloc(st.st_size + 1));
   check(fread(*style, 1, st.st_size, fp) == st.st_size);
-  (*style)[st.st_size] = '\000';
-  *len                 = st.st_size;
+  (*style)[st.st_size]    = '\000';
+  *len                    = st.st_size;
   fclose(fp);
+  if (!memcmp(*style, "/* DEFINES_", 11)) {
+    char *e               = strchr(*style + 11, ' ');
+    if (e) {
+      if (!defines) {
+        defines           = newHashMap(definesDestructor, NULL);
+      }
+      char *def;
+      check(def           = malloc(e - *style - 2));
+      memcpy(def, *style + 3, e - *style - 3);
+      def[e - *style - 3] = '\000';
+      addToHashMap(defines, def, (char *)userCSS);
+    }
+  }
 }
 
 void initUserCSS(struct UserCSS *userCSS, const char *arg) {
@@ -130,7 +150,8 @@ void initUserCSS(struct UserCSS *userCSS, const char *arg) {
               "active by default");
     }
 
-    readStylesheet(filename + 1, (char **)&userCSS->style, &userCSS->styleLen);
+    readStylesheet(userCSS, filename + 1, (char **)&userCSS->style,
+                   &userCSS->styleLen);
     free(filename);
 
     arg                                   = colon + 1 + filenameLen;
@@ -208,3 +229,10 @@ char *getUserCSSString(struct UserCSS *userCSS) {
   }
   return stringPrintf(s, " ]");
 }
+
+struct UserCSS *userCSSGetDefine(const char *def) {
+  if (!defines) {
+    return NULL;
+  }
+  return (struct UserCSS *)getFromHashMap(defines, def);
+}
This page took 0.060213 seconds and 4 git commands to generate.