Console.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. //---------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include "Console.h"
  5. #include <Common.h>
  6. #include <TextsWin.h>
  7. #include <Interface.h>
  8. #include <ScpFileSystem.h>
  9. #include <ScpMain.h>
  10. #include <VCLCommon.h>
  11. #include <CustomWinConfiguration.h>
  12. //---------------------------------------------------------------------
  13. #pragma link "HistoryComboBox"
  14. #pragma link "PathLabel"
  15. #pragma resource "*.dfm"
  16. //---------------------------------------------------------------------
  17. void __fastcall DoConsoleDialog(TTerminal * Terminal, const AnsiString Command)
  18. {
  19. TConsoleDialog * Dialog = new TConsoleDialog(Application);
  20. try
  21. {
  22. Dialog->Terminal = Terminal;
  23. Dialog->Execute(Command);
  24. }
  25. __finally
  26. {
  27. delete Dialog;
  28. }
  29. }
  30. //---------------------------------------------------------------------
  31. __fastcall TConsoleDialog::TConsoleDialog(TComponent* AOwner)
  32. : TForm(AOwner)
  33. {
  34. FTerminal = NULL;
  35. FOldLogAddLine = NULL;
  36. FOldChangeDirectory = NULL;
  37. FAddOutput = false;
  38. OutputMemo->Color = clBlack;
  39. OutputMemo->Font->Color = (TColor)0x00BBBBBB; //clGray;
  40. UseSystemSettings(this);
  41. try
  42. {
  43. OutputMemo->Font->Name = "Courier New";
  44. }
  45. catch(...)
  46. {
  47. }
  48. }
  49. //---------------------------------------------------------------------
  50. __fastcall TConsoleDialog::~TConsoleDialog()
  51. {
  52. Terminal = NULL;
  53. }
  54. //---------------------------------------------------------------------
  55. void __fastcall TConsoleDialog::SetTerminal(TTerminal * value)
  56. {
  57. if (FTerminal != value)
  58. {
  59. if (FTerminal)
  60. {
  61. assert(FTerminal->OnChangeDirectory == DoChangeDirectory);
  62. FTerminal->OnChangeDirectory = FOldChangeDirectory;
  63. assert(FTerminal->Log->OnAddLine == DoLogAddLine);
  64. FTerminal->Log->OnAddLine = FOldLogAddLine;
  65. FOldChangeDirectory = NULL;
  66. FOldLogAddLine = NULL;
  67. FTerminal->EndTransaction();
  68. }
  69. FTerminal = value;
  70. if (FTerminal)
  71. {
  72. OutputMemo->Clear();
  73. FOldChangeDirectory = FTerminal->OnChangeDirectory;
  74. FTerminal->OnChangeDirectory = DoChangeDirectory;
  75. FOldLogAddLine = FTerminal->Log->OnAddLine;
  76. FTerminal->Log->OnAddLine = DoLogAddLine;
  77. // avoid reloading directory after each change of current directory from console
  78. FTerminal->BeginTransaction();
  79. }
  80. UpdateControls();
  81. }
  82. }
  83. //---------------------------------------------------------------------
  84. void __fastcall TConsoleDialog::DoChangeDirectory(TObject * Sender)
  85. {
  86. if (FOldChangeDirectory) FOldChangeDirectory(Sender);
  87. UpdateControls();
  88. }
  89. //---------------------------------------------------------------------
  90. void __fastcall TConsoleDialog::UpdateControls()
  91. {
  92. DirectoryLabel->Caption = (FTerminal ? FTerminal->CurrentDirectory : AnsiString());
  93. EnableControl(ExecuteButton, !CommandEdit->Text.IsEmpty());
  94. }
  95. //---------------------------------------------------------------------
  96. bool __fastcall TConsoleDialog::Execute(const AnsiString Command)
  97. {
  98. FPrevTerminalClose = NULL;;
  99. if (FTerminal)
  100. {
  101. FPrevTerminalClose = FTerminal->OnClose;
  102. // used instead of previous TTerminalManager::OnChangeTerminal
  103. FTerminal->OnClose = TerminalClose;
  104. }
  105. try
  106. {
  107. TStrings * CommandsHistory = CustomWinConfiguration->History["Commands"];
  108. if ((CommandsHistory != NULL) && (CommandsHistory->Count > 0))
  109. {
  110. CommandEdit->Items = CommandsHistory;
  111. }
  112. else
  113. {
  114. CommandEdit->Items->Clear();
  115. }
  116. if (!Command.IsEmpty())
  117. {
  118. CommandEdit->Text = Command;
  119. DoExecuteCommand();
  120. }
  121. ShowModal();
  122. }
  123. __finally
  124. {
  125. if (FTerminal)
  126. {
  127. assert(FTerminal->OnClose == TerminalClose);
  128. FTerminal->OnClose = FPrevTerminalClose;
  129. CustomWinConfiguration->History["Commands"] = CommandEdit->Items;
  130. }
  131. }
  132. return true;
  133. }
  134. //---------------------------------------------------------------------------
  135. void __fastcall TConsoleDialog::TerminalClose(TObject * Sender)
  136. {
  137. Close();
  138. Terminal = NULL;
  139. if (FPrevTerminalClose)
  140. {
  141. FPrevTerminalClose(Sender);
  142. }
  143. }
  144. //---------------------------------------------------------------------------
  145. void __fastcall TConsoleDialog::ExecuteButtonClick(TObject * /*Sender*/)
  146. {
  147. ExecuteCommand();
  148. }
  149. //---------------------------------------------------------------------------
  150. void __fastcall TConsoleDialog::DoExecuteCommand()
  151. {
  152. CommandEdit->SelectAll();
  153. FTerminal->ExceptionOnFail = true;
  154. try
  155. {
  156. AnsiString Command = CommandEdit->Text;
  157. OutputMemo->Lines->Add(FORMAT("$ %s", ((Command))));
  158. FAddOutput = true;
  159. FTerminal->AnyCommand(Command);
  160. }
  161. __finally
  162. {
  163. FAddOutput = false;
  164. if (FTerminal)
  165. {
  166. FTerminal->ExceptionOnFail = false;
  167. if (FTerminal->Active)
  168. {
  169. FTerminal->ReadCurrentDirectory();
  170. }
  171. }
  172. }
  173. }
  174. //---------------------------------------------------------------------------
  175. void __fastcall TConsoleDialog::ExecuteCommand()
  176. {
  177. try
  178. {
  179. DoExecuteCommand();
  180. }
  181. catch(Exception & E)
  182. {
  183. ShowExtendedException(&E, this);
  184. }
  185. }
  186. //---------------------------------------------------------------------------
  187. void __fastcall TConsoleDialog::CommandEditChange(TObject * /*Sender*/)
  188. {
  189. UpdateControls();
  190. }
  191. //---------------------------------------------------------------------------
  192. void __fastcall TConsoleDialog::DoLogAddLine(TObject* /*Sender*/,
  193. const AnsiString AddedLine)
  194. {
  195. if (FAddOutput &&
  196. !AddedLine.IsEmpty() && (AddedLine[1] == '<' || AddedLine[1] == '!'))
  197. {
  198. int ReturnCode;
  199. AnsiString Line = AddedLine;
  200. Line.Delete(1, 2);
  201. if (!TSCPFileSystem::RemoveLastLine(Line, ReturnCode) ||
  202. !Line.IsEmpty())
  203. {
  204. OutputMemo->Lines->Add(Line);
  205. }
  206. }
  207. }
  208. //---------------------------------------------------------------------------
  209. void __fastcall TConsoleDialog::CreateParams(TCreateParams & Params)
  210. {
  211. TForm::CreateParams(Params);
  212. Params.Style = Params.Style & ~WS_SYSMENU;
  213. }
  214. //---------------------------------------------------------------------------