cosmopolitan/tool/viz/tailf.c
Justine Tunney 9e3e985ae5 Make terminal ui binaries work well everywhere
Here's some screenshots of an emulator tui program that was compiled on
Linux, then scp'd it to Windows, Mac, and FreeBSD.

https://justine.storage.googleapis.com/blinkenlights-cmdexe.png
https://justine.storage.googleapis.com/blinkenlights-imac.png
https://justine.storage.googleapis.com/blinkenlights-freebsd.png
https://justine.storage.googleapis.com/blinkenlights-lisp.png

How is this even possible that we have a nontrivial ui binary that just
works on Mac, Windows, Linux, and BSD? Surely a first ever achievement.

Fixed many bugs. Bootstrapped John McCarthy's metacircular evaluator on
bare metal in half the size of Altair BASIC (about 2.5kb) and ran it in
emulator for fun and profit.
2020-10-19 06:38:31 -07:00

102 lines
3.7 KiB
C

/*-*- 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 2020 Justine Alexandra Roberts Tunney │
│ │
│ This program is free software; you can redistribute it and/or modify │
│ it under the terms of the GNU General Public License as published by │
│ the Free Software Foundation; version 2 of the License. │
│ │
│ This program is distributed in the hope that it will be useful, but │
│ WITHOUT ANY WARRANTY; without even the implied warranty of │
│ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU │
│ General Public License for more details. │
│ │
│ You should have received a copy of the GNU General Public License │
│ along with this program; if not, write to the Free Software │
│ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA │
│ 02110-1301 USA │
╚─────────────────────────────────────────────────────────────────────────────*/
#include "libc/bits/safemacros.h"
#include "libc/calls/calls.h"
#include "libc/calls/struct/stat.h"
#include "libc/macros.h"
#include "libc/runtime/runtime.h"
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/o.h"
#include "libc/sysv/consts/sig.h"
#include "libc/time/time.h"
#include "libc/x/x.h"
/**
* @fileoverview tail -f with lower poll rate
* @see busybox not having interval flag
*/
int fd;
bool exited;
struct stat st;
char buf[FRAMESIZE];
int WriteString(const char *s) {
return write(1, s, strlen(s));
}
void HideCursor(void) {
WriteString("\e[?25l");
}
void ShowCursor(void) {
WriteString("\e[?25h");
}
void OnInt(void) {
exited = true;
}
void OnExit(void) {
ShowCursor();
}
int main(int argc, char *argv[]) {
char *p;
ssize_t n;
size_t i, j;
bool chopped;
if (argc < 2) return 1;
if ((fd = open(argv[1], O_RDONLY)) == -1) return 2;
if (fstat(fd, &st) == -1) return 3;
n = st.st_size - MIN(st.st_size, sizeof(buf));
if ((n = pread(fd, buf, sizeof(buf), n)) == -1) return 4;
for (p = buf + n, i = 0; i < 10; ++i) {
p = firstnonnull(memrchr(buf, '\n', p - buf), buf);
}
chopped = false;
if (buf + n - p) ++p;
i = st.st_size - (buf + n - p);
atexit(OnExit);
HideCursor();
xsigaction(SIGINT, OnInt, 0, 0, 0);
xsigaction(SIGTERM, OnInt, 0, 0, 0);
while (!exited) {
if (fstat(fd, &st) == -1) return 5;
if (i > st.st_size) i = 0;
for (; i < st.st_size; i += n) {
if ((n = pread(fd, buf, sizeof(buf), i)) == -1) return 6;
j = n;
while (j && (buf[j - 1] == '\n' || buf[j - 1] == '\r')) --j;
if (j) {
if (chopped) {
WriteString("\r\n");
}
write(1, buf, j);
}
chopped = j != n;
}
dsleep(.01);
}
close(fd);
WriteString("\r\n");
return 0;
}