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
|
#include "common.h"
#include <util/folder/dirut.h>
#include <util/folder/path.h>
#include <util/string/strip.h>
#if defined(_unix_)
#include <sys/ioctl.h>
#include <termios.h>
#elif defined(_win_)
#include <windows.h>
#endif
namespace NYdb {
namespace NConsoleClient {
TProfileConfig::TProfileConfig(const TString& profileName)
: ProfileName(profileName)
{
}
bool TProfileConfig::GetVariable(const TString& name, TString& value) {
if (!InMemory) {
ReadFromFile();
}
const auto it = Values.find(name);
if (it != Values.end()) {
value = it->second;
return true;
}
return false;
}
void TProfileConfig::ReadFromFile() {
TString profileConfigFile = "~/.ydb/" + ProfileName + "/config";
correctpath(profileConfigFile);
TFsPath configFile(profileConfigFile);
if (!configFile.Exists()) {
throw yexception() << "Can't find provided config file \"" << profileConfigFile << "\" for given profile.";
}
TFileInput config(profileConfigFile);
TString line;
while (config.ReadLine(line)) {
auto it = line.find('=');
if (it != TString::npos) {
TString name = Strip(line.substr(0, it));
TString value = Strip(line.substr(it + 1));
if (name && value) {
Values[name] = value;
continue;
}
}
throw yexception() << "Error parsing config file \"" << profileConfigFile << "\" for given profile.";
}
if (Values.empty()) {
throw yexception() << "Empty config file \"" << profileConfigFile << "\" for given profile.";
}
InMemory = true;
}
bool ReadFromFileIfExists(TString& filePath, const TString& fileName, TString& output, bool allowEmpty) {
if (filePath.StartsWith("~")) {
filePath = HomeDir + filePath.substr(1);
}
TFsPath fsPath(filePath);
if (!fsPath.Exists()) {
correctpath(filePath);
fsPath = TFsPath(filePath);
}
TString content;
if (fsPath.Exists()) {
content = Strip(TUnbufferedFileInput(fsPath).ReadAll());
if (!content && !allowEmpty) {
throw yexception() << "Empty " << fileName << " file provided: \"" << filePath << "\".";
} else {
output = content;
return true;
}
}
return false;
}
TString ReadFromFile(TString& filePath, const TString& fileName, bool allowEmpty) {
TString content;
if (ReadFromFileIfExists(filePath, fileName, content, allowEmpty)) {
return content;
} else {
throw yexception() << "Can't find " << fileName << " file \"" << filePath << "\".";
}
}
size_t TermWidth() {
#ifdef _unix_
struct winsize ws;
if (!ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws)) {
return ws.ws_col;
}
#endif
return Max<size_t>();
}
TString InputPassword() {
// Disable echoing characters and enable per-symbol input handling
#if defined(_unix_)
termios oldState;
tcgetattr(STDIN_FILENO, &oldState);
termios newState = oldState;
newState.c_lflag &= ~ECHO;
newState.c_lflag &= VEOF;
tcsetattr(STDIN_FILENO, TCSANOW, &newState);
#elif defined(_win_)
HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
DWORD mode;
DWORD count;
GetConsoleMode(hStdin, &mode);
SetConsoleMode(hStdin, mode & (~ENABLE_ECHO_INPUT) & (~ENABLE_LINE_INPUT));
#endif
TString password;
char c;
for (
#if defined(_unix_)
Cin >> c;
#elif defined(_win_)
// Cin doesn't handle "Enter" press properly on Windows
ReadConsoleA(hStdin, &c, 1, &count, NULL);
#endif
c != '\r' && c != '\n';
#if defined(_unix_)
Cin >> c
#elif defined(_win_)
ReadConsoleA(hStdin, &c, 1, &count, NULL)
#endif
) {
if (c == '\b' || c == 0x7F) {
// Backspace. Remove last char if there is any
if (password.size()) {
Cout << "\b \b";
password.pop_back();
}
} else if (c == 0x03) {
// Ctrl + C
Cerr << Endl << "Input interrupted" << Endl;
#if defined(_unix_)
tcsetattr(STDIN_FILENO, TCSANOW, &oldState);
#elif defined(_win_)
SetConsoleMode(hStdin, mode);
#endif
exit(EXIT_FAILURE);
} else {
Cout << '*';
password.push_back(c);
}
}
Cout << Endl;
#if defined(_unix_)
tcsetattr(STDIN_FILENO, TCSANOW, &oldState);
#elif defined(_win_)
SetConsoleMode(hStdin, mode);
#endif
return password;
}
}
}
|