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