ConsoleRunner.cpp 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include <Common.h>
  5. #include <Exceptions.h>
  6. #include <Script.h>
  7. #include <CoreMain.h>
  8. #include <Terminal.h>
  9. #include <PuttyTools.h>
  10. #include <Consts.hpp>
  11. #include "Console.h"
  12. #include "WinInterface.h"
  13. #include "ProgParams.h"
  14. #include "TextsWin.h"
  15. #include "TextsCore.h"
  16. #include "CustomWinConfiguration.h"
  17. #include "SynchronizeController.h"
  18. #include "GUITools.h"
  19. enum { RESULT_SUCCESS = 0, RESULT_ANY_ERROR = 1 };
  20. //---------------------------------------------------------------------------
  21. #define WM_INTERUPT_IDLE (WM_WINSCP_USER + 3)
  22. #define BATCH_INPUT_TIMEOUT 10000
  23. //---------------------------------------------------------------------------
  24. #pragma package(smart_init)
  25. //---------------------------------------------------------------------------
  26. class TConsole
  27. {
  28. public:
  29. virtual __fastcall ~TConsole() {};
  30. virtual void __fastcall Print(AnsiString Str, bool FromBeginning = false) = 0;
  31. virtual bool __fastcall Input(AnsiString & Str, bool Echo, unsigned int Timer) = 0;
  32. virtual int __fastcall Choice(AnsiString Options, int Cancel, int Break,
  33. int Timeouted, unsigned int Timer) = 0;
  34. virtual bool __fastcall PendingAbort() = 0;
  35. virtual void __fastcall SetTitle(AnsiString Title) = 0;
  36. virtual bool __fastcall LimitedOutput() = 0;
  37. virtual void __fastcall WaitBeforeExit() = 0;
  38. };
  39. //---------------------------------------------------------------------------
  40. class TOwnConsole : public TConsole
  41. {
  42. public:
  43. static TOwnConsole * __fastcall Instance();
  44. virtual void __fastcall Print(AnsiString Str, bool FromBeginning = false);
  45. virtual bool __fastcall Input(AnsiString & Str, bool Echo, unsigned int Timer);
  46. virtual int __fastcall Choice(AnsiString Options, int Cancel, int Break,
  47. int Timeouted, unsigned int Timer);
  48. virtual bool __fastcall PendingAbort();
  49. virtual void __fastcall SetTitle(AnsiString Title);
  50. virtual bool __fastcall LimitedOutput();
  51. virtual void __fastcall WaitBeforeExit();
  52. protected:
  53. static TOwnConsole * FInstance;
  54. __fastcall TOwnConsole();
  55. virtual __fastcall ~TOwnConsole();
  56. void __fastcall BreakInput();
  57. static BOOL WINAPI HandlerRoutine(DWORD CtrlType);
  58. static int __fastcall InputTimerThreadProc(void * Parameter);
  59. private:
  60. HANDLE FInput;
  61. HANDLE FOutput;
  62. HANDLE FInputTimerEvent;
  63. static TCriticalSection FSection;
  64. bool FPendingAbort;
  65. };
  66. //---------------------------------------------------------------------------
  67. TOwnConsole * TOwnConsole::FInstance = NULL;
  68. TCriticalSection TOwnConsole::FSection;
  69. //---------------------------------------------------------------------------
  70. __fastcall TOwnConsole::TOwnConsole()
  71. {
  72. assert(FInstance == NULL);
  73. FInstance = this;
  74. AllocConsole();
  75. SetConsoleCtrlHandler(HandlerRoutine, true);
  76. FInput = GetStdHandle(STD_INPUT_HANDLE);
  77. FOutput = GetStdHandle(STD_OUTPUT_HANDLE);
  78. FInputTimerEvent = NULL;
  79. FPendingAbort = false;
  80. }
  81. //---------------------------------------------------------------------------
  82. __fastcall TOwnConsole::~TOwnConsole()
  83. {
  84. TGuard Guard(&FSection);
  85. // deliberatelly do not remove ConsoleCtrlHandler as it causes
  86. // failures while exiting
  87. FreeConsole();
  88. assert(FInstance == this);
  89. FInstance = NULL;
  90. }
  91. //---------------------------------------------------------------------------
  92. TOwnConsole * __fastcall TOwnConsole::Instance()
  93. {
  94. return new TOwnConsole();
  95. }
  96. //---------------------------------------------------------------------------
  97. void __fastcall TOwnConsole::BreakInput()
  98. {
  99. FlushConsoleInputBuffer(FInput);
  100. INPUT_RECORD InputRecord;
  101. memset(&InputRecord, 0, sizeof(InputRecord));
  102. InputRecord.EventType = KEY_EVENT;
  103. InputRecord.Event.KeyEvent.bKeyDown = true;
  104. InputRecord.Event.KeyEvent.wRepeatCount = 1;
  105. InputRecord.Event.KeyEvent.uChar.AsciiChar = '\r';
  106. unsigned long Written;
  107. // this assertion occasionally fails (when console is being exited)
  108. CHECK(WriteConsoleInput(FInput, &InputRecord, 1, &Written));
  109. assert(Written == 1);
  110. FPendingAbort = true;
  111. PostMessage(Application->Handle, WM_INTERUPT_IDLE, 0, 0);
  112. }
  113. //---------------------------------------------------------------------------
  114. BOOL WINAPI TOwnConsole::HandlerRoutine(DWORD CtrlType)
  115. {
  116. if ((CtrlType == CTRL_C_EVENT) || (CtrlType == CTRL_BREAK_EVENT))
  117. {
  118. {
  119. TGuard Guard(&FSection);
  120. // just to be real thread-safe
  121. if (FInstance != NULL)
  122. {
  123. FInstance->BreakInput();
  124. }
  125. }
  126. return true;
  127. }
  128. else
  129. {
  130. return false;
  131. }
  132. }
  133. //---------------------------------------------------------------------------
  134. int __fastcall TOwnConsole::InputTimerThreadProc(void * Parameter)
  135. {
  136. assert(FInstance != NULL);
  137. unsigned int Timer = reinterpret_cast<unsigned int>(Parameter);
  138. if (WaitForSingleObject(FInstance->FInputTimerEvent, Timer) == WAIT_TIMEOUT)
  139. {
  140. FInstance->BreakInput();
  141. }
  142. return 0;
  143. }
  144. //---------------------------------------------------------------------------
  145. bool __fastcall TOwnConsole::PendingAbort()
  146. {
  147. if (FPendingAbort)
  148. {
  149. FPendingAbort = false;
  150. return true;
  151. }
  152. else
  153. {
  154. return FPendingAbort;
  155. }
  156. }
  157. //---------------------------------------------------------------------------
  158. void __fastcall TOwnConsole::Print(AnsiString Str, bool FromBeginning)
  159. {
  160. if (FromBeginning)
  161. {
  162. CONSOLE_SCREEN_BUFFER_INFO BufferInfo;
  163. GetConsoleScreenBufferInfo(FOutput, &BufferInfo);
  164. BufferInfo.dwCursorPosition.X = 0;
  165. SetConsoleCursorPosition(FOutput, BufferInfo.dwCursorPosition);
  166. }
  167. unsigned long Written;
  168. AnsiToOem(Str);
  169. bool Result = WriteConsole(FOutput, Str.c_str(), Str.Length(), &Written, NULL);
  170. assert(Result);
  171. USEDPARAM(Result);
  172. assert(Str.Length() == static_cast<long>(Written));
  173. }
  174. //---------------------------------------------------------------------------
  175. bool __fastcall TOwnConsole::Input(AnsiString & Str, bool Echo, unsigned int Timer)
  176. {
  177. unsigned long PrevMode, NewMode;
  178. GetConsoleMode(FInput, &PrevMode);
  179. NewMode = PrevMode | ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT;
  180. if (Echo)
  181. {
  182. NewMode |= ENABLE_ECHO_INPUT;
  183. }
  184. else
  185. {
  186. NewMode &= ~ENABLE_ECHO_INPUT;
  187. }
  188. SetConsoleMode(FInput, NewMode);
  189. HANDLE InputTimerThread = NULL;
  190. bool Result;
  191. try
  192. {
  193. if (Timer > 0)
  194. {
  195. unsigned int ThreadId;
  196. assert(FInputTimerEvent == NULL);
  197. FInputTimerEvent = CreateEvent(NULL, false, false, NULL);
  198. InputTimerThread = (HANDLE)BeginThread(NULL, 0, InputTimerThreadProc,
  199. reinterpret_cast<void *>(Timer), 0, ThreadId);
  200. }
  201. unsigned long Read;
  202. Str.SetLength(10240);
  203. Result = ReadConsole(FInput, Str.c_str(), Str.Length(), &Read, NULL);
  204. assert(Result);
  205. Str.SetLength(Read);
  206. OemToAnsi(Str);
  207. if (FPendingAbort || !Echo)
  208. {
  209. Print("\n");
  210. }
  211. if (FPendingAbort || (Read == 0))
  212. {
  213. Result = false;
  214. FPendingAbort = false;
  215. }
  216. }
  217. __finally
  218. {
  219. if (InputTimerThread != NULL)
  220. {
  221. SetEvent(FInputTimerEvent);
  222. WaitForSingleObject(InputTimerThread, 100);
  223. CloseHandle(FInputTimerEvent);
  224. FInputTimerEvent = NULL;
  225. CloseHandle(InputTimerThread);
  226. }
  227. SetConsoleMode(FInput, PrevMode);
  228. }
  229. return Result;
  230. }
  231. //---------------------------------------------------------------------------
  232. int __fastcall TOwnConsole::Choice(AnsiString Options, int Cancel, int Break,
  233. int Timeouted, unsigned int Timer)
  234. {
  235. AnsiToOem(Options);
  236. unsigned int ATimer = Timer;
  237. int Result = 0;
  238. unsigned long PrevMode, NewMode;
  239. GetConsoleMode(FInput, &PrevMode);
  240. NewMode = (PrevMode | ENABLE_PROCESSED_INPUT) & ~(ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT);
  241. SetConsoleMode(FInput, NewMode);
  242. try
  243. {
  244. do
  245. {
  246. unsigned long Read;
  247. INPUT_RECORD Record;
  248. if ((PeekConsoleInput(FInput, &Record, 1, &Read) != 0) &&
  249. (Read == 1))
  250. {
  251. if ((ReadConsoleInput(FInput, &Record, 1, &Read) != 0) &&
  252. (Read == 1))
  253. {
  254. if (PendingAbort())
  255. {
  256. Result = Break;
  257. }
  258. else if ((Record.EventType == KEY_EVENT) &&
  259. Record.Event.KeyEvent.bKeyDown)
  260. {
  261. char C = AnsiUpperCase(Record.Event.KeyEvent.uChar.AsciiChar)[1];
  262. if (C == 27)
  263. {
  264. Result = Cancel;
  265. }
  266. else if ((Options.Pos(C) > 0) &&
  267. FLAGCLEAR(Record.Event.KeyEvent.dwControlKeyState,
  268. LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED | LEFT_ALT_PRESSED |
  269. RIGHT_CTRL_PRESSED))
  270. {
  271. Result = Options.Pos(C);
  272. }
  273. }
  274. }
  275. }
  276. if (Result == 0)
  277. {
  278. unsigned int TimerSlice = 50;
  279. Sleep(TimerSlice);
  280. if (Timer > 0)
  281. {
  282. if (ATimer > TimerSlice)
  283. {
  284. ATimer -= TimerSlice;
  285. }
  286. else
  287. {
  288. Result = Timeouted;
  289. }
  290. }
  291. }
  292. }
  293. while (Result == 0);
  294. }
  295. __finally
  296. {
  297. SetConsoleMode(FInput, PrevMode);
  298. }
  299. return Result;
  300. }
  301. //---------------------------------------------------------------------------
  302. void __fastcall TOwnConsole::SetTitle(AnsiString Title)
  303. {
  304. AnsiToOem(Title);
  305. SetConsoleTitle(Title.c_str());
  306. }
  307. //---------------------------------------------------------------------------
  308. bool __fastcall TOwnConsole::LimitedOutput()
  309. {
  310. return true;
  311. }
  312. //---------------------------------------------------------------------------
  313. void __fastcall TOwnConsole::WaitBeforeExit()
  314. {
  315. unsigned long Read;
  316. INPUT_RECORD Record;
  317. while (ReadConsoleInput(FInput, &Record, 1, &Read))
  318. {
  319. if ((Read == 1) && (Record.EventType == KEY_EVENT) &&
  320. (Record.Event.KeyEvent.uChar.AsciiChar != 0) &&
  321. Record.Event.KeyEvent.bKeyDown)
  322. {
  323. break;
  324. }
  325. }
  326. }
  327. //---------------------------------------------------------------------------
  328. class TExternalConsole : public TConsole
  329. {
  330. public:
  331. __fastcall TExternalConsole(const AnsiString Instance);
  332. virtual __fastcall ~TExternalConsole();
  333. virtual void __fastcall Print(AnsiString Str, bool FromBeginning = false);
  334. virtual bool __fastcall Input(AnsiString & Str, bool Echo, unsigned int Timer);
  335. virtual int __fastcall Choice(AnsiString Options, int Cancel, int Break,
  336. int Timeouted, unsigned int Timer);
  337. virtual bool __fastcall PendingAbort();
  338. virtual void __fastcall SetTitle(AnsiString Title);
  339. virtual bool __fastcall LimitedOutput();
  340. virtual void __fastcall WaitBeforeExit();
  341. private:
  342. bool FPendingAbort;
  343. HANDLE FRequestEvent;
  344. HANDLE FResponseEvent;
  345. HANDLE FCancelEvent;
  346. HANDLE FFileMapping;
  347. bool FLimitedOutput;
  348. static const int PrintTimeout = 5000;
  349. inline TConsoleCommStruct * __fastcall GetCommStruct();
  350. inline void __fastcall FreeCommStruct(TConsoleCommStruct * CommStruct);
  351. inline void __fastcall SendEvent(int Timeout);
  352. void __fastcall Init();
  353. };
  354. //---------------------------------------------------------------------------
  355. __fastcall TExternalConsole::TExternalConsole(const AnsiString Instance)
  356. {
  357. FRequestEvent = OpenEvent(EVENT_ALL_ACCESS, false,
  358. FORMAT("%s%s", (CONSOLE_EVENT_REQUEST, (Instance))).c_str());
  359. FResponseEvent = OpenEvent(EVENT_ALL_ACCESS, false,
  360. FORMAT("%s%s", (CONSOLE_EVENT_RESPONSE, (Instance))).c_str());
  361. FCancelEvent = OpenEvent(EVENT_ALL_ACCESS, false,
  362. FORMAT("%s%s", (CONSOLE_EVENT_CANCEL, (Instance))).c_str());
  363. FFileMapping = OpenFileMapping(FILE_MAP_ALL_ACCESS, false,
  364. FORMAT("%s%s", (CONSOLE_MAPPING, (Instance))).c_str());
  365. if ((FRequestEvent == NULL) || (FResponseEvent == NULL) || (FFileMapping == NULL))
  366. {
  367. throw Exception(LoadStr(EXTERNAL_CONSOLE_INIT_ERROR));
  368. }
  369. TConsoleCommStruct * CommStruct = GetCommStruct();
  370. try
  371. {
  372. if (CommStruct->Version != TConsoleCommStruct::CurrentVersion)
  373. {
  374. throw Exception(FMTLOAD(EXTERNAL_CONSOLE_INCOMPATIBLE, (CommStruct->CurrentVersion)));
  375. }
  376. CommStruct->Version = TConsoleCommStruct::CurrentVersionConfirmed;
  377. }
  378. __finally
  379. {
  380. FreeCommStruct(CommStruct);
  381. }
  382. // to break application event loop regularly during "watching for changes"
  383. // to allow user to abort it
  384. SetTimer(Application->Handle, 1, 500, NULL);
  385. Init();
  386. }
  387. //---------------------------------------------------------------------------
  388. __fastcall TExternalConsole::~TExternalConsole()
  389. {
  390. CloseHandle(FRequestEvent);
  391. CloseHandle(FResponseEvent);
  392. CloseHandle(FCancelEvent);
  393. CloseHandle(FFileMapping);
  394. KillTimer(Application->Handle, 1);
  395. }
  396. //---------------------------------------------------------------------------
  397. TConsoleCommStruct * __fastcall TExternalConsole::GetCommStruct()
  398. {
  399. TConsoleCommStruct * Result;
  400. Result = static_cast<TConsoleCommStruct*>(MapViewOfFile(FFileMapping,
  401. FILE_MAP_ALL_ACCESS, 0, 0, 0));
  402. if (Result == NULL)
  403. {
  404. throw Exception(LoadStr(CONSOLE_COMM_ERROR));
  405. }
  406. return Result;
  407. }
  408. //---------------------------------------------------------------------------
  409. void __fastcall TExternalConsole::FreeCommStruct(TConsoleCommStruct * CommStruct)
  410. {
  411. UnmapViewOfFile(CommStruct);
  412. }
  413. //---------------------------------------------------------------------------
  414. void __fastcall TExternalConsole::SendEvent(int Timeout)
  415. {
  416. SetEvent(FRequestEvent);
  417. if (WaitForSingleObject(FResponseEvent, Timeout) != WAIT_OBJECT_0)
  418. {
  419. throw Exception(LoadStr(CONSOLE_SEND_TIMEOUT));
  420. }
  421. }
  422. //---------------------------------------------------------------------------
  423. void __fastcall TExternalConsole::Print(AnsiString Str, bool FromBeginning)
  424. {
  425. TConsoleCommStruct * CommStruct = GetCommStruct();
  426. try
  427. {
  428. if (Str.Length() >= sizeof(CommStruct->PrintEvent.Message))
  429. {
  430. throw Exception(FMTLOAD(CONSOLE_PRINT_TOO_LONG, (Str.Length())));
  431. }
  432. CommStruct->Event = TConsoleCommStruct::PRINT;
  433. CharToOem(Str.c_str(), CommStruct->PrintEvent.Message);
  434. CommStruct->PrintEvent.FromBeginning = FromBeginning;
  435. }
  436. __finally
  437. {
  438. FreeCommStruct(CommStruct);
  439. }
  440. SendEvent(PrintTimeout);
  441. }
  442. //---------------------------------------------------------------------------
  443. bool __fastcall TExternalConsole::Input(AnsiString & Str, bool Echo, unsigned int Timer)
  444. {
  445. TConsoleCommStruct * CommStruct = GetCommStruct();
  446. try
  447. {
  448. CommStruct->Event = TConsoleCommStruct::INPUT;
  449. CommStruct->InputEvent.Echo = Echo;
  450. CommStruct->InputEvent.Result = false;
  451. CommStruct->InputEvent.Str[0] = '\0';
  452. CommStruct->InputEvent.Timer = Timer;
  453. }
  454. __finally
  455. {
  456. FreeCommStruct(CommStruct);
  457. }
  458. SendEvent(INFINITE);
  459. bool Result;
  460. CommStruct = GetCommStruct();
  461. try
  462. {
  463. Result = CommStruct->InputEvent.Result;
  464. Str.SetLength(strlen(CommStruct->InputEvent.Str));
  465. OemToChar(CommStruct->InputEvent.Str, Str.c_str());
  466. }
  467. __finally
  468. {
  469. FreeCommStruct(CommStruct);
  470. }
  471. return Result;
  472. }
  473. //---------------------------------------------------------------------------
  474. int __fastcall TExternalConsole::Choice(AnsiString Options, int Cancel, int Break,
  475. int Timeouted, unsigned int Timer)
  476. {
  477. TConsoleCommStruct * CommStruct = GetCommStruct();
  478. try
  479. {
  480. CommStruct->Event = TConsoleCommStruct::CHOICE;
  481. assert(Options.Length() < sizeof(CommStruct->ChoiceEvent.Options));
  482. CharToOem(Options.c_str(), CommStruct->ChoiceEvent.Options);
  483. CommStruct->ChoiceEvent.Cancel = Cancel;
  484. CommStruct->ChoiceEvent.Break = Break;
  485. CommStruct->ChoiceEvent.Result = Break;
  486. CommStruct->ChoiceEvent.Timeouted = Timeouted;
  487. CommStruct->ChoiceEvent.Timer = Timer;
  488. }
  489. __finally
  490. {
  491. FreeCommStruct(CommStruct);
  492. }
  493. SendEvent(INFINITE);
  494. int Result;
  495. CommStruct = GetCommStruct();
  496. try
  497. {
  498. Result = CommStruct->ChoiceEvent.Result;
  499. }
  500. __finally
  501. {
  502. FreeCommStruct(CommStruct);
  503. }
  504. return Result;
  505. }
  506. //---------------------------------------------------------------------------
  507. bool __fastcall TExternalConsole::PendingAbort()
  508. {
  509. return (WaitForSingleObject(FCancelEvent, 0) == WAIT_OBJECT_0);
  510. }
  511. //---------------------------------------------------------------------------
  512. void __fastcall TExternalConsole::SetTitle(AnsiString Title)
  513. {
  514. TConsoleCommStruct * CommStruct = GetCommStruct();
  515. try
  516. {
  517. if (Title.Length() >= sizeof(CommStruct->TitleEvent.Title))
  518. {
  519. throw Exception(FMTLOAD(CONSOLE_PRINT_TOO_LONG, (Title.Length())));
  520. }
  521. CommStruct->Event = TConsoleCommStruct::TITLE;
  522. CharToOem(Title.c_str(), CommStruct->TitleEvent.Title);
  523. }
  524. __finally
  525. {
  526. FreeCommStruct(CommStruct);
  527. }
  528. SendEvent(INFINITE);
  529. }
  530. //---------------------------------------------------------------------------
  531. void __fastcall TExternalConsole::Init()
  532. {
  533. TConsoleCommStruct * CommStruct = GetCommStruct();
  534. try
  535. {
  536. CommStruct->Event = TConsoleCommStruct::INIT;
  537. }
  538. __finally
  539. {
  540. FreeCommStruct(CommStruct);
  541. }
  542. SendEvent(INFINITE);
  543. CommStruct = GetCommStruct();
  544. try
  545. {
  546. FLimitedOutput = (CommStruct->InitEvent.OutputType == FILE_TYPE_CHAR);
  547. }
  548. __finally
  549. {
  550. FreeCommStruct(CommStruct);
  551. }
  552. }
  553. //---------------------------------------------------------------------------
  554. bool __fastcall TExternalConsole::LimitedOutput()
  555. {
  556. return FLimitedOutput;
  557. }
  558. //---------------------------------------------------------------------------
  559. void __fastcall TExternalConsole::WaitBeforeExit()
  560. {
  561. // noop
  562. }
  563. //---------------------------------------------------------------------------
  564. class TNullConsole : public TConsole
  565. {
  566. public:
  567. __fastcall TNullConsole();
  568. virtual void __fastcall Print(AnsiString Str, bool FromBeginning = false);
  569. virtual bool __fastcall Input(AnsiString & Str, bool Echo, unsigned int Timer);
  570. virtual int __fastcall Choice(AnsiString Options, int Cancel, int Break,
  571. int Timeouted, unsigned int Timer);
  572. virtual bool __fastcall PendingAbort();
  573. virtual void __fastcall SetTitle(AnsiString Title);
  574. virtual bool __fastcall LimitedOutput();
  575. virtual void __fastcall WaitBeforeExit();
  576. };
  577. //---------------------------------------------------------------------------
  578. __fastcall TNullConsole::TNullConsole()
  579. {
  580. }
  581. //---------------------------------------------------------------------------
  582. void __fastcall TNullConsole::Print(AnsiString /*Str*/, bool /*FromBeginning*/)
  583. {
  584. // noop
  585. }
  586. //---------------------------------------------------------------------------
  587. bool __fastcall TNullConsole::Input(AnsiString & /*Str*/, bool /*Echo*/,
  588. unsigned int /*Timer*/)
  589. {
  590. return false;
  591. }
  592. //---------------------------------------------------------------------------
  593. int __fastcall TNullConsole::Choice(AnsiString /*Options*/, int /*Cancel*/,
  594. int Break, int /*Timeouted*/, unsigned int /*Timer*/)
  595. {
  596. return Break;
  597. }
  598. //---------------------------------------------------------------------------
  599. bool __fastcall TNullConsole::PendingAbort()
  600. {
  601. return false;
  602. }
  603. //---------------------------------------------------------------------------
  604. void __fastcall TNullConsole::SetTitle(AnsiString /*Title*/)
  605. {
  606. // noop
  607. }
  608. //---------------------------------------------------------------------------
  609. bool __fastcall TNullConsole::LimitedOutput()
  610. {
  611. return false;
  612. }
  613. //---------------------------------------------------------------------------
  614. void __fastcall TNullConsole::WaitBeforeExit()
  615. {
  616. assert(false);
  617. // noop
  618. }
  619. //---------------------------------------------------------------------------
  620. class TConsoleRunner
  621. {
  622. public:
  623. TConsoleRunner(TConsole * Console);
  624. ~TConsoleRunner();
  625. int __fastcall Run(const AnsiString Session, TOptions * Options,
  626. TStrings * ScriptCommands);
  627. void __fastcall ShowException(Exception * E);
  628. protected:
  629. bool __fastcall Input(AnsiString & Str, bool Echo, unsigned int Timer);
  630. inline void __fastcall Print(const AnsiString & Str, bool FromBeginning = false);
  631. inline void __fastcall PrintLine(const AnsiString & Str);
  632. inline void __fastcall PrintMessage(const AnsiString & Str);
  633. void __fastcall UpdateTitle();
  634. inline void __fastcall NotifyAbort();
  635. inline bool __fastcall Aborted(bool AllowCompleteAbort = true);
  636. private:
  637. TManagementScript * FScript;
  638. TConsole * FConsole;
  639. TSynchronizeController FSynchronizeController;
  640. int FLastProgressLen;
  641. bool FSynchronizeAborted;
  642. bool FCommandError;
  643. bool FBatchScript;
  644. bool FAborted;
  645. TTimer * Timer;
  646. void __fastcall ScriptPrint(TScript * Script, const AnsiString Str);
  647. void __fastcall ScriptPrintProgress(TScript * Script, bool First, const AnsiString Str);
  648. void __fastcall ScriptInput(TScript * Script, const AnsiString Prompt, AnsiString & Str);
  649. void __fastcall ScriptTerminalPromptUser(TTerminal * Terminal,
  650. TPromptKind Kind, AnsiString Name, AnsiString Instructions, TStrings * Prompts,
  651. TStrings * Results, bool & Result, void * Arg);
  652. void __fastcall ScriptShowExtendedException(TTerminal * Terminal,
  653. Exception * E, void * Arg);
  654. void __fastcall ScriptTerminalQueryUser(TObject * Sender, const AnsiString Query,
  655. TStrings * MoreMessages, int Answers, const TQueryParams * Params, int & Answer,
  656. TQueryType QueryType, void * Arg);
  657. void __fastcall ScriptQueryCancel(TScript * Script, bool & Cancel);
  658. void __fastcall SynchronizeControllerAbort(TObject * Sender, bool Close);
  659. void __fastcall SynchronizeControllerLog(TSynchronizeController * Controller,
  660. TSynchronizeLogEntry Entry, const AnsiString Message);
  661. void __fastcall ScriptSynchronizeStartStop(TScript * Script,
  662. const AnsiString LocalDirectory, const AnsiString RemoteDirectory,
  663. const TCopyParamType & CopyParam, int SynchronizeParams);
  664. void __fastcall SynchronizeControllerSynchronize(TSynchronizeController * Sender,
  665. const AnsiString LocalDirectory, const AnsiString RemoteDirectory,
  666. const TCopyParamType & CopyParam, const TSynchronizeParamType & Params,
  667. TSynchronizeChecklist ** Checklist, TSynchronizeOptions * Options, bool Full);
  668. void __fastcall SynchronizeControllerSynchronizeInvalid(TSynchronizeController * Sender,
  669. const AnsiString Directory, const AnsiString ErrorStr);
  670. void __fastcall SynchronizeControllerTooManyDirectories(TSynchronizeController * Sender,
  671. int & MaxDirectories);
  672. unsigned int InputTimeout();
  673. void __fastcall TimerTimer(TObject * Sender);
  674. };
  675. //---------------------------------------------------------------------------
  676. TConsoleRunner::TConsoleRunner(TConsole * Console) :
  677. FSynchronizeController(&SynchronizeControllerSynchronize,
  678. &SynchronizeControllerSynchronizeInvalid,
  679. &SynchronizeControllerTooManyDirectories)
  680. {
  681. FConsole = Console;
  682. FLastProgressLen = 0;
  683. FScript = NULL;
  684. FAborted = false;
  685. FBatchScript = false;
  686. Timer = new TTimer(Application);
  687. Timer->OnTimer = TimerTimer;
  688. Timer->Interval = 1000;
  689. Timer->Enabled = true;
  690. }
  691. //---------------------------------------------------------------------------
  692. TConsoleRunner::~TConsoleRunner()
  693. {
  694. delete Timer;
  695. }
  696. //---------------------------------------------------------------------------
  697. void __fastcall TConsoleRunner::TimerTimer(TObject * /*Sender*/)
  698. {
  699. // sole presence of timer causes message to be dispatched,
  700. // hence breaks the loops
  701. }
  702. //---------------------------------------------------------------------------
  703. unsigned int TConsoleRunner::InputTimeout()
  704. {
  705. return (FScript->Batch != TScript::BatchOff ? BATCH_INPUT_TIMEOUT : 0);
  706. }
  707. //---------------------------------------------------------------------------
  708. void __fastcall TConsoleRunner::ScriptInput(TScript * /*Script*/,
  709. const AnsiString Prompt, AnsiString & Str)
  710. {
  711. Print(Prompt);
  712. if (!Input(Str, true, InputTimeout()))
  713. {
  714. Abort();
  715. }
  716. }
  717. //---------------------------------------------------------------------------
  718. void __fastcall TConsoleRunner::Print(const AnsiString & Str, bool FromBeginning)
  719. {
  720. if (FLastProgressLen > 0)
  721. {
  722. FConsole->Print("\n" + Str, FromBeginning);
  723. FLastProgressLen = 0;
  724. }
  725. else
  726. {
  727. FConsole->Print(Str, FromBeginning);
  728. }
  729. }
  730. //---------------------------------------------------------------------------
  731. void __fastcall TConsoleRunner::PrintLine(const AnsiString & Str)
  732. {
  733. Print(Str + "\n");
  734. }
  735. //---------------------------------------------------------------------------
  736. void __fastcall TConsoleRunner::PrintMessage(const AnsiString & Str)
  737. {
  738. PrintLine(
  739. StringReplace(StringReplace(Str.TrimRight(), "\n\n", "\n", TReplaceFlags() << rfReplaceAll),
  740. "\n \n", "\n", TReplaceFlags() << rfReplaceAll));
  741. }
  742. //---------------------------------------------------------------------------
  743. void __fastcall TConsoleRunner::NotifyAbort()
  744. {
  745. if (FBatchScript)
  746. {
  747. FAborted = true;
  748. }
  749. }
  750. //---------------------------------------------------------------------------
  751. bool __fastcall TConsoleRunner::Aborted(bool AllowCompleteAbort)
  752. {
  753. bool Result;
  754. if (FAborted)
  755. {
  756. Result = true;
  757. }
  758. else
  759. {
  760. Result = FConsole->PendingAbort();
  761. if (Result)
  762. {
  763. PrintLine(LoadStr(USER_TERMINATED));
  764. if (AllowCompleteAbort)
  765. {
  766. NotifyAbort();
  767. }
  768. }
  769. }
  770. return Result;
  771. }
  772. //---------------------------------------------------------------------------
  773. void __fastcall TConsoleRunner::ScriptPrint(TScript * /*Script*/,
  774. const AnsiString Str)
  775. {
  776. Print(Str);
  777. }
  778. //---------------------------------------------------------------------------
  779. void __fastcall TConsoleRunner::ScriptPrintProgress(TScript * /*Script*/,
  780. bool First, const AnsiString Str)
  781. {
  782. AnsiString S = Str;
  783. if (First && (FLastProgressLen > 0))
  784. {
  785. S = "\n" + S;
  786. }
  787. else if (S.Length() < FLastProgressLen)
  788. {
  789. int Padding = FLastProgressLen - S.Length();
  790. S += AnsiString::StringOfChar(' ', Padding) +
  791. AnsiString::StringOfChar('\b', Padding);
  792. }
  793. FConsole->Print(S, true);
  794. FLastProgressLen = Str.Length();
  795. }
  796. //---------------------------------------------------------------------------
  797. void __fastcall TConsoleRunner::ScriptTerminalPromptUser(TTerminal * /*Terminal*/,
  798. TPromptKind /*Kind*/, AnsiString Name, AnsiString Instructions, TStrings * Prompts,
  799. TStrings * Results, bool & Result, void * /*Arg*/)
  800. {
  801. if (!Instructions.IsEmpty())
  802. {
  803. PrintLine(Instructions);
  804. }
  805. for (int Index = 0; Index < Prompts->Count; Index++)
  806. {
  807. AnsiString Prompt = Prompts->Strings[Index];
  808. if (!Prompt.IsEmpty() && (Prompt[Prompt.Length()] != ' '))
  809. {
  810. Prompt += ' ';
  811. }
  812. int P = Prompt.Pos('&');
  813. if (P > 0)
  814. {
  815. Prompt.Delete(P, 1);
  816. }
  817. Print(Prompt);
  818. AnsiString AResult = Results->Strings[Index]; // useless
  819. Result = Input(AResult, bool(Prompts->Objects[Index]), InputTimeout());
  820. Results->Strings[Index] = AResult;
  821. }
  822. }
  823. //---------------------------------------------------------------------------
  824. void __fastcall TConsoleRunner::ScriptShowExtendedException(
  825. TTerminal * /*Terminal*/, Exception * E, void * /*Arg*/)
  826. {
  827. ShowException(E);
  828. }
  829. //---------------------------------------------------------------------------
  830. void __fastcall TConsoleRunner::ScriptTerminalQueryUser(TObject * /*Sender*/,
  831. const AnsiString Query, TStrings * MoreMessages, int Answers,
  832. const TQueryParams * Params, int & Answer, TQueryType /*QueryType*/,
  833. void * /*Arg*/)
  834. {
  835. AnsiString AQuery = Query;
  836. unsigned int Timer = 0;
  837. unsigned int Timeout = 0;
  838. int TimeoutA = 0;
  839. if (Params != NULL)
  840. {
  841. if (Params->Timeout > 0)
  842. {
  843. assert(Params->Timer == 0);
  844. Timeout = Params->Timeout;
  845. TimeoutA = Params->TimeoutAnswer;
  846. }
  847. if (Params->Timer > 0)
  848. {
  849. assert(Params->Timeout == 0);
  850. Timer = Params->Timer;
  851. if (Params->TimerAnswers > 0)
  852. {
  853. Answers = Params->TimerAnswers;
  854. }
  855. if (!Params->TimerMessage.IsEmpty())
  856. {
  857. AQuery = Params->TimerMessage;
  858. }
  859. }
  860. if (FLAGSET(Params->Params, qpFatalAbort))
  861. {
  862. AQuery = FMTLOAD(WARN_FATAL_ERROR, (AQuery));
  863. }
  864. }
  865. int AAnswers = Answers;
  866. PrintMessage(AQuery);
  867. if ((MoreMessages != NULL) && (MoreMessages->Count > 0))
  868. {
  869. PrintMessage(MoreMessages->Text);
  870. }
  871. static const int MaxButtonCount = 15;
  872. int Buttons[MaxButtonCount];
  873. AnsiString Captions[MaxButtonCount];
  874. TNotifyEvent OnClicks[MaxButtonCount];
  875. int ButtonCount = 0;
  876. #define ADD_BUTTON(TYPE, CAPTION) \
  877. if (FLAGSET(AAnswers, qa ## TYPE)) \
  878. { \
  879. assert(ButtonCount < MaxButtonCount); \
  880. Captions[ButtonCount] = CAPTION; \
  881. Buttons[ButtonCount] = qa ## TYPE; \
  882. OnClicks[ButtonCount] = NULL; \
  883. ButtonCount++; \
  884. AAnswers -= qa ## TYPE; \
  885. }
  886. #define ADD_BUTTON_RES(TYPE) ADD_BUTTON(TYPE, LoadResourceString(&_SMsgDlg ## TYPE));
  887. ADD_BUTTON_RES(Yes);
  888. ADD_BUTTON_RES(No);
  889. ADD_BUTTON_RES(OK);
  890. ADD_BUTTON_RES(Cancel);
  891. ADD_BUTTON_RES(Abort);
  892. ADD_BUTTON_RES(Retry);
  893. ADD_BUTTON_RES(Ignore);
  894. // to keep the same order as for GUI message box
  895. ADD_BUTTON(Skip, LoadStr(SKIP_BUTTON));
  896. ADD_BUTTON_RES(All);
  897. ADD_BUTTON_RES(NoToAll);
  898. ADD_BUTTON_RES(YesToAll);
  899. ADD_BUTTON_RES(Help);
  900. #undef ADD_BUTTON_RES
  901. #undef ADD_BUTTON
  902. USEDPARAM(AAnswers);
  903. assert(AAnswers == 0);
  904. assert(ButtonCount > 0);
  905. if ((Params != NULL) && (Params->Aliases != NULL))
  906. {
  907. for (int bi = 0; bi < ButtonCount; bi++)
  908. {
  909. for (unsigned int ai = 0; ai < Params->AliasesCount; ai++)
  910. {
  911. if (static_cast<int>(Params->Aliases[ai].Button) == Buttons[bi])
  912. {
  913. Captions[bi] = Params->Aliases[ai].Alias;
  914. OnClicks[bi] = Params->Aliases[ai].OnClick;
  915. break;
  916. }
  917. }
  918. }
  919. }
  920. AnsiString Accels;
  921. for (int Index = 0; Index < ButtonCount; Index++)
  922. {
  923. AnsiString & Caption = Captions[Index];
  924. int P = Caption.Pos('&');
  925. if ((P > 0) && (P < Caption.Length()))
  926. {
  927. char Accel = AnsiUpperCase(Caption)[P + 1];
  928. if (Accels.Pos(Accel) > 0)
  929. {
  930. Caption.Delete(P, 1);
  931. Accels += ' ';
  932. }
  933. else
  934. {
  935. Accels += Accel;
  936. }
  937. }
  938. else
  939. {
  940. Accels += ' ';
  941. }
  942. }
  943. assert(Accels.Length() == ButtonCount);
  944. int NumberAccel = 0;
  945. int CancelA = CancelAnswer(Answers);
  946. int CancelIndex;
  947. int AbortA = AbortAnswer(Answers);
  948. int AbortIndex;
  949. int ContinueA = ContinueAnswer(Answers);
  950. int ContinueIndex;
  951. int TimeoutIndex = 0;
  952. for (int Index = 0; Index < ButtonCount; Index++)
  953. {
  954. AnsiString & Caption = Captions[Index];
  955. if (Accels[Index + 1] == ' ')
  956. {
  957. for (int Index2 = 1; Index2 <= Caption.Length(); Index2++)
  958. {
  959. char C = AnsiUpperCase(Caption)[Index2];
  960. if ((C >= 'A') && (C <= 'Z') && (Accels.Pos(C) == 0))
  961. {
  962. Caption.Insert("&", Index2);
  963. Accels[Index + 1] = C;
  964. break;
  965. }
  966. }
  967. }
  968. if (Accels[Index + 1] == ' ')
  969. {
  970. for (int Index2 = 1; Index2 <= Caption.Length(); Index2++)
  971. {
  972. char C = AnsiUpperCase(Caption)[Index2];
  973. if ((C != ' ') && (Accels.Pos(C) == 0))
  974. {
  975. Caption.Insert("&", Index2);
  976. Accels[Index + 1] = C;
  977. break;
  978. }
  979. }
  980. }
  981. if (Accels[Index + 1] == ' ')
  982. {
  983. NumberAccel++;
  984. assert(NumberAccel <= 9);
  985. Caption = FORMAT("&%d%s", (NumberAccel, Caption));
  986. Accels[Index + 1] = Caption[2];
  987. }
  988. if (Buttons[Index] == CancelA)
  989. {
  990. CancelIndex = Index + 1;
  991. }
  992. if (Buttons[Index] == AbortA)
  993. {
  994. AbortIndex = Index + 1;
  995. }
  996. if (Buttons[Index] == ContinueA)
  997. {
  998. ContinueIndex = Index + 1;
  999. }
  1000. if (Buttons[Index] == ContinueA)
  1001. {
  1002. ContinueIndex = Index + 1;
  1003. }
  1004. if (Buttons[Index] == TimeoutA)
  1005. {
  1006. TimeoutIndex = Index + 1;
  1007. }
  1008. }
  1009. assert(Accels.Pos(' ') == 0);
  1010. bool Timeouting = (Timeout > 0);
  1011. do
  1012. {
  1013. Answer = 0;
  1014. int AnswerIndex;
  1015. bool Retry;
  1016. do
  1017. {
  1018. Retry = false;
  1019. AnsiString Output;
  1020. for (int i = 0; i < ButtonCount; i++)
  1021. {
  1022. if (i > 0)
  1023. {
  1024. Output += ", ";
  1025. }
  1026. AnsiString Caption = Captions[i];
  1027. int P = Caption.Pos('&');
  1028. assert(P >= 0);
  1029. Caption[P] = '(';
  1030. Caption.Insert(")", P + 2);
  1031. if (i + 1 == TimeoutIndex)
  1032. {
  1033. assert(Timeouting);
  1034. Caption = FMTLOAD(TIMEOUT_BUTTON, (Caption, int(Timeout / 1000)));
  1035. }
  1036. Output += Caption;
  1037. }
  1038. Output += ": ";
  1039. // note that length of string may decrease over time due to number of
  1040. // seconds length, but supposing it decreases by one character at time
  1041. // at most, we do not mind as the prompt is terminated with space
  1042. Print(Output, true);
  1043. if (!Timeouting && (FScript->Batch == TScript::BatchContinue))
  1044. {
  1045. AnswerIndex = ContinueIndex;
  1046. }
  1047. else if (!Timeouting && (FScript->Batch != TScript::BatchOff))
  1048. {
  1049. AnswerIndex = AbortIndex;
  1050. }
  1051. else if (Timeouting && (Timeout < 1000))
  1052. {
  1053. AnswerIndex = TimeoutIndex;
  1054. }
  1055. else
  1056. {
  1057. AnswerIndex = FConsole->Choice(Accels, CancelIndex, -1, -2,
  1058. (Timeouting ? 1000 : Timer));
  1059. if (AnswerIndex == -1)
  1060. {
  1061. NotifyAbort();
  1062. AnswerIndex = AbortIndex;
  1063. }
  1064. else if (AnswerIndex == -2)
  1065. {
  1066. if (Timeouting)
  1067. {
  1068. assert(Timeout >= 1000);
  1069. Timeout -= 1000;
  1070. Retry = true;
  1071. }
  1072. else
  1073. {
  1074. assert((Params != NULL) && (Params->TimerEvent != NULL));
  1075. if ((Params != NULL) && (Params->TimerEvent != NULL))
  1076. {
  1077. unsigned int AAnswer = 0;
  1078. Params->TimerEvent(AAnswer);
  1079. if (AAnswer != 0)
  1080. {
  1081. Answer = AAnswer;
  1082. }
  1083. else
  1084. {
  1085. Retry = true;
  1086. }
  1087. }
  1088. }
  1089. }
  1090. }
  1091. }
  1092. while (Retry);
  1093. if (Answer == 0)
  1094. {
  1095. assert((AnswerIndex >= 1) && (AnswerIndex <= Accels.Length()));
  1096. AnsiString AnswerCaption = Captions[AnswerIndex - 1];
  1097. int P = AnswerCaption.Pos("&");
  1098. assert(P >= 0);
  1099. AnswerCaption.Delete(P, 1);
  1100. PrintLine(AnswerCaption);
  1101. if (OnClicks[AnswerIndex - 1] != NULL)
  1102. {
  1103. OnClicks[AnswerIndex - 1](NULL);
  1104. }
  1105. else
  1106. {
  1107. Answer = Buttons[AnswerIndex - 1];
  1108. }
  1109. }
  1110. else
  1111. {
  1112. PrintLine("");
  1113. }
  1114. }
  1115. while (Answer == 0);
  1116. if (Answer == AbortA)
  1117. {
  1118. FCommandError = true;
  1119. }
  1120. }
  1121. //---------------------------------------------------------------------------
  1122. void __fastcall TConsoleRunner::ScriptQueryCancel(TScript * /*Script*/, bool & Cancel)
  1123. {
  1124. if (Aborted())
  1125. {
  1126. Cancel = true;
  1127. }
  1128. }
  1129. //---------------------------------------------------------------------------
  1130. void __fastcall TConsoleRunner::ScriptSynchronizeStartStop(TScript * /*Script*/,
  1131. const AnsiString LocalDirectory, const AnsiString RemoteDirectory,
  1132. const TCopyParamType & CopyParam, int SynchronizeParams)
  1133. {
  1134. TSynchronizeParamType Params;
  1135. Params.LocalDirectory = LocalDirectory;
  1136. Params.RemoteDirectory = RemoteDirectory;
  1137. Params.Params = SynchronizeParams;
  1138. Params.Options = soRecurse;
  1139. FSynchronizeController.StartStop(Application, true, Params,
  1140. CopyParam, NULL, SynchronizeControllerAbort, NULL,
  1141. SynchronizeControllerLog);
  1142. try
  1143. {
  1144. FSynchronizeAborted = false;
  1145. while (!FSynchronizeAborted && !Aborted(false))
  1146. {
  1147. Application->HandleMessage();
  1148. FScript->Terminal->Idle();
  1149. }
  1150. }
  1151. __finally
  1152. {
  1153. FSynchronizeController.StartStop(Application, false, Params,
  1154. CopyParam, NULL, SynchronizeControllerAbort, NULL,
  1155. SynchronizeControllerLog);
  1156. }
  1157. }
  1158. //---------------------------------------------------------------------------
  1159. void __fastcall TConsoleRunner::SynchronizeControllerLog(
  1160. TSynchronizeController * /*Controller*/, TSynchronizeLogEntry /*Entry*/,
  1161. const AnsiString Message)
  1162. {
  1163. PrintLine(Message);
  1164. }
  1165. //---------------------------------------------------------------------------
  1166. void __fastcall TConsoleRunner::SynchronizeControllerAbort(TObject * /*Sender*/,
  1167. bool /*Close*/)
  1168. {
  1169. FSynchronizeAborted = true;
  1170. }
  1171. //---------------------------------------------------------------------------
  1172. void __fastcall TConsoleRunner::SynchronizeControllerSynchronize(
  1173. TSynchronizeController * /*Sender*/, const AnsiString LocalDirectory,
  1174. const AnsiString RemoteDirectory, const TCopyParamType & CopyParam,
  1175. const TSynchronizeParamType & Params, TSynchronizeChecklist ** Checklist,
  1176. TSynchronizeOptions * /*Options*/, bool Full)
  1177. {
  1178. if (!Full)
  1179. {
  1180. FScript->Synchronize(LocalDirectory, RemoteDirectory, CopyParam,
  1181. Params.Params, Checklist);
  1182. }
  1183. }
  1184. //---------------------------------------------------------------------------
  1185. void __fastcall TConsoleRunner::SynchronizeControllerSynchronizeInvalid(
  1186. TSynchronizeController * /*Sender*/, const AnsiString Directory, const AnsiString ErrorStr)
  1187. {
  1188. if (!Directory.IsEmpty())
  1189. {
  1190. PrintLine(FMTLOAD(WATCH_ERROR_DIRECTORY, (Directory)));
  1191. }
  1192. else
  1193. {
  1194. PrintLine(LoadStr(WATCH_ERROR_GENERAL));
  1195. }
  1196. if (!ErrorStr.IsEmpty())
  1197. {
  1198. PrintLine(ErrorStr);
  1199. }
  1200. }
  1201. //---------------------------------------------------------------------------
  1202. void __fastcall TConsoleRunner::SynchronizeControllerTooManyDirectories(
  1203. TSynchronizeController * /*Sender*/, int & MaxDirectories)
  1204. {
  1205. if (Aborted())
  1206. {
  1207. Abort();
  1208. }
  1209. if (MaxDirectories < GUIConfiguration->MaxWatchDirectories)
  1210. {
  1211. MaxDirectories = GUIConfiguration->MaxWatchDirectories;
  1212. }
  1213. else
  1214. {
  1215. MaxDirectories *= 2;
  1216. }
  1217. }
  1218. //---------------------------------------------------------------------------
  1219. void __fastcall TConsoleRunner::ShowException(Exception * E)
  1220. {
  1221. if (!E->Message.IsEmpty() &&
  1222. (dynamic_cast<EAbort *>(E) == NULL))
  1223. {
  1224. FCommandError = true;
  1225. PrintMessage(TranslateExceptionMessage(E));
  1226. ExtException * EE = dynamic_cast<ExtException *>(E);
  1227. if ((EE != NULL) && (EE->MoreMessages != NULL))
  1228. {
  1229. PrintMessage(EE->MoreMessages->Text);
  1230. }
  1231. }
  1232. }
  1233. //---------------------------------------------------------------------------
  1234. bool __fastcall TConsoleRunner::Input(AnsiString & Str, bool Echo, unsigned int Timeout)
  1235. {
  1236. bool Result = FConsole->Input(Str, Echo, Timeout);
  1237. if (Result)
  1238. {
  1239. while (!Str.IsEmpty() &&
  1240. ((Str[Str.Length()] == '\n') || (Str[Str.Length()] == '\r')))
  1241. {
  1242. Str.SetLength(Str.Length() - 1);
  1243. }
  1244. }
  1245. else
  1246. {
  1247. NotifyAbort();
  1248. }
  1249. return Result;
  1250. }
  1251. //---------------------------------------------------------------------------
  1252. int __fastcall TConsoleRunner::Run(const AnsiString Session, TOptions * Options,
  1253. TStrings * ScriptCommands)
  1254. {
  1255. bool AnyError = false;
  1256. try
  1257. {
  1258. FScript = new TManagementScript(StoredSessions, FConsole->LimitedOutput());
  1259. try
  1260. {
  1261. FScript->CopyParam = GUIConfiguration->DefaultCopyParam;
  1262. FScript->SynchronizeParams = GUIConfiguration->SynchronizeParams;
  1263. FScript->OnPrint = ScriptPrint;
  1264. FScript->OnPrintProgress = ScriptPrintProgress;
  1265. FScript->OnInput = ScriptInput;
  1266. FScript->OnTerminalPromptUser = ScriptTerminalPromptUser;
  1267. FScript->OnShowExtendedException = ScriptShowExtendedException;
  1268. FScript->OnTerminalQueryUser = ScriptTerminalQueryUser;
  1269. FScript->OnQueryCancel = ScriptQueryCancel;
  1270. FScript->OnSynchronizeStartStop = ScriptSynchronizeStartStop;
  1271. UpdateTitle();
  1272. // everything until the first manually entered command is "batch"
  1273. // (including opening session from command line and script file)
  1274. FBatchScript = true;
  1275. if (!Session.IsEmpty())
  1276. {
  1277. FScript->Connect(Session, Options, false);
  1278. }
  1279. int ScriptPos = 0;
  1280. bool Result;
  1281. do
  1282. {
  1283. UpdateTitle();
  1284. AnsiString Command;
  1285. if ((ScriptCommands != NULL) && (ScriptPos < ScriptCommands->Count))
  1286. {
  1287. Result = true;
  1288. Command = ScriptCommands->Strings[ScriptPos];
  1289. ScriptPos++;
  1290. }
  1291. else
  1292. {
  1293. // no longer batch
  1294. FBatchScript = false;
  1295. Print("winscp> ");
  1296. Result = Input(Command, true, 0);
  1297. }
  1298. if (Result)
  1299. {
  1300. FCommandError = false;
  1301. FScript->Command(ExpandEnvironmentVariables(Command));
  1302. if (FCommandError)
  1303. {
  1304. AnyError = true;
  1305. if (FScript->Batch == TScript::BatchAbort)
  1306. {
  1307. Result = false;
  1308. }
  1309. }
  1310. if (FScript->Terminal != NULL)
  1311. {
  1312. FScript->Terminal->Idle();
  1313. }
  1314. }
  1315. }
  1316. while (Result && FScript->Continue && !Aborted());
  1317. }
  1318. __finally
  1319. {
  1320. delete FScript;
  1321. FScript = NULL;
  1322. }
  1323. }
  1324. catch(Exception & E)
  1325. {
  1326. ShowException(&E);
  1327. AnyError = true;
  1328. }
  1329. return AnyError ? RESULT_ANY_ERROR : RESULT_SUCCESS;
  1330. }
  1331. //---------------------------------------------------------------------------
  1332. void __fastcall TConsoleRunner::UpdateTitle()
  1333. {
  1334. AnsiString NewTitle;
  1335. if (FScript->Terminal != NULL)
  1336. {
  1337. NewTitle = FMTLOAD(APP_CAPTION, (FScript->Terminal->SessionData->SessionName, AppName));
  1338. }
  1339. else
  1340. {
  1341. NewTitle = AppName;
  1342. }
  1343. FConsole->SetTitle(NewTitle);
  1344. }
  1345. //---------------------------------------------------------------------------
  1346. void __fastcall LoadScriptFromFile(AnsiString FileName, TStrings * Lines)
  1347. {
  1348. AnsiString UTFBOM = "\xEF\xBB\xBF";
  1349. Lines->LoadFromFile(FileName);
  1350. if ((Lines->Count > 0) &&
  1351. (Lines->Strings[0].SubString(1, UTFBOM.Length()) == UTFBOM))
  1352. {
  1353. Lines->Strings[0] = Lines->Strings[0].SubString(
  1354. UTFBOM.Length() + 1, Lines->Strings[0].Length() - UTFBOM.Length());
  1355. for (int Index = 0; Index < Lines->Count; Index++)
  1356. {
  1357. Lines->Strings[Index] = DecodeUTF(Lines->Strings[Index]);
  1358. }
  1359. }
  1360. }
  1361. //---------------------------------------------------------------------------
  1362. int __fastcall Console(bool Help)
  1363. {
  1364. TProgramParams * Params = TProgramParams::Instance();
  1365. int Result = 0;
  1366. TConsole * Console = NULL;
  1367. TConsoleRunner * Runner = NULL;
  1368. TStrings * ScriptCommands = new TStringList();
  1369. try
  1370. {
  1371. AnsiString ConsoleInstance;
  1372. if (Params->FindSwitch("consoleinstance", ConsoleInstance))
  1373. {
  1374. Console = new TExternalConsole(ConsoleInstance);
  1375. }
  1376. else if (Params->FindSwitch("Console") || Help)
  1377. {
  1378. Console = TOwnConsole::Instance();
  1379. }
  1380. else
  1381. {
  1382. Console = new TNullConsole();
  1383. }
  1384. if (Help)
  1385. {
  1386. AnsiString Usage = LoadStr(USAGE4, 10240);
  1387. AnsiString ExeBaseName = ChangeFileExt(ExtractFileName(Application->ExeName), "");
  1388. Usage = StringReplace(Usage, "%APP%", ExeBaseName,
  1389. TReplaceFlags() << rfReplaceAll << rfIgnoreCase);
  1390. AnsiString Copyright = StringReplace(LoadStr(WINSCP_COPYRIGHT), "©", "(c)",
  1391. TReplaceFlags() << rfReplaceAll << rfIgnoreCase);
  1392. Usage = FORMAT(Usage, (Configuration->VersionStr, Copyright));
  1393. Console->Print(Usage);
  1394. Console->WaitBeforeExit();
  1395. }
  1396. else
  1397. {
  1398. Runner = new TConsoleRunner(Console);
  1399. try
  1400. {
  1401. AnsiString Value;
  1402. if (Params->FindSwitch("script", Value) && !Value.IsEmpty())
  1403. {
  1404. LoadScriptFromFile(Value, ScriptCommands);
  1405. }
  1406. Params->FindSwitch("command", ScriptCommands);
  1407. bool Url = false;
  1408. AnsiString Session;
  1409. if (Params->ParamCount >= 1)
  1410. {
  1411. Session = Params->Param[1];
  1412. }
  1413. bool DefaultsOnly;
  1414. delete StoredSessions->ParseUrl(Session, Params, DefaultsOnly,
  1415. puDecodeUrlChars, NULL, &Url);
  1416. if (Url || Params->FindSwitch("Unsafe"))
  1417. {
  1418. // prevent any automatic action when URL is provided on
  1419. // command-line (the check is duplicated in Execute())
  1420. if ((ScriptCommands->Count > 0) || Params->FindSwitch("Log"))
  1421. {
  1422. Console->Print(LoadStr(UNSAFE_ACTIONS_DISABLED) + "\n");
  1423. }
  1424. ScriptCommands->Clear();
  1425. }
  1426. else
  1427. {
  1428. AnsiString LogFile;
  1429. if (Params->FindSwitch("Log", LogFile))
  1430. {
  1431. Configuration->TemporaryLogging(LogFile);
  1432. }
  1433. }
  1434. Result = Runner->Run(Session, Params,
  1435. (ScriptCommands->Count > 0 ? ScriptCommands : NULL));
  1436. }
  1437. catch(Exception & E)
  1438. {
  1439. Runner->ShowException(&E);
  1440. Result = RESULT_ANY_ERROR;
  1441. }
  1442. }
  1443. }
  1444. __finally
  1445. {
  1446. delete Runner;
  1447. delete Console;
  1448. delete ScriptCommands;
  1449. }
  1450. return Result;
  1451. }