]> andersk Git - test.git/blob - shellinabox/usercss.c
Internet Explorer still doesn't properly support CSS. In particular, it has
[test.git] / shellinabox / usercss.c
1 // usercss.c -- Defines user-selectable CSS options
2 // Copyright (C) 2008-2009 Markus Gutschke <markus@shellinabox.com>
3 //
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License version 2 as
6 // published by the Free Software Foundation.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License along
14 // with this program; if not, write to the Free Software Foundation, Inc.,
15 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 //
17 // In addition to these license terms, the author grants the following
18 // additional rights:
19 //
20 // If you modify this program, or any covered work, by linking or
21 // combining it with the OpenSSL project's OpenSSL library (or a
22 // modified version of that library), containing parts covered by the
23 // terms of the OpenSSL or SSLeay licenses, the author
24 // grants you additional permission to convey the resulting work.
25 // Corresponding Source for a non-source form of such a combination
26 // shall include the source code for the parts of OpenSSL used as well
27 // as that of the covered work.
28 //
29 // You may at your option choose to remove this additional permission from
30 // the work, or from any part of it.
31 //
32 // It is possible to build this program in a way that it loads OpenSSL
33 // libraries at run-time. If doing so, the following notices are required
34 // by the OpenSSL and SSLeay licenses:
35 //
36 // This product includes software developed by the OpenSSL Project
37 // for use in the OpenSSL Toolkit. (http://www.openssl.org/)
38 //
39 // This product includes cryptographic software written by Eric Young
40 // (eay@cryptsoft.com)
41 //
42 //
43 // The most up-to-date version of this program is always available from
44 // http://shellinabox.com
45
46 #include "config.h"
47
48 #include <fcntl.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <sys/stat.h>
53 #include <sys/types.h>
54 #include <unistd.h>
55
56 #include "logging/logging.h"
57 #include "shellinabox/usercss.h"
58 #include "libhttp/hashmap.h"
59
60 static struct HashMap *defines;
61
62 static void definesDestructor(void *arg, char *key, char *value) {
63   free(key);
64 }
65
66 static void readStylesheet(struct UserCSS *userCSS, const char *filename,
67                            char **style, size_t *len) {
68   int fd                  = open(filename, O_RDONLY);
69   struct stat st;
70   if (fd < 0 || fstat(fd, &st)) {
71     fatal("Cannot access style sheet \"%s\"", filename);
72   }
73   FILE *fp;
74   check(fp                = fdopen(fd, "r"));
75   check(*style            = malloc(st.st_size + 1));
76   check(fread(*style, 1, st.st_size, fp) == st.st_size);
77   (*style)[st.st_size]    = '\000';
78   *len                    = st.st_size;
79   fclose(fp);
80   if (!memcmp(*style, "/* DEFINES_", 11)) {
81     char *e               = strchr(*style + 11, ' ');
82     if (e) {
83       if (!defines) {
84         defines           = newHashMap(definesDestructor, NULL);
85       }
86       char *def;
87       check(def           = malloc(e - *style - 2));
88       memcpy(def, *style + 3, e - *style - 3);
89       def[e - *style - 3] = '\000';
90       addToHashMap(defines, def, (char *)userCSS);
91     }
92   }
93 }
94
95 void initUserCSS(struct UserCSS *userCSS, const char *arg) {
96   userCSS->newGroup                       = 1;
97
98   int numMembers                          = 1;
99   int hasActiveMember                     = 0;
100   for (;;) {
101     const char *colon                     = strchr(arg, ':');
102     if (!colon) {
103       fatal("Incomplete user CSS definition: \"%s\"", arg);
104     }
105
106     check(userCSS->label                  = malloc(6*(colon - arg) + 1));
107     for (const char *src = arg, *dst = userCSS->label;;) {
108       if (src == colon) {
109         *(char *)dst                      = '\000';
110         break;
111       }
112       char ch                             = *src++;
113       if (ch == '<') {
114         memcpy((char *)dst, "&lt;", 4);
115         dst                              += 4;
116       } else if (ch == '&') {
117         memcpy((char *)dst, "&amp;", 5);
118         dst                              += 5;
119       } else if (ch == '\'') {
120         memcpy((char *)dst, "&apos;", 6);
121         dst                              += 6;
122       } else if (ch == '"') {
123         memcpy((char *)dst, "&quot;", 6);
124         dst                              += 6;
125       } else {
126         *(char *)dst++                    = ch;
127       }
128     }
129
130     int filenameLen                       = strcspn(colon + 1, ",;");
131     char *filename;
132     check(filename                        = malloc(filenameLen + 1));
133     memcpy(filename, colon + 1, filenameLen);
134     filename[filenameLen]                 = '\000';
135
136     switch (*filename) {
137       case '-':
138         userCSS->isActivated              = 0;
139         break;
140       case '+':
141         if (hasActiveMember) {
142           fatal("There can only be one active style option per group. Maybe "
143                 "use ';' instead of ',' to start a new group.");
144         }
145         hasActiveMember                   = 1;
146         userCSS->isActivated              = 1;
147         break;
148       default:
149         fatal("Must indicate with '+' or '-' whether the style option is "
150               "active by default");
151     }
152
153     readStylesheet(userCSS, filename + 1, (char **)&userCSS->style,
154                    &userCSS->styleLen);
155     free(filename);
156
157     arg                                   = colon + 1 + filenameLen;
158     if (!*arg) {
159       userCSS->next                       = NULL;
160       break;
161     }
162     check(userCSS->next                   = malloc(sizeof(struct UserCSS)));
163     userCSS                               = userCSS->next;
164     userCSS->newGroup                     = *arg++ == ';';
165     if (userCSS->newGroup) {
166       if (!hasActiveMember && numMembers > 1) {
167         // Print error message
168         break;
169       }
170       numMembers                          = 1;
171       hasActiveMember                     = 0;
172     } else {
173       ++numMembers;
174     }
175   }
176   if (!hasActiveMember && numMembers > 1) {
177     fatal("Each group of style options must have exactly one style that is "
178           "active by\n"
179           "default.");
180   }
181 }
182
183 struct UserCSS *newUserCSS(const char *arg) {
184   struct UserCSS *userCSS;
185   check(userCSS = malloc(sizeof(struct UserCSS)));
186   initUserCSS(userCSS, arg);
187   return userCSS;
188 }
189
190 void parseUserCSS(struct UserCSS **userCSSList, const char *arg) {
191   while (*userCSSList) {
192     userCSSList = &(*userCSSList)->next;
193   }
194   *userCSSList  = newUserCSS(arg);
195 }
196
197 void destroyUserCSS(struct UserCSS *userCSS) {
198   if (userCSS) {
199     free((void *)userCSS->label);
200     userCSS->label         = NULL;
201     free((void *)userCSS->style);
202     userCSS->style         = NULL;
203     userCSS->styleLen      = -1;
204     for (struct UserCSS *child = userCSS->next; child; ) {
205       struct UserCSS *next = child->next;
206       free((void *)child->label);
207       free((void *)child->style);
208       free(child);
209       child                = next;
210     }
211     userCSS->next          = NULL;
212   }
213 }
214
215 void deleteUserCSS(struct UserCSS *userCSS) {
216   destroyUserCSS(userCSS);
217   free(userCSS);
218 }
219
220 char *getUserCSSString(struct UserCSS *userCSS) {
221   char *s   = stringPrintf(NULL, "[ ");
222   while (userCSS) {
223     s       = stringPrintf(s, "[ '%s', %s, %s ]%s",
224                            userCSS->label,
225                            userCSS->newGroup    ? "true" : "false",
226                            userCSS->isActivated ? "true" : "false",
227                            userCSS->next        ? ", "   : "");
228     userCSS = userCSS->next;
229   }
230   return stringPrintf(s, " ]");
231 }
232
233 struct UserCSS *userCSSGetDefine(const char *def) {
234   if (!defines) {
235     return NULL;
236   }
237   return (struct UserCSS *)getFromHashMap(defines, def);
238 }
This page took 0.077439 seconds and 5 git commands to generate.