ucspilogd.c (2042B)
1 /* ISC license. */ 2 3 #include <sys/types.h> 4 #include <errno.h> 5 #include <stdlib.h> 6 7 #include <skalibs/sgetopt.h> 8 #include <skalibs/bytestr.h> 9 #include <skalibs/buffer.h> 10 #include <skalibs/strerr.h> 11 #include <skalibs/stralloc.h> 12 #include <skalibs/skamisc.h> 13 14 #include "lolsyslog.h" 15 16 #define USAGE "ucspilogd [ -D default ] [ var... ]" 17 #define dieusage() strerr_dieusage(100, USAGE) 18 19 static inline void die (void) 20 { 21 strerr_diefu1sys(111, "write to stdout") ; 22 } 23 24 int main (int argc, char const *const *argv) 25 { 26 char const *d = "<undefined>" ; 27 PROG = "ucspilogd" ; 28 { 29 subgetopt l = SUBGETOPT_ZERO ; 30 for (;;) 31 { 32 int opt = subgetopt_r(argc, argv, "D:", &l) ; 33 if (opt == -1) break ; 34 switch (opt) 35 { 36 case 'D' : d = l.arg ; break ; 37 default : dieusage() ; 38 } 39 } 40 argc -= l.ind ; argv += l.ind ; 41 } 42 43 { 44 unsigned int i = 0 ; 45 stralloc sa = STRALLOC_ZERO ; 46 char buf[LOLSYSLOG_STRING] ; 47 char const *envs[argc] ; 48 for (; i < (unsigned int)argc ; i++) 49 { 50 envs[i] = getenv(argv[i]) ; 51 if (!envs[i]) envs[i] = d ; 52 } 53 for (;;) 54 { 55 size_t pos = 0 ; 56 size_t j ; 57 sa.len = 0 ; 58 { 59 int r = skagetlnsep(buffer_0f1, &sa, "\n", 2) ; 60 if (r < 0) 61 { 62 if (errno != EPIPE || !stralloc_0(&sa)) 63 strerr_diefu1sys(111, "read from stdin") ; 64 } 65 if (!r) break ; 66 } 67 if (!sa.len) continue ; 68 sa.s[sa.len-1] = 0 ; 69 if ((sa.s[0] == '@') && (sa.len > 26) && (byte_chr(sa.s, 26, ' ') == 25)) 70 { 71 if (buffer_put(buffer_1, sa.s, 26) < 26) die() ; 72 pos += 26 ; 73 } 74 for (i = 0 ; i < (unsigned int)argc ; i++) 75 if ((buffer_puts(buffer_1, envs[i]) < 0) 76 || (buffer_put(buffer_1, ": ", 2) < 2)) die() ; 77 j = lolsyslog_string(buf, sa.s + pos) ; pos += j ; 78 if (j && buffer_puts(buffer_1, buf) < 0) die() ; 79 sa.s[sa.len-1] = '\n' ; 80 if (buffer_put(buffer_1, sa.s + pos, sa.len - pos) < 0) die() ; 81 } 82 } 83 return 0 ; 84 }