comparison src/c/lacweb.c @ 190:3eb53c957d10

Checkboxes
author Adam Chlipala <adamc@hcoop.net>
date Thu, 07 Aug 2008 13:09:26 -0400
parents 20bf7487c370
children ab86aa858e6c
comparison
equal deleted inserted replaced
189:20bf7487c370 190:3eb53c957d10
16 char *heap, *heap_front, *heap_back; 16 char *heap, *heap_front, *heap_back;
17 char **inputs; 17 char **inputs;
18 18
19 jmp_buf jmp_buf; 19 jmp_buf jmp_buf;
20 20
21 failure_kind failure_kind;
22 char error_message[ERROR_BUF_LEN]; 21 char error_message[ERROR_BUF_LEN];
23 }; 22 };
24 23
25 extern int lw_inputs_len; 24 extern int lw_inputs_len;
26 25
33 ctx->heap_front = ctx->heap = malloc(heap_len); 32 ctx->heap_front = ctx->heap = malloc(heap_len);
34 ctx->heap_back = ctx->heap_front + heap_len; 33 ctx->heap_back = ctx->heap_front + heap_len;
35 34
36 ctx->inputs = calloc(lw_inputs_len, sizeof(char *)); 35 ctx->inputs = calloc(lw_inputs_len, sizeof(char *));
37 36
38 ctx->failure_kind = SUCCESS;
39 ctx->error_message[0] = 0; 37 ctx->error_message[0] = 0;
40 38
41 return ctx; 39 return ctx;
42 } 40 }
43 41
50 48
51 void lw_reset_keep_request(lw_context ctx) { 49 void lw_reset_keep_request(lw_context ctx) {
52 ctx->page_front = ctx->page; 50 ctx->page_front = ctx->page;
53 ctx->heap_front = ctx->heap; 51 ctx->heap_front = ctx->heap;
54 52
55 ctx->failure_kind = SUCCESS;
56 ctx->error_message[0] = 0; 53 ctx->error_message[0] = 0;
57 } 54 }
58 55
59 void lw_reset_keep_error_message(lw_context ctx) { 56 void lw_reset_keep_error_message(lw_context ctx) {
60 ctx->page_front = ctx->page; 57 ctx->page_front = ctx->page;
61 ctx->heap_front = ctx->heap; 58 ctx->heap_front = ctx->heap;
62
63 ctx->failure_kind = SUCCESS;
64 } 59 }
65 60
66 void lw_reset(lw_context ctx) { 61 void lw_reset(lw_context ctx) {
67 lw_reset_keep_request(ctx); 62 lw_reset_keep_request(ctx);
68 memset(ctx->inputs, 0, lw_inputs_len * sizeof(char *)); 63 memset(ctx->inputs, 0, lw_inputs_len * sizeof(char *));
69 } 64 }
70 65
71 void lw_handle(lw_context, char *); 66 void lw_handle(lw_context, char *);
72 67
73 failure_kind lw_begin(lw_context ctx, char *path) { 68 failure_kind lw_begin(lw_context ctx, char *path) {
74 if (!setjmp(ctx->jmp_buf)) 69 int r = setjmp(ctx->jmp_buf);
70
71 if (r == 0)
75 lw_handle(ctx, path); 72 lw_handle(ctx, path);
76 73
77 return ctx->failure_kind; 74 return r;
78 } 75 }
79 76
80 void lw_error(lw_context ctx, failure_kind fk, const char *fmt, ...) { 77 void lw_error(lw_context ctx, failure_kind fk, const char *fmt, ...) {
81 va_list ap; 78 va_list ap;
82 va_start(ap, fmt); 79 va_start(ap, fmt);
83 80
84 ctx->failure_kind = fk;
85 vsnprintf(ctx->error_message, ERROR_BUF_LEN, fmt, ap); 81 vsnprintf(ctx->error_message, ERROR_BUF_LEN, fmt, ap);
86 82
87 longjmp(ctx->jmp_buf, 1); 83 longjmp(ctx->jmp_buf, fk);
88 } 84 }
89 85
90 char *lw_error_message(lw_context ctx) { 86 char *lw_error_message(lw_context ctx) {
91 return ctx->error_message; 87 return ctx->error_message;
92 } 88 }
112 lw_error(ctx, FATAL, "Negative input index %d", n); 108 lw_error(ctx, FATAL, "Negative input index %d", n);
113 if (n >= lw_inputs_len) 109 if (n >= lw_inputs_len)
114 lw_error(ctx, FATAL, "Out-of-bounds input index %d", n); 110 lw_error(ctx, FATAL, "Out-of-bounds input index %d", n);
115 printf("[%d] = %s\n", n, ctx->inputs[n]); 111 printf("[%d] = %s\n", n, ctx->inputs[n]);
116 return ctx->inputs[n]; 112 return ctx->inputs[n];
113 }
114
115 char *lw_get_optional_input(lw_context ctx, int n) {
116 if (n < 0)
117 lw_error(ctx, FATAL, "Negative input index %d", n);
118 if (n >= lw_inputs_len)
119 lw_error(ctx, FATAL, "Out-of-bounds input index %d", n);
120 printf("[%d] = %s\n", n, ctx->inputs[n]);
121 return (ctx->inputs[n] == NULL ? "" : ctx->inputs[n]);
117 } 122 }
118 123
119 static void lw_check_heap(lw_context ctx, size_t extra) { 124 static void lw_check_heap(lw_context ctx, size_t extra) {
120 if (ctx->heap_back - ctx->heap_front < extra) { 125 if (ctx->heap_back - ctx->heap_front < extra) {
121 size_t desired = ctx->heap_back - ctx->heap_front + extra, next; 126 size_t desired = ctx->heap_back - ctx->heap_front + extra, next;