Mercurial > urweb
comparison src/c/lacweb.c @ 169:2232ab355f66
Took more advantage of new ability to exit with an error message
author | Adam Chlipala <adamc@hcoop.net> |
---|---|
date | Tue, 29 Jul 2008 16:02:02 -0400 |
parents | 2be573fec9a6 |
children | c7a5c8e0a0e0 |
comparison
equal
deleted
inserted
replaced
168:25b169416ea8 | 169:2232ab355f66 |
---|---|
1 #include <stdlib.h> | 1 #include <stdlib.h> |
2 #include <stdio.h> | 2 #include <stdio.h> |
3 #include <string.h> | 3 #include <string.h> |
4 #include <ctype.h> | 4 #include <ctype.h> |
5 #include <assert.h> | |
6 #include <setjmp.h> | 5 #include <setjmp.h> |
7 #include <stdarg.h> | 6 #include <stdarg.h> |
8 | 7 |
9 #include "types.h" | 8 #include "types.h" |
10 | 9 |
95 int lw_input_num(char*); | 94 int lw_input_num(char*); |
96 | 95 |
97 void lw_set_input(lw_context ctx, char *name, char *value) { | 96 void lw_set_input(lw_context ctx, char *name, char *value) { |
98 int n = lw_input_num(name); | 97 int n = lw_input_num(name); |
99 | 98 |
100 if (n < 0) { | 99 if (n < 0) |
101 printf("Bad input name"); | 100 lw_error(ctx, FATAL, "Bad input name %s", name); |
102 exit(1); | 101 |
103 } | 102 if (n >= lw_inputs_len) |
104 | 103 lw_error(ctx, FATAL, "For input name %s, index %d is out of range", name, n); |
105 assert(n < lw_inputs_len); | 104 |
106 ctx->inputs[n] = value; | 105 ctx->inputs[n] = value; |
107 | 106 |
108 printf("[%d] %s = %s\n", n, name, value); | 107 printf("[%d] %s = %s\n", n, name, value); |
109 } | 108 } |
110 | 109 |
111 char *lw_get_input(lw_context ctx, int n) { | 110 char *lw_get_input(lw_context ctx, int n) { |
112 assert(n >= 0); | 111 if (n < 0) |
113 assert(n < lw_inputs_len); | 112 lw_error(ctx, FATAL, "Negative input index %d", n); |
113 if (n >= lw_inputs_len) | |
114 lw_error(ctx, FATAL, "Out-of-bounds input index %d", n); | |
114 printf("[%d] = %s\n", n, ctx->inputs[n]); | 115 printf("[%d] = %s\n", n, ctx->inputs[n]); |
115 return ctx->inputs[n]; | 116 return ctx->inputs[n]; |
116 } | 117 } |
117 | 118 |
118 static void lw_check_heap(lw_context ctx, size_t extra) { | 119 static void lw_check_heap(lw_context ctx, size_t extra) { |
121 char *new_heap; | 122 char *new_heap; |
122 | 123 |
123 for (next = ctx->heap_back - ctx->heap_front; next < desired; next *= 2); | 124 for (next = ctx->heap_back - ctx->heap_front; next < desired; next *= 2); |
124 | 125 |
125 new_heap = realloc(ctx->heap, next); | 126 new_heap = realloc(ctx->heap, next); |
127 ctx->heap_front = new_heap; | |
128 ctx->heap_back = new_heap + next; | |
126 | 129 |
127 if (new_heap != ctx->heap) { | 130 if (new_heap != ctx->heap) { |
128 ctx->heap = new_heap; | 131 ctx->heap = new_heap; |
129 puts("Couldn't get contiguous chunk"); | 132 lw_error(ctx, UNLIMITED_RETRY, "Couldn't allocate new heap chunk contiguously"); |
130 exit(1); | 133 } |
131 } | 134 |
132 | 135 ctx->heap = new_heap; |
133 ctx->heap_back = new_heap + next; | |
134 } | 136 } |
135 } | 137 } |
136 | 138 |
137 void *lw_malloc(lw_context ctx, size_t len) { | 139 void *lw_malloc(lw_context ctx, size_t len) { |
138 void *result; | 140 void *result; |
400 r = atof(*s); | 402 r = atof(*s); |
401 *s = new_s; | 403 *s = new_s; |
402 return r; | 404 return r; |
403 } | 405 } |
404 | 406 |
405 static lw_Basis_string lw_unurlifyString_to(char *r, char *s) { | 407 static lw_Basis_string lw_unurlifyString_to(lw_context ctx, char *r, char *s) { |
406 char *s1, *s2; | 408 char *s1, *s2; |
407 int n; | 409 int n; |
408 | 410 |
409 for (s1 = r, s2 = s; *s2; ++s1, ++s2) { | 411 for (s1 = r, s2 = s; *s2; ++s1, ++s2) { |
410 char c = *s2; | 412 char c = *s2; |
412 switch (c) { | 414 switch (c) { |
413 case '+': | 415 case '+': |
414 *s1 = ' '; | 416 *s1 = ' '; |
415 break; | 417 break; |
416 case '%': | 418 case '%': |
417 assert(s2[1] != 0 && s2[2] != 0); | 419 if (s2[1] == 0) |
418 sscanf(s2+1, "%02X", &n); | 420 lw_error(ctx, FATAL, "Missing first character of escaped URL byte"); |
421 if (s2[2] == 0) | |
422 lw_error(ctx, FATAL, "Missing second character of escaped URL byte"); | |
423 if (sscanf(s2+1, "%02X", &n) != 1) | |
424 lw_error(ctx, FATAL, "Invalid escaped URL byte starting at: %s", s2); | |
419 *s1 = n; | 425 *s1 = n; |
420 s2 += 2; | 426 s2 += 2; |
421 break; | 427 break; |
422 default: | 428 default: |
423 *s1 = c; | 429 *s1 = c; |
434 | 440 |
435 len = strlen(*s); | 441 len = strlen(*s); |
436 lw_check_heap(ctx, len + 1); | 442 lw_check_heap(ctx, len + 1); |
437 | 443 |
438 r = ctx->heap_front; | 444 r = ctx->heap_front; |
439 ctx->heap_front = lw_unurlifyString_to(ctx->heap_front, *s); | 445 ctx->heap_front = lw_unurlifyString_to(ctx, ctx->heap_front, *s); |
440 *s = new_s; | 446 *s = new_s; |
441 return r; | 447 return r; |
442 } | 448 } |
443 | 449 |
444 | 450 |