Commit bb51f91d39ed0e75de22866872fbd65a3c141440

Thomas de Grivel 2024-07-31T16:34:06

wip httpd

diff --git a/httpd/httpd.c b/httpd/httpd.c
index 98dc664..cff9c4d 100644
--- a/httpd/httpd.c
+++ b/httpd/httpd.c
@@ -15,14 +15,42 @@
 
 int main (int argc, char **argv)
 {
-  s_str path;
+  s_call call = {0};
+  const s_sym *module = NULL;
+  int r = 1;
+  s_tag tmp = {0};
   kc3_init(NULL, &argc, &argv);
-  if (! module_path(sym_1("HTTPd"), &g_kc3_env.module_path, KC3_EXT,
-                    &path)) {
+  io_puts("KC3 HTTPd loading, please wait...");
+  module = sym_1("HTTPd");
+  if (! module_load(module)) {
     kc3_clean(NULL);
     return 1;
   }
-  kc3_load(&path);
+  call_init(&call);
+  call.ident.module = module;
+  call.ident.sym = sym_1("main");
+  if (argc >= 2)
+    call.arguments = list_new_str_1
+      (NULL, argv[0], list_new_str_1
+       (NULL, argv[1], NULL));
+  else if (argc == 1)
+    call.arguments = list_new_str_1(NULL, argv[0], NULL);
+  else
+    call.arguments = NULL;
+  if (! eval_call(&call, &tmp))
+    goto clean;
+  switch (tmp.type) {
+  case TAG_U8:
+    r = tmp.data.u8;
+    break;
+  default:
+    err_write_1("invalid return type from main: ");
+    err_inspect_tag(&tmp);
+    err_write_1("\n");
+  }
+ clean:
+  tag_clean(&tmp);
+  call_clean(&call);
   kc3_clean(NULL);
-  return 0;
+  return r;
 }
diff --git a/lib/kc3/0.1/httpd.kc3 b/lib/kc3/0.1/httpd.kc3
index 8c9a617..a586971 100644
--- a/lib/kc3/0.1/httpd.kc3
+++ b/lib/kc3/0.1/httpd.kc3
@@ -1,5 +1,3 @@
-puts("KC3 HTTPd loading, please wait...")
-
 defmodule HTTPd do
 
   def server_loop = fn (client) {
@@ -32,10 +30,19 @@ defmodule HTTPd do
     puts("KC3 HTTPd: listening on #{host}:#{port}")
     while true do
       client = Socket.Buf.accept(socket)
-      HTTPd.server_loop(client)
+      server_loop(client)
     end
   }
 
-end
+  def main = fn {
+    () {
+      host = getenv("KC3_HTTPD_HOST")
+      port = getenv("KC3_HTTPD_PORT")
+      server(host, port)
+    }
+    (host, port) {
+      server(host, port)
+    }     
+  }
 
-HTTPd.server(getenv("KC3_HTTPD_HOST"), getenv("KC3_HTTPD_PORT"))
+end
diff --git a/libkc3/eval.c b/libkc3/eval.c
index 890fcaa..69ee5cb 100644
--- a/libkc3/eval.c
+++ b/libkc3/eval.c
@@ -14,12 +14,18 @@
 #include "types.h"
 #include "env.h"
 
-bool eval_tag (const s_tag *tag, s_tag *dest)
+
+bool eval_call (const s_call *call, s_tag *dest)
 {
-  return env_eval_tag(&g_kc3_env, tag, dest);
+  return env_eval_call(&g_kc3_env, call, dest);
 }
 
 bool eval_fn_call (const s_fn *fn, const s_list *args, s_tag *dest)
 {
   return env_eval_call_fn_args(&g_kc3_env, fn, args, dest);
 }
+
+bool eval_tag (const s_tag *tag, s_tag *dest)
+{
+  return env_eval_tag(&g_kc3_env, tag, dest);
+}
diff --git a/libkc3/eval.h b/libkc3/eval.h
index 2db3724..a99e5c7 100644
--- a/libkc3/eval.h
+++ b/libkc3/eval.h
@@ -15,6 +15,7 @@
 
 #include "types.h"
 
+bool eval_call (const s_call *call, s_tag *dest);
 bool eval_call_function (const s_call *call,
                          s_tag *dest);
 bool eval_call_macro (const s_call *call, s_tag *dest);