sync and try changes

This commit is contained in:
Alexander Nicholi 2021-02-04 08:33:22 -05:00
commit dbd7edba10
No known key found for this signature in database
GPG key ID: B75B2EB05540F74C
811 changed files with 2014 additions and 11694 deletions

View file

@ -50,6 +50,7 @@
#include "libc/str/str.h"
#include "libc/str/thompike.h"
#include "libc/str/tpdecode.internal.h"
#include "libc/str/tpenc.h"
#include "libc/str/tpencode.internal.h"
#include "libc/sysv/consts/auxv.h"
#include "libc/sysv/consts/ex.h"
@ -1957,12 +1958,16 @@ static int GetVidyaByte(unsigned char b) {
}
static void OnVidyaServiceWriteCharacter(void) {
uint64_t w;
int i, n, y, x;
char *p, buf[32];
p = buf;
p += FormatCga(m->bx[0], p);
p = stpcpy(p, "\e7");
p += tpencode(p, 8, GetVidyaByte(m->ax[0]), false);
w = tpenc(GetVidyaByte(m->ax[0]));
do {
*p++ = w;
} while ((w >>= 8));
p = stpcpy(p, "\e8");
for (i = Read16(m->cx); i--;) {
PtyWrite(pty, buf, p - buf);
@ -1984,9 +1989,13 @@ static char16_t VidyaServiceXlatTeletype(uint8_t c) {
static void OnVidyaServiceTeletypeOutput(void) {
int n;
uint64_t w;
char buf[12];
n = FormatCga(m->bx[0], buf);
n += tpencode(buf + n, 6, VidyaServiceXlatTeletype(m->ax[0]), false);
w = tpenc(VidyaServiceXlatTeletype(m->ax[0]));
do {
buf[n++] = w;
} while ((w >>= 8));
PtyWrite(pty, buf, n);
}

View file

@ -21,6 +21,7 @@
#include "libc/errno.h"
#include "libc/fmt/fmt.h"
#include "libc/macros.h"
#include "libc/mem/fmt.h"
#include "libc/mem/mem.h"
#include "libc/str/str.h"
#include "libc/str/tpenc.h"

View file

@ -28,7 +28,7 @@
#include "libc/mem/mem.h"
#include "libc/runtime/runtime.h"
#include "libc/str/str.h"
#include "libc/str/tpencode.internal.h"
#include "libc/str/tpenc.h"
#include "third_party/xed/x86.h"
#include "tool/build/lib/case.h"
#include "tool/build/lib/demangle.h"
@ -134,12 +134,18 @@ static char *DisLineCode(struct Dis *d, char *p) {
static char *DisLineData(struct Dis *d, char *p, const uint8_t *b, size_t n) {
size_t i;
uint64_t w;
p = DisColumn(DisAddr(d, p), p, ADDRLEN);
p = DisColumn(DisByte(p, b, n), p, 64);
p = HighStart(p, g_high.comment);
*p++ = '#';
*p++ = ' ';
for (i = 0; i < n; ++i) p += tpencode(p, 8, bing(b[i], 0), false);
for (i = 0; i < n; ++i) {
w = tpenc(bing(b[i], 0));
do {
*p++ = w;
} while ((w >>= 8));
}
p = HighEnd(p);
*p = '\0';
return p;

View file

@ -21,6 +21,7 @@
#include "libc/calls/calls.h"
#include "libc/log/check.h"
#include "libc/macros.h"
#include "libc/mem/fmt.h"
#include "libc/mem/mem.h"
#include "libc/runtime/gc.h"
#include "libc/runtime/memtrack.h"

View file

@ -140,7 +140,7 @@ void LoadProgram(struct Machine *m, const char *prog, char **args, char **vars,
size_t i, mappedsize;
DCHECK_NOTNULL(prog);
elf->prog = prog;
if ((fd = open(prog, O_RDONLY)) == -1 ||
if ((fd = open(prog, O_RDWR)) == -1 ||
(fstat(fd, &st) == -1 || !st.st_size)) {
fputs(prog, stderr);
fputs(": not found\n", stderr);

View file

@ -466,7 +466,7 @@ static int AppendIovsGuest(struct Machine *m, struct Iovs *iv, int64_t iovaddr,
}
static struct sigaction *CoerceSigactionToCosmo(
struct sigaction *dst, const struct sigaction$linux *src) {
struct sigaction *dst, const struct sigaction_linux *src) {
if (!src) return NULL;
memset(dst, 0, sizeof(*dst));
ASSIGN(dst->sa_handler, src->sa_handler);
@ -476,8 +476,8 @@ static struct sigaction *CoerceSigactionToCosmo(
return dst;
}
static struct sigaction$linux *CoerceSigactionToLinux(
struct sigaction$linux *dst, const struct sigaction *src) {
static struct sigaction_linux *CoerceSigactionToLinux(
struct sigaction_linux *dst, const struct sigaction *src) {
if (!dst) return NULL;
memset(dst, 0, sizeof(*dst));
ASSIGN(dst->sa_handler, src->sa_handler);
@ -1145,14 +1145,14 @@ static int OpSigaction(struct Machine *m, int sig, int64_t act, int64_t old) {
int rc;
struct OpSigactionMemory {
struct sigaction act, old;
uint8_t b[sizeof(struct sigaction$linux)];
uint8_t b[sizeof(struct sigaction_linux)];
void *p[2];
} * mem;
if (!(mem = malloc(sizeof(*mem)))) return enomem();
if ((rc = sigaction(
XlatSignal(sig),
CoerceSigactionToCosmo(
&mem->act, LoadBuf(m, act, sizeof(struct sigaction$linux))),
&mem->act, LoadBuf(m, act, sizeof(struct sigaction_linux))),
&mem->old)) != -1) {
CoerceSigactionToLinux(BeginStoreNp(m, old, sizeof(mem->b), mem->p, mem->b),
&mem->old);

View file

@ -367,7 +367,7 @@ int ReadResponse(void) {
goto drop;
case kRunitStderr:
CHECK_GE(n, 4);
size = read32be(p), p += 4, n -= 4;
size = READ32BE(p), p += 4, n -= 4;
while (size) {
if (n) {
CHECK_NE(-1, (rc = write(STDERR_FILENO, p, min(n, size))));

View file

@ -263,12 +263,12 @@ void HandleClient(void) {
got = recv(g_clifd, (p = &g_buf[0]), sizeof(g_buf), 0);
CHECK_GE(got, kMinMsgSize);
CHECK_LE(got, sizeof(g_buf));
CHECK_EQ(RUNITD_MAGIC, read32be(p));
CHECK_EQ(RUNITD_MAGIC, READ32BE(p));
p += 4, got -= 4;
CHECK_EQ(kRunitExecute, *p++);
got--;
namesize = read32be(p), p += 4, got -= 4;
filesize = read32be(p), p += 4, got -= 4;
namesize = READ32BE(p), p += 4, got -= 4;
filesize = READ32BE(p), p += 4, got -= 4;
CHECK_GE(got, namesize);
CHECK_LE(namesize, kMaxNameSize);
CHECK_LE(filesize, kMaxFileSize);

View file

@ -1,961 +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
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/bits/bits.h"
#include "libc/bits/bswap.h"
#include "libc/bits/safemacros.h"
#include "libc/calls/calls.h"
#include "libc/errno.h"
#include "libc/fmt/conv.h"
#include "libc/log/log.h"
#include "libc/macros.h"
#include "libc/math.h"
#include "libc/mem/mem.h"
#include "libc/nexgen32e/bsf.h"
#include "libc/nexgen32e/bsr.h"
#include "libc/nexgen32e/ffs.h"
#include "libc/nexgen32e/x86feature.h"
#include "libc/rand/rand.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/o.h"
#include "libc/time/time.h"
#include "libc/x/x.h"
#include "o/tool/calc/calc.c.inc"
#include "o/tool/calc/calc.h.inc"
#include "third_party/gdtoa/gdtoa.h"
#include "tool/calc/calc.h"
/**
* make -j8 o//tool/calc
* rlwrap -A -H ~/.calc -f tool/calc/calc.lst -e\( o//tool/calc/calc.com
* @see https://github.com/hanslub42/rlwrap
*/
static jmp_buf jb;
static int g_line;
static int g_column;
static const char *g_file;
static yyParser g_parser[1];
wontreturn static void Error(const char *msg) {
fprintf(stderr, "%s:%d:%d: %s\n", g_file, g_line, g_column, msg);
longjmp(jb, 1);
}
wontreturn static void SyntaxError(void) {
Error("SYNTAX ERROR");
}
wontreturn static void LexError(void) {
Error("LEX ERROR");
}
wontreturn static void MissingArgumentError(void) {
Error("MISSING ARGUMENT");
}
wontreturn static void MissingFunctionError(void) {
Error("MISSING FUNCTION");
}
wontreturn static void SyscallError(const char *name) {
fprintf(stderr, "ERROR: %s[%s]: %d\n", name, g_file, errno);
exit(1);
}
static void NumbersFree(struct Numbers *n) {
if (n) {
NumbersFree(n->n);
free(n);
}
}
static struct Numbers *NumbersAppend(struct Numbers *n, long double x) {
struct Numbers *a;
a = malloc(sizeof(struct Numbers));
a->n = n;
a->x = x;
return a;
}
static long double ParseNumber(struct Token t) {
char *ep;
ep = t.s + t.n;
if (t.s[0] == '0') {
return strtoumax(t.s, &ep, 0);
} else {
return strtod(t.s, &ep);
}
}
static long double FnRand(struct Numbers *a) {
return rand();
}
static long double FnRand32(struct Numbers *a) {
return rand32();
}
static long double FnRand64(struct Numbers *a) {
return rand64();
}
static long double FnRdrand(struct Numbers *a) {
if (X86_HAVE(RDRND)) {
return rdrand();
} else {
return NAN;
}
}
static long double FnRdseed(struct Numbers *a) {
if (X86_HAVE(RDSEED)) {
return rdseed();
} else {
return NAN;
}
}
static long double FnAtan2(struct Numbers *a) {
if (!a || !a->n) MissingArgumentError();
return atan2l(a->n->x, a->x);
}
static long double FnLdexp(struct Numbers *a) {
if (!a || !a->n) MissingArgumentError();
return ldexpl(a->n->x, a->x);
}
static long double FnCopysign(struct Numbers *a) {
if (!a || !a->n) MissingArgumentError();
return copysignl(a->n->x, a->x);
}
static long double FnFmax(struct Numbers *a) {
if (!a || !a->n) MissingArgumentError();
return fmaxl(a->n->x, a->x);
}
static long double FnFmin(struct Numbers *a) {
if (!a || !a->n) MissingArgumentError();
return fminl(a->n->x, a->x);
}
static long double FnFmod(struct Numbers *a) {
if (!a || !a->n) MissingArgumentError();
return fmodl(a->n->x, a->x);
}
static long double FnHypot(struct Numbers *a) {
if (!a || !a->n) MissingArgumentError();
return hypotl(a->n->x, a->x);
}
static long double FnPowi(struct Numbers *a) {
if (!a || !a->n) MissingArgumentError();
return powil(a->n->x, a->x);
}
static long double FnPow(struct Numbers *a) {
if (!a || !a->n) MissingArgumentError();
return powl(a->n->x, a->x);
}
static long double FnScalb(struct Numbers *a) {
if (!a || !a->n) MissingArgumentError();
return scalbl(a->n->x, a->x);
}
static long double FnIsgreater(struct Numbers *a) {
if (!a || !a->n) MissingArgumentError();
return isgreater(a->n->x, a->x);
}
static long double FnRemainder(struct Numbers *a) {
if (!a || !a->n) MissingArgumentError();
return remainderl(a->n->x, a->x);
}
static long double FnIsgreaterequal(struct Numbers *a) {
if (!a || !a->n) MissingArgumentError();
return isgreaterequal(a->n->x, a->x);
}
static long double FnIsless(struct Numbers *a) {
if (!a || !a->n) MissingArgumentError();
return isless(a->n->x, a->x);
}
static long double FnIslessequal(struct Numbers *a) {
if (!a || !a->n) MissingArgumentError();
return islessequal(a->n->x, a->x);
}
static long double FnIslessgreater(struct Numbers *a) {
if (!a || !a->n) MissingArgumentError();
return islessgreater(a->n->x, a->x);
}
static long double FnIsunordered(struct Numbers *a) {
if (!a || !a->n) MissingArgumentError();
return isunordered(a->n->x, a->x);
}
static long double FnRounddown(struct Numbers *a) {
if (!a || !a->n) MissingArgumentError();
return ROUNDDOWN((int128_t)a->n->x, (int128_t)a->x);
}
static long double FnRoundup(struct Numbers *a) {
if (!a || !a->n) MissingArgumentError();
return ROUNDUP((int128_t)a->n->x, (int128_t)a->x);
}
static long double FnAcos(struct Numbers *a) {
if (!a) MissingArgumentError();
return acosl(a->x);
}
static long double FnAsin(struct Numbers *a) {
if (!a) MissingArgumentError();
return asinl(a->x);
}
static long double FnAtan(struct Numbers *a) {
if (!a) MissingArgumentError();
return atanl(a->x);
}
static long double FnCbrt(struct Numbers *a) {
if (!a) MissingArgumentError();
return cbrtl(a->x);
}
static long double FnCeil(struct Numbers *a) {
if (!a) MissingArgumentError();
return ceill(a->x);
}
static long double FnCos(struct Numbers *a) {
if (!a) MissingArgumentError();
return cosl(a->x);
}
static long double FnExp10(struct Numbers *a) {
if (!a) MissingArgumentError();
return exp10l(a->x);
}
static long double FnExp2(struct Numbers *a) {
if (!a) MissingArgumentError();
return exp2l(a->x);
}
static long double FnExp(struct Numbers *a) {
if (!a) MissingArgumentError();
return expl(a->x);
}
static long double FnExpm1(struct Numbers *a) {
if (!a) MissingArgumentError();
return expm1l(a->x);
}
static long double FnFabs(struct Numbers *a) {
if (!a) MissingArgumentError();
return fabsl(a->x);
}
static long double FnFloor(struct Numbers *a) {
if (!a) MissingArgumentError();
return floorl(a->x);
}
static long double FnIlogb(struct Numbers *a) {
if (!a) MissingArgumentError();
return ilogbl(a->x);
}
static long double FnLog10(struct Numbers *a) {
if (!a) MissingArgumentError();
return log10l(a->x);
}
static long double FnLog1p(struct Numbers *a) {
if (!a) MissingArgumentError();
return log1pl(a->x);
}
static long double FnLog2(struct Numbers *a) {
if (!a) MissingArgumentError();
return log2l(a->x);
}
static long double FnLogb(struct Numbers *a) {
if (!a) MissingArgumentError();
return logbl(a->x);
}
static long double FnLog(struct Numbers *a) {
if (!a) MissingArgumentError();
return logl(a->x);
}
static long double FnLrint(struct Numbers *a) {
if (!a) MissingArgumentError();
return lrintl(a->x);
}
static long double FnLround(struct Numbers *a) {
if (!a) MissingArgumentError();
return lroundl(a->x);
}
static long double FnNearbyint(struct Numbers *a) {
if (!a) MissingArgumentError();
return nearbyintl(a->x);
}
static long double FnRint(struct Numbers *a) {
if (!a) MissingArgumentError();
return rintl(a->x);
}
static long double FnRound(struct Numbers *a) {
if (!a) MissingArgumentError();
return roundl(a->x);
}
static long double FnSignificand(struct Numbers *a) {
if (!a) MissingArgumentError();
return significandl(a->x);
}
static long double FnSin(struct Numbers *a) {
if (!a) MissingArgumentError();
return sinl(a->x);
}
static long double FnSqrt(struct Numbers *a) {
if (!a) MissingArgumentError();
return sqrtl(a->x);
}
static long double FnTan(struct Numbers *a) {
if (!a) MissingArgumentError();
return tanl(a->x);
}
static long double FnTrunc(struct Numbers *a) {
if (!a) MissingArgumentError();
return truncl(a->x);
}
static long double FnIsinf(struct Numbers *a) {
if (!a) MissingArgumentError();
return isinf(a->x);
}
static long double FnIsnan(struct Numbers *a) {
if (!a) MissingArgumentError();
return isnan(a->x);
}
static long double FnIsfinite(struct Numbers *a) {
if (!a) MissingArgumentError();
return isfinite(a->x);
}
static long double FnIsnormal(struct Numbers *a) {
if (!a) MissingArgumentError();
return isnormal(a->x);
}
static long double FnSignbit(struct Numbers *a) {
if (!a) MissingArgumentError();
return signbit(a->x);
}
static long double FnFpclassify(struct Numbers *a) {
if (!a) MissingArgumentError();
return fpclassify(a->x);
}
static long double FnBswap16(struct Numbers *a) {
if (!a) MissingArgumentError();
return bswap_16((uint16_t)a->x);
}
static long double FnBswap32(struct Numbers *a) {
if (!a) MissingArgumentError();
return bswap_32((uint32_t)a->x);
}
static long double FnBswap64(struct Numbers *a) {
if (!a) MissingArgumentError();
return bswap_64((uint64_t)a->x);
}
static long double FnBsr(struct Numbers *a) {
if (!a) MissingArgumentError();
return bsr(a->x);
}
static long double FnBsrl(struct Numbers *a) {
if (!a) MissingArgumentError();
return bsrl(a->x);
}
static long double FnBsfl(struct Numbers *a) {
if (!a) MissingArgumentError();
return bsfl(a->x);
}
static long double FnFfs(struct Numbers *a) {
if (!a) MissingArgumentError();
return ffs(a->x);
}
static long double FnFfsl(struct Numbers *a) {
if (!a) MissingArgumentError();
return ffsl(a->x);
}
static long double FnGray(struct Numbers *a) {
if (!a) MissingArgumentError();
return gray(a->x);
}
static long double FnUngray(struct Numbers *a) {
if (!a) MissingArgumentError();
return ungray(a->x);
}
static long double FnRounddown2pow(struct Numbers *a) {
if (!a) MissingArgumentError();
return rounddown2pow(a->x);
}
static long double FnRoundup2pow(struct Numbers *a) {
if (!a) MissingArgumentError();
return roundup2pow(a->x);
}
static long double FnRoundup2log(struct Numbers *a) {
if (!a) MissingArgumentError();
return roundup2log(a->x);
}
static long double FnBitreverse8(struct Numbers *a) {
if (!a) MissingArgumentError();
return bitreverse8(a->x);
}
static long double FnBitreverse16(struct Numbers *a) {
if (!a) MissingArgumentError();
return bitreverse16(a->x);
}
static long double FnBitreverse32(struct Numbers *a) {
if (!a) MissingArgumentError();
return bitreverse32(a->x);
}
static long double FnBitreverse64(struct Numbers *a) {
if (!a) MissingArgumentError();
return bitreverse64(a->x);
}
static int8_t sarb(int8_t x, uint8_t y) {
return x >> (y & 7);
}
static int16_t sarw(int16_t x, uint8_t y) {
return x >> (y & 15);
}
static int32_t sarl(int32_t x, uint8_t y) {
return x >> (y & 31);
}
static int64_t sarq(int64_t x, uint8_t y) {
return x >> (y & 63);
}
static long double FnSarb(struct Numbers *a) {
if (!a || !a->n) MissingArgumentError();
return sarb(a->n->x, a->x);
}
static long double FnSarw(struct Numbers *a) {
if (!a || !a->n) MissingArgumentError();
return sarw(a->n->x, a->x);
}
static long double FnSarl(struct Numbers *a) {
if (!a || !a->n) MissingArgumentError();
return sarl(a->n->x, a->x);
}
static long double FnSarq(struct Numbers *a) {
if (!a || !a->n) MissingArgumentError();
return sarq(a->n->x, a->x);
}
static long double FnSar(struct Numbers *a) {
if (!a || !a->n) MissingArgumentError();
return sarq(a->n->x, a->x);
}
static uint8_t rorb(uint8_t x, uint8_t y) {
return x >> (y & 7) | x << (8 - (y & 7));
}
static uint16_t rorw(uint16_t x, uint8_t y) {
return x >> (y & 15) | x << (16 - (y & 15));
}
static uint32_t rorl(uint32_t x, uint8_t y) {
return x >> (y & 31) | x << (32 - (y & 31));
}
static uint64_t rorq(uint64_t x, uint8_t y) {
return x >> (y & 63) | x << (64 - (y & 63));
}
static long double FnRorb(struct Numbers *a) {
if (!a || !a->n) MissingArgumentError();
return rorb(a->n->x, a->x);
}
static long double FnRorw(struct Numbers *a) {
if (!a || !a->n) MissingArgumentError();
return rorw(a->n->x, a->x);
}
static long double FnRorl(struct Numbers *a) {
if (!a || !a->n) MissingArgumentError();
return rorl(a->n->x, a->x);
}
static long double FnRorq(struct Numbers *a) {
if (!a || !a->n) MissingArgumentError();
return rorq(a->n->x, a->x);
}
static long double FnRor(struct Numbers *a) {
if (!a || !a->n) MissingArgumentError();
return rorq(a->n->x, a->x);
}
static uint8_t rolb(uint8_t x, uint8_t y) {
return x << (y & 7) | x >> (8 - (y & 7));
}
static uint16_t rolw(uint16_t x, uint8_t y) {
return x << (y & 15) | x >> (16 - (y & 15));
}
static uint32_t roll(uint32_t x, uint8_t y) {
return x << (y & 31) | x >> (32 - (y & 31));
}
static uint64_t rolq(uint64_t x, uint8_t y) {
return x << (y & 63) | x >> (64 - (y & 63));
}
static long double FnRolb(struct Numbers *a) {
if (!a || !a->n) MissingArgumentError();
return rolb(a->n->x, a->x);
}
static long double FnRolw(struct Numbers *a) {
if (!a || !a->n) MissingArgumentError();
return rolw(a->n->x, a->x);
}
static long double FnRoll(struct Numbers *a) {
if (!a || !a->n) MissingArgumentError();
return roll(a->n->x, a->x);
}
static long double FnRolq(struct Numbers *a) {
if (!a || !a->n) MissingArgumentError();
return rolq(a->n->x, a->x);
}
static long double FnRol(struct Numbers *a) {
if (!a || !a->n) MissingArgumentError();
return rolq(a->n->x, a->x);
}
static long double FnTime(struct Numbers *a) {
return nowl();
}
static long double FnBin(struct Numbers *a) {
if (!a) MissingArgumentError();
printf("0b%jb\n", (uint128_t)a->x);
return 0;
}
static long double FnOct(struct Numbers *a) {
if (!a) MissingArgumentError();
printf("0%jo\n", (uint128_t)a->x);
return 0;
}
static long double FnHex(struct Numbers *a) {
if (!a) MissingArgumentError();
printf("0x%jx\n", (uint128_t)a->x);
return 0;
}
static void PrintNumber(long double x) {
char buf[32];
g_xfmt_p(buf, &x, 15, sizeof(buf), 0);
fputs(buf, stdout);
}
static void Print(struct Numbers *a) {
if (a) {
Print(a->n);
if (a->n) fputc(' ', stdout);
PrintNumber(a->x);
}
}
static long double FnPrint(struct Numbers *a) {
Print(a);
fputc('\n', stdout);
return 0;
}
static const struct Fn {
const char *s;
long double (*f)(struct Numbers *);
} kFunctions[] = {
{"abs", FnFabs},
{"acos", FnAcos},
{"asin", FnAsin},
{"atan", FnAtan},
{"atan2", FnAtan2},
{"bin", FnBin},
{"bitreverse16", FnBitreverse16},
{"bitreverse32", FnBitreverse32},
{"bitreverse64", FnBitreverse64},
{"bitreverse8", FnBitreverse8},
{"bsfl", FnBsfl},
{"bsfl", FnBsfl},
{"bsr", FnBsr},
{"bsrl", FnBsrl},
{"bswap16", FnBswap16},
{"bswap32", FnBswap32},
{"bswap64", FnBswap64},
{"cbrt", FnCbrt},
{"ceil", FnCeil},
{"copysign", FnCopysign},
{"cos", FnCos},
{"exp", FnExp},
{"exp10", FnExp10},
{"exp2", FnExp2},
{"expm1", FnExpm1},
{"fabs", FnFabs},
{"ffs", FnFfs},
{"ffsl", FnFfsl},
{"floor", FnFloor},
{"fmax", FnFmax},
{"fmin", FnFmin},
{"fmod", FnFmod},
{"fpclassify", FnFpclassify},
{"gray", FnGray},
{"hex", FnHex},
{"hypot", FnHypot},
{"ilogb", FnIlogb},
{"isfinite", FnIsfinite},
{"isgreater", FnIsgreater},
{"isgreaterequal", FnIsgreaterequal},
{"isinf", FnIsinf},
{"isless", FnIsless},
{"islessequal", FnIslessequal},
{"islessgreater", FnIslessgreater},
{"isnan", FnIsnan},
{"isnormal", FnIsnormal},
{"isunordered", FnIsunordered},
{"ldexp", FnLdexp},
{"ldexp", FnLdexp},
{"log", FnLog},
{"log10", FnLog10},
{"log1p", FnLog1p},
{"log2", FnLog2},
{"logb", FnLogb},
{"lrint", FnLrint},
{"lround", FnLround},
{"max", FnFmax},
{"min", FnFmin},
{"nearbyint", FnNearbyint},
{"oct", FnOct},
{"pow", FnPow},
{"powi", FnPowi},
{"print", FnPrint},
{"rand", FnRand},
{"rand32", FnRand32},
{"rand64", FnRand64},
{"rdrand", FnRdrand},
{"rdseed", FnRdseed},
{"remainder", FnRemainder},
{"rint", FnRint},
{"rol", FnRol},
{"rolb", FnRolb},
{"roll", FnRoll},
{"rolq", FnRolq},
{"rolw", FnRolw},
{"ror", FnRor},
{"rorb", FnRorb},
{"rorl", FnRorl},
{"rorq", FnRorq},
{"rorw", FnRorw},
{"round", FnRound},
{"rounddown", FnRounddown},
{"rounddown2pow", FnRounddown2pow},
{"roundup", FnRoundup},
{"roundup2log", FnRoundup2log},
{"roundup2pow", FnRoundup2pow},
{"sar", FnSar},
{"sarb", FnSarb},
{"sarl", FnSarl},
{"sarq", FnSarq},
{"sarw", FnSarw},
{"scalb", FnScalb},
{"signbit", FnSignbit},
{"signbit", FnSignbit},
{"significand", FnSignificand},
{"sin", FnSin},
{"sqrt", FnSqrt},
{"tan", FnTan},
{"time", FnTime},
{"trunc", FnTrunc},
{"ungray", FnUngray},
};
static long double CallFunction(struct Token fn, struct Numbers *args) {
int l, r, m, p;
l = 0;
r = ARRAYLEN(kFunctions) - 1;
while (l <= r) {
m = (l + r) >> 1;
p = strncmp(kFunctions[m].s, fn.s, fn.n);
if (p < 0) {
l = m + 1;
} else if (p > 0) {
r = m - 1;
} else {
return kFunctions[m].f(args);
}
}
MissingFunctionError();
}
static void Tokenize(const char *s, size_t size) {
size_t n;
char *se;
for (se = s + size; s < se; s += n, ++g_column) {
n = 1;
switch (*s & 0xff) {
case ' ':
case '\t':
case '\v':
case '\r':
break;
case '\n':
++g_line;
g_column = 0;
break;
case 'A' ... 'Z':
case 'a' ... 'z':
n = strspn(s, "0123456789"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz");
Parse(g_parser, SYMBOL, (struct Token){s, n});
break;
case '0':
n = strspn(s, "xXbB0123456789abcdefABCDEF");
Parse(g_parser, NUMBER, (struct Token){s, n});
n += strspn(s + n, "LUlu");
break;
case '1' ... '9':
n = strspn(s, "0123456789.");
if (s[n] == 'e' || s[n] == 'E') {
++n;
if (s[n] == '+' || s[n] == '-') ++n;
n += strspn(s + n, "0123456789");
}
Parse(g_parser, NUMBER, (struct Token){s, n});
n += strspn(s + n, "LUlu");
break;
case '(':
Parse(g_parser, LP, (struct Token){0, 0});
break;
case ')':
Parse(g_parser, RP, (struct Token){0, 0});
break;
case ',':
Parse(g_parser, COMMA, (struct Token){0, 0});
break;
case '^':
Parse(g_parser, XOR, (struct Token){0, 0});
break;
case '%':
Parse(g_parser, REM, (struct Token){0, 0});
break;
case '+':
Parse(g_parser, PLUS, (struct Token){0, 0});
break;
case '-':
Parse(g_parser, MINUS, (struct Token){0, 0});
break;
case '~':
Parse(g_parser, NOT, (struct Token){0, 0});
break;
case '/':
if (s[1] == '/') {
Parse(g_parser, DDIV, (struct Token){0, 0});
++n;
} else {
Parse(g_parser, DIV, (struct Token){0, 0});
}
break;
case '*':
if (s[1] == '*') {
Parse(g_parser, EXP, (struct Token){0, 0});
++n;
} else {
Parse(g_parser, MUL, (struct Token){0, 0});
}
break;
case '|':
if (s[1] == '|') {
Parse(g_parser, LOR, (struct Token){0, 0});
++n;
} else {
Parse(g_parser, OR, (struct Token){0, 0});
}
break;
case '&':
if (s[1] == '&') {
Parse(g_parser, LAND, (struct Token){0, 0});
++n;
} else {
Parse(g_parser, AND, (struct Token){0, 0});
}
break;
case '!':
if (s[1] == '=') {
Parse(g_parser, NE, (struct Token){0, 0});
++n;
} else {
Parse(g_parser, LNOT, (struct Token){0, 0});
}
break;
case '=':
if (s[1] == '=') {
Parse(g_parser, EQ, (struct Token){0, 0});
++n;
} else {
LexError();
}
break;
case '>':
if (s[1] == '=') {
Parse(g_parser, GE, (struct Token){0, 0});
++n;
} else if (s[1] == '>') {
Parse(g_parser, SHR, (struct Token){0, 0});
++n;
} else {
Parse(g_parser, GT, (struct Token){0, 0});
}
break;
case '<':
if (s[1] == '=') {
Parse(g_parser, LE, (struct Token){0, 0});
++n;
} else if (s[1] == '<') {
Parse(g_parser, SHL, (struct Token){0, 0});
++n;
} else {
Parse(g_parser, LT, (struct Token){0, 0});
}
break;
default:
LexError();
}
}
}
int main(int argc, char *argv[]) {
int i;
int ec;
int fd;
size_t n;
char *buf;
ssize_t rc;
size_t bufcap;
if (!(ec = setjmp(jb))) {
if (argc > 1) {
ParseInit(g_parser);
bufcap = BIGPAGESIZE;
buf = malloc(bufcap);
for (i = 1; i < argc; ++i) {
g_file = argv[i];
g_line = 0;
g_column = 0;
n = 0; /* wut */
if ((fd = open(g_file, O_RDONLY)) == -1) SyscallError("open");
for (;;) {
if ((rc = read(fd, buf, bufcap)) == -1) SyscallError("read");
if (!(n = rc)) break;
Tokenize(buf, n);
}
close(fd);
Parse(g_parser, 0, (struct Token){0, 0});
}
ParseFinalize(g_parser);
} else {
g_file = "/dev/stdin";
g_line = 0;
g_column = 0;
buf = NULL;
bufcap = 0;
while (getline(&buf, &bufcap, stdin) != -1) {
if ((n = strlen(buf))) {
ParseInit(g_parser);
if (!setjmp(jb)) {
Tokenize("print(", 6);
Tokenize(buf, n);
Tokenize(")", 1);
Parse(g_parser, 0, (struct Token){0, 0});
}
ParseFinalize(g_parser);
}
}
}
}
free(buf);
return ec;
}

View file

@ -1,24 +0,0 @@
#ifndef COSMOPOLITAN_TOOL_CALC_CALC_H_
#define COSMOPOLITAN_TOOL_CALC_CALC_H_
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
struct Token {
const char *s;
size_t n;
};
struct Numbers {
struct Numbers *n;
long double x;
};
static void SyntaxError(void) wontreturn;
static long double ParseNumber(struct Token);
static void NumbersFree(struct Numbers *);
static struct Numbers *NumbersAppend(struct Numbers *, long double);
static long double CallFunction(struct Token, struct Numbers *);
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_TOOL_CALC_CALC_H_ */

View file

@ -1,93 +0,0 @@
abs
acos
asin
atan
atan2
bin
bitreverse16
bitreverse32
bitreverse64
bitreverse8
bsfl
bsfl
bsr
bsr
cbrt
ceil
copysign
cos
exp
exp10
exp2
expm1
fabs
ffs
ffsl
floor
fmax
fmin
fmod
fpclassify
gray
hex
hypot
ilogb
isfinite
isgreater
isgreaterequal
isinf
isless
islessequal
islessgreater
isnan
isnormal
isunordered
ldexp
ldexp
log
log10
log1p
log2
logb
lrint
lround
max
min
nearbyint
oct
pow
powi
print
remainder
rint
rol
rolb
roll
rolq
rolw
ror
rorb
rorl
rorq
rorw
round
rounddown
rounddown2pow
roundup
roundup2log
roundup2pow
sar
sarb
sarl
sarq
sarw
scalb
signbit
signbit
significand
sin
sqrt
tan
time
trunc
ungray

View file

@ -1,79 +0,0 @@
#-*-mode:makefile-gmake;indent-tabs-mode:t;tab-width:8;coding:utf-8-*-┐
#───vi: set et ft=make ts=8 tw=8 fenc=utf-8 :vi───────────────────────┘
PKGS += TOOL_CALC
TOOL_CALC = $(TOOL_LIB_A_DEPS) $(TOOL_LIB_A)
TOOL_CALC_A = o/$(MODE)/tool/calc/calc.a
TOOL_CALC_FILES := $(wildcard tool/calc/*)
TOOL_CALC_COMS = $(TOOL_CALC_OBJS:%.o=%.com)
TOOL_CALC_SRCS = $(filter %.c,$(TOOL_CALC_FILES))
TOOL_CALC_HDRS = $(filter %.h,$(TOOL_CALC_FILES))
TOOL_CALC_OBJS = \
$(TOOL_CALC_SRCS:%.c=o/$(MODE)/%.o)
TOOL_CALC_COMS = \
$(TOOL_CALC_SRCS:%.c=o/$(MODE)/%.com)
TOOL_CALC_BINS = \
$(TOOL_CALC_COMS) \
$(TOOL_CALC_COMS:%=%.dbg)
TOOL_CALC_CHECKS = \
$(TOOL_CALC_HDRS:%=o/$(MODE)/%.ok)
TOOL_CALC_DIRECTDEPS = \
LIBC_BITS \
LIBC_CALLS \
LIBC_FMT \
LIBC_INTRIN \
LIBC_LOG \
LIBC_MEM \
LIBC_NEXGEN32E \
LIBC_RAND \
LIBC_RUNTIME \
LIBC_STDIO \
LIBC_STR \
LIBC_STUBS \
LIBC_SYSV \
LIBC_TINYMATH \
LIBC_X \
THIRD_PARTY_COMPILER_RT \
THIRD_PARTY_GDTOA
TOOL_CALC_DEPS := \
$(call uniq,$(foreach x,$(TOOL_CALC_DIRECTDEPS),$($(x))))
$(TOOL_CALC_A): \
tool/calc/ \
$(TOOL_CALC_A).pkg \
$(TOOL_CALC_OBJS)
$(TOOL_CALC_A).pkg: \
$(TOOL_CALC_OBJS) \
$(foreach x,$(TOOL_CALC_DIRECTDEPS),$($(x)_A).pkg)
o/tool/calc/calc.h.inc: o/tool/calc/calc.c.inc
o/tool/calc/calc.c.inc: \
tool/calc/calc.y \
$(THIRD_PARTY_LEMON)
@$(LEMON) -l -d$(@D) $<
o/$(MODE)/tool/calc/%.com.dbg: \
$(TOOL_CALC_DEPS) \
$(TOOL_CALC_A) \
o/$(MODE)/tool/calc/%.o \
$(TOOL_CALC_A).pkg \
$(CRT) \
$(APE)
@$(APELINK)
tool/calc/calc.c: \
o/tool/calc/calc.c.inc \
o/tool/calc/calc.h.inc
.PHONY: o/$(MODE)/tool/calc
o/$(MODE)/tool/calc: \
$(TOOL_CALC_BINS) \
$(TOOL_CALC_CHECKS)

View file

@ -1,79 +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 │
│ │
│ 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 {
#include "libc/stdio/stdio.h"
#include "tool/calc/calc.h"
#include "libc/calls/calls.h"
#include "libc/str/str.h"
#include "libc/x/x.h"
#include "libc/runtime/gc.h"
#include "libc/math.h"
}
%token_type {struct Token}
%type number {long double}
%syntax_error { SyntaxError(); }
%left LOR.
%left LAND.
%left OR.
%left XOR.
%left AND.
%left EQ NE.
%left LT LE GT GE.
%left SHL SHR.
%left PLUS MINUS.
%left MUL DIV DDIV REM.
%right NOT LNOT.
%right EXP.
program ::= number.
number(A) ::= NUMBER(B). { A = ParseNumber(B); }
number(A) ::= LP number(B) RP. { A = B; }
number(A) ::= LNOT number(B). { A = !B; }
number(A) ::= NOT number(B). { A = ~(long)B; }
number(A) ::= PLUS number(B). { A = +B; } [NOT]
number(A) ::= MINUS number(B). { A = -B; } [NOT]
number(A) ::= SYMBOL(F) LP numbers(N) RP. { A = CallFunction(F, N); }
number(A) ::= number(B) EQ number(C). { A = B == C; }
number(A) ::= number(B) NE number(C). { A = B != C; }
number(A) ::= number(B) LT number(C). { A = B < C; }
number(A) ::= number(B) LE number(C). { A = B <= C; }
number(A) ::= number(B) GT number(C). { A = B > C; }
number(A) ::= number(B) GE number(C). { A = B >= C; }
number(A) ::= number(B) LOR number(C). { A = B || C; }
number(A) ::= number(B) LAND number(C). { A = B && C; }
number(A) ::= number(B) PLUS number(C). { A = B + C; }
number(A) ::= number(B) MINUS number(C). { A = B - C; }
number(A) ::= number(B) MUL number(C). { A = B * C; }
number(A) ::= number(B) DIV number(C). { A = B / C; }
number(A) ::= number(B) REM number(C). { A = remainderl(B, C); }
number(A) ::= number(B) EXP number(C). { A = powl(B, C); }
number(A) ::= number(B) DDIV number(C). { A = truncl(B / C); }
number(A) ::= number(B) OR number(C). { A = (long)B | (long)C; }
number(A) ::= number(B) XOR number(C). { A = (long)B ^ (long)C; }
number(A) ::= number(B) AND number(C). { A = (long)B & (long)C; }
number(A) ::= number(B) SHL number(C). { A = (long)B << (long)C; }
number(A) ::= number(B) SHR number(C). { A = (long)B >> (long)C; }
%type numbers {struct Numbers *}
%destructor numbers { NumbersFree($$); }
numbers(A) ::= . { A = 0; }
numbers(A) ::= number(B). { A = NumbersAppend(0, B); }
numbers(A) ::= numbers(A) COMMA number(B). { A = NumbersAppend(A, B); }

View file

@ -100,7 +100,7 @@ static void Print(void) {
arsize = atoi((char *)(data + 8 + 48));
CHECK_LE(4, arsize);
CHECK_LE(8 + 60 + arsize, size);
entries = read32be(data + 8 + 60);
entries = READ32BE(data + 8 + 60);
CHECK_LE(4 + entries * 4 + 1, arsize);
printf("\t# %'s\n", path);
PrintString(data, 8, "file signature");
@ -110,7 +110,7 @@ static void Print(void) {
printf("\t.long\t%u\t\t\t# %s\n", entries, "symbol table entries");
table = 8 + 60 + 4;
for (i = 0; i < entries; ++i) {
printf("\t.long\t%#x\t\t\t\t# %u\n", read32be(data + table + i * 4), i);
printf("\t.long\t%#x\t\t\t\t# %u\n", READ32BE(data + table + i * 4), i);
}
symbols = table + entries * 4;
symbolslen = arsize - (4 + entries * 4);

View file

@ -94,22 +94,22 @@ void showcompressmethod(uint16_t compressmethod) {
void showextrantfs(uint8_t *ntfs) {
struct timespec mtime, atime, ctime;
mtime = FileTimeToTimeSpec(
(struct NtFileTime){read32le(ntfs + 8), read32le(ntfs + 12)});
(struct NtFileTime){READ32LE(ntfs + 8), READ32LE(ntfs + 12)});
atime = FileTimeToTimeSpec(
(struct NtFileTime){read32le(ntfs + 16), read32le(ntfs + 20)});
(struct NtFileTime){READ32LE(ntfs + 16), READ32LE(ntfs + 20)});
ctime = FileTimeToTimeSpec(
(struct NtFileTime){read32le(ntfs + 24), read32le(ntfs + 28)});
show(".long", gc(xasprintf("%d", read32le(ntfs))), "ntfs reserved");
show(".short", gc(xasprintf("0x%04x", read16le(ntfs + 4))),
(struct NtFileTime){READ32LE(ntfs + 24), READ32LE(ntfs + 28)});
show(".long", gc(xasprintf("%d", READ32LE(ntfs))), "ntfs reserved");
show(".short", gc(xasprintf("0x%04x", READ16LE(ntfs + 4))),
"ntfs attribute tag value #1");
show(".short", gc(xasprintf("%hu", read16le(ntfs + 6))),
show(".short", gc(xasprintf("%hu", READ16LE(ntfs + 6))),
"ntfs attribute tag size");
show(".quad", gc(xasprintf("%lu", read64le(ntfs + 8))),
show(".quad", gc(xasprintf("%lu", READ64LE(ntfs + 8))),
gc(xasprintf("%s (%s)", "ntfs last modified time",
gc(xiso8601(&mtime)))));
show(".quad", gc(xasprintf("%lu", read64le(ntfs + 16))),
show(".quad", gc(xasprintf("%lu", READ64LE(ntfs + 16))),
gc(xasprintf("%s (%s)", "ntfs last access time", gc(xiso8601(&atime)))));
show(".quad", gc(xasprintf("%lu", read64le(ntfs + 24))),
show(".quad", gc(xasprintf("%lu", READ64LE(ntfs + 24))),
gc(xasprintf("%s (%s)", "ntfs creation time", gc(xiso8601(&ctime)))));
}

View file

@ -4,7 +4,6 @@
.PHONY: o/$(MODE)/tool
o/$(MODE)/tool: \
o/$(MODE)/tool/build \
o/$(MODE)/tool/calc \
o/$(MODE)/tool/decode \
o/$(MODE)/tool/hash \
o/$(MODE)/tool/net \

View file

@ -87,7 +87,7 @@ noasan void PrintStartupInfo(void) {
\n\
\n");
#define X(D, F) \
printf("%s.%-22s= " D "\n", "g_ntstartupinfo", #F, g_ntstartupinfo.F);
printf("%s.%-22s= " D "\n", "__nt_startupinfo", #F, __nt_startupinfo.F);
X("%u", cb);
X("%p", lpReserved);
X("%hs", lpDesktop);
@ -99,8 +99,8 @@ noasan void PrintStartupInfo(void) {
X("%u", dwXCountChars);
X("%u", dwYCountChars);
X("%u", dwFillAttribute);
printf("%s.%-22s: %s\n", "g_ntstartupinfo", "dwFlags",
RecreateFlags(kNtStartfFlagNames, g_ntstartupinfo.dwFlags));
printf("%s.%-22s: %s\n", "__nt_startupinfo", "dwFlags",
RecreateFlags(kNtStartfFlagNames, __nt_startupinfo.dwFlags));
X("%hu", wShowWindow);
X("%hu", cbReserved2);
X("%s", lpReserved2);
@ -117,7 +117,7 @@ void PrintSystemInfo(void) {
\n\
\n");
#define X(D, F) \
printf("%s.%-28s= " D "\n", "g_ntsysteminfo", #F, g_ntsysteminfo.F);
printf("%s.%-28s= " D "\n", "__nt_systeminfo", #F, __nt_systeminfo.F);
X("%08x", dwOemId);
X("%04hx", wProcessorArchitecture);
X("%d", dwPageSize);

View file

@ -127,12 +127,17 @@ Flags & Keyboard Shortcuts:\n\
CTRL+L redraw [keyboard]\n\
CTRL+Z suspend [keyboard]\n\
CTRL+C exit [keyboard]\n\
q quit [keyboard]\n\
\n\
Effects Shortcuts:\n\
\n\
H +Hue ALT+H -Hue\n\
S +Saturation ALT+S -Saturation\n\
L +Lightness ALT+L -Lightness\n\
S Toggle Swing (TV, PC)\n\
Y Toggle Black/White Mode\n\
p Toggle Primaries (BT.601, BT.709)\n\
g +Gamma G -Gamma\n\
l +Illumination L -Illumination\n\
k +LumaKernel K -LumaKernel\n\
j +ChromaKernel J -ChromaKernel\n\
CTRL-G {Unsharp,Sharp}\n\
\n\
Environment Variables:\n\
@ -1018,6 +1023,7 @@ static optimizesize void ReadKeyboard(void) {
chromakernel_ = MOD(sgn + chromakernel_, ARRAYLEN(kMagkern));
memcpy(g_magkern, kMagkern[chromakernel_], sizeof(kMagkern[0]));
break;
case 'q':
case CTRL('C'):
longjmp(jb_, 1);
break;