Make some quick fixes

This commit is contained in:
Justine Tunney 2023-10-08 17:56:59 -07:00
parent 94dc7a684e
commit 820c3599ed
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
10 changed files with 50 additions and 358 deletions

View file

@ -143,33 +143,27 @@ static void GetRandomArnd(char *p, size_t n) {
}
static ssize_t GetRandomBsd(char *p, size_t n, void impl(char *, size_t)) {
errno_t e;
size_t m, i;
if (_weaken(pthread_testcancel_np) &&
(e = _weaken(pthread_testcancel_np)())) {
errno = e;
return -1;
}
for (i = 0;;) {
m = MIN(n - i, 256);
impl(p + i, m);
if ((i += m) == n) {
return n;
}
if (_weaken(pthread_testcancel)) {
_weaken(pthread_testcancel)();
}
}
}
static ssize_t GetDevUrandom(char *p, size_t n) {
int fd;
ssize_t rc;
BLOCK_SIGNALS;
fd = sys_openat(AT_FDCWD, "/dev/urandom", O_RDONLY | O_CLOEXEC, 0);
if (fd == -1) return -1;
pthread_cleanup_push((void *)sys_close, (void *)(intptr_t)fd);
rc = sys_read(fd, p, n);
pthread_cleanup_pop(1);
if (fd != -1) {
rc = sys_read(fd, p, n);
} else {
rc = -1;
}
ALLOW_SIGNALS;
return rc;
}
@ -252,7 +246,7 @@ ssize_t getrandom(void *p, size_t n, unsigned f) {
ssize_t rc;
if ((!p && n) || (IsAsan() && !__asan_is_valid(p, n))) {
rc = efault();
} else if ((f & ~(GRND_RANDOM | GRND_NONBLOCK))) {
} else if (f & ~(GRND_RANDOM | GRND_NONBLOCK)) {
rc = einval();
} else {
rc = __getrandom(p, n, f);

View file

@ -133,11 +133,12 @@ struct Keystrokes {
struct Dll *line;
struct Dll *free;
bool end_of_file;
bool ohno_decckm;
unsigned char pc;
uint16_t utf16hs;
pthread_mutex_t lock;
struct Keystroke pool[512];
const struct VirtualKey *vkt;
struct Keystroke pool[512];
};
static struct Keystrokes __keystroke;
@ -352,11 +353,11 @@ static textwindows int ProcessMouseEvent(const struct NtInputRecord *r,
// we disable mouse highlighting when the tty is put in raw mode
// to mouse wheel events with widely understood vt100 arrow keys
*p++ = 033;
*p++ = '[';
*p++ = !__keystroke.ohno_decckm ? '[' : 'O';
if (isup) {
*p++ = 'A'; // \e[A up
*p++ = 'A';
} else {
*p++ = 'B'; // \e[B down
*p++ = 'B';
}
}
}
@ -637,6 +638,7 @@ textwindows void InterceptTerminalCommands(const char *data, size_t size) {
} else if (data[i] == 'h') {
if (x == 1) {
__keystroke.vkt = kDecckm; // \e[?1h decckm on
__keystroke.ohno_decckm = true;
} else if ((ismouse |= IsMouseModeCommand(x))) {
__ttyconf.magic |= kTtyXtMouse;
cm2 |= kNtEnableMouseInput;
@ -646,6 +648,7 @@ textwindows void InterceptTerminalCommands(const char *data, size_t size) {
} else if (data[i] == 'l') {
if (x == 1) {
__keystroke.vkt = kVirtualKey; // \e[?1l decckm off
__keystroke.ohno_decckm = false;
} else if ((ismouse |= IsMouseModeCommand(x))) {
__ttyconf.magic &= ~kTtyXtMouse;
cm2 |= kNtEnableQuickEditMode; // release mouse
@ -709,6 +712,7 @@ static textwindows int WaitForConsole(struct Fd *f, sigset_t waitmask) {
sigset_t m;
int64_t sem;
uint32_t ms = -1u;
struct PosixThread *pt;
if (!__ttyconf.vmin) {
if (!__ttyconf.vtime) {
return 0; // non-blocking w/o raising eagain
@ -719,7 +723,7 @@ static textwindows int WaitForConsole(struct Fd *f, sigset_t waitmask) {
if (f->flags & O_NONBLOCK) {
return eagain(); // standard unix non-blocking
}
struct PosixThread *pt = _pthread_self();
pt = _pthread_self();
pt->pt_flags |= PT_RESTARTABLE;
pt->pt_semaphore = sem = CreateSemaphore(0, 0, 1, 0);
pthread_cleanup_push((void *)CloseHandle, (void *)sem);
@ -733,8 +737,8 @@ static textwindows int WaitForConsole(struct Fd *f, sigset_t waitmask) {
}
__sig_finishwait(m);
atomic_store_explicit(&pt->pt_blocker, PT_BLOCKER_CPU, memory_order_release);
pthread_cleanup_pop(true);
pt->pt_flags &= ~PT_RESTARTABLE;
pthread_cleanup_pop(true);
return rc;
}

View file

@ -20,7 +20,6 @@
#include "libc/log/libfatal.internal.h"
#include "libc/nt/struct/context.h"
#include "libc/str/str.h"
#ifdef __x86_64__
textwindows void _ntcontext2linux(ucontext_t *ctx, const struct NtContext *cr) {

View file

@ -42,8 +42,6 @@ static struct {
* @note this function takes 5 cycles (30 if `__threaded`)
* @note this function is not intended for cryptography
* @note this function passes bigcrush and practrand
* @asyncsignalsafe
* @vforksafe
*/
uint64_t _rand64(void) {
void *p;

View file

@ -136,7 +136,7 @@ static abi wontreturn void WinInit(const char16_t *cmdline) {
m |= kNtEnableMouseInput | kNtEnableWindowInput |
kNtEnableProcessedInput;
} else {
m |= kNtEnableVirtualTerminalProcessing;
m |= kNtEnableProcessedOutput | kNtEnableVirtualTerminalProcessing;
}
__imp_SetConsoleMode(h, m);
}