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
|
/*
* msg
* Copyright (C) 2025 Raghuram Subramani <raghus2247@gmail.com>
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*/
#define _GNU_SOURCE
#include <config.h>
#include <copy.h>
#include <engine.h>
#include <filehandler.h>
#include <lexer.h>
#include <list.h>
#include <msg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <util.h>
extern msg_t *msg;
/*
* First reads the config, if present. Then streams each directive and parses
* it, modifying the buffer. At the end, all applicable directives in the
* buffer are parsed and evaluated.
*
* ENDEACHDO, BODY, CONTENT, _RAW, etc. are not applicable,
* for varying reasons. For example, ENDEACHDO isn't really meant to be parsed.
* It's only there to define the end of the EACHDO content block.
*/
engine_t *
engine_ingest(char **buffer)
{
engine_t *engine = malloc(sizeof(engine_t));
engine->config = NULL;
char *p = strstr(*buffer, "---");
if (p != NULL) {
char *config;
asprintf(&config, "%.*s\n", (int) (p - *buffer), *buffer);
engine->config = config_parse(config);
free(config);
char *tempbuffer = strdup(p);
free(*buffer);
asprintf(buffer, "%s", tempbuffer + strlen("---"));
free(tempbuffer);
}
key_match_t *match;
engine->content_headers = list_create(sizeof(contentfor_operand_t));
if (engine->content_headers == NULL) {
printf("Could not create content_headers\n");
return NULL;
}
size_t skip = 0;
while (true) {
match = find_next_key(*buffer, skip);
if (match == NULL)
break;
#ifdef DEBUG
printf("Match: %.*s LENGTH(%d) OFFSET(%d)\n",
match->length,
*buffer + match->offset,
match->length,
match->offset);
#endif
directive_t *directive = find_directive(*buffer, match);
if (directive == NULL) {
printf(
"Unknown directive: %.*s\n", match->length, *buffer + match->offset);
break;
}
switch (directive->type) {
case INCLUDE:
handle_include(buffer, match, directive);
break;
case CONTENTFOR:
handle_contentfor(buffer, match, directive, engine->content_headers);
break;
case EACHDO:
handle_eachdo(buffer, match, directive);
break;
case PUTPAGE:
/* TODO: handle */
case PUT:
case ENDEACHDO:
case BODY:
case CONTENT:
case ENDCONTENT:
case _RAW:
skip++;
break;
}
directive_delete(directive);
free(match);
}
return engine;
}
/*
* Frees the provided engine_t's children before freeing the structure itself.
*/
void
engine_delete(engine_t *engine)
{
if (engine->config != NULL)
config_delete(engine->config);
for (size_t i = 0; i < engine->content_headers->size; i++) {
contentfor_operand_t *operand = list_get(engine->content_headers, i);
free(operand->content);
free(operand->key);
}
list_delete(engine->content_headers);
free(engine);
}
|