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
|
#define _GNU_SOURCE
#include <engine.h>
#include <filehandler.h>
#include <lexer.h>
#include <list.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include "../config.h"
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", 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);
char *temp_buffer;
asprintf(&temp_buffer, "%s", *buffer);
free(*buffer);
asprintf(buffer,
"%.*s%s%s\n",
match->offset,
temp_buffer,
partial_content,
temp_buffer + match->offset + match->length);
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;
asprintf(&temp_buffer, "%s", *buffer);
free(*buffer);
asprintf(buffer,
"%.*s%s",
match->offset,
temp_buffer,
temp_buffer + operand->length);
free(temp_buffer);
free(operand);
}
void
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;
}
while (true) {
match = find_next_key(*buffer);
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;
/* TODO: Handle this gracefully */
case BODY:
case CONTENT:
case ENDCONTENT:
case _RAW:
break;
}
if (directive != NULL)
free(directive);
if (match != NULL)
free(match);
}
for (size_t i = 0; i < content_headers->size; i++) {
contentfor_operand_t *op = list_get(content_headers, i);
free(op->content);
free(op->key);
}
list_delete(content_headers);
}
|