From 2340d1f9d33c265a5d3a8016b62acc04b2587136 Mon Sep 17 00:00:00 2001 From: alexpeshkoff Date: Mon, 1 Dec 2008 15:40:27 +0000 Subject: [PATCH] CORE-1667 - turn echo off --- configure.in | 2 +- src/common/utils.cpp | 59 +++++++++++++++++++++++++++++++++++++------- 2 files changed, 51 insertions(+), 10 deletions(-) diff --git a/configure.in b/configure.in index 99d5e31bec..45d9b60cd9 100644 --- a/configure.in +++ b/configure.in @@ -489,8 +489,8 @@ AC_CHECK_HEADERS(stdarg.h) AC_CHECK_HEADERS(stdlib.h) AC_CHECK_HEADERS(string.h) AC_CHECK_HEADERS(signal.h sys/signal.h sys/siginfo.h) +AC_CHECK_HEADERS(termio.h termios.h) if test "$EDITLINE_FLG" = "Y"; then - AC_CHECK_HEADERS(termio.h termios.h) AC_HEADER_DIRENT AC_DEFINE(HAVE_EDITLINE_H, 1, [Define this if editline is in use]) fi diff --git a/src/common/utils.cpp b/src/common/utils.cpp index be43e53bce..4e43bed1e4 100644 --- a/src/common/utils.cpp +++ b/src/common/utils.cpp @@ -51,6 +51,9 @@ #ifdef HAVE_UNISTD_H #include #endif +#ifdef HAVE_TERMIOS_H +#include +#endif namespace fb_utils { @@ -686,33 +689,71 @@ void getCwd(Firebird::PathName& pn) } namespace { - class FileClose + class InputFile { public: - static void clear(FILE* f) + InputFile(const Firebird::PathName& name) + : flagEcho(false) { + if (name == "stdin") { + f = stdin; + } + else { + f = fopen(name.c_str(), "rt"); + } + if (isatty(fileno(f))) { +#ifdef HAVE_TERMIOS_H + flagEcho = tcgetattr(fileno(f), &oldState) == 0; + if (flagEcho) + { + flagEcho = oldState.c_lflag & ECHO; + } + if (flagEcho) + { + struct termios newState(oldState); + newState.c_lflag &= ~ECHO; + tcsetattr(fileno(f), TCSANOW, &newState); + } + fprintf(stderr, "Enter password: "); + fflush(stderr); +#endif + } + } + ~InputFile() + { +#ifdef HAVE_TERMIOS_H + if (flagEcho) { + fprintf(stderr, "\n"); + fflush(stderr); + tcsetattr(fileno(f), TCSANOW, &oldState); + } +#endif if (f && f != stdin) { fclose(f); } } + + operator FILE*() { return f;} + bool operator!() { return !f; } + + private: + FILE* f; +#ifdef HAVE_TERMIOS_H + struct termios oldState; +#endif + bool flagEcho; }; } // namespace // fetch password from file FetchPassResult fetchPassword(const Firebird::PathName& name, const char*& password) { - Firebird::AutoPtr file((name == "stdin") ? stdin : fopen(name.c_str(), "rt")); + InputFile file(name); if (!file) { return FETCH_PASS_FILE_OPEN_ERROR; } - if (isatty(fileno(file))) - { - fprintf(stderr, "Enter password: "); - fflush(stderr); - } - Firebird::string pwd; if (! pwd.LoadFromFile(file)) {