annotate src/c/urweb.c @ 1063:e3f6620afd51

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