-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathconsole.cc
4336 lines (4045 loc) · 121 KB
/
console.cc
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
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* fhandler_console.cc
This file is part of Cygwin.
This software is a copyrighted work licensed under the terms of the
Cygwin license. Please consult the file "CYGWIN_LICENSE" for
details. */
#include "winsup.h"
#include "miscfuncs.h"
#include <stdio.h>
#include <stdlib.h>
#include <wchar.h>
#include <ctype.h>
#include <sys/param.h>
#include <sys/cygwin.h>
#include <cygwin/kd.h>
#include <unistd.h>
#include "cygerrno.h"
#include "security.h"
#include "path.h"
#include "fhandler.h"
#include "dtable.h"
#include "cygheap.h"
#include "sigproc.h"
#include "pinfo.h"
#include "shared_info.h"
#include "cygtls.h"
#include "tls_pbuf.h"
#include "registry.h"
#include <asm/socket.h>
#include "sync.h"
#include "child_info.h"
#include "cygwait.h"
#include "winf.h"
/* Don't make this bigger than NT_MAX_PATH as long as the temporary buffer
is allocated using tmp_pathbuf!!! */
#define CONVERT_LIMIT NT_MAX_PATH
#define ALT_PRESSED (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED)
#define CTRL_PRESSED (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED)
#define con (shared_console_info->con)
#define srTop (con.b.srWindow.Top + con.scroll_region.Top)
#define srBottom ((con.scroll_region.Bottom < 0) ? \
con.b.srWindow.Bottom : \
con.b.srWindow.Top + con.scroll_region.Bottom)
#define con_is_legacy (shared_console_info && con.is_legacy)
#define CONS_THREAD_SYNC "cygcons.thread_sync"
static bool NO_COPY master_thread_started = false;
const unsigned fhandler_console::MAX_WRITE_CHARS = 16384;
fhandler_console::console_state NO_COPY *fhandler_console::shared_console_info;
bool NO_COPY fhandler_console::invisible_console;
/* con_ra is shared in the same process.
Only one console can exist in a process, therefore, static is suitable. */
static struct fhandler_base::rabuf_t con_ra;
/* Write pending buffer for ESC sequence handling
in xterm compatible mode */
static wchar_t last_char;
/* simple helper class to accumulate output in a buffer
and send that to the console on request: */
static class write_pending_buffer
{
private:
static const size_t WPBUF_LEN = 256u;
char buf[WPBUF_LEN];
size_t ixput;
HANDLE output_handle;
public:
void init (HANDLE &handle)
{
output_handle = handle;
empty ();
}
inline void put (char x)
{
if (ixput == WPBUF_LEN)
send ();
buf[ixput++] = x;
}
inline void empty () { ixput = 0u; }
inline void send ()
{
if (!output_handle)
{
empty ();
return;
}
mbtowc_p f_mbtowc =
(__MBTOWC == __ascii_mbtowc) ? __utf8_mbtowc : __MBTOWC;
wchar_t bufw[WPBUF_LEN];
DWORD len = 0;
mbstate_t ps;
memset (&ps, 0, sizeof (ps));
char *p = buf;
while (ixput)
{
int bytes = f_mbtowc (_REENT, bufw + len, p, ixput, &ps);
if (bytes < 0)
{
if ((size_t) ps.__count < ixput)
{ /* Discard one byte and retry. */
p++;
ixput--;
memset (&ps, 0, sizeof (ps));
continue;
}
/* Halfway through the multibyte char. */
memmove (buf, p, ixput);
break;
}
else
{
len++;
p += bytes;
ixput -= bytes;
}
}
acquire_attach_mutex (mutex_timeout);
WriteConsoleW (output_handle, bufw, len, NULL, 0);
release_attach_mutex ();
}
} wpbuf;
static void
beep ()
{
const WCHAR ding[] = L"\\media\\ding.wav";
reg_key r (HKEY_CURRENT_USER, KEY_ALL_ACCESS, L"AppEvents", L"Schemes",
L"Apps", L".Default", L".Default", L".Current", NULL);
if (r.created ())
{
tmp_pathbuf tp;
PWCHAR ding_path = tp.w_get ();
wcpcpy (wcpcpy (ding_path, windows_directory), ding);
r.set_string (L"", ding_path);
}
MessageBeep (MB_OK);
}
fhandler_console::console_state *
fhandler_console::open_shared_console (HWND hw, HANDLE& h, bool& created)
{
wchar_t namebuf[(sizeof "XXXXXXXXXXXXXXXXXX-consNNNNNNNNNN")];
__small_swprintf (namebuf, L"%S-cons%p", &cygheap->installation_key, hw);
shared_locations m = created ? SH_SHARED_CONSOLE : SH_JUSTOPEN;
console_state *res = (console_state *)
open_shared (namebuf, 0, h, sizeof (*shared_console_info), m, created);
return res;
}
class console_unit
{
int n;
unsigned long bitmask;
HWND me;
public:
operator int () const {return n;}
console_unit (HWND);
friend BOOL CALLBACK enum_windows (HWND, LPARAM);
};
BOOL CALLBACK
enum_windows (HWND hw, LPARAM lp)
{
console_unit *this1 = (console_unit *) lp;
if (hw == this1->me)
return TRUE;
HANDLE h = NULL;
fhandler_console::console_state *cs;
if ((cs = fhandler_console::open_shared_console (hw, h)))
{
this1->bitmask ^= 1 << cs->tty_min_state.getntty ();
UnmapViewOfFile ((void *) cs);
CloseHandle (h);
}
return TRUE;
}
console_unit::console_unit (HWND me0):
bitmask (0xffffffff), me (me0)
{
EnumWindows (enum_windows, (LPARAM) this);
n = (_minor_t) ffs (bitmask) - 1;
if (n < 0)
api_fatal ("console device allocation failure - too many consoles in use, max consoles is 32");
}
static DWORD
cons_master_thread (VOID *arg)
{
fhandler_console *fh = (fhandler_console *) arg;
tty *ttyp = (tty *) fh->tc ();
fhandler_console::handle_set_t handle_set;
fh->get_duplicated_handle_set (&handle_set);
HANDLE thread_sync_event;
DuplicateHandle (GetCurrentProcess (), fh->thread_sync_event,
GetCurrentProcess (), &thread_sync_event,
0, FALSE, DUPLICATE_SAME_ACCESS);
SetEvent (thread_sync_event);
master_thread_started = true;
/* Do not touch class members after here because the class instance
may have been destroyed. */
fhandler_console::cons_master_thread (&handle_set, ttyp);
fhandler_console::close_handle_set (&handle_set);
SetEvent (thread_sync_event);
CloseHandle (thread_sync_event);
return 0;
}
/* Compare two INPUT_RECORD sequences */
static inline bool
inrec_eq (const INPUT_RECORD *a, const INPUT_RECORD *b, DWORD n)
{
for (DWORD i = 0; i < n; i++)
{
if (a[i].EventType != b[i].EventType)
return false;
else if (a[i].EventType == KEY_EVENT)
{ /* wVirtualKeyCode, wVirtualScanCode and dwControlKeyState
of the readback key event may be different from that of
written event. Therefore they are ignored. */
const KEY_EVENT_RECORD *ak = &a[i].Event.KeyEvent;
const KEY_EVENT_RECORD *bk = &b[i].Event.KeyEvent;
if (ak->bKeyDown != bk->bKeyDown
|| ak->uChar.UnicodeChar != bk->uChar.UnicodeChar
|| ak->wRepeatCount != bk->wRepeatCount)
return false;
}
else if (a[i].EventType == MOUSE_EVENT)
{
const MOUSE_EVENT_RECORD *am = &a[i].Event.MouseEvent;
const MOUSE_EVENT_RECORD *bm = &b[i].Event.MouseEvent;
if (am->dwMousePosition.X != bm->dwMousePosition.X
|| am->dwMousePosition.Y != bm->dwMousePosition.Y
|| am->dwButtonState != bm->dwButtonState
|| am->dwControlKeyState != bm->dwControlKeyState
|| am->dwEventFlags != bm->dwEventFlags)
return false;
}
else if (a[i].EventType == WINDOW_BUFFER_SIZE_EVENT)
{
const WINDOW_BUFFER_SIZE_RECORD
*aw = &a[i].Event.WindowBufferSizeEvent;
const WINDOW_BUFFER_SIZE_RECORD
*bw = &b[i].Event.WindowBufferSizeEvent;
if (aw->dwSize.X != bw->dwSize.X
|| aw->dwSize.Y != bw->dwSize.Y)
return false;
}
else if (a[i].EventType == MENU_EVENT)
{
const MENU_EVENT_RECORD *am = &a[i].Event.MenuEvent;
const MENU_EVENT_RECORD *bm = &b[i].Event.MenuEvent;
if (am->dwCommandId != bm->dwCommandId)
return false;
}
else if (a[i].EventType == FOCUS_EVENT)
{
const FOCUS_EVENT_RECORD *af = &a[i].Event.FocusEvent;
const FOCUS_EVENT_RECORD *bf = &b[i].Event.FocusEvent;
if (af->bSetFocus != bf->bSetFocus)
return false;
}
}
return true;
}
/* This thread processes signals derived from input messages.
Without this thread, those signals can be handled only when
the process calls read() or select(). This thread reads input
records, processes signals and removes corresponding record.
The other input records are kept back for read() or select(). */
void
fhandler_console::cons_master_thread (handle_set_t *p, tty *ttyp)
{
const int additional_space = 128; /* Possible max number of incoming events
during the process. Additional space
should be left for writeback fix. */
DWORD inrec_size = INREC_SIZE + additional_space;
INPUT_RECORD *input_rec =
(INPUT_RECORD *) malloc (inrec_size * sizeof (INPUT_RECORD));
INPUT_RECORD *input_tmp =
(INPUT_RECORD *) malloc (inrec_size * sizeof (INPUT_RECORD));
if (!input_rec || !input_tmp)
{ /* Cannot continue */
free (input_rec);
free (input_tmp);
return;
}
DWORD inrec_size1 =
wincap.cons_need_small_input_record_buf () ? INREC_SIZE : inrec_size;
struct m
{
inline static size_t bytes (size_t n)
{
return sizeof (INPUT_RECORD) * n;
}
};
termios &ti = ttyp->ti;
while (con.owner == myself->pid)
{
DWORD total_read, n, i;
if (con.disable_master_thread)
{
cygwait (40);
continue;
}
acquire_attach_mutex (mutex_timeout);
GetNumberOfConsoleInputEvents (p->input_handle, &total_read);
release_attach_mutex ();
if (total_read > INREC_SIZE)
{
cygwait (40);
acquire_attach_mutex (mutex_timeout);
GetNumberOfConsoleInputEvents (p->input_handle, &n);
release_attach_mutex ();
if (n < total_read)
{
/* read() seems to be called. Process special keys
in process_input_message (). */
con.master_thread_suspended = true;
continue;
}
total_read = n;
}
con.master_thread_suspended = false;
if (total_read + additional_space > inrec_size)
{
DWORD new_inrec_size = total_read + additional_space;
INPUT_RECORD *new_input_rec = (INPUT_RECORD *)
realloc (input_rec, m::bytes (new_inrec_size));
if (new_input_rec)
input_rec = new_input_rec;
INPUT_RECORD *new_input_tmp = (INPUT_RECORD *)
realloc (input_tmp, m::bytes (new_inrec_size));
if (new_input_tmp)
input_tmp = new_input_tmp;
if (new_input_rec && new_input_tmp)
{
inrec_size = new_inrec_size;
if (!wincap.cons_need_small_input_record_buf ())
inrec_size1 = inrec_size;
}
}
WaitForSingleObject (p->input_mutex, mutex_timeout);
total_read = 0;
switch (cygwait (p->input_handle, (DWORD) 0))
{
case WAIT_OBJECT_0:
acquire_attach_mutex (mutex_timeout);
total_read = 0;
while (cygwait (p->input_handle, (DWORD) 0) == WAIT_OBJECT_0
&& total_read < inrec_size)
{
DWORD len;
ReadConsoleInputW (p->input_handle, input_rec + total_read,
min (inrec_size - total_read, inrec_size1),
&len);
total_read += len;
}
release_attach_mutex ();
break;
case WAIT_TIMEOUT:
con.num_processed = 0;
case WAIT_SIGNALED:
case WAIT_CANCELED:
break;
default: /* Error */
ReleaseMutex (p->input_mutex);
return;
}
/* If ENABLE_VIRTUAL_TERMINAL_INPUT is not set, changing
window height does not generate WINDOW_BUFFER_SIZE_EVENT.
Therefore, check windows size every time here. */
if (!wincap.has_con_24bit_colors () || con_is_legacy)
{
SHORT y = con.dwWinSize.Y;
SHORT x = con.dwWinSize.X;
con.fillin (p->output_handle);
if (y != con.dwWinSize.Y || x != con.dwWinSize.X)
{
con.scroll_region.Top = 0;
con.scroll_region.Bottom = -1;
ttyp->kill_pgrp (SIGWINCH);
}
}
for (i = con.num_processed; i < total_read; i++)
{
wchar_t wc;
char c;
bool processed = false;
switch (input_rec[i].EventType)
{
case KEY_EVENT:
if (!input_rec[i].Event.KeyEvent.bKeyDown)
continue;
wc = input_rec[i].Event.KeyEvent.uChar.UnicodeChar;
if (!wc || (wint_t) wc >= 0x80)
continue;
c = (char) wc;
switch (process_sigs (c, ttyp, NULL))
{
case signalled:
case not_signalled_but_done:
case done_with_debugger:
processed = true;
ttyp->output_stopped = false;
if (ti.c_lflag & NOFLSH)
goto remove_record;
con.num_processed = 0;
goto skip_writeback;
default: /* not signalled */
break;
}
processed = process_stop_start (c, ttyp);
break;
case WINDOW_BUFFER_SIZE_EVENT:
SHORT y = con.dwWinSize.Y;
SHORT x = con.dwWinSize.X;
con.fillin (p->output_handle);
if (y != con.dwWinSize.Y || x != con.dwWinSize.X)
{
con.scroll_region.Top = 0;
con.scroll_region.Bottom = -1;
if (wincap.has_con_24bit_colors () && !con_is_legacy
&& wincap.has_con_broken_tabs ())
fix_tab_position (p->output_handle);
ttyp->kill_pgrp (SIGWINCH);
}
processed = true;
break;
}
remove_record:
if (processed)
{ /* Remove corresponding record. */
if (total_read > i + 1)
memmove (input_rec + i, input_rec + i + 1,
m::bytes (total_read - i - 1));
total_read--;
i--;
}
}
con.num_processed = total_read;
if (total_read)
{
do
{
/* Writeback input records other than interrupt. */
acquire_attach_mutex (mutex_timeout);
n = 0;
while (n < total_read)
{
DWORD len;
WriteConsoleInputW (p->input_handle, input_rec + n,
min (total_read - n, inrec_size1), &len);
n += len;
}
release_attach_mutex ();
acquire_attach_mutex (mutex_timeout);
GetNumberOfConsoleInputEvents (p->input_handle, &n);
release_attach_mutex ();
if (n + additional_space > inrec_size)
{
DWORD new_inrec_size = n + additional_space;
INPUT_RECORD *new_input_rec = (INPUT_RECORD *)
realloc (input_rec, m::bytes (new_inrec_size));
if (new_input_rec)
input_rec = new_input_rec;
INPUT_RECORD *new_input_tmp = (INPUT_RECORD *)
realloc (input_tmp, m::bytes (new_inrec_size));
if (new_input_tmp)
input_tmp = new_input_tmp;
if (new_input_rec && new_input_tmp)
{
inrec_size = new_inrec_size;
if (!wincap.cons_need_small_input_record_buf ())
inrec_size1 = inrec_size;
}
}
/* Check if writeback was successfull. */
acquire_attach_mutex (mutex_timeout);
PeekConsoleInputW (p->input_handle, input_tmp, inrec_size1, &n);
release_attach_mutex ();
if (n < min (total_read, inrec_size1))
break; /* Someone has read input without acquiring
input_mutex. ConEmu cygwin-connector? */
if (inrec_eq (input_rec, input_tmp,
min (total_read, inrec_size1)))
break; /* OK */
/* Try to fix */
acquire_attach_mutex (mutex_timeout);
n = 0;
while (cygwait (p->input_handle, (DWORD) 0) == WAIT_OBJECT_0
&& n < inrec_size)
{
DWORD len;
ReadConsoleInputW (p->input_handle, input_tmp + n,
min (inrec_size - n, inrec_size1), &len);
n += len;
}
release_attach_mutex ();
bool fixed = false;
for (DWORD ofs = n - total_read; ofs > 0; ofs--)
{
if (inrec_eq (input_rec, input_tmp + ofs, total_read))
{
memcpy (input_rec + total_read, input_tmp,
m::bytes (ofs));
memcpy (input_rec + total_read + ofs,
input_tmp + total_read + ofs,
m::bytes (n - ofs - total_read));
fixed = true;
break;
}
}
if (!fixed)
{
for (DWORD i = 0, j = 0; j < n; j++)
if (i == total_read
|| !inrec_eq (input_rec + i, input_tmp + j, 1))
{
if (total_read + j - i >= n)
{ /* Something is wrong. Giving up. */
acquire_attach_mutex (mutex_timeout);
DWORD l = 0;
while (l < n)
{
DWORD len;
WriteConsoleInputW (p->input_handle,
input_tmp + l,
min (n - l, inrec_size1),
&len);
l += len;
}
release_attach_mutex ();
goto skip_writeback;
}
input_rec[total_read + j - i] = input_tmp[j];
}
else
i++;
}
total_read = n;
}
while (true);
}
skip_writeback:
ReleaseMutex (p->input_mutex);
cygwait (40);
}
free (input_rec);
free (input_tmp);
}
bool
fhandler_console::set_unit ()
{
bool created;
fh_devices devset;
lock_ttys here;
HWND me;
fh_devices this_unit = dev ();
bool generic_console = this_unit == FH_CONIN || this_unit == FH_CONOUT;
if (shared_console_info)
{
fh_devices shared_unit =
(fh_devices) shared_console_info->tty_min_state.getntty ();
devset = (shared_unit == this_unit || this_unit == FH_CONSOLE
|| generic_console
|| this_unit == FH_TTY) ?
shared_unit : FH_ERROR;
created = false;
}
else if ((!generic_console &&
(myself->ctty != -1 && !iscons_dev (myself->ctty)))
|| !(me = GetConsoleWindow ()))
devset = FH_ERROR;
else
{
created = true;
shared_console_info =
open_shared_console (me, cygheap->console_h, created);
ProtectHandleINH (cygheap->console_h);
if (created)
shared_console_info->
tty_min_state.setntty (DEV_CONS_MAJOR, console_unit (me));
devset = (fh_devices) shared_console_info->tty_min_state.getntty ();
if (created)
con.owner = myself->pid;
}
if (!created && shared_console_info)
{
while (con.owner > MAX_PID)
Sleep (1);
pinfo p (con.owner);
if (!p)
con.owner = myself->pid;
}
dev ().parse (devset);
if (devset != FH_ERROR)
pc.file_attributes (FILE_ATTRIBUTE_NORMAL);
else
{
set_handle (NULL);
set_output_handle (NULL);
created = false;
}
return created;
}
/* Allocate and initialize the shared record for the current console. */
void
fhandler_console::setup ()
{
if (set_unit ())
{
con.scroll_region.Bottom = -1;
con.dwLastCursorPosition.X = -1;
con.dwLastCursorPosition.Y = -1;
con.dwLastMousePosition.X = -1;
con.dwLastMousePosition.Y = -1;
con.savex = con.savey = -1;
con.screen_alternated = false;
con.dwLastButtonState = 0; /* none pressed */
con.last_button_code = 3; /* released */
con.underline_color = FOREGROUND_GREEN | FOREGROUND_BLUE;
con.dim_color = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
con.meta_mask = LEFT_ALT_PRESSED;
/* Set the mask that determines if an input keystroke is modified by
META. We set this based on the keyboard layout language loaded
for the current thread. The left <ALT> key always generates
META, but the right <ALT> key only generates META if we are using
an English keyboard because many "international" keyboards
replace common shell symbols ('[', '{', etc.) with accented
language-specific characters (umlaut, accent grave, etc.). On
these keyboards right <ALT> (called AltGr) is used to produce the
shell symbols and should not be interpreted as META. */
if (PRIMARYLANGID (LOWORD (GetKeyboardLayout (0))) == LANG_ENGLISH)
con.meta_mask |= RIGHT_ALT_PRESSED;
con.set_default_attr ();
con.backspace_keycode = CERASE;
con.cons_rapoi = NULL;
shared_console_info->tty_min_state.is_console = true;
con.cursor_key_app_mode = false;
con.disable_master_thread = true;
con.master_thread_suspended = false;
con.num_processed = 0;
}
}
char *&
fhandler_console::rabuf ()
{
return con_ra.rabuf;
}
size_t &
fhandler_console::ralen ()
{
return con_ra.ralen;
}
size_t &
fhandler_console::raixget ()
{
return con_ra.raixget;
}
size_t &
fhandler_console::raixput ()
{
return con_ra.raixput;
}
size_t &
fhandler_console::rabuflen ()
{
return con_ra.rabuflen;
}
/* The function set_{in,out}put_mode() should be static so that they
can be called even after the fhandler_console instance is deleted. */
void
fhandler_console::set_input_mode (tty::cons_mode m, const termios *t,
const handle_set_t *p)
{
DWORD oflags;
WaitForSingleObject (p->input_mutex, mutex_timeout);
acquire_attach_mutex (mutex_timeout);
GetConsoleMode (p->input_handle, &oflags);
DWORD flags = oflags
& (ENABLE_EXTENDED_FLAGS | ENABLE_INSERT_MODE | ENABLE_QUICK_EDIT_MODE);
switch (m)
{
case tty::restore:
flags |= ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT;
break;
case tty::cygwin:
flags |= ENABLE_WINDOW_INPUT;
if (con.master_thread_suspended)
flags |= ENABLE_PROCESSED_INPUT;
if (wincap.has_con_24bit_colors () && !con_is_legacy)
flags |= ENABLE_VIRTUAL_TERMINAL_INPUT;
else
flags |= ENABLE_MOUSE_INPUT;
break;
case tty::native:
if (t->c_lflag & ECHO)
flags |= ENABLE_ECHO_INPUT;
if (t->c_lflag & ICANON)
flags |= ENABLE_LINE_INPUT;
if (flags & ENABLE_ECHO_INPUT && !(flags & ENABLE_LINE_INPUT))
/* This is illegal, so turn off the echo here, and fake it
when we read the characters */
flags &= ~ENABLE_ECHO_INPUT;
if (t->c_lflag & ISIG)
flags |= ENABLE_PROCESSED_INPUT;
break;
}
SetConsoleMode (p->input_handle, flags);
if (!(oflags & ENABLE_VIRTUAL_TERMINAL_INPUT)
&& (flags & ENABLE_VIRTUAL_TERMINAL_INPUT)
&& con.cursor_key_app_mode)
{ /* Restore DECCKM */
set_output_mode (tty::cygwin, t, p);
WriteConsoleW (p->output_handle, L"\033[?1h", 5, NULL, 0);
}
release_attach_mutex ();
ReleaseMutex (p->input_mutex);
}
void
fhandler_console::set_output_mode (tty::cons_mode m, const termios *t,
const handle_set_t *p)
{
DWORD flags = ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT;
if (con.orig_virtual_terminal_processing_mode)
flags |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
WaitForSingleObject (p->output_mutex, mutex_timeout);
switch (m)
{
case tty::restore:
break;
case tty::cygwin:
if (wincap.has_con_24bit_colors () && !con_is_legacy)
flags |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
fallthrough;
case tty::native:
if (wincap.has_con_24bit_colors () && !con_is_legacy
&& (!(t->c_oflag & OPOST) || !(t->c_oflag & ONLCR)))
flags |= DISABLE_NEWLINE_AUTO_RETURN;
break;
}
acquire_attach_mutex (mutex_timeout);
SetConsoleMode (p->output_handle, flags);
release_attach_mutex ();
ReleaseMutex (p->output_mutex);
}
void
fhandler_console::setup_for_non_cygwin_app ()
{
/* Setting-up console mode for non-cygwin app. */
/* If conmode is set to tty::native for non-cygwin apps
in background, tty settings of the shell is reflected
to the console mode of the app. So, use tty::restore
for background process instead. */
tty::cons_mode conmode =
(get_ttyp ()->getpgid ()== myself->pgid) ? tty::native : tty::restore;
set_input_mode (conmode, &tc ()->ti, get_handle_set ());
set_output_mode (conmode, &tc ()->ti, get_handle_set ());
set_disable_master_thread (true, this);
}
void
fhandler_console::cleanup_for_non_cygwin_app (handle_set_t *p)
{
termios dummy = {0, };
termios *ti =
shared_console_info ? &(shared_console_info->tty_min_state.ti) : &dummy;
/* Cleaning-up console mode for non-cygwin app. */
/* conmode can be tty::restore when non-cygwin app is
exec'ed from login shell. */
tty::cons_mode conmode =
(con.owner == myself->pid) ? tty::restore : tty::cygwin;
set_output_mode (conmode, ti, p);
set_input_mode (conmode, ti, p);
set_disable_master_thread (con.owner == myself->pid);
}
/* Return the tty structure associated with a given tty number. If the
tty number is < 0, just return a dummy record. */
tty_min *
tty_list::get_cttyp ()
{
dev_t n = myself->ctty;
if (iscons_dev (n))
return fhandler_console::shared_console_info ?
&fhandler_console::shared_console_info->tty_min_state : NULL;
else if (istty_slave_dev (n))
return &ttys[device::minor (n)];
else
return NULL;
}
void
fhandler_console::setup_io_mutex (void)
{
char buf[MAX_PATH];
DWORD res;
res = WAIT_FAILED;
if (!input_mutex || WAIT_FAILED == (res = acquire_input_mutex (0)))
{
shared_name (buf, "cygcons.input.mutex", get_minor ());
input_mutex = OpenMutex (MAXIMUM_ALLOWED, TRUE, buf);
if (!input_mutex)
input_mutex = CreateMutex (&sec_none, FALSE, buf);
if (!input_mutex)
{
__seterrno ();
return;
}
}
if (res == WAIT_OBJECT_0)
release_input_mutex ();
res = WAIT_FAILED;
if (!output_mutex || WAIT_FAILED == (res = acquire_output_mutex (0)))
{
shared_name (buf, "cygcons.output.mutex", get_minor ());
output_mutex = OpenMutex (MAXIMUM_ALLOWED, TRUE, buf);
if (!output_mutex)
output_mutex = CreateMutex (&sec_none, FALSE, buf);
if (!output_mutex)
{
__seterrno ();
return;
}
}
if (res == WAIT_OBJECT_0)
release_output_mutex ();
}
inline DWORD
dev_console::con_to_str (char *d, int dlen, WCHAR w)
{
return sys_wcstombs (d, dlen, &w, 1);
}
inline UINT
dev_console::get_console_cp ()
{
/* The alternate charset is always 437, just as in the Linux console. */
return alternate_charset_active ? 437 : 0;
}
inline DWORD
dev_console::str_to_con (mbtowc_p f_mbtowc, PWCHAR d, const char *s, DWORD sz)
{
return _sys_mbstowcs (f_mbtowc, d, CONVERT_LIMIT, s, sz);
}
bool
fhandler_console::set_raw_win32_keyboard_mode (bool new_mode)
{
bool old_mode = con.raw_win32_keyboard_mode;
con.raw_win32_keyboard_mode = new_mode;
syscall_printf ("raw keyboard mode %sabled",
con.raw_win32_keyboard_mode ? "en" : "dis");
return old_mode;
};
void
fhandler_console::set_cursor_maybe ()
{
con.fillin (get_output_handle ());
/* Nothing to do for xterm compatible mode. */
if (wincap.has_con_24bit_colors () && !con_is_legacy)
return;
if (con.dwLastCursorPosition.X != con.b.dwCursorPosition.X ||
con.dwLastCursorPosition.Y != con.b.dwCursorPosition.Y)
{
acquire_attach_mutex (mutex_timeout);
SetConsoleCursorPosition (get_output_handle (), con.b.dwCursorPosition);
release_attach_mutex ();
con.dwLastCursorPosition = con.b.dwCursorPosition;
}
}
/* Workaround for a bug of windows xterm compatible mode. */
/* The horizontal tab positions are broken after resize. */
void
fhandler_console::fix_tab_position (HANDLE h)
{
/* Re-setting ENABLE_VIRTUAL_TERMINAL_PROCESSING
fixes the tab position. */
DWORD mode;
acquire_attach_mutex (mutex_timeout);
GetConsoleMode (h, &mode);
SetConsoleMode (h, mode & ~ENABLE_VIRTUAL_TERMINAL_PROCESSING);
SetConsoleMode (h, mode);
release_attach_mutex ();
}
bool
fhandler_console::send_winch_maybe ()
{
SHORT y = con.dwWinSize.Y;
SHORT x = con.dwWinSize.X;
con.fillin (get_output_handle ());
if (y != con.dwWinSize.Y || x != con.dwWinSize.X)
{
con.scroll_region.Top = 0;
con.scroll_region.Bottom = -1;
if (wincap.has_con_24bit_colors () && !con_is_legacy
&& wincap.has_con_broken_tabs ())
fix_tab_position (get_output_handle ());
/* longjmp() may be called in the signal handler like less,
so release input_mutex temporarily before kill_pgrp(). */
release_input_mutex ();
get_ttyp ()->kill_pgrp (SIGWINCH);
acquire_input_mutex (mutex_timeout);
return true;
}
return false;
}
/* Check whether a mouse event is to be reported as an escape sequence */
bool
fhandler_console::mouse_aware (MOUSE_EVENT_RECORD& mouse_event)
{
if (!con.use_mouse)
return 0;
/* Adjust mouse position by window scroll buffer offset
and remember adjusted position in state for use by read() */
CONSOLE_SCREEN_BUFFER_INFO now;
acquire_attach_mutex (mutex_timeout);
BOOL r = GetConsoleScreenBufferInfo (get_output_handle (), &now);
release_attach_mutex ();
if (!r)
/* Cannot adjust position by window scroll buffer offset */
return 0;
con.dwMousePosition.X = mouse_event.dwMousePosition.X - now.srWindow.Left;
con.dwMousePosition.Y = mouse_event.dwMousePosition.Y - now.srWindow.Top;
return ((mouse_event.dwEventFlags == 0
|| mouse_event.dwEventFlags == DOUBLE_CLICK)
&& mouse_event.dwButtonState != con.dwLastButtonState)
|| mouse_event.dwEventFlags == MOUSE_WHEELED
|| (mouse_event.dwEventFlags == MOUSE_MOVED
&& (con.dwMousePosition.X != con.dwLastMousePosition.X
|| con.dwMousePosition.Y != con.dwLastMousePosition.Y)
&& ((con.use_mouse >= 2 && mouse_event.dwButtonState)
|| con.use_mouse >= 3));
}
bg_check_types
fhandler_console::bg_check (int sig, bool dontsignal)
{
/* Setting-up console mode for cygwin app. This is necessary if the
cygwin app and other non-cygwin apps are started simultaneously
in the same process group. */
if (sig == SIGTTIN)
{
set_input_mode (tty::cygwin, &tc ()->ti, get_handle_set ());
set_disable_master_thread (false, this);
}
if (sig == SIGTTOU)
set_output_mode (tty::cygwin, &tc ()->ti, get_handle_set ());
return fhandler_termios::bg_check (sig, dontsignal);
}
void