/
template.c
372 lines (326 loc) · 8.72 KB
/
template.c
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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <time.h>
#include "template.h"
#include "array.h"
#include "sglib.h"
#include "ff.h"
#include "form.h"
/**
* Hold stuff about the template
*/
typedef struct template_t {
char *path;
struct chunk_t *head;
time_t mtime;
/* For Sglib */
char color_field;
struct template_t *left;
struct template_t *right;
} template;
#define COMPARATOR(x,y) (strcmp(x->path,y->path))
SGLIB_DEFINE_RBTREE_PROTOTYPES( template, left, right, color_field, COMPARATOR );
SGLIB_DEFINE_RBTREE_FUNCTIONS( template, left, right, color_field, COMPARATOR );
char *path;
template *template_cache = NULL;
int template_init() {
path = getcwd(NULL, 0 );
if( !path ) {
fprintf(stderr,"Could not get current path: %d\n", errno );
return -1;
}
}
/**
* Load, parse, evaulate, display, with a form
*/
void template_run_form( char *path, struct form_t *form,
struct array_t *them ) {
struct array_t *arr = array_new();
node *n;
/* If not empty we validated */
if( ! array_empty( form->err) ) {
array_add_obj( arr, "errors", form->err );
}
for( n=array_first( form->clean ); n != NULL;
n = array_next( form->clean ) ) {
// printf("FUCK %s %s\n", n->key, n->value );
array_add_str( arr, n->key, nully(n->value) );
}
template_run( path, arr );
array_free(arr);
}
/**
* Walk the key, recall we break a key with . into a set of subkeys
* isolated by \0
*/
char * walk_macro( struct array_t *arr, char *key ) {
node *n = array_get_node( arr, key );
char *p = key;
while( n ) {
p += strlen( key ) + 1;
if( p ) {
n = array_get_node( n->obj, p );
} else {
/* Nothing else to walk */
return n->value;
}
}
}
/**
* Load, parse, evaulate, display
*/
void template_run( char *path, struct array_t *arr ) {
struct chunk_t *chunk = template_load(path);
char *value;
int iftrue = 1;
int freeit = 1;
if( arr == NULL ) arr = array_new();
else freeit = 0;
printf("Content-type: text/html\r\n\r\n");
while( chunk ) {
if( chunk->macro && chunk->isif == 0 ) {
/* have macro not an if statement, echo it */
if( (value=array_get_macro( arr, chunk->text )) != NULL ) {
printf("%s", value );
}
} else if( chunk->isif ) {
/* Have an if statement, see if macro evals to true/fales */
value = array_get_macro( arr, chunk->text );
if( value == NULL || *value == 0 || *value == '0' ) {
/* Its false don't echo anything until we get endif */
iftrue = 0;
}
} else if( chunk->endif ) {
/* Turn output back on */
iftrue = 1;
} else if( iftrue ) {
/* Echo text by default or if last IF statement eval to true */
printf("%s", chunk->text );
}
chunk = chunk->next;
}
/* Free it here to make life easier in controller */
if( freeit ) array_free( arr );
}
/**
* Macro chunk - break into subkeys
* For macro,s "." indicates a sub object is expected so
* to make this easier to walk data structures as a key we convert
* to \0
* errors.password becomes errors\0password\0\0
* allowing us to walk the string for each subkey
*/
struct chunk_t * chunk_new_macro( char *text ) {
struct chunk_t *t = (struct chunk_t*)calloc(1,sizeof(struct chunk_t));
int len;
char *p=text;
char *d;
if( text ) {
/* Copy over data with extra space for null and end null */
len = strlen(text);
d = t->text = malloc(len+2);
while( *p ) {
*d = *p;
if( *d == '.' ) *d = 0;
p++;
d++;
}
*d = *p;
}
t->next = 0;
}
/* Regular text chunk */
struct chunk_t * chunk_new( char * text ) {
struct chunk_t *t = (struct chunk_t*)calloc(1,sizeof(struct chunk_t));
if( text ) t->text = strdup(text);
t->next = 0;
}
struct chunk_t * template_load( char * source ) {
struct stat st;
char *bigbuff;
char *p;
char ch;
FILE *fp;
struct chunk_t *chunk;
struct chunk_t *iter;
template *tp;
template entry;
char finalpath[PATH_MAX];
sprintf( finalpath, "%s/%s", path, source );
/* Stat file to get the mtime and size */
if( stat( finalpath, &st ) < 0 ) {
fprintf(stderr,"Could not stat: %s %d\n", finalpath, errno );
return NULL;
}
/* See if we already have this sucker */
entry.path = finalpath;
if( (tp=sglib_template_find_member( template_cache, &entry )) != NULL ) {
/* We do if it hasn't modified - exit */
if( tp->mtime == st.st_mtime ) return tp->head;
/* Modified, free old data */
iter = tp->head;
while( iter ) {
chunk = iter;
iter = iter->next;
if( chunk->text ) free(chunk->text);
free(chunk);
}
} else {
/* New template */
tp = calloc(1, sizeof(template));
if( tp == NULL ) {
fprintf(stderr,"Could not allocate memory for template: %s %d\n", finalpath, errno );
return NULL;
}
tp->path = strdup(finalpath);
tp->mtime = st.st_mtime;
}
/* Allocate buffer for the template and read it in from the file */
bigbuff = malloc(st.st_size+1);
if( bigbuff == NULL ) {
fprintf(stderr,"Could not allocate memory to read: %s %d\n", finalpath, errno );
return NULL;
}
fp = fopen( finalpath, "r");
if( fp == NULL ) {
fprintf(stderr,"Could not open to read: %s %d\n", finalpath, errno );
free(bigbuff);
return NULL;
}
/* Possibly mmap and memcpy later */
p = bigbuff;
while( (ch=fgetc(fp)) != EOF ) {
*p = ch; p++;
}
*p = 0;
/* Parse the template, link in the chunk header */
chunk = template_parse( bigbuff );
tp->head = chunk;
sglib_template_add(&template_cache, tp);
fclose(fp);
free(bigbuff);
return chunk;
}
/**
* Parse macro and simple if statement
*/
struct chunk_t * parse_logic( char ** them ) {
char *p = *them;
char *start;
int have_if = 0;
int have_endif = 0;
int have_while = 0;
struct chunk_t * chunk=NULL;
char ch;
while( *p ) {
/* White space */
while( *p && *p == '\t' && *p == ' ' ) p++;
if( !*p ) return NULL;
if( *p == '$' ) {
start = p+1;
/* We have a straight expansion */
while( *p != '\t' && *p != ' ' ) p++;
if( !*p ) return NULL;
ch = *p;
*p = 0;/* Isolate macro name */
chunk = chunk_new_macro( start );
chunk->macro = 1;
chunk->isif = have_if;
*p = ch;
*them = p;
return chunk;
} else if( *p == '%' && *(p+1) == '>' ) {
/* Cloes tag */
fprintf( stderr, "Close tag without macro\n");
p += 2;
*them = p;
return chunk;
} else if( strncmp( p, "while", 5 ) == 0 ) {
/* While */
have_while = 1;
p+=5;
continue;
} else if( strncmp( p, "endwhile", 8 ) == 0 ) {
/* While */
chunk = chunk_new( NULL );
chunk->endwhile = 1;
p+=8;
return chunk;
} else if( *p == 'i' && *(p+1) == 'f' ) {
/* boolean logic */
have_if = 1;
p+=2;
continue;
} else if( strncmp( p, "endif", 5 ) == 0 ) {
/* close boolean logic */
chunk = chunk_new( NULL );
chunk->endif = 1;
return chunk;
p+=5;
}
p++;
}
*them = p;
return NULL;
}
struct chunk_t * template_parse( char * source ) {
char *p = source;
char *start = p;
struct chunk_t *head;
struct chunk_t *iter;
struct chunk_t *temp;
iter = head = chunk_new( "" );
while( *p ) {
if( *p == '<' && *(p+1) == '%' ) {
/* Open tag */
if( p != start ) {
/* Grab everthing after the last close tag up to the start tag */
*p = 0;
iter->next = chunk_new( start );
iter = iter->next;
*p = '<';
}
/* Skip the <% literal and parse the logic inside this tag */
p += 2;
temp = parse_logic( &p );
if( temp ) {
/* valid logic link it in */
iter->next = temp;
iter = iter->next;
}
} else if( *p == '%' && *(p+1) == '>' ) {
/* Record start of next text chunk */
start = p+2;
}
p++;
}
/* Grab any trailing text */
iter->next = chunk_new( start );
iter = iter->next;
return head;
}
#if 0
char temp2[] = "<% $dude %> Ouch <html></html>";
char temp3[] = "<% %> Ouch <html></html>";
char temp4[] = "<% if $d %> <% $crap %> Ouch <html></html>";
char temp1[] = " if $d %>";
int main() {
struct chunk_t *t;
struct array_t *arr = array_new();
template_init();
t = template_parse( temp1 );
while( t ) {
printf("{%s} ismacro: %x isif: %x\n", t->text, t->macro!=0, t->isif!=0);
t = t->next;
}
array_add_str( arr, "dog", "GONZO!");
// array_add_str( arr, "house", "doctor!");
template_run("test.tpl", arr);
}
#endif