source: trunk/webif/pages_gen.c@ 8445

Last change on this file since 8445 was 8445, checked in by gf, 9 years ago

webif: Do not store images in templates array as base64.

Save a little bit of binary size.

This commit introduces no user visible changes.

File size: 10.2 KB
Line 
1/*
2 * OSCam WebIf pages generator
3 * Copyright (C) 2013 Unix Solutions Ltd.
4 *
5 * Authors: Georgi Chorbadzhiyski (gf@unixsol.org)
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20#include <ctype.h>
21#include <stdio.h>
22#include <stdarg.h>
23#include <stdlib.h>
24#include <stdbool.h>
25#include <string.h>
26#include <errno.h>
27#include <sys/types.h>
28#include <sys/stat.h>
29#include <unistd.h>
30#include <fcntl.h>
31#include <inttypes.h>
32#include <libgen.h>
33
34#define MAX_TEMPLATES 256
35static char *index_filename = "pages_index.txt";
36static char *output_pages_c = "pages.c";
37static char *output_pages_h = "pages.h";
38
39struct templates {
40 unsigned int num;
41 struct {
42 char ident[64];
43 char file[128];
44 char deps[256];
45 uint32_t data_len;
46 enum { TXT, BIN } type;
47 uint8_t mime_type;
48 } data[MAX_TEMPLATES];
49};
50
51static struct templates templates;
52static FILE *output_file;
53
54static void die(const char * s, ...) {
55 va_list args;
56 va_start(args, s);
57 fprintf(stderr, "ERROR: ");
58 vfprintf(stderr, s, args);
59 if (s[strlen(s) - 1] != '\n')
60 fprintf(stderr, "\n");
61 va_end(args);
62 exit(EXIT_FAILURE);
63}
64
65static FILE *xfopen(char *filename, char *mode) {
66 FILE *fh = fopen(filename, mode);
67 if (!fh)
68 die("fopen(%s, %s): %s\n", filename, mode, strerror(errno));
69 return fh;
70}
71
72static void readfile(const char *filename, uint8_t **data, size_t *data_len) {
73 struct stat sb;
74 if (stat(filename, &sb) != 0)
75 die("stat(%s): %s\n", filename, strerror(errno));
76 int fd = open(filename, O_RDONLY);
77 if (fd < 0)
78 die("open(%s): %s\n", filename, strerror(errno));
79 *data_len = sb.st_size;
80 *data = malloc(*data_len);
81 if (!*data)
82 die("%s(%s): can't alloc %zd bytes\n", __func__, filename, *data_len);
83 if (read(fd, *data, *data_len) < 0)
84 die("read(%d, %zd): %s\n", fd, *data_len, strerror(errno));
85}
86
87static bool is_text(char *filename) {
88 char *ext = strchr(basename(filename), '.');
89 if (ext) {
90 ext++;
91 if (strcmp(ext, "html") == 0) return true;
92 else if (strcmp(ext, "json") == 0) return true;
93 else if (strcmp(ext, "xml") == 0) return true;
94 else if (strcmp(ext, "css") == 0) return true;
95 else if (strcmp(ext, "svg") == 0) return true;
96 else if (strcmp(ext, "js") == 0) return true;
97 }
98 return false;
99}
100
101static uint8_t mime_type_from_filename(char *filename) {
102 char *ext = strchr(basename(filename), '.');
103 if (ext) {
104 ext++;
105 // See "enum template_types" bellow
106 if (strcmp(ext, "png") == 0) return 1;
107 else if (strcmp(ext, "gif") == 0) return 2;
108 else if (strcmp(ext, "ico") == 0) return 3;
109 else if (strcmp(ext, "jpg") == 0) return 4;
110 }
111 return 0;
112}
113
114static void parse_index_file(char *filename) {
115 FILE *f = xfopen(filename, "r");
116 int max_fields = 3;
117 char line[1024];
118 while (fgets(line, sizeof(line) - 1, f)) {
119 int field = 0, pos = 0;
120 char *ident = "", *file = "", *deps = "";
121 int len = strlen(line);
122 if (!len || !isalnum(line[0])) // Skip comments and junk
123 continue;
124 // Parse text[ ]text[ ]text
125 do {
126 while (line[pos] == ' ' || line[pos] == '\t') // Skip white space
127 pos++;
128 if (line[pos] == '\n')
129 break;
130 int start = pos;
131 while (line[pos] != ' ' && line[pos] != '\t' && line[pos] != '\n') // Data
132 pos++;
133 switch (++field) {
134 case 1: ident = line + start; line[pos] = '\0'; break;
135 case 2: file = line + start; line[pos] = '\0'; break;
136 case 3: deps = line + start; line[pos] = '\0'; break;
137 }
138 if (field >= max_fields)
139 break;
140 pos++;
141 } while (pos < len);
142 if (!strlen(ident) || !strlen(file))
143 continue;
144
145#define template_set(var) \
146 do { \
147 len = strlen(var); \
148 pos = sizeof(templates.data[0].var); \
149 if (len > pos - 1) \
150 die("%s=%s length exceeds maxlen (%d > %d)\n", #var, var, len, pos - 1); \
151 snprintf(templates.data[templates.num].var, pos, "%s", var); \
152 } while (0)
153 template_set(ident);
154 template_set(file);
155 template_set(deps);
156
157 templates.data[templates.num].type = is_text(file) ? TXT : BIN;
158 templates.data[templates.num].mime_type = mime_type_from_filename(file);
159 templates.num++;
160 if (templates.num == MAX_TEMPLATES - 1) {
161 die("Too many templates in %s. Maximum is %d. Increase MAX_TEMPLATES!\n",
162 filename, MAX_TEMPLATES);
163 }
164 }
165 fclose(f);
166}
167
168static void print_template(int tpl_idx) {
169 static bool ifdef_open = 0;
170 char *prev_deps = "";
171 char *next_deps = "";
172 char *ident = templates.data[tpl_idx].ident;
173 char *deps = templates.data[tpl_idx].deps;
174 if (tpl_idx > 0)
175 prev_deps = templates.data[tpl_idx - 1].deps;
176 if (tpl_idx + 1 < templates.num)
177 next_deps = templates.data[tpl_idx + 1].deps;
178 int deps_len = strlen(deps);
179
180 // Put guards
181 if (deps_len && strcmp(deps, prev_deps) != 0) {
182 int i, commas = 0;
183 for (i = 0; i < deps_len; i++) {
184 if (deps[i] == ',')
185 commas++;
186 }
187 if (commas == 0) {
188 fprintf(output_file, "#ifdef %s\n", deps);
189 } else {
190 char *ptr, *saveptr1 = NULL;
191 char *split_deps = strdup(deps);
192 for (i = 0, ptr = strtok_r(split_deps, ",", &saveptr1); ptr; ptr = strtok_r(NULL, ",", &saveptr1), i++) {
193 if (i == 0)
194 fprintf(output_file, "#if defined(%s)", ptr);
195 else
196 fprintf(output_file, " || defined(%s)", ptr);
197 }
198 fprintf(output_file, "\n");
199 free(split_deps);
200 }
201 ifdef_open = 1;
202 }
203
204 fprintf(output_file, "\t{ .tpl_name=\"%s\", .tpl_data=TPL%s, .tpl_deps=\"%s\", .tpl_data_len=%u, .tpl_type=%u },\n",
205 ident, ident, deps, templates.data[tpl_idx].data_len, templates.data[tpl_idx].mime_type
206 );
207
208 if (ifdef_open && strcmp(deps, next_deps) != 0) {
209 fprintf(output_file, "#endif\n");
210 ifdef_open = 0;
211 }
212}
213
214static void dump_text(char *ident, uint8_t *buf, size_t buf_len) {
215 int i;
216 fprintf(output_file, "#define TPL%s \\\n\"", ident);
217 for (i = 0; i < buf_len; i++) {
218 switch (buf[i]) {
219 case '\n':
220 if (i < buf_len - 1)
221 fprintf(output_file, "\\n\\\n");
222 else
223 fprintf(output_file, "\\n");
224 break;
225 case '\\': fprintf(output_file, "\\\\"); break;
226 case '"' : fprintf(output_file, "\\\""); break;
227 default : fprintf(output_file, "%c", buf[i]); break;
228 }
229 }
230 fprintf(output_file, "\"\n\n");
231}
232
233static void dump_binary(char *ident, uint8_t *buf, size_t buf_len) {
234 fprintf(output_file, "#define TPL%s \\\n\"", ident);
235 int i;
236 for (i = 0; i < buf_len; i++) {
237 fprintf(output_file, "\\x%02x", buf[i]);
238 }
239 fprintf(output_file, "\"\n\n");
240}
241
242int main(void) {
243 int i;
244
245 parse_index_file(index_filename);
246
247 output_file = xfopen(output_pages_h, "w");
248 fprintf(output_file, "#ifndef WEBIF_PAGES_H_\n");
249 fprintf(output_file, "#define WEBIF_PAGES_H_\n");
250 fprintf(output_file, "\n");
251 fprintf(output_file, "enum template_types {\n");
252 fprintf(output_file, " TEMPLATE_TYPE_TEXT = 0,\n");
253 fprintf(output_file, " TEMPLATE_TYPE_PNG = 1,\n");
254 fprintf(output_file, " TEMPLATE_TYPE_GIF = 2,\n");
255 fprintf(output_file, " TEMPLATE_TYPE_ICO = 3,\n");
256 fprintf(output_file, " TEMPLATE_TYPE_JPG = 4,\n");
257 fprintf(output_file, "};\n");
258 fprintf(output_file, "\n");
259 fprintf(output_file, "struct template {\n");
260 fprintf(output_file, " char *tpl_name;\n");
261 fprintf(output_file, " char *tpl_data;\n");
262 fprintf(output_file, " char *tpl_deps;\n");
263 fprintf(output_file, " uint32_t tpl_data_len;\n");
264 fprintf(output_file, " uint8_t tpl_type;\n");
265 fprintf(output_file, "};\n");
266 fprintf(output_file, "\n");
267 fprintf(output_file, "int32_t templates_count(void);\n");
268 fprintf(output_file, "bool template_is_image(enum template_types tpl_type);\n");
269 fprintf(output_file, "const char *template_get_mimetype(enum template_types tpl_type);\n");
270 fprintf(output_file, "\n");
271 fprintf(output_file, "#endif\n");
272 fclose(output_file);
273
274 output_file = xfopen(output_pages_c, "w");
275 fprintf(output_file, "#include \"../globals.h\"\n");
276 fprintf(output_file, "\n");
277 fprintf(output_file, "#ifdef WEBIF\n");
278 fprintf(output_file, "\n");
279 fprintf(output_file, "#include \"pages.h\"\n");
280 fprintf(output_file, "\n");
281
282 for (i = 0; i < templates.num; i++) {
283 uint8_t *buf;
284 size_t buf_len;
285 readfile(templates.data[i].file, &buf, &buf_len);
286 templates.data[i].data_len = buf_len;
287 switch (templates.data[i].type) {
288 case TXT: dump_text(templates.data[i].ident, buf, buf_len); break;
289 case BIN: dump_binary(templates.data[i].ident, buf, buf_len); break;
290 }
291 free(buf);
292 }
293
294 fprintf(output_file, "const struct template templates[] = {\n");
295 for (i = 0; i < templates.num; i++) {
296 print_template(i);
297 }
298 fprintf(output_file, "};\n");
299 fprintf(output_file, "\n");
300 fprintf(output_file, "int32_t templates_count(void) { return sizeof(templates) / sizeof(struct template); }\n");
301 fprintf(output_file, "\n");
302 fprintf(output_file, "bool template_is_image(enum template_types tpl_type) {\n");
303 fprintf(output_file, " switch (tpl_type) {\n");
304 fprintf(output_file, " case TEMPLATE_TYPE_PNG:\n");
305 fprintf(output_file, " case TEMPLATE_TYPE_GIF:\n");
306 fprintf(output_file, " case TEMPLATE_TYPE_ICO:\n");
307 fprintf(output_file, " case TEMPLATE_TYPE_JPG:\n");
308 fprintf(output_file, " return true;\n");
309 fprintf(output_file, " default:\n");
310 fprintf(output_file, " return false;\n");
311 fprintf(output_file, " }\n");
312 fprintf(output_file, " return false;\n");
313 fprintf(output_file, "}\n");
314 fprintf(output_file, "\n");
315 fprintf(output_file, "const char *template_get_mimetype(enum template_types tpl_type) {\n");
316 fprintf(output_file, " switch (tpl_type) {\n");
317 fprintf(output_file, " case TEMPLATE_TYPE_TEXT: return \"text/plain\";\n");
318 fprintf(output_file, " case TEMPLATE_TYPE_PNG : return \"image/png\";\n");
319 fprintf(output_file, " case TEMPLATE_TYPE_GIF : return \"image/gif\";\n");
320 fprintf(output_file, " case TEMPLATE_TYPE_ICO : return \"image/x-icon\";\n");
321 fprintf(output_file, " case TEMPLATE_TYPE_JPG : return \"image/jpg\";\n");
322 fprintf(output_file, " }\n");
323 fprintf(output_file, " return \"\";\n");
324 fprintf(output_file, "}\n");
325 fprintf(output_file, "\n");
326 fprintf(output_file, "#endif\n");
327 fclose(output_file);
328
329 return 0;
330}
Note: See TracBrowser for help on using the repository browser.