123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091 |
- //---------------------------------------------------------------------------
- #include <vcl.h>
- #pragma hdrstop
- #include <Common.h>
- #include <Exceptions.h>
- #include <Script.h>
- #include <CoreMain.h>
- #include <Terminal.h>
- #include <PuttyTools.h>
- #include <Queue.h>
- #include <Consts.hpp>
- #include "Console.h"
- #include "WinInterface.h"
- #include "ProgParams.h"
- #include "TextsWin.h"
- #include "TextsCore.h"
- #include "WinConfiguration.h"
- #include "SynchronizeController.h"
- #include "GUITools.h"
- enum { RESULT_SUCCESS = 0, RESULT_ANY_ERROR = 1 };
- //---------------------------------------------------------------------------
- #define WM_INTERUPT_IDLE (WM_WINSCP_USER + 3)
- #define BATCH_INPUT_TIMEOUT 10000
- //---------------------------------------------------------------------------
- #pragma package(smart_init)
- //---------------------------------------------------------------------------
- class TConsole
- {
- public:
- virtual __fastcall ~TConsole() {};
- virtual void __fastcall Print(UnicodeString Str, bool FromBeginning = false) = 0;
- virtual bool __fastcall Input(UnicodeString & Str, bool Echo, unsigned int Timer) = 0;
- virtual int __fastcall Choice(UnicodeString Options, int Cancel, int Break,
- int Timeouted, bool Timeouting, unsigned int Timer) = 0;
- virtual bool __fastcall PendingAbort() = 0;
- virtual void __fastcall SetTitle(UnicodeString Title) = 0;
- virtual bool __fastcall LimitedOutput() = 0;
- virtual bool __fastcall LiveOutput() = 0;
- virtual bool __fastcall NoInteractiveInput() = 0;
- virtual void __fastcall WaitBeforeExit() = 0;
- virtual bool __fastcall CommandLineOnly() = 0;
- virtual bool __fastcall WantsProgress() = 0;
- virtual void __fastcall Progress(const TScriptProgress & Progress) = 0;
- };
- //---------------------------------------------------------------------------
- class TOwnConsole : public TConsole
- {
- public:
- static TOwnConsole * __fastcall Instance();
- virtual void __fastcall Print(UnicodeString Str, bool FromBeginning = false);
- virtual bool __fastcall Input(UnicodeString & Str, bool Echo, unsigned int Timer);
- virtual int __fastcall Choice(UnicodeString Options, int Cancel, int Break,
- int Timeouted, bool Timeouting, unsigned int Timer);
- virtual bool __fastcall PendingAbort();
- virtual void __fastcall SetTitle(UnicodeString Title);
- virtual bool __fastcall LimitedOutput();
- virtual bool __fastcall LiveOutput();
- virtual bool __fastcall NoInteractiveInput();
- virtual void __fastcall WaitBeforeExit();
- virtual bool __fastcall CommandLineOnly();
- virtual bool __fastcall WantsProgress();
- virtual void __fastcall Progress(const TScriptProgress & Progress);
- protected:
- static TOwnConsole * FInstance;
- friend class TConsoleInputThread;
- __fastcall TOwnConsole();
- virtual __fastcall ~TOwnConsole();
- void __fastcall BreakInput();
- void __fastcall CancelInput();
- static BOOL WINAPI HandlerRoutine(DWORD CtrlType);
- void __fastcall WindowStateTimer(TObject * Sender);
- void __fastcall ProcessMessages();
- void __fastcall TrayIconClick(TObject * Sender);
- private:
- HANDLE FInput;
- HANDLE FOutput;
- HWND FConsoleWindow;
- TTimer * FWindowStateTimer;
- bool FMinimized;
- ::TTrayIcon * FTrayIcon;
- static std::auto_ptr<TCriticalSection> FSection;
- bool FPendingAbort;
- };
- //---------------------------------------------------------------------------
- TOwnConsole * TOwnConsole::FInstance = NULL;
- std::auto_ptr<TCriticalSection> TOwnConsole::FSection(new TCriticalSection());
- //---------------------------------------------------------------------------
- __fastcall TOwnConsole::TOwnConsole()
- {
- assert(FInstance == NULL);
- FInstance = this;
- AllocConsole();
- SetConsoleCtrlHandler(HandlerRoutine, true);
- FInput = GetStdHandle(STD_INPUT_HANDLE);
- FOutput = GetStdHandle(STD_OUTPUT_HANDLE);
- FPendingAbort = false;
- FConsoleWindow = NULL;
- FWindowStateTimer = NULL;
- FMinimized = false;
- FTrayIcon = new ::TTrayIcon(0);
- FTrayIcon->OnClick = TrayIconClick;
- if (WinConfiguration->MinimizeToTray)
- {
- HINSTANCE Kernel32 = GetModuleHandle(kernel32);
- typedef HWND WINAPI (* TGetConsoleWindow)();
- TGetConsoleWindow GetConsoleWindow =
- (TGetConsoleWindow)GetProcAddress(Kernel32, "GetConsoleWindow");
- if (GetConsoleWindow != NULL)
- {
- FConsoleWindow = GetConsoleWindow();
- if (FConsoleWindow != NULL)
- {
- FWindowStateTimer = new TTimer(Application);
- FWindowStateTimer->OnTimer = WindowStateTimer;
- FWindowStateTimer->Interval = 250;
- FWindowStateTimer->Enabled = true;
- }
- else
- {
- assert(false);
- }
- }
- }
- }
- //---------------------------------------------------------------------------
- __fastcall TOwnConsole::~TOwnConsole()
- {
- TGuard Guard(FSection.get());
- delete FTrayIcon;
- delete FWindowStateTimer;
- // deliberatelly do not remove ConsoleCtrlHandler as it causes
- // failures while exiting
- FreeConsole();
- assert(FInstance == this);
- FInstance = NULL;
- }
- //---------------------------------------------------------------------------
- TOwnConsole * __fastcall TOwnConsole::Instance()
- {
- return new TOwnConsole();
- }
- //---------------------------------------------------------------------------
- void __fastcall TOwnConsole::WindowStateTimer(TObject * /*Sender*/)
- {
- assert(FConsoleWindow != NULL);
- WINDOWPLACEMENT Placement;
- memset(&Placement, 0, sizeof(Placement));
- Placement.length = sizeof(Placement);
- if (GetWindowPlacement(FConsoleWindow, &Placement))
- {
- bool Minimized = (Placement.showCmd == SW_SHOWMINIMIZED);
- if (FMinimized != Minimized)
- {
- FMinimized = Minimized;
- if (FMinimized && WinConfiguration->MinimizeToTray)
- {
- FTrayIcon->Visible = true;
- ShowWindow(FConsoleWindow, SW_HIDE);
- }
- else
- {
- FTrayIcon->Visible = false;
- ShowWindow(FConsoleWindow, SW_SHOW);
- }
- }
- }
- else
- {
- assert(false);
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TOwnConsole::ProcessMessages()
- {
- // as of now, there's no point doing this unless we have icon tray
- // (i.e. we need to monitor window state and eventually process tray icon messages)
- if (FWindowStateTimer != NULL)
- {
- assert(WinConfiguration->MinimizeToTray);
- Application->ProcessMessages();
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TOwnConsole::TrayIconClick(TObject * /*Sender*/)
- {
- assert(FConsoleWindow != NULL);
- SetForegroundWindow(FConsoleWindow);
- ShowWindow(FConsoleWindow, SW_RESTORE);
- }
- //---------------------------------------------------------------------------
- void __fastcall TOwnConsole::BreakInput()
- {
- FlushConsoleInputBuffer(FInput);
- INPUT_RECORD InputRecord;
- memset(&InputRecord, 0, sizeof(InputRecord));
- InputRecord.EventType = KEY_EVENT;
- InputRecord.Event.KeyEvent.bKeyDown = true;
- InputRecord.Event.KeyEvent.wRepeatCount = 1;
- InputRecord.Event.KeyEvent.uChar.UnicodeChar = L'\r';
- unsigned long Written;
- // this assertion occasionally fails (when console is being exited)
- CHECK(WriteConsoleInput(FInput, &InputRecord, 1, &Written));
- assert(Written == 1);
- CancelInput();
- }
- //---------------------------------------------------------------------------
- void __fastcall TOwnConsole::CancelInput()
- {
- FPendingAbort = true;
- PostMessage(Application->Handle, WM_INTERUPT_IDLE, 0, 0);
- }
- //---------------------------------------------------------------------------
- BOOL WINAPI TOwnConsole::HandlerRoutine(DWORD CtrlType)
- {
- if ((CtrlType == CTRL_C_EVENT) || (CtrlType == CTRL_BREAK_EVENT))
- {
- {
- TGuard Guard(FSection.get());
- // just to be real thread-safe
- if (FInstance != NULL)
- {
- FInstance->CancelInput();
- }
- }
- return true;
- }
- else
- {
- return false;
- }
- }
- //---------------------------------------------------------------------------
- bool __fastcall TOwnConsole::PendingAbort()
- {
- if (FPendingAbort)
- {
- FPendingAbort = false;
- return true;
- }
- else
- {
- return FPendingAbort;
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TOwnConsole::Print(UnicodeString Str, bool FromBeginning)
- {
- if (FromBeginning)
- {
- CONSOLE_SCREEN_BUFFER_INFO BufferInfo;
- GetConsoleScreenBufferInfo(FOutput, &BufferInfo);
- BufferInfo.dwCursorPosition.X = 0;
- SetConsoleCursorPosition(FOutput, BufferInfo.dwCursorPosition);
- }
- unsigned long Written;
- bool Result = WriteConsole(FOutput, Str.c_str(), Str.Length(), &Written, NULL);
- assert(Result);
- USEDPARAM(Result);
- assert(Str.Length() == static_cast<long>(Written));
- ProcessMessages();
- }
- //---------------------------------------------------------------------------
- class TConsoleInputThread : public TSimpleThread
- {
- public:
- __fastcall TConsoleInputThread(HANDLE Input, UnicodeString & Str, bool & Result) :
- FInput(Input),
- FStr(Str),
- FResult(Result)
- {
- }
- virtual __fastcall ~TConsoleInputThread()
- {
- Close();
- }
- protected:
- virtual void __fastcall Execute()
- {
- unsigned long Read;
- FStr.SetLength(10240);
- FResult = ReadConsole(FInput, FStr.c_str(), FStr.Length(), &Read, NULL);
- assert(FResult);
- FStr.SetLength(Read);
- }
- virtual void __fastcall Terminate()
- {
- TOwnConsole::FInstance->BreakInput();
- }
- private:
- HANDLE FInput;
- UnicodeString & FStr;
- bool & FResult;
- };
- //---------------------------------------------------------------------------
- bool __fastcall TOwnConsole::Input(UnicodeString & Str, bool Echo, unsigned int Timer)
- {
- unsigned long PrevMode, NewMode;
- GetConsoleMode(FInput, &PrevMode);
- NewMode = PrevMode | ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT;
- if (Echo)
- {
- NewMode |= ENABLE_ECHO_INPUT;
- }
- else
- {
- NewMode &= ~ENABLE_ECHO_INPUT;
- }
- SetConsoleMode(FInput, NewMode);
- bool Result = false;
- try
- {
- {
- TConsoleInputThread InputThread(FInput, Str, Result);
- InputThread.Start();
- double Start = Now();
- double End = Start + double(Timer)/MSecsPerDay;
- while (!InputThread.IsFinished() &&
- ((Timer == 0) || (double(Now()) < End)))
- {
- ProcessMessages();
- InputThread.WaitFor(50);
- }
- }
- if (FPendingAbort || !Echo)
- {
- Print(L"\n");
- }
- if (FPendingAbort || (Str.Length() == 0))
- {
- Result = false;
- FPendingAbort = false;
- }
- }
- __finally
- {
- SetConsoleMode(FInput, PrevMode);
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- int __fastcall TOwnConsole::Choice(UnicodeString Options, int Cancel, int Break,
- int Timeouted, bool /*Timeouting*/, unsigned int Timer)
- {
- unsigned int ATimer = Timer;
- int Result = 0;
- unsigned long PrevMode, NewMode;
- GetConsoleMode(FInput, &PrevMode);
- NewMode = (PrevMode | ENABLE_PROCESSED_INPUT) & ~(ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT);
- SetConsoleMode(FInput, NewMode);
- try
- {
- do
- {
- unsigned long Read;
- INPUT_RECORD Record;
- if ((PeekConsoleInput(FInput, &Record, 1, &Read) != 0) &&
- (Read == 1))
- {
- if ((ReadConsoleInput(FInput, &Record, 1, &Read) != 0) &&
- (Read == 1))
- {
- if (PendingAbort())
- {
- Result = Break;
- }
- else if ((Record.EventType == KEY_EVENT) &&
- Record.Event.KeyEvent.bKeyDown)
- {
- wchar_t C = AnsiUpperCase(Record.Event.KeyEvent.uChar.UnicodeChar)[1];
- if (C == 27)
- {
- Result = Cancel;
- }
- else if ((Options.Pos(C) > 0) &&
- FLAGCLEAR(Record.Event.KeyEvent.dwControlKeyState,
- LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED | LEFT_ALT_PRESSED |
- RIGHT_ALT_PRESSED))
- {
- Result = Options.Pos(C);
- }
- }
- }
- }
- if (Result == 0)
- {
- unsigned int TimerSlice = 50;
- Sleep(TimerSlice);
- if (Timer > 0)
- {
- if (ATimer > TimerSlice)
- {
- ATimer -= TimerSlice;
- }
- else
- {
- Result = Timeouted;
- }
- }
- }
- ProcessMessages();
- }
- while (Result == 0);
- }
- __finally
- {
- SetConsoleMode(FInput, PrevMode);
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- void __fastcall TOwnConsole::SetTitle(UnicodeString Title)
- {
- FTrayIcon->Hint = Title;
- SetConsoleTitle(Title.c_str());
- }
- //---------------------------------------------------------------------------
- bool __fastcall TOwnConsole::LimitedOutput()
- {
- return true;
- }
- //---------------------------------------------------------------------------
- bool __fastcall TOwnConsole::LiveOutput()
- {
- return true;
- }
- //---------------------------------------------------------------------------
- bool __fastcall TOwnConsole::NoInteractiveInput()
- {
- return false;
- }
- //---------------------------------------------------------------------------
- void __fastcall TOwnConsole::WaitBeforeExit()
- {
- unsigned long Read;
- INPUT_RECORD Record;
- while (true)
- {
- if (PeekConsoleInput(FInput, &Record, 1, &Read) && (Read == 1) &&
- ReadConsoleInput(FInput, &Record, 1, &Read) &&
- (Read == 1) && (Record.EventType == KEY_EVENT) &&
- (Record.Event.KeyEvent.uChar.UnicodeChar != 0) &&
- Record.Event.KeyEvent.bKeyDown)
- {
- break;
- }
- Sleep(50);
- ProcessMessages();
- }
- }
- //---------------------------------------------------------------------------
- bool __fastcall TOwnConsole::CommandLineOnly()
- {
- return false;
- }
- //---------------------------------------------------------------------------
- bool __fastcall TOwnConsole::WantsProgress()
- {
- return false;
- }
- //---------------------------------------------------------------------------
- void __fastcall TOwnConsole::Progress(const TScriptProgress & /*Progress*/)
- {
- FAIL;
- }
- //---------------------------------------------------------------------------
- class TExternalConsole : public TConsole
- {
- public:
- __fastcall TExternalConsole(const UnicodeString Instance, bool NoInteractiveInput);
- virtual __fastcall ~TExternalConsole();
- virtual void __fastcall Print(UnicodeString Str, bool FromBeginning = false);
- virtual bool __fastcall Input(UnicodeString & Str, bool Echo, unsigned int Timer);
- virtual int __fastcall Choice(UnicodeString Options, int Cancel, int Break,
- int Timeouted, bool Timeouting, unsigned int Timer);
- virtual bool __fastcall PendingAbort();
- virtual void __fastcall SetTitle(UnicodeString Title);
- virtual bool __fastcall LimitedOutput();
- virtual bool __fastcall LiveOutput();
- virtual bool __fastcall NoInteractiveInput();
- virtual void __fastcall WaitBeforeExit();
- virtual bool __fastcall CommandLineOnly();
- virtual bool __fastcall WantsProgress();
- virtual void __fastcall Progress(const TScriptProgress & Progress);
- private:
- bool FPendingAbort;
- HANDLE FRequestEvent;
- HANDLE FResponseEvent;
- HANDLE FCancelEvent;
- HANDLE FFileMapping;
- bool FLimitedOutput;
- bool FLiveOutput;
- bool FPipeOutput;
- bool FNoInteractiveInput;
- bool FWantsProgress;
- static const int PrintTimeout = 30000;
- inline TConsoleCommStruct * __fastcall GetCommStruct();
- inline void __fastcall FreeCommStruct(TConsoleCommStruct * CommStruct);
- inline void __fastcall SendEvent(int Timeout);
- void __fastcall Init();
- };
- //---------------------------------------------------------------------------
- __fastcall TExternalConsole::TExternalConsole(
- const UnicodeString Instance, bool NoInteractiveInput)
- {
- FRequestEvent = OpenEvent(EVENT_ALL_ACCESS, false,
- FORMAT(L"%s%s", (CONSOLE_EVENT_REQUEST, (Instance))).c_str());
- FResponseEvent = OpenEvent(EVENT_ALL_ACCESS, false,
- FORMAT(L"%s%s", (CONSOLE_EVENT_RESPONSE, (Instance))).c_str());
- FCancelEvent = OpenEvent(EVENT_ALL_ACCESS, false,
- FORMAT(L"%s%s", (CONSOLE_EVENT_CANCEL, (Instance))).c_str());
- FFileMapping = OpenFileMapping(FILE_MAP_ALL_ACCESS, false,
- FORMAT(L"%s%s", (CONSOLE_MAPPING, (Instance))).c_str());
- if ((FRequestEvent == NULL) || (FResponseEvent == NULL) || (FFileMapping == NULL))
- {
- throw Exception(LoadStr(EXTERNAL_CONSOLE_INIT_ERROR));
- }
- HINSTANCE Kernel32 = GetModuleHandle(kernel32);
- typedef HANDLE WINAPI (* TOpenJobObject)(DWORD DesiredAccess, BOOL InheritHandles, LPCTSTR Name);
- typedef HANDLE WINAPI (* TAssignProcessToJobObject)(HANDLE Job, HANDLE Process);
- TOpenJobObject OpenJobObject =
- (TOpenJobObject)GetProcAddress(Kernel32, "OpenJobObjectW");
- TAssignProcessToJobObject AssignProcessToJobObject =
- (TAssignProcessToJobObject)GetProcAddress(Kernel32, "AssignProcessToJobObject");
- if ((OpenJobObject != NULL) && (AssignProcessToJobObject != NULL))
- {
- HANDLE Job = OpenJobObject(JOB_OBJECT_ASSIGN_PROCESS, FALSE,
- FORMAT(L"%s%s", (CONSOLE_JOB, Instance)).c_str());
- if (Job != NULL)
- {
- AssignProcessToJobObject(Job, GetCurrentProcess());
- // winscp.com keeps the only reference to the job.
- // once it gets closed (because winscp.com if forcefully terminated),
- // we get terminated as well
- CloseHandle(Job);
- }
- }
- TConsoleCommStruct * CommStruct = GetCommStruct();
- try
- {
- if (CommStruct->Version != TConsoleCommStruct::CurrentVersion)
- {
- throw Exception(FMTLOAD(EXTERNAL_CONSOLE_INCOMPATIBLE, (CommStruct->Version)));
- }
- CommStruct->Version = TConsoleCommStruct::CurrentVersionConfirmed;
- }
- __finally
- {
- FreeCommStruct(CommStruct);
- }
- // to break application event loop regularly during "watching for changes"
- // to allow user to abort it
- SetTimer(Application->Handle, 1, 500, NULL);
- FNoInteractiveInput = NoInteractiveInput;
- Init();
- }
- //---------------------------------------------------------------------------
- __fastcall TExternalConsole::~TExternalConsole()
- {
- CloseHandle(FRequestEvent);
- CloseHandle(FResponseEvent);
- CloseHandle(FCancelEvent);
- CloseHandle(FFileMapping);
- KillTimer(Application->Handle, 1);
- }
- //---------------------------------------------------------------------------
- TConsoleCommStruct * __fastcall TExternalConsole::GetCommStruct()
- {
- TConsoleCommStruct * Result;
- Result = static_cast<TConsoleCommStruct*>(MapViewOfFile(FFileMapping,
- FILE_MAP_ALL_ACCESS, 0, 0, 0));
- if (Result == NULL)
- {
- throw Exception(LoadStr(CONSOLE_COMM_ERROR));
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- void __fastcall TExternalConsole::FreeCommStruct(TConsoleCommStruct * CommStruct)
- {
- UnmapViewOfFile(CommStruct);
- }
- //---------------------------------------------------------------------------
- void __fastcall TExternalConsole::SendEvent(int Timeout)
- {
- SetEvent(FRequestEvent);
- unsigned int Result = WaitForSingleObject(FResponseEvent, Timeout);
- if (Result != WAIT_OBJECT_0)
- {
- UnicodeString Message = LoadStr(CONSOLE_SEND_TIMEOUT);
- if (FPipeOutput)
- {
- Message = FORMAT("%s %s", (Message, LoadStr(CONSOLE_SEND_PIPE)));
- }
- throw Exception(Message);
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TExternalConsole::Print(UnicodeString Str, bool FromBeginning)
- {
- TConsoleCommStruct * CommStruct = GetCommStruct();
- try
- {
- if (Str.Length() >= static_cast<int>(LENOF(CommStruct->PrintEvent.Message)))
- {
- throw Exception(FMTLOAD(CONSOLE_PRINT_TOO_LONG, (Str.Length())));
- }
- CommStruct->Event = TConsoleCommStruct::PRINT;
- wcscpy(CommStruct->PrintEvent.Message, Str.c_str());
- CommStruct->PrintEvent.FromBeginning = FromBeginning;
- }
- __finally
- {
- FreeCommStruct(CommStruct);
- }
- SendEvent(PrintTimeout);
- }
- //---------------------------------------------------------------------------
- bool __fastcall TExternalConsole::Input(UnicodeString & Str, bool Echo, unsigned int Timer)
- {
- TConsoleCommStruct * CommStruct = GetCommStruct();
- try
- {
- CommStruct->Event = TConsoleCommStruct::INPUT;
- CommStruct->InputEvent.Echo = Echo;
- CommStruct->InputEvent.Result = false;
- CommStruct->InputEvent.Str[0] = L'\0';
- CommStruct->InputEvent.Timer = Timer;
- }
- __finally
- {
- FreeCommStruct(CommStruct);
- }
- SendEvent(INFINITE);
- bool Result;
- CommStruct = GetCommStruct();
- try
- {
- Result = CommStruct->InputEvent.Result;
- Str = CommStruct->InputEvent.Str;
- }
- __finally
- {
- FreeCommStruct(CommStruct);
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- int __fastcall TExternalConsole::Choice(UnicodeString Options, int Cancel, int Break,
- int Timeouted, bool Timeouting, unsigned int Timer)
- {
- TConsoleCommStruct * CommStruct = GetCommStruct();
- try
- {
- CommStruct->Event = TConsoleCommStruct::CHOICE;
- assert(Options.Length() < static_cast<int>(LENOF(CommStruct->ChoiceEvent.Options)));
- wcscpy(CommStruct->ChoiceEvent.Options, Options.c_str());
- CommStruct->ChoiceEvent.Cancel = Cancel;
- CommStruct->ChoiceEvent.Break = Break;
- CommStruct->ChoiceEvent.Result = Break;
- CommStruct->ChoiceEvent.Timeouted = Timeouted;
- CommStruct->ChoiceEvent.Timer = Timer;
- CommStruct->ChoiceEvent.Timeouting = Timeouting;
- }
- __finally
- {
- FreeCommStruct(CommStruct);
- }
- SendEvent(INFINITE);
- int Result;
- CommStruct = GetCommStruct();
- try
- {
- Result = CommStruct->ChoiceEvent.Result;
- }
- __finally
- {
- FreeCommStruct(CommStruct);
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- bool __fastcall TExternalConsole::PendingAbort()
- {
- return (WaitForSingleObject(FCancelEvent, 0) == WAIT_OBJECT_0);
- }
- //---------------------------------------------------------------------------
- void __fastcall TExternalConsole::SetTitle(UnicodeString Title)
- {
- TConsoleCommStruct * CommStruct = GetCommStruct();
- try
- {
- if (Title.Length() >= static_cast<int>(LENOF(CommStruct->TitleEvent.Title)))
- {
- throw Exception(FMTLOAD(CONSOLE_PRINT_TOO_LONG, (Title.Length())));
- }
- CommStruct->Event = TConsoleCommStruct::TITLE;
- wcscpy(CommStruct->TitleEvent.Title, Title.c_str());
- }
- __finally
- {
- FreeCommStruct(CommStruct);
- }
- SendEvent(INFINITE);
- }
- //---------------------------------------------------------------------------
- void __fastcall TExternalConsole::Init()
- {
- TConsoleCommStruct * CommStruct = GetCommStruct();
- try
- {
- CommStruct->Event = TConsoleCommStruct::INIT;
- CommStruct->InitEvent.WantsProgress = false;
- }
- __finally
- {
- FreeCommStruct(CommStruct);
- }
- SendEvent(INFINITE);
- CommStruct = GetCommStruct();
- try
- {
- FLimitedOutput = (CommStruct->InitEvent.OutputType == FILE_TYPE_CHAR);
- FLiveOutput =
- (CommStruct->InitEvent.OutputType != FILE_TYPE_DISK) &&
- (CommStruct->InitEvent.OutputType != FILE_TYPE_PIPE);
- FPipeOutput = (CommStruct->InitEvent.OutputType != FILE_TYPE_PIPE);
- FWantsProgress = CommStruct->InitEvent.WantsProgress;
- }
- __finally
- {
- FreeCommStruct(CommStruct);
- }
- }
- //---------------------------------------------------------------------------
- bool __fastcall TExternalConsole::LimitedOutput()
- {
- return FLimitedOutput;
- }
- //---------------------------------------------------------------------------
- bool __fastcall TExternalConsole::LiveOutput()
- {
- return FLiveOutput;
- }
- //---------------------------------------------------------------------------
- bool __fastcall TExternalConsole::NoInteractiveInput()
- {
- return FNoInteractiveInput;
- }
- //---------------------------------------------------------------------------
- void __fastcall TExternalConsole::WaitBeforeExit()
- {
- // noop
- }
- //---------------------------------------------------------------------------
- bool __fastcall TExternalConsole::CommandLineOnly()
- {
- return true;
- }
- //---------------------------------------------------------------------------
- bool __fastcall TExternalConsole::WantsProgress()
- {
- return FWantsProgress;
- }
- //---------------------------------------------------------------------------
- void __fastcall TExternalConsole::Progress(const TScriptProgress & Progress)
- {
- TConsoleCommStruct * CommStruct = GetCommStruct();
- try
- {
- CommStruct->Event = TConsoleCommStruct::PROGRESS;
- typedef TConsoleCommStruct::TProgressEvent TProgressEvent;
- TProgressEvent & ProgressEvent = CommStruct->ProgressEvent;
- switch (Progress.Operation)
- {
- case foCopy:
- case foMove:
- ProgressEvent.Operation = TProgressEvent::COPY;
- break;
- default:
- FAIL;
- }
- switch (Progress.Side)
- {
- case osLocal:
- ProgressEvent.Side = TProgressEvent::LOCAL;
- break;
- case osRemote:
- ProgressEvent.Side = TProgressEvent::REMOTE;
- break;
- default:
- FAIL;
- }
- wcsncpy(ProgressEvent.FileName, Progress.FileName.c_str(), LENOF(ProgressEvent.FileName));
- ProgressEvent.FileName[LENOF(ProgressEvent.FileName) - 1] = L'\0';
- wcsncpy(ProgressEvent.Directory, Progress.Directory.c_str(), LENOF(ProgressEvent.Directory));
- ProgressEvent.Directory[LENOF(ProgressEvent.Directory) - 1] = L'\0';
- ProgressEvent.OverallProgress = Progress.OverallProgress;
- ProgressEvent.FileProgress = Progress.FileProgress;
- ProgressEvent.CPS = Progress.CPS;
- }
- __finally
- {
- FreeCommStruct(CommStruct);
- }
- SendEvent(INFINITE);
- // nothing to read from response, just wait for it
- FreeCommStruct(GetCommStruct());
- }
- //---------------------------------------------------------------------------
- class TNullConsole : public TConsole
- {
- public:
- __fastcall TNullConsole();
- virtual void __fastcall Print(UnicodeString Str, bool FromBeginning = false);
- virtual bool __fastcall Input(UnicodeString & Str, bool Echo, unsigned int Timer);
- virtual int __fastcall Choice(UnicodeString Options, int Cancel, int Break,
- int Timeouted, bool Timeouting, unsigned int Timer);
- virtual bool __fastcall PendingAbort();
- virtual void __fastcall SetTitle(UnicodeString Title);
- virtual bool __fastcall LimitedOutput();
- virtual bool __fastcall LiveOutput();
- virtual bool __fastcall NoInteractiveInput();
- virtual void __fastcall WaitBeforeExit();
- virtual bool __fastcall CommandLineOnly();
- virtual bool __fastcall WantsProgress();
- virtual void __fastcall Progress(const TScriptProgress & Progress);
- };
- //---------------------------------------------------------------------------
- __fastcall TNullConsole::TNullConsole()
- {
- }
- //---------------------------------------------------------------------------
- void __fastcall TNullConsole::Print(UnicodeString /*Str*/, bool /*FromBeginning*/)
- {
- // noop
- }
- //---------------------------------------------------------------------------
- bool __fastcall TNullConsole::Input(UnicodeString & /*Str*/, bool /*Echo*/,
- unsigned int /*Timer*/)
- {
- return false;
- }
- //---------------------------------------------------------------------------
- int __fastcall TNullConsole::Choice(UnicodeString /*Options*/, int /*Cancel*/,
- int Break, int Timeouted, bool Timeouting, unsigned int Timer)
- {
- int Result;
- if (Timeouting)
- {
- Sleep(Timer);
- Result = Timeouted;
- }
- else
- {
- Result = Break;
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- bool __fastcall TNullConsole::PendingAbort()
- {
- return false;
- }
- //---------------------------------------------------------------------------
- void __fastcall TNullConsole::SetTitle(UnicodeString /*Title*/)
- {
- // noop
- }
- //---------------------------------------------------------------------------
- bool __fastcall TNullConsole::LimitedOutput()
- {
- return false;
- }
- //---------------------------------------------------------------------------
- bool __fastcall TNullConsole::LiveOutput()
- {
- return false;
- }
- //---------------------------------------------------------------------------
- bool __fastcall TNullConsole::NoInteractiveInput()
- {
- // do not matter, even if we return false,
- // it fails immediatelly afterwards in TNullConsole::Input
- return true;
- }
- //---------------------------------------------------------------------------
- void __fastcall TNullConsole::WaitBeforeExit()
- {
- assert(false);
- // noop
- }
- //---------------------------------------------------------------------------
- bool __fastcall TNullConsole::CommandLineOnly()
- {
- assert(false);
- return false;
- }
- //---------------------------------------------------------------------------
- bool __fastcall TNullConsole::WantsProgress()
- {
- return false;
- }
- //---------------------------------------------------------------------------
- void __fastcall TNullConsole::Progress(const TScriptProgress & /*Progress*/)
- {
- FAIL;
- }
- //---------------------------------------------------------------------------
- class TConsoleRunner
- {
- public:
- TConsoleRunner(TConsole * Console);
- ~TConsoleRunner();
- int __fastcall Run(const UnicodeString Session, TOptions * Options,
- TStrings * ScriptCommands, TStrings * ScriptParameters);
- void __fastcall ShowException(Exception * E);
- protected:
- bool __fastcall DoInput(UnicodeString & Str, bool Echo, unsigned int Timer,
- bool Interactive);
- void __fastcall Input(const UnicodeString Prompt, UnicodeString & Str,
- bool Echo, bool Interactive);
- inline void __fastcall Print(const UnicodeString & Str, bool FromBeginning = false);
- inline void __fastcall PrintLine(const UnicodeString & Str);
- inline void __fastcall PrintMessage(const UnicodeString & Str);
- void __fastcall UpdateTitle();
- inline void __fastcall NotifyAbort();
- inline bool __fastcall Aborted(bool AllowCompleteAbort = true);
- void __fastcall MasterPasswordPrompt();
- void __fastcall DoShowException(TTerminal * Terminal, Exception * E);
- private:
- TManagementScript * FScript;
- TConsole * FConsole;
- TSynchronizeController FSynchronizeController;
- int FLastProgressLen;
- bool FSynchronizeAborted;
- bool FCommandError;
- bool FBatchScript;
- bool FAborted;
- TTimer * Timer;
- void __fastcall ScriptPrint(TScript * Script, const UnicodeString Str);
- void __fastcall ScriptPrintProgress(TScript * Script, bool First, const UnicodeString Str);
- void __fastcall ScriptInput(TScript * Script, const UnicodeString Prompt, UnicodeString & Str);
- void __fastcall ScriptTerminalPromptUser(TTerminal * Terminal,
- TPromptKind Kind, UnicodeString Name, UnicodeString Instructions, TStrings * Prompts,
- TStrings * Results, bool & Result, void * Arg);
- void __fastcall ScriptShowExtendedException(TTerminal * Terminal,
- Exception * E, void * Arg);
- void __fastcall ScriptTerminalQueryUser(TObject * Sender, const UnicodeString Query,
- TStrings * MoreMessages, unsigned int Answers, const TQueryParams * Params, unsigned int & Answer,
- TQueryType QueryType, void * Arg);
- void __fastcall ScriptQueryCancel(TScript * Script, bool & Cancel);
- void __fastcall SynchronizeControllerAbort(TObject * Sender, bool Close);
- void __fastcall SynchronizeControllerLog(TSynchronizeController * Controller,
- TSynchronizeLogEntry Entry, const UnicodeString Message);
- void __fastcall ScriptSynchronizeStartStop(TScript * Script,
- const UnicodeString LocalDirectory, const UnicodeString RemoteDirectory,
- const TCopyParamType & CopyParam, int SynchronizeParams);
- void __fastcall SynchronizeControllerSynchronize(TSynchronizeController * Sender,
- const UnicodeString LocalDirectory, const UnicodeString RemoteDirectory,
- const TCopyParamType & CopyParam, const TSynchronizeParamType & Params,
- TSynchronizeChecklist ** Checklist, TSynchronizeOptions * Options, bool Full);
- void __fastcall SynchronizeControllerSynchronizeInvalid(TSynchronizeController * Sender,
- const UnicodeString Directory, const UnicodeString ErrorStr);
- void __fastcall SynchronizeControllerTooManyDirectories(TSynchronizeController * Sender,
- int & MaxDirectories);
- unsigned int InputTimeout();
- void __fastcall TimerTimer(TObject * Sender);
- UnicodeString ExpandCommand(UnicodeString Command, TStrings * ScriptParameters);
- void __fastcall Failed(bool & AnyError);
- void __fastcall ScriptProgress(TScript * Script, const TScriptProgress & Progress);
- void __fastcall ConfigurationChange(TObject * Sender);
- };
- //---------------------------------------------------------------------------
- TConsoleRunner::TConsoleRunner(TConsole * Console) :
- FSynchronizeController(&SynchronizeControllerSynchronize,
- &SynchronizeControllerSynchronizeInvalid,
- &SynchronizeControllerTooManyDirectories)
- {
- FConsole = Console;
- FLastProgressLen = 0;
- FScript = NULL;
- FAborted = false;
- FBatchScript = false;
- Timer = new TTimer(Application);
- Timer->OnTimer = TimerTimer;
- Timer->Interval = MSecsPerSec;
- Timer->Enabled = true;
- assert(WinConfiguration->OnMasterPasswordPrompt == NULL);
- WinConfiguration->OnMasterPasswordPrompt = MasterPasswordPrompt;
- assert(Configuration->OnChange == NULL);
- Configuration->OnChange = ConfigurationChange;
- }
- //---------------------------------------------------------------------------
- TConsoleRunner::~TConsoleRunner()
- {
- assert(WinConfiguration->OnMasterPasswordPrompt == MasterPasswordPrompt);
- WinConfiguration->OnMasterPasswordPrompt = NULL;
- assert(Configuration->OnChange == ConfigurationChange);
- Configuration->OnChange = NULL;
- delete Timer;
- }
- //---------------------------------------------------------------------------
- void __fastcall TConsoleRunner::TimerTimer(TObject * /*Sender*/)
- {
- // sole presence of timer causes message to be dispatched,
- // hence breaks the loops
- }
- //---------------------------------------------------------------------------
- unsigned int TConsoleRunner::InputTimeout()
- {
- return ((FScript != NULL) && (FScript->Batch != TScript::BatchOff) ? BATCH_INPUT_TIMEOUT : 0);
- }
- //---------------------------------------------------------------------------
- void __fastcall TConsoleRunner::Input(
- const UnicodeString Prompt, UnicodeString & Str, bool Echo, bool Interactive)
- {
- Print(Prompt);
- if (!DoInput(Str, Echo, InputTimeout(), Interactive))
- {
- Abort();
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TConsoleRunner::ScriptInput(TScript * /*Script*/,
- const UnicodeString Prompt, UnicodeString & Str)
- {
- Input(Prompt, Str, true, true);
- }
- //---------------------------------------------------------------------------
- void __fastcall TConsoleRunner::Print(const UnicodeString & Str, bool FromBeginning)
- {
- if (FLastProgressLen > 0)
- {
- FConsole->Print(L"\n" + Str, FromBeginning);
- FLastProgressLen = 0;
- }
- else
- {
- FConsole->Print(Str, FromBeginning);
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TConsoleRunner::PrintLine(const UnicodeString & Str)
- {
- Print(Str + L"\n");
- }
- //---------------------------------------------------------------------------
- void __fastcall TConsoleRunner::PrintMessage(const UnicodeString & Str)
- {
- UnicodeString Line =
- StringReplace(StringReplace(Str.TrimRight(), L"\n\n", L"\n", TReplaceFlags() << rfReplaceAll),
- L"\n \n", L"\n", TReplaceFlags() << rfReplaceAll);
- if (FScript != NULL)
- {
- // this also logs the message
- FScript->PrintLine(Line);
- }
- else
- {
- PrintLine(Line);
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TConsoleRunner::NotifyAbort()
- {
- if (FBatchScript)
- {
- FAborted = true;
- }
- }
- //---------------------------------------------------------------------------
- bool __fastcall TConsoleRunner::Aborted(bool AllowCompleteAbort)
- {
- bool Result;
- if (FAborted)
- {
- Result = true;
- }
- else
- {
- Result = FConsole->PendingAbort();
- if (Result)
- {
- PrintMessage(LoadStr(USER_TERMINATED));
- if (AllowCompleteAbort)
- {
- NotifyAbort();
- }
- }
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- void __fastcall TConsoleRunner::ScriptPrint(TScript * /*Script*/,
- const UnicodeString Str)
- {
- Print(Str);
- }
- //---------------------------------------------------------------------------
- void __fastcall TConsoleRunner::ScriptPrintProgress(TScript * /*Script*/,
- bool First, const UnicodeString Str)
- {
- UnicodeString S = Str;
- if (First && (FLastProgressLen > 0))
- {
- S = L"\n" + S;
- }
- else if (S.Length() < FLastProgressLen)
- {
- int Padding = FLastProgressLen - S.Length();
- S += UnicodeString::StringOfChar(L' ', Padding) +
- UnicodeString::StringOfChar(L'\b', Padding);
- }
- FConsole->Print(S, true);
- FLastProgressLen = Str.Length();
- }
- //---------------------------------------------------------------------------
- void __fastcall TConsoleRunner::ScriptTerminalPromptUser(TTerminal * /*Terminal*/,
- TPromptKind /*Kind*/, UnicodeString Name, UnicodeString Instructions, TStrings * Prompts,
- TStrings * Results, bool & Result, void * /*Arg*/)
- {
- if (!Instructions.IsEmpty())
- {
- PrintMessage(Instructions);
- }
- // if there are no prompts, success is default
- Result = true;
- for (int Index = 0; Index < Prompts->Count; Index++)
- {
- UnicodeString Prompt = Prompts->Strings[Index];
- if (!Prompt.IsEmpty() && (Prompt[Prompt.Length()] != L' '))
- {
- Prompt += L' ';
- }
- int P = Prompt.Pos(L'&');
- if (P > 0)
- {
- Prompt.Delete(P, 1);
- }
- Print(Prompt);
- UnicodeString AResult = Results->Strings[Index]; // useless
- bool Echo = FLAGSET(int(Prompts->Objects[Index]), pupEcho);
- Result = DoInput(AResult, Echo, InputTimeout(), true);
- Results->Strings[Index] = AResult;
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TConsoleRunner::ScriptShowExtendedException(
- TTerminal * Terminal, Exception * E, void * /*Arg*/)
- {
- DoShowException(Terminal, E);
- }
- //---------------------------------------------------------------------------
- void __fastcall TConsoleRunner::ScriptTerminalQueryUser(TObject * /*Sender*/,
- const UnicodeString Query, TStrings * MoreMessages, unsigned int Answers,
- const TQueryParams * Params, unsigned int & Answer, TQueryType /*QueryType*/,
- void * /*Arg*/)
- {
- UnicodeString AQuery = Query;
- unsigned int Timer = 0;
- unsigned int Timeout = 0;
- unsigned int TimeoutA = 0;
- unsigned int NoBatchA = 0;
- if (Params != NULL)
- {
- if (Params->Timeout > 0)
- {
- Timeout = Params->Timeout;
- TimeoutA = Params->TimeoutAnswer;
- }
- if (Params->Timer > 0)
- {
- Timer = Params->Timer;
- if (Params->TimerAnswers > 0)
- {
- Answers = Params->TimerAnswers;
- }
- if (!Params->TimerMessage.IsEmpty())
- {
- AQuery = Params->TimerMessage;
- }
- }
- if (FLAGSET(Params->Params, qpFatalAbort))
- {
- AQuery = FMTLOAD(WARN_FATAL_ERROR, (AQuery));
- }
- NoBatchA = Params->NoBatchAnswers;
- }
- unsigned int AAnswers = Answers;
- PrintMessage(AQuery);
- if ((MoreMessages != NULL) && (MoreMessages->Count > 0))
- {
- PrintMessage(MoreMessages->Text);
- }
- static const int MaxButtonCount = 15;
- unsigned int Buttons[MaxButtonCount];
- UnicodeString Captions[MaxButtonCount];
- TNotifyEvent OnClicks[MaxButtonCount];
- int ButtonCount = 0;
- #define ADD_BUTTON(TYPE, CAPTION) \
- if (FLAGSET(AAnswers, qa ## TYPE)) \
- { \
- assert(ButtonCount < MaxButtonCount); \
- Captions[ButtonCount] = CAPTION; \
- Buttons[ButtonCount] = qa ## TYPE; \
- OnClicks[ButtonCount] = NULL; \
- ButtonCount++; \
- AAnswers -= qa ## TYPE; \
- }
- #define ADD_BUTTON_RES(TYPE) ADD_BUTTON(TYPE, LoadResourceString(&_SMsgDlg ## TYPE));
- ADD_BUTTON_RES(Yes);
- ADD_BUTTON_RES(No);
- ADD_BUTTON_RES(OK);
- ADD_BUTTON_RES(Cancel);
- ADD_BUTTON_RES(Abort);
- ADD_BUTTON_RES(Retry);
- ADD_BUTTON_RES(Ignore);
- // to keep the same order as for GUI message box
- ADD_BUTTON(Skip, LoadStr(SKIP_BUTTON));
- ADD_BUTTON_RES(All);
- ADD_BUTTON_RES(NoToAll);
- ADD_BUTTON_RES(YesToAll);
- ADD_BUTTON_RES(Help);
- #undef ADD_BUTTON_RES
- #undef ADD_BUTTON
- USEDPARAM(AAnswers);
- assert(AAnswers == 0);
- assert(ButtonCount > 0);
- if ((Params != NULL) && (Params->Aliases != NULL))
- {
- for (int bi = 0; bi < ButtonCount; bi++)
- {
- for (unsigned int ai = 0; ai < Params->AliasesCount; ai++)
- {
- if (Params->Aliases[ai].Button == Buttons[bi])
- {
- Captions[bi] = Params->Aliases[ai].Alias;
- OnClicks[bi] = Params->Aliases[ai].OnClick;
- break;
- }
- }
- }
- }
- UnicodeString Accels;
- for (int Index = 0; Index < ButtonCount; Index++)
- {
- UnicodeString & Caption = Captions[Index];
- int P = Caption.Pos(L'&');
- if ((P > 0) && (P < Caption.Length()))
- {
- wchar_t Accel = AnsiUpperCase(Caption)[P + 1];
- if (Accels.Pos(Accel) > 0)
- {
- Caption.Delete(P, 1);
- Accels += L' ';
- }
- else
- {
- Accels += Accel;
- }
- }
- else
- {
- Accels += L' ';
- }
- }
- assert(Accels.Length() == ButtonCount);
- int NumberAccel = 0;
- unsigned int CancelA = CancelAnswer(Answers);
- int CancelIndex;
- unsigned int AbortA = AbortAnswer(Answers & ~NoBatchA);
- int AbortIndex;
- unsigned int ContinueA = ContinueAnswer(Answers & ~NoBatchA);
- int ContinueIndex;
- int TimeoutIndex = 0;
- for (int Index = 0; Index < ButtonCount; Index++)
- {
- UnicodeString & Caption = Captions[Index];
- if (Accels[Index + 1] == L' ')
- {
- for (int Index2 = 1; Index2 <= Caption.Length(); Index2++)
- {
- wchar_t C = AnsiUpperCase(Caption)[Index2];
- if ((C >= L'A') && (C <= L'Z') && (Accels.Pos(C) == 0))
- {
- Caption.Insert(L"&", Index2);
- Accels[Index + 1] = C;
- break;
- }
- }
- }
- if (Accels[Index + 1] == L' ')
- {
- for (int Index2 = 1; Index2 <= Caption.Length(); Index2++)
- {
- wchar_t C = AnsiUpperCase(Caption)[Index2];
- if ((C != L' ') && (Accels.Pos(C) == 0))
- {
- Caption.Insert(L"&", Index2);
- Accels[Index + 1] = C;
- break;
- }
- }
- }
- if (Accels[Index + 1] == L' ')
- {
- NumberAccel++;
- assert(NumberAccel <= 9);
- Caption = FORMAT(L"&%d%s", (NumberAccel, Caption));
- Accels[Index + 1] = Caption[2];
- }
- if (Buttons[Index] == CancelA)
- {
- CancelIndex = Index + 1;
- }
- if (Buttons[Index] == AbortA)
- {
- AbortIndex = Index + 1;
- }
- if (Buttons[Index] == ContinueA)
- {
- ContinueIndex = Index + 1;
- }
- if (Buttons[Index] == TimeoutA)
- {
- TimeoutIndex = Index + 1;
- }
- }
- assert(Accels.Pos(L' ') == 0);
- bool Timeouting = (Timeout > 0);
- bool FirstOutput = true;
- do
- {
- Answer = 0;
- int AnswerIndex;
- bool Retry;
- do
- {
- Retry = false;
- if (FirstOutput || FConsole->LiveOutput())
- {
- UnicodeString Output;
- for (int i = 0; i < ButtonCount; i++)
- {
- if (i > 0)
- {
- Output += L", ";
- }
- UnicodeString Caption = Captions[i];
- int P = Caption.Pos(L'&');
- assert(P >= 0);
- Caption[P] = L'(';
- Caption.Insert(L")", P + 2);
- if (i + 1 == TimeoutIndex)
- {
- assert(Timeouting);
- Caption = FMTLOAD(TIMEOUT_BUTTON, (Caption, int(Timeout / MSecsPerSec)));
- }
- Output += Caption;
- }
- Output += L": ";
- // note that length of string may decrease over time due to number of
- // seconds length, but supposing it decreases by one character at time
- // at most, we do not mind as the prompt is terminated with space
- // If output is not live (file or pipe), do not use 'from beginning'
- // as it means that the output is not actually stored until new line
- // is sent (and we will not [because we cannot] rewrite the output anyway)
- Print(Output, !FirstOutput);
- FirstOutput = false;
- }
- if (!Timeouting && (FScript->Batch == TScript::BatchContinue))
- {
- AnswerIndex = ContinueIndex;
- }
- else if (!Timeouting && (FScript->Batch != TScript::BatchOff))
- {
- AnswerIndex = AbortIndex;
- }
- else if (Timeouting && (Timeout < MSecsPerSec))
- {
- AnswerIndex = TimeoutIndex;
- }
- else
- {
- unsigned int ActualTimer =
- (Timeouting && ((Timer == 0) || (Timer > MSecsPerSec)) ? MSecsPerSec : Timer);
- AnswerIndex = FConsole->Choice(Accels, CancelIndex, -1, -2,
- Timeouting, ActualTimer);
- if (AnswerIndex == -1)
- {
- NotifyAbort();
- AnswerIndex = AbortIndex;
- }
- else if (AnswerIndex == -2)
- {
- if (Timeouting)
- {
- assert(Timeout >= MSecsPerSec);
- Timeout -= ActualTimer;
- Retry = true;
- }
- // this does not take Timer into account,
- // but as of now Timer is used for TSecureShell timeout prompt only,
- // where Timer is less than MSecsPerSec
- if (Timer > 0)
- {
- assert((Params != NULL) && (Params->TimerEvent != NULL));
- if ((Params != NULL) && (Params->TimerEvent != NULL))
- {
- unsigned int AAnswer = 0;
- Params->TimerEvent(AAnswer);
- if (AAnswer != 0)
- {
- Answer = AAnswer;
- Retry = false;
- }
- else
- {
- Retry = true;
- }
- }
- }
- }
- }
- }
- while (Retry);
- if (Answer == 0)
- {
- assert((AnswerIndex >= 1) && (AnswerIndex <= Accels.Length()));
- UnicodeString AnswerCaption = Captions[AnswerIndex - 1];
- int P = AnswerCaption.Pos(L"&");
- assert(P >= 0);
- AnswerCaption.Delete(P, 1);
- PrintLine(AnswerCaption);
- FirstOutput = true;
- if (OnClicks[AnswerIndex - 1] != NULL)
- {
- OnClicks[AnswerIndex - 1](NULL);
- }
- else
- {
- Answer = Buttons[AnswerIndex - 1];
- }
- }
- else
- {
- PrintLine(L"");
- }
- }
- while (Answer == 0);
- if ((Answer == AbortA) && FLAGCLEAR(Params->Params, qpIgnoreAbort))
- {
- if (FScript->Terminal != NULL)
- {
- TStrings * Messages = new TStringList();
- try
- {
- Messages->Add(Query);
- if (MoreMessages != NULL)
- {
- Messages->AddStrings(MoreMessages);
- }
- FScript->Terminal->ActionLog->AddFailure(Messages);
- }
- __finally
- {
- delete Messages;
- }
- }
- FCommandError = true;
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TConsoleRunner::ScriptQueryCancel(TScript * /*Script*/, bool & Cancel)
- {
- if (Aborted())
- {
- Cancel = true;
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TConsoleRunner::ScriptSynchronizeStartStop(TScript * /*Script*/,
- const UnicodeString LocalDirectory, const UnicodeString RemoteDirectory,
- const TCopyParamType & CopyParam, int SynchronizeParams)
- {
- TSynchronizeParamType Params;
- Params.LocalDirectory = LocalDirectory;
- Params.RemoteDirectory = RemoteDirectory;
- Params.Params = SynchronizeParams;
- Params.Options = soRecurse;
- FSynchronizeController.StartStop(Application, true, Params,
- CopyParam, NULL, SynchronizeControllerAbort, NULL,
- SynchronizeControllerLog);
- try
- {
- FSynchronizeAborted = false;
- while (!FSynchronizeAborted && !Aborted(false))
- {
- Application->HandleMessage();
- FScript->Terminal->Idle();
- }
- }
- __finally
- {
- FSynchronizeController.StartStop(Application, false, Params,
- CopyParam, NULL, SynchronizeControllerAbort, NULL,
- SynchronizeControllerLog);
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TConsoleRunner::ScriptProgress(TScript * /*Script*/, const TScriptProgress & Progress)
- {
- FConsole->Progress(Progress);
- }
- //---------------------------------------------------------------------------
- void __fastcall TConsoleRunner::SynchronizeControllerLog(
- TSynchronizeController * /*Controller*/, TSynchronizeLogEntry /*Entry*/,
- const UnicodeString Message)
- {
- PrintMessage(Message);
- }
- //---------------------------------------------------------------------------
- void __fastcall TConsoleRunner::SynchronizeControllerAbort(TObject * /*Sender*/,
- bool /*Close*/)
- {
- FSynchronizeAborted = true;
- }
- //---------------------------------------------------------------------------
- void __fastcall TConsoleRunner::SynchronizeControllerSynchronize(
- TSynchronizeController * /*Sender*/, const UnicodeString LocalDirectory,
- const UnicodeString RemoteDirectory, const TCopyParamType & CopyParam,
- const TSynchronizeParamType & Params, TSynchronizeChecklist ** Checklist,
- TSynchronizeOptions * /*Options*/, bool Full)
- {
- if (!Full)
- {
- FScript->Synchronize(LocalDirectory, RemoteDirectory, CopyParam,
- Params.Params, Checklist);
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TConsoleRunner::SynchronizeControllerSynchronizeInvalid(
- TSynchronizeController * /*Sender*/, const UnicodeString Directory, const UnicodeString ErrorStr)
- {
- if (!Directory.IsEmpty())
- {
- PrintMessage(FMTLOAD(WATCH_ERROR_DIRECTORY, (Directory)));
- }
- else
- {
- PrintMessage(LoadStr(WATCH_ERROR_GENERAL));
- }
- if (!ErrorStr.IsEmpty())
- {
- PrintMessage(ErrorStr);
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TConsoleRunner::SynchronizeControllerTooManyDirectories(
- TSynchronizeController * /*Sender*/, int & MaxDirectories)
- {
- if (Aborted())
- {
- Abort();
- }
- if (MaxDirectories < GUIConfiguration->MaxWatchDirectories)
- {
- MaxDirectories = GUIConfiguration->MaxWatchDirectories;
- }
- else
- {
- MaxDirectories *= 2;
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TConsoleRunner::ShowException(Exception * E)
- {
- DoShowException(NULL, E);
- }
- //---------------------------------------------------------------------------
- void __fastcall TConsoleRunner::DoShowException(TTerminal * Terminal, Exception * E)
- {
- if ((Terminal == NULL) && (FScript != NULL))
- {
- Terminal = FScript->Terminal;
- }
- UnicodeString Message;
- if (ExceptionMessage(E, Message))
- {
- FCommandError = true;
- PrintMessage(Message);
- ExtException * EE = dynamic_cast<ExtException *>(E);
- if ((EE != NULL) && (EE->MoreMessages != NULL))
- {
- PrintMessage(EE->MoreMessages->Text);
- }
- }
- TTerminal * LoggingTerminal = Terminal;
- TSecondaryTerminal * SecondaryTerminal = dynamic_cast<TSecondaryTerminal *>(LoggingTerminal);
- if (SecondaryTerminal != NULL)
- {
- LoggingTerminal = SecondaryTerminal->MainTerminal;
- }
- if (LoggingTerminal != NULL)
- {
- LoggingTerminal->ActionLog->AddFailure(E);
- }
- }
- //---------------------------------------------------------------------------
- bool __fastcall TConsoleRunner::DoInput(UnicodeString & Str, bool Echo,
- unsigned int Timeout, bool Interactive)
- {
- bool Result;
- if (Interactive && FConsole->NoInteractiveInput())
- {
- Result = false;
- }
- else
- {
- Result = FConsole->Input(Str, Echo, Timeout);
- }
- if (Result)
- {
- while (!Str.IsEmpty() &&
- ((Str[Str.Length()] == L'\n') || (Str[Str.Length()] == L'\r')))
- {
- Str.SetLength(Str.Length() - 1);
- }
- }
- else
- {
- NotifyAbort();
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- void __fastcall TConsoleRunner::MasterPasswordPrompt()
- {
- UnicodeString Password;
- Input(LoadStr(CONSOLE_MASTER_PASSWORD_PROMPT), Password, false, true);
- WinConfiguration->SetMasterPassword(Password);
- }
- //---------------------------------------------------------------------------
- UnicodeString TConsoleRunner::ExpandCommand(UnicodeString Command, TStrings * ScriptParameters)
- {
- assert(ScriptParameters != NULL);
- for (int Index = 0; Index < ScriptParameters->Count; Index++)
- {
- Command = StringReplace(Command, FORMAT(L"%%%d%%", (Index+1)),
- ScriptParameters->Strings[Index], TReplaceFlags() << rfReplaceAll);
- }
- Command = ExpandEnvironmentVariables(Command);
- return Command;
- }
- //---------------------------------------------------------------------------
- void __fastcall TConsoleRunner::Failed(bool & AnyError)
- {
- if (FScript != NULL)
- {
- FScript->Log(llMessage, L"Failed");
- }
- AnyError = true;
- }
- //---------------------------------------------------------------------------
- int __fastcall TConsoleRunner::Run(const UnicodeString Session, TOptions * Options,
- TStrings * ScriptCommands, TStrings * ScriptParameters)
- {
- int ExitCode;
- try
- {
- bool AnyError = false;
- try
- {
- FScript = new TManagementScript(StoredSessions, FConsole->LimitedOutput());
- FScript->CopyParam = GUIConfiguration->DefaultCopyParam;
- FScript->SynchronizeParams = GUIConfiguration->SynchronizeParams;
- FScript->WantsProgress = FConsole->WantsProgress();
- FScript->OnPrint = ScriptPrint;
- FScript->OnPrintProgress = ScriptPrintProgress;
- FScript->OnInput = ScriptInput;
- FScript->OnTerminalPromptUser = ScriptTerminalPromptUser;
- FScript->OnShowExtendedException = ScriptShowExtendedException;
- FScript->OnTerminalQueryUser = ScriptTerminalQueryUser;
- FScript->OnQueryCancel = ScriptQueryCancel;
- FScript->OnSynchronizeStartStop = ScriptSynchronizeStartStop;
- FScript->OnProgress = ScriptProgress;
- UpdateTitle();
- // everything until the first manually entered command is "batch"
- // (including opening session from command line and script file)
- FBatchScript = true;
- if (!Session.IsEmpty())
- {
- PrintMessage(LoadStr(SCRIPT_CMDLINE_SESSION));
- FScript->Connect(Session, Options, false);
- if (FCommandError)
- {
- Failed(AnyError);
- }
- }
- FScript->Groups = Options->SwitchValue(L"xmlgroups", true, false);
- int ScriptPos = 0;
- bool Result;
- do
- {
- UpdateTitle();
- UnicodeString Command;
- if ((ScriptCommands != NULL) && (ScriptPos < ScriptCommands->Count))
- {
- Result = true;
- Command = ScriptCommands->Strings[ScriptPos];
- ScriptPos++;
- }
- else
- {
- // no longer batch
- FBatchScript = false;
- Print(L"winscp> ");
- Result = DoInput(Command, true, 0, false);
- }
- if (Result)
- {
- FCommandError = false;
- FScript->Command(ExpandCommand(Command, ScriptParameters));
- if (FCommandError)
- {
- Failed(AnyError);
- if (FScript->Batch == TScript::BatchAbort)
- {
- Result = false;
- }
- }
- if (FScript->Terminal != NULL)
- {
- FScript->Terminal->Idle();
- }
- }
- }
- while (Result && FScript->Continue && !Aborted());
- }
- catch(Exception & E)
- {
- Failed(AnyError);
- ShowException(&E);
- }
- ExitCode = AnyError ? RESULT_ANY_ERROR : RESULT_SUCCESS;
- if (FScript != NULL)
- {
- FScript->Log(llMessage, FORMAT(L"Exit code: %d", (ExitCode)));
- }
- }
- __finally
- {
- delete FScript;
- FScript = NULL;
- }
- return ExitCode;
- }
- //---------------------------------------------------------------------------
- void __fastcall TConsoleRunner::UpdateTitle()
- {
- UnicodeString NewTitle;
- if (FScript->Terminal != NULL)
- {
- NewTitle = FMTLOAD(APP_CAPTION, (FScript->Terminal->SessionData->SessionName, AppName));
- }
- else
- {
- NewTitle = AppName;
- }
- FConsole->SetTitle(NewTitle);
- }
- //---------------------------------------------------------------------------
- void __fastcall TConsoleRunner::ConfigurationChange(TObject * /*Sender*/)
- {
- if (FScript != NULL)
- {
- FScript->ReflectSettings();
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall LoadScriptFromFile(UnicodeString FileName, TStrings * Lines)
- {
- Lines->LoadFromFile(FileName);
- }
- //---------------------------------------------------------------------------
- void __fastcall Usage(TConsole * Console)
- {
- UnicodeString Usage = LoadStr(USAGE8, 10240);
- UnicodeString ExeBaseName = ChangeFileExt(ExtractFileName(Application->ExeName), L"");
- Usage = StringReplace(Usage, L"%APP%", ExeBaseName,
- TReplaceFlags() << rfReplaceAll << rfIgnoreCase);
- UnicodeString Copyright =
- StringReplace(LoadStr(WINSCP_COPYRIGHT), "©", "(c)",
- TReplaceFlags() << rfReplaceAll << rfIgnoreCase);
- Usage = FORMAT(Usage, (Configuration->VersionStr, Copyright));
- TStrings * Lines = new TStringList();
- try
- {
- Lines->Text = Usage;
- for (int Index = 0; Index < Lines->Count; Index++)
- {
- bool Print = true;
- UnicodeString Line = Lines->Strings[Index];
- if ((Line.Length() >= 2) && (Line[2] == L':'))
- {
- switch (Line[1])
- {
- case L'G':
- Print = !Console->CommandLineOnly();
- break;
- case L'C':
- Print = Console->CommandLineOnly();
- break;
- case L'B':
- Print = true;
- break;
- default:
- assert(false);
- break;
- }
- Line.Delete(1, 2);
- }
- if (Print)
- {
- Console->Print(Line + L"\n");
- }
- }
- }
- __finally
- {
- delete Lines;
- }
- Console->WaitBeforeExit();
- }
- //---------------------------------------------------------------------------
- int __fastcall Console(bool Help)
- {
- TProgramParams * Params = TProgramParams::Instance();
- int Result = 0;
- TConsole * Console = NULL;
- TConsoleRunner * Runner = NULL;
- TStrings * ScriptCommands = new TStringList();
- TStrings * ScriptParameters = new TStringList();
- try
- {
- UnicodeString ConsoleInstance;
- if (Params->FindSwitch(L"consoleinstance", ConsoleInstance))
- {
- Configuration->Usage->Inc(L"ConsoleExternal");
- Console = new TExternalConsole(ConsoleInstance, Params->FindSwitch(L"nointeractiveinput"));
- }
- else if (Params->FindSwitch(L"Console") || Help)
- {
- Configuration->Usage->Inc(L"ConsoleOwn");
- Console = TOwnConsole::Instance();
- }
- else
- {
- Configuration->Usage->Inc(L"ConsoleNull");
- Console = new TNullConsole();
- }
- if (Help)
- {
- Usage(Console);
- }
- else
- {
- Runner = new TConsoleRunner(Console);
- try
- {
- UnicodeString Value;
- if (Params->FindSwitch(L"script", Value) && !Value.IsEmpty())
- {
- Configuration->Usage->Inc(L"ScriptFile");
- LoadScriptFromFile(Value, ScriptCommands);
- }
- Params->FindSwitch(L"command", ScriptCommands);
- if (ScriptCommands->Count > 0)
- {
- Configuration->Usage->Inc(L"ScriptCommands");
- }
- Params->FindSwitch(L"parameter", ScriptParameters);
- if (ScriptParameters->Count > 0)
- {
- Configuration->Usage->Inc(L"ScriptParameters");
- }
- bool Url = false;
- UnicodeString Session;
- if (Params->ParamCount >= 1)
- {
- Session = Params->Param[1];
- }
- bool DefaultsOnly;
- delete StoredSessions->ParseUrl(Session, Params, DefaultsOnly,
- NULL, &Url);
- if (Url || Params->FindSwitch(L"Unsafe"))
- {
- // prevent any automatic action when URL is provided on
- // command-line (the check is duplicated in Execute())
- if ((ScriptCommands->Count > 0) || Params->FindSwitch(L"Log") || Params->FindSwitch(L"XmlLog"))
- {
- Console->Print(LoadStr(UNSAFE_ACTIONS_DISABLED) + L"\n");
- }
- ScriptCommands->Clear();
- }
- else
- {
- UnicodeString LogFile;
- if (Params->FindSwitch(L"Log", LogFile))
- {
- Configuration->Usage->Inc(L"ScriptLog");
- Configuration->TemporaryLogging(LogFile);
- }
- if (Params->FindSwitch(L"XmlLog", LogFile))
- {
- Configuration->Usage->Inc(L"ScriptXmlLog");
- Configuration->TemporaryActionsLogging(LogFile);
- }
- }
- Result = Runner->Run(Session, Params,
- (ScriptCommands->Count > 0 ? ScriptCommands : NULL),
- ScriptParameters);
- }
- catch(Exception & E)
- {
- Runner->ShowException(&E);
- Result = RESULT_ANY_ERROR;
- }
- }
- }
- __finally
- {
- delete Runner;
- delete Console;
- delete ScriptCommands;
- delete ScriptParameters;
- }
- return Result;
- }
|