Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix console aliases not working #14991

Merged
merged 3 commits into from
Mar 17, 2023

Conversation

lhecker
Copy link
Member

@lhecker lhecker commented Mar 14, 2023

#14745 contains two regressions related to console alias handling:

  • When ProcessAliases expands the backup buffer into (an) aliased
    command(s) it changes the _bytesRead field of COOKED_READ_DATA,
    requiring us to re-read it and reconstruct the input string-view.
  • Multiline aliases are read line-by-line whereas Remove TranslateUnicodeToOem and all related code #14745 didn't treat
    them any different from regular single-line inputs.

Validation Steps Performed

In cmd.exe run

doskey test=echo foo$Techo bar$Techo baz
test

The output should look exactly like this:

C:\>doskey test=echo foo$Techo bar$Techo baz

C:\>test
foo

C:\>bar

C:\>baz

C:\>

@lhecker lhecker added Product-Conhost For issues in the Console codebase Area-Output Related to output processing (inserting text into buffer, retrieving buffer text, etc.) Issue-Bug It either shouldn't be doing this or needs an investigation. Priority-1 A description (P1) labels Mar 14, 2023
@@ -20,7 +20,7 @@ bool INPUT_READ_HANDLE_DATA::IsInputPending() const

bool INPUT_READ_HANDLE_DATA::IsMultilineInput() const
{
FAIL_FAST_IF(!_isInputPending); // we shouldn't have multiline input without a pending input.
assert(_isInputPending); // we shouldn't have multiline input without a pending input.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another one down the list #14298.

Comment on lines 1048 to 1054
// This is a continuation of the above identical if condition.
// We've truncated the `input` slice and now we need to restore it.
const auto inputSizeAfter = input.size();
const auto amountConsumed = inputSizeBefore - inputSizeAfter;
input = { _backupLimit, _bytesRead / sizeof(wchar_t) };
input = input.substr(amountConsumed);
GetInputReadHandleData()->SaveMultilinePendingInput(input);
Copy link
Member Author

@lhecker lhecker Mar 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't necessarily the most readable code I've written so far... But it does do the correct thing: After Consume() returns, input might not be empty! So if we restore the original, full input slice we need to manually calculate the amount of data that was read (amountConsumed).

An alternative I've considered is to change Consume() to return the amount of characters that were read, but given how Consume() works internally, this would be identical to this inputSizeBefore / inputSizeAfter code. Alternatively we could pass a "character limit" argument, but that would be, once again, similar to the existing code, since string-views are already strings with a limit, so I felt like passing 2 limits wouldn't be any less confusing.

Comment on lines 309 to 311
const auto inputSizeAfter = input.size();
const auto amountConsumed = inputSizeBefore - inputSizeAfter;
input = pending.substr(amountConsumed);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code block is identical to the other one, but it's shorter because we can easily access the previously saved pending slice.

@zadjii-msft
Copy link
Member

@miniksa thanks for selfhosting 😉

Copy link
Member

@DHowett DHowett left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, this makes some sense to me.

@DHowett
Copy link
Member

DHowett commented Mar 14, 2023

I have to trust, to some extent, the code here. I understand it but only to a point. :D

@lhecker lhecker force-pushed the dev/lhecker/8000-TranslateUnicodeToOem-fixup branch from bfb11cd to 1924a7e Compare March 14, 2023 16:54
@DHowett DHowett added the Needs-Second It's a PR that needs another sign-off label Mar 16, 2023
Comment on lines +1023 to +1024
// Don't be fooled by ProcessAliases only taking one argument. It rewrites multiple
// class members on return, including `_bytesRead`, requiring us to reconstruct `input`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good callout!

@microsoft-github-policy-service microsoft-github-policy-service bot removed the Needs-Second It's a PR that needs another sign-off label Mar 17, 2023
@carlos-zamora carlos-zamora merged commit 00af187 into main Mar 17, 2023
@carlos-zamora carlos-zamora deleted the dev/lhecker/8000-TranslateUnicodeToOem-fixup branch March 17, 2023 20:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Output Related to output processing (inserting text into buffer, retrieving buffer text, etc.) Issue-Bug It either shouldn't be doing this or needs an investigation. Priority-1 A description (P1) Product-Conhost For issues in the Console codebase
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants