adamc@436
|
1 #define _XOPEN_SOURCE
|
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@102
|
10
|
adamc@102
|
11 #include "types.h"
|
adamc@102
|
12
|
adamc@311
|
13 uw_unit uw_unit_v = {};
|
adamc@102
|
14
|
adamc@167
|
15 #define ERROR_BUF_LEN 1024
|
adamc@167
|
16
|
adamc@323
|
17 typedef struct regions {
|
adamc@323
|
18 struct regions *next;
|
adamc@323
|
19 } regions;
|
adamc@323
|
20
|
adamc@425
|
21 typedef struct {
|
adamc@425
|
22 void (*func)(void*);
|
adamc@425
|
23 void *arg;
|
adamc@425
|
24 } cleanup;
|
adamc@425
|
25
|
adamc@311
|
26 struct uw_context {
|
adamc@458
|
27 char *headers, *headers_end;
|
adamc@457
|
28
|
adamc@462
|
29 char *outHeaders, *outHeaders_front, *outHeaders_back;
|
adamc@117
|
30 char *page, *page_front, *page_back;
|
adamc@136
|
31 char *heap, *heap_front, *heap_back;
|
adamc@144
|
32 char **inputs;
|
adamc@167
|
33
|
adamc@272
|
34 void *db;
|
adamc@272
|
35
|
adamc@167
|
36 jmp_buf jmp_buf;
|
adamc@167
|
37
|
adamc@323
|
38 regions *regions;
|
adamc@323
|
39
|
adamc@425
|
40 cleanup *cleanup, *cleanup_front, *cleanup_back;
|
adamc@425
|
41
|
adamc@167
|
42 char error_message[ERROR_BUF_LEN];
|
adamc@117
|
43 };
|
adamc@117
|
44
|
adamc@311
|
45 extern int uw_inputs_len;
|
adamc@144
|
46
|
adamc@462
|
47 uw_context uw_init(size_t outHeaders_len, size_t page_len, size_t heap_len) {
|
adamc@311
|
48 uw_context ctx = malloc(sizeof(struct uw_context));
|
adamc@136
|
49
|
adamc@458
|
50 ctx->headers = ctx->headers_end = NULL;
|
adamc@457
|
51
|
adamc@462
|
52 ctx->outHeaders_front = ctx->outHeaders = malloc(outHeaders_len);
|
adamc@462
|
53 ctx->outHeaders_back = ctx->outHeaders_front + outHeaders_len;
|
adamc@462
|
54
|
adamc@462
|
55 ctx->heap_front = ctx->heap = malloc(heap_len);
|
adamc@462
|
56
|
adamc@117
|
57 ctx->page_front = ctx->page = malloc(page_len);
|
adamc@117
|
58 ctx->page_back = ctx->page_front + page_len;
|
adamc@136
|
59
|
adamc@136
|
60 ctx->heap_front = ctx->heap = malloc(heap_len);
|
adamc@136
|
61 ctx->heap_back = ctx->heap_front + heap_len;
|
adamc@136
|
62
|
adamc@311
|
63 ctx->inputs = calloc(uw_inputs_len, sizeof(char *));
|
adamc@144
|
64
|
adamc@272
|
65 ctx->db = NULL;
|
adamc@272
|
66
|
adamc@323
|
67 ctx->regions = NULL;
|
adamc@323
|
68
|
adamc@425
|
69 ctx->cleanup_front = ctx->cleanup_back = ctx->cleanup = malloc(0);
|
adamc@425
|
70
|
adamc@167
|
71 ctx->error_message[0] = 0;
|
adamc@167
|
72
|
adamc@117
|
73 return ctx;
|
adamc@106
|
74 }
|
adamc@106
|
75
|
adamc@311
|
76 void uw_set_db(uw_context ctx, void *db) {
|
adamc@272
|
77 ctx->db = db;
|
adamc@272
|
78 }
|
adamc@272
|
79
|
adamc@311
|
80 void *uw_get_db(uw_context ctx) {
|
adamc@272
|
81 return ctx->db;
|
adamc@272
|
82 }
|
adamc@272
|
83
|
adamc@311
|
84 void uw_free(uw_context ctx) {
|
adamc@462
|
85 free(ctx->outHeaders);
|
adamc@117
|
86 free(ctx->page);
|
adamc@136
|
87 free(ctx->heap);
|
adamc@144
|
88 free(ctx->inputs);
|
adamc@425
|
89 free(ctx->cleanup);
|
adamc@136
|
90 free(ctx);
|
adamc@136
|
91 }
|
adamc@136
|
92
|
adamc@311
|
93 void uw_reset_keep_request(uw_context ctx) {
|
adamc@462
|
94 ctx->outHeaders_front = ctx->outHeaders;
|
adamc@138
|
95 ctx->page_front = ctx->page;
|
adamc@138
|
96 ctx->heap_front = ctx->heap;
|
adamc@323
|
97 ctx->regions = NULL;
|
adamc@425
|
98 ctx->cleanup_front = ctx->cleanup;
|
adamc@167
|
99
|
adamc@167
|
100 ctx->error_message[0] = 0;
|
adamc@167
|
101 }
|
adamc@167
|
102
|
adamc@311
|
103 void uw_reset_keep_error_message(uw_context ctx) {
|
adamc@462
|
104 ctx->outHeaders_front = ctx->outHeaders;
|
adamc@167
|
105 ctx->page_front = ctx->page;
|
adamc@167
|
106 ctx->heap_front = ctx->heap;
|
adamc@323
|
107 ctx->regions = NULL;
|
adamc@425
|
108 ctx->cleanup_front = ctx->cleanup;
|
adamc@167
|
109 }
|
adamc@167
|
110
|
adamc@311
|
111 void uw_reset(uw_context ctx) {
|
adamc@311
|
112 uw_reset_keep_request(ctx);
|
adamc@311
|
113 memset(ctx->inputs, 0, uw_inputs_len * sizeof(char *));
|
adamc@144
|
114 }
|
adamc@144
|
115
|
adamc@311
|
116 void uw_db_init(uw_context);
|
adamc@311
|
117 void uw_handle(uw_context, char *);
|
adamc@167
|
118
|
adamc@311
|
119 failure_kind uw_begin_init(uw_context ctx) {
|
adamc@272
|
120 int r = setjmp(ctx->jmp_buf);
|
adamc@272
|
121
|
adamc@272
|
122 if (r == 0)
|
adamc@311
|
123 uw_db_init(ctx);
|
adamc@272
|
124
|
adamc@272
|
125 return r;
|
adamc@272
|
126 }
|
adamc@272
|
127
|
adamc@458
|
128 void uw_set_headers(uw_context ctx, char *headers) {
|
adamc@458
|
129 char *s = headers, *s2;
|
adamc@458
|
130 ctx->headers = headers;
|
adamc@458
|
131
|
adamc@458
|
132 while (s2 = strchr(s, '\r')) {
|
adamc@458
|
133 s = s2;
|
adamc@458
|
134
|
adamc@458
|
135 if (s[1] == 0)
|
adamc@458
|
136 break;
|
adamc@458
|
137
|
adamc@458
|
138 *s = 0;
|
adamc@458
|
139 s += 2;
|
adamc@458
|
140 }
|
adamc@458
|
141
|
adamc@458
|
142 ctx->headers_end = s;
|
adamc@458
|
143 }
|
adamc@458
|
144
|
adamc@458
|
145 failure_kind uw_begin(uw_context ctx, char *path) {
|
adamc@190
|
146 int r = setjmp(ctx->jmp_buf);
|
adamc@190
|
147
|
adamc@190
|
148 if (r == 0)
|
adamc@311
|
149 uw_handle(ctx, path);
|
adamc@167
|
150
|
adamc@190
|
151 return r;
|
adamc@167
|
152 }
|
adamc@167
|
153
|
adamc@311
|
154 __attribute__((noreturn)) void uw_error(uw_context ctx, failure_kind fk, const char *fmt, ...) {
|
adamc@425
|
155 cleanup *cl;
|
adamc@425
|
156
|
adamc@167
|
157 va_list ap;
|
adamc@167
|
158 va_start(ap, fmt);
|
adamc@167
|
159
|
adamc@167
|
160 vsnprintf(ctx->error_message, ERROR_BUF_LEN, fmt, ap);
|
adamc@167
|
161
|
adamc@425
|
162 for (cl = ctx->cleanup; cl < ctx->cleanup_front; ++cl)
|
adamc@425
|
163 cl->func(cl->arg);
|
adamc@425
|
164
|
adamc@425
|
165 ctx->cleanup_front = ctx->cleanup;
|
adamc@425
|
166
|
adamc@190
|
167 longjmp(ctx->jmp_buf, fk);
|
adamc@167
|
168 }
|
adamc@167
|
169
|
adamc@425
|
170 void uw_push_cleanup(uw_context ctx, void (*func)(void *), void *arg) {
|
adamc@425
|
171 if (ctx->cleanup_front >= ctx->cleanup_back) {
|
adamc@425
|
172 int len = ctx->cleanup_back - ctx->cleanup, newLen;
|
adamc@425
|
173 if (len == 0)
|
adamc@425
|
174 newLen = 1;
|
adamc@425
|
175 else
|
adamc@428
|
176 newLen = len * 2;
|
adamc@470
|
177 ctx->cleanup = realloc(ctx->cleanup, newLen * sizeof(cleanup));
|
adamc@425
|
178 ctx->cleanup_front = ctx->cleanup + len;
|
adamc@425
|
179 ctx->cleanup_back = ctx->cleanup + newLen;
|
adamc@425
|
180 }
|
adamc@425
|
181
|
adamc@425
|
182 ctx->cleanup_front->func = func;
|
adamc@425
|
183 ctx->cleanup_front->arg = arg;
|
adamc@425
|
184 ++ctx->cleanup_front;
|
adamc@425
|
185 }
|
adamc@425
|
186
|
adamc@425
|
187 void uw_pop_cleanup(uw_context ctx) {
|
adamc@425
|
188 if (ctx->cleanup_front == ctx->cleanup)
|
adamc@425
|
189 uw_error(ctx, FATAL, "Attempt to pop from empty cleanup action stack");
|
adamc@425
|
190
|
adamc@425
|
191 --ctx->cleanup_front;
|
adamc@425
|
192 ctx->cleanup_front->func(ctx->cleanup_front->arg);
|
adamc@425
|
193 }
|
adamc@425
|
194
|
adamc@311
|
195 char *uw_error_message(uw_context ctx) {
|
adamc@167
|
196 return ctx->error_message;
|
adamc@167
|
197 }
|
adamc@167
|
198
|
adamc@311
|
199 int uw_input_num(char*);
|
adamc@144
|
200
|
adamc@311
|
201 void uw_set_input(uw_context ctx, char *name, char *value) {
|
adamc@311
|
202 int n = uw_input_num(name);
|
adamc@144
|
203
|
adamc@169
|
204 if (n < 0)
|
adamc@311
|
205 uw_error(ctx, FATAL, "Bad input name %s", name);
|
adamc@144
|
206
|
adamc@311
|
207 if (n >= uw_inputs_len)
|
adamc@311
|
208 uw_error(ctx, FATAL, "For input name %s, index %d is out of range", name, n);
|
adamc@169
|
209
|
adamc@144
|
210 ctx->inputs[n] = value;
|
adamc@144
|
211
|
adamc@282
|
212 //printf("[%d] %s = %s\n", n, name, value);
|
adamc@144
|
213 }
|
adamc@144
|
214
|
adamc@311
|
215 char *uw_get_input(uw_context ctx, int n) {
|
adamc@169
|
216 if (n < 0)
|
adamc@311
|
217 uw_error(ctx, FATAL, "Negative input index %d", n);
|
adamc@311
|
218 if (n >= uw_inputs_len)
|
adamc@311
|
219 uw_error(ctx, FATAL, "Out-of-bounds input index %d", n);
|
adamc@282
|
220 //printf("[%d] = %s\n", n, ctx->inputs[n]);
|
adamc@144
|
221 return ctx->inputs[n];
|
adamc@138
|
222 }
|
adamc@138
|
223
|
adamc@311
|
224 char *uw_get_optional_input(uw_context ctx, int n) {
|
adamc@190
|
225 if (n < 0)
|
adamc@311
|
226 uw_error(ctx, FATAL, "Negative input index %d", n);
|
adamc@311
|
227 if (n >= uw_inputs_len)
|
adamc@311
|
228 uw_error(ctx, FATAL, "Out-of-bounds input index %d", n);
|
adamc@426
|
229 //printf("[%d] = %s\n", n, ctx->inputs[n]);
|
adamc@190
|
230 return (ctx->inputs[n] == NULL ? "" : ctx->inputs[n]);
|
adamc@190
|
231 }
|
adamc@190
|
232
|
adamc@311
|
233 static void uw_check_heap(uw_context ctx, size_t extra) {
|
adamc@136
|
234 if (ctx->heap_back - ctx->heap_front < extra) {
|
adamc@321
|
235 size_t desired = ctx->heap_front - ctx->heap + extra, next;
|
adamc@136
|
236 char *new_heap;
|
adamc@136
|
237
|
adamc@317
|
238 next = ctx->heap_back - ctx->heap;
|
adamc@317
|
239 if (next == 0)
|
adamc@317
|
240 next = 1;
|
adamc@317
|
241 for (; next < desired; next *= 2);
|
adamc@136
|
242
|
adamc@136
|
243 new_heap = realloc(ctx->heap, next);
|
adamc@321
|
244 ctx->heap_front = new_heap + (ctx->heap_front - ctx->heap);
|
adamc@169
|
245 ctx->heap_back = new_heap + next;
|
adamc@136
|
246
|
adamc@136
|
247 if (new_heap != ctx->heap) {
|
adamc@136
|
248 ctx->heap = new_heap;
|
adamc@311
|
249 uw_error(ctx, UNLIMITED_RETRY, "Couldn't allocate new heap chunk contiguously");
|
adamc@136
|
250 }
|
adamc@136
|
251
|
adamc@169
|
252 ctx->heap = new_heap;
|
adamc@136
|
253 }
|
adamc@136
|
254 }
|
adamc@136
|
255
|
adamc@311
|
256 void *uw_malloc(uw_context ctx, size_t len) {
|
adamc@136
|
257 void *result;
|
adamc@136
|
258
|
adamc@311
|
259 uw_check_heap(ctx, len);
|
adamc@136
|
260
|
adamc@136
|
261 result = ctx->heap_front;
|
adamc@136
|
262 ctx->heap_front += len;
|
adamc@136
|
263 return result;
|
adamc@117
|
264 }
|
adamc@117
|
265
|
adamc@323
|
266 void uw_begin_region(uw_context ctx) {
|
adamc@323
|
267 regions *r = (regions *) ctx->heap_front;
|
adamc@323
|
268
|
adamc@323
|
269 uw_check_heap(ctx, sizeof(regions));
|
adamc@323
|
270
|
adamc@323
|
271 ctx->heap_front += sizeof(regions);
|
adamc@323
|
272
|
adamc@323
|
273 r->next = ctx->regions;
|
adamc@323
|
274 ctx->regions = r;
|
adamc@323
|
275 }
|
adamc@323
|
276
|
adamc@323
|
277 void uw_end_region(uw_context ctx) {
|
adamc@323
|
278 regions *r = ctx->regions;
|
adamc@323
|
279
|
adamc@323
|
280 if (r == NULL)
|
adamc@323
|
281 uw_error(ctx, FATAL, "Region stack underflow");
|
adamc@323
|
282
|
adamc@323
|
283 ctx->heap_front = (char *) r;
|
adamc@323
|
284 ctx->regions = r->next;
|
adamc@323
|
285 }
|
adamc@323
|
286
|
adamc@324
|
287 void uw_memstats(uw_context ctx) {
|
adamc@462
|
288 printf("Headers: %d/%d\n", ctx->outHeaders_front - ctx->outHeaders, ctx->outHeaders_back - ctx->outHeaders);
|
adamc@324
|
289 printf("Page: %d/%d\n", ctx->page_front - ctx->page, ctx->page_back - ctx->page);
|
adamc@324
|
290 printf("Heap: %d/%d\n", ctx->heap_front - ctx->heap, ctx->heap_back - ctx->heap);
|
adamc@324
|
291 }
|
adamc@324
|
292
|
adamc@436
|
293 int uw_really_send(int sock, const void *buf, size_t len) {
|
adamc@117
|
294 while (len > 0) {
|
adamc@436
|
295 size_t n = send(sock, buf, len, 0);
|
adamc@117
|
296
|
adamc@117
|
297 if (n < 0)
|
adamc@117
|
298 return n;
|
adamc@117
|
299
|
adamc@117
|
300 buf += n;
|
adamc@117
|
301 len -= n;
|
adamc@117
|
302 }
|
adamc@117
|
303
|
adamc@117
|
304 return 0;
|
adamc@117
|
305 }
|
adamc@117
|
306
|
adamc@311
|
307 int uw_send(uw_context ctx, int sock) {
|
adamc@462
|
308 int n = uw_really_send(sock, ctx->outHeaders, ctx->outHeaders_front - ctx->outHeaders);
|
adamc@462
|
309
|
adamc@462
|
310 if (n < 0)
|
adamc@462
|
311 return n;
|
adamc@462
|
312
|
adamc@462
|
313 n = uw_really_send(sock, "\r\n", 2);
|
adamc@462
|
314
|
adamc@462
|
315 if (n < 0)
|
adamc@462
|
316 return n;
|
adamc@462
|
317
|
adamc@464
|
318 return uw_really_send(sock, ctx->page, ctx->page_front - ctx->page);
|
adamc@462
|
319 }
|
adamc@462
|
320
|
adamc@462
|
321 static void uw_check_headers(uw_context ctx, size_t extra) {
|
adamc@462
|
322 size_t desired = ctx->outHeaders_front - ctx->outHeaders + extra, next;
|
adamc@462
|
323 char *new_outHeaders;
|
adamc@462
|
324
|
adamc@462
|
325 next = ctx->outHeaders_back - ctx->outHeaders;
|
adamc@462
|
326 if (next < desired) {
|
adamc@462
|
327 if (next == 0)
|
adamc@462
|
328 next = 1;
|
adamc@462
|
329 for (; next < desired; next *= 2);
|
adamc@462
|
330
|
adamc@462
|
331 new_outHeaders = realloc(ctx->outHeaders, next);
|
adamc@462
|
332 ctx->outHeaders_front = new_outHeaders + (ctx->outHeaders_front - ctx->outHeaders);
|
adamc@462
|
333 ctx->outHeaders_back = new_outHeaders + next;
|
adamc@462
|
334 ctx->outHeaders = new_outHeaders;
|
adamc@462
|
335 }
|
adamc@462
|
336 }
|
adamc@462
|
337
|
adamc@462
|
338 void uw_write_header(uw_context ctx, uw_Basis_string s) {
|
adamc@462
|
339 int len = strlen(s);
|
adamc@462
|
340
|
adamc@462
|
341 uw_check_headers(ctx, len + 1);
|
adamc@462
|
342 strcpy(ctx->outHeaders_front, s);
|
adamc@462
|
343 ctx->outHeaders_front += len;
|
adamc@474
|
344 *ctx->outHeaders_front = 0;
|
adamc@117
|
345 }
|
adamc@117
|
346
|
adamc@311
|
347 static void uw_check(uw_context ctx, size_t extra) {
|
adamc@321
|
348 size_t desired = ctx->page_front - ctx->page + extra, next;
|
adamc@117
|
349 char *new_page;
|
adamc@117
|
350
|
adamc@317
|
351 next = ctx->page_back - ctx->page;
|
adamc@428
|
352 if (next < desired) {
|
adamc@428
|
353 if (next == 0)
|
adamc@428
|
354 next = 1;
|
adamc@428
|
355 for (; next < desired; next *= 2);
|
adamc@117
|
356
|
adamc@428
|
357 new_page = realloc(ctx->page, next);
|
adamc@428
|
358 ctx->page_front = new_page + (ctx->page_front - ctx->page);
|
adamc@428
|
359 ctx->page_back = new_page + next;
|
adamc@428
|
360 ctx->page = new_page;
|
adamc@428
|
361 }
|
adamc@117
|
362 }
|
adamc@117
|
363
|
adamc@311
|
364 static void uw_writec_unsafe(uw_context ctx, char c) {
|
adamc@117
|
365 *(ctx->page_front)++ = c;
|
adamc@117
|
366 }
|
adamc@117
|
367
|
adamc@311
|
368 void uw_writec(uw_context ctx, char c) {
|
adamc@311
|
369 uw_check(ctx, 1);
|
adamc@311
|
370 uw_writec_unsafe(ctx, c);
|
adamc@117
|
371 }
|
adamc@117
|
372
|
adamc@311
|
373 static void uw_write_unsafe(uw_context ctx, const char* s) {
|
adamc@117
|
374 int len = strlen(s);
|
adamc@117
|
375 memcpy(ctx->page_front, s, len);
|
adamc@117
|
376 ctx->page_front += len;
|
adamc@117
|
377 }
|
adamc@117
|
378
|
adamc@311
|
379 void uw_write(uw_context ctx, const char* s) {
|
adamc@311
|
380 uw_check(ctx, strlen(s) + 1);
|
adamc@311
|
381 uw_write_unsafe(ctx, s);
|
adamc@183
|
382 *ctx->page_front = 0;
|
adamc@102
|
383 }
|
adamc@106
|
384
|
adamc@135
|
385
|
adamc@311
|
386 char *uw_Basis_attrifyInt(uw_context ctx, uw_Basis_int n) {
|
adamc@136
|
387 char *result;
|
adamc@136
|
388 int len;
|
adamc@311
|
389 uw_check_heap(ctx, INTS_MAX);
|
adamc@136
|
390 result = ctx->heap_front;
|
adamc@276
|
391 sprintf(result, "%lld%n", n, &len);
|
adamc@137
|
392 ctx->heap_front += len+1;
|
adamc@136
|
393 return result;
|
adamc@106
|
394 }
|
adamc@106
|
395
|
adamc@311
|
396 char *uw_Basis_attrifyFloat(uw_context ctx, uw_Basis_float n) {
|
adamc@136
|
397 char *result;
|
adamc@136
|
398 int len;
|
adamc@311
|
399 uw_check_heap(ctx, FLOATS_MAX);
|
adamc@136
|
400 result = ctx->heap_front;
|
adamc@136
|
401 sprintf(result, "%g%n", n, &len);
|
adamc@137
|
402 ctx->heap_front += len+1;
|
adamc@136
|
403 return result;
|
adamc@106
|
404 }
|
adamc@106
|
405
|
adamc@311
|
406 char *uw_Basis_attrifyString(uw_context ctx, uw_Basis_string s) {
|
adamc@136
|
407 int len = strlen(s);
|
adamc@136
|
408 char *result, *p;
|
adamc@311
|
409 uw_check_heap(ctx, len * 6 + 1);
|
adamc@136
|
410
|
adamc@136
|
411 result = p = ctx->heap_front;
|
adamc@136
|
412
|
adamc@136
|
413 for (; *s; s++) {
|
adamc@136
|
414 char c = *s;
|
adamc@136
|
415
|
adamc@136
|
416 if (c == '"') {
|
adamc@136
|
417 strcpy(p, """);
|
adamc@136
|
418 p += 6;
|
adamc@136
|
419 } else if (c == '&') {
|
adamc@136
|
420 strcpy(p, "&");
|
adamc@136
|
421 p += 5;
|
adamc@136
|
422 }
|
adamc@136
|
423 else if (isprint(c))
|
adamc@136
|
424 *p++ = c;
|
adamc@136
|
425 else {
|
adamc@136
|
426 int len2;
|
adamc@136
|
427 sprintf(p, "&#%d;%n", c, &len2);
|
adamc@136
|
428 p += len2;
|
adamc@136
|
429 }
|
adamc@136
|
430 }
|
adamc@136
|
431
|
adamc@137
|
432 *p++ = 0;
|
adamc@136
|
433 ctx->heap_front = p;
|
adamc@136
|
434 return result;
|
adamc@106
|
435 }
|
adamc@106
|
436
|
adamc@311
|
437 static void uw_Basis_attrifyInt_w_unsafe(uw_context ctx, uw_Basis_int n) {
|
adamc@117
|
438 int len;
|
adamc@117
|
439
|
adamc@276
|
440 sprintf(ctx->page_front, "%lld%n", n, &len);
|
adamc@117
|
441 ctx->page_front += len;
|
adamc@106
|
442 }
|
adamc@106
|
443
|
adamc@428
|
444 uw_unit uw_Basis_attrifyInt_w(uw_context ctx, uw_Basis_int n) {
|
adamc@311
|
445 uw_check(ctx, INTS_MAX);
|
adamc@311
|
446 uw_Basis_attrifyInt_w_unsafe(ctx, n);
|
adamc@428
|
447
|
adamc@428
|
448 return uw_unit_v;
|
adamc@106
|
449 }
|
adamc@106
|
450
|
adamc@428
|
451 uw_unit uw_Basis_attrifyFloat_w(uw_context ctx, uw_Basis_float n) {
|
adamc@117
|
452 int len;
|
adamc@117
|
453
|
adamc@311
|
454 uw_check(ctx, FLOATS_MAX);
|
adamc@117
|
455 sprintf(ctx->page_front, "%g%n", n, &len);
|
adamc@117
|
456 ctx->page_front += len;
|
adamc@428
|
457
|
adamc@428
|
458 return uw_unit_v;
|
adamc@117
|
459 }
|
adamc@117
|
460
|
adamc@428
|
461 uw_unit uw_Basis_attrifyString_w(uw_context ctx, uw_Basis_string s) {
|
adamc@311
|
462 uw_check(ctx, strlen(s) * 6);
|
adamc@117
|
463
|
adamc@106
|
464 for (; *s; s++) {
|
adamc@106
|
465 char c = *s;
|
adamc@106
|
466
|
adamc@106
|
467 if (c == '"')
|
adamc@311
|
468 uw_write_unsafe(ctx, """);
|
adamc@136
|
469 else if (c == '&')
|
adamc@311
|
470 uw_write_unsafe(ctx, "&");
|
adamc@106
|
471 else if (isprint(c))
|
adamc@311
|
472 uw_writec_unsafe(ctx, c);
|
adamc@106
|
473 else {
|
adamc@311
|
474 uw_write_unsafe(ctx, "&#");
|
adamc@311
|
475 uw_Basis_attrifyInt_w_unsafe(ctx, c);
|
adamc@311
|
476 uw_writec_unsafe(ctx, ';');
|
adamc@106
|
477 }
|
adamc@106
|
478 }
|
adamc@428
|
479
|
adamc@428
|
480 return uw_unit_v;
|
adamc@106
|
481 }
|
adamc@120
|
482
|
adamc@120
|
483
|
adamc@311
|
484 char *uw_Basis_urlifyInt(uw_context ctx, uw_Basis_int n) {
|
adamc@137
|
485 int len;
|
adamc@137
|
486 char *r;
|
adamc@137
|
487
|
adamc@311
|
488 uw_check_heap(ctx, INTS_MAX);
|
adamc@137
|
489 r = ctx->heap_front;
|
adamc@276
|
490 sprintf(r, "%lld%n", n, &len);
|
adamc@137
|
491 ctx->heap_front += len+1;
|
adamc@137
|
492 return r;
|
adamc@120
|
493 }
|
adamc@120
|
494
|
adamc@311
|
495 char *uw_Basis_urlifyFloat(uw_context ctx, uw_Basis_float n) {
|
adamc@137
|
496 int len;
|
adamc@137
|
497 char *r;
|
adamc@137
|
498
|
adamc@311
|
499 uw_check_heap(ctx, FLOATS_MAX);
|
adamc@137
|
500 r = ctx->heap_front;
|
adamc@137
|
501 sprintf(r, "%g%n", n, &len);
|
adamc@137
|
502 ctx->heap_front += len+1;
|
adamc@137
|
503 return r;
|
adamc@120
|
504 }
|
adamc@120
|
505
|
adamc@311
|
506 char *uw_Basis_urlifyString(uw_context ctx, uw_Basis_string s) {
|
adamc@137
|
507 char *r, *p;
|
adamc@137
|
508
|
adamc@311
|
509 uw_check_heap(ctx, strlen(s) * 3 + 1);
|
adamc@137
|
510
|
adamc@137
|
511 for (r = p = ctx->heap_front; *s; s++) {
|
adamc@137
|
512 char c = *s;
|
adamc@137
|
513
|
adamc@137
|
514 if (c == ' ')
|
adamc@137
|
515 *p++ = '+';
|
adamc@137
|
516 else if (isalnum(c))
|
adamc@137
|
517 *p++ = c;
|
adamc@137
|
518 else {
|
adamc@137
|
519 sprintf(p, "%%%02X", c);
|
adamc@137
|
520 p += 3;
|
adamc@137
|
521 }
|
adamc@137
|
522 }
|
adamc@137
|
523
|
adamc@137
|
524 *p++ = 0;
|
adamc@137
|
525 ctx->heap_front = p;
|
adamc@137
|
526 return r;
|
adamc@120
|
527 }
|
adamc@120
|
528
|
adamc@311
|
529 char *uw_Basis_urlifyBool(uw_context ctx, uw_Basis_bool b) {
|
adamc@311
|
530 if (b == uw_Basis_False)
|
adamc@186
|
531 return "0";
|
adamc@186
|
532 else
|
adamc@186
|
533 return "1";
|
adamc@186
|
534 }
|
adamc@186
|
535
|
adamc@311
|
536 static void uw_Basis_urlifyInt_w_unsafe(uw_context ctx, uw_Basis_int n) {
|
adamc@120
|
537 int len;
|
adamc@120
|
538
|
adamc@276
|
539 sprintf(ctx->page_front, "%lld%n", n, &len);
|
adamc@120
|
540 ctx->page_front += len;
|
adamc@120
|
541 }
|
adamc@120
|
542
|
adamc@428
|
543 uw_unit uw_Basis_urlifyInt_w(uw_context ctx, uw_Basis_int n) {
|
adamc@311
|
544 uw_check(ctx, INTS_MAX);
|
adamc@311
|
545 uw_Basis_urlifyInt_w_unsafe(ctx, n);
|
adamc@428
|
546
|
adamc@428
|
547 return uw_unit_v;
|
adamc@120
|
548 }
|
adamc@120
|
549
|
adamc@428
|
550 uw_unit uw_Basis_urlifyFloat_w(uw_context ctx, uw_Basis_float n) {
|
adamc@120
|
551 int len;
|
adamc@120
|
552
|
adamc@311
|
553 uw_check(ctx, FLOATS_MAX);
|
adamc@120
|
554 sprintf(ctx->page_front, "%g%n", n, &len);
|
adamc@120
|
555 ctx->page_front += len;
|
adamc@428
|
556
|
adamc@428
|
557 return uw_unit_v;
|
adamc@120
|
558 }
|
adamc@120
|
559
|
adamc@488
|
560 uw_Basis_string uw_Basis_urlifyTime(uw_context ctx, uw_Basis_time t) {
|
adamc@488
|
561 return uw_Basis_urlifyInt(ctx, t);
|
adamc@488
|
562 }
|
adamc@488
|
563
|
adamc@428
|
564 uw_unit uw_Basis_urlifyString_w(uw_context ctx, uw_Basis_string s) {
|
adamc@311
|
565 uw_check(ctx, strlen(s) * 3);
|
adamc@120
|
566
|
adamc@120
|
567 for (; *s; s++) {
|
adamc@120
|
568 char c = *s;
|
adamc@120
|
569
|
adamc@120
|
570 if (c == ' ')
|
adamc@311
|
571 uw_writec_unsafe(ctx, '+');
|
adamc@120
|
572 else if (isalnum(c))
|
adamc@311
|
573 uw_writec_unsafe(ctx, c);
|
adamc@120
|
574 else {
|
adamc@120
|
575 sprintf(ctx->page_front, "%%%02X", c);
|
adamc@120
|
576 ctx->page_front += 3;
|
adamc@120
|
577 }
|
adamc@120
|
578 }
|
adamc@428
|
579
|
adamc@428
|
580 return uw_unit_v;
|
adamc@120
|
581 }
|
adamc@120
|
582
|
adamc@428
|
583 uw_unit uw_Basis_urlifyBool_w(uw_context ctx, uw_Basis_bool b) {
|
adamc@311
|
584 if (b == uw_Basis_False)
|
adamc@311
|
585 uw_writec(ctx, '0');
|
adamc@186
|
586 else
|
adamc@311
|
587 uw_writec(ctx, '1');
|
adamc@428
|
588
|
adamc@428
|
589 return uw_unit_v;
|
adamc@186
|
590 }
|
adamc@186
|
591
|
adamc@120
|
592
|
adamc@311
|
593 static char *uw_unurlify_advance(char *s) {
|
adamc@144
|
594 char *new_s = strchr(s, '/');
|
adamc@120
|
595
|
adamc@120
|
596 if (new_s)
|
adamc@120
|
597 *new_s++ = 0;
|
adamc@120
|
598 else
|
adamc@144
|
599 new_s = strchr(s, 0);
|
adamc@144
|
600
|
adamc@144
|
601 return new_s;
|
adamc@144
|
602 }
|
adamc@144
|
603
|
adamc@311
|
604 uw_Basis_int uw_Basis_unurlifyInt(uw_context ctx, char **s) {
|
adamc@311
|
605 char *new_s = uw_unurlify_advance(*s);
|
adamc@311
|
606 uw_Basis_int r;
|
adamc@120
|
607
|
adamc@276
|
608 r = atoll(*s);
|
adamc@120
|
609 *s = new_s;
|
adamc@120
|
610 return r;
|
adamc@120
|
611 }
|
adamc@120
|
612
|
adamc@311
|
613 uw_Basis_float uw_Basis_unurlifyFloat(uw_context ctx, char **s) {
|
adamc@311
|
614 char *new_s = uw_unurlify_advance(*s);
|
adamc@311
|
615 uw_Basis_float r;
|
adamc@120
|
616
|
adamc@120
|
617 r = atof(*s);
|
adamc@120
|
618 *s = new_s;
|
adamc@120
|
619 return r;
|
adamc@120
|
620 }
|
adamc@120
|
621
|
adamc@488
|
622 uw_Basis_time uw_Basis_unurlifyTime(uw_context ctx, char **s) {
|
adamc@488
|
623 return uw_Basis_unurlifyInt(ctx, s);
|
adamc@488
|
624 }
|
adamc@488
|
625
|
adamc@311
|
626 static uw_Basis_string uw_unurlifyString_to(uw_context ctx, char *r, char *s) {
|
adamc@144
|
627 char *s1, *s2;
|
adamc@144
|
628 int n;
|
adamc@136
|
629
|
adamc@144
|
630 for (s1 = r, s2 = s; *s2; ++s1, ++s2) {
|
adamc@136
|
631 char c = *s2;
|
adamc@136
|
632
|
adamc@136
|
633 switch (c) {
|
adamc@136
|
634 case '+':
|
adamc@136
|
635 *s1 = ' ';
|
adamc@136
|
636 break;
|
adamc@136
|
637 case '%':
|
adamc@169
|
638 if (s2[1] == 0)
|
adamc@311
|
639 uw_error(ctx, FATAL, "Missing first character of escaped URL byte");
|
adamc@169
|
640 if (s2[2] == 0)
|
adamc@311
|
641 uw_error(ctx, FATAL, "Missing second character of escaped URL byte");
|
adamc@169
|
642 if (sscanf(s2+1, "%02X", &n) != 1)
|
adamc@311
|
643 uw_error(ctx, FATAL, "Invalid escaped URL byte starting at: %s", s2);
|
adamc@136
|
644 *s1 = n;
|
adamc@136
|
645 s2 += 2;
|
adamc@136
|
646 break;
|
adamc@136
|
647 default:
|
adamc@136
|
648 *s1 = c;
|
adamc@136
|
649 }
|
adamc@136
|
650 }
|
adamc@136
|
651 *s1++ = 0;
|
adamc@144
|
652 return s1;
|
adamc@144
|
653 }
|
adamc@144
|
654
|
adamc@311
|
655 uw_Basis_bool uw_Basis_unurlifyBool(uw_context ctx, char **s) {
|
adamc@311
|
656 char *new_s = uw_unurlify_advance(*s);
|
adamc@311
|
657 uw_Basis_bool r;
|
adamc@186
|
658
|
adamc@186
|
659 if (*s[0] == 0 || !strcmp(*s, "0") || !strcmp(*s, "off"))
|
adamc@311
|
660 r = uw_Basis_False;
|
adamc@186
|
661 else
|
adamc@311
|
662 r = uw_Basis_True;
|
adamc@186
|
663
|
adamc@186
|
664 *s = new_s;
|
adamc@186
|
665 return r;
|
adamc@186
|
666 }
|
adamc@186
|
667
|
adamc@311
|
668 uw_Basis_string uw_Basis_unurlifyString(uw_context ctx, char **s) {
|
adamc@311
|
669 char *new_s = uw_unurlify_advance(*s);
|
adamc@144
|
670 char *r, *s1, *s2;
|
adamc@144
|
671 int len, n;
|
adamc@144
|
672
|
adamc@200
|
673 len = strlen(*s);
|
adamc@311
|
674 uw_check_heap(ctx, len + 1);
|
adamc@144
|
675
|
adamc@144
|
676 r = ctx->heap_front;
|
adamc@311
|
677 ctx->heap_front = uw_unurlifyString_to(ctx, ctx->heap_front, *s);
|
adamc@136
|
678 *s = new_s;
|
adamc@136
|
679 return r;
|
adamc@120
|
680 }
|
adamc@135
|
681
|
adamc@135
|
682
|
adamc@311
|
683 char *uw_Basis_htmlifyInt(uw_context ctx, uw_Basis_int n) {
|
adamc@286
|
684 int len;
|
adamc@286
|
685 char *r;
|
adamc@286
|
686
|
adamc@311
|
687 uw_check_heap(ctx, INTS_MAX);
|
adamc@286
|
688 r = ctx->heap_front;
|
adamc@286
|
689 sprintf(r, "%lld%n", n, &len);
|
adamc@286
|
690 ctx->heap_front += len+1;
|
adamc@286
|
691 return r;
|
adamc@286
|
692 }
|
adamc@286
|
693
|
adamc@428
|
694 uw_unit uw_Basis_htmlifyInt_w(uw_context ctx, uw_Basis_int n) {
|
adamc@286
|
695 int len;
|
adamc@286
|
696
|
adamc@311
|
697 uw_check(ctx, INTS_MAX);
|
adamc@286
|
698 sprintf(ctx->page_front, "%lld%n", n, &len);
|
adamc@286
|
699 ctx->page_front += len;
|
adamc@428
|
700
|
adamc@428
|
701 return uw_unit_v;
|
adamc@286
|
702 }
|
adamc@286
|
703
|
adamc@311
|
704 char *uw_Basis_htmlifyFloat(uw_context ctx, uw_Basis_float n) {
|
adamc@286
|
705 int len;
|
adamc@286
|
706 char *r;
|
adamc@286
|
707
|
adamc@311
|
708 uw_check_heap(ctx, FLOATS_MAX);
|
adamc@286
|
709 r = ctx->heap_front;
|
adamc@286
|
710 sprintf(r, "%g%n", n, &len);
|
adamc@286
|
711 ctx->heap_front += len+1;
|
adamc@286
|
712 return r;
|
adamc@286
|
713 }
|
adamc@286
|
714
|
adamc@428
|
715 uw_unit uw_Basis_htmlifyFloat_w(uw_context ctx, uw_Basis_float n) {
|
adamc@286
|
716 int len;
|
adamc@286
|
717
|
adamc@311
|
718 uw_check(ctx, FLOATS_MAX);
|
adamc@286
|
719 sprintf(ctx->page_front, "%g%n", n, &len);
|
adamc@286
|
720 ctx->page_front += len;
|
adamc@428
|
721
|
adamc@428
|
722 return uw_unit_v;
|
adamc@286
|
723 }
|
adamc@286
|
724
|
adamc@311
|
725 char *uw_Basis_htmlifyString(uw_context ctx, uw_Basis_string s) {
|
adamc@137
|
726 char *r, *s2;
|
adamc@137
|
727
|
adamc@311
|
728 uw_check_heap(ctx, strlen(s) * 5 + 1);
|
adamc@137
|
729
|
adamc@137
|
730 for (r = s2 = ctx->heap_front; *s; s++) {
|
adamc@137
|
731 char c = *s;
|
adamc@137
|
732
|
adamc@137
|
733 switch (c) {
|
adamc@137
|
734 case '<':
|
adamc@137
|
735 strcpy(s2, "<");
|
adamc@137
|
736 s2 += 4;
|
adamc@137
|
737 break;
|
adamc@137
|
738 case '&':
|
adamc@137
|
739 strcpy(s2, "&");
|
adamc@137
|
740 s2 += 5;
|
adamc@137
|
741 break;
|
adamc@137
|
742 default:
|
adamc@137
|
743 if (isprint(c))
|
adamc@137
|
744 *s2++ = c;
|
adamc@137
|
745 else {
|
adamc@137
|
746 int len2;
|
adamc@137
|
747 sprintf(s2, "&#%d;%n", c, &len2);
|
adamc@137
|
748 s2 += len2;
|
adamc@137
|
749 }
|
adamc@137
|
750 }
|
adamc@137
|
751 }
|
adamc@137
|
752
|
adamc@137
|
753 *s2++ = 0;
|
adamc@137
|
754 ctx->heap_front = s2;
|
adamc@137
|
755 return r;
|
adamc@135
|
756 }
|
adamc@135
|
757
|
adamc@428
|
758 uw_unit uw_Basis_htmlifyString_w(uw_context ctx, uw_Basis_string s) {
|
adamc@321
|
759 uw_check(ctx, strlen(s) * 6);
|
adamc@135
|
760
|
adamc@135
|
761 for (; *s; s++) {
|
adamc@135
|
762 char c = *s;
|
adamc@135
|
763
|
adamc@135
|
764 switch (c) {
|
adamc@135
|
765 case '<':
|
adamc@311
|
766 uw_write_unsafe(ctx, "<");
|
adamc@135
|
767 break;
|
adamc@135
|
768 case '&':
|
adamc@311
|
769 uw_write_unsafe(ctx, "&");
|
adamc@135
|
770 break;
|
adamc@135
|
771 default:
|
adamc@135
|
772 if (isprint(c))
|
adamc@311
|
773 uw_writec_unsafe(ctx, c);
|
adamc@135
|
774 else {
|
adamc@311
|
775 uw_write_unsafe(ctx, "&#");
|
adamc@311
|
776 uw_Basis_attrifyInt_w_unsafe(ctx, c);
|
adamc@311
|
777 uw_writec_unsafe(ctx, ';');
|
adamc@135
|
778 }
|
adamc@135
|
779 }
|
adamc@135
|
780 }
|
adamc@428
|
781
|
adamc@428
|
782 return uw_unit_v;
|
adamc@135
|
783 }
|
adamc@180
|
784
|
adamc@311
|
785 uw_Basis_string uw_Basis_htmlifyBool(uw_context ctx, uw_Basis_bool b) {
|
adamc@311
|
786 if (b == uw_Basis_False)
|
adamc@286
|
787 return "False";
|
adamc@286
|
788 else
|
adamc@286
|
789 return "True";
|
adamc@286
|
790 }
|
adamc@286
|
791
|
adamc@428
|
792 uw_unit uw_Basis_htmlifyBool_w(uw_context ctx, uw_Basis_bool b) {
|
adamc@311
|
793 if (b == uw_Basis_False) {
|
adamc@311
|
794 uw_check(ctx, 6);
|
adamc@286
|
795 strcpy(ctx->page_front, "False");
|
adamc@286
|
796 ctx->page_front += 5;
|
adamc@286
|
797 } else {
|
adamc@311
|
798 uw_check(ctx, 5);
|
adamc@286
|
799 strcpy(ctx->page_front, "True");
|
adamc@286
|
800 ctx->page_front += 4;
|
adamc@286
|
801 }
|
adamc@428
|
802
|
adamc@428
|
803 return uw_unit_v;
|
adamc@286
|
804 }
|
adamc@286
|
805
|
adamc@436
|
806 #define TIME_FMT "%x %X"
|
adamc@438
|
807 #define TIME_FMT_PG "%Y-%m-%d %T"
|
adamc@436
|
808
|
adamc@436
|
809 uw_Basis_string uw_Basis_htmlifyTime(uw_context ctx, uw_Basis_time t) {
|
adamc@436
|
810 size_t len;
|
adamc@436
|
811 char *r;
|
adamc@436
|
812 struct tm stm;
|
adamc@436
|
813
|
adamc@436
|
814 if (localtime_r(&t, &stm)) {
|
adamc@436
|
815 uw_check_heap(ctx, TIMES_MAX);
|
adamc@436
|
816 r = ctx->heap_front;
|
adamc@436
|
817 len = strftime(r, TIMES_MAX, TIME_FMT, &stm);
|
adamc@436
|
818 ctx->heap_front += len+1;
|
adamc@436
|
819 return r;
|
adamc@436
|
820 } else
|
adamc@436
|
821 return "<i>Invalid time</i>";
|
adamc@436
|
822 }
|
adamc@436
|
823
|
adamc@436
|
824 uw_unit uw_Basis_htmlifyTime_w(uw_context ctx, uw_Basis_time t) {
|
adamc@436
|
825 size_t len;
|
adamc@436
|
826 char *r;
|
adamc@436
|
827 struct tm stm;
|
adamc@436
|
828
|
adamc@436
|
829 if (localtime_r(&t, &stm)) {
|
adamc@436
|
830 uw_check(ctx, TIMES_MAX);
|
adamc@436
|
831 r = ctx->page_front;
|
adamc@436
|
832 len = strftime(r, TIMES_MAX, TIME_FMT, &stm);
|
adamc@436
|
833 ctx->page_front += len;
|
adamc@436
|
834 } else {
|
adamc@436
|
835 uw_check(ctx, 20);
|
adamc@436
|
836 strcpy(ctx->page_front, "<i>Invalid time</i>");
|
adamc@436
|
837 ctx->page_front += 19;
|
adamc@436
|
838 }
|
adamc@436
|
839
|
adamc@436
|
840 return uw_unit_v;
|
adamc@436
|
841 }
|
adamc@436
|
842
|
adamc@311
|
843 uw_Basis_string uw_Basis_strcat(uw_context ctx, uw_Basis_string s1, uw_Basis_string s2) {
|
adamc@180
|
844 int len = strlen(s1) + strlen(s2) + 1;
|
adamc@180
|
845 char *s;
|
adamc@180
|
846
|
adamc@311
|
847 uw_check_heap(ctx, len);
|
adamc@180
|
848
|
adamc@180
|
849 s = ctx->heap_front;
|
adamc@180
|
850
|
adamc@180
|
851 strcpy(s, s1);
|
adamc@180
|
852 strcat(s, s2);
|
adamc@180
|
853 ctx->heap_front += len;
|
adamc@180
|
854
|
adamc@180
|
855 return s;
|
adamc@180
|
856 }
|
adamc@278
|
857
|
adamc@311
|
858 uw_Basis_string uw_Basis_strdup(uw_context ctx, uw_Basis_string s1) {
|
adamc@278
|
859 int len = strlen(s1) + 1;
|
adamc@278
|
860 char *s;
|
adamc@278
|
861
|
adamc@311
|
862 uw_check_heap(ctx, len);
|
adamc@278
|
863
|
adamc@278
|
864 s = ctx->heap_front;
|
adamc@278
|
865
|
adamc@278
|
866 strcpy(s, s1);
|
adamc@278
|
867 ctx->heap_front += len;
|
adamc@278
|
868
|
adamc@278
|
869 return s;
|
adamc@278
|
870 }
|
adamc@280
|
871
|
adamc@280
|
872
|
adamc@311
|
873 char *uw_Basis_sqlifyInt(uw_context ctx, uw_Basis_int n) {
|
adamc@281
|
874 int len;
|
adamc@281
|
875 char *r;
|
adamc@281
|
876
|
adamc@311
|
877 uw_check_heap(ctx, INTS_MAX + 6);
|
adamc@281
|
878 r = ctx->heap_front;
|
adamc@281
|
879 sprintf(r, "%lld::int8%n", n, &len);
|
adamc@281
|
880 ctx->heap_front += len+1;
|
adamc@281
|
881 return r;
|
adamc@281
|
882 }
|
adamc@281
|
883
|
adamc@467
|
884 char *uw_Basis_sqlifyIntN(uw_context ctx, uw_Basis_int *n) {
|
adamc@467
|
885 if (n == NULL)
|
adamc@467
|
886 return "NULL";
|
adamc@467
|
887 else
|
adamc@467
|
888 return uw_Basis_sqlifyInt(ctx, *n);
|
adamc@467
|
889 }
|
adamc@467
|
890
|
adamc@311
|
891 char *uw_Basis_sqlifyFloat(uw_context ctx, uw_Basis_float n) {
|
adamc@281
|
892 int len;
|
adamc@281
|
893 char *r;
|
adamc@281
|
894
|
adamc@311
|
895 uw_check_heap(ctx, FLOATS_MAX + 8);
|
adamc@281
|
896 r = ctx->heap_front;
|
adamc@281
|
897 sprintf(r, "%g::float8%n", n, &len);
|
adamc@281
|
898 ctx->heap_front += len+1;
|
adamc@281
|
899 return r;
|
adamc@281
|
900 }
|
adamc@281
|
901
|
adamc@467
|
902 char *uw_Basis_sqlifyFloatN(uw_context ctx, uw_Basis_float *n) {
|
adamc@467
|
903 if (n == NULL)
|
adamc@467
|
904 return "NULL";
|
adamc@467
|
905 else
|
adamc@467
|
906 return uw_Basis_sqlifyFloat(ctx, *n);
|
adamc@467
|
907 }
|
adamc@467
|
908
|
adamc@281
|
909
|
adamc@311
|
910 uw_Basis_string uw_Basis_sqlifyString(uw_context ctx, uw_Basis_string s) {
|
adamc@280
|
911 char *r, *s2;
|
adamc@280
|
912
|
adamc@311
|
913 uw_check_heap(ctx, strlen(s) * 2 + 10);
|
adamc@280
|
914
|
adamc@280
|
915 r = s2 = ctx->heap_front;
|
adamc@280
|
916 *s2++ = 'E';
|
adamc@280
|
917 *s2++ = '\'';
|
adamc@280
|
918
|
adamc@280
|
919 for (; *s; s++) {
|
adamc@280
|
920 char c = *s;
|
adamc@280
|
921
|
adamc@280
|
922 switch (c) {
|
adamc@280
|
923 case '\'':
|
adamc@280
|
924 strcpy(s2, "\\'");
|
adamc@280
|
925 s2 += 2;
|
adamc@280
|
926 break;
|
adamc@280
|
927 case '\\':
|
adamc@280
|
928 strcpy(s2, "\\\\");
|
adamc@280
|
929 s2 += 2;
|
adamc@280
|
930 break;
|
adamc@280
|
931 default:
|
adamc@280
|
932 if (isprint(c))
|
adamc@280
|
933 *s2++ = c;
|
adamc@280
|
934 else {
|
adamc@280
|
935 sprintf(s2, "\\%3o", c);
|
adamc@280
|
936 s2 += 4;
|
adamc@280
|
937 }
|
adamc@280
|
938 }
|
adamc@280
|
939 }
|
adamc@280
|
940
|
adamc@281
|
941 strcpy(s2, "'::text");
|
adamc@281
|
942 ctx->heap_front = s2 + 8;
|
adamc@280
|
943 return r;
|
adamc@280
|
944 }
|
adamc@281
|
945
|
adamc@467
|
946 uw_Basis_string uw_Basis_sqlifyStringN(uw_context ctx, uw_Basis_string s) {
|
adamc@467
|
947 if (s == NULL)
|
adamc@467
|
948 return "NULL";
|
adamc@467
|
949 else
|
adamc@467
|
950 return uw_Basis_sqlifyString(ctx, s);
|
adamc@467
|
951 }
|
adamc@467
|
952
|
adamc@311
|
953 char *uw_Basis_sqlifyBool(uw_context ctx, uw_Basis_bool b) {
|
adamc@311
|
954 if (b == uw_Basis_False)
|
adamc@281
|
955 return "FALSE";
|
adamc@281
|
956 else
|
adamc@281
|
957 return "TRUE";
|
adamc@281
|
958 }
|
adamc@282
|
959
|
adamc@467
|
960 char *uw_Basis_sqlifyBoolN(uw_context ctx, uw_Basis_bool *b) {
|
adamc@467
|
961 if (b == NULL)
|
adamc@467
|
962 return "NULL";
|
adamc@467
|
963 else
|
adamc@467
|
964 return uw_Basis_sqlifyBool(ctx, *b);
|
adamc@467
|
965 }
|
adamc@467
|
966
|
adamc@439
|
967 char *uw_Basis_sqlifyTime(uw_context ctx, uw_Basis_time t) {
|
adamc@439
|
968 size_t len;
|
adamc@439
|
969 char *r;
|
adamc@439
|
970 struct tm stm;
|
adamc@439
|
971
|
adamc@439
|
972 if (localtime_r(&t, &stm)) {
|
adamc@439
|
973 uw_check_heap(ctx, TIMES_MAX);
|
adamc@439
|
974 r = ctx->heap_front;
|
adamc@439
|
975 len = strftime(r, TIMES_MAX, TIME_FMT, &stm);
|
adamc@439
|
976 ctx->heap_front += len+1;
|
adamc@439
|
977 return r;
|
adamc@439
|
978 } else
|
adamc@439
|
979 return "<Invalid time>";
|
adamc@439
|
980 }
|
adamc@439
|
981
|
adamc@467
|
982 char *uw_Basis_sqlifyTimeN(uw_context ctx, uw_Basis_time *t) {
|
adamc@467
|
983 if (t == NULL)
|
adamc@467
|
984 return "NULL";
|
adamc@467
|
985 else
|
adamc@467
|
986 return uw_Basis_sqlifyTime(ctx, *t);
|
adamc@467
|
987 }
|
adamc@467
|
988
|
adamc@311
|
989 char *uw_Basis_ensqlBool(uw_Basis_bool b) {
|
adamc@311
|
990 static uw_Basis_int true = 1;
|
adamc@311
|
991 static uw_Basis_int false = 0;
|
adamc@282
|
992
|
adamc@311
|
993 if (b == uw_Basis_False)
|
adamc@282
|
994 return (char *)&false;
|
adamc@282
|
995 else
|
adamc@282
|
996 return (char *)&true;
|
adamc@282
|
997 }
|
adamc@284
|
998
|
adamc@311
|
999 uw_Basis_string uw_Basis_intToString(uw_context ctx, uw_Basis_int n) {
|
adamc@284
|
1000 int len;
|
adamc@284
|
1001 char *r;
|
adamc@284
|
1002
|
adamc@311
|
1003 uw_check_heap(ctx, INTS_MAX);
|
adamc@284
|
1004 r = ctx->heap_front;
|
adamc@284
|
1005 sprintf(r, "%lld%n", n, &len);
|
adamc@284
|
1006 ctx->heap_front += len+1;
|
adamc@284
|
1007 return r;
|
adamc@284
|
1008 }
|
adamc@285
|
1009
|
adamc@311
|
1010 uw_Basis_string uw_Basis_floatToString(uw_context ctx, uw_Basis_float n) {
|
adamc@285
|
1011 int len;
|
adamc@285
|
1012 char *r;
|
adamc@285
|
1013
|
adamc@311
|
1014 uw_check_heap(ctx, FLOATS_MAX);
|
adamc@285
|
1015 r = ctx->heap_front;
|
adamc@285
|
1016 sprintf(r, "%g%n", n, &len);
|
adamc@285
|
1017 ctx->heap_front += len+1;
|
adamc@285
|
1018 return r;
|
adamc@285
|
1019 }
|
adamc@285
|
1020
|
adamc@311
|
1021 uw_Basis_string uw_Basis_boolToString(uw_context ctx, uw_Basis_bool b) {
|
adamc@311
|
1022 if (b == uw_Basis_False)
|
adamc@285
|
1023 return "False";
|
adamc@285
|
1024 else
|
adamc@285
|
1025 return "True";
|
adamc@285
|
1026 }
|
adamc@288
|
1027
|
adamc@436
|
1028 uw_Basis_string uw_Basis_timeToString(uw_context ctx, uw_Basis_time t) {
|
adamc@436
|
1029 size_t len;
|
adamc@436
|
1030 char *r;
|
adamc@436
|
1031 struct tm stm;
|
adamc@436
|
1032
|
adamc@436
|
1033 if (localtime_r(&t, &stm)) {
|
adamc@436
|
1034 uw_check_heap(ctx, TIMES_MAX);
|
adamc@436
|
1035 r = ctx->heap_front;
|
adamc@436
|
1036 len = strftime(r, TIMES_MAX, TIME_FMT, &stm);
|
adamc@436
|
1037 ctx->heap_front += len+1;
|
adamc@436
|
1038 return r;
|
adamc@436
|
1039 } else
|
adamc@436
|
1040 return "<Invalid time>";
|
adamc@436
|
1041 }
|
adamc@288
|
1042
|
adamc@311
|
1043 uw_Basis_int *uw_Basis_stringToInt(uw_context ctx, uw_Basis_string s) {
|
adamc@288
|
1044 char *endptr;
|
adamc@311
|
1045 uw_Basis_int n = strtoll(s, &endptr, 10);
|
adamc@288
|
1046
|
adamc@288
|
1047 if (*s != '\0' && *endptr == '\0') {
|
adamc@311
|
1048 uw_Basis_int *r = uw_malloc(ctx, sizeof(uw_Basis_int));
|
adamc@288
|
1049 *r = n;
|
adamc@288
|
1050 return r;
|
adamc@288
|
1051 } else
|
adamc@288
|
1052 return NULL;
|
adamc@288
|
1053 }
|
adamc@289
|
1054
|
adamc@311
|
1055 uw_Basis_float *uw_Basis_stringToFloat(uw_context ctx, uw_Basis_string s) {
|
adamc@289
|
1056 char *endptr;
|
adamc@311
|
1057 uw_Basis_float n = strtod(s, &endptr);
|
adamc@289
|
1058
|
adamc@289
|
1059 if (*s != '\0' && *endptr == '\0') {
|
adamc@311
|
1060 uw_Basis_float *r = uw_malloc(ctx, sizeof(uw_Basis_float));
|
adamc@289
|
1061 *r = n;
|
adamc@289
|
1062 return r;
|
adamc@289
|
1063 } else
|
adamc@289
|
1064 return NULL;
|
adamc@289
|
1065 }
|
adamc@289
|
1066
|
adamc@311
|
1067 uw_Basis_bool *uw_Basis_stringToBool(uw_context ctx, uw_Basis_string s) {
|
adamc@311
|
1068 static uw_Basis_bool true = uw_Basis_True;
|
adamc@311
|
1069 static uw_Basis_bool false = uw_Basis_False;
|
adamc@289
|
1070
|
adamc@289
|
1071 if (!strcasecmp (s, "True"))
|
adamc@289
|
1072 return &true;
|
adamc@289
|
1073 else if (!strcasecmp (s, "False"))
|
adamc@289
|
1074 return &false;
|
adamc@289
|
1075 else
|
adamc@289
|
1076 return NULL;
|
adamc@289
|
1077 }
|
adamc@292
|
1078
|
adamc@436
|
1079 uw_Basis_time *uw_Basis_stringToTime(uw_context ctx, uw_Basis_string s) {
|
adamc@438
|
1080 char *dot = strchr(s, '.'), *end = strchr(s, 0);
|
adamc@436
|
1081 struct tm stm;
|
adamc@436
|
1082
|
adamc@439
|
1083 if (dot) {
|
adamc@439
|
1084 *dot = 0;
|
adamc@439
|
1085 if (strptime(s, TIME_FMT_PG, &stm) == end) {
|
adamc@439
|
1086 *dot = '.';
|
adamc@439
|
1087 uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time));
|
adamc@439
|
1088 *r = mktime(&stm);
|
adamc@439
|
1089 return r;
|
adamc@439
|
1090 }
|
adamc@439
|
1091 else {
|
adamc@439
|
1092 *dot = '.';
|
adamc@439
|
1093 return NULL;
|
adamc@439
|
1094 }
|
adamc@436
|
1095 }
|
adamc@439
|
1096 else {
|
adamc@439
|
1097 if (strptime(s, TIME_FMT_PG, &stm) == end) {
|
adamc@439
|
1098 uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time));
|
adamc@439
|
1099 *r = mktime(&stm);
|
adamc@439
|
1100 return r;
|
adamc@439
|
1101 }
|
adamc@439
|
1102 else if (strptime(s, TIME_FMT, &stm) == end) {
|
adamc@439
|
1103 uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time));
|
adamc@439
|
1104 *r = mktime(&stm);
|
adamc@439
|
1105 return r;
|
adamc@439
|
1106 }
|
adamc@439
|
1107 else
|
adamc@439
|
1108 return NULL;
|
adamc@439
|
1109 }
|
adamc@436
|
1110 }
|
adamc@436
|
1111
|
adamc@311
|
1112 uw_Basis_int uw_Basis_stringToInt_error(uw_context ctx, uw_Basis_string s) {
|
adamc@292
|
1113 char *endptr;
|
adamc@311
|
1114 uw_Basis_int n = strtoll(s, &endptr, 10);
|
adamc@292
|
1115
|
adamc@292
|
1116 if (*s != '\0' && *endptr == '\0')
|
adamc@292
|
1117 return n;
|
adamc@292
|
1118 else
|
adamc@311
|
1119 uw_error(ctx, FATAL, "Can't parse int: %s", s);
|
adamc@292
|
1120 }
|
adamc@293
|
1121
|
adamc@311
|
1122 uw_Basis_float uw_Basis_stringToFloat_error(uw_context ctx, uw_Basis_string s) {
|
adamc@293
|
1123 char *endptr;
|
adamc@311
|
1124 uw_Basis_float n = strtod(s, &endptr);
|
adamc@293
|
1125
|
adamc@293
|
1126 if (*s != '\0' && *endptr == '\0')
|
adamc@293
|
1127 return n;
|
adamc@293
|
1128 else
|
adamc@311
|
1129 uw_error(ctx, FATAL, "Can't parse float: %s", s);
|
adamc@293
|
1130 }
|
adamc@293
|
1131
|
adamc@311
|
1132 uw_Basis_bool uw_Basis_stringToBool_error(uw_context ctx, uw_Basis_string s) {
|
adamc@296
|
1133 if (!strcasecmp(s, "T") || !strcasecmp (s, "True"))
|
adamc@311
|
1134 return uw_Basis_True;
|
adamc@296
|
1135 else if (!strcasecmp(s, "F") || !strcasecmp (s, "False"))
|
adamc@311
|
1136 return uw_Basis_False;
|
adamc@293
|
1137 else
|
adamc@311
|
1138 uw_error(ctx, FATAL, "Can't parse bool: %s", s);
|
adamc@293
|
1139 }
|
adamc@436
|
1140
|
adamc@436
|
1141 uw_Basis_time uw_Basis_stringToTime_error(uw_context ctx, uw_Basis_string s) {
|
adamc@438
|
1142 char *dot = strchr(s, '.'), *end = strchr(s, 0);
|
adamc@436
|
1143 struct tm stm = {};
|
adamc@436
|
1144
|
adamc@438
|
1145 if (dot) {
|
adamc@438
|
1146 *dot = 0;
|
adamc@438
|
1147 if (strptime(s, TIME_FMT_PG, &stm)) {
|
adamc@438
|
1148 *dot = '.';
|
adamc@438
|
1149 return mktime(&stm);
|
adamc@438
|
1150 }
|
adamc@438
|
1151 else {
|
adamc@438
|
1152 *dot = '.';
|
adamc@438
|
1153 uw_error(ctx, FATAL, "Can't parse time: %s", s);
|
adamc@438
|
1154 }
|
adamc@438
|
1155 }
|
adamc@438
|
1156 else {
|
adamc@439
|
1157 if (strptime(s, TIME_FMT_PG, &stm) == end)
|
adamc@439
|
1158 return mktime(&stm);
|
adamc@439
|
1159 else if (strptime(s, TIME_FMT, &stm) == end)
|
adamc@438
|
1160 return mktime(&stm);
|
adamc@438
|
1161 else
|
adamc@438
|
1162 uw_error(ctx, FATAL, "Can't parse time: %s", s);
|
adamc@438
|
1163 }
|
adamc@436
|
1164 }
|
adamc@457
|
1165
|
adamc@457
|
1166 uw_Basis_string uw_Basis_requestHeader(uw_context ctx, uw_Basis_string h) {
|
adamc@457
|
1167 int len = strlen(h);
|
adamc@457
|
1168 char *s = ctx->headers, *p;
|
adamc@457
|
1169
|
adamc@457
|
1170 while (p = strchr(s, ':')) {
|
adamc@457
|
1171 if (p - s == len && !strncasecmp(s, h, len)) {
|
adamc@458
|
1172 return p + 2;
|
adamc@457
|
1173 } else {
|
adamc@458
|
1174 if ((s = strchr(p, 0)) && s < ctx->headers_end)
|
adamc@458
|
1175 s += 2;
|
adamc@457
|
1176 else
|
adamc@457
|
1177 return NULL;
|
adamc@457
|
1178 }
|
adamc@457
|
1179 }
|
adamc@463
|
1180 }
|
adamc@458
|
1181
|
adamc@463
|
1182 uw_Basis_string uw_Basis_get_cookie(uw_context ctx, uw_Basis_string c) {
|
adamc@463
|
1183 int len = strlen(c);
|
adamc@474
|
1184 char *s = ctx->headers, *p = ctx->outHeaders;
|
adamc@474
|
1185
|
adamc@474
|
1186 while (p = strstr(p, "\nSet-Cookie: ")) {
|
adamc@474
|
1187 char *p2;
|
adamc@474
|
1188 p += 13;
|
adamc@474
|
1189 p2 = strchr(p, '=');
|
adamc@474
|
1190
|
adamc@474
|
1191 if (p2) {
|
adamc@474
|
1192 size_t sz = strcspn(p2+1, ";\r\n");
|
adamc@474
|
1193
|
adamc@474
|
1194 if (!strncasecmp(p, c, p2 - p)) {
|
adamc@474
|
1195 char *ret = uw_malloc(ctx, sz + 1);
|
adamc@474
|
1196 memcpy(ret, p2+1, sz);
|
adamc@474
|
1197 ret[sz] = 0;
|
adamc@474
|
1198 return ret;
|
adamc@474
|
1199 }
|
adamc@474
|
1200 }
|
adamc@474
|
1201 }
|
adamc@463
|
1202
|
adamc@463
|
1203 while (p = strchr(s, ':')) {
|
adamc@476
|
1204 if (!strncasecmp(s, "Cookie: ", 8)) {
|
adamc@476
|
1205 p += 2;
|
adamc@476
|
1206 while (1) {
|
adamc@476
|
1207 if (!strncmp(p, c, len)
|
adamc@476
|
1208 && p + len < ctx->headers_end && p[len] == '=')
|
adamc@476
|
1209 return p + 1 + len;
|
adamc@476
|
1210 else if (p = strchr(p, ';'))
|
adamc@476
|
1211 p += 2;
|
adamc@476
|
1212 else if ((s = strchr(s, 0)) && s < ctx->headers_end) {
|
adamc@476
|
1213 s += 2;
|
adamc@476
|
1214 break;
|
adamc@476
|
1215 }
|
adamc@476
|
1216 else
|
adamc@476
|
1217 return NULL;
|
adamc@476
|
1218 }
|
adamc@463
|
1219 } else {
|
adamc@463
|
1220 if ((s = strchr(p, 0)) && s < ctx->headers_end)
|
adamc@463
|
1221 s += 2;
|
adamc@463
|
1222 else
|
adamc@463
|
1223 return NULL;
|
adamc@463
|
1224 }
|
adamc@463
|
1225 }
|
adamc@457
|
1226 }
|
adamc@462
|
1227
|
adamc@466
|
1228 uw_unit uw_Basis_set_cookie(uw_context ctx, uw_Basis_string prefix, uw_Basis_string c, uw_Basis_string v) {
|
adamc@462
|
1229 uw_write_header(ctx, "Set-Cookie: ");
|
adamc@462
|
1230 uw_write_header(ctx, c);
|
adamc@462
|
1231 uw_write_header(ctx, "=");
|
adamc@462
|
1232 uw_write_header(ctx, v);
|
adamc@466
|
1233 uw_write_header(ctx, "; path=");
|
adamc@466
|
1234 uw_write_header(ctx, prefix);
|
adamc@462
|
1235 uw_write_header(ctx, "\r\n");
|
adamc@462
|
1236
|
adamc@462
|
1237 return uw_unit_v;
|
adamc@462
|
1238 }
|