source: trunk/webif/pages_gen.c@ 8427

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

webif: Convert templates array to structure.

This allows to extend the data we keep for each template (hash,
datasize, etc).

File size: 10.0 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 enum { TXT, BIN } type;
46 } data[MAX_TEMPLATES];
47};
48
49static struct templates templates;
50static FILE *output_file;
51
52static void die(const char * s, ...) {
53 va_list args;
54 va_start(args, s);
55 fprintf(stderr, "ERROR: ");
56 vfprintf(stderr, s, args);
57 if (s[strlen(s) - 1] != '\n')
58 fprintf(stderr, "\n");
59 va_end(args);
60 exit(EXIT_FAILURE);
61}
62
63static FILE *xfopen(char *filename, char *mode) {
64 FILE *fh = fopen(filename, mode);
65 if (!fh)
66 die("fopen(%s, %s): %s\n", filename, mode, strerror(errno));
67 return fh;
68}
69
70static void readfile(const char *filename, uint8_t **data, size_t *data_len) {
71 struct stat sb;
72 if (stat(filename, &sb) != 0)
73 die("stat(%s): %s\n", filename, strerror(errno));
74 int fd = open(filename, O_RDONLY);
75 if (fd < 0)
76 die("open(%s): %s\n", filename, strerror(errno));
77 *data_len = sb.st_size;
78 *data = malloc(*data_len);
79 if (!*data)
80 die("%s(%s): can't alloc %zd bytes\n", __func__, filename, *data_len);
81 if (read(fd, *data, *data_len) < 0)
82 die("read(%d, %zd): %s\n", fd, *data_len, strerror(errno));
83}
84
85static const char Base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
86static const char Pad64 = '=';
87
88int b64_encode(const uint8_t *src, size_t srclength, uint8_t *target, size_t targsize)
89{
90 size_t datalength = 0;
91 uint8_t input[3] = {0,0,0};
92 uint8_t output[4] = {0,0,0,0};
93 unsigned int i;
94
95 while (2 < srclength) {
96 input[0] = *src++;
97 input[1] = *src++;
98 input[2] = *src++;
99 srclength -= 3;
100 output[0] = input[0] >> 2;
101 output[1] = ((input[0] & 0x03) << 4) + (input[1] >> 4);
102 output[2] = ((input[1] & 0x0f) << 2) + (input[2] >> 6);
103 output[3] = input[2] & 0x3f;
104 if (datalength + 4 > targsize)
105 return -1;
106 target[datalength++] = Base64[output[0]];
107 target[datalength++] = Base64[output[1]];
108 target[datalength++] = Base64[output[2]];
109 target[datalength++] = Base64[output[3]];
110 }
111
112 /* Now we worry about padding. */
113 if (0 != srclength) {
114 /* Get what's left. */
115 input[0] = input[1] = input[2] = '\0';
116 for (i = 0; i < srclength; i++)
117 input[i] = *src++;
118
119 output[0] = input[0] >> 2;
120 output[1] = ((input[0] & 0x03) << 4) + (input[1] >> 4);
121 output[2] = ((input[1] & 0x0f) << 2) + (input[2] >> 6);
122
123 if (datalength + 4 > targsize)
124 return (-1);
125 target[datalength++] = Base64[output[0]];
126 target[datalength++] = Base64[output[1]];
127 if (srclength == 1)
128 target[datalength++] = Pad64;
129 else
130 target[datalength++] = Base64[output[2]];
131 target[datalength++] = Pad64;
132 }
133 if (datalength >= targsize)
134 return -1;
135 target[datalength] = '\0'; /* Returned value doesn't count \0. */
136 return datalength;
137}
138
139static bool is_text(char *filename) {
140 char *ext = strchr(basename(filename), '.');
141 if (ext) {
142 ext++;
143 if (strcmp(ext, "html") == 0) return true;
144 else if (strcmp(ext, "json") == 0) return true;
145 else if (strcmp(ext, "xml") == 0) return true;
146 else if (strcmp(ext, "css") == 0) return true;
147 else if (strcmp(ext, "svg") == 0) return true;
148 else if (strcmp(ext, "js") == 0) return true;
149 }
150 return false;
151}
152
153static void parse_index_file(char *filename) {
154 FILE *f = xfopen(filename, "r");
155 int max_fields = 3;
156 char line[1024];
157 while (fgets(line, sizeof(line) - 1, f)) {
158 int field = 0, pos = 0;
159 char *ident = "", *file = "", *deps = "";
160 int len = strlen(line);
161 if (!len || !isalnum(line[0])) // Skip comments and junk
162 continue;
163 // Parse text[ ]text[ ]text
164 do {
165 while (line[pos] == ' ' || line[pos] == '\t') // Skip white space
166 pos++;
167 if (line[pos] == '\n')
168 break;
169 int start = pos;
170 while (line[pos] != ' ' && line[pos] != '\t' && line[pos] != '\n') // Data
171 pos++;
172 switch (++field) {
173 case 1: ident = line + start; line[pos] = '\0'; break;
174 case 2: file = line + start; line[pos] = '\0'; break;
175 case 3: deps = line + start; line[pos] = '\0'; break;
176 }
177 if (field >= max_fields)
178 break;
179 pos++;
180 } while (pos < len);
181 if (!strlen(ident) || !strlen(file))
182 continue;
183
184#define template_set(var) \
185 do { \
186 len = strlen(var); \
187 pos = sizeof(templates.data[0].var); \
188 if (len > pos - 1) \
189 die("%s=%s length exceeds maxlen (%d > %d)\n", #var, var, len, pos - 1); \
190 snprintf(templates.data[templates.num].var, pos, "%s", var); \
191 } while (0)
192 template_set(ident);
193 template_set(file);
194 template_set(deps);
195
196 templates.data[templates.num].type = is_text(file) ? TXT : BIN;
197 templates.num++;
198 if (templates.num == MAX_TEMPLATES - 1) {
199 die("Too many templates in %s. Maximum is %d. Increase MAX_TEMPLATES!\n",
200 filename, MAX_TEMPLATES);
201 }
202 }
203 fclose(f);
204}
205
206static void parse_deps(int tpl_idx) {
207 static bool ifdef_open = 0;
208 char *prev_deps = "";
209 char *next_deps = "";
210 char *ident = templates.data[tpl_idx].ident;
211 char *deps = templates.data[tpl_idx].deps;
212 if (tpl_idx > 0)
213 prev_deps = templates.data[tpl_idx - 1].deps;
214 if (tpl_idx + 1 < templates.num)
215 next_deps = templates.data[tpl_idx + 1].deps;
216 int deps_len = strlen(deps);
217
218 // Put guards
219 if (deps_len && strcmp(deps, prev_deps) != 0) {
220 int i, commas = 0;
221 for (i = 0; i < deps_len; i++) {
222 if (deps[i] == ',')
223 commas++;
224 }
225 if (commas == 0) {
226 fprintf(output_file, "#ifdef %s\n", deps);
227 } else {
228 char *ptr, *saveptr1 = NULL;
229 char *split_deps = strdup(deps);
230 for (i = 0, ptr = strtok_r(split_deps, ",", &saveptr1); ptr; ptr = strtok_r(NULL, ",", &saveptr1), i++) {
231 if (i == 0)
232 fprintf(output_file, "#if defined(%s)", ptr);
233 else
234 fprintf(output_file, " || defined(%s)", ptr);
235 }
236 fprintf(output_file, "\n");
237 free(split_deps);
238 }
239 ifdef_open = 1;
240 }
241
242 fprintf(output_file, "\t{ .tpl_name=\"%s\", .tpl_data=%s%s, .tpl_deps=\"%s\" },\n",
243 ident,
244 templates.data[tpl_idx].type == TXT ? "TPL" : "", ident,
245 deps);
246
247 if (ifdef_open && strcmp(deps, next_deps) != 0) {
248 fprintf(output_file, "#endif\n");
249 ifdef_open = 0;
250 }
251}
252
253static void dump_text(char *ident, uint8_t *buf, size_t buf_len) {
254 int i;
255 fprintf(output_file, "#define TPL%s \\\n\"", ident);
256 for (i = 0; i < buf_len; i++) {
257 switch (buf[i]) {
258 case '\n':
259 if (i < buf_len - 1)
260 fprintf(output_file, "\\n\\\n");
261 else
262 fprintf(output_file, "\\n");
263 break;
264 case '\\': fprintf(output_file, "\\\\"); break;
265 case '"' : fprintf(output_file, "\\\""); break;
266 default : fprintf(output_file, "%c", buf[i]); break;
267 }
268 }
269 fprintf(output_file, "\"\n\n");
270}
271
272static char *get_mime(char *filename) {
273 char *ext = strchr(filename, '.');
274 if (ext) {
275 ext++;
276 if (strcmp(ext, "png") == 0) return "image/png";
277 else if (strcmp(ext, "gif") == 0) return "image/gif";
278 else if (strcmp(ext, "jpg") == 0) return "image/jpg";
279 else if (strcmp(ext, "ico") == 0) return "image/x-icon";
280 }
281 return "unknown";
282}
283
284static void dump_base64(char *ident, char *mime, uint8_t *buf, size_t buf_len) {
285 size_t b64_buf_len = buf_len * 4 + 16;
286 uint8_t *b64_buf = malloc(b64_buf_len);
287 if (!b64_buf)
288 die("%s: can't alloc %zd bytes\n", __func__, b64_buf_len);
289 int i, b64_len = b64_encode(buf, buf_len, b64_buf, b64_buf_len);
290 fprintf(output_file, "#define %s \"data:%s;base64,\\\n", ident, mime);
291 for (i = 0; i < b64_len; i++) {
292 if (i && i % 76 == 0)
293 fprintf(output_file, "\\\n");
294 fprintf(output_file, "%c", b64_buf[i]);
295 }
296 fprintf(output_file, "\"\n\n");
297 free(b64_buf);
298}
299
300int main(void) {
301 int i;
302
303 parse_index_file(index_filename);
304
305 output_file = xfopen(output_pages_h, "w");
306 fprintf(output_file, "#ifndef WEBIF_PAGES_H_\n");
307 fprintf(output_file, "#define WEBIF_PAGES_H_\n");
308 fprintf(output_file, "\n");
309 fprintf(output_file, "struct template {\n");
310 fprintf(output_file, " char *tpl_name;\n");
311 fprintf(output_file, " char *tpl_data;\n");
312 fprintf(output_file, " char *tpl_deps;\n");
313 fprintf(output_file, "};\n");
314 fprintf(output_file, "\n");
315 fprintf(output_file, "int32_t tpl_count(void);\n");
316 fprintf(output_file, "\n");
317 fprintf(output_file, "#endif\n");
318 fclose(output_file);
319
320 output_file = xfopen(output_pages_c, "w");
321 fprintf(output_file, "#include \"../globals.h\"\n");
322 fprintf(output_file, "\n");
323 fprintf(output_file, "#ifdef WEBIF\n");
324 fprintf(output_file, "\n");
325 fprintf(output_file, "#include \"pages.h\"\n");
326 fprintf(output_file, "\n");
327
328 for (i = 0; i < templates.num; i++) {
329 uint8_t *buf;
330 size_t buf_len;
331 readfile(templates.data[i].file, &buf, &buf_len);
332 switch (templates.data[i].type) {
333 case TXT: dump_text(templates.data[i].ident, buf, buf_len); break;
334 case BIN: dump_base64(templates.data[i].ident, get_mime(templates.data[i].file), buf, buf_len); break;
335 }
336 free(buf);
337 }
338
339 fprintf(output_file, "const struct template templates[] = {\n");
340 for (i = 0; i < templates.num; i++) {
341 parse_deps(i);
342 }
343 fprintf(output_file, "};\n");
344 fprintf(output_file, "\n");
345 fprintf(output_file, "int32_t tpl_count(void) { return sizeof(templates) / sizeof(struct template); }\n");
346 fprintf(output_file, "\n");
347 fprintf(output_file, "#endif\n");
348 fclose(output_file);
349
350 return 0;
351}
Note: See TracBrowser for help on using the repository browser.