Commit 6b3de68c0ab8ba29ee1d09046f731c04dfbaa43f

Thomas de Grivel 2023-11-26T13:03:57

debug

diff --git a/libc3/env.c b/libc3/env.c
index 451736f..2d0152f 100644
--- a/libc3/env.c
+++ b/libc3/env.c
@@ -776,24 +776,26 @@ s_env * env_init (s_env *env, sw argc, s8 **argv)
 
 s_env * env_init_args (s_env *env, sw argc, s8 **argv)
 {
+  s8 a[PATH_MAX];
+  s_buf buf;
   s8 *dir;
-  uw len;
+  sw r;
   assert(env);
-  if (argv) {
+  if (argc && argv) {
     env->argc = argc;
     env->argv = argv;
+    buf_init(&buf, false, sizeof(a), a);
     dir = dirname(argv[0]);
-    len = strlen(dir);
-    assert(len);
-    dir[len + 1] = '\0';
-    dir[len] = '/';
-    str_init_copy_1(&env->argv0_dir, dir);
-  }
-  else {
-    env->argc = 0;
-    env->argv = NULL;
-    str_init_1(&env->argv0_dir, NULL, "./");
+    if ((r = buf_write_1(&buf, dir)) < 0 ||
+        (r = buf_write_u8(&buf, '/') < 0))
+      goto ko;
+    buf_read_to_str(&buf, &env->argv0_dir);
+    return env;
   }
+ ko:
+  env->argc = 0;
+  env->argv = NULL;
+  str_init_1(&env->argv0_dir, NULL, "./");
   return env;
 }
 
diff --git a/libc3/facts.c b/libc3/facts.c
index ba0c1cd..0860831 100644
--- a/libc3/facts.c
+++ b/libc3/facts.c
@@ -29,6 +29,9 @@
 #include "set__tag.h"
 #include "skiplist__fact.h"
 #include "tag.h"
+/* debug */
+#include "env.h"
+#include "io.h"
 
 sw facts_open_file_create (s_facts *facts, const s_str *path);
 sw facts_open_log (s_facts *facts, s_buf *buf);
@@ -252,6 +255,8 @@ sw facts_load (s_facts *facts, s_buf *buf, const s_str *path)
       goto ko_fact;
     result += r;
     factp = fact_r(&fact);
+    buf_write_1(&g_c3_env.out, replace ? "replace " : "add ");
+    io_inspect_fact(factp);
     if (replace)
       facts_replace_fact(facts, factp);
     else
diff --git a/libc3/hash.c b/libc3/hash.c
index e060f2f..064a88c 100644
--- a/libc3/hash.c
+++ b/libc3/hash.c
@@ -17,6 +17,8 @@
 #include "hash.h"
 #include "list.h"
 #include "str.h"
+/* debug */
+#include "io.h"
 
 #define HASH_UPDATE_DEF(type)                                          \
   void hash_update_##type (t_hash *hash, type x)                       \
@@ -132,6 +134,8 @@ void hash_update_fact (t_hash *hash, const s_fact *fact)
   assert(fact->subject);
   assert(fact->predicate);
   assert(fact->object);
+  io_puts("hash_update_fact");
+  io_inspect_fact(fact);
   hash_update(hash, type, sizeof(type));
   hash_update_tag(hash, fact->subject);
   hash_update_tag(hash, fact->predicate);
diff --git a/libc3/io.c b/libc3/io.c
index 198799f..4c149e3 100644
--- a/libc3/io.c
+++ b/libc3/io.c
@@ -15,43 +15,79 @@
 #include "env.h"
 #include "io.h"
 
-sw err_inspect (const s_tag *tag)
+#define DEF_ERR_INSPECT(name, type)                                    \
+  sw err_inspect_ ## name (const type *x)                              \
+  {                                                                    \
+    sw r;                                                              \
+    sw result = 0;                                                     \
+    if ((r = buf_inspect_ ## name(&g_c3_env.err, x)) < 0)              \
+      return r;                                                        \
+    result += r;                                                       \
+    if ((r = buf_write_u8(&g_c3_env.err, '\n')) < 0)                   \
+      return r;                                                        \
+    result += r;                                                       \
+    buf_flush(&g_c3_env.err);                                          \
+    return result;                                                     \
+  }
+
+#define DEF_IO_INSPECT(name, type)                                     \
+  sw io_inspect_ ## name (const type *x)                               \
+  {                                                                    \
+    sw r;                                                              \
+    sw result = 0;                                                     \
+    if ((r = buf_inspect_ ## name(&g_c3_env.out, x)) < 0)              \
+      return r;                                                        \
+    result += r;                                                       \
+    if ((r = buf_write_u8(&g_c3_env.out, '\n')) < 0)                   \
+      return r;                                                        \
+    result += r;                                                       \
+    buf_flush(&g_c3_env.out);                                          \
+    return result;                                                     \
+  }
+
+sw err_inspect (const s_tag *x)
 {
   sw r;
-  r = buf_inspect_tag(&g_c3_env.err, tag);
+  sw result = 0;
+  if ((r = buf_inspect_tag(&g_c3_env.err, x)) < 0)
+    return r;
+  result += r;
+  if ((r = buf_write_u8(&g_c3_env.err, '\n')) < 0)
+    return r;
+  result += r;
   buf_flush(&g_c3_env.err);
-  return r;
+  return result;
 }
 
-sw err_inspect_fn_pattern (const s_list *list)
+sw err_inspect_fn_pattern (const s_list *x)
 {
   sw r;
-  r = buf_inspect_fn_pattern(&g_c3_env.err, list);
+  r = buf_inspect_fn_pattern(&g_c3_env.err, x);
   buf_flush(&g_c3_env.err);
   return r;
 }
 
-sw err_inspect_list (const s_list *list)
+sw err_inspect_list (const s_list *x)
 {
   sw r;
-  r = buf_inspect_list(&g_c3_env.err, &list);
+  r = buf_inspect_list(&g_c3_env.err, &x);
   buf_flush(&g_c3_env.err);
   return r;
 }
 
-sw err_puts (const s8 *s)
+sw err_puts (const s8 *x)
 {
   sw r;
-  r = buf_write_1(&g_c3_env.err, s);
+  r = buf_write_1(&g_c3_env.err, x);
   buf_flush(&g_c3_env.err);
   return r;
 }
 
-sw io_inspect (const s_tag *tag)
+sw io_inspect (const s_tag *x)
 {
   sw r;
   sw result = 0;
-  if ((r = buf_inspect_tag(&g_c3_env.out, tag)) < 0)
+  if ((r = buf_inspect_tag(&g_c3_env.out, x)) < 0)
     return r;
   result += r;
   if ((r = buf_write_u8(&g_c3_env.out, '\n')) < 0)
@@ -61,11 +97,14 @@ sw io_inspect (const s_tag *tag)
   return result;
 }
 
-sw io_inspect_str (const s_str *str)
+DEF_IO_INSPECT(fact, s_fact)
+DEF_IO_INSPECT(str,  s_str)
+
+sw io_puts (const s8 *x)
 {
   sw r;
   sw result = 0;
-  if ((r = buf_inspect_str(&g_c3_env.out, str)) < 0)
+  if ((r = buf_write_1(&g_c3_env.out, x)) < 0)
     return r;
   result += r;
   if ((r = buf_write_u8(&g_c3_env.out, '\n')) < 0)
@@ -74,11 +113,3 @@ sw io_inspect_str (const s_str *str)
   buf_flush(&g_c3_env.out);
   return result;
 }
-
-sw io_puts (const s8 *s)
-{
-  sw r;
-  r = buf_write_1(&g_c3_env.out, s);
-  buf_flush(&g_c3_env.out);
-  return r;
-}
diff --git a/libc3/io.h b/libc3/io.h
index 24f671a..bea2409 100644
--- a/libc3/io.h
+++ b/libc3/io.h
@@ -15,15 +15,22 @@
 
 #include "types.h"
 
+#define ERR_INSPECT(name, type)                                        \
+  sw err_inspect_ ## name (const type *x)                              \
+
+#define IO_INSPECT(name, type)                                         \
+  sw io_inspect_ ## name (const type *x)                               \
+
 /* error output */
-sw err_inspect (const s_tag *tag);
-sw err_inspect_fn_pattern (const s_list *list);
-sw err_inspect_list (const s_list *list);
-sw err_puts (const s8 *s);
+sw err_inspect (const s_tag *x);
+sw err_inspect_fn_pattern (const s_list *x);
+ERR_INSPECT(list, s_list);
+sw err_puts (const s8 *x);
 
 /* standard output */
-sw io_inspect (const s_tag *tag);
-sw io_inspect_str (const s_str *str);
-sw io_puts (const s8 *s);
+sw io_inspect (const s_tag *x);
+IO_INSPECT(fact, s_fact);
+IO_INSPECT(str,  s_str);
+sw io_puts (const s8 *x);
 
 #endif /* LIBC3_IO_H */