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