master
h 56 lines 1.66 KB
Raw
1 /*++
2
3 Copyright (c) Microsoft. All rights reserved.
4
5 Module Name:
6
7 InputChannel.h
8
9 Abstract:
10
11 Byte source used by Terminal for user input. Reads a line at a time from the
12 console (or a redirected file/pipe). For console sources the channel can mask
13 echo while reading (password entry) and reports whether input is interactive.
14
15 --*/
16 #pragma once
17
18 #include "defs.h"
19
20 #include <cstdio>
21 #include <optional>
22 #include <string>
23 #include <Windows.h>
24
25 namespace wsl::windows::wslc {
26
27 class InputChannel
28 {
29 public:
30 NON_COPYABLE(InputChannel);
31 NON_MOVABLE(InputChannel);
32
33 // Console path: probes the handle for console mode (masking, interactivity);
34 // reads are always drawn from readFile (the CRT stdin stream).
35 InputChannel(HANDLE consoleHandle, FILE* readFile);
36
37 // FILE* path with explicit interactivity override (for tests). No console
38 // handle, so masking is a no-op and interactivity is whatever is passed.
39 InputChannel(FILE* readFile, bool interactiveOverride);
40
41 // True when input is attached to a console (a prompt can be shown, echo can be masked).
42 bool IsInteractive() const noexcept;
43
44 // Reads a single line, stripping the trailing CR and/or LF. Returns nullopt at
45 // end of input with nothing read (so an empty line and EOF are distinguishable).
46 // When mask is true and input is an interactive console, console echo is disabled
47 // for the duration of the read (restored on return, including on exception).
48 std::optional<std::wstring> ReadLine(bool mask) const;
49
50 private:
51 HANDLE m_consoleHandle = nullptr;
52 FILE* m_file = nullptr;
53 bool m_interactiveOverride = false;
54 };
55
56 } // namespace wsl::windows::wslc