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
|
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <setjmp.h>
#include <stdarg.h>
#include "types.h"
lw_unit lw_unit_v = {};
#define ERROR_BUF_LEN 1024
struct lw_context {
char *page, *page_front, *page_back;
char *heap, *heap_front, *heap_back;
char **inputs;
jmp_buf jmp_buf;
failure_kind failure_kind;
char error_message[ERROR_BUF_LEN];
};
extern int lw_inputs_len;
lw_context lw_init(size_t page_len, size_t heap_len) {
lw_context ctx = malloc(sizeof(struct lw_context));
ctx->page_front = ctx->page = malloc(page_len);
ctx->page_back = ctx->page_front + page_len;
ctx->heap_front = ctx->heap = malloc(heap_len);
ctx->heap_back = ctx->heap_front + heap_len;
ctx->inputs = calloc(lw_inputs_len, sizeof(char *));
ctx->failure_kind = SUCCESS;
ctx->error_message[0] = 0;
return ctx;
}
void lw_free(lw_context ctx) {
free(ctx->page);
free(ctx->heap);
free(ctx->inputs);
free(ctx);
}
void lw_reset_keep_request(lw_context ctx) {
ctx->page_front = ctx->page;
ctx->heap_front = ctx->heap;
ctx->failure_kind = SUCCESS;
ctx->error_message[0] = 0;
}
void lw_reset_keep_error_message(lw_context ctx) {
ctx->page_front = ctx->page;
ctx->heap_front = ctx->heap;
ctx->failure_kind = SUCCESS;
}
void lw_reset(lw_context ctx) {
lw_reset_keep_request(ctx);
memset(ctx->inputs, 0, lw_inputs_len * sizeof(char *));
}
void lw_handle(lw_context, char *);
failure_kind lw_begin(lw_context ctx, char *path) {
if (!setjmp(ctx->jmp_buf))
lw_handle(ctx, path);
return ctx->failure_kind;
}
void lw_error(lw_context ctx, failure_kind fk, const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
ctx->failure_kind = fk;
vsnprintf(ctx->error_message, ERROR_BUF_LEN, fmt, ap);
longjmp(ctx->jmp_buf, 1);
}
char *lw_error_message(lw_context ctx) {
return ctx->error_message;
}
int lw_input_num(char*);
void lw_set_input(lw_context ctx, char *name, char *value) {
int n = lw_input_num(name);
if (n < 0)
lw_error(ctx, FATAL, "Bad input name %s", name);
if (n >= lw_inputs_len)
lw_error(ctx, FATAL, "For input name %s, index %d is out of range", name, n);
ctx->inputs[n] = value;
printf("[%d] %s = %s\n", n, name, value);
}
char *lw_get_input(lw_context ctx, int n) {
if (n < 0)
lw_error(ctx, FATAL, "Negative input index %d", n);
if (n >= lw_inputs_len)
lw_error(ctx, FATAL, "Out-of-bounds input index %d", n);
printf("[%d] = %s\n", n, ctx->inputs[n]);
return ctx->inputs[n];
}
static void lw_check_heap(lw_context ctx, size_t extra) {
if (ctx->heap_back - ctx->heap_front < extra) {
size_t desired = ctx->heap_back - ctx->heap_front + extra, next;
char *new_heap;
for (next = ctx->heap_back - ctx->heap_front; next < desired; next *= 2);
new_heap = realloc(ctx->heap, next);
ctx->heap_front = new_heap;
ctx->heap_back = new_heap + next;
if (new_heap != ctx->heap) {
ctx->heap = new_heap;
lw_error(ctx, UNLIMITED_RETRY, "Couldn't allocate new heap chunk contiguously");
}
ctx->heap = new_heap;
}
}
void *lw_malloc(lw_context ctx, size_t len) {
void *result;
lw_check_heap(ctx, len);
result = ctx->heap_front;
ctx->heap_front += len;
return result;
}
int lw_really_send(int sock, const void *buf, ssize_t len) {
while (len > 0) {
ssize_t n = send(sock, buf, len, 0);
if (n < 0)
return n;
buf += n;
len -= n;
}
return 0;
}
int lw_send(lw_context ctx, int sock) {
return lw_really_send(sock, ctx->page, ctx->page_front - ctx->page);
}
static void lw_check(lw_context ctx, size_t extra) {
size_t desired = ctx->page_back - ctx->page_front + extra, next;
char *new_page;
for (next = ctx->page_back - ctx->page_front; next < desired; next *= 2);
new_page = realloc(ctx->page, next);
ctx->page_front = new_page + (ctx->page_front - ctx->page);
ctx->page_back = new_page + (ctx->page_back - ctx->page);
ctx->page = new_page;
}
static void lw_writec_unsafe(lw_context ctx, char c) {
*(ctx->page_front)++ = c;
}
void lw_writec(lw_context ctx, char c) {
lw_check(ctx, 1);
lw_writec_unsafe(ctx, c);
}
static void lw_write_unsafe(lw_context ctx, const char* s) {
int len = strlen(s);
memcpy(ctx->page_front, s, len);
ctx->page_front += len;
}
void lw_write(lw_context ctx, const char* s) {
lw_check(ctx, strlen(s));
lw_write_unsafe(ctx, s);
}
#define INTS_MAX 50
#define FLOATS_MAX 100
char *lw_Basis_attrifyInt(lw_context ctx, lw_Basis_int n) {
char *result;
int len;
lw_check_heap(ctx, INTS_MAX);
result = ctx->heap_front;
sprintf(result, "%d%n", n, &len);
ctx->heap_front += len+1;
return result;
}
char *lw_Basis_attrifyFloat(lw_context ctx, lw_Basis_float n) {
char *result;
int len;
lw_check_heap(ctx, FLOATS_MAX);
result = ctx->heap_front;
sprintf(result, "%g%n", n, &len);
ctx->heap_front += len+1;
return result;
}
char *lw_Basis_attrifyString(lw_context ctx, lw_Basis_string s) {
int len = strlen(s);
char *result, *p;
lw_check_heap(ctx, len * 6 + 1);
result = p = ctx->heap_front;
for (; *s; s++) {
char c = *s;
if (c == '"') {
strcpy(p, """);
p += 6;
} else if (c == '&') {
strcpy(p, "&");
p += 5;
}
else if (isprint(c))
*p++ = c;
else {
int len2;
sprintf(p, "&#%d;%n", c, &len2);
p += len2;
}
}
*p++ = 0;
ctx->heap_front = p;
return result;
}
static void lw_Basis_attrifyInt_w_unsafe(lw_context ctx, lw_Basis_int n) {
int len;
sprintf(ctx->page_front, "%d%n", n, &len);
ctx->page_front += len;
}
void lw_Basis_attrifyInt_w(lw_context ctx, lw_Basis_int n) {
lw_check(ctx, INTS_MAX);
lw_Basis_attrifyInt_w_unsafe(ctx, n);
}
void lw_Basis_attrifyFloat_w(lw_context ctx, lw_Basis_float n) {
int len;
lw_check(ctx, FLOATS_MAX);
sprintf(ctx->page_front, "%g%n", n, &len);
ctx->page_front += len;
}
void lw_Basis_attrifyString_w(lw_context ctx, lw_Basis_string s) {
lw_check(ctx, strlen(s) * 6);
for (; *s; s++) {
char c = *s;
if (c == '"')
lw_write_unsafe(ctx, """);
else if (c == '&')
lw_write_unsafe(ctx, "&");
else if (isprint(c))
lw_writec_unsafe(ctx, c);
else {
lw_write_unsafe(ctx, "&#");
lw_Basis_attrifyInt_w_unsafe(ctx, c);
lw_writec_unsafe(ctx, ';');
}
}
}
char *lw_Basis_urlifyInt(lw_context ctx, lw_Basis_int n) {
int len;
char *r;
lw_check_heap(ctx, INTS_MAX);
r = ctx->heap_front;
sprintf(r, "%d%n", n, &len);
ctx->heap_front += len+1;
return r;
}
char *lw_Basis_urlifyFloat(lw_context ctx, lw_Basis_float n) {
int len;
char *r;
lw_check_heap(ctx, FLOATS_MAX);
r = ctx->heap_front;
sprintf(r, "%g%n", n, &len);
ctx->heap_front += len+1;
return r;
}
char *lw_Basis_urlifyString(lw_context ctx, lw_Basis_string s) {
char *r, *p;
lw_check_heap(ctx, strlen(s) * 3 + 1);
for (r = p = ctx->heap_front; *s; s++) {
char c = *s;
if (c == ' ')
*p++ = '+';
else if (isalnum(c))
*p++ = c;
else {
sprintf(p, "%%%02X", c);
p += 3;
}
}
*p++ = 0;
ctx->heap_front = p;
return r;
}
static void lw_Basis_urlifyInt_w_unsafe(lw_context ctx, lw_Basis_int n) {
int len;
sprintf(ctx->page_front, "%d%n", n, &len);
ctx->page_front += len;
}
void lw_Basis_urlifyInt_w(lw_context ctx, lw_Basis_int n) {
lw_check(ctx, INTS_MAX);
lw_Basis_urlifyInt_w_unsafe(ctx, n);
}
void lw_Basis_urlifyFloat_w(lw_context ctx, lw_Basis_float n) {
int len;
lw_check(ctx, FLOATS_MAX);
sprintf(ctx->page_front, "%g%n", n, &len);
ctx->page_front += len;
}
void lw_Basis_urlifyString_w(lw_context ctx, lw_Basis_string s) {
lw_check(ctx, strlen(s) * 3);
for (; *s; s++) {
char c = *s;
if (c == ' ')
lw_writec_unsafe(ctx, '+');
else if (isalnum(c))
lw_writec_unsafe(ctx, c);
else {
sprintf(ctx->page_front, "%%%02X", c);
ctx->page_front += 3;
}
}
}
static char *lw_unurlify_advance(char *s) {
char *new_s = strchr(s, '/');
if (new_s)
*new_s++ = 0;
else
new_s = strchr(s, 0);
return new_s;
}
lw_Basis_int lw_unurlifyInt(char **s) {
char *new_s = lw_unurlify_advance(*s);
int r;
r = atoi(*s);
*s = new_s;
return r;
}
lw_Basis_float lw_unurlifyFloat(char **s) {
char *new_s = lw_unurlify_advance(*s);
int r;
r = atof(*s);
*s = new_s;
return r;
}
static lw_Basis_string lw_unurlifyString_to(lw_context ctx, char *r, char *s) {
char *s1, *s2;
int n;
for (s1 = r, s2 = s; *s2; ++s1, ++s2) {
char c = *s2;
switch (c) {
case '+':
*s1 = ' ';
break;
case '%':
if (s2[1] == 0)
lw_error(ctx, FATAL, "Missing first character of escaped URL byte");
if (s2[2] == 0)
lw_error(ctx, FATAL, "Missing second character of escaped URL byte");
if (sscanf(s2+1, "%02X", &n) != 1)
lw_error(ctx, FATAL, "Invalid escaped URL byte starting at: %s", s2);
*s1 = n;
s2 += 2;
break;
default:
*s1 = c;
}
}
*s1++ = 0;
return s1;
}
lw_Basis_string lw_unurlifyString(lw_context ctx, char **s) {
char *new_s = lw_unurlify_advance(*s);
char *r, *s1, *s2;
int len, n;
len = strlen(*s);
lw_check_heap(ctx, len + 1);
r = ctx->heap_front;
ctx->heap_front = lw_unurlifyString_to(ctx, ctx->heap_front, *s);
*s = new_s;
return r;
}
char *lw_Basis_htmlifyString(lw_context ctx, lw_Basis_string s) {
char *r, *s2;
lw_check_heap(ctx, strlen(s) * 5 + 1);
for (r = s2 = ctx->heap_front; *s; s++) {
char c = *s;
switch (c) {
case '<':
strcpy(s2, "<");
s2 += 4;
break;
case '&':
strcpy(s2, "&");
s2 += 5;
break;
default:
if (isprint(c))
*s2++ = c;
else {
int len2;
sprintf(s2, "&#%d;%n", c, &len2);
s2 += len2;
}
}
}
*s2++ = 0;
ctx->heap_front = s2;
return r;
}
void lw_Basis_htmlifyString_w(lw_context ctx, lw_Basis_string s) {
lw_check(ctx, strlen(s) * 5);
for (; *s; s++) {
char c = *s;
switch (c) {
case '<':
lw_write_unsafe(ctx, "<");
break;
case '&':
lw_write_unsafe(ctx, "&");
break;
default:
if (isprint(c))
lw_writec_unsafe(ctx, c);
else {
lw_write_unsafe(ctx, "&#");
lw_Basis_attrifyInt_w_unsafe(ctx, c);
lw_writec_unsafe(ctx, ';');
}
}
}
}
lw_Basis_string lw_Basis_strcat(lw_context ctx, lw_Basis_string s1, lw_Basis_string s2) {
int len = strlen(s1) + strlen(s2) + 1;
char *s;
lw_check(ctx, len);
s = ctx->heap_front;
strcpy(s, s1);
strcat(s, s2);
ctx->heap_front += len;
return s;
}
|