-
Notifications
You must be signed in to change notification settings - Fork 8.5k
/
Copy pathcmdline.cpp
1306 lines (1221 loc) · 53.9 KB
/
cmdline.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 Corporation.
// Licensed under the MIT license.
#include "precomp.h"
#include "cmdline.h"
#include "popup.h"
#include "CommandNumberPopup.hpp"
#include "CommandListPopup.hpp"
#include "CopyFromCharPopup.hpp"
#include "CopyToCharPopup.hpp"
#include "_output.h"
#include "output.h"
#include "stream.h"
#include "_stream.h"
#include "dbcs.h"
#include "handle.h"
#include "misc.h"
#include "../types/inc/convert.hpp"
#include "srvinit.h"
#include "ApiRoutines.h"
#include "../interactivity/inc/ServiceLocator.hpp"
#pragma hdrstop
using Microsoft::Console::Interactivity::ServiceLocator;
// Routine Description:
// - This routine validates a string buffer and returns the pointers of where the strings start within the buffer.
// Arguments:
// - Unicode - Supplies a boolean that is TRUE if the buffer contains Unicode strings, FALSE otherwise.
// - Buffer - Supplies the buffer to be validated.
// - Size - Supplies the size, in bytes, of the buffer to be validated.
// - Count - Supplies the expected number of strings in the buffer.
// ... - Supplies a pair of arguments per expected string. The first one is the expected size, in bytes, of the string
// and the second one receives a pointer to where the string starts.
// Return Value:
// - TRUE if the buffer is valid, FALSE otherwise.
bool IsValidStringBuffer(_In_ bool Unicode, _In_reads_bytes_(Size) PVOID Buffer, _In_ ULONG Size, _In_ ULONG Count, ...)
{
va_list Marker;
va_start(Marker, Count);
while (Count > 0)
{
const auto StringSize = va_arg(Marker, ULONG);
const auto StringStart = va_arg(Marker, PVOID*);
// Make sure the string fits in the supplied buffer and that it is properly aligned.
if (StringSize > Size)
{
break;
}
if (Unicode && (StringSize % sizeof(WCHAR)) != 0)
{
break;
}
*StringStart = Buffer;
// Go to the next string.
Buffer = RtlOffsetToPointer(Buffer, StringSize);
Size -= StringSize;
Count -= 1;
}
va_end(Marker);
return Count == 0;
}
// Routine Description:
// - Detects Word delimiters
bool IsWordDelim(const wchar_t wch)
{
// the space character is always a word delimiter. Do not add it to the WordDelimiters global because
// that contains the user configurable word delimiters only.
if (wch == UNICODE_SPACE)
{
return true;
}
const auto& delimiters = ServiceLocator::LocateGlobals().WordDelimiters;
return std::ranges::find(delimiters, wch) != delimiters.end();
}
bool IsWordDelim(const std::wstring_view charData)
{
return charData.size() == 1 && IsWordDelim(charData.front());
}
CommandLine::CommandLine() :
_isVisible{ true }
{
}
CommandLine::~CommandLine() = default;
CommandLine& CommandLine::Instance()
{
static CommandLine c;
return c;
}
bool CommandLine::IsEditLineEmpty()
{
const auto& gci = ServiceLocator::LocateGlobals().getConsoleInformation();
if (!gci.HasPendingCookedRead())
{
// If the cooked read data pointer is null, there is no edit line data and therefore it's empty.
return true;
}
else if (0 == gci.CookedReadData().VisibleCharCount())
{
// If we had a valid pointer, but there are no visible characters for the edit line, then it's empty.
// Someone started editing and back spaced the whole line out so it exists, but has no data.
return true;
}
else
{
return false;
}
}
void CommandLine::Hide(const bool fUpdateFields)
{
auto& gci = ServiceLocator::LocateGlobals().getConsoleInformation();
if (!IsEditLineEmpty())
{
DeleteCommandLine(gci.CookedReadData(), fUpdateFields);
}
_isVisible = false;
}
void CommandLine::Show()
{
_isVisible = true;
auto& gci = ServiceLocator::LocateGlobals().getConsoleInformation();
if (!IsEditLineEmpty())
{
RedrawCommandLine(gci.CookedReadData());
}
}
// Routine Description:
// - Returns true if the commandline is currently being displayed. This is false
// after Hide() is called, and before Show() is called again.
// Return Value:
// - true if the commandline should be displayed. Does not take into account
// the echo state of the input. This is only controlled by calls to Hide/Show
bool CommandLine::IsVisible() const noexcept
{
return _isVisible;
}
// Routine Description:
// - checks for the presence of a popup
// Return Value:
// - true if popup is present
bool CommandLine::HasPopup() const noexcept
{
return !_popups.empty();
}
// Routine Description:
// - gets the topmost popup
// Arguments:
// Return Value:
// - ref to the topmost popup
Popup& CommandLine::GetPopup() const
{
return *_popups.front();
}
// Routine Description:
// - stops the current popup
void CommandLine::EndCurrentPopup()
{
if (!_popups.empty())
{
_popups.front()->End();
_popups.pop_front();
}
}
// Routine Description:
// - stops all popups
void CommandLine::EndAllPopups()
{
while (!_popups.empty())
{
_popups.front()->End();
_popups.pop_front();
}
}
void DeleteCommandLine(COOKED_READ_DATA& cookedReadData, const bool fUpdateFields)
{
auto CharsToWrite = cookedReadData.VisibleCharCount();
auto coordOriginalCursor = cookedReadData.OriginalCursorPosition();
const auto coordBufferSize = cookedReadData.ScreenInfo().GetBufferSize().Dimensions();
// catch the case where the current command has scrolled off the top of the screen.
if (coordOriginalCursor.Y < 0)
{
CharsToWrite += coordBufferSize.X * coordOriginalCursor.Y;
CharsToWrite += cookedReadData.OriginalCursorPosition().X; // account for prompt
cookedReadData.OriginalCursorPosition().X = 0;
cookedReadData.OriginalCursorPosition().Y = 0;
coordOriginalCursor.X = 0;
coordOriginalCursor.Y = 0;
}
if (!CheckBisectStringW(cookedReadData.BufferStartPtr(),
CharsToWrite,
coordBufferSize.X - cookedReadData.OriginalCursorPosition().X))
{
CharsToWrite++;
}
try
{
cookedReadData.ScreenInfo().Write(OutputCellIterator(UNICODE_SPACE, CharsToWrite), coordOriginalCursor);
}
CATCH_LOG();
if (fUpdateFields)
{
cookedReadData.Erase();
}
LOG_IF_FAILED(cookedReadData.ScreenInfo().SetCursorPosition(cookedReadData.OriginalCursorPosition(), true));
}
void RedrawCommandLine(COOKED_READ_DATA& cookedReadData)
{
if (cookedReadData.IsEchoInput())
{
// Draw the command line
cookedReadData.OriginalCursorPosition() = cookedReadData.ScreenInfo().GetTextBuffer().GetCursor().GetPosition();
til::CoordType ScrollY = 0;
#pragma prefast(suppress : 28931, "Status is not unused. It's used in debug assertions.")
auto Status = WriteCharsLegacy(cookedReadData.ScreenInfo(),
cookedReadData.BufferStartPtr(),
cookedReadData.BufferStartPtr(),
cookedReadData.BufferStartPtr(),
&cookedReadData.BytesRead(),
&cookedReadData.VisibleCharCount(),
cookedReadData.OriginalCursorPosition().X,
WC_DESTRUCTIVE_BACKSPACE | WC_KEEP_CURSOR_VISIBLE | WC_PRINTABLE_CONTROL_CHARS,
&ScrollY);
FAIL_FAST_IF_NTSTATUS_FAILED(Status);
cookedReadData.OriginalCursorPosition().Y += ScrollY;
// Move the cursor back to the right position
auto CursorPosition = cookedReadData.OriginalCursorPosition();
CursorPosition.X += RetrieveTotalNumberOfSpaces(cookedReadData.OriginalCursorPosition().X,
cookedReadData.BufferStartPtr(),
cookedReadData.InsertionPoint());
if (CheckBisectStringW(cookedReadData.BufferStartPtr(),
cookedReadData.InsertionPoint(),
cookedReadData.ScreenInfo().GetBufferSize().Width() - cookedReadData.OriginalCursorPosition().X))
{
CursorPosition.X++;
}
Status = AdjustCursorPosition(cookedReadData.ScreenInfo(), CursorPosition, TRUE, nullptr);
FAIL_FAST_IF_NTSTATUS_FAILED(Status);
}
}
// Routine Description:
// - This routine copies the commandline specified by Index into the cooked read buffer
void SetCurrentCommandLine(COOKED_READ_DATA& cookedReadData, _In_ SHORT Index) // index, not command number
{
DeleteCommandLine(cookedReadData, TRUE);
FAIL_FAST_IF_FAILED(cookedReadData.History().RetrieveNth(Index,
cookedReadData.SpanWholeBuffer(),
cookedReadData.BytesRead()));
FAIL_FAST_IF(!(cookedReadData.BufferStartPtr() == cookedReadData.BufferCurrentPtr()));
if (cookedReadData.IsEchoInput())
{
til::CoordType ScrollY = 0;
FAIL_FAST_IF_NTSTATUS_FAILED(WriteCharsLegacy(cookedReadData.ScreenInfo(),
cookedReadData.BufferStartPtr(),
cookedReadData.BufferCurrentPtr(),
cookedReadData.BufferCurrentPtr(),
&cookedReadData.BytesRead(),
&cookedReadData.VisibleCharCount(),
cookedReadData.OriginalCursorPosition().X,
WC_DESTRUCTIVE_BACKSPACE | WC_KEEP_CURSOR_VISIBLE | WC_PRINTABLE_CONTROL_CHARS,
&ScrollY));
cookedReadData.OriginalCursorPosition().Y += ScrollY;
}
const auto CharsToWrite = cookedReadData.BytesRead() / sizeof(WCHAR);
cookedReadData.InsertionPoint() = CharsToWrite;
cookedReadData.SetBufferCurrentPtr(cookedReadData.BufferStartPtr() + CharsToWrite);
}
// Routine Description:
// - This routine handles the command list popup. It puts up the popup, then calls ProcessCommandListInput to get and process input.
// Return Value:
// - CONSOLE_STATUS_WAIT - we ran out of input, so a wait block was created
// - STATUS_SUCCESS - read was fully completed (user hit return)
[[nodiscard]] NTSTATUS CommandLine::_startCommandListPopup(COOKED_READ_DATA& cookedReadData)
{
if (cookedReadData.HasHistory() &&
cookedReadData.History().GetNumberOfCommands())
{
try
{
auto& popup = *_popups.emplace_front(std::make_unique<CommandListPopup>(cookedReadData.ScreenInfo(),
cookedReadData.History()));
popup.Draw();
return popup.Process(cookedReadData);
}
CATCH_RETURN();
}
else
{
return S_FALSE;
}
}
// Routine Description:
// - This routine handles the "delete up to this char" popup. It puts up the popup, then calls ProcessCopyFromCharInput to get and process input.
// Return Value:
// - CONSOLE_STATUS_WAIT - we ran out of input, so a wait block was created
// - STATUS_SUCCESS - read was fully completed (user hit return)
[[nodiscard]] NTSTATUS CommandLine::_startCopyFromCharPopup(COOKED_READ_DATA& cookedReadData)
{
// Delete the current command from cursor position to the
// letter specified by the user. The user is prompted via
// popup to enter a character.
if (cookedReadData.HasHistory())
{
try
{
auto& popup = *_popups.emplace_front(std::make_unique<CopyFromCharPopup>(cookedReadData.ScreenInfo()));
popup.Draw();
return popup.Process(cookedReadData);
}
CATCH_RETURN();
}
else
{
return S_FALSE;
}
}
// Routine Description:
// - This routine handles the "copy up to this char" popup. It puts up the popup, then calls ProcessCopyToCharInput to get and process input.
// Return Value:
// - CONSOLE_STATUS_WAIT - we ran out of input, so a wait block was created
// - STATUS_SUCCESS - read was fully completed (user hit return)
// - S_FALSE - if we couldn't make a popup because we had no commands
[[nodiscard]] NTSTATUS CommandLine::_startCopyToCharPopup(COOKED_READ_DATA& cookedReadData)
{
// copy the previous command to the current command, up to but
// not including the character specified by the user. the user
// is prompted via popup to enter a character.
if (cookedReadData.HasHistory())
{
try
{
auto& popup = *_popups.emplace_front(std::make_unique<CopyToCharPopup>(cookedReadData.ScreenInfo()));
popup.Draw();
return popup.Process(cookedReadData);
}
CATCH_RETURN();
}
else
{
return S_FALSE;
}
}
// Routine Description:
// - This routine handles the "enter command number" popup. It puts up the popup, then calls ProcessCommandNumberInput to get and process input.
// Return Value:
// - CONSOLE_STATUS_WAIT - we ran out of input, so a wait block was created
// - STATUS_SUCCESS - read was fully completed (user hit return)
// - S_FALSE - if we couldn't make a popup because we had no commands or it wouldn't fit.
[[nodiscard]] HRESULT CommandLine::StartCommandNumberPopup(COOKED_READ_DATA& cookedReadData)
{
if (cookedReadData.HasHistory() &&
cookedReadData.History().GetNumberOfCommands() &&
cookedReadData.ScreenInfo().GetBufferSize().Width() >= Popup::MINIMUM_COMMAND_PROMPT_SIZE + 2)
{
try
{
auto& popup = *_popups.emplace_front(std::make_unique<CommandNumberPopup>(cookedReadData.ScreenInfo()));
popup.Draw();
// Save the original cursor position in case the user cancels out of the dialog
cookedReadData.BeforeDialogCursorPosition() = cookedReadData.ScreenInfo().GetTextBuffer().GetCursor().GetPosition();
// Move the cursor into the dialog so the user can type multiple characters for the command number
const auto CursorPosition = popup.GetCursorPosition();
LOG_IF_FAILED(cookedReadData.ScreenInfo().SetCursorPosition(CursorPosition, TRUE));
// Transfer control to the handler routine
return popup.Process(cookedReadData);
}
CATCH_RETURN();
}
else
{
return S_FALSE;
}
}
// Routine Description:
// - Process virtual key code and updates the prompt line with the next history element in the direction
// specified by wch
// Arguments:
// - cookedReadData - The cooked read data to operate on
// - searchDirection - Direction in history to search
// Note:
// - May throw exceptions
void CommandLine::_processHistoryCycling(COOKED_READ_DATA& cookedReadData,
const CommandHistory::SearchDirection searchDirection)
{
// for doskey compatibility, buffer isn't circular. don't do anything if attempting
// to cycle history past the bounds of the history buffer
if (!cookedReadData.HasHistory())
{
return;
}
else if (searchDirection == CommandHistory::SearchDirection::Previous && cookedReadData.History().AtFirstCommand())
{
return;
}
else if (searchDirection == CommandHistory::SearchDirection::Next && cookedReadData.History().AtLastCommand())
{
return;
}
DeleteCommandLine(cookedReadData, true);
THROW_IF_FAILED(cookedReadData.History().Retrieve(searchDirection,
cookedReadData.SpanWholeBuffer(),
cookedReadData.BytesRead()));
FAIL_FAST_IF(!(cookedReadData.BufferStartPtr() == cookedReadData.BufferCurrentPtr()));
if (cookedReadData.IsEchoInput())
{
til::CoordType ScrollY = 0;
FAIL_FAST_IF_NTSTATUS_FAILED(WriteCharsLegacy(cookedReadData.ScreenInfo(),
cookedReadData.BufferStartPtr(),
cookedReadData.BufferCurrentPtr(),
cookedReadData.BufferCurrentPtr(),
&cookedReadData.BytesRead(),
&cookedReadData.VisibleCharCount(),
cookedReadData.OriginalCursorPosition().X,
WC_DESTRUCTIVE_BACKSPACE | WC_KEEP_CURSOR_VISIBLE | WC_PRINTABLE_CONTROL_CHARS,
&ScrollY));
cookedReadData.OriginalCursorPosition().Y += ScrollY;
}
const auto CharsToWrite = cookedReadData.BytesRead() / sizeof(WCHAR);
cookedReadData.InsertionPoint() = CharsToWrite;
cookedReadData.SetBufferCurrentPtr(cookedReadData.BufferStartPtr() + CharsToWrite);
}
// Routine Description:
// - Sets the text on the prompt to the oldest run command in the cookedReadData's history
// Arguments:
// - cookedReadData - The cooked read data to operate on
// Note:
// - May throw exceptions
void CommandLine::_setPromptToOldestCommand(COOKED_READ_DATA& cookedReadData)
{
if (cookedReadData.HasHistory() && cookedReadData.History().GetNumberOfCommands())
{
DeleteCommandLine(cookedReadData, true);
const short commandNumber = 0;
THROW_IF_FAILED(cookedReadData.History().RetrieveNth(commandNumber,
cookedReadData.SpanWholeBuffer(),
cookedReadData.BytesRead()));
FAIL_FAST_IF(!(cookedReadData.BufferStartPtr() == cookedReadData.BufferCurrentPtr()));
if (cookedReadData.IsEchoInput())
{
til::CoordType ScrollY = 0;
FAIL_FAST_IF_NTSTATUS_FAILED(WriteCharsLegacy(cookedReadData.ScreenInfo(),
cookedReadData.BufferStartPtr(),
cookedReadData.BufferCurrentPtr(),
cookedReadData.BufferCurrentPtr(),
&cookedReadData.BytesRead(),
&cookedReadData.VisibleCharCount(),
cookedReadData.OriginalCursorPosition().X,
WC_DESTRUCTIVE_BACKSPACE | WC_KEEP_CURSOR_VISIBLE | WC_PRINTABLE_CONTROL_CHARS,
&ScrollY));
cookedReadData.OriginalCursorPosition().Y += ScrollY;
}
auto CharsToWrite = cookedReadData.BytesRead() / sizeof(WCHAR);
cookedReadData.InsertionPoint() = CharsToWrite;
cookedReadData.SetBufferCurrentPtr(cookedReadData.BufferStartPtr() + CharsToWrite);
}
}
// Routine Description:
// - Sets the text on the prompt the most recently run command in cookedReadData's history
// Arguments:
// - cookedReadData - The cooked read data to operate on
// Note:
// - May throw exceptions
void CommandLine::_setPromptToNewestCommand(COOKED_READ_DATA& cookedReadData)
{
DeleteCommandLine(cookedReadData, true);
if (cookedReadData.HasHistory() && cookedReadData.History().GetNumberOfCommands())
{
const auto commandNumber = (SHORT)(cookedReadData.History().GetNumberOfCommands() - 1);
THROW_IF_FAILED(cookedReadData.History().RetrieveNth(commandNumber,
cookedReadData.SpanWholeBuffer(),
cookedReadData.BytesRead()));
FAIL_FAST_IF(!(cookedReadData.BufferStartPtr() == cookedReadData.BufferCurrentPtr()));
if (cookedReadData.IsEchoInput())
{
til::CoordType ScrollY = 0;
FAIL_FAST_IF_NTSTATUS_FAILED(WriteCharsLegacy(cookedReadData.ScreenInfo(),
cookedReadData.BufferStartPtr(),
cookedReadData.BufferCurrentPtr(),
cookedReadData.BufferCurrentPtr(),
&cookedReadData.BytesRead(),
&cookedReadData.VisibleCharCount(),
cookedReadData.OriginalCursorPosition().X,
WC_DESTRUCTIVE_BACKSPACE | WC_KEEP_CURSOR_VISIBLE | WC_PRINTABLE_CONTROL_CHARS,
&ScrollY));
cookedReadData.OriginalCursorPosition().Y += ScrollY;
}
auto CharsToWrite = cookedReadData.BytesRead() / sizeof(WCHAR);
cookedReadData.InsertionPoint() = CharsToWrite;
cookedReadData.SetBufferCurrentPtr(cookedReadData.BufferStartPtr() + CharsToWrite);
}
}
// Routine Description:
// - Deletes all prompt text to the right of the cursor
// Arguments:
// - cookedReadData - The cooked read data to operate on
void CommandLine::DeletePromptAfterCursor(COOKED_READ_DATA& cookedReadData) noexcept
{
DeleteCommandLine(cookedReadData, false);
cookedReadData.BytesRead() = cookedReadData.InsertionPoint() * sizeof(WCHAR);
if (cookedReadData.IsEchoInput())
{
FAIL_FAST_IF_NTSTATUS_FAILED(WriteCharsLegacy(cookedReadData.ScreenInfo(),
cookedReadData.BufferStartPtr(),
cookedReadData.BufferStartPtr(),
cookedReadData.BufferStartPtr(),
&cookedReadData.BytesRead(),
&cookedReadData.VisibleCharCount(),
cookedReadData.OriginalCursorPosition().X,
WC_DESTRUCTIVE_BACKSPACE | WC_KEEP_CURSOR_VISIBLE | WC_PRINTABLE_CONTROL_CHARS,
nullptr));
}
}
// Routine Description:
// - Deletes all user input on the prompt to the left of the cursor
// Arguments:
// - cookedReadData - The cooked read data to operate on
// Return Value:
// - The new cursor position
til::point CommandLine::_deletePromptBeforeCursor(COOKED_READ_DATA& cookedReadData) noexcept
{
DeleteCommandLine(cookedReadData, false);
cookedReadData.BytesRead() -= cookedReadData.InsertionPoint() * sizeof(WCHAR);
cookedReadData.InsertionPoint() = 0;
memmove(cookedReadData.BufferStartPtr(), cookedReadData.BufferCurrentPtr(), cookedReadData.BytesRead());
cookedReadData.SetBufferCurrentPtr(cookedReadData.BufferStartPtr());
if (cookedReadData.IsEchoInput())
{
FAIL_FAST_IF_NTSTATUS_FAILED(WriteCharsLegacy(cookedReadData.ScreenInfo(),
cookedReadData.BufferStartPtr(),
cookedReadData.BufferStartPtr(),
cookedReadData.BufferStartPtr(),
&cookedReadData.BytesRead(),
&cookedReadData.VisibleCharCount(),
cookedReadData.OriginalCursorPosition().X,
WC_DESTRUCTIVE_BACKSPACE | WC_KEEP_CURSOR_VISIBLE | WC_PRINTABLE_CONTROL_CHARS,
nullptr));
}
return cookedReadData.OriginalCursorPosition();
}
// Routine Description:
// - Moves the cursor to the end of the prompt text
// Arguments:
// - cookedReadData - The cooked read data to operate on
// Return Value:
// - The new cursor position
til::point CommandLine::_moveCursorToEndOfPrompt(COOKED_READ_DATA& cookedReadData) noexcept
{
cookedReadData.InsertionPoint() = cookedReadData.BytesRead() / sizeof(WCHAR);
cookedReadData.SetBufferCurrentPtr(cookedReadData.BufferStartPtr() + cookedReadData.InsertionPoint());
til::point cursorPosition;
cursorPosition.X = gsl::narrow<til::CoordType>(cookedReadData.OriginalCursorPosition().X + cookedReadData.VisibleCharCount());
cursorPosition.Y = cookedReadData.OriginalCursorPosition().Y;
const auto sScreenBufferSizeX = cookedReadData.ScreenInfo().GetBufferSize().Width();
if (CheckBisectProcessW(cookedReadData.ScreenInfo(),
cookedReadData.BufferStartPtr(),
cookedReadData.InsertionPoint(),
sScreenBufferSizeX - cookedReadData.OriginalCursorPosition().X,
cookedReadData.OriginalCursorPosition().X,
true))
{
cursorPosition.X++;
}
return cursorPosition;
}
// Routine Description:
// - Moves the cursor to the start of the user input on the prompt
// Arguments:
// - cookedReadData - The cooked read data to operate on
// Return Value:
// - The new cursor position
til::point CommandLine::_moveCursorToStartOfPrompt(COOKED_READ_DATA& cookedReadData) noexcept
{
cookedReadData.InsertionPoint() = 0;
cookedReadData.SetBufferCurrentPtr(cookedReadData.BufferStartPtr());
return cookedReadData.OriginalCursorPosition();
}
// Routine Description:
// - Moves the cursor left by a word
// Arguments:
// - cookedReadData - The cooked read data to operate on
// Return Value:
// - New cursor position
til::point CommandLine::_moveCursorLeftByWord(COOKED_READ_DATA& cookedReadData) noexcept
{
PWCHAR LastWord;
auto cursorPosition = cookedReadData.ScreenInfo().GetTextBuffer().GetCursor().GetPosition();
if (cookedReadData.BufferCurrentPtr() != cookedReadData.BufferStartPtr())
{
// A bit better word skipping.
LastWord = cookedReadData.BufferCurrentPtr() - 1;
if (LastWord != cookedReadData.BufferStartPtr())
{
if (*LastWord == L' ')
{
// Skip spaces, until the non-space character is found.
while (--LastWord != cookedReadData.BufferStartPtr())
{
FAIL_FAST_IF(!(LastWord > cookedReadData.BufferStartPtr()));
if (*LastWord != L' ')
{
break;
}
}
}
if (LastWord != cookedReadData.BufferStartPtr())
{
if (IsWordDelim(*LastWord))
{
// Skip WORD_DELIMs until space or non WORD_DELIM is found.
while (--LastWord != cookedReadData.BufferStartPtr())
{
FAIL_FAST_IF(!(LastWord > cookedReadData.BufferStartPtr()));
if (*LastWord == L' ' || !IsWordDelim(*LastWord))
{
break;
}
}
}
else
{
// Skip the regular words
while (--LastWord != cookedReadData.BufferStartPtr())
{
FAIL_FAST_IF(!(LastWord > cookedReadData.BufferStartPtr()));
if (IsWordDelim(*LastWord))
{
break;
}
}
}
}
FAIL_FAST_IF(!(LastWord >= cookedReadData.BufferStartPtr()));
if (LastWord != cookedReadData.BufferStartPtr())
{
// LastWord is currently pointing to the last character
// of the previous word, unless it backed up to the beginning
// of the buffer.
// Let's increment LastWord so that it points to the expected
// insertion point.
++LastWord;
}
cookedReadData.SetBufferCurrentPtr(LastWord);
}
cookedReadData.InsertionPoint() = (cookedReadData.BufferCurrentPtr() - cookedReadData.BufferStartPtr());
cursorPosition = cookedReadData.OriginalCursorPosition();
cursorPosition.X = cursorPosition.X +
RetrieveTotalNumberOfSpaces(cookedReadData.OriginalCursorPosition().X,
cookedReadData.BufferStartPtr(),
cookedReadData.InsertionPoint());
const auto sScreenBufferSizeX = cookedReadData.ScreenInfo().GetBufferSize().Width();
if (CheckBisectStringW(cookedReadData.BufferStartPtr(),
cookedReadData.InsertionPoint() + 1,
sScreenBufferSizeX - cookedReadData.OriginalCursorPosition().X))
{
cursorPosition.X++;
}
}
return cursorPosition;
}
// Routine Description:
// - Moves cursor left by a glyph
// Arguments:
// - cookedReadData - The cooked read data to operate on
// Return Value:
// - New cursor position
til::point CommandLine::_moveCursorLeft(COOKED_READ_DATA& cookedReadData)
{
auto cursorPosition = cookedReadData.ScreenInfo().GetTextBuffer().GetCursor().GetPosition();
if (cookedReadData.BufferCurrentPtr() != cookedReadData.BufferStartPtr())
{
cookedReadData.SetBufferCurrentPtr(cookedReadData.BufferCurrentPtr() - 1);
cookedReadData.InsertionPoint()--;
cursorPosition.X = cookedReadData.ScreenInfo().GetTextBuffer().GetCursor().GetPosition().X;
cursorPosition.Y = cookedReadData.ScreenInfo().GetTextBuffer().GetCursor().GetPosition().Y;
cursorPosition.X = cursorPosition.X -
RetrieveNumberOfSpaces(cookedReadData.OriginalCursorPosition().X,
cookedReadData.BufferStartPtr(),
cookedReadData.InsertionPoint());
const auto sScreenBufferSizeX = cookedReadData.ScreenInfo().GetBufferSize().Width();
if (CheckBisectProcessW(cookedReadData.ScreenInfo(),
cookedReadData.BufferStartPtr(),
cookedReadData.InsertionPoint() + 2,
sScreenBufferSizeX - cookedReadData.OriginalCursorPosition().X,
cookedReadData.OriginalCursorPosition().X,
true))
{
if ((cursorPosition.X == -2) || (cursorPosition.X == -1))
{
cursorPosition.X--;
}
}
}
return cursorPosition;
}
// Routine Description:
// - Moves the cursor to the right by a word
// Arguments:
// - cookedReadData - The cooked read data to operate on
// Return Value:
// - The new cursor position
til::point CommandLine::_moveCursorRightByWord(COOKED_READ_DATA& cookedReadData) noexcept
{
auto cursorPosition = cookedReadData.ScreenInfo().GetTextBuffer().GetCursor().GetPosition();
if (cookedReadData.InsertionPoint() < (cookedReadData.BytesRead() / sizeof(WCHAR)))
{
auto NextWord = cookedReadData.BufferCurrentPtr();
// A bit better word skipping.
auto BufLast = cookedReadData.BufferStartPtr() + cookedReadData.BytesRead() / sizeof(WCHAR);
FAIL_FAST_IF(!(NextWord < BufLast));
if (*NextWord == L' ')
{
// If the current character is space, skip to the next non-space character.
while (++NextWord < BufLast)
{
if (*NextWord != L' ')
{
break;
}
}
}
else
{
// Skip the body part.
auto fStartFromDelim = IsWordDelim(*NextWord);
while (++NextWord < BufLast)
{
if (fStartFromDelim != IsWordDelim(*NextWord))
{
break;
}
}
// Skip the space block.
for (; NextWord < BufLast; NextWord++)
{
if (*NextWord != L' ')
{
break;
}
}
}
cookedReadData.SetBufferCurrentPtr(NextWord);
cookedReadData.InsertionPoint() = (ULONG)(cookedReadData.BufferCurrentPtr() - cookedReadData.BufferStartPtr());
cursorPosition = cookedReadData.OriginalCursorPosition();
cursorPosition.X = cursorPosition.X +
RetrieveTotalNumberOfSpaces(cookedReadData.OriginalCursorPosition().X,
cookedReadData.BufferStartPtr(),
cookedReadData.InsertionPoint());
const auto sScreenBufferSizeX = cookedReadData.ScreenInfo().GetBufferSize().Width();
if (CheckBisectStringW(cookedReadData.BufferStartPtr(),
cookedReadData.InsertionPoint() + 1,
sScreenBufferSizeX - cookedReadData.OriginalCursorPosition().X))
{
cursorPosition.X++;
}
}
return cursorPosition;
}
// Routine Description:
// - Moves the cursor to the right by a glyph
// Arguments:
// - cookedReadData - The cooked read data to operate on
// Return Value:
// - The new cursor position
til::point CommandLine::_moveCursorRight(COOKED_READ_DATA& cookedReadData) noexcept
{
auto cursorPosition = cookedReadData.ScreenInfo().GetTextBuffer().GetCursor().GetPosition();
const auto sScreenBufferSizeX = cookedReadData.ScreenInfo().GetBufferSize().Width();
// If not at the end of the line, move cursor position right.
if (cookedReadData.InsertionPoint() < (cookedReadData.BytesRead() / sizeof(WCHAR)))
{
cursorPosition = cookedReadData.ScreenInfo().GetTextBuffer().GetCursor().GetPosition();
cursorPosition.X = cursorPosition.X +
RetrieveNumberOfSpaces(cookedReadData.OriginalCursorPosition().X,
cookedReadData.BufferStartPtr(),
cookedReadData.InsertionPoint());
if (CheckBisectProcessW(cookedReadData.ScreenInfo(),
cookedReadData.BufferStartPtr(),
cookedReadData.InsertionPoint() + 2,
sScreenBufferSizeX - cookedReadData.OriginalCursorPosition().X,
cookedReadData.OriginalCursorPosition().X,
true))
{
// Snap cursorPosition.X to sScreenBufferSizeX if it is at the edge of the screen
if (cursorPosition.X == (sScreenBufferSizeX - 1))
cursorPosition.X = sScreenBufferSizeX;
}
cookedReadData.SetBufferCurrentPtr(cookedReadData.BufferCurrentPtr() + 1);
cookedReadData.InsertionPoint()++;
}
// if at the end of the line, copy a character from the same position in the last command
else if (cookedReadData.HasHistory())
{
size_t NumSpaces;
const auto LastCommand = cookedReadData.History().GetLastCommand();
if (!LastCommand.empty() && LastCommand.size() > cookedReadData.InsertionPoint())
{
*cookedReadData.BufferCurrentPtr() = LastCommand[cookedReadData.InsertionPoint()];
cookedReadData.BytesRead() += sizeof(WCHAR);
cookedReadData.InsertionPoint()++;
if (cookedReadData.IsEchoInput())
{
til::CoordType ScrollY = 0;
auto CharsToWrite = sizeof(WCHAR);
FAIL_FAST_IF_NTSTATUS_FAILED(WriteCharsLegacy(cookedReadData.ScreenInfo(),
cookedReadData.BufferStartPtr(),
cookedReadData.BufferCurrentPtr(),
cookedReadData.BufferCurrentPtr(),
&CharsToWrite,
&NumSpaces,
cookedReadData.OriginalCursorPosition().X,
WC_DESTRUCTIVE_BACKSPACE | WC_KEEP_CURSOR_VISIBLE | WC_PRINTABLE_CONTROL_CHARS,
&ScrollY));
cookedReadData.OriginalCursorPosition().Y += ScrollY;
cookedReadData.VisibleCharCount() += NumSpaces;
// update reported cursor position
if (ScrollY != 0)
{
cursorPosition.X = 0;
cursorPosition.Y += ScrollY;
}
else
{
cursorPosition.X += 1;
}
}
cookedReadData.SetBufferCurrentPtr(cookedReadData.BufferCurrentPtr() + 1);
}
}
return cursorPosition;
}
// Routine Description:
// - Place a ctrl-z in the current command line
// Arguments:
// - cookedReadData - The cooked read data to operate on
void CommandLine::_insertCtrlZ(COOKED_READ_DATA& cookedReadData) noexcept
{
size_t NumSpaces = 0;
*cookedReadData.BufferCurrentPtr() = (WCHAR)0x1a; // ctrl-z
cookedReadData.BytesRead() += sizeof(WCHAR);
cookedReadData.InsertionPoint()++;
if (cookedReadData.IsEchoInput())
{
til::CoordType ScrollY = 0;
auto CharsToWrite = sizeof(WCHAR);
FAIL_FAST_IF_NTSTATUS_FAILED(WriteCharsLegacy(cookedReadData.ScreenInfo(),
cookedReadData.BufferStartPtr(),
cookedReadData.BufferCurrentPtr(),
cookedReadData.BufferCurrentPtr(),
&CharsToWrite,
&NumSpaces,
cookedReadData.OriginalCursorPosition().X,
WC_DESTRUCTIVE_BACKSPACE | WC_KEEP_CURSOR_VISIBLE | WC_PRINTABLE_CONTROL_CHARS,
&ScrollY));
cookedReadData.OriginalCursorPosition().Y += ScrollY;
cookedReadData.VisibleCharCount() += NumSpaces;
}
cookedReadData.SetBufferCurrentPtr(cookedReadData.BufferCurrentPtr() + 1);
}
// Routine Description:
// - Empties the command history for cookedReadData
// Arguments:
// - cookedReadData - The cooked read data to operate on
void CommandLine::_deleteCommandHistory(COOKED_READ_DATA& cookedReadData) noexcept
{
if (cookedReadData.HasHistory())
{
cookedReadData.History().Empty();
cookedReadData.History().Flags |= CommandHistory::CLE_ALLOCATED;
}
}
// Routine Description:
// - Copy the remainder of the previous command to the current command.
// Arguments:
// - cookedReadData - The cooked read data to operate on
void CommandLine::_fillPromptWithPreviousCommandFragment(COOKED_READ_DATA& cookedReadData) noexcept
{
if (cookedReadData.HasHistory())
{
size_t NumSpaces, cchCount;
const auto LastCommand = cookedReadData.History().GetLastCommand();
if (!LastCommand.empty() && LastCommand.size() > cookedReadData.InsertionPoint())
{
cchCount = LastCommand.size() - cookedReadData.InsertionPoint();
const auto bufferSpan = cookedReadData.SpanAtPointer();
std::copy_n(LastCommand.cbegin() + cookedReadData.InsertionPoint(), cchCount, bufferSpan.begin());
cookedReadData.InsertionPoint() += cchCount;
cchCount *= sizeof(WCHAR);
cookedReadData.BytesRead() = std::max(LastCommand.size() * sizeof(wchar_t), cookedReadData.BytesRead());
if (cookedReadData.IsEchoInput())
{
til::CoordType ScrollY = 0;
FAIL_FAST_IF_NTSTATUS_FAILED(WriteCharsLegacy(cookedReadData.ScreenInfo(),
cookedReadData.BufferStartPtr(),
cookedReadData.BufferCurrentPtr(),
cookedReadData.BufferCurrentPtr(),
&cchCount,
&NumSpaces,
cookedReadData.OriginalCursorPosition().X,
WC_DESTRUCTIVE_BACKSPACE | WC_KEEP_CURSOR_VISIBLE | WC_PRINTABLE_CONTROL_CHARS,
&ScrollY));
cookedReadData.OriginalCursorPosition().Y += ScrollY;
cookedReadData.VisibleCharCount() += NumSpaces;
}
cookedReadData.SetBufferCurrentPtr(cookedReadData.BufferCurrentPtr() + cchCount / sizeof(WCHAR));
}
}
}
// Routine Description:
// - Cycles through the stored commands that start with the characters in the current command.
// Arguments:
// - cookedReadData - The cooked read data to operate on
// Return Value:
// - The new cursor position
til::point CommandLine::_cycleMatchingCommandHistoryToPrompt(COOKED_READ_DATA& cookedReadData)
{
auto cursorPosition = cookedReadData.ScreenInfo().GetTextBuffer().GetCursor().GetPosition();
if (cookedReadData.HasHistory())
{
SHORT index;
if (cookedReadData.History().FindMatchingCommand({ cookedReadData.BufferStartPtr(), cookedReadData.InsertionPoint() },
cookedReadData.History().LastDisplayed,
index,
CommandHistory::MatchOptions::None))
{
// save cursor position
const auto CurrentPos = cookedReadData.InsertionPoint();
DeleteCommandLine(cookedReadData, true);
THROW_IF_FAILED(cookedReadData.History().RetrieveNth((SHORT)index,
cookedReadData.SpanWholeBuffer(),
cookedReadData.BytesRead()));
FAIL_FAST_IF(!(cookedReadData.BufferStartPtr() == cookedReadData.BufferCurrentPtr()));