-
-
Notifications
You must be signed in to change notification settings - Fork 766
Expand file tree
/
Copy pathread-nt.c
More file actions
843 lines (773 loc) · 25.5 KB
/
Copy pathread-nt.c
File metadata and controls
843 lines (773 loc) · 25.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
╞══════════════════════════════════════════════════════════════════════════════╡
│ Copyright 2020 Justine Alexandra Roberts Tunney │
│ │
│ Permission to use, copy, modify, and/or distribute this software for │
│ any purpose with or without fee is hereby granted, provided that the │
│ above copyright notice and this permission notice appear in all copies. │
│ │
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
│ PERFORMANCE OF THIS SOFTWARE. │
╚─────────────────────────────────────────────────────────────────────────────*/
#include "libc/assert.h"
#include "libc/atomic.h"
#include "libc/calls/createfileflags.internal.h"
#include "libc/calls/internal.h"
#include "libc/calls/sig.internal.h"
#include "libc/calls/state.internal.h"
#include "libc/calls/struct/fd.internal.h"
#include "libc/calls/struct/iovec.h"
#include "libc/calls/struct/sigset.internal.h"
#include "libc/calls/syscall_support-nt.internal.h"
#include "libc/cosmo.h"
#include "libc/errno.h"
#include "libc/fmt/itoa.h"
#include "libc/intrin/atomic.h"
#include "libc/intrin/describeflags.internal.h"
#include "libc/intrin/dll.h"
#include "libc/intrin/nomultics.internal.h"
#include "libc/intrin/strace.internal.h"
#include "libc/intrin/weaken.h"
#include "libc/macros.internal.h"
#include "libc/mem/mem.h"
#include "libc/nt/console.h"
#include "libc/nt/createfile.h"
#include "libc/nt/enum/accessmask.h"
#include "libc/nt/enum/consolemodeflags.h"
#include "libc/nt/enum/creationdisposition.h"
#include "libc/nt/enum/filesharemode.h"
#include "libc/nt/enum/vk.h"
#include "libc/nt/errors.h"
#include "libc/nt/runtime.h"
#include "libc/nt/struct/inputrecord.h"
#include "libc/nt/synchronization.h"
#include "libc/str/str.h"
#include "libc/str/utf16.h"
#include "libc/sysv/consts/o.h"
#include "libc/sysv/consts/sig.h"
#include "libc/sysv/errfuns.h"
#include "libc/thread/posixthread.internal.h"
#include "libc/thread/thread.h"
#include "libc/thread/tls.h"
#ifdef __x86_64__
/**
* @fileoverview Cosmopolitan Standard Input
*
* This file implements pollable terminal i/o for Windows consoles. On
* Windows 10 the "virtual terminal processing" feature works great on
* output but their solution for input processing isn't good enough to
* support running Linux programs like Emacs. This polyfill fixes that
* and it most importantly ensures we can poll() standard input, which
* would otherwise have been impossible. We aren't using threads. What
* we do instead is have termios behaviors e.g. canonical mode editing
* happen on demand as a side effect of read/poll/ioctl activity.
*/
struct VirtualKey {
int vk;
int normal_str;
int shift_str;
int ctrl_str;
int shift_ctrl_str;
};
#define S(s) W(s "\0\0")
#define W(s) (s[3] << 24 | s[2] << 16 | s[1] << 8 | s[0])
static const struct VirtualKey kVirtualKey[] = {
{kNtVkUp, S("A"), S("1;2A"), S("1;5A"), S("1;6A")},
{kNtVkDown, S("B"), S("1;2B"), S("1;5B"), S("1;6B")},
{kNtVkRight, S("C"), S("1;2C"), S("1;5C"), S("1;6C")},
{kNtVkLeft, S("D"), S("1;2D"), S("1;5D"), S("1;6D")},
{kNtVkInsert, S("2~"), S("2;2~"), S("2;5~"), S("2;6~")},
{kNtVkDelete, S("3~"), S("3;2~"), S("3;5~"), S("3;6~")},
{kNtVkHome, S("H"), S("1;2H"), S("1;5H"), S("1;6H")},
{kNtVkEnd, S("F"), S("1;2F"), S("1;5F"), S("1;6F")},
{kNtVkPrior, S("5~"), S("5;2~"), S("5;5~"), S("5;6~")},
{kNtVkNext, S("6~"), S("6;2~"), S("6;5~"), S("6;6~")},
{kNtVkF1, -S("OP"), S("1;2P"), S("11^"), S("1;6P")},
{kNtVkF2, -S("OQ"), S("1;2Q"), S("12^"), S("1;6Q")},
{kNtVkF3, -S("OR"), S("1;2R"), S("13^"), S("1;6R")},
{kNtVkF4, -S("OS"), S("1;2S"), S("14^"), S("1;6S")},
{kNtVkF5, S("15~"), S("28~"), S("15^"), S("28^")},
{kNtVkF6, S("17~"), S("29~"), S("17^"), S("29^")},
{kNtVkF7, S("18~"), S("31~"), S("18^"), S("31^")},
{kNtVkF8, S("19~"), S("32~"), S("19^"), S("32^")},
{kNtVkF9, S("20~"), S("33~"), S("20^"), S("33^")},
{kNtVkF10, S("21~"), S("34~"), S("21^"), S("34^")},
{kNtVkF11, S("23~"), S("23$"), S("23^"), S("23@")},
{kNtVkF12, S("24~"), S("24$"), S("24^"), S("24@")},
{0},
};
// TODO: How can we configure `less` to not need this bloat?
static const struct VirtualKey kDecckm[] = {
{kNtVkUp, -S("OA"), -S("OA"), S("A"), S("A")},
{kNtVkDown, -S("OB"), -S("OB"), S("B"), S("B")},
{kNtVkRight, -S("OC"), -S("OC"), S("C"), S("C")},
{kNtVkLeft, -S("OD"), -S("OD"), S("D"), S("D")},
{kNtVkPrior, S("5~"), S("5;2~"), S("5;5~"), S("5;6~")},
{kNtVkNext, S("6~"), S("6;2~"), S("6;5~"), S("6;6~")},
{0},
};
#define KEYSTROKE_CONTAINER(e) DLL_CONTAINER(struct Keystroke, elem, e)
struct Keystroke {
char buf[23];
unsigned char buflen;
struct Dll elem;
};
struct Keystrokes {
atomic_uint once;
bool end_of_file;
bool ohno_decckm;
uint16_t utf16hs;
atomic_int_fast16_t used;
int64_t cin, cot;
struct Dll *list;
struct Dll *line;
struct Dll *free;
pthread_mutex_t lock;
const struct VirtualKey *vkt;
struct Keystroke pool[512];
};
static struct Keystrokes __keystroke;
textwindows void __keystroke_wipe(void) {
bzero(&__keystroke, sizeof(__keystroke));
}
static textwindows void OpenConsole(void) {
__keystroke.vkt = kVirtualKey;
__keystroke.cin = CreateFile(u"CONIN$", kNtGenericRead | kNtGenericWrite,
kNtFileShareRead, 0, kNtOpenExisting, 0, 0);
__keystroke.cot = CreateFile(u"CONOUT$", kNtGenericRead | kNtGenericWrite,
kNtFileShareWrite, 0, kNtOpenExisting, 0, 0);
}
static textwindows int AddSignal(int sig) {
atomic_fetch_or_explicit(&__get_tls()->tib_sigpending, 1ull << (sig - 1),
memory_order_relaxed);
return 0;
}
static textwindows void InitConsole(void) {
cosmo_once(&__keystroke.once, OpenConsole);
}
static textwindows void LockKeystrokes(void) {
pthread_mutex_lock(&__keystroke.lock);
}
static textwindows void UnlockKeystrokes(void) {
pthread_mutex_unlock(&__keystroke.lock);
}
textwindows int64_t GetConsoleInputHandle(void) {
InitConsole();
return __keystroke.cin;
}
textwindows int64_t GetConsoleOutputHandle(void) {
InitConsole();
return __keystroke.cot;
}
static textwindows bool IsMouseModeCommand(int x) {
return x == 1000 || // SET_VT200_MOUSE
x == 1002 || // SET_BTN_EVENT_MOUSE
x == 1006 || // SET_SGR_EXT_MODE_MOUSE
x == 1015; // SET_URXVT_EXT_MODE_MOUSE
}
static textwindows int GetVirtualKey(uint16_t vk, bool shift, bool ctrl) {
for (int i = 0; __keystroke.vkt[i].vk; ++i) {
if (__keystroke.vkt[i].vk == vk) {
if (shift && ctrl) {
return __keystroke.vkt[i].shift_ctrl_str;
} else if (shift) {
return __keystroke.vkt[i].shift_str;
} else if (ctrl) {
return __keystroke.vkt[i].ctrl_str;
} else {
return __keystroke.vkt[i].normal_str;
}
}
}
return 0;
}
static textwindows int ProcessKeyEvent(const struct NtInputRecord *r, char *p) {
uint32_t c = r->Event.KeyEvent.uChar.UnicodeChar;
uint16_t vk = r->Event.KeyEvent.wVirtualKeyCode;
uint16_t cks = r->Event.KeyEvent.dwControlKeyState;
// ignore keyup events
if (!r->Event.KeyEvent.bKeyDown && (!c || vk != kNtVkMenu)) {
return 0;
}
#if 0
// this code is useful for troubleshooting why keys don't work
kprintf("bKeyDown=%hhhd wVirtualKeyCode=%s wVirtualScanCode=%s "
"UnicodeChar=%#x[%#lc] dwControlKeyState=%s\n",
r->Event.KeyEvent.bKeyDown,
DescribeVirtualKeyCode(r->Event.KeyEvent.wVirtualKeyCode),
DescribeVirtualKeyCode(r->Event.KeyEvent.wVirtualScanCode),
r->Event.KeyEvent.uChar.UnicodeChar,
r->Event.KeyEvent.uChar.UnicodeChar,
DescribeControlKeyState(r->Event.KeyEvent.dwControlKeyState));
#endif
// turn arrow/function keys into vt100/ansi/xterm byte sequences
int n = 0;
int v = GetVirtualKey(vk, !!(cks & kNtShiftPressed),
!!(cks & (kNtLeftCtrlPressed | kNtRightCtrlPressed)));
if (v) {
p[n++] = 033;
if (cks & (kNtLeftAltPressed | kNtRightAltPressed)) {
p[n++] = 033;
}
if (v > 0) {
p[n++] = '[';
} else {
v = -v;
}
do p[n++] = v;
while ((v >>= 8));
return n;
}
// ^/ (crtl+slash) maps to ^_ (ctrl-hyphen) on linux
if (vk == kNtVkOem_2 && (cks & (kNtLeftCtrlPressed | kNtRightCtrlPressed))) {
p[n++] = 037;
return n;
}
// handle cases where win32 doesn't provide character
if (!c) {
if (vk == '2' && (cks & (kNtLeftCtrlPressed | kNtRightCtrlPressed))) {
c = 0; // ctrl-2 → "\000"
} else if (isascii(vk) && isdigit(vk) &&
(cks & (kNtLeftCtrlPressed | kNtRightCtrlPressed))) {
c = 030 + (vk - '0'); // e.g. ctrl-3 → "\033"
} else if (isascii(vk) && isgraph(vk) &&
(cks & (kNtLeftCtrlPressed | kNtRightCtrlPressed))) {
c = vk ^ 0100; // e.g. ctrl-alt-b → "\033\002"
} else {
return 0;
}
}
// convert utf-16 to utf-32
if (IsHighSurrogate(c)) {
__keystroke.utf16hs = c;
return 0;
}
if (IsLowSurrogate(c)) {
c = MergeUtf16(__keystroke.utf16hs, c);
}
// enter sends \r with raw terminals
// make it a multics newline instead
if (c == '\r' && !(__ttyconf.magic & kTtyNoCr2Nl)) {
c = '\n';
}
// ctrl-space (^@) is literally zero
if (c == ' ' && (cks & (kNtLeftCtrlPressed | kNtRightCtrlPressed))) {
c = '\0';
}
// make backspace (^?) distinguishable from ctrl-h (^H)
if (c == kNtVkBack && !(cks & (kNtLeftCtrlPressed | kNtRightCtrlPressed))) {
c = 0177;
}
// handle ctrl-\ and ctrl-c
// note we define _POSIX_VDISABLE as zero
// tcsetattr() lets anyone reconfigure these keybindings
if (c && !(__ttyconf.magic & kTtyNoIsigs)) {
if (c == __ttyconf.vintr) {
return AddSignal(SIGINT);
} else if (c == __ttyconf.vquit) {
return AddSignal(SIGQUIT);
}
}
// handle ctrl-d which generates end-of-file, unless pending line data
// is present, in which case we flush that without the newline instead
if (c && c == __ttyconf.veof && !(__ttyconf.magic & kTtyUncanon)) {
if (dll_is_empty(__keystroke.line)) {
__keystroke.end_of_file = true;
} else {
dll_make_last(&__keystroke.list, __keystroke.line);
__keystroke.line = 0;
}
return 0;
}
// insert esc prefix when alt is held
// for example "h" becomes "\033h" (alt-h)
// if up arrow is "\033[A" then alt-up is "\033\033[A"
if ((cks & (kNtLeftAltPressed | kNtRightAltPressed)) &&
r->Event.KeyEvent.bKeyDown) {
p[n++] = 033;
}
// finally apply thompson-pike varint encoding
uint64_t w = tpenc(c);
do p[n++] = w;
while ((w >>= 8));
return n;
}
// To use the tty mouse events feature:
// - write(1, "\e[?1000;1002;1015;1006h") to enable
// - write(1, "\e[?1000;1002;1015;1006l") to disable
// See o//examples/ttyinfo.com and o//tool/viz/life.com
static textwindows int ProcessMouseEvent(const struct NtInputRecord *r,
char *b) {
char *p = b;
unsigned char e = 0;
uint32_t currentbs = __ttyconf.mousebs;
uint32_t ev = r->Event.MouseEvent.dwEventFlags;
uint32_t bs = r->Event.MouseEvent.dwButtonState;
ev &= kNtMouseMoved | kNtMouseWheeled;
bs &= kNtFromLeft1stButtonPressed | kNtRightmostButtonPressed;
if (ev & kNtMouseWheeled) {
// scroll wheel (unnatural mode)
bool isup = ((int)r->Event.MouseEvent.dwButtonState >> 16) > 0;
if (__ttyconf.magic & kTtyXtMouse) {
if (r->Event.MouseEvent.dwControlKeyState &
(kNtLeftCtrlPressed | kNtRightCtrlPressed)) {
e = isup ? 80 : 81;
} else {
e = isup ? 64 : 65;
}
goto OutputXtermMouseEvent;
} else if (!(r->Event.MouseEvent.dwControlKeyState &
(kNtShiftPressed | kNtLeftCtrlPressed | kNtRightCtrlPressed |
kNtLeftAltPressed | kNtRightAltPressed))) {
// 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++ = !__keystroke.ohno_decckm ? '[' : 'O';
if (isup) {
*p++ = 'A';
} else {
*p++ = 'B';
}
}
} else if ((bs || currentbs) && (__ttyconf.magic & kTtyXtMouse)) {
if (bs && (ev & kNtMouseMoved) && currentbs) {
e |= 32; // dragging
}
if ((bs | currentbs) & kNtRightmostButtonPressed) {
e |= 2; // right
}
OutputXtermMouseEvent:
*p++ = 033;
*p++ = '[';
*p++ = '<';
p = FormatInt32(p, e);
*p++ = ';';
p = FormatInt32(p, (r->Event.MouseEvent.dwMousePosition.X + 1) & 0x7fff);
*p++ = ';';
p = FormatInt32(p, (r->Event.MouseEvent.dwMousePosition.Y + 1) & 0x7fff);
if (!bs && currentbs) {
*p++ = 'm'; // up
} else {
*p++ = 'M'; // down
}
__ttyconf.mousebs = bs;
}
return p - b;
}
static textwindows int ConvertConsoleInputToAnsi(const struct NtInputRecord *r,
char p[hasatleast 23]) {
switch (r->EventType) {
case kNtKeyEvent:
return ProcessKeyEvent(r, p);
case kNtMouseEvent:
return ProcessMouseEvent(r, p);
case kNtWindowBufferSizeEvent:
return AddSignal(SIGWINCH);
default:
return 0;
}
}
static textwindows struct Keystroke *NewKeystroke(void) {
struct Dll *e;
struct Keystroke *k = 0;
int i, n = ARRAYLEN(__keystroke.pool);
if (atomic_load_explicit(&__keystroke.used, memory_order_acquire) < n &&
(i = atomic_fetch_add(&__keystroke.used, 1)) < n) {
k = __keystroke.pool + i;
} else {
if ((e = dll_first(__keystroke.free))) {
k = KEYSTROKE_CONTAINER(e);
dll_remove(&__keystroke.free, &k->elem);
}
if (!k) {
if (_weaken(malloc)) {
k = _weaken(malloc)(sizeof(struct Keystroke));
} else {
enomem();
return 0;
}
}
}
if (k) {
bzero(k, sizeof(*k));
dll_init(&k->elem);
}
return k;
}
static textwindows void WriteTty(const char *p, size_t n) {
WriteFile(__keystroke.cot, p, n, 0, 0);
}
static textwindows bool IsCtl(int c) {
return isascii(c) && iscntrl(c) && c != '\n' && c != '\t';
}
static textwindows void WriteCtl(const char *p, size_t n) {
size_t i;
for (i = 0; i < n; ++i) {
if (IsCtl(p[i])) {
char ctl[2];
ctl[0] = '^';
ctl[1] = p[i] ^ 0100;
WriteTty(ctl, 2);
} else {
WriteTty(p + i, 1);
}
}
}
static textwindows void EchoTty(const char *p, size_t n) {
if (__ttyconf.magic & kTtyEchoRaw) {
WriteTty(p, n);
} else {
WriteCtl(p, n);
}
}
static textwindows void EraseCharacter(void) {
WriteTty("\b \b", 3);
}
static textwindows bool EraseKeystroke(void) {
struct Dll *e;
if ((e = dll_last(__keystroke.line))) {
struct Keystroke *k = KEYSTROKE_CONTAINER(e);
dll_remove(&__keystroke.line, e);
dll_make_first(&__keystroke.free, e);
for (int i = k->buflen; i--;) {
if ((k->buf[i] & 0300) == 0200) continue; // utf-8 cont
EraseCharacter();
if (!(__ttyconf.magic & kTtyEchoRaw) && IsCtl(k->buf[i])) {
EraseCharacter();
}
}
return true;
} else {
return false;
}
}
static textwindows void IngestConsoleInputRecord(struct NtInputRecord *r) {
// convert win32 console event into ansi
int len;
char buf[23];
if (!(len = ConvertConsoleInputToAnsi(r, buf))) {
return;
}
// handle backspace in canonical mode
if (len == 1 && buf[0] && //
(buf[0] & 255) == __ttyconf.verase && //
!(__ttyconf.magic & kTtyUncanon)) {
EraseKeystroke();
return;
}
// handle kill in canonical mode
if (len == 1 && buf[0] && //
(buf[0] & 255) == __ttyconf.vkill && //
!(__ttyconf.magic & kTtyUncanon)) {
while (EraseKeystroke()) {
}
return;
}
// allocate object to hold keystroke
struct Keystroke *k;
if (!(k = NewKeystroke())) {
STRACE("out of keystroke memory");
return;
}
memcpy(k->buf, buf, sizeof(k->buf));
k->buflen = len;
// echo input if it was successfully recorded
// assuming the win32 console isn't doing it already
if (!(__ttyconf.magic & kTtySilence)) {
EchoTty(buf, len);
}
// save keystroke to appropriate list
if (__ttyconf.magic & kTtyUncanon) {
dll_make_last(&__keystroke.list, &k->elem);
} else {
dll_make_last(&__keystroke.line, &k->elem);
// handle enter in canonical mode
if (len == 1 && buf[0] &&
((buf[0] & 255) == '\n' || //
(buf[0] & 255) == __ttyconf.veol || //
(buf[0] & 255) == __ttyconf.veol2)) {
dll_make_last(&__keystroke.list, __keystroke.line);
__keystroke.line = 0;
}
}
}
static textwindows void IngestConsoleInput(void) {
uint32_t i, n;
struct NtInputRecord records[16];
for (;;) {
if (__keystroke.end_of_file) return;
if (!GetNumberOfConsoleInputEvents(__keystroke.cin, &n)) {
goto UnexpectedEof;
}
if (!n) return;
n = MIN(ARRAYLEN(records), n);
if (!ReadConsoleInput(__keystroke.cin, records, n, &n)) {
goto UnexpectedEof;
}
for (i = 0; i < n && !__keystroke.end_of_file; ++i) {
IngestConsoleInputRecord(records + i);
}
}
UnexpectedEof:
STRACE("console read error %d", GetLastError());
__keystroke.end_of_file = true;
}
// Discards all unread stdin bytes.
textwindows int FlushConsoleInputBytes(void) {
int rc;
BLOCK_SIGNALS;
InitConsole();
LockKeystrokes();
FlushConsoleInputBuffer(__keystroke.cin);
dll_make_first(&__keystroke.free, __keystroke.list);
__keystroke.list = 0;
dll_make_first(&__keystroke.free, __keystroke.line);
__keystroke.line = 0;
rc = 0;
UnlockKeystrokes();
ALLOW_SIGNALS;
return rc;
}
// Returns number of stdin bytes that may be read without blocking.
textwindows int CountConsoleInputBytes(void) {
struct Dll *e;
int count = 0;
BLOCK_SIGNALS;
InitConsole();
LockKeystrokes();
IngestConsoleInput();
for (e = dll_first(__keystroke.list); e; e = dll_next(__keystroke.list, e)) {
count += KEYSTROKE_CONTAINER(e)->buflen;
}
if (!count && __keystroke.end_of_file) {
count = -1;
}
UnlockKeystrokes();
ALLOW_SIGNALS;
return count;
}
// Intercept ANSI TTY commands that enable features.
textwindows void InterceptTerminalCommands(const char *data, size_t size) {
int i;
unsigned x;
bool ismouse;
uint32_t cm, cm2;
enum { ASC, ESC, CSI, CMD } t;
GetConsoleMode(GetConsoleInputHandle(), &cm), cm2 = cm;
for (ismouse = false, x = i = t = 0; i < size; ++i) {
switch (t) {
case ASC:
if (data[i] == 033) {
t = ESC;
}
break;
case ESC:
if (data[i] == '[') {
t = CSI;
} else {
t = ASC;
}
break;
case CSI:
if (data[i] == '?') {
t = CMD;
x = 0;
} else {
t = ASC;
}
break;
case CMD:
if ('0' <= data[i] && data[i] <= '9') {
x *= 10;
x += data[i] - '0';
} else if (data[i] == ';') {
ismouse |= IsMouseModeCommand(x);
x = 0;
} 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;
cm2 &= ~kNtEnableQuickEditMode; // take mouse
}
t = ASC;
} 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
}
t = ASC;
}
break;
default:
__builtin_unreachable();
}
}
if (cm2 != cm) {
SetConsoleMode(GetConsoleInputHandle(), cm2);
}
}
static textwindows bool DigestConsoleInput(char *data, size_t size, int *rc) {
// handle eof once available input is consumed
if (dll_is_empty(__keystroke.list) && __keystroke.end_of_file) {
*rc = 0;
return true;
}
// copy keystroke(s) into user buffer
int toto = 0;
struct Dll *e;
while (size && (e = dll_first(__keystroke.list))) {
struct Keystroke *k = KEYSTROKE_CONTAINER(e);
uint32_t got = MIN(size, k->buflen);
uint32_t remain = k->buflen - got;
if (got) {
memcpy(data, k->buf, got);
data += got;
size -= got;
toto += got;
}
if (remain) {
memmove(k->buf, k->buf + got, remain);
} else {
dll_remove(&__keystroke.list, e);
dll_make_first(&__keystroke.free, e);
}
k->buflen = remain;
if ((__ttyconf.magic & kTtyUncanon) && toto >= __ttyconf.vmin) {
break;
}
}
// return result
if (toto) {
*rc = toto;
return true;
} else {
return false;
}
}
static textwindows int WaitForConsole(struct Fd *f, sigset_t waitmask) {
int rc;
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
} else {
ms = __ttyconf.vtime * 100;
}
}
if (f->flags & _O_NONBLOCK) {
return eagain(); // standard unix non-blocking
}
pt = _pthread_self();
pt->pt_flags |= PT_RESTARTABLE;
pt->pt_semaphore = sem = CreateSemaphore(0, 0, 1, 0);
pthread_cleanup_push((void *)CloseHandle, (void *)sem);
atomic_store_explicit(&pt->pt_blocker, PT_BLOCKER_SEM, memory_order_release);
m = __sig_beginwait(waitmask);
if ((rc = _check_signal(true)) != -1) {
int64_t hands[2] = {sem, __keystroke.cin};
if (WaitForMultipleObjects(2, hands, 0, ms) != -1u) {
if (!(pt->pt_flags & PT_RESTARTABLE)) rc = eintr();
rc |= _check_signal(true);
} else {
rc = __winerr();
}
}
__sig_finishwait(m);
atomic_store_explicit(&pt->pt_blocker, PT_BLOCKER_CPU, memory_order_release);
pt->pt_flags &= ~PT_RESTARTABLE;
pthread_cleanup_pop(true);
return rc;
}
static textwindows ssize_t ReadFromConsole(struct Fd *f, void *data,
size_t size, sigset_t waitmask) {
int rc;
bool done = false;
InitConsole();
do {
LockKeystrokes();
IngestConsoleInput();
done = DigestConsoleInput(data, size, &rc);
UnlockKeystrokes();
} while (!done && !(rc = WaitForConsole(f, waitmask)));
return rc;
}
textwindows ssize_t sys_read_nt_impl(int fd, void *data, size_t size,
int64_t offset, sigset_t waitmask) {
// switch to terminal polyfill if reading from win32 console
struct Fd *f = g_fds.p + fd;
if (f->kind == kFdDevNull) {
return 0;
}
if (f->kind == kFdConsole) {
return ReadFromConsole(f, data, size, waitmask);
}
// perform heavy lifting
ssize_t rc;
rc = sys_readwrite_nt(fd, data, size, offset, f->handle, waitmask, ReadFile);
if (rc != -2) return rc;
// mops up win32 errors
switch (GetLastError()) {
case kNtErrorBrokenPipe: // broken pipe
case kNtErrorNoData: // closing named pipe
case kNtErrorHandleEof: // pread read past EOF
return 0; //
case kNtErrorAccessDenied: // read doesn't return EACCESS
return ebadf(); //
default:
return __winerr();
}
}
static textwindows ssize_t sys_read_nt2(int fd, const struct iovec *iov,
size_t iovlen, int64_t opt_offset,
sigset_t waitmask) {
ssize_t rc;
size_t i, total;
if (opt_offset < -1) return einval();
while (iovlen && !iov[0].iov_len) iov++, iovlen--;
if (iovlen) {
for (total = i = 0; i < iovlen; ++i) {
if (!iov[i].iov_len) continue;
rc = sys_read_nt_impl(fd, iov[i].iov_base, iov[i].iov_len, opt_offset,
waitmask);
if (rc == -1) {
if (total && errno != ECANCELED) {
return total;
} else {
return -1;
}
}
total += rc;
if (opt_offset != -1) opt_offset += rc;
if (rc < iov[i].iov_len) break;
waitmask = -1; // disable eintr/ecanceled for remaining iovecs
}
return total;
} else {
return sys_read_nt_impl(fd, NULL, 0, opt_offset, waitmask);
}
}
textwindows ssize_t sys_read_nt(int fd, const struct iovec *iov, size_t iovlen,
int64_t opt_offset) {
ssize_t rc;
sigset_t m = __sig_block();
rc = sys_read_nt2(fd, iov, iovlen, opt_offset, m);
__sig_unblock(m);
return rc;
}
#endif /* __x86_64__ */