Commit 9290451825009fe205ffba0c52c51b7ab03f17bf

Thomas de Grivel 2024-03-01T13:02:32

wip err.h

diff --git a/libc3/fn.c b/libc3/fn.c
index 56eb224..0a92eb0 100644
--- a/libc3/fn.c
+++ b/libc3/fn.c
@@ -11,7 +11,6 @@
  * THIS SOFTWARE.
  */
 #include "assert.h"
-#include <err.h>
 #include <stdlib.h>
 #include <string.h>
 #include <strings.h>
diff --git a/libc3/fn_clause.c b/libc3/fn_clause.c
index 7b82a4d..77f7c57 100644
--- a/libc3/fn_clause.c
+++ b/libc3/fn_clause.c
@@ -10,8 +10,7 @@
  * AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
  * THIS SOFTWARE.
  */
-#include <assert.h>
-#include <err.h>
+#include "assert.h"
 #include <stdlib.h>
 #include <strings.h>
 #include "arg.h"
@@ -71,8 +70,11 @@ s_fn_clause * fn_clause_init (s_fn_clause *fn_clause, s_fn_clause *next_clause)
 s_fn_clause * fn_clause_new (s_fn_clause *next_clause)
 {
   s_fn_clause *fn_clause;
-  if (! (fn_clause = calloc(1, sizeof(s_fn_clause))))
-    err(1, "fn_clause_new: calloc");
-  fn_clause_init(fn_clause, next_clause);
-  return fn_clause;
+  fn_clause = calloc(1, sizeof(s_fn_clause));
+  if (! fn_clause) {
+    err_puts("fn_clause_new: failed to allocate memory");
+    assert(! "fn_clause_new: failed to allocate memory");
+    return NULL;
+  }
+  return fn_clause_init(fn_clause, next_clause);
 }
diff --git a/libc3/frame.c b/libc3/frame.c
index 3e4c3c5..8a4f3fd 100644
--- a/libc3/frame.c
+++ b/libc3/frame.c
@@ -10,17 +10,21 @@
  * AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
  * THIS SOFTWARE.
  */
-#include <assert.h>
-#include <err.h>
+#include "assert.h"
 #include <stdlib.h>
 #include "binding.h"
 #include "frame.h"
 #include "list.h"
 
-void frame_binding_new (s_frame *frame, const s_sym *name,
-                        const s_tag *value)
+s_frame * frame_binding_new (s_frame *frame, const s_sym *name,
+                             const s_tag *value)
 {
-  frame->bindings = binding_new(name, value, frame->bindings);
+  s_binding *b;
+  b = binding_new(name, value, frame->bindings);
+  if (! b)
+    return NULL;
+  frame->bindings = b;
+  return frame;
 }
 
 s_frame * frame_clean (s_frame *frame)
@@ -55,16 +59,21 @@ const s_tag * frame_get (const s_frame *frame, const s_sym *sym)
 
 s_frame * frame_init (s_frame *frame, s_frame *next)
 {
+  s_frame tmp = {0};
   assert(frame);
-  frame->bindings = NULL;
-  frame->next = next;
+  tmp.next = next;
+  *frame = tmp;
   return frame;
 }
 
 s_frame * frame_new (s_frame *next)
 {
   s_frame *frame;
-  if (! (frame = malloc(sizeof(s_frame))))
-    errx(1, "frame_new: out of memory");
+  frame = calloc(1, sizeof(s_frame));
+  if (! frame) {
+    err_puts("frame_new: failed to allocate memory");
+    assert(! "frame_new: failed to allocate memory");
+    return NULL;
+  }
   return frame_init(frame, next);
 }
diff --git a/libc3/frame.h b/libc3/frame.h
index 1679261..d470d2e 100644
--- a/libc3/frame.h
+++ b/libc3/frame.h
@@ -27,8 +27,8 @@ s_frame * frame_delete (s_frame *frame);
 void      frame_delete_all (s_frame *frame);
 
 /* modifiers */
-void frame_binding_new(s_frame *frame, const s_sym *name,
-                       const s_tag *value);
+s_frame * frame_binding_new (s_frame *frame, const s_sym *name,
+                             const s_tag *value);
 
 /* observers */
 const s_tag * frame_get (const s_frame *frame, const s_sym *sym);
diff --git a/libc3/hash.c b/libc3/hash.c
index 668fac7..95eae6f 100644
--- a/libc3/hash.c
+++ b/libc3/hash.c
@@ -10,9 +10,7 @@
  * AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
  * THIS SOFTWARE.
  */
-#include <assert.h>
-#include <err.h>
-#include <stdlib.h>
+#include "assert.h"
 #include <string.h>
 #include "data.h"
 #include "hash.h"
@@ -490,7 +488,7 @@ bool hash_update_tag (t_hash *hash, const s_tag *tag)
   case TAG_VAR:   return hash_update_var(hash, NULL);
   case TAG_VOID:  return hash_update_void(hash, NULL);
   }
-  warnx("hash_update_tag: unknown tag type: %d", tag->type);
+  err_puts("hash_update_tag: unknown tag type");
   assert(! "hash_update_tag: unknown tag type");
   return false;
 }