diff src/c/lacweb.c @ 190:3eb53c957d10

Checkboxes
author Adam Chlipala <adamc@hcoop.net>
date Thu, 07 Aug 2008 13:09:26 -0400
parents 20bf7487c370
children ab86aa858e6c
line wrap: on
line diff
--- a/src/c/lacweb.c	Sun Aug 03 19:52:37 2008 -0400
+++ b/src/c/lacweb.c	Thu Aug 07 13:09:26 2008 -0400
@@ -18,7 +18,6 @@
 
   jmp_buf jmp_buf;
 
-  failure_kind failure_kind;
   char error_message[ERROR_BUF_LEN];
 };
 
@@ -35,7 +34,6 @@
 
   ctx->inputs = calloc(lw_inputs_len, sizeof(char *));
 
-  ctx->failure_kind = SUCCESS;
   ctx->error_message[0] = 0;
 
   return ctx;
@@ -52,15 +50,12 @@
   ctx->page_front = ctx->page;
   ctx->heap_front = ctx->heap;
 
-  ctx->failure_kind = SUCCESS;
   ctx->error_message[0] = 0;
 }
 
 void lw_reset_keep_error_message(lw_context ctx) {
   ctx->page_front = ctx->page;
   ctx->heap_front = ctx->heap;
-
-  ctx->failure_kind = SUCCESS;
 }
 
 void lw_reset(lw_context ctx) {
@@ -71,20 +66,21 @@
 void lw_handle(lw_context, char *);
 
 failure_kind lw_begin(lw_context ctx, char *path) {
-  if (!setjmp(ctx->jmp_buf))
+  int r = setjmp(ctx->jmp_buf);
+
+  if (r == 0)
     lw_handle(ctx, path);
 
-  return ctx->failure_kind;
+  return r;
 }
 
 void lw_error(lw_context ctx, failure_kind fk, const char *fmt, ...) {
   va_list ap;
   va_start(ap, fmt);
 
-  ctx->failure_kind = fk;
   vsnprintf(ctx->error_message, ERROR_BUF_LEN, fmt, ap);
 
-  longjmp(ctx->jmp_buf, 1);
+  longjmp(ctx->jmp_buf, fk);
 }
 
 char *lw_error_message(lw_context ctx) {
@@ -116,6 +112,15 @@
   return ctx->inputs[n];
 }
 
+char *lw_get_optional_input(lw_context ctx, int n) {
+  if (n < 0)
+    lw_error(ctx, FATAL, "Negative input index %d", n);
+  if (n >= lw_inputs_len)
+    lw_error(ctx, FATAL, "Out-of-bounds input index %d", n);
+  printf("[%d] = %s\n", n, ctx->inputs[n]);
+  return (ctx->inputs[n] == NULL ? "" : ctx->inputs[n]);
+}
+
 static void lw_check_heap(lw_context ctx, size_t extra) {
   if (ctx->heap_back - ctx->heap_front < extra) {
     size_t desired = ctx->heap_back - ctx->heap_front + extra, next;