comparison src/c/urweb.c @ 666:5130228d2b29

Factor out some common buffer functionality
author Adam Chlipala <adamc@hcoop.net>
date Thu, 19 Mar 2009 13:47:02 -0400
parents 1b571a05874c
children a93d5324f400
comparison
equal deleted inserted replaced
665:910bf013da4a 666:5130228d2b29
21 typedef struct { 21 typedef struct {
22 void (*func)(void*); 22 void (*func)(void*);
23 void *arg; 23 void *arg;
24 } cleanup; 24 } cleanup;
25 25
26 typedef struct {
27 char *start, *front, *back;
28 } buf;
29
26 struct uw_context { 30 struct uw_context {
27 char *headers, *headers_end; 31 char *headers, *headers_end;
28 32
29 char *outHeaders, *outHeaders_front, *outHeaders_back; 33 buf outHeaders, page, heap, script;
30 char *page, *page_front, *page_back;
31 char *heap, *heap_front, *heap_back;
32 char **inputs; 34 char **inputs;
33 35
34 char *script, *script_front, *script_back;
35 int source_count; 36 int source_count;
36 37
37 void *db; 38 void *db;
38 39
39 jmp_buf jmp_buf; 40 jmp_buf jmp_buf;
47 char error_message[ERROR_BUF_LEN]; 48 char error_message[ERROR_BUF_LEN];
48 }; 49 };
49 50
50 extern int uw_inputs_len; 51 extern int uw_inputs_len;
51 52
53 static void buf_init(buf *b, size_t s) {
54 b->front = b->start = malloc(s);
55 b->back = b->front + s;
56 }
57
52 uw_context uw_init(size_t outHeaders_len, size_t script_len, size_t page_len, size_t heap_len) { 58 uw_context uw_init(size_t outHeaders_len, size_t script_len, size_t page_len, size_t heap_len) {
53 uw_context ctx = malloc(sizeof(struct uw_context)); 59 uw_context ctx = malloc(sizeof(struct uw_context));
54 60
55 ctx->headers = ctx->headers_end = NULL; 61 ctx->headers = ctx->headers_end = NULL;
56 62
57 ctx->outHeaders_front = ctx->outHeaders = malloc(outHeaders_len); 63 buf_init(&ctx->outHeaders, outHeaders_len);
58 ctx->outHeaders_back = ctx->outHeaders_front + outHeaders_len; 64 buf_init(&ctx->page, page_len);
59 65 buf_init(&ctx->heap, heap_len);
60 ctx->heap_front = ctx->heap = malloc(heap_len); 66 buf_init(&ctx->script, script_len);
61
62 ctx->page_front = ctx->page = malloc(page_len);
63 ctx->page_back = ctx->page_front + page_len;
64
65 ctx->heap_front = ctx->heap = malloc(heap_len);
66 ctx->heap_back = ctx->heap_front + heap_len;
67 67
68 ctx->inputs = calloc(uw_inputs_len, sizeof(char *)); 68 ctx->inputs = calloc(uw_inputs_len, sizeof(char *));
69 69
70 ctx->db = NULL; 70 ctx->db = NULL;
71 71
75 75
76 ctx->script_header = ""; 76 ctx->script_header = "";
77 77
78 ctx->error_message[0] = 0; 78 ctx->error_message[0] = 0;
79 79
80 ctx->script_front = ctx->script = malloc(script_len);
81 ctx->script_back = ctx->script_front + script_len;
82 ctx->source_count = 0; 80 ctx->source_count = 0;
83 81
84 return ctx; 82 return ctx;
85 } 83 }
86 84
90 88
91 void *uw_get_db(uw_context ctx) { 89 void *uw_get_db(uw_context ctx) {
92 return ctx->db; 90 return ctx->db;
93 } 91 }
94 92
93 static void buf_free(buf *b) {
94 free(b->front);
95 }
96
95 void uw_free(uw_context ctx) { 97 void uw_free(uw_context ctx) {
96 free(ctx->outHeaders); 98 buf_free(&ctx->outHeaders);
97 free(ctx->script); 99 buf_free(&ctx->script);
98 free(ctx->page); 100 buf_free(&ctx->page);
99 free(ctx->heap); 101 buf_free(&ctx->heap);
100 free(ctx->inputs); 102 free(ctx->inputs);
101 free(ctx->cleanup); 103 free(ctx->cleanup);
102 free(ctx); 104 free(ctx);
103 } 105 }
104 106
107 static void buf_reset(buf *b) {
108 b->front = b->start;
109 }
110
105 void uw_reset_keep_error_message(uw_context ctx) { 111 void uw_reset_keep_error_message(uw_context ctx) {
106 ctx->outHeaders_front = ctx->outHeaders; 112 buf_reset(&ctx->outHeaders);
107 ctx->script_front = ctx->script; 113 buf_reset(&ctx->script);
108 ctx->page_front = ctx->page; 114 buf_reset(&ctx->page);
109 ctx->heap_front = ctx->heap; 115 buf_reset(&ctx->heap);
110 ctx->regions = NULL; 116 ctx->regions = NULL;
111 ctx->cleanup_front = ctx->cleanup; 117 ctx->cleanup_front = ctx->cleanup;
112 ctx->source_count = 0; 118 ctx->source_count = 0;
113 } 119 }
114 120
241 247
242 void uw_set_script_header(uw_context ctx, const char *s) { 248 void uw_set_script_header(uw_context ctx, const char *s) {
243 ctx->script_header = s; 249 ctx->script_header = s;
244 } 250 }
245 251
246 static void uw_check_heap(uw_context ctx, size_t extra) { 252 static void buf_check(uw_context ctx, buf *b, size_t extra, const char *desc) {
247 if (ctx->heap_back - ctx->heap_front < extra) { 253 if (b->back - b->front < extra) {
248 size_t desired = ctx->heap_front - ctx->heap + extra, next; 254 size_t desired = b->front - b->start + extra, next;
249 char *new_heap; 255 char *new_heap;
250 256
251 next = ctx->heap_back - ctx->heap; 257 next = b->back - b->start;
252 if (next == 0) 258 if (next == 0)
253 next = 1; 259 next = 1;
254 for (; next < desired; next *= 2); 260 for (; next < desired; next *= 2);
255 261
256 new_heap = realloc(ctx->heap, next); 262 new_heap = realloc(b->start, next);
257 ctx->heap_front = new_heap + (ctx->heap_front - ctx->heap); 263 b->front = new_heap + (b->front - b->start);
258 ctx->heap_back = new_heap + next; 264 b->back = new_heap + next;
259 265
260 if (new_heap != ctx->heap) { 266 if (desc && new_heap != b->start) {
261 ctx->heap = new_heap; 267 b->start = new_heap;
262 uw_error(ctx, UNLIMITED_RETRY, "Couldn't allocate new heap chunk contiguously"); 268 uw_error(ctx, UNLIMITED_RETRY, "Couldn't allocate new %s contiguously", desc);
263 } 269 }
264 270
265 ctx->heap = new_heap; 271 b->start = new_heap;
266 } 272 }
273 }
274
275 static void uw_check_heap(uw_context ctx, size_t extra) {
276 buf_check(ctx, &ctx->heap, extra, "heap chunk");
267 } 277 }
268 278
269 void *uw_malloc(uw_context ctx, size_t len) { 279 void *uw_malloc(uw_context ctx, size_t len) {
270 void *result; 280 void *result;
271 281
272 uw_check_heap(ctx, len); 282 uw_check_heap(ctx, len);
273 283
274 result = ctx->heap_front; 284 result = ctx->heap.front;
275 ctx->heap_front += len; 285 ctx->heap.front += len;
276 return result; 286 return result;
277 } 287 }
278 288
279 void uw_begin_region(uw_context ctx) { 289 void uw_begin_region(uw_context ctx) {
280 regions *r = (regions *) ctx->heap_front; 290 regions *r = (regions *) ctx->heap.front;
281 291
282 uw_check_heap(ctx, sizeof(regions)); 292 uw_check_heap(ctx, sizeof(regions));
283 293
284 ctx->heap_front += sizeof(regions); 294 ctx->heap.front += sizeof(regions);
285 295
286 r->next = ctx->regions; 296 r->next = ctx->regions;
287 ctx->regions = r; 297 ctx->regions = r;
288 } 298 }
289 299
291 regions *r = ctx->regions; 301 regions *r = ctx->regions;
292 302
293 if (r == NULL) 303 if (r == NULL)
294 uw_error(ctx, FATAL, "Region stack underflow"); 304 uw_error(ctx, FATAL, "Region stack underflow");
295 305
296 ctx->heap_front = (char *) r; 306 ctx->heap.front = (char *) r;
297 ctx->regions = r->next; 307 ctx->regions = r->next;
298 } 308 }
299 309
310 static size_t buf_used(buf *b) {
311 return b->front - b->start;
312 }
313
314 static size_t buf_avail(buf *b) {
315 return b->back - b->start;
316 }
317
300 void uw_memstats(uw_context ctx) { 318 void uw_memstats(uw_context ctx) {
301 printf("Headers: %d/%d\n", ctx->outHeaders_front - ctx->outHeaders, ctx->outHeaders_back - ctx->outHeaders); 319 printf("Headers: %d/%d\n", buf_used(&ctx->outHeaders), buf_avail(&ctx->outHeaders));
302 printf("Script: %d/%d\n", ctx->script_front - ctx->script, ctx->script_back - ctx->script); 320 printf("Script: %d/%d\n", buf_used(&ctx->script), buf_avail(&ctx->script));
303 printf("Page: %d/%d\n", ctx->page_front - ctx->page, ctx->page_back - ctx->page); 321 printf("Page: %d/%d\n", buf_used(&ctx->page), buf_avail(&ctx->page));
304 printf("Heap: %d/%d\n", ctx->heap_front - ctx->heap, ctx->heap_back - ctx->heap); 322 printf("Heap: %d/%d\n", buf_used(&ctx->heap), buf_avail(&ctx->heap));
305 } 323 }
306 324
307 int uw_really_send(int sock, const void *buf, size_t len) { 325 int uw_really_send(int sock, const void *buf, size_t len) {
308 while (len > 0) { 326 while (len > 0) {
309 size_t n = send(sock, buf, len, 0); 327 size_t n = send(sock, buf, len, 0);
317 335
318 return 0; 336 return 0;
319 } 337 }
320 338
321 int uw_send(uw_context ctx, int sock) { 339 int uw_send(uw_context ctx, int sock) {
322 int n = uw_really_send(sock, ctx->outHeaders, ctx->outHeaders_front - ctx->outHeaders); 340 int n = uw_really_send(sock, ctx->outHeaders.start, ctx->outHeaders.front - ctx->outHeaders.start);
323 341
324 if (n < 0) 342 if (n < 0)
325 return n; 343 return n;
326 344
327 n = uw_really_send(sock, "\r\n", 2); 345 n = uw_really_send(sock, "\r\n", 2);
328 346
329 if (n < 0) 347 if (n < 0)
330 return n; 348 return n;
331 349
332 return uw_really_send(sock, ctx->page, ctx->page_front - ctx->page); 350 return uw_really_send(sock, ctx->page.start, ctx->page.front - ctx->page.start);
333 } 351 }
334 352
335 static void uw_check_headers(uw_context ctx, size_t extra) { 353 static void uw_check_headers(uw_context ctx, size_t extra) {
336 size_t desired = ctx->outHeaders_front - ctx->outHeaders + extra, next; 354 buf_check(ctx, &ctx->outHeaders, extra, NULL);
337 char *new_outHeaders;
338
339 next = ctx->outHeaders_back - ctx->outHeaders;
340 if (next < desired) {
341 if (next == 0)
342 next = 1;
343 for (; next < desired; next *= 2);
344
345 new_outHeaders = realloc(ctx->outHeaders, next);
346 ctx->outHeaders_front = new_outHeaders + (ctx->outHeaders_front - ctx->outHeaders);
347 ctx->outHeaders_back = new_outHeaders + next;
348 ctx->outHeaders = new_outHeaders;
349 }
350 } 355 }
351 356
352 void uw_write_header(uw_context ctx, uw_Basis_string s) { 357 void uw_write_header(uw_context ctx, uw_Basis_string s) {
353 int len = strlen(s); 358 int len = strlen(s);
354 359
355 uw_check_headers(ctx, len + 1); 360 uw_check_headers(ctx, len + 1);
356 strcpy(ctx->outHeaders_front, s); 361 strcpy(ctx->outHeaders.front, s);
357 ctx->outHeaders_front += len; 362 ctx->outHeaders.front += len;
358 } 363 }
359 364
360 static void uw_check_script(uw_context ctx, size_t extra) { 365 static void uw_check_script(uw_context ctx, size_t extra) {
361 size_t desired = ctx->script_front - ctx->script + extra, next; 366 buf_check(ctx, &ctx->script, extra, NULL);
362 char *new_script;
363
364 next = ctx->script_back - ctx->script;
365 if (next < desired) {
366 if (next == 0)
367 next = 1;
368 for (; next < desired; next *= 2);
369
370 new_script = realloc(ctx->script, next);
371 ctx->script_front = new_script + (ctx->script_front - ctx->script);
372 ctx->script_back = new_script + next;
373 ctx->script = new_script;
374 }
375 } 367 }
376 368
377 void uw_write_script(uw_context ctx, uw_Basis_string s) { 369 void uw_write_script(uw_context ctx, uw_Basis_string s) {
378 int len = strlen(s); 370 int len = strlen(s);
379 371
380 uw_check_script(ctx, len + 1); 372 uw_check_script(ctx, len + 1);
381 strcpy(ctx->script_front, s); 373 strcpy(ctx->script.front, s);
382 ctx->script_front += len; 374 ctx->script.front += len;
383 } 375 }
384 376
385 const char *uw_Basis_get_script(uw_context ctx, uw_unit u) { 377 const char *uw_Basis_get_script(uw_context ctx, uw_unit u) {
386 if (ctx->script_front == ctx->script) { 378 if (ctx->script.front == ctx->script.start) {
387 return ctx->script_header; 379 return ctx->script_header;
388 } else { 380 } else {
389 char *r = uw_malloc(ctx, 41 + (ctx->script_front - ctx->script) + strlen(ctx->script_header)); 381 char *r = uw_malloc(ctx, 41 + (ctx->script.front - ctx->script.start) + strlen(ctx->script_header));
390 382
391 sprintf(r, "%s<script>%s</script>", ctx->script_header, ctx->script); 383 sprintf(r, "%s<script>%s</script>", ctx->script_header, ctx->script.start);
392 return r; 384 return r;
393 } 385 }
394 } 386 }
395 387
396 uw_Basis_string uw_Basis_jsifyString(uw_context ctx, uw_Basis_string s) { 388 uw_Basis_string uw_Basis_jsifyString(uw_context ctx, uw_Basis_string s) {
397 char *r, *s2; 389 char *r, *s2;
398 390
399 uw_check_heap(ctx, strlen(s) * 4 + 2); 391 uw_check_heap(ctx, strlen(s) * 4 + 2);
400 392
401 r = s2 = ctx->heap_front; 393 r = s2 = ctx->heap.front;
402 *s2++ = '"'; 394 *s2++ = '"';
403 395
404 for (; *s; s++) { 396 for (; *s; s++) {
405 char c = *s; 397 char c = *s;
406 398
422 } 414 }
423 } 415 }
424 } 416 }
425 417
426 strcpy(s2, "\""); 418 strcpy(s2, "\"");
427 ctx->heap_front = s2 + 1; 419 ctx->heap.front = s2 + 1;
428 return r; 420 return r;
429 } 421 }
430 422
431 uw_Basis_string uw_Basis_jsifyString_ws(uw_context ctx, uw_Basis_string s) { 423 uw_Basis_string uw_Basis_jsifyString_ws(uw_context ctx, uw_Basis_string s) {
432 char *r, *s2; 424 char *r, *s2;
433 425
434 uw_check_script(ctx, strlen(s) * 4 + 2); 426 uw_check_script(ctx, strlen(s) * 4 + 2);
435 427
436 r = s2 = ctx->script_front; 428 r = s2 = ctx->script.front;
437 *s2++ = '"'; 429 *s2++ = '"';
438 430
439 for (; *s; s++) { 431 for (; *s; s++) {
440 char c = *s; 432 char c = *s;
441 433
457 } 449 }
458 } 450 }
459 } 451 }
460 452
461 strcpy(s2, "\""); 453 strcpy(s2, "\"");
462 ctx->script_front = s2 + 1; 454 ctx->script.front = s2 + 1;
463 return r; 455 return r;
464 } 456 }
465 457
466 uw_Basis_int uw_Basis_new_client_source(uw_context ctx, uw_Basis_string s) { 458 uw_Basis_int uw_Basis_new_client_source(uw_context ctx, uw_Basis_string s) {
467 int len; 459 int len;
468 size_t s_len = strlen(s); 460 size_t s_len = strlen(s);
469 461
470 uw_check_script(ctx, 12 + INTS_MAX + s_len); 462 uw_check_script(ctx, 12 + INTS_MAX + s_len);
471 sprintf(ctx->script_front, "var s%d=sc(%n", ctx->source_count, &len); 463 sprintf(ctx->script.front, "var s%d=sc(%n", ctx->source_count, &len);
472 ctx->script_front += len; 464 ctx->script.front += len;
473 strcpy(ctx->script_front, s); 465 strcpy(ctx->script.front, s);
474 ctx->script_front += s_len; 466 ctx->script.front += s_len;
475 strcpy(ctx->script_front, ");"); 467 strcpy(ctx->script.front, ");");
476 ctx->script_front += 2; 468 ctx->script.front += 2;
477 469
478 return ctx->source_count++; 470 return ctx->source_count++;
479 } 471 }
480 472
481 uw_unit uw_Basis_set_client_source(uw_context ctx, uw_Basis_int n, uw_Basis_string s) { 473 uw_unit uw_Basis_set_client_source(uw_context ctx, uw_Basis_int n, uw_Basis_string s) {
482 int len; 474 int len;
483 size_t s_len = strlen(s); 475 size_t s_len = strlen(s);
484 476
485 uw_check_script(ctx, 6 + INTS_MAX + s_len); 477 uw_check_script(ctx, 6 + INTS_MAX + s_len);
486 sprintf(ctx->script_front, "s%d.v=%n", (int)n, &len); 478 sprintf(ctx->script.front, "s%d.v=%n", (int)n, &len);
487 ctx->script_front += len; 479 ctx->script.front += len;
488 strcpy(ctx->script_front, s); 480 strcpy(ctx->script.front, s);
489 ctx->script_front += s_len; 481 ctx->script.front += s_len;
490 strcpy(ctx->script_front, ";"); 482 strcpy(ctx->script.front, ";");
491 ctx->script_front++; 483 ctx->script.front++;
492 484
493 return uw_unit_v; 485 return uw_unit_v;
494 } 486 }
495 487
496 static void uw_check(uw_context ctx, size_t extra) { 488 static void uw_check(uw_context ctx, size_t extra) {
497 size_t desired = ctx->page_front - ctx->page + extra, next; 489 buf_check(ctx, &ctx->page, extra, NULL);
498 char *new_page;
499
500 next = ctx->page_back - ctx->page;
501 if (next < desired) {
502 if (next == 0)
503 next = 1;
504 for (; next < desired; next *= 2);
505
506 new_page = realloc(ctx->page, next);
507 ctx->page_front = new_page + (ctx->page_front - ctx->page);
508 ctx->page_back = new_page + next;
509 ctx->page = new_page;
510 }
511 } 490 }
512 491
513 static void uw_writec_unsafe(uw_context ctx, char c) { 492 static void uw_writec_unsafe(uw_context ctx, char c) {
514 *(ctx->page_front)++ = c; 493 *(ctx->page.front)++ = c;
515 } 494 }
516 495
517 void uw_writec(uw_context ctx, char c) { 496 void uw_writec(uw_context ctx, char c) {
518 uw_check(ctx, 1); 497 uw_check(ctx, 1);
519 uw_writec_unsafe(ctx, c); 498 uw_writec_unsafe(ctx, c);
520 } 499 }
521 500
522 static void uw_write_unsafe(uw_context ctx, const char* s) { 501 static void uw_write_unsafe(uw_context ctx, const char* s) {
523 int len = strlen(s); 502 int len = strlen(s);
524 memcpy(ctx->page_front, s, len); 503 memcpy(ctx->page.front, s, len);
525 ctx->page_front += len; 504 ctx->page.front += len;
526 } 505 }
527 506
528 void uw_write(uw_context ctx, const char* s) { 507 void uw_write(uw_context ctx, const char* s) {
529 uw_check(ctx, strlen(s) + 1); 508 uw_check(ctx, strlen(s) + 1);
530 uw_write_unsafe(ctx, s); 509 uw_write_unsafe(ctx, s);
531 *ctx->page_front = 0; 510 *ctx->page.front = 0;
532 } 511 }
533
534 512
535 char *uw_Basis_attrifyInt(uw_context ctx, uw_Basis_int n) { 513 char *uw_Basis_attrifyInt(uw_context ctx, uw_Basis_int n) {
536 char *result; 514 char *result;
537 int len; 515 int len;
538 uw_check_heap(ctx, INTS_MAX); 516 uw_check_heap(ctx, INTS_MAX);
539 result = ctx->heap_front; 517 result = ctx->heap.front;
540 sprintf(result, "%lld%n", n, &len); 518 sprintf(result, "%lld%n", n, &len);
541 ctx->heap_front += len+1; 519 ctx->heap.front += len+1;
542 return result; 520 return result;
543 } 521 }
544 522
545 char *uw_Basis_attrifyFloat(uw_context ctx, uw_Basis_float n) { 523 char *uw_Basis_attrifyFloat(uw_context ctx, uw_Basis_float n) {
546 char *result; 524 char *result;
547 int len; 525 int len;
548 uw_check_heap(ctx, FLOATS_MAX); 526 uw_check_heap(ctx, FLOATS_MAX);
549 result = ctx->heap_front; 527 result = ctx->heap.front;
550 sprintf(result, "%g%n", n, &len); 528 sprintf(result, "%g%n", n, &len);
551 ctx->heap_front += len+1; 529 ctx->heap.front += len+1;
552 return result; 530 return result;
553 } 531 }
554 532
555 char *uw_Basis_attrifyString(uw_context ctx, uw_Basis_string s) { 533 char *uw_Basis_attrifyString(uw_context ctx, uw_Basis_string s) {
556 int len = strlen(s); 534 int len = strlen(s);
557 char *result, *p; 535 char *result, *p;
558 uw_check_heap(ctx, len * 6 + 1); 536 uw_check_heap(ctx, len * 6 + 1);
559 537
560 result = p = ctx->heap_front; 538 result = p = ctx->heap.front;
561 539
562 for (; *s; s++) { 540 for (; *s; s++) {
563 char c = *s; 541 char c = *s;
564 542
565 if (c == '"') { 543 if (c == '"') {
577 p += len2; 555 p += len2;
578 } 556 }
579 } 557 }
580 558
581 *p++ = 0; 559 *p++ = 0;
582 ctx->heap_front = p; 560 ctx->heap.front = p;
583 return result; 561 return result;
584 } 562 }
585 563
586 static void uw_Basis_attrifyInt_w_unsafe(uw_context ctx, uw_Basis_int n) { 564 static void uw_Basis_attrifyInt_w_unsafe(uw_context ctx, uw_Basis_int n) {
587 int len; 565 int len;
588 566
589 sprintf(ctx->page_front, "%lld%n", n, &len); 567 sprintf(ctx->page.front, "%lld%n", n, &len);
590 ctx->page_front += len; 568 ctx->page.front += len;
591 } 569 }
592 570
593 uw_unit uw_Basis_attrifyInt_w(uw_context ctx, uw_Basis_int n) { 571 uw_unit uw_Basis_attrifyInt_w(uw_context ctx, uw_Basis_int n) {
594 uw_check(ctx, INTS_MAX); 572 uw_check(ctx, INTS_MAX);
595 uw_Basis_attrifyInt_w_unsafe(ctx, n); 573 uw_Basis_attrifyInt_w_unsafe(ctx, n);
599 577
600 uw_unit uw_Basis_attrifyFloat_w(uw_context ctx, uw_Basis_float n) { 578 uw_unit uw_Basis_attrifyFloat_w(uw_context ctx, uw_Basis_float n) {
601 int len; 579 int len;
602 580
603 uw_check(ctx, FLOATS_MAX); 581 uw_check(ctx, FLOATS_MAX);
604 sprintf(ctx->page_front, "%g%n", n, &len); 582 sprintf(ctx->page.front, "%g%n", n, &len);
605 ctx->page_front += len; 583 ctx->page.front += len;
606 584
607 return uw_unit_v; 585 return uw_unit_v;
608 } 586 }
609 587
610 uw_unit uw_Basis_attrifyString_w(uw_context ctx, uw_Basis_string s) { 588 uw_unit uw_Basis_attrifyString_w(uw_context ctx, uw_Basis_string s) {
633 char *uw_Basis_urlifyInt(uw_context ctx, uw_Basis_int n) { 611 char *uw_Basis_urlifyInt(uw_context ctx, uw_Basis_int n) {
634 int len; 612 int len;
635 char *r; 613 char *r;
636 614
637 uw_check_heap(ctx, INTS_MAX); 615 uw_check_heap(ctx, INTS_MAX);
638 r = ctx->heap_front; 616 r = ctx->heap.front;
639 sprintf(r, "%lld%n", n, &len); 617 sprintf(r, "%lld%n", n, &len);
640 ctx->heap_front += len+1; 618 ctx->heap.front += len+1;
641 return r; 619 return r;
642 } 620 }
643 621
644 char *uw_Basis_urlifyFloat(uw_context ctx, uw_Basis_float n) { 622 char *uw_Basis_urlifyFloat(uw_context ctx, uw_Basis_float n) {
645 int len; 623 int len;
646 char *r; 624 char *r;
647 625
648 uw_check_heap(ctx, FLOATS_MAX); 626 uw_check_heap(ctx, FLOATS_MAX);
649 r = ctx->heap_front; 627 r = ctx->heap.front;
650 sprintf(r, "%g%n", n, &len); 628 sprintf(r, "%g%n", n, &len);
651 ctx->heap_front += len+1; 629 ctx->heap.front += len+1;
652 return r; 630 return r;
653 } 631 }
654 632
655 char *uw_Basis_urlifyString(uw_context ctx, uw_Basis_string s) { 633 char *uw_Basis_urlifyString(uw_context ctx, uw_Basis_string s) {
656 char *r, *p; 634 char *r, *p;
657 635
658 uw_check_heap(ctx, strlen(s) * 3 + 1); 636 uw_check_heap(ctx, strlen(s) * 3 + 1);
659 637
660 for (r = p = ctx->heap_front; *s; s++) { 638 for (r = p = ctx->heap.front; *s; s++) {
661 char c = *s; 639 char c = *s;
662 640
663 if (c == ' ') 641 if (c == ' ')
664 *p++ = '+'; 642 *p++ = '+';
665 else if (isalnum(c)) 643 else if (isalnum(c))
669 p += 3; 647 p += 3;
670 } 648 }
671 } 649 }
672 650
673 *p++ = 0; 651 *p++ = 0;
674 ctx->heap_front = p; 652 ctx->heap.front = p;
675 return r; 653 return r;
676 } 654 }
677 655
678 char *uw_Basis_urlifyBool(uw_context ctx, uw_Basis_bool b) { 656 char *uw_Basis_urlifyBool(uw_context ctx, uw_Basis_bool b) {
679 if (b == uw_Basis_False) 657 if (b == uw_Basis_False)
683 } 661 }
684 662
685 static void uw_Basis_urlifyInt_w_unsafe(uw_context ctx, uw_Basis_int n) { 663 static void uw_Basis_urlifyInt_w_unsafe(uw_context ctx, uw_Basis_int n) {
686 int len; 664 int len;
687 665
688 sprintf(ctx->page_front, "%lld%n", n, &len); 666 sprintf(ctx->page.front, "%lld%n", n, &len);
689 ctx->page_front += len; 667 ctx->page.front += len;
690 } 668 }
691 669
692 uw_unit uw_Basis_urlifyInt_w(uw_context ctx, uw_Basis_int n) { 670 uw_unit uw_Basis_urlifyInt_w(uw_context ctx, uw_Basis_int n) {
693 uw_check(ctx, INTS_MAX); 671 uw_check(ctx, INTS_MAX);
694 uw_Basis_urlifyInt_w_unsafe(ctx, n); 672 uw_Basis_urlifyInt_w_unsafe(ctx, n);
698 676
699 uw_unit uw_Basis_urlifyFloat_w(uw_context ctx, uw_Basis_float n) { 677 uw_unit uw_Basis_urlifyFloat_w(uw_context ctx, uw_Basis_float n) {
700 int len; 678 int len;
701 679
702 uw_check(ctx, FLOATS_MAX); 680 uw_check(ctx, FLOATS_MAX);
703 sprintf(ctx->page_front, "%g%n", n, &len); 681 sprintf(ctx->page.front, "%g%n", n, &len);
704 ctx->page_front += len; 682 ctx->page.front += len;
705 683
706 return uw_unit_v; 684 return uw_unit_v;
707 } 685 }
708 686
709 uw_Basis_string uw_Basis_urlifyTime(uw_context ctx, uw_Basis_time t) { 687 uw_Basis_string uw_Basis_urlifyTime(uw_context ctx, uw_Basis_time t) {
719 if (c == ' ') 697 if (c == ' ')
720 uw_writec_unsafe(ctx, '+'); 698 uw_writec_unsafe(ctx, '+');
721 else if (isalnum(c)) 699 else if (isalnum(c))
722 uw_writec_unsafe(ctx, c); 700 uw_writec_unsafe(ctx, c);
723 else { 701 else {
724 sprintf(ctx->page_front, "%%%02X", c); 702 sprintf(ctx->page.front, "%%%02X", c);
725 ctx->page_front += 3; 703 ctx->page.front += 3;
726 } 704 }
727 } 705 }
728 706
729 return uw_unit_v; 707 return uw_unit_v;
730 } 708 }
820 int len, n; 798 int len, n;
821 799
822 len = strlen(*s); 800 len = strlen(*s);
823 uw_check_heap(ctx, len + 1); 801 uw_check_heap(ctx, len + 1);
824 802
825 r = ctx->heap_front; 803 r = ctx->heap.front;
826 ctx->heap_front = uw_unurlifyString_to(ctx, ctx->heap_front, *s); 804 ctx->heap.front = uw_unurlifyString_to(ctx, ctx->heap.front, *s);
827 *s = new_s; 805 *s = new_s;
828 return r; 806 return r;
829 } 807 }
830 808
831 809
832 char *uw_Basis_htmlifyInt(uw_context ctx, uw_Basis_int n) { 810 char *uw_Basis_htmlifyInt(uw_context ctx, uw_Basis_int n) {
833 int len; 811 int len;
834 char *r; 812 char *r;
835 813
836 uw_check_heap(ctx, INTS_MAX); 814 uw_check_heap(ctx, INTS_MAX);
837 r = ctx->heap_front; 815 r = ctx->heap.front;
838 sprintf(r, "%lld%n", n, &len); 816 sprintf(r, "%lld%n", n, &len);
839 ctx->heap_front += len+1; 817 ctx->heap.front += len+1;
840 return r; 818 return r;
841 } 819 }
842 820
843 uw_unit uw_Basis_htmlifyInt_w(uw_context ctx, uw_Basis_int n) { 821 uw_unit uw_Basis_htmlifyInt_w(uw_context ctx, uw_Basis_int n) {
844 int len; 822 int len;
845 823
846 uw_check(ctx, INTS_MAX); 824 uw_check(ctx, INTS_MAX);
847 sprintf(ctx->page_front, "%lld%n", n, &len); 825 sprintf(ctx->page.front, "%lld%n", n, &len);
848 ctx->page_front += len; 826 ctx->page.front += len;
849 827
850 return uw_unit_v; 828 return uw_unit_v;
851 } 829 }
852 830
853 char *uw_Basis_htmlifyFloat(uw_context ctx, uw_Basis_float n) { 831 char *uw_Basis_htmlifyFloat(uw_context ctx, uw_Basis_float n) {
854 int len; 832 int len;
855 char *r; 833 char *r;
856 834
857 uw_check_heap(ctx, FLOATS_MAX); 835 uw_check_heap(ctx, FLOATS_MAX);
858 r = ctx->heap_front; 836 r = ctx->heap.front;
859 sprintf(r, "%g%n", n, &len); 837 sprintf(r, "%g%n", n, &len);
860 ctx->heap_front += len+1; 838 ctx->heap.front += len+1;
861 return r; 839 return r;
862 } 840 }
863 841
864 uw_unit uw_Basis_htmlifyFloat_w(uw_context ctx, uw_Basis_float n) { 842 uw_unit uw_Basis_htmlifyFloat_w(uw_context ctx, uw_Basis_float n) {
865 int len; 843 int len;
866 844
867 uw_check(ctx, FLOATS_MAX); 845 uw_check(ctx, FLOATS_MAX);
868 sprintf(ctx->page_front, "%g%n", n, &len); 846 sprintf(ctx->page.front, "%g%n", n, &len);
869 ctx->page_front += len; 847 ctx->page.front += len;
870 848
871 return uw_unit_v; 849 return uw_unit_v;
872 } 850 }
873 851
874 char *uw_Basis_htmlifyString(uw_context ctx, uw_Basis_string s) { 852 char *uw_Basis_htmlifyString(uw_context ctx, uw_Basis_string s) {
875 char *r, *s2; 853 char *r, *s2;
876 854
877 uw_check_heap(ctx, strlen(s) * 5 + 1); 855 uw_check_heap(ctx, strlen(s) * 5 + 1);
878 856
879 for (r = s2 = ctx->heap_front; *s; s++) { 857 for (r = s2 = ctx->heap.front; *s; s++) {
880 char c = *s; 858 char c = *s;
881 859
882 switch (c) { 860 switch (c) {
883 case '<': 861 case '<':
884 strcpy(s2, "&lt;"); 862 strcpy(s2, "&lt;");
898 } 876 }
899 } 877 }
900 } 878 }
901 879
902 *s2++ = 0; 880 *s2++ = 0;
903 ctx->heap_front = s2; 881 ctx->heap.front = s2;
904 return r; 882 return r;
905 } 883 }
906 884
907 uw_unit uw_Basis_htmlifyString_w(uw_context ctx, uw_Basis_string s) { 885 uw_unit uw_Basis_htmlifyString_w(uw_context ctx, uw_Basis_string s) {
908 uw_check(ctx, strlen(s) * 6); 886 uw_check(ctx, strlen(s) * 6);
939 } 917 }
940 918
941 uw_unit uw_Basis_htmlifyBool_w(uw_context ctx, uw_Basis_bool b) { 919 uw_unit uw_Basis_htmlifyBool_w(uw_context ctx, uw_Basis_bool b) {
942 if (b == uw_Basis_False) { 920 if (b == uw_Basis_False) {
943 uw_check(ctx, 6); 921 uw_check(ctx, 6);
944 strcpy(ctx->page_front, "False"); 922 strcpy(ctx->page.front, "False");
945 ctx->page_front += 5; 923 ctx->page.front += 5;
946 } else { 924 } else {
947 uw_check(ctx, 5); 925 uw_check(ctx, 5);
948 strcpy(ctx->page_front, "True"); 926 strcpy(ctx->page.front, "True");
949 ctx->page_front += 4; 927 ctx->page.front += 4;
950 } 928 }
951 929
952 return uw_unit_v; 930 return uw_unit_v;
953 } 931 }
954 932
960 char *r; 938 char *r;
961 struct tm stm; 939 struct tm stm;
962 940
963 if (localtime_r(&t, &stm)) { 941 if (localtime_r(&t, &stm)) {
964 uw_check_heap(ctx, TIMES_MAX); 942 uw_check_heap(ctx, TIMES_MAX);
965 r = ctx->heap_front; 943 r = ctx->heap.front;
966 len = strftime(r, TIMES_MAX, TIME_FMT, &stm); 944 len = strftime(r, TIMES_MAX, TIME_FMT, &stm);
967 ctx->heap_front += len+1; 945 ctx->heap.front += len+1;
968 return r; 946 return r;
969 } else 947 } else
970 return "<i>Invalid time</i>"; 948 return "<i>Invalid time</i>";
971 } 949 }
972 950
975 char *r; 953 char *r;
976 struct tm stm; 954 struct tm stm;
977 955
978 if (localtime_r(&t, &stm)) { 956 if (localtime_r(&t, &stm)) {
979 uw_check(ctx, TIMES_MAX); 957 uw_check(ctx, TIMES_MAX);
980 r = ctx->page_front; 958 r = ctx->page.front;
981 len = strftime(r, TIMES_MAX, TIME_FMT, &stm); 959 len = strftime(r, TIMES_MAX, TIME_FMT, &stm);
982 ctx->page_front += len; 960 ctx->page.front += len;
983 } else { 961 } else {
984 uw_check(ctx, 20); 962 uw_check(ctx, 20);
985 strcpy(ctx->page_front, "<i>Invalid time</i>"); 963 strcpy(ctx->page.front, "<i>Invalid time</i>");
986 ctx->page_front += 19; 964 ctx->page.front += 19;
987 } 965 }
988 966
989 return uw_unit_v; 967 return uw_unit_v;
990 } 968 }
991 969
993 int len = strlen(s1) + strlen(s2) + 1; 971 int len = strlen(s1) + strlen(s2) + 1;
994 char *s; 972 char *s;
995 973
996 uw_check_heap(ctx, len); 974 uw_check_heap(ctx, len);
997 975
998 s = ctx->heap_front; 976 s = ctx->heap.front;
999 977
1000 strcpy(s, s1); 978 strcpy(s, s1);
1001 strcat(s, s2); 979 strcat(s, s2);
1002 ctx->heap_front += len; 980 ctx->heap.front += len;
1003 981
1004 return s; 982 return s;
1005 } 983 }
1006 984
1007 uw_Basis_string uw_Basis_strdup(uw_context ctx, uw_Basis_string s1) { 985 uw_Basis_string uw_Basis_strdup(uw_context ctx, uw_Basis_string s1) {
1008 int len = strlen(s1) + 1; 986 int len = strlen(s1) + 1;
1009 char *s; 987 char *s;
1010 988
1011 uw_check_heap(ctx, len); 989 uw_check_heap(ctx, len);
1012 990
1013 s = ctx->heap_front; 991 s = ctx->heap.front;
1014 992
1015 strcpy(s, s1); 993 strcpy(s, s1);
1016 ctx->heap_front += len; 994 ctx->heap.front += len;
1017 995
1018 return s; 996 return s;
1019 } 997 }
1020 998
1021 uw_Basis_string uw_Basis_maybe_strdup(uw_context ctx, uw_Basis_string s1) { 999 uw_Basis_string uw_Basis_maybe_strdup(uw_context ctx, uw_Basis_string s1) {
1029 char *uw_Basis_sqlifyInt(uw_context ctx, uw_Basis_int n) { 1007 char *uw_Basis_sqlifyInt(uw_context ctx, uw_Basis_int n) {
1030 int len; 1008 int len;
1031 char *r; 1009 char *r;
1032 1010
1033 uw_check_heap(ctx, INTS_MAX + 6); 1011 uw_check_heap(ctx, INTS_MAX + 6);
1034 r = ctx->heap_front; 1012 r = ctx->heap.front;
1035 sprintf(r, "%lld::int8%n", n, &len); 1013 sprintf(r, "%lld::int8%n", n, &len);
1036 ctx->heap_front += len+1; 1014 ctx->heap.front += len+1;
1037 return r; 1015 return r;
1038 } 1016 }
1039 1017
1040 char *uw_Basis_sqlifyIntN(uw_context ctx, uw_Basis_int *n) { 1018 char *uw_Basis_sqlifyIntN(uw_context ctx, uw_Basis_int *n) {
1041 if (n == NULL) 1019 if (n == NULL)
1047 char *uw_Basis_sqlifyFloat(uw_context ctx, uw_Basis_float n) { 1025 char *uw_Basis_sqlifyFloat(uw_context ctx, uw_Basis_float n) {
1048 int len; 1026 int len;
1049 char *r; 1027 char *r;
1050 1028
1051 uw_check_heap(ctx, FLOATS_MAX + 8); 1029 uw_check_heap(ctx, FLOATS_MAX + 8);
1052 r = ctx->heap_front; 1030 r = ctx->heap.front;
1053 sprintf(r, "%g::float8%n", n, &len); 1031 sprintf(r, "%g::float8%n", n, &len);
1054 ctx->heap_front += len+1; 1032 ctx->heap.front += len+1;
1055 return r; 1033 return r;
1056 } 1034 }
1057 1035
1058 char *uw_Basis_sqlifyFloatN(uw_context ctx, uw_Basis_float *n) { 1036 char *uw_Basis_sqlifyFloatN(uw_context ctx, uw_Basis_float *n) {
1059 if (n == NULL) 1037 if (n == NULL)
1066 uw_Basis_string uw_Basis_sqlifyString(uw_context ctx, uw_Basis_string s) { 1044 uw_Basis_string uw_Basis_sqlifyString(uw_context ctx, uw_Basis_string s) {
1067 char *r, *s2; 1045 char *r, *s2;
1068 1046
1069 uw_check_heap(ctx, strlen(s) * 2 + 10); 1047 uw_check_heap(ctx, strlen(s) * 2 + 10);
1070 1048
1071 r = s2 = ctx->heap_front; 1049 r = s2 = ctx->heap.front;
1072 *s2++ = 'E'; 1050 *s2++ = 'E';
1073 *s2++ = '\''; 1051 *s2++ = '\'';
1074 1052
1075 for (; *s; s++) { 1053 for (; *s; s++) {
1076 char c = *s; 1054 char c = *s;
1093 } 1071 }
1094 } 1072 }
1095 } 1073 }
1096 1074
1097 strcpy(s2, "'::text"); 1075 strcpy(s2, "'::text");
1098 ctx->heap_front = s2 + 8; 1076 ctx->heap.front = s2 + 8;
1099 return r; 1077 return r;
1100 } 1078 }
1101 1079
1102 uw_Basis_string uw_Basis_sqlifyStringN(uw_context ctx, uw_Basis_string s) { 1080 uw_Basis_string uw_Basis_sqlifyStringN(uw_context ctx, uw_Basis_string s) {
1103 if (s == NULL) 1081 if (s == NULL)
1125 char *r; 1103 char *r;
1126 struct tm stm; 1104 struct tm stm;
1127 1105
1128 if (localtime_r(&t, &stm)) { 1106 if (localtime_r(&t, &stm)) {
1129 uw_check_heap(ctx, TIMES_MAX); 1107 uw_check_heap(ctx, TIMES_MAX);
1130 r = ctx->heap_front; 1108 r = ctx->heap.front;
1131 len = strftime(r, TIMES_MAX, TIME_FMT, &stm); 1109 len = strftime(r, TIMES_MAX, TIME_FMT, &stm);
1132 ctx->heap_front += len+1; 1110 ctx->heap.front += len+1;
1133 return r; 1111 return r;
1134 } else 1112 } else
1135 return "<Invalid time>"; 1113 return "<Invalid time>";
1136 } 1114 }
1137 1115
1155 uw_Basis_string uw_Basis_intToString(uw_context ctx, uw_Basis_int n) { 1133 uw_Basis_string uw_Basis_intToString(uw_context ctx, uw_Basis_int n) {
1156 int len; 1134 int len;
1157 char *r; 1135 char *r;
1158 1136
1159 uw_check_heap(ctx, INTS_MAX); 1137 uw_check_heap(ctx, INTS_MAX);
1160 r = ctx->heap_front; 1138 r = ctx->heap.front;
1161 sprintf(r, "%lld%n", n, &len); 1139 sprintf(r, "%lld%n", n, &len);
1162 ctx->heap_front += len+1; 1140 ctx->heap.front += len+1;
1163 return r; 1141 return r;
1164 } 1142 }
1165 1143
1166 uw_Basis_string uw_Basis_floatToString(uw_context ctx, uw_Basis_float n) { 1144 uw_Basis_string uw_Basis_floatToString(uw_context ctx, uw_Basis_float n) {
1167 int len; 1145 int len;
1168 char *r; 1146 char *r;
1169 1147
1170 uw_check_heap(ctx, FLOATS_MAX); 1148 uw_check_heap(ctx, FLOATS_MAX);
1171 r = ctx->heap_front; 1149 r = ctx->heap.front;
1172 sprintf(r, "%g%n", n, &len); 1150 sprintf(r, "%g%n", n, &len);
1173 ctx->heap_front += len+1; 1151 ctx->heap.front += len+1;
1174 return r; 1152 return r;
1175 } 1153 }
1176 1154
1177 uw_Basis_string uw_Basis_boolToString(uw_context ctx, uw_Basis_bool b) { 1155 uw_Basis_string uw_Basis_boolToString(uw_context ctx, uw_Basis_bool b) {
1178 if (b == uw_Basis_False) 1156 if (b == uw_Basis_False)
1186 char *r; 1164 char *r;
1187 struct tm stm; 1165 struct tm stm;
1188 1166
1189 if (localtime_r(&t, &stm)) { 1167 if (localtime_r(&t, &stm)) {
1190 uw_check_heap(ctx, TIMES_MAX); 1168 uw_check_heap(ctx, TIMES_MAX);
1191 r = ctx->heap_front; 1169 r = ctx->heap.front;
1192 len = strftime(r, TIMES_MAX, TIME_FMT, &stm); 1170 len = strftime(r, TIMES_MAX, TIME_FMT, &stm);
1193 ctx->heap_front += len+1; 1171 ctx->heap.front += len+1;
1194 return r; 1172 return r;
1195 } else 1173 } else
1196 return "<Invalid time>"; 1174 return "<Invalid time>";
1197 } 1175 }
1198 1176
1335 } 1313 }
1336 } 1314 }
1337 1315
1338 uw_Basis_string uw_Basis_get_cookie(uw_context ctx, uw_Basis_string c) { 1316 uw_Basis_string uw_Basis_get_cookie(uw_context ctx, uw_Basis_string c) {
1339 int len = strlen(c); 1317 int len = strlen(c);
1340 char *s = ctx->headers, *p = ctx->outHeaders; 1318 char *s = ctx->headers, *p = ctx->outHeaders.start;
1341 1319
1342 while (p = strstr(p, "\nSet-Cookie: ")) { 1320 while (p = strstr(p, "\nSet-Cookie: ")) {
1343 char *p2; 1321 char *p2;
1344 p += 13; 1322 p += 13;
1345 p2 = strchr(p, '='); 1323 p2 = strchr(p, '=');