int main (int argc, char const *const *argv) { genalloc lines = GENALLOC_ZERO ; /* array of stralloc */ char sep = '\n' ; int flagcheck = 0 ; PROG = "s6-sort" ; { subgetopt_t l = SUBGETOPT_ZERO ; for (;;) { register int opt = subgetopt_r(argc, argv, "bcfru0", &l) ; if (opt == -1) break ; switch (opt) { case 'b' : flagnoblanks = 1 ; break ; case 'c' : flagcheck = 1 ; break ; case 'f' : comp = &strncasecmp ; break ; case 'r' : flagreverse = 1 ; break ; case 'u' : flaguniq = 1 ; break ; case '0' : sep = '\0' ; break ; default : strerr_dieusage(100, USAGE) ; } } argc -= l.ind ; argv += l.ind ; } if (slurplines(&lines, sep) < 0) strerr_diefu1sys(111, "read from stdin") ; if (flagcheck) return !check(genalloc_s(stralloc, &lines), genalloc_len(stralloc, &lines)) ; qsort(genalloc_s(stralloc, &lines), genalloc_len(stralloc, &lines), sizeof(stralloc), (qsortcmp_t_ref)&sacmp) ; if (flaguniq) uniq(&lines) ; if (!outputlines(genalloc_s(stralloc, &lines), genalloc_len(stralloc, &lines))) strerr_diefu1sys(111, "write to stdout") ; return 0 ; }
static void remove (unsigned int i) { register unsigned int n = genalloc_len(ftrigio_t, &a) - 1 ; ftrigio_deepfree(genalloc_s(ftrigio_t, &a) + i) ; genalloc_s(ftrigio_t, &a)[i] = genalloc_s(ftrigio_t, &a)[n] ; genalloc_setlen(ftrigio_t, &a, n) ; }
static void add_leapsecs (uint64 *t) { uint64 *tab = genalloc_s(uint64, &table) ; unsigned int n = genalloc_len(uint64, &table) ; unsigned int i = 0 ; for (; i < n ; i++) if (*t >= tab[i]) (*t)++ ; }
static void uniq (genalloc *lines) { unsigned int len = genalloc_len(stralloc, lines) ; register stralloc *s = genalloc_s(stralloc, lines) ; register unsigned int i = 1 ; for (; i < len ; i++) if (!sacmp(s+i-1, s+i)) stralloc_free(s+i-1) ; }
static void diuintalloc_normalize (genalloc *list) { unsigned int i = 1, cur = 0 ; unsigned int len = genalloc_len(diuint, list) ; register diuint_ref const s = genalloc_s(diuint, list) ; qsort(s, len, sizeof(diuint), &diuint_cmpleft) ; for (; i < len ; i++) if (!s[cur].right) break ; else if (s[i].left > s[cur].right) s[++cur] = s[i] ; else if (s[cur].right < s[i].right) s[cur].right = s[i].right ; genalloc_setlen(diuint, list, cur+1) ; }
int unixmessage_unput_and_maybe_drop (unixmessage_sender_t *b, int drop) { diuint *start ; unsigned int n = genalloc_len(diuint, &b->offsets) ; if (!n) return 0 ; start = genalloc_s(diuint, &b->offsets) + n - 1 ; if (drop) { register int *fds = genalloc_s(int, &b->fds) + start->right ; register unsigned int i = genalloc_len(int, &b->fds) - start->right ; while (i--) fd_close(fds[i] < 0 ? -(fds[i]+1) : fds[i]) ; } b->data.len = start->left ; genalloc_setlen(int, &b->fds, start->right) ; genalloc_setlen(diuint, &b->offsets, n-1) ; return 1 ; }
int main (void) { stralloc indata = STRALLOC_ZERO ; unsigned int instate = 0 ; PROG = "s6-ftrigrd" ; if (ndelay_on(0) < 0) strerr_diefu2sys(111, "ndelay_on ", "0") ; if (ndelay_on(1) < 0) strerr_diefu2sys(111, "ndelay_on ", "1") ; if (sig_ignore(SIGPIPE) < 0) strerr_diefu1sys(111, "ignore SIGPIPE") ; { struct taia deadline, stamp ; taia_now(&stamp) ; taia_addsec(&deadline, &stamp, 2) ; if (!skaserver2_sync(&asyncout, FTRIGR_BANNER1, FTRIGR_BANNER1_LEN, FTRIGR_BANNER2, FTRIGR_BANNER2_LEN, &deadline, &stamp)) strerr_diefu1sys(111, "sync with client") ; } for (;;) { register unsigned int n = genalloc_len(ftrigio_t, &a) ; iopause_fd x[3 + n] ; unsigned int i = 0 ; int r ; x[0].fd = 0 ; x[0].events = IOPAUSE_EXCEPT | IOPAUSE_READ ; x[1].fd = 1 ; x[1].events = IOPAUSE_EXCEPT | (bufalloc_len(bufalloc_1) ? IOPAUSE_WRITE : 0) ; x[2].fd = bufalloc_fd(&asyncout) ; x[2].events = IOPAUSE_EXCEPT | (bufalloc_len(&asyncout) ? IOPAUSE_WRITE : 0) ; for (; i < n ; i++) { register ftrigio_t_ref p = genalloc_s(ftrigio_t, &a) + i ; p->xindex = 3 + i ; x[3+i].fd = p->trig.fd ; x[3+i].events = IOPAUSE_READ ; } r = iopause(x, 3 + n, 0, 0) ; if (r < 0) { cleanup() ; strerr_diefu1sys(111, "iopause") ; } /* client closed => exit */ if ((x[0].revents | x[1].revents) & IOPAUSE_EXCEPT) break ; /* client reading => flush pending data */ if (x[1].revents & IOPAUSE_WRITE) if ((bufalloc_flush(bufalloc_1) == -1) && !error_isagain(errno)) { cleanup() ; strerr_diefu1sys(111, "flush stdout") ; } if (x[2].revents & IOPAUSE_WRITE) if ((bufalloc_flush(&asyncout) == -1) && !error_isagain(errno)) { cleanup() ; strerr_diefu1sys(111, "flush asyncout") ; } /* scan listening ftrigs */ for (i = 0 ; i < genalloc_len(ftrigio_t, &a) ; i++) { register ftrigio_t_ref p = genalloc_s(ftrigio_t, &a) + i ; if (x[p->xindex].revents & IOPAUSE_READ) { char c ; register int r = sanitize_read(fd_read(p->trig.fd, &c, 1)) ; if (!r) continue ; if (r < 0) { trig(p->id, 'd', errno) ; remove(i--) ; } else if (!sredfa_feed(p->re, &p->dfastate, c)) { trig(p->id, 'd', ENOEXEC) ; remove(i--) ; } else if (p->dfastate & SREDFA_ACCEPT) { trig(p->id, '!', c) ; if (p->options & FTRIGR_REPEAT) p->dfastate = SREDFA_START ; else remove(i--) ; } } } /* client writing => get data and parse it */ if (buffer_len(buffer_0small) || x[0].revents & IOPAUSE_READ) { int r ; for (;;) { uint16 id ; r = sanitize_read(netstring_get(buffer_0small, &indata, &instate)) ; if (r <= 0) break ; if (indata.len < 3) { cleanup() ; strerr_dief1x(100, "invalid client request") ; } uint16_unpack_big(indata.s, &id) ; switch (indata.s[2]) /* protocol parsing */ { case 'U' : /* unsubscribe */ { register unsigned int i = genalloc_len(ftrigio_t, &a) ; for (; i ; i--) if (genalloc_s(ftrigio_t, &a)[i-1].id == id) break ; if (i) remove(i-1) ; answer(0) ; break ; } case 'L' : /* subscribe to path and match re */ { ftrigio_t f = FTRIGIO_ZERO ; uint32 pathlen, relen ; if (indata.len < 18) { answer(EPROTO) ; break ; } uint32_unpack_big(indata.s + 3, &f.options) ; uint32_unpack_big(indata.s + 7, &pathlen) ; uint32_unpack_big(indata.s + 11, &relen) ; if (((pathlen + relen + 16) != indata.len) || indata.s[15 + pathlen]) { answer(EPROTO) ; break ; } f.id = id ; if (!stralloc_0(&indata)) { answer(errno) ; break ; } f.re = sredfa_new() ; if (!f.re) { answer(errno) ; break ; } if (!sredfa_from_regexp(f.re, indata.s + 16 + pathlen) || !ftrig1_make(&f.trig, indata.s + 15)) { sredfa_delete(f.re) ; answer(errno) ; break ; } if (!genalloc_append(ftrigio_t, &a, &f)) { ftrigio_deepfree(&f) ; answer(errno) ; break ; } answer(0) ; break ; } default : { cleanup() ; strerr_dief1x(100, "invalid client request") ; } } indata.len = 0 ; } /* end loop: parse input from client */ if (r < 0) { if (errno == EPIPE) break ; /* client closed */ else { cleanup() ; strerr_diefu1sys(111, "read a netstring") ; } } } /* end if: stuff to read on stdin */ } /* end loop: main iopause */ cleanup() ; return 0 ; }
static void cleanup (void) { register unsigned int i = genalloc_len(ftrigio_t, &a) ; for (; i ; i--) ftrigio_deepfree(genalloc_s(ftrigio_t, &a) + i - 1) ; genalloc_setlen(ftrigio_t, &a, 0) ; }
int main (int argc, char const *const *argv) { genalloc list = GENALLOC_ZERO ; /* array of diuint */ char delim = '\t' ; unsigned long what = 0 ; PROG = "s6-cut" ; { subgetopt_t l = SUBGETOPT_ZERO ; int flagnodel = 1 ; for (;;) { register int opt = subgetopt_r(argc, argv, "nsb:c:f:d:", &l) ; if (opt == -1) break ; switch (opt) { case 'n': break ; /* ignored */ case 's': flagnodel = 0 ; break ; case 'd': delim = *l.arg ; break ; case 'b': case 'c': { if (what) strerr_dieusage(100, USAGE) ; what = 2 ; scanlist(&list, l.arg) ; break ; } case 'f': { if (what) strerr_dieusage(100, USAGE) ; what = 4 ; scanlist(&list, l.arg) ; break ; } default : strerr_dieusage(100, USAGE) ; } } what += flagnodel ; argc -= l.ind ; argv += l.ind ; } if (!genalloc_len(diuint, &list)) strerr_dieusage(100, USAGE) ; diuintalloc_normalize(&list) ; if (!argc) { if (!doit(0, genalloc_s(diuint, &list), genalloc_len(diuint, &list), what, delim)) strerr_diefu1sys(111, "cut stdin") ; } else { for (; *argv ; argv++) { if ((*argv[0] == '-') && !*argv[1]) { if (!doit(0, genalloc_s(diuint, &list), genalloc_len(diuint, &list), what, delim)) strerr_diefu1sys(111, "process stdin") ; } else { int fd = open_readb(*argv) ; if (fd == -1) strerr_diefu3sys(111, "open ", *argv, " for reading") ; if (!doit(fd, genalloc_s(diuint, &list), genalloc_len(diuint, &list), what, delim)) strerr_diefu2sys(111, "cut ", *argv) ; fd_close(fd) ; } } } return 0 ; }