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.
This commit is contained in:
Justine Tunney 2020-10-10 21:18:53 -07:00
parent 680daf1210
commit 9e3e985ae5
276 changed files with 7026 additions and 3790 deletions

View file

@ -1,37 +0,0 @@
/*-*- 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/dce.h"
#include "libc/sysv/errfuns.h"
#include "libc/time/time.h"
/**
* Retrieves last setitimer() value, correcting for remaining time.
*
* @param which can be ITIMER_REAL, ITIMER_VIRTUAL, etc.
* @return 0 on success or -1 w/ errno
*/
int getitimer(int which, struct itimerval *curvalue) {
if (!IsWindows()) {
int getitimer$sysv(int, struct itimerval *) hidden;
return getitimer$sysv(which, curvalue);
} else {
return enosys(); /* TODO(jart): Implement me! */
}
}

View file

@ -28,7 +28,19 @@
#define TM_ZONE tm_zone
#define INITIALIZE(x) x = 0
STATIC_YOINK("zip_uri_support");
STATIC_YOINK("usr/share/zoneinfo/Beijing");
STATIC_YOINK("usr/share/zoneinfo/Berlin");
STATIC_YOINK("usr/share/zoneinfo/Boulder");
STATIC_YOINK("usr/share/zoneinfo/Chicago");
STATIC_YOINK("usr/share/zoneinfo/GST");
STATIC_YOINK("usr/share/zoneinfo/Honolulu");
STATIC_YOINK("usr/share/zoneinfo/Israel");
STATIC_YOINK("usr/share/zoneinfo/Japan");
STATIC_YOINK("usr/share/zoneinfo/London");
STATIC_YOINK("usr/share/zoneinfo/Melbourne");
STATIC_YOINK("usr/share/zoneinfo/New_York");
STATIC_YOINK("usr/share/zoneinfo/Singapore");
/* clang-format off */
/*
@ -807,15 +819,15 @@ getrule(strp, rulep)
static time_t
transtime(janfirst, year, rulep, offset)
const time_t janfirst;
const int year;
register const struct rule * const rulep;
const int32_t offset;
const time_t janfirst;
const int year;
register const struct rule * const rulep;
const int32_t offset;
{
register int leapyear;
register time_t value;
register int i;
int d, m1, yy0, yy1, yy2, dow;
register int leapyear;
register time_t value;
register int i;
int d, m1, yy0, yy1, yy2, dow;
INITIALIZE(value);
leapyear = isleap(year);
@ -857,9 +869,9 @@ const int32_t offset;
*/
m1 = (rulep->r_mon + 9) % 12 + 1;
yy0 = (rulep->r_mon <= 2) ? (year - 1) : year;
yy1 = div100int64(yy0);
yy2 = rem100int64(yy0);
dow = (div10int64(26 * m1 - 2) +
yy1 = yy0 / 100;
yy2 = yy0 % 100;
dow = ((26 * m1 - 2) / 10 +
1 + yy2 + yy2 / 4 + yy1 / 4 - 2 * yy1) % 7;
if (dow < 0)
dow += DAYSPERWEEK;

View file

@ -1,54 +0,0 @@
/*-*- 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/calls/internal.h"
#include "libc/calls/struct/itimerval.h"
#include "libc/dce.h"
#include "libc/sysv/errfuns.h"
#include "libc/time/time.h"
/**
* Schedules delivery of one-shot or intermittent wakeup signal, e.g.
*
* CHECK_NE(-1, sigaction(SIGALRM,
* &(struct sigaction){.sa_sigaction = missingno,
* .sa_flags = SA_RESETHAND},
* NULL));
* CHECK_NE(-1, setitimer(ITIMER_REAL,
* &(const struct itimerval){{0, 0}, {0, 50000}},
* NULL));
* if (connect(...) == -1 && errno == EINTR) { ... }
* CHECK_NE(-1, setitimer(ITIMER_REAL,
* &(const struct itimerval){{0, 0}, {0, 0}},
* NULL));
*
* @param which can be ITIMER_REAL, ITIMER_VIRTUAL, etc.
* @param newvalue specifies the interval ({0,0} means one-shot) and
* duration ({0,0} means disarm) in microseconds
* @param out_opt_old may receive remainder of previous op (if any)
* @return 0 on success or -1 w/ errno
*/
int setitimer(int which, const struct itimerval *newvalue,
struct itimerval *out_opt_oldvalue) {
if (!IsWindows()) {
return setitimer$sysv(which, newvalue, out_opt_oldvalue);
} else {
return enosys(); /* TODO(jart): Implement me! */
}
}

View file

@ -16,6 +16,7 @@
IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*/
#include "libc/assert.h"
#include "libc/calls/calls.h"
#include "libc/fmt/fmt.h"
#include "libc/macros.h"
@ -381,6 +382,7 @@ static char *strftime_timefmt(char *p, const char *pe, const char *format,
*/
size_t strftime(char *s, size_t size, const char *f, const struct tm *t) {
char *p;
assert(t);
p = strftime_timefmt(s, s + size, f, t);
if (p < s + size) {
*p = '\0';

View file

@ -27,9 +27,8 @@ int sleep(uint32_t);
int usleep(uint32_t);
int nanosleep(const struct timespec *, struct timespec *);
unsigned alarm(unsigned);
int getitimer(int, struct itimerval *) paramsnonnull();
int setitimer(int, const struct itimerval *, struct itimerval *)
paramsnonnull((2));
int getitimer(int, struct itimerval *);
int setitimer(int, const struct itimerval *, struct itimerval *);
void tzset(void);
struct tm *gmtime(const int64_t *);

View file

@ -3,28 +3,11 @@
PKGS += LIBC_TIME
LIBC_TIME_ZONEINFOS = \
Beijing \
Berlin \
Boulder \
Chicago \
GST \
Honolulu \
Israel \
Japan \
London \
Melbourne \
New_York \
Singapore \
Sydney \
UTC
LIBC_TIME_ARTIFACTS += LIBC_TIME_A
LIBC_TIME_ZONEINFOS = $(wildcard usr/share/zoneinfo/*)
LIBC_TIME = $(LIBC_TIME_A_DEPS) $(LIBC_TIME_A)
LIBC_TIME_A = o/$(MODE)/libc/time/time.a
LIBC_TIME_A_FILES := \
$(wildcard libc/time/struct/*) \
$(wildcard libc/time/*)
LIBC_TIME_A_FILES := $(wildcard libc/time/struct/*) $(wildcard libc/time/*)
LIBC_TIME_A_HDRS := $(filter %.h,$(LIBC_TIME_A_FILES))
LIBC_TIME_A_SRCS_S = $(filter %.S,$(LIBC_TIME_A_FILES))
LIBC_TIME_A_SRCS_C = $(filter %.c,$(LIBC_TIME_A_FILES))
@ -37,7 +20,8 @@ LIBC_TIME_A_OBJS = \
$(LIBC_TIME_A_SRCS:%=o/$(MODE)/%.zip.o) \
$(LIBC_TIME_A_SRCS_S:%.S=o/$(MODE)/%.o) \
$(LIBC_TIME_A_SRCS_C:%.c=o/$(MODE)/%.o) \
o//libc/time/zoneinfo.o
$(LIBC_TIME_A_SRCS_C:%.c=o/$(MODE)/%.o) \
$(LIBC_TIME_ZONEINFOS:%=o/$(MODE)/%.zip.o)
LIBC_TIME_A_CHECKS = \
$(LIBC_TIME_A).pkg \
@ -87,10 +71,6 @@ o/$(MODE)/libc/time/now.o: \
OVERRIDE_CFLAGS += \
-O3
o//libc/time/zoneinfo.o: \
$(LIBC_TIME_ZONEINFOS:%=usr/share/zoneinfo/%)
@build/zipobj $(OUTPUT_OPTION) $(LIBC_TIME_ZONEINFOS:%=usr/share/zoneinfo/%)
LIBC_TIME_LIBS = $(foreach x,$(LIBC_TIME_ARTIFACTS),$($(x)))
LIBC_TIME_SRCS = $(foreach x,$(LIBC_TIME_ARTIFACTS),$($(x)_SRCS))
LIBC_TIME_HDRS = $(foreach x,$(LIBC_TIME_ARTIFACTS),$($(x)_HDRS))