annotate src/c/urweb.c @ 1065:217eb87dde31

Basis.url and redirects
author Adam Chlipala <adamc@hcoop.net>
date Thu, 10 Dec 2009 13:32:09 -0500
parents e3f6620afd51
children 740b85ef4352
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@863 22 int uw_really_send(int sock, const void *buf, ssize_t len) {
adamc@667 23 while (len > 0) {
adamc@856 24 ssize_t n = send(sock, buf, len, 0);
adamc@856 25
adamc@856 26 if (n < 0)
adamc@856 27 return n;
adamc@856 28
adamc@856 29 buf += n;
adamc@856 30 len -= n;
adamc@856 31 }
adamc@856 32
adamc@856 33 return 0;
adamc@856 34 }
adamc@856 35
adamc@856 36 int uw_really_write(int fd, const void *buf, size_t len) {
adamc@856 37 while (len > 0) {
adamc@856 38 ssize_t n = write(fd, buf, len);
adamc@667 39
adamc@667 40 if (n < 0)
adamc@667 41 return n;
adamc@667 42
adamc@667 43 buf += n;
adamc@667 44 len -= n;
adamc@667 45 }
adamc@667 46
adamc@667 47 return 0;
adamc@667 48 }
adamc@667 49
adamc@667 50
adamc@667 51 // Buffers
adamc@667 52
adamc@667 53 typedef struct {
adamc@667 54 char *start, *front, *back;
adamc@667 55 } buf;
adamc@667 56
adamc@667 57 static void buf_init(buf *b, size_t s) {
adamc@667 58 b->front = b->start = malloc(s);
adamc@667 59 b->back = b->front + s;
adamc@667 60 }
adamc@667 61
adamc@667 62 static void buf_free(buf *b) {
adamc@667 63 free(b->start);
adamc@667 64 }
adamc@667 65
adamc@667 66 static void buf_reset(buf *b) {
adamc@667 67 b->front = b->start;
adamc@667 68 }
adamc@667 69
adamc@667 70 static void buf_check(buf *b, size_t extra) {
adamc@667 71 if (b->back - b->front < extra) {
adamc@667 72 size_t desired = b->front - b->start + extra, next;
adamc@667 73 char *new_heap;
adamc@667 74
adamc@667 75 next = b->back - b->start;
adamc@667 76 if (next == 0)
adamc@667 77 next = 1;
adamc@667 78 for (; next < desired; next *= 2);
adamc@667 79
adamc@667 80 new_heap = realloc(b->start, next);
adamc@667 81 b->front = new_heap + (b->front - b->start);
adamc@667 82 b->back = new_heap + next;
adamc@667 83 b->start = new_heap;
adamc@667 84 }
adamc@667 85 }
adamc@667 86
adamc@667 87 static size_t buf_used(buf *b) {
adamc@667 88 return b->front - b->start;
adamc@667 89 }
adamc@667 90
adamc@667 91 static size_t buf_avail(buf *b) {
adamc@667 92 return b->back - b->start;
adamc@667 93 }
adamc@667 94
adamc@668 95 static void buf_append(buf *b, const char *s, size_t len) {
adamc@672 96 buf_check(b, len+1);
adamc@668 97 memcpy(b->front, s, len);
adamc@668 98 b->front += len;
adamc@672 99 *b->front = 0;
adamc@668 100 }
adamc@667 101
adamc@668 102
adamc@672 103 // Persistent state types
adamc@667 104
adamc@667 105 typedef enum { UNUSED, USED } usage;
adamc@667 106
adamc@667 107 typedef struct client {
adamc@682 108 unsigned id;
adamc@667 109 usage mode;
adamc@682 110 int pass;
adamc@682 111 struct client *next;
adamc@689 112 pthread_mutex_t lock, pull_lock;
adamc@682 113 buf msgs;
adamc@682 114 int sock;
adamc@863 115 int (*send)(int sockfd, const void *buf, ssize_t len);
adamc@863 116 int (*close)(int fd);
adamc@682 117 time_t last_contact;
adamc@682 118 unsigned n_channels;
adamc@685 119 unsigned refcount;
adamc@667 120 } client;
adamc@667 121
adamc@672 122
adamc@672 123 // Persistent client state
adamc@672 124
adamc@682 125 static client **clients, *clients_free, *clients_used;
adamc@682 126 static unsigned n_clients;
adamc@667 127
adamc@667 128 static pthread_mutex_t clients_mutex = PTHREAD_MUTEX_INITIALIZER;
adamc@667 129
adamc@682 130 static client *new_client() {
adamc@667 131 client *c;
adamc@667 132
adamc@667 133 pthread_mutex_lock(&clients_mutex);
adamc@667 134
adamc@667 135 if (clients_free) {
adamc@667 136 c = clients_free;
adamc@682 137 clients_free = clients_free->next;
adamc@667 138 }
adamc@667 139 else {
adamc@667 140 ++n_clients;
adamc@667 141 clients = realloc(clients, sizeof(client) * n_clients);
adamc@667 142 c = malloc(sizeof(client));
adamc@667 143 c->id = n_clients-1;
adamc@682 144 pthread_mutex_init(&c->lock, NULL);
adamc@689 145 pthread_mutex_init(&c->pull_lock, NULL);
adamc@682 146 buf_init(&c->msgs, 0);
adamc@667 147 clients[n_clients-1] = c;
adamc@667 148 }
adamc@667 149
adamc@682 150 pthread_mutex_lock(&c->lock);
adamc@667 151 c->mode = USED;
adamc@682 152 c->pass = rand();
adamc@682 153 c->sock = -1;
adamc@682 154 c->last_contact = time(NULL);
adamc@682 155 buf_reset(&c->msgs);
adamc@682 156 c->n_channels = 0;
adamc@685 157 c->refcount = 0;
adamc@682 158 pthread_mutex_unlock(&c->lock);
adamc@682 159
adamc@682 160 c->next = clients_used;
adamc@682 161 clients_used = c;
adamc@667 162
adamc@667 163 pthread_mutex_unlock(&clients_mutex);
adamc@667 164
adamc@667 165 return c;
adamc@667 166 }
adamc@667 167
adamc@685 168 static void use_client(client *c) {
adamc@685 169 pthread_mutex_lock(&c->lock);
adamc@685 170 ++c->refcount;
adamc@685 171 pthread_mutex_unlock(&c->lock);
adamc@689 172 pthread_mutex_lock(&c->pull_lock);
adamc@685 173 }
adamc@685 174
adamc@685 175 static void release_client(client *c) {
adamc@689 176 pthread_mutex_unlock(&c->pull_lock);
adamc@685 177 pthread_mutex_lock(&c->lock);
adamc@685 178 --c->refcount;
adamc@685 179 pthread_mutex_unlock(&c->lock);
adamc@689 180
adamc@685 181 }
adamc@685 182
adamc@856 183 static const char begin_msgs[] = "Content-type: text/plain\r\n\r\n";
adamc@667 184
adamc@682 185 static client *find_client(unsigned id) {
adamc@667 186 client *c;
adamc@667 187
adamc@667 188 pthread_mutex_lock(&clients_mutex);
adamc@667 189
adamc@667 190 if (id >= n_clients) {
adamc@667 191 pthread_mutex_unlock(&clients_mutex);
adamc@668 192 return NULL;
adamc@667 193 }
adamc@667 194
adamc@667 195 c = clients[id];
adamc@667 196
adamc@668 197 pthread_mutex_unlock(&clients_mutex);
adamc@668 198 return c;
adamc@668 199 }
adamc@668 200
adamc@856 201 static char *on_success = "HTTP/1.1 200 OK\r\n";
adamc@1065 202 static char *on_redirect = "HTTP/1.1 303 See Other\r\n";
adamc@856 203
adamc@856 204 void uw_set_on_success(char *s) {
adamc@856 205 on_success = s;
adamc@856 206 }
adamc@856 207
adamc@863 208 void uw_client_connect(unsigned id, int pass, int sock,
adamc@863 209 int (*send)(int sockfd, const void *buf, ssize_t len),
adamc@864 210 int (*close)(int fd),
adamc@864 211 void *logger_data, uw_logger log_error) {
adamc@682 212 client *c = find_client(id);
adamc@668 213
adamc@668 214 if (c == NULL) {
adamc@667 215 close(sock);
adamc@864 216 log_error(logger_data, "Out-of-bounds client request (%u)\n", id);
adamc@667 217 return;
adamc@667 218 }
adamc@667 219
adamc@682 220 pthread_mutex_lock(&c->lock);
adamc@671 221
adamc@682 222 if (c->mode != USED) {
adamc@682 223 pthread_mutex_unlock(&c->lock);
adamc@667 224 close(sock);
adamc@864 225 log_error(logger_data, "Client request for unused slot (%u)\n", id);
adamc@667 226 return;
adamc@667 227 }
adamc@667 228
adamc@682 229 if (pass != c->pass) {
adamc@682 230 pthread_mutex_unlock(&c->lock);
adamc@682 231 close(sock);
adamc@864 232 log_error(logger_data, "Wrong client password (%u, %d)\n", id, pass);
adamc@682 233 return;
adamc@667 234 }
adamc@667 235
adamc@682 236 if (c->sock != -1) {
adamc@863 237 c->close(c->sock);
adamc@682 238 c->sock = -1;
adamc@682 239 }
adamc@667 240
adamc@682 241 c->last_contact = time(NULL);
adamc@682 242
adamc@682 243 if (buf_used(&c->msgs) > 0) {
adamc@863 244 send(sock, on_success, strlen(on_success));
adamc@863 245 send(sock, begin_msgs, sizeof(begin_msgs) - 1);
adamc@863 246 send(sock, c->msgs.start, buf_used(&c->msgs));
adamc@682 247 buf_reset(&c->msgs);
adamc@667 248 close(sock);
adamc@667 249 }
adamc@863 250 else {
adamc@682 251 c->sock = sock;
adamc@863 252 c->send = send;
adamc@863 253 c->close = close;
adamc@863 254 }
adamc@667 255
adamc@682 256 pthread_mutex_unlock(&c->lock);
adamc@667 257 }
adamc@667 258
adamc@682 259 static void free_client(client *c) {
adamc@682 260 c->mode = UNUSED;
adamc@682 261 c->pass = -1;
adamc@667 262
adamc@682 263 c->next = clients_free;
adamc@682 264 clients_free = c;
adamc@667 265 }
adamc@667 266
adamc@682 267 static uw_Basis_channel new_channel(client *c) {
adamc@682 268 uw_Basis_channel ch = {c->id, c->n_channels++};
adamc@668 269 return ch;
adamc@668 270 }
adamc@668 271
adamc@685 272 static void client_send(client *c, buf *msg) {
adamc@685 273 pthread_mutex_lock(&c->lock);
adamc@671 274
adamc@682 275 if (c->sock != -1) {
adamc@863 276 c->send(c->sock, on_success, strlen(on_success));
adamc@863 277 c->send(c->sock, begin_msgs, sizeof(begin_msgs) - 1);
adamc@863 278 c->send(c->sock, msg->start, buf_used(msg));
adamc@863 279 c->close(c->sock);
adamc@682 280 c->sock = -1;
adamc@682 281 } else
adamc@682 282 buf_append(&c->msgs, msg->start, buf_used(msg));
adamc@671 283
adamc@685 284 pthread_mutex_unlock(&c->lock);
adamc@668 285 }
adamc@668 286
adamc@668 287
adamc@668 288 // Global entry points
adamc@668 289
adamc@866 290 extern void uw_client_init();
adamc@866 291
adamc@668 292 void uw_global_init() {
adamc@668 293 srand(time(NULL) ^ getpid());
adamc@668 294
adamc@668 295 clients = malloc(0);
adamc@866 296
adamc@866 297 uw_client_init();
adamc@668 298 }
adamc@668 299
adamc@668 300
adamc@667 301 // Single-request state
adamc@667 302
adamc@167 303 #define ERROR_BUF_LEN 1024
adamc@167 304
adamc@323 305 typedef struct regions {
adamc@323 306 struct regions *next;
adamc@323 307 } regions;
adamc@323 308
adamc@425 309 typedef struct {
adamc@425 310 void (*func)(void*);
adamc@425 311 void *arg;
adamc@425 312 } cleanup;
adamc@425 313
adamc@671 314 typedef struct {
adamc@682 315 unsigned client;
adamc@671 316 buf msgs;
adamc@682 317 } delta;
adamc@671 318
adamc@737 319 typedef enum {
adamc@759 320 UNSET, NORMAL, FIL, SUBFORM, SUBFORMS, ENTRY
adamc@737 321 } input_kind;
adamc@737 322
adamc@756 323 typedef struct input {
adamc@737 324 input_kind kind;
adamc@737 325 union {
adamc@737 326 char *normal;
adamc@739 327 uw_Basis_file file;
adamc@756 328 struct {
adamc@759 329 struct input *fields, *parent;
adamc@756 330 } subform;
adamc@759 331 struct {
adamc@759 332 struct input *entries, *parent;
adamc@759 333 } subforms;
adamc@759 334 struct {
adamc@759 335 struct input *fields, *next, *parent;
adamc@759 336 } entry;
adamc@737 337 } data;
adamc@737 338 } input;
adamc@737 339
adamc@771 340 typedef struct {
adamc@771 341 void *data;
adamc@771 342 uw_callback commit, rollback, free;
adamc@771 343 } transactional;
adamc@771 344
adamc@1061 345 typedef struct {
adamc@1061 346 char *name;
adamc@1061 347 void *data;
adamc@1061 348 void (*free)(void*);
adamc@1061 349 } global;
adamc@1061 350
adamc@311 351 struct uw_context {
adamc@854 352 char *(*get_header)(void *, const char *);
adamc@854 353 void *get_header_data;
adamc@457 354
adamc@666 355 buf outHeaders, page, heap, script;
adamc@1065 356 int returning_indirectly;
adamc@759 357 input *inputs, *subinputs, *cur_container;
adamc@756 358 size_t n_subinputs, used_subinputs;
adamc@167 359
adamc@565 360 int source_count;
adamc@562 361
adamc@272 362 void *db;
adamc@272 363
adamc@167 364 jmp_buf jmp_buf;
adamc@167 365
adamc@323 366 regions *regions;
adamc@323 367
adamc@425 368 cleanup *cleanup, *cleanup_front, *cleanup_back;
adamc@425 369
adamc@667 370 const char *script_header, *url_prefix;
adamc@643 371
adamc@736 372 int needs_push, needs_sig;
adamc@693 373
adamc@682 374 size_t n_deltas, used_deltas;
adamc@682 375 delta *deltas;
adamc@671 376
adamc@673 377 int timeout;
adamc@673 378
adamc@682 379 client *client;
adamc@682 380
adamc@771 381 transactional *transactionals;
adamc@771 382 size_t n_transactionals, used_transactionals;
adamc@771 383
adamc@1061 384 global *globals;
adamc@1061 385 size_t n_globals;
adamc@1061 386
adamc@167 387 char error_message[ERROR_BUF_LEN];
adamc@117 388 };
adamc@117 389
adamc@683 390 extern int uw_inputs_len, uw_timeout;
adamc@144 391
adamc@687 392 uw_context uw_init() {
adamc@311 393 uw_context ctx = malloc(sizeof(struct uw_context));
adamc@136 394
adamc@854 395 ctx->get_header = NULL;
adamc@854 396 ctx->get_header_data = NULL;
adamc@457 397
adamc@687 398 buf_init(&ctx->outHeaders, 0);
adamc@687 399 buf_init(&ctx->page, 0);
adamc@1065 400 ctx->returning_indirectly = 0;
adamc@687 401 buf_init(&ctx->heap, 0);
adamc@698 402 buf_init(&ctx->script, 1);
adamc@667 403 ctx->script.start[0] = 0;
adamc@136 404
adamc@737 405 ctx->inputs = calloc(uw_inputs_len, sizeof(input));
adamc@759 406 ctx->cur_container = NULL;
adamc@756 407 ctx->subinputs = malloc(0);
adamc@756 408 ctx->n_subinputs = ctx->used_subinputs = 0;
adamc@144 409
adamc@272 410 ctx->db = NULL;
adamc@272 411
adamc@323 412 ctx->regions = NULL;
adamc@323 413
adamc@425 414 ctx->cleanup_front = ctx->cleanup_back = ctx->cleanup = malloc(0);
adamc@425 415
adamc@643 416 ctx->script_header = "";
adamc@667 417 ctx->url_prefix = "/";
adamc@693 418 ctx->needs_push = 0;
adamc@736 419 ctx->needs_sig = 0;
adamc@643 420
adamc@167 421 ctx->error_message[0] = 0;
adamc@167 422
adamc@565 423 ctx->source_count = 0;
adamc@562 424
adamc@682 425 ctx->n_deltas = ctx->used_deltas = 0;
adamc@671 426 ctx->deltas = malloc(0);
adamc@671 427
adamc@673 428 ctx->timeout = uw_timeout;
adamc@673 429
adamc@700 430 ctx->client = NULL;
adamc@700 431
adamc@700 432 ctx->error_message[0] = 0;
adamc@700 433
adamc@771 434 ctx->transactionals = malloc(0);
adamc@771 435 ctx->n_transactionals = ctx->used_transactionals = 0;
adamc@771 436
adamc@1061 437 ctx->globals = malloc(0);
adamc@1061 438 ctx->n_globals = 0;
adamc@1061 439
adamc@117 440 return ctx;
adamc@106 441 }
adamc@106 442
adamc@311 443 void uw_set_db(uw_context ctx, void *db) {
adamc@272 444 ctx->db = db;
adamc@272 445 }
adamc@272 446
adamc@311 447 void *uw_get_db(uw_context ctx) {
adamc@272 448 return ctx->db;
adamc@272 449 }
adamc@272 450
adamc@311 451 void uw_free(uw_context ctx) {
adamc@671 452 size_t i;
adamc@671 453
adamc@666 454 buf_free(&ctx->outHeaders);
adamc@666 455 buf_free(&ctx->script);
adamc@666 456 buf_free(&ctx->page);
adamc@666 457 buf_free(&ctx->heap);
adamc@144 458 free(ctx->inputs);
adamc@756 459 free(ctx->subinputs);
adamc@425 460 free(ctx->cleanup);
adamc@771 461 free(ctx->transactionals);
adamc@671 462
adamc@682 463 for (i = 0; i < ctx->n_deltas; ++i)
adamc@671 464 buf_free(&ctx->deltas[i].msgs);
adamc@671 465
adamc@1061 466 for (i = 0; i < ctx->n_globals; ++i)
adamc@1061 467 ctx->globals[i].free(ctx->globals[i].data);
adamc@1061 468
adamc@136 469 free(ctx);
adamc@136 470 }
adamc@136 471
adamc@562 472 void uw_reset_keep_error_message(uw_context ctx) {
adamc@756 473 size_t i;
adamc@756 474
adamc@666 475 buf_reset(&ctx->outHeaders);
adamc@666 476 buf_reset(&ctx->script);
adamc@667 477 ctx->script.start[0] = 0;
adamc@666 478 buf_reset(&ctx->page);
adamc@1065 479 ctx->returning_indirectly = 0;
adamc@666 480 buf_reset(&ctx->heap);
adamc@323 481 ctx->regions = NULL;
adamc@425 482 ctx->cleanup_front = ctx->cleanup;
adamc@565 483 ctx->source_count = 0;
adamc@682 484 ctx->used_deltas = 0;
adamc@682 485 ctx->client = NULL;
adamc@759 486 ctx->cur_container = NULL;
adamc@771 487 ctx->used_transactionals = 0;
adamc@167 488 }
adamc@167 489
adamc@562 490 void uw_reset_keep_request(uw_context ctx) {
adamc@562 491 uw_reset_keep_error_message(ctx);
adamc@562 492 ctx->error_message[0] = 0;
adamc@167 493 }
adamc@167 494
adamc@311 495 void uw_reset(uw_context ctx) {
adamc@311 496 uw_reset_keep_request(ctx);
adamc@737 497 memset(ctx->inputs, 0, uw_inputs_len * sizeof(input));
adamc@756 498 memset(ctx->subinputs, 0, ctx->n_subinputs * sizeof(input));
adamc@756 499 ctx->used_subinputs = 0;
adamc@144 500 }
adamc@144 501
adamc@311 502 void uw_db_init(uw_context);
adamc@311 503 void uw_handle(uw_context, char *);
adamc@167 504
adamc@311 505 failure_kind uw_begin_init(uw_context ctx) {
adamc@272 506 int r = setjmp(ctx->jmp_buf);
adamc@272 507
adamc@272 508 if (r == 0)
adamc@311 509 uw_db_init(ctx);
adamc@272 510
adamc@272 511 return r;
adamc@272 512 }
adamc@272 513
adamc@854 514 void uw_set_headers(uw_context ctx, char *(*get_header)(void *, const char *), void *get_header_data) {
adamc@854 515 ctx->get_header = get_header;
adamc@854 516 ctx->get_header_data = get_header_data;
adamc@742 517 }
adamc@742 518
adamc@671 519 int uw_db_begin(uw_context);
adamc@167 520
adamc@801 521 static void uw_set_error(uw_context ctx, const char *fmt, ...) {
adamc@801 522 va_list ap;
adamc@801 523 va_start(ap, fmt);
adamc@801 524
adamc@801 525 vsnprintf(ctx->error_message, ERROR_BUF_LEN, fmt, ap);
adamc@801 526 }
adamc@801 527
adamc@311 528 __attribute__((noreturn)) void uw_error(uw_context ctx, failure_kind fk, const char *fmt, ...) {
adamc@425 529 cleanup *cl;
adamc@425 530
adamc@167 531 va_list ap;
adamc@167 532 va_start(ap, fmt);
adamc@167 533
adamc@167 534 vsnprintf(ctx->error_message, ERROR_BUF_LEN, fmt, ap);
adamc@167 535
adamc@425 536 for (cl = ctx->cleanup; cl < ctx->cleanup_front; ++cl)
adamc@425 537 cl->func(cl->arg);
adamc@425 538
adamc@425 539 ctx->cleanup_front = ctx->cleanup;
adamc@425 540
adamc@190 541 longjmp(ctx->jmp_buf, fk);
adamc@167 542 }
adamc@167 543
adamc@425 544 void uw_push_cleanup(uw_context ctx, void (*func)(void *), void *arg) {
adamc@425 545 if (ctx->cleanup_front >= ctx->cleanup_back) {
adamc@425 546 int len = ctx->cleanup_back - ctx->cleanup, newLen;
adamc@425 547 if (len == 0)
adamc@425 548 newLen = 1;
adamc@425 549 else
adamc@428 550 newLen = len * 2;
adamc@470 551 ctx->cleanup = realloc(ctx->cleanup, newLen * sizeof(cleanup));
adamc@425 552 ctx->cleanup_front = ctx->cleanup + len;
adamc@425 553 ctx->cleanup_back = ctx->cleanup + newLen;
adamc@425 554 }
adamc@425 555
adamc@425 556 ctx->cleanup_front->func = func;
adamc@425 557 ctx->cleanup_front->arg = arg;
adamc@425 558 ++ctx->cleanup_front;
adamc@425 559 }
adamc@425 560
adamc@682 561 uw_Basis_string uw_Basis_requestHeader(uw_context ctx, uw_Basis_string h) {
adamc@854 562 return ctx->get_header(ctx->get_header_data, h);
adamc@682 563 }
adamc@682 564
adamc@682 565 void uw_login(uw_context ctx) {
adamc@693 566 if (ctx->needs_push) {
adamc@682 567 char *id_s, *pass_s;
adamc@682 568
adamc@682 569 if ((id_s = uw_Basis_requestHeader(ctx, "UrWeb-Client"))
adamc@682 570 && (pass_s = uw_Basis_requestHeader(ctx, "UrWeb-Pass"))) {
adamc@682 571 unsigned id = atoi(id_s);
adamc@682 572 int pass = atoi(pass_s);
adamc@682 573 client *c = find_client(id);
adamc@682 574
adamc@682 575 if (c == NULL)
adamc@682 576 uw_error(ctx, FATAL, "Unknown client ID in HTTP headers (%s, %s)", id_s, pass_s);
adamc@682 577 else {
adamc@685 578 use_client(c);
adamc@682 579 ctx->client = c;
adamc@682 580
adamc@682 581 if (c->mode != USED)
adamc@682 582 uw_error(ctx, FATAL, "Stale client ID (%u) in subscription request", id);
adamc@682 583 if (c->pass != pass)
adamc@682 584 uw_error(ctx, FATAL, "Wrong client password (%u, %d) in subscription request", id, pass);
adamc@682 585 }
adamc@682 586 } else {
adamc@682 587 client *c = new_client();
adamc@685 588 use_client(c);
adamc@682 589 ctx->client = c;
adamc@864 590 }
adamc@682 591 }
adamc@682 592 }
adamc@682 593
adamc@671 594 failure_kind uw_begin(uw_context ctx, char *path) {
adamc@671 595 int r = setjmp(ctx->jmp_buf);
adamc@671 596
adamc@671 597 if (r == 0) {
adamc@671 598 if (uw_db_begin(ctx))
adamc@671 599 uw_error(ctx, BOUNDED_RETRY, "Error running SQL BEGIN");
adamc@682 600
adamc@671 601 uw_handle(ctx, path);
adamc@671 602 }
adamc@671 603
adamc@671 604 return r;
adamc@671 605 }
adamc@671 606
adamc@772 607 uw_Basis_client uw_Basis_self(uw_context ctx) {
adamc@682 608 if (ctx->client == NULL)
adamc@682 609 uw_error(ctx, FATAL, "Call to Basis.self() from page that has only server-side code");
adamc@682 610
adamc@682 611 return ctx->client->id;
adamc@682 612 }
adamc@682 613
adamc@425 614 void uw_pop_cleanup(uw_context ctx) {
adamc@425 615 if (ctx->cleanup_front == ctx->cleanup)
adamc@425 616 uw_error(ctx, FATAL, "Attempt to pop from empty cleanup action stack");
adamc@425 617
adamc@425 618 --ctx->cleanup_front;
adamc@425 619 ctx->cleanup_front->func(ctx->cleanup_front->arg);
adamc@425 620 }
adamc@425 621
adamc@311 622 char *uw_error_message(uw_context ctx) {
adamc@167 623 return ctx->error_message;
adamc@167 624 }
adamc@167 625
adamc@737 626 extern int uw_input_num(const char*);
adamc@144 627
adamc@759 628 static input *INP(uw_context ctx) {
adamc@759 629 if (ctx->cur_container == NULL)
adamc@759 630 return ctx->inputs;
adamc@759 631 else if (ctx->cur_container->kind == SUBFORM)
adamc@759 632 return ctx->cur_container->data.subform.fields;
adamc@759 633 else if (ctx->cur_container->kind == ENTRY)
adamc@759 634 return ctx->cur_container->data.entry.fields;
adamc@759 635 else
adamc@760 636 uw_error(ctx, FATAL, "INP: Wrong kind (%d, %p)", ctx->cur_container->kind, ctx->cur_container);
adamc@759 637 }
adamc@759 638
adamc@760 639 static void adjust_pointer(input **ptr, input *old_start, input *new_start, size_t len) {
adamc@760 640 if (*ptr != NULL && *ptr >= old_start && *ptr < old_start + len)
adamc@760 641 *ptr += new_start - old_start;
adamc@760 642 }
adamc@760 643
adamc@760 644 static void adjust_input(input *x, input *old_start, input *new_start, size_t len) {
adamc@759 645 switch (x->kind) {
adamc@759 646 case SUBFORM:
adamc@760 647 adjust_pointer(&x->data.subform.fields, old_start, new_start, len);
adamc@760 648 adjust_pointer(&x->data.subform.parent, old_start, new_start, len);
adamc@759 649 break;
adamc@759 650 case SUBFORMS:
adamc@760 651 adjust_pointer(&x->data.subforms.entries, old_start, new_start, len);
adamc@760 652 adjust_pointer(&x->data.subforms.parent, old_start, new_start, len);
adamc@759 653 break;
adamc@759 654 case ENTRY:
adamc@760 655 adjust_pointer(&x->data.entry.fields, old_start, new_start, len);
adamc@760 656 adjust_pointer(&x->data.entry.next, old_start, new_start, len);
adamc@760 657 adjust_pointer(&x->data.entry.parent, old_start, new_start, len);
adamc@759 658 }
adamc@759 659 }
adamc@759 660
adamc@759 661 static input *check_input_space(uw_context ctx, size_t len) {
adamc@759 662 size_t i;
adamc@759 663 input *r;
adamc@759 664
adamc@759 665 if (ctx->used_subinputs + len >= ctx->n_subinputs) {
adamc@759 666 input *new_subinputs = realloc(ctx->subinputs, sizeof(input) * (ctx->used_subinputs + len));
adamc@759 667 size_t offset = new_subinputs - ctx->subinputs;
adamc@759 668
adamc@760 669 if (ctx->subinputs != new_subinputs) {
adamc@760 670 for (i = 0; i < ctx->used_subinputs; ++i)
adamc@760 671 adjust_input(&new_subinputs[i], ctx->subinputs, new_subinputs, ctx->used_subinputs);
adamc@760 672 for (i = 0; i < uw_inputs_len; ++i)
adamc@760 673 adjust_input(&ctx->inputs[i], ctx->subinputs, new_subinputs, ctx->used_subinputs);
adamc@760 674
adamc@760 675 adjust_pointer(&ctx->cur_container, ctx->subinputs, new_subinputs, ctx->used_subinputs);
adamc@760 676
adamc@760 677 ctx->n_subinputs = ctx->used_subinputs + len;
adamc@760 678 ctx->subinputs = new_subinputs;
adamc@760 679 }
adamc@759 680 }
adamc@759 681
adamc@759 682 r = &ctx->subinputs[ctx->used_subinputs];
adamc@759 683
adamc@759 684 for (i = 0; i < len; ++i)
adamc@759 685 ctx->subinputs[ctx->used_subinputs++].kind = UNUSED;
adamc@759 686
adamc@759 687 return r;
adamc@759 688 }
adamc@756 689
adamc@801 690 int uw_set_input(uw_context ctx, const char *name, char *value) {
adamc@1050 691 //printf("Input name %s\n", name);
adamc@1038 692
adamc@756 693 if (!strcasecmp(name, ".b")) {
adamc@756 694 int n = uw_input_num(value);
adamc@756 695 input *inps;
adamc@756 696
adamc@801 697 if (n < 0) {
adamc@801 698 uw_set_error(ctx, "Bad subform name %s", value);
adamc@801 699 return -1;
adamc@801 700 }
adamc@801 701
adamc@801 702 if (n >= uw_inputs_len) {
adamc@801 703 uw_set_error(ctx, "For subform name %s, index %d is out of range", value, n);
adamc@801 704 return -1;
adamc@801 705 }
adamc@756 706
adamc@759 707 inps = check_input_space(ctx, uw_inputs_len);
adamc@759 708
adamc@759 709 INP(ctx)[n].kind = SUBFORM;
adamc@759 710 INP(ctx)[n].data.subform.parent = ctx->cur_container;
adamc@759 711 INP(ctx)[n].data.subform.fields = inps;
adamc@759 712 ctx->cur_container = &INP(ctx)[n];
adamc@756 713 } else if (!strcasecmp(name, ".e")) {
adamc@756 714 input *tmp;
adamc@756 715
adamc@801 716 if (ctx->cur_container == NULL) {
adamc@801 717 uw_set_error(ctx, "Unmatched subform closer");
adamc@801 718 return -1;
adamc@801 719 }
adamc@756 720
adamc@759 721 tmp = ctx->cur_container;
adamc@759 722 switch (tmp->kind) {
adamc@759 723 case SUBFORM:
adamc@759 724 ctx->cur_container = tmp->data.subform.parent;
adamc@759 725 tmp->data.subform.parent = NULL;
adamc@759 726 break;
adamc@759 727 case SUBFORMS:
adamc@759 728 ctx->cur_container = tmp->data.subforms.parent;
adamc@759 729 tmp->data.subforms.parent = NULL;
adamc@759 730 break;
adamc@759 731 case ENTRY:
adamc@759 732 ctx->cur_container = tmp->data.entry.parent;
adamc@759 733 break;
adamc@759 734 default:
adamc@801 735 uw_set_error(ctx, "uw_set_input: Wrong kind");
adamc@801 736 return -1;
adamc@759 737 }
adamc@759 738 } else if (!strcasecmp(name, ".s")) {
adamc@759 739 int n = uw_input_num(value);
adamc@759 740
adamc@801 741 if (n < 0) {
adamc@801 742 uw_set_error(ctx, "Bad subforms name %s", value);
adamc@801 743 return -1;
adamc@801 744 }
adamc@801 745
adamc@801 746 if (n >= uw_inputs_len) {
adamc@801 747 uw_set_error(ctx, "For subforms name %s, index %d is out of range", value, n);
adamc@801 748 return -1;
adamc@801 749 }
adamc@759 750
adamc@759 751 INP(ctx)[n].kind = SUBFORMS;
adamc@759 752 INP(ctx)[n].data.subforms.parent = ctx->cur_container;
adamc@759 753 INP(ctx)[n].data.subforms.entries = NULL;
adamc@759 754 ctx->cur_container = &INP(ctx)[n];
adamc@759 755 } else if (!strcasecmp(name, ".i")) {
adamc@759 756 input *inps;
adamc@759 757
adamc@801 758 if (!ctx->cur_container) {
adamc@801 759 uw_set_error(ctx, "New entry without container");
adamc@801 760 return -1;
adamc@801 761 }
adamc@801 762
adamc@801 763 if (ctx->cur_container->kind != SUBFORMS) {
adamc@801 764 uw_set_error(ctx, "Bad kind for entry parent");
adamc@801 765 return -1;
adamc@801 766 }
adamc@759 767
adamc@759 768 inps = check_input_space(ctx, uw_inputs_len + 1);
adamc@759 769
adamc@759 770 inps->kind = ENTRY;
adamc@759 771 inps->data.entry.parent = ctx->cur_container;
adamc@759 772 inps->data.entry.next = ctx->cur_container->data.subforms.entries;
adamc@759 773 ctx->cur_container->data.subforms.entries = inps;
adamc@759 774
adamc@759 775 inps->data.entry.fields = inps+1;
adamc@759 776 ctx->cur_container = inps;
adamc@756 777 } else {
adamc@756 778 int n = uw_input_num(name);
adamc@756 779
adamc@801 780 if (n < 0) {
adamc@1038 781 if (!strcmp(name, "null"))
adamc@1038 782 return 0;
adamc@801 783 uw_set_error(ctx, "Bad input name %s", name);
adamc@801 784 return -1;
adamc@801 785 }
adamc@801 786
adamc@801 787 if (n >= uw_inputs_len) {
adamc@801 788 uw_set_error(ctx, "For input name %s, index %d is out of range", name, n);
adamc@801 789 return -1;
adamc@801 790 }
adamc@756 791
adamc@756 792 INP(ctx)[n].kind = NORMAL;
adamc@756 793 INP(ctx)[n].data.normal = value;
adamc@756 794 }
adamc@801 795
adamc@801 796 return 0;
adamc@144 797 }
adamc@144 798
adamc@311 799 char *uw_get_input(uw_context ctx, int n) {
adamc@169 800 if (n < 0)
adamc@311 801 uw_error(ctx, FATAL, "Negative input index %d", n);
adamc@311 802 if (n >= uw_inputs_len)
adamc@311 803 uw_error(ctx, FATAL, "Out-of-bounds input index %d", n);
adamc@737 804
adamc@756 805 switch (INP(ctx)[n].kind) {
adamc@737 806 case UNSET:
adamc@737 807 return NULL;
adamc@739 808 case FIL:
adamc@739 809 uw_error(ctx, FATAL, "Tried to read a file form input as normal");
adamc@756 810 case SUBFORM:
adamc@756 811 uw_error(ctx, FATAL, "Tried to read a subform form input as normal");
adamc@759 812 case SUBFORMS:
adamc@759 813 uw_error(ctx, FATAL, "Tried to read a subforms form input as normal");
adamc@759 814 case ENTRY:
adamc@759 815 uw_error(ctx, FATAL, "Tried to read an entry form input as normal");
adamc@737 816 case NORMAL:
adamc@756 817 return INP(ctx)[n].data.normal;
adamc@737 818 default:
adamc@737 819 uw_error(ctx, FATAL, "Impossible input kind");
adamc@737 820 }
adamc@138 821 }
adamc@138 822
adamc@311 823 char *uw_get_optional_input(uw_context ctx, int n) {
adamc@190 824 if (n < 0)
adamc@311 825 uw_error(ctx, FATAL, "Negative input index %d", n);
adamc@311 826 if (n >= uw_inputs_len)
adamc@311 827 uw_error(ctx, FATAL, "Out-of-bounds input index %d", n);
adamc@737 828
adamc@756 829 switch (INP(ctx)[n].kind) {
adamc@737 830 case UNSET:
adamc@737 831 return "";
adamc@739 832 case FIL:
adamc@739 833 uw_error(ctx, FATAL, "Tried to read a file form input as normal");
adamc@756 834 case SUBFORM:
adamc@756 835 uw_error(ctx, FATAL, "Tried to read a subform form input as normal");
adamc@759 836 case SUBFORMS:
adamc@759 837 uw_error(ctx, FATAL, "Tried to read a subforms form input as normal");
adamc@759 838 case ENTRY:
adamc@759 839 uw_error(ctx, FATAL, "Tried to read an entry form input as normal");
adamc@737 840 case NORMAL:
adamc@756 841 return INP(ctx)[n].data.normal;
adamc@737 842 default:
adamc@737 843 uw_error(ctx, FATAL, "Impossible input kind");
adamc@737 844 }
adamc@737 845 }
adamc@737 846
adamc@801 847 int uw_set_file_input(uw_context ctx, const char *name, uw_Basis_file f) {
adamc@737 848 int n = uw_input_num(name);
adamc@737 849
adamc@801 850 if (n < 0) {
adamc@801 851 uw_set_error(ctx, "Bad file input name %s", name);
adamc@801 852 return -1;
adamc@801 853 }
adamc@801 854
adamc@801 855 if (n >= uw_inputs_len) {
adamc@801 856 uw_set_error(ctx, "For file input name %s, index %d is out of range", name, n);
adamc@801 857 return -1;
adamc@801 858 }
adamc@737 859
adamc@739 860 ctx->inputs[n].kind = FIL;
adamc@739 861 ctx->inputs[n].data.file = f;
adamc@801 862
adamc@801 863 return 0;
adamc@737 864 }
adamc@737 865
adamc@739 866 void *uw_malloc(uw_context ctx, size_t len);
adamc@739 867
adamc@760 868
adamc@760 869 static void parents(input *inp) {
adamc@760 870 printf("Stack: %p\n", inp);
adamc@760 871 while (inp) {
adamc@760 872 switch (inp->kind) {
adamc@760 873 case NORMAL:
adamc@760 874 printf("Normal(%p)\n", inp);
adamc@760 875 break;
adamc@760 876 case FIL:
adamc@760 877 printf("File(%p)\n", inp);
adamc@760 878 break;
adamc@760 879 case SUBFORM:
adamc@760 880 printf("Subform; fields = %p\n", inp->data.subform.fields);
adamc@760 881 inp = inp->data.subform.parent;
adamc@760 882 break;
adamc@760 883 case SUBFORMS:
adamc@760 884 printf("Subforms; entries = %p\n", inp->data.subforms.entries);
adamc@760 885 inp = inp->data.subforms.parent;
adamc@760 886 break;
adamc@760 887 case ENTRY:
adamc@760 888 printf("Entry; fields = %p; next = %p\n", inp->data.entry.fields, inp->data.entry.next);
adamc@760 889 inp = inp->data.entry.parent;
adamc@760 890 break;
adamc@760 891 default:
adamc@760 892 inp = NULL;
adamc@760 893 }
adamc@760 894 }
adamc@760 895 }
adamc@760 896
adamc@739 897 uw_Basis_file uw_get_file_input(uw_context ctx, int n) {
adamc@737 898 if (n < 0)
adamc@737 899 uw_error(ctx, FATAL, "Negative file input index %d", n);
adamc@737 900 if (n >= uw_inputs_len)
adamc@737 901 uw_error(ctx, FATAL, "Out-of-bounds file input index %d", n);
adamc@737 902
adamc@756 903 switch (INP(ctx)[n].kind) {
adamc@737 904 case UNSET:
adamc@737 905 {
adamc@739 906 char *data = uw_malloc(ctx, 0);
adamc@740 907 uw_Basis_file f = {NULL, "", {0, data}};
adamc@739 908 return f;
adamc@737 909 }
adamc@739 910 case FIL:
adamc@756 911 return INP(ctx)[n].data.file;
adamc@737 912 case NORMAL:
adamc@737 913 uw_error(ctx, FATAL, "Tried to read a normal form input as files");
adamc@756 914 case SUBFORM:
adamc@756 915 uw_error(ctx, FATAL, "Tried to read a subform form input as files");
adamc@759 916 case SUBFORMS:
adamc@759 917 uw_error(ctx, FATAL, "Tried to read a subforms form input as files");
adamc@759 918 case ENTRY:
adamc@759 919 uw_error(ctx, FATAL, "Tried to read an entry form input as files");
adamc@737 920 default:
adamc@737 921 uw_error(ctx, FATAL, "Impossible input kind");
adamc@737 922 }
adamc@190 923 }
adamc@190 924
adamc@756 925 void uw_enter_subform(uw_context ctx, int n) {
adamc@756 926 if (n < 0)
adamc@756 927 uw_error(ctx, FATAL, "Negative subform index %d", n);
adamc@756 928 if (n >= uw_inputs_len)
adamc@756 929 uw_error(ctx, FATAL, "Out-of-bounds subform index %d", n);
adamc@756 930
adamc@756 931 switch (INP(ctx)[n].kind) {
adamc@756 932 case UNSET:
adamc@756 933 uw_error(ctx, FATAL, "Missing subform");
adamc@756 934 case FIL:
adamc@756 935 uw_error(ctx, FATAL, "Tried to read a file form input as subform");
adamc@756 936 case NORMAL:
adamc@756 937 uw_error(ctx, FATAL, "Tried to read a normal form input as subform");
adamc@759 938 case SUBFORMS:
adamc@759 939 uw_error(ctx, FATAL, "Tried to read a subforms form input as subform");
adamc@759 940 case ENTRY:
adamc@759 941 uw_error(ctx, FATAL, "Tried to read an entry form input as subform");
adamc@756 942 case SUBFORM:
adamc@759 943 INP(ctx)[n].data.subform.parent = ctx->cur_container;
adamc@760 944 ctx->cur_container = &INP(ctx)[n];
adamc@756 945 return;
adamc@756 946 default:
adamc@756 947 uw_error(ctx, FATAL, "Impossible input kind");
adamc@756 948 }
adamc@756 949 }
adamc@756 950
adamc@756 951 void uw_leave_subform(uw_context ctx) {
adamc@756 952 input *tmp;
adamc@756 953
adamc@759 954 if (ctx->cur_container == NULL)
adamc@756 955 uw_error(ctx, FATAL, "Unmatched uw_leave_subform");
adamc@756 956
adamc@759 957 tmp = ctx->cur_container;
adamc@759 958 ctx->cur_container = tmp->data.subform.parent;
adamc@759 959 tmp->data.subform.parent = NULL;
adamc@759 960 }
adamc@759 961
adamc@759 962 int uw_enter_subforms(uw_context ctx, int n) {
adamc@759 963 input *inps;
adamc@759 964
adamc@759 965 if (n < 0)
adamc@759 966 uw_error(ctx, FATAL, "Negative subforms index %d", n);
adamc@759 967 if (n >= uw_inputs_len)
adamc@759 968 uw_error(ctx, FATAL, "Out-of-bounds subforms index %d", n);
adamc@759 969
adamc@759 970 switch (INP(ctx)[n].kind) {
adamc@759 971 case UNSET:
adamc@759 972 uw_error(ctx, FATAL, "Missing subforms");
adamc@759 973 case FIL:
adamc@759 974 uw_error(ctx, FATAL, "Tried to read a file form input as subforms");
adamc@759 975 case NORMAL:
adamc@759 976 uw_error(ctx, FATAL, "Tried to read a normal form input %p as subforms", &INP(ctx)[n]);
adamc@759 977 case SUBFORM:
adamc@759 978 uw_error(ctx, FATAL, "Tried to read a subform form input as subforms");
adamc@759 979 case ENTRY:
adamc@759 980 uw_error(ctx, FATAL, "Tried to read an entry form input as subforms");
adamc@759 981 case SUBFORMS:
adamc@759 982 inps = INP(ctx)[n].data.subforms.entries;
adamc@759 983 if (inps) {
adamc@759 984 INP(ctx)[n].data.subforms.parent = ctx->cur_container;
adamc@759 985 ctx->cur_container = INP(ctx)[n].data.subforms.entries;
adamc@759 986 return 1;
adamc@759 987 } else
adamc@759 988 return 0;
adamc@759 989 default:
adamc@759 990 uw_error(ctx, FATAL, "Impossible input kind");
adamc@759 991 }
adamc@759 992 }
adamc@759 993
adamc@759 994 int uw_next_entry(uw_context ctx) {
adamc@759 995 if (ctx->cur_container == NULL)
adamc@759 996 uw_error(ctx, FATAL, "uw_next_entry(NULL)");
adamc@759 997
adamc@759 998 switch (ctx->cur_container->kind) {
adamc@759 999 case UNSET:
adamc@759 1000 uw_error(ctx, FATAL, "Missing entry");
adamc@759 1001 case FIL:
adamc@759 1002 uw_error(ctx, FATAL, "Tried to read a file form input as entry");
adamc@759 1003 case NORMAL:
adamc@759 1004 uw_error(ctx, FATAL, "Tried to read a normal form input as entry");
adamc@759 1005 case SUBFORM:
adamc@759 1006 uw_error(ctx, FATAL, "Tried to read a subform form input as entry");
adamc@759 1007 case SUBFORMS:
adamc@759 1008 uw_error(ctx, FATAL, "Tried to read a subforms form input as entry");
adamc@759 1009 case ENTRY:
adamc@759 1010 if (ctx->cur_container->data.entry.next) {
adamc@759 1011 ctx->cur_container = ctx->cur_container->data.entry.next;
adamc@759 1012 return 1;
adamc@759 1013 } else {
adamc@759 1014 ctx->cur_container = ctx->cur_container->data.entry.parent->data.subforms.parent;
adamc@759 1015 return 0;
adamc@759 1016 }
adamc@759 1017 default:
adamc@759 1018 uw_error(ctx, FATAL, "Impossible input kind");
adamc@759 1019 }
adamc@756 1020 }
adamc@756 1021
adamc@643 1022 void uw_set_script_header(uw_context ctx, const char *s) {
adamc@643 1023 ctx->script_header = s;
adamc@643 1024 }
adamc@643 1025
adamc@667 1026 void uw_set_url_prefix(uw_context ctx, const char *s) {
adamc@667 1027 ctx->url_prefix = s;
adamc@667 1028 }
adamc@667 1029
adamc@693 1030 void uw_set_needs_push(uw_context ctx, int n) {
adamc@693 1031 ctx->needs_push = n;
adamc@693 1032 }
adamc@693 1033
adamc@736 1034 void uw_set_needs_sig(uw_context ctx, int n) {
adamc@736 1035 ctx->needs_sig = n;
adamc@736 1036 }
adamc@736 1037
adamc@667 1038
adamc@667 1039 static void buf_check_ctx(uw_context ctx, buf *b, size_t extra, const char *desc) {
adamc@666 1040 if (b->back - b->front < extra) {
adamc@666 1041 size_t desired = b->front - b->start + extra, next;
adamc@136 1042 char *new_heap;
adamc@136 1043
adamc@666 1044 next = b->back - b->start;
adamc@317 1045 if (next == 0)
adamc@317 1046 next = 1;
adamc@317 1047 for (; next < desired; next *= 2);
adamc@136 1048
adamc@666 1049 new_heap = realloc(b->start, next);
adamc@666 1050 b->front = new_heap + (b->front - b->start);
adamc@666 1051 b->back = new_heap + next;
adamc@136 1052
adamc@667 1053 if (new_heap != b->start) {
adamc@666 1054 b->start = new_heap;
adamc@666 1055 uw_error(ctx, UNLIMITED_RETRY, "Couldn't allocate new %s contiguously", desc);
adamc@136 1056 }
adamc@136 1057
adamc@666 1058 b->start = new_heap;
adamc@136 1059 }
adamc@136 1060 }
adamc@136 1061
adamc@832 1062 void uw_check_heap(uw_context ctx, size_t extra) {
adamc@667 1063 buf_check_ctx(ctx, &ctx->heap, extra, "heap chunk");
adamc@666 1064 }
adamc@666 1065
adamc@832 1066 char *uw_heap_front(uw_context ctx) {
adamc@832 1067 return ctx->heap.front;
adamc@832 1068 }
adamc@832 1069
adamc@832 1070 void uw_set_heap_front(uw_context ctx, char *fr) {
adamc@832 1071 ctx->heap.front = fr;
adamc@832 1072 }
adamc@832 1073
adamc@311 1074 void *uw_malloc(uw_context ctx, size_t len) {
adamc@136 1075 void *result;
adamc@136 1076
adamc@311 1077 uw_check_heap(ctx, len);
adamc@136 1078
adamc@666 1079 result = ctx->heap.front;
adamc@666 1080 ctx->heap.front += len;
adamc@136 1081 return result;
adamc@117 1082 }
adamc@117 1083
adamc@323 1084 void uw_begin_region(uw_context ctx) {
adamc@666 1085 regions *r = (regions *) ctx->heap.front;
adamc@323 1086
adamc@323 1087 uw_check_heap(ctx, sizeof(regions));
adamc@323 1088
adamc@666 1089 ctx->heap.front += sizeof(regions);
adamc@323 1090
adamc@323 1091 r->next = ctx->regions;
adamc@323 1092 ctx->regions = r;
adamc@323 1093 }
adamc@323 1094
adamc@323 1095 void uw_end_region(uw_context ctx) {
adamc@323 1096 regions *r = ctx->regions;
adamc@323 1097
adamc@323 1098 if (r == NULL)
adamc@323 1099 uw_error(ctx, FATAL, "Region stack underflow");
adamc@323 1100
adamc@666 1101 ctx->heap.front = (char *) r;
adamc@323 1102 ctx->regions = r->next;
adamc@323 1103 }
adamc@323 1104
adamc@324 1105 void uw_memstats(uw_context ctx) {
adamc@666 1106 printf("Headers: %d/%d\n", buf_used(&ctx->outHeaders), buf_avail(&ctx->outHeaders));
adamc@666 1107 printf("Script: %d/%d\n", buf_used(&ctx->script), buf_avail(&ctx->script));
adamc@666 1108 printf("Page: %d/%d\n", buf_used(&ctx->page), buf_avail(&ctx->page));
adamc@666 1109 printf("Heap: %d/%d\n", buf_used(&ctx->heap), buf_avail(&ctx->heap));
adamc@324 1110 }
adamc@324 1111
adamc@311 1112 int uw_send(uw_context ctx, int sock) {
adamc@666 1113 int n = uw_really_send(sock, ctx->outHeaders.start, ctx->outHeaders.front - ctx->outHeaders.start);
adamc@462 1114
adamc@462 1115 if (n < 0)
adamc@462 1116 return n;
adamc@462 1117
adamc@462 1118 n = uw_really_send(sock, "\r\n", 2);
adamc@462 1119
adamc@462 1120 if (n < 0)
adamc@462 1121 return n;
adamc@462 1122
adamc@666 1123 return uw_really_send(sock, ctx->page.start, ctx->page.front - ctx->page.start);
adamc@462 1124 }
adamc@462 1125
adamc@856 1126 int uw_print(uw_context ctx, int fd) {
adamc@856 1127 int n = uw_really_write(fd, ctx->outHeaders.start, ctx->outHeaders.front - ctx->outHeaders.start);
adamc@856 1128
adamc@856 1129 if (n < 0)
adamc@856 1130 return n;
adamc@856 1131
adamc@856 1132 n = uw_really_write(fd, "\r\n", 2);
adamc@856 1133
adamc@856 1134 if (n < 0)
adamc@856 1135 return n;
adamc@856 1136
adamc@856 1137 return uw_really_write(fd, ctx->page.start, ctx->page.front - ctx->page.start);
adamc@856 1138 }
adamc@856 1139
adamc@860 1140 int uw_output(uw_context ctx, int (*output)(void *data, char *buf, size_t len), void *data) {
adamc@860 1141 int n = output(data, ctx->outHeaders.start, ctx->outHeaders.front - ctx->outHeaders.start);
adamc@860 1142
adamc@860 1143 if (n < 0)
adamc@860 1144 return n;
adamc@860 1145
adamc@860 1146 n = output(data, "\r\n", 2);
adamc@860 1147
adamc@860 1148 if (n < 0)
adamc@860 1149 return n;
adamc@860 1150
adamc@860 1151 return output(data, ctx->page.start, ctx->page.front - ctx->page.start);
adamc@860 1152 }
adamc@860 1153
adamc@462 1154 static void uw_check_headers(uw_context ctx, size_t extra) {
adamc@667 1155 buf_check(&ctx->outHeaders, extra);
adamc@462 1156 }
adamc@462 1157
adamc@462 1158 void uw_write_header(uw_context ctx, uw_Basis_string s) {
adamc@462 1159 int len = strlen(s);
adamc@462 1160
adamc@462 1161 uw_check_headers(ctx, len + 1);
adamc@666 1162 strcpy(ctx->outHeaders.front, s);
adamc@666 1163 ctx->outHeaders.front += len;
adamc@562 1164 }
adamc@562 1165
adamc@562 1166 static void uw_check_script(uw_context ctx, size_t extra) {
adamc@667 1167 buf_check(&ctx->script, extra);
adamc@562 1168 }
adamc@562 1169
adamc@562 1170 void uw_write_script(uw_context ctx, uw_Basis_string s) {
adamc@562 1171 int len = strlen(s);
adamc@562 1172
adamc@562 1173 uw_check_script(ctx, len + 1);
adamc@666 1174 strcpy(ctx->script.front, s);
adamc@666 1175 ctx->script.front += len;
adamc@562 1176 }
adamc@562 1177
adamc@645 1178 const char *uw_Basis_get_script(uw_context ctx, uw_unit u) {
adamc@916 1179 return "<sc>";
adamc@565 1180 }
adamc@565 1181
adamc@695 1182 uw_Basis_string uw_Basis_maybe_onload(uw_context ctx, uw_Basis_string s) {
adamc@695 1183 if (s[0] == 0)
adamc@695 1184 return "";
adamc@695 1185 else {
adamc@695 1186 char *r = uw_malloc(ctx, 11 + strlen(s));
adamc@695 1187 sprintf(r, " onload='%s'", s);
adamc@695 1188 return r;
adamc@695 1189 }
adamc@695 1190 }
adamc@695 1191
adamc@1042 1192 uw_Basis_string uw_Basis_maybe_onunload(uw_context ctx, uw_Basis_string s) {
adamc@1042 1193 if (ctx->script_header[0] == 0)
adamc@1042 1194 return "";
adamc@1042 1195 else {
adamc@1042 1196 char *r = uw_malloc(ctx, 22 + strlen(s));
adamc@1042 1197 sprintf(r, " onunload='unload();%s'", s);
adamc@1042 1198 return r;
adamc@1042 1199 }
adamc@1042 1200 }
adamc@1042 1201
adamc@736 1202 extern uw_Basis_string uw_cookie_sig(uw_context);
adamc@736 1203
adamc@694 1204 const char *uw_Basis_get_settings(uw_context ctx, uw_unit u) {
adamc@736 1205 if (ctx->client == NULL) {
adamc@736 1206 if (ctx->needs_sig) {
adamc@736 1207 char *sig = uw_cookie_sig(ctx);
adamc@736 1208 char *r = uw_malloc(ctx, strlen(sig) + 8);
adamc@736 1209 sprintf(r, "sig=\"%s\";", sig);
adamc@736 1210 return r;
adamc@736 1211 }
adamc@736 1212 else
adamc@736 1213 return "";
adamc@736 1214 } else {
adamc@736 1215 char *sig = ctx->needs_sig ? uw_cookie_sig(ctx) : "";
adamc@736 1216 char *r = uw_malloc(ctx, 59 + 3 * INTS_MAX + strlen(ctx->url_prefix)
adamc@736 1217 + (ctx->needs_sig ? strlen(sig) + 7 : 0));
adamc@736 1218 sprintf(r, "client_id=%u;client_pass=%d;url_prefix=\"%s\";timeout=%d;%s%s%slistener();",
adamc@682 1219 ctx->client->id,
adamc@682 1220 ctx->client->pass,
adamc@679 1221 ctx->url_prefix,
adamc@736 1222 ctx->timeout,
adamc@736 1223 ctx->needs_sig ? "sig=\"" : "",
adamc@736 1224 sig,
adamc@736 1225 ctx->needs_sig ? "\";" : "");
adamc@679 1226 return r;
adamc@668 1227 }
adamc@667 1228 }
adamc@667 1229
adamc@574 1230 uw_Basis_string uw_Basis_jsifyString(uw_context ctx, uw_Basis_string s) {
adamc@574 1231 char *r, *s2;
adamc@574 1232
adamc@574 1233 uw_check_heap(ctx, strlen(s) * 4 + 2);
adamc@574 1234
adamc@666 1235 r = s2 = ctx->heap.front;
adamc@574 1236 *s2++ = '"';
adamc@574 1237
adamc@574 1238 for (; *s; s++) {
adamc@574 1239 char c = *s;
adamc@574 1240
adamc@574 1241 switch (c) {
adamc@574 1242 case '"':
adamc@574 1243 strcpy(s2, "\\\"");
adamc@574 1244 s2 += 2;
adamc@574 1245 break;
adamc@1027 1246 case '\'':
adamc@1027 1247 strcpy(s2, "\\047");
adamc@1027 1248 s2 += 4;
adamc@1027 1249 break;
adamc@574 1250 case '\\':
adamc@574 1251 strcpy(s2, "\\\\");
adamc@574 1252 s2 += 2;
adamc@574 1253 break;
adamc@574 1254 default:
adamc@574 1255 if (isprint(c))
adamc@574 1256 *s2++ = c;
adamc@574 1257 else {
adamc@574 1258 sprintf(s2, "\\%3o", c);
adamc@574 1259 s2 += 4;
adamc@574 1260 }
adamc@574 1261 }
adamc@574 1262 }
adamc@574 1263
adamc@574 1264 strcpy(s2, "\"");
adamc@910 1265 ctx->heap.front = s2 + 2;
adamc@574 1266 return r;
adamc@574 1267 }
adamc@574 1268
adamc@1025 1269 uw_Basis_string uw_Basis_jsifyChar(uw_context ctx, uw_Basis_char c) {
adamc@1025 1270 char *r, *s2;
adamc@1025 1271
adamc@1025 1272 uw_check_heap(ctx, 6);
adamc@1025 1273
adamc@1025 1274 r = s2 = ctx->heap.front;
adamc@1025 1275 *s2++ = '"';
adamc@1025 1276
adamc@1025 1277 switch (c) {
adamc@1025 1278 case '"':
adamc@1025 1279 strcpy(s2, "\\\"");
adamc@1025 1280 s2 += 2;
adamc@1025 1281 break;
adamc@1027 1282 case '\'':
adamc@1027 1283 strcpy(s2, "\\047");
adamc@1027 1284 s2 += 4;
adamc@1027 1285 break;
adamc@1025 1286 case '\\':
adamc@1025 1287 strcpy(s2, "\\\\");
adamc@1025 1288 s2 += 2;
adamc@1025 1289 break;
adamc@1025 1290 default:
adamc@1025 1291 if (isprint(c))
adamc@1025 1292 *s2++ = c;
adamc@1025 1293 else {
adamc@1025 1294 sprintf(s2, "\\%3o", c);
adamc@1025 1295 s2 += 4;
adamc@1025 1296 }
adamc@1025 1297 }
adamc@1025 1298
adamc@1025 1299 strcpy(s2, "\"");
adamc@1025 1300 ctx->heap.front = s2 + 2;
adamc@1025 1301 return r;
adamc@1025 1302 }
adamc@1025 1303
adamc@574 1304 uw_Basis_string uw_Basis_jsifyString_ws(uw_context ctx, uw_Basis_string s) {
adamc@574 1305 char *r, *s2;
adamc@574 1306
adamc@574 1307 uw_check_script(ctx, strlen(s) * 4 + 2);
adamc@574 1308
adamc@666 1309 r = s2 = ctx->script.front;
adamc@574 1310 *s2++ = '"';
adamc@574 1311
adamc@574 1312 for (; *s; s++) {
adamc@574 1313 char c = *s;
adamc@574 1314
adamc@574 1315 switch (c) {
adamc@577 1316 case '\'':
adamc@1027 1317 strcpy(s2, "\\");
adamc@574 1318 s2 += 2;
adamc@574 1319 break;
adamc@574 1320 case '\\':
adamc@574 1321 strcpy(s2, "\\\\");
adamc@574 1322 s2 += 2;
adamc@574 1323 break;
adamc@574 1324 default:
adamc@574 1325 if (isprint(c))
adamc@574 1326 *s2++ = c;
adamc@574 1327 else {
adamc@574 1328 sprintf(s2, "\\%3o", c);
adamc@574 1329 s2 += 4;
adamc@574 1330 }
adamc@574 1331 }
adamc@574 1332 }
adamc@574 1333
adamc@574 1334 strcpy(s2, "\"");
adamc@666 1335 ctx->script.front = s2 + 1;
adamc@574 1336 return r;
adamc@574 1337 }
adamc@574 1338
adamc@682 1339 char *uw_Basis_jsifyChannel(uw_context ctx, uw_Basis_channel chn) {
adamc@682 1340 if (ctx->client == NULL || chn.cli != ctx->client->id)
adamc@682 1341 return "null";
adamc@682 1342 else {
adamc@682 1343 int len;
adamc@682 1344 char *r;
adamc@682 1345
adamc@682 1346 uw_check_heap(ctx, INTS_MAX + 1);
adamc@682 1347 r = ctx->heap.front;
adamc@682 1348 sprintf(r, "%u%n", chn.chn, &len);
adamc@682 1349 ctx->heap.front += len+1;
adamc@682 1350 return r;
adamc@682 1351 }
adamc@682 1352 }
adamc@682 1353
adamc@577 1354 uw_Basis_int uw_Basis_new_client_source(uw_context ctx, uw_Basis_string s) {
adamc@577 1355 int len;
adamc@577 1356 size_t s_len = strlen(s);
adamc@562 1357
adamc@970 1358 uw_check_script(ctx, 18 + INTS_MAX + s_len);
adamc@970 1359 sprintf(ctx->script.front, "var s%d=sc(exec(%n", ctx->source_count, &len);
adamc@666 1360 ctx->script.front += len;
adamc@666 1361 strcpy(ctx->script.front, s);
adamc@666 1362 ctx->script.front += s_len;
adamc@970 1363 strcpy(ctx->script.front, "));");
adamc@972 1364 ctx->script.front += 3;
adamc@562 1365
adamc@565 1366 return ctx->source_count++;
adamc@117 1367 }
adamc@117 1368
adamc@577 1369 uw_unit uw_Basis_set_client_source(uw_context ctx, uw_Basis_int n, uw_Basis_string s) {
adamc@577 1370 int len;
adamc@577 1371 size_t s_len = strlen(s);
adamc@577 1372
adamc@970 1373 uw_check_script(ctx, 12 + INTS_MAX + s_len);
adamc@970 1374 sprintf(ctx->script.front, "sv(s%d,exec(%n", (int)n, &len);
adamc@666 1375 ctx->script.front += len;
adamc@666 1376 strcpy(ctx->script.front, s);
adamc@666 1377 ctx->script.front += s_len;
adamc@970 1378 strcpy(ctx->script.front, "));");
adamc@972 1379 ctx->script.front += 3;
adamc@577 1380
adamc@577 1381 return uw_unit_v;
adamc@577 1382 }
adamc@577 1383
adamc@311 1384 static void uw_check(uw_context ctx, size_t extra) {
adamc@667 1385 buf_check(&ctx->page, extra);
adamc@117 1386 }
adamc@117 1387
adamc@311 1388 static void uw_writec_unsafe(uw_context ctx, char c) {
adamc@666 1389 *(ctx->page.front)++ = c;
adamc@117 1390 }
adamc@117 1391
adamc@311 1392 void uw_writec(uw_context ctx, char c) {
adamc@311 1393 uw_check(ctx, 1);
adamc@311 1394 uw_writec_unsafe(ctx, c);
adamc@117 1395 }
adamc@117 1396
adamc@311 1397 static void uw_write_unsafe(uw_context ctx, const char* s) {
adamc@117 1398 int len = strlen(s);
adamc@666 1399 memcpy(ctx->page.front, s, len);
adamc@666 1400 ctx->page.front += len;
adamc@117 1401 }
adamc@117 1402
adamc@311 1403 void uw_write(uw_context ctx, const char* s) {
adamc@311 1404 uw_check(ctx, strlen(s) + 1);
adamc@311 1405 uw_write_unsafe(ctx, s);
adamc@666 1406 *ctx->page.front = 0;
adamc@102 1407 }
adamc@106 1408
adamc@311 1409 char *uw_Basis_attrifyInt(uw_context ctx, uw_Basis_int n) {
adamc@136 1410 char *result;
adamc@136 1411 int len;
adamc@311 1412 uw_check_heap(ctx, INTS_MAX);
adamc@666 1413 result = ctx->heap.front;
adamc@276 1414 sprintf(result, "%lld%n", n, &len);
adamc@666 1415 ctx->heap.front += len+1;
adamc@136 1416 return result;
adamc@106 1417 }
adamc@106 1418
adamc@311 1419 char *uw_Basis_attrifyFloat(uw_context ctx, uw_Basis_float n) {
adamc@136 1420 char *result;
adamc@136 1421 int len;
adamc@311 1422 uw_check_heap(ctx, FLOATS_MAX);
adamc@666 1423 result = ctx->heap.front;
adamc@136 1424 sprintf(result, "%g%n", n, &len);
adamc@666 1425 ctx->heap.front += len+1;
adamc@136 1426 return result;
adamc@106 1427 }
adamc@106 1428
adamc@1054 1429 static int isCont(unsigned char ch) {
adamc@1054 1430 return ch / 64 == 2;
adamc@1054 1431 }
adamc@1054 1432
adamc@311 1433 char *uw_Basis_attrifyString(uw_context ctx, uw_Basis_string s) {
adamc@136 1434 int len = strlen(s);
adamc@136 1435 char *result, *p;
adamc@311 1436 uw_check_heap(ctx, len * 6 + 1);
adamc@136 1437
adamc@666 1438 result = p = ctx->heap.front;
adamc@136 1439
adamc@136 1440 for (; *s; s++) {
adamc@1054 1441 unsigned char c = *s;
adamc@136 1442
adamc@136 1443 if (c == '"') {
adamc@136 1444 strcpy(p, "&quot;");
adamc@136 1445 p += 6;
adamc@136 1446 } else if (c == '&') {
adamc@136 1447 strcpy(p, "&amp;");
adamc@136 1448 p += 5;
adamc@136 1449 }
adamc@1059 1450 else
adamc@136 1451 *p++ = c;
adamc@136 1452 }
adamc@136 1453
adamc@137 1454 *p++ = 0;
adamc@666 1455 ctx->heap.front = p;
adamc@136 1456 return result;
adamc@106 1457 }
adamc@106 1458
adamc@1011 1459 char *uw_Basis_attrifyChar(uw_context ctx, uw_Basis_char c) {
adamc@1011 1460 char *result, *p;
adamc@1011 1461 uw_check_heap(ctx, 7);
adamc@1011 1462
adamc@1011 1463 result = p = ctx->heap.front;
adamc@1011 1464
adamc@1011 1465 if (c == '"') {
adamc@1011 1466 strcpy(p, "&quot;");
adamc@1011 1467 p += 6;
adamc@1011 1468 } else if (c == '&') {
adamc@1011 1469 strcpy(p, "&amp;");
adamc@1011 1470 p += 5;
adamc@1011 1471 }
adamc@1059 1472 else
adamc@1011 1473 *p++ = c;
adamc@1011 1474
adamc@1011 1475 *p++ = 0;
adamc@1011 1476 ctx->heap.front = p;
adamc@1011 1477 return result;
adamc@1011 1478 }
adamc@1011 1479
adamc@721 1480 char *uw_Basis_attrifyCss_class(uw_context ctx, uw_Basis_css_class s) {
adamc@721 1481 return s;
adamc@721 1482 }
adamc@721 1483
adamc@311 1484 static void uw_Basis_attrifyInt_w_unsafe(uw_context ctx, uw_Basis_int n) {
adamc@117 1485 int len;
adamc@117 1486
adamc@666 1487 sprintf(ctx->page.front, "%lld%n", n, &len);
adamc@666 1488 ctx->page.front += len;
adamc@106 1489 }
adamc@106 1490
adamc@428 1491 uw_unit uw_Basis_attrifyInt_w(uw_context ctx, uw_Basis_int n) {
adamc@311 1492 uw_check(ctx, INTS_MAX);
adamc@311 1493 uw_Basis_attrifyInt_w_unsafe(ctx, n);
adamc@428 1494
adamc@428 1495 return uw_unit_v;
adamc@106 1496 }
adamc@106 1497
adamc@428 1498 uw_unit uw_Basis_attrifyFloat_w(uw_context ctx, uw_Basis_float n) {
adamc@117 1499 int len;
adamc@117 1500
adamc@311 1501 uw_check(ctx, FLOATS_MAX);
adamc@666 1502 sprintf(ctx->page.front, "%g%n", n, &len);
adamc@666 1503 ctx->page.front += len;
adamc@428 1504
adamc@428 1505 return uw_unit_v;
adamc@117 1506 }
adamc@117 1507
adamc@428 1508 uw_unit uw_Basis_attrifyString_w(uw_context ctx, uw_Basis_string s) {
adamc@311 1509 uw_check(ctx, strlen(s) * 6);
adamc@117 1510
adamc@106 1511 for (; *s; s++) {
adamc@1054 1512 unsigned char c = *s;
adamc@106 1513
adamc@106 1514 if (c == '"')
adamc@311 1515 uw_write_unsafe(ctx, "&quot;");
adamc@136 1516 else if (c == '&')
adamc@311 1517 uw_write_unsafe(ctx, "&amp;");
adamc@1059 1518 else
adamc@311 1519 uw_writec_unsafe(ctx, c);
adamc@106 1520 }
adamc@428 1521
adamc@428 1522 return uw_unit_v;
adamc@106 1523 }
adamc@120 1524
adamc@1011 1525 uw_unit uw_Basis_attrifyChar_w(uw_context ctx, uw_Basis_char c) {
adamc@1011 1526 uw_check(ctx, 6);
adamc@1011 1527
adamc@1011 1528 if (c == '"')
adamc@1011 1529 uw_write_unsafe(ctx, "&quot;");
adamc@1011 1530 else if (c == '&')
adamc@1011 1531 uw_write_unsafe(ctx, "&amp;");
adamc@1059 1532 else
adamc@1011 1533 uw_writec_unsafe(ctx, c);
adamc@1011 1534
adamc@1011 1535 return uw_unit_v;
adamc@1011 1536 }
adamc@1011 1537
adamc@120 1538
adamc@311 1539 char *uw_Basis_urlifyInt(uw_context ctx, uw_Basis_int n) {
adamc@137 1540 int len;
adamc@137 1541 char *r;
adamc@137 1542
adamc@311 1543 uw_check_heap(ctx, INTS_MAX);
adamc@666 1544 r = ctx->heap.front;
adamc@276 1545 sprintf(r, "%lld%n", n, &len);
adamc@666 1546 ctx->heap.front += len+1;
adamc@137 1547 return r;
adamc@120 1548 }
adamc@120 1549
adamc@682 1550 char *uw_Basis_urlifyChannel(uw_context ctx, uw_Basis_channel chn) {
adamc@682 1551 if (ctx->client == NULL || chn.cli != ctx->client->id)
adamc@682 1552 return "";
adamc@682 1553 else {
adamc@682 1554 int len;
adamc@682 1555 char *r;
adamc@679 1556
adamc@682 1557 uw_check_heap(ctx, INTS_MAX + 1);
adamc@682 1558 r = ctx->heap.front;
adamc@682 1559 sprintf(r, "%u%n", chn.chn, &len);
adamc@682 1560 ctx->heap.front += len+1;
adamc@682 1561 return r;
adamc@682 1562 }
adamc@679 1563 }
adamc@679 1564
adamc@311 1565 char *uw_Basis_urlifyFloat(uw_context ctx, uw_Basis_float n) {
adamc@137 1566 int len;
adamc@137 1567 char *r;
adamc@137 1568
adamc@311 1569 uw_check_heap(ctx, FLOATS_MAX);
adamc@666 1570 r = ctx->heap.front;
adamc@137 1571 sprintf(r, "%g%n", n, &len);
adamc@666 1572 ctx->heap.front += len+1;
adamc@137 1573 return r;
adamc@120 1574 }
adamc@120 1575
adamc@311 1576 char *uw_Basis_urlifyString(uw_context ctx, uw_Basis_string s) {
adamc@137 1577 char *r, *p;
adamc@137 1578
adamc@925 1579 if (s[0] == '\0')
adamc@925 1580 return "_";
adamc@925 1581
adamc@925 1582 uw_check_heap(ctx, strlen(s) * 3 + 1 + !!(s[0] == '_'));
adamc@925 1583
adamc@925 1584 r = p = ctx->heap.front;
adamc@925 1585 if (s[0] == '_')
adamc@925 1586 *p++ = '_';
adamc@925 1587
adamc@925 1588 for (; *s; s++) {
adamc@137 1589 char c = *s;
adamc@137 1590
adamc@137 1591 if (c == ' ')
adamc@137 1592 *p++ = '+';
adamc@137 1593 else if (isalnum(c))
adamc@137 1594 *p++ = c;
adamc@137 1595 else {
adamc@137 1596 sprintf(p, "%%%02X", c);
adamc@137 1597 p += 3;
adamc@137 1598 }
adamc@137 1599 }
adamc@137 1600
adamc@137 1601 *p++ = 0;
adamc@666 1602 ctx->heap.front = p;
adamc@137 1603 return r;
adamc@120 1604 }
adamc@120 1605
adamc@311 1606 char *uw_Basis_urlifyBool(uw_context ctx, uw_Basis_bool b) {
adamc@311 1607 if (b == uw_Basis_False)
adamc@186 1608 return "0";
adamc@186 1609 else
adamc@186 1610 return "1";
adamc@186 1611 }
adamc@186 1612
adamc@311 1613 static void uw_Basis_urlifyInt_w_unsafe(uw_context ctx, uw_Basis_int n) {
adamc@120 1614 int len;
adamc@120 1615
adamc@666 1616 sprintf(ctx->page.front, "%lld%n", n, &len);
adamc@666 1617 ctx->page.front += len;
adamc@120 1618 }
adamc@120 1619
adamc@428 1620 uw_unit uw_Basis_urlifyInt_w(uw_context ctx, uw_Basis_int n) {
adamc@311 1621 uw_check(ctx, INTS_MAX);
adamc@311 1622 uw_Basis_urlifyInt_w_unsafe(ctx, n);
adamc@428 1623
adamc@428 1624 return uw_unit_v;
adamc@120 1625 }
adamc@120 1626
adamc@682 1627 uw_unit uw_Basis_urlifyChannel_w(uw_context ctx, uw_Basis_channel chn) {
adamc@682 1628 if (ctx->client != NULL && chn.cli == ctx->client->id) {
adamc@682 1629 int len;
adamc@682 1630
adamc@682 1631 uw_check(ctx, INTS_MAX + 1);
adamc@682 1632 sprintf(ctx->page.front, "%u%n", chn.chn, &len);
adamc@682 1633 ctx->page.front += len;
adamc@682 1634 }
adamc@682 1635
adamc@679 1636 return uw_unit_v;
adamc@679 1637 }
adamc@679 1638
adamc@428 1639 uw_unit uw_Basis_urlifyFloat_w(uw_context ctx, uw_Basis_float n) {
adamc@120 1640 int len;
adamc@120 1641
adamc@311 1642 uw_check(ctx, FLOATS_MAX);
adamc@666 1643 sprintf(ctx->page.front, "%g%n", n, &len);
adamc@666 1644 ctx->page.front += len;
adamc@428 1645
adamc@428 1646 return uw_unit_v;
adamc@120 1647 }
adamc@120 1648
adamc@488 1649 uw_Basis_string uw_Basis_urlifyTime(uw_context ctx, uw_Basis_time t) {
adamc@488 1650 return uw_Basis_urlifyInt(ctx, t);
adamc@488 1651 }
adamc@488 1652
adamc@428 1653 uw_unit uw_Basis_urlifyString_w(uw_context ctx, uw_Basis_string s) {
adamc@925 1654 if (s[0] == '\0') {
adamc@925 1655 uw_check(ctx, 1);
adamc@925 1656 uw_writec_unsafe(ctx, '_');
adamc@925 1657 return uw_unit_v;
adamc@925 1658 }
adamc@925 1659
adamc@925 1660 uw_check(ctx, strlen(s) * 3 + !!(s[0] == '_'));
adamc@925 1661
adamc@925 1662 if (s[0] == '_')
adamc@925 1663 uw_writec_unsafe(ctx, '_');
adamc@120 1664
adamc@120 1665 for (; *s; s++) {
adamc@120 1666 char c = *s;
adamc@120 1667
adamc@120 1668 if (c == ' ')
adamc@311 1669 uw_writec_unsafe(ctx, '+');
adamc@120 1670 else if (isalnum(c))
adamc@311 1671 uw_writec_unsafe(ctx, c);
adamc@120 1672 else {
adamc@666 1673 sprintf(ctx->page.front, "%%%02X", c);
adamc@666 1674 ctx->page.front += 3;
adamc@120 1675 }
adamc@120 1676 }
adamc@428 1677
adamc@428 1678 return uw_unit_v;
adamc@120 1679 }
adamc@120 1680
adamc@428 1681 uw_unit uw_Basis_urlifyBool_w(uw_context ctx, uw_Basis_bool b) {
adamc@311 1682 if (b == uw_Basis_False)
adamc@311 1683 uw_writec(ctx, '0');
adamc@186 1684 else
adamc@311 1685 uw_writec(ctx, '1');
adamc@428 1686
adamc@428 1687 return uw_unit_v;
adamc@186 1688 }
adamc@186 1689
adamc@120 1690
adamc@311 1691 static char *uw_unurlify_advance(char *s) {
adamc@144 1692 char *new_s = strchr(s, '/');
adamc@120 1693
adamc@120 1694 if (new_s)
adamc@120 1695 *new_s++ = 0;
adamc@120 1696 else
adamc@144 1697 new_s = strchr(s, 0);
adamc@144 1698
adamc@144 1699 return new_s;
adamc@144 1700 }
adamc@144 1701
adamc@311 1702 uw_Basis_int uw_Basis_unurlifyInt(uw_context ctx, char **s) {
adamc@311 1703 char *new_s = uw_unurlify_advance(*s);
adamc@311 1704 uw_Basis_int r;
adamc@120 1705
adamc@276 1706 r = atoll(*s);
adamc@120 1707 *s = new_s;
adamc@120 1708 return r;
adamc@120 1709 }
adamc@120 1710
adamc@311 1711 uw_Basis_float uw_Basis_unurlifyFloat(uw_context ctx, char **s) {
adamc@311 1712 char *new_s = uw_unurlify_advance(*s);
adamc@311 1713 uw_Basis_float r;
adamc@120 1714
adamc@120 1715 r = atof(*s);
adamc@120 1716 *s = new_s;
adamc@120 1717 return r;
adamc@120 1718 }
adamc@120 1719
adamc@488 1720 uw_Basis_time uw_Basis_unurlifyTime(uw_context ctx, char **s) {
adamc@488 1721 return uw_Basis_unurlifyInt(ctx, s);
adamc@488 1722 }
adamc@488 1723
adamc@1023 1724 static uw_Basis_string uw_unurlifyString_to(int fromClient, uw_context ctx, char *r, char *s) {
adamc@927 1725 char *s1, *s2 = s;
adamc@144 1726 int n;
adamc@136 1727
adamc@1023 1728 if (!fromClient) {
adamc@1023 1729 if (*s2 == '_')
adamc@1023 1730 ++s2;
adamc@1023 1731 else if (s2[0] == '%' && s2[1] == '5' && (s2[2] == 'f' || s2[2] == 'F'))
adamc@1023 1732 s2 += 3;
adamc@1023 1733 }
adamc@925 1734
adamc@927 1735 for (s1 = r; *s2; ++s1, ++s2) {
adamc@136 1736 char c = *s2;
adamc@136 1737
adamc@136 1738 switch (c) {
adamc@136 1739 case '+':
adamc@136 1740 *s1 = ' ';
adamc@136 1741 break;
adamc@136 1742 case '%':
adamc@169 1743 if (s2[1] == 0)
adamc@311 1744 uw_error(ctx, FATAL, "Missing first character of escaped URL byte");
adamc@169 1745 if (s2[2] == 0)
adamc@311 1746 uw_error(ctx, FATAL, "Missing second character of escaped URL byte");
adamc@169 1747 if (sscanf(s2+1, "%02X", &n) != 1)
adamc@311 1748 uw_error(ctx, FATAL, "Invalid escaped URL byte starting at: %s", s2);
adamc@136 1749 *s1 = n;
adamc@136 1750 s2 += 2;
adamc@136 1751 break;
adamc@136 1752 default:
adamc@136 1753 *s1 = c;
adamc@136 1754 }
adamc@136 1755 }
adamc@136 1756 *s1++ = 0;
adamc@144 1757 return s1;
adamc@144 1758 }
adamc@144 1759
adamc@311 1760 uw_Basis_bool uw_Basis_unurlifyBool(uw_context ctx, char **s) {
adamc@311 1761 char *new_s = uw_unurlify_advance(*s);
adamc@311 1762 uw_Basis_bool r;
adamc@186 1763
adamc@186 1764 if (*s[0] == 0 || !strcmp(*s, "0") || !strcmp(*s, "off"))
adamc@311 1765 r = uw_Basis_False;
adamc@186 1766 else
adamc@311 1767 r = uw_Basis_True;
adamc@186 1768
adamc@186 1769 *s = new_s;
adamc@186 1770 return r;
adamc@186 1771 }
adamc@186 1772
adamc@311 1773 uw_Basis_string uw_Basis_unurlifyString(uw_context ctx, char **s) {
adamc@311 1774 char *new_s = uw_unurlify_advance(*s);
adamc@144 1775 char *r, *s1, *s2;
adamc@144 1776 int len, n;
adamc@144 1777
adamc@200 1778 len = strlen(*s);
adamc@311 1779 uw_check_heap(ctx, len + 1);
adamc@144 1780
adamc@666 1781 r = ctx->heap.front;
adamc@1023 1782 ctx->heap.front = uw_unurlifyString_to(0, ctx, ctx->heap.front, *s);
adamc@1023 1783 *s = new_s;
adamc@1023 1784 return r;
adamc@1023 1785 }
adamc@1023 1786
adamc@1023 1787 uw_Basis_string uw_Basis_unurlifyString_fromClient(uw_context ctx, char **s) {
adamc@1023 1788 char *new_s = uw_unurlify_advance(*s);
adamc@1023 1789 char *r, *s1, *s2;
adamc@1023 1790 int len, n;
adamc@1023 1791
adamc@1023 1792 len = strlen(*s);
adamc@1023 1793 uw_check_heap(ctx, len + 1);
adamc@1023 1794
adamc@1023 1795 r = ctx->heap.front;
adamc@1023 1796 ctx->heap.front = uw_unurlifyString_to(1, ctx, ctx->heap.front, *s);
adamc@136 1797 *s = new_s;
adamc@136 1798 return r;
adamc@120 1799 }
adamc@135 1800
adamc@135 1801
adamc@311 1802 char *uw_Basis_htmlifyInt(uw_context ctx, uw_Basis_int n) {
adamc@286 1803 int len;
adamc@286 1804 char *r;
adamc@286 1805
adamc@311 1806 uw_check_heap(ctx, INTS_MAX);
adamc@666 1807 r = ctx->heap.front;
adamc@286 1808 sprintf(r, "%lld%n", n, &len);
adamc@666 1809 ctx->heap.front += len+1;
adamc@286 1810 return r;
adamc@286 1811 }
adamc@286 1812
adamc@428 1813 uw_unit uw_Basis_htmlifyInt_w(uw_context ctx, uw_Basis_int n) {
adamc@286 1814 int len;
adamc@286 1815
adamc@311 1816 uw_check(ctx, INTS_MAX);
adamc@666 1817 sprintf(ctx->page.front, "%lld%n", n, &len);
adamc@666 1818 ctx->page.front += len;
adamc@428 1819
adamc@428 1820 return uw_unit_v;
adamc@286 1821 }
adamc@286 1822
adamc@311 1823 char *uw_Basis_htmlifyFloat(uw_context ctx, uw_Basis_float n) {
adamc@286 1824 int len;
adamc@286 1825 char *r;
adamc@286 1826
adamc@311 1827 uw_check_heap(ctx, FLOATS_MAX);
adamc@666 1828 r = ctx->heap.front;
adamc@286 1829 sprintf(r, "%g%n", n, &len);
adamc@666 1830 ctx->heap.front += len+1;
adamc@286 1831 return r;
adamc@286 1832 }
adamc@286 1833
adamc@428 1834 uw_unit uw_Basis_htmlifyFloat_w(uw_context ctx, uw_Basis_float n) {
adamc@286 1835 int len;
adamc@286 1836
adamc@311 1837 uw_check(ctx, FLOATS_MAX);
adamc@666 1838 sprintf(ctx->page.front, "%g%n", n, &len);
adamc@666 1839 ctx->page.front += len;
adamc@428 1840
adamc@428 1841 return uw_unit_v;
adamc@286 1842 }
adamc@286 1843
adamc@311 1844 char *uw_Basis_htmlifyString(uw_context ctx, uw_Basis_string s) {
adamc@137 1845 char *r, *s2;
adamc@137 1846
adamc@311 1847 uw_check_heap(ctx, strlen(s) * 5 + 1);
adamc@137 1848
adamc@666 1849 for (r = s2 = ctx->heap.front; *s; s++) {
adamc@1054 1850 unsigned char c = *s;
adamc@137 1851
adamc@137 1852 switch (c) {
adamc@137 1853 case '<':
adamc@137 1854 strcpy(s2, "&lt;");
adamc@137 1855 s2 += 4;
adamc@137 1856 break;
adamc@137 1857 case '&':
adamc@137 1858 strcpy(s2, "&amp;");
adamc@137 1859 s2 += 5;
adamc@137 1860 break;
adamc@137 1861 default:
adamc@1059 1862 *s2++ = c;
adamc@137 1863 }
adamc@137 1864 }
adamc@137 1865
adamc@137 1866 *s2++ = 0;
adamc@666 1867 ctx->heap.front = s2;
adamc@137 1868 return r;
adamc@135 1869 }
adamc@135 1870
adamc@428 1871 uw_unit uw_Basis_htmlifyString_w(uw_context ctx, uw_Basis_string s) {
adamc@321 1872 uw_check(ctx, strlen(s) * 6);
adamc@135 1873
adamc@135 1874 for (; *s; s++) {
adamc@1054 1875 unsigned char c = *s;
adamc@135 1876
adamc@135 1877 switch (c) {
adamc@135 1878 case '<':
adamc@311 1879 uw_write_unsafe(ctx, "&lt;");
adamc@135 1880 break;
adamc@135 1881 case '&':
adamc@311 1882 uw_write_unsafe(ctx, "&amp;");
adamc@135 1883 break;
adamc@135 1884 default:
adamc@1059 1885 uw_writec_unsafe(ctx, c);
adamc@135 1886 }
adamc@135 1887 }
adamc@428 1888
adamc@428 1889 return uw_unit_v;
adamc@135 1890 }
adamc@180 1891
adamc@311 1892 uw_Basis_string uw_Basis_htmlifyBool(uw_context ctx, uw_Basis_bool b) {
adamc@311 1893 if (b == uw_Basis_False)
adamc@286 1894 return "False";
adamc@286 1895 else
adamc@286 1896 return "True";
adamc@286 1897 }
adamc@286 1898
adamc@428 1899 uw_unit uw_Basis_htmlifyBool_w(uw_context ctx, uw_Basis_bool b) {
adamc@311 1900 if (b == uw_Basis_False) {
adamc@311 1901 uw_check(ctx, 6);
adamc@666 1902 strcpy(ctx->page.front, "False");
adamc@666 1903 ctx->page.front += 5;
adamc@286 1904 } else {
adamc@311 1905 uw_check(ctx, 5);
adamc@666 1906 strcpy(ctx->page.front, "True");
adamc@666 1907 ctx->page.front += 4;
adamc@286 1908 }
adamc@428 1909
adamc@428 1910 return uw_unit_v;
adamc@286 1911 }
adamc@286 1912
adamc@436 1913 #define TIME_FMT "%x %X"
adamc@438 1914 #define TIME_FMT_PG "%Y-%m-%d %T"
adamc@436 1915
adamc@436 1916 uw_Basis_string uw_Basis_htmlifyTime(uw_context ctx, uw_Basis_time t) {
adamc@436 1917 size_t len;
adamc@436 1918 char *r;
adamc@436 1919 struct tm stm;
adamc@436 1920
adamc@436 1921 if (localtime_r(&t, &stm)) {
adamc@436 1922 uw_check_heap(ctx, TIMES_MAX);
adamc@666 1923 r = ctx->heap.front;
adamc@436 1924 len = strftime(r, TIMES_MAX, TIME_FMT, &stm);
adamc@666 1925 ctx->heap.front += len+1;
adamc@436 1926 return r;
adamc@436 1927 } else
adamc@436 1928 return "<i>Invalid time</i>";
adamc@436 1929 }
adamc@436 1930
adamc@436 1931 uw_unit uw_Basis_htmlifyTime_w(uw_context ctx, uw_Basis_time t) {
adamc@436 1932 size_t len;
adamc@436 1933 char *r;
adamc@436 1934 struct tm stm;
adamc@436 1935
adamc@436 1936 if (localtime_r(&t, &stm)) {
adamc@436 1937 uw_check(ctx, TIMES_MAX);
adamc@666 1938 r = ctx->page.front;
adamc@436 1939 len = strftime(r, TIMES_MAX, TIME_FMT, &stm);
adamc@666 1940 ctx->page.front += len;
adamc@436 1941 } else {
adamc@436 1942 uw_check(ctx, 20);
adamc@666 1943 strcpy(ctx->page.front, "<i>Invalid time</i>");
adamc@666 1944 ctx->page.front += 19;
adamc@436 1945 }
adamc@436 1946
adamc@436 1947 return uw_unit_v;
adamc@436 1948 }
adamc@436 1949
adamc@821 1950 uw_Basis_char uw_Basis_strsub(uw_context ctx, uw_Basis_string s, uw_Basis_int n) {
adamc@821 1951 if (n >= 0 && n < strlen(s))
adamc@821 1952 return s[n];
adamc@821 1953 else
adamc@821 1954 uw_error(ctx, FATAL, "Out-of-bounds strsub");
adamc@821 1955 }
adamc@821 1956
adamc@821 1957 uw_Basis_string uw_Basis_strsuffix(uw_context ctx, uw_Basis_string s, uw_Basis_int n) {
adamc@821 1958 if (n >= 0 && n < strlen(s))
adamc@821 1959 return &s[n];
adamc@821 1960 else
adamc@821 1961 uw_error(ctx, FATAL, "Out-of-bounds strsuffix");
adamc@821 1962 }
adamc@821 1963
adamc@828 1964 uw_Basis_int uw_Basis_strlen(uw_context ctx, uw_Basis_string s) {
adamc@828 1965 return strlen(s);
adamc@828 1966 }
adamc@828 1967
adamc@829 1968 uw_Basis_string uw_Basis_strchr(uw_context ctx, uw_Basis_string s, uw_Basis_char ch) {
adamc@829 1969 return strchr(s, ch);
adamc@829 1970 }
adamc@829 1971
adamc@831 1972 uw_Basis_int uw_Basis_strcspn(uw_context ctx, uw_Basis_string s, uw_Basis_string chs) {
adamc@831 1973 return strcspn(s, chs);
adamc@831 1974 }
adamc@831 1975
adamc@829 1976 uw_Basis_int *uw_Basis_strindex(uw_context ctx, uw_Basis_string s, uw_Basis_char ch) {
adamc@829 1977 uw_Basis_string r = strchr(s, ch);
adamc@829 1978 if (r == NULL)
adamc@829 1979 return NULL;
adamc@829 1980 else {
adamc@829 1981 uw_Basis_int *nr = uw_malloc(ctx, sizeof(uw_Basis_int));
adamc@829 1982 *nr = r - s;
adamc@829 1983 return nr;
adamc@829 1984 }
adamc@829 1985 }
adamc@829 1986
adamc@311 1987 uw_Basis_string uw_Basis_strcat(uw_context ctx, uw_Basis_string s1, uw_Basis_string s2) {
adamc@828 1988 int len = uw_Basis_strlen(ctx, s1) + uw_Basis_strlen(ctx, s2) + 1;
adamc@180 1989 char *s;
adamc@180 1990
adamc@311 1991 uw_check_heap(ctx, len);
adamc@180 1992
adamc@666 1993 s = ctx->heap.front;
adamc@180 1994
adamc@180 1995 strcpy(s, s1);
adamc@180 1996 strcat(s, s2);
adamc@666 1997 ctx->heap.front += len;
adamc@180 1998
adamc@180 1999 return s;
adamc@180 2000 }
adamc@278 2001
adamc@829 2002 uw_Basis_string uw_Basis_substring(uw_context ctx, uw_Basis_string s, uw_Basis_int start, uw_Basis_int len) {
adamc@829 2003 size_t full_len = uw_Basis_strlen(ctx, s);
adamc@829 2004
adamc@829 2005 if (start < 0)
adamc@829 2006 uw_error(ctx, FATAL, "substring: Negative start index");
adamc@829 2007 if (len < 0)
adamc@829 2008 uw_error(ctx, FATAL, "substring: Negative length");
adamc@829 2009 if (start + len > full_len)
adamc@829 2010 uw_error(ctx, FATAL, "substring: Start index plus length is too large");
adamc@829 2011
adamc@829 2012 if (start + len == full_len)
adamc@829 2013 return &s[start];
adamc@829 2014 else {
adamc@829 2015 uw_Basis_string r = uw_malloc(ctx, len+1);
adamc@959 2016 memcpy(r, s+start, len);
adamc@829 2017 r[len] = 0;
adamc@829 2018 return r;
adamc@829 2019 }
adamc@829 2020
adamc@829 2021 }
adamc@829 2022
adamc@1023 2023 uw_Basis_string uw_Basis_str1(uw_context ctx, uw_Basis_char ch) {
adamc@1023 2024 char *r;
adamc@1023 2025
adamc@1023 2026 uw_check_heap(ctx, 2);
adamc@1023 2027 r = ctx->heap.front;
adamc@1023 2028 r[0] = ch;
adamc@1023 2029 r[1] = 0;
adamc@1023 2030
adamc@1023 2031 ctx->heap.front += 2;
adamc@1023 2032
adamc@1023 2033 return r;
adamc@1023 2034 }
adamc@1023 2035
adamc@737 2036 uw_Basis_string uw_strdup(uw_context ctx, uw_Basis_string s1) {
adamc@828 2037 int len = uw_Basis_strlen(ctx, s1) + 1;
adamc@278 2038 char *s;
adamc@278 2039
adamc@311 2040 uw_check_heap(ctx, len);
adamc@278 2041
adamc@666 2042 s = ctx->heap.front;
adamc@278 2043
adamc@278 2044 strcpy(s, s1);
adamc@666 2045 ctx->heap.front += len;
adamc@278 2046
adamc@278 2047 return s;
adamc@278 2048 }
adamc@280 2049
adamc@737 2050 uw_Basis_string uw_maybe_strdup(uw_context ctx, uw_Basis_string s1) {
adamc@493 2051 if (s1)
adamc@737 2052 return uw_strdup(ctx, s1);
adamc@493 2053 else
adamc@493 2054 return NULL;
adamc@493 2055 }
adamc@493 2056
adamc@742 2057 char *uw_memdup(uw_context ctx, const char *p, size_t len) {
adamc@742 2058 char *r = uw_malloc(ctx, len);
adamc@742 2059 memcpy(r, p, len);
adamc@742 2060 return r;
adamc@742 2061 }
adamc@280 2062
adamc@879 2063 char *uw_sqlfmtInt = "%lld::int8%n";
adamc@879 2064
adamc@311 2065 char *uw_Basis_sqlifyInt(uw_context ctx, uw_Basis_int n) {
adamc@281 2066 int len;
adamc@281 2067 char *r;
adamc@281 2068
adamc@311 2069 uw_check_heap(ctx, INTS_MAX + 6);
adamc@666 2070 r = ctx->heap.front;
adamc@879 2071 sprintf(r, uw_sqlfmtInt, n, &len);
adamc@666 2072 ctx->heap.front += len+1;
adamc@281 2073 return r;
adamc@281 2074 }
adamc@281 2075
adamc@467 2076 char *uw_Basis_sqlifyIntN(uw_context ctx, uw_Basis_int *n) {
adamc@467 2077 if (n == NULL)
adamc@467 2078 return "NULL";
adamc@467 2079 else
adamc@467 2080 return uw_Basis_sqlifyInt(ctx, *n);
adamc@467 2081 }
adamc@467 2082
adamc@879 2083 char *uw_sqlfmtFloat = "%g::float8%n";
adamc@879 2084
adamc@311 2085 char *uw_Basis_sqlifyFloat(uw_context ctx, uw_Basis_float n) {
adamc@281 2086 int len;
adamc@281 2087 char *r;
adamc@281 2088
adamc@311 2089 uw_check_heap(ctx, FLOATS_MAX + 8);
adamc@666 2090 r = ctx->heap.front;
adamc@879 2091 sprintf(r, uw_sqlfmtFloat, n, &len);
adamc@666 2092 ctx->heap.front += len+1;
adamc@281 2093 return r;
adamc@281 2094 }
adamc@281 2095
adamc@467 2096 char *uw_Basis_sqlifyFloatN(uw_context ctx, uw_Basis_float *n) {
adamc@467 2097 if (n == NULL)
adamc@467 2098 return "NULL";
adamc@467 2099 else
adamc@467 2100 return uw_Basis_sqlifyFloat(ctx, *n);
adamc@467 2101 }
adamc@467 2102
adamc@879 2103 int uw_Estrings = 1;
adamc@879 2104 char *uw_sqlsuffixString = "::text";
adamc@1011 2105 char *uw_sqlsuffixChar = "::char";
adamc@281 2106
adamc@311 2107 uw_Basis_string uw_Basis_sqlifyString(uw_context ctx, uw_Basis_string s) {
adamc@280 2108 char *r, *s2;
adamc@280 2109
adamc@879 2110 uw_check_heap(ctx, strlen(s) * 2 + 3 + uw_Estrings + strlen(uw_sqlsuffixString));
adamc@280 2111
adamc@666 2112 r = s2 = ctx->heap.front;
adamc@879 2113 if (uw_Estrings)
adamc@879 2114 *s2++ = 'E';
adamc@280 2115 *s2++ = '\'';
adamc@280 2116
adamc@280 2117 for (; *s; s++) {
adamc@280 2118 char c = *s;
adamc@280 2119
adamc@280 2120 switch (c) {
adamc@280 2121 case '\'':
adamc@885 2122 if (uw_Estrings)
adamc@885 2123 strcpy(s2, "\\'");
adamc@885 2124 else
adamc@885 2125 strcpy(s2, "''");
adamc@280 2126 s2 += 2;
adamc@280 2127 break;
adamc@280 2128 case '\\':
adamc@885 2129 if (uw_Estrings) {
adamc@885 2130 strcpy(s2, "\\\\");
adamc@885 2131 s2 += 2;
adamc@885 2132 } else
adamc@885 2133 *s2++ = '\\';
adamc@280 2134 break;
adamc@280 2135 default:
adamc@280 2136 if (isprint(c))
adamc@280 2137 *s2++ = c;
adamc@879 2138 else if (uw_Estrings) {
adamc@737 2139 sprintf(s2, "\\%03o", c);
adamc@280 2140 s2 += 4;
adamc@280 2141 }
adamc@879 2142 else
adamc@879 2143 uw_error(ctx, FATAL, "Non-printable character %u in string to SQLify", c);
adamc@280 2144 }
adamc@280 2145 }
adamc@280 2146
adamc@879 2147 *s2++ = '\'';
adamc@879 2148 strcpy(s2, uw_sqlsuffixString);
adamc@879 2149 ctx->heap.front = s2 + 1 + strlen(uw_sqlsuffixString);
adamc@280 2150 return r;
adamc@280 2151 }
adamc@281 2152
adamc@1011 2153 uw_Basis_string uw_Basis_sqlifyChar(uw_context ctx, uw_Basis_char c) {
adamc@1011 2154 char *r, *s2;
adamc@1011 2155
adamc@1011 2156 uw_check_heap(ctx, 5 + uw_Estrings + strlen(uw_sqlsuffixChar));
adamc@1011 2157
adamc@1011 2158 r = s2 = ctx->heap.front;
adamc@1011 2159 if (uw_Estrings)
adamc@1011 2160 *s2++ = 'E';
adamc@1011 2161 *s2++ = '\'';
adamc@1011 2162
adamc@1011 2163 switch (c) {
adamc@1011 2164 case '\'':
adamc@1011 2165 if (uw_Estrings)
adamc@1011 2166 strcpy(s2, "\\'");
adamc@1011 2167 else
adamc@1011 2168 strcpy(s2, "''");
adamc@1011 2169 s2 += 2;
adamc@1011 2170 break;
adamc@1011 2171 case '\\':
adamc@1011 2172 if (uw_Estrings) {
adamc@1011 2173 strcpy(s2, "\\\\");
adamc@1011 2174 s2 += 2;
adamc@1011 2175 } else
adamc@1011 2176 *s2++ = '\\';
adamc@1011 2177 break;
adamc@1011 2178 default:
adamc@1011 2179 if (isprint(c))
adamc@1011 2180 *s2++ = c;
adamc@1011 2181 else if (uw_Estrings) {
adamc@1011 2182 sprintf(s2, "\\%03o", c);
adamc@1011 2183 s2 += 4;
adamc@1011 2184 }
adamc@1011 2185 else
adamc@1011 2186 uw_error(ctx, FATAL, "Non-printable character %u in char to SQLify", c);
adamc@1011 2187 }
adamc@1011 2188
adamc@1011 2189 *s2++ = '\'';
adamc@1011 2190 strcpy(s2, uw_sqlsuffixChar);
adamc@1011 2191 ctx->heap.front = s2 + 1 + strlen(uw_sqlsuffixChar);
adamc@1011 2192 return r;
adamc@1011 2193 }
adamc@1011 2194
adamc@879 2195 char *uw_sqlsuffixBlob = "::bytea";
adamc@879 2196
adamc@737 2197 uw_Basis_string uw_Basis_sqlifyBlob(uw_context ctx, uw_Basis_blob b) {
adamc@737 2198 char *r, *s2;
adamc@737 2199 size_t i;
adamc@737 2200
adamc@879 2201 uw_check_heap(ctx, b.size * 5 + 3 + uw_Estrings + strlen(uw_sqlsuffixBlob));
adamc@737 2202
adamc@737 2203 r = s2 = ctx->heap.front;
adamc@879 2204 if (uw_Estrings)
adamc@879 2205 *s2++ = 'E';
adamc@737 2206 *s2++ = '\'';
adamc@737 2207
adamc@737 2208 for (i = 0; i < b.size; ++i) {
adamc@737 2209 char c = b.data[i];
adamc@737 2210
adamc@737 2211 switch (c) {
adamc@737 2212 case '\'':
adamc@885 2213 if (uw_Estrings)
adamc@885 2214 strcpy(s2, "\\'");
adamc@885 2215 else
adamc@885 2216 strcpy(s2, "''");
adamc@737 2217 s2 += 2;
adamc@737 2218 break;
adamc@737 2219 case '\\':
adamc@885 2220 if (uw_Estrings) {
adamc@885 2221 strcpy(s2, "\\\\\\\\");
adamc@885 2222 s2 += 4;
adamc@885 2223 } else
adamc@885 2224 *s2++ = '\\';
adamc@737 2225 break;
adamc@737 2226 default:
adamc@737 2227 if (isprint(c))
adamc@737 2228 *s2++ = c;
adamc@879 2229 else if (uw_Estrings) {
adamc@737 2230 sprintf(s2, "\\\\%03o", c);
adamc@737 2231 s2 += 5;
adamc@737 2232 }
adamc@879 2233 else
adamc@879 2234 uw_error(ctx, FATAL, "Non-printable character %u in blob to SQLify", c);
adamc@737 2235 }
adamc@737 2236 }
adamc@737 2237
adamc@879 2238 *s2++ = '\'';
adamc@879 2239 strcpy(s2, uw_sqlsuffixBlob);
adamc@879 2240 ctx->heap.front = s2 + 1 + strlen(uw_sqlsuffixBlob);
adamc@737 2241 return r;
adamc@737 2242 }
adamc@737 2243
adamc@682 2244 char *uw_Basis_sqlifyChannel(uw_context ctx, uw_Basis_channel chn) {
adamc@682 2245 int len;
adamc@682 2246 char *r;
adamc@682 2247 unsigned long long combo = ((unsigned long long)chn.cli << 32) | chn.chn;
adamc@682 2248
adamc@682 2249 uw_check_heap(ctx, INTS_MAX + 7);
adamc@682 2250 r = ctx->heap.front;
adamc@879 2251 sprintf(r, uw_sqlfmtInt, combo, &len);
adamc@682 2252 ctx->heap.front += len+1;
adamc@682 2253 return r;
adamc@682 2254 }
adamc@682 2255
adamc@682 2256 char *uw_Basis_attrifyChannel(uw_context ctx, uw_Basis_channel chn) {
adamc@682 2257 int len;
adamc@682 2258 char *r;
adamc@682 2259 unsigned long long combo = ((unsigned long long)chn.cli << 32) | chn.chn;
adamc@682 2260
adamc@682 2261 uw_check_heap(ctx, INTS_MAX + 1);
adamc@682 2262 r = ctx->heap.front;
adamc@682 2263 sprintf(r, "%lld%n", combo, &len);
adamc@682 2264 ctx->heap.front += len+1;
adamc@682 2265 return r;
adamc@682 2266 }
adamc@682 2267
adamc@879 2268 char *uw_sqlfmtUint4 = "%u::int4%n";
adamc@879 2269
adamc@682 2270 char *uw_Basis_sqlifyClient(uw_context ctx, uw_Basis_client cli) {
adamc@682 2271 int len;
adamc@682 2272 char *r;
adamc@682 2273
adamc@682 2274 uw_check_heap(ctx, INTS_MAX + 7);
adamc@682 2275 r = ctx->heap.front;
adamc@879 2276 sprintf(r, uw_sqlfmtUint4, cli, &len);
adamc@682 2277 ctx->heap.front += len+1;
adamc@682 2278 return r;
adamc@682 2279 }
adamc@682 2280
adamc@682 2281 char *uw_Basis_attrifyClient(uw_context ctx, uw_Basis_client cli) {
adamc@682 2282 int len;
adamc@682 2283 char *r;
adamc@682 2284
adamc@682 2285 uw_check_heap(ctx, INTS_MAX + 1);
adamc@682 2286 r = ctx->heap.front;
adamc@682 2287 sprintf(r, "%u%n", cli, &len);
adamc@682 2288 ctx->heap.front += len+1;
adamc@682 2289 return r;
adamc@682 2290 }
adamc@682 2291
adamc@467 2292 uw_Basis_string uw_Basis_sqlifyStringN(uw_context ctx, uw_Basis_string s) {
adamc@467 2293 if (s == NULL)
adamc@467 2294 return "NULL";
adamc@467 2295 else
adamc@467 2296 return uw_Basis_sqlifyString(ctx, s);
adamc@467 2297 }
adamc@467 2298
adamc@311 2299 char *uw_Basis_sqlifyBool(uw_context ctx, uw_Basis_bool b) {
adamc@311 2300 if (b == uw_Basis_False)
adamc@281 2301 return "FALSE";
adamc@281 2302 else
adamc@281 2303 return "TRUE";
adamc@281 2304 }
adamc@282 2305
adamc@467 2306 char *uw_Basis_sqlifyBoolN(uw_context ctx, uw_Basis_bool *b) {
adamc@467 2307 if (b == NULL)
adamc@467 2308 return "NULL";
adamc@467 2309 else
adamc@467 2310 return uw_Basis_sqlifyBool(ctx, *b);
adamc@467 2311 }
adamc@467 2312
adamc@439 2313 char *uw_Basis_sqlifyTime(uw_context ctx, uw_Basis_time t) {
adamc@439 2314 size_t len;
adamc@682 2315 char *r, *s;
adamc@682 2316 struct tm stm;
adamc@682 2317
adamc@682 2318 if (localtime_r(&t, &stm)) {
adamc@682 2319 s = uw_malloc(ctx, TIMES_MAX);
adamc@995 2320 --stm.tm_hour;
adamc@682 2321 len = strftime(s, TIMES_MAX, TIME_FMT, &stm);
adamc@682 2322 r = uw_malloc(ctx, len + 14);
adamc@682 2323 sprintf(r, "'%s'::timestamp", s);
adamc@682 2324 return r;
adamc@682 2325 } else
adamc@682 2326 return "<Invalid time>";
adamc@682 2327 }
adamc@682 2328
adamc@682 2329 char *uw_Basis_attrifyTime(uw_context ctx, uw_Basis_time t) {
adamc@682 2330 size_t len;
adamc@439 2331 char *r;
adamc@439 2332 struct tm stm;
adamc@439 2333
adamc@439 2334 if (localtime_r(&t, &stm)) {
adamc@439 2335 uw_check_heap(ctx, TIMES_MAX);
adamc@666 2336 r = ctx->heap.front;
adamc@439 2337 len = strftime(r, TIMES_MAX, TIME_FMT, &stm);
adamc@666 2338 ctx->heap.front += len+1;
adamc@439 2339 return r;
adamc@439 2340 } else
adamc@439 2341 return "<Invalid time>";
adamc@439 2342 }
adamc@439 2343
adamc@467 2344 char *uw_Basis_sqlifyTimeN(uw_context ctx, uw_Basis_time *t) {
adamc@467 2345 if (t == NULL)
adamc@467 2346 return "NULL";
adamc@467 2347 else
adamc@467 2348 return uw_Basis_sqlifyTime(ctx, *t);
adamc@467 2349 }
adamc@467 2350
adamc@311 2351 char *uw_Basis_ensqlBool(uw_Basis_bool b) {
adamc@311 2352 static uw_Basis_int true = 1;
adamc@311 2353 static uw_Basis_int false = 0;
adamc@282 2354
adamc@311 2355 if (b == uw_Basis_False)
adamc@282 2356 return (char *)&false;
adamc@282 2357 else
adamc@282 2358 return (char *)&true;
adamc@282 2359 }
adamc@284 2360
adamc@311 2361 uw_Basis_string uw_Basis_intToString(uw_context ctx, uw_Basis_int n) {
adamc@284 2362 int len;
adamc@284 2363 char *r;
adamc@284 2364
adamc@311 2365 uw_check_heap(ctx, INTS_MAX);
adamc@666 2366 r = ctx->heap.front;
adamc@284 2367 sprintf(r, "%lld%n", n, &len);
adamc@666 2368 ctx->heap.front += len+1;
adamc@284 2369 return r;
adamc@284 2370 }
adamc@285 2371
adamc@311 2372 uw_Basis_string uw_Basis_floatToString(uw_context ctx, uw_Basis_float n) {
adamc@285 2373 int len;
adamc@285 2374 char *r;
adamc@285 2375
adamc@311 2376 uw_check_heap(ctx, FLOATS_MAX);
adamc@666 2377 r = ctx->heap.front;
adamc@285 2378 sprintf(r, "%g%n", n, &len);
adamc@666 2379 ctx->heap.front += len+1;
adamc@285 2380 return r;
adamc@285 2381 }
adamc@285 2382
adamc@821 2383 uw_Basis_string uw_Basis_charToString(uw_context ctx, uw_Basis_char ch) {
adamc@821 2384 char *r = uw_malloc(ctx, 2);
adamc@821 2385 r[0] = ch;
adamc@821 2386 r[1] = 0;
adamc@821 2387 return r;
adamc@821 2388 }
adamc@821 2389
adamc@311 2390 uw_Basis_string uw_Basis_boolToString(uw_context ctx, uw_Basis_bool b) {
adamc@311 2391 if (b == uw_Basis_False)
adamc@285 2392 return "False";
adamc@285 2393 else
adamc@285 2394 return "True";
adamc@285 2395 }
adamc@288 2396
adamc@436 2397 uw_Basis_string uw_Basis_timeToString(uw_context ctx, uw_Basis_time t) {
adamc@436 2398 size_t len;
adamc@436 2399 char *r;
adamc@436 2400 struct tm stm;
adamc@436 2401
adamc@436 2402 if (localtime_r(&t, &stm)) {
adamc@436 2403 uw_check_heap(ctx, TIMES_MAX);
adamc@666 2404 r = ctx->heap.front;
adamc@436 2405 len = strftime(r, TIMES_MAX, TIME_FMT, &stm);
adamc@666 2406 ctx->heap.front += len+1;
adamc@436 2407 return r;
adamc@436 2408 } else
adamc@436 2409 return "<Invalid time>";
adamc@436 2410 }
adamc@288 2411
adamc@311 2412 uw_Basis_int *uw_Basis_stringToInt(uw_context ctx, uw_Basis_string s) {
adamc@288 2413 char *endptr;
adamc@311 2414 uw_Basis_int n = strtoll(s, &endptr, 10);
adamc@288 2415
adamc@288 2416 if (*s != '\0' && *endptr == '\0') {
adamc@311 2417 uw_Basis_int *r = uw_malloc(ctx, sizeof(uw_Basis_int));
adamc@288 2418 *r = n;
adamc@288 2419 return r;
adamc@288 2420 } else
adamc@288 2421 return NULL;
adamc@288 2422 }
adamc@289 2423
adamc@311 2424 uw_Basis_float *uw_Basis_stringToFloat(uw_context ctx, uw_Basis_string s) {
adamc@289 2425 char *endptr;
adamc@311 2426 uw_Basis_float n = strtod(s, &endptr);
adamc@289 2427
adamc@289 2428 if (*s != '\0' && *endptr == '\0') {
adamc@311 2429 uw_Basis_float *r = uw_malloc(ctx, sizeof(uw_Basis_float));
adamc@289 2430 *r = n;
adamc@289 2431 return r;
adamc@289 2432 } else
adamc@289 2433 return NULL;
adamc@289 2434 }
adamc@289 2435
adamc@821 2436 uw_Basis_char *uw_Basis_stringToChar(uw_context ctx, uw_Basis_string s) {
adamc@821 2437 if (s[0] == 0) {
adamc@821 2438 uw_Basis_char *r = uw_malloc(ctx, 1);
adamc@821 2439 r[0] = 0;
adamc@821 2440 return r;
adamc@821 2441 } else if (s[1] != 0)
adamc@821 2442 return NULL;
adamc@821 2443 else {
adamc@821 2444 uw_Basis_char *r = uw_malloc(ctx, 1);
adamc@821 2445 r[0] = s[0];
adamc@821 2446 return r;
adamc@821 2447 }
adamc@821 2448 }
adamc@821 2449
adamc@311 2450 uw_Basis_bool *uw_Basis_stringToBool(uw_context ctx, uw_Basis_string s) {
adamc@311 2451 static uw_Basis_bool true = uw_Basis_True;
adamc@311 2452 static uw_Basis_bool false = uw_Basis_False;
adamc@289 2453
adamc@289 2454 if (!strcasecmp (s, "True"))
adamc@289 2455 return &true;
adamc@289 2456 else if (!strcasecmp (s, "False"))
adamc@289 2457 return &false;
adamc@289 2458 else
adamc@289 2459 return NULL;
adamc@289 2460 }
adamc@292 2461
adamc@436 2462 uw_Basis_time *uw_Basis_stringToTime(uw_context ctx, uw_Basis_string s) {
adamc@438 2463 char *dot = strchr(s, '.'), *end = strchr(s, 0);
adamc@436 2464 struct tm stm;
adamc@436 2465
adamc@439 2466 if (dot) {
adamc@439 2467 *dot = 0;
adamc@439 2468 if (strptime(s, TIME_FMT_PG, &stm) == end) {
adamc@439 2469 *dot = '.';
adamc@439 2470 uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time));
adamc@439 2471 *r = mktime(&stm);
adamc@439 2472 return r;
adamc@439 2473 }
adamc@439 2474 else {
adamc@439 2475 *dot = '.';
adamc@439 2476 return NULL;
adamc@439 2477 }
adamc@436 2478 }
adamc@439 2479 else {
adamc@439 2480 if (strptime(s, TIME_FMT_PG, &stm) == end) {
adamc@439 2481 uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time));
adamc@439 2482 *r = mktime(&stm);
adamc@439 2483 return r;
adamc@439 2484 }
adamc@439 2485 else if (strptime(s, TIME_FMT, &stm) == end) {
adamc@439 2486 uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time));
adamc@439 2487 *r = mktime(&stm);
adamc@439 2488 return r;
adamc@439 2489 }
adamc@439 2490 else
adamc@439 2491 return NULL;
adamc@439 2492 }
adamc@436 2493 }
adamc@436 2494
adamc@311 2495 uw_Basis_int uw_Basis_stringToInt_error(uw_context ctx, uw_Basis_string s) {
adamc@292 2496 char *endptr;
adamc@311 2497 uw_Basis_int n = strtoll(s, &endptr, 10);
adamc@292 2498
adamc@292 2499 if (*s != '\0' && *endptr == '\0')
adamc@292 2500 return n;
adamc@292 2501 else
adamc@311 2502 uw_error(ctx, FATAL, "Can't parse int: %s", s);
adamc@292 2503 }
adamc@293 2504
adamc@682 2505 #include <errno.h>
adamc@682 2506
adamc@678 2507 uw_Basis_channel uw_Basis_stringToChannel_error(uw_context ctx, uw_Basis_string s) {
adamc@682 2508 unsigned long long n;
adamc@682 2509
adamc@682 2510 if (sscanf(s, "%llu", &n) < 1)
adamc@682 2511 uw_error(ctx, FATAL, "Can't parse channel: %s", s);
adamc@682 2512 else {
adamc@682 2513 uw_Basis_channel ch = {n >> 32, n & ((1ull << 32) - 1)};
adamc@682 2514 return ch;
adamc@682 2515 }
adamc@682 2516 }
adamc@682 2517
adamc@682 2518 uw_Basis_client uw_Basis_stringToClient_error(uw_context ctx, uw_Basis_string s) {
adamc@678 2519 char *endptr;
adamc@682 2520 unsigned long n = strtoul(s, &endptr, 10);
adamc@678 2521
adamc@678 2522 if (*s != '\0' && *endptr == '\0')
adamc@678 2523 return n;
adamc@678 2524 else
adamc@682 2525 uw_error(ctx, FATAL, "Can't parse client: %s", s);
adamc@678 2526 }
adamc@678 2527
adamc@311 2528 uw_Basis_float uw_Basis_stringToFloat_error(uw_context ctx, uw_Basis_string s) {
adamc@293 2529 char *endptr;
adamc@311 2530 uw_Basis_float n = strtod(s, &endptr);
adamc@293 2531
adamc@293 2532 if (*s != '\0' && *endptr == '\0')
adamc@293 2533 return n;
adamc@293 2534 else
adamc@311 2535 uw_error(ctx, FATAL, "Can't parse float: %s", s);
adamc@293 2536 }
adamc@293 2537
adamc@821 2538 uw_Basis_char uw_Basis_stringToChar_error(uw_context ctx, uw_Basis_string s) {
adamc@821 2539 if (s[0] == 0)
adamc@821 2540 return 0;
adamc@821 2541 else if (s[1] != 0)
adamc@821 2542 uw_error(ctx, FATAL, "Can't parse char: %s", s);
adamc@821 2543 else
adamc@821 2544 return s[0];
adamc@821 2545 }
adamc@821 2546
adamc@311 2547 uw_Basis_bool uw_Basis_stringToBool_error(uw_context ctx, uw_Basis_string s) {
adamc@296 2548 if (!strcasecmp(s, "T") || !strcasecmp (s, "True"))
adamc@311 2549 return uw_Basis_True;
adamc@296 2550 else if (!strcasecmp(s, "F") || !strcasecmp (s, "False"))
adamc@311 2551 return uw_Basis_False;
adamc@293 2552 else
adamc@311 2553 uw_error(ctx, FATAL, "Can't parse bool: %s", s);
adamc@293 2554 }
adamc@436 2555
adamc@436 2556 uw_Basis_time uw_Basis_stringToTime_error(uw_context ctx, uw_Basis_string s) {
adamc@438 2557 char *dot = strchr(s, '.'), *end = strchr(s, 0);
adamc@436 2558 struct tm stm = {};
adamc@436 2559
adamc@438 2560 if (dot) {
adamc@438 2561 *dot = 0;
adamc@438 2562 if (strptime(s, TIME_FMT_PG, &stm)) {
adamc@438 2563 *dot = '.';
adamc@438 2564 return mktime(&stm);
adamc@438 2565 }
adamc@438 2566 else {
adamc@438 2567 *dot = '.';
adamc@438 2568 uw_error(ctx, FATAL, "Can't parse time: %s", s);
adamc@438 2569 }
adamc@438 2570 }
adamc@438 2571 else {
adamc@439 2572 if (strptime(s, TIME_FMT_PG, &stm) == end)
adamc@439 2573 return mktime(&stm);
adamc@439 2574 else if (strptime(s, TIME_FMT, &stm) == end)
adamc@438 2575 return mktime(&stm);
adamc@438 2576 else
adamc@438 2577 uw_error(ctx, FATAL, "Can't parse time: %s", s);
adamc@438 2578 }
adamc@436 2579 }
adamc@457 2580
adamc@939 2581 uw_Basis_time uw_Basis_unsqlTime(uw_context ctx, uw_Basis_string s) {
adamc@939 2582 char *dot = strchr(s, '.'), *end = strchr(s, 0);
adamc@939 2583 struct tm stm = {};
adamc@939 2584
adamc@939 2585 if (dot) {
adamc@939 2586 *dot = 0;
adamc@939 2587 if (strptime(s, TIME_FMT_PG, &stm)) {
adamc@939 2588 *dot = '.';
adamc@939 2589 return mktime(&stm);
adamc@939 2590 }
adamc@939 2591 else {
adamc@939 2592 *dot = '.';
adamc@939 2593 uw_error(ctx, FATAL, "Can't parse time: %s", s);
adamc@939 2594 }
adamc@939 2595 }
adamc@939 2596 else {
adamc@939 2597 if (strptime(s, TIME_FMT_PG, &stm) == end) {
adamc@939 2598 return mktime(&stm);
adamc@939 2599 } else if (strptime(s, TIME_FMT, &stm) == end) {
adamc@939 2600 return mktime(&stm);
adamc@939 2601 } else
adamc@939 2602 uw_error(ctx, FATAL, "Can't parse time: %s", s);
adamc@939 2603 }
adamc@939 2604 }
adamc@939 2605
adamc@742 2606 uw_Basis_blob uw_Basis_stringToBlob_error(uw_context ctx, uw_Basis_string s, size_t len) {
adamc@742 2607 char *r = ctx->heap.front;
adamc@742 2608 uw_Basis_blob b = {len, r};
adamc@742 2609
adamc@742 2610 uw_check_heap(ctx, len);
adamc@742 2611
adamc@742 2612 while (*s) {
adamc@742 2613 if (s[0] == '\\') {
adamc@742 2614 if (s[1] == '\\') {
adamc@742 2615 *r++ = '\\';
adamc@742 2616 s += 2;
adamc@742 2617 } else if (isdigit(s[1]) && isdigit(s[2]) && isdigit(s[3])) {
adamc@742 2618 *r++ = (s[1] - '0') * 8 * 8 + ((s[2] - '0') * 8) + (s[3] - '0');
adamc@742 2619 s += 4;
adamc@742 2620 }
adamc@742 2621 else {
adamc@742 2622 *r++ = '\\';
adamc@742 2623 ++s;
adamc@742 2624 }
adamc@742 2625 } else {
adamc@742 2626 *r++ = s[0];
adamc@742 2627 ++s;
adamc@742 2628 }
adamc@742 2629 }
adamc@742 2630
adamc@742 2631 b.size = r - ctx->heap.front;
adamc@742 2632 ctx->heap.front = r;
adamc@742 2633 return b;
adamc@742 2634 }
adamc@742 2635
adamc@1052 2636 #define THE_PAST "expires=Mon, 01-01-1970 00:00:00 GMT"
adamc@1052 2637
adamc@463 2638 uw_Basis_string uw_Basis_get_cookie(uw_context ctx, uw_Basis_string c) {
adamc@463 2639 int len = strlen(c);
adamc@854 2640 char *p = ctx->outHeaders.start;
adamc@474 2641
adamc@474 2642 while (p = strstr(p, "\nSet-Cookie: ")) {
adamc@474 2643 char *p2;
adamc@474 2644 p += 13;
adamc@474 2645 p2 = strchr(p, '=');
adamc@474 2646
adamc@474 2647 if (p2) {
adamc@474 2648 size_t sz = strcspn(p2+1, ";\r\n");
adamc@474 2649
adamc@474 2650 if (!strncasecmp(p, c, p2 - p)) {
adamc@1052 2651 if (sz == 0 && strstr(p2+2, THE_PAST))
adamc@1052 2652 return NULL;
adamc@1052 2653 else {
adamc@1052 2654 char *ret = uw_malloc(ctx, sz + 1);
adamc@1052 2655 memcpy(ret, p2+1, sz);
adamc@1052 2656 ret[sz] = 0;
adamc@1052 2657 return ret;
adamc@1052 2658 }
adamc@474 2659 }
adamc@474 2660 }
adamc@474 2661 }
adamc@463 2662
adamc@854 2663 if (p = uw_Basis_requestHeader(ctx, "Cookie")) {
adamc@1051 2664 char *p2;
adamc@1051 2665
adamc@854 2666 while (1) {
adamc@1051 2667 if (!strncmp(p, c, len) && p[len] == '=') {
adamc@1051 2668 if (p2 = strchr(p, ';')) {
adamc@1051 2669 size_t n = p2 - (p + len);
adamc@1051 2670 char *r = uw_malloc(ctx, n);
adamc@1051 2671 memcpy(r, p + 1 + len, n-1);
adamc@1051 2672 r[n-1] = 0;
adamc@1051 2673 return r;
adamc@1051 2674 } else
adamc@1051 2675 return p + 1 + len;
adamc@1051 2676 } else if (p = strchr(p, ';'))
adamc@854 2677 p += 2;
adamc@463 2678 else
adamc@463 2679 return NULL;
adamc@463 2680 }
adamc@463 2681 }
adamc@792 2682
adamc@792 2683 return NULL;
adamc@457 2684 }
adamc@462 2685
adamc@1050 2686 uw_unit uw_Basis_set_cookie(uw_context ctx, uw_Basis_string prefix, uw_Basis_string c, uw_Basis_string v, uw_Basis_time *expires, uw_Basis_bool secure) {
adamc@462 2687 uw_write_header(ctx, "Set-Cookie: ");
adamc@462 2688 uw_write_header(ctx, c);
adamc@462 2689 uw_write_header(ctx, "=");
adamc@462 2690 uw_write_header(ctx, v);
adamc@466 2691 uw_write_header(ctx, "; path=");
adamc@466 2692 uw_write_header(ctx, prefix);
adamc@1050 2693 if (expires) {
adamc@1050 2694 char formatted[30];
adamc@1050 2695 struct tm tm;
adamc@1050 2696
adamc@1050 2697 gmtime_r(expires, &tm);
adamc@1050 2698
adamc@1050 2699 strftime(formatted, sizeof formatted, "%a, %d-%b-%Y %T GMT", &tm);
adamc@1050 2700
adamc@1050 2701 uw_write_header(ctx, "; expires=");
adamc@1050 2702 uw_write_header(ctx, formatted);
adamc@1050 2703 }
adamc@1050 2704 if (secure)
adamc@1050 2705 uw_write_header(ctx, "; secure");
adamc@462 2706 uw_write_header(ctx, "\r\n");
adamc@462 2707
adamc@462 2708 return uw_unit_v;
adamc@462 2709 }
adamc@667 2710
adamc@1050 2711 uw_unit uw_Basis_clear_cookie(uw_context ctx, uw_Basis_string prefix, uw_Basis_string c) {
adamc@1050 2712 uw_write_header(ctx, "Set-Cookie: ");
adamc@1050 2713 uw_write_header(ctx, c);
adamc@1050 2714 uw_write_header(ctx, "=; path=");
adamc@1050 2715 uw_write_header(ctx, prefix);
adamc@1052 2716 uw_write_header(ctx, "; " THE_PAST "\r\n");
adamc@1050 2717
adamc@1050 2718 return uw_unit_v;
adamc@1050 2719 }
adamc@1050 2720
adamc@682 2721 static delta *allocate_delta(uw_context ctx, unsigned client) {
adamc@682 2722 unsigned i;
adamc@682 2723 delta *d;
adamc@671 2724
adamc@682 2725 for (i = 0; i < ctx->used_deltas; ++i)
adamc@682 2726 if (ctx->deltas[i].client == client)
adamc@682 2727 return &ctx->deltas[i];
adamc@671 2728
adamc@682 2729 if (ctx->used_deltas >= ctx->n_deltas) {
adamc@682 2730 ctx->deltas = realloc(ctx->deltas, sizeof(delta) * ++ctx->n_deltas);
adamc@682 2731 buf_init(&ctx->deltas[ctx->n_deltas-1].msgs, 0);
adamc@682 2732 }
adamc@671 2733
adamc@682 2734 d = &ctx->deltas[ctx->used_deltas++];
adamc@682 2735 d->client = client;
adamc@682 2736 buf_reset(&d->msgs);
adamc@682 2737 return d;
adamc@671 2738 }
adamc@671 2739
adamc@668 2740 uw_Basis_channel uw_Basis_new_channel(uw_context ctx, uw_unit u) {
adamc@682 2741 if (ctx->client == NULL)
adamc@682 2742 uw_error(ctx, FATAL, "Attempt to create channel on request not associated with a persistent connection");
adamc@671 2743
adamc@682 2744 return new_channel(ctx->client);
adamc@668 2745 }
adamc@668 2746
adamc@668 2747 uw_unit uw_Basis_send(uw_context ctx, uw_Basis_channel chn, uw_Basis_string msg) {
adamc@682 2748 delta *d = allocate_delta(ctx, chn.cli);
adamc@682 2749 size_t len;
adamc@682 2750 int preLen;
adamc@682 2751 char pre[INTS_MAX + 2];
adamc@668 2752
adamc@682 2753 len = strlen(msg);
adamc@682 2754
adamc@682 2755 sprintf(pre, "%u\n%n", chn.chn, &preLen);
adamc@682 2756
adamc@682 2757 buf_append(&d->msgs, pre, preLen);
adamc@682 2758 buf_append(&d->msgs, msg, len);
adamc@682 2759 buf_append(&d->msgs, "\n", 1);
adamc@671 2760
adamc@671 2761 return uw_unit_v;
adamc@668 2762 }
adamc@671 2763
adamc@671 2764 int uw_db_commit(uw_context);
adamc@671 2765 int uw_db_rollback(uw_context);
adamc@671 2766
adamc@671 2767 void uw_commit(uw_context ctx) {
adamc@682 2768 unsigned i;
adamc@671 2769
adamc@771 2770 for (i = 0; i < ctx->used_transactionals; ++i)
adamc@771 2771 if (ctx->transactionals[i].rollback != NULL)
adamc@771 2772 ctx->transactionals[i].commit(ctx->transactionals[i].data);
adamc@771 2773
adamc@771 2774 for (i = 0; i < ctx->used_transactionals; ++i)
adamc@771 2775 if (ctx->transactionals[i].rollback == NULL)
adamc@771 2776 ctx->transactionals[i].commit(ctx->transactionals[i].data);
adamc@771 2777
adamc@771 2778 if (uw_db_commit(ctx))
adamc@671 2779 uw_error(ctx, FATAL, "Error running SQL COMMIT");
adamc@682 2780
adamc@682 2781 for (i = 0; i < ctx->used_deltas; ++i) {
adamc@682 2782 delta *d = &ctx->deltas[i];
adamc@682 2783 client *c = find_client(d->client);
adamc@682 2784
adamc@682 2785 assert (c != NULL && c->mode == USED);
adamc@682 2786
adamc@685 2787 client_send(c, &d->msgs);
adamc@682 2788 }
adamc@682 2789
adamc@682 2790 if (ctx->client)
adamc@685 2791 release_client(ctx->client);
adamc@771 2792
adamc@771 2793 for (i = 0; i < ctx->used_transactionals; ++i)
adamc@771 2794 ctx->transactionals[i].free(ctx->transactionals[i].data);
adamc@916 2795
adamc@916 2796 // Splice script data into appropriate part of page
adamc@1065 2797 if (ctx->returning_indirectly || ctx->script_header[0] == 0) {
adamc@995 2798 char *start = strstr(ctx->page.start, "<sc>");
adamc@995 2799 if (start) {
adamc@995 2800 memmove(start, start + 4, buf_used(&ctx->page) - (start - ctx->page.start) - 4);
adamc@995 2801 ctx->page.front -= 4;
adamc@995 2802 }
adamc@995 2803 } else if (buf_used(&ctx->script) == 0) {
adamc@916 2804 size_t len = strlen(ctx->script_header);
adamc@916 2805 char *start = strstr(ctx->page.start, "<sc>");
adamc@916 2806 if (start) {
adamc@916 2807 buf_check(&ctx->page, buf_used(&ctx->page) - 4 + len);
adamc@917 2808 start = strstr(ctx->page.start, "<sc>");
adamc@916 2809 memmove(start + len, start + 4, buf_used(&ctx->page) - (start - ctx->page.start) - 3);
adamc@916 2810 ctx->page.front += len - 4;
adamc@916 2811 memcpy(start, ctx->script_header, len);
adamc@916 2812 }
adamc@916 2813 } else {
adamc@916 2814 size_t lenH = strlen(ctx->script_header), len = buf_used(&ctx->script);
adamc@916 2815 size_t lenP = lenH + 40 + len;
adamc@916 2816 char *start = strstr(ctx->page.start, "<sc>");
adamc@916 2817 if (start) {
adamc@916 2818 buf_check(&ctx->page, buf_used(&ctx->page) - 4 + lenP);
adamc@917 2819 start = strstr(ctx->page.start, "<sc>");
adamc@916 2820 memmove(start + lenP, start + 4, buf_used(&ctx->page) - (start - ctx->page.start) - 3);
adamc@916 2821 ctx->page.front += lenP - 4;
adamc@916 2822 memcpy(start, ctx->script_header, lenH);
adamc@916 2823 memcpy(start + lenH, "<script type=\"text/javascript\">", 31);
adamc@916 2824 memcpy(start + lenH + 31, ctx->script.start, len);
adamc@916 2825 memcpy(start + lenH + 31 + len, "</script>", 9);
adamc@916 2826 }
adamc@916 2827 }
adamc@671 2828 }
adamc@671 2829
adamc@671 2830 int uw_rollback(uw_context ctx) {
adamc@771 2831 size_t i;
adamc@885 2832 cleanup *cl;
adamc@771 2833
adamc@682 2834 if (ctx->client)
adamc@685 2835 release_client(ctx->client);
adamc@671 2836
adamc@885 2837 for (cl = ctx->cleanup; cl < ctx->cleanup_front; ++cl)
adamc@885 2838 cl->func(cl->arg);
adamc@885 2839
adamc@885 2840 ctx->cleanup_front = ctx->cleanup;
adamc@885 2841
adamc@771 2842 for (i = 0; i < ctx->used_transactionals; ++i)
adamc@771 2843 if (ctx->transactionals[i].rollback != NULL)
adamc@771 2844 ctx->transactionals[i].rollback(ctx->transactionals[i].data);
adamc@771 2845
adamc@771 2846 for (i = 0; i < ctx->used_transactionals; ++i)
adamc@771 2847 ctx->transactionals[i].free(ctx->transactionals[i].data);
adamc@771 2848
adamc@671 2849 return uw_db_rollback(ctx);
adamc@671 2850 }
adamc@683 2851
adamc@771 2852 void uw_register_transactional(uw_context ctx, void *data, uw_callback commit, uw_callback rollback,
adamc@771 2853 uw_callback free) {
adamc@771 2854 if (commit == NULL)
adamc@771 2855 uw_error(ctx, FATAL, "uw_register_transactional: NULL commit callback");
adamc@771 2856
adamc@771 2857 if (ctx->used_transactionals >= ctx->n_transactionals) {
adamc@771 2858 ctx->transactionals = realloc(ctx->transactionals, ctx->used_transactionals+1);
adamc@771 2859 ++ctx->n_transactionals;
adamc@771 2860 }
adamc@771 2861
adamc@771 2862 ctx->transactionals[ctx->used_transactionals].data = data;
adamc@771 2863 ctx->transactionals[ctx->used_transactionals].commit = commit;
adamc@771 2864 ctx->transactionals[ctx->used_transactionals].rollback = rollback;
adamc@771 2865 ctx->transactionals[ctx->used_transactionals++].free = free;
adamc@771 2866 }
adamc@771 2867
adamc@683 2868
adamc@683 2869 // "Garbage collection"
adamc@683 2870
adamc@683 2871 void uw_expunger(uw_context ctx, uw_Basis_client cli);
adamc@683 2872
adamc@683 2873 static failure_kind uw_expunge(uw_context ctx, uw_Basis_client cli) {
adamc@683 2874 int r = setjmp(ctx->jmp_buf);
adamc@683 2875
adamc@684 2876 if (r == 0) {
adamc@684 2877 if (uw_db_begin(ctx))
adamc@684 2878 uw_error(ctx, FATAL, "Error running SQL BEGIN");
adamc@683 2879 uw_expunger(ctx, cli);
adamc@684 2880 if (uw_db_commit(ctx))
adamc@684 2881 uw_error(ctx, FATAL, "Error running SQL COMMIT");
adamc@684 2882 }
adamc@683 2883
adamc@683 2884 return r;
adamc@683 2885 }
adamc@683 2886
adamc@683 2887 void uw_prune_clients(uw_context ctx) {
adamc@683 2888 client *c, *next, *prev = NULL;
adamc@683 2889 time_t cutoff;
adamc@683 2890
adamc@683 2891 cutoff = time(NULL) - uw_timeout;
adamc@683 2892
adamc@683 2893 pthread_mutex_lock(&clients_mutex);
adamc@683 2894
adamc@683 2895 for (c = clients_used; c; c = next) {
adamc@683 2896 next = c->next;
adamc@683 2897 pthread_mutex_lock(&c->lock);
adamc@685 2898 if (c->last_contact < cutoff && c->refcount == 0) {
adamc@683 2899 failure_kind fk = UNLIMITED_RETRY;
adamc@683 2900 if (prev)
adamc@683 2901 prev->next = next;
adamc@683 2902 else
adamc@683 2903 clients_used = next;
adamc@684 2904 uw_reset(ctx);
adamc@683 2905 while (fk == UNLIMITED_RETRY) {
adamc@683 2906 fk = uw_expunge(ctx, c->id);
adamc@684 2907 if (fk == UNLIMITED_RETRY) {
adamc@684 2908 uw_db_rollback(ctx);
adamc@684 2909 printf("Unlimited retry during expunge: %s\n", uw_error_message(ctx));
adamc@683 2910 }
adamc@683 2911 }
adamc@684 2912 if (fk == SUCCESS)
adamc@684 2913 free_client(c);
adamc@684 2914 else {
adamc@684 2915 uw_db_rollback(ctx);
adamc@856 2916 fprintf(stderr, "Expunge blocked by error: %s\n", uw_error_message(ctx));
adamc@684 2917 }
adamc@683 2918 }
adamc@683 2919 else
adamc@683 2920 prev = c;
adamc@683 2921 pthread_mutex_unlock(&c->lock);
adamc@683 2922 }
adamc@683 2923
adamc@683 2924 pthread_mutex_unlock(&clients_mutex);
adamc@683 2925 }
adamc@687 2926
adamc@687 2927 void uw_initializer(uw_context ctx);
adamc@687 2928
adamc@687 2929 failure_kind uw_initialize(uw_context ctx) {
adamc@687 2930 int r = setjmp(ctx->jmp_buf);
adamc@687 2931
adamc@687 2932 if (r == 0) {
adamc@687 2933 if (uw_db_begin(ctx))
adamc@687 2934 uw_error(ctx, FATAL, "Error running SQL BEGIN");
adamc@687 2935 uw_initializer(ctx);
adamc@687 2936 if (uw_db_commit(ctx))
adamc@687 2937 uw_error(ctx, FATAL, "Error running SQL COMMIT");
adamc@687 2938 }
adamc@687 2939
adamc@687 2940 return r;
adamc@687 2941 }
adamc@717 2942
adamc@770 2943 extern int uw_check_url(const char *);
adamc@770 2944 extern int uw_check_mime(const char *);
adamc@770 2945
adamc@1065 2946 static int url_bad(uw_Basis_string s) {
adamc@1065 2947 for (; *s; ++s)
adamc@1065 2948 if (!isgraph(*s))
adamc@1065 2949 return 1;
adamc@1065 2950
adamc@1065 2951 return 0;
adamc@1065 2952 }
adamc@1065 2953
adamc@717 2954 uw_Basis_string uw_Basis_bless(uw_context ctx, uw_Basis_string s) {
adamc@1065 2955 if (url_bad(s))
adamc@1065 2956 uw_error(ctx, FATAL, "Invalid URL %s", uw_Basis_htmlifyString(ctx, s));
adamc@770 2957 if (uw_check_url(s))
adamc@770 2958 return s;
adamc@770 2959 else
adamc@770 2960 uw_error(ctx, FATAL, "Disallowed URL %s", uw_Basis_htmlifyString(ctx, s));
adamc@717 2961 }
adamc@734 2962
adamc@770 2963 uw_Basis_string uw_Basis_checkUrl(uw_context ctx, uw_Basis_string s) {
adamc@1065 2964 if (url_bad(s))
adamc@1065 2965 return NULL;
adamc@770 2966 if (uw_check_url(s))
adamc@770 2967 return s;
adamc@770 2968 else
adamc@770 2969 return NULL;
adamc@770 2970 }
adamc@770 2971
adamc@770 2972 int mime_format(const char *s) {
adamc@770 2973 for (; *s; ++s)
adamc@770 2974 if (!isalnum(*s) && *s != '/' && *s != '-' && *s != '.')
adamc@770 2975 return 0;
adamc@770 2976
adamc@770 2977 return 1;
adamc@770 2978 }
adamc@770 2979
adamc@741 2980 uw_Basis_string uw_Basis_blessMime(uw_context ctx, uw_Basis_string s) {
adamc@770 2981 if (!mime_format(s))
adamc@770 2982 uw_error(ctx, FATAL, "MIME type \"%s\" contains invalid character", uw_Basis_htmlifyString(ctx, s));
adamc@770 2983
adamc@770 2984 if (uw_check_mime(s))
adamc@770 2985 return s;
adamc@770 2986 else
adamc@770 2987 uw_error(ctx, FATAL, "Disallowed MIME type %s", uw_Basis_htmlifyString(ctx, s));
adamc@770 2988 }
adamc@770 2989
adamc@770 2990 uw_Basis_string uw_Basis_checkMime(uw_context ctx, uw_Basis_string s) {
adamc@770 2991 if (!mime_format(s))
adamc@770 2992 return NULL;
adamc@770 2993
adamc@770 2994 if (uw_check_mime(s))
adamc@770 2995 return s;
adamc@770 2996 else
adamc@770 2997 return NULL;
adamc@741 2998 }
adamc@741 2999
adamc@734 3000 uw_Basis_string uw_unnull(uw_Basis_string s) {
adamc@734 3001 return s ? s : "";
adamc@734 3002 }
adamc@734 3003
adamc@734 3004 extern int uw_hash_blocksize;
adamc@734 3005
adamc@734 3006 uw_Basis_string uw_Basis_makeSigString(uw_context ctx, uw_Basis_string sig) {
adamc@734 3007 uw_Basis_string r = uw_malloc(ctx, 2 * uw_hash_blocksize + 1);
adamc@734 3008 int i;
adamc@734 3009
adamc@734 3010 for (i = 0; i < uw_hash_blocksize; ++i)
adamc@734 3011 sprintf(&r[2*i], "%.02X", ((unsigned char *)sig)[i]);
adamc@734 3012
adamc@734 3013 return r;
adamc@734 3014 }
adamc@734 3015
adamc@734 3016 uw_Basis_string uw_Basis_sigString(uw_context ctx, uw_unit u) {
adamc@734 3017 return uw_cookie_sig(ctx);
adamc@734 3018 }
adamc@737 3019
adamc@737 3020 uw_Basis_string uw_Basis_fileName(uw_context ctx, uw_Basis_file f) {
adamc@737 3021 return f.name;
adamc@737 3022 }
adamc@737 3023
adamc@740 3024 uw_Basis_string uw_Basis_fileMimeType(uw_context ctx, uw_Basis_file f) {
adamc@740 3025 return f.type;
adamc@740 3026 }
adamc@740 3027
adamc@745 3028 uw_Basis_int uw_Basis_blobSize(uw_context ctx, uw_Basis_blob b) {
adamc@745 3029 return b.size;
adamc@745 3030 }
adamc@745 3031
adamc@737 3032 uw_Basis_blob uw_Basis_fileData(uw_context ctx, uw_Basis_file f) {
adamc@737 3033 return f.data;
adamc@737 3034 }
adamc@741 3035
adamc@741 3036 __attribute__((noreturn)) void uw_return_blob(uw_context ctx, uw_Basis_blob b, uw_Basis_string mimeType) {
adamc@741 3037 cleanup *cl;
adamc@741 3038 int len;
adamc@741 3039
adamc@1065 3040 ctx->returning_indirectly = 1;
adamc@741 3041 buf_reset(&ctx->outHeaders);
adamc@741 3042 buf_reset(&ctx->page);
adamc@741 3043
adamc@856 3044 uw_write_header(ctx, on_success);
adamc@856 3045 uw_write_header(ctx, "Content-Type: ");
adamc@741 3046 uw_write_header(ctx, mimeType);
adamc@741 3047 uw_write_header(ctx, "\r\nContent-Length: ");
adamc@741 3048 buf_check(&ctx->outHeaders, INTS_MAX);
adamc@741 3049 sprintf(ctx->outHeaders.front, "%d%n", b.size, &len);
adamc@741 3050 ctx->outHeaders.front += len;
adamc@741 3051 uw_write_header(ctx, "\r\n");
adamc@741 3052
adamc@741 3053 buf_append(&ctx->page, b.data, b.size);
adamc@741 3054
adamc@741 3055 for (cl = ctx->cleanup; cl < ctx->cleanup_front; ++cl)
adamc@741 3056 cl->func(cl->arg);
adamc@741 3057
adamc@741 3058 ctx->cleanup_front = ctx->cleanup;
adamc@741 3059
adamc@1065 3060 longjmp(ctx->jmp_buf, RETURN_INDIRECTLY);
adamc@1065 3061 }
adamc@1065 3062
adamc@1065 3063 __attribute__((noreturn)) void uw_redirect(uw_context ctx, uw_Basis_string url) {
adamc@1065 3064 cleanup *cl;
adamc@1065 3065 int len;
adamc@1065 3066
adamc@1065 3067 ctx->returning_indirectly = 1;
adamc@1065 3068 buf_reset(&ctx->outHeaders);
adamc@1065 3069 buf_reset(&ctx->page);
adamc@1065 3070
adamc@1065 3071 uw_write_header(ctx, on_redirect);
adamc@1065 3072 uw_write_header(ctx, "Location: ");
adamc@1065 3073 uw_write_header(ctx, url);
adamc@1065 3074 uw_write_header(ctx, "\r\n\r\n");
adamc@1065 3075
adamc@1065 3076 for (cl = ctx->cleanup; cl < ctx->cleanup_front; ++cl)
adamc@1065 3077 cl->func(cl->arg);
adamc@1065 3078
adamc@1065 3079 ctx->cleanup_front = ctx->cleanup;
adamc@1065 3080
adamc@1065 3081 longjmp(ctx->jmp_buf, RETURN_INDIRECTLY);
adamc@741 3082 }
adamc@874 3083
adamc@874 3084 uw_Basis_string uw_Basis_unAs(uw_context ctx, uw_Basis_string s) {
adamc@874 3085 uw_Basis_string r = uw_malloc(ctx, strlen(s) + 1);
adamc@874 3086
adamc@874 3087 for (; *s; ++s) {
adamc@874 3088 if (s[0] == '\'') {
adamc@874 3089 *r++ = '\'';
adamc@874 3090 for (++s; *s; ++s) {
adamc@874 3091 if (s[0] == '\'') {
adamc@874 3092 *r++ = '\'';
adamc@874 3093 break;
adamc@874 3094 } else if (s[0] == '\\') {
adamc@874 3095 if (s[1] == '\\') {
adamc@874 3096 *r++ = '\\';
adamc@874 3097 *r++ = '\\';
adamc@874 3098 ++s;
adamc@874 3099 } else if (s[1] == '\'') {
adamc@874 3100 *r++ = '\\';
adamc@874 3101 *r++ = '\'';
adamc@874 3102 ++s;
adamc@874 3103 } else
adamc@874 3104 *r++ = '\'';
adamc@874 3105 } else
adamc@874 3106 *r++ = s[0];
adamc@874 3107 }
adamc@874 3108 if (*s == 0) break;
adamc@874 3109 } else if (s[0] == 'T' && s[1] == '.')
adamc@874 3110 ++s;
adamc@874 3111 else
adamc@874 3112 *r++ = s[0];
adamc@874 3113 }
adamc@874 3114
adamc@874 3115 return r;
adamc@874 3116 }
adamc@874 3117
adamc@922 3118 uw_Basis_string uw_Basis_mstrcat(uw_context ctx, ...) {
adamc@922 3119 va_list ap;
adamc@922 3120 size_t len = 1;
adamc@922 3121 char *s, *r, *s2;
adamc@922 3122
adamc@922 3123 va_start(ap, ctx);
adamc@922 3124 for (s = va_arg(ap, char*); s; s = va_arg(ap, char*))
adamc@922 3125 len += strlen(s);
adamc@922 3126 va_end(ap);
adamc@922 3127
adamc@922 3128 r = uw_malloc(ctx, len);
adamc@922 3129 va_start(ap, ctx);
adamc@922 3130 for (s2 = r, s = va_arg(ap, char*); s; s = va_arg(ap, char*))
adamc@922 3131 while (*s)
adamc@922 3132 *s2++ = *s++;
adamc@922 3133 va_end(ap);
adamc@922 3134 *s2 = 0;
adamc@922 3135
adamc@922 3136 return r;
adamc@922 3137 }
adamc@1006 3138
adamc@1050 3139 const uw_Basis_time minTime = 0;
adamc@1050 3140
adamc@1006 3141 uw_Basis_time uw_Basis_now(uw_context ctx) {
adamc@1006 3142 return time(NULL);
adamc@1006 3143 }
adamc@1061 3144
adamc@1061 3145 void *uw_get_global(uw_context ctx, char *name) {
adamc@1061 3146 int i;
adamc@1061 3147
adamc@1061 3148 for (i = 0; i < ctx->n_globals; ++i)
adamc@1061 3149 if (!strcmp(name, ctx->globals[i].name))
adamc@1061 3150 return ctx->globals[i].data;
adamc@1061 3151
adamc@1061 3152 return NULL;
adamc@1061 3153 }
adamc@1061 3154
adamc@1061 3155 void uw_set_global(uw_context ctx, char *name, void *data, void (*free)(void*)) {
adamc@1061 3156 int i;
adamc@1061 3157
adamc@1061 3158 if (data == NULL) uw_error(ctx, FATAL, "NULL data value for global '%s'", name);
adamc@1061 3159
adamc@1061 3160 for (i = 0; i < ctx->n_globals; ++i)
adamc@1061 3161 if (!strcmp(name, ctx->globals[i].name)) {
adamc@1061 3162 if (ctx->globals[i].data)
adamc@1061 3163 ctx->globals[i].free(ctx->globals[i].data);
adamc@1061 3164 ctx->globals[i].data = data;
adamc@1061 3165 ctx->globals[i].free = free;
adamc@1061 3166 return;
adamc@1061 3167 }
adamc@1061 3168
adamc@1061 3169 ++ctx->n_globals;
adamc@1061 3170 ctx->globals = realloc(ctx->globals, ctx->n_globals * sizeof(global));
adamc@1061 3171 ctx->globals[ctx->n_globals-1].name = name;
adamc@1061 3172 ctx->globals[ctx->n_globals-1].data = data;
adamc@1061 3173 ctx->globals[ctx->n_globals-1].free = free;
adamc@1061 3174 }
adamc@1061 3175
adamc@1061 3176 uw_Basis_bool uw_Basis_isalnum(uw_context ctx, uw_Basis_char c) {
adamc@1063 3177 return !!isalnum(c);
adamc@1061 3178 }
adamc@1061 3179
adamc@1061 3180 uw_Basis_bool uw_Basis_isalpha(uw_context ctx, uw_Basis_char c) {
adamc@1063 3181 return !!isalpha(c);
adamc@1061 3182 }
adamc@1061 3183
adamc@1061 3184 uw_Basis_bool uw_Basis_isblank(uw_context ctx, uw_Basis_char c) {
adamc@1063 3185 return !!isblank(c);
adamc@1061 3186 }
adamc@1061 3187
adamc@1061 3188 uw_Basis_bool uw_Basis_iscntrl(uw_context ctx, uw_Basis_char c) {
adamc@1063 3189 return !!iscntrl(c);
adamc@1061 3190 }
adamc@1061 3191
adamc@1061 3192 uw_Basis_bool uw_Basis_isdigit(uw_context ctx, uw_Basis_char c) {
adamc@1063 3193 return !!isdigit(c);
adamc@1061 3194 }
adamc@1061 3195
adamc@1061 3196 uw_Basis_bool uw_Basis_isgraph(uw_context ctx, uw_Basis_char c) {
adamc@1063 3197 return !!isgraph(c);
adamc@1061 3198 }
adamc@1061 3199
adamc@1061 3200 uw_Basis_bool uw_Basis_islower(uw_context ctx, uw_Basis_char c) {
adamc@1063 3201 return !!islower(c);
adamc@1061 3202 }
adamc@1061 3203
adamc@1061 3204 uw_Basis_bool uw_Basis_isprint(uw_context ctx, uw_Basis_char c) {
adamc@1063 3205 return !!isprint(c);
adamc@1061 3206 }
adamc@1061 3207
adamc@1061 3208 uw_Basis_bool uw_Basis_ispunct(uw_context ctx, uw_Basis_char c) {
adamc@1063 3209 return !!ispunct(c);
adamc@1061 3210 }
adamc@1061 3211
adamc@1061 3212 uw_Basis_bool uw_Basis_isspace(uw_context ctx, uw_Basis_char c) {
adamc@1063 3213 return !!isspace(c);
adamc@1061 3214 }
adamc@1063 3215
adamc@1061 3216 uw_Basis_bool uw_Basis_isupper(uw_context ctx, uw_Basis_char c) {
adamc@1063 3217 return !!isupper(c);
adamc@1061 3218 }
adamc@1061 3219
adamc@1061 3220 uw_Basis_bool uw_Basis_isxdigit(uw_context ctx, uw_Basis_char c) {
adamc@1063 3221 return !!isxdigit(c);
adamc@1061 3222 }
adamc@1061 3223
adamc@1061 3224 uw_Basis_char uw_Basis_tolower(uw_context ctx, uw_Basis_char c) {
adamc@1061 3225 return tolower(c);
adamc@1061 3226 }
adamc@1061 3227
adamc@1061 3228 uw_Basis_char uw_Basis_toupper(uw_context ctx, uw_Basis_char c) {
adamc@1061 3229 return toupper(c);
adamc@1061 3230 }