| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- //---------------------------------------------------------------------
- #include <vcl.h>
- #pragma hdrstop
- #include "Console.h"
- #include <Common.h>
- #include <TextsWin.h>
- #include <Interface.h>
- #include <ScpFileSystem.h>
- #include <ScpMain.h>
- #include <VCLCommon.h>
- #include <CustomWinConfiguration.h>
- //---------------------------------------------------------------------
- #pragma link "HistoryComboBox"
- #pragma link "PathLabel"
- #pragma resource "*.dfm"
- //---------------------------------------------------------------------
- void __fastcall DoConsoleDialog(TTerminal * Terminal, const AnsiString Command)
- {
- TConsoleDialog * Dialog = new TConsoleDialog(Application);
- try
- {
- Dialog->Terminal = Terminal;
- Dialog->Execute(Command);
- }
- __finally
- {
- delete Dialog;
- }
- }
- //---------------------------------------------------------------------
- __fastcall TConsoleDialog::TConsoleDialog(TComponent* AOwner)
- : TForm(AOwner)
- {
- FTerminal = NULL;
- FOldLogAddLine = NULL;
- FOldChangeDirectory = NULL;
- FAddOutput = false;
- OutputMemo->Color = clBlack;
- OutputMemo->Font->Color = (TColor)0x00BBBBBB; //clGray;
- UseSystemSettings(this);
- try
- {
- OutputMemo->Font->Name = "Courier New";
- }
- catch(...)
- {
- }
- }
- //---------------------------------------------------------------------
- __fastcall TConsoleDialog::~TConsoleDialog()
- {
- Terminal = NULL;
- }
- //---------------------------------------------------------------------
- void __fastcall TConsoleDialog::SetTerminal(TTerminal * value)
- {
- if (FTerminal != value)
- {
- if (FTerminal)
- {
- assert(FTerminal->OnChangeDirectory == DoChangeDirectory);
- FTerminal->OnChangeDirectory = FOldChangeDirectory;
- assert(FTerminal->Log->OnAddLine == DoLogAddLine);
- FTerminal->Log->OnAddLine = FOldLogAddLine;
- FOldChangeDirectory = NULL;
- FOldLogAddLine = NULL;
- FTerminal->EndTransaction();
- }
- FTerminal = value;
- if (FTerminal)
- {
- OutputMemo->Clear();
- FOldChangeDirectory = FTerminal->OnChangeDirectory;
- FTerminal->OnChangeDirectory = DoChangeDirectory;
- FOldLogAddLine = FTerminal->Log->OnAddLine;
- FTerminal->Log->OnAddLine = DoLogAddLine;
- // avoid reloading directory after each change of current directory from console
- FTerminal->BeginTransaction();
- }
- UpdateControls();
- }
- }
- //---------------------------------------------------------------------
- void __fastcall TConsoleDialog::DoChangeDirectory(TObject * Sender)
- {
- if (FOldChangeDirectory) FOldChangeDirectory(Sender);
- UpdateControls();
- }
- //---------------------------------------------------------------------
- void __fastcall TConsoleDialog::UpdateControls()
- {
- DirectoryLabel->Caption = (FTerminal ? FTerminal->CurrentDirectory : AnsiString());
- EnableControl(ExecuteButton, !CommandEdit->Text.IsEmpty());
- }
- //---------------------------------------------------------------------
- bool __fastcall TConsoleDialog::Execute(const AnsiString Command)
- {
- FPrevTerminalClose = NULL;;
- if (FTerminal)
- {
- FPrevTerminalClose = FTerminal->OnClose;
- // used instead of previous TTerminalManager::OnChangeTerminal
- FTerminal->OnClose = TerminalClose;
- }
-
- try
- {
- TStrings * CommandsHistory = CustomWinConfiguration->History["Commands"];
- if ((CommandsHistory != NULL) && (CommandsHistory->Count > 0))
- {
- CommandEdit->Items = CommandsHistory;
- }
- else
- {
- CommandEdit->Items->Clear();
- }
- if (!Command.IsEmpty())
- {
- CommandEdit->Text = Command;
- DoExecuteCommand();
- }
- ShowModal();
- }
- __finally
- {
- if (FTerminal)
- {
- assert(FTerminal->OnClose == TerminalClose);
- FTerminal->OnClose = FPrevTerminalClose;
- CustomWinConfiguration->History["Commands"] = CommandEdit->Items;
- }
- }
- return true;
- }
- //---------------------------------------------------------------------------
- void __fastcall TConsoleDialog::TerminalClose(TObject * Sender)
- {
- Close();
- Terminal = NULL;
- if (FPrevTerminalClose)
- {
- FPrevTerminalClose(Sender);
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TConsoleDialog::ExecuteButtonClick(TObject * /*Sender*/)
- {
- ExecuteCommand();
- }
- //---------------------------------------------------------------------------
- void __fastcall TConsoleDialog::DoExecuteCommand()
- {
- CommandEdit->SelectAll();
- FTerminal->ExceptionOnFail = true;
- try
- {
- AnsiString Command = CommandEdit->Text;
- OutputMemo->Lines->Add(FORMAT("$ %s", ((Command))));
- FAddOutput = true;
- FTerminal->AnyCommand(Command);
- }
- __finally
- {
- FAddOutput = false;
- if (FTerminal)
- {
- FTerminal->ExceptionOnFail = false;
- if (FTerminal->Active)
- {
- FTerminal->ReadCurrentDirectory();
- }
- }
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TConsoleDialog::ExecuteCommand()
- {
- try
- {
- DoExecuteCommand();
- }
- catch(Exception & E)
- {
- ShowExtendedException(&E, this);
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TConsoleDialog::CommandEditChange(TObject * /*Sender*/)
- {
- UpdateControls();
- }
- //---------------------------------------------------------------------------
- void __fastcall TConsoleDialog::DoLogAddLine(TObject* /*Sender*/,
- const AnsiString AddedLine)
- {
- if (FAddOutput &&
- !AddedLine.IsEmpty() && (AddedLine[1] == '<' || AddedLine[1] == '!'))
- {
- int ReturnCode;
- AnsiString Line = AddedLine;
- Line.Delete(1, 2);
- if (!TSCPFileSystem::RemoveLastLine(Line, ReturnCode) ||
- !Line.IsEmpty())
- {
- OutputMemo->Lines->Add(Line);
- }
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TConsoleDialog::CreateParams(TCreateParams & Params)
- {
- TForm::CreateParams(Params);
- Params.Style = Params.Style & ~WS_SYSMENU;
- }
- //---------------------------------------------------------------------------
|