summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLaurent Bercot <ska-skaware@skarnet.org>2023-05-18 02:19:59 +0000
committerLaurent Bercot <ska@appnovation.com>2023-05-18 02:19:59 +0000
commit2af587d68d11b1826c356364b4e9679199eee2ed (patch)
tree4cffead67aea555d1306495e60fc30ce1185db7b /src
parent8ef88c7d9a7108409c1e58c21b3d0590c859384f (diff)
downloadskalibs-2af587d68d11b1826c356364b4e9679199eee2ed.tar.xz
Add slurpn, refactor slurp, fix avltree/cdbmake bugs, add strerr_warnf
Signed-off-by: Laurent Bercot <ska@appnovation.com>
Diffstat (limited to 'src')
-rw-r--r--src/include/skalibs/avltree.h2
-rw-r--r--src/include/skalibs/cdbmake.h2
-rw-r--r--src/include/skalibs/djbunix.h8
-rw-r--r--src/include/skalibs/strerr.h52
-rw-r--r--src/libstddjb/openslurpclose.c8
-rw-r--r--src/libstddjb/openslurpnclose.c13
-rw-r--r--src/libstddjb/slurp.c26
-rw-r--r--src/libstddjb/slurpn.c35
8 files changed, 112 insertions, 34 deletions
diff --git a/src/include/skalibs/avltree.h b/src/include/skalibs/avltree.h
index 5840395..11f1f23 100644
--- a/src/include/skalibs/avltree.h
+++ b/src/include/skalibs/avltree.h
@@ -51,8 +51,8 @@ extern int avltree_insert (avltree *, uint32_t) ;
extern int avltree_delete (avltree *, void const *) ;
-#define avltree_iter(t, f, p) avlnode_iter(avltree_nodes(t), avltree_totalsize(t), avltree_root(t), f, p)
#define avltree_iter_nocancel(t, cut, f, p) avlnode_iter(avltree_nodes(t), avltree_totalsize(t), cut, avltree_root(t), f, p)
+#define avltree_iter(t, f, p) avltree_iter_nocancel(t, avltree_totalsize(t), f, p)
#define avltree_iter_withcancel(t, f, cancelf, p) avlnode_iter_withcancel(avltree_nodes(t), avltree_totalsize(t), avltree_root(t), f, cancelf, p)
#endif
diff --git a/src/include/skalibs/cdbmake.h b/src/include/skalibs/cdbmake.h
index 26fcba5..0da0a86 100644
--- a/src/include/skalibs/cdbmake.h
+++ b/src/include/skalibs/cdbmake.h
@@ -22,7 +22,7 @@ struct cdbmaker_s
#define CDBMAKER_ZERO { .hplist = GENALLOC_ZERO, .pos = 2048, .b = BUFFER_INIT(&fd_writev, -1, 0, 0) }
extern int cdbmake_start (cdbmaker *, int) ;
-extern int cdbmake_add (cdbmaker *, char const *, unsigned int, char const *, unsigned int) ;
+extern int cdbmake_add (cdbmaker *, char const *, uint32_t, char const *, uint32_t) ;
extern int cdbmake_finish (cdbmaker *) ;
#endif
diff --git a/src/include/skalibs/djbunix.h b/src/include/skalibs/djbunix.h
index cae59b4..5401515 100644
--- a/src/include/skalibs/djbunix.h
+++ b/src/include/skalibs/djbunix.h
@@ -84,6 +84,14 @@ extern int sagethostname (stralloc *) ;
extern int slurp (stralloc *, int) ;
extern int openslurpclose (stralloc *, char const *) ;
+/*
+ TODO: next ABI break: change to
+#define slurp(sa, fd) slurpn((fd), (sa), 0)
+#define openslurpclose(sa, fn) openslurpnclose((fn), (sa), 0)
+*/
+
+extern int slurpn (int, stralloc *, size_t) ;
+extern int openslurpnclose (char const *, stralloc *, size_t) ;
extern ssize_t readnclose (int fd, char *, size_t) ; /* closes fd */
extern ssize_t openreadnclose (char const *, char *, size_t) ;
extern ssize_t openreadnclose_nb (char const *, char *, size_t) ;
diff --git a/src/include/skalibs/strerr.h b/src/include/skalibs/strerr.h
index d2b7492..648a104 100644
--- a/src/include/skalibs/strerr.h
+++ b/src/include/skalibs/strerr.h
@@ -25,6 +25,8 @@ extern char const *PROG ;
#define strerr_warnwn(n, ...) strerr_warnn((n)+2, PROG, ": warning: ", __VA_ARGS__)
#define strerr_warnwnsys(n, ...) strerr_warnnsys((n)+2, PROG, ": warning: ", __VA_ARGS__)
+#define strerr_warnfn(n, ...) strerr_warnn((n)+2, PROG, ": fatal: ", __VA_ARGS__)
+#define strerr_warnfnsys(n, ...) strerr_warnnsys((n)+2, PROG, ": fatal: ", __VA_ARGS__)
#define strerr_diewn(e, n, ...) strerr_dien(e, (n)+2, PROG, ": warning: ", __VA_ARGS__)
#define strerr_diewnsys(e, n, ...) strerr_diensys(e, (n)+2, PROG, ": warning: ", __VA_ARGS__)
#define strerr_diefn(e, n, ...) strerr_dien(e, (n)+2, PROG, ": fatal: ", __VA_ARGS__)
@@ -32,6 +34,8 @@ extern char const *PROG ;
#define strerr_warnw(...) strerr_warn(PROG, ": warning: ", __VA_ARGS__)
#define strerr_warnwsys(...) strerr_warnsys(PROG, ": warning: ", __VA_ARGS__)
+#define strerr_warnf(...) strerr_warn(PROG, ": fatal: ", __VA_ARGS__)
+#define strerr_warnfsys(...) strerr_warnsys(PROG, ": fatal: ", __VA_ARGS__)
#define strerr_diew(e, ...) strerr_die(e, PROG, ": warning: ", __VA_ARGS__)
#define strerr_diewsys(e, ...) strerr_diesys(e, PROG, ": warning: ", __VA_ARGS__)
#define strerr_dief(e, ...) strerr_die(e, PROG, ": fatal: ", __VA_ARGS__)
@@ -39,6 +43,8 @@ extern char const *PROG ;
#define strerr_warnwun(n, ...) strerr_warnwn((n)+1, "unable to ", __VA_ARGS__)
#define strerr_warnwunsys(n, ...) strerr_warnwnsys((n)+1, "unable to ", __VA_ARGS__)
+#define strerr_warnfun(n, ...) strerr_warnfn((n)+1, "unable to ", __VA_ARGS__)
+#define strerr_warnfunsys(n, ...) strerr_warnfnsys((n)+1, "unable to ", __VA_ARGS__)
#define strerr_diewun(e, n, ...) strerr_diewn(e, (n)+1, "unable to ", __VA_ARGS__)
#define strerr_diewunsys(e, n, ...) strerr_diewnsys(e, (n)+1, "unable to ", __VA_ARGS__)
#define strerr_diefun(e, n, ...) strerr_diefn(e, (n)+1, ": unable to ", __VA_ARGS__)
@@ -46,6 +52,8 @@ extern char const *PROG ;
#define strerr_warnwu(...) strerr_warnw("unable to ", __VA_ARGS__)
#define strerr_warnwusys(...) strerr_warnwsys("unable to ", __VA_ARGS__)
+#define strerr_warnfu(...) strerr_warnf("unable to ", __VA_ARGS__)
+#define strerr_warnfusys(...) strerr_warnfsys("unable to ", __VA_ARGS__)
#define strerr_diewu(e, ...) strerr_diew(e, "unable to ", __VA_ARGS__)
#define strerr_diewusys(e, ...) strerr_diewsys(e, "unable to ", __VA_ARGS__)
#define strerr_diefu(e, ...) strerr_dief(e, "unable to ", __VA_ARGS__)
@@ -91,6 +99,28 @@ extern char const *PROG ;
#define strerr_warnw9sys(x1, x2, x3, x4, x5, x6, x7, x8, x9) strerr_warnwnsys(9, x1, x2, x3, x4, x5, x6, x7, x8, x9)
#define strerr_warnw10sys(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10) strerr_warnwnsys(10, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10)
+#define strerr_warnf1x(x1) strerr_warnfn(1, x1)
+#define strerr_warnf2x(x1, x2) strerr_warnfn(2, x1, x2)
+#define strerr_warnf3x(x1, x2, x3) strerr_warnfn(3, x1, x2, x3)
+#define strerr_warnf4x(x1, x2, x3, x4) strerr_warnfn(4, x1, x2, x3, x4)
+#define strerr_warnf5x(x1, x2, x3, x4, x5) strerr_warnfn(5, x1, x2, x3, x4, x5)
+#define strerr_warnf6x(x1, x2, x3, x4, x5, x6) strerr_warnfn(6, x1, x2, x3, x4, x5, x6)
+#define strerr_warnf7x(x1, x2, x3, x4, x5, x6, x7) strerr_warnfn(7, x1, x2, x3, x4, x5, x6, x7)
+#define strerr_warnf8x(x1, x2, x3, x4, x5, x6, x7, x8) strerr_warnfn(8, x1, x2, x3, x4, x5, x6, x7, x8)
+#define strerr_warnf9x(x1, x2, x3, x4, x5, x6, x7, x8, x9) strerr_warnfn(9, x1, x2, x3, x4, x5, x6, x7, x8, x9)
+#define strerr_warnf10x(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10) strerr_warnfn(10, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10)
+
+#define strerr_warnf1sys(x1) strerr_warnfnsys(1, x1)
+#define strerr_warnf2sys(x1, x2) strerr_warnfnsys(2, x1, x2)
+#define strerr_warnf3sys(x1, x2, x3) strerr_warnfnsys(3, x1, x2, x3)
+#define strerr_warnf4sys(x1, x2, x3, x4) strerr_warnfnsys(4, x1, x2, x3, x4)
+#define strerr_warnf5sys(x1, x2, x3, x4, x5) strerr_warnfnsys(5, x1, x2, x3, x4, x5)
+#define strerr_warnf6sys(x1, x2, x3, x4, x5, x6) strerr_warnfnsys(6, x1, x2, x3, x4, x5, x6)
+#define strerr_warnf7sys(x1, x2, x3, x4, x5, x6, x7) strerr_warnfnsys(7, x1, x2, x3, x4, x5, x6, x7)
+#define strerr_warnf8sys(x1, x2, x3, x4, x5, x6, x7, x8) strerr_warnfnsys(8, x1, x2, x3, x4, x5, x6, x7, x8)
+#define strerr_warnf9sys(x1, x2, x3, x4, x5, x6, x7, x8, x9) strerr_warnfnsys(9, x1, x2, x3, x4, x5, x6, x7, x8, x9)
+#define strerr_warnf10sys(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10) strerr_warnfnsys(10, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10)
+
#define strerr_diew1x(e, x1) strerr_diewn(e, 1, x1)
#define strerr_diew2x(e, x1, x2) strerr_diewn(e, 2, x1, x2)
#define strerr_diew3x(e, x1, x2, x3) strerr_diewn(e, 3, x1, x2, x3)
@@ -157,6 +187,28 @@ extern char const *PROG ;
#define strerr_warnwu9sys(x1, x2, x3, x4, x5, x6, x7, x8, x9) strerr_warnwunsys(9, x1, x2, x3, x4, x5, x6, x7, x8, x9)
#define strerr_warnwu10sys(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10) strerr_warnwunsys(10, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10)
+#define strerr_warnfu1x(x1) strerr_warnfun(1, x1)
+#define strerr_warnfu2x(x1, x2) strerr_warnfun(2, x1, x2)
+#define strerr_warnfu3x(x1, x2, x3) strerr_warnfun(3, x1, x2, x3)
+#define strerr_warnfu4x(x1, x2, x3, x4) strerr_warnfun(4, x1, x2, x3, x4)
+#define strerr_warnfu5x(x1, x2, x3, x4, x5) strerr_warnfun(5, x1, x2, x3, x4, x5)
+#define strerr_warnfu6x(x1, x2, x3, x4, x5, x6) strerr_warnfun(6, x1, x2, x3, x4, x5, x6)
+#define strerr_warnfu7x(x1, x2, x3, x4, x5, x6, x7) strerr_warnfun(7, x1, x2, x3, x4, x5, x6, x7)
+#define strerr_warnfu8x(x1, x2, x3, x4, x5, x6, x7, x8) strerr_warnfun(8, x1, x2, x3, x4, x5, x6, x7, x8)
+#define strerr_warnfu9x(x1, x2, x3, x4, x5, x6, x7, x8, x9) strerr_warnfun(9, x1, x2, x3, x4, x5, x6, x7, x8, x9)
+#define strerr_warnfu10x(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10) strerr_warnfun(10, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10)
+
+#define strerr_warnfu1sys(x1) strerr_warnfunsys(1, x1)
+#define strerr_warnfu2sys(x1, x2) strerr_warnfunsys(2, x1, x2)
+#define strerr_warnfu3sys(x1, x2, x3) strerr_warnfunsys(3, x1, x2, x3)
+#define strerr_warnfu4sys(x1, x2, x3, x4) strerr_warnfunsys(4, x1, x2, x3, x4)
+#define strerr_warnfu5sys(x1, x2, x3, x4, x5) strerr_warnfunsys(5, x1, x2, x3, x4, x5)
+#define strerr_warnfu6sys(x1, x2, x3, x4, x5, x6) strerr_warnfunsys(6, x1, x2, x3, x4, x5, x6)
+#define strerr_warnfu7sys(x1, x2, x3, x4, x5, x6, x7) strerr_warnfunsys(7, x1, x2, x3, x4, x5, x6, x7)
+#define strerr_warnfu8sys(x1, x2, x3, x4, x5, x6, x7, x8) strerr_warnfunsys(8, x1, x2, x3, x4, x5, x6, x7, x8)
+#define strerr_warnfu9sys(x1, x2, x3, x4, x5, x6, x7, x8, x9) strerr_warnfunsys(9, x1, x2, x3, x4, x5, x6, x7, x8, x9)
+#define strerr_warnfu10sys(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10) strerr_warnfunsys(10, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10)
+
#define strerr_diewu1x(e, x1) strerr_diewun(e, 1, x1)
#define strerr_diewu2x(e, x1, x2) strerr_diewun(e, 2, x1, x2)
#define strerr_diewu3x(e, x1, x2, x3) strerr_diewun(e, 3, x1, x2, x3)
diff --git a/src/libstddjb/openslurpclose.c b/src/libstddjb/openslurpclose.c
index 8485413..e6b0570 100644
--- a/src/libstddjb/openslurpclose.c
+++ b/src/libstddjb/openslurpclose.c
@@ -1,14 +1,8 @@
/* ISC license. */
-#include <skalibs/stralloc.h>
#include <skalibs/djbunix.h>
int openslurpclose (stralloc *sa, char const *fn)
{
- int r ;
- int fd = openbc_read(fn) ;
- if (fd == -1) return 0 ;
- r = slurp(sa, fd) ;
- fd_close(fd) ;
- return r ;
+ return openslurpnclose(fn, sa, 0) ;
}
diff --git a/src/libstddjb/openslurpnclose.c b/src/libstddjb/openslurpnclose.c
new file mode 100644
index 0000000..3672e16
--- /dev/null
+++ b/src/libstddjb/openslurpnclose.c
@@ -0,0 +1,13 @@
+/* ISC license. */
+
+#include <skalibs/djbunix.h>
+
+int openslurpnclose (char const *fn, stralloc *sa, size_t max)
+{
+ int r ;
+ int fd = openbc_read(fn) ;
+ if (fd == -1) return 0 ;
+ r = slurpn(fd, sa, max) ;
+ fd_close(fd) ;
+ return r ;
+}
diff --git a/src/libstddjb/slurp.c b/src/libstddjb/slurp.c
index 7437142..eccd833 100644
--- a/src/libstddjb/slurp.c
+++ b/src/libstddjb/slurp.c
@@ -1,32 +1,8 @@
/* ISC license. */
-#include <sys/types.h>
-
-#include <skalibs/allreadwrite.h>
-#include <skalibs/stralloc.h>
#include <skalibs/djbunix.h>
-#define N 4096
-
int slurp (stralloc *sa, int fd)
{
- size_t sabase = sa->len ;
- int wasnull = !sa->s ;
- for (;;)
- {
- ssize_t r ;
- if (!stralloc_readyplus(sa, N)) break ;
- r = fd_read(fd, sa->s + sa->len, N) ;
- switch (r)
- {
- case -1 : goto err ;
- case 0 : return 1 ;
- default : sa->len += r ;
- }
- }
-
-err:
- if (wasnull) stralloc_free(sa) ;
- else sa->len = sabase ;
- return 0 ;
+ return slurpn(fd, sa, 0) ;
}
diff --git a/src/libstddjb/slurpn.c b/src/libstddjb/slurpn.c
new file mode 100644
index 0000000..c9c5c11
--- /dev/null
+++ b/src/libstddjb/slurpn.c
@@ -0,0 +1,35 @@
+/* ISC license. */
+
+#include <sys/types.h>
+#include <errno.h>
+
+#include <skalibs/allreadwrite.h>
+#include <skalibs/stralloc.h>
+#include <skalibs/djbunix.h>
+
+#define N 4096
+
+int slurpn (int fd, stralloc *sa, size_t max)
+{
+ size_t sabase = sa->len ;
+ int wasnull = !sa->s ;
+ for (;;)
+ {
+ ssize_t r ;
+ size_t n = max && sa->len + N > max ? max - sa->len : N ;
+ if (!n) { errno = ENOBUFS ; goto err ; }
+ if (!stralloc_readyplus(sa, n)) break ;
+ r = fd_read(fd, sa->s + sa->len, n) ;
+ switch (r)
+ {
+ case -1 : goto err ;
+ case 0 : return 1 ;
+ default : sa->len += r ;
+ }
+ }
+
+err:
+ if (wasnull) stralloc_free(sa) ;
+ else sa->len = sabase ;
+ return 0 ;
+}