comparison src/c/urweb.c @ 311:9ad92047a499

Rename 'lw' prefixes to 'uw'
author Adam Chlipala <adamc@hcoop.net>
date Sun, 07 Sep 2008 15:40:42 -0400
parents 5dc11235129d
children 6a4e365db60c
comparison
equal deleted inserted replaced
310:0aee86b8a6d6 311:9ad92047a499
5 #include <setjmp.h> 5 #include <setjmp.h>
6 #include <stdarg.h> 6 #include <stdarg.h>
7 7
8 #include "types.h" 8 #include "types.h"
9 9
10 lw_unit lw_unit_v = {}; 10 uw_unit uw_unit_v = {};
11 11
12 #define ERROR_BUF_LEN 1024 12 #define ERROR_BUF_LEN 1024
13 13
14 struct lw_context { 14 struct uw_context {
15 char *page, *page_front, *page_back; 15 char *page, *page_front, *page_back;
16 char *heap, *heap_front, *heap_back; 16 char *heap, *heap_front, *heap_back;
17 char **inputs; 17 char **inputs;
18 18
19 void *db; 19 void *db;
21 jmp_buf jmp_buf; 21 jmp_buf jmp_buf;
22 22
23 char error_message[ERROR_BUF_LEN]; 23 char error_message[ERROR_BUF_LEN];
24 }; 24 };
25 25
26 extern int lw_inputs_len; 26 extern int uw_inputs_len;
27 27
28 lw_context lw_init(size_t page_len, size_t heap_len) { 28 uw_context uw_init(size_t page_len, size_t heap_len) {
29 lw_context ctx = malloc(sizeof(struct lw_context)); 29 uw_context ctx = malloc(sizeof(struct uw_context));
30 30
31 ctx->page_front = ctx->page = malloc(page_len); 31 ctx->page_front = ctx->page = malloc(page_len);
32 ctx->page_back = ctx->page_front + page_len; 32 ctx->page_back = ctx->page_front + page_len;
33 33
34 ctx->heap_front = ctx->heap = malloc(heap_len); 34 ctx->heap_front = ctx->heap = malloc(heap_len);
35 ctx->heap_back = ctx->heap_front + heap_len; 35 ctx->heap_back = ctx->heap_front + heap_len;
36 36
37 ctx->inputs = calloc(lw_inputs_len, sizeof(char *)); 37 ctx->inputs = calloc(uw_inputs_len, sizeof(char *));
38 38
39 ctx->db = NULL; 39 ctx->db = NULL;
40 40
41 ctx->error_message[0] = 0; 41 ctx->error_message[0] = 0;
42 42
43 return ctx; 43 return ctx;
44 } 44 }
45 45
46 void lw_set_db(lw_context ctx, void *db) { 46 void uw_set_db(uw_context ctx, void *db) {
47 ctx->db = db; 47 ctx->db = db;
48 } 48 }
49 49
50 void *lw_get_db(lw_context ctx) { 50 void *uw_get_db(uw_context ctx) {
51 return ctx->db; 51 return ctx->db;
52 } 52 }
53 53
54 void lw_free(lw_context ctx) { 54 void uw_free(uw_context ctx) {
55 free(ctx->page); 55 free(ctx->page);
56 free(ctx->heap); 56 free(ctx->heap);
57 free(ctx->inputs); 57 free(ctx->inputs);
58 free(ctx); 58 free(ctx);
59 } 59 }
60 60
61 void lw_reset_keep_request(lw_context ctx) { 61 void uw_reset_keep_request(uw_context ctx) {
62 ctx->page_front = ctx->page; 62 ctx->page_front = ctx->page;
63 ctx->heap_front = ctx->heap; 63 ctx->heap_front = ctx->heap;
64 64
65 ctx->error_message[0] = 0; 65 ctx->error_message[0] = 0;
66 } 66 }
67 67
68 void lw_reset_keep_error_message(lw_context ctx) { 68 void uw_reset_keep_error_message(uw_context ctx) {
69 ctx->page_front = ctx->page; 69 ctx->page_front = ctx->page;
70 ctx->heap_front = ctx->heap; 70 ctx->heap_front = ctx->heap;
71 } 71 }
72 72
73 void lw_reset(lw_context ctx) { 73 void uw_reset(uw_context ctx) {
74 lw_reset_keep_request(ctx); 74 uw_reset_keep_request(ctx);
75 memset(ctx->inputs, 0, lw_inputs_len * sizeof(char *)); 75 memset(ctx->inputs, 0, uw_inputs_len * sizeof(char *));
76 } 76 }
77 77
78 void lw_db_init(lw_context); 78 void uw_db_init(uw_context);
79 void lw_handle(lw_context, char *); 79 void uw_handle(uw_context, char *);
80 80
81 failure_kind lw_begin_init(lw_context ctx) { 81 failure_kind uw_begin_init(uw_context ctx) {
82 int r = setjmp(ctx->jmp_buf); 82 int r = setjmp(ctx->jmp_buf);
83 83
84 if (r == 0) 84 if (r == 0)
85 lw_db_init(ctx); 85 uw_db_init(ctx);
86 86
87 return r; 87 return r;
88 } 88 }
89 89
90 failure_kind lw_begin(lw_context ctx, char *path) { 90 failure_kind uw_begin(uw_context ctx, char *path) {
91 int r = setjmp(ctx->jmp_buf); 91 int r = setjmp(ctx->jmp_buf);
92 92
93 if (r == 0) 93 if (r == 0)
94 lw_handle(ctx, path); 94 uw_handle(ctx, path);
95 95
96 return r; 96 return r;
97 } 97 }
98 98
99 __attribute__((noreturn)) void lw_error(lw_context ctx, failure_kind fk, const char *fmt, ...) { 99 __attribute__((noreturn)) void uw_error(uw_context ctx, failure_kind fk, const char *fmt, ...) {
100 va_list ap; 100 va_list ap;
101 va_start(ap, fmt); 101 va_start(ap, fmt);
102 102
103 vsnprintf(ctx->error_message, ERROR_BUF_LEN, fmt, ap); 103 vsnprintf(ctx->error_message, ERROR_BUF_LEN, fmt, ap);
104 104
105 longjmp(ctx->jmp_buf, fk); 105 longjmp(ctx->jmp_buf, fk);
106 } 106 }
107 107
108 char *lw_error_message(lw_context ctx) { 108 char *uw_error_message(uw_context ctx) {
109 return ctx->error_message; 109 return ctx->error_message;
110 } 110 }
111 111
112 int lw_input_num(char*); 112 int uw_input_num(char*);
113 113
114 void lw_set_input(lw_context ctx, char *name, char *value) { 114 void uw_set_input(uw_context ctx, char *name, char *value) {
115 int n = lw_input_num(name); 115 int n = uw_input_num(name);
116 116
117 if (n < 0) 117 if (n < 0)
118 lw_error(ctx, FATAL, "Bad input name %s", name); 118 uw_error(ctx, FATAL, "Bad input name %s", name);
119 119
120 if (n >= lw_inputs_len) 120 if (n >= uw_inputs_len)
121 lw_error(ctx, FATAL, "For input name %s, index %d is out of range", name, n); 121 uw_error(ctx, FATAL, "For input name %s, index %d is out of range", name, n);
122 122
123 ctx->inputs[n] = value; 123 ctx->inputs[n] = value;
124 124
125 //printf("[%d] %s = %s\n", n, name, value); 125 //printf("[%d] %s = %s\n", n, name, value);
126 } 126 }
127 127
128 char *lw_get_input(lw_context ctx, int n) { 128 char *uw_get_input(uw_context ctx, int n) {
129 if (n < 0) 129 if (n < 0)
130 lw_error(ctx, FATAL, "Negative input index %d", n); 130 uw_error(ctx, FATAL, "Negative input index %d", n);
131 if (n >= lw_inputs_len) 131 if (n >= uw_inputs_len)
132 lw_error(ctx, FATAL, "Out-of-bounds input index %d", n); 132 uw_error(ctx, FATAL, "Out-of-bounds input index %d", n);
133 //printf("[%d] = %s\n", n, ctx->inputs[n]); 133 //printf("[%d] = %s\n", n, ctx->inputs[n]);
134 return ctx->inputs[n]; 134 return ctx->inputs[n];
135 } 135 }
136 136
137 char *lw_get_optional_input(lw_context ctx, int n) { 137 char *uw_get_optional_input(uw_context ctx, int n) {
138 if (n < 0) 138 if (n < 0)
139 lw_error(ctx, FATAL, "Negative input index %d", n); 139 uw_error(ctx, FATAL, "Negative input index %d", n);
140 if (n >= lw_inputs_len) 140 if (n >= uw_inputs_len)
141 lw_error(ctx, FATAL, "Out-of-bounds input index %d", n); 141 uw_error(ctx, FATAL, "Out-of-bounds input index %d", n);
142 printf("[%d] = %s\n", n, ctx->inputs[n]); 142 printf("[%d] = %s\n", n, ctx->inputs[n]);
143 return (ctx->inputs[n] == NULL ? "" : ctx->inputs[n]); 143 return (ctx->inputs[n] == NULL ? "" : ctx->inputs[n]);
144 } 144 }
145 145
146 static void lw_check_heap(lw_context ctx, size_t extra) { 146 static void uw_check_heap(uw_context ctx, size_t extra) {
147 if (ctx->heap_back - ctx->heap_front < extra) { 147 if (ctx->heap_back - ctx->heap_front < extra) {
148 size_t desired = ctx->heap_back - ctx->heap_front + extra, next; 148 size_t desired = ctx->heap_back - ctx->heap_front + extra, next;
149 char *new_heap; 149 char *new_heap;
150 150
151 for (next = ctx->heap_back - ctx->heap_front; next < desired; next *= 2); 151 for (next = ctx->heap_back - ctx->heap_front; next < desired; next *= 2);
154 ctx->heap_front = new_heap; 154 ctx->heap_front = new_heap;
155 ctx->heap_back = new_heap + next; 155 ctx->heap_back = new_heap + next;
156 156
157 if (new_heap != ctx->heap) { 157 if (new_heap != ctx->heap) {
158 ctx->heap = new_heap; 158 ctx->heap = new_heap;
159 lw_error(ctx, UNLIMITED_RETRY, "Couldn't allocate new heap chunk contiguously"); 159 uw_error(ctx, UNLIMITED_RETRY, "Couldn't allocate new heap chunk contiguously");
160 } 160 }
161 161
162 ctx->heap = new_heap; 162 ctx->heap = new_heap;
163 } 163 }
164 } 164 }
165 165
166 void *lw_malloc(lw_context ctx, size_t len) { 166 void *uw_malloc(uw_context ctx, size_t len) {
167 void *result; 167 void *result;
168 168
169 lw_check_heap(ctx, len); 169 uw_check_heap(ctx, len);
170 170
171 result = ctx->heap_front; 171 result = ctx->heap_front;
172 ctx->heap_front += len; 172 ctx->heap_front += len;
173 return result; 173 return result;
174 } 174 }
175 175
176 int lw_really_send(int sock, const void *buf, ssize_t len) { 176 int uw_really_send(int sock, const void *buf, ssize_t len) {
177 while (len > 0) { 177 while (len > 0) {
178 ssize_t n = send(sock, buf, len, 0); 178 ssize_t n = send(sock, buf, len, 0);
179 179
180 if (n < 0) 180 if (n < 0)
181 return n; 181 return n;
185 } 185 }
186 186
187 return 0; 187 return 0;
188 } 188 }
189 189
190 int lw_send(lw_context ctx, int sock) { 190 int uw_send(uw_context ctx, int sock) {
191 return lw_really_send(sock, ctx->page, ctx->page_front - ctx->page); 191 return uw_really_send(sock, ctx->page, ctx->page_front - ctx->page);
192 } 192 }
193 193
194 static void lw_check(lw_context ctx, size_t extra) { 194 static void uw_check(uw_context ctx, size_t extra) {
195 size_t desired = ctx->page_back - ctx->page_front + extra, next; 195 size_t desired = ctx->page_back - ctx->page_front + extra, next;
196 char *new_page; 196 char *new_page;
197 197
198 for (next = ctx->page_back - ctx->page_front; next < desired; next *= 2); 198 for (next = ctx->page_back - ctx->page_front; next < desired; next *= 2);
199 199
201 ctx->page_front = new_page + (ctx->page_front - ctx->page); 201 ctx->page_front = new_page + (ctx->page_front - ctx->page);
202 ctx->page_back = new_page + (ctx->page_back - ctx->page); 202 ctx->page_back = new_page + (ctx->page_back - ctx->page);
203 ctx->page = new_page; 203 ctx->page = new_page;
204 } 204 }
205 205
206 static void lw_writec_unsafe(lw_context ctx, char c) { 206 static void uw_writec_unsafe(uw_context ctx, char c) {
207 *(ctx->page_front)++ = c; 207 *(ctx->page_front)++ = c;
208 } 208 }
209 209
210 void lw_writec(lw_context ctx, char c) { 210 void uw_writec(uw_context ctx, char c) {
211 lw_check(ctx, 1); 211 uw_check(ctx, 1);
212 lw_writec_unsafe(ctx, c); 212 uw_writec_unsafe(ctx, c);
213 } 213 }
214 214
215 static void lw_write_unsafe(lw_context ctx, const char* s) { 215 static void uw_write_unsafe(uw_context ctx, const char* s) {
216 int len = strlen(s); 216 int len = strlen(s);
217 memcpy(ctx->page_front, s, len); 217 memcpy(ctx->page_front, s, len);
218 ctx->page_front += len; 218 ctx->page_front += len;
219 } 219 }
220 220
221 void lw_write(lw_context ctx, const char* s) { 221 void uw_write(uw_context ctx, const char* s) {
222 lw_check(ctx, strlen(s) + 1); 222 uw_check(ctx, strlen(s) + 1);
223 lw_write_unsafe(ctx, s); 223 uw_write_unsafe(ctx, s);
224 *ctx->page_front = 0; 224 *ctx->page_front = 0;
225 } 225 }
226 226
227 227
228 char *lw_Basis_attrifyInt(lw_context ctx, lw_Basis_int n) { 228 char *uw_Basis_attrifyInt(uw_context ctx, uw_Basis_int n) {
229 char *result; 229 char *result;
230 int len; 230 int len;
231 lw_check_heap(ctx, INTS_MAX); 231 uw_check_heap(ctx, INTS_MAX);
232 result = ctx->heap_front; 232 result = ctx->heap_front;
233 sprintf(result, "%lld%n", n, &len); 233 sprintf(result, "%lld%n", n, &len);
234 ctx->heap_front += len+1; 234 ctx->heap_front += len+1;
235 return result; 235 return result;
236 } 236 }
237 237
238 char *lw_Basis_attrifyFloat(lw_context ctx, lw_Basis_float n) { 238 char *uw_Basis_attrifyFloat(uw_context ctx, uw_Basis_float n) {
239 char *result; 239 char *result;
240 int len; 240 int len;
241 lw_check_heap(ctx, FLOATS_MAX); 241 uw_check_heap(ctx, FLOATS_MAX);
242 result = ctx->heap_front; 242 result = ctx->heap_front;
243 sprintf(result, "%g%n", n, &len); 243 sprintf(result, "%g%n", n, &len);
244 ctx->heap_front += len+1; 244 ctx->heap_front += len+1;
245 return result; 245 return result;
246 } 246 }
247 247
248 char *lw_Basis_attrifyString(lw_context ctx, lw_Basis_string s) { 248 char *uw_Basis_attrifyString(uw_context ctx, uw_Basis_string s) {
249 int len = strlen(s); 249 int len = strlen(s);
250 char *result, *p; 250 char *result, *p;
251 lw_check_heap(ctx, len * 6 + 1); 251 uw_check_heap(ctx, len * 6 + 1);
252 252
253 result = p = ctx->heap_front; 253 result = p = ctx->heap_front;
254 254
255 for (; *s; s++) { 255 for (; *s; s++) {
256 char c = *s; 256 char c = *s;
274 *p++ = 0; 274 *p++ = 0;
275 ctx->heap_front = p; 275 ctx->heap_front = p;
276 return result; 276 return result;
277 } 277 }
278 278
279 static void lw_Basis_attrifyInt_w_unsafe(lw_context ctx, lw_Basis_int n) { 279 static void uw_Basis_attrifyInt_w_unsafe(uw_context ctx, uw_Basis_int n) {
280 int len; 280 int len;
281 281
282 sprintf(ctx->page_front, "%lld%n", n, &len); 282 sprintf(ctx->page_front, "%lld%n", n, &len);
283 ctx->page_front += len; 283 ctx->page_front += len;
284 } 284 }
285 285
286 void lw_Basis_attrifyInt_w(lw_context ctx, lw_Basis_int n) { 286 void uw_Basis_attrifyInt_w(uw_context ctx, uw_Basis_int n) {
287 lw_check(ctx, INTS_MAX); 287 uw_check(ctx, INTS_MAX);
288 lw_Basis_attrifyInt_w_unsafe(ctx, n); 288 uw_Basis_attrifyInt_w_unsafe(ctx, n);
289 } 289 }
290 290
291 void lw_Basis_attrifyFloat_w(lw_context ctx, lw_Basis_float n) { 291 void uw_Basis_attrifyFloat_w(uw_context ctx, uw_Basis_float n) {
292 int len; 292 int len;
293 293
294 lw_check(ctx, FLOATS_MAX); 294 uw_check(ctx, FLOATS_MAX);
295 sprintf(ctx->page_front, "%g%n", n, &len); 295 sprintf(ctx->page_front, "%g%n", n, &len);
296 ctx->page_front += len; 296 ctx->page_front += len;
297 } 297 }
298 298
299 void lw_Basis_attrifyString_w(lw_context ctx, lw_Basis_string s) { 299 void uw_Basis_attrifyString_w(uw_context ctx, uw_Basis_string s) {
300 lw_check(ctx, strlen(s) * 6); 300 uw_check(ctx, strlen(s) * 6);
301 301
302 for (; *s; s++) { 302 for (; *s; s++) {
303 char c = *s; 303 char c = *s;
304 304
305 if (c == '"') 305 if (c == '"')
306 lw_write_unsafe(ctx, "&quot;"); 306 uw_write_unsafe(ctx, "&quot;");
307 else if (c == '&') 307 else if (c == '&')
308 lw_write_unsafe(ctx, "&amp;"); 308 uw_write_unsafe(ctx, "&amp;");
309 else if (isprint(c)) 309 else if (isprint(c))
310 lw_writec_unsafe(ctx, c); 310 uw_writec_unsafe(ctx, c);
311 else { 311 else {
312 lw_write_unsafe(ctx, "&#"); 312 uw_write_unsafe(ctx, "&#");
313 lw_Basis_attrifyInt_w_unsafe(ctx, c); 313 uw_Basis_attrifyInt_w_unsafe(ctx, c);
314 lw_writec_unsafe(ctx, ';'); 314 uw_writec_unsafe(ctx, ';');
315 } 315 }
316 } 316 }
317 } 317 }
318 318
319 319
320 char *lw_Basis_urlifyInt(lw_context ctx, lw_Basis_int n) { 320 char *uw_Basis_urlifyInt(uw_context ctx, uw_Basis_int n) {
321 int len; 321 int len;
322 char *r; 322 char *r;
323 323
324 lw_check_heap(ctx, INTS_MAX); 324 uw_check_heap(ctx, INTS_MAX);
325 r = ctx->heap_front; 325 r = ctx->heap_front;
326 sprintf(r, "%lld%n", n, &len); 326 sprintf(r, "%lld%n", n, &len);
327 ctx->heap_front += len+1; 327 ctx->heap_front += len+1;
328 return r; 328 return r;
329 } 329 }
330 330
331 char *lw_Basis_urlifyFloat(lw_context ctx, lw_Basis_float n) { 331 char *uw_Basis_urlifyFloat(uw_context ctx, uw_Basis_float n) {
332 int len; 332 int len;
333 char *r; 333 char *r;
334 334
335 lw_check_heap(ctx, FLOATS_MAX); 335 uw_check_heap(ctx, FLOATS_MAX);
336 r = ctx->heap_front; 336 r = ctx->heap_front;
337 sprintf(r, "%g%n", n, &len); 337 sprintf(r, "%g%n", n, &len);
338 ctx->heap_front += len+1; 338 ctx->heap_front += len+1;
339 return r; 339 return r;
340 } 340 }
341 341
342 char *lw_Basis_urlifyString(lw_context ctx, lw_Basis_string s) { 342 char *uw_Basis_urlifyString(uw_context ctx, uw_Basis_string s) {
343 char *r, *p; 343 char *r, *p;
344 344
345 lw_check_heap(ctx, strlen(s) * 3 + 1); 345 uw_check_heap(ctx, strlen(s) * 3 + 1);
346 346
347 for (r = p = ctx->heap_front; *s; s++) { 347 for (r = p = ctx->heap_front; *s; s++) {
348 char c = *s; 348 char c = *s;
349 349
350 if (c == ' ') 350 if (c == ' ')
360 *p++ = 0; 360 *p++ = 0;
361 ctx->heap_front = p; 361 ctx->heap_front = p;
362 return r; 362 return r;
363 } 363 }
364 364
365 char *lw_Basis_urlifyBool(lw_context ctx, lw_Basis_bool b) { 365 char *uw_Basis_urlifyBool(uw_context ctx, uw_Basis_bool b) {
366 if (b == lw_Basis_False) 366 if (b == uw_Basis_False)
367 return "0"; 367 return "0";
368 else 368 else
369 return "1"; 369 return "1";
370 } 370 }
371 371
372 static void lw_Basis_urlifyInt_w_unsafe(lw_context ctx, lw_Basis_int n) { 372 static void uw_Basis_urlifyInt_w_unsafe(uw_context ctx, uw_Basis_int n) {
373 int len; 373 int len;
374 374
375 sprintf(ctx->page_front, "%lld%n", n, &len); 375 sprintf(ctx->page_front, "%lld%n", n, &len);
376 ctx->page_front += len; 376 ctx->page_front += len;
377 } 377 }
378 378
379 void lw_Basis_urlifyInt_w(lw_context ctx, lw_Basis_int n) { 379 void uw_Basis_urlifyInt_w(uw_context ctx, uw_Basis_int n) {
380 lw_check(ctx, INTS_MAX); 380 uw_check(ctx, INTS_MAX);
381 lw_Basis_urlifyInt_w_unsafe(ctx, n); 381 uw_Basis_urlifyInt_w_unsafe(ctx, n);
382 } 382 }
383 383
384 void lw_Basis_urlifyFloat_w(lw_context ctx, lw_Basis_float n) { 384 void uw_Basis_urlifyFloat_w(uw_context ctx, uw_Basis_float n) {
385 int len; 385 int len;
386 386
387 lw_check(ctx, FLOATS_MAX); 387 uw_check(ctx, FLOATS_MAX);
388 sprintf(ctx->page_front, "%g%n", n, &len); 388 sprintf(ctx->page_front, "%g%n", n, &len);
389 ctx->page_front += len; 389 ctx->page_front += len;
390 } 390 }
391 391
392 void lw_Basis_urlifyString_w(lw_context ctx, lw_Basis_string s) { 392 void uw_Basis_urlifyString_w(uw_context ctx, uw_Basis_string s) {
393 lw_check(ctx, strlen(s) * 3); 393 uw_check(ctx, strlen(s) * 3);
394 394
395 for (; *s; s++) { 395 for (; *s; s++) {
396 char c = *s; 396 char c = *s;
397 397
398 if (c == ' ') 398 if (c == ' ')
399 lw_writec_unsafe(ctx, '+'); 399 uw_writec_unsafe(ctx, '+');
400 else if (isalnum(c)) 400 else if (isalnum(c))
401 lw_writec_unsafe(ctx, c); 401 uw_writec_unsafe(ctx, c);
402 else { 402 else {
403 sprintf(ctx->page_front, "%%%02X", c); 403 sprintf(ctx->page_front, "%%%02X", c);
404 ctx->page_front += 3; 404 ctx->page_front += 3;
405 } 405 }
406 } 406 }
407 } 407 }
408 408
409 void lw_Basis_urlifyBool_w(lw_context ctx, lw_Basis_bool b) { 409 void uw_Basis_urlifyBool_w(uw_context ctx, uw_Basis_bool b) {
410 if (b == lw_Basis_False) 410 if (b == uw_Basis_False)
411 lw_writec(ctx, '0'); 411 uw_writec(ctx, '0');
412 else 412 else
413 lw_writec(ctx, '1'); 413 uw_writec(ctx, '1');
414 } 414 }
415 415
416 416
417 static char *lw_unurlify_advance(char *s) { 417 static char *uw_unurlify_advance(char *s) {
418 char *new_s = strchr(s, '/'); 418 char *new_s = strchr(s, '/');
419 419
420 if (new_s) 420 if (new_s)
421 *new_s++ = 0; 421 *new_s++ = 0;
422 else 422 else
423 new_s = strchr(s, 0); 423 new_s = strchr(s, 0);
424 424
425 return new_s; 425 return new_s;
426 } 426 }
427 427
428 lw_Basis_int lw_Basis_unurlifyInt(lw_context ctx, char **s) { 428 uw_Basis_int uw_Basis_unurlifyInt(uw_context ctx, char **s) {
429 char *new_s = lw_unurlify_advance(*s); 429 char *new_s = uw_unurlify_advance(*s);
430 lw_Basis_int r; 430 uw_Basis_int r;
431 431
432 r = atoll(*s); 432 r = atoll(*s);
433 *s = new_s; 433 *s = new_s;
434 return r; 434 return r;
435 } 435 }
436 436
437 lw_Basis_float lw_Basis_unurlifyFloat(lw_context ctx, char **s) { 437 uw_Basis_float uw_Basis_unurlifyFloat(uw_context ctx, char **s) {
438 char *new_s = lw_unurlify_advance(*s); 438 char *new_s = uw_unurlify_advance(*s);
439 lw_Basis_float r; 439 uw_Basis_float r;
440 440
441 r = atof(*s); 441 r = atof(*s);
442 *s = new_s; 442 *s = new_s;
443 return r; 443 return r;
444 } 444 }
445 445
446 static lw_Basis_string lw_unurlifyString_to(lw_context ctx, char *r, char *s) { 446 static uw_Basis_string uw_unurlifyString_to(uw_context ctx, char *r, char *s) {
447 char *s1, *s2; 447 char *s1, *s2;
448 int n; 448 int n;
449 449
450 for (s1 = r, s2 = s; *s2; ++s1, ++s2) { 450 for (s1 = r, s2 = s; *s2; ++s1, ++s2) {
451 char c = *s2; 451 char c = *s2;
454 case '+': 454 case '+':
455 *s1 = ' '; 455 *s1 = ' ';
456 break; 456 break;
457 case '%': 457 case '%':
458 if (s2[1] == 0) 458 if (s2[1] == 0)
459 lw_error(ctx, FATAL, "Missing first character of escaped URL byte"); 459 uw_error(ctx, FATAL, "Missing first character of escaped URL byte");
460 if (s2[2] == 0) 460 if (s2[2] == 0)
461 lw_error(ctx, FATAL, "Missing second character of escaped URL byte"); 461 uw_error(ctx, FATAL, "Missing second character of escaped URL byte");
462 if (sscanf(s2+1, "%02X", &n) != 1) 462 if (sscanf(s2+1, "%02X", &n) != 1)
463 lw_error(ctx, FATAL, "Invalid escaped URL byte starting at: %s", s2); 463 uw_error(ctx, FATAL, "Invalid escaped URL byte starting at: %s", s2);
464 *s1 = n; 464 *s1 = n;
465 s2 += 2; 465 s2 += 2;
466 break; 466 break;
467 default: 467 default:
468 *s1 = c; 468 *s1 = c;
470 } 470 }
471 *s1++ = 0; 471 *s1++ = 0;
472 return s1; 472 return s1;
473 } 473 }
474 474
475 lw_Basis_bool lw_Basis_unurlifyBool(lw_context ctx, char **s) { 475 uw_Basis_bool uw_Basis_unurlifyBool(uw_context ctx, char **s) {
476 char *new_s = lw_unurlify_advance(*s); 476 char *new_s = uw_unurlify_advance(*s);
477 lw_Basis_bool r; 477 uw_Basis_bool r;
478 478
479 if (*s[0] == 0 || !strcmp(*s, "0") || !strcmp(*s, "off")) 479 if (*s[0] == 0 || !strcmp(*s, "0") || !strcmp(*s, "off"))
480 r = lw_Basis_False; 480 r = uw_Basis_False;
481 else 481 else
482 r = lw_Basis_True; 482 r = uw_Basis_True;
483 483
484 *s = new_s; 484 *s = new_s;
485 return r; 485 return r;
486 } 486 }
487 487
488 lw_Basis_string lw_Basis_unurlifyString(lw_context ctx, char **s) { 488 uw_Basis_string uw_Basis_unurlifyString(uw_context ctx, char **s) {
489 char *new_s = lw_unurlify_advance(*s); 489 char *new_s = uw_unurlify_advance(*s);
490 char *r, *s1, *s2; 490 char *r, *s1, *s2;
491 int len, n; 491 int len, n;
492 492
493 len = strlen(*s); 493 len = strlen(*s);
494 lw_check_heap(ctx, len + 1); 494 uw_check_heap(ctx, len + 1);
495 495
496 r = ctx->heap_front; 496 r = ctx->heap_front;
497 ctx->heap_front = lw_unurlifyString_to(ctx, ctx->heap_front, *s); 497 ctx->heap_front = uw_unurlifyString_to(ctx, ctx->heap_front, *s);
498 *s = new_s; 498 *s = new_s;
499 return r; 499 return r;
500 } 500 }
501 501
502 502
503 char *lw_Basis_htmlifyInt(lw_context ctx, lw_Basis_int n) { 503 char *uw_Basis_htmlifyInt(uw_context ctx, uw_Basis_int n) {
504 int len; 504 int len;
505 char *r; 505 char *r;
506 506
507 lw_check_heap(ctx, INTS_MAX); 507 uw_check_heap(ctx, INTS_MAX);
508 r = ctx->heap_front; 508 r = ctx->heap_front;
509 sprintf(r, "%lld%n", n, &len); 509 sprintf(r, "%lld%n", n, &len);
510 ctx->heap_front += len+1; 510 ctx->heap_front += len+1;
511 return r; 511 return r;
512 } 512 }
513 513
514 void lw_Basis_htmlifyInt_w(lw_context ctx, lw_Basis_int n) { 514 void uw_Basis_htmlifyInt_w(uw_context ctx, uw_Basis_int n) {
515 int len; 515 int len;
516 516
517 lw_check(ctx, INTS_MAX); 517 uw_check(ctx, INTS_MAX);
518 sprintf(ctx->page_front, "%lld%n", n, &len); 518 sprintf(ctx->page_front, "%lld%n", n, &len);
519 ctx->page_front += len; 519 ctx->page_front += len;
520 } 520 }
521 521
522 char *lw_Basis_htmlifyFloat(lw_context ctx, lw_Basis_float n) { 522 char *uw_Basis_htmlifyFloat(uw_context ctx, uw_Basis_float n) {
523 int len; 523 int len;
524 char *r; 524 char *r;
525 525
526 lw_check_heap(ctx, FLOATS_MAX); 526 uw_check_heap(ctx, FLOATS_MAX);
527 r = ctx->heap_front; 527 r = ctx->heap_front;
528 sprintf(r, "%g%n", n, &len); 528 sprintf(r, "%g%n", n, &len);
529 ctx->heap_front += len+1; 529 ctx->heap_front += len+1;
530 return r; 530 return r;
531 } 531 }
532 532
533 void lw_Basis_htmlifyFloat_w(lw_context ctx, lw_Basis_float n) { 533 void uw_Basis_htmlifyFloat_w(uw_context ctx, uw_Basis_float n) {
534 int len; 534 int len;
535 535
536 lw_check(ctx, FLOATS_MAX); 536 uw_check(ctx, FLOATS_MAX);
537 sprintf(ctx->page_front, "%g%n", n, &len); 537 sprintf(ctx->page_front, "%g%n", n, &len);
538 ctx->page_front += len; 538 ctx->page_front += len;
539 } 539 }
540 540
541 char *lw_Basis_htmlifyString(lw_context ctx, lw_Basis_string s) { 541 char *uw_Basis_htmlifyString(uw_context ctx, uw_Basis_string s) {
542 char *r, *s2; 542 char *r, *s2;
543 543
544 lw_check_heap(ctx, strlen(s) * 5 + 1); 544 uw_check_heap(ctx, strlen(s) * 5 + 1);
545 545
546 for (r = s2 = ctx->heap_front; *s; s++) { 546 for (r = s2 = ctx->heap_front; *s; s++) {
547 char c = *s; 547 char c = *s;
548 548
549 switch (c) { 549 switch (c) {
569 *s2++ = 0; 569 *s2++ = 0;
570 ctx->heap_front = s2; 570 ctx->heap_front = s2;
571 return r; 571 return r;
572 } 572 }
573 573
574 void lw_Basis_htmlifyString_w(lw_context ctx, lw_Basis_string s) { 574 void uw_Basis_htmlifyString_w(uw_context ctx, uw_Basis_string s) {
575 lw_check(ctx, strlen(s) * 5); 575 uw_check(ctx, strlen(s) * 5);
576 576
577 for (; *s; s++) { 577 for (; *s; s++) {
578 char c = *s; 578 char c = *s;
579 579
580 switch (c) { 580 switch (c) {
581 case '<': 581 case '<':
582 lw_write_unsafe(ctx, "&lt;"); 582 uw_write_unsafe(ctx, "&lt;");
583 break; 583 break;
584 case '&': 584 case '&':
585 lw_write_unsafe(ctx, "&amp;"); 585 uw_write_unsafe(ctx, "&amp;");
586 break; 586 break;
587 default: 587 default:
588 if (isprint(c)) 588 if (isprint(c))
589 lw_writec_unsafe(ctx, c); 589 uw_writec_unsafe(ctx, c);
590 else { 590 else {
591 lw_write_unsafe(ctx, "&#"); 591 uw_write_unsafe(ctx, "&#");
592 lw_Basis_attrifyInt_w_unsafe(ctx, c); 592 uw_Basis_attrifyInt_w_unsafe(ctx, c);
593 lw_writec_unsafe(ctx, ';'); 593 uw_writec_unsafe(ctx, ';');
594 } 594 }
595 } 595 }
596 } 596 }
597 } 597 }
598 598
599 lw_Basis_string lw_Basis_htmlifyBool(lw_context ctx, lw_Basis_bool b) { 599 uw_Basis_string uw_Basis_htmlifyBool(uw_context ctx, uw_Basis_bool b) {
600 if (b == lw_Basis_False) 600 if (b == uw_Basis_False)
601 return "False"; 601 return "False";
602 else 602 else
603 return "True"; 603 return "True";
604 } 604 }
605 605
606 void lw_Basis_htmlifyBool_w(lw_context ctx, lw_Basis_bool b) { 606 void uw_Basis_htmlifyBool_w(uw_context ctx, uw_Basis_bool b) {
607 if (b == lw_Basis_False) { 607 if (b == uw_Basis_False) {
608 lw_check(ctx, 6); 608 uw_check(ctx, 6);
609 strcpy(ctx->page_front, "False"); 609 strcpy(ctx->page_front, "False");
610 ctx->page_front += 5; 610 ctx->page_front += 5;
611 } else { 611 } else {
612 lw_check(ctx, 5); 612 uw_check(ctx, 5);
613 strcpy(ctx->page_front, "True"); 613 strcpy(ctx->page_front, "True");
614 ctx->page_front += 4; 614 ctx->page_front += 4;
615 } 615 }
616 } 616 }
617 617
618 lw_Basis_string lw_Basis_strcat(lw_context ctx, lw_Basis_string s1, lw_Basis_string s2) { 618 uw_Basis_string uw_Basis_strcat(uw_context ctx, uw_Basis_string s1, uw_Basis_string s2) {
619 int len = strlen(s1) + strlen(s2) + 1; 619 int len = strlen(s1) + strlen(s2) + 1;
620 char *s; 620 char *s;
621 621
622 lw_check_heap(ctx, len); 622 uw_check_heap(ctx, len);
623 623
624 s = ctx->heap_front; 624 s = ctx->heap_front;
625 625
626 strcpy(s, s1); 626 strcpy(s, s1);
627 strcat(s, s2); 627 strcat(s, s2);
628 ctx->heap_front += len; 628 ctx->heap_front += len;
629 629
630 return s; 630 return s;
631 } 631 }
632 632
633 lw_Basis_string lw_Basis_strdup(lw_context ctx, lw_Basis_string s1) { 633 uw_Basis_string uw_Basis_strdup(uw_context ctx, uw_Basis_string s1) {
634 int len = strlen(s1) + 1; 634 int len = strlen(s1) + 1;
635 char *s; 635 char *s;
636 636
637 lw_check_heap(ctx, len); 637 uw_check_heap(ctx, len);
638 638
639 s = ctx->heap_front; 639 s = ctx->heap_front;
640 640
641 strcpy(s, s1); 641 strcpy(s, s1);
642 ctx->heap_front += len; 642 ctx->heap_front += len;
643 643
644 return s; 644 return s;
645 } 645 }
646 646
647 647
648 char *lw_Basis_sqlifyInt(lw_context ctx, lw_Basis_int n) { 648 char *uw_Basis_sqlifyInt(uw_context ctx, uw_Basis_int n) {
649 int len; 649 int len;
650 char *r; 650 char *r;
651 651
652 lw_check_heap(ctx, INTS_MAX + 6); 652 uw_check_heap(ctx, INTS_MAX + 6);
653 r = ctx->heap_front; 653 r = ctx->heap_front;
654 sprintf(r, "%lld::int8%n", n, &len); 654 sprintf(r, "%lld::int8%n", n, &len);
655 ctx->heap_front += len+1; 655 ctx->heap_front += len+1;
656 return r; 656 return r;
657 } 657 }
658 658
659 char *lw_Basis_sqlifyFloat(lw_context ctx, lw_Basis_float n) { 659 char *uw_Basis_sqlifyFloat(uw_context ctx, uw_Basis_float n) {
660 int len; 660 int len;
661 char *r; 661 char *r;
662 662
663 lw_check_heap(ctx, FLOATS_MAX + 8); 663 uw_check_heap(ctx, FLOATS_MAX + 8);
664 r = ctx->heap_front; 664 r = ctx->heap_front;
665 sprintf(r, "%g::float8%n", n, &len); 665 sprintf(r, "%g::float8%n", n, &len);
666 ctx->heap_front += len+1; 666 ctx->heap_front += len+1;
667 return r; 667 return r;
668 } 668 }
669 669
670 670
671 lw_Basis_string lw_Basis_sqlifyString(lw_context ctx, lw_Basis_string s) { 671 uw_Basis_string uw_Basis_sqlifyString(uw_context ctx, uw_Basis_string s) {
672 char *r, *s2; 672 char *r, *s2;
673 673
674 lw_check_heap(ctx, strlen(s) * 2 + 10); 674 uw_check_heap(ctx, strlen(s) * 2 + 10);
675 675
676 r = s2 = ctx->heap_front; 676 r = s2 = ctx->heap_front;
677 *s2++ = 'E'; 677 *s2++ = 'E';
678 *s2++ = '\''; 678 *s2++ = '\'';
679 679
702 strcpy(s2, "'::text"); 702 strcpy(s2, "'::text");
703 ctx->heap_front = s2 + 8; 703 ctx->heap_front = s2 + 8;
704 return r; 704 return r;
705 } 705 }
706 706
707 char *lw_Basis_sqlifyBool(lw_context ctx, lw_Basis_bool b) { 707 char *uw_Basis_sqlifyBool(uw_context ctx, uw_Basis_bool b) {
708 if (b == lw_Basis_False) 708 if (b == uw_Basis_False)
709 return "FALSE"; 709 return "FALSE";
710 else 710 else
711 return "TRUE"; 711 return "TRUE";
712 } 712 }
713 713
714 char *lw_Basis_ensqlBool(lw_Basis_bool b) { 714 char *uw_Basis_ensqlBool(uw_Basis_bool b) {
715 static lw_Basis_int true = 1; 715 static uw_Basis_int true = 1;
716 static lw_Basis_int false = 0; 716 static uw_Basis_int false = 0;
717 717
718 if (b == lw_Basis_False) 718 if (b == uw_Basis_False)
719 return (char *)&false; 719 return (char *)&false;
720 else 720 else
721 return (char *)&true; 721 return (char *)&true;
722 } 722 }
723 723
724 lw_Basis_string lw_Basis_intToString(lw_context ctx, lw_Basis_int n) { 724 uw_Basis_string uw_Basis_intToString(uw_context ctx, uw_Basis_int n) {
725 int len; 725 int len;
726 char *r; 726 char *r;
727 727
728 lw_check_heap(ctx, INTS_MAX); 728 uw_check_heap(ctx, INTS_MAX);
729 r = ctx->heap_front; 729 r = ctx->heap_front;
730 sprintf(r, "%lld%n", n, &len); 730 sprintf(r, "%lld%n", n, &len);
731 ctx->heap_front += len+1; 731 ctx->heap_front += len+1;
732 return r; 732 return r;
733 } 733 }
734 734
735 lw_Basis_string lw_Basis_floatToString(lw_context ctx, lw_Basis_float n) { 735 uw_Basis_string uw_Basis_floatToString(uw_context ctx, uw_Basis_float n) {
736 int len; 736 int len;
737 char *r; 737 char *r;
738 738
739 lw_check_heap(ctx, FLOATS_MAX); 739 uw_check_heap(ctx, FLOATS_MAX);
740 r = ctx->heap_front; 740 r = ctx->heap_front;
741 sprintf(r, "%g%n", n, &len); 741 sprintf(r, "%g%n", n, &len);
742 ctx->heap_front += len+1; 742 ctx->heap_front += len+1;
743 return r; 743 return r;
744 } 744 }
745 745
746 lw_Basis_string lw_Basis_boolToString(lw_context ctx, lw_Basis_bool b) { 746 uw_Basis_string uw_Basis_boolToString(uw_context ctx, uw_Basis_bool b) {
747 if (b == lw_Basis_False) 747 if (b == uw_Basis_False)
748 return "False"; 748 return "False";
749 else 749 else
750 return "True"; 750 return "True";
751 } 751 }
752 752
753 753
754 lw_Basis_int *lw_Basis_stringToInt(lw_context ctx, lw_Basis_string s) { 754 uw_Basis_int *uw_Basis_stringToInt(uw_context ctx, uw_Basis_string s) {
755 char *endptr; 755 char *endptr;
756 lw_Basis_int n = strtoll(s, &endptr, 10); 756 uw_Basis_int n = strtoll(s, &endptr, 10);
757 757
758 if (*s != '\0' && *endptr == '\0') { 758 if (*s != '\0' && *endptr == '\0') {
759 lw_Basis_int *r = lw_malloc(ctx, sizeof(lw_Basis_int)); 759 uw_Basis_int *r = uw_malloc(ctx, sizeof(uw_Basis_int));
760 *r = n; 760 *r = n;
761 return r; 761 return r;
762 } else 762 } else
763 return NULL; 763 return NULL;
764 } 764 }
765 765
766 lw_Basis_float *lw_Basis_stringToFloat(lw_context ctx, lw_Basis_string s) { 766 uw_Basis_float *uw_Basis_stringToFloat(uw_context ctx, uw_Basis_string s) {
767 char *endptr; 767 char *endptr;
768 lw_Basis_float n = strtod(s, &endptr); 768 uw_Basis_float n = strtod(s, &endptr);
769 769
770 if (*s != '\0' && *endptr == '\0') { 770 if (*s != '\0' && *endptr == '\0') {
771 lw_Basis_float *r = lw_malloc(ctx, sizeof(lw_Basis_float)); 771 uw_Basis_float *r = uw_malloc(ctx, sizeof(uw_Basis_float));
772 *r = n; 772 *r = n;
773 return r; 773 return r;
774 } else 774 } else
775 return NULL; 775 return NULL;
776 } 776 }
777 777
778 lw_Basis_bool *lw_Basis_stringToBool(lw_context ctx, lw_Basis_string s) { 778 uw_Basis_bool *uw_Basis_stringToBool(uw_context ctx, uw_Basis_string s) {
779 static lw_Basis_bool true = lw_Basis_True; 779 static uw_Basis_bool true = uw_Basis_True;
780 static lw_Basis_bool false = lw_Basis_False; 780 static uw_Basis_bool false = uw_Basis_False;
781 781
782 if (!strcasecmp (s, "True")) 782 if (!strcasecmp (s, "True"))
783 return &true; 783 return &true;
784 else if (!strcasecmp (s, "False")) 784 else if (!strcasecmp (s, "False"))
785 return &false; 785 return &false;
786 else 786 else
787 return NULL; 787 return NULL;
788 } 788 }
789 789
790 lw_Basis_int lw_Basis_stringToInt_error(lw_context ctx, lw_Basis_string s) { 790 uw_Basis_int uw_Basis_stringToInt_error(uw_context ctx, uw_Basis_string s) {
791 char *endptr; 791 char *endptr;
792 lw_Basis_int n = strtoll(s, &endptr, 10); 792 uw_Basis_int n = strtoll(s, &endptr, 10);
793 793
794 if (*s != '\0' && *endptr == '\0') 794 if (*s != '\0' && *endptr == '\0')
795 return n; 795 return n;
796 else 796 else
797 lw_error(ctx, FATAL, "Can't parse int: %s", s); 797 uw_error(ctx, FATAL, "Can't parse int: %s", s);
798 } 798 }
799 799
800 lw_Basis_float lw_Basis_stringToFloat_error(lw_context ctx, lw_Basis_string s) { 800 uw_Basis_float uw_Basis_stringToFloat_error(uw_context ctx, uw_Basis_string s) {
801 char *endptr; 801 char *endptr;
802 lw_Basis_float n = strtod(s, &endptr); 802 uw_Basis_float n = strtod(s, &endptr);
803 803
804 if (*s != '\0' && *endptr == '\0') 804 if (*s != '\0' && *endptr == '\0')
805 return n; 805 return n;
806 else 806 else
807 lw_error(ctx, FATAL, "Can't parse float: %s", s); 807 uw_error(ctx, FATAL, "Can't parse float: %s", s);
808 } 808 }
809 809
810 lw_Basis_bool lw_Basis_stringToBool_error(lw_context ctx, lw_Basis_string s) { 810 uw_Basis_bool uw_Basis_stringToBool_error(uw_context ctx, uw_Basis_string s) {
811 if (!strcasecmp(s, "T") || !strcasecmp (s, "True")) 811 if (!strcasecmp(s, "T") || !strcasecmp (s, "True"))
812 return lw_Basis_True; 812 return uw_Basis_True;
813 else if (!strcasecmp(s, "F") || !strcasecmp (s, "False")) 813 else if (!strcasecmp(s, "F") || !strcasecmp (s, "False"))
814 return lw_Basis_False; 814 return uw_Basis_False;
815 else 815 else
816 lw_error(ctx, FATAL, "Can't parse bool: %s", s); 816 uw_error(ctx, FATAL, "Can't parse bool: %s", s);
817 } 817 }