annotate src/c/urweb.c @ 764:7f653298dd66

C FFI compiler options
author Adam Chlipala <adamc@hcoop.net>
date Thu, 30 Apr 2009 17:15:14 -0400
parents 21f6d2e65685
children c125df6fabfc
rev   line source
adamc@741 1 #define _XOPEN_SOURCE 500
adamc@436 2
adamc@117 3 #include <stdlib.h>
adamc@102 4 #include <stdio.h>
adamc@117 5 #include <string.h>
adamc@457 6 #include <strings.h>
adamc@106 7 #include <ctype.h>
adamc@167 8 #include <setjmp.h>
adamc@167 9 #include <stdarg.h>
adamc@682 10 #include <assert.h>
adamc@741 11 #include <ctype.h>
adamc@102 12
adamc@667 13 #include <pthread.h>
adamc@667 14
adamc@102 15 #include "types.h"
adamc@102 16
adamc@311 17 uw_unit uw_unit_v = {};
adamc@102 18
adamc@667 19
adamc@667 20 // Socket extras
adamc@667 21
adamc@667 22 int uw_really_send(int sock, const void *buf, size_t len) {
adamc@667 23 while (len > 0) {
adamc@667 24 size_t n = send(sock, buf, len, 0);
adamc@667 25
adamc@667 26 if (n < 0)
adamc@667 27 return n;
adamc@667 28
adamc@667 29 buf += n;
adamc@667 30 len -= n;
adamc@667 31 }
adamc@667 32
adamc@667 33 return 0;
adamc@667 34 }
adamc@667 35
adamc@667 36
adamc@667 37 // Buffers
adamc@667 38
adamc@667 39 typedef struct {
adamc@667 40 char *start, *front, *back;
adamc@667 41 } buf;
adamc@667 42
adamc@667 43 static void buf_init(buf *b, size_t s) {
adamc@667 44 b->front = b->start = malloc(s);
adamc@667 45 b->back = b->front + s;
adamc@667 46 }
adamc@667 47
adamc@667 48 static void buf_free(buf *b) {
adamc@667 49 free(b->start);
adamc@667 50 }
adamc@667 51
adamc@667 52 static void buf_reset(buf *b) {
adamc@667 53 b->front = b->start;
adamc@667 54 }
adamc@667 55
adamc@667 56 static void buf_check(buf *b, size_t extra) {
adamc@667 57 if (b->back - b->front < extra) {
adamc@667 58 size_t desired = b->front - b->start + extra, next;
adamc@667 59 char *new_heap;
adamc@667 60
adamc@667 61 next = b->back - b->start;
adamc@667 62 if (next == 0)
adamc@667 63 next = 1;
adamc@667 64 for (; next < desired; next *= 2);
adamc@667 65
adamc@667 66 new_heap = realloc(b->start, next);
adamc@667 67 b->front = new_heap + (b->front - b->start);
adamc@667 68 b->back = new_heap + next;
adamc@667 69 b->start = new_heap;
adamc@667 70 }
adamc@667 71 }
adamc@667 72
adamc@667 73 static size_t buf_used(buf *b) {
adamc@667 74 return b->front - b->start;
adamc@667 75 }
adamc@667 76
adamc@667 77 static size_t buf_avail(buf *b) {
adamc@667 78 return b->back - b->start;
adamc@667 79 }
adamc@667 80
adamc@668 81 static void buf_append(buf *b, const char *s, size_t len) {
adamc@672 82 buf_check(b, len+1);
adamc@668 83 memcpy(b->front, s, len);
adamc@668 84 b->front += len;
adamc@672 85 *b->front = 0;
adamc@668 86 }
adamc@667 87
adamc@668 88
adamc@672 89 // Persistent state types
adamc@667 90
adamc@667 91 typedef enum { UNUSED, USED } usage;
adamc@667 92
adamc@667 93 typedef struct client {
adamc@682 94 unsigned id;
adamc@667 95 usage mode;
adamc@682 96 int pass;
adamc@682 97 struct client *next;
adamc@689 98 pthread_mutex_t lock, pull_lock;
adamc@682 99 buf msgs;
adamc@682 100 int sock;
adamc@682 101 time_t last_contact;
adamc@682 102 unsigned n_channels;
adamc@685 103 unsigned refcount;
adamc@667 104 } client;
adamc@667 105
adamc@672 106
adamc@672 107 // Persistent client state
adamc@672 108
adamc@682 109 static client **clients, *clients_free, *clients_used;
adamc@682 110 static unsigned n_clients;
adamc@667 111
adamc@667 112 static pthread_mutex_t clients_mutex = PTHREAD_MUTEX_INITIALIZER;
adamc@667 113
adamc@682 114 static client *new_client() {
adamc@667 115 client *c;
adamc@667 116
adamc@667 117 pthread_mutex_lock(&clients_mutex);
adamc@667 118
adamc@667 119 if (clients_free) {
adamc@667 120 c = clients_free;
adamc@682 121 clients_free = clients_free->next;
adamc@667 122 }
adamc@667 123 else {
adamc@667 124 ++n_clients;
adamc@667 125 clients = realloc(clients, sizeof(client) * n_clients);
adamc@667 126 c = malloc(sizeof(client));
adamc@667 127 c->id = n_clients-1;
adamc@682 128 pthread_mutex_init(&c->lock, NULL);
adamc@689 129 pthread_mutex_init(&c->pull_lock, NULL);
adamc@682 130 buf_init(&c->msgs, 0);
adamc@667 131 clients[n_clients-1] = c;
adamc@667 132 }
adamc@667 133
adamc@682 134 pthread_mutex_lock(&c->lock);
adamc@667 135 c->mode = USED;
adamc@682 136 c->pass = rand();
adamc@682 137 c->sock = -1;
adamc@682 138 c->last_contact = time(NULL);
adamc@682 139 buf_reset(&c->msgs);
adamc@682 140 c->n_channels = 0;
adamc@685 141 c->refcount = 0;
adamc@682 142 pthread_mutex_unlock(&c->lock);
adamc@682 143
adamc@682 144 c->next = clients_used;
adamc@682 145 clients_used = c;
adamc@667 146
adamc@667 147 pthread_mutex_unlock(&clients_mutex);
adamc@667 148
adamc@667 149 return c;
adamc@667 150 }
adamc@667 151
adamc@685 152 static void use_client(client *c) {
adamc@685 153 pthread_mutex_lock(&c->lock);
adamc@685 154 ++c->refcount;
adamc@685 155 pthread_mutex_unlock(&c->lock);
adamc@689 156 pthread_mutex_lock(&c->pull_lock);
adamc@685 157 }
adamc@685 158
adamc@685 159 static void release_client(client *c) {
adamc@689 160 pthread_mutex_unlock(&c->pull_lock);
adamc@685 161 pthread_mutex_lock(&c->lock);
adamc@685 162 --c->refcount;
adamc@685 163 pthread_mutex_unlock(&c->lock);
adamc@689 164
adamc@685 165 }
adamc@685 166
adamc@667 167 static const char begin_msgs[] = "HTTP/1.1 200 OK\r\nContent-type: text/plain\r\n\r\n";
adamc@667 168
adamc@682 169 static client *find_client(unsigned id) {
adamc@667 170 client *c;
adamc@667 171
adamc@667 172 pthread_mutex_lock(&clients_mutex);
adamc@667 173
adamc@667 174 if (id >= n_clients) {
adamc@667 175 pthread_mutex_unlock(&clients_mutex);
adamc@668 176 return NULL;
adamc@667 177 }
adamc@667 178
adamc@667 179 c = clients[id];
adamc@667 180
adamc@668 181 pthread_mutex_unlock(&clients_mutex);
adamc@668 182 return c;
adamc@668 183 }
adamc@668 184
adamc@682 185 void uw_client_connect(unsigned id, int pass, int sock) {
adamc@682 186 client *c = find_client(id);
adamc@668 187
adamc@668 188 if (c == NULL) {
adamc@667 189 close(sock);
adamc@682 190 fprintf(stderr, "Out-of-bounds client request (%u)\n", id);
adamc@667 191 return;
adamc@667 192 }
adamc@667 193
adamc@682 194 pthread_mutex_lock(&c->lock);
adamc@671 195
adamc@682 196 if (c->mode != USED) {
adamc@682 197 pthread_mutex_unlock(&c->lock);
adamc@667 198 close(sock);
adamc@682 199 fprintf(stderr, "Client request for unused slot (%u)\n", id);
adamc@667 200 return;
adamc@667 201 }
adamc@667 202
adamc@682 203 if (pass != c->pass) {
adamc@682 204 pthread_mutex_unlock(&c->lock);
adamc@682 205 close(sock);
adamc@682 206 fprintf(stderr, "Wrong client password (%u, %d)\n", id, pass);
adamc@682 207 return;
adamc@667 208 }
adamc@667 209
adamc@682 210 if (c->sock != -1) {
adamc@682 211 close(c->sock);
adamc@682 212 c->sock = -1;
adamc@682 213 }
adamc@667 214
adamc@682 215 c->last_contact = time(NULL);
adamc@682 216
adamc@682 217 if (buf_used(&c->msgs) > 0) {
adamc@667 218 uw_really_send(sock, begin_msgs, sizeof(begin_msgs) - 1);
adamc@682 219 uw_really_send(sock, c->msgs.start, buf_used(&c->msgs));
adamc@682 220 buf_reset(&c->msgs);
adamc@667 221 close(sock);
adamc@667 222 }
adamc@668 223 else
adamc@682 224 c->sock = sock;
adamc@667 225
adamc@682 226 pthread_mutex_unlock(&c->lock);
adamc@667 227 }
adamc@667 228
adamc@682 229 static void free_client(client *c) {
adamc@683 230 printf("Freeing client %u\n", c->id);
adamc@667 231
adamc@682 232 c->mode = UNUSED;
adamc@682 233 c->pass = -1;
adamc@667 234
adamc@682 235 c->next = clients_free;
adamc@682 236 clients_free = c;
adamc@667 237 }
adamc@667 238
adamc@682 239 static uw_Basis_channel new_channel(client *c) {
adamc@682 240 uw_Basis_channel ch = {c->id, c->n_channels++};
adamc@668 241 return ch;
adamc@668 242 }
adamc@668 243
adamc@685 244 static void client_send(client *c, buf *msg) {
adamc@685 245 pthread_mutex_lock(&c->lock);
adamc@671 246
adamc@682 247 if (c->sock != -1) {
adamc@682 248 uw_really_send(c->sock, begin_msgs, sizeof(begin_msgs) - 1);
adamc@682 249 uw_really_send(c->sock, msg->start, buf_used(msg));
adamc@682 250 close(c->sock);
adamc@682 251 c->sock = -1;
adamc@682 252 } else
adamc@682 253 buf_append(&c->msgs, msg->start, buf_used(msg));
adamc@671 254
adamc@685 255 pthread_mutex_unlock(&c->lock);
adamc@668 256 }
adamc@668 257
adamc@668 258
adamc@668 259 // Global entry points
adamc@668 260
adamc@668 261 void uw_global_init() {
adamc@668 262 srand(time(NULL) ^ getpid());
adamc@668 263
adamc@668 264 clients = malloc(0);
adamc@668 265 }
adamc@668 266
adamc@668 267
adamc@667 268 // Single-request state
adamc@667 269
adamc@167 270 #define ERROR_BUF_LEN 1024
adamc@167 271
adamc@323 272 typedef struct regions {
adamc@323 273 struct regions *next;
adamc@323 274 } regions;
adamc@323 275
adamc@425 276 typedef struct {
adamc@425 277 void (*func)(void*);
adamc@425 278 void *arg;
adamc@425 279 } cleanup;
adamc@425 280
adamc@671 281 typedef struct {
adamc@682 282 unsigned client;
adamc@671 283 buf msgs;
adamc@682 284 } delta;
adamc@671 285
adamc@737 286 typedef enum {
adamc@759 287 UNSET, NORMAL, FIL, SUBFORM, SUBFORMS, ENTRY
adamc@737 288 } input_kind;
adamc@737 289
adamc@756 290 typedef struct input {
adamc@737 291 input_kind kind;
adamc@737 292 union {
adamc@737 293 char *normal;
adamc@739 294 uw_Basis_file file;
adamc@756 295 struct {
adamc@759 296 struct input *fields, *parent;
adamc@756 297 } subform;
adamc@759 298 struct {
adamc@759 299 struct input *entries, *parent;
adamc@759 300 } subforms;
adamc@759 301 struct {
adamc@759 302 struct input *fields, *next, *parent;
adamc@759 303 } entry;
adamc@737 304 } data;
adamc@737 305 } input;
adamc@737 306
adamc@311 307 struct uw_context {
adamc@458 308 char *headers, *headers_end;
adamc@457 309
adamc@666 310 buf outHeaders, page, heap, script;
adamc@759 311 input *inputs, *subinputs, *cur_container;
adamc@756 312 size_t n_subinputs, used_subinputs;
adamc@167 313
adamc@565 314 int source_count;
adamc@562 315
adamc@272 316 void *db;
adamc@272 317
adamc@167 318 jmp_buf jmp_buf;
adamc@167 319
adamc@323 320 regions *regions;
adamc@323 321
adamc@425 322 cleanup *cleanup, *cleanup_front, *cleanup_back;
adamc@425 323
adamc@667 324 const char *script_header, *url_prefix;
adamc@643 325
adamc@736 326 int needs_push, needs_sig;
adamc@693 327
adamc@682 328 size_t n_deltas, used_deltas;
adamc@682 329 delta *deltas;
adamc@671 330
adamc@673 331 int timeout;
adamc@673 332
adamc@682 333 client *client;
adamc@682 334
adamc@167 335 char error_message[ERROR_BUF_LEN];
adamc@117 336 };
adamc@117 337
adamc@683 338 extern int uw_inputs_len, uw_timeout;
adamc@144 339
adamc@687 340 uw_context uw_init() {
adamc@311 341 uw_context ctx = malloc(sizeof(struct uw_context));
adamc@136 342
adamc@458 343 ctx->headers = ctx->headers_end = NULL;
adamc@457 344
adamc@687 345 buf_init(&ctx->outHeaders, 0);
adamc@687 346 buf_init(&ctx->page, 0);
adamc@687 347 buf_init(&ctx->heap, 0);
adamc@698 348 buf_init(&ctx->script, 1);
adamc@667 349 ctx->script.start[0] = 0;
adamc@136 350
adamc@737 351 ctx->inputs = calloc(uw_inputs_len, sizeof(input));
adamc@759 352 ctx->cur_container = NULL;
adamc@756 353 ctx->subinputs = malloc(0);
adamc@756 354 ctx->n_subinputs = ctx->used_subinputs = 0;
adamc@144 355
adamc@272 356 ctx->db = NULL;
adamc@272 357
adamc@323 358 ctx->regions = NULL;
adamc@323 359
adamc@425 360 ctx->cleanup_front = ctx->cleanup_back = ctx->cleanup = malloc(0);
adamc@425 361
adamc@643 362 ctx->script_header = "";
adamc@667 363 ctx->url_prefix = "/";
adamc@693 364 ctx->needs_push = 0;
adamc@736 365 ctx->needs_sig = 0;
adamc@643 366
adamc@167 367 ctx->error_message[0] = 0;
adamc@167 368
adamc@565 369 ctx->source_count = 0;
adamc@562 370
adamc@682 371 ctx->n_deltas = ctx->used_deltas = 0;
adamc@671 372 ctx->deltas = malloc(0);
adamc@671 373
adamc@673 374 ctx->timeout = uw_timeout;
adamc@673 375
adamc@700 376 ctx->client = NULL;
adamc@700 377
adamc@700 378 ctx->error_message[0] = 0;
adamc@700 379
adamc@117 380 return ctx;
adamc@106 381 }
adamc@106 382
adamc@311 383 void uw_set_db(uw_context ctx, void *db) {
adamc@272 384 ctx->db = db;
adamc@272 385 }
adamc@272 386
adamc@311 387 void *uw_get_db(uw_context ctx) {
adamc@272 388 return ctx->db;
adamc@272 389 }
adamc@272 390
adamc@311 391 void uw_free(uw_context ctx) {
adamc@671 392 size_t i;
adamc@671 393
adamc@666 394 buf_free(&ctx->outHeaders);
adamc@666 395 buf_free(&ctx->script);
adamc@666 396 buf_free(&ctx->page);
adamc@666 397 buf_free(&ctx->heap);
adamc@144 398 free(ctx->inputs);
adamc@756 399 free(ctx->subinputs);
adamc@425 400 free(ctx->cleanup);
adamc@671 401
adamc@682 402 for (i = 0; i < ctx->n_deltas; ++i)
adamc@671 403 buf_free(&ctx->deltas[i].msgs);
adamc@671 404
adamc@136 405 free(ctx);
adamc@136 406 }
adamc@136 407
adamc@562 408 void uw_reset_keep_error_message(uw_context ctx) {
adamc@756 409 size_t i;
adamc@756 410
adamc@666 411 buf_reset(&ctx->outHeaders);
adamc@666 412 buf_reset(&ctx->script);
adamc@667 413 ctx->script.start[0] = 0;
adamc@666 414 buf_reset(&ctx->page);
adamc@666 415 buf_reset(&ctx->heap);
adamc@323 416 ctx->regions = NULL;
adamc@425 417 ctx->cleanup_front = ctx->cleanup;
adamc@565 418 ctx->source_count = 0;
adamc@682 419 ctx->used_deltas = 0;
adamc@682 420 ctx->client = NULL;
adamc@759 421 ctx->cur_container = NULL;
adamc@167 422 }
adamc@167 423
adamc@562 424 void uw_reset_keep_request(uw_context ctx) {
adamc@562 425 uw_reset_keep_error_message(ctx);
adamc@562 426 ctx->error_message[0] = 0;
adamc@167 427 }
adamc@167 428
adamc@311 429 void uw_reset(uw_context ctx) {
adamc@311 430 uw_reset_keep_request(ctx);
adamc@737 431 memset(ctx->inputs, 0, uw_inputs_len * sizeof(input));
adamc@756 432 memset(ctx->subinputs, 0, ctx->n_subinputs * sizeof(input));
adamc@756 433 ctx->used_subinputs = 0;
adamc@144 434 }
adamc@144 435
adamc@311 436 void uw_db_init(uw_context);
adamc@311 437 void uw_handle(uw_context, char *);
adamc@167 438
adamc@311 439 failure_kind uw_begin_init(uw_context ctx) {
adamc@272 440 int r = setjmp(ctx->jmp_buf);
adamc@272 441
adamc@272 442 if (r == 0)
adamc@311 443 uw_db_init(ctx);
adamc@272 444
adamc@272 445 return r;
adamc@272 446 }
adamc@272 447
adamc@458 448 void uw_set_headers(uw_context ctx, char *headers) {
adamc@458 449 char *s = headers, *s2;
adamc@458 450 ctx->headers = headers;
adamc@458 451
adamc@458 452 while (s2 = strchr(s, '\r')) {
adamc@458 453 s = s2;
adamc@458 454
adamc@458 455 if (s[1] == 0)
adamc@458 456 break;
adamc@458 457
adamc@458 458 *s = 0;
adamc@458 459 s += 2;
adamc@458 460 }
adamc@458 461
adamc@458 462 ctx->headers_end = s;
adamc@458 463 }
adamc@458 464
adamc@742 465 void uw_headers_moved(uw_context ctx, char *headers) {
adamc@742 466 ctx->headers_end = headers + (ctx->headers_end - ctx->headers);
adamc@742 467 ctx->headers = headers;
adamc@742 468 }
adamc@742 469
adamc@671 470 int uw_db_begin(uw_context);
adamc@167 471
adamc@311 472 __attribute__((noreturn)) void uw_error(uw_context ctx, failure_kind fk, const char *fmt, ...) {
adamc@425 473 cleanup *cl;
adamc@425 474
adamc@167 475 va_list ap;
adamc@167 476 va_start(ap, fmt);
adamc@167 477
adamc@167 478 vsnprintf(ctx->error_message, ERROR_BUF_LEN, fmt, ap);
adamc@167 479
adamc@425 480 for (cl = ctx->cleanup; cl < ctx->cleanup_front; ++cl)
adamc@425 481 cl->func(cl->arg);
adamc@425 482
adamc@425 483 ctx->cleanup_front = ctx->cleanup;
adamc@425 484
adamc@190 485 longjmp(ctx->jmp_buf, fk);
adamc@167 486 }
adamc@167 487
adamc@425 488 void uw_push_cleanup(uw_context ctx, void (*func)(void *), void *arg) {
adamc@425 489 if (ctx->cleanup_front >= ctx->cleanup_back) {
adamc@425 490 int len = ctx->cleanup_back - ctx->cleanup, newLen;
adamc@425 491 if (len == 0)
adamc@425 492 newLen = 1;
adamc@425 493 else
adamc@428 494 newLen = len * 2;
adamc@470 495 ctx->cleanup = realloc(ctx->cleanup, newLen * sizeof(cleanup));
adamc@425 496 ctx->cleanup_front = ctx->cleanup + len;
adamc@425 497 ctx->cleanup_back = ctx->cleanup + newLen;
adamc@425 498 }
adamc@425 499
adamc@425 500 ctx->cleanup_front->func = func;
adamc@425 501 ctx->cleanup_front->arg = arg;
adamc@425 502 ++ctx->cleanup_front;
adamc@425 503 }
adamc@425 504
adamc@682 505 uw_Basis_string uw_Basis_requestHeader(uw_context ctx, uw_Basis_string h) {
adamc@682 506 int len = strlen(h);
adamc@682 507 char *s = ctx->headers, *p;
adamc@682 508
adamc@682 509 while (p = strchr(s, ':')) {
adamc@682 510 if (p - s == len && !strncasecmp(s, h, len)) {
adamc@682 511 return p + 2;
adamc@682 512 } else {
adamc@682 513 if ((s = strchr(p, 0)) && s < ctx->headers_end)
adamc@682 514 s += 2;
adamc@682 515 else
adamc@682 516 return NULL;
adamc@682 517 }
adamc@682 518 }
adamc@682 519
adamc@682 520 return NULL;
adamc@682 521 }
adamc@682 522
adamc@682 523 void uw_login(uw_context ctx) {
adamc@693 524 if (ctx->needs_push) {
adamc@682 525 char *id_s, *pass_s;
adamc@682 526
adamc@682 527 if ((id_s = uw_Basis_requestHeader(ctx, "UrWeb-Client"))
adamc@682 528 && (pass_s = uw_Basis_requestHeader(ctx, "UrWeb-Pass"))) {
adamc@682 529 unsigned id = atoi(id_s);
adamc@682 530 int pass = atoi(pass_s);
adamc@682 531 client *c = find_client(id);
adamc@682 532
adamc@682 533 if (c == NULL)
adamc@682 534 uw_error(ctx, FATAL, "Unknown client ID in HTTP headers (%s, %s)", id_s, pass_s);
adamc@682 535 else {
adamc@685 536 use_client(c);
adamc@682 537 ctx->client = c;
adamc@682 538
adamc@682 539 if (c->mode != USED)
adamc@682 540 uw_error(ctx, FATAL, "Stale client ID (%u) in subscription request", id);
adamc@682 541 if (c->pass != pass)
adamc@682 542 uw_error(ctx, FATAL, "Wrong client password (%u, %d) in subscription request", id, pass);
adamc@682 543 }
adamc@682 544 } else {
adamc@682 545 client *c = new_client();
adamc@685 546 use_client(c);
adamc@682 547 ctx->client = c;
adamc@682 548 }
adamc@682 549 }
adamc@682 550 }
adamc@682 551
adamc@671 552 failure_kind uw_begin(uw_context ctx, char *path) {
adamc@671 553 int r = setjmp(ctx->jmp_buf);
adamc@671 554
adamc@671 555 if (r == 0) {
adamc@671 556 if (uw_db_begin(ctx))
adamc@671 557 uw_error(ctx, BOUNDED_RETRY, "Error running SQL BEGIN");
adamc@682 558
adamc@671 559 uw_handle(ctx, path);
adamc@671 560 }
adamc@671 561
adamc@671 562 return r;
adamc@671 563 }
adamc@671 564
adamc@682 565 uw_Basis_client uw_Basis_self(uw_context ctx, uw_unit u) {
adamc@682 566 if (ctx->client == NULL)
adamc@682 567 uw_error(ctx, FATAL, "Call to Basis.self() from page that has only server-side code");
adamc@682 568
adamc@682 569 return ctx->client->id;
adamc@682 570 }
adamc@682 571
adamc@425 572 void uw_pop_cleanup(uw_context ctx) {
adamc@425 573 if (ctx->cleanup_front == ctx->cleanup)
adamc@425 574 uw_error(ctx, FATAL, "Attempt to pop from empty cleanup action stack");
adamc@425 575
adamc@425 576 --ctx->cleanup_front;
adamc@425 577 ctx->cleanup_front->func(ctx->cleanup_front->arg);
adamc@425 578 }
adamc@425 579
adamc@311 580 char *uw_error_message(uw_context ctx) {
adamc@167 581 return ctx->error_message;
adamc@167 582 }
adamc@167 583
adamc@737 584 extern int uw_input_num(const char*);
adamc@144 585
adamc@759 586 static input *INP(uw_context ctx) {
adamc@759 587 if (ctx->cur_container == NULL)
adamc@759 588 return ctx->inputs;
adamc@759 589 else if (ctx->cur_container->kind == SUBFORM)
adamc@759 590 return ctx->cur_container->data.subform.fields;
adamc@759 591 else if (ctx->cur_container->kind == ENTRY)
adamc@759 592 return ctx->cur_container->data.entry.fields;
adamc@759 593 else
adamc@760 594 uw_error(ctx, FATAL, "INP: Wrong kind (%d, %p)", ctx->cur_container->kind, ctx->cur_container);
adamc@759 595 }
adamc@759 596
adamc@760 597 static void adjust_pointer(input **ptr, input *old_start, input *new_start, size_t len) {
adamc@760 598 if (*ptr != NULL && *ptr >= old_start && *ptr < old_start + len)
adamc@760 599 *ptr += new_start - old_start;
adamc@760 600 }
adamc@760 601
adamc@760 602 static void adjust_input(input *x, input *old_start, input *new_start, size_t len) {
adamc@759 603 switch (x->kind) {
adamc@759 604 case SUBFORM:
adamc@760 605 adjust_pointer(&x->data.subform.fields, old_start, new_start, len);
adamc@760 606 adjust_pointer(&x->data.subform.parent, old_start, new_start, len);
adamc@759 607 break;
adamc@759 608 case SUBFORMS:
adamc@760 609 adjust_pointer(&x->data.subforms.entries, old_start, new_start, len);
adamc@760 610 adjust_pointer(&x->data.subforms.parent, old_start, new_start, len);
adamc@759 611 break;
adamc@759 612 case ENTRY:
adamc@760 613 adjust_pointer(&x->data.entry.fields, old_start, new_start, len);
adamc@760 614 adjust_pointer(&x->data.entry.next, old_start, new_start, len);
adamc@760 615 adjust_pointer(&x->data.entry.parent, old_start, new_start, len);
adamc@759 616 }
adamc@759 617 }
adamc@759 618
adamc@759 619 static input *check_input_space(uw_context ctx, size_t len) {
adamc@759 620 size_t i;
adamc@759 621 input *r;
adamc@759 622
adamc@759 623 if (ctx->used_subinputs + len >= ctx->n_subinputs) {
adamc@759 624 input *new_subinputs = realloc(ctx->subinputs, sizeof(input) * (ctx->used_subinputs + len));
adamc@759 625 size_t offset = new_subinputs - ctx->subinputs;
adamc@759 626
adamc@760 627 if (ctx->subinputs != new_subinputs) {
adamc@760 628 for (i = 0; i < ctx->used_subinputs; ++i)
adamc@760 629 adjust_input(&new_subinputs[i], ctx->subinputs, new_subinputs, ctx->used_subinputs);
adamc@760 630 for (i = 0; i < uw_inputs_len; ++i)
adamc@760 631 adjust_input(&ctx->inputs[i], ctx->subinputs, new_subinputs, ctx->used_subinputs);
adamc@760 632
adamc@760 633 adjust_pointer(&ctx->cur_container, ctx->subinputs, new_subinputs, ctx->used_subinputs);
adamc@760 634
adamc@760 635 ctx->n_subinputs = ctx->used_subinputs + len;
adamc@760 636 ctx->subinputs = new_subinputs;
adamc@760 637 }
adamc@759 638 }
adamc@759 639
adamc@759 640 r = &ctx->subinputs[ctx->used_subinputs];
adamc@759 641
adamc@759 642 for (i = 0; i < len; ++i)
adamc@759 643 ctx->subinputs[ctx->used_subinputs++].kind = UNUSED;
adamc@759 644
adamc@759 645 return r;
adamc@759 646 }
adamc@756 647
adamc@737 648 void uw_set_input(uw_context ctx, const char *name, char *value) {
adamc@756 649 if (!strcasecmp(name, ".b")) {
adamc@756 650 int n = uw_input_num(value);
adamc@756 651 input *inps;
adamc@756 652
adamc@756 653 if (n < 0)
adamc@756 654 uw_error(ctx, FATAL, "Bad subform name %s", value);
adamc@756 655
adamc@756 656 if (n >= uw_inputs_len)
adamc@756 657 uw_error(ctx, FATAL, "For subform name %s, index %d is out of range", value, n);
adamc@756 658
adamc@759 659 inps = check_input_space(ctx, uw_inputs_len);
adamc@759 660
adamc@759 661 INP(ctx)[n].kind = SUBFORM;
adamc@759 662 INP(ctx)[n].data.subform.parent = ctx->cur_container;
adamc@759 663 INP(ctx)[n].data.subform.fields = inps;
adamc@759 664 ctx->cur_container = &INP(ctx)[n];
adamc@756 665 } else if (!strcasecmp(name, ".e")) {
adamc@756 666 input *tmp;
adamc@756 667
adamc@759 668 if (ctx->cur_container == NULL)
adamc@756 669 uw_error(ctx, FATAL, "Unmatched subform closer");
adamc@756 670
adamc@759 671 tmp = ctx->cur_container;
adamc@759 672 switch (tmp->kind) {
adamc@759 673 case SUBFORM:
adamc@759 674 ctx->cur_container = tmp->data.subform.parent;
adamc@759 675 tmp->data.subform.parent = NULL;
adamc@759 676 break;
adamc@759 677 case SUBFORMS:
adamc@759 678 ctx->cur_container = tmp->data.subforms.parent;
adamc@759 679 tmp->data.subforms.parent = NULL;
adamc@759 680 break;
adamc@759 681 case ENTRY:
adamc@759 682 ctx->cur_container = tmp->data.entry.parent;
adamc@759 683 break;
adamc@759 684 default:
adamc@759 685 uw_error(ctx, FATAL, "uw_set_input: Wrong kind");
adamc@759 686 }
adamc@759 687 } else if (!strcasecmp(name, ".s")) {
adamc@759 688 int n = uw_input_num(value);
adamc@759 689
adamc@759 690 if (n < 0)
adamc@759 691 uw_error(ctx, FATAL, "Bad subforms name %s", value);
adamc@759 692
adamc@759 693 if (n >= uw_inputs_len)
adamc@759 694 uw_error(ctx, FATAL, "For subforms name %s, index %d is out of range", value, n);
adamc@759 695
adamc@759 696 INP(ctx)[n].kind = SUBFORMS;
adamc@759 697 INP(ctx)[n].data.subforms.parent = ctx->cur_container;
adamc@759 698 INP(ctx)[n].data.subforms.entries = NULL;
adamc@759 699 ctx->cur_container = &INP(ctx)[n];
adamc@759 700 } else if (!strcasecmp(name, ".i")) {
adamc@759 701 input *inps;
adamc@759 702
adamc@759 703 if (!ctx->cur_container)
adamc@759 704 uw_error(ctx, FATAL, "New entry without container");
adamc@759 705
adamc@759 706 if (ctx->cur_container->kind != SUBFORMS)
adamc@759 707 uw_error(ctx, FATAL, "Bad kind for entry parent");
adamc@759 708
adamc@759 709 inps = check_input_space(ctx, uw_inputs_len + 1);
adamc@759 710
adamc@759 711 inps->kind = ENTRY;
adamc@759 712 inps->data.entry.parent = ctx->cur_container;
adamc@759 713 inps->data.entry.next = ctx->cur_container->data.subforms.entries;
adamc@759 714 ctx->cur_container->data.subforms.entries = inps;
adamc@759 715
adamc@759 716 inps->data.entry.fields = inps+1;
adamc@759 717 ctx->cur_container = inps;
adamc@756 718 } else {
adamc@756 719 int n = uw_input_num(name);
adamc@756 720
adamc@756 721 if (n < 0)
adamc@756 722 uw_error(ctx, FATAL, "Bad input name %s", name);
adamc@756 723
adamc@756 724 if (n >= uw_inputs_len)
adamc@756 725 uw_error(ctx, FATAL, "For input name %s, index %d is out of range", name, n);
adamc@756 726
adamc@756 727 INP(ctx)[n].kind = NORMAL;
adamc@756 728 INP(ctx)[n].data.normal = value;
adamc@756 729 }
adamc@144 730 }
adamc@144 731
adamc@311 732 char *uw_get_input(uw_context ctx, int n) {
adamc@169 733 if (n < 0)
adamc@311 734 uw_error(ctx, FATAL, "Negative input index %d", n);
adamc@311 735 if (n >= uw_inputs_len)
adamc@311 736 uw_error(ctx, FATAL, "Out-of-bounds input index %d", n);
adamc@737 737
adamc@756 738 switch (INP(ctx)[n].kind) {
adamc@737 739 case UNSET:
adamc@737 740 return NULL;
adamc@739 741 case FIL:
adamc@739 742 uw_error(ctx, FATAL, "Tried to read a file form input as normal");
adamc@756 743 case SUBFORM:
adamc@756 744 uw_error(ctx, FATAL, "Tried to read a subform form input as normal");
adamc@759 745 case SUBFORMS:
adamc@759 746 uw_error(ctx, FATAL, "Tried to read a subforms form input as normal");
adamc@759 747 case ENTRY:
adamc@759 748 uw_error(ctx, FATAL, "Tried to read an entry form input as normal");
adamc@737 749 case NORMAL:
adamc@756 750 return INP(ctx)[n].data.normal;
adamc@737 751 default:
adamc@737 752 uw_error(ctx, FATAL, "Impossible input kind");
adamc@737 753 }
adamc@138 754 }
adamc@138 755
adamc@311 756 char *uw_get_optional_input(uw_context ctx, int n) {
adamc@190 757 if (n < 0)
adamc@311 758 uw_error(ctx, FATAL, "Negative input index %d", n);
adamc@311 759 if (n >= uw_inputs_len)
adamc@311 760 uw_error(ctx, FATAL, "Out-of-bounds input index %d", n);
adamc@737 761
adamc@756 762 switch (INP(ctx)[n].kind) {
adamc@737 763 case UNSET:
adamc@737 764 return "";
adamc@739 765 case FIL:
adamc@739 766 uw_error(ctx, FATAL, "Tried to read a file form input as normal");
adamc@756 767 case SUBFORM:
adamc@756 768 uw_error(ctx, FATAL, "Tried to read a subform form input as normal");
adamc@759 769 case SUBFORMS:
adamc@759 770 uw_error(ctx, FATAL, "Tried to read a subforms form input as normal");
adamc@759 771 case ENTRY:
adamc@759 772 uw_error(ctx, FATAL, "Tried to read an entry form input as normal");
adamc@737 773 case NORMAL:
adamc@756 774 return INP(ctx)[n].data.normal;
adamc@737 775 default:
adamc@737 776 uw_error(ctx, FATAL, "Impossible input kind");
adamc@737 777 }
adamc@737 778 }
adamc@737 779
adamc@739 780 void uw_set_file_input(uw_context ctx, const char *name, uw_Basis_file f) {
adamc@737 781 int n = uw_input_num(name);
adamc@737 782
adamc@737 783 if (n < 0)
adamc@737 784 uw_error(ctx, FATAL, "Bad file input name %s", name);
adamc@737 785
adamc@737 786 if (n >= uw_inputs_len)
adamc@737 787 uw_error(ctx, FATAL, "For file input name %s, index %d is out of range", name, n);
adamc@737 788
adamc@739 789 ctx->inputs[n].kind = FIL;
adamc@739 790 ctx->inputs[n].data.file = f;
adamc@737 791 }
adamc@737 792
adamc@739 793 void *uw_malloc(uw_context ctx, size_t len);
adamc@739 794
adamc@760 795
adamc@760 796 static void parents(input *inp) {
adamc@760 797 printf("Stack: %p\n", inp);
adamc@760 798 while (inp) {
adamc@760 799 switch (inp->kind) {
adamc@760 800 case NORMAL:
adamc@760 801 printf("Normal(%p)\n", inp);
adamc@760 802 break;
adamc@760 803 case FIL:
adamc@760 804 printf("File(%p)\n", inp);
adamc@760 805 break;
adamc@760 806 case SUBFORM:
adamc@760 807 printf("Subform; fields = %p\n", inp->data.subform.fields);
adamc@760 808 inp = inp->data.subform.parent;
adamc@760 809 break;
adamc@760 810 case SUBFORMS:
adamc@760 811 printf("Subforms; entries = %p\n", inp->data.subforms.entries);
adamc@760 812 inp = inp->data.subforms.parent;
adamc@760 813 break;
adamc@760 814 case ENTRY:
adamc@760 815 printf("Entry; fields = %p; next = %p\n", inp->data.entry.fields, inp->data.entry.next);
adamc@760 816 inp = inp->data.entry.parent;
adamc@760 817 break;
adamc@760 818 default:
adamc@760 819 inp = NULL;
adamc@760 820 }
adamc@760 821 }
adamc@760 822 }
adamc@760 823
adamc@739 824 uw_Basis_file uw_get_file_input(uw_context ctx, int n) {
adamc@737 825 if (n < 0)
adamc@737 826 uw_error(ctx, FATAL, "Negative file input index %d", n);
adamc@737 827 if (n >= uw_inputs_len)
adamc@737 828 uw_error(ctx, FATAL, "Out-of-bounds file input index %d", n);
adamc@737 829
adamc@756 830 switch (INP(ctx)[n].kind) {
adamc@737 831 case UNSET:
adamc@737 832 {
adamc@739 833 char *data = uw_malloc(ctx, 0);
adamc@740 834 uw_Basis_file f = {NULL, "", {0, data}};
adamc@739 835 return f;
adamc@737 836 }
adamc@739 837 case FIL:
adamc@756 838 return INP(ctx)[n].data.file;
adamc@737 839 case NORMAL:
adamc@737 840 uw_error(ctx, FATAL, "Tried to read a normal form input as files");
adamc@756 841 case SUBFORM:
adamc@756 842 uw_error(ctx, FATAL, "Tried to read a subform form input as files");
adamc@759 843 case SUBFORMS:
adamc@759 844 uw_error(ctx, FATAL, "Tried to read a subforms form input as files");
adamc@759 845 case ENTRY:
adamc@759 846 uw_error(ctx, FATAL, "Tried to read an entry form input as files");
adamc@737 847 default:
adamc@737 848 uw_error(ctx, FATAL, "Impossible input kind");
adamc@737 849 }
adamc@190 850 }
adamc@190 851
adamc@756 852 void uw_enter_subform(uw_context ctx, int n) {
adamc@756 853 if (n < 0)
adamc@756 854 uw_error(ctx, FATAL, "Negative subform index %d", n);
adamc@756 855 if (n >= uw_inputs_len)
adamc@756 856 uw_error(ctx, FATAL, "Out-of-bounds subform index %d", n);
adamc@756 857
adamc@756 858 switch (INP(ctx)[n].kind) {
adamc@756 859 case UNSET:
adamc@756 860 uw_error(ctx, FATAL, "Missing subform");
adamc@756 861 case FIL:
adamc@756 862 uw_error(ctx, FATAL, "Tried to read a file form input as subform");
adamc@756 863 case NORMAL:
adamc@756 864 uw_error(ctx, FATAL, "Tried to read a normal form input as subform");
adamc@759 865 case SUBFORMS:
adamc@759 866 uw_error(ctx, FATAL, "Tried to read a subforms form input as subform");
adamc@759 867 case ENTRY:
adamc@759 868 uw_error(ctx, FATAL, "Tried to read an entry form input as subform");
adamc@756 869 case SUBFORM:
adamc@759 870 INP(ctx)[n].data.subform.parent = ctx->cur_container;
adamc@760 871 ctx->cur_container = &INP(ctx)[n];
adamc@756 872 return;
adamc@756 873 default:
adamc@756 874 uw_error(ctx, FATAL, "Impossible input kind");
adamc@756 875 }
adamc@756 876 }
adamc@756 877
adamc@756 878 void uw_leave_subform(uw_context ctx) {
adamc@756 879 input *tmp;
adamc@756 880
adamc@759 881 if (ctx->cur_container == NULL)
adamc@756 882 uw_error(ctx, FATAL, "Unmatched uw_leave_subform");
adamc@756 883
adamc@759 884 tmp = ctx->cur_container;
adamc@759 885 ctx->cur_container = tmp->data.subform.parent;
adamc@759 886 tmp->data.subform.parent = NULL;
adamc@759 887 }
adamc@759 888
adamc@759 889 int uw_enter_subforms(uw_context ctx, int n) {
adamc@759 890 input *inps;
adamc@759 891
adamc@759 892 if (n < 0)
adamc@759 893 uw_error(ctx, FATAL, "Negative subforms index %d", n);
adamc@759 894 if (n >= uw_inputs_len)
adamc@759 895 uw_error(ctx, FATAL, "Out-of-bounds subforms index %d", n);
adamc@759 896
adamc@759 897 switch (INP(ctx)[n].kind) {
adamc@759 898 case UNSET:
adamc@759 899 uw_error(ctx, FATAL, "Missing subforms");
adamc@759 900 case FIL:
adamc@759 901 uw_error(ctx, FATAL, "Tried to read a file form input as subforms");
adamc@759 902 case NORMAL:
adamc@759 903 uw_error(ctx, FATAL, "Tried to read a normal form input %p as subforms", &INP(ctx)[n]);
adamc@759 904 case SUBFORM:
adamc@759 905 uw_error(ctx, FATAL, "Tried to read a subform form input as subforms");
adamc@759 906 case ENTRY:
adamc@759 907 uw_error(ctx, FATAL, "Tried to read an entry form input as subforms");
adamc@759 908 case SUBFORMS:
adamc@759 909 inps = INP(ctx)[n].data.subforms.entries;
adamc@759 910 if (inps) {
adamc@759 911 INP(ctx)[n].data.subforms.parent = ctx->cur_container;
adamc@759 912 ctx->cur_container = INP(ctx)[n].data.subforms.entries;
adamc@759 913 return 1;
adamc@759 914 } else
adamc@759 915 return 0;
adamc@759 916 default:
adamc@759 917 uw_error(ctx, FATAL, "Impossible input kind");
adamc@759 918 }
adamc@759 919 }
adamc@759 920
adamc@759 921 int uw_next_entry(uw_context ctx) {
adamc@759 922 if (ctx->cur_container == NULL)
adamc@759 923 uw_error(ctx, FATAL, "uw_next_entry(NULL)");
adamc@759 924
adamc@759 925 switch (ctx->cur_container->kind) {
adamc@759 926 case UNSET:
adamc@759 927 uw_error(ctx, FATAL, "Missing entry");
adamc@759 928 case FIL:
adamc@759 929 uw_error(ctx, FATAL, "Tried to read a file form input as entry");
adamc@759 930 case NORMAL:
adamc@759 931 uw_error(ctx, FATAL, "Tried to read a normal form input as entry");
adamc@759 932 case SUBFORM:
adamc@759 933 uw_error(ctx, FATAL, "Tried to read a subform form input as entry");
adamc@759 934 case SUBFORMS:
adamc@759 935 uw_error(ctx, FATAL, "Tried to read a subforms form input as entry");
adamc@759 936 case ENTRY:
adamc@759 937 if (ctx->cur_container->data.entry.next) {
adamc@759 938 ctx->cur_container = ctx->cur_container->data.entry.next;
adamc@759 939 return 1;
adamc@759 940 } else {
adamc@759 941 ctx->cur_container = ctx->cur_container->data.entry.parent->data.subforms.parent;
adamc@759 942 return 0;
adamc@759 943 }
adamc@759 944 default:
adamc@759 945 uw_error(ctx, FATAL, "Impossible input kind");
adamc@759 946 }
adamc@756 947 }
adamc@756 948
adamc@643 949 void uw_set_script_header(uw_context ctx, const char *s) {
adamc@643 950 ctx->script_header = s;
adamc@643 951 }
adamc@643 952
adamc@667 953 void uw_set_url_prefix(uw_context ctx, const char *s) {
adamc@667 954 ctx->url_prefix = s;
adamc@667 955 }
adamc@667 956
adamc@693 957 void uw_set_needs_push(uw_context ctx, int n) {
adamc@693 958 ctx->needs_push = n;
adamc@693 959 }
adamc@693 960
adamc@736 961 void uw_set_needs_sig(uw_context ctx, int n) {
adamc@736 962 ctx->needs_sig = n;
adamc@736 963 }
adamc@736 964
adamc@667 965
adamc@667 966 static void buf_check_ctx(uw_context ctx, buf *b, size_t extra, const char *desc) {
adamc@666 967 if (b->back - b->front < extra) {
adamc@666 968 size_t desired = b->front - b->start + extra, next;
adamc@136 969 char *new_heap;
adamc@136 970
adamc@666 971 next = b->back - b->start;
adamc@317 972 if (next == 0)
adamc@317 973 next = 1;
adamc@317 974 for (; next < desired; next *= 2);
adamc@136 975
adamc@666 976 new_heap = realloc(b->start, next);
adamc@666 977 b->front = new_heap + (b->front - b->start);
adamc@666 978 b->back = new_heap + next;
adamc@136 979
adamc@667 980 if (new_heap != b->start) {
adamc@666 981 b->start = new_heap;
adamc@666 982 uw_error(ctx, UNLIMITED_RETRY, "Couldn't allocate new %s contiguously", desc);
adamc@136 983 }
adamc@136 984
adamc@666 985 b->start = new_heap;
adamc@136 986 }
adamc@136 987 }
adamc@136 988
adamc@666 989 static void uw_check_heap(uw_context ctx, size_t extra) {
adamc@667 990 buf_check_ctx(ctx, &ctx->heap, extra, "heap chunk");
adamc@666 991 }
adamc@666 992
adamc@311 993 void *uw_malloc(uw_context ctx, size_t len) {
adamc@136 994 void *result;
adamc@136 995
adamc@311 996 uw_check_heap(ctx, len);
adamc@136 997
adamc@666 998 result = ctx->heap.front;
adamc@666 999 ctx->heap.front += len;
adamc@136 1000 return result;
adamc@117 1001 }
adamc@117 1002
adamc@323 1003 void uw_begin_region(uw_context ctx) {
adamc@666 1004 regions *r = (regions *) ctx->heap.front;
adamc@323 1005
adamc@323 1006 uw_check_heap(ctx, sizeof(regions));
adamc@323 1007
adamc@666 1008 ctx->heap.front += sizeof(regions);
adamc@323 1009
adamc@323 1010 r->next = ctx->regions;
adamc@323 1011 ctx->regions = r;
adamc@323 1012 }
adamc@323 1013
adamc@323 1014 void uw_end_region(uw_context ctx) {
adamc@323 1015 regions *r = ctx->regions;
adamc@323 1016
adamc@323 1017 if (r == NULL)
adamc@323 1018 uw_error(ctx, FATAL, "Region stack underflow");
adamc@323 1019
adamc@666 1020 ctx->heap.front = (char *) r;
adamc@323 1021 ctx->regions = r->next;
adamc@323 1022 }
adamc@323 1023
adamc@324 1024 void uw_memstats(uw_context ctx) {
adamc@666 1025 printf("Headers: %d/%d\n", buf_used(&ctx->outHeaders), buf_avail(&ctx->outHeaders));
adamc@666 1026 printf("Script: %d/%d\n", buf_used(&ctx->script), buf_avail(&ctx->script));
adamc@666 1027 printf("Page: %d/%d\n", buf_used(&ctx->page), buf_avail(&ctx->page));
adamc@666 1028 printf("Heap: %d/%d\n", buf_used(&ctx->heap), buf_avail(&ctx->heap));
adamc@324 1029 }
adamc@324 1030
adamc@311 1031 int uw_send(uw_context ctx, int sock) {
adamc@666 1032 int n = uw_really_send(sock, ctx->outHeaders.start, ctx->outHeaders.front - ctx->outHeaders.start);
adamc@462 1033
adamc@462 1034 if (n < 0)
adamc@462 1035 return n;
adamc@462 1036
adamc@462 1037 n = uw_really_send(sock, "\r\n", 2);
adamc@462 1038
adamc@462 1039 if (n < 0)
adamc@462 1040 return n;
adamc@462 1041
adamc@666 1042 return uw_really_send(sock, ctx->page.start, ctx->page.front - ctx->page.start);
adamc@462 1043 }
adamc@462 1044
adamc@462 1045 static void uw_check_headers(uw_context ctx, size_t extra) {
adamc@667 1046 buf_check(&ctx->outHeaders, extra);
adamc@462 1047 }
adamc@462 1048
adamc@462 1049 void uw_write_header(uw_context ctx, uw_Basis_string s) {
adamc@462 1050 int len = strlen(s);
adamc@462 1051
adamc@462 1052 uw_check_headers(ctx, len + 1);
adamc@666 1053 strcpy(ctx->outHeaders.front, s);
adamc@666 1054 ctx->outHeaders.front += len;
adamc@562 1055 }
adamc@562 1056
adamc@562 1057 static void uw_check_script(uw_context ctx, size_t extra) {
adamc@667 1058 buf_check(&ctx->script, extra);
adamc@562 1059 }
adamc@562 1060
adamc@562 1061 void uw_write_script(uw_context ctx, uw_Basis_string s) {
adamc@562 1062 int len = strlen(s);
adamc@562 1063
adamc@562 1064 uw_check_script(ctx, len + 1);
adamc@666 1065 strcpy(ctx->script.front, s);
adamc@666 1066 ctx->script.front += len;
adamc@562 1067 }
adamc@562 1068
adamc@645 1069 const char *uw_Basis_get_script(uw_context ctx, uw_unit u) {
adamc@667 1070 if (ctx->script_header[0] == 0)
adamc@667 1071 return "";
adamc@696 1072 else if (buf_used(&ctx->script) == 0)
adamc@696 1073 return ctx->script_header;
adamc@667 1074 else {
adamc@695 1075 char *r = uw_malloc(ctx, strlen(ctx->script_header) + 42 + buf_used(&ctx->script));
adamc@695 1076 sprintf(r, "%s<script type=\"text/javascript\">%s</script>",
adamc@673 1077 ctx->script_header,
adamc@673 1078 ctx->script.start);
adamc@565 1079 return r;
adamc@565 1080 }
adamc@565 1081 }
adamc@565 1082
adamc@695 1083 uw_Basis_string uw_Basis_maybe_onload(uw_context ctx, uw_Basis_string s) {
adamc@695 1084 if (s[0] == 0)
adamc@695 1085 return "";
adamc@695 1086 else {
adamc@695 1087 char *r = uw_malloc(ctx, 11 + strlen(s));
adamc@695 1088 sprintf(r, " onload='%s'", s);
adamc@695 1089 return r;
adamc@695 1090 }
adamc@695 1091 }
adamc@695 1092
adamc@736 1093 extern uw_Basis_string uw_cookie_sig(uw_context);
adamc@736 1094
adamc@694 1095 const char *uw_Basis_get_settings(uw_context ctx, uw_unit u) {
adamc@736 1096 if (ctx->client == NULL) {
adamc@736 1097 if (ctx->needs_sig) {
adamc@736 1098 char *sig = uw_cookie_sig(ctx);
adamc@736 1099 char *r = uw_malloc(ctx, strlen(sig) + 8);
adamc@736 1100 sprintf(r, "sig=\"%s\";", sig);
adamc@736 1101 return r;
adamc@736 1102 }
adamc@736 1103 else
adamc@736 1104 return "";
adamc@736 1105 } else {
adamc@736 1106 char *sig = ctx->needs_sig ? uw_cookie_sig(ctx) : "";
adamc@736 1107 char *r = uw_malloc(ctx, 59 + 3 * INTS_MAX + strlen(ctx->url_prefix)
adamc@736 1108 + (ctx->needs_sig ? strlen(sig) + 7 : 0));
adamc@736 1109 sprintf(r, "client_id=%u;client_pass=%d;url_prefix=\"%s\";timeout=%d;%s%s%slistener();",
adamc@682 1110 ctx->client->id,
adamc@682 1111 ctx->client->pass,
adamc@679 1112 ctx->url_prefix,
adamc@736 1113 ctx->timeout,
adamc@736 1114 ctx->needs_sig ? "sig=\"" : "",
adamc@736 1115 sig,
adamc@736 1116 ctx->needs_sig ? "\";" : "");
adamc@679 1117 return r;
adamc@668 1118 }
adamc@667 1119 }
adamc@667 1120
adamc@574 1121 uw_Basis_string uw_Basis_jsifyString(uw_context ctx, uw_Basis_string s) {
adamc@574 1122 char *r, *s2;
adamc@574 1123
adamc@574 1124 uw_check_heap(ctx, strlen(s) * 4 + 2);
adamc@574 1125
adamc@666 1126 r = s2 = ctx->heap.front;
adamc@574 1127 *s2++ = '"';
adamc@574 1128
adamc@574 1129 for (; *s; s++) {
adamc@574 1130 char c = *s;
adamc@574 1131
adamc@574 1132 switch (c) {
adamc@574 1133 case '"':
adamc@574 1134 strcpy(s2, "\\\"");
adamc@574 1135 s2 += 2;
adamc@574 1136 break;
adamc@574 1137 case '\\':
adamc@574 1138 strcpy(s2, "\\\\");
adamc@574 1139 s2 += 2;
adamc@574 1140 break;
adamc@574 1141 default:
adamc@574 1142 if (isprint(c))
adamc@574 1143 *s2++ = c;
adamc@574 1144 else {
adamc@574 1145 sprintf(s2, "\\%3o", c);
adamc@574 1146 s2 += 4;
adamc@574 1147 }
adamc@574 1148 }
adamc@574 1149 }
adamc@574 1150
adamc@574 1151 strcpy(s2, "\"");
adamc@666 1152 ctx->heap.front = s2 + 1;
adamc@574 1153 return r;
adamc@574 1154 }
adamc@574 1155
adamc@574 1156 uw_Basis_string uw_Basis_jsifyString_ws(uw_context ctx, uw_Basis_string s) {
adamc@574 1157 char *r, *s2;
adamc@574 1158
adamc@574 1159 uw_check_script(ctx, strlen(s) * 4 + 2);
adamc@574 1160
adamc@666 1161 r = s2 = ctx->script.front;
adamc@574 1162 *s2++ = '"';
adamc@574 1163
adamc@574 1164 for (; *s; s++) {
adamc@574 1165 char c = *s;
adamc@574 1166
adamc@574 1167 switch (c) {
adamc@577 1168 case '\'':
adamc@574 1169 strcpy(s2, "\\\"");
adamc@574 1170 s2 += 2;
adamc@574 1171 break;
adamc@574 1172 case '\\':
adamc@574 1173 strcpy(s2, "\\\\");
adamc@574 1174 s2 += 2;
adamc@574 1175 break;
adamc@574 1176 default:
adamc@574 1177 if (isprint(c))
adamc@574 1178 *s2++ = c;
adamc@574 1179 else {
adamc@574 1180 sprintf(s2, "\\%3o", c);
adamc@574 1181 s2 += 4;
adamc@574 1182 }
adamc@574 1183 }
adamc@574 1184 }
adamc@574 1185
adamc@574 1186 strcpy(s2, "\"");
adamc@666 1187 ctx->script.front = s2 + 1;
adamc@574 1188 return r;
adamc@574 1189 }
adamc@574 1190
adamc@682 1191 char *uw_Basis_jsifyChannel(uw_context ctx, uw_Basis_channel chn) {
adamc@682 1192 if (ctx->client == NULL || chn.cli != ctx->client->id)
adamc@682 1193 return "null";
adamc@682 1194 else {
adamc@682 1195 int len;
adamc@682 1196 char *r;
adamc@682 1197
adamc@682 1198 uw_check_heap(ctx, INTS_MAX + 1);
adamc@682 1199 r = ctx->heap.front;
adamc@682 1200 sprintf(r, "%u%n", chn.chn, &len);
adamc@682 1201 ctx->heap.front += len+1;
adamc@682 1202 return r;
adamc@682 1203 }
adamc@682 1204 }
adamc@682 1205
adamc@577 1206 uw_Basis_int uw_Basis_new_client_source(uw_context ctx, uw_Basis_string s) {
adamc@577 1207 int len;
adamc@577 1208 size_t s_len = strlen(s);
adamc@562 1209
adamc@577 1210 uw_check_script(ctx, 12 + INTS_MAX + s_len);
adamc@666 1211 sprintf(ctx->script.front, "var s%d=sc(%n", ctx->source_count, &len);
adamc@666 1212 ctx->script.front += len;
adamc@666 1213 strcpy(ctx->script.front, s);
adamc@666 1214 ctx->script.front += s_len;
adamc@666 1215 strcpy(ctx->script.front, ");");
adamc@666 1216 ctx->script.front += 2;
adamc@562 1217
adamc@565 1218 return ctx->source_count++;
adamc@117 1219 }
adamc@117 1220
adamc@577 1221 uw_unit uw_Basis_set_client_source(uw_context ctx, uw_Basis_int n, uw_Basis_string s) {
adamc@577 1222 int len;
adamc@577 1223 size_t s_len = strlen(s);
adamc@577 1224
adamc@577 1225 uw_check_script(ctx, 6 + INTS_MAX + s_len);
adamc@666 1226 sprintf(ctx->script.front, "s%d.v=%n", (int)n, &len);
adamc@666 1227 ctx->script.front += len;
adamc@666 1228 strcpy(ctx->script.front, s);
adamc@666 1229 ctx->script.front += s_len;
adamc@666 1230 strcpy(ctx->script.front, ";");
adamc@666 1231 ctx->script.front++;
adamc@577 1232
adamc@577 1233 return uw_unit_v;
adamc@577 1234 }
adamc@577 1235
adamc@311 1236 static void uw_check(uw_context ctx, size_t extra) {
adamc@667 1237 buf_check(&ctx->page, extra);
adamc@117 1238 }
adamc@117 1239
adamc@311 1240 static void uw_writec_unsafe(uw_context ctx, char c) {
adamc@666 1241 *(ctx->page.front)++ = c;
adamc@117 1242 }
adamc@117 1243
adamc@311 1244 void uw_writec(uw_context ctx, char c) {
adamc@311 1245 uw_check(ctx, 1);
adamc@311 1246 uw_writec_unsafe(ctx, c);
adamc@117 1247 }
adamc@117 1248
adamc@311 1249 static void uw_write_unsafe(uw_context ctx, const char* s) {
adamc@117 1250 int len = strlen(s);
adamc@666 1251 memcpy(ctx->page.front, s, len);
adamc@666 1252 ctx->page.front += len;
adamc@117 1253 }
adamc@117 1254
adamc@311 1255 void uw_write(uw_context ctx, const char* s) {
adamc@311 1256 uw_check(ctx, strlen(s) + 1);
adamc@311 1257 uw_write_unsafe(ctx, s);
adamc@666 1258 *ctx->page.front = 0;
adamc@102 1259 }
adamc@106 1260
adamc@311 1261 char *uw_Basis_attrifyInt(uw_context ctx, uw_Basis_int n) {
adamc@136 1262 char *result;
adamc@136 1263 int len;
adamc@311 1264 uw_check_heap(ctx, INTS_MAX);
adamc@666 1265 result = ctx->heap.front;
adamc@276 1266 sprintf(result, "%lld%n", n, &len);
adamc@666 1267 ctx->heap.front += len+1;
adamc@136 1268 return result;
adamc@106 1269 }
adamc@106 1270
adamc@311 1271 char *uw_Basis_attrifyFloat(uw_context ctx, uw_Basis_float n) {
adamc@136 1272 char *result;
adamc@136 1273 int len;
adamc@311 1274 uw_check_heap(ctx, FLOATS_MAX);
adamc@666 1275 result = ctx->heap.front;
adamc@136 1276 sprintf(result, "%g%n", n, &len);
adamc@666 1277 ctx->heap.front += len+1;
adamc@136 1278 return result;
adamc@106 1279 }
adamc@106 1280
adamc@311 1281 char *uw_Basis_attrifyString(uw_context ctx, uw_Basis_string s) {
adamc@136 1282 int len = strlen(s);
adamc@136 1283 char *result, *p;
adamc@311 1284 uw_check_heap(ctx, len * 6 + 1);
adamc@136 1285
adamc@666 1286 result = p = ctx->heap.front;
adamc@136 1287
adamc@136 1288 for (; *s; s++) {
adamc@136 1289 char c = *s;
adamc@136 1290
adamc@136 1291 if (c == '"') {
adamc@136 1292 strcpy(p, "&quot;");
adamc@136 1293 p += 6;
adamc@136 1294 } else if (c == '&') {
adamc@136 1295 strcpy(p, "&amp;");
adamc@136 1296 p += 5;
adamc@136 1297 }
adamc@136 1298 else if (isprint(c))
adamc@136 1299 *p++ = c;
adamc@136 1300 else {
adamc@136 1301 int len2;
adamc@136 1302 sprintf(p, "&#%d;%n", c, &len2);
adamc@136 1303 p += len2;
adamc@136 1304 }
adamc@136 1305 }
adamc@136 1306
adamc@137 1307 *p++ = 0;
adamc@666 1308 ctx->heap.front = p;
adamc@136 1309 return result;
adamc@106 1310 }
adamc@106 1311
adamc@721 1312 char *uw_Basis_attrifyCss_class(uw_context ctx, uw_Basis_css_class s) {
adamc@721 1313 return s;
adamc@721 1314 }
adamc@721 1315
adamc@311 1316 static void uw_Basis_attrifyInt_w_unsafe(uw_context ctx, uw_Basis_int n) {
adamc@117 1317 int len;
adamc@117 1318
adamc@666 1319 sprintf(ctx->page.front, "%lld%n", n, &len);
adamc@666 1320 ctx->page.front += len;
adamc@106 1321 }
adamc@106 1322
adamc@428 1323 uw_unit uw_Basis_attrifyInt_w(uw_context ctx, uw_Basis_int n) {
adamc@311 1324 uw_check(ctx, INTS_MAX);
adamc@311 1325 uw_Basis_attrifyInt_w_unsafe(ctx, n);
adamc@428 1326
adamc@428 1327 return uw_unit_v;
adamc@106 1328 }
adamc@106 1329
adamc@428 1330 uw_unit uw_Basis_attrifyFloat_w(uw_context ctx, uw_Basis_float n) {
adamc@117 1331 int len;
adamc@117 1332
adamc@311 1333 uw_check(ctx, FLOATS_MAX);
adamc@666 1334 sprintf(ctx->page.front, "%g%n", n, &len);
adamc@666 1335 ctx->page.front += len;
adamc@428 1336
adamc@428 1337 return uw_unit_v;
adamc@117 1338 }
adamc@117 1339
adamc@428 1340 uw_unit uw_Basis_attrifyString_w(uw_context ctx, uw_Basis_string s) {
adamc@311 1341 uw_check(ctx, strlen(s) * 6);
adamc@117 1342
adamc@106 1343 for (; *s; s++) {
adamc@106 1344 char c = *s;
adamc@106 1345
adamc@106 1346 if (c == '"')
adamc@311 1347 uw_write_unsafe(ctx, "&quot;");
adamc@136 1348 else if (c == '&')
adamc@311 1349 uw_write_unsafe(ctx, "&amp;");
adamc@106 1350 else if (isprint(c))
adamc@311 1351 uw_writec_unsafe(ctx, c);
adamc@106 1352 else {
adamc@311 1353 uw_write_unsafe(ctx, "&#");
adamc@311 1354 uw_Basis_attrifyInt_w_unsafe(ctx, c);
adamc@311 1355 uw_writec_unsafe(ctx, ';');
adamc@106 1356 }
adamc@106 1357 }
adamc@428 1358
adamc@428 1359 return uw_unit_v;
adamc@106 1360 }
adamc@120 1361
adamc@120 1362
adamc@311 1363 char *uw_Basis_urlifyInt(uw_context ctx, uw_Basis_int n) {
adamc@137 1364 int len;
adamc@137 1365 char *r;
adamc@137 1366
adamc@311 1367 uw_check_heap(ctx, INTS_MAX);
adamc@666 1368 r = ctx->heap.front;
adamc@276 1369 sprintf(r, "%lld%n", n, &len);
adamc@666 1370 ctx->heap.front += len+1;
adamc@137 1371 return r;
adamc@120 1372 }
adamc@120 1373
adamc@682 1374 char *uw_Basis_urlifyChannel(uw_context ctx, uw_Basis_channel chn) {
adamc@682 1375 if (ctx->client == NULL || chn.cli != ctx->client->id)
adamc@682 1376 return "";
adamc@682 1377 else {
adamc@682 1378 int len;
adamc@682 1379 char *r;
adamc@679 1380
adamc@682 1381 uw_check_heap(ctx, INTS_MAX + 1);
adamc@682 1382 r = ctx->heap.front;
adamc@682 1383 sprintf(r, "%u%n", chn.chn, &len);
adamc@682 1384 ctx->heap.front += len+1;
adamc@682 1385 return r;
adamc@682 1386 }
adamc@679 1387 }
adamc@679 1388
adamc@311 1389 char *uw_Basis_urlifyFloat(uw_context ctx, uw_Basis_float n) {
adamc@137 1390 int len;
adamc@137 1391 char *r;
adamc@137 1392
adamc@311 1393 uw_check_heap(ctx, FLOATS_MAX);
adamc@666 1394 r = ctx->heap.front;
adamc@137 1395 sprintf(r, "%g%n", n, &len);
adamc@666 1396 ctx->heap.front += len+1;
adamc@137 1397 return r;
adamc@120 1398 }
adamc@120 1399
adamc@311 1400 char *uw_Basis_urlifyString(uw_context ctx, uw_Basis_string s) {
adamc@137 1401 char *r, *p;
adamc@137 1402
adamc@311 1403 uw_check_heap(ctx, strlen(s) * 3 + 1);
adamc@137 1404
adamc@666 1405 for (r = p = ctx->heap.front; *s; s++) {
adamc@137 1406 char c = *s;
adamc@137 1407
adamc@137 1408 if (c == ' ')
adamc@137 1409 *p++ = '+';
adamc@137 1410 else if (isalnum(c))
adamc@137 1411 *p++ = c;
adamc@137 1412 else {
adamc@137 1413 sprintf(p, "%%%02X", c);
adamc@137 1414 p += 3;
adamc@137 1415 }
adamc@137 1416 }
adamc@137 1417
adamc@137 1418 *p++ = 0;
adamc@666 1419 ctx->heap.front = p;
adamc@137 1420 return r;
adamc@120 1421 }
adamc@120 1422
adamc@311 1423 char *uw_Basis_urlifyBool(uw_context ctx, uw_Basis_bool b) {
adamc@311 1424 if (b == uw_Basis_False)
adamc@186 1425 return "0";
adamc@186 1426 else
adamc@186 1427 return "1";
adamc@186 1428 }
adamc@186 1429
adamc@311 1430 static void uw_Basis_urlifyInt_w_unsafe(uw_context ctx, uw_Basis_int n) {
adamc@120 1431 int len;
adamc@120 1432
adamc@666 1433 sprintf(ctx->page.front, "%lld%n", n, &len);
adamc@666 1434 ctx->page.front += len;
adamc@120 1435 }
adamc@120 1436
adamc@428 1437 uw_unit uw_Basis_urlifyInt_w(uw_context ctx, uw_Basis_int n) {
adamc@311 1438 uw_check(ctx, INTS_MAX);
adamc@311 1439 uw_Basis_urlifyInt_w_unsafe(ctx, n);
adamc@428 1440
adamc@428 1441 return uw_unit_v;
adamc@120 1442 }
adamc@120 1443
adamc@682 1444 uw_unit uw_Basis_urlifyChannel_w(uw_context ctx, uw_Basis_channel chn) {
adamc@682 1445 if (ctx->client != NULL && chn.cli == ctx->client->id) {
adamc@682 1446 int len;
adamc@682 1447
adamc@682 1448 uw_check(ctx, INTS_MAX + 1);
adamc@682 1449 sprintf(ctx->page.front, "%u%n", chn.chn, &len);
adamc@682 1450 ctx->page.front += len;
adamc@682 1451 }
adamc@682 1452
adamc@679 1453 return uw_unit_v;
adamc@679 1454 }
adamc@679 1455
adamc@428 1456 uw_unit uw_Basis_urlifyFloat_w(uw_context ctx, uw_Basis_float n) {
adamc@120 1457 int len;
adamc@120 1458
adamc@311 1459 uw_check(ctx, FLOATS_MAX);
adamc@666 1460 sprintf(ctx->page.front, "%g%n", n, &len);
adamc@666 1461 ctx->page.front += len;
adamc@428 1462
adamc@428 1463 return uw_unit_v;
adamc@120 1464 }
adamc@120 1465
adamc@488 1466 uw_Basis_string uw_Basis_urlifyTime(uw_context ctx, uw_Basis_time t) {
adamc@488 1467 return uw_Basis_urlifyInt(ctx, t);
adamc@488 1468 }
adamc@488 1469
adamc@428 1470 uw_unit uw_Basis_urlifyString_w(uw_context ctx, uw_Basis_string s) {
adamc@311 1471 uw_check(ctx, strlen(s) * 3);
adamc@120 1472
adamc@120 1473 for (; *s; s++) {
adamc@120 1474 char c = *s;
adamc@120 1475
adamc@120 1476 if (c == ' ')
adamc@311 1477 uw_writec_unsafe(ctx, '+');
adamc@120 1478 else if (isalnum(c))
adamc@311 1479 uw_writec_unsafe(ctx, c);
adamc@120 1480 else {
adamc@666 1481 sprintf(ctx->page.front, "%%%02X", c);
adamc@666 1482 ctx->page.front += 3;
adamc@120 1483 }
adamc@120 1484 }
adamc@428 1485
adamc@428 1486 return uw_unit_v;
adamc@120 1487 }
adamc@120 1488
adamc@428 1489 uw_unit uw_Basis_urlifyBool_w(uw_context ctx, uw_Basis_bool b) {
adamc@311 1490 if (b == uw_Basis_False)
adamc@311 1491 uw_writec(ctx, '0');
adamc@186 1492 else
adamc@311 1493 uw_writec(ctx, '1');
adamc@428 1494
adamc@428 1495 return uw_unit_v;
adamc@186 1496 }
adamc@186 1497
adamc@120 1498
adamc@311 1499 static char *uw_unurlify_advance(char *s) {
adamc@144 1500 char *new_s = strchr(s, '/');
adamc@120 1501
adamc@120 1502 if (new_s)
adamc@120 1503 *new_s++ = 0;
adamc@120 1504 else
adamc@144 1505 new_s = strchr(s, 0);
adamc@144 1506
adamc@144 1507 return new_s;
adamc@144 1508 }
adamc@144 1509
adamc@311 1510 uw_Basis_int uw_Basis_unurlifyInt(uw_context ctx, char **s) {
adamc@311 1511 char *new_s = uw_unurlify_advance(*s);
adamc@311 1512 uw_Basis_int r;
adamc@120 1513
adamc@276 1514 r = atoll(*s);
adamc@120 1515 *s = new_s;
adamc@120 1516 return r;
adamc@120 1517 }
adamc@120 1518
adamc@311 1519 uw_Basis_float uw_Basis_unurlifyFloat(uw_context ctx, char **s) {
adamc@311 1520 char *new_s = uw_unurlify_advance(*s);
adamc@311 1521 uw_Basis_float r;
adamc@120 1522
adamc@120 1523 r = atof(*s);
adamc@120 1524 *s = new_s;
adamc@120 1525 return r;
adamc@120 1526 }
adamc@120 1527
adamc@488 1528 uw_Basis_time uw_Basis_unurlifyTime(uw_context ctx, char **s) {
adamc@488 1529 return uw_Basis_unurlifyInt(ctx, s);
adamc@488 1530 }
adamc@488 1531
adamc@311 1532 static uw_Basis_string uw_unurlifyString_to(uw_context ctx, char *r, char *s) {
adamc@144 1533 char *s1, *s2;
adamc@144 1534 int n;
adamc@136 1535
adamc@144 1536 for (s1 = r, s2 = s; *s2; ++s1, ++s2) {
adamc@136 1537 char c = *s2;
adamc@136 1538
adamc@136 1539 switch (c) {
adamc@136 1540 case '+':
adamc@136 1541 *s1 = ' ';
adamc@136 1542 break;
adamc@136 1543 case '%':
adamc@169 1544 if (s2[1] == 0)
adamc@311 1545 uw_error(ctx, FATAL, "Missing first character of escaped URL byte");
adamc@169 1546 if (s2[2] == 0)
adamc@311 1547 uw_error(ctx, FATAL, "Missing second character of escaped URL byte");
adamc@169 1548 if (sscanf(s2+1, "%02X", &n) != 1)
adamc@311 1549 uw_error(ctx, FATAL, "Invalid escaped URL byte starting at: %s", s2);
adamc@136 1550 *s1 = n;
adamc@136 1551 s2 += 2;
adamc@136 1552 break;
adamc@136 1553 default:
adamc@136 1554 *s1 = c;
adamc@136 1555 }
adamc@136 1556 }
adamc@136 1557 *s1++ = 0;
adamc@144 1558 return s1;
adamc@144 1559 }
adamc@144 1560
adamc@311 1561 uw_Basis_bool uw_Basis_unurlifyBool(uw_context ctx, char **s) {
adamc@311 1562 char *new_s = uw_unurlify_advance(*s);
adamc@311 1563 uw_Basis_bool r;
adamc@186 1564
adamc@186 1565 if (*s[0] == 0 || !strcmp(*s, "0") || !strcmp(*s, "off"))
adamc@311 1566 r = uw_Basis_False;
adamc@186 1567 else
adamc@311 1568 r = uw_Basis_True;
adamc@186 1569
adamc@186 1570 *s = new_s;
adamc@186 1571 return r;
adamc@186 1572 }
adamc@186 1573
adamc@311 1574 uw_Basis_string uw_Basis_unurlifyString(uw_context ctx, char **s) {
adamc@311 1575 char *new_s = uw_unurlify_advance(*s);
adamc@144 1576 char *r, *s1, *s2;
adamc@144 1577 int len, n;
adamc@144 1578
adamc@200 1579 len = strlen(*s);
adamc@311 1580 uw_check_heap(ctx, len + 1);
adamc@144 1581
adamc@666 1582 r = ctx->heap.front;
adamc@666 1583 ctx->heap.front = uw_unurlifyString_to(ctx, ctx->heap.front, *s);
adamc@136 1584 *s = new_s;
adamc@136 1585 return r;
adamc@120 1586 }
adamc@135 1587
adamc@135 1588
adamc@311 1589 char *uw_Basis_htmlifyInt(uw_context ctx, uw_Basis_int n) {
adamc@286 1590 int len;
adamc@286 1591 char *r;
adamc@286 1592
adamc@311 1593 uw_check_heap(ctx, INTS_MAX);
adamc@666 1594 r = ctx->heap.front;
adamc@286 1595 sprintf(r, "%lld%n", n, &len);
adamc@666 1596 ctx->heap.front += len+1;
adamc@286 1597 return r;
adamc@286 1598 }
adamc@286 1599
adamc@428 1600 uw_unit uw_Basis_htmlifyInt_w(uw_context ctx, uw_Basis_int n) {
adamc@286 1601 int len;
adamc@286 1602
adamc@311 1603 uw_check(ctx, INTS_MAX);
adamc@666 1604 sprintf(ctx->page.front, "%lld%n", n, &len);
adamc@666 1605 ctx->page.front += len;
adamc@428 1606
adamc@428 1607 return uw_unit_v;
adamc@286 1608 }
adamc@286 1609
adamc@311 1610 char *uw_Basis_htmlifyFloat(uw_context ctx, uw_Basis_float n) {
adamc@286 1611 int len;
adamc@286 1612 char *r;
adamc@286 1613
adamc@311 1614 uw_check_heap(ctx, FLOATS_MAX);
adamc@666 1615 r = ctx->heap.front;
adamc@286 1616 sprintf(r, "%g%n", n, &len);
adamc@666 1617 ctx->heap.front += len+1;
adamc@286 1618 return r;
adamc@286 1619 }
adamc@286 1620
adamc@428 1621 uw_unit uw_Basis_htmlifyFloat_w(uw_context ctx, uw_Basis_float n) {
adamc@286 1622 int len;
adamc@286 1623
adamc@311 1624 uw_check(ctx, FLOATS_MAX);
adamc@666 1625 sprintf(ctx->page.front, "%g%n", n, &len);
adamc@666 1626 ctx->page.front += len;
adamc@428 1627
adamc@428 1628 return uw_unit_v;
adamc@286 1629 }
adamc@286 1630
adamc@311 1631 char *uw_Basis_htmlifyString(uw_context ctx, uw_Basis_string s) {
adamc@137 1632 char *r, *s2;
adamc@137 1633
adamc@311 1634 uw_check_heap(ctx, strlen(s) * 5 + 1);
adamc@137 1635
adamc@666 1636 for (r = s2 = ctx->heap.front; *s; s++) {
adamc@137 1637 char c = *s;
adamc@137 1638
adamc@137 1639 switch (c) {
adamc@137 1640 case '<':
adamc@137 1641 strcpy(s2, "&lt;");
adamc@137 1642 s2 += 4;
adamc@137 1643 break;
adamc@137 1644 case '&':
adamc@137 1645 strcpy(s2, "&amp;");
adamc@137 1646 s2 += 5;
adamc@137 1647 break;
adamc@137 1648 default:
adamc@137 1649 if (isprint(c))
adamc@137 1650 *s2++ = c;
adamc@137 1651 else {
adamc@137 1652 int len2;
adamc@137 1653 sprintf(s2, "&#%d;%n", c, &len2);
adamc@137 1654 s2 += len2;
adamc@137 1655 }
adamc@137 1656 }
adamc@137 1657 }
adamc@137 1658
adamc@137 1659 *s2++ = 0;
adamc@666 1660 ctx->heap.front = s2;
adamc@137 1661 return r;
adamc@135 1662 }
adamc@135 1663
adamc@428 1664 uw_unit uw_Basis_htmlifyString_w(uw_context ctx, uw_Basis_string s) {
adamc@321 1665 uw_check(ctx, strlen(s) * 6);
adamc@135 1666
adamc@135 1667 for (; *s; s++) {
adamc@135 1668 char c = *s;
adamc@135 1669
adamc@135 1670 switch (c) {
adamc@135 1671 case '<':
adamc@311 1672 uw_write_unsafe(ctx, "&lt;");
adamc@135 1673 break;
adamc@135 1674 case '&':
adamc@311 1675 uw_write_unsafe(ctx, "&amp;");
adamc@135 1676 break;
adamc@135 1677 default:
adamc@135 1678 if (isprint(c))
adamc@311 1679 uw_writec_unsafe(ctx, c);
adamc@135 1680 else {
adamc@311 1681 uw_write_unsafe(ctx, "&#");
adamc@311 1682 uw_Basis_attrifyInt_w_unsafe(ctx, c);
adamc@311 1683 uw_writec_unsafe(ctx, ';');
adamc@135 1684 }
adamc@135 1685 }
adamc@135 1686 }
adamc@428 1687
adamc@428 1688 return uw_unit_v;
adamc@135 1689 }
adamc@180 1690
adamc@311 1691 uw_Basis_string uw_Basis_htmlifyBool(uw_context ctx, uw_Basis_bool b) {
adamc@311 1692 if (b == uw_Basis_False)
adamc@286 1693 return "False";
adamc@286 1694 else
adamc@286 1695 return "True";
adamc@286 1696 }
adamc@286 1697
adamc@428 1698 uw_unit uw_Basis_htmlifyBool_w(uw_context ctx, uw_Basis_bool b) {
adamc@311 1699 if (b == uw_Basis_False) {
adamc@311 1700 uw_check(ctx, 6);
adamc@666 1701 strcpy(ctx->page.front, "False");
adamc@666 1702 ctx->page.front += 5;
adamc@286 1703 } else {
adamc@311 1704 uw_check(ctx, 5);
adamc@666 1705 strcpy(ctx->page.front, "True");
adamc@666 1706 ctx->page.front += 4;
adamc@286 1707 }
adamc@428 1708
adamc@428 1709 return uw_unit_v;
adamc@286 1710 }
adamc@286 1711
adamc@436 1712 #define TIME_FMT "%x %X"
adamc@438 1713 #define TIME_FMT_PG "%Y-%m-%d %T"
adamc@436 1714
adamc@436 1715 uw_Basis_string uw_Basis_htmlifyTime(uw_context ctx, uw_Basis_time t) {
adamc@436 1716 size_t len;
adamc@436 1717 char *r;
adamc@436 1718 struct tm stm;
adamc@436 1719
adamc@436 1720 if (localtime_r(&t, &stm)) {
adamc@436 1721 uw_check_heap(ctx, TIMES_MAX);
adamc@666 1722 r = ctx->heap.front;
adamc@436 1723 len = strftime(r, TIMES_MAX, TIME_FMT, &stm);
adamc@666 1724 ctx->heap.front += len+1;
adamc@436 1725 return r;
adamc@436 1726 } else
adamc@436 1727 return "<i>Invalid time</i>";
adamc@436 1728 }
adamc@436 1729
adamc@436 1730 uw_unit uw_Basis_htmlifyTime_w(uw_context ctx, uw_Basis_time t) {
adamc@436 1731 size_t len;
adamc@436 1732 char *r;
adamc@436 1733 struct tm stm;
adamc@436 1734
adamc@436 1735 if (localtime_r(&t, &stm)) {
adamc@436 1736 uw_check(ctx, TIMES_MAX);
adamc@666 1737 r = ctx->page.front;
adamc@436 1738 len = strftime(r, TIMES_MAX, TIME_FMT, &stm);
adamc@666 1739 ctx->page.front += len;
adamc@436 1740 } else {
adamc@436 1741 uw_check(ctx, 20);
adamc@666 1742 strcpy(ctx->page.front, "<i>Invalid time</i>");
adamc@666 1743 ctx->page.front += 19;
adamc@436 1744 }
adamc@436 1745
adamc@436 1746 return uw_unit_v;
adamc@436 1747 }
adamc@436 1748
adamc@311 1749 uw_Basis_string uw_Basis_strcat(uw_context ctx, uw_Basis_string s1, uw_Basis_string s2) {
adamc@180 1750 int len = strlen(s1) + strlen(s2) + 1;
adamc@180 1751 char *s;
adamc@180 1752
adamc@311 1753 uw_check_heap(ctx, len);
adamc@180 1754
adamc@666 1755 s = ctx->heap.front;
adamc@180 1756
adamc@180 1757 strcpy(s, s1);
adamc@180 1758 strcat(s, s2);
adamc@666 1759 ctx->heap.front += len;
adamc@180 1760
adamc@180 1761 return s;
adamc@180 1762 }
adamc@278 1763
adamc@737 1764 uw_Basis_string uw_strdup(uw_context ctx, uw_Basis_string s1) {
adamc@278 1765 int len = strlen(s1) + 1;
adamc@278 1766 char *s;
adamc@278 1767
adamc@311 1768 uw_check_heap(ctx, len);
adamc@278 1769
adamc@666 1770 s = ctx->heap.front;
adamc@278 1771
adamc@278 1772 strcpy(s, s1);
adamc@666 1773 ctx->heap.front += len;
adamc@278 1774
adamc@278 1775 return s;
adamc@278 1776 }
adamc@280 1777
adamc@737 1778 uw_Basis_string uw_maybe_strdup(uw_context ctx, uw_Basis_string s1) {
adamc@493 1779 if (s1)
adamc@737 1780 return uw_strdup(ctx, s1);
adamc@493 1781 else
adamc@493 1782 return NULL;
adamc@493 1783 }
adamc@493 1784
adamc@742 1785 char *uw_memdup(uw_context ctx, const char *p, size_t len) {
adamc@742 1786 char *r = uw_malloc(ctx, len);
adamc@742 1787 memcpy(r, p, len);
adamc@742 1788 return r;
adamc@742 1789 }
adamc@280 1790
adamc@311 1791 char *uw_Basis_sqlifyInt(uw_context ctx, uw_Basis_int n) {
adamc@281 1792 int len;
adamc@281 1793 char *r;
adamc@281 1794
adamc@311 1795 uw_check_heap(ctx, INTS_MAX + 6);
adamc@666 1796 r = ctx->heap.front;
adamc@281 1797 sprintf(r, "%lld::int8%n", n, &len);
adamc@666 1798 ctx->heap.front += len+1;
adamc@281 1799 return r;
adamc@281 1800 }
adamc@281 1801
adamc@467 1802 char *uw_Basis_sqlifyIntN(uw_context ctx, uw_Basis_int *n) {
adamc@467 1803 if (n == NULL)
adamc@467 1804 return "NULL";
adamc@467 1805 else
adamc@467 1806 return uw_Basis_sqlifyInt(ctx, *n);
adamc@467 1807 }
adamc@467 1808
adamc@311 1809 char *uw_Basis_sqlifyFloat(uw_context ctx, uw_Basis_float n) {
adamc@281 1810 int len;
adamc@281 1811 char *r;
adamc@281 1812
adamc@311 1813 uw_check_heap(ctx, FLOATS_MAX + 8);
adamc@666 1814 r = ctx->heap.front;
adamc@281 1815 sprintf(r, "%g::float8%n", n, &len);
adamc@666 1816 ctx->heap.front += len+1;
adamc@281 1817 return r;
adamc@281 1818 }
adamc@281 1819
adamc@467 1820 char *uw_Basis_sqlifyFloatN(uw_context ctx, uw_Basis_float *n) {
adamc@467 1821 if (n == NULL)
adamc@467 1822 return "NULL";
adamc@467 1823 else
adamc@467 1824 return uw_Basis_sqlifyFloat(ctx, *n);
adamc@467 1825 }
adamc@467 1826
adamc@281 1827
adamc@311 1828 uw_Basis_string uw_Basis_sqlifyString(uw_context ctx, uw_Basis_string s) {
adamc@280 1829 char *r, *s2;
adamc@280 1830
adamc@311 1831 uw_check_heap(ctx, strlen(s) * 2 + 10);
adamc@280 1832
adamc@666 1833 r = s2 = ctx->heap.front;
adamc@280 1834 *s2++ = 'E';
adamc@280 1835 *s2++ = '\'';
adamc@280 1836
adamc@280 1837 for (; *s; s++) {
adamc@280 1838 char c = *s;
adamc@280 1839
adamc@280 1840 switch (c) {
adamc@280 1841 case '\'':
adamc@280 1842 strcpy(s2, "\\'");
adamc@280 1843 s2 += 2;
adamc@280 1844 break;
adamc@280 1845 case '\\':
adamc@280 1846 strcpy(s2, "\\\\");
adamc@280 1847 s2 += 2;
adamc@280 1848 break;
adamc@280 1849 default:
adamc@280 1850 if (isprint(c))
adamc@280 1851 *s2++ = c;
adamc@280 1852 else {
adamc@737 1853 sprintf(s2, "\\%03o", c);
adamc@280 1854 s2 += 4;
adamc@280 1855 }
adamc@280 1856 }
adamc@280 1857 }
adamc@280 1858
adamc@281 1859 strcpy(s2, "'::text");
adamc@666 1860 ctx->heap.front = s2 + 8;
adamc@280 1861 return r;
adamc@280 1862 }
adamc@281 1863
adamc@737 1864 uw_Basis_string uw_Basis_sqlifyBlob(uw_context ctx, uw_Basis_blob b) {
adamc@737 1865 char *r, *s2;
adamc@737 1866 size_t i;
adamc@737 1867
adamc@737 1868 uw_check_heap(ctx, b.size * 5 + 11);
adamc@737 1869
adamc@737 1870 r = s2 = ctx->heap.front;
adamc@737 1871 *s2++ = 'E';
adamc@737 1872 *s2++ = '\'';
adamc@737 1873
adamc@737 1874 for (i = 0; i < b.size; ++i) {
adamc@737 1875 char c = b.data[i];
adamc@737 1876
adamc@737 1877 switch (c) {
adamc@737 1878 case '\'':
adamc@737 1879 strcpy(s2, "\\'");
adamc@737 1880 s2 += 2;
adamc@737 1881 break;
adamc@737 1882 case '\\':
adamc@737 1883 strcpy(s2, "\\\\\\\\");
adamc@737 1884 s2 += 4;
adamc@737 1885 break;
adamc@737 1886 default:
adamc@737 1887 if (isprint(c))
adamc@737 1888 *s2++ = c;
adamc@737 1889 else {
adamc@737 1890 sprintf(s2, "\\\\%03o", c);
adamc@737 1891 s2 += 5;
adamc@737 1892 }
adamc@737 1893 }
adamc@737 1894 }
adamc@737 1895
adamc@737 1896 strcpy(s2, "'::bytea");
adamc@737 1897 ctx->heap.front = s2 + 9;
adamc@737 1898 return r;
adamc@737 1899 }
adamc@737 1900
adamc@682 1901 char *uw_Basis_sqlifyChannel(uw_context ctx, uw_Basis_channel chn) {
adamc@682 1902 int len;
adamc@682 1903 char *r;
adamc@682 1904 unsigned long long combo = ((unsigned long long)chn.cli << 32) | chn.chn;
adamc@682 1905
adamc@682 1906 uw_check_heap(ctx, INTS_MAX + 7);
adamc@682 1907 r = ctx->heap.front;
adamc@682 1908 sprintf(r, "%lld::int8%n", combo, &len);
adamc@682 1909 ctx->heap.front += len+1;
adamc@682 1910 return r;
adamc@682 1911 }
adamc@682 1912
adamc@682 1913 char *uw_Basis_attrifyChannel(uw_context ctx, uw_Basis_channel chn) {
adamc@682 1914 int len;
adamc@682 1915 char *r;
adamc@682 1916 unsigned long long combo = ((unsigned long long)chn.cli << 32) | chn.chn;
adamc@682 1917
adamc@682 1918 uw_check_heap(ctx, INTS_MAX + 1);
adamc@682 1919 r = ctx->heap.front;
adamc@682 1920 sprintf(r, "%lld%n", combo, &len);
adamc@682 1921 ctx->heap.front += len+1;
adamc@682 1922 return r;
adamc@682 1923 }
adamc@682 1924
adamc@682 1925 char *uw_Basis_sqlifyClient(uw_context ctx, uw_Basis_client cli) {
adamc@682 1926 int len;
adamc@682 1927 char *r;
adamc@682 1928
adamc@682 1929 uw_check_heap(ctx, INTS_MAX + 7);
adamc@682 1930 r = ctx->heap.front;
adamc@682 1931 sprintf(r, "%u::int4%n", cli, &len);
adamc@682 1932 ctx->heap.front += len+1;
adamc@682 1933 return r;
adamc@682 1934 }
adamc@682 1935
adamc@682 1936 char *uw_Basis_attrifyClient(uw_context ctx, uw_Basis_client cli) {
adamc@682 1937 int len;
adamc@682 1938 char *r;
adamc@682 1939
adamc@682 1940 uw_check_heap(ctx, INTS_MAX + 1);
adamc@682 1941 r = ctx->heap.front;
adamc@682 1942 sprintf(r, "%u%n", cli, &len);
adamc@682 1943 ctx->heap.front += len+1;
adamc@682 1944 return r;
adamc@682 1945 }
adamc@682 1946
adamc@467 1947 uw_Basis_string uw_Basis_sqlifyStringN(uw_context ctx, uw_Basis_string s) {
adamc@467 1948 if (s == NULL)
adamc@467 1949 return "NULL";
adamc@467 1950 else
adamc@467 1951 return uw_Basis_sqlifyString(ctx, s);
adamc@467 1952 }
adamc@467 1953
adamc@311 1954 char *uw_Basis_sqlifyBool(uw_context ctx, uw_Basis_bool b) {
adamc@311 1955 if (b == uw_Basis_False)
adamc@281 1956 return "FALSE";
adamc@281 1957 else
adamc@281 1958 return "TRUE";
adamc@281 1959 }
adamc@282 1960
adamc@467 1961 char *uw_Basis_sqlifyBoolN(uw_context ctx, uw_Basis_bool *b) {
adamc@467 1962 if (b == NULL)
adamc@467 1963 return "NULL";
adamc@467 1964 else
adamc@467 1965 return uw_Basis_sqlifyBool(ctx, *b);
adamc@467 1966 }
adamc@467 1967
adamc@439 1968 char *uw_Basis_sqlifyTime(uw_context ctx, uw_Basis_time t) {
adamc@439 1969 size_t len;
adamc@682 1970 char *r, *s;
adamc@682 1971 struct tm stm;
adamc@682 1972
adamc@682 1973 if (localtime_r(&t, &stm)) {
adamc@682 1974 s = uw_malloc(ctx, TIMES_MAX);
adamc@682 1975 len = strftime(s, TIMES_MAX, TIME_FMT, &stm);
adamc@682 1976 r = uw_malloc(ctx, len + 14);
adamc@682 1977 sprintf(r, "'%s'::timestamp", s);
adamc@682 1978 return r;
adamc@682 1979 } else
adamc@682 1980 return "<Invalid time>";
adamc@682 1981 }
adamc@682 1982
adamc@682 1983 char *uw_Basis_attrifyTime(uw_context ctx, uw_Basis_time t) {
adamc@682 1984 size_t len;
adamc@439 1985 char *r;
adamc@439 1986 struct tm stm;
adamc@439 1987
adamc@439 1988 if (localtime_r(&t, &stm)) {
adamc@439 1989 uw_check_heap(ctx, TIMES_MAX);
adamc@666 1990 r = ctx->heap.front;
adamc@439 1991 len = strftime(r, TIMES_MAX, TIME_FMT, &stm);
adamc@666 1992 ctx->heap.front += len+1;
adamc@439 1993 return r;
adamc@439 1994 } else
adamc@439 1995 return "<Invalid time>";
adamc@439 1996 }
adamc@439 1997
adamc@467 1998 char *uw_Basis_sqlifyTimeN(uw_context ctx, uw_Basis_time *t) {
adamc@467 1999 if (t == NULL)
adamc@467 2000 return "NULL";
adamc@467 2001 else
adamc@467 2002 return uw_Basis_sqlifyTime(ctx, *t);
adamc@467 2003 }
adamc@467 2004
adamc@311 2005 char *uw_Basis_ensqlBool(uw_Basis_bool b) {
adamc@311 2006 static uw_Basis_int true = 1;
adamc@311 2007 static uw_Basis_int false = 0;
adamc@282 2008
adamc@311 2009 if (b == uw_Basis_False)
adamc@282 2010 return (char *)&false;
adamc@282 2011 else
adamc@282 2012 return (char *)&true;
adamc@282 2013 }
adamc@284 2014
adamc@311 2015 uw_Basis_string uw_Basis_intToString(uw_context ctx, uw_Basis_int n) {
adamc@284 2016 int len;
adamc@284 2017 char *r;
adamc@284 2018
adamc@311 2019 uw_check_heap(ctx, INTS_MAX);
adamc@666 2020 r = ctx->heap.front;
adamc@284 2021 sprintf(r, "%lld%n", n, &len);
adamc@666 2022 ctx->heap.front += len+1;
adamc@284 2023 return r;
adamc@284 2024 }
adamc@285 2025
adamc@311 2026 uw_Basis_string uw_Basis_floatToString(uw_context ctx, uw_Basis_float n) {
adamc@285 2027 int len;
adamc@285 2028 char *r;
adamc@285 2029
adamc@311 2030 uw_check_heap(ctx, FLOATS_MAX);
adamc@666 2031 r = ctx->heap.front;
adamc@285 2032 sprintf(r, "%g%n", n, &len);
adamc@666 2033 ctx->heap.front += len+1;
adamc@285 2034 return r;
adamc@285 2035 }
adamc@285 2036
adamc@311 2037 uw_Basis_string uw_Basis_boolToString(uw_context ctx, uw_Basis_bool b) {
adamc@311 2038 if (b == uw_Basis_False)
adamc@285 2039 return "False";
adamc@285 2040 else
adamc@285 2041 return "True";
adamc@285 2042 }
adamc@288 2043
adamc@436 2044 uw_Basis_string uw_Basis_timeToString(uw_context ctx, uw_Basis_time t) {
adamc@436 2045 size_t len;
adamc@436 2046 char *r;
adamc@436 2047 struct tm stm;
adamc@436 2048
adamc@436 2049 if (localtime_r(&t, &stm)) {
adamc@436 2050 uw_check_heap(ctx, TIMES_MAX);
adamc@666 2051 r = ctx->heap.front;
adamc@436 2052 len = strftime(r, TIMES_MAX, TIME_FMT, &stm);
adamc@666 2053 ctx->heap.front += len+1;
adamc@436 2054 return r;
adamc@436 2055 } else
adamc@436 2056 return "<Invalid time>";
adamc@436 2057 }
adamc@288 2058
adamc@311 2059 uw_Basis_int *uw_Basis_stringToInt(uw_context ctx, uw_Basis_string s) {
adamc@288 2060 char *endptr;
adamc@311 2061 uw_Basis_int n = strtoll(s, &endptr, 10);
adamc@288 2062
adamc@288 2063 if (*s != '\0' && *endptr == '\0') {
adamc@311 2064 uw_Basis_int *r = uw_malloc(ctx, sizeof(uw_Basis_int));
adamc@288 2065 *r = n;
adamc@288 2066 return r;
adamc@288 2067 } else
adamc@288 2068 return NULL;
adamc@288 2069 }
adamc@289 2070
adamc@311 2071 uw_Basis_float *uw_Basis_stringToFloat(uw_context ctx, uw_Basis_string s) {
adamc@289 2072 char *endptr;
adamc@311 2073 uw_Basis_float n = strtod(s, &endptr);
adamc@289 2074
adamc@289 2075 if (*s != '\0' && *endptr == '\0') {
adamc@311 2076 uw_Basis_float *r = uw_malloc(ctx, sizeof(uw_Basis_float));
adamc@289 2077 *r = n;
adamc@289 2078 return r;
adamc@289 2079 } else
adamc@289 2080 return NULL;
adamc@289 2081 }
adamc@289 2082
adamc@311 2083 uw_Basis_bool *uw_Basis_stringToBool(uw_context ctx, uw_Basis_string s) {
adamc@311 2084 static uw_Basis_bool true = uw_Basis_True;
adamc@311 2085 static uw_Basis_bool false = uw_Basis_False;
adamc@289 2086
adamc@289 2087 if (!strcasecmp (s, "True"))
adamc@289 2088 return &true;
adamc@289 2089 else if (!strcasecmp (s, "False"))
adamc@289 2090 return &false;
adamc@289 2091 else
adamc@289 2092 return NULL;
adamc@289 2093 }
adamc@292 2094
adamc@436 2095 uw_Basis_time *uw_Basis_stringToTime(uw_context ctx, uw_Basis_string s) {
adamc@438 2096 char *dot = strchr(s, '.'), *end = strchr(s, 0);
adamc@436 2097 struct tm stm;
adamc@436 2098
adamc@439 2099 if (dot) {
adamc@439 2100 *dot = 0;
adamc@439 2101 if (strptime(s, TIME_FMT_PG, &stm) == end) {
adamc@439 2102 *dot = '.';
adamc@439 2103 uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time));
adamc@439 2104 *r = mktime(&stm);
adamc@439 2105 return r;
adamc@439 2106 }
adamc@439 2107 else {
adamc@439 2108 *dot = '.';
adamc@439 2109 return NULL;
adamc@439 2110 }
adamc@436 2111 }
adamc@439 2112 else {
adamc@439 2113 if (strptime(s, TIME_FMT_PG, &stm) == end) {
adamc@439 2114 uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time));
adamc@439 2115 *r = mktime(&stm);
adamc@439 2116 return r;
adamc@439 2117 }
adamc@439 2118 else if (strptime(s, TIME_FMT, &stm) == end) {
adamc@439 2119 uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time));
adamc@439 2120 *r = mktime(&stm);
adamc@439 2121 return r;
adamc@439 2122 }
adamc@439 2123 else
adamc@439 2124 return NULL;
adamc@439 2125 }
adamc@436 2126 }
adamc@436 2127
adamc@311 2128 uw_Basis_int uw_Basis_stringToInt_error(uw_context ctx, uw_Basis_string s) {
adamc@292 2129 char *endptr;
adamc@311 2130 uw_Basis_int n = strtoll(s, &endptr, 10);
adamc@292 2131
adamc@292 2132 if (*s != '\0' && *endptr == '\0')
adamc@292 2133 return n;
adamc@292 2134 else
adamc@311 2135 uw_error(ctx, FATAL, "Can't parse int: %s", s);
adamc@292 2136 }
adamc@293 2137
adamc@682 2138 #include <errno.h>
adamc@682 2139
adamc@678 2140 uw_Basis_channel uw_Basis_stringToChannel_error(uw_context ctx, uw_Basis_string s) {
adamc@682 2141 unsigned long long n;
adamc@682 2142
adamc@682 2143 if (sscanf(s, "%llu", &n) < 1)
adamc@682 2144 uw_error(ctx, FATAL, "Can't parse channel: %s", s);
adamc@682 2145 else {
adamc@682 2146 uw_Basis_channel ch = {n >> 32, n & ((1ull << 32) - 1)};
adamc@682 2147 return ch;
adamc@682 2148 }
adamc@682 2149 }
adamc@682 2150
adamc@682 2151 uw_Basis_client uw_Basis_stringToClient_error(uw_context ctx, uw_Basis_string s) {
adamc@678 2152 char *endptr;
adamc@682 2153 unsigned long n = strtoul(s, &endptr, 10);
adamc@678 2154
adamc@678 2155 if (*s != '\0' && *endptr == '\0')
adamc@678 2156 return n;
adamc@678 2157 else
adamc@682 2158 uw_error(ctx, FATAL, "Can't parse client: %s", s);
adamc@678 2159 }
adamc@678 2160
adamc@311 2161 uw_Basis_float uw_Basis_stringToFloat_error(uw_context ctx, uw_Basis_string s) {
adamc@293 2162 char *endptr;
adamc@311 2163 uw_Basis_float n = strtod(s, &endptr);
adamc@293 2164
adamc@293 2165 if (*s != '\0' && *endptr == '\0')
adamc@293 2166 return n;
adamc@293 2167 else
adamc@311 2168 uw_error(ctx, FATAL, "Can't parse float: %s", s);
adamc@293 2169 }
adamc@293 2170
adamc@311 2171 uw_Basis_bool uw_Basis_stringToBool_error(uw_context ctx, uw_Basis_string s) {
adamc@296 2172 if (!strcasecmp(s, "T") || !strcasecmp (s, "True"))
adamc@311 2173 return uw_Basis_True;
adamc@296 2174 else if (!strcasecmp(s, "F") || !strcasecmp (s, "False"))
adamc@311 2175 return uw_Basis_False;
adamc@293 2176 else
adamc@311 2177 uw_error(ctx, FATAL, "Can't parse bool: %s", s);
adamc@293 2178 }
adamc@436 2179
adamc@436 2180 uw_Basis_time uw_Basis_stringToTime_error(uw_context ctx, uw_Basis_string s) {
adamc@438 2181 char *dot = strchr(s, '.'), *end = strchr(s, 0);
adamc@436 2182 struct tm stm = {};
adamc@436 2183
adamc@438 2184 if (dot) {
adamc@438 2185 *dot = 0;
adamc@438 2186 if (strptime(s, TIME_FMT_PG, &stm)) {
adamc@438 2187 *dot = '.';
adamc@438 2188 return mktime(&stm);
adamc@438 2189 }
adamc@438 2190 else {
adamc@438 2191 *dot = '.';
adamc@438 2192 uw_error(ctx, FATAL, "Can't parse time: %s", s);
adamc@438 2193 }
adamc@438 2194 }
adamc@438 2195 else {
adamc@439 2196 if (strptime(s, TIME_FMT_PG, &stm) == end)
adamc@439 2197 return mktime(&stm);
adamc@439 2198 else if (strptime(s, TIME_FMT, &stm) == end)
adamc@438 2199 return mktime(&stm);
adamc@438 2200 else
adamc@438 2201 uw_error(ctx, FATAL, "Can't parse time: %s", s);
adamc@438 2202 }
adamc@436 2203 }
adamc@457 2204
adamc@742 2205 uw_Basis_blob uw_Basis_stringToBlob_error(uw_context ctx, uw_Basis_string s, size_t len) {
adamc@742 2206 char *r = ctx->heap.front;
adamc@742 2207 uw_Basis_blob b = {len, r};
adamc@742 2208
adamc@742 2209 uw_check_heap(ctx, len);
adamc@742 2210
adamc@742 2211 while (*s) {
adamc@742 2212 if (s[0] == '\\') {
adamc@742 2213 if (s[1] == '\\') {
adamc@742 2214 *r++ = '\\';
adamc@742 2215 s += 2;
adamc@742 2216 } else if (isdigit(s[1]) && isdigit(s[2]) && isdigit(s[3])) {
adamc@742 2217 *r++ = (s[1] - '0') * 8 * 8 + ((s[2] - '0') * 8) + (s[3] - '0');
adamc@742 2218 s += 4;
adamc@742 2219 }
adamc@742 2220 else {
adamc@742 2221 *r++ = '\\';
adamc@742 2222 ++s;
adamc@742 2223 }
adamc@742 2224 } else {
adamc@742 2225 *r++ = s[0];
adamc@742 2226 ++s;
adamc@742 2227 }
adamc@742 2228 }
adamc@742 2229
adamc@742 2230 b.size = r - ctx->heap.front;
adamc@742 2231 ctx->heap.front = r;
adamc@742 2232 return b;
adamc@742 2233 }
adamc@742 2234
adamc@463 2235 uw_Basis_string uw_Basis_get_cookie(uw_context ctx, uw_Basis_string c) {
adamc@463 2236 int len = strlen(c);
adamc@666 2237 char *s = ctx->headers, *p = ctx->outHeaders.start;
adamc@474 2238
adamc@474 2239 while (p = strstr(p, "\nSet-Cookie: ")) {
adamc@474 2240 char *p2;
adamc@474 2241 p += 13;
adamc@474 2242 p2 = strchr(p, '=');
adamc@474 2243
adamc@474 2244 if (p2) {
adamc@474 2245 size_t sz = strcspn(p2+1, ";\r\n");
adamc@474 2246
adamc@474 2247 if (!strncasecmp(p, c, p2 - p)) {
adamc@474 2248 char *ret = uw_malloc(ctx, sz + 1);
adamc@474 2249 memcpy(ret, p2+1, sz);
adamc@474 2250 ret[sz] = 0;
adamc@474 2251 return ret;
adamc@474 2252 }
adamc@474 2253 }
adamc@474 2254 }
adamc@463 2255
adamc@463 2256 while (p = strchr(s, ':')) {
adamc@476 2257 if (!strncasecmp(s, "Cookie: ", 8)) {
adamc@476 2258 p += 2;
adamc@476 2259 while (1) {
adamc@476 2260 if (!strncmp(p, c, len)
adamc@476 2261 && p + len < ctx->headers_end && p[len] == '=')
adamc@476 2262 return p + 1 + len;
adamc@476 2263 else if (p = strchr(p, ';'))
adamc@476 2264 p += 2;
adamc@476 2265 else if ((s = strchr(s, 0)) && s < ctx->headers_end) {
adamc@476 2266 s += 2;
adamc@476 2267 break;
adamc@476 2268 }
adamc@476 2269 else
adamc@476 2270 return NULL;
adamc@476 2271 }
adamc@463 2272 } else {
adamc@463 2273 if ((s = strchr(p, 0)) && s < ctx->headers_end)
adamc@463 2274 s += 2;
adamc@463 2275 else
adamc@463 2276 return NULL;
adamc@463 2277 }
adamc@463 2278 }
adamc@457 2279 }
adamc@462 2280
adamc@466 2281 uw_unit uw_Basis_set_cookie(uw_context ctx, uw_Basis_string prefix, uw_Basis_string c, uw_Basis_string v) {
adamc@462 2282 uw_write_header(ctx, "Set-Cookie: ");
adamc@462 2283 uw_write_header(ctx, c);
adamc@462 2284 uw_write_header(ctx, "=");
adamc@462 2285 uw_write_header(ctx, v);
adamc@466 2286 uw_write_header(ctx, "; path=");
adamc@466 2287 uw_write_header(ctx, prefix);
adamc@462 2288 uw_write_header(ctx, "\r\n");
adamc@462 2289
adamc@462 2290 return uw_unit_v;
adamc@462 2291 }
adamc@667 2292
adamc@682 2293 static delta *allocate_delta(uw_context ctx, unsigned client) {
adamc@682 2294 unsigned i;
adamc@682 2295 delta *d;
adamc@671 2296
adamc@682 2297 for (i = 0; i < ctx->used_deltas; ++i)
adamc@682 2298 if (ctx->deltas[i].client == client)
adamc@682 2299 return &ctx->deltas[i];
adamc@671 2300
adamc@682 2301 if (ctx->used_deltas >= ctx->n_deltas) {
adamc@682 2302 ctx->deltas = realloc(ctx->deltas, sizeof(delta) * ++ctx->n_deltas);
adamc@682 2303 buf_init(&ctx->deltas[ctx->n_deltas-1].msgs, 0);
adamc@682 2304 }
adamc@671 2305
adamc@682 2306 d = &ctx->deltas[ctx->used_deltas++];
adamc@682 2307 d->client = client;
adamc@682 2308 buf_reset(&d->msgs);
adamc@682 2309 return d;
adamc@671 2310 }
adamc@671 2311
adamc@668 2312 uw_Basis_channel uw_Basis_new_channel(uw_context ctx, uw_unit u) {
adamc@682 2313 if (ctx->client == NULL)
adamc@682 2314 uw_error(ctx, FATAL, "Attempt to create channel on request not associated with a persistent connection");
adamc@671 2315
adamc@682 2316 return new_channel(ctx->client);
adamc@668 2317 }
adamc@668 2318
adamc@668 2319 uw_unit uw_Basis_send(uw_context ctx, uw_Basis_channel chn, uw_Basis_string msg) {
adamc@682 2320 delta *d = allocate_delta(ctx, chn.cli);
adamc@682 2321 size_t len;
adamc@682 2322 int preLen;
adamc@682 2323 char pre[INTS_MAX + 2];
adamc@668 2324
adamc@682 2325 len = strlen(msg);
adamc@682 2326
adamc@682 2327 sprintf(pre, "%u\n%n", chn.chn, &preLen);
adamc@682 2328
adamc@682 2329 buf_append(&d->msgs, pre, preLen);
adamc@682 2330 buf_append(&d->msgs, msg, len);
adamc@682 2331 buf_append(&d->msgs, "\n", 1);
adamc@671 2332
adamc@671 2333 return uw_unit_v;
adamc@668 2334 }
adamc@671 2335
adamc@671 2336 int uw_db_commit(uw_context);
adamc@671 2337 int uw_db_rollback(uw_context);
adamc@671 2338
adamc@671 2339 void uw_commit(uw_context ctx) {
adamc@682 2340 unsigned i;
adamc@671 2341
adamc@700 2342 if (uw_db_commit(ctx))
adamc@671 2343 uw_error(ctx, FATAL, "Error running SQL COMMIT");
adamc@682 2344
adamc@682 2345 for (i = 0; i < ctx->used_deltas; ++i) {
adamc@682 2346 delta *d = &ctx->deltas[i];
adamc@682 2347 client *c = find_client(d->client);
adamc@682 2348
adamc@682 2349 assert (c != NULL && c->mode == USED);
adamc@682 2350
adamc@685 2351 client_send(c, &d->msgs);
adamc@682 2352 }
adamc@682 2353
adamc@682 2354 if (ctx->client)
adamc@685 2355 release_client(ctx->client);
adamc@671 2356 }
adamc@671 2357
adamc@671 2358 int uw_rollback(uw_context ctx) {
adamc@682 2359 if (ctx->client)
adamc@685 2360 release_client(ctx->client);
adamc@671 2361
adamc@671 2362 return uw_db_rollback(ctx);
adamc@671 2363 }
adamc@683 2364
adamc@683 2365
adamc@683 2366 // "Garbage collection"
adamc@683 2367
adamc@683 2368 void uw_expunger(uw_context ctx, uw_Basis_client cli);
adamc@683 2369
adamc@683 2370 static failure_kind uw_expunge(uw_context ctx, uw_Basis_client cli) {
adamc@683 2371 int r = setjmp(ctx->jmp_buf);
adamc@683 2372
adamc@684 2373 if (r == 0) {
adamc@684 2374 if (uw_db_begin(ctx))
adamc@684 2375 uw_error(ctx, FATAL, "Error running SQL BEGIN");
adamc@683 2376 uw_expunger(ctx, cli);
adamc@684 2377 if (uw_db_commit(ctx))
adamc@684 2378 uw_error(ctx, FATAL, "Error running SQL COMMIT");
adamc@684 2379 }
adamc@683 2380
adamc@683 2381 return r;
adamc@683 2382 }
adamc@683 2383
adamc@683 2384 void uw_prune_clients(uw_context ctx) {
adamc@683 2385 client *c, *next, *prev = NULL;
adamc@683 2386 time_t cutoff;
adamc@683 2387
adamc@683 2388 cutoff = time(NULL) - uw_timeout;
adamc@683 2389
adamc@683 2390 pthread_mutex_lock(&clients_mutex);
adamc@683 2391
adamc@683 2392 for (c = clients_used; c; c = next) {
adamc@683 2393 next = c->next;
adamc@683 2394 pthread_mutex_lock(&c->lock);
adamc@685 2395 if (c->last_contact < cutoff && c->refcount == 0) {
adamc@683 2396 failure_kind fk = UNLIMITED_RETRY;
adamc@683 2397 if (prev)
adamc@683 2398 prev->next = next;
adamc@683 2399 else
adamc@683 2400 clients_used = next;
adamc@684 2401 uw_reset(ctx);
adamc@683 2402 while (fk == UNLIMITED_RETRY) {
adamc@683 2403 fk = uw_expunge(ctx, c->id);
adamc@684 2404 if (fk == UNLIMITED_RETRY) {
adamc@684 2405 uw_db_rollback(ctx);
adamc@684 2406 printf("Unlimited retry during expunge: %s\n", uw_error_message(ctx));
adamc@683 2407 }
adamc@683 2408 }
adamc@684 2409 if (fk == SUCCESS)
adamc@684 2410 free_client(c);
adamc@684 2411 else {
adamc@684 2412 uw_db_rollback(ctx);
adamc@683 2413 printf("Expunge blocked by error: %s\n", uw_error_message(ctx));
adamc@684 2414 }
adamc@683 2415 }
adamc@683 2416 else
adamc@683 2417 prev = c;
adamc@683 2418 pthread_mutex_unlock(&c->lock);
adamc@683 2419 }
adamc@683 2420
adamc@683 2421 pthread_mutex_unlock(&clients_mutex);
adamc@683 2422 }
adamc@687 2423
adamc@687 2424 void uw_initializer(uw_context ctx);
adamc@687 2425
adamc@687 2426 failure_kind uw_initialize(uw_context ctx) {
adamc@687 2427 int r = setjmp(ctx->jmp_buf);
adamc@687 2428
adamc@687 2429 if (r == 0) {
adamc@687 2430 if (uw_db_begin(ctx))
adamc@687 2431 uw_error(ctx, FATAL, "Error running SQL BEGIN");
adamc@687 2432 uw_initializer(ctx);
adamc@687 2433 if (uw_db_commit(ctx))
adamc@687 2434 uw_error(ctx, FATAL, "Error running SQL COMMIT");
adamc@687 2435 }
adamc@687 2436
adamc@687 2437 return r;
adamc@687 2438 }
adamc@717 2439
adamc@717 2440 uw_Basis_string uw_Basis_bless(uw_context ctx, uw_Basis_string s) {
adamc@717 2441 return s;
adamc@717 2442 }
adamc@734 2443
adamc@741 2444 uw_Basis_string uw_Basis_blessMime(uw_context ctx, uw_Basis_string s) {
adamc@741 2445 char *s2;
adamc@741 2446
adamc@741 2447 for (s2 = s; *s2; ++s2)
adamc@741 2448 if (!isalnum(*s2) && *s2 != '/' && *s2 != '-' && *s2 != '.')
adamc@741 2449 uw_error(ctx, FATAL, "MIME type \"%s\" contains invalid character %c\n", s, *s2);
adamc@741 2450
adamc@741 2451 return s;
adamc@741 2452 }
adamc@741 2453
adamc@734 2454 uw_Basis_string uw_unnull(uw_Basis_string s) {
adamc@734 2455 return s ? s : "";
adamc@734 2456 }
adamc@734 2457
adamc@734 2458 extern int uw_hash_blocksize;
adamc@734 2459
adamc@734 2460 uw_Basis_string uw_Basis_makeSigString(uw_context ctx, uw_Basis_string sig) {
adamc@734 2461 uw_Basis_string r = uw_malloc(ctx, 2 * uw_hash_blocksize + 1);
adamc@734 2462 int i;
adamc@734 2463
adamc@734 2464 for (i = 0; i < uw_hash_blocksize; ++i)
adamc@734 2465 sprintf(&r[2*i], "%.02X", ((unsigned char *)sig)[i]);
adamc@734 2466
adamc@734 2467 return r;
adamc@734 2468 }
adamc@734 2469
adamc@734 2470 uw_Basis_string uw_Basis_sigString(uw_context ctx, uw_unit u) {
adamc@734 2471 return uw_cookie_sig(ctx);
adamc@734 2472 }
adamc@737 2473
adamc@737 2474 uw_Basis_string uw_Basis_fileName(uw_context ctx, uw_Basis_file f) {
adamc@737 2475 return f.name;
adamc@737 2476 }
adamc@737 2477
adamc@740 2478 uw_Basis_string uw_Basis_fileMimeType(uw_context ctx, uw_Basis_file f) {
adamc@740 2479 return f.type;
adamc@740 2480 }
adamc@740 2481
adamc@745 2482 uw_Basis_int uw_Basis_blobSize(uw_context ctx, uw_Basis_blob b) {
adamc@745 2483 return b.size;
adamc@745 2484 }
adamc@745 2485
adamc@737 2486 uw_Basis_blob uw_Basis_fileData(uw_context ctx, uw_Basis_file f) {
adamc@737 2487 return f.data;
adamc@737 2488 }
adamc@741 2489
adamc@741 2490 __attribute__((noreturn)) void uw_return_blob(uw_context ctx, uw_Basis_blob b, uw_Basis_string mimeType) {
adamc@741 2491 cleanup *cl;
adamc@741 2492 int len;
adamc@741 2493
adamc@741 2494 buf_reset(&ctx->outHeaders);
adamc@741 2495 buf_reset(&ctx->page);
adamc@741 2496
adamc@741 2497 uw_write_header(ctx, "HTTP/1.1 200 OK\r\nContent-Type: ");
adamc@741 2498 uw_write_header(ctx, mimeType);
adamc@741 2499 uw_write_header(ctx, "\r\nContent-Length: ");
adamc@741 2500 buf_check(&ctx->outHeaders, INTS_MAX);
adamc@741 2501 sprintf(ctx->outHeaders.front, "%d%n", b.size, &len);
adamc@741 2502 ctx->outHeaders.front += len;
adamc@741 2503 uw_write_header(ctx, "\r\n");
adamc@741 2504
adamc@741 2505 buf_append(&ctx->page, b.data, b.size);
adamc@741 2506
adamc@741 2507 for (cl = ctx->cleanup; cl < ctx->cleanup_front; ++cl)
adamc@741 2508 cl->func(cl->arg);
adamc@741 2509
adamc@741 2510 ctx->cleanup_front = ctx->cleanup;
adamc@741 2511
adamc@741 2512 longjmp(ctx->jmp_buf, RETURN_BLOB);
adamc@741 2513 }