Browse Source

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 1 year ago
parent
commit
9e3e985ae5
  1. 1
      .gitignore
  2. 4
      ape/ape.S
  3. 2
      dsp/core/getintegercoefficients.c
  4. 1
      dsp/scale/gyarados.c
  5. 3
      dsp/tty/hidecursor.c
  6. 2
      dsp/tty/ident.c
  7. 6
      dsp/tty/send.c
  8. 2
      dsp/tty/sendtitle.c
  9. 2
      dsp/tty/ttyraw.c
  10. 2
      dsp/tty/write.c
  11. 136
      examples/acid2.c
  12. 6
      examples/backtrace.c
  13. 4
      examples/examples.mk
  14. 2
      examples/hellojs.c
  15. 2
      examples/ispell.c
  16. 104
      examples/kilo.c
  17. 4
      examples/printargs.c
  18. 10
      examples/printmysymbols.c
  19. 3
      examples/tiny-raw-linux-tutorial.S
  20. 181
      examples/ttyinfo.c
  21. 4
      libc/calls/calls.h
  22. 6
      libc/calls/getitimer.c
  23. 2
      libc/calls/hefty/mkntcmdline.c
  24. 5
      libc/calls/internal.h
  25. 16
      libc/calls/ioctl-tcsets-nt.c
  26. 47
      libc/calls/ioctl-tiocgwinsz-nt.c
  27. 44
      libc/calls/nanosleep-nt.c
  28. 21
      libc/calls/nanosleep-xnu.c
  29. 31
      libc/calls/nanosleep.c
  30. 36
      libc/calls/now.c
  31. 34
      libc/calls/nowl.S
  32. 31
      libc/calls/onntalarm.c
  33. 6
      libc/calls/onntconsoleevent.c
  34. 3
      libc/calls/pread.c
  35. 2
      libc/calls/preadv.c
  36. 9
      libc/calls/pwritev.c
  37. 124
      libc/calls/readansi.c
  38. 104
      libc/calls/setitimer-nt.c
  39. 33
      libc/calls/setitimer.c
  40. 2
      libc/calls/struct/stat.h
  41. 14
      libc/calls/thunks/onntalarm.S
  42. 5
      libc/calls/typedef/sigaction_f.h
  43. 1
      libc/calls/ucontext.h
  44. 5
      libc/calls/writev.c
  45. 9
      libc/conv/conv.h
  46. 3
      libc/conv/conv.mk
  47. 22
      libc/fmt/palandprintf.c
  48. 57
      libc/fmt/stoa.c
  49. 2
      libc/intrin/pcmpgtb.c
  50. 6
      libc/intrin/pmovmskb.c
  51. 6
      libc/intrin/pshufd.c
  52. 4
      libc/intrin/psubb.c
  53. 2
      libc/intrin/psubb.h
  54. 1
      libc/isystem/windows.h
  55. 12
      libc/linux/close.h
  56. 2
      libc/linux/exit.h
  57. 16
      libc/linux/fstat.h
  58. 20
      libc/linux/mmap.h
  59. 15
      libc/linux/munmap.h
  60. 15
      libc/linux/open.h
  61. 2
      libc/linux/read.h
  62. 4
      libc/linux/write.h
  63. 92
      libc/log/asan.c
  64. 12
      libc/log/attachdebugger.c
  65. 5
      libc/log/backtrace.h
  66. 20
      libc/log/backtrace2.c
  67. 14
      libc/log/backtrace3.c
  68. 4
      libc/log/check.h
  69. 2
      libc/log/checkaligned.c
  70. 16
      libc/log/checkfail.c
  71. 2
      libc/log/checkfail_ndebug.c
  72. 4
      libc/log/die.c
  73. 2
      libc/log/gdbexec.c
  74. 8
      libc/log/getsymboltable.c
  75. 1
      libc/log/log.h
  76. 6
      libc/log/malloc_stats.c
  77. 4
      libc/log/meminfo.c
  78. 86
      libc/log/oncrash.c
  79. 6
      libc/log/perror.c
  80. 4
      libc/log/ubsan.c
  81. 6
      libc/log/vflogf.c
  82. 2
      libc/macros.h
  83. 1
      libc/nexgen32e/bcopy.S
  84. 13
      libc/nexgen32e/cescapec.S
  85. 101
      libc/nexgen32e/ctype.S
  86. 33
      libc/nexgen32e/hextoint.S
  87. 35
      libc/nexgen32e/iscntrl.S
  88. 38
      libc/nexgen32e/isgraph.S
  89. 33
      libc/nexgen32e/iswalnum.S
  90. 34
      libc/nexgen32e/iswalpha.S
  91. 34
      libc/nexgen32e/iswblank.S
  92. 35
      libc/nexgen32e/iswcntrl.S
  93. 34
      libc/nexgen32e/iswdigit.S
  94. 38
      libc/nexgen32e/iswgraph.S
  95. 34
      libc/nexgen32e/iswlower.S
  96. 35
      libc/nexgen32e/iswprint.S
  97. 39
      libc/nexgen32e/iswpunct.S
  98. 33
      libc/nexgen32e/iswspace.S
  99. 34
      libc/nexgen32e/iswupper.S
  100. 34
      libc/nexgen32e/iswxdigit.S

1
.gitignore

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

4
ape/ape.S

@ -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

2
dsp/core/getintegercoefficients.c

@ -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]) {

1
dsp/scale/gyarados.c

@ -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;

3
dsp/tty/hidecursor.c

@ -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;

2
dsp/tty/ident.c

@ -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;

6
dsp/tty/send.c

@ -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));
}

2
dsp/tty/sendtitle.c

@ -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.
*

2
dsp/tty/ttyraw.c

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

2
dsp/tty/write.c

@ -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

@ -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;
}

6
examples/backtrace.c

@ -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) {

4
examples/examples.mk

@ -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
$<

2
examples/hellojs.c

@ -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);

2
examples/ispell.c

@ -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.
*

104
examples/kilo.c

@ -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'):

4
examples/printargs.c

@ -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;
}

10
examples/printmysymbols.c

@ -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;
}

3
examples/tiny-raw-linux-tutorial.S

@ -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

181
examples/ttyinfo.c

@ -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 OnResize(void) {
resized_ = true;
void onkilled(int sig) {
killed = true;
}
void OnKilled(int sig) {
longjmp(jb_, 128 + sig + 1);
void restoretty(void) {
write(1, DISABLE_MOUSE_TRACKING, strlen(DISABLE_MOUSE_TRACKING));
ioctl(1, TCSETS, &oldterm);
}
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);
}
if (got_ >= 0) {
printf("%`'.*s\r\n", got_, buf_);
if (got_ > 0 && buf_[0] == CTRL('C')) {
longjmp(jb_, EXIT_SUCCESS + 1);
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 (resized_) {
CHECK_NE(-1, getttysize(fd_, &wsize_));
printf("SIGWINCH → %hu×%hu\r\n", wsize_.ws_row, wsize_.ws_col);
resized_ = false;
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));
}
}
void probe(const char *s) {
printf("%`'s → ", s);
write(fd_, s, strlen(s));
getsome();
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");
}
}
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;
}

4
libc/calls/calls.h

@ -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);

6
libc/time/getitimer.c → libc/calls/getitimer.c

@ -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);
}
}

2
libc/calls/hefty/mkntcmdline.c

@ -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

5
libc/calls/internal.h

@ -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;

16
libc/calls/ioctl-tcsets-nt.c

@ -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 {

47
libc/calls/ioctl-tiocgwinsz-nt.c

@ -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;
}

44
libc/calls/nanosleep-nt.c

@ -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;
}

21
libc/log/getsymbolname.c → libc/calls/nanosleep-xnu.c

@ -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});
}

31
libc/calls/nanosleep.c

@ -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);
}
}

36
libc/calls/now.c

@ -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;
}
})

34
libc/nexgen32e/ispunct.S → libc/calls/nowl.S

@ -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__

31
libc/calls/onntalarm.c

@ -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);
}

6
libc/calls/onntconsoleevent.c

@ -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;

3
libc/calls/pread.c