forked from microsoft/terminal
/
cookedRead.cpp
1112 lines (1001 loc) · 35.5 KB
/
cookedRead.cpp
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
/********************************************************
* *
* Copyright (C) Microsoft. All rights reserved. *
* *
********************************************************/
#include "precomp.h"
#include "cookedRead.hpp"
#include "stream.h"
#include "_stream.h"
#include "../types/inc/Utf16Parser.hpp"
#include "../types/inc/GlyphWidth.hpp"
#include "../interactivity/inc/ServiceLocator.hpp"
CookedRead::CookedRead(InputBuffer* const pInputBuffer,
INPUT_READ_HANDLE_DATA* const pInputReadHandleData,
SCREEN_INFORMATION& screenInfo,
std::shared_ptr<CommandHistory> pCommandHistory,
wchar_t* userBuffer,
const size_t cchUserBuffer,
const ULONG ctrlWakeupMask,
const std::wstring_view exeName
) :
ReadData(pInputBuffer, pInputReadHandleData),
_screenInfo{ screenInfo },
_userBuffer{ userBuffer },
_cchUserBuffer{ cchUserBuffer },
_ctrlWakeupMask{ ctrlWakeupMask },
_pCommandHistory{ pCommandHistory },
_insertMode{ ServiceLocator::LocateGlobals().getConsoleInformation().GetInsertMode() },
_state{ ReadState::Ready },
_status{ STATUS_SUCCESS },
_insertionIndex{ 0 },
_commandLineEditingKeys{ false },
_keyState{ 0 },
_commandKeyChar{ UNICODE_NULL },
_echoInput{ WI_IsFlagSet(pInputBuffer->InputMode, ENABLE_ECHO_INPUT) },
_bufferedInput{},
_exeName{ exeName }
{
_prompt.reserve(256);
_promptStartLocation = _screenInfo.GetTextBuffer().GetCursor().GetPosition();
_beforePopupCursorPosition = _promptStartLocation;
}
SCREEN_INFORMATION& CookedRead::ScreenInfo()
{
return _screenInfo;
}
bool CookedRead::HasHistory() const noexcept
{
return _pCommandHistory != nullptr;
}
CommandHistory& CookedRead::History() noexcept
{
return *_pCommandHistory;
}
void CookedRead::Erase()
{
_clearPromptCells();
_prompt.erase();
_insertionIndex = 0;
_writeToScreen(true);
}
bool CookedRead::IsInsertMode() const noexcept
{
return _insertMode;
}
void CookedRead::SetInsertMode(const bool mode) noexcept
{
_insertMode = mode;
}
bool CookedRead::IsEchoInput() const noexcept
{
return _echoInput;
}
COORD CookedRead::PromptStartLocation() const noexcept
{
return _promptStartLocation;
}
COORD CookedRead::BeforePopupCursorPosition() const noexcept
{
return _beforePopupCursorPosition;
}
void CookedRead::BufferInput(const wchar_t wch)
{
_bufferedInput.push_back(wch);
}
std::wstring_view CookedRead::Prompt()
{
return { &_prompt.at(0), _prompt.size() };
}
std::wstring_view CookedRead::PromptFromInsertionIndex()
{
FAIL_FAST_IF(_insertionIndex > _prompt.size());
return { &_prompt.at(_insertionIndex), _prompt.size() - _insertionIndex };
}
size_t CookedRead::InsertionIndex() const noexcept
{
return _insertionIndex;
}
// Routine Description:
// - overwrites the text in the prompt, starting at the insertion index
// Arguments:
// - startIt - starting iterator of the text to copy
// - endIt - ending it of the text to copy
void CookedRead::Overwrite(const std::wstring_view::const_iterator startIt, const std::wstring_view::const_iterator endIt)
{
FAIL_FAST_IF(_insertionIndex > _prompt.size());
// TODO this needs to support emojis, both in the prompt and in the text to write
auto it = startIt;
while (it != endIt)
{
if (_insertionIndex < _prompt.size())
{
_prompt.at(_insertionIndex) = *it;
}
else
{
_prompt.push_back(*it);
}
++_insertionIndex;
++it;
}
FAIL_FAST_IF(_insertionIndex > _prompt.size());
_writeToScreen(true);
}
// Routine Description:
// - counts the number of visible characters in the prompt
// Return Value:
// - the number of visible characters in the prompt (counts surrogate pairs as one character)
size_t CookedRead::VisibleCharCount() const
{
return _visibleCharCountOf({ _prompt.c_str(), _prompt.size() });
}
// Routine Description:
// - counts the number of visible characters in wstrView
// Arguments:
// - wstrView - the text to count the visible characters of
// Return Value:
// - the number of visible characters in the string view (counts surrogate pairs as one character)
size_t CookedRead::_visibleCharCountOf(const std::wstring_view wstrView)
{
size_t count = 0;
for (size_t i = 0; i < wstrView.size(); ++i)
{
if (i + 1 < wstrView.size() && _isSurrogatePairAt(wstrView, i))
{
// need to increment i in order to jump past surrogate pair
++i;
}
++count;
}
return count;
}
// Routine Description:
// - moves the insertion index one codepoint to the left if possible
// Return Value:
// - returns the number of cells that the move operation passed over
size_t CookedRead::MoveInsertionIndexLeft()
{
// check if there's anything to the left to
if (_isInsertionIndexAtPromptBegin())
{
return 0;
}
// move the insertion index left by one codepoint
std::wstring_view glyph;
if (_insertionIndex >= 2 &&
_isSurrogatePairAt(_insertionIndex - 2))
{
_insertionIndex -= 2;
glyph = { &_prompt.at(_insertionIndex), 2 };
}
else
{
--_insertionIndex;
glyph = { &_prompt.at(_insertionIndex), 1 };
}
// calculate how many cells the cursor was moved by the insertion index move
size_t cellsMoved = 0;
if (IsGlyphFullWidth(glyph))
{
cellsMoved = 2;
}
else
{
cellsMoved = 1;
}
return cellsMoved;
}
// Routine Description:
// - moves the insertion index one codepoint to the right if possible
// right beyond the end of the current prompt text
// Return Value:
// - returns the number of cells that the move operation passed over
size_t CookedRead::MoveInsertionIndexRight()
{
FAIL_FAST_IF(_insertionIndex > _prompt.size());
// check if we're at the far right side of the prompt text for character insertion from last command in
// the history
if (_isInsertionIndexAtPromptEnd())
{
// if we have any command history, grab the nth next character from the previous command and write it
// to the buffer, then move over it
if (_pCommandHistory)
{
const std::wstring_view lastCommand = _pCommandHistory->GetLastCommand();
const size_t lastCommandCodepointCount = _visibleCharCountOf(lastCommand);
const size_t currentCommandCodepointCount = VisibleCharCount();
if (lastCommandCodepointCount > currentCommandCodepointCount)
{
const std::vector<std::vector<wchar_t>> parsedLastCommand = Utf16Parser::Parse(lastCommand);
const std::vector<wchar_t>& glyph = parsedLastCommand.at(currentCommandCodepointCount);
for (const wchar_t wch : glyph)
{
// don't bother adjusting the insertion index here, it will be handled below
_prompt.push_back(wch);
}
_writeToScreen(true);
}
else
{
// no history data to copy, so we can't move to the right
return 0;
}
}
else
{
// no history data to copy, so we can't move to the right
return 0;
}
}
// move the insertion index right by one codepoint
std::wstring_view glyph;
if (_insertionIndex + 1 < _prompt.size() &&
_isSurrogatePairAt(_insertionIndex))
{
glyph = { &_prompt.at(_insertionIndex), 2 };
_insertionIndex += 2;
}
else
{
glyph = { &_prompt.at(_insertionIndex), 1 };
++_insertionIndex;
}
// calculate how many cells the cursor was moved by the insertion index move
size_t cellsMoved = 0;
if (IsGlyphFullWidth(glyph))
{
cellsMoved = 2;
}
else
{
cellsMoved = 1;
}
FAIL_FAST_IF(_insertionIndex > _prompt.size());
return cellsMoved;
}
// Routine Description:
// - moves insertion index to the beginning of the prompt
void CookedRead::MoveInsertionIndexToStart() noexcept
{
_insertionIndex = 0;
}
// Routine Description:
// - moves insertion index to the end of the prompt
// Return Value:
// - the number of cells that the insertion point has moved by
size_t CookedRead::MoveInsertionIndexToEnd()
{
const size_t leftCells = _calculatePromptCellLength(false);
const size_t allCells = _calculatePromptCellLength(true);
_insertionIndex = _prompt.size();
return allCells - leftCells;
}
// Routine Description:
// - moves insertion index to the left by a word
// Return Value:
// - the number of cells that the insertion point has moved by
size_t CookedRead::MoveInsertionIndexLeftByWord()
{
if (_isInsertionIndexAtPromptBegin())
{
return 0;
}
size_t cellsMoved = 0;
// move through any word delimiters to the left
while (!_isInsertionIndexAtPromptBegin() && IsWordDelim(_prompt.at(_insertionIndex - 1)))
{
cellsMoved += MoveInsertionIndexLeft();
}
// move through word to next word delimiter
while (!_isInsertionIndexAtPromptBegin() && !IsWordDelim(_prompt.at(_insertionIndex - 1)))
{
cellsMoved += MoveInsertionIndexLeft();
}
return cellsMoved;
}
// Routine Description:
// - moves insertion index to the right by a word
// Return Value:
// - the number of cells that the insertion point has moved by
size_t CookedRead::MoveInsertionIndexRightByWord()
{
FAIL_FAST_IF(_insertionIndex > _prompt.size());
if (_isInsertionIndexAtPromptEnd())
{
return 0;
}
size_t cellsMoved = 0;
if (!IsWordDelim(_prompt.at(_insertionIndex)))
{
// move through a word until we come to the first word delimiter
while (!_isInsertionIndexAtPromptEnd() && !IsWordDelim(_prompt.at(_insertionIndex)))
{
cellsMoved += MoveInsertionIndexRight();
}
}
// move through word delimiters until we encounter a char that isn't one
while (!_isInsertionIndexAtPromptEnd() && IsWordDelim(_prompt.at(_insertionIndex)))
{
cellsMoved += MoveInsertionIndexRight();
}
return cellsMoved;
}
// Routine Description:
// - sets prompt to the oldest command in the command history
void CookedRead::SetPromptToOldestCommand()
{
SetPromptToCommand(0);
}
// Routine Description:
// - sets prompt to the newest command in the command history
void CookedRead::SetPromptToNewestCommand()
{
if (_pCommandHistory && _pCommandHistory->GetNumberOfCommands() > 0)
{
const short commandNumber = static_cast<short>(_pCommandHistory->GetNumberOfCommands() - 1);
SetPromptToCommand(gsl::narrow<size_t>(commandNumber));
}
}
// Routine Description:
// - sets prompt to the command in the command history at index
// Arguments:
// - index - the command index to set the prompt to
void CookedRead::SetPromptToCommand(const size_t index)
{
FAIL_FAST_IF(_insertionIndex > _prompt.size());
if (_pCommandHistory && _pCommandHistory->GetNumberOfCommands() > 0)
{
const std::wstring_view command = _pCommandHistory->GetNth(static_cast<short>(index));
// update the history's last displayde since we're setting it to the prompt
_pCommandHistory->LastDisplayed = static_cast<short>(index);
Erase();
_prompt = command;
_insertionIndex = _prompt.size();
_writeToScreen(true);
}
}
// Routine Description:
// - sets prompt to the next command in the direction searched
// Arguments:
// - searchDirection - the direction to get the command to write into the prompt from
void CookedRead::SetPromptToCommand(const CommandHistory::SearchDirection searchDirection)
{
FAIL_FAST_IF(_insertionIndex > _prompt.size());
if (_pCommandHistory && _pCommandHistory->GetNumberOfCommands() > 0)
{
// we're using an arbitarily large buffer to temporarily store the command in
const size_t cchBuffer = 512;
wchar_t buffer[cchBuffer];
gsl::span<wchar_t> span{ &buffer[0], static_cast<std::ptrdiff_t>(cchBuffer) };
size_t bytes = cchBuffer * sizeof(wchar_t);
THROW_IF_FAILED(_pCommandHistory->Retrieve(searchDirection, span, bytes));
Erase();
_prompt = std::wstring(&buffer[0], bytes / sizeof(wchar_t));
_insertionIndex = _prompt.size();
_writeToScreen(true);
}
}
// Routine Description:
// - searches the command history for a command that matches the text to the left of the insertion index and
// fills the prompt with the command text
void CookedRead::SetPromptToMatchingHistoryCommand()
{
FAIL_FAST_IF(_insertionIndex > _prompt.size());
if (_pCommandHistory && _pCommandHistory->GetNumberOfCommands() > 0)
{
short index = 0;
if (_pCommandHistory->FindMatchingCommand({ _prompt.c_str(), _insertionIndex },
_pCommandHistory->LastDisplayed,
index,
CommandHistory::MatchOptions::None))
{
const size_t oldInsertionIndex = _insertionIndex;
SetPromptToCommand(index);
_pCommandHistory->LastDisplayed = index;
_insertionIndex = oldInsertionIndex;
_adjustCursorToInsertionIndex();
}
}
}
// Routine Description:
// - fills prompt with characters from the last command in the history
void CookedRead::FillPromptWithPreviousCommandFragment()
{
FAIL_FAST_IF(_insertionIndex > _prompt.size());
if (_pCommandHistory && _pCommandHistory->GetNumberOfCommands() > 0)
{
const size_t leftChars = _visibleCharCountOf({ _prompt.c_str(), _insertionIndex });
const std::wstring_view lastCommand = _pCommandHistory->GetLastCommand();
const size_t lastCommandChars = _visibleCharCountOf(lastCommand);
if (lastCommandChars > leftChars)
{
// fill prompt with chars from last command
const std::vector<std::vector<wchar_t>> parsedLastCommand = Utf16Parser::Parse(lastCommand);
auto it = parsedLastCommand.cbegin() + leftChars;
while (it != parsedLastCommand.cend())
{
const std::vector<wchar_t>& glyph = *it;
for (const wchar_t wch : glyph)
{
_prompt.push_back(wch);
}
++it;
}
_writeToScreen(false);
}
}
}
// Routine Description:
// - deletes all text to the left of the insertion index
// Return Value:
// - number of cells that the insertion point has moved by
size_t CookedRead::DeletePromptBeforeInsertionIndex()
{
FAIL_FAST_IF(_insertionIndex > _prompt.size());
_clearPromptCells();
const size_t cellsMoved = _insertionIndex;
_prompt.erase(0, _insertionIndex);
_insertionIndex = 0;
_writeToScreen(true);
return cellsMoved;
}
// Routine Description:
// - deletes all text to the right of the insertion index
void CookedRead::DeletePromptAfterInsertionIndex()
{
_clearPromptCells();
_prompt.resize(_insertionIndex);
_writeToScreen(true);
}
// Routine Description:
// - deletes codepoint to the right of the insertion index
void CookedRead::DeleteFromRightOfInsertionIndex()
{
FAIL_FAST_IF(_insertionIndex > _prompt.size());
if (_isInsertionIndexAtPromptEnd())
{
return;
}
_clearPromptCells();
// delete codepoint to the right of the insertion index
if (_insertionIndex + 1 < _prompt.size() &&
_isSurrogatePairAt(_insertionIndex))
{
_prompt.erase(_insertionIndex, 2);
}
else
{
_prompt.erase(_insertionIndex, 1);
}
FAIL_FAST_IF(_insertionIndex > _prompt.size());
_writeToScreen(true);
}
// Routine Description:
// - checks if wch matches up with the control character masking for early data return
// Arguments:
// - wch - the wchar to check
// Return Value:
// - true if wch is part of the the control character mask, false otherwise
bool CookedRead::_isCtrlWakeupMaskTriggered(const wchar_t wch) const noexcept
{
// can't use wil flag set macros here because they require a static scope for the flag
const ULONG flag = 1 << wch;
const bool masked = (_ctrlWakeupMask & flag) != 0;
return (wch < UNICODE_SPACE && masked);
}
// Routine Description:
// - This routine is called to complete a cooked read that blocked in ReadInputBuffer.
// - The context of the read was saved in the CookedReadData structure.
// - This routine is called when events have been written to the input buffer.
// - It is called in the context of the writing thread.
// - It may be called more than once.
// Arguments:
// - TerminationReason - if this routine is called because a ctrl-c or ctrl-break was seen, this argument
// contains CtrlC or CtrlBreak. If the owning thread is exiting, it will have ThreadDying. Otherwise 0.
// - fIsUnicode - Whether to convert the final data to A (using Console Input CP) at the end or treat everything as Unicode (UCS-2)
// - pReplyStatus - The status code to return to the client application that originally called the API (before it was queued to wait)
// - pNumBytes - The number of bytes of data that the server/driver will need to transmit back to the client process
// - pControlKeyState - For certain types of reads, this specifies which modifier keys were held.
// - pOutputData - not used
// Return Value:
// - true if the wait is done and result buffer/status code can be sent back to the client.
// - false if we need to continue to wait until more data is available.
bool CookedRead::Notify(const WaitTerminationReason TerminationReason,
const bool fIsUnicode,
_Out_ NTSTATUS* const pReplyStatus,
_Out_ size_t* const pNumBytes,
_Out_ DWORD* const pControlKeyState,
_Out_ void* const pOutputData)
{
// TODO
pOutputData;
pNumBytes;
fIsUnicode;
CONSOLE_INFORMATION& gci = ServiceLocator::LocateGlobals().getConsoleInformation();
// this routine should be called by a thread owning the same
// lock on the same console as we're reading from.
FAIL_FAST_IF(!gci.IsConsoleLocked());
*pNumBytes = 0;
*pControlKeyState = 0;
*pReplyStatus = STATUS_SUCCESS;
FAIL_FAST_IF(_pInputReadHandleData->IsInputPending());
// this routine should be called by a thread owning the same lock on the same console as we're reading from.
FAIL_FAST_IF(_pInputReadHandleData->GetReadCount() == 0);
// if ctrl-c or ctrl-break was seen, terminate read.
if (WI_IsAnyFlagSet(TerminationReason, (WaitTerminationReason::CtrlC | WaitTerminationReason::CtrlBreak)))
{
*pReplyStatus = STATUS_ALERTED;
gci.SetCookedReadData(nullptr);
return true;
}
// See if we were called because the thread that owns this wait block is exiting.
if (WI_IsFlagSet(TerminationReason, WaitTerminationReason::ThreadDying))
{
*pReplyStatus = STATUS_THREAD_IS_TERMINATING;
gci.SetCookedReadData(nullptr);
return true;
}
// We must see if we were woken up because the handle is being closed. If
// so, we decrement the read count. If it goes to zero, we wake up the
// close thread. Otherwise, we wake up any other thread waiting for data.
if (WI_IsFlagSet(TerminationReason, WaitTerminationReason::HandleClosing))
{
*pReplyStatus = STATUS_ALERTED;
gci.SetCookedReadData(nullptr);
return true;
}
// TODO insert command history code checking here
*pReplyStatus = Read(fIsUnicode, *pNumBytes, *pControlKeyState);
if (*pReplyStatus != CONSOLE_STATUS_WAIT)
{
gci.SetCookedReadData(nullptr);
return true;
}
else
{
return false;
}
}
void CookedRead::Hide()
{
_clearPromptCells();
}
void CookedRead::Show()
{
_writeToScreen(true);
}
// Routine Description:
// - inserts a ctrl+z character into the prompt at the insertion index
void CookedRead::InsertCtrlZ()
{
std::deque<wchar_t> chars = { UNICODE_CTRL_Z };
_writeToPrompt(chars);
_writeToScreen(true);
}
// Routine Description:
// - Reads characters from user input
// Arguments:
// - isUnicode - TODO
// - numBytes - TODO
// - controlKeyState - TODO
// Return Value:
// - STATUS_SUCCESS if read is finished
// - CONSOLE_STATUS_WAIT if need to wait for more input
// - other status code otherwise
[[nodiscard]]
NTSTATUS CookedRead::Read(const bool /*isUnicode*/,
size_t& numBytes,
ULONG& controlKeyState) noexcept
{
controlKeyState = 0;
std::deque<wchar_t> unprocessedChars;
while(true)
{
switch(_state)
{
case ReadState::Ready:
_readChar(unprocessedChars);
break;
case ReadState::GotChar:
_processChars(unprocessedChars);
break;
case ReadState::Wait:
_wait();
return _status;
case ReadState::Complete:
_complete(numBytes);
return _status;
case ReadState::Error:
_error();
return _status;
case ReadState::CommandKey:
_commandKey();
break;
case ReadState::Popup:
_popup();
return _status;
break;
}
}
}
void CookedRead::_popup()
{
auto& commandLine = CommandLine::Instance();
FAIL_FAST_IF(!commandLine.HasPopup());
_status = commandLine.GetPopup().Process(*this);
if (_status == CONSOLE_STATUS_READ_COMPLETE ||
_status != CONSOLE_STATUS_WAIT && _status != CONSOLE_STATUS_WAIT_NO_BLOCK)
{
auto& gci = ServiceLocator::LocateGlobals().getConsoleInformation();
gci.SetCookedReadData(nullptr);
_status = S_OK; // uhh, this should be an NTSTATUS
_state = ReadState::Complete;
}
else if (_status == CONSOLE_STATUS_WAIT_NO_BLOCK)
{
_status = CONSOLE_STATUS_WAIT;
_state = ReadState::Ready;
}
}
// Routine Description:
// - handles a command line editing key
void CookedRead::_commandKey()
{
// the command line might start a popup so save the current cursor position just in case
_beforePopupCursorPosition = _screenInfo.GetTextBuffer().GetCursor().GetPosition();
_status = CommandLine::Instance().ProcessCommandLine(*this, _commandKeyChar, _keyState);
// TODO check this, it means that a popup was completed but should that mean we should go to the Ready
// or Complete state?
if (_status == CONSOLE_STATUS_READ_COMPLETE)
{
_state = ReadState::Ready;
}
else if (_status == CONSOLE_STATUS_WAIT_NO_BLOCK)
{
_status = CONSOLE_STATUS_WAIT;
_state = ReadState::Wait;
}
else if (_status == CONSOLE_STATUS_WAIT)
{
if (CommandLine::Instance().HasPopup())
{
_state = ReadState::Popup;
}
else
{
_state = ReadState::Wait;
}
}
else if (NT_SUCCESS(_status))
{
_state = ReadState::Ready;
}
else
{
_state = ReadState::Error;
}
}
// Routine Description:
// - executes wait state
void CookedRead::_wait()
{
_status = CONSOLE_STATUS_WAIT;
_state = ReadState::Ready;
}
// Routine Description:
// - executes error state
void CookedRead::_error()
{
Erase();
_state = ReadState::Ready;
}
// Routine Description:
// - executes Complete state. copies pompt data to the user buffer.
// Arguments:
// - numBytes - the number of bytes that were copied to the user buffer
void CookedRead::_complete(size_t& numBytes)
{
// store to history if enter was pressed
if (_pCommandHistory)
{
auto findIt = std::find(_prompt.cbegin(), _prompt.cend(), UNICODE_CARRIAGERETURN);
if (findIt != _prompt.cend())
{
// commands are stored without the /r/n so don't use the full prompt
const size_t length = std::distance(_prompt.cbegin(), findIt);
auto& gci = ServiceLocator::LocateGlobals().getConsoleInformation();
LOG_IF_FAILED(_pCommandHistory->Add({ _prompt.c_str(), length },
WI_IsFlagSet(gci.Flags, CONSOLE_HISTORY_NODUP)));
}
// process aliases
size_t lineCount = 0;
const std::wstring processedPrompt = Alias::s_MatchAndCopyAlias(_prompt, _exeName, lineCount);
if (!processedPrompt.empty())
{
_prompt = std::move(processedPrompt);
_insertionIndex = _prompt.size();
}
}
std::copy_n(_prompt.begin(), _prompt.size(), _userBuffer);
numBytes = _prompt.size() * sizeof(wchar_t);
_status = STATUS_SUCCESS;
_state = ReadState::Ready;
}
// Routine Description:
// - reads a character from the input buffer
// Arguments:
// - unprocessedChars - any newly read characters will be appended here
void CookedRead::_readChar(std::deque<wchar_t>& unprocessedChars)
{
wchar_t wch = UNICODE_NULL;
_commandLineEditingKeys = false;
_keyState = 0;
if (!_bufferedInput.empty())
{
wch = _bufferedInput.front();
_bufferedInput.pop_front();
_status = STATUS_SUCCESS;
}
else
{
// get a char from the input buffer
_status = GetChar(_pInputBuffer,
&wch,
true,
&_commandLineEditingKeys,
nullptr,
&_keyState);
}
if (_status == CONSOLE_STATUS_WAIT)
{
_state = ReadState::Wait;
}
else if (NT_SUCCESS(_status))
{
if (_commandLineEditingKeys)
{
_commandKeyChar = wch;
_state = ReadState::CommandKey;
return;
}
// insert char
unprocessedChars.push_back(wch);
if (Utf16Parser::IsLeadingSurrogate(wch))
{
// a leading surrogate can't be processed by iteslf so try to read another wchar
_state = ReadState::Ready;
}
else
{
_state = ReadState::GotChar;
}
}
else
{
_state = ReadState::Error;
}
}
// Routine Description:
// - processes any character data read. updates prompt and screen.
// Arguments:
// - unprocessedChars - character data to process. must not be empty
void CookedRead::_processChars(std::deque<wchar_t>& unprocessedChars)
{
FAIL_FAST_IF(unprocessedChars.empty());
_clearPromptCells();
const bool enterPressed = unprocessedChars.back() == UNICODE_CARRIAGERETURN;
// carriage return needs to be written at the end of the prompt in order to send all text correctly
if (enterPressed)
{
_insertionIndex = _prompt.size();
}
// store character data
_writeToPrompt(unprocessedChars);
// check for control character masking
if (!_prompt.empty() && _isCtrlWakeupMaskTriggered(_prompt.back()))
{
_state = ReadState::Complete;
return;
}
// update prompt shown on the screen
_writeToScreen(!enterPressed);
if (enterPressed)
{
_state = ReadState::Complete;
}
else
{
_state = ReadState::Ready;
}
}
// Routine Description:
// - overwrites prompt cells with spaces
void CookedRead::_clearPromptCells()
{
std::wstring spaces(_calculatePromptCellLength(true), UNICODE_SPACE);
LOG_IF_FAILED(_screenInfo.SetCursorPosition(_promptStartLocation, true));
size_t bytesToWrite = spaces.size() * sizeof(wchar_t);
short scrollY = 0;
_status = WriteCharsLegacy(_screenInfo,
spaces.c_str(),
spaces.c_str(),
spaces.c_str(),
&bytesToWrite,
nullptr,
_promptStartLocation.X,
WC_DESTRUCTIVE_BACKSPACE | WC_KEEP_CURSOR_VISIBLE | WC_ECHO,
&scrollY);
// move the cursor to the correct insert location
COORD cursorPosition = _promptStartLocation;
cursorPosition.X += gsl::narrow<short>(_calculatePromptCellLength(false));
_status = AdjustCursorPosition(_screenInfo, cursorPosition, true, nullptr);
FAIL_FAST_IF_NTSTATUS_FAILED(_status);
}
// Routine Description:
// - performs some character transformations and stores the text to the prompt
// Arguments:
// - unprocessedChars - the chars that need to be written to the prompt
void CookedRead::_writeToPrompt(std::deque<wchar_t>& unprocessedChars)
{
if (unprocessedChars.back() == EXTKEY_ERASE_PREV_WORD || unprocessedChars.back() == UNICODE_BACKSPACE2)
{
unprocessedChars.back() = UNICODE_BACKSPACE;
}
else if (unprocessedChars.back() == UNICODE_CARRIAGERETURN)
{
unprocessedChars.push_back(UNICODE_LINEFEED);
}
while (!unprocessedChars.empty())
{
const wchar_t wch = unprocessedChars.front();
unprocessedChars.pop_front();
if (wch == UNICODE_BACKSPACE)
{
_backspace();
}
else
{
_prompt.insert(_insertionIndex, 1, wch);
++_insertionIndex;
}
}
FAIL_FAST_IF(_insertionIndex > _prompt.size());
}
// Routine Description:
// - handles a backspace character, deleting a codepoint from the left of the insertion point
void CookedRead::_backspace()
{
// if at the beginning there's nothing to delete
if (_isInsertionIndexAtPromptBegin())
{
return;
}
// check if there's a surrogate pair to delete
if (_insertionIndex >= 2 && _isSurrogatePairAt(_insertionIndex - 2))
{
_insertionIndex -= 2;
_prompt.erase(_insertionIndex, 2);
return;
}
// there isn't a surrogate pair to the left, delete a single char
--_insertionIndex;
_prompt.erase(_insertionIndex, 1);
}
// Routine Description:
// - writes the entire prompt data to the screen
// Arguments:
// - resetCursor - true if we need to manually adjust the cursor
void CookedRead::_writeToScreen(const bool resetCursor)
{
LOG_IF_FAILED(_screenInfo.SetCursorPosition(_promptStartLocation, true));
size_t bytesToWrite = _prompt.size() * sizeof(wchar_t);
short scrollY = 0;
_status = WriteCharsLegacy(_screenInfo,
_prompt.c_str(),
_prompt.c_str(),
_prompt.c_str(),