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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
|
#include "LuaEngine/LuaEngine.h"
#include "DirectoryWalker/DirectoryWalker.h"
#include "Dom/Dom.h"
#include "Logger/Logger.h"
#include "Logger/LuaLogger.h"
#include "Path/Path.h"
#include "Version/Version.h"
#include <lauxlib.h>
#include <lua.h>
#include <lualib.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
typedef enum { SCRIPT_INLINE, SCRIPT_EXTERNAL } ScriptType;
typedef struct {
char *source;
ScriptType type;
unsigned long line;
} LuaScript;
typedef struct {
LuaScript *items;
size_t length;
size_t capacity;
} ScriptList;
static int is_lua_element(const xmlNode *node) {
return node->type == XML_ELEMENT_NODE &&
strcasecmp((const char *)node->name, "lua") == 0;
}
static char *duplicate_string(const char *value) {
size_t length = strlen(value);
char *copy = malloc(length + 1);
if (copy != NULL) {
memcpy(copy, value, length + 1);
}
return copy;
}
static int grow_script_list(ScriptList *scripts) {
size_t larger_capacity;
LuaScript *larger_items;
if (scripts->capacity > SIZE_MAX / (2 * sizeof(*scripts->items))) {
return -1;
}
larger_capacity = scripts->capacity == 0 ? 4 : scripts->capacity * 2;
larger_items =
realloc(scripts->items, larger_capacity * sizeof(*scripts->items));
if (larger_items == NULL) {
return -1;
}
scripts->items = larger_items;
scripts->capacity = larger_capacity;
return 0;
}
static int append_script(ScriptList *scripts, const char *source,
ScriptType type, unsigned long line) {
LuaScript *script;
if (scripts->length == scripts->capacity && grow_script_list(scripts) != 0) {
return -1;
}
script = &scripts->items[scripts->length];
script->source = duplicate_string(source);
if (script->source == NULL) {
return -1;
}
script->type = type;
script->line = line;
++scripts->length;
return 0;
}
static int collect_script(xmlNode *node, ScriptList *scripts) {
xmlChar *source_path = xmlGetProp(node, BAD_CAST "src");
xmlChar *code;
int result;
if (source_path != NULL) {
result = append_script(scripts, (const char *)source_path, SCRIPT_EXTERNAL,
node->line);
xmlFree(source_path);
return result;
}
code = xmlNodeGetContent(node);
result = append_script(scripts, code == NULL ? "" : (const char *)code,
SCRIPT_INLINE, node->line);
xmlFree(code);
return result;
}
static int collect_scripts(xmlNode *node, ScriptList *scripts) {
for (; node != NULL; node = node->next) {
if (is_lua_element(node)) {
if (collect_script(node, scripts) != 0) {
return -1;
}
} else if (collect_scripts(node->children, scripts) != 0) {
return -1;
}
}
return 0;
}
static void free_scripts(ScriptList *scripts) {
size_t index;
for (index = 0; index < scripts->length; ++index) {
free(scripts->items[index].source);
}
free(scripts->items);
}
static char *make_chunk_name(const char *input_path, unsigned long line) {
size_t input_length = strlen(input_path);
size_t capacity;
char *chunk_name;
if (input_length > SIZE_MAX - 32) {
return NULL;
}
capacity = input_length + 32;
chunk_name = malloc(capacity);
if (chunk_name != NULL) {
snprintf(chunk_name, capacity, "@%s:%lu", input_path, line);
}
return chunk_name;
}
typedef struct {
htmlDocPtr source;
EmitList *emits;
} EmitContext;
static int grow_emit_list(EmitList *emits) {
size_t larger_capacity;
EmittedPage *larger_items;
if (emits->capacity > SIZE_MAX / (2 * sizeof(*emits->items))) {
return -1;
}
larger_capacity = emits->capacity == 0 ? 4 : emits->capacity * 2;
larger_items =
realloc(emits->items, larger_capacity * sizeof(*emits->items));
if (larger_items == NULL) {
return -1;
}
emits->items = larger_items;
emits->capacity = larger_capacity;
return 0;
}
static int append_emit(EmitList *emits, const char *path,
htmlDocPtr document) {
EmittedPage *emitted;
if (emits->length == emits->capacity && grow_emit_list(emits) != 0) {
return -1;
}
emitted = &emits->items[emits->length];
emitted->path = duplicate_string(path);
if (emitted->path == NULL) {
return -1;
}
emitted->document = document;
++emits->length;
return 0;
}
void emit_list_free(EmitList *emits) {
size_t index;
for (index = 0; index < emits->length; ++index) {
free(emits->items[index].path);
xmlFreeDoc(emits->items[index].document);
}
free(emits->items);
emits->items = NULL;
emits->length = 0;
emits->capacity = 0;
}
static htmlDocPtr snapshot_document(htmlDocPtr source, lua_State *lua) {
htmlDocPtr copy;
xmlDtdPtr dtd;
xmlNodePtr root;
lua_getglobal(lua, "page");
root = lua_istable(lua, -1) ? dom_page_from_lua(lua, lua_gettop(lua)) : NULL;
lua_pop(lua, 1);
if (root == NULL) {
return NULL;
}
copy = xmlNewDoc(BAD_CAST "1.0");
if (copy == NULL) {
xmlFreeNode(root);
return NULL;
}
if (source->encoding != NULL) {
copy->encoding = xmlStrdup(source->encoding);
}
dtd = xmlGetIntSubset(source);
if (dtd != NULL) {
dtd = xmlCopyDtd(dtd);
if (dtd != NULL) {
xmlAddChild((xmlNodePtr)copy, (xmlNodePtr)dtd);
copy->intSubset = dtd;
}
}
xmlAddChild((xmlNodePtr)copy, root);
return copy;
}
static int page_emit(lua_State *lua) {
EmitContext *context;
const char *path;
htmlDocPtr document;
luaL_checktype(lua, 1, LUA_TTABLE);
path = luaL_checkstring(lua, 2);
if (path[0] == '\0') {
return luaL_error(lua, "emit path cannot be empty");
}
context = lua_touserdata(lua, lua_upvalueindex(1));
document = snapshot_document(context->source, lua);
if (document == NULL) {
return luaL_error(lua, "unable to snapshot the current page");
}
if (append_emit(context->emits, path, document) != 0) {
xmlFreeDoc(document);
return luaL_error(lua, "unable to record emitted page");
}
return 0;
}
static int run_script(lua_State *lua, const LuaScript *script,
const char *input_path) {
char *source_path = NULL;
char *chunk_name = NULL;
const char *display_path;
int load_result;
int result;
if (script->type == SCRIPT_EXTERNAL) {
source_path = path_resolve_from_file(input_path, script->source);
if (source_path == NULL) {
logger_error("unable to allocate Lua source path");
return -1;
}
display_path = source_path;
load_result = luaL_loadfile(lua, source_path);
} else {
chunk_name = make_chunk_name(input_path, script->line);
if (chunk_name == NULL) {
logger_error("unable to allocate Lua chunk name");
return -1;
}
display_path = input_path;
load_result = luaL_loadbuffer(lua, script->source, strlen(script->source),
chunk_name);
}
result = load_result == 0 ? lua_pcall(lua, 0, 0, 0) : load_result;
if (result != 0) {
const char *message = lua_tostring(lua, -1);
logger_error("Lua error in %s: %s", display_path,
message == NULL ? "unknown error" : message);
lua_pop(lua, 1);
}
free(chunk_name);
free(source_path);
return result == 0 ? 0 : -1;
}
static int run_scripts(lua_State *lua, const ScriptList *scripts,
const char *input_path) {
size_t index;
for (index = 0; index < scripts->length; ++index) {
if (run_script(lua, &scripts->items[index], input_path) != 0) {
return -1;
}
}
return 0;
}
static void register_yapssg(lua_State *lua) {
lua_newtable(lua);
lua_pushstring(lua, yapssg_version);
lua_setfield(lua, -2, "version");
lua_setglobal(lua, "yapssg");
}
static int replace_document_root(lua_State *lua, htmlDocPtr document,
const char *input_path) {
xmlNodePtr replacement;
xmlNodePtr old_root;
lua_getglobal(lua, "page");
if (!lua_istable(lua, -1)) {
logger_error("Lua global 'page' must remain a table: %s", input_path);
lua_pop(lua, 1);
return -1;
}
replacement = dom_page_from_lua(lua, lua_gettop(lua));
lua_pop(lua, 1);
if (replacement == NULL || replacement->type != XML_ELEMENT_NODE) {
logger_error("Lua produced an invalid page root: %s", input_path);
xmlFreeNode(replacement);
return -1;
}
old_root = xmlDocSetRootElement(document, replacement);
xmlFreeNode(old_root);
return 0;
}
int process_html_document(htmlDocPtr document, const char *input_path,
EmitList *emits) {
EmitContext context = {document, emits};
xmlNodePtr root = xmlDocGetRootElement(document);
ScriptList scripts = {0};
lua_State *lua;
int result;
if (root == NULL) {
logger_error("HTML document has no root element: %s", input_path);
return -1;
}
if (collect_scripts(root, &scripts) != 0) {
logger_error("unable to collect Lua scripts: %s", input_path);
free_scripts(&scripts);
return -1;
}
if (scripts.length == 0) {
free_scripts(&scripts);
return 0;
}
lua = luaL_newstate();
if (lua == NULL) {
logger_error("unable to create Lua state");
free_scripts(&scripts);
return -1;
}
luaL_openlibs(lua);
register_yapssg(lua);
dom_register_html(lua);
lua_logger_register(lua);
directory_walker_register(lua, input_path);
dom_push_page(lua, root);
lua_setglobal(lua, "page");
lua_getglobal(lua, "page");
lua_pushlightuserdata(lua, &context);
lua_pushcclosure(lua, page_emit, 1);
lua_setfield(lua, -2, "emit");
lua_pop(lua, 1);
result = run_scripts(lua, &scripts, input_path);
if (result == 0) {
result = replace_document_root(lua, document, input_path);
}
lua_close(lua);
free_scripts(&scripts);
return result;
}
|