annotate src/c/urweb.c @ 1128:e1cf925e2074

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