aboutsummaryrefslogtreecommitdiffstats
path: root/src/pipe-tools
diff options
context:
space:
mode:
authorLaurent Bercot <ska-skaware@skarnet.org>2026-03-16 21:09:38 +0000
committerLaurent Bercot <ska-skaware@skarnet.org>2026-03-16 21:09:38 +0000
commit41dec6436242c4daccdd9fdee3ac052004c439bd (patch)
treead554b53318b2a0880192f7f021908fd5f03254c /src/pipe-tools
parent2505243cd4becc70b6745fb2da742ca8a456a3be (diff)
downloads6-41dec6436242c4daccdd9fdee3ac052004c439bd.tar.gz
Refactor ftrig using sass; clean up API a bit, update its users
Diffstat (limited to 'src/pipe-tools')
-rw-r--r--src/pipe-tools/s6-ftrig-listen.c100
-rw-r--r--src/pipe-tools/s6-ftrig-listen1.c73
-rw-r--r--src/pipe-tools/s6-ftrig-wait.c62
3 files changed, 134 insertions, 101 deletions
diff --git a/src/pipe-tools/s6-ftrig-listen.c b/src/pipe-tools/s6-ftrig-listen.c
index 041226e..5fa3e33 100644
--- a/src/pipe-tools/s6-ftrig-listen.c
+++ b/src/pipe-tools/s6-ftrig-listen.c
@@ -5,9 +5,11 @@
#include <signal.h>
#include <unistd.h>
-#include <skalibs/sgetopt.h>
+#include <skalibs/posixplz.h>
+#include <skalibs/uint64.h>
#include <skalibs/types.h>
#include <skalibs/strerr.h>
+#include <skalibs/gol.h>
#include <skalibs/tai.h>
#include <skalibs/iopause.h>
#include <skalibs/djbunix.h>
@@ -21,6 +23,17 @@
#define USAGE "s6-ftrig-listen [ -a | -o ] [ -t timeout ] fifodir1 regexp1 ... \"\" prog..."
#define dieusage() strerr_dieusage(100, USAGE)
+enum golb_e
+{
+ GOLB_OR = 0x01,
+} ;
+
+enum gola_e
+{
+ GOLA_TIMEOUT,
+ GOLA_N
+} ;
+
static void handle_signals (void)
{
for (;;) switch (selfpipe_read())
@@ -32,37 +45,42 @@ static void handle_signals (void)
}
}
-int main (int argc, char const **argv, char const *const *envp)
+int main (int argc, char const **argv)
{
- iopause_fd x[2] = { { -1, IOPAUSE_READ, 0 }, { -1, IOPAUSE_READ, 0 } } ;
- tain deadline, tto ;
- ftrigr_t a = FTRIGR_ZERO ;
+ static gol_bool const rgolb[] =
+ {
+ { .so = 'a', .lo = "and", .clear = GOLB_OR, .set = 0 },
+ { .so = 'o', .lo = "or", .clear = 0, .set = GOLB_OR },
+ } ;
+ static gol_arg const rgola[GOLA_N] =
+ {
+ { .so = 't', .lo = "timeout", .i = GOLA_TIMEOUT },
+ } ;
+ char const *wgola[GOLA_N] = { 0 } ;
+ uint64_t wgolb = 0 ;
+ iopause_fd x[2] = { { .events = IOPAUSE_READ }, { .events = IOPAUSE_READ } } ;
+ tain deadline ;
+ tain tto = TAIN_INFINITE_RELATIVE ;
+ ftrigr a = FTRIGR_ZERO ;
int argc1 ;
- unsigned int i = 0 ;
- char or = 0 ;
+ unsigned int golc ;
+
PROG = "s6-ftrig-listen" ;
+ golc = GOL_main(argc, argv, rgolb, rgola, &wgolb, wgola) ;
+ argc -= golc ; argv += golc ;
+ if (argc < 4) dieusage() ;
+ if (wgola[GOLA_TIMEOUT])
{
unsigned int t = 0 ;
- for (;;)
- {
- int opt = lgetopt(argc, argv, "aot:") ;
- if (opt == -1) break ;
- switch (opt)
- {
- case 'a' : or = 0 ; break ;
- case 'o' : or = 1 ; break ;
- case 't' : if (uint0_scan(subgetopt_here.arg, &t)) break ;
- default : dieusage() ;
- }
- }
- if (t) tain_from_millisecs(&tto, t) ; else tto = tain_infinite_relative ;
- argc -= subgetopt_here.ind ; argv += subgetopt_here.ind ;
+ if (!uint0_scan(wgola[GOLA_TIMEOUT], &t))
+ strerr_dief1x(100, "timeout must be an unsigned integer") ;
+ if (t) tain_from_millisecs(&tto, t) ;
}
- if (argc < 4) dieusage() ;
+
argc1 = s6_el_semicolon(argv) ;
if (!argc1 || (argc1 & 1) || (argc == argc1 + 1)) dieusage() ;
if (argc1 >= argc) strerr_dief1x(100, "unterminated fifodir+regex block") ;
- tain_now_set_stopwatch_g() ;
+ if (!tain_now_set_stopwatch_g()) strerr_diefu1sys(111, "tain_now") ;
tain_add_g(&deadline, &tto) ;
x[0].fd = selfpipe_init() ;
if (x[0].fd < 0) strerr_diefu1sys(111, "selfpipe_init") ;
@@ -74,33 +92,31 @@ int main (int argc, char const **argv, char const *const *envp)
{
pid_t pid = 0 ;
- unsigned int idlen = argc1 >> 1 ;
- uint16_t ids[idlen] ;
- for (; i < idlen ; i++)
- {
- ids[i] = ftrigr_subscribe_g(&a, argv[i<<1], argv[(i<<1)+1], 0, &deadline) ;
- if (!ids[i]) strerr_diefu4sys(111, "subscribe to ", argv[i<<1], " with regexp ", argv[(i<<1)+1]) ;
- }
+ unsigned int n = argc1 >> 1 ;
+ uint32_t ids[n] ;
+ for (unsigned int i = 0 ; i < n ; i++)
+ if (!ftrigr_subscribe_g(&a, ids + i, 0, 0, argv[i<<1], argv[(i<<1)+1], &deadline))
+ strerr_diefu4sys(111, "subscribe to ", argv[i<<1], " with regexp ", argv[(i<<1)+1]) ;
- pid = cspawn(argv[argc1 + 1], argv + argc1 + 1, envp, CSPAWN_FLAGS_SELFPIPE_FINISH, 0, 0) ;
+ pid = cspawn(argv[argc1 + 1], argv + argc1 + 1, (char const *const *)environ, CSPAWN_FLAGS_SELFPIPE_FINISH, 0, 0) ;
if (!pid) strerr_diefu2sys(111, "spawn ", argv[argc1 + 1]) ;
for (;;)
{
int r ;
- i = 0 ;
- while (i < idlen)
+ unsigned int i = 0 ;
+ while (i < n)
{
- char dummy ;
- r = ftrigr_check(&a, ids[i], &dummy) ;
- if (r < 0) strerr_diefu1sys(111, "ftrigr_check") ;
+ struct iovec v ;
+ r = ftrigr_peek(&a, ids[i], &v) ;
+ if (r == -1) strerr_diefu1sys(111, "ftrigr_check") ;
else if (!r) i++ ;
- else if (or) idlen = 0 ;
- else ids[i] = ids[--idlen] ;
+ else if (wgolb & GOLB_OR) n = 0 ;
+ else ids[i] = ids[--n] ;
}
- if (!idlen) break ;
+ if (!n) break ;
r = iopause_g(x, 2, &deadline) ;
- if (r < 0) strerr_diefu1sys(111, "iopause") ;
+ if (r == -1) strerr_diefu1sys(111, "iopause") ;
else if (!r)
{
errno = ETIMEDOUT ;
@@ -108,10 +124,8 @@ int main (int argc, char const **argv, char const *const *envp)
}
if (x[0].revents & IOPAUSE_READ) handle_signals() ;
if (x[1].revents & IOPAUSE_READ)
- {
if (ftrigr_update(&a) < 0) strerr_diefu1sys(111, "ftrigr_update") ;
- }
}
}
- return 0 ;
+ _exit(0) ;
}
diff --git a/src/pipe-tools/s6-ftrig-listen1.c b/src/pipe-tools/s6-ftrig-listen1.c
index 4c42820..1a08e6d 100644
--- a/src/pipe-tools/s6-ftrig-listen1.c
+++ b/src/pipe-tools/s6-ftrig-listen1.c
@@ -5,9 +5,10 @@
#include <signal.h>
#include <unistd.h>
-#include <skalibs/sgetopt.h>
+#include <skalibs/posixplz.h>
#include <skalibs/types.h>
#include <skalibs/allreadwrite.h>
+#include <skalibs/gol.h>
#include <skalibs/strerr.h>
#include <skalibs/tai.h>
#include <skalibs/iopause.h>
@@ -20,6 +21,12 @@
#define USAGE "s6-ftrig-listen1 [ -t timeout ] fifodir regexp prog..."
+enum gola_e
+{
+ GOLA_TIMEOUT,
+ GOLA_N
+} ;
+
static void handle_signals (void)
{
for (;;) switch (selfpipe_read())
@@ -31,55 +38,57 @@ static void handle_signals (void)
}
}
-int main (int argc, char const *const *argv, char const *const *envp)
+int main (int argc, char const *const *argv)
{
- iopause_fd x[2] = { { -1, IOPAUSE_READ, 0 }, { -1, IOPAUSE_READ, 0 } } ;
- tain deadline, tto ;
- ftrigr_t a = FTRIGR_ZERO ;
+ static gol_arg const rgola[GOLA_N] =
+ {
+ { .so = 't', .lo = "timeout", .i = GOLA_TIMEOUT },
+ } ;
+ char const *wgola[GOLA_N] = { 0 } ;
+ iopause_fd x[2] = { { .events = IOPAUSE_READ }, { .events = IOPAUSE_READ } } ;
+ tain deadline ;
+ tain tto = TAIN_INFINITE_RELATIVE ;
+ ftrigr a = FTRIGR_ZERO ;
pid_t pid ;
- uint16_t id ;
- char pack[2] = " \n" ;
+ uint32_t id ;
+ struct iovec v[2] = { [1] = { .iov_base = "\n", .iov_len = 1 } } ;
+ unsigned int golc ;
+
PROG = "s6-ftrig-listen1" ;
+ golc = gol_main(argc, argv, 0, 0, rgola, GOLA_N, 0, wgola) ;
+ argc -= golc ; argv += golc ;
+ if (argc < 3) strerr_dieusage(100, USAGE) ;
+ if (wgola[GOLA_TIMEOUT])
{
unsigned int t = 0 ;
- for (;;)
- {
- int opt = lgetopt(argc, argv, "t:") ;
- if (opt == -1) break ;
- switch (opt)
- {
- case 't' : if (uint0_scan(subgetopt_here.arg, &t)) break ;
- default : strerr_dieusage(100, USAGE) ;
- }
- }
+ if (!uint0_scan(wgola[GOLA_TIMEOUT], &t))
+ strerr_dief1x(100, "timeout must be an unsigned integer") ;
if (t) tain_from_millisecs(&tto, t) ;
- else tto = tain_infinite_relative ;
- argc -= subgetopt_here.ind ; argv += subgetopt_here.ind ;
}
- if (argc < 3) strerr_dieusage(100, USAGE) ;
- tain_now_set_stopwatch_g() ;
+ if (!tain_now_set_stopwatch_g()) strerr_diefu1sys(111, "tain_now") ;
tain_add_g(&deadline, &tto) ;
+
if (!sig_altignore(SIGPIPE)) strerr_diefu1sys(111, "sig_ignore") ;
if (!ftrigr_startf_g(&a, &deadline)) strerr_diefu1sys(111, "ftrigr_startf") ;
- id = ftrigr_subscribe_g(&a, argv[0], argv[1], 0, &deadline) ;
- if (!id) strerr_diefu4sys(111, "subscribe to ", argv[0], " with regexp ", argv[1]) ;
+ if (!ftrigr_subscribe_g(&a, &id, 0, 0, argv[0], argv[1], &deadline))
+ strerr_diefu4sys(111, "subscribe to ", argv[0], " with regexp ", argv[1]) ;
x[0].fd = selfpipe_init() ;
- if (x[0].fd < 0) strerr_diefu1sys(111, "selfpipe_init") ;
+ if (x[0].fd == -1) strerr_diefu1sys(111, "selfpipe_init") ;
if (!selfpipe_trap(SIGCHLD)) strerr_diefu1sys(111, "selfpipe_trap") ;
x[1].fd = ftrigr_fd(&a) ;
- pid = cspawn(argv[2], argv+2, envp, CSPAWN_FLAGS_SELFPIPE_FINISH, 0, 0) ;
+ pid = cspawn(argv[2], argv+2, (char const *const *)environ, CSPAWN_FLAGS_SELFPIPE_FINISH, 0, 0) ;
if (!pid) strerr_diefu2sys(111, "spawn ", argv[2]) ;
for (;;)
{
- int r = ftrigr_check(&a, id, &pack[0]) ;
- if (r < 0) strerr_diefu1sys(111, "ftrigr_check") ;
+ int r = ftrigr_peek(&a, id, &v[0]) ;
+ if (r == -1) strerr_diefu1sys(111, "ftrigr_peek") ;
if (r) break ;
r = iopause_g(x, 2, &deadline) ;
- if (r < 0) strerr_diefu1sys(111, "iopause") ;
+ if (r == -1) strerr_diefu1sys(111, "iopause") ;
else if (!r)
{
errno = ETIMEDOUT ;
@@ -87,11 +96,9 @@ int main (int argc, char const *const *argv, char const *const *envp)
}
if (x[0].revents & IOPAUSE_READ) handle_signals() ;
if (x[1].revents & IOPAUSE_READ)
- {
- if (ftrigr_update(&a) < 0) strerr_diefu1sys(111, "ftrigr_update") ;
- }
+ if (ftrigr_update(&a) == -1) strerr_diefu1sys(111, "ftrigr_update") ;
}
- if (allwrite(1, pack, 2) < 2) strerr_diefu1sys(111, "write to stdout") ;
- return 0 ;
+ if (allwritev(1, v, 2) < 2) strerr_diefu1sys(111, "write to stdout") ;
+ _exit(0) ;
}
diff --git a/src/pipe-tools/s6-ftrig-wait.c b/src/pipe-tools/s6-ftrig-wait.c
index d0e3a1a..87b573d 100644
--- a/src/pipe-tools/s6-ftrig-wait.c
+++ b/src/pipe-tools/s6-ftrig-wait.c
@@ -1,49 +1,61 @@
/* ISC license. */
+#include <sys/uio.h>
#include <stdint.h>
+#include <unistd.h>
#include <errno.h>
+
#include <skalibs/allreadwrite.h>
-#include <skalibs/sgetopt.h>
+#include <skalibs/gol.h>
#include <skalibs/types.h>
#include <skalibs/strerr.h>
#include <skalibs/tai.h>
+#include <skalibs/siovec.h>
+
#include <s6/ftrigr.h>
#define USAGE "s6-ftrig-wait [ -t timeout ] fifodir regexp"
+enum gola_e
+{
+ GOLA_TIMEOUT,
+ GOLA_N
+} ;
+
int main (int argc, char const *const *argv)
{
- tain deadline, tto ;
- ftrigr_t a = FTRIGR_ZERO ;
- uint16_t id ;
- char pack[2] = " \n" ;
+ static gol_arg const rgola[GOLA_N] =
+ {
+ { .so = 't', .lo = "timeout", .i = GOLA_TIMEOUT },
+ } ;
+ char const *wgola[GOLA_N] = { 0 } ;
+ tain deadline ;
+ tain tto = TAIN_INFINITE_RELATIVE ;
+ ftrigr a = FTRIGR_ZERO ;
+ uint32_t id ;
+ unsigned int golc ;
+ struct iovec v[2] = { [1] = { .iov_base = "\n", .iov_len = 1 } } ;
+
PROG = "s6-ftrig-wait" ;
+ golc = gol_main(argc, argv, 0, 0, rgola, GOLA_N, 0, wgola) ;
+ argc -= golc ; argv += golc ;
+ if (argc < 2) strerr_dieusage(100, USAGE) ;
+ if (wgola[GOLA_TIMEOUT])
{
unsigned int t = 0 ;
- for (;;)
- {
- int opt = lgetopt(argc, argv, "t:") ;
- if (opt == -1) break ;
- switch (opt)
- {
- case 't' : if (uint0_scan(subgetopt_here.arg, &t)) break ;
- default : strerr_dieusage(100, USAGE) ;
- }
- }
+ if (!uint0_scan(wgola[GOLA_TIMEOUT], &t))
+ strerr_dief1x(100, "timeout must be an unsigned integer") ;
if (t) tain_from_millisecs(&tto, t) ;
- else tto = tain_infinite_relative ;
- argc -= subgetopt_here.ind ; argv += subgetopt_here.ind ;
}
- if (argc < 2) strerr_dieusage(100, USAGE) ;
- tain_now_set_stopwatch_g() ;
+ if (!tain_now_set_stopwatch_g()) strerr_diefu1sys(111, "tain_now") ;
tain_add_g(&deadline, &tto) ;
if (!ftrigr_startf_g(&a, &deadline)) strerr_diefu1sys(111, "ftrigr_startf") ;
- id = ftrigr_subscribe_g(&a, argv[0], argv[1], 0, &deadline) ;
- if (!id) strerr_diefu4sys(111, "subscribe to ", argv[0], " with regexp ", argv[1]) ;
- if (ftrigr_wait_or_g(&a, &id, 1, &deadline, &pack[0]) == -1)
- strerr_diefu2sys((errno == ETIMEDOUT) ? 1 : 111, "match regexp on ", argv[1]) ;
- if (allwrite(1, pack, 2) < 2) strerr_diefu1sys(111, "write to stdout") ;
- return 0 ;
+ if (!ftrigr_subscribe_g(&a, &id, 0, 0, argv[0], argv[1], &deadline))
+ strerr_diefu4sys(111, "subscribe to ", argv[0], " with regexp ", argv[1]) ;
+ if (ftrigr_wait_or_g(&a, &id, 1, &v[0], &deadline) == -1)
+ strerr_diefu2sys((errno == ETIMEDOUT) ? 99 : 111, "match regexp on ", argv[1]) ;
+ if (allwritev(1, v, 2) < siovec_len(v, 2)) strerr_diefu1sys(111, "write to stdout") ;
+ _exit(0) ;
}