1 // usercss.c -- Defines user-selectable CSS options
2 // Copyright (C) 2008-2009 Markus Gutschke <markus@shellinabox.com>
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.
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.
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.
17 // In addition to these license terms, the author grants the following
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.
29 // You may at your option choose to remove this additional permission from
30 // the work, or from any part of it.
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:
36 // This product includes software developed by the OpenSSL Project
37 // for use in the OpenSSL Toolkit. (http://www.openssl.org/)
39 // This product includes cryptographic software written by Eric Young
40 // (eay@cryptsoft.com)
43 // The most up-to-date version of this program is always available from
44 // http://shellinabox.com
53 #include <sys/types.h>
56 #include "logging/logging.h"
57 #include "shellinabox/usercss.h"
58 #include "libhttp/hashmap.h"
60 static struct HashMap *defines;
62 static void definesDestructor(void *arg, char *key, char *value) {
66 static void readStylesheet(struct UserCSS *userCSS, const char *filename,
67 char **style, size_t *len) {
68 int fd = open(filename, O_RDONLY);
70 if (fd < 0 || fstat(fd, &st)) {
71 fatal("Cannot access style sheet \"%s\"", filename);
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';
80 if (!memcmp(*style, "/* DEFINES_", 11)) {
81 char *e = strchr(*style + 11, ' ');
84 defines = newHashMap(definesDestructor, NULL);
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);
95 void initUserCSS(struct UserCSS *userCSS, const char *arg) {
96 userCSS->newGroup = 1;
99 int hasActiveMember = 0;
101 const char *colon = strchr(arg, ':');
103 fatal("Incomplete user CSS definition: \"%s\"", arg);
106 check(userCSS->label = malloc(6*(colon - arg) + 1));
107 for (const char *src = arg, *dst = userCSS->label;;) {
109 *(char *)dst = '\000';
114 memcpy((char *)dst, "<", 4);
116 } else if (ch == '&') {
117 memcpy((char *)dst, "&", 5);
119 } else if (ch == '\'') {
120 memcpy((char *)dst, "'", 6);
122 } else if (ch == '"') {
123 memcpy((char *)dst, """, 6);
130 int filenameLen = strcspn(colon + 1, ",;");
132 check(filename = malloc(filenameLen + 1));
133 memcpy(filename, colon + 1, filenameLen);
134 filename[filenameLen] = '\000';
138 userCSS->isActivated = 0;
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.");
146 userCSS->isActivated = 1;
149 fatal("Must indicate with '+' or '-' whether the style option is "
150 "active by default");
153 readStylesheet(userCSS, filename + 1, (char **)&userCSS->style,
157 arg = colon + 1 + filenameLen;
159 userCSS->next = NULL;
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
176 if (!hasActiveMember && numMembers > 1) {
177 fatal("Each group of style options must have exactly one style that is "
183 struct UserCSS *newUserCSS(const char *arg) {
184 struct UserCSS *userCSS;
185 check(userCSS = malloc(sizeof(struct UserCSS)));
186 initUserCSS(userCSS, arg);
190 void parseUserCSS(struct UserCSS **userCSSList, const char *arg) {
191 while (*userCSSList) {
192 userCSSList = &(*userCSSList)->next;
194 *userCSSList = newUserCSS(arg);
197 void destroyUserCSS(struct UserCSS *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);
211 userCSS->next = NULL;
215 void deleteUserCSS(struct UserCSS *userCSS) {
216 destroyUserCSS(userCSS);
220 char *getUserCSSString(struct UserCSS *userCSS) {
221 char *s = stringPrintf(NULL, "[ ");
223 s = stringPrintf(s, "[ '%s', %s, %s ]%s",
225 userCSS->newGroup ? "true" : "false",
226 userCSS->isActivated ? "true" : "false",
227 userCSS->next ? ", " : "");
228 userCSS = userCSS->next;
230 return stringPrintf(s, " ]");
233 struct UserCSS *userCSSGetDefine(const char *def) {
237 return (struct UserCSS *)getFromHashMap(defines, def);