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

1
.gitignore vendored
View File

@ -6,3 +6,4 @@
/TAGS
/bx_enh_dbg.ini
/tool/emacs/*.elc
/usr/share/dict/words

View File

@ -815,8 +815,8 @@ ape.pe: .ascin "PE",4
.short .LDLLEXE # DllCharacteristics
.quad 0x0000000000100000 # StackReserve
.quad 0x0000000000100000 # StackCommit
.quad 0x0000000000080000 # HeapReserve
.quad 0x0000000000001000 # HeapCommit
.quad 0x0000000000000000 # HeapReserve
.quad 0x0000000000000000 # HeapCommit
.long 0x00000000 # LoaderFlags
.long 16 # NumberOfDirectoryEntries
.long 0,0 # ExportsDirectory

View File

@ -85,7 +85,7 @@ long GetIntegerCoefficients(long N[static 6], const double C[static 6], long M,
N[i] = 0;
}
}
if (!IsTiny() && least > 1) {
if (!NoDebug() && least > 1) {
for (j[0] = 0; j[0] < J[0]; ++j[0]) {
for (j[1] = 0; j[1] < J[1]; ++j[1]) {
for (j[2] = 0; j[2] < J[2]; ++j[2]) {

View File

@ -75,7 +75,6 @@ static struct SamplingSolution *NewSamplingSolution(long n, long s) {
ss->indices = xcalloc(n * s, sizeof(short));
return ss;
}
static bool IsNormalized(int n, double A[n]) {
int i;
double x;

View File

@ -24,6 +24,9 @@
#include "libc/nt/console.h"
#include "libc/nt/runtime.h"
#include "libc/nt/struct/consolecursorinfo.h"
#include "libc/runtime/runtime.h"
/* TODO(jart): DELETE */
static int ttysetcursor(int fd, bool visible) {
struct NtConsoleCursorInfo ntcursor;

View File

@ -30,6 +30,8 @@
#include "libc/sysv/consts/poll.h"
#include "libc/sysv/errfuns.h"
/* TODO(jart): DELETE */
static int ttyident_probe(struct TtyIdent *ti, int ttyinfd, int ttyoutfd,
const char *msg) {
ssize_t rc;

View File

@ -20,6 +20,8 @@
#include "dsp/tty/tty.h"
#include "libc/str/str.h"
/* TODO(jart): DELETE */
/**
* Sends data to teletypewriter.
*
@ -27,4 +29,6 @@
*
* @return 0 on success, or -1 w/ errno
*/
int ttysend(int fd, const char *str) { return ttywrite(fd, str, strlen(str)); }
int ttysend(int fd, const char *str) {
return ttywrite(fd, str, strlen(str));
}

View File

@ -22,6 +22,8 @@
#include "libc/runtime/gc.h"
#include "libc/x/x.h"
/* TODO(jart): DELETE */
/**
* Changes text in title bar of pseudo-teletypewriter window.
*

View File

@ -38,6 +38,8 @@
#include "libc/sysv/errfuns.h"
#include "libc/x/x.h"
/* TODO(jart): DELETE */
#define FD STDOUT_FILENO
static struct TtyRaw {

View File

@ -25,6 +25,8 @@
#include "libc/sysv/consts/poll.h"
#include "libc/sysv/consts/termios.h"
/* TODO(jart): DELETE */
/**
* Sends data to teletypewriter the pain-free way.
*

136
examples/acid2.c 100644
View File

@ -0,0 +1,136 @@
#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/ioctl.h"
#include "libc/calls/struct/termios.h"
#include "libc/fmt/fmt.h"
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/termios.h"
int yn2, xn2;
int yn3, xn3;
char b[128], inbuf[128];
int y, x, yn, xn, my, mx;
struct termios term, oldterm;
int main(int argc, char *argv[]) {
int i;
setvbuf(stdout, inbuf, _IONBF, 128); /* make things slower */
/* raw mode */
ioctl(1, TCGETS, &oldterm);
memcpy(&term, &oldterm, sizeof(term));
term.c_cc[VMIN] = 1;
term.c_cc[VTIME] = 1;
term.c_iflag &= ~(INPCK | ISTRIP | PARMRK | INLCR | IGNCR | ICRNL | IXON);
term.c_lflag &= ~(IEXTEN | ICANON | ECHO | ECHONL);
term.c_cflag &= ~(CSIZE | PARENB);
term.c_oflag &= ~OPOST;
term.c_cflag |= CS8;
term.c_iflag |= IUTF8;
ioctl(1, TCSETSF, &term);
/* get cursor position and display dimensions */
printf("\e7\e[6n\e[9979;9979H\e[6n\e8");
read(0, b, sizeof(b));
sscanf(b, "\e[%d;%dR\e[%d;%dR", &y, &x, &yn, &xn);
printf("\e[1q"); /* turn on led one */
printf("\e[2J"); /* clear display */
printf("\e#8"); /* fill display with E's */
printf("\e[H");
/* clear display again */
printf("\e[2q"); /* turn on led two */
for (i = 0; i < yn; ++i) {
if (i) printf("\n");
printf(" ");
printf("\e[0K");
}
for (i = 0; i < yn - 1; ++i) {
if (i) printf("\eM");
printf("\e[1K");
}
printf("\e(0"); /* line drawing mode */
printf("\e[3q"); /* turn on led three */
printf("\e[H");
/* move to center */
my = yn / 2;
mx = xn / 2 - 7;
if (y > my) {
printf("\e[%dA", y - my);
} else if (y < my) {
printf("\e[%dB", my - y);
}
if (x > mx) {
printf("\e[%dD", x - mx);
} else if (x < mx) {
printf("\e[%dC", mx - x);
}
printf("\e[90;103m"); /* black on yellow */
printf("\e[90;103ma ` a"); /* draw nose */
printf("\e[0m"); /* reset style */
printf("\e(B"); /* ascii mode */
/* draw corners */
printf("\e[H"); /* top left */
printf("A");
printf("\e[9979C"); /* rightmost */
printf("B");
printf("\e[9979;9979H"); /* bottom right corner */
printf("\e[C"); /* move right gets clamped */
printf("D"); /* write, set redzone flag */
printf("\e[2A"); /* move up, unsets redzone */
/* gnu screen now reports out of bounds position */
/* kitty hasnt got a redzone reporting next line */
printf("\e[6n");
read(0, b, sizeof(b));
sscanf(b, "\e[%d;%dR", &yn2, &xn2);
/* writes to (yn-3,xn-1) normally and (yn-2,0) in gnu screen */
printf("!");
/* draw ruler on top */
printf("\e[H");
for (i = 8; i + 1 < xn; i += 8) {
printf("\e[%dG%d", i + 1, i); /* set column */
}
printf("\e[9979;9979H"); /* bottom right */
printf("\e[9979D"); /* leftmost */
printf("C");
/* let's break gnu screen again with multimonospace redzone */
printf("\e[%d;9979H", yn / 2); /* right middle */
printf("\e[D"); /* left */
printf("");
printf("\e[6n");
read(0, b, sizeof(b));
sscanf(b, "\e[%d;%dR", &yn2, &xn2);
printf("\e[%dH", yn / 2);
printf("%d %d vs. %d %d\r\n", yn, xn, yn2, xn2);
printf("%d %d vs. %d %d\r\n", yn / 2, xn, yn2, xn2);
printf("\e#6double width\e#5\r\n");
printf("\e[3mthis text is so \e[1mitalic\e[0m\r\n");
printf("\e[1;20mpress any fraktur exit\e[0m");
printf("\a");
read(0, b, sizeof(b));
printf("\r\n");
ioctl(1, TCSETS, &oldterm);
return 0;
}

View File

@ -8,12 +8,14 @@
*/
#endif
#include "libc/calls/calls.h"
#include "libc/log/backtrace.h"
#include "libc/log/log.h"
#include "libc/mem/mem.h"
#include "libc/runtime/gc.h"
#include "libc/runtime/runtime.h"
#include "libc/runtime/symbols.h"
#include "libc/stdio/stdio.h"
#include "libc/sysv/consts/fileno.h"
/*
@ -45,9 +47,9 @@
void hello(void) {
gc(malloc(1));
backtrace(stdout);
ShowBacktrace(STDOUT_FILENO, NULL);
setenv("ADDR2LINE", "", true);
backtrace(stdout);
ShowBacktrace(STDOUT_FILENO, NULL);
}
void world(void) {

View File

@ -141,9 +141,9 @@ o/$(MODE)/examples/nesemu1.com.dbg: \
$(APE)
@$(APELINK)
o/$(MODE)/usr/share/dict/words: usr/share/dict/words.gz
usr/share/dict/words: usr/share/dict/words.gz
@$(MKDIR) $(dir $@)
@$(GZ) $(ZFLAGS) <$< >$@
@$(GZ) $(ZFLAGS) -d <$< >$@
o/$(MODE)/examples/ugh.ok: o/$(MODE)/examples/wut.com
$<

View File

@ -15,6 +15,8 @@
#include "libc/sysv/consts/o.h"
#include "third_party/duktape/duktape.h"
STATIC_YOINK("zip_uri_support");
static duk_ret_t NativePrint(duk_context *ctx) {
duk_push_string(ctx, " ");
duk_insert(ctx, 0);

View File

@ -22,6 +22,8 @@
#include "libc/sysv/consts/fileno.h"
#include "libc/x/x.h"
STATIC_YOINK("zip_uri_support");
/**
* @fileoverview Simple Interactive Spell Checker.
*

View File

@ -51,6 +51,7 @@ Contact: antirez@gmail.com\"\n\
*/
#define KILO_VERSION "0.0.1"
#define SYNTAX 1
#ifndef _BSD_SOURCE
#define _BSD_SOURCE
@ -169,14 +170,16 @@ void editorSetStatusMessage(const char *fmt, ...);
* There is no support to highlight patterns currently. */
/* C / C++ */
const char *const C_HL_extensions[] = {".c", ".cpp", NULL};
const char *const C_HL_extensions[] = {".c", ".h", ".cpp", NULL};
const char *const C_HL_keywords[] = {
/* A few C / C++ keywords */
"switch", "if", "while", "for", "break", "continue", "return", "else",
"struct", "union", "typedef", "static", "enum", "class",
/* C types */
"int|", "long|", "double|", "float|", "char|", "unsigned|", "signed|",
"void|", NULL};
"void|", "const|", "size_t|", "ssize_t|", "uint8_t|", "int8_t|",
"uint16_t|", "int16_t|", "uint32_t|", "int32_t|", "uint64_t|", "int64_t|",
NULL};
/* Here we define an array of syntax highlights by extensions, keywords,
* comments delimiters and flags. */
@ -201,7 +204,10 @@ void disableRawMode(int64_t fd) {
/* Called at exit to avoid remaining in raw mode. */
void editorAtExit(void) {
char buf[64];
disableRawMode(STDIN_FILENO);
write(STDOUT_FILENO, buf,
sprintf(buf, "\e[%d;%dH\r\n\r\n\r\n", E.screenrows, E.screencols));
}
/* Raw mode: 1960 magic shit. */
@ -530,8 +536,10 @@ void editorUpdateSyntax(erow *row) {
* state changed. This may recursively affect all the following rows
* in the file. */
int oc = editorRowHasOpenComment(row);
/* if (row->hl_oc != oc && row->idx + 1 < E.numrows) */
/* editorUpdateSyntax(&E.row[row->idx + 1]); */
#if SYNTAX
if (row->hl_oc != oc && row->idx + 1 < E.numrows)
editorUpdateSyntax(&E.row[row->idx + 1]);
#endif
row->hl_oc = oc;
}
@ -586,15 +594,17 @@ void editorUpdateRow(erow *row) {
* respecting tabs, substituting non printable characters with '?'. */
free(row->render);
for (j = 0; j < row->size; j++) {
if (row->chars[j] == CTRL('I')) tabs++;
if (row->chars[j] == '\t') tabs++;
}
row->render = malloc(row->size + tabs * 8 + nonprint * 9 + 1);
idx = 0;
for (j = 0; j < row->size; j++) {
if (row->chars[j] == CTRL('I')) {
if (row->chars[j] == '\t') {
row->render[idx++] = ' ';
while ((idx + 1) % 8 != 0) row->render[idx++] = ' ';
while (idx % 8 != 0) {
row->render[idx++] = ' ';
}
} else {
row->render[idx++] = row->chars[j];
}
@ -603,7 +613,9 @@ void editorUpdateRow(erow *row) {
row->render[idx] = '\0';
/* Update the syntax highlighting attributes of the row. */
/* editorUpdateSyntax(row); */
#if SYNTAX
editorUpdateSyntax(row);
#endif
}
/* Insert a row at the specified position, shifting the other rows on the bottom
@ -918,38 +930,46 @@ void editorRefreshScreen(void) {
r = &E.row[filerow];
int len = r->rsize - E.coloff;
/* int current_color = -1; */
#if SYNTAX
int current_color = -1;
#endif
if (len > 0) {
if (len > E.screencols) len = E.screencols;
char *c = r->render + E.coloff;
/* unsigned char *hl = r->hl + E.coloff; */
#if SYNTAX
unsigned char *hl = r->hl + E.coloff;
#endif
int j;
for (j = 0; j < len; j++) {
/* if (hl[j] == HL_NONPRINT) { */
/* char sym; */
/* abAppend(&ab, "\e[7m", 4); */
/* if (c[j] <= 26) */
/* sym = '@' + c[j]; */
/* else */
/* sym = '?'; */
/* abAppend(&ab, &sym, 1); */
/* abAppend(&ab, "\e[0m", 4); */
/* } else if (hl[j] == HL_NORMAL) { */
/* if (current_color != -1) { */
/* abAppend(&ab, "\e[39m", 5); */
/* current_color = -1; */
/* } */
abAppend(&ab, c + j, 1);
/* } else { */
/* int color = editorSyntaxToColor(hl[j]); */
/* if (color != current_color) { */
/* char buf_[16]; */
/* int clen = snprintf(buf_, sizeof(buf_), "\e[%dm", color); */
/* current_color = color; */
/* abAppend(&ab, buf_, clen); */
/* } */
/* abAppend(&ab, c + j, 1); */
/* } */
#if SYNTAX
if (hl[j] == HL_NONPRINT) {
char sym;
abAppend(&ab, "\e[7m", 4);
if (c[j] <= 26)
sym = '@' + c[j];
else
sym = '?';
abAppend(&ab, &sym, 1);
abAppend(&ab, "\e[0m", 4);
} else if (hl[j] == HL_NORMAL) {
if (current_color != -1) {
abAppend(&ab, "\e[39m", 5);
current_color = -1;
}
#endif
abAppend(&ab, c + j, 1);
#if SYNTAX
} else {
int color = editorSyntaxToColor(hl[j]);
if (color != current_color) {
char buf_[16];
int clen = snprintf(buf_, sizeof(buf_), "\e[%dm", color);
current_color = color;
abAppend(&ab, buf_, clen);
}
abAppend(&ab, c + j, 1);
}
#endif
}
}
abAppend(&ab, "\e[39m", 5);
@ -1200,8 +1220,8 @@ void editorMoveCursor(int key) {
void editorProcessKeypress(int64_t fd) {
/* When the file is modified, requires Ctrl-q to be pressed N times
* before actually quitting. */
int c, times;
static int quit_times;
int c, c2, times, oldcx;
c = editorReadKey(fd);
switch (c) {
@ -1226,16 +1246,16 @@ void editorProcessKeypress(int64_t fd) {
case CTRL('U'):
case CTRL('X'): {
int c2 = editorReadKey(fd);
c2 = editorReadKey(fd);
switch (c2) {
case CTRL('S'):
editorSave();
break;
case CTRL('C'):
/* Erase in Display (ED): Clear from cursor to end of screen. */
write(STDOUT_FILENO, "\r\e[0J", 5);
case CTRL('C'): {
/* write(STDOUT_FILENO, "\r\e[0J", 5); */
exit(0);
break;
}
default: /* ignore */
break;
}
@ -1254,7 +1274,7 @@ void editorProcessKeypress(int64_t fd) {
break;
case CTRL('K'): { /* Kill Line */
int oldcx = E.cx;
oldcx = E.cx;
do {
editorMoveCursor(ARROW_RIGHT);
} while (E.cx);
@ -1294,7 +1314,7 @@ void editorProcessKeypress(int64_t fd) {
case HOME_KEY:
case CTRL('A'):
while (E.cx) editorMoveCursor(ARROW_LEFT);
while (E.cx || E.coloff) editorMoveCursor(ARROW_LEFT);
break;
case END_KEY:
case CTRL('E'):

View File

@ -72,5 +72,9 @@ int main(int argc, char *argv[], char **envp) {
fmt),
kAuxiliaryValues[i].name, key, val, kAuxiliaryValues[i].description);
}
printf("\nSpecial Directories:\n");
printf(" ☼ kTmpPath = %`'s\n", kTmpPath);
printf(" ☼ kNtSystemDirectory = %`'s\n", kNtSystemDirectory);
printf(" ☼ kNtWindowsDirectory = %`'s\n", kNtWindowsDirectory);
return 0;
}

View File

@ -9,16 +9,18 @@
#endif
#include "libc/calls/calls.h"
#include "libc/elf/elf.h"
#include "libc/log/backtrace.h"
#include "libc/log/log.h"
#include "libc/runtime/symbols.h"
#include "libc/stdio/stdio.h"
#include "libc/sysv/consts/fileno.h"
int main(int argc, char *argv[]) {
int rc = 0;
char *filename;
struct SymbolTable *tab = NULL;
if ((filename = finddebugbinary()) != NULL &&
(tab = opensymboltable(filename))) {
if ((filename = FindDebugBinary()) != NULL &&
(tab = OpenSymbolTable(filename))) {
for (unsigned i = 0; i < tab->count; ++i) {
printf("%p %s\n", tab->addr_base + tab->symbols[i].addr_rva,
getelfstring(tab->elf, tab->elfsize, tab->name_base,
@ -26,9 +28,9 @@ int main(int argc, char *argv[]) {
}
} else {
perror("printmysymbols");
backtrace(stderr);
ShowBacktrace(STDERR_FILENO, NULL);
rc = 1;
}
closesymboltable(&tab);
CloseSymbolTable(&tab);
return rc;
}

View File

@ -58,6 +58,3 @@ _start: mov $12,%rdx # arg no. 3 is length
jmp 0b
.endfn _start,globl
.source __FILE__
.rodata.cst4
1: .float -1.5

View File

@ -16,7 +16,6 @@
#include "libc/fmt/fmt.h"
#include "libc/log/check.h"
#include "libc/log/log.h"
#include "libc/runtime/gc.h"
#include "libc/runtime/runtime.h"
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"
@ -27,82 +26,138 @@
#include "libc/sysv/consts/termios.h"
#include "libc/x/x.h"
/*
"\e[c" "\e[?1;2c"
"\e[x" "\e[2;1;1;112;112;1;0x"
"\e[>c" "\e[>83;40500;0c"
"\e[6n" "\e[52;1R"
*/
#define CTRL(C) ((C) ^ 0b01000000)
#define ENABLE_MOUSE_TRACKING "\e[?1000;1002;1015;1006h"
#define DISABLE_MOUSE_TRACKING "\e[?1000;1002;1015;1006l"
#define PROBE_DISPLAY_SIZE "\e7\e[9979;9979H\e[6n\e8"
#define CTRL(C) ((C) ^ 0b01000000)
#define PROBE_VT100 "\e[c" /* e.g. "\e[?1;2c", "\e[>0c" */
#define PROBE_SECONDARY "\e[>c" /* "\e[>83;40500;0c" (Screen v4.05.00) */
#define PROBE_PARAMETERS "\e[x" /* e.g. "\e[2;1;1;112;112;1;0x" */
#define PROBE_CURSOR_POSITION "\e[6n" /* e.g. "\e[𝑦;𝑥R" */
#define PROBE_SUN_DTTERM_SIZE "\e[14t" /* e.g. "\e[𝑦;𝑥R" */
char code[512];
struct winsize wsize;
struct termios oldterm;
volatile bool resized, killed;
int fd_;
jmp_buf jb_;
ssize_t got_;
uint8_t buf_[128];
struct TtyIdent ti_;
struct winsize wsize_;
volatile bool resized_;
void OnResize(void) {
resized_ = true;
}
void OnKilled(int sig) {
longjmp(jb_, 128 + sig + 1);
void onresize(void) {
resized = true;
}
void getsome(void) {
if ((got_ = read(fd_, buf_, sizeof(buf_))) == -1 && errno != EINTR) {
printf("%s%s\r\n", "error: ", strerror(errno));
longjmp(jb_, EXIT_FAILURE + 1);
void onkilled(int sig) {
killed = true;
}
void restoretty(void) {
write(1, DISABLE_MOUSE_TRACKING, strlen(DISABLE_MOUSE_TRACKING));
ioctl(1, TCSETS, &oldterm);
}
int rawmode(void) {
static bool once;
struct termios t;
if (!once) {
if (ioctl(1, TCGETS, &oldterm) != -1) {
atexit(restoretty);
} else {
return -1;
}
once = true;
}
if (got_ >= 0) {
printf("%`'.*s\r\n", got_, buf_);
if (got_ > 0 && buf_[0] == CTRL('C')) {
longjmp(jb_, EXIT_SUCCESS + 1);
memcpy(&t, &oldterm, sizeof(t));
t.c_cc[VMIN] = 1;
t.c_cc[VTIME] = 1;
t.c_iflag &= ~(INPCK | ISTRIP | PARMRK | INLCR | IGNCR | ICRNL | IXON |
IGNBRK | BRKINT);
t.c_lflag &= ~(IEXTEN | ICANON | ECHO | ECHONL | ISIG);
t.c_cflag &= ~(CSIZE | PARENB);
t.c_oflag &= ~OPOST;
t.c_cflag |= CS8;
t.c_iflag |= IUTF8;
ioctl(1, TCSETS, &t);
write(1, ENABLE_MOUSE_TRACKING, strlen(ENABLE_MOUSE_TRACKING));
write(1, PROBE_DISPLAY_SIZE, strlen(PROBE_DISPLAY_SIZE));
return 0;
}
void getsize(void) {
if (getttysize(1, &wsize) != -1) {
printf("termios says terminal size is %hu×%hu\r\n", wsize.ws_col,
wsize.ws_row);
} else {
printf("%s\n", strerror(errno));
}
}
const char *describemouseevent(int e) {
static char buf[64];
buf[0] = 0;
if (e & 0x10) {
strcat(buf, " ctrl");
}
if (e & 0x40) {
strcat(buf, " wheel");
if (e & 0x01) {
strcat(buf, " down");
} else {
strcat(buf, " up");
}
} else {
switch (e & 3) {
case 0:
strcat(buf, " left");
break;
case 1:
strcat(buf, " middle");
break;
case 2:
strcat(buf, " right");
break;
default:
unreachable;
}
if (e & 0x20) {
strcat(buf, " drag");
} else if (e & 0x04) {
strcat(buf, " up");
} else {
strcat(buf, " down");
}
}
if (resized_) {
CHECK_NE(-1, getttysize(fd_, &wsize_));
printf("SIGWINCH → %hu×%hu\r\n", wsize_.ws_row, wsize_.ws_col);
resized_ = false;
}
}
void probe(const char *s) {
printf("%`'s → ", s);
write(fd_, s, strlen(s));
getsome();
return buf + 1;
}
int main(int argc, char *argv[]) {
int rc;
char ttyname[64];
struct termios old;
CHECK_NE(-1, (fd_ = open("/dev/tty", O_RDWR)));
CHECK_NE(-1, ttyconfig(fd_, ttysetrawmode, 0, &old));
if (!(rc = setjmp(jb_))) {
xsigaction(SIGTERM, OnKilled, 0, 0, NULL);
xsigaction(SIGWINCH, OnResize, 0, 0, NULL);
if (ttyident(&ti_, STDIN_FILENO, STDOUT_FILENO) != -1) {
ttysendtitle(fd_, "justine was here", &ti_);
fputs(ttydescribe(ttyname, sizeof(ttyname), &ti_), stdout);
int e, c, y, x, n, yn, xn;
xsigaction(SIGTERM, onkilled, 0, 0, NULL);
xsigaction(SIGWINCH, onresize, 0, 0, NULL);
xsigaction(SIGCONT, onresize, 0, 0, NULL);
rawmode();
getsize();
while (!killed) {
if (resized) {
printf("SIGWINCH ");
getsize();
resized = false;
}
if ((n = readansi(0, code, sizeof(code))) == -1) {
if (errno == EINTR) continue;
printf("ERROR: READ: %s\r\n", strerror(errno));
exit(1);
}
printf("%`'.*s ", n, code);
if (iscntrl(code[0]) && !code[1]) {
printf("is CTRL-%c a.k.a. ^%c\r\n", CTRL(code[0]), CTRL(code[0]));
if (code[0] == CTRL('C') || code[0] == CTRL('D')) break;
} else if (startswith(code, "\e[") && endswith(code, "R")) {
yn = 1, xn = 1;
sscanf(code, "\e[%d;%dR", &yn, &xn);
printf("inband signalling says terminal size is %d×%d\r\n", xn, yn);
} else if (startswith(code, "\e[<") &&
(endswith(code, "m") || endswith(code, "M"))) {
e = 0, y = 1, x = 1;
sscanf(code, "\e[<%d;%d;%d%c", &e, &y, &x, &c);
printf("mouse %s at %d×%d\r\n", describemouseevent(e | (c == 'm') << 2),
x, y);
} else {
printf("\r\n");
}
fputs("\r\n", stdout);
probe(PROBE_VT100);
probe(PROBE_SECONDARY);
probe(PROBE_PARAMETERS);
probe(PROBE_CURSOR_POSITION);
/* probe(PROBE_SUN_DTTERM_SIZE); */
getsome();
for (;;) getsome();
}
ttyrestore(fd_, &old);
ttyidentclear(&ti_);
return rc - 1;
return 0;
}

View File

@ -1,5 +1,6 @@
#ifndef COSMOPOLITAN_LIBC_CALLS_SYSCALLS_H_
#define COSMOPOLITAN_LIBC_CALLS_SYSCALLS_H_
#include "libc/calls/struct/sigaction.h"
#include "libc/calls/struct/timespec.h"
#include "libc/calls/typedef/sighandler_t.h"
#include "libc/dce.h"
@ -58,12 +59,10 @@ struct iovec;
struct rlimit;
struct rusage;
struct sigaction;
struct siginfo;
struct sigset;
struct stat;
struct sysinfo;
struct tms;
struct ucontext;
struct utsname;
typedef int sig_atomic_t;
@ -210,6 +209,7 @@ size_t getfiledescriptorsize(int);
ssize_t copy_file_range(int, long *, int, long *, size_t, uint32_t);
ssize_t copyfd(int, int64_t *, int, int64_t *, size_t, uint32_t);
ssize_t read(int, void *, size_t);
ssize_t readansi(int, char *, size_t);
ssize_t readlinkat(int, const char *, char *, size_t);
ssize_t splice(int, int64_t *, int, int64_t *, size_t, uint32_t);
ssize_t vmsplice(int, const struct iovec *, int64_t, uint32_t);

View File

@ -17,9 +17,10 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/calls/calls.h"
#include "libc/calls/internal.h"
#include "libc/dce.h"
#include "libc/sysv/errfuns.h"
#include "libc/time/time.h"
/**
* Retrieves last setitimer() value, correcting for remaining time.
@ -29,9 +30,8 @@
*/
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! */
return setitimer$nt(which, NULL, curvalue);
}
}

View File

@ -45,7 +45,7 @@ static int mkntcmdline_append(char16_t **p, size_t *i, size_t *n, char16_t c) {
* Converts System V argv to Windows-style command line.
*
* Escaping is performed and it's designed to round-trip with
* getdosargv() or getdosargv(). This function does NOT escape
* GetDosArgv() or GetDosArgv(). This function does NOT escape
* command interpreter syntax, e.g. $VAR (sh), %VAR% (cmd).
*
* @param argv is an a NULL-terminated array of UTF-8 strings

View File

@ -181,6 +181,7 @@ i64 sendfile$sysv(i32, i32, i64 *, u64) hidden;
i64 splice$sysv(i32, i64 *, i32, i64 *, u64, u32) hidden;
i64 vmsplice$sysv(i32, const struct iovec *, i64, u32) hidden;
i64 write$sysv(i32, const void *, u64) hidden;
int getitimer$sysv(i32, struct itimerval *) hidden;
int setresgid$sysv(uint32_t, uint32_t, uint32_t) hidden;
int setresuid$sysv(uint32_t, uint32_t, uint32_t) hidden;
u32 getgid$sysv(void) hidden;
@ -204,6 +205,7 @@ u32 prot2nt(i32, i32) privileged;
void __restore_rt() hidden;
void __sigenter$xnu(void *, i32, i32, void *, void *) hidden noreturn;
int utimensat$xnu(int, const char *, const struct timespec *, int) hidden;
int nanosleep$xnu(const struct timespec *, struct timespec *) hidden;
void stat2linux(void *) hidden;
void xnutrampoline(void *, i32, i32, const struct __darwin_siginfo *,
const struct __darwin_ucontext *) hidden noreturn;
@ -247,6 +249,8 @@ ssize_t read$nt(struct Fd *, const struct iovec *, size_t, ssize_t) hidden;
ssize_t write$nt(struct Fd *, const struct iovec *, size_t, ssize_t) hidden;
int utimensat$nt(int, const char *, const struct timespec *, int) hidden;
int getrusage$nt(int, struct rusage *) hidden;
int setitimer$nt(int, const struct itimerval *, struct itimerval *) hidden;
int nanosleep$nt(const struct timespec *, struct timespec *) hidden;
/*───────────────────────────────────────────────────────────────────────────│─╗
cosmopolitan § syscalls » windows nt » support
@ -257,6 +261,7 @@ void ntcontext2linux(struct ucontext *, const struct NtContext *) hidden;
struct NtOverlapped *offset2overlap(int64_t, struct NtOverlapped *) hidden;
bool32 ntsetprivilege(i64, const char16_t *, u32) hidden;
bool32 onntconsoleevent$nt(u32) hidden;
void onntalarm(void *, uint32_t, uint32_t) hidden;
int ntaccesscheck(const char16_t *, u32) paramsnonnull() hidden;
i64 ntreturn(u32);
i64 winerr(void) nocallback privileged;

View File

@ -41,15 +41,23 @@ textwindows int ioctl$tcsets$nt(int ignored, uint64_t request,
}
inmode &=
~(kNtEnableLineInput | kNtEnableEchoInput | kNtEnableProcessedInput);
if (tio->c_lflag & ICANON) inmode |= kNtEnableLineInput;
if (tio->c_lflag & ECHO) inmode |= kNtEnableEchoInput;
if (tio->c_lflag & (IEXTEN | ISIG)) inmode |= kNtEnableProcessedInput;
inmode |= kNtEnableWindowInput;
if (NtGetVersion() >= kNtVersionWindows10) {
inmode |= kNtEnableVirtualTerminalInput;
}
SetConsoleMode(in, inmode);
}
if (outok) {
SetConsoleMode(out, outmode | kNtEnableProcessedOutput |
(NtGetVersion() >= kNtVersionWindows10
? kNtEnableVirtualTerminalProcessing
: 0));
outmode |= kNtEnableWrapAtEolOutput;
outmode |= kNtEnableProcessedOutput;
if (!(tio->c_oflag & OPOST)) outmode |= kNtDisableNewlineAutoReturn;
if (NtGetVersion() >= kNtVersionWindows10) {
outmode |= kNtEnableVirtualTerminalProcessing;
}
SetConsoleMode(out, outmode);
}
return 0;
} else {

View File

@ -28,25 +28,36 @@
#include "libc/sysv/errfuns.h"
textwindows int ioctl$tiocgwinsz$nt(int fd, struct winsize *ws) {
int i, fds[3];
uint32_t mode;
struct NtConsoleScreenBufferInfoEx sbinfo;
if (!isfdkind(fd, kFdFile)) return ebadf();
if (!GetConsoleMode(g_fds.p[fd].handle, &mode)) return enotty();
memset(&sbinfo, 0, sizeof(sbinfo));
sbinfo.cbSize = sizeof(sbinfo);
if (GetConsoleScreenBufferInfoEx(g_fds.p[fd].handle, &sbinfo)) {
ws->ws_col = sbinfo.srWindow.Right - sbinfo.srWindow.Left;
ws->ws_row = sbinfo.srWindow.Bottom - sbinfo.srWindow.Top;
ws->ws_xpixel = 0;
ws->ws_ypixel = 0;
return 0;
} else if (g_ntstartupinfo.dwFlags & kNtStartfUsecountchars) {
ws->ws_col = g_ntstartupinfo.dwXCountChars;
ws->ws_row = g_ntstartupinfo.dwYCountChars;
ws->ws_xpixel = 0;
ws->ws_ypixel = 0;
return 0;
} else {
return winerr();
fds[0] = fd, fds[1] = 1, fds[2] = 0;
for (i = 0; i < ARRAYLEN(fds); ++i) {
if (isfdkind(fds[i], kFdFile) || isfdkind(fds[i], kFdConsole)) {
if (GetConsoleMode(g_fds.p[fds[i]].handle, &mode)) {
memset(&sbinfo, 0, sizeof(sbinfo));
sbinfo.cbSize = sizeof(sbinfo);
if (GetConsoleScreenBufferInfoEx(g_fds.p[fds[i]].handle, &sbinfo)) {
ws->ws_col = sbinfo.srWindow.Right - sbinfo.srWindow.Left + 1;
ws->ws_row = sbinfo.srWindow.Bottom - sbinfo.srWindow.Top + 1;
ws->ws_xpixel = 0;
ws->ws_ypixel = 0;
return 0;
} else if (g_ntstartupinfo.dwFlags & kNtStartfUsecountchars) {
ws->ws_col = g_ntstartupinfo.dwXCountChars;
ws->ws_row = g_ntstartupinfo.dwYCountChars;
ws->ws_xpixel = 0;
ws->ws_ypixel = 0;
return 0;
} else {
winerr();
}
} else {
enotty();
}
} else {
ebadf();
}
}
return -1;
}

View File

@ -0,0 +1,44 @@
/*-*- 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/nexgen32e/nexgen32e.h"
#include "libc/nt/enum/status.h"
#include "libc/nt/errors.h"
#include "libc/nt/nt/time.h"
#include "libc/nt/synchronization.h"
#include "libc/str/str.h"
#include "libc/sysv/errfuns.h"
textwindows int nanosleep$nt(const struct timespec *req, struct timespec *rem) {
int64_t millis, hectonanos, relasleep;
if (rem) memcpy(rem, req, sizeof(*rem));
hectonanos = req->tv_sec * 10000000ull + div100int64(req->tv_nsec);
hectonanos = MAX(1, hectonanos);
relasleep = -hectonanos;
if (NtError(NtDelayExecution(true, &relasleep))) {
millis = div10000int64(hectonanos);
millis = MAX(1, millis);
if (SleepEx(millis, true) == kNtWaitIoCompletion) {
return eintr();
}
}
if (rem) memset(rem, 0, sizeof(*rem));
return 0;
}

View File

@ -17,16 +17,15 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/elf/elf.h"
#include "libc/runtime/symbols.h"
#include "libc/calls/calls.h"
#include "libc/calls/struct/timeval.h"
#include "libc/macros.h"
#include "libc/nexgen32e/nexgen32e.h"
#include "libc/sock/internal.h"
/**
* Finds starting address of symbol.
* @param symbol table pointer (null propagating)
* @return name string or null if not found
*/
const char *getsymbolname(struct SymbolTable *t, const struct Symbol *symbol) {
return (t && symbol)
? getelfstring(t->elf, t->elfsize, t->name_base, symbol->name_rva)
: NULL;
int nanosleep$xnu(const struct timespec *req, struct timespec *rem) {
long millis;
millis = div1000int64(req->tv_nsec);
millis = MAX(1, millis);
return select$sysv(0, 0, 0, 0, &(struct timeval){req->tv_sec, millis});
}

View File

@ -17,48 +17,23 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/calls/calls.h"
#include "libc/calls/internal.h"
#include "libc/calls/struct/timespec.h"
#include "libc/calls/struct/timeval.h"
#include "libc/conv/conv.h"
#include "libc/dce.h"
#include "libc/macros.h"
#include "libc/nexgen32e/nexgen32e.h"
#include "libc/nt/enum/status.h"
#include "libc/nt/errors.h"
#include "libc/nt/nt/time.h"
#include "libc/nt/synchronization.h"
#include "libc/sock/internal.h"
#include "libc/str/str.h"
#include "libc/sysv/errfuns.h"
/**
* Sleeps for a particular amount of time.
*/
int nanosleep(const struct timespec *req, struct timespec *rem) {
long res, millis, hectonanos;
if (!req) return efault();
if (!IsWindows()) {
if (!IsXnu()) {
return nanosleep$sysv(req, rem);
} else {
return select$sysv(
0, 0, 0, 0, /* lool */
&(struct timeval){req->tv_sec, div1000int64(req->tv_nsec)});
return nanosleep$xnu(req, rem);
}
} else {
if (rem) memcpy(rem, req, sizeof(*rem));
if (req->tv_sec && req->tv_nsec) {
hectonanos = MAX(1, req->tv_sec * 10000000L + div100int64(req->tv_nsec));
} else {
hectonanos = 1;
}
if (NtError(NtDelayExecution(true, &hectonanos))) {
millis = div10000int64(hectonanos);
res = SleepEx(millis, true);
if (res == kNtWaitIoCompletion) return eintr();
}
if (rem) memset(rem, 0, sizeof(*rem));
return 0;
return nanosleep$nt(req, rem);
}
}

View File

@ -31,13 +31,7 @@ static struct Now {
bool once;
uint64_t k0;
long double r0, cpn;
} now_;
/**
* Returns timestamp without needing system calls.
* @note uses microsecond scale fallback on k8 or vm
*/
long double (*nowl)(void);
} g_now;
static long double GetTimeSample(void) {
uint64_t tick1, tick2;
@ -48,7 +42,7 @@ static long double GetTimeSample(void) {
nanosleep(&(struct timespec){0, 100000}, NULL);
time2 = dtime(CLOCK_MONOTONIC);
tick2 = rdtsc();
return (time2 - time1) * 1e9 / (tick2 - tick1);
return (time2 - time1) * 1e9 / MAX(1, tick2 - tick1);
}
static long double MeasureNanosPerCycle(void) {
@ -62,15 +56,15 @@ static long double MeasureNanosPerCycle(void) {
}
static void InitTime(void) {
now_.cpn = MeasureNanosPerCycle();
now_.r0 = dtime(CLOCK_REALTIME);
now_.k0 = rdtsc();
now_.once = true;
g_now.cpn = MeasureNanosPerCycle();
g_now.r0 = dtime(CLOCK_REALTIME);
g_now.k0 = rdtsc();
g_now.once = true;
}
long double converttickstonanos(uint64_t ticks) {
if (!now_.once) InitTime();
return ticks * now_.cpn; /* pico scale */
if (!g_now.once) InitTime();
return ticks * g_now.cpn; /* pico scale */
}
long double converttickstoseconds(uint64_t ticks) {
@ -83,15 +77,7 @@ long double nowl$sys(void) {
long double nowl$art(void) {
uint64_t ticks;
if (!now_.once) InitTime();
ticks = unsignedsubtract(rdtsc(), now_.k0);
return now_.r0 + converttickstoseconds(ticks);
if (!g_now.once) InitTime();
ticks = unsignedsubtract(rdtsc(), g_now.k0);
return g_now.r0 + converttickstoseconds(ticks);
}
INITIALIZER(301, _init_nowl, {
if (X86_HAVE(INVTSC)) {
nowl = nowl$art;
} else {
nowl = nowl$sys;
}
})

View File

@ -17,25 +17,23 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/nexgen32e/x86feature.h"
#include "libc/macros.h"
/ Checks for printable characters that aren't space / alnum.
/ Returns timestamp without needing system calls.
/
/ @param edi is character to test (treated as uint8_t)
/ @return nonzero if character matches, otherwise 0
ispunct:.leafprologue
.profilable
xor %eax,%eax
cmp $255,%edi
ja 1f
movslq %edi,%rdi
ezlea kCtype,cx
mov (%rcx,%rdi),%dl
test $16,%dl
je 1f
xor %eax,%eax
and $7,%dl
sete %al
1: .leafepilogue
.endfn ispunct,globl
/ @return seconds since unix epoch in %st0
/ @note uses microsecond scale fallback on k8 or vm
.initbss 202,_init_nowl
nowl: .quad 0
.endobj nowl,globl
.previous
.init.start 202,_init_nowl
ezlea nowl$sys,ax
ezlea nowl$art,cx
testb X86_HAVE(INVTSC)+kCpuids(%rip)
cmovnz %rcx,%rax
stosq
.init.end 202,_init_nowl
.source __FILE__

View File

@ -0,0 +1,31 @@
/*-*- 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/siginfo.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/sig.h"
void onntalarm(void *lpArgToCompletionRoutine, uint32_t dwTimerLowValue,
uint32_t dwTimerHighValue) {
siginfo_t info;
memset(&info, 0, sizeof(info));
info.si_signo = SIGALRM;
__sigenter(info.si_signo, &info, NULL);
}

View File

@ -37,9 +37,9 @@ textwindows bool32 onntconsoleevent(uint32_t CtrlType) {
case kNtCtrlCloseEvent:
sig = pushpop(SIGHUP);
break;
case kNtCtrlLogoffEvent:
case kNtCtrlShutdownEvent:
sig = pushpop(SIGTERM);
case kNtCtrlLogoffEvent: // only received by services so hack hack hack
case kNtCtrlShutdownEvent: // only received by services so hack hack hack
sig = pushpop(SIGALRM);
break;
default:
return false;

View File

@ -21,6 +21,7 @@
#include "libc/bits/weaken.h"
#include "libc/calls/calls.h"
#include "libc/calls/internal.h"
#include "libc/calls/struct/iovec.h"
#include "libc/dce.h"
#include "libc/macros.h"
#include "libc/runtime/runtime.h"
@ -47,7 +48,7 @@ ssize_t pread(int fd, void *buf, size_t size, int64_t offset) {
} else if (!IsWindows()) {
rc = pread$sysv(fd, buf, size, offset);
} else if (isfdkind(fd, kFdFile)) {
rc = read$nt(&g_fds.p[fd], buf, size, offset);
rc = read$nt(&g_fds.p[fd], (struct iovec[]){{buf, size}}, 1, offset);
} else {
rc = ebadf();
}

View File

@ -48,7 +48,7 @@ ssize_t preadv(int fd, struct iovec *iovec, int count, int64_t off) {
*/
if (!once) {
once = true;
if (IsLinux() && iovec->iov_len >= __NR_preadv_linux) {
if (IsModeDbg() || (IsLinux() && iovec->iov_len >= __NR_preadv_linux)) {
/*
* Read size is too large to detect older kernels safely without
* introducing nontrivial mechanics. We'll try again later.

View File

@ -29,7 +29,12 @@
#define __NR_pwritev_linux 0x0128
/**
* Writes data from multiple buffers to file descriptor at offset.
* Writes data from multiple buffers to offset.
*
* Please note that it's not an error for a short write to happen. This
* can happen in the kernel if EINTR happens after some of the write has
* been committed. It can also happen if we need to polyfill this system
* call using pwrite().
*
* @param count is recommended to be 16 or fewer; if it exceeds IOV_MAX
* then the extra buffers are simply ignored
@ -48,7 +53,7 @@ ssize_t pwritev(int fd, const struct iovec *iovec, int count, int64_t off) {
*/
if (!once) {
once = true;
if (IsLinux() && iovec->iov_len >= __NR_pwritev_linux) {
if (IsModeDbg() || (IsLinux() && iovec->iov_len >= __NR_pwritev_linux)) {
/*
* Write size is too large to detect older kernels safely without
* introducing nontrivial mechanics. We'll try again later.

View File

@ -0,0 +1,124 @@
/*-*- 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/calls.h"
#include "libc/str/thompike.h"
#include "libc/sysv/errfuns.h"
/**
* Reads single keystroke or control sequence from character device.
*
* When reading ANSI UTF-8 text streams, characters and control codes
* are oftentimes encoded as multi-byte sequences. This function knows
* how long each sequence is, so that each read consumes a single thing
* from the underlying file descriptor, e.g.
*
* "a" ALFA
* "\316\261" ALPHA
* "\033[A" CURSOR UP
* "\033[38;5;202m" ORANGERED
* "\eOP" PF1
*
* This routine generalizes to ascii, utf-8, chorded modifier keys,
* function keys, color codes, c0/c1 control codes, cursor movement,
* mouse movement, etc.
*
* Userspace buffering isn't required, since ANSI escape sequences and
* UTF-8 are decoded without peeking. Noncanonical overlong encodings
* can cause the stream to go out of sync. This function recovers such
* events by ignoring continuation bytes at the beginning of each read.
*
* String control sequences, e.g. "\e_hello\e\\" currently are not
* tokenized as a single read. Lastly note, this function has limited
* support for UNICODE representations of C0/C1 control codes, e.g.
*
* "\000" NUL
* "\300\200" NUL
* "\302\233A" CURSOR UP
*
* @param buf is guaranteed to receive a NUL terminator if size>0
* @return number of bytes read (helps differentiate "\0" vs. "")
* @see examples/ttyinfo.c
* @see ANSI X3.64-1979
* @see ISO/IEC 6429
* @see FIPS-86
* @see ECMA-48
*/
ssize_t readansi(int fd, char *buf, size_t size) {
int i, j;
uint8_t c;
enum { kAscii, kUtf8, kEsc, kCsi, kSs } t;
if (size) buf[0] = 0;
for (j = i = 0, t = kAscii;;) {
if (i + 2 >= size) return enomem();
if (read(fd, &c, 1) != 1) return -1;
buf[i++] = c;
buf[i] = 0;
switch (t) {
case kAscii:
if (c < 0200) {
if (c == '\e') {
t = kEsc;
} else {
return i;
}
} else if (c >= 0300) {
t = kUtf8;
j = ThomPikeLen(c) - 1;
}
break;
case kUtf8:
if (!--j) return i;
break;
case kEsc:
switch (c) {
case '[':
t = kCsi;
break;
case 'N':
case 'O':
t = kSs;
break;
case 0x20 ... 0x2F:
break;
default:
return i;
}
break;
case kCsi:
switch (c) {
case ':':
case ';':
case '<':
case '=':
case '>':
case '?':
case '0' ... '9':
break;
default:
return i;
}
break;
case kSs:
return i;
default:
unreachable;
}
}
}

View File

@ -0,0 +1,104 @@
/*-*- 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/calls.h"
#include "libc/calls/internal.h"
#include "libc/calls/struct/itimerval.h"
#include "libc/conv/conv.h"
#include "libc/errno.h"
#include "libc/log/check.h"
#include "libc/nexgen32e/nexgen32e.h"
#include "libc/nt/files.h"
#include "libc/nt/runtime.h"
#include "libc/nt/synchronization.h"
#include "libc/nt/thread.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/itimer.h"
#include "libc/sysv/errfuns.h"
/**
* @fileoverview Heartbreaking polyfill for SIGALRM on NT.
*
* Threads are used to trigger the SIGALRM handler, which should
* hopefully be an unfancy function like this:
*
* void OnAlarm(int sig, struct siginfo *si, struct ucontext *uc) {
* g_alarmed = true;
* }
*
* This is needed because WIN32 provides no obvious solutions for
* interrupting i/o operations on the standard input handle.
*/
static struct ItimerNt {
int64_t ith;
uint32_t tid;
struct itimerval itv;
} g_itimernt;
static uint32_t ItimerWorker(void *arg) {
do {
if (!WaitForSingleObject(g_itimernt.ith, -1)) {
onntalarm(NULL, 0, 0);
}
} while (g_itimernt.ith && g_itimernt.tid == GetCurrentThreadId());
return 0;
}
textwindows int setitimer$nt(int which, const struct itimerval *newvalue,
struct itimerval *out_opt_oldvalue) {
int32_t period;
int64_t ith, duetime;
if (which != ITIMER_REAL) return einval();
if (newvalue) {
if (newvalue->it_value.tv_sec && newvalue->it_value.tv_usec) {
if (!(ith = CreateWaitableTimer(NULL, false, NULL))) {
return winerr();
}
duetime = -(newvalue->it_value.tv_sec * HECTONANOSECONDS +
newvalue->it_value.tv_usec * 10);
period = newvalue->it_value.tv_sec * 1000 +
div1000int64(newvalue->it_value.tv_usec);
if (!period && newvalue->it_value.tv_usec) period = 1;
if (!SetWaitableTimer(ith, &duetime, period, NULL, NULL, false)) {
errno = GetLastError();
CloseHandle(ith);
return -1;
}
} else {
ith = 0;
}
if (g_itimernt.ith) {
CloseHandle(g_itimernt.ith);
g_itimernt.ith = 0;
}
} else {
ith = 0;
}
if (out_opt_oldvalue) {
memcpy(out_opt_oldvalue, &g_itimernt.itv, sizeof(struct itimerval));
}
if (ith) {
g_itimernt.ith = ith;
memcpy(&g_itimernt.itv, newvalue, sizeof(struct itimerval));
CloseHandle(
CreateThread(NULL, STACKSIZE, ItimerWorker, NULL, 0, &g_itimernt.tid));
}
return 0;
}

View File

@ -24,7 +24,18 @@
#include "libc/time/time.h"
/**
* Schedules delivery of one-shot or intermittent wakeup signal, e.g.
* Schedules delivery of one-shot or intermittent interrupt signal, e.g.
*
* Raise SIGALRM every 1.5s:
*
* CHECK_NE(-1, sigaction(SIGALRM,
* &(struct sigaction){.sa_sigaction = missingno},
* NULL));
* CHECK_NE(-1, setitimer(ITIMER_REAL,
* &(const struct itimerval){{1, 500000}, {1, 500000}},
* NULL));
*
* Set single-shot 50ms timer callback to interrupt laggy connect():
*
* CHECK_NE(-1, sigaction(SIGALRM,
* &(struct sigaction){.sa_sigaction = missingno,
@ -34,21 +45,29 @@
* &(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));
*
* Disarm timer:
*
* CHECK_NE(-1, setitimer(ITIMER_REAL, &(const struct itimerval){0}, NULL));
*
* Be sure to check for EINTR on your i/o calls, for best low latency.
*
* @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
* duration ({0,0} means disarm) in microseconds [0,999999] and
* if this parameter is NULL, we'll polyfill getitimer() behavior
* @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);
if (newvalue) {
return setitimer$sysv(which, newvalue, out_opt_oldvalue);
} else {
return getitimer$sysv(which, out_opt_oldvalue);
}
} else {
return enosys(); /* TODO(jart): Implement me! */
return setitimer$nt(which, newvalue, out_opt_oldvalue);
}
}

View File

@ -3,7 +3,7 @@
#include "libc/calls/struct/timespec.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
struct stat {
struct stat { /* linux abi */
int64_t st_dev; /* 0: id of device with file */
int64_t st_ino; /* 8: inode number in disk b-tree */
int64_t st_nlink; /* 16: hard link count */

View File

@ -18,14 +18,10 @@
02110-1301 USA
*/
#include "libc/macros.h"
.real
.text.windows
.source __FILE__
.code16 # .code32 .code64
__unprovable:
push %bp
mov %sp,%bp
.softicebp
pop %bp
ret
.endfn __unprovable,globl,hidden
onntalarm$nt:
ezlea onntalarm,ax
jmp nt2sysv
.endfn onntalarm$nt,globl,hidden

View File

@ -1,10 +1,9 @@
#ifndef COSMOPOLITAN_LIBC_CALLS_TYPEDEF_SIGACTION_F_H_
#define COSMOPOLITAN_LIBC_CALLS_TYPEDEF_SIGACTION_F_H_
#include "libc/calls/struct/siginfo.h"
#include "libc/calls/ucontext.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
struct siginfo;
struct ucontext;
typedef void (*sigaction_f)(int, struct siginfo *, struct ucontext *);
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */

View File

@ -1,6 +1,5 @@
#ifndef COSMOPOLITAN_LIBC_CALLS_UCONTEXT_H_
#define COSMOPOLITAN_LIBC_CALLS_UCONTEXT_H_
#include "libc/calls/calls.h"
#include "libc/calls/struct/sigaltstack.h"
#include "libc/calls/struct/sigset.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)

View File

@ -27,6 +27,11 @@
/**
* Writes data from multiple buffers.
*
* Please note that it's not an error for a short write to happen. This
* can happen in the kernel if EINTR happens after some of the write has
* been committed. It can also happen if we need to polyfill this system
* call using write().
*
* @return number of bytes actually handed off, or -1 w/ errno
*/
ssize_t writev(int fd, const struct iovec *iov, int iovlen) {

View File

@ -27,9 +27,9 @@ long long llabs(long long) libcesque pureconst;
char *ltpcpy(char *, long) paramsnonnull() libcesque nocallback;
int llog10(unsigned long) libcesque pureconst;
int unsleb128(const void *, size_t, int64_t *);
int atoi(const char *) paramsnonnull() libcesque nosideeffect;
long atol(const char *) paramsnonnull() libcesque nosideeffect;
long long atoll(const char *) paramsnonnull() libcesque nosideeffect;
int atoi(const char *) paramsnonnull() libcesque;
long atol(const char *) paramsnonnull() libcesque;
long long atoll(const char *) paramsnonnull() libcesque;
unsigned long strtoul(const char *, char **, int) paramsnonnull((1));
long long strtoll(const char *, char **, int) paramsnonnull((1));
unsigned long long strtoull(const char *, char **, int) paramsnonnull((1));
@ -39,8 +39,7 @@ intmax_t strtoimax(const char *, char **, int) paramsnonnull((1));
uintmax_t strtoumax(const char *, char **, int) paramsnonnull((1));
intmax_t wcstoimax(const wchar_t *, wchar_t **, int);
long wcstol(const wchar_t *, wchar_t **, int);
long strtol(const char *, char **, int)
paramsnonnull((1)) libcesque nosideeffect;
long strtol(const char *, char **, int) paramsnonnull((1)) libcesque;
/*───────────────────────────────────────────────────────────────────────────│─╗
cosmopolitan § conversion » time

View File

@ -35,8 +35,9 @@ LIBC_CONV_A_CHECKS = \
$(LIBC_CONV_A_HDRS:%=o/$(MODE)/%.ok)
LIBC_CONV_A_DIRECTDEPS = \
LIBC_STUBS \
LIBC_NEXGEN32E \
LIBC_STUBS \
LIBC_STR \
LIBC_TINYMATH \
LIBC_SYSV \
THIRD_PARTY_COMPILER_RT

View File

@ -92,14 +92,21 @@ static int ppatoi(const char **str) {
*
* Precision Modifiers
*
* - `%.8s` supplied character length (ignore nul terminator)
* - `%.*s` supplied character length argument (ignore nul terminator)
* - `%.8s` supplied byte length (obeys nul terminator)
* - `%.*s` supplied byte length argument (obeys nul terminator)
* - `%`.*s` supplied byte length argument c escaped (ignores nul terminator)
* - `%#.*s` supplied byte length argument visualized (ignores nul terminator)
* - `%.*hs` supplied char16_t length argument (obeys nul terminator)
* - `%.*ls` supplied wchar_t length argument (obeys nul terminator)
*
* Formatting Modifiers
*
* - `%,d` thousands separators
* - `%'s` escaped c string literal
* - `%'c` escaped c character literal
* - `%`c` c escaped character
* - `%`'c` c escaped character quoted
* - `%`s` c escaped string
* - `%`'s` c escaped string quoted
* - `%`s` escaped double quoted c string literal
* - `%`c` escaped double quoted c character literal
* - `%+d` plus leftpad if positive (aligns w/ negatives)
@ -114,7 +121,7 @@ hidden int palandprintf(void *fn, void *arg, const char *format, va_list va) {
void *p;
char qchar;
long double ldbl;
wchar_t charbuf[3];
wchar_t charbuf[1];
const char *alphabet;
int (*out)(long, void *);
unsigned char signbit, log2base;
@ -281,14 +288,15 @@ hidden int palandprintf(void *fn, void *arg, const char *format, va_list va) {
break;
case 'c':
precision = 1;
flags |= FLAGS_PRECISION;
qchar = '\'';
p = charbuf;
charbuf[0] = va_arg(va, int);
charbuf[1] = L'\0';
charbuf[0] = va_arg(va, int); /* assume little endian */
goto showstr;
case 'm':
p = weaken(strerror)(lasterr);
p = weaken(strerror) ? weaken(strerror)(lasterr) : "?";
signbit = 0;
goto showstr;

View File

@ -44,7 +44,7 @@ static int StoaEmitWordEncodedString(int f(long, void *), void *a, uint64_t w) {
}
static int StoaEmitUnicode(int f(long, void *), void *a, wint_t c) {
if (0 <= c && c <= 127) {
if (isascii(c)) {
return f(c, a);
} else {
return StoaEmitWordEncodedString(f, a, tpenc(c));
@ -52,7 +52,7 @@ static int StoaEmitUnicode(int f(long, void *), void *a, wint_t c) {
}
static int StoaEmitQuoted(int f(long, void *), void *a, wint_t c) {
if (0 <= c && c <= 127) {
if (isascii(c)) {
return StoaEmitWordEncodedString(f, a, cescapec(c));
} else {
return StoaEmitWordEncodedString(f, a, tpenc(c));
@ -92,8 +92,8 @@ int stoa(int out(long, void *), void *arg, void *data, unsigned long flags,
wint_t wc;
unsigned n;
emit_f emit;
bool justdobytes;
unsigned w, c, pad;
bool justdobytes, ignorenul;
p = data;
if (!p) {
@ -105,13 +105,35 @@ int stoa(int out(long, void *), void *arg, void *data, unsigned long flags,
if (StoaEmitQuote(out, arg, flags, qchar, signbit) == -1) return -1;
}
if (!(flags & FLAGS_PRECISION)) {
if (signbit == 63) {
precision = tinywcsnlen((const wchar_t *)p, -1);
} else if (signbit == 15) {
precision = tinystrnlen16((const char16_t *)p, -1);
ignorenul = false;
justdobytes = false;
if (signbit == 15 || signbit == 63) {
if (flags & FLAGS_QUOTE) {
emit = StoaEmitQuoted;
ignorenul = flags & FLAGS_PRECISION;
} else {
precision = strlen(p);
emit = StoaEmitUnicode;
}
} else if ((flags & FLAGS_HASH) && weaken(kCp437)) {
justdobytes = true;
emit = StoaEmitVisualized;
ignorenul = flags & FLAGS_PRECISION;
} else if (flags & FLAGS_QUOTE) {
emit = StoaEmitQuoted;
ignorenul = flags & FLAGS_PRECISION;
} else {
justdobytes = true;
emit = StoaEmitByte;
}
if (!(flags & FLAGS_PRECISION)) precision = -1;
if (!(flags & FLAGS_PRECISION) || !ignorenul) {
if (signbit == 63) {
precision = tinywcsnlen((const wchar_t *)p, precision);
} else if (signbit == 15) {
precision = tinystrnlen16((const char16_t *)p, precision);
} else {
precision = strnlen(p, precision);
}
}
@ -138,32 +160,17 @@ int stoa(int out(long, void *), void *arg, void *data, unsigned long flags,
if (spacepad(out, arg, pad) == -1) return -1;
}
justdobytes = false;
if (signbit == 15 || signbit == 63) {
if (flags & FLAGS_QUOTE) {
emit = StoaEmitQuoted;
} else {
emit = StoaEmitUnicode;
}
} else if ((flags & FLAGS_HASH) && weaken(kCp437)) {
justdobytes = true;
emit = StoaEmitVisualized;
} else if (flags & FLAGS_QUOTE) {
emit = StoaEmitQuoted;
} else {
justdobytes = true;
emit = StoaEmitByte;
}
if (justdobytes) {
while (precision--) {
wc = *p++ & 0xff;
if (!wc && !ignorenul) break;
if (emit(out, arg, wc) == -1) return -1;
}
} else {
while (precision--) {
if (signbit == 15) {
wc = *(const char16_t *)p;
if (!wc && !ignorenul) break;
if (IsUcs2(wc)) {
p += sizeof(char16_t);
} else if (IsUtf16Cont(wc)) {
@ -177,10 +184,12 @@ int stoa(int out(long, void *), void *arg, void *data, unsigned long flags,
}
} else if (signbit == 63) {
wc = *(const wint_t *)p;
if (!wc && !ignorenul) break;
p += sizeof(wint_t);
if (!wc) break;
} else {
wc = *p++ & 0xff;
if (!wc && !ignorenul) break;
if (!isascii(wc)) {
if (ThomPikeCont(wc)) continue;
n = ThomPikeLen(wc) - 1;

View File

@ -23,6 +23,8 @@
/**
* Compares signed 8-bit integers w/ greater than predicate.
*
* Note that operands can be xor'd with 0x80 for unsigned compares.
*
* @param 𝑎 [w/o] receives result
* @param 𝑏 [r/o] supplies first input vector
* @param 𝑐 [r/o] supplies second input vector

View File

@ -19,6 +19,12 @@
*/
#include "libc/intrin/pmovmskb.h"
/**
* Turns result of byte comparison into bitmask.
*
* @param 𝑝 is byte vector to crunch
* @see pcmpeqb(), bsf(), etc.
*/
uint32_t(pmovmskb)(const uint8_t p[16]) {
uint32_t i, m;
for (m = i = 0; i < 16; ++i) {

View File

@ -18,6 +18,7 @@
02110-1301 USA
*/
#include "libc/intrin/pshufd.h"
#include "libc/str/str.h"
/**
* Shuffles int vector.
@ -30,8 +31,5 @@ void(pshufd)(int32_t b[4], const int32_t a[4], uint8_t m) {
t[1] = a[(m & 0b00001100) >> 2];
t[2] = a[(m & 0b00110000) >> 4];
t[3] = a[(m & 0b11000000) >> 6];
b[0] = t[0];
b[1] = t[1];
b[2] = t[2];
b[3] = t[3];
memcpy(b, t, 16);
}

View File

@ -28,9 +28,9 @@
* @param 𝑐 [r/o] supplies second input vector
* @mayalias
*/
void(psubb)(int8_t a[16], const int8_t b[16], const int8_t c[16]) {
void(psubb)(uint8_t a[16], const uint8_t b[16], const uint8_t c[16]) {
unsigned i;
int8_t r[16];
uint8_t r[16];
for (i = 0; i < 16; ++i) {
r[i] = b[i] - c[i];
}

View File

@ -4,7 +4,7 @@
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
void psubb(int8_t[16], const int8_t[16], const int8_t[16]);
void psubb(uint8_t[16], const uint8_t[16], const uint8_t[16]);
#define psubb(A, B, C) \
INTRIN_SSEVEX_X_X_X_(psubb, SSE2, "psubb", INTRIN_NONCOMMUTATIVE, A, B, C)

View File

@ -37,6 +37,7 @@
#define PVOID void*
#define PVOID64 void*
#define LPCVOID const void*
#define CHAR char
#define SHORT short
#define CONST const

12
libc/linux/close.h 100644
View File

@ -0,0 +1,12 @@
#ifndef COSMOPOLITAN_LIBC_LINUX_CLOSE_H_
#define COSMOPOLITAN_LIBC_LINUX_CLOSE_H_
#if !(__ASSEMBLER__ + __LINKER__ + 0)
forceinline long LinuxClose(long fd) {
long rc;
asm volatile("syscall" : "=a"(rc) : "0"(3), "D"(fd) : "rcx", "r11", "memory");
return rc;
}
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_LINUX_CLOSE_H_ */

View File

@ -3,7 +3,7 @@
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
forceinline noreturn long LinuxExit(int rc) {
forceinline noreturn long LinuxExit(long rc) {
asm volatile("syscall"
: /* no outputs */
: "a"(0xE7), "D"(rc)

16
libc/linux/fstat.h 100644
View File

@ -0,0 +1,16 @@
#ifndef COSMOPOLITAN_LIBC_LINUX_FSTAT_H_
#define COSMOPOLITAN_LIBC_LINUX_FSTAT_H_
#include "libc/calls/struct/stat.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
forceinline long LinuxFstat(long fd, struct stat *st) {
long rc;
asm volatile("syscall"
: "=a"(rc)
: "0"(5), "D"(fd), "S"(st)
: "rcx", "r11", "memory");
return rc;
}
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_LINUX_FSTAT_H_ */

20
libc/linux/mmap.h 100644
View File

@ -0,0 +1,20 @@
#ifndef COSMOPOLITAN_LIBC_LINUX_MMAP_H_
#define COSMOPOLITAN_LIBC_LINUX_MMAP_H_
#if !(__ASSEMBLER__ + __LINKER__ + 0)
forceinline long LinuxMmap(void *addr, size_t size, long prot, long flags,
long fd, long off) {
long rc;
register long r10 asm("r10") = flags;
register long r8 asm("r8") = fd;
register long r9 asm("r9") = off;
asm volatile("syscall"
: "=a"(rc)
: "0"(9), "D"(addr), "S"(size), "d"(prot), "r"(r10), "r"(r8),
"r"(r9)
: "rcx", "r11", "memory");
return rc;
}
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_LINUX_MMAP_H_ */

View File

@ -0,0 +1,15 @@
#ifndef COSMOPOLITAN_LIBC_LINUX_MUNMAP_H_
#define COSMOPOLITAN_LIBC_LINUX_MUNMAP_H_
#if !(__ASSEMBLER__ + __LINKER__ + 0)
forceinline long LinuxMunmap(void *addr, size_t size) {
long rc;
asm volatile("syscall"
: "=a"(rc)
: "0"(0xb), "D"(addr), "S"(size)
: "rcx", "r11", "memory");
return rc;
}
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_LINUX_MUNMAP_H_ */

15
libc/linux/open.h 100644
View File

@ -0,0 +1,15 @@
#ifndef COSMOPOLITAN_LIBC_LINUX_OPEN_H_
#define COSMOPOLITAN_LIBC_LINUX_OPEN_H_
#if !(__ASSEMBLER__ + __LINKER__ + 0)
forceinline long LinuxOpen(const char *path, long flags, long mode) {
long rc;
asm volatile("syscall"
: "=a"(rc)
: "0"(2), "D"(path), "S"(flags), "d"(mode)
: "rcx", "r11", "memory");
return rc;
}
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_LINUX_OPEN_H_ */

View File

@ -2,7 +2,7 @@
#define COSMOPOLITAN_LIBC_LINUX_READ_H_
#if !(__ASSEMBLER__ + __LINKER__ + 0)
forceinline long LinuxRead(int fd, void *data, unsigned long size) {
forceinline long LinuxRead(long fd, void *data, unsigned long size) {
long rc;
asm volatile("syscall"
: "=a"(rc), "=m"(*(char(*)[size])data)

View File

@ -1,9 +1,8 @@
#ifndef COSMOPOLITAN_LIBC_LINUX_WRITE_H_
#define COSMOPOLITAN_LIBC_LINUX_WRITE_H_
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
forceinline long LinuxWrite(int fd, const void *data, unsigned long size) {
forceinline long LinuxWrite(long fd, const void *data, unsigned long size) {
long rc;
asm volatile("syscall"
: "=a"(rc)
@ -13,6 +12,5 @@ forceinline long LinuxWrite(int fd, const void *data, unsigned long size) {
return rc;
}
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_LINUX_WRITE_H_ */

View File

@ -17,25 +17,14 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/assert.h"
#include "libc/bits/safemacros.h"
#include "libc/bits/weaken.h"
#include "libc/calls/calls.h"
#include "libc/conv/conv.h"
#include "libc/conv/itoa.h"
#include "libc/log/asan.h"
#include "libc/log/backtrace.h"
#include "libc/log/log.h"
#include "libc/macros.h"
#include "libc/mem/hook/hook.h"
#include "libc/runtime/directmap.h"
#include "libc/runtime/internal.h"
#include "libc/runtime/memtrack.h"
#include "libc/runtime/missioncritical.h"
#include "libc/runtime/runtime.h"
#include "libc/runtime/symbols.h"
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/fileno.h"
#include "libc/sysv/consts/map.h"
#include "libc/sysv/consts/prot.h"
@ -154,9 +143,9 @@ static const char *__asan_describe_access_poison(int c) {
}
static noreturn void __asan_die(const char *msg, size_t size) {
__print(msg, size);
PrintBacktraceUsingSymbols(stderr, __builtin_frame_address(0),
getsymboltable());
write(STDERR_FILENO, msg, size);
PrintBacktraceUsingSymbols(STDERR_FILENO, __builtin_frame_address(0),
GetSymbolTable());
DebugBreak();
_Exit(66);
}
@ -170,7 +159,7 @@ static noreturn void __asan_report_deallocate_fault(void *addr, int c) {
p = mempcpy(p, ibuf, int64toarray_radix10(c, ibuf));
p = stpcpy(p, " at 0x");
p = mempcpy(p, ibuf, uint64toarray_fixed16((intptr_t)addr, ibuf, 48));
p = stpcpy(p, "\n");
p = stpcpy(p, "\r\n");
__asan_die(buf, p - buf);
}
@ -186,7 +175,7 @@ static noreturn void __asan_report_memory_fault(uint8_t *addr, int size,
p = stpcpy(p, kind);
p = stpcpy(p, " at 0x");
p = mempcpy(p, ibuf, uint64toarray_fixed16((intptr_t)addr, ibuf, 48));
p = stpcpy(p, "\n");
p = stpcpy(p, "\r\n");
__asan_die(buf, p - buf);
}
@ -209,13 +198,6 @@ static void __asan_morgue_flush(void) {
}
}
static bool __asan_is_mapped(void *p) {
int x, i;
x = (intptr_t)p >> 16;
i = FindMemoryInterval(&_mmi, x);
return i < _mmi.i && x >= _mmi.p[i].x && x <= _mmi.p[i].y;
}
static void *__asan_allocate(size_t align, size_t size, int underrun,
int overrun) {
char *p, *s;
@ -394,44 +376,62 @@ void __asan_install_malloc_hooks(void) {
HOOK(hook$malloc_usable_size, __asan_malloc_usable_size);
}
void __asan_map_shadow(void *addr, size_t size) {
int i, n, x;
char *a, *b;
static bool __asan_is_mapped(int x) {
int i = FindMemoryInterval(&_mmi, x);
return i < _mmi.i && x >= _mmi.p[i].x && x <= _mmi.p[i].y;
}
void __asan_map_shadow(void *p, size_t n) {
int i, x, a, b;
struct DirectMap sm;
a = (char *)ROUNDDOWN(SHADOW((intptr_t)addr), FRAMESIZE);
b = (char *)ROUNDDOWN(SHADOW((intptr_t)addr + size - 1), FRAMESIZE);
for (; a <= b; a += FRAMESIZE) {
a = SHADOW((uintptr_t)p) >> 16;
b = ROUNDUP(SHADOW(ROUNDUP((uintptr_t)p + n, 8)), 1 << 16) >> 16;
for (; a < b; ++a) {
if (!__asan_is_mapped(a)) {
sm = DirectMap(a, FRAMESIZE, PROT_READ | PROT_WRITE,
sm = DirectMap((void *)((uintptr_t)a << 16), 1 << 16,
PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
if (sm.addr == MAP_FAILED ||
TrackMemoryInterval(&_mmi, (intptr_t)a >> 16, (intptr_t)a >> 16,
sm.maphandle) == -1) {
TrackMemoryInterval(&_mmi, a, a, sm.maphandle) == -1) {
abort();
}
}
}
}
textstartup void __asan_init(int argc, char *argv[], char **envp,
intptr_t *auxv) {
int i;
static bool once;
register intptr_t rsp asm("rsp");
if (!once) {
__asan_map_shadow(_base, _end - _base);
__asan_map_shadow((void *)ROUNDDOWN(rsp, STACKSIZE), STACKSIZE);
for (i = 0; i < argc; ++i) __asan_map_shadow(argv[i], strlen(argv[i]));
for (; *envp; ++envp) __asan_map_shadow(*envp, strlen(*envp));
__asan_map_shadow(auxv, sizeof(intptr_t) * 2);
__asan_install_malloc_hooks();
once = true;
static char *__asan_get_stack_base(void) {
register uintptr_t rsp asm("rsp");
return (char *)ROUNDDOWN(ROUNDDOWN(rsp, STACKSIZE), FRAMESIZE);
}
static textstartup size_t __asan_get_auxv_size(intptr_t *auxv) {
unsigned i;
for (i = 0;; i += 2) {
if (!auxv[i]) break;
}
return (i + 2) * sizeof(intptr_t);
}
static textstartup void __asan_shadow_string_list(char **list) {
for (; *list; ++list) {
__asan_map_shadow(*list, strlen(*list) + 1);
}
}
textstartup void __asan_init(int argc, char **argv, char **envp,
intptr_t *auxv) {
static bool once;
if (once) return;
__asan_map_shadow(_base, _end - _base);
__asan_map_shadow(__asan_get_stack_base(), STACKSIZE);
__asan_shadow_string_list(argv);
__asan_shadow_string_list(envp);
__asan_map_shadow(auxv, __asan_get_auxv_size(auxv));
__asan_install_malloc_hooks();
}
static textstartup void __asan_ctor(void) {
/* __cxa_atexit(__asan_morgue_flush, NULL, NULL); */
getsymboltable();
__cxa_atexit(__asan_morgue_flush, NULL, NULL);
}
const void *const g_asan_ctor[] initarray = {__asan_ctor};

View File

@ -29,10 +29,13 @@
#include "libc/runtime/runtime.h"
#include "libc/runtime/symbols.h"
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/fileno.h"
#include "libc/sysv/consts/o.h"
#include "libc/sysv/consts/w.h"
#define RESTORE_TTY "\e[?1000;1002;1015;1006l\e[?25h"
/**
* Launches GDB debugger GUI for current process.
*
@ -50,17 +53,18 @@
* @note this is called via eponymous spinlock macro wrapper
*/
relegated int(attachdebugger)(intptr_t continuetoaddr) {
int ttyin, ttyout;
int ttyfd;
struct StackFrame *bp;
char pidstr[11], breakcmd[40];
const char *se, *elf, *gdb, *rewind, *layout;
if (IsGenuineCosmo() || !(gdb = GetGdbPath()) ||
(ttyin = ttyout = open(_PATH_TTY, O_RDWR, 0)) == -1) {
(ttyfd = open(_PATH_TTY, O_RDWR, 0)) == -1) {
return -1;
}
write(ttyfd, RESTORE_TTY, strlen(RESTORE_TTY));
snprintf(pidstr, sizeof(pidstr), "%u", getpid());
layout = "layout asm";
if ((elf = finddebugbinary())) {
if ((elf = FindDebugBinary())) {
se = "-se";
if (fileexists(__FILE__)) layout = "layout src";
} else {
@ -78,7 +82,7 @@ relegated int(attachdebugger)(intptr_t continuetoaddr) {
rewind = NULL;
breakcmd[0] = '\0';
}
return spawnve(0, (int[3]){ttyin, ttyout, STDERR_FILENO}, gdb,
return spawnve(0, (int[3]){ttyfd, ttyfd, STDERR_FILENO}, gdb,
(char *const[]){
"gdb", "--tui",
"-p", pidstr,

View File

@ -2,12 +2,11 @@
#define COSMOPOLITAN_LIBC_LOG_BACKTRACE_H_
#include "libc/nexgen32e/stackframe.h"
#include "libc/runtime/symbols.h"
#include "libc/stdio/stdio.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
void showbacktrace(FILE *, const struct StackFrame *);
int PrintBacktraceUsingSymbols(FILE *, const struct StackFrame *,
void ShowBacktrace(int, const struct StackFrame *);
int PrintBacktraceUsingSymbols(int, const struct StackFrame *,
struct SymbolTable *);
COSMOPOLITAN_C_END_

View File

@ -31,14 +31,13 @@
#include "libc/nexgen32e/gc.h"
#include "libc/runtime/runtime.h"
#include "libc/runtime/symbols.h"
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/fileno.h"
#define kBacktraceMaxFrames 128
#define kBacktraceBufSize ((kBacktraceMaxFrames - 1) * (16 + 1))
static int PrintBacktraceUsingAddr2line(FILE *f, const struct StackFrame *bp) {
static int PrintBacktraceUsingAddr2line(int fd, const struct StackFrame *bp) {
ssize_t got;
intptr_t addr;
size_t i, j, gi;
@ -47,7 +46,7 @@ static int PrintBacktraceUsingAddr2line(FILE *f, const struct StackFrame *bp) {
const struct StackFrame *frame;
const char *debugbin, *p1, *p2, *p3, *addr2line;
char buf[kBacktraceBufSize], *argv[kBacktraceMaxFrames];
if (!(debugbin = finddebugbinary()) || !(addr2line = GetAddr2linePath())) {
if (!(debugbin = FindDebugBinary()) || !(addr2line = GetAddr2linePath())) {
return -1;
}
i = 0;
@ -88,11 +87,11 @@ static int PrintBacktraceUsingAddr2line(FILE *f, const struct StackFrame *bp) {
strlen(" (discriminator ") - 1)) &&
(p3 = memchr(p2, '\n', got - (p2 - p1)))) {
if (p3 > p2 && p3[-1] == '\r') --p3;
fwrite(p1, 1, p2 - p1, f);
write(fd, p1, p2 - p1);
got -= p3 - p1;
p1 += p3 - p1;
} else {
fwrite(p1, 1, got, f);
write(fd, p1, got);
break;
}
}
@ -103,20 +102,21 @@ static int PrintBacktraceUsingAddr2line(FILE *f, const struct StackFrame *bp) {
return 0;
}
static int PrintBacktrace(FILE *f, const struct StackFrame *bp) {
static int PrintBacktrace(int fd, const struct StackFrame *bp) {
if (!IsTiny()) {
if (PrintBacktraceUsingAddr2line(f, bp) != -1) {
if (PrintBacktraceUsingAddr2line(fd, bp) != -1) {
return 0;
}
}
return PrintBacktraceUsingSymbols(f, bp, getsymboltable());
return PrintBacktraceUsingSymbols(fd, bp, GetSymbolTable());
}
void showbacktrace(FILE *f, const struct StackFrame *bp) {
void ShowBacktrace(int fd, const struct StackFrame *bp) {
static bool noreentry;
if (!bp) bp = __builtin_frame_address(0);
if (!noreentry) {
noreentry = true;
PrintBacktrace(f, bp);
PrintBacktrace(fd, bp);
noreentry = 0;
}
}

View File

@ -20,6 +20,7 @@
#include "libc/alg/bisectcarleft.h"
#include "libc/assert.h"
#include "libc/bits/weaken.h"
#include "libc/calls/calls.h"
#include "libc/conv/itoa.h"
#include "libc/fmt/fmt.h"
#include "libc/log/backtrace.h"
@ -28,26 +29,26 @@
#include "libc/nexgen32e/stackframe.h"
#include "libc/runtime/missioncritical.h"
#include "libc/runtime/symbols.h"
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"
/**
* Prints stack frames with symbols.
*
* PrintBacktraceUsingSymbols(stdout, NULL, getsymboltable());
* PrintBacktraceUsingSymbols(STDOUT_FILENO, NULL, GetSymbolTable());
*
* @param f is output stream
* @param bp is rbp which can be NULL to detect automatically
* @param st is open symbol table for current executable
* @return -1 w/ errno if error happened
*/
int PrintBacktraceUsingSymbols(FILE *f, const struct StackFrame *bp,
int PrintBacktraceUsingSymbols(int fd, const struct StackFrame *bp,
struct SymbolTable *st) {
char *p;
size_t gi;
intptr_t addr;
int64_t addend;
struct Garbages *garbage;
char *p, buf[256], ibuf[21];
char buf[256], ibuf[21];
const struct Symbol *symbol;
const struct StackFrame *frame;
if (!st) return -1;
@ -78,8 +79,11 @@ int PrintBacktraceUsingSymbols(FILE *f, const struct StackFrame *bp,
} else {
p = stpcpy(p, "UNKNOWN");
}
*p++ = '\r';
*p++ = '\n';
__print(buf, p - buf);
if (write(fd, buf, p - buf) == -1) {
return -1;
}
}
return 0;
}

View File

@ -10,7 +10,7 @@
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
#define CHECK(X, ...) __CHK(eq, ==, true, "true", !!(X), #X, "" __VA_ARGS__)
#define CHECK(X, ...) __CHK(ne, !=, false, "false", !!(X), #X, "" __VA_ARGS__)
#define CHECK_EQ(Y, X, ...) __CHK(eq, ==, Y, #Y, X, #X, "" __VA_ARGS__)
#define CHECK_NE(Y, X, ...) __CHK(ne, !=, Y, #Y, X, #X, "" __VA_ARGS__)
#define CHECK_LE(Y, X, ...) __CHK(le, <=, Y, #Y, X, #X, "" __VA_ARGS__)
@ -19,7 +19,7 @@ COSMOPOLITAN_C_START_
#define CHECK_GT(Y, X, ...) __CHK(gt, >, Y, #Y, X, #X, "" __VA_ARGS__)
#define CHECK_NOTNULL(X, ...) __CHK(ne, !=, NULL, "NULL", X, #X, "" __VA_ARGS__)
#define DCHECK(X, ...) __DCHK(eq, ==, true, "true", !!(X), #X, "" __VA_ARGS__)
#define DCHECK(X, ...) __DCHK(ne, !=, false, "false", !!(X), #X, "" __VA_ARGS__)
#define DCHECK_EQ(Y, X, ...) __DCHK(eq, ==, Y, #Y, X, #X, "" __VA_ARGS__)
#define DCHECK_NE(Y, X, ...) __DCHK(ne, !=, Y, #Y, X, #X, "" __VA_ARGS__)
#define DCHECK_LE(Y, X, ...) __DCHK(le, <=, Y, #Y, X, #X, "" __VA_ARGS__)

View File

@ -29,7 +29,7 @@ STATIC_YOINK("stoa");
void __check_fail_aligned(unsigned bytes, uint64_t ptr) {
fflush(stderr);
if (!IsTiny()) memsummary(fileno(stderr));
(dprintf)(fileno(stderr), "%s%d%s%#p\n", "error: pointer not ", bytes,
(dprintf)(fileno(stderr), "%s%d%s%#p\r\n", "error: pointer not ", bytes,
"-byte aligned: ", ptr);
die();
}

View File

@ -52,10 +52,10 @@ relegated void __check_fail(const char *suffix, const char *opstr,
strtoupper(sufbuf);
(fprintf)(stderr,
"check failed\n"
"\tCHECK_%s(%s, %s);\n"
"\t\t → %#lx (%s)\n"
"\t\t%s %#lx (%s)\n",
"check failed\r\n"
"\tCHECK_%s(%s, %s);\r\n"
"\t\t → %#lx (%s)\r\n"
"\t\t%s %#lx (%s)\r\n",
sufbuf, wantstr, gotstr, want, wantstr, opstr, got, gotstr);
if (!isempty(fmt)) {
@ -63,18 +63,18 @@ relegated void __check_fail(const char *suffix, const char *opstr,
va_start(va, fmt);
(vfprintf)(stderr, fmt, va);
va_end(va);
fputc('\n', stderr);
fputs("\r\n", stderr);
}
(fprintf)(stderr, "\t%s\n\t%s%s%s%s\n", strerror(lasterr), SUBTLE,
(fprintf)(stderr, "\t%s\r\n\t%s%s%s%s\r\n", strerror(lasterr), SUBTLE,
getauxval(AT_EXECFN), g_argc > 1 ? " \\" : "", RESET);
for (i = 1; i < g_argc; ++i) {
(fprintf)(stderr, "\t\t%s%s\n", g_argv[i], i < g_argc - 1 ? " \\" : "");
(fprintf)(stderr, "\t\t%s%s\r\n", g_argv[i], i < g_argc - 1 ? " \\" : "");
}
if (!IsTiny() && lasterr == ENOMEM) {
(fprintf)(stderr, "\n");
(fprintf)(stderr, "\r\n");
fflush(stderr);
PrintMemoryIntervals(fileno(stderr), &_mmi);
}

View File

@ -47,5 +47,5 @@ relegated void ___check_fail_ndebug(uint64_t want, uint64_t got,
__print(bx, uint64toarray_radix16(got, bx));
__print_string(" (");
__print(bx, int64toarray_radix10(lasterr, bx));
__print_string(")\n");
__print_string(")\r\n");
}

View File

@ -18,10 +18,12 @@
02110-1301 USA
*/
#include "libc/bits/bits.h"
#include "libc/log/backtrace.h"
#include "libc/log/log.h"
#include "libc/runtime/internal.h"
#include "libc/stdio/stdio.h"
#include "libc/sysv/consts/exit.h"
#include "libc/sysv/consts/fileno.h"
/**
* Aborts process after printing details on its current state.
@ -33,7 +35,7 @@ relegated noreturn void die(void) {
if (!IsTiny()) {
g_runstate |= RUNSTATE_BROKEN;
if (IsDebuggerPresent(false)) DebugBreak();
backtrace(stderr);
ShowBacktrace(STDERR_FILENO, NULL);
}
exit(EXIT_FAILURE);
unreachable;

View File

@ -38,7 +38,7 @@ int(gdbexec)(const char *cmd) {
char pidstr[11], breakcmd[40];
if (!(gdb = GetGdbPath())) return -1;
snprintf(pidstr, sizeof(pidstr), "%u", getpid());
if ((elf = finddebugbinary())) {
if ((elf = FindDebugBinary())) {
se = "-se";
} else {
se = "-q";

View File

@ -24,15 +24,15 @@
* Returns debug binary symbol table, as global singleton.
* @return symbol table, or NULL w/ errno on first call
*/
struct SymbolTable *getsymboltable(void) {
struct SymbolTable *GetSymbolTable(void) {
static bool once;
static struct SymbolTable *singleton;
const char *debugbin;
if (!once) {
once = true;
if ((debugbin = finddebugbinary()) &&
(singleton = opensymboltable(debugbin))) {
__cxa_atexit(closesymboltable, &singleton, NULL);
if ((debugbin = FindDebugBinary()) &&
(singleton = OpenSymbolTable(debugbin))) {
__cxa_atexit(CloseSymbolTable, &singleton, NULL);
}
}
return singleton;

View File

@ -33,7 +33,6 @@ typedef struct FILE FILE;
extern FILE *g_logfile;
void backtrace(FILE *) relegated; /* shows fn backtrace and args */
void perror(const char *) relegated; /* print the last system error */
void die(void) relegated noreturn; /* print backtrace and abort() */
void meminfo(int); /* shows malloc statistics &c. */

View File

@ -25,7 +25,7 @@ STATIC_YOINK("ntoa");
void malloc_stats(void) {
struct MallocStats res = dlmalloc_stats(g_dlmalloc);
(fprintf)(stderr, "max system bytes = %'10zu\n", res.maxfp);
(fprintf)(stderr, "system bytes = %'10zu\n", res.fp);
(fprintf)(stderr, "in use bytes = %'10zu\n", res.used);
(fprintf)(stderr, "max system bytes = %'10zu\r\n", res.maxfp);
(fprintf)(stderr, "system bytes = %'10zu\r\n", res.fp);
(fprintf)(stderr, "in use bytes = %'10zu\r\n", res.used);
}

View File

@ -26,7 +26,7 @@ STATIC_YOINK("ntoa");
STATIC_YOINK("stoa");
static void onmemchunk(void *start, void *end, size_t used_bytes, void *arg) {
(dprintf)(*(int *)arg, "%p - %p : %08zx / %08lx\n", start, end, used_bytes,
(dprintf)(*(int *)arg, "%p - %p : %08zx / %08lx\r\n", start, end, used_bytes,
(intptr_t)end - (intptr_t)start);
}
@ -35,7 +35,7 @@ static void onmemchunk(void *start, void *end, size_t used_bytes, void *arg) {
*/
void meminfo(int fd) {
memsummary(fd);
(dprintf)(fd, "%*s %*s %*s %*s\n", POINTER_XDIGITS, "start",
(dprintf)(fd, "%*s %*s %*s %*s\r\n", POINTER_XDIGITS, "start",
POINTER_XDIGITS, "end", 8, "used", 8, "size");
malloc_inspect_all(onmemchunk, &fd);
}

View File

@ -32,9 +32,9 @@
#include "libc/runtime/internal.h"
#include "libc/runtime/memtrack.h"
#include "libc/runtime/runtime.h"
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/auxv.h"
#include "libc/sysv/consts/fileno.h"
#include "libc/sysv/consts/o.h"
#include "libc/sysv/consts/sig.h"
@ -49,17 +49,17 @@ STATIC_YOINK("stoa");
struct siginfo;
aligned(1) const char kGregOrder[17] = {
const char kGregOrder[17] aligned(1) = {
13, 11, 8, 14, 12, 9, 10, 15, 16, 0, 1, 2, 3, 4, 5, 6, 7,
};
aligned(1) const char kGregNames[17][4] = {
const char kGregNames[17][4] aligned(1) = {
"R8", "R9", "R10", "R11", "R12", "R13", "R14", "R15", "RDI",
"RSI", "RBP", "RBX", "RDX", "RAX", "RCX", "RSP", "RIP",
};
aligned(1) const char kGodHatesFlags[12] = "CVPRAKZSTIDO";
aligned(1) const char kCrashSigNames[8][5] = {"QUIT", "FPE", "ILL", "SEGV",
const char kGodHatesFlags[12] aligned(1) = "CVPRAKZSTIDO";
const char kCrashSigNames[8][5] aligned(1) = {"QUIT", "FPE", "ILL", "SEGV",
"TRAP", "ABRT", "BUS"};
int kCrashSigs[8];
@ -75,60 +75,60 @@ relegated static const char *TinyStrSignal(int sig) {
return "???";
}
relegated static void ShowFunctionCalls(FILE *f, ucontext_t *ctx) {
relegated static void ShowFunctionCalls(int fd, ucontext_t *ctx) {
struct StackFrame *bp;
struct StackFrame goodframe;
fputc('\n', f);
write(fd, "\r\n", 2);
if (ctx && ctx->uc_mcontext.rip && ctx->uc_mcontext.rbp) {
goodframe.next = (struct StackFrame *)ctx->uc_mcontext.rbp;
goodframe.addr = ctx->uc_mcontext.rip;
bp = &goodframe;
showbacktrace(f, bp);
ShowBacktrace(fd, bp);
}
}
relegated static void DescribeCpuFlags(FILE *f, unsigned efl) {
size_t i;
relegated static void DescribeCpuFlags(int fd, unsigned flags) {
unsigned i;
char buf[64], *p;
p = buf;
for (i = 0; i < ARRAYLEN(kGodHatesFlags); ++i) {
if (efl & 1) {
fputc(' ', f);
fputc(kGodHatesFlags[i], f);
fputc('F', f);
if (flags & 1) {
*p++ = ' ';
*p++ = kGodHatesFlags[i];
*p++ = 'F';
}
efl >>= 1;
flags >>= 1;
}
(fprintf)(f, " %s%d\n", "IOPL", efl & 3);
p = stpcpy(p, " IOPL");
*p++ = '0' + (flags & 3);
write(fd, buf, p - buf);
}
relegated static void ShowGeneralRegisters(FILE *f, ucontext_t *ctx) {
relegated static void ShowGeneralRegisters(int fd, ucontext_t *ctx) {
size_t i, j, k;
long double st;
fputc('\n', f);
write(fd, "\r\n", 2);
for (i = 0, j = 0, k = 0; i < ARRAYLEN(kGregNames); ++i) {
if (j > 0) {
fputc(' ', f);
}
(fprintf)(f, "%-3s %016lx", kGregNames[(unsigned)kGregOrder[i]],
if (j > 0) write(fd, " ", 1);
(dprintf)(fd, "%-3s %016lx", kGregNames[(unsigned)kGregOrder[i]],
ctx->uc_mcontext.gregs[(unsigned)kGregOrder[i]]);
if (++j == 3) {
j = 0;
memcpy(&st, (char *)&ctx->fpustate.st[k], sizeof(st));
(fprintf)(f, " %s(%zu) %Lf", "ST", k, st);
(dprintf)(fd, " %s(%zu) %Lf", "ST", k, st);
++k;
fputc('\r', f);
fputc('\n', f);
write(fd, "\r\n", 2);
}
}
fflush(stderr);
DescribeCpuFlags(f, ctx->uc_mcontext.gregs[REG_EFL]);
DescribeCpuFlags(fd, ctx->uc_mcontext.gregs[REG_EFL]);
}
relegated static void ShowSseRegisters(FILE *f, ucontext_t *ctx) {
relegated static void ShowSseRegisters(int fd, ucontext_t *ctx) {
size_t i;
fputc('\n', f);
write(fd, "\r\n", 2);
for (i = 0; i < 8; ++i) {
(fprintf)(f, VEIL("r", "%s%-2zu %016lx%016lx %s%-2d %016lx%016lx\n"), "XMM",
i + 0, ctx->fpustate.xmm[i + 0].u64[0],
(dprintf)(fd, VEIL("r", "%s%-2zu %016lx%016lx %s%-2d %016lx%016lx\r\n"),
"XMM", i + 0, ctx->fpustate.xmm[i + 0].u64[0],
ctx->fpustate.xmm[i + 0].u64[1], "XMM", i + 8,
ctx->fpustate.xmm[i + 8].u64[0], ctx->fpustate.xmm[i + 8].u64[1]);
}
@ -149,23 +149,23 @@ relegated static void ShowMemoryMappings(int outfd) {
}
}
relegated static void ShowCrashReport(int err, FILE *f, int sig,
relegated static void ShowCrashReport(int err, int fd, int sig,
ucontext_t *ctx) {
struct utsname names;
(fprintf)(f, VEIL("r", "\n%serror%s: Uncaught SIG%s\n %s\n %s\n"), RED2,
RESET, TinyStrSignal(sig), getauxval(AT_EXECFN), strerror(err));
(dprintf)(fd, VEIL("r", "\r\n%serror%s: Uncaught SIG%s\r\n %s\r\n %s\r\n"),
RED2, RESET, TinyStrSignal(sig), getauxval(AT_EXECFN),
strerror(err));
if (uname(&names) != -1) {
(fprintf)(f, VEIL("r", " %s %s %s %s\n"), names.sysname, names.nodename,
(dprintf)(fd, VEIL("r", " %s %s %s %s\r\n"), names.sysname, names.nodename,
names.release, names.version);
}
ShowFunctionCalls(f, ctx);
ShowFunctionCalls(fd, ctx);
if (ctx) {
ShowGeneralRegisters(f, ctx);
ShowSseRegisters(f, ctx);
ShowGeneralRegisters(fd, ctx);
ShowSseRegisters(fd, ctx);
}
fputc('\n', f);
fflush(f);
ShowMemoryMappings(fileno(f));
write(fd, "\r\n", 2);
ShowMemoryMappings(fd);
}
relegated static void RestoreDefaultCrashSignalHandlers(void) {
@ -215,7 +215,7 @@ relegated void oncrash(int sig, struct siginfo *si, ucontext_t *ctx) {
: 0);
}
if (gdbpid > 0 && (sig == SIGTRAP || sig == SIGQUIT)) return;
ShowCrashReport(err, stderr, sig, ctx);
quick_exit(128 + sig);
ShowCrashReport(err, STDERR_FILENO, sig, ctx);
exit(128 + sig);
unreachable;
}

View File

@ -17,12 +17,14 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/errno.h"
#include "libc/fmt/fmt.h"
#include "libc/log/internal.h"
#include "libc/log/log.h"
#include "libc/runtime/runtime.h"
#include "libc/stdio/stdio.h"
void perror(const char *message) {
fprintf(stderr, "%s%s%s: %s: %m: %s\n", RED2, "error", RESET,
program_invocation_name, message);
fprintf(stderr, "%s%s%s: %s: %s: %s\r\n", RED2, "error", RESET,
program_invocation_name, strerror(errno), message);
}

View File

@ -56,7 +56,7 @@ void __ubsan_abort(const struct UbsanSourceLocation *loc,
g_runstate |= RUNSTATE_BROKEN;
if (IsDebuggerPresent(false)) DebugBreak();
startfatal(loc->file, loc->line);
fprintf(stderr, "%s\n", description);
fprintf(stderr, "%s\r\n", description);
die();
unreachable;
}
@ -86,7 +86,7 @@ void __ubsan_handle_type_mismatch(struct UbsanTypeMismatchInfo *type_mismatch,
type_mismatch->alignment);
} else {
description = __ubsan_buf;
snprintf(__ubsan_buf, sizeof(__ubsan_buf), "%s\n\t%s %s %p %s %s",
snprintf(__ubsan_buf, sizeof(__ubsan_buf), "%s\r\n\t%s %s %p %s %s",
"insufficient size", kind, "address", pointer,
"with insufficient space for object of type",
type_mismatch->type->name);

View File

@ -72,7 +72,7 @@ void vflogf_onfail(FILE *f) {
fseek(f, SEEK_SET, 0);
f->beg = f->end = 0;
clearerr(f);
(fprintf)(f, "performed emergency log truncation: %s\n", strerror(err));
(fprintf)(f, "performed emergency log truncation: %s\r\n", strerror(err));
}
}
@ -111,10 +111,10 @@ void(vflogf)(unsigned level, const char *file, int line, FILE *f,
}
(vfprintf)(f, fmt, va);
va_end(va);
fputc('\n', f);
fputs("\r\n", f);
if (level == kLogFatal) {
startfatal(file, line);
(fprintf)(stderr, "fatal error see logfile\n");
(fprintf)(stderr, "fatal error see logfile\r\n");
die();
unreachable;
}

View File

@ -15,7 +15,7 @@
#define TRUE 1
#define FALSE 0
#define ROUNDUP(X, K) (((X) + ((K)-1)) & ~((K)-1))
#define ROUNDUP(X, K) (((X) + (K)-1) & -(K))
#define ROUNDDOWN(X, K) ((X) & -(K))
#define ABS(X) ((X) >= 0 ? (X) : -(X))
#define MIN(X, Y) ((Y) > (X) ? (X) : (Y))

View File

@ -29,4 +29,3 @@
bcopy: jmp memmove
.endfn bcopy,globl
.source __FILE__
.source __FILE__

View File

@ -25,8 +25,10 @@
/ This turns stuff like (char)0xFF into \0377. The returned
/ string is word-encoded, e.g. '\\'|'0'<<010|'3'<<020|etc.
/
/ @param dil contains byte to escape
/ @see libc/nexgen32e/cescapec.c
cescapec:
movzbl %dil,%edi
lea -7(%rdi),%ecx
cmp $85,%cl
ja 1f
@ -63,12 +65,11 @@ cescapec:
ret
.LQM:
#endif
1: movzbl %dil,%eax
cmp $127,%eax
1: mov %edi,%eax
lea -0x20(%rax),%ecx
cmp $0x5E,%ecx
ja 2f
ezlea kCtype,cx
testb $16,(%rcx,%rax)
jne 3f
ret
2: and $-64,%eax
mov %edi,%ecx
and $56,%ecx
@ -78,7 +79,7 @@ cescapec:
or %ecx,%edi
lea (%rdi,%rax,4),%eax
add $'0<<030|'0<<020|'0<<010|'\\,%eax
3: ret
ret
.endfn cescapec,globl
.initro 300,_init_cescapec

View File

@ -1,101 +0,0 @@
/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
vi: set et ft=asm ts=8 tw=8 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/macros.h"
/ Returns nonzero if %dil [ \t\v\r\n].
/
/ @param edi is character to test (treated as uint8_t)
/ @return nonzero if character matches, otherwise 0
isspace:push $1
jmp ctypep
.endfn isspace,globl
/ Returns nonzero if %dil [A-Za-z].
/
/ @param edi is character to test (treated as uint8_t)
/ @return nonzero if character matches, otherwise 0
isalpha:push $2
jmp ctypep
.endfn isalpha,globl
/ Returns nonzero if %dil [0-9].
/
/ @param edi is character to test (treated as uint8_t)
/ @return nonzero if character matches, otherwise 0
isdigit:push $4
jmp ctypep
.endfn isdigit,globl
/ Returns nonzero if %dil [0-9A-Za-z].
/
/ @param edi is character to test (treated as uint8_t)
/ @return nonzero if character matches, otherwise 0
isalnum:push $6
jmp ctypep
.endfn isalnum,globl
/ Returns nonzero if %dil [0-9A-fa-f].
/
/ @param edi is character to test (treated as uint8_t)
/ @return nonzero if character matches, otherwise 0
isxdigit:push $8
jmp ctypep
.endfn isxdigit,globl
/ Returns nonzero if %dil [ -~] a.k.a. [\x20-\x7e].
/
/ @param edi is character to test (treated as uint8_t)
/ @return nonzero if character matches, otherwise 0
isprint:push $16
jmp ctypep
.endfn isprint,globl
/ Returns nonzero if %dil [a-z]
/
/ @param edi is character to test (treated as uint8_t)
/ @return nonzero if character matches, otherwise 0
islower:push $32
jmp ctypep
.endfn islower,globl
/ Returns nonzero if %dil [A-Z]
/
/ @param edi is character to test (treated as uint8_t)
/ @return nonzero if character matches, otherwise 0
isupper:push $64
jmp ctypep
.endfn isupper,globl
/ Returns nonzero if %dil [ \t]
/
/ @param edi is character to test (treated as uint8_t)
/ @return nonzero if character matches, otherwise 0
isblank:push $-128
/ fallthrough
.endfn isblank,globl
ctypep: pop %rsi
movzbl %dil,%edi
ezlea kCtype,cx
movzbl (%rcx,%rdi),%eax
and %esi,%eax
ret
.endfn ctypep
.source __FILE__

View File

@ -1,33 +0,0 @@
/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
vi: set et ft=asm ts=8 tw=8 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/macros.h"
hextoint:
.leafprologue
.profilable
mov %edi,%eax
sar $6,%eax
and $1,%eax
lea (%rax,%rax,8),%eax
add %edi,%eax
and $15,%eax
.leafepilogue
.endfn hextoint,globl
.source __FILE__

View File

@ -1,35 +0,0 @@
/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
vi: set et ft=asm ts=8 tw=8 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/macros.h"
iscntrl:.leafprologue
.profilable
xor %eax,%eax
cmp $255,%edi
ja 1f
movslq %edi,%rdi
ezlea kCtype,cx
mov (%rcx,%rdi),%al
shr $4,%al
xor $1,%eax
and $1,%eax
1: .leafepilogue
.endfn iscntrl,globl
.source __FILE__

View File

@ -1,38 +0,0 @@
/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
vi: set et ft=asm ts=8 tw=8 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/macros.h"
isgraph:.leafprologue
.profilable
xor %eax,%eax
cmp $255,%edi
ja 1f
movslq %edi,%rax
ezlea kCtype,cx
mov (%rcx,%rax),%al
shr $4,%al
mov %eax,%edx
xor %eax,%eax
cmp $32,%edi
setne %al
and %edx,%eax
1: .leafepilogue
.endfn isgraph,globl
.source __FILE__

View File

@ -1,33 +0,0 @@
/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
vi: set et ft=asm ts=8 tw=8 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/macros.h"
iswalnum:
.leafprologue
.profilable
movslq %edi,%rax
cmpl $127,%eax
ja 1f
ezlea kCtype,cx
movb (%rcx,%rax),%al
andl $6,%eax
1: .leafepilogue
.endfn iswalnum,globl
.source __FILE__

View File

@ -1,34 +0,0 @@
/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
vi: set et ft=asm ts=8 tw=8 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/macros.h"
iswalpha:
.leafprologue
.profilable
movslq %edi,%rax
cmpl $127,%eax
ja 1f
ezlea kCtype,cx
movb (%rcx,%rax),%al
andl $2,%eax
movzbl %al,%eax
1: .leafepilogue
.endfn iswalpha,globl
.source __FILE__

View File

@ -1,34 +0,0 @@
/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
vi: set et ft=asm ts=8 tw=8 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/macros.h"
iswblank:
.leafprologue
.profilable
movslq %edi,%rax
cmpl $127,%eax
ja 1f
ezlea kCtype,cx
movb (%rcx,%rax),%al
andl $-128,%eax
movzbl %al,%eax
1: .leafepilogue
.endfn iswblank,globl
.source __FILE__

View File

@ -1,35 +0,0 @@
/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
vi: set et ft=asm ts=8 tw=8 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/macros.h"
iswcntrl:
.leafprologue
.profilable
movslq %edi,%rax
cmp $127,%eax
ja 1f
ezlea kCtype,cx
mov (%rcx,%rax),%al
shr $4,%al
xor $1,%eax
and $1,%eax
1: .leafepilogue
.endfn iswcntrl,globl
.source __FILE__

View File

@ -1,34 +0,0 @@
/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
vi: set et ft=asm ts=8 tw=8 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/macros.h"
iswdigit:
.leafprologue
.profilable
movl %edi,%eax
cmpl $127,%edi
ja 1f
subl $48,%eax
cmpl $9,%eax
setbe %al
movzbl %al,%eax
1: .leafepilogue
.endfn iswdigit,globl
.source __FILE__

View File

@ -1,38 +0,0 @@
/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
vi: set et ft=asm ts=8 tw=8 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/macros.h"
iswgraph:
.leafprologue
.profilable
movl %edi,%eax
cmpl $127,%edi
ja 1f
movslq %edi,%rdx
ezlea kCtype,cx
movb (%rcx,%rdx),%dl
shrb $4,%dl
cmpl $32,%edi
setne %al
movzbl %al,%eax
andl %edx,%eax
1: .leafepilogue
.endfn iswgraph,globl
.source __FILE__

View File

@ -1,34 +0,0 @@
/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
vi: set et ft=asm ts=8 tw=8 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/macros.h"
iswlower:
.leafprologue
.profilable
movslq %edi,%rax
cmpl $127,%eax
ja 1f
ezlea kCtype,cx
movb (%rcx,%rax),%al
andl $32,%eax
movzbl %al,%eax
1: .leafepilogue
.endfn iswlower,globl
.source __FILE__

View File

@ -1,35 +0,0 @@
/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
vi: set et ft=asm ts=8 tw=8 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/macros.h"
iswprint:
.leafprologue
.profilable
movl $1,%eax
cmpl $127,%edi
ja 1f
movslq %edi,%rdi
ezlea kCtype,cx
movb (%rcx,%rdi),%al
shrb $4,%al
andl $1,%eax
1: .leafepilogue
.endfn iswprint,globl
.source __FILE__

View File

@ -1,39 +0,0 @@
/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
vi: set et ft=asm ts=8 tw=8 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/macros.h"
iswpunct:
.leafprologue
.profilable
mov $1,%eax
cmp $127,%edi
ja 1f
movslq %edi,%rdi
ezlea kCtype,cx
mov (%rcx,%rdi),%dl
xor %eax,%eax
test $16,%dl
je 1f
xor %eax,%eax
and $7,%dl
sete %al
1: .leafepilogue
.endfn iswpunct,globl
.source __FILE__

View File

@ -1,33 +0,0 @@
/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
vi: set et ft=asm ts=8 tw=8 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/macros.h"
iswspace:
.leafprologue
.profilable
movslq %edi,%rax
cmpl $127,%eax
ja 1f
ezlea kCtype,cx
movb (%rcx,%rax),%al
andl $1,%eax
1: .leafepilogue
.endfn iswspace,globl
.source __FILE__

View File

@ -1,34 +0,0 @@
/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
vi: set et ft=asm ts=8 tw=8 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/macros.h"
iswupper:
.leafprologue
.profilable
movslq %edi,%rax
cmpl $127,%eax
ja 1f
ezlea kCtype,cx
movb (%rcx,%rax),%al
andl $64,%eax
movzbl %al,%eax
1: .leafepilogue
.endfn iswupper,globl
.source __FILE__

View File

@ -1,34 +0,0 @@
/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
vi: set et ft=asm ts=8 tw=8 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/macros.h"
iswxdigit:
.leafprologue
.profilable
movslq %edi,%rax
cmpl $127,%eax
ja 1f
ezlea kCtype,cx
movb (%rcx,%rax),%al
andl $8,%eax
movzbl %al,%eax
1: .leafepilogue
.endfn iswxdigit,globl
.source __FILE__

Some files were not shown because too many files have changed in this diff Show More