s6-ftrig-wait.c (1458B)
1 /* ISC license. */ 2 3 #include <stdint.h> 4 #include <errno.h> 5 #include <skalibs/allreadwrite.h> 6 #include <skalibs/sgetopt.h> 7 #include <skalibs/types.h> 8 #include <skalibs/strerr.h> 9 #include <skalibs/tai.h> 10 #include <s6/ftrigr.h> 11 12 #define USAGE "s6-ftrig-wait [ -t timeout ] fifodir regexp" 13 14 int main (int argc, char const *const *argv) 15 { 16 tain deadline, tto ; 17 ftrigr_t a = FTRIGR_ZERO ; 18 uint16_t id ; 19 char pack[2] = " \n" ; 20 PROG = "s6-ftrig-wait" ; 21 { 22 unsigned int t = 0 ; 23 for (;;) 24 { 25 int opt = lgetopt(argc, argv, "t:") ; 26 if (opt == -1) break ; 27 switch (opt) 28 { 29 case 't' : if (uint0_scan(subgetopt_here.arg, &t)) break ; 30 default : strerr_dieusage(100, USAGE) ; 31 } 32 } 33 if (t) tain_from_millisecs(&tto, t) ; 34 else tto = tain_infinite_relative ; 35 argc -= subgetopt_here.ind ; argv += subgetopt_here.ind ; 36 } 37 if (argc < 2) strerr_dieusage(100, USAGE) ; 38 39 tain_now_set_stopwatch_g() ; 40 tain_add_g(&deadline, &tto) ; 41 42 if (!ftrigr_startf_g(&a, &deadline)) strerr_diefu1sys(111, "ftrigr_startf") ; 43 id = ftrigr_subscribe_g(&a, argv[0], argv[1], 0, &deadline) ; 44 if (!id) strerr_diefu4sys(111, "subscribe to ", argv[0], " with regexp ", argv[1]) ; 45 if (ftrigr_wait_or_g(&a, &id, 1, &deadline, &pack[0]) == -1) 46 strerr_diefu2sys((errno == ETIMEDOUT) ? 1 : 111, "match regexp on ", argv[1]) ; 47 if (allwrite(1, pack, 2) < 2) strerr_diefu1sys(111, "write to stdout") ; 48 return 0 ; 49 }