Do some work on unbuffer command

This commit is contained in:
Justine Tunney 2022-09-04 02:25:34 -07:00
parent e1590e1e38
commit b9dc74b672
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
2 changed files with 190 additions and 143 deletions

View file

@ -1,143 +0,0 @@
#if 0
/*─────────────────────────────────────────────────────────────────╗
To the extent possible under law, Justine Tunney has waived
all copyright and related or neighboring rights to this file,
as it is written in the following disclaimers:
http://unlicense.org/ │
http://creativecommons.org/publicdomain/zero/1.0/ │
*/
#endif
#include "libc/calls/calls.h"
#include "libc/calls/struct/sigaction.h"
#include "libc/calls/struct/termios.h"
#include "libc/calls/struct/winsize.h"
#include "libc/calls/termios.h"
#include "libc/calls/ttydefaults.h"
#include "libc/log/check.h"
#include "libc/log/log.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/sig.h"
#include "libc/sysv/consts/termios.h"
#include "libc/sysv/consts/w.h"
#define STR(s) s, strlen(s)
struct termios tio;
struct winsize wsz;
sigset_t chldmask, savemask;
struct sigaction ignore, saveint, savequit;
char databuf[512];
char pathbuf[PATH_MAX];
int main(int argc, char *argv[]) {
ssize_t rc;
const char *prog;
size_t i, got, wrote;
int ws, mfd, sfd, pid;
if (argc < 2) {
write(2, STR("usage: "));
if (argc > 0) write(2, argv[0], strlen(argv[0]));
write(2, STR(" prog [argv₁...]\n"));
return 1;
}
if (!(prog = commandv(argv[1], pathbuf, sizeof(pathbuf)))) {
write(2, STR("error: "));
write(2, argv[1], strlen(argv[1]));
write(2, STR(" not found\n"));
return 2;
}
bzero(&wsz, sizeof(wsz));
wsz.ws_col = 80;
wsz.ws_row = 45;
bzero(&tio, sizeof(tio));
tio.c_iflag = ICRNL | IXON | IUTF8;
tio.c_oflag = OPOST | ONLCR;
tio.c_cflag = CREAD | CS8;
tio.c_lflag =
ISIG | ICANON | ECHO | ECHOE | ECHOK | IEXTEN | ECHOCTL | ECHOKE;
tio.c_cc[VMIN] = 1;
tio.c_cc[VINTR] = CTRL('C');
tio.c_cc[VQUIT] = CTRL('\\');
tio.c_cc[VERASE] = CTRL('?');
tio.c_cc[VKILL] = CTRL('U');
tio.c_cc[VEOF] = CTRL('D');
tio.c_cc[VSTART] = CTRL('Q');
tio.c_cc[VSTOP] = CTRL('S');
tio.c_cc[VSUSP] = CTRL('Z');
tio.c_cc[VREPRINT] = CTRL('R');
tio.c_cc[VDISCARD] = CTRL('O');
tio.c_cc[VWERASE] = CTRL('W');
tio.c_cc[VLNEXT] = CTRL('V');
if (openpty(&mfd, &sfd, 0, &tio, &wsz)) {
write(2, STR("error: openpty() failed\n"));
return 3;
}
ignore.sa_flags = 0;
ignore.sa_handler = SIG_IGN;
sigemptyset(&ignore.sa_mask);
sigaction(SIGINT, &ignore, &saveint);
sigaction(SIGQUIT, &ignore, &savequit);
sigemptyset(&chldmask);
sigaddset(&chldmask, SIGCHLD);
sigprocmask(SIG_BLOCK, &chldmask, &savemask);
if ((pid = vfork()) == -1) {
write(2, STR("error: vfork() failed\n"));
return 4;
}
if (!pid) {
close(0);
dup(sfd);
close(1);
dup(sfd);
close(2);
dup(sfd);
close(mfd);
sigaction(SIGINT, &saveint, 0);
sigaction(SIGQUIT, &savequit, 0);
sigprocmask(SIG_SETMASK, &savemask, 0);
execv(prog, argv + 1);
return 127;
}
close(sfd);
for (;;) {
if (waitpid(pid, &ws, WNOHANG) == pid) {
break;
}
rc = read(mfd, databuf, sizeof(databuf));
if (rc == -1) {
perror("read");
return 5;
}
if (!(got = rc)) {
continue;
}
for (i = 0; i < got; i += wrote) {
rc = write(1, databuf + i, got - i);
if (rc != -1) {
wrote = rc;
} else {
write(2, STR("error: read() failed\n"));
return 5;
}
}
}
sigaction(SIGINT, &saveint, 0);
sigaction(SIGQUIT, &savequit, 0);
sigprocmask(SIG_SETMASK, &savemask, 0);
if (WIFEXITED(ws)) {
return WEXITSTATUS(ws);
} else {
return 128 + WTERMSIG(ws);
}
}

190
tool/build/unbuffer.c Normal file
View file

@ -0,0 +1,190 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2022 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/calls/calls.h"
#include "libc/calls/struct/sigaction.h"
#include "libc/calls/struct/termios.h"
#include "libc/calls/struct/winsize.h"
#include "libc/calls/termios.h"
#include "libc/calls/ttydefaults.h"
#include "libc/intrin/kprintf.h"
#include "libc/log/check.h"
#include "libc/log/log.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/sig.h"
#include "libc/sysv/consts/termios.h"
#include "libc/sysv/consts/w.h"
#include "third_party/getopt/getopt.h"
#define GETOPTS "o:"
#define USAGE \
"\
Usage: unbuffer.com [FLAGS] PROG ARGS...\n\
-o PATH output file\n\
"
int outfd;
struct winsize wsz;
struct termios tio, oldterm;
sigset_t chldmask, savemask;
struct sigaction ignore, saveint, savequit;
char databuf[512];
char pathbuf[PATH_MAX];
const char *outputpath;
static void GetOpts(int argc, char *argv[]) {
int opt;
while ((opt = getopt(argc, argv, GETOPTS)) != -1) {
switch (opt) {
case 'o':
outputpath = optarg;
break;
case '?':
write(1, USAGE, sizeof(USAGE) - 1);
exit(0);
default:
write(2, USAGE, sizeof(USAGE) - 1);
exit(64);
}
}
}
int main(int argc, char *argv[]) {
ssize_t rc;
const char *prog;
size_t i, got, wrote;
int e, ws, mfd, sfd, pid;
GetOpts(argc, argv);
if (optind == argc) {
write(2, USAGE, sizeof(USAGE) - 1);
exit(64);
}
if (!(prog = commandv(argv[optind], pathbuf, sizeof(pathbuf)))) {
kprintf("%s: command not found\n", argv[optind]);
return __COUNTER__ + 1;
}
if (outputpath) {
if ((outfd = creat(outputpath, 0644)) == -1) {
perror(outputpath);
return __COUNTER__ + 1;
}
}
bzero(&wsz, sizeof(wsz));
wsz.ws_col = 80;
wsz.ws_row = 45;
if (ioctl(1, TCGETS, &tio)) {
perror("tcgets");
return __COUNTER__ + 1;
}
if (openpty(&mfd, &sfd, 0, &tio, &wsz)) {
perror("openpty");
return __COUNTER__ + 1;
}
ignore.sa_flags = 0;
ignore.sa_handler = SIG_IGN;
sigemptyset(&ignore.sa_mask);
sigaction(SIGINT, &ignore, &saveint);
sigaction(SIGQUIT, &ignore, &savequit);
sigemptyset(&chldmask);
sigaddset(&chldmask, SIGCHLD);
sigprocmask(SIG_BLOCK, &chldmask, &savemask);
if ((pid = fork()) == -1) {
perror("fork");
return __COUNTER__ + 1;
}
if (!pid) {
close(0);
dup(sfd);
close(1);
dup(sfd);
close(2);
dup(sfd);
close(mfd);
sigaction(SIGINT, &saveint, 0);
sigaction(SIGQUIT, &savequit, 0);
sigprocmask(SIG_SETMASK, &savemask, 0);
execv(prog, argv + optind);
perror("execve");
return 127;
}
close(sfd);
for (;;) {
if (waitpid(pid, &ws, WNOHANG) == pid) {
break;
}
e = errno;
rc = read(mfd, databuf, sizeof(databuf));
if (rc == -1) {
if (errno == EIO) {
errno = e;
rc = 0;
} else {
perror("read");
return __COUNTER__ + 1;
}
}
if (!(got = rc)) {
if (waitpid(pid, &ws, 0) == -1) {
perror("waitpid");
return __COUNTER__ + 1;
}
break;
}
for (i = 0; i < got; i += wrote) {
rc = write(1, databuf + i, got - i);
if (rc != -1) {
wrote = rc;
} else {
perror("write");
return __COUNTER__ + 1;
}
}
if (outputpath) {
for (i = 0; i < got; i += wrote) {
rc = write(outfd, databuf + i, got - i);
if (rc != -1) {
wrote = rc;
} else {
perror("write");
return __COUNTER__ + 1;
}
}
}
}
sigaction(SIGINT, &saveint, 0);
sigaction(SIGQUIT, &savequit, 0);
sigprocmask(SIG_SETMASK, &savemask, 0);
if (WIFEXITED(ws)) {
return WEXITSTATUS(ws);
} else {
return 128 + WTERMSIG(ws);
}
}