ConsoleRunner.cpp 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include <Common.h>
  5. #include <Exceptions.h>
  6. #include <Script.h>
  7. #include <ScpMain.h>
  8. #include <Terminal.h>
  9. #include <Consts.hpp>
  10. #include "Console.h"
  11. #include "WinInterface.h"
  12. #include "ProgParams.h"
  13. #include "TextsWin.h"
  14. #include "TextsCore.h"
  15. #include "CustomWinConfiguration.h"
  16. #include "SynchronizeController.h"
  17. #include "GUITools.h"
  18. enum { RESULT_SUCCESS = 0, RESULT_ANY_ERROR = 1 };
  19. //---------------------------------------------------------------------------
  20. #define WM_INTERUPT_IDLE (WM_WINSCP_USER + 3)
  21. //---------------------------------------------------------------------------
  22. #pragma package(smart_init)
  23. //---------------------------------------------------------------------------
  24. class TConsole
  25. {
  26. public:
  27. virtual void __fastcall Print(AnsiString Str, bool FromBeginning = false) = 0;
  28. virtual bool __fastcall Input(AnsiString & Str, bool Echo) = 0;
  29. virtual int __fastcall Choice(AnsiString Options, int Cancel, int Break) = 0;
  30. virtual bool __fastcall PendingAbort() = 0;
  31. virtual void __fastcall SetTitle(AnsiString Title) = 0;
  32. virtual void __fastcall WaitBeforeExit() = 0;
  33. };
  34. //---------------------------------------------------------------------------
  35. class TOwnConsole : public TConsole
  36. {
  37. public:
  38. static TOwnConsole * __fastcall Instance();
  39. virtual void __fastcall Print(AnsiString Str, bool FromBeginning = false);
  40. virtual bool __fastcall Input(AnsiString & Str, bool Echo);
  41. virtual int __fastcall Choice(AnsiString Options, int Cancel, int Break);
  42. virtual bool __fastcall PendingAbort();
  43. virtual void __fastcall SetTitle(AnsiString Title);
  44. virtual void __fastcall WaitBeforeExit();
  45. protected:
  46. static TOwnConsole * FInstance;
  47. __fastcall TOwnConsole();
  48. __fastcall ~TOwnConsole();
  49. static BOOL WINAPI HandlerRoutine(DWORD CtrlType);
  50. private:
  51. HANDLE FInput;
  52. HANDLE FOutput;
  53. bool FPendingAbort;
  54. };
  55. //---------------------------------------------------------------------------
  56. TOwnConsole * TOwnConsole::FInstance = NULL;
  57. //---------------------------------------------------------------------------
  58. __fastcall TOwnConsole::TOwnConsole()
  59. {
  60. assert(FInstance == NULL);
  61. FInstance = this;
  62. AllocConsole();
  63. SetConsoleCtrlHandler(HandlerRoutine, true);
  64. FInput = GetStdHandle(STD_INPUT_HANDLE);
  65. FOutput = GetStdHandle(STD_OUTPUT_HANDLE);
  66. FPendingAbort = false;
  67. }
  68. //---------------------------------------------------------------------------
  69. __fastcall TOwnConsole::~TOwnConsole()
  70. {
  71. FreeConsole();
  72. assert(FInstance == this);
  73. FInstance = NULL;
  74. }
  75. //---------------------------------------------------------------------------
  76. TOwnConsole * __fastcall TOwnConsole::Instance()
  77. {
  78. return new TOwnConsole();
  79. }
  80. //---------------------------------------------------------------------------
  81. BOOL WINAPI TOwnConsole::HandlerRoutine(DWORD CtrlType)
  82. {
  83. if ((CtrlType == CTRL_C_EVENT) || (CtrlType == CTRL_BREAK_EVENT))
  84. {
  85. assert(FInstance != NULL);
  86. FlushConsoleInputBuffer(FInstance->FInput);
  87. INPUT_RECORD InputRecord;
  88. memset(&InputRecord, 0, sizeof(InputRecord));
  89. InputRecord.EventType = KEY_EVENT;
  90. InputRecord.Event.KeyEvent.bKeyDown = true;
  91. InputRecord.Event.KeyEvent.wRepeatCount = 1;
  92. InputRecord.Event.KeyEvent.wVirtualKeyCode = VK_RETURN;
  93. InputRecord.Event.KeyEvent.uChar.AsciiChar = '\n';
  94. unsigned long Written;
  95. // this assertion occasionally fails (when console is being exited)
  96. CHECK(WriteConsoleInput(FInstance->FInput, &InputRecord, 1, &Written));
  97. assert(Written == 1);
  98. FInstance->FPendingAbort = true;
  99. SendMessage(Application->Handle, WM_INTERUPT_IDLE, 0, 0);
  100. return true;
  101. }
  102. else
  103. {
  104. return false;
  105. }
  106. }
  107. //---------------------------------------------------------------------------
  108. bool __fastcall TOwnConsole::PendingAbort()
  109. {
  110. if (FPendingAbort)
  111. {
  112. FPendingAbort = false;
  113. return true;
  114. }
  115. else
  116. {
  117. return FPendingAbort;
  118. }
  119. }
  120. //---------------------------------------------------------------------------
  121. void __fastcall TOwnConsole::Print(AnsiString Str, bool FromBeginning)
  122. {
  123. if (FromBeginning)
  124. {
  125. CONSOLE_SCREEN_BUFFER_INFO BufferInfo;
  126. GetConsoleScreenBufferInfo(FOutput, &BufferInfo);
  127. BufferInfo.dwCursorPosition.X = 0;
  128. SetConsoleCursorPosition(FOutput, BufferInfo.dwCursorPosition);
  129. }
  130. unsigned long Written;
  131. AnsiToOem(Str);
  132. bool Result = WriteConsole(FOutput, Str.c_str(), Str.Length(), &Written, NULL);
  133. assert(Result);
  134. USEDPARAM(Result);
  135. assert(Str.Length() == static_cast<long>(Written));
  136. }
  137. //---------------------------------------------------------------------------
  138. bool __fastcall TOwnConsole::Input(AnsiString & Str, bool Echo)
  139. {
  140. unsigned long PrevMode, NewMode;
  141. GetConsoleMode(FInput, &PrevMode);
  142. NewMode = PrevMode | ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT;
  143. if (Echo)
  144. {
  145. NewMode |= ENABLE_ECHO_INPUT;
  146. }
  147. else
  148. {
  149. NewMode &= ~ENABLE_ECHO_INPUT;
  150. }
  151. SetConsoleMode(FInput, NewMode);
  152. bool Result;
  153. try
  154. {
  155. unsigned long Read;
  156. Str.SetLength(10240);
  157. Result = ReadConsole(FInput, Str.c_str(), Str.Length(), &Read, NULL);
  158. assert(Result);
  159. Str.SetLength(Read);
  160. OemToAnsi(Str);
  161. if (FPendingAbort || !Echo)
  162. {
  163. Print("\n");
  164. }
  165. if (FPendingAbort || (Read == 0))
  166. {
  167. Result = false;
  168. FPendingAbort = false;
  169. }
  170. }
  171. __finally
  172. {
  173. SetConsoleMode(FInput, PrevMode);
  174. }
  175. return Result;
  176. }
  177. //---------------------------------------------------------------------------
  178. int __fastcall TOwnConsole::Choice(AnsiString Options, int Cancel, int Break)
  179. {
  180. AnsiToOem(Options);
  181. int Result = 0;
  182. unsigned long PrevMode, NewMode;
  183. GetConsoleMode(FInput, &PrevMode);
  184. NewMode = (PrevMode | ENABLE_PROCESSED_INPUT) & ~(ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT);
  185. SetConsoleMode(FInput, NewMode);
  186. try
  187. {
  188. do
  189. {
  190. unsigned long Read;
  191. INPUT_RECORD Record;
  192. if ((ReadConsoleInput(FInput, &Record, 1, &Read) != 0) &&
  193. (Read == 1))
  194. {
  195. if (PendingAbort())
  196. {
  197. Result = Break;
  198. }
  199. else if ((Record.EventType == KEY_EVENT) &&
  200. Record.Event.KeyEvent.bKeyDown)
  201. {
  202. char C = AnsiUpperCase(Record.Event.KeyEvent.uChar.AsciiChar)[1];
  203. if (C == 27)
  204. {
  205. Result = Cancel;
  206. }
  207. else if ((Options.Pos(C) > 0) &&
  208. FLAGCLEAR(Record.Event.KeyEvent.dwControlKeyState,
  209. LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED | LEFT_ALT_PRESSED |
  210. RIGHT_CTRL_PRESSED))
  211. {
  212. Result = Options.Pos(C);
  213. }
  214. }
  215. }
  216. }
  217. while (Result == 0);
  218. }
  219. __finally
  220. {
  221. SetConsoleMode(FInput, PrevMode);
  222. }
  223. return Result;
  224. }
  225. //---------------------------------------------------------------------------
  226. void __fastcall TOwnConsole::SetTitle(AnsiString Title)
  227. {
  228. AnsiToOem(Title);
  229. SetConsoleTitle(Title.c_str());
  230. }
  231. //---------------------------------------------------------------------------
  232. void __fastcall TOwnConsole::WaitBeforeExit()
  233. {
  234. unsigned long Read;
  235. INPUT_RECORD Record;
  236. while (ReadConsoleInput(FInput, &Record, 1, &Read))
  237. {
  238. if ((Read == 1) && (Record.EventType == KEY_EVENT) &&
  239. (Record.Event.KeyEvent.uChar.AsciiChar != 0) &&
  240. Record.Event.KeyEvent.bKeyDown)
  241. {
  242. break;
  243. }
  244. }
  245. }
  246. //---------------------------------------------------------------------------
  247. class TExternalConsole : public TConsole
  248. {
  249. public:
  250. __fastcall TExternalConsole(const AnsiString Instance);
  251. __fastcall ~TExternalConsole();
  252. virtual void __fastcall Print(AnsiString Str, bool FromBeginning = false);
  253. virtual bool __fastcall Input(AnsiString & Str, bool Echo);
  254. virtual int __fastcall Choice(AnsiString Options, int Cancel, int Break);
  255. virtual bool __fastcall PendingAbort();
  256. virtual void __fastcall SetTitle(AnsiString Title);
  257. virtual void __fastcall WaitBeforeExit();
  258. private:
  259. bool FPendingAbort;
  260. HANDLE FRequestEvent;
  261. HANDLE FResponseEvent;
  262. HANDLE FCancelEvent;
  263. HANDLE FFileMapping;
  264. static const int PrintTimeout = 5000;
  265. inline TConsoleCommStruct * __fastcall GetCommStruct();
  266. inline void __fastcall FreeCommStruct(TConsoleCommStruct * CommStruct);
  267. inline void __fastcall SendEvent(int Timeout);
  268. };
  269. //---------------------------------------------------------------------------
  270. __fastcall TExternalConsole::TExternalConsole(const AnsiString Instance)
  271. {
  272. FRequestEvent = OpenEvent(EVENT_ALL_ACCESS, false,
  273. FORMAT("%s%s", (CONSOLE_EVENT_REQUEST, (Instance))).c_str());
  274. FResponseEvent = OpenEvent(EVENT_ALL_ACCESS, false,
  275. FORMAT("%s%s", (CONSOLE_EVENT_RESPONSE, (Instance))).c_str());
  276. FCancelEvent = OpenEvent(EVENT_ALL_ACCESS, false,
  277. FORMAT("%s%s", (CONSOLE_EVENT_CANCEL, (Instance))).c_str());
  278. FFileMapping = OpenFileMapping(FILE_MAP_ALL_ACCESS, false,
  279. FORMAT("%s%s", (CONSOLE_MAPPING, (Instance))).c_str());
  280. if ((FRequestEvent == NULL) || (FResponseEvent == NULL) || (FFileMapping == NULL))
  281. {
  282. throw Exception(LoadStr(EXTERNAL_CONSOLE_INIT_ERROR));
  283. }
  284. TConsoleCommStruct * CommStruct = GetCommStruct();
  285. try
  286. {
  287. if (CommStruct->Version < TConsoleCommStruct::MinVersion)
  288. {
  289. throw Exception(FMTLOAD(EXTERNAL_CONSOLE_INCOMPATIBLE, (CommStruct->Version)));
  290. }
  291. }
  292. __finally
  293. {
  294. FreeCommStruct(CommStruct);
  295. }
  296. // to break application event loop regularly during "watching for changes"
  297. // to allow user to abort it
  298. SetTimer(Application->Handle, 1, 500, NULL);
  299. }
  300. //---------------------------------------------------------------------------
  301. __fastcall TExternalConsole::~TExternalConsole()
  302. {
  303. CloseHandle(FRequestEvent);
  304. CloseHandle(FResponseEvent);
  305. CloseHandle(FCancelEvent);
  306. CloseHandle(FFileMapping);
  307. KillTimer(Application->Handle, 1);
  308. }
  309. //---------------------------------------------------------------------------
  310. TConsoleCommStruct * __fastcall TExternalConsole::GetCommStruct()
  311. {
  312. TConsoleCommStruct * Result;
  313. Result = static_cast<TConsoleCommStruct*>(MapViewOfFile(FFileMapping,
  314. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  315. if (Result == NULL)
  316. {
  317. throw Exception(LoadStr(CONSOLE_COMM_ERROR));
  318. }
  319. return Result;
  320. }
  321. //---------------------------------------------------------------------------
  322. void __fastcall TExternalConsole::FreeCommStruct(TConsoleCommStruct * CommStruct)
  323. {
  324. UnmapViewOfFile(CommStruct);
  325. }
  326. //---------------------------------------------------------------------------
  327. void __fastcall TExternalConsole::SendEvent(int Timeout)
  328. {
  329. SetEvent(FRequestEvent);
  330. if (WaitForSingleObject(FResponseEvent, Timeout) != WAIT_OBJECT_0)
  331. {
  332. throw Exception(LoadStr(CONSOLE_SEND_TIMEOUT));
  333. }
  334. }
  335. //---------------------------------------------------------------------------
  336. void __fastcall TExternalConsole::Print(AnsiString Str, bool FromBeginning)
  337. {
  338. TConsoleCommStruct * CommStruct = GetCommStruct();
  339. try
  340. {
  341. if (Str.Length() >= sizeof(CommStruct->PrintEvent.Message))
  342. {
  343. throw Exception(FMTLOAD(CONSOLE_PRINT_TOO_LONG, (Str.Length())));
  344. }
  345. CommStruct->Event = TConsoleCommStruct::PRINT;
  346. CharToOem(Str.c_str(), CommStruct->PrintEvent.Message);
  347. CommStruct->PrintEvent.FromBeginning = FromBeginning;
  348. }
  349. __finally
  350. {
  351. FreeCommStruct(CommStruct);
  352. }
  353. SendEvent(PrintTimeout);
  354. }
  355. //---------------------------------------------------------------------------
  356. bool __fastcall TExternalConsole::Input(AnsiString & Str, bool Echo)
  357. {
  358. TConsoleCommStruct * CommStruct = GetCommStruct();
  359. try
  360. {
  361. CommStruct->Event = TConsoleCommStruct::INPUT;
  362. CommStruct->InputEvent.Echo = Echo;
  363. CommStruct->InputEvent.Result = false;
  364. CommStruct->InputEvent.Str[0] = '\0';
  365. }
  366. __finally
  367. {
  368. FreeCommStruct(CommStruct);
  369. }
  370. SendEvent(INFINITE);
  371. bool Result;
  372. CommStruct = GetCommStruct();
  373. try
  374. {
  375. Result = CommStruct->InputEvent.Result;
  376. Str.SetLength(strlen(CommStruct->InputEvent.Str));
  377. OemToChar(CommStruct->InputEvent.Str, Str.c_str());
  378. }
  379. __finally
  380. {
  381. FreeCommStruct(CommStruct);
  382. }
  383. return Result;
  384. }
  385. //---------------------------------------------------------------------------
  386. int __fastcall TExternalConsole::Choice(AnsiString Options, int Cancel, int Break)
  387. {
  388. TConsoleCommStruct * CommStruct = GetCommStruct();
  389. try
  390. {
  391. CommStruct->Event = TConsoleCommStruct::CHOICE;
  392. assert(Options.Length() < sizeof(CommStruct->ChoiceEvent.Options));
  393. CharToOem(Options.c_str(), CommStruct->ChoiceEvent.Options);
  394. CommStruct->ChoiceEvent.Cancel = Cancel;
  395. CommStruct->ChoiceEvent.Break = Break;
  396. CommStruct->ChoiceEvent.Result = Break;
  397. }
  398. __finally
  399. {
  400. FreeCommStruct(CommStruct);
  401. }
  402. SendEvent(INFINITE);
  403. int Result;
  404. CommStruct = GetCommStruct();
  405. try
  406. {
  407. Result = CommStruct->ChoiceEvent.Result;
  408. }
  409. __finally
  410. {
  411. FreeCommStruct(CommStruct);
  412. }
  413. return Result;
  414. }
  415. //---------------------------------------------------------------------------
  416. bool __fastcall TExternalConsole::PendingAbort()
  417. {
  418. return (WaitForSingleObject(FCancelEvent, 0) == WAIT_OBJECT_0);
  419. }
  420. //---------------------------------------------------------------------------
  421. void __fastcall TExternalConsole::SetTitle(AnsiString Title)
  422. {
  423. TConsoleCommStruct * CommStruct = GetCommStruct();
  424. try
  425. {
  426. if (Title.Length() >= sizeof(CommStruct->TitleEvent.Title))
  427. {
  428. throw Exception(FMTLOAD(CONSOLE_PRINT_TOO_LONG, (Title.Length())));
  429. }
  430. CommStruct->Event = TConsoleCommStruct::TITLE;
  431. CharToOem(Title.c_str(), CommStruct->TitleEvent.Title);
  432. }
  433. __finally
  434. {
  435. FreeCommStruct(CommStruct);
  436. }
  437. SendEvent(PrintTimeout);
  438. }
  439. //---------------------------------------------------------------------------
  440. void __fastcall TExternalConsole::WaitBeforeExit()
  441. {
  442. // noop
  443. }
  444. //---------------------------------------------------------------------------
  445. class TNullConsole : public TConsole
  446. {
  447. public:
  448. __fastcall TNullConsole();
  449. virtual void __fastcall Print(AnsiString Str, bool FromBeginning = false);
  450. virtual bool __fastcall Input(AnsiString & Str, bool Echo);
  451. virtual int __fastcall Choice(AnsiString Options, int Cancel, int Break);
  452. virtual bool __fastcall PendingAbort();
  453. virtual void __fastcall SetTitle(AnsiString Title);
  454. virtual void __fastcall WaitBeforeExit();
  455. };
  456. //---------------------------------------------------------------------------
  457. __fastcall TNullConsole::TNullConsole()
  458. {
  459. }
  460. //---------------------------------------------------------------------------
  461. void __fastcall TNullConsole::Print(AnsiString /*Str*/, bool /*FromBeginning*/)
  462. {
  463. // noop
  464. }
  465. //---------------------------------------------------------------------------
  466. bool __fastcall TNullConsole::Input(AnsiString & /*Str*/, bool /*Echo*/)
  467. {
  468. return false;
  469. }
  470. //---------------------------------------------------------------------------
  471. int __fastcall TNullConsole::Choice(AnsiString /*Options*/, int /*Cancel*/,
  472. int Break)
  473. {
  474. return Break;
  475. }
  476. //---------------------------------------------------------------------------
  477. bool __fastcall TNullConsole::PendingAbort()
  478. {
  479. return false;
  480. }
  481. //---------------------------------------------------------------------------
  482. void __fastcall TNullConsole::SetTitle(AnsiString /*Title*/)
  483. {
  484. // noop
  485. }
  486. //---------------------------------------------------------------------------
  487. void __fastcall TNullConsole::WaitBeforeExit()
  488. {
  489. assert(false);
  490. // noop
  491. }
  492. //---------------------------------------------------------------------------
  493. class TConsoleRunner
  494. {
  495. public:
  496. TConsoleRunner(TConsole * Console);
  497. int __fastcall Run(const AnsiString Session, TStrings * ScriptCommands);
  498. void __fastcall ShowException(Exception * E);
  499. protected:
  500. bool __fastcall Input(AnsiString & Str, bool Echo);
  501. inline void __fastcall Print(const AnsiString & Str);
  502. inline void __fastcall PrintLine(const AnsiString & Str);
  503. inline void __fastcall PrintMessage(const AnsiString & Str);
  504. void __fastcall UpdateTitle();
  505. inline void __fastcall NotifyAbort();
  506. inline bool __fastcall Aborted(bool AllowCompleteAbort = true);
  507. private:
  508. TManagementScript * FScript;
  509. TConsole * FConsole;
  510. TSynchronizeController FSynchronizeController;
  511. int FLastProgressLen;
  512. bool FSynchronizeAborted;
  513. bool FCommandError;
  514. bool FBatchScript;
  515. bool FAborted;
  516. void __fastcall ScriptPrint(TScript * Script, const AnsiString Str);
  517. void __fastcall ScriptPrintProgress(TScript * Script, bool First, const AnsiString Str);
  518. void __fastcall ScriptInput(TScript * Script, const AnsiString Prompt, AnsiString & Str);
  519. void __fastcall ScriptTerminalUpdateStatus(TSecureShell * SecureShell, bool Active);
  520. void __fastcall ScriptTerminalPromptUser(TSecureShell * SecureShell,
  521. AnsiString Prompt, TPromptKind Kind, AnsiString & Response, bool & Result, void * Arg);
  522. void __fastcall ScriptShowExtendedException(TSecureShell * SecureShell,
  523. Exception * E, void * Arg);
  524. void __fastcall ScriptTerminalQueryUser(TObject * Sender, const AnsiString Query,
  525. TStrings * MoreMessages, int Answers, const TQueryParams * Params, int & Answer,
  526. TQueryType QueryType, void * Arg);
  527. void __fastcall ScriptQueryCancel(TScript * Script, bool & Cancel);
  528. void __fastcall SynchronizeControllerAbort(TObject * Sender, bool Close);
  529. void __fastcall SynchronizeControllerLog(TSynchronizeController * Controller,
  530. TSynchronizeLogEntry Entry, const AnsiString Message);
  531. void __fastcall ScriptSynchronizeStartStop(TScript * Script,
  532. const AnsiString LocalDirectory, const AnsiString RemoteDirectory);
  533. void __fastcall SynchronizeControllerSynchronize(TSynchronizeController * Sender,
  534. const AnsiString LocalDirectory, const AnsiString RemoteDirectory,
  535. const TCopyParamType & CopyParam, const TSynchronizeParamType & Params,
  536. TSynchronizeChecklist ** Checklist, TSynchronizeOptions * Options, bool Full);
  537. void __fastcall SynchronizeControllerSynchronizeInvalid(TSynchronizeController * Sender,
  538. const AnsiString Directory, const AnsiString ErrorStr);
  539. void __fastcall SynchronizeControllerTooManyDirectories(TSynchronizeController * Sender,
  540. int & MaxDirectories);
  541. };
  542. //---------------------------------------------------------------------------
  543. TConsoleRunner::TConsoleRunner(TConsole * Console) :
  544. FSynchronizeController(&SynchronizeControllerSynchronize,
  545. &SynchronizeControllerSynchronizeInvalid,
  546. &SynchronizeControllerTooManyDirectories)
  547. {
  548. FConsole = Console;
  549. FLastProgressLen = 0;
  550. FScript = NULL;
  551. FAborted = false;
  552. FBatchScript = false;
  553. }
  554. //---------------------------------------------------------------------------
  555. void __fastcall TConsoleRunner::ScriptInput(TScript * /*Script*/,
  556. const AnsiString Prompt, AnsiString & Str)
  557. {
  558. Print(Prompt);
  559. if (!Input(Str, true))
  560. {
  561. Abort();
  562. }
  563. }
  564. //---------------------------------------------------------------------------
  565. void __fastcall TConsoleRunner::Print(const AnsiString & Str)
  566. {
  567. if (FLastProgressLen > 0)
  568. {
  569. FConsole->Print("\n" + Str);
  570. FLastProgressLen = 0;
  571. }
  572. else
  573. {
  574. FConsole->Print(Str);
  575. }
  576. }
  577. //---------------------------------------------------------------------------
  578. void __fastcall TConsoleRunner::PrintLine(const AnsiString & Str)
  579. {
  580. Print(Str + "\n");
  581. }
  582. //---------------------------------------------------------------------------
  583. void __fastcall TConsoleRunner::PrintMessage(const AnsiString & Str)
  584. {
  585. PrintLine(
  586. StringReplace(StringReplace(Str.TrimRight(), "\n\n", "\n", TReplaceFlags() << rfReplaceAll),
  587. "\n \n", "\n", TReplaceFlags() << rfReplaceAll));
  588. }
  589. //---------------------------------------------------------------------------
  590. void __fastcall TConsoleRunner::NotifyAbort()
  591. {
  592. if (FBatchScript)
  593. {
  594. FAborted = true;
  595. }
  596. }
  597. //---------------------------------------------------------------------------
  598. bool __fastcall TConsoleRunner::Aborted(bool AllowCompleteAbort)
  599. {
  600. bool Result;
  601. if (FAborted)
  602. {
  603. Result = true;
  604. }
  605. else
  606. {
  607. Result = FConsole->PendingAbort();
  608. if (Result)
  609. {
  610. PrintLine(LoadStr(USER_TERMINATED));
  611. if (AllowCompleteAbort)
  612. {
  613. NotifyAbort();
  614. }
  615. }
  616. }
  617. return Result;
  618. }
  619. //---------------------------------------------------------------------------
  620. void __fastcall TConsoleRunner::ScriptPrint(TScript * /*Script*/,
  621. const AnsiString Str)
  622. {
  623. Print(Str);
  624. }
  625. //---------------------------------------------------------------------------
  626. void __fastcall TConsoleRunner::ScriptPrintProgress(TScript * /*Script*/,
  627. bool First, const AnsiString Str)
  628. {
  629. AnsiString S = Str;
  630. if (First && (FLastProgressLen > 0))
  631. {
  632. S = "\n" + S;
  633. }
  634. else if (S.Length() < FLastProgressLen)
  635. {
  636. S += AnsiString::StringOfChar(' ', FLastProgressLen - S.Length());
  637. }
  638. FConsole->Print(S, true);
  639. FLastProgressLen = Str.Length();
  640. }
  641. //---------------------------------------------------------------------------
  642. void __fastcall TConsoleRunner::ScriptTerminalUpdateStatus(
  643. TSecureShell * SecureShell, bool Active)
  644. {
  645. if (Active)
  646. {
  647. assert((SecureShell->Status >= 0) && (SecureShell->Status < ConnectionStatusStringsCount));
  648. AnsiString Status = LoadStr(ConnectionStatusStrings[SecureShell->Status]);
  649. PrintLine(Status);
  650. }
  651. }
  652. //---------------------------------------------------------------------------
  653. void __fastcall TConsoleRunner::ScriptTerminalPromptUser(TSecureShell * /*SecureShell*/,
  654. AnsiString Prompt, TPromptKind Kind, AnsiString & Response, bool & Result,
  655. void * /*Arg*/)
  656. {
  657. if (!Prompt.IsEmpty() && (Prompt[Prompt.Length()] != ' '))
  658. {
  659. Prompt += ' ';
  660. }
  661. Print(Prompt);
  662. Result = Input(Response, (Kind == pkPrompt));
  663. }
  664. //---------------------------------------------------------------------------
  665. void __fastcall TConsoleRunner::ScriptShowExtendedException(
  666. TSecureShell * /*SecureShell*/, Exception * E, void * /*Arg*/)
  667. {
  668. ShowException(E);
  669. }
  670. //---------------------------------------------------------------------------
  671. void __fastcall TConsoleRunner::ScriptTerminalQueryUser(TObject * /*Sender*/,
  672. const AnsiString Query, TStrings * MoreMessages, int Answers,
  673. const TQueryParams * Params, int & Answer, TQueryType /*QueryType*/,
  674. void * /*Arg*/)
  675. {
  676. PrintMessage(Query);
  677. if ((MoreMessages != NULL) && (MoreMessages->Count > 0))
  678. {
  679. PrintMessage(MoreMessages->Text);
  680. }
  681. static const int MaxButtonCount = 15;
  682. int Buttons[MaxButtonCount];
  683. AnsiString Captions[MaxButtonCount];
  684. int ButtonCount = 0;
  685. int AAnswers = Answers;
  686. #define ADD_BUTTON(TYPE, CAPTION) \
  687. if (FLAGSET(AAnswers, qa ## TYPE)) \
  688. { \
  689. assert(ButtonCount < MaxButtonCount); \
  690. Captions[ButtonCount] = CAPTION; \
  691. Buttons[ButtonCount] = qa ## TYPE; \
  692. ButtonCount++; \
  693. AAnswers -= qa ## TYPE; \
  694. }
  695. #define ADD_BUTTON_RES(TYPE) ADD_BUTTON(TYPE, LoadResourceString(&_SMsgDlg ## TYPE));
  696. ADD_BUTTON_RES(Yes);
  697. ADD_BUTTON_RES(No);
  698. ADD_BUTTON_RES(OK);
  699. ADD_BUTTON_RES(Cancel);
  700. ADD_BUTTON_RES(Abort);
  701. ADD_BUTTON_RES(Retry);
  702. ADD_BUTTON_RES(Ignore);
  703. // to keep the same order as for GUI message box
  704. ADD_BUTTON(Skip, LoadStr(SKIP_BUTTON));
  705. ADD_BUTTON_RES(All);
  706. ADD_BUTTON_RES(NoToAll);
  707. ADD_BUTTON_RES(YesToAll);
  708. ADD_BUTTON_RES(Help);
  709. #undef ADD_BUTTON_RES
  710. #undef ADD_BUTTON
  711. USEDPARAM(AAnswers);
  712. assert(AAnswers == 0);
  713. assert(ButtonCount > 0);
  714. if ((Params != NULL) && (Params->Aliases != NULL))
  715. {
  716. for (int bi = 0; bi < ButtonCount; bi++)
  717. {
  718. for (unsigned int ai = 0; ai < Params->AliasesCount; ai++)
  719. {
  720. if (static_cast<int>(Params->Aliases[ai].Button) == Buttons[bi])
  721. {
  722. Captions[bi] = Params->Aliases[ai].Alias;
  723. break;
  724. }
  725. }
  726. }
  727. }
  728. AnsiString Accels;
  729. for (int Index = 0; Index < ButtonCount; Index++)
  730. {
  731. AnsiString & Caption = Captions[Index];
  732. int P = Caption.Pos('&');
  733. if ((P > 0) && (P < Caption.Length()))
  734. {
  735. char Accel = AnsiUpperCase(Caption)[P + 1];
  736. if (Accels.Pos(Accel) > 0)
  737. {
  738. Caption.Delete(P, 1);
  739. Accels += ' ';
  740. }
  741. else
  742. {
  743. Accels += Accel;
  744. }
  745. }
  746. else
  747. {
  748. Accels += ' ';
  749. }
  750. }
  751. assert(Accels.Length() == ButtonCount);
  752. int NumberAccel = 0;
  753. int CancelA = CancelAnswer(Answers);
  754. int CancelIndex;
  755. int AbortA = AbortAnswer(Answers);
  756. int AbortIndex;
  757. int ContinueA = ContinueAnswer(Answers);
  758. int ContinueIndex;
  759. for (int Index = 0; Index < ButtonCount; Index++)
  760. {
  761. AnsiString & Caption = Captions[Index];
  762. if (Accels[Index + 1] == ' ')
  763. {
  764. for (int Index2 = 1; Index2 <= Caption.Length(); Index2++)
  765. {
  766. char C = AnsiUpperCase(Caption)[Index2];
  767. if ((C >= 'A') && (C <= 'Z') && (Accels.Pos(C) == 0))
  768. {
  769. Caption.Insert("&", Index2);
  770. Accels[Index + 1] = C;
  771. break;
  772. }
  773. }
  774. }
  775. if (Accels[Index + 1] == ' ')
  776. {
  777. for (int Index2 = 1; Index2 <= Caption.Length(); Index2++)
  778. {
  779. char C = AnsiUpperCase(Caption)[Index2];
  780. if ((C != ' ') && (Accels.Pos(C) == 0))
  781. {
  782. Caption.Insert("&", Index2);
  783. Accels[Index + 1] = C;
  784. break;
  785. }
  786. }
  787. }
  788. if (Accels[Index + 1] == ' ')
  789. {
  790. NumberAccel++;
  791. assert(NumberAccel <= 9);
  792. Caption = FORMAT("&%d%s", (NumberAccel, Caption));
  793. Accels[Index + 1] = Caption[2];
  794. }
  795. if (Buttons[Index] == CancelA)
  796. {
  797. CancelIndex = Index + 1;
  798. }
  799. if (Buttons[Index] == AbortA)
  800. {
  801. AbortIndex = Index + 1;
  802. }
  803. if (Buttons[Index] == ContinueA)
  804. {
  805. ContinueIndex = Index + 1;
  806. }
  807. }
  808. assert(Accels.Pos(' ') == 0);
  809. AnsiString Output;
  810. for (int i = 0; i < ButtonCount; i++)
  811. {
  812. if (i > 0)
  813. {
  814. Output += ", ";
  815. }
  816. AnsiString Caption = Captions[i];
  817. int P = Caption.Pos('&');
  818. assert(P >= 0);
  819. Caption[P] = '(';
  820. Caption.Insert(")", P + 2);
  821. Output += Caption;
  822. }
  823. Output += ": ";
  824. Print(Output);
  825. int AnswerIndex;
  826. if (FScript->Batch == TScript::BatchContinue)
  827. {
  828. AnswerIndex = ContinueIndex;
  829. }
  830. else if (FScript->Batch != TScript::BatchOff)
  831. {
  832. AnswerIndex = AbortIndex;
  833. }
  834. else
  835. {
  836. AnswerIndex = FConsole->Choice(Accels, CancelIndex, -1);
  837. if (AnswerIndex == -1)
  838. {
  839. NotifyAbort();
  840. AnswerIndex = AbortIndex;
  841. }
  842. }
  843. assert((AnswerIndex >= 1) && (AnswerIndex <= Accels.Length()));
  844. AnsiString AnswerCaption = Captions[AnswerIndex - 1];
  845. int P = AnswerCaption.Pos("&");
  846. assert(P >= 0);
  847. AnswerCaption.Delete(P, 1);
  848. PrintLine(AnswerCaption);
  849. Answer = Buttons[AnswerIndex - 1];
  850. if (Answer == qaAbort)
  851. {
  852. FCommandError = true;
  853. }
  854. }
  855. //---------------------------------------------------------------------------
  856. void __fastcall TConsoleRunner::ScriptQueryCancel(TScript * /*Script*/, bool & Cancel)
  857. {
  858. if (Aborted())
  859. {
  860. Cancel = true;
  861. }
  862. }
  863. //---------------------------------------------------------------------------
  864. void __fastcall TConsoleRunner::ScriptSynchronizeStartStop(TScript * /*Script*/,
  865. const AnsiString LocalDirectory, const AnsiString RemoteDirectory)
  866. {
  867. TSynchronizeParamType Params;
  868. Params.LocalDirectory = LocalDirectory;
  869. Params.RemoteDirectory = RemoteDirectory;
  870. Params.Params = -1; // never used
  871. Params.Options = soRecurse;
  872. FSynchronizeController.StartStop(Application, true, Params,
  873. FScript->CopyParam, NULL, SynchronizeControllerAbort, NULL,
  874. SynchronizeControllerLog);
  875. try
  876. {
  877. FSynchronizeAborted = false;
  878. while (!FSynchronizeAborted && !Aborted(false))
  879. {
  880. Application->HandleMessage();
  881. }
  882. }
  883. __finally
  884. {
  885. FSynchronizeController.StartStop(Application, false, Params,
  886. FScript->CopyParam, NULL, SynchronizeControllerAbort, NULL,
  887. SynchronizeControllerLog);
  888. }
  889. }
  890. //---------------------------------------------------------------------------
  891. void __fastcall TConsoleRunner::SynchronizeControllerLog(
  892. TSynchronizeController * /*Controller*/, TSynchronizeLogEntry /*Entry*/,
  893. const AnsiString Message)
  894. {
  895. PrintLine(Message);
  896. }
  897. //---------------------------------------------------------------------------
  898. void __fastcall TConsoleRunner::SynchronizeControllerAbort(TObject * /*Sender*/,
  899. bool /*Close*/)
  900. {
  901. FSynchronizeAborted = true;
  902. }
  903. //---------------------------------------------------------------------------
  904. void __fastcall TConsoleRunner::SynchronizeControllerSynchronize(
  905. TSynchronizeController * /*Sender*/, const AnsiString LocalDirectory,
  906. const AnsiString RemoteDirectory, const TCopyParamType & CopyParam,
  907. const TSynchronizeParamType & /*Params*/, TSynchronizeChecklist ** Checklist,
  908. TSynchronizeOptions * /*Options*/, bool Full)
  909. {
  910. if (!Full)
  911. {
  912. FScript->Synchronize(LocalDirectory, RemoteDirectory, CopyParam, Checklist);
  913. }
  914. }
  915. //---------------------------------------------------------------------------
  916. void __fastcall TConsoleRunner::SynchronizeControllerSynchronizeInvalid(
  917. TSynchronizeController * /*Sender*/, const AnsiString Directory, const AnsiString ErrorStr)
  918. {
  919. if (!Directory.IsEmpty())
  920. {
  921. PrintLine(FMTLOAD(WATCH_ERROR_DIRECTORY, (Directory)));
  922. }
  923. else
  924. {
  925. PrintLine(LoadStr(WATCH_ERROR_GENERAL));
  926. }
  927. if (!ErrorStr.IsEmpty())
  928. {
  929. PrintLine(ErrorStr);
  930. }
  931. }
  932. //---------------------------------------------------------------------------
  933. void __fastcall TConsoleRunner::SynchronizeControllerTooManyDirectories(
  934. TSynchronizeController * /*Sender*/, int & MaxDirectories)
  935. {
  936. if (Aborted())
  937. {
  938. Abort();
  939. }
  940. if (MaxDirectories < GUIConfiguration->MaxWatchDirectories)
  941. {
  942. MaxDirectories = GUIConfiguration->MaxWatchDirectories;
  943. }
  944. else
  945. {
  946. MaxDirectories *= 2;
  947. }
  948. }
  949. //---------------------------------------------------------------------------
  950. void __fastcall TConsoleRunner::ShowException(Exception * E)
  951. {
  952. if (!E->Message.IsEmpty() &&
  953. (dynamic_cast<EAbort *>(E) == NULL))
  954. {
  955. FCommandError = true;
  956. PrintMessage(TranslateExceptionMessage(E));
  957. ExtException * EE = dynamic_cast<ExtException *>(E);
  958. if ((EE != NULL) && (EE->MoreMessages != NULL))
  959. {
  960. PrintMessage(EE->MoreMessages->Text);
  961. }
  962. }
  963. }
  964. //---------------------------------------------------------------------------
  965. bool __fastcall TConsoleRunner::Input(AnsiString & Str, bool Echo)
  966. {
  967. bool Result = FConsole->Input(Str, Echo);
  968. if (Result)
  969. {
  970. while (!Str.IsEmpty() &&
  971. ((Str[Str.Length()] == '\n') || (Str[Str.Length()] == '\r')))
  972. {
  973. Str.SetLength(Str.Length() - 1);
  974. }
  975. }
  976. else
  977. {
  978. NotifyAbort();
  979. }
  980. return Result;
  981. }
  982. //---------------------------------------------------------------------------
  983. int __fastcall TConsoleRunner::Run(const AnsiString Session, TStrings * ScriptCommands)
  984. {
  985. bool AnyError = false;
  986. try
  987. {
  988. FScript = new TManagementScript(StoredSessions);
  989. try
  990. {
  991. FScript->CopyParam = GUIConfiguration->DefaultCopyParam;
  992. FScript->SynchronizeParams = GUIConfiguration->SynchronizeParams;
  993. FScript->OnPrint = ScriptPrint;
  994. FScript->OnPrintProgress = ScriptPrintProgress;
  995. FScript->OnInput = ScriptInput;
  996. FScript->OnTerminalUpdateStatus = ScriptTerminalUpdateStatus;
  997. FScript->OnTerminalPromptUser = ScriptTerminalPromptUser;
  998. FScript->OnShowExtendedException = ScriptShowExtendedException;
  999. FScript->OnTerminalQueryUser = ScriptTerminalQueryUser;
  1000. FScript->OnQueryCancel = ScriptQueryCancel;
  1001. FScript->OnSynchronizeStartStop = ScriptSynchronizeStartStop;
  1002. UpdateTitle();
  1003. // everything until the first manually entered command is "batch"
  1004. // (including opening session from command line and script file)
  1005. FBatchScript = true;
  1006. if (!Session.IsEmpty())
  1007. {
  1008. FScript->Connect(Session);
  1009. }
  1010. int ScriptPos = 0;
  1011. bool Result;
  1012. do
  1013. {
  1014. UpdateTitle();
  1015. AnsiString Command;
  1016. if ((ScriptCommands != NULL) && (ScriptPos < ScriptCommands->Count))
  1017. {
  1018. Result = true;
  1019. Command = ScriptCommands->Strings[ScriptPos];
  1020. ScriptPos++;
  1021. }
  1022. else
  1023. {
  1024. // no longer batch
  1025. FBatchScript = false;
  1026. Print("winscp> ");
  1027. Result = Input(Command, true);
  1028. }
  1029. if (Result)
  1030. {
  1031. FCommandError = false;
  1032. FScript->Command(Command);
  1033. if (FCommandError)
  1034. {
  1035. AnyError = true;
  1036. if (FScript->Batch == TScript::BatchAbort)
  1037. {
  1038. Result = false;
  1039. }
  1040. }
  1041. }
  1042. }
  1043. while (Result && FScript->Continue && !Aborted());
  1044. }
  1045. __finally
  1046. {
  1047. delete FScript;
  1048. FScript = NULL;
  1049. }
  1050. }
  1051. catch(Exception & E)
  1052. {
  1053. ShowException(&E);
  1054. AnyError = true;
  1055. }
  1056. return AnyError ? RESULT_ANY_ERROR : RESULT_SUCCESS;
  1057. }
  1058. //---------------------------------------------------------------------------
  1059. void __fastcall TConsoleRunner::UpdateTitle()
  1060. {
  1061. AnsiString NewTitle;
  1062. if (FScript->Terminal != NULL)
  1063. {
  1064. NewTitle = FMTLOAD(APP_CAPTION, (FScript->Terminal->SessionData->SessionName, AppName));
  1065. }
  1066. else
  1067. {
  1068. NewTitle = AppName;
  1069. }
  1070. FConsole->SetTitle(NewTitle);
  1071. }
  1072. //---------------------------------------------------------------------------
  1073. void __fastcall LoadScriptFromFile(AnsiString FileName, TStrings * Lines)
  1074. {
  1075. AnsiString UTFBOM = "\xEF\xBB\xBF";
  1076. Lines->LoadFromFile(FileName);
  1077. if ((Lines->Count > 0) &&
  1078. (Lines->Strings[0].SubString(1, UTFBOM.Length()) == UTFBOM))
  1079. {
  1080. Lines->Strings[0] = Lines->Strings[0].SubString(
  1081. UTFBOM.Length() + 1, Lines->Strings[0].Length() - UTFBOM.Length());
  1082. for (int Index = 0; Index < Lines->Count; Index++)
  1083. {
  1084. Lines->Strings[Index] = DecodeUTF(Lines->Strings[Index]);
  1085. }
  1086. }
  1087. }
  1088. //---------------------------------------------------------------------------
  1089. int __fastcall Console(TProgramParams * Params, bool Help)
  1090. {
  1091. int Result = 0;
  1092. TConsole * Console = NULL;
  1093. TConsoleRunner * Runner = NULL;
  1094. TStrings * ScriptCommands = new TStringList();
  1095. try
  1096. {
  1097. AnsiString ConsoleInstance;
  1098. if (Params->FindSwitch("consoleinstance", ConsoleInstance))
  1099. {
  1100. Console = new TExternalConsole(ConsoleInstance);
  1101. }
  1102. else if (Params->FindSwitch("Console") || Help)
  1103. {
  1104. Console = TOwnConsole::Instance();
  1105. }
  1106. else
  1107. {
  1108. Console = new TNullConsole();
  1109. }
  1110. if (Help)
  1111. {
  1112. AnsiString Usage = LoadStr(USAGE3, 10240);
  1113. AnsiString ExeBaseName = ChangeFileExt(ExtractFileName(Application->ExeName), "");
  1114. Usage = StringReplace(Usage, "%APP%", ExeBaseName,
  1115. TReplaceFlags() << rfReplaceAll << rfIgnoreCase);
  1116. AnsiString Copyright = StringReplace(LoadStr(WINSCP_COPYRIGHT), "©", "(c)",
  1117. TReplaceFlags() << rfReplaceAll << rfIgnoreCase);
  1118. Usage = FORMAT(Usage, (Configuration->VersionStr, Copyright));
  1119. Console->Print(Usage);
  1120. Console->WaitBeforeExit();
  1121. }
  1122. else
  1123. {
  1124. Runner = new TConsoleRunner(Console);
  1125. try
  1126. {
  1127. AnsiString Value;
  1128. if (Params->FindSwitch("script", Value) && !Value.IsEmpty())
  1129. {
  1130. LoadScriptFromFile(Value, ScriptCommands);
  1131. }
  1132. Params->FindSwitch("command", ScriptCommands);
  1133. bool Url = false;
  1134. AnsiString Session;
  1135. if (Params->ParamCount >= 1)
  1136. {
  1137. Session = Params->Param[1];
  1138. }
  1139. bool DefaultsOnly;
  1140. delete StoredSessions->ParseUrl(Session, DefaultsOnly,
  1141. puDecodeUrlChars, NULL, &Url);
  1142. if (Url)
  1143. {
  1144. // prevent any automatic action when URL is provided on
  1145. // command-line
  1146. ScriptCommands->Clear();
  1147. }
  1148. else
  1149. {
  1150. AnsiString LogFile;
  1151. if (Params->FindSwitch("Log", LogFile))
  1152. {
  1153. Configuration->TemporaryLogging(LogFile);
  1154. }
  1155. }
  1156. Result = Runner->Run(Session,
  1157. (ScriptCommands->Count > 0 ? ScriptCommands : NULL));
  1158. }
  1159. catch(Exception & E)
  1160. {
  1161. Runner->ShowException(&E);
  1162. Result = RESULT_ANY_ERROR;
  1163. }
  1164. }
  1165. }
  1166. __finally
  1167. {
  1168. delete Runner;
  1169. delete Console;
  1170. delete ScriptCommands;
  1171. }
  1172. return Result;
  1173. }