adamc@436: #define _XOPEN_SOURCE adamc@436: adamc@117: #include adamc@102: #include adamc@117: #include adamc@457: #include adamc@106: #include adamc@167: #include adamc@167: #include adamc@102: adamc@102: #include "types.h" adamc@102: adamc@311: uw_unit uw_unit_v = {}; adamc@102: adamc@167: #define ERROR_BUF_LEN 1024 adamc@167: adamc@323: typedef struct regions { adamc@323: struct regions *next; adamc@323: } regions; adamc@323: adamc@425: typedef struct { adamc@425: void (*func)(void*); adamc@425: void *arg; adamc@425: } cleanup; adamc@425: adamc@311: struct uw_context { adamc@457: char *headers; adamc@457: adamc@117: char *page, *page_front, *page_back; adamc@136: char *heap, *heap_front, *heap_back; adamc@144: char **inputs; adamc@167: adamc@272: void *db; adamc@272: adamc@167: jmp_buf jmp_buf; adamc@167: adamc@323: regions *regions; adamc@323: adamc@425: cleanup *cleanup, *cleanup_front, *cleanup_back; adamc@425: adamc@167: char error_message[ERROR_BUF_LEN]; adamc@117: }; adamc@117: adamc@311: extern int uw_inputs_len; adamc@144: adamc@311: uw_context uw_init(size_t page_len, size_t heap_len) { adamc@311: uw_context ctx = malloc(sizeof(struct uw_context)); adamc@136: adamc@457: ctx->headers = NULL; adamc@457: adamc@117: ctx->page_front = ctx->page = malloc(page_len); adamc@117: ctx->page_back = ctx->page_front + page_len; adamc@136: adamc@136: ctx->heap_front = ctx->heap = malloc(heap_len); adamc@136: ctx->heap_back = ctx->heap_front + heap_len; adamc@136: adamc@311: ctx->inputs = calloc(uw_inputs_len, sizeof(char *)); adamc@144: adamc@272: ctx->db = NULL; adamc@272: adamc@323: ctx->regions = NULL; adamc@323: adamc@425: ctx->cleanup_front = ctx->cleanup_back = ctx->cleanup = malloc(0); adamc@425: adamc@167: ctx->error_message[0] = 0; adamc@167: adamc@117: return ctx; adamc@106: } adamc@106: adamc@311: void uw_set_db(uw_context ctx, void *db) { adamc@272: ctx->db = db; adamc@272: } adamc@272: adamc@311: void *uw_get_db(uw_context ctx) { adamc@272: return ctx->db; adamc@272: } adamc@272: adamc@311: void uw_free(uw_context ctx) { adamc@117: free(ctx->page); adamc@136: free(ctx->heap); adamc@144: free(ctx->inputs); adamc@425: free(ctx->cleanup); adamc@136: free(ctx); adamc@136: } adamc@136: adamc@311: void uw_reset_keep_request(uw_context ctx) { adamc@138: ctx->page_front = ctx->page; adamc@138: ctx->heap_front = ctx->heap; adamc@323: ctx->regions = NULL; adamc@425: ctx->cleanup_front = ctx->cleanup; adamc@167: adamc@167: ctx->error_message[0] = 0; adamc@167: } adamc@167: adamc@311: void uw_reset_keep_error_message(uw_context ctx) { adamc@167: ctx->page_front = ctx->page; adamc@167: ctx->heap_front = ctx->heap; adamc@323: ctx->regions = NULL; adamc@425: ctx->cleanup_front = ctx->cleanup; adamc@167: } adamc@167: adamc@311: void uw_reset(uw_context ctx) { adamc@311: uw_reset_keep_request(ctx); adamc@311: memset(ctx->inputs, 0, uw_inputs_len * sizeof(char *)); adamc@144: } adamc@144: adamc@311: void uw_db_init(uw_context); adamc@311: void uw_handle(uw_context, char *); adamc@167: adamc@311: failure_kind uw_begin_init(uw_context ctx) { adamc@272: int r = setjmp(ctx->jmp_buf); adamc@272: adamc@272: if (r == 0) adamc@311: uw_db_init(ctx); adamc@272: adamc@272: return r; adamc@272: } adamc@272: adamc@457: failure_kind uw_begin(uw_context ctx, char *headers, char *path) { adamc@190: int r = setjmp(ctx->jmp_buf); adamc@190: adamc@457: ctx->headers = headers; adamc@457: adamc@190: if (r == 0) adamc@311: uw_handle(ctx, path); adamc@167: adamc@190: return r; adamc@167: } adamc@167: adamc@311: __attribute__((noreturn)) void uw_error(uw_context ctx, failure_kind fk, const char *fmt, ...) { adamc@425: cleanup *cl; adamc@425: adamc@167: va_list ap; adamc@167: va_start(ap, fmt); adamc@167: adamc@167: vsnprintf(ctx->error_message, ERROR_BUF_LEN, fmt, ap); adamc@167: adamc@425: for (cl = ctx->cleanup; cl < ctx->cleanup_front; ++cl) adamc@425: cl->func(cl->arg); adamc@425: adamc@425: ctx->cleanup_front = ctx->cleanup; adamc@425: adamc@190: longjmp(ctx->jmp_buf, fk); adamc@167: } adamc@167: adamc@425: void uw_push_cleanup(uw_context ctx, void (*func)(void *), void *arg) { adamc@425: if (ctx->cleanup_front >= ctx->cleanup_back) { adamc@425: int len = ctx->cleanup_back - ctx->cleanup, newLen; adamc@425: if (len == 0) adamc@425: newLen = 1; adamc@425: else adamc@428: newLen = len * 2; adamc@425: ctx->cleanup = realloc(ctx->cleanup, newLen); adamc@425: ctx->cleanup_front = ctx->cleanup + len; adamc@425: ctx->cleanup_back = ctx->cleanup + newLen; adamc@425: } adamc@425: adamc@425: ctx->cleanup_front->func = func; adamc@425: ctx->cleanup_front->arg = arg; adamc@425: ++ctx->cleanup_front; adamc@425: } adamc@425: adamc@425: void uw_pop_cleanup(uw_context ctx) { adamc@425: if (ctx->cleanup_front == ctx->cleanup) adamc@425: uw_error(ctx, FATAL, "Attempt to pop from empty cleanup action stack"); adamc@425: adamc@425: --ctx->cleanup_front; adamc@425: ctx->cleanup_front->func(ctx->cleanup_front->arg); adamc@425: } adamc@425: adamc@311: char *uw_error_message(uw_context ctx) { adamc@167: return ctx->error_message; adamc@167: } adamc@167: adamc@311: int uw_input_num(char*); adamc@144: adamc@311: void uw_set_input(uw_context ctx, char *name, char *value) { adamc@311: int n = uw_input_num(name); adamc@144: adamc@169: if (n < 0) adamc@311: uw_error(ctx, FATAL, "Bad input name %s", name); adamc@144: adamc@311: if (n >= uw_inputs_len) adamc@311: uw_error(ctx, FATAL, "For input name %s, index %d is out of range", name, n); adamc@169: adamc@144: ctx->inputs[n] = value; adamc@144: adamc@282: //printf("[%d] %s = %s\n", n, name, value); adamc@144: } adamc@144: adamc@311: char *uw_get_input(uw_context ctx, int n) { adamc@169: if (n < 0) adamc@311: uw_error(ctx, FATAL, "Negative input index %d", n); adamc@311: if (n >= uw_inputs_len) adamc@311: uw_error(ctx, FATAL, "Out-of-bounds input index %d", n); adamc@282: //printf("[%d] = %s\n", n, ctx->inputs[n]); adamc@144: return ctx->inputs[n]; adamc@138: } adamc@138: adamc@311: char *uw_get_optional_input(uw_context ctx, int n) { adamc@190: if (n < 0) adamc@311: uw_error(ctx, FATAL, "Negative input index %d", n); adamc@311: if (n >= uw_inputs_len) adamc@311: uw_error(ctx, FATAL, "Out-of-bounds input index %d", n); adamc@426: //printf("[%d] = %s\n", n, ctx->inputs[n]); adamc@190: return (ctx->inputs[n] == NULL ? "" : ctx->inputs[n]); adamc@190: } adamc@190: adamc@311: static void uw_check_heap(uw_context ctx, size_t extra) { adamc@136: if (ctx->heap_back - ctx->heap_front < extra) { adamc@321: size_t desired = ctx->heap_front - ctx->heap + extra, next; adamc@136: char *new_heap; adamc@136: adamc@317: next = ctx->heap_back - ctx->heap; adamc@317: if (next == 0) adamc@317: next = 1; adamc@317: for (; next < desired; next *= 2); adamc@136: adamc@136: new_heap = realloc(ctx->heap, next); adamc@321: ctx->heap_front = new_heap + (ctx->heap_front - ctx->heap); adamc@169: ctx->heap_back = new_heap + next; adamc@136: adamc@136: if (new_heap != ctx->heap) { adamc@136: ctx->heap = new_heap; adamc@311: uw_error(ctx, UNLIMITED_RETRY, "Couldn't allocate new heap chunk contiguously"); adamc@136: } adamc@136: adamc@169: ctx->heap = new_heap; adamc@136: } adamc@136: } adamc@136: adamc@311: void *uw_malloc(uw_context ctx, size_t len) { adamc@136: void *result; adamc@136: adamc@311: uw_check_heap(ctx, len); adamc@136: adamc@136: result = ctx->heap_front; adamc@136: ctx->heap_front += len; adamc@136: return result; adamc@117: } adamc@117: adamc@323: void uw_begin_region(uw_context ctx) { adamc@323: regions *r = (regions *) ctx->heap_front; adamc@323: adamc@323: uw_check_heap(ctx, sizeof(regions)); adamc@323: adamc@323: ctx->heap_front += sizeof(regions); adamc@323: adamc@323: r->next = ctx->regions; adamc@323: ctx->regions = r; adamc@323: } adamc@323: adamc@323: void uw_end_region(uw_context ctx) { adamc@323: regions *r = ctx->regions; adamc@323: adamc@323: if (r == NULL) adamc@323: uw_error(ctx, FATAL, "Region stack underflow"); adamc@323: adamc@323: ctx->heap_front = (char *) r; adamc@323: ctx->regions = r->next; adamc@323: } adamc@323: adamc@324: void uw_memstats(uw_context ctx) { adamc@324: printf("Page: %d/%d\n", ctx->page_front - ctx->page, ctx->page_back - ctx->page); adamc@324: printf("Heap: %d/%d\n", ctx->heap_front - ctx->heap, ctx->heap_back - ctx->heap); adamc@324: } adamc@324: adamc@436: int uw_really_send(int sock, const void *buf, size_t len) { adamc@117: while (len > 0) { adamc@436: size_t n = send(sock, buf, len, 0); adamc@117: adamc@117: if (n < 0) adamc@117: return n; adamc@117: adamc@117: buf += n; adamc@117: len -= n; adamc@117: } adamc@117: adamc@117: return 0; adamc@117: } adamc@117: adamc@311: int uw_send(uw_context ctx, int sock) { adamc@311: return uw_really_send(sock, ctx->page, ctx->page_front - ctx->page); adamc@117: } adamc@117: adamc@311: static void uw_check(uw_context ctx, size_t extra) { adamc@321: size_t desired = ctx->page_front - ctx->page + extra, next; adamc@117: char *new_page; adamc@117: adamc@317: next = ctx->page_back - ctx->page; adamc@428: if (next < desired) { adamc@428: if (next == 0) adamc@428: next = 1; adamc@428: for (; next < desired; next *= 2); adamc@117: adamc@428: new_page = realloc(ctx->page, next); adamc@428: ctx->page_front = new_page + (ctx->page_front - ctx->page); adamc@428: ctx->page_back = new_page + next; adamc@428: ctx->page = new_page; adamc@428: } adamc@117: } adamc@117: adamc@311: static void uw_writec_unsafe(uw_context ctx, char c) { adamc@117: *(ctx->page_front)++ = c; adamc@117: } adamc@117: adamc@311: void uw_writec(uw_context ctx, char c) { adamc@311: uw_check(ctx, 1); adamc@311: uw_writec_unsafe(ctx, c); adamc@117: } adamc@117: adamc@311: static void uw_write_unsafe(uw_context ctx, const char* s) { adamc@117: int len = strlen(s); adamc@117: memcpy(ctx->page_front, s, len); adamc@117: ctx->page_front += len; adamc@117: } adamc@117: adamc@311: void uw_write(uw_context ctx, const char* s) { adamc@311: uw_check(ctx, strlen(s) + 1); adamc@311: uw_write_unsafe(ctx, s); adamc@183: *ctx->page_front = 0; adamc@102: } adamc@106: adamc@135: adamc@311: char *uw_Basis_attrifyInt(uw_context ctx, uw_Basis_int n) { adamc@136: char *result; adamc@136: int len; adamc@311: uw_check_heap(ctx, INTS_MAX); adamc@136: result = ctx->heap_front; adamc@276: sprintf(result, "%lld%n", n, &len); adamc@137: ctx->heap_front += len+1; adamc@136: return result; adamc@106: } adamc@106: adamc@311: char *uw_Basis_attrifyFloat(uw_context ctx, uw_Basis_float n) { adamc@136: char *result; adamc@136: int len; adamc@311: uw_check_heap(ctx, FLOATS_MAX); adamc@136: result = ctx->heap_front; adamc@136: sprintf(result, "%g%n", n, &len); adamc@137: ctx->heap_front += len+1; adamc@136: return result; adamc@106: } adamc@106: adamc@311: char *uw_Basis_attrifyString(uw_context ctx, uw_Basis_string s) { adamc@136: int len = strlen(s); adamc@136: char *result, *p; adamc@311: uw_check_heap(ctx, len * 6 + 1); adamc@136: adamc@136: result = p = ctx->heap_front; adamc@136: adamc@136: for (; *s; s++) { adamc@136: char c = *s; adamc@136: adamc@136: if (c == '"') { adamc@136: strcpy(p, """); adamc@136: p += 6; adamc@136: } else if (c == '&') { adamc@136: strcpy(p, "&"); adamc@136: p += 5; adamc@136: } adamc@136: else if (isprint(c)) adamc@136: *p++ = c; adamc@136: else { adamc@136: int len2; adamc@136: sprintf(p, "&#%d;%n", c, &len2); adamc@136: p += len2; adamc@136: } adamc@136: } adamc@136: adamc@137: *p++ = 0; adamc@136: ctx->heap_front = p; adamc@136: return result; adamc@106: } adamc@106: adamc@311: static void uw_Basis_attrifyInt_w_unsafe(uw_context ctx, uw_Basis_int n) { adamc@117: int len; adamc@117: adamc@276: sprintf(ctx->page_front, "%lld%n", n, &len); adamc@117: ctx->page_front += len; adamc@106: } adamc@106: adamc@428: uw_unit uw_Basis_attrifyInt_w(uw_context ctx, uw_Basis_int n) { adamc@311: uw_check(ctx, INTS_MAX); adamc@311: uw_Basis_attrifyInt_w_unsafe(ctx, n); adamc@428: adamc@428: return uw_unit_v; adamc@106: } adamc@106: adamc@428: uw_unit uw_Basis_attrifyFloat_w(uw_context ctx, uw_Basis_float n) { adamc@117: int len; adamc@117: adamc@311: uw_check(ctx, FLOATS_MAX); adamc@117: sprintf(ctx->page_front, "%g%n", n, &len); adamc@117: ctx->page_front += len; adamc@428: adamc@428: return uw_unit_v; adamc@117: } adamc@117: adamc@428: uw_unit uw_Basis_attrifyString_w(uw_context ctx, uw_Basis_string s) { adamc@311: uw_check(ctx, strlen(s) * 6); adamc@117: adamc@106: for (; *s; s++) { adamc@106: char c = *s; adamc@106: adamc@106: if (c == '"') adamc@311: uw_write_unsafe(ctx, """); adamc@136: else if (c == '&') adamc@311: uw_write_unsafe(ctx, "&"); adamc@106: else if (isprint(c)) adamc@311: uw_writec_unsafe(ctx, c); adamc@106: else { adamc@311: uw_write_unsafe(ctx, "&#"); adamc@311: uw_Basis_attrifyInt_w_unsafe(ctx, c); adamc@311: uw_writec_unsafe(ctx, ';'); adamc@106: } adamc@106: } adamc@428: adamc@428: return uw_unit_v; adamc@106: } adamc@120: adamc@120: adamc@311: char *uw_Basis_urlifyInt(uw_context ctx, uw_Basis_int n) { adamc@137: int len; adamc@137: char *r; adamc@137: adamc@311: uw_check_heap(ctx, INTS_MAX); adamc@137: r = ctx->heap_front; adamc@276: sprintf(r, "%lld%n", n, &len); adamc@137: ctx->heap_front += len+1; adamc@137: return r; adamc@120: } adamc@120: adamc@311: char *uw_Basis_urlifyFloat(uw_context ctx, uw_Basis_float n) { adamc@137: int len; adamc@137: char *r; adamc@137: adamc@311: uw_check_heap(ctx, FLOATS_MAX); adamc@137: r = ctx->heap_front; adamc@137: sprintf(r, "%g%n", n, &len); adamc@137: ctx->heap_front += len+1; adamc@137: return r; adamc@120: } adamc@120: adamc@311: char *uw_Basis_urlifyString(uw_context ctx, uw_Basis_string s) { adamc@137: char *r, *p; adamc@137: adamc@311: uw_check_heap(ctx, strlen(s) * 3 + 1); adamc@137: adamc@137: for (r = p = ctx->heap_front; *s; s++) { adamc@137: char c = *s; adamc@137: adamc@137: if (c == ' ') adamc@137: *p++ = '+'; adamc@137: else if (isalnum(c)) adamc@137: *p++ = c; adamc@137: else { adamc@137: sprintf(p, "%%%02X", c); adamc@137: p += 3; adamc@137: } adamc@137: } adamc@137: adamc@137: *p++ = 0; adamc@137: ctx->heap_front = p; adamc@137: return r; adamc@120: } adamc@120: adamc@311: char *uw_Basis_urlifyBool(uw_context ctx, uw_Basis_bool b) { adamc@311: if (b == uw_Basis_False) adamc@186: return "0"; adamc@186: else adamc@186: return "1"; adamc@186: } adamc@186: adamc@311: static void uw_Basis_urlifyInt_w_unsafe(uw_context ctx, uw_Basis_int n) { adamc@120: int len; adamc@120: adamc@276: sprintf(ctx->page_front, "%lld%n", n, &len); adamc@120: ctx->page_front += len; adamc@120: } adamc@120: adamc@428: uw_unit uw_Basis_urlifyInt_w(uw_context ctx, uw_Basis_int n) { adamc@311: uw_check(ctx, INTS_MAX); adamc@311: uw_Basis_urlifyInt_w_unsafe(ctx, n); adamc@428: adamc@428: return uw_unit_v; adamc@120: } adamc@120: adamc@428: uw_unit uw_Basis_urlifyFloat_w(uw_context ctx, uw_Basis_float n) { adamc@120: int len; adamc@120: adamc@311: uw_check(ctx, FLOATS_MAX); adamc@120: sprintf(ctx->page_front, "%g%n", n, &len); adamc@120: ctx->page_front += len; adamc@428: adamc@428: return uw_unit_v; adamc@120: } adamc@120: adamc@428: uw_unit uw_Basis_urlifyString_w(uw_context ctx, uw_Basis_string s) { adamc@311: uw_check(ctx, strlen(s) * 3); adamc@120: adamc@120: for (; *s; s++) { adamc@120: char c = *s; adamc@120: adamc@120: if (c == ' ') adamc@311: uw_writec_unsafe(ctx, '+'); adamc@120: else if (isalnum(c)) adamc@311: uw_writec_unsafe(ctx, c); adamc@120: else { adamc@120: sprintf(ctx->page_front, "%%%02X", c); adamc@120: ctx->page_front += 3; adamc@120: } adamc@120: } adamc@428: adamc@428: return uw_unit_v; adamc@120: } adamc@120: adamc@428: uw_unit uw_Basis_urlifyBool_w(uw_context ctx, uw_Basis_bool b) { adamc@311: if (b == uw_Basis_False) adamc@311: uw_writec(ctx, '0'); adamc@186: else adamc@311: uw_writec(ctx, '1'); adamc@428: adamc@428: return uw_unit_v; adamc@186: } adamc@186: adamc@120: adamc@311: static char *uw_unurlify_advance(char *s) { adamc@144: char *new_s = strchr(s, '/'); adamc@120: adamc@120: if (new_s) adamc@120: *new_s++ = 0; adamc@120: else adamc@144: new_s = strchr(s, 0); adamc@144: adamc@144: return new_s; adamc@144: } adamc@144: adamc@311: uw_Basis_int uw_Basis_unurlifyInt(uw_context ctx, char **s) { adamc@311: char *new_s = uw_unurlify_advance(*s); adamc@311: uw_Basis_int r; adamc@120: adamc@276: r = atoll(*s); adamc@120: *s = new_s; adamc@120: return r; adamc@120: } adamc@120: adamc@311: uw_Basis_float uw_Basis_unurlifyFloat(uw_context ctx, char **s) { adamc@311: char *new_s = uw_unurlify_advance(*s); adamc@311: uw_Basis_float r; adamc@120: adamc@120: r = atof(*s); adamc@120: *s = new_s; adamc@120: return r; adamc@120: } adamc@120: adamc@311: static uw_Basis_string uw_unurlifyString_to(uw_context ctx, char *r, char *s) { adamc@144: char *s1, *s2; adamc@144: int n; adamc@136: adamc@144: for (s1 = r, s2 = s; *s2; ++s1, ++s2) { adamc@136: char c = *s2; adamc@136: adamc@136: switch (c) { adamc@136: case '+': adamc@136: *s1 = ' '; adamc@136: break; adamc@136: case '%': adamc@169: if (s2[1] == 0) adamc@311: uw_error(ctx, FATAL, "Missing first character of escaped URL byte"); adamc@169: if (s2[2] == 0) adamc@311: uw_error(ctx, FATAL, "Missing second character of escaped URL byte"); adamc@169: if (sscanf(s2+1, "%02X", &n) != 1) adamc@311: uw_error(ctx, FATAL, "Invalid escaped URL byte starting at: %s", s2); adamc@136: *s1 = n; adamc@136: s2 += 2; adamc@136: break; adamc@136: default: adamc@136: *s1 = c; adamc@136: } adamc@136: } adamc@136: *s1++ = 0; adamc@144: return s1; adamc@144: } adamc@144: adamc@311: uw_Basis_bool uw_Basis_unurlifyBool(uw_context ctx, char **s) { adamc@311: char *new_s = uw_unurlify_advance(*s); adamc@311: uw_Basis_bool r; adamc@186: adamc@186: if (*s[0] == 0 || !strcmp(*s, "0") || !strcmp(*s, "off")) adamc@311: r = uw_Basis_False; adamc@186: else adamc@311: r = uw_Basis_True; adamc@186: adamc@186: *s = new_s; adamc@186: return r; adamc@186: } adamc@186: adamc@311: uw_Basis_string uw_Basis_unurlifyString(uw_context ctx, char **s) { adamc@311: char *new_s = uw_unurlify_advance(*s); adamc@144: char *r, *s1, *s2; adamc@144: int len, n; adamc@144: adamc@200: len = strlen(*s); adamc@311: uw_check_heap(ctx, len + 1); adamc@144: adamc@144: r = ctx->heap_front; adamc@311: ctx->heap_front = uw_unurlifyString_to(ctx, ctx->heap_front, *s); adamc@136: *s = new_s; adamc@136: return r; adamc@120: } adamc@135: adamc@135: adamc@311: char *uw_Basis_htmlifyInt(uw_context ctx, uw_Basis_int n) { adamc@286: int len; adamc@286: char *r; adamc@286: adamc@311: uw_check_heap(ctx, INTS_MAX); adamc@286: r = ctx->heap_front; adamc@286: sprintf(r, "%lld%n", n, &len); adamc@286: ctx->heap_front += len+1; adamc@286: return r; adamc@286: } adamc@286: adamc@428: uw_unit uw_Basis_htmlifyInt_w(uw_context ctx, uw_Basis_int n) { adamc@286: int len; adamc@286: adamc@311: uw_check(ctx, INTS_MAX); adamc@286: sprintf(ctx->page_front, "%lld%n", n, &len); adamc@286: ctx->page_front += len; adamc@428: adamc@428: return uw_unit_v; adamc@286: } adamc@286: adamc@311: char *uw_Basis_htmlifyFloat(uw_context ctx, uw_Basis_float n) { adamc@286: int len; adamc@286: char *r; adamc@286: adamc@311: uw_check_heap(ctx, FLOATS_MAX); adamc@286: r = ctx->heap_front; adamc@286: sprintf(r, "%g%n", n, &len); adamc@286: ctx->heap_front += len+1; adamc@286: return r; adamc@286: } adamc@286: adamc@428: uw_unit uw_Basis_htmlifyFloat_w(uw_context ctx, uw_Basis_float n) { adamc@286: int len; adamc@286: adamc@311: uw_check(ctx, FLOATS_MAX); adamc@286: sprintf(ctx->page_front, "%g%n", n, &len); adamc@286: ctx->page_front += len; adamc@428: adamc@428: return uw_unit_v; adamc@286: } adamc@286: adamc@311: char *uw_Basis_htmlifyString(uw_context ctx, uw_Basis_string s) { adamc@137: char *r, *s2; adamc@137: adamc@311: uw_check_heap(ctx, strlen(s) * 5 + 1); adamc@137: adamc@137: for (r = s2 = ctx->heap_front; *s; s++) { adamc@137: char c = *s; adamc@137: adamc@137: switch (c) { adamc@137: case '<': adamc@137: strcpy(s2, "<"); adamc@137: s2 += 4; adamc@137: break; adamc@137: case '&': adamc@137: strcpy(s2, "&"); adamc@137: s2 += 5; adamc@137: break; adamc@137: default: adamc@137: if (isprint(c)) adamc@137: *s2++ = c; adamc@137: else { adamc@137: int len2; adamc@137: sprintf(s2, "&#%d;%n", c, &len2); adamc@137: s2 += len2; adamc@137: } adamc@137: } adamc@137: } adamc@137: adamc@137: *s2++ = 0; adamc@137: ctx->heap_front = s2; adamc@137: return r; adamc@135: } adamc@135: adamc@428: uw_unit uw_Basis_htmlifyString_w(uw_context ctx, uw_Basis_string s) { adamc@321: uw_check(ctx, strlen(s) * 6); adamc@135: adamc@135: for (; *s; s++) { adamc@135: char c = *s; adamc@135: adamc@135: switch (c) { adamc@135: case '<': adamc@311: uw_write_unsafe(ctx, "<"); adamc@135: break; adamc@135: case '&': adamc@311: uw_write_unsafe(ctx, "&"); adamc@135: break; adamc@135: default: adamc@135: if (isprint(c)) adamc@311: uw_writec_unsafe(ctx, c); adamc@135: else { adamc@311: uw_write_unsafe(ctx, "&#"); adamc@311: uw_Basis_attrifyInt_w_unsafe(ctx, c); adamc@311: uw_writec_unsafe(ctx, ';'); adamc@135: } adamc@135: } adamc@135: } adamc@428: adamc@428: return uw_unit_v; adamc@135: } adamc@180: adamc@311: uw_Basis_string uw_Basis_htmlifyBool(uw_context ctx, uw_Basis_bool b) { adamc@311: if (b == uw_Basis_False) adamc@286: return "False"; adamc@286: else adamc@286: return "True"; adamc@286: } adamc@286: adamc@428: uw_unit uw_Basis_htmlifyBool_w(uw_context ctx, uw_Basis_bool b) { adamc@311: if (b == uw_Basis_False) { adamc@311: uw_check(ctx, 6); adamc@286: strcpy(ctx->page_front, "False"); adamc@286: ctx->page_front += 5; adamc@286: } else { adamc@311: uw_check(ctx, 5); adamc@286: strcpy(ctx->page_front, "True"); adamc@286: ctx->page_front += 4; adamc@286: } adamc@428: adamc@428: return uw_unit_v; adamc@286: } adamc@286: adamc@436: #define TIME_FMT "%x %X" adamc@438: #define TIME_FMT_PG "%Y-%m-%d %T" adamc@436: adamc@436: uw_Basis_string uw_Basis_htmlifyTime(uw_context ctx, uw_Basis_time t) { adamc@436: size_t len; adamc@436: char *r; adamc@436: struct tm stm; adamc@436: adamc@436: if (localtime_r(&t, &stm)) { adamc@436: uw_check_heap(ctx, TIMES_MAX); adamc@436: r = ctx->heap_front; adamc@436: len = strftime(r, TIMES_MAX, TIME_FMT, &stm); adamc@436: ctx->heap_front += len+1; adamc@436: return r; adamc@436: } else adamc@436: return "Invalid time"; adamc@436: } adamc@436: adamc@436: uw_unit uw_Basis_htmlifyTime_w(uw_context ctx, uw_Basis_time t) { adamc@436: size_t len; adamc@436: char *r; adamc@436: struct tm stm; adamc@436: adamc@436: if (localtime_r(&t, &stm)) { adamc@436: uw_check(ctx, TIMES_MAX); adamc@436: r = ctx->page_front; adamc@436: len = strftime(r, TIMES_MAX, TIME_FMT, &stm); adamc@436: ctx->page_front += len; adamc@436: } else { adamc@436: uw_check(ctx, 20); adamc@436: strcpy(ctx->page_front, "Invalid time"); adamc@436: ctx->page_front += 19; adamc@436: } adamc@436: adamc@436: return uw_unit_v; adamc@436: } adamc@436: adamc@311: uw_Basis_string uw_Basis_strcat(uw_context ctx, uw_Basis_string s1, uw_Basis_string s2) { adamc@180: int len = strlen(s1) + strlen(s2) + 1; adamc@180: char *s; adamc@180: adamc@311: uw_check_heap(ctx, len); adamc@180: adamc@180: s = ctx->heap_front; adamc@180: adamc@180: strcpy(s, s1); adamc@180: strcat(s, s2); adamc@180: ctx->heap_front += len; adamc@180: adamc@180: return s; adamc@180: } adamc@278: adamc@311: uw_Basis_string uw_Basis_strdup(uw_context ctx, uw_Basis_string s1) { adamc@278: int len = strlen(s1) + 1; adamc@278: char *s; adamc@278: adamc@311: uw_check_heap(ctx, len); adamc@278: adamc@278: s = ctx->heap_front; adamc@278: adamc@278: strcpy(s, s1); adamc@278: ctx->heap_front += len; adamc@278: adamc@278: return s; adamc@278: } adamc@280: adamc@280: adamc@311: char *uw_Basis_sqlifyInt(uw_context ctx, uw_Basis_int n) { adamc@281: int len; adamc@281: char *r; adamc@281: adamc@311: uw_check_heap(ctx, INTS_MAX + 6); adamc@281: r = ctx->heap_front; adamc@281: sprintf(r, "%lld::int8%n", n, &len); adamc@281: ctx->heap_front += len+1; adamc@281: return r; adamc@281: } adamc@281: adamc@311: char *uw_Basis_sqlifyFloat(uw_context ctx, uw_Basis_float n) { adamc@281: int len; adamc@281: char *r; adamc@281: adamc@311: uw_check_heap(ctx, FLOATS_MAX + 8); adamc@281: r = ctx->heap_front; adamc@281: sprintf(r, "%g::float8%n", n, &len); adamc@281: ctx->heap_front += len+1; adamc@281: return r; adamc@281: } adamc@281: adamc@281: adamc@311: uw_Basis_string uw_Basis_sqlifyString(uw_context ctx, uw_Basis_string s) { adamc@280: char *r, *s2; adamc@280: adamc@311: uw_check_heap(ctx, strlen(s) * 2 + 10); adamc@280: adamc@280: r = s2 = ctx->heap_front; adamc@280: *s2++ = 'E'; adamc@280: *s2++ = '\''; adamc@280: adamc@280: for (; *s; s++) { adamc@280: char c = *s; adamc@280: adamc@280: switch (c) { adamc@280: case '\'': adamc@280: strcpy(s2, "\\'"); adamc@280: s2 += 2; adamc@280: break; adamc@280: case '\\': adamc@280: strcpy(s2, "\\\\"); adamc@280: s2 += 2; adamc@280: break; adamc@280: default: adamc@280: if (isprint(c)) adamc@280: *s2++ = c; adamc@280: else { adamc@280: sprintf(s2, "\\%3o", c); adamc@280: s2 += 4; adamc@280: } adamc@280: } adamc@280: } adamc@280: adamc@281: strcpy(s2, "'::text"); adamc@281: ctx->heap_front = s2 + 8; adamc@280: return r; adamc@280: } adamc@281: adamc@311: char *uw_Basis_sqlifyBool(uw_context ctx, uw_Basis_bool b) { adamc@311: if (b == uw_Basis_False) adamc@281: return "FALSE"; adamc@281: else adamc@281: return "TRUE"; adamc@281: } adamc@282: adamc@439: char *uw_Basis_sqlifyTime(uw_context ctx, uw_Basis_time t) { adamc@439: size_t len; adamc@439: char *r; adamc@439: struct tm stm; adamc@439: adamc@439: if (localtime_r(&t, &stm)) { adamc@439: uw_check_heap(ctx, TIMES_MAX); adamc@439: r = ctx->heap_front; adamc@439: len = strftime(r, TIMES_MAX, TIME_FMT, &stm); adamc@439: ctx->heap_front += len+1; adamc@439: return r; adamc@439: } else adamc@439: return ""; adamc@439: } adamc@439: adamc@311: char *uw_Basis_ensqlBool(uw_Basis_bool b) { adamc@311: static uw_Basis_int true = 1; adamc@311: static uw_Basis_int false = 0; adamc@282: adamc@311: if (b == uw_Basis_False) adamc@282: return (char *)&false; adamc@282: else adamc@282: return (char *)&true; adamc@282: } adamc@284: adamc@311: uw_Basis_string uw_Basis_intToString(uw_context ctx, uw_Basis_int n) { adamc@284: int len; adamc@284: char *r; adamc@284: adamc@311: uw_check_heap(ctx, INTS_MAX); adamc@284: r = ctx->heap_front; adamc@284: sprintf(r, "%lld%n", n, &len); adamc@284: ctx->heap_front += len+1; adamc@284: return r; adamc@284: } adamc@285: adamc@311: uw_Basis_string uw_Basis_floatToString(uw_context ctx, uw_Basis_float n) { adamc@285: int len; adamc@285: char *r; adamc@285: adamc@311: uw_check_heap(ctx, FLOATS_MAX); adamc@285: r = ctx->heap_front; adamc@285: sprintf(r, "%g%n", n, &len); adamc@285: ctx->heap_front += len+1; adamc@285: return r; adamc@285: } adamc@285: adamc@311: uw_Basis_string uw_Basis_boolToString(uw_context ctx, uw_Basis_bool b) { adamc@311: if (b == uw_Basis_False) adamc@285: return "False"; adamc@285: else adamc@285: return "True"; adamc@285: } adamc@288: adamc@436: uw_Basis_string uw_Basis_timeToString(uw_context ctx, uw_Basis_time t) { adamc@436: size_t len; adamc@436: char *r; adamc@436: struct tm stm; adamc@436: adamc@436: if (localtime_r(&t, &stm)) { adamc@436: uw_check_heap(ctx, TIMES_MAX); adamc@436: r = ctx->heap_front; adamc@436: len = strftime(r, TIMES_MAX, TIME_FMT, &stm); adamc@436: ctx->heap_front += len+1; adamc@436: return r; adamc@436: } else adamc@436: return ""; adamc@436: } adamc@288: adamc@311: uw_Basis_int *uw_Basis_stringToInt(uw_context ctx, uw_Basis_string s) { adamc@288: char *endptr; adamc@311: uw_Basis_int n = strtoll(s, &endptr, 10); adamc@288: adamc@288: if (*s != '\0' && *endptr == '\0') { adamc@311: uw_Basis_int *r = uw_malloc(ctx, sizeof(uw_Basis_int)); adamc@288: *r = n; adamc@288: return r; adamc@288: } else adamc@288: return NULL; adamc@288: } adamc@289: adamc@311: uw_Basis_float *uw_Basis_stringToFloat(uw_context ctx, uw_Basis_string s) { adamc@289: char *endptr; adamc@311: uw_Basis_float n = strtod(s, &endptr); adamc@289: adamc@289: if (*s != '\0' && *endptr == '\0') { adamc@311: uw_Basis_float *r = uw_malloc(ctx, sizeof(uw_Basis_float)); adamc@289: *r = n; adamc@289: return r; adamc@289: } else adamc@289: return NULL; adamc@289: } adamc@289: adamc@311: uw_Basis_bool *uw_Basis_stringToBool(uw_context ctx, uw_Basis_string s) { adamc@311: static uw_Basis_bool true = uw_Basis_True; adamc@311: static uw_Basis_bool false = uw_Basis_False; adamc@289: adamc@289: if (!strcasecmp (s, "True")) adamc@289: return &true; adamc@289: else if (!strcasecmp (s, "False")) adamc@289: return &false; adamc@289: else adamc@289: return NULL; adamc@289: } adamc@292: adamc@436: uw_Basis_time *uw_Basis_stringToTime(uw_context ctx, uw_Basis_string s) { adamc@438: char *dot = strchr(s, '.'), *end = strchr(s, 0); adamc@436: struct tm stm; adamc@436: adamc@439: if (dot) { adamc@439: *dot = 0; adamc@439: if (strptime(s, TIME_FMT_PG, &stm) == end) { adamc@439: *dot = '.'; adamc@439: uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time)); adamc@439: *r = mktime(&stm); adamc@439: return r; adamc@439: } adamc@439: else { adamc@439: *dot = '.'; adamc@439: return NULL; adamc@439: } adamc@436: } adamc@439: else { adamc@439: if (strptime(s, TIME_FMT_PG, &stm) == end) { adamc@439: uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time)); adamc@439: *r = mktime(&stm); adamc@439: return r; adamc@439: } adamc@439: else if (strptime(s, TIME_FMT, &stm) == end) { adamc@439: uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time)); adamc@439: *r = mktime(&stm); adamc@439: return r; adamc@439: } adamc@439: else adamc@439: return NULL; adamc@439: } adamc@436: } adamc@436: adamc@311: uw_Basis_int uw_Basis_stringToInt_error(uw_context ctx, uw_Basis_string s) { adamc@292: char *endptr; adamc@311: uw_Basis_int n = strtoll(s, &endptr, 10); adamc@292: adamc@292: if (*s != '\0' && *endptr == '\0') adamc@292: return n; adamc@292: else adamc@311: uw_error(ctx, FATAL, "Can't parse int: %s", s); adamc@292: } adamc@293: adamc@311: uw_Basis_float uw_Basis_stringToFloat_error(uw_context ctx, uw_Basis_string s) { adamc@293: char *endptr; adamc@311: uw_Basis_float n = strtod(s, &endptr); adamc@293: adamc@293: if (*s != '\0' && *endptr == '\0') adamc@293: return n; adamc@293: else adamc@311: uw_error(ctx, FATAL, "Can't parse float: %s", s); adamc@293: } adamc@293: adamc@311: uw_Basis_bool uw_Basis_stringToBool_error(uw_context ctx, uw_Basis_string s) { adamc@296: if (!strcasecmp(s, "T") || !strcasecmp (s, "True")) adamc@311: return uw_Basis_True; adamc@296: else if (!strcasecmp(s, "F") || !strcasecmp (s, "False")) adamc@311: return uw_Basis_False; adamc@293: else adamc@311: uw_error(ctx, FATAL, "Can't parse bool: %s", s); adamc@293: } adamc@436: adamc@436: uw_Basis_time uw_Basis_stringToTime_error(uw_context ctx, uw_Basis_string s) { adamc@438: char *dot = strchr(s, '.'), *end = strchr(s, 0); adamc@436: struct tm stm = {}; adamc@436: adamc@438: if (dot) { adamc@438: *dot = 0; adamc@438: if (strptime(s, TIME_FMT_PG, &stm)) { adamc@438: *dot = '.'; adamc@438: return mktime(&stm); adamc@438: } adamc@438: else { adamc@438: *dot = '.'; adamc@438: uw_error(ctx, FATAL, "Can't parse time: %s", s); adamc@438: } adamc@438: } adamc@438: else { adamc@439: if (strptime(s, TIME_FMT_PG, &stm) == end) adamc@439: return mktime(&stm); adamc@439: else if (strptime(s, TIME_FMT, &stm) == end) adamc@438: return mktime(&stm); adamc@438: else adamc@438: uw_error(ctx, FATAL, "Can't parse time: %s", s); adamc@438: } adamc@436: } adamc@457: adamc@457: uw_Basis_string uw_Basis_requestHeader(uw_context ctx, uw_Basis_string h) { adamc@457: int len = strlen(h); adamc@457: char *s = ctx->headers, *p; adamc@457: adamc@457: while (p = strchr(s, ':')) { adamc@457: if (p - s == len && !strncasecmp(s, h, len)) { adamc@457: s = p + 2; adamc@457: if (p = strchr(s, '\r')) { adamc@457: uw_Basis_string ret = uw_malloc(ctx, p - s + 1); adamc@457: memcpy(ret, s, p - s); adamc@457: ret[p - s] = 0; adamc@457: return ret; adamc@457: } adamc@457: else adamc@457: return NULL; adamc@457: } else { adamc@457: if (s = strchr(s, '\n')) adamc@457: ++s; adamc@457: else adamc@457: return NULL; adamc@457: } adamc@457: } adamc@457: adamc@457: }