Да, мультитейл кажется связанным с понятием «окна» как подмножества терминала; Я не мог заставить его играть как компонент конвейера.
Так выглядит как мы Хафт сделать этому себе трещины суставы
/* Copyright © 2015 sqweek@gmail.com
** Use/modify as you see fit but leave this attribution.
** If you change the interface and want to distribute the
** result please change the binary name too! */
#include <err.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/select.h>
/* typedefs are for pussies */
struct {
char *filename; /* for clarity of errors */
char *data;
long len;
long cap;
} saved[FD_SETSIZE] = {0};
void
ewriten(int fd, char *buf, int n)
{
int done = 0, c;
while (done < n) {
if ((c=write(fd, buf + done, n - done)) <= 0 && errno != EINTR) {
err(1, "write");
}
done += c;
}
}
int
empty(fd_set *fdset, int maxfd)
{
int i;
for (i=0; i <= maxfd; i++) {
if (FD_ISSET(i, fdset)) return 0;
}
return 1;
}
void
combine(fd_set *fdset, int maxfd)
{
char buf[4096], *cp;
fd_set ready;
int n, i, fd, left;
while (!empty(fdset, maxfd)) {
ready = *fdset;
/* timeouts are for pussies */
if (select(maxfd + 1, &ready, NULL, NULL, NULL) == -1) err(1, "select");
for (fd=0; fd <= maxfd; fd++) {
if (!FD_ISSET(fd, &ready)) continue;
switch (n=read(fd, &buf, sizeof(buf))) {
case -1:
if (errno == EINTR)
break; /* ignore interrupts; we'll re-read next iteration */
if (saved[fd].filename) err(1, "read: %s", saved[fd].filename);
err(1, "read: %d", fd);
case 0:
if (saved[fd].len > 0) {
/* someone forgot their newline at EOF... */
ewriten(1, saved[fd].data, saved[fd].len);
saved[fd].data[0] = '\n'; /* put it back for them */
ewriten(1, saved[fd].data, 1);
}
free(saved[fd].data);
FD_CLR(fd, fdset);
break;
default:
for (cp=buf + n - 1; cp >= buf && *cp != '\n'; cp--); /* find last newline */
left = n - (cp - buf + 1);
if (cp >= buf) {
/* we found one! first dump any saved data from the last read */
if (saved[fd].len > 0) {
ewriten(1, saved[fd].data, saved[fd].len);
saved[fd].len = 0;
}
ewriten(1, buf, cp - buf + 1);
}
if (left > 0) {
/* now save any leftover data for later */
int need = saved[fd].len + left;
if (saved[fd].cap < need &&
(saved[fd].data=realloc(saved[fd].data, need)) == NULL) {
errx(1, "realloc: failed on %d bytes", need);
/* it was good enough for quake... */
}
saved[fd].cap = need;
memcpy(saved[fd].data + saved[fd].len, buf + n - 1 - left, left);
saved[fd].len += left;
}
}
}
}
}
void
addfd(int fd, fd_set *fdset, int *maxfd)
{
FD_SET(fd, fdset);
if (*maxfd < fd) {
*maxfd = fd;
}
}
int
main(int argc, char **argv)
{
fd_set fdset;
char **arg = argv + 1;
char *cp;
struct stat st;
int fd, maxfd = -1;
FD_ZERO(&fdset);
while (*arg != NULL) {
/* getopt is for pussies */
if (strncmp("-u", *arg, 2) == 0) {
*arg += 2;
if (**arg == '\0' && *++arg == NULL ) errx(1, "-u requires argument (comma separated FD list)");
/* reentrancy is for pussies */
for (cp=strtok(*arg, ","); cp != NULL; cp=strtok(NULL, ",")) {
fd = atoi(cp);
if (fstat(fd, &st) != 0) err(1, "%d", fd);
addfd(fd, &fdset, &maxfd);
}
arg++;
} else if (strcmp("-", *arg) == 0) {
if (fstat(0, &st) != 0) err(1, "stdin", fd);
addfd(0, &fdset, &maxfd);
saved[0].filename = "stdin";
arg++;
} else if (strcmp("--", *arg) == 0) {
arg++;
break;
} else if (**arg == '-') {
errx(1, "unrecognized argument %s", *arg);
} else {
break; /* treat as filename */
}
}
/* remaining args are filenames */
for (; *arg != NULL; arg++) {
/* stdio is for pussies */
if ((fd=open(*arg, O_RDONLY)) == -1) err(1, "open: %s", *arg);
addfd(fd, &fdset, &maxfd);
saved[fd].filename = *arg;
}
combine(&fdset, maxfd);
return 0;
}
Ах, это было хорошо.
(примечание: тестируется на двух наборах входных данных. ошибок может или не может быть)