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