tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

UserInputUtils.cpp (2787B)


      1 // UserInputUtils.cpp
      2 
      3 #include "StdAfx.h"
      4 
      5 #include "../../../Common/StdInStream.h"
      6 #include "../../../Common/StringConvert.h"
      7 
      8 #include "UserInputUtils.h"
      9 
     10 static const char kYes = 'y';
     11 static const char kNo = 'n';
     12 static const char kYesAll = 'a';
     13 static const char kNoAll = 's';
     14 static const char kAutoRenameAll = 'u';
     15 static const char kQuit = 'q';
     16 
     17 static const char * const kFirstQuestionMessage = "? ";
     18 static const char * const kHelpQuestionMessage =
     19  "(Y)es / (N)o / (A)lways / (S)kip all / A(u)to rename all / (Q)uit? ";
     20 
     21 // return true if pressed Quite;
     22 
     23 NUserAnswerMode::EEnum ScanUserYesNoAllQuit(CStdOutStream *outStream)
     24 {
     25  if (outStream)
     26    *outStream << kFirstQuestionMessage;
     27  for (;;)
     28  {
     29    if (outStream)
     30    {
     31      *outStream << kHelpQuestionMessage;
     32      outStream->Flush();
     33    }
     34    AString scannedString;
     35    if (!g_StdIn.ScanAStringUntilNewLine(scannedString))
     36      return NUserAnswerMode::kError;
     37    if (g_StdIn.Error())
     38      return NUserAnswerMode::kError;
     39    scannedString.Trim();
     40    if (scannedString.IsEmpty() && g_StdIn.Eof())
     41      return NUserAnswerMode::kEof;
     42 
     43    if (scannedString.Len() == 1)
     44      switch (::MyCharLower_Ascii(scannedString[0]))
     45      {
     46        case kYes:    return NUserAnswerMode::kYes;
     47        case kNo:     return NUserAnswerMode::kNo;
     48        case kYesAll: return NUserAnswerMode::kYesAll;
     49        case kNoAll:  return NUserAnswerMode::kNoAll;
     50        case kAutoRenameAll: return NUserAnswerMode::kAutoRenameAll;
     51        case kQuit:   return NUserAnswerMode::kQuit;
     52      }
     53  }
     54 }
     55 
     56 #ifdef _WIN32
     57 #ifndef UNDER_CE
     58 #define MY_DISABLE_ECHO
     59 #endif
     60 #endif
     61 
     62 static bool GetPassword(CStdOutStream *outStream, UString &psw)
     63 {
     64  if (outStream)
     65  {
     66    *outStream << "\nEnter password"
     67      #ifdef MY_DISABLE_ECHO
     68      " (will not be echoed)"
     69      #endif
     70      ":";
     71    outStream->Flush();
     72  }
     73 
     74  #ifdef MY_DISABLE_ECHO
     75  
     76  HANDLE console = GetStdHandle(STD_INPUT_HANDLE);
     77  bool wasChanged = false;
     78  DWORD mode = 0;
     79  if (console != INVALID_HANDLE_VALUE && console != 0)
     80    if (GetConsoleMode(console, &mode))
     81      wasChanged = (SetConsoleMode(console, mode & ~ENABLE_ECHO_INPUT) != 0);
     82  bool res = g_StdIn.ScanUStringUntilNewLine(psw);
     83  if (wasChanged)
     84    SetConsoleMode(console, mode);
     85  
     86  #else
     87  
     88  bool res = g_StdIn.ScanUStringUntilNewLine(psw);
     89  
     90  #endif
     91 
     92  if (outStream)
     93  {
     94    *outStream << endl;
     95    outStream->Flush();
     96  }
     97 
     98  return res;
     99 }
    100 
    101 HRESULT GetPassword_HRESULT(CStdOutStream *outStream, UString &psw)
    102 {
    103  if (!GetPassword(outStream, psw))
    104    return E_INVALIDARG;
    105  if (g_StdIn.Error())
    106    return E_FAIL;
    107  if (g_StdIn.Eof() && psw.IsEmpty())
    108    return E_ABORT;
    109  return S_OK;
    110 }