Skip to content

Commit aca7fec

Browse files
authored
Merge pull request #1506 from joto/password-prompt
Modernize the password prompt code
2 parents 1cb89ce + 877f04f commit aca7fec

File tree

6 files changed

+44
-208
lines changed

6 files changed

+44
-208
lines changed

src/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ set(osm2pgsql_lib_SOURCES
2525
pgsql-helper.cpp
2626
progress-display.cpp
2727
reprojection.cpp
28-
sprompt.cpp
2928
table.cpp
3029
taginfo.cpp
3130
tagtransform-c.cpp

src/options.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#include "logging.hpp"
1313
#include "options.hpp"
1414
#include "reprojection.hpp"
15-
#include "sprompt.hpp"
15+
#include "util.hpp"
1616
#include "version.hpp"
1717

1818
#include <algorithm>
@@ -639,10 +639,7 @@ options_t::options_t(int argc, char *argv[]) : options_t()
639639
check_options();
640640

641641
if (pass_prompt) {
642-
char const *prompt = simple_prompt("Password:", 100, 0);
643-
if (prompt != nullptr) {
644-
database_options.password = prompt;
645-
}
642+
database_options.password = util::get_password();
646643
}
647644
}
648645

src/sprompt.cpp

Lines changed: 0 additions & 187 deletions
This file was deleted.

src/sprompt.hpp

Lines changed: 0 additions & 15 deletions
This file was deleted.

src/util.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,19 @@
77
* For a full list of authors see the git log.
88
*/
99

10+
#include "config.h"
11+
1012
#include "util.hpp"
1113

14+
#include <iostream>
15+
16+
#ifdef _WIN32
17+
#include <windows.h>
18+
#elif defined(HAVE_TERMIOS_H)
19+
#include <termios.h>
20+
#include <unistd.h>
21+
#endif
22+
1223
namespace util {
1324

1425
void string_id_list_t::add(osmid_t id)
@@ -44,4 +55,33 @@ std::string human_readable_duration(std::chrono::milliseconds ms)
4455
std::chrono::duration_cast<std::chrono::seconds>(ms).count()));
4556
}
4657

58+
std::string get_password()
59+
{
60+
#ifdef _WIN32
61+
HANDLE const handle_stdin = GetStdHandle(STD_INPUT_HANDLE);
62+
DWORD mode = 0;
63+
GetConsoleMode(handle_stdin, &mode);
64+
SetConsoleMode(handle_stdin, mode & (~ENABLE_ECHO_INPUT));
65+
#elif defined(HAVE_TERMIOS_H)
66+
termios orig_flags;
67+
tcgetattr(STDIN_FILENO, &orig_flags);
68+
termios flags = orig_flags;
69+
flags.c_lflag &= ~static_cast<unsigned int>(ECHO);
70+
tcsetattr(STDIN_FILENO, TCSANOW, &flags);
71+
#endif
72+
73+
std::string password;
74+
std::cout << "Password:";
75+
std::getline(std::cin, password);
76+
std::cout << "\n";
77+
78+
#ifdef _WIN32
79+
SetConsoleMode(handle_stdin, mode);
80+
#elif defined(HAVE_TERMIOS_H)
81+
tcsetattr(STDIN_FILENO, TCSANOW, &orig_flags);
82+
#endif
83+
84+
return password;
85+
}
86+
4787
} // namespace util

src/util.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ std::string human_readable_duration(uint64_t seconds);
126126

127127
std::string human_readable_duration(std::chrono::milliseconds ms);
128128

129+
std::string get_password();
130+
129131
} // namespace util
130132

131133
#endif // OSM2PGSQL_UTIL_HPP

0 commit comments

Comments
 (0)