-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhouselights_template.c
More file actions
203 lines (166 loc) · 6.2 KB
/
houselights_template.c
File metadata and controls
203 lines (166 loc) · 6.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/* houseslights - A simple home web server for lighting control
*
* Copyright 2025, Pascal Martin
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
*
* houselights_template.c - Generate HTML on the fly from template files.
*
* SYNOPSYS:
*
* This module handles pages that include SVG content created using
* Inkscape. The tool does not embbed the SVG in an HTML page by itself,
* so you must do this yourself. However any modification to the SVG means
* the HTML integration is to be done all over again. This module is a
* runtime solution for automating that process.
*
* const char *houselights_template_initialize
* (int argc, const char **argv, const char *rooturi);
*
* Install the templating mechanism.
*/
#include <sys/time.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <echttp.h>
#include <echttp_static.h>
#include "houselog.h"
#include "houseconfig.h"
#include "houselights_template.h"
#define DEBUG if (echttp_isdebug()) printf
static const char *HouseLightsContentRoot = "/var/lib/house/lights";
static const char *HouseLightsWebRoot = "/var/cache/house/lights";
static int HouseLightsContentRootLength = 0;
static int HouseLightsWebRootLength = 0;
static echttp_not_found_handler *HouseLightsTranscodeChain = 0;
static void houselights_template_patch (char *text, const char *prefix) {
char *attribute = strstr (text, prefix);
if (!attribute) return;
attribute += strlen (prefix);
*(attribute++) = '1';
*(attribute++) = '0';
*(attribute++) = '0';
*(attribute++) = '%';
if (*attribute != '"') {
*(attribute++) = '"';
while (*attribute != '"') *(attribute++) = ' ';
*attribute = ' ';
}
}
static void houselights_template_expand (FILE *in, FILE *out) {
char source[1024];
while (fgets(source, sizeof(source), in)) {
char *cursor = source - 1;
while (*(++cursor) <= ' ') {if (*cursor != ' ' || *cursor != '\t') break;}
if (cursor[0] == '<' && cursor[1] == '<') {
*cursor = 0; // Maintain the original indentation.
// Extract the include file name.
char *name = cursor + 2;
cursor = strchr (name, '\n');
if (cursor) *cursor = 0;
// Read the include file and expand into the output.
// Patch the width and height of the "svg" element.
//
char fullpath[1048];
snprintf (fullpath, sizeof(fullpath),
"%s/%s", HouseLightsContentRoot, name);
FILE *include = fopen (fullpath, "r");
if (include) {
char included[1024];
int issvg = 0;
while (fgets(included, sizeof(included), include)) {
if (strstr (included, "<svg")) issvg = 1;
else if (strchr (included, '<')) issvg = 0;
if (included[0] == '<') { // Skip XML info.
if (included[1] == '?' || included[1] == '!') continue;
}
if (issvg) {
houselights_template_patch (included, "width=\"");
houselights_template_patch (included, "height=\"");
}
fputs (source, out);
fputs (included, out);
}
fputs ("\n", out);
fclose (include);
}
} else {
fputs (source, out); // No include to process: write as-is.
}
}
}
static int houselights_template_render (const char *filename) {
int fd;
if (HouseLightsTranscodeChain) {
fd = HouseLightsTranscodeChain (filename);
if (fd >= 0) return fd;
}
if (strncmp (filename, HouseLightsWebRoot, HouseLightsWebRootLength)) {
// Reject any URL that does not point to the cache.
return -1;
}
char fullpath[1024];
const char *base = filename + HouseLightsWebRootLength;
snprintf (fullpath, sizeof(fullpath), "%s%s", HouseLightsContentRoot, base);
if (!strstr (base, ".html")) {
// Only render to HTML, but support other formats as-is.
// In that case, we just pretend that the file was found
// by opening it at its "installed" location. If the file does not
// exists, open() will fail and a 404 status will be returned.
snprintf (fullpath, sizeof(fullpath), "%s%s", HouseLightsContentRoot, base);
return open (fullpath, O_RDONLY);
}
FILE *in = 0;
FILE *out = 0;
// Build the source name.
char *sep = strrchr (fullpath, '.');
if (!sep) return -1;
sep[4] = 't'; // The source is an ".htmt" file.
in = fopen (fullpath, "r");
if (!in) goto failure;
// Create all the directories listed in the target file's path.
// This is as brute force as it can get. To publish is not high
// volume enough to justify spending brain power..
//
snprintf (fullpath, sizeof(fullpath), "mkdir -p %s", filename);
sep = strrchr (fullpath, '/');
if (sep) {
*sep = 0;
system (fullpath);
}
out = fopen (filename, "w+");
if (!out) goto failure;
houselights_template_expand (in, out);
fclose (in);
fclose (out);
return open (filename, O_RDONLY);
failure:
if (in) fclose (in);
if (out) fclose (out);
return -1;
}
const char *houselights_template_initialize
(int argc, const char **argv, const char *rooturi) {
HouseLightsContentRootLength = strlen(HouseLightsContentRoot);
HouseLightsWebRootLength = strlen(HouseLightsWebRoot);
echttp_static_route (rooturi, HouseLightsWebRoot);
HouseLightsTranscodeChain =
echttp_static_on_not_found (houselights_template_render);
return 0;
}