浏览代码

Refactoring TConsole interface

(cherry picked from commit d2afd421bfbbc38377bf559e9a0f23b56c337513)

Source commit: 850e8690d7ba1fdaac976fa1a91a83738517999b
Martin Prikryl 5 年之前
父节点
当前提交
f18a7c022b
共有 2 个文件被更改,包括 91 次插入122 次删除
  1. 80 116
      source/windows/ConsoleRunner.cpp
  2. 11 6
      source/windows/WinInterface.h

+ 80 - 116
source/windows/ConsoleRunner.cpp

@@ -55,15 +55,10 @@ public:
   virtual int __fastcall Choice(
   virtual int __fastcall Choice(
     UnicodeString Options, int Cancel, int Break, int Continue, int Timeouted, bool Timeouting, unsigned int Timer,
     UnicodeString Options, int Cancel, int Break, int Continue, int Timeouted, bool Timeouting, unsigned int Timer,
     UnicodeString Message);
     UnicodeString Message);
+  virtual bool __fastcall HasFlag(TConsoleFlag Flag) const;
   virtual bool __fastcall PendingAbort();
   virtual bool __fastcall PendingAbort();
   virtual void __fastcall SetTitle(UnicodeString Title);
   virtual void __fastcall SetTitle(UnicodeString Title);
-  virtual bool __fastcall LimitedOutput();
-  virtual bool __fastcall LiveOutput();
-  virtual bool __fastcall NoInteractiveInput();
-  virtual bool __fastcall Interactive();
   virtual void __fastcall WaitBeforeExit();
   virtual void __fastcall WaitBeforeExit();
-  virtual bool __fastcall CommandLineOnly();
-  virtual bool __fastcall WantsProgress();
   virtual void __fastcall Progress(TScriptProgress & Progress);
   virtual void __fastcall Progress(TScriptProgress & Progress);
   virtual UnicodeString __fastcall FinalLogMessage();
   virtual UnicodeString __fastcall FinalLogMessage();
 
 
@@ -464,24 +459,24 @@ void __fastcall TOwnConsole::SetTitle(UnicodeString Title)
   SetConsoleTitle(Title.c_str());
   SetConsoleTitle(Title.c_str());
 }
 }
 //---------------------------------------------------------------------------
 //---------------------------------------------------------------------------
-bool __fastcall TOwnConsole::LimitedOutput()
+bool __fastcall TOwnConsole::HasFlag(TConsoleFlag Flag) const
 {
 {
-  return true;
-}
-//---------------------------------------------------------------------------
-bool __fastcall TOwnConsole::LiveOutput()
-{
-  return true;
-}
-//---------------------------------------------------------------------------
-bool __fastcall TOwnConsole::NoInteractiveInput()
-{
-  return false;
-}
-//---------------------------------------------------------------------------
-bool __fastcall TOwnConsole::Interactive()
-{
-  return true;
+  switch (Flag)
+  {
+    case cfLimitedOutput:
+    case cfLiveOutput:
+    case cfInteractive:
+      return true;
+
+    case cfNoInteractiveInput:
+    case cfCommandLineOnly:
+    case cfWantsProgress:
+      return false;
+
+    default:
+      DebugFail();
+      return false;
+  }
 }
 }
 //---------------------------------------------------------------------------
 //---------------------------------------------------------------------------
 void __fastcall TOwnConsole::WaitBeforeExit()
 void __fastcall TOwnConsole::WaitBeforeExit()
@@ -503,16 +498,6 @@ void __fastcall TOwnConsole::WaitBeforeExit()
   }
   }
 }
 }
 //---------------------------------------------------------------------------
 //---------------------------------------------------------------------------
-bool __fastcall TOwnConsole::CommandLineOnly()
-{
-  return false;
-}
-//---------------------------------------------------------------------------
-bool __fastcall TOwnConsole::WantsProgress()
-{
-  return false;
-}
-//---------------------------------------------------------------------------
 void __fastcall TOwnConsole::Progress(TScriptProgress & /*Progress*/)
 void __fastcall TOwnConsole::Progress(TScriptProgress & /*Progress*/)
 {
 {
   DebugFail();
   DebugFail();
@@ -534,15 +519,10 @@ public:
   virtual int __fastcall Choice(
   virtual int __fastcall Choice(
     UnicodeString Options, int Cancel, int Break, int Continue, int Timeouted, bool Timeouting, unsigned int Timer,
     UnicodeString Options, int Cancel, int Break, int Continue, int Timeouted, bool Timeouting, unsigned int Timer,
     UnicodeString Message);
     UnicodeString Message);
+  virtual bool __fastcall HasFlag(TConsoleFlag Flag) const;
   virtual bool __fastcall PendingAbort();
   virtual bool __fastcall PendingAbort();
   virtual void __fastcall SetTitle(UnicodeString Title);
   virtual void __fastcall SetTitle(UnicodeString Title);
-  virtual bool __fastcall LimitedOutput();
-  virtual bool __fastcall LiveOutput();
-  virtual bool __fastcall NoInteractiveInput();
   virtual void __fastcall WaitBeforeExit();
   virtual void __fastcall WaitBeforeExit();
-  virtual bool __fastcall Interactive();
-  virtual bool __fastcall CommandLineOnly();
-  virtual bool __fastcall WantsProgress();
   virtual void __fastcall Progress(TScriptProgress & Progress);
   virtual void __fastcall Progress(TScriptProgress & Progress);
   virtual UnicodeString __fastcall FinalLogMessage();
   virtual UnicodeString __fastcall FinalLogMessage();
 
 
@@ -850,24 +830,32 @@ void __fastcall TExternalConsole::Init()
   }
   }
 }
 }
 //---------------------------------------------------------------------------
 //---------------------------------------------------------------------------
-bool __fastcall TExternalConsole::LimitedOutput()
-{
-  return FLimitedOutput;
-}
-//---------------------------------------------------------------------------
-bool __fastcall TExternalConsole::LiveOutput()
-{
-  return FLiveOutput;
-}
-//---------------------------------------------------------------------------
-bool __fastcall TExternalConsole::NoInteractiveInput()
+bool __fastcall TExternalConsole::HasFlag(TConsoleFlag Flag) const
 {
 {
-  return FNoInteractiveInput;
-}
-//---------------------------------------------------------------------------
-bool __fastcall TExternalConsole::Interactive()
-{
-  return FInteractive;
+  switch (Flag)
+  {
+    case cfLimitedOutput:
+      return FLimitedOutput;
+
+    case cfLiveOutput:
+      return FLiveOutput;
+
+    case cfNoInteractiveInput:
+      return FNoInteractiveInput;
+
+    case cfInteractive:
+      return FInteractive;
+
+    case cfCommandLineOnly:
+      return true;
+
+    case cfWantsProgress:
+      return FWantsProgress;
+
+    default:
+      DebugFail();
+      return false;
+  }
 }
 }
 //---------------------------------------------------------------------------
 //---------------------------------------------------------------------------
 void __fastcall TExternalConsole::WaitBeforeExit()
 void __fastcall TExternalConsole::WaitBeforeExit()
@@ -875,16 +863,6 @@ void __fastcall TExternalConsole::WaitBeforeExit()
   // noop
   // noop
 }
 }
 //---------------------------------------------------------------------------
 //---------------------------------------------------------------------------
-bool __fastcall TExternalConsole::CommandLineOnly()
-{
-  return true;
-}
-//---------------------------------------------------------------------------
-bool __fastcall TExternalConsole::WantsProgress()
-{
-  return FWantsProgress;
-}
-//---------------------------------------------------------------------------
 void __fastcall TExternalConsole::Progress(TScriptProgress & Progress)
 void __fastcall TExternalConsole::Progress(TScriptProgress & Progress)
 {
 {
   TConsoleCommStruct * CommStruct = GetCommStruct();
   TConsoleCommStruct * CommStruct = GetCommStruct();
@@ -961,16 +939,11 @@ public:
   virtual int __fastcall Choice(
   virtual int __fastcall Choice(
     UnicodeString Options, int Cancel, int Break, int Continue, int Timeouted, bool Timeouting, unsigned int Timer,
     UnicodeString Options, int Cancel, int Break, int Continue, int Timeouted, bool Timeouting, unsigned int Timer,
     UnicodeString Message);
     UnicodeString Message);
+  virtual bool __fastcall HasFlag(TConsoleFlag Flag) const;
   virtual bool __fastcall PendingAbort();
   virtual bool __fastcall PendingAbort();
   virtual void __fastcall SetTitle(UnicodeString Title);
   virtual void __fastcall SetTitle(UnicodeString Title);
-  virtual bool __fastcall LimitedOutput();
-  virtual bool __fastcall LiveOutput();
-  virtual bool __fastcall NoInteractiveInput();
-  virtual bool __fastcall Interactive();
   virtual void __fastcall WaitBeforeExit();
   virtual void __fastcall WaitBeforeExit();
-  virtual bool __fastcall CommandLineOnly();
 
 
-  virtual bool __fastcall WantsProgress();
   virtual void __fastcall Progress(TScriptProgress & Progress);
   virtual void __fastcall Progress(TScriptProgress & Progress);
   virtual UnicodeString __fastcall FinalLogMessage();
   virtual UnicodeString __fastcall FinalLogMessage();
 };
 };
@@ -1017,26 +990,27 @@ void __fastcall TNullConsole::SetTitle(UnicodeString /*Title*/)
   // noop
   // noop
 }
 }
 //---------------------------------------------------------------------------
 //---------------------------------------------------------------------------
-bool __fastcall TNullConsole::LimitedOutput()
+bool __fastcall TNullConsole::HasFlag(TConsoleFlag Flag) const
 {
 {
-  return false;
-}
-//---------------------------------------------------------------------------
-bool __fastcall TNullConsole::LiveOutput()
-{
-  return false;
-}
-//---------------------------------------------------------------------------
-bool __fastcall TNullConsole::NoInteractiveInput()
-{
-  // do not matter, even if we return false,
-  // it fails immediately afterwards in TNullConsole::Input
-  return true;
-}
-//---------------------------------------------------------------------------
-bool __fastcall TNullConsole::Interactive()
-{
-  return false;
+  switch (Flag)
+  {
+    // do not matter, even if we return false,
+    // it fails immediately afterwards in TNullConsole::Input
+    case cfNoInteractiveInput:
+      return true;
+
+    case cfLimitedOutput:
+    case cfLiveOutput:
+    case cfInteractive:
+    case cfCommandLineOnly:
+    case cfWantsProgress:
+      return false;
+
+    default:
+      DebugFail();
+      return false;
+  }
+
 }
 }
 //---------------------------------------------------------------------------
 //---------------------------------------------------------------------------
 void __fastcall TNullConsole::WaitBeforeExit()
 void __fastcall TNullConsole::WaitBeforeExit()
@@ -1045,17 +1019,6 @@ void __fastcall TNullConsole::WaitBeforeExit()
   // noop
   // noop
 }
 }
 //---------------------------------------------------------------------------
 //---------------------------------------------------------------------------
-bool __fastcall TNullConsole::CommandLineOnly()
-{
-  DebugFail();
-  return false;
-}
-//---------------------------------------------------------------------------
-bool __fastcall TNullConsole::WantsProgress()
-{
-  return false;
-}
-//---------------------------------------------------------------------------
 void __fastcall TNullConsole::Progress(TScriptProgress & /*Progress*/)
 void __fastcall TNullConsole::Progress(TScriptProgress & /*Progress*/)
 {
 {
   DebugFail();
   DebugFail();
@@ -1540,7 +1503,7 @@ void __fastcall TConsoleRunner::ScriptTerminalQueryUser(TObject * /*Sender*/,
     {
     {
       Retry = false;
       Retry = false;
 
 
-      if (FirstOutput || FConsole->LiveOutput())
+      if (FirstOutput || FConsole->HasFlag(cfLiveOutput))
       {
       {
         UnicodeString Output;
         UnicodeString Output;
         for (unsigned int i = 0; i < Buttons.size(); i++)
         for (unsigned int i = 0; i < Buttons.size(); i++)
@@ -1598,7 +1561,7 @@ void __fastcall TConsoleRunner::ScriptTerminalQueryUser(TObject * /*Sender*/,
         {
         {
           if (Timer == 0)
           if (Timer == 0)
           {
           {
-            if (FConsole->NoInteractiveInput())
+            if (FConsole->HasFlag(cfNoInteractiveInput))
             {
             {
               ActualTimer = Timeout;
               ActualTimer = Timeout;
             }
             }
@@ -1624,7 +1587,7 @@ void __fastcall TConsoleRunner::ScriptTerminalQueryUser(TObject * /*Sender*/,
           ActualTimer = Timer;
           ActualTimer = Timer;
         }
         }
         // Not to get preliminary "host is not responding" messages to .NET assembly
         // Not to get preliminary "host is not responding" messages to .NET assembly
-        if (FConsole->NoInteractiveInput() && (Timer > 0))
+        if (FConsole->HasFlag(cfNoInteractiveInput) && (Timer > 0))
         {
         {
           Sleep(Timer);
           Sleep(Timer);
           AnswerIndex = -2;
           AnswerIndex = -2;
@@ -1885,7 +1848,7 @@ bool __fastcall TConsoleRunner::DoInput(UnicodeString & Str, bool Echo,
   unsigned int Timeout, bool Interactive)
   unsigned int Timeout, bool Interactive)
 {
 {
   bool Result;
   bool Result;
-  if (Interactive && FConsole->NoInteractiveInput())
+  if (Interactive && FConsole->HasFlag(cfNoInteractiveInput))
   {
   {
     Result = false;
     Result = false;
   }
   }
@@ -2002,11 +1965,11 @@ int __fastcall TConsoleRunner::Run(const UnicodeString Session, TOptions * Optio
 
 
     try
     try
     {
     {
-      FScript = new TManagementScript(StoredSessions, FConsole->LimitedOutput());
+      FScript = new TManagementScript(StoredSessions, FConsole->HasFlag(cfLimitedOutput));
 
 
       FScript->CopyParam = GUIConfiguration->DefaultCopyParam;
       FScript->CopyParam = GUIConfiguration->DefaultCopyParam;
       FScript->SynchronizeParams = GUIConfiguration->SynchronizeParams;
       FScript->SynchronizeParams = GUIConfiguration->SynchronizeParams;
-      FScript->WantsProgress = FConsole->WantsProgress();
+      FScript->WantsProgress = FConsole->HasFlag(cfWantsProgress);
       FScript->OnPrint = ScriptPrint;
       FScript->OnPrint = ScriptPrint;
       FScript->OnPrintProgress = ScriptPrintProgress;
       FScript->OnPrintProgress = ScriptPrintProgress;
       FScript->OnInput = ScriptInput;
       FScript->OnInput = ScriptInput;
@@ -2016,7 +1979,7 @@ int __fastcall TConsoleRunner::Run(const UnicodeString Session, TOptions * Optio
       FScript->OnQueryCancel = ScriptQueryCancel;
       FScript->OnQueryCancel = ScriptQueryCancel;
       FScript->OnSynchronizeStartStop = ScriptSynchronizeStartStop;
       FScript->OnSynchronizeStartStop = ScriptSynchronizeStartStop;
       FScript->OnProgress = ScriptProgress;
       FScript->OnProgress = ScriptProgress;
-      FScript->Interactive = (ScriptCommands == NULL) && FConsole->Interactive();
+      FScript->Interactive = (ScriptCommands == NULL) && FConsole->HasFlag(cfInteractive);
 
 
       UpdateTitle();
       UpdateTitle();
 
 
@@ -2183,7 +2146,8 @@ void __fastcall Usage(TConsole * Console)
   Console->PrintLine();
   Console->PrintLine();
   Console->PrintLine(LoadStr(USAGE_SYNTAX_LABEL));
   Console->PrintLine(LoadStr(USAGE_SYNTAX_LABEL));
 
 
-  if (!Console->CommandLineOnly())
+  bool CommandLineOnly = Console->HasFlag(cfCommandLineOnly);
+  if (!CommandLineOnly)
   {
   {
     PrintUsageSyntax(Console, L"site|workspace|folder");
     PrintUsageSyntax(Console, L"site|workspace|folder");
     PrintUsageSyntax(Console, L"(sftp|scp|ftp[es]|dav[s]|s3)://[user[:password]@]host[:port][/path/[file]]");
     PrintUsageSyntax(Console, L"(sftp|scp|ftp[es]|dav[s]|s3)://[user[:password]@]host[:port][/path/[file]]");
@@ -2204,11 +2168,11 @@ void __fastcall Usage(TConsole * Console)
     PrintUsageSyntax(Console, L"[mysession] [/rawsettings setting1=value1 setting2=value2 ...]");
     PrintUsageSyntax(Console, L"[mysession] [/rawsettings setting1=value1 setting2=value2 ...]");
   }
   }
   PrintUsageSyntax(Console,
   PrintUsageSyntax(Console,
-    UnicodeString(!Console->CommandLineOnly() ? L"[/console] " : L"") +
+    UnicodeString(!CommandLineOnly ? L"[/console] " : L"") +
     FORMAT(L"[/script=<file>] [/%s cmd1...] [/parameter // param1...]", (LowerCase(COMMAND_SWITCH))));
     FORMAT(L"[/script=<file>] [/%s cmd1...] [/parameter // param1...]", (LowerCase(COMMAND_SWITCH))));
   PrintUsageSyntax(Console,
   PrintUsageSyntax(Console,
     FORMAT(L"[/%s=<logfile> [/loglevel=<level>]] [/%s=[<count>%s]<size>]", (LowerCase(LOG_SWITCH), LowerCase(LOGSIZE_SWITCH), LOGSIZE_SEPARATOR)));
     FORMAT(L"[/%s=<logfile> [/loglevel=<level>]] [/%s=[<count>%s]<size>]", (LowerCase(LOG_SWITCH), LowerCase(LOGSIZE_SWITCH), LOGSIZE_SEPARATOR)));
-  if (!Console->CommandLineOnly())
+  if (!CommandLineOnly)
   {
   {
     PrintUsageSyntax(Console, L"[/xmllog=<logfile> [/xmlgroups]]");
     PrintUsageSyntax(Console, L"[/xmllog=<logfile> [/xmlgroups]]");
   }
   }
@@ -2223,7 +2187,7 @@ void __fastcall Usage(TConsole * Console)
   PrintUsageSyntax(Console, L"/batchsettings <site_mask> setting1=value1 setting2=value2 ...");
   PrintUsageSyntax(Console, L"/batchsettings <site_mask> setting1=value1 setting2=value2 ...");
   PrintUsageSyntax(Console, FORMAT(L"/%s keyfile [/%s=<file>] [/%s] [/%s=<text>]",
   PrintUsageSyntax(Console, FORMAT(L"/%s keyfile [/%s=<file>] [/%s] [/%s=<text>]",
     (LowerCase(KEYGEN_SWITCH), LowerCase(KEYGEN_OUTPUT_SWITCH), LowerCase(KEYGEN_CHANGE_PASSPHRASE_SWITCH), LowerCase(KEYGEN_COMMENT_SWITCH))));
     (LowerCase(KEYGEN_SWITCH), LowerCase(KEYGEN_OUTPUT_SWITCH), LowerCase(KEYGEN_CHANGE_PASSPHRASE_SWITCH), LowerCase(KEYGEN_COMMENT_SWITCH))));
-  if (!Console->CommandLineOnly())
+  if (!CommandLineOnly)
   {
   {
     PrintUsageSyntax(Console, L"/update");
     PrintUsageSyntax(Console, L"/update");
   }
   }
@@ -2233,7 +2197,7 @@ void __fastcall Usage(TConsole * Console)
   Console->PrintLine();
   Console->PrintLine();
 
 
   TSwitchesUsage SwitchesUsage;
   TSwitchesUsage SwitchesUsage;
-  if (!Console->CommandLineOnly())
+  if (!CommandLineOnly)
   {
   {
     RegisterSwitch(SwitchesUsage, L"session", USAGE_SESSION);
     RegisterSwitch(SwitchesUsage, L"session", USAGE_SESSION);
     RegisterSwitch(SwitchesUsage, TProgramParams::FormatSwitch(SESSIONNAME_SWICH) + L"=", USAGE_SESSIONNAME);
     RegisterSwitch(SwitchesUsage, TProgramParams::FormatSwitch(SESSIONNAME_SWICH) + L"=", USAGE_SESSIONNAME);
@@ -2266,7 +2230,7 @@ void __fastcall Usage(TConsole * Console)
   RegisterSwitch(SwitchesUsage, TProgramParams::FormatSwitch(LOGSIZE_SWITCH) + L"=", USAGE_LOGSIZE);
   RegisterSwitch(SwitchesUsage, TProgramParams::FormatSwitch(LOGSIZE_SWITCH) + L"=", USAGE_LOGSIZE);
   RegisterSwitch(SwitchesUsage, L"/xmllog=", USAGE_XMLLOG);
   RegisterSwitch(SwitchesUsage, L"/xmllog=", USAGE_XMLLOG);
   RegisterSwitch(SwitchesUsage, L"/xmlgroups", USAGE_XMLGROUPS);
   RegisterSwitch(SwitchesUsage, L"/xmlgroups", USAGE_XMLGROUPS);
-  if (Console->CommandLineOnly())
+  if (CommandLineOnly)
   {
   {
     RegisterSwitch(SwitchesUsage, TProgramParams::FormatSwitch(NOINTERACTIVEINPUT_SWITCH), USAGE_INTERACTIVEINPUT);
     RegisterSwitch(SwitchesUsage, TProgramParams::FormatSwitch(NOINTERACTIVEINPUT_SWITCH), USAGE_INTERACTIVEINPUT);
   }
   }
@@ -2280,7 +2244,7 @@ void __fastcall Usage(TConsole * Console)
       TProgramParams::FormatSwitch(LowerCase(KEYGEN_CHANGE_PASSPHRASE_SWITCH)),
       TProgramParams::FormatSwitch(LowerCase(KEYGEN_CHANGE_PASSPHRASE_SWITCH)),
       TProgramParams::FormatSwitch(LowerCase(KEYGEN_COMMENT_SWITCH)) + L"="));
       TProgramParams::FormatSwitch(LowerCase(KEYGEN_COMMENT_SWITCH)) + L"="));
   RegisterSwitch(SwitchesUsage, TProgramParams::FormatSwitch(KEYGEN_SWITCH), KeyGenDesc);
   RegisterSwitch(SwitchesUsage, TProgramParams::FormatSwitch(KEYGEN_SWITCH), KeyGenDesc);
-  if (!Console->CommandLineOnly())
+  if (!CommandLineOnly)
   {
   {
     RegisterSwitch(SwitchesUsage, L"/update", USAGE_UPDATE);
     RegisterSwitch(SwitchesUsage, L"/update", USAGE_UPDATE);
   }
   }

+ 11 - 6
source/windows/WinInterface.h

@@ -614,6 +614,16 @@ private:
   void __fastcall BalloonCancelled();
   void __fastcall BalloonCancelled();
 };
 };
 //---------------------------------------------------------------------------
 //---------------------------------------------------------------------------
+enum TConsoleFlag
+{
+  cfLimitedOutput,
+  cfLiveOutput,
+  cfNoInteractiveInput,
+  cfInteractive,
+  cfCommandLineOnly,
+  cfWantsProgress
+};
+//---------------------------------------------------------------------------
 class TConsole
 class TConsole
 {
 {
 public:
 public:
@@ -624,15 +634,10 @@ public:
   virtual int __fastcall Choice(
   virtual int __fastcall Choice(
     UnicodeString Options, int Cancel, int Break, int Continue, int Timeouted, bool Timeouting, unsigned int Timer,
     UnicodeString Options, int Cancel, int Break, int Continue, int Timeouted, bool Timeouting, unsigned int Timer,
     UnicodeString Message) = 0;
     UnicodeString Message) = 0;
+  virtual bool __fastcall HasFlag(TConsoleFlag Flag) const = 0;
   virtual bool __fastcall PendingAbort() = 0;
   virtual bool __fastcall PendingAbort() = 0;
   virtual void __fastcall SetTitle(UnicodeString Title) = 0;
   virtual void __fastcall SetTitle(UnicodeString Title) = 0;
-  virtual bool __fastcall LimitedOutput() = 0;
-  virtual bool __fastcall LiveOutput() = 0;
-  virtual bool __fastcall NoInteractiveInput() = 0;
-  virtual bool __fastcall Interactive() = 0;
   virtual void __fastcall WaitBeforeExit() = 0;
   virtual void __fastcall WaitBeforeExit() = 0;
-  virtual bool __fastcall CommandLineOnly() = 0;
-  virtual bool __fastcall WantsProgress() = 0;
   virtual void __fastcall Progress(TScriptProgress & Progress) = 0;
   virtual void __fastcall Progress(TScriptProgress & Progress) = 0;
   virtual UnicodeString __fastcall FinalLogMessage() = 0;
   virtual UnicodeString __fastcall FinalLogMessage() = 0;
 };
 };