Mercurial > urweb
comparison src/c/urweb.c @ 1446:36f7d1debb37
Each context gets its own non-repeating sequence of source numbers
author | Adam Chlipala <adam@chlipala.net> |
---|---|
date | Sat, 09 Apr 2011 14:36:47 -0400 |
parents | 0fc7b676b88b |
children | 17393c5e2b90 |
comparison
equal
deleted
inserted
replaced
1445:6e6f1643c4e9 | 1446:36f7d1debb37 |
---|---|
397 void (*free)(void*); | 397 void (*free)(void*); |
398 } global; | 398 } global; |
399 | 399 |
400 struct uw_context { | 400 struct uw_context { |
401 uw_app *app; | 401 uw_app *app; |
402 int id; | |
402 | 403 |
403 char *(*get_header)(void *, const char *); | 404 char *(*get_header)(void *, const char *); |
404 void *get_header_data; | 405 void *get_header_data; |
405 | 406 |
406 uw_buffer outHeaders, page, heap, script; | 407 uw_buffer outHeaders, page, heap, script; |
407 int returning_indirectly; | 408 int returning_indirectly; |
408 input *inputs, *subinputs, *cur_container; | 409 input *inputs, *subinputs, *cur_container; |
409 size_t sz_inputs, n_subinputs, used_subinputs; | 410 size_t sz_inputs, n_subinputs, used_subinputs; |
410 | 411 |
411 int source_count; | 412 unsigned long long source_count; |
412 | 413 |
413 void *db; | 414 void *db; |
414 | 415 |
415 jmp_buf jmp_buf; | 416 jmp_buf jmp_buf; |
416 | 417 |
452 size_t uw_headers_max = SIZE_MAX; | 453 size_t uw_headers_max = SIZE_MAX; |
453 size_t uw_page_max = SIZE_MAX; | 454 size_t uw_page_max = SIZE_MAX; |
454 size_t uw_heap_max = SIZE_MAX; | 455 size_t uw_heap_max = SIZE_MAX; |
455 size_t uw_script_max = SIZE_MAX; | 456 size_t uw_script_max = SIZE_MAX; |
456 | 457 |
457 uw_context uw_init(void *logger_data, uw_logger log_debug) { | 458 uw_context uw_init(int id, void *logger_data, uw_logger log_debug) { |
458 uw_context ctx = malloc(sizeof(struct uw_context)); | 459 uw_context ctx = malloc(sizeof(struct uw_context)); |
459 | 460 |
460 ctx->app = NULL; | 461 ctx->app = NULL; |
462 ctx->id = id; | |
461 | 463 |
462 ctx->get_header = NULL; | 464 ctx->get_header = NULL; |
463 ctx->get_header_data = NULL; | 465 ctx->get_header_data = NULL; |
464 | 466 |
465 uw_buffer_init(uw_headers_max, &ctx->outHeaders, 1); | 467 uw_buffer_init(uw_headers_max, &ctx->outHeaders, 1); |
582 uw_buffer_reset(&ctx->page); | 584 uw_buffer_reset(&ctx->page); |
583 ctx->returning_indirectly = 0; | 585 ctx->returning_indirectly = 0; |
584 uw_buffer_reset(&ctx->heap); | 586 uw_buffer_reset(&ctx->heap); |
585 ctx->regions = NULL; | 587 ctx->regions = NULL; |
586 ctx->cleanup_front = ctx->cleanup; | 588 ctx->cleanup_front = ctx->cleanup; |
587 ctx->source_count = 0; | |
588 ctx->used_deltas = 0; | 589 ctx->used_deltas = 0; |
589 ctx->client = NULL; | 590 ctx->client = NULL; |
590 ctx->cur_container = NULL; | 591 ctx->cur_container = NULL; |
591 ctx->used_transactionals = 0; | 592 ctx->used_transactionals = 0; |
592 ctx->script_header = ""; | 593 ctx->script_header = ""; |
619 | 620 |
620 void uw_close(uw_context ctx) { | 621 void uw_close(uw_context ctx) { |
621 ctx->app->db_close(ctx); | 622 ctx->app->db_close(ctx); |
622 } | 623 } |
623 | 624 |
625 uw_Basis_string uw_Basis_requestHeader(uw_context ctx, uw_Basis_string h) { | |
626 return ctx->get_header(ctx->get_header_data, h); | |
627 } | |
628 | |
624 void uw_set_headers(uw_context ctx, char *(*get_header)(void *, const char *), void *get_header_data) { | 629 void uw_set_headers(uw_context ctx, char *(*get_header)(void *, const char *), void *get_header_data) { |
625 ctx->get_header = get_header; | 630 ctx->get_header = get_header; |
626 ctx->get_header_data = get_header_data; | 631 ctx->get_header_data = get_header_data; |
627 } | 632 } |
628 | 633 |
676 } | 681 } |
677 | 682 |
678 ctx->cleanup_front->func = func; | 683 ctx->cleanup_front->func = func; |
679 ctx->cleanup_front->arg = arg; | 684 ctx->cleanup_front->arg = arg; |
680 ++ctx->cleanup_front; | 685 ++ctx->cleanup_front; |
681 } | |
682 | |
683 uw_Basis_string uw_Basis_requestHeader(uw_context ctx, uw_Basis_string h) { | |
684 return ctx->get_header(ctx->get_header_data, h); | |
685 } | 686 } |
686 | 687 |
687 char *uw_Basis_htmlifyString(uw_context, const char *); | 688 char *uw_Basis_htmlifyString(uw_context, const char *); |
688 | 689 |
689 void uw_login(uw_context ctx) { | 690 void uw_login(uw_context ctx) { |
1294 | 1295 |
1295 const char *uw_Basis_get_script(uw_context ctx, uw_unit u) { | 1296 const char *uw_Basis_get_script(uw_context ctx, uw_unit u) { |
1296 return "<sc>"; | 1297 return "<sc>"; |
1297 } | 1298 } |
1298 | 1299 |
1300 const char *uw_get_real_script(uw_context ctx) { | |
1301 return ctx->script.start; | |
1302 } | |
1303 | |
1299 uw_Basis_string uw_Basis_maybe_onload(uw_context ctx, uw_Basis_string s) { | 1304 uw_Basis_string uw_Basis_maybe_onload(uw_context ctx, uw_Basis_string s) { |
1300 if (s[0] == 0) | 1305 if (s[0] == 0) |
1301 return ""; | 1306 return ""; |
1302 else { | 1307 else { |
1303 char *r = uw_malloc(ctx, 11 + strlen(s)); | 1308 char *r = uw_malloc(ctx, 11 + strlen(s)); |
1343 } | 1348 } |
1344 | 1349 |
1345 uw_Basis_string uw_Basis_jsifyString(uw_context ctx, uw_Basis_string s) { | 1350 uw_Basis_string uw_Basis_jsifyString(uw_context ctx, uw_Basis_string s) { |
1346 char *r, *s2; | 1351 char *r, *s2; |
1347 | 1352 |
1348 uw_check_heap(ctx, strlen(s) * 4 + 2); | 1353 uw_check_heap(ctx, strlen(s) * 4 + 3); |
1349 | 1354 |
1350 r = s2 = ctx->heap.front; | 1355 r = s2 = ctx->heap.front; |
1351 *s2++ = '"'; | 1356 *s2++ = '"'; |
1352 | 1357 |
1353 for (; *s; s++) { | 1358 for (; *s; s++) { |
1383 | 1388 |
1384 uw_Basis_string uw_Basis_jsifyChar(uw_context ctx, uw_Basis_char c1) { | 1389 uw_Basis_string uw_Basis_jsifyChar(uw_context ctx, uw_Basis_char c1) { |
1385 unsigned char c = c1; | 1390 unsigned char c = c1; |
1386 char *r, *s2; | 1391 char *r, *s2; |
1387 | 1392 |
1388 uw_check_heap(ctx, 6); | 1393 uw_check_heap(ctx, 7); |
1389 | 1394 |
1390 r = s2 = ctx->heap.front; | 1395 r = s2 = ctx->heap.front; |
1391 *s2++ = '"'; | 1396 *s2++ = '"'; |
1392 | 1397 |
1393 switch (c) { | 1398 switch (c) { |
1418 } | 1423 } |
1419 | 1424 |
1420 uw_Basis_string uw_Basis_jsifyString_ws(uw_context ctx, uw_Basis_string s) { | 1425 uw_Basis_string uw_Basis_jsifyString_ws(uw_context ctx, uw_Basis_string s) { |
1421 char *r, *s2; | 1426 char *r, *s2; |
1422 | 1427 |
1423 uw_check_script(ctx, strlen(s) * 4 + 2); | 1428 uw_check_script(ctx, strlen(s) * 4 + 3); |
1424 | 1429 |
1425 r = s2 = ctx->script.front; | 1430 r = s2 = ctx->script.front; |
1426 *s2++ = '"'; | 1431 *s2++ = '"'; |
1427 | 1432 |
1428 for (; *s; s++) { | 1433 for (; *s; s++) { |
1465 ctx->heap.front += len+1; | 1470 ctx->heap.front += len+1; |
1466 return r; | 1471 return r; |
1467 } | 1472 } |
1468 } | 1473 } |
1469 | 1474 |
1470 uw_Basis_int uw_Basis_new_client_source(uw_context ctx, uw_Basis_string s) { | 1475 uw_Basis_source uw_Basis_new_client_source(uw_context ctx, uw_Basis_string s) { |
1471 int len; | 1476 int len; |
1472 size_t s_len = strlen(s); | 1477 size_t s_len = strlen(s); |
1473 | 1478 |
1474 uw_check_script(ctx, 18 + INTS_MAX + s_len); | 1479 uw_check_script(ctx, 15 + 2 * INTS_MAX + s_len); |
1475 sprintf(ctx->script.front, "var s%d=sc(exec(%n", ctx->source_count, &len); | 1480 sprintf(ctx->script.front, "s%d_%llu=sc(exec(%n", ctx->id, ctx->source_count, &len); |
1476 ctx->script.front += len; | 1481 ctx->script.front += len; |
1477 strcpy(ctx->script.front, s); | 1482 strcpy(ctx->script.front, s); |
1478 ctx->script.front += s_len; | 1483 ctx->script.front += s_len; |
1479 strcpy(ctx->script.front, "));"); | 1484 strcpy(ctx->script.front, "));"); |
1480 ctx->script.front += 3; | 1485 ctx->script.front += 3; |
1481 | 1486 |
1482 return ctx->source_count++; | 1487 uw_Basis_source r = {ctx->id, ctx->source_count++}; |
1483 } | 1488 return r; |
1484 | 1489 } |
1485 uw_unit uw_Basis_set_client_source(uw_context ctx, uw_Basis_int n, uw_Basis_string s) { | 1490 |
1491 uw_unit uw_Basis_set_client_source(uw_context ctx, uw_Basis_source src, uw_Basis_string s) { | |
1486 int len; | 1492 int len; |
1487 size_t s_len = strlen(s); | 1493 size_t s_len = strlen(s); |
1488 | 1494 |
1489 uw_check_script(ctx, 12 + INTS_MAX + s_len); | 1495 uw_check_script(ctx, 15 + 2 * INTS_MAX + s_len); |
1490 sprintf(ctx->script.front, "sv(s%d,exec(%n", (int)n, &len); | 1496 sprintf(ctx->script.front, "sv(s%d_%llu,exec(%n", src.context, src.source, &len); |
1491 ctx->script.front += len; | 1497 ctx->script.front += len; |
1492 strcpy(ctx->script.front, s); | 1498 strcpy(ctx->script.front, s); |
1493 ctx->script.front += s_len; | 1499 ctx->script.front += s_len; |
1494 strcpy(ctx->script.front, "));"); | 1500 strcpy(ctx->script.front, "));"); |
1495 ctx->script.front += 3; | 1501 ctx->script.front += 3; |
2126 } | 2132 } |
2127 | 2133 |
2128 return uw_unit_v; | 2134 return uw_unit_v; |
2129 } | 2135 } |
2130 | 2136 |
2137 char *uw_Basis_htmlifySource(uw_context ctx, uw_Basis_source src) { | |
2138 int len; | |
2139 char *r; | |
2140 | |
2141 uw_check_heap(ctx, 2 * INTS_MAX + 2); | |
2142 r = ctx->heap.front; | |
2143 sprintf(r, "s%d_%llu%n", src.context, src.source, &len); | |
2144 ctx->heap.front += len+1; | |
2145 return r; | |
2146 } | |
2147 | |
2148 uw_unit uw_Basis_htmlifySource_w(uw_context ctx, uw_Basis_source src) { | |
2149 int len; | |
2150 | |
2151 uw_check(ctx, 2 * INTS_MAX + 1); | |
2152 sprintf(ctx->page.front, "s%d_%llu%n", src.context, src.source, &len); | |
2153 ctx->page.front += len; | |
2154 | |
2155 return uw_unit_v; | |
2156 } | |
2157 | |
2131 uw_Basis_char uw_Basis_strsub(uw_context ctx, uw_Basis_string s, uw_Basis_int n) { | 2158 uw_Basis_char uw_Basis_strsub(uw_context ctx, uw_Basis_string s, uw_Basis_int n) { |
2132 while (n >= 0) { | 2159 while (n >= 0) { |
2133 if (*s == 0) | 2160 if (*s == 0) |
2134 uw_error(ctx, FATAL, "Out-of-bounds strsub"); | 2161 uw_error(ctx, FATAL, "Out-of-bounds strsub"); |
2135 | 2162 |