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
|
#include "util.h"
#define _GNU_SOURCE
#include <config.h>
#include <copy.h>
#include <engine.h>
#include <filehandler.h>
#include <lexer.h>
#include <list.h>
#include <main.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define DEBUG
extern msg_t *msg;
void
handle_include(char **buffer, key_match_t *match, directive_t *directive)
{
char *operand = directive->operands;
char *partial_path;
asprintf(&partial_path, "%s/%s/%s", msg->base_directory, PARTIALS, operand);
FILE *f = fopen(partial_path, "r");
if (f == NULL) {
printf("Could not open: %s\n", partial_path);
return;
}
unsigned int size = fsize(f);
char *partial_content = fcontent(f, size);
fclose(f);
char *temp_buffer = strdup(*buffer);
free(*buffer);
asprintf(buffer,
"%.*s%s%s\n",
match->offset,
temp_buffer,
partial_content,
temp_buffer + match->offset + match->length);
free(partial_content);
free(temp_buffer);
}
void
handle_contentfor(char **buffer,
key_match_t *match,
directive_t *directive,
list_t *content_headers)
{
contentfor_operand_t *operand = directive->operands;
list_add(content_headers, operand);
#ifdef DEBUG
printf("CONTENTFOR: %s\n", operand->key);
printf("CONTENT: %s\n", operand->content);
#endif
char *temp_buffer = strdup(*buffer);
free(*buffer);
asprintf(buffer,
"%.*s%s",
match->offset,
temp_buffer,
temp_buffer + operand->length);
free(temp_buffer);
free(operand);
}
void
handle_eachdo(char **buffer, key_match_t *match, directive_t *directive)
{
eachdo_operands_t *operands = directive->operands;
#ifdef DEBUG
printf("KEY: %s\n", operands->key);
printf("CONTENT: %s\n", operands->content);
#endif
char *path;
asprintf(&path, "%s/%s", msg->base_directory, operands->key);
list_t *files = enumfilesindir(path);
free(path);
if (files == NULL) {
printf("Could not find key %s\n", operands->key);
free(operands);
return;
}
for (size_t i = 0; i < files->size; i++) {
ptr_wrapper_t *wrapper = list_get(files, i);
asprintf(&path,
"%s/%s/%s",
msg->base_directory,
operands->key,
(char *) wrapper->ptr);
FILE *f = fopen(path, "r");
free(path);
size_t size = fsize(f);
char *content = fcontent(f, size);
fclose(f);
config_t *config = NULL;
char *p = strstr(content, "---");
if (p != NULL) {
*p = '\0';
config = config_parse(content);
}
free(content);
for (size_t j = 0; j < config->keys->size; j++) {
ptr_wrapper_t *wrapper = list_get(config->keys, j);
printf("%s\n", (char *) wrapper->ptr);
}
}
exit(1);
free(operands);
}
list_t *
engine_ingest(char **buffer)
{
key_match_t *match;
list_t *content_headers = list_create(sizeof(contentfor_operand_t));
if (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, content_headers);
break;
case EACHDO:
handle_eachdo(buffer, match, directive);
break;
case PUT:
case ENDEACHDO:
case BODY:
case CONTENT:
case ENDCONTENT:
case _RAW:
skip++;
break;
}
if (directive != NULL)
free(directive);
if (match != NULL)
free(match);
}
return content_headers;
}
|