summaryrefslogtreecommitdiff
path: root/src/idx.c
blob: 623acbba78f16086149f18447c1181dd79941c26 (plain)
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
#include <stdlib.h>
#include <stdio.h>
#include <sys/stat.h>
#include <errno.h>
#include <assert.h>
#include <string.h>
#include <sys/queue.h>
#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>
#include <time.h>
#include "fileserv.h"
#include "mimetypes.h"
#include "wordsplit.h"

enum index_node_type {
	NODE_TEXT,
	NODE_LOOP,
	NODE_COND,
	NODE_EXPR
};

typedef STAILQ_HEAD(index_node_list, index_node) INDEX_NODE_LIST;

struct index_cond {
	char *expr;
	INDEX_NODE_LIST branch[2];
};
						 
struct index_node {
	int type;
	STAILQ_ENTRY(index_node) next;
	union {
		char *text;
		struct index_node_list loop;
		struct index_cond cond;
	} v;
};

typedef struct index_node INDEX_NODE;

struct condition {
	INDEX_NODE *node;
	int brn;
	STAILQ_ENTRY(condition) next;
};
typedef STAILQ_HEAD(, condition) COND_LIST;

struct template_state {
	int escape:1;
	int loop:1;
	COND_LIST cond;
	char const *start;
	char const *cur;
};

static void parse_template(INDEX_NODE_LIST *nodes, struct template_state *st);

static INDEX_NODE *
new_text_node(int type, char const *start, size_t len)
{
	INDEX_NODE *np = xmalloc(sizeof(*np));
	np->type = type;
	np->v.text = xmalloc(len + 1);
	memcpy(np->v.text, start, len);
	np->v.text[len] = 0;
	return np;
}

static INDEX_NODE *
next_text_node(struct template_state *st)
{
	INDEX_NODE *np;
	size_t len;
	
	st->start = st->cur;
	while (*st->cur) {
		if (*st->cur == '{' && st->cur[1] == '%') {
			st->escape = 1;
			break;
		}
		st->cur++;
	}

	return new_text_node(NODE_TEXT, st->start, st->cur - st->start);
}

static INDEX_NODE *
parse_cond(struct template_state *st, int brn, size_t off, size_t len)
{
	struct condition cond;
	INDEX_NODE *np;

	while (off < len && isspace(st->start[off]))
		off++;
	
	np = xmalloc(sizeof(*np));
	np->type = NODE_COND;
	len -= off;
	np->v.cond.expr = xmalloc(len + 1);
	memcpy(np->v.cond.expr, st->start + off, len);
	np->v.cond.expr[len] = 0;
	
	cond.node = np;
	cond.brn = brn;
	STAILQ_INSERT_HEAD(&st->cond, &cond, next);
	STAILQ_INIT(&np->v.cond.branch[!cond.brn]);
	parse_template(&np->v.cond.branch[cond.brn], st);

	return np;
}

static INDEX_NODE *
next_escape_node(struct template_state *st)
{
	INDEX_NODE *np;
	size_t len;
	
	st->start = st->cur + 2;
	while (*st->cur) {
		if (*st->cur == '%' && st->cur[1] == '}') {
			st->escape = 0;
			break;
		}
		st->cur++;
	}

	np = xmalloc(sizeof(*np));
	if (st->escape) {
		st->escape = 0;
		return new_text_node(NODE_TEXT, st->start,
				     st->cur - st->start);
	}
	
	while (st->start < st->cur && isspace(st->start[0]))
		st->start++;

	len = st->cur - st->start;
	while (len > 0 && isspace(st->start[len-1]))
		len--;

	st->cur += 2;

	if (memcmp(st->start, "loop", len) == 0) {
		assert(st->loop == 0);
		st->loop = 1;
		np = xmalloc(sizeof(*np));
		np->type = NODE_LOOP;
		parse_template(&np->v.loop, st);
		return np;
	}

	if (memcmp(st->start, "endloop", len) == 0) {
		assert(st->loop);
		st->loop = 0;
		return NULL;
	}

	if (len > 2
	    && (memcmp(st->start, "if", 2) == 0	&& isspace(st->start[2]))) 
		return parse_cond(st, 1, 2, len);

	if (len > 6
	    && (memcmp(st->start, "unless", 6) == 0 && isspace(st->start[6])))
		return parse_cond(st, 0, 6, len);
		
	if (memcmp(st->start, "endif", len) == 0) {
		STAILQ_REMOVE_HEAD(&st->cond, next);
		return NULL;
	}
	    

	if (memcmp(st->start, "else", len) == 0) {
		struct condition *cond = STAILQ_FIRST(&st->cond);
		assert(cond);
		np = cond->node;
		assert(STAILQ_FIRST(&np->v.cond.branch[!cond->brn]) == NULL);
		parse_template(&np->v.cond.branch[!cond->brn], st);
		return NULL;
	}
	
	return new_text_node(NODE_EXPR, st->start, len);
}

static INDEX_NODE *
next_node(struct template_state *st)
{
	if (*st->cur == 0)
		return NULL;
	return (st->escape ? next_escape_node : next_text_node)(st);
}
	
static void
parse_template(INDEX_NODE_LIST *nodes, struct template_state *st)
{
	INDEX_NODE *np;
	
	STAILQ_INIT(nodes);
	
	while (np = next_node(st))
		STAILQ_INSERT_TAIL(nodes, np, next);
}

INDEX_NODE_LIST node_list = STAILQ_HEAD_INITIALIZER(node_list);

void
parse_template_string(char const *str)
{
	struct template_state state;

	state.escape = 0;
	state.loop = 0;
	STAILQ_INIT(&state.cond);
	state.start = str;
	state.cur = str;

	parse_template(&node_list, &state);

	assert(state.escape == 0);
	assert(state.loop == 0);
	assert(STAILQ_FIRST(&state.cond) == NULL);
}
	
void
parse_template_file(char const *file_name)
{
	struct stat st;
	char *buf;
	FILE *fp;
	ssize_t n;
	
	if (stat(file_name, &st)) {
		error("can't stat %s: %s", file_name, strerror(errno));
		return;
	}
	fp = fopen(file_name, "r");
	if (!fp) {
		error("can't open %s: %s", file_name, strerror(errno));
		return;
	}
		
	buf = xmalloc(st.st_size + 1);
	n = fread(buf, st.st_size, 1, fp);
	if (n == 1) {
		buf[st.st_size] = 0;
		parse_template_string(buf);
	} else
		error("error reading from %s: %s", file_name, strerror(errno));
	fclose(fp);
	free(buf);
}

typedef struct idxent {
	char *name;
	struct stat st;
	char const *type;
	STAILQ_ENTRY(idxent) next;
} IDXENT;
typedef STAILQ_HEAD(, idxent) IDXLIST;

void
idxlist_free(IDXLIST *lst)
{
	IDXENT *ent;
	
	while ((ent = STAILQ_FIRST(lst))) {
		STAILQ_REMOVE_HEAD(lst, next);
		free(ent->name);
		free(ent);
	}
}

static int
idxlist_scan(IDXLIST *idx_list, char *path, DIRCONFIG *conf)
{
	DIR *dir;
	struct dirent *ent;
	IDXENT *idxent;
	
	dir = opendir(path);
	if (!dir) {
		int ec = errno;
		error("can't open directory %s: %s",
		      path, strerror(ec));
		return ec;
	}
	STAILQ_INIT(idx_list);
	while ((ent = readdir(dir))) {
		char *name;
		struct stat st;
		
		if (ent->d_name[0] == '.'
		    && (ent->d_name[1] == 0
			|| (ent->d_name[1] == '.' && ent->d_name[2] == 0)))
			continue;

		name = catfile(path, ent->d_name);
		
		if (stat(name, &st)) {
			error("can't stat %s: %s", name, strerror(errno));
			free(name);
			continue;
		}

		if (conf->list_unreadable == 0) {
			if (access(name, R_OK)) {
				free(name);
				continue;
			}
		}

		idxent = xmalloc(sizeof(*idxent));
		idxent->name = xstrdup(ent->d_name);
		idxent->st = st;
		idxent->type = get_file_type(name);
		STAILQ_INSERT_TAIL(idx_list, idxent, next);
		free(name);
	}
	closedir(dir);

	/* FIXME: sort list */
	return 0;
}

typedef struct {
	int fd;
	DIRCONFIG *conf;
	IDXENT *ent;
	int n;
	struct wordsplit ws;
	int wsflags;
#define expansion ws.ws_wordv[0]
} EVAL_ENV;

static int
eval_expand(char const *str, EVAL_ENV *env)
{
	if (wordsplit(str, &env->ws, env->wsflags)) 
		return -1;
	env->wsflags |= WRDSF_REUSE;
	return 0;
}

static int
node_text_eval(INDEX_NODE *node, EVAL_ENV *env)
{
	size_t len = strlen(node->v.text);
	ssize_t n = write(env->fd, node->v.text, len);
	if (n != len) {
		if (n == -1)
			error("index write error: %s", strerror(errno));
		else
			error("index write error: %s", "disk full?");
		return -1;
	}
	return 0;
}

static int
node_expr_eval(INDEX_NODE *node, EVAL_ENV *env)	
{
	size_t len;
	ssize_t n;
	
	if (eval_expand(node->v.text, env)) 
		return -1;
	len = strlen(env->expansion);
	n = write(env->fd, env->expansion, len);
	if (n != len) {
		if (n == -1)
			error("index write error: %s", strerror(errno));
		else
			error("index write error: %s", "disk full?");
		return -1;
	}
	return 0;
}
	
static int
node_cond_eval(INDEX_NODE *node, EVAL_ENV *env)
{
	if (eval_expand(node->v.cond.expr, env)) 
		return -1;
	return node_list_eval(&node->v.cond.branch[env->expansion[0] != 0],
			      env);
}

static int
node_loop_eval(INDEX_NODE *node, EVAL_ENV *env)
{
	while (env->ent) {
		//FIXME: set variables?
		if (node_list_eval(&node->v.loop, env))
			return -1;
		env->ent = STAILQ_NEXT(env->ent, next);
		env->n++;
	}
	return 0;
}

static int (*eval[])(INDEX_NODE *node, EVAL_ENV *env) = {
	[NODE_TEXT] = node_text_eval,
	[NODE_LOOP] = node_loop_eval,
	[NODE_COND] = node_cond_eval,
	[NODE_EXPR] = node_expr_eval
};
		
int
node_list_eval(INDEX_NODE_LIST *nodes, EVAL_ENV *env)
{
	INDEX_NODE *node;

	STAILQ_FOREACH(node, nodes, next) {
		assert(node->type >= 0
		       && node->type < sizeof(eval)/sizeof(eval[0])
		       && eval[node->type]);
		if (eval[node->type](node, env))
			return -1;
	}
	return 0;
}

static inline int
streq(const char *v, const char *s, size_t l)
{
	return strncmp(v, s, l) == 0 && v[l] == 0;
}

static int
exp_rowclass(char **ret, EVAL_ENV *env)
{
	*ret = xstrdup(env->n % 2 ? "odd" : "even");
	return WRDSE_OK;
}

static int
exp_filename(char **ret, EVAL_ENV *env)
{
	if (env->ent) {
		*ret = xstrdup(env->ent->name);
		return WRDSE_OK;
	} else
		return WRDSE_UNDEF;
}

static int
exp_filesize(char **ret, EVAL_ENV *env)
{
	if (env->ent) {
		char const *suf[] = { "", "K", "M", "G", "T" };
		int i;
		double s = env->ent->st.st_size;
		char buf[80];
		
		for (i = 0; i < sizeof(suf)/sizeof(suf[0]); i++) {
			if (s < 1024)
				break;
			s /= 1024;
		}

		snprintf(buf, sizeof(buf), "%.1f%s", s, suf[i]);
		*ret = strdup(buf);
		return WRDSE_OK;
	} else
		return WRDSE_UNDEF;
}
	
static int
exp_filetype(char **ret, EVAL_ENV *env)
{
	if (env->ent && env->ent->type) {
		*ret = xstrdup(env->ent->type);
		return WRDSE_OK;
	} else
		return WRDSE_UNDEF;
} 

static int
exp_filetime(char **ret, EVAL_ENV *env)
{
	if (env->ent) {
		time_t t = env->ent->st.st_mtime;
		char buf[80];
		
		strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M", gmtime(&t));
		*ret = xstrdup(buf);
		return WRDSE_OK;
	} else
		return WRDSE_UNDEF;
}

struct var_dcl {
	char const *name;
	int (*exp)(char **, EVAL_ENV *);
};

static struct var_dcl var_dcl[] = {
	{ "ROWCLASS", exp_rowclass },
	{ "FILENAME", exp_filename },
	{ "FILESIZE", exp_filesize },
	{ "FILETIME", exp_filetime },
	{ NULL }
};

static int
template_getvar(char **ret, const char *var, size_t len, void *clos)
{
	EVAL_ENV *env = clos;
	struct var_dcl *vd;

	for (vd = var_dcl; vd->name; vd++) {
		if (streq(vd->name, var, len))
			return vd->exp(ret, env);
	}
	return WRDSE_UNDEF;
}

static int
cmd_alt(char **ret, char **argv, EVAL_ENV *env)
{
	char *s = strdup(argv[1] ? argv[1] : "");
	if (!s)
		return WRDSE_NOSPACE;
	*ret = s;
	for (; *s; s++)
		*s = toupper(*s);
	return WRDSE_OK;
}

static int
cmd_icon(char **ret, char **argv, EVAL_ENV *env)
{
	/* FIXME */
	*ret = strdup("");
	return *ret ? WRDSE_OK : WRDSE_NOSPACE;
}
	
static int
template_command(char **ret, const char *cmd, size_t len, char **argv,
		 void *clos)
{
	EVAL_ENV *env = clos;

	if (strcmp(argv[0], "icon") == 0) {
		return cmd_icon(ret, argv, clos);
	}

	if (strcmp(argv[0], "alt") == 0) {
		return cmd_alt(ret, argv, clos);
	}
	return WRDSE_UNDEF;
}

static char defidx[] =
#include "defidx.h"
	;	

int		
directory_index(int fd, DIRCONFIG *conf, char *uri, char *path)
{
	int rc;
	IDXLIST idxlist;
	EVAL_ENV env;
	char const *varenv[5];

	if (STAILQ_EMPTY(&node_list)) {
		parse_template_string(defidx);
		assert(!STAILQ_EMPTY(&node_list));
	}
	
	rc = idxlist_scan(&idxlist, path, conf);
	if (rc)
		return rc;

	env.fd = fd;
	env.conf = conf;
	env.ent = STAILQ_FIRST(&idxlist);
	env.n = 0;

	varenv[0] = "URI";
	varenv[1] = uri;
	varenv[2] = "INDEXCSS";
	varenv[3] = index_css;
	varenv[4] = NULL;
	
	env.ws.ws_error = error;
	env.ws.ws_getvar = template_getvar;
	env.ws.ws_command = template_command;
	env.ws.ws_closure = &env;
	env.ws.ws_env = varenv;
	env.ws.ws_options = WRDSO_ARGV;
	
	env.wsflags = WRDSF_NOSPLIT
		| WRDSF_WS
		| WRDSF_ERROR
		| WRDSF_SHOWERR 
		| WRDSF_GETVAR 
		| WRDSF_CLOSURE
		| WRDSF_OPTIONS
		| WRDSF_ENV
		| WRDSF_ENV_KV 
	        ;

	rc = node_list_eval(&node_list, &env);
	
	idxlist_free(&idxlist);
        if (env.wsflags & WRDSF_REUSE)
		wordsplit_free(&env.ws);
	return rc;
}

Return to:

Send suggestions and report system problems to the System administrator.