1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043 |
- //---------------------------------------------------------------------------
- #include <WinPCH.h>
- #pragma hdrstop
- #include <Script.h>
- #include <Terminal.h>
- #include <PuttyTools.h>
- #include <Queue.h>
- #include <HierarchicalStorage.h>
- #include "Console.h"
- #include "ProgParams.h"
- #include "SynchronizeController.h"
- #include "Setup.h"
- #include "FtpFileSystem.h"
- #include "SessionInfo.h"
- //---------------------------------------------------------------------------
- #define WM_INTERUPT_IDLE (WM_WINSCP_USER + 3)
- #define BATCH_INPUT_TIMEOUT 10000
- //---------------------------------------------------------------------------
- void TrimNewLine(UnicodeString & Str)
- {
- while (!Str.IsEmpty() &&
- ((Str[Str.Length()] == L'\n') || (Str[Str.Length()] == L'\r')))
- {
- Str.SetLength(Str.Length() - 1);
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TConsole::PrintLine(const UnicodeString & Str, bool Error)
- {
- Print(Str + L"\n", false, Error);
- };
- //---------------------------------------------------------------------------
- class TOwnConsole : public TConsole
- {
- public:
- static TOwnConsole * __fastcall Instance();
- virtual void __fastcall Print(UnicodeString Str, bool FromBeginning = false, bool Error = false);
- virtual bool __fastcall Input(UnicodeString & Str, bool Echo, unsigned int Timer);
- virtual int __fastcall Choice(
- UnicodeString Options, int Cancel, int Break, int Continue, int Timeouted, bool Timeouting, unsigned int Timer,
- UnicodeString Message);
- virtual bool __fastcall HasFlag(TConsoleFlag Flag) const;
- virtual bool __fastcall PendingAbort();
- virtual void __fastcall SetTitle(UnicodeString Title);
- virtual void __fastcall WaitBeforeExit();
- virtual void __fastcall Progress(TScriptProgress & Progress);
- virtual void __fastcall TransferOut(const unsigned char * Data, size_t Len);
- virtual size_t __fastcall TransferIn(unsigned char * Data, size_t Len);
- virtual UnicodeString __fastcall FinalLogMessage();
- 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::unique_ptr<TCriticalSection> FSection;
- bool FPendingAbort;
- };
- //---------------------------------------------------------------------------
- TOwnConsole * TOwnConsole::FInstance = NULL;
- std::unique_ptr<TCriticalSection> TOwnConsole::FSection(TraceInitPtr(new TCriticalSection()));
- //---------------------------------------------------------------------------
- __fastcall TOwnConsole::TOwnConsole()
- {
- DebugAssert(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)
- {
- FConsoleWindow = GetConsoleWindow();
- if (DebugAlwaysTrue(FConsoleWindow != NULL))
- {
- FWindowStateTimer = new TTimer(Application);
- FWindowStateTimer->OnTimer = WindowStateTimer;
- FWindowStateTimer->Interval = 250;
- FWindowStateTimer->Enabled = true;
- }
- }
- }
- //---------------------------------------------------------------------------
- __fastcall TOwnConsole::~TOwnConsole()
- {
- TGuard Guard(FSection.get());
- delete FTrayIcon;
- delete FWindowStateTimer;
- // deliberately do not remove ConsoleCtrlHandler as it causes
- // failures while exiting
- FreeConsole();
- DebugAssert(FInstance == this);
- FInstance = NULL;
- }
- //---------------------------------------------------------------------------
- TOwnConsole * __fastcall TOwnConsole::Instance()
- {
- return new TOwnConsole();
- }
- //---------------------------------------------------------------------------
- void __fastcall TOwnConsole::WindowStateTimer(TObject * /*Sender*/)
- {
- DebugAssert(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
- {
- DebugFail();
- }
- }
- //---------------------------------------------------------------------------
- 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)
- {
- DebugAssert(WinConfiguration->MinimizeToTray);
- Application->ProcessMessages();
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TOwnConsole::TrayIconClick(TObject * /*Sender*/)
- {
- DebugAssert(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)
- DebugCheck(WriteConsoleInput(FInput, &InputRecord, 1, &Written));
- DebugAssert(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, bool /*Error*/)
- {
- 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);
- DebugAssert(Result);
- DebugUsedParam(Result);
- DebugAssert(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);
- DebugAssert(FResult);
- FStr.SetLength(Read);
- TrimNewLine(FStr);
- }
- 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
- {
- {
- const int FirstKey = VK_LBUTTON; // 0x01
- const int LastKey = VK_OEM_CLEAR; // 0xFE
- // reset key state
- for (int Key = FirstKey; Key <= LastKey; Key++)
- {
- GetAsyncKeyState(Key);
- }
- TConsoleInputThread InputThread(FInput, Str, Result);
- InputThread.Start();
- double TimerD = double(Timer)/MSecsPerDay;
- double End = Now() + TimerD;
- while (!InputThread.IsFinished() &&
- ((Timer == 0) || (double(Now()) < End)))
- {
- ProcessMessages();
- InputThread.WaitFor(50);
- for (int Key = FirstKey; Key <= LastKey; Key++)
- {
- if ((GetAsyncKeyState(Key) & 0x01) != 0)
- {
- End = Now() + TimerD;
- // Finishing the loop nevertheless to reset state of all keys
- }
- }
- }
- }
- 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 /*Continue*/, int Timeouted, bool /*Timeouting*/, unsigned int Timer,
- UnicodeString DebugUsedArg(Message))
- {
- 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::HasFlag(TConsoleFlag Flag) const
- {
- switch (Flag)
- {
- case cfLimitedOutput:
- case cfLiveOutput:
- case cfInteractive:
- return true;
- case cfNoInteractiveInput:
- case cfCommandLineOnly:
- case cfWantsProgress:
- case cfStdOut:
- case cfStdIn:
- return false;
- default:
- DebugFail();
- 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();
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TOwnConsole::Progress(TScriptProgress & /*Progress*/)
- {
- DebugFail();
- }
- //---------------------------------------------------------------------------
- void __fastcall TOwnConsole::TransferOut(const unsigned char * DebugUsedArg(Data), size_t DebugUsedArg(Len))
- {
- DebugFail();
- }
- //---------------------------------------------------------------------------
- size_t __fastcall TOwnConsole::TransferIn(unsigned char * DebugUsedArg(Data), size_t DebugUsedArg(Len))
- {
- DebugFail();
- return 0;
- }
- //---------------------------------------------------------------------------
- UnicodeString __fastcall TOwnConsole::FinalLogMessage()
- {
- return UnicodeString();
- }
- //---------------------------------------------------------------------------
- typedef TConsoleCommStruct::TInitEvent::STDINOUT TStdInOutMode;
- //---------------------------------------------------------------------------
- class TExternalConsole : public TConsole
- {
- public:
- __fastcall TExternalConsole(const UnicodeString Instance, bool NoInteractiveInput, TStdInOutMode StdOut, TStdInOutMode StdIn);
- virtual __fastcall ~TExternalConsole();
- virtual void __fastcall Print(UnicodeString Str, bool FromBeginning = false, bool Error = false);
- virtual bool __fastcall Input(UnicodeString & Str, bool Echo, unsigned int Timer);
- virtual int __fastcall Choice(
- UnicodeString Options, int Cancel, int Break, int Continue, int Timeouted, bool Timeouting, unsigned int Timer,
- UnicodeString Message);
- virtual bool __fastcall HasFlag(TConsoleFlag Flag) const;
- virtual bool __fastcall PendingAbort();
- virtual void __fastcall SetTitle(UnicodeString Title);
- virtual void __fastcall WaitBeforeExit();
- virtual void __fastcall Progress(TScriptProgress & Progress);
- virtual void __fastcall TransferOut(const unsigned char * Data, size_t Len);
- virtual size_t __fastcall TransferIn(unsigned char * Data, size_t Len);
- virtual UnicodeString __fastcall FinalLogMessage();
- private:
- HANDLE FRequestEvent;
- HANDLE FResponseEvent;
- HANDLE FCancelEvent;
- HANDLE FFileMapping;
- bool FLimitedOutput;
- bool FLiveOutput;
- bool FPipeOutput;
- bool FNoInteractiveInput;
- TStdInOutMode FStdOut;
- TStdInOutMode FStdIn;
- bool FWantsProgress;
- bool FInteractive;
- unsigned int FMaxSend;
- // Particularly FTP calls TransferOut/In from other thread
- std::unique_ptr<TCriticalSection> FSection;
- inline TConsoleCommStruct * __fastcall GetCommStruct();
- inline void __fastcall FreeCommStruct(TConsoleCommStruct * CommStruct);
- inline void __fastcall SendEvent(int Timeout);
- void __fastcall Init();
- void __fastcall CheckHandle(HANDLE Handle, const UnicodeString & Desc);
- };
- //---------------------------------------------------------------------------
- __fastcall TExternalConsole::TExternalConsole(
- const UnicodeString Instance, bool NoInteractiveInput, TStdInOutMode StdOut, TStdInOutMode StdIn)
- {
- UnicodeString Name;
- Name = FORMAT(L"%s%s", (CONSOLE_EVENT_REQUEST, (Instance)));
- CheckHandle(FRequestEvent = OpenEvent(EVENT_ALL_ACCESS, false, Name.c_str()), L"Request event");
- Name = FORMAT(L"%s%s", (CONSOLE_EVENT_RESPONSE, (Instance)));
- CheckHandle(FResponseEvent = OpenEvent(EVENT_ALL_ACCESS, false, Name.c_str()), L"Response event");
- Name = FORMAT(L"%s%s", (CONSOLE_EVENT_CANCEL, (Instance)));
- CheckHandle(FCancelEvent = OpenEvent(EVENT_ALL_ACCESS, false, Name.c_str()), L"Cancel event");
- Name = FORMAT(L"%s%s", (CONSOLE_MAPPING, (Instance)));
- CheckHandle(FFileMapping = OpenFileMapping(FILE_MAP_ALL_ACCESS, false, Name.c_str()), L"File mapping");
- HANDLE Job = OpenJobObject(JOB_OBJECT_ASSIGN_PROCESS, FALSE,
- FORMAT(L"%s%s", (CONSOLE_JOB, Instance)).c_str());
- if (DebugAlwaysTrue(Job != NULL))
- {
- AssignProcessToJobObject(Job, GetCurrentProcess());
- // winscp.com/winscp.dll keeps the only reference to the job.
- // once it gets closed (because winscp.com is forcefully terminated),
- // we get terminated as well
- CloseHandle(Job);
- }
- FSection.reset(new TCriticalSection());
- 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;
- FStdOut = StdOut;
- FStdIn = StdIn;
- FMaxSend = 0;
- Init();
- }
- //---------------------------------------------------------------------------
- __fastcall TExternalConsole::~TExternalConsole()
- {
- CloseHandle(FRequestEvent);
- CloseHandle(FResponseEvent);
- CloseHandle(FCancelEvent);
- CloseHandle(FFileMapping);
- KillTimer(Application->Handle, 1);
- }
- //---------------------------------------------------------------------------
- void __fastcall TExternalConsole::CheckHandle(HANDLE Handle, const UnicodeString & Desc)
- {
- if (Handle == NULL)
- {
- throw ExtException(LoadStr(EXTERNAL_CONSOLE_INIT_ERROR), FORMAT(L"%s\n%s", (Desc, LastSysErrorMessage())));
- }
- }
- //---------------------------------------------------------------------------
- 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 Start = 0; // shut up
- if (Configuration->ActualLogProtocol >= 1)
- {
- Start = GetTickCount();
- }
- unsigned int Result = WaitForSingleObject(FResponseEvent, Timeout);
- if (Configuration->ActualLogProtocol >= 1)
- {
- unsigned int End = GetTickCount();
- unsigned int Duration = End - Start;
- FMaxSend = std::max(Duration, FMaxSend);
- }
- 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);
- }
- }
- //---------------------------------------------------------------------------
- UnicodeString __fastcall TExternalConsole::FinalLogMessage()
- {
- return FORMAT(L"Max roundtrip: %d", (static_cast<int>(FMaxSend)));
- }
- //---------------------------------------------------------------------------
- void __fastcall TExternalConsole::Print(UnicodeString Str, bool FromBeginning, bool Error)
- {
- TGuard Guard(FSection.get());
- // need to do at least one iteration, even when Str is empty (new line)
- do
- {
- TConsoleCommStruct * CommStruct = GetCommStruct();
- try
- {
- size_t MaxLen = LENOF(CommStruct->PrintEvent.Message) - 1;
- UnicodeString Piece = Str.SubString(1, MaxLen);
- Str.Delete(1, MaxLen);
- CommStruct->Event = TConsoleCommStruct::PRINT;
- wcscpy(CommStruct->PrintEvent.Message, Piece.c_str());
- CommStruct->PrintEvent.FromBeginning = FromBeginning;
- CommStruct->PrintEvent.Error = Error;
- // In the next iteration we need to append never overwrite.
- // Note that this won't work properly for disk/pipe outputs,
- // when the next line is also FromBeginning,
- // as !FromBeginning print effectively commits previous FromBeginning print.
- // On the other hand, FromBeginning print is always initiated by us,
- // and it's not likely we ever issue print over 10 KB.
- FromBeginning = false;
- }
- __finally
- {
- FreeCommStruct(CommStruct);
- }
- SendEvent(INFINITE);
- }
- while (!Str.IsEmpty());
- }
- //---------------------------------------------------------------------------
- bool __fastcall TExternalConsole::Input(UnicodeString & Str, bool Echo, unsigned int Timer)
- {
- TGuard Guard(FSection.get());
- 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;
- TrimNewLine(Str);
- }
- __finally
- {
- FreeCommStruct(CommStruct);
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- int __fastcall TExternalConsole::Choice(
- UnicodeString Options, int Cancel, int Break, int Continue, int Timeouted, bool Timeouting, unsigned int Timer,
- UnicodeString Message)
- {
- TGuard Guard(FSection.get());
- TConsoleCommStruct * CommStruct = GetCommStruct();
- try
- {
- CommStruct->Event = TConsoleCommStruct::CHOICE;
- DebugAssert(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.Continue = Continue;
- CommStruct->ChoiceEvent.Timeouted = Timeouted;
- CommStruct->ChoiceEvent.Timer = Timer;
- CommStruct->ChoiceEvent.Timeouting = Timeouting;
- size_t MaxLen = LENOF(CommStruct->ChoiceEvent.Message) - 1;
- Message = Message.SubString(1, MaxLen);
- wcscpy(CommStruct->ChoiceEvent.Message, Message.c_str());
- }
- __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)
- {
- TGuard Guard(FSection.get());
- TConsoleCommStruct * CommStruct = GetCommStruct();
- try
- {
- // Truncate to maximum allowed. Title over 10 KB won't fit to screen anyway
- Title = Title.SubString(1, LENOF(CommStruct->TitleEvent.Title) - 1);
- CommStruct->Event = TConsoleCommStruct::TITLE;
- wcscpy(CommStruct->TitleEvent.Title, Title.c_str());
- }
- __finally
- {
- FreeCommStruct(CommStruct);
- }
- SendEvent(INFINITE);
- }
- //---------------------------------------------------------------------------
- void __fastcall TExternalConsole::Init()
- {
- TGuard Guard(FSection.get());
- TConsoleCommStruct * CommStruct = GetCommStruct();
- try
- {
- CommStruct->Event = TConsoleCommStruct::INIT;
- CommStruct->InitEvent.WantsProgress = false;
- CommStruct->InitEvent.UseStdErr = (FStdOut != TConsoleCommStruct::TInitEvent::OFF);
- CommStruct->InitEvent.OutputFormat = FStdOut;
- CommStruct->InitEvent.InputFormat = FStdIn;
- }
- __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);
- FInteractive =
- (CommStruct->InitEvent.InputType != FILE_TYPE_DISK) &&
- (CommStruct->InitEvent.InputType != FILE_TYPE_PIPE);
- FWantsProgress = CommStruct->InitEvent.WantsProgress;
- }
- __finally
- {
- FreeCommStruct(CommStruct);
- }
- }
- //---------------------------------------------------------------------------
- bool __fastcall TExternalConsole::HasFlag(TConsoleFlag Flag) const
- {
- switch (Flag)
- {
- case cfLimitedOutput:
- return FLimitedOutput;
- case cfLiveOutput:
- return FLiveOutput;
- case cfNoInteractiveInput:
- return FNoInteractiveInput;
- case cfInteractive:
- return FInteractive;
- case cfCommandLineOnly:
- return true;
- case cfWantsProgress:
- return FWantsProgress;
- case cfStdOut:
- return (FStdOut != TConsoleCommStruct::TInitEvent::OFF);
- case cfStdIn:
- return (FStdIn != TConsoleCommStruct::TInitEvent::OFF);
- default:
- DebugFail();
- return false;
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TExternalConsole::WaitBeforeExit()
- {
- // noop
- }
- //---------------------------------------------------------------------------
- void __fastcall TExternalConsole::Progress(TScriptProgress & Progress)
- {
- TGuard Guard(FSection.get());
- TConsoleCommStruct * CommStruct = GetCommStruct();
- typedef TConsoleCommStruct::TProgressEvent TProgressEvent;
- try
- {
- TProgressEvent & ProgressEvent = CommStruct->ProgressEvent;
- CommStruct->Event = TConsoleCommStruct::PROGRESS;
- if (DebugAlwaysTrue(TFileOperationProgressType::IsTransferOperation(Progress.Operation)))
- {
- ProgressEvent.Operation = TProgressEvent::COPY;
- }
- switch (Progress.Side)
- {
- case osLocal:
- ProgressEvent.Side = TProgressEvent::LOCAL;
- break;
- case osRemote:
- ProgressEvent.Side = TProgressEvent::REMOTE;
- break;
- default:
- DebugFail();
- }
- wcsncpy(ProgressEvent.FileName, Progress.FileName.c_str(), LENOF(ProgressEvent.FileName));
- NULL_TERMINATE(ProgressEvent.FileName);
- wcsncpy(ProgressEvent.Directory, Progress.Directory.c_str(), LENOF(ProgressEvent.Directory));
- NULL_TERMINATE(ProgressEvent.Directory);
- ProgressEvent.OverallProgress = Progress.OverallProgress;
- ProgressEvent.FileProgress = Progress.FileProgress;
- ProgressEvent.CPS = Progress.CPS;
- ProgressEvent.Cancel = Progress.Cancel;
- }
- __finally
- {
- FreeCommStruct(CommStruct);
- }
- SendEvent(INFINITE);
- CommStruct = GetCommStruct();
- try
- {
- TProgressEvent & ProgressEvent = CommStruct->ProgressEvent;
- Progress.Cancel = ProgressEvent.Cancel;
- }
- __finally
- {
- FreeCommStruct(CommStruct);
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TExternalConsole::TransferOut(const unsigned char * Data, size_t Len)
- {
- TGuard Guard(FSection.get());
- DebugAssert((Data == NULL) == (Len == 0));
- size_t Offset = 0;
- do
- {
- TConsoleCommStruct * CommStruct = GetCommStruct();
- try
- {
- CommStruct->Event = TConsoleCommStruct::TRANSFEROUT;
- unsigned int BlockLen = std::min(Len - Offset, sizeof(CommStruct->TransferEvent.Data));
- memcpy(CommStruct->TransferEvent.Data, Data + Offset, BlockLen);
- CommStruct->TransferEvent.Len = BlockLen;
- Offset += BlockLen;
- }
- __finally
- {
- FreeCommStruct(CommStruct);
- }
- SendEvent(INFINITE);
- }
- while (Offset < Len);
- }
- //---------------------------------------------------------------------------
- size_t __fastcall TExternalConsole::TransferIn(unsigned char * Data, size_t Len)
- {
- TGuard Guard(FSection.get());
- size_t Offset = 0;
- size_t Result = 0;
- while ((Result == Offset) && (Offset < Len))
- {
- TConsoleCommStruct * CommStruct;
- size_t BlockLen = std::min(Len - Offset, sizeof(CommStruct->TransferEvent.Data));
- CommStruct = GetCommStruct();
- try
- {
- CommStruct->Event = TConsoleCommStruct::TRANSFERIN;
- CommStruct->TransferEvent.Len = BlockLen;
- CommStruct->TransferEvent.Error = false;
- }
- __finally
- {
- FreeCommStruct(CommStruct);
- }
- SendEvent(INFINITE);
- CommStruct = GetCommStruct();
- try
- {
- if (CommStruct->TransferEvent.Error)
- {
- throw Exception(LoadStr(STREAM_READ_ERROR));
- }
- DebugAssert(CommStruct->TransferEvent.Len <= BlockLen);
- Result += CommStruct->TransferEvent.Len;
- memcpy(Data + Offset, CommStruct->TransferEvent.Data, CommStruct->TransferEvent.Len);
- Offset += BlockLen;
- }
- __finally
- {
- FreeCommStruct(CommStruct);
- }
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- class TNullConsole : public TConsole
- {
- public:
- __fastcall TNullConsole();
- virtual void __fastcall Print(UnicodeString Str, bool FromBeginning = false, bool Error = false);
- virtual bool __fastcall Input(UnicodeString & Str, bool Echo, unsigned int Timer);
- virtual int __fastcall Choice(
- UnicodeString Options, int Cancel, int Break, int Continue, int Timeouted, bool Timeouting, unsigned int Timer,
- UnicodeString Message);
- virtual bool __fastcall HasFlag(TConsoleFlag Flag) const;
- virtual bool __fastcall PendingAbort();
- virtual void __fastcall SetTitle(UnicodeString Title);
- virtual void __fastcall WaitBeforeExit();
- virtual void __fastcall Progress(TScriptProgress & Progress);
- virtual void __fastcall TransferOut(const unsigned char * Data, size_t Len);
- virtual size_t __fastcall TransferIn(unsigned char * Data, size_t Len);
- virtual UnicodeString __fastcall FinalLogMessage();
- };
- //---------------------------------------------------------------------------
- __fastcall TNullConsole::TNullConsole()
- {
- }
- //---------------------------------------------------------------------------
- void __fastcall TNullConsole::Print(UnicodeString /*Str*/, bool /*FromBeginning*/, bool /*Error*/)
- {
- // 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 /*Continue*/, int Timeouted, bool Timeouting,
- unsigned int Timer, UnicodeString /*Message*/)
- {
- 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::HasFlag(TConsoleFlag Flag) const
- {
- switch (Flag)
- {
- // do not matter, even if we return false,
- // it fails immediately afterwards in TNullConsole::Input
- case cfNoInteractiveInput:
- return true;
- case cfLimitedOutput:
- case cfLiveOutput:
- case cfInteractive:
- case cfCommandLineOnly:
- case cfWantsProgress:
- case cfStdOut:
- case cfStdIn:
- return false;
- default:
- DebugFail();
- return false;
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TNullConsole::WaitBeforeExit()
- {
- DebugFail();
- // noop
- }
- //---------------------------------------------------------------------------
- void __fastcall TNullConsole::Progress(TScriptProgress & /*Progress*/)
- {
- DebugFail();
- }
- //---------------------------------------------------------------------------
- void __fastcall TNullConsole::TransferOut(const unsigned char * DebugUsedArg(Data), size_t DebugUsedArg(Len))
- {
- DebugFail();
- }
- //---------------------------------------------------------------------------
- size_t __fastcall TNullConsole::TransferIn(unsigned char * DebugUsedArg(Data), size_t DebugUsedArg(Len))
- {
- DebugFail();
- return 0;
- }
- //---------------------------------------------------------------------------
- UnicodeString __fastcall TNullConsole::FinalLogMessage()
- {
- return UnicodeString();
- }
- //---------------------------------------------------------------------------
- static UnicodeString TimestampVarName(L"TIMESTAMP");
- //---------------------------------------------------------------------------
- class TConsoleRunner
- {
- public:
- TConsoleRunner(TConsole * Console);
- ~TConsoleRunner();
- int Run(
- const UnicodeString Session, TOptions * Options, TStrings * ScriptCommands, TStrings * ScriptParameters,
- bool UsageWarnings);
- void __fastcall ShowException(Exception * E);
- inline void __fastcall PrintMessage(const UnicodeString & Str, bool Error = false);
- 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, bool Error = false);
- void __fastcall UpdateTitle();
- inline bool __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;
- bool FExternalTimestampVar;
- void __fastcall ScriptPrint(TScript * Script, const UnicodeString Str, bool Error);
- 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, TScriptProgress & Progress);
- void __fastcall ScriptTransferOut(TObject *, const unsigned char * Data, size_t Len);
- size_t __fastcall ScriptTransferIn(TObject *, unsigned char * Data, size_t Len);
- 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;
- DebugAssert(WinConfiguration->OnMasterPasswordPrompt == NULL);
- WinConfiguration->OnMasterPasswordPrompt = MasterPasswordPrompt;
- DebugAssert(Configuration->OnChange == NULL);
- FExternalTimestampVar = !GetEnvironmentVariable(TimestampVarName).IsEmpty();
- Configuration->OnChange = ConfigurationChange;
- Configuration->Scripting = true;
- }
- //---------------------------------------------------------------------------
- TConsoleRunner::~TConsoleRunner()
- {
- DebugAssert(WinConfiguration->OnMasterPasswordPrompt == MasterPasswordPrompt);
- WinConfiguration->OnMasterPasswordPrompt = NULL;
- DebugAssert(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, bool Error)
- {
- if (FLastProgressLen > 0)
- {
- FConsole->Print(L"\n" + Str, FromBeginning, Error);
- FLastProgressLen = 0;
- }
- else
- {
- FConsole->Print(Str, FromBeginning, Error);
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TConsoleRunner::PrintMessage(const UnicodeString & Str, bool Error)
- {
- UnicodeString Line = RemoveEmptyLines(Str);
- if (FScript != NULL)
- {
- // this also logs the message
- FScript->PrintLine(Line, Error);
- }
- else
- {
- FConsole->PrintLine(Line, Error);
- }
- }
- //---------------------------------------------------------------------------
- bool __fastcall TConsoleRunner::NotifyAbort()
- {
- bool Result = FBatchScript;
- if (Result)
- {
- FAborted = true;
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- bool __fastcall TConsoleRunner::Aborted(bool AllowCompleteAbort)
- {
- bool Result;
- if (FAborted)
- {
- Result = true;
- }
- else
- {
- Result = FConsole->PendingAbort();
- if (Result)
- {
- PrintMessage(LoadStr(USER_TERMINATED), true);
- if (AllowCompleteAbort && NotifyAbort())
- {
- if (FScript->Terminal != NULL)
- {
- std::unique_ptr<TStringList> Failure(TextToStringList(LoadStr(USER_TERMINATED)));
- FScript->Terminal->ActionLog->AddFailure(Failure.get());
- }
- }
- }
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- void __fastcall TConsoleRunner::ScriptPrint(TScript * /*Script*/,
- const UnicodeString Str, bool Error)
- {
- Print(Str, false, Error);
- }
- //---------------------------------------------------------------------------
- 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 DebugUsedArg(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 TimeoutR = 0;
- unsigned int NoBatchA = 0;
- if (Params != NULL)
- {
- if (Params->Timeout > 0)
- {
- Timeout = Params->Timeout;
- TimeoutA = Params->TimeoutAnswer;
- TimeoutR = Params->TimeoutResponse;
- DebugAssert((TimeoutA != 0) && (TimeoutR != 0));
- }
- if (Params->Timer > 0)
- {
- Timer = Params->Timer;
- if (Params->TimerAnswers > 0)
- {
- Answers = Params->TimerAnswers;
- }
- // not considering TimerQueryType as we do not use QueryType anyway
- if (!Params->TimerMessage.IsEmpty())
- {
- AQuery = Params->TimerMessage;
- }
- }
- if (FLAGSET(Params->Params, qpFatalAbort))
- {
- AQuery = FMTLOAD(WARN_FATAL_ERROR, (AQuery));
- }
- NoBatchA = Params->NoBatchAnswers;
- if (FLAGSET(Params->Params, qpWaitInBatch))
- {
- DebugAssert(Timeout == 0);
- Timeout = InputTimeout();
- if (Timeout != 0)
- {
- DebugAssert((TimeoutA == 0) && (TimeoutR == 0));
- // See a duplicate AbortAnswer call below
- TimeoutA = AbortAnswer(Answers & ~NoBatchA);
- TimeoutR = TimeoutA;
- }
- }
- }
- AQuery = UnformatMessage(AQuery);
- AQuery = RemoveInteractiveMsgTag(AQuery);
- ApplyTabs(AQuery, L' ', NULL, NULL);
- unsigned int AAnswers = Answers;
- UnicodeString Message = AQuery;
- PrintMessage(AQuery);
- if ((MoreMessages != NULL) && (MoreMessages->Count > 0))
- {
- PrintMessage(MoreMessages->Text);
- Message += L"\n" + MoreMessages->Text;
- }
- std::vector<unsigned int> Buttons;
- std::vector<UnicodeString> Captions;
- std::vector<TButtonSubmitEvent> OnSubmits;
- for (unsigned int Answer = qaFirst; Answer <= qaLast; Answer = Answer << 1)
- {
- if (FLAGSET(Answers, Answer))
- {
- UnicodeString Name; // unused
- UnicodeString Caption;
- AnswerNameAndCaption(Answer, Name, Caption);
- Captions.push_back(Caption);
- Buttons.push_back(Answer);
- OnSubmits.push_back(NULL);
- AAnswers -= Answer;
- }
- }
- DebugUsedParam(AAnswers);
- DebugAssert(AAnswers == 0);
- DebugAssert(!Buttons.empty());
- if ((Params != NULL) && (Params->Aliases != NULL))
- {
- for (unsigned int bi = 0; bi < Buttons.size(); bi++)
- {
- for (unsigned int ai = 0; ai < Params->AliasesCount; ai++)
- {
- if (Params->Aliases[ai].Button == Buttons[bi])
- {
- if (!Params->Aliases[ai].Alias.IsEmpty())
- {
- Captions[bi] = Params->Aliases[ai].Alias;
- }
- OnSubmits[bi] = Params->Aliases[ai].OnSubmit;
- break;
- }
- }
- }
- }
- UnicodeString Accels;
- for (unsigned int Index = 0; Index < Buttons.size(); 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' ';
- }
- }
- DebugAssert(Accels.Length() == static_cast<int>(Buttons.size()));
- int NumberAccel = 0;
- unsigned int CancelA = CancelAnswer(Answers);
- int CancelIndex = -1;
- // AbortAnswer call duplicated in qpWaitInBatch branch above
- unsigned int AbortA = AbortAnswer(Answers & ~NoBatchA);
- int AbortIndex = -1;
- unsigned int ContinueA = ContinueAnswer(Answers & ~NoBatchA);
- int ContinueIndex = -1;
- int TimeoutIndex = 0;
- for (unsigned int Index = 0; Index < Buttons.size(); 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 (IsLetter(C) && (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++;
- DebugAssert(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;
- }
- }
- DebugAssert(Accels.Pos(L' ') == 0);
- bool Timeouting = (Timeout > 0);
- bool FirstOutput = true;
- do
- {
- Answer = 0;
- int AnswerIndex;
- bool Retry;
- do
- {
- Retry = false;
- if (FirstOutput || FConsole->HasFlag(cfLiveOutput))
- {
- UnicodeString Output;
- for (unsigned int i = 0; i < Buttons.size(); i++)
- {
- if (i > 0)
- {
- Output += L", ";
- }
- UnicodeString Caption = Captions[i];
- int P = Caption.Pos(L'&');
- if (DebugAlwaysTrue(P >= 0))
- {
- Caption[P] = L'(';
- Caption.Insert(L")", P + 2);
- }
- if (i + 1 == static_cast<unsigned int>(TimeoutIndex))
- {
- DebugAssert(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;
- Answer = TimeoutR;
- }
- else
- {
- unsigned int ActualTimer;
- if (Timeouting)
- {
- if (Timer == 0)
- {
- if (FConsole->HasFlag(cfNoInteractiveInput))
- {
- ActualTimer = Timeout;
- }
- else
- {
- ActualTimer = MSecsPerSec;
- }
- }
- else
- {
- if (Timer < MSecsPerSec)
- {
- ActualTimer = Timer;
- }
- else
- {
- ActualTimer = MSecsPerSec;
- }
- }
- }
- else
- {
- ActualTimer = Timer;
- }
- // Not to get preliminary "host is not responding" messages to .NET assembly
- if (FConsole->HasFlag(cfNoInteractiveInput) && (Timer > 0))
- {
- Sleep(Timer);
- AnswerIndex = -2;
- }
- else
- {
- AnswerIndex =
- FConsole->Choice(Accels, CancelIndex, -1, ContinueIndex, -2, Timeouting, ActualTimer, Message);
- }
- if (AnswerIndex == -1)
- {
- NotifyAbort();
- AnswerIndex = AbortIndex;
- }
- else if (AnswerIndex == -2)
- {
- if (Timeouting)
- {
- DebugAssert(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)
- {
- DebugAssert((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)
- {
- DebugAssert((AnswerIndex >= 1) && (AnswerIndex <= Accels.Length()));
- UnicodeString AnswerCaption = Captions[AnswerIndex - 1];
- int P = AnswerCaption.Pos(L"&");
- DebugAssert(P >= 0);
- AnswerCaption.Delete(P, 1);
- FConsole->PrintLine(AnswerCaption);
- FirstOutput = true;
- if (OnSubmits[AnswerIndex - 1] != NULL)
- {
- OnSubmits[AnswerIndex - 1](NULL, Answer);
- }
- else
- {
- Answer = Buttons[AnswerIndex - 1];
- }
- }
- else
- {
- FConsole->PrintLine();
- }
- }
- while (Answer == 0);
- if ((Answer == AbortA) &&
- ((Params == NULL) || 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*/, TScriptProgress & Progress)
- {
- FConsole->Progress(Progress);
- }
- //---------------------------------------------------------------------------
- void __fastcall TConsoleRunner::ScriptTransferOut(TObject *, const unsigned char * Data, size_t Len)
- {
- FConsole->TransferOut(Data, Len);
- }
- //---------------------------------------------------------------------------
- size_t __fastcall TConsoleRunner::ScriptTransferIn(TObject *, unsigned char * Data, size_t Len)
- {
- return FConsole->TransferIn(Data, Len);
- }
- //---------------------------------------------------------------------------
- void __fastcall TConsoleRunner::SynchronizeControllerLog(
- TSynchronizeController * /*Controller*/, TSynchronizeLogEntry /*Entry*/,
- const UnicodeString Message)
- {
- PrintMessage(Message);
- LogSynchronizeEvent(FScript->Terminal, Message);
- }
- //---------------------------------------------------------------------------
- void __fastcall TConsoleRunner::SynchronizeControllerAbort(TObject * /*Sender*/,
- bool /*Close*/)
- {
- FSynchronizeAborted = true;
- NotifyAbort();
- }
- //---------------------------------------------------------------------------
- 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)
- {
- try
- {
- FScript->Synchronize(LocalDirectory, RemoteDirectory, CopyParam,
- Params.Params, Checklist);
- }
- catch (Exception &)
- {
- if ((FScript->Batch == TScript::BatchContinue) &&
- FScript->Terminal->Active)
- {
- // noop
- }
- else
- {
- throw;
- }
- }
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TConsoleRunner::SynchronizeControllerSynchronizeInvalid(
- TSynchronizeController * /*Sender*/, const UnicodeString Directory, const UnicodeString ErrorStr)
- {
- if (!Directory.IsEmpty())
- {
- PrintMessage(FMTLOAD(WATCH_ERROR_DIRECTORY, (Directory)), true);
- }
- else
- {
- PrintMessage(LoadStr(WATCH_ERROR_GENERAL), true);
- }
- if (!ErrorStr.IsEmpty())
- {
- PrintMessage(ErrorStr, true);
- }
- }
- //---------------------------------------------------------------------------
- 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 (ExceptionFullMessage(E, Message))
- {
- FCommandError = true;
- PrintMessage(Message, true);
- }
- 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->HasFlag(cfNoInteractiveInput))
- {
- Result = false;
- }
- else
- {
- Result = FConsole->Input(Str, Echo, Timeout);
- }
- if (!Result)
- {
- NotifyAbort();
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- void __fastcall TConsoleRunner::MasterPasswordPrompt()
- {
- bool Retry;
- do
- {
- UnicodeString Password;
- Input(LoadStr(CONSOLE_MASTER_PASSWORD_PROMPT), Password, false, true);
- Retry = !WinConfiguration->ValidateMasterPassword(Password);
- if (Retry)
- {
- FConsole->PrintLine(LoadStr(MASTER_PASSWORD_INCORRECT));
- }
- else
- {
- WinConfiguration->SetMasterPassword(Password);
- }
- }
- while (Retry);
- }
- //---------------------------------------------------------------------------
- UnicodeString TConsoleRunner::ExpandCommand(UnicodeString Command, TStrings * ScriptParameters)
- {
- DebugAssert(ScriptParameters != NULL);
- for (int Index = 0; Index < ScriptParameters->Count; Index++)
- {
- Command = ReplaceStr(Command, FORMAT(L"%%%d%%", (Index+1)),
- ScriptParameters->Strings[Index]);
- }
- TDateTime N = Now();
- if (!FExternalTimestampVar)
- {
- Command =
- ReplaceStr(Command, FORMAT(L"%%%s%%", (TimestampVarName)), FormatDateTime(L"yyyymmddhhnnss", N));
- }
- int Offset = 1;
- int P2;
- do
- {
- int P = Pos(UpperCase(L"%" + TimestampVarName), UpperCase(Command), Offset);
- if (P > 0)
- {
- Offset = P + 1 + TimestampVarName.Length();
- P2 = Pos(L"%", Command, Offset);
- int P3 = Pos(L"#", Command, Offset);
- if ((P2 > 0) && (P3 > 0) && (P3 < P2) &&
- ((P3 == Offset) || (Command[Offset] == L'+') || (Command[Offset] == L'-')))
- {
- bool Valid = true;
- TDateTime T = N;
- if (P3 > Offset)
- {
- bool Add = (Command[Offset] == L'+');
- Offset++;
- Valid = TryRelativeStrToDateTime(Command.SubString(Offset, P3 - Offset), T, Add);
- }
- Offset = P3 + 1;
- if (Valid)
- {
- UnicodeString TimestampFormat = Command.SubString(Offset, P2 - Offset);
- UnicodeString TimestampValue = FormatDateTime(TimestampFormat, T);
- Command = Command.SubString(1, P - 1) + TimestampValue + Command.SubString(P2 + 1, Command.Length() - P2);
- Offset = P + TimestampValue.Length();
- }
- }
- }
- else
- {
- P2 = 0;
- }
- }
- while (P2 > 0);
- Command = ExpandEnvironmentVariables(Command);
- return Command;
- }
- //---------------------------------------------------------------------------
- void __fastcall TConsoleRunner::Failed(bool & AnyError)
- {
- if (FScript != NULL)
- {
- FScript->Log(llMessage, L"Failed");
- }
- AnyError = true;
- }
- //---------------------------------------------------------------------------
- int TConsoleRunner::Run(
- const UnicodeString Session, TOptions * Options, TStrings * ScriptCommands, TStrings * ScriptParameters,
- bool UsageWarnings)
- {
- int ExitCode;
- try
- {
- bool AnyError = false;
- try
- {
- FScript = new TManagementScript(StoredSessions, FConsole->HasFlag(cfLimitedOutput));
- FScript->CopyParam = GUIConfiguration->DefaultCopyParam;
- FScript->SynchronizeParams = GUIConfiguration->SynchronizeParams;
- FScript->WantsProgress = FConsole->HasFlag(cfWantsProgress);
- 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;
- FScript->UsageWarnings = UsageWarnings;
- if (FConsole->HasFlag(cfStdOut))
- {
- FScript->OnTransferOut = ScriptTransferOut;
- }
- if (FConsole->HasFlag(cfStdIn))
- {
- FScript->OnTransferIn = ScriptTransferIn;
- }
- UpdateTitle();
- // everything until the first manually entered command is "batch"
- // (including opening session from command line and script file)
- FBatchScript = true;
- if (!Session.IsEmpty())
- {
- if (UsageWarnings)
- {
- PrintMessage(LoadStr(SCRIPT_CMDLINE_SESSION));
- }
- FCommandError = false;
- 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
- {
- if (FBatchScript)
- {
- // no longer batch
- FBatchScript = false;
- FScript->StartInteractive();
- }
- 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);
- }
- if (FLastProgressLen > 0)
- {
- FConsole->Print(L"\n");
- FLastProgressLen = 0;
- }
- ExitCode = (AnyError || FAborted) ? RESULT_ANY_ERROR : RESULT_SUCCESS;
- if (FScript != NULL)
- {
- UnicodeString ExitCodeMessage = FORMAT(L"Exit code: %d", (ExitCode));
- FScript->Log(llMessage, ExitCodeMessage);
- if (Configuration->ActualLogProtocol >= 1)
- {
- FConsole->Print(ExitCodeMessage + L"\n");
- UnicodeString LogMessage = FConsole->FinalLogMessage();
- if (!LogMessage.IsEmpty())
- {
- FScript->Log(llMessage, LogMessage);
- FConsole->Print(LogMessage + L"\n");
- }
- }
- }
- }
- __finally
- {
- delete FScript;
- FScript = NULL;
- }
- return ExitCode;
- }
- //---------------------------------------------------------------------------
- void __fastcall TConsoleRunner::UpdateTitle()
- {
- UnicodeString NewTitle;
- if (FScript->Terminal != NULL)
- {
- NewTitle = FormatMainFormCaption(FScript->Terminal->SessionData->SessionName);
- }
- else
- {
- NewTitle = FormatMainFormCaption(L"");
- }
- FConsole->SetTitle(NewTitle);
- }
- //---------------------------------------------------------------------------
- void __fastcall TConsoleRunner::ConfigurationChange(TObject * /*Sender*/)
- {
- if (FScript != NULL)
- {
- FScript->ReflectSettings();
- }
- }
- //---------------------------------------------------------------------------
- static UnicodeString __fastcall GetExeBaseName()
- {
- return ExtractFileBaseName(Application->ExeName);
- }
- //---------------------------------------------------------------------------
- static void __fastcall PrintUsageSyntax(TConsole * Console, const UnicodeString & Str)
- {
- Console->PrintLine(GetExeBaseName() + L" " + Str);
- }
- //---------------------------------------------------------------------------
- typedef std::vector<std::pair<UnicodeString, UnicodeString> > TSwitchesUsage;
- //---------------------------------------------------------------------------
- static void __fastcall RegisterSwitch(
- TSwitchesUsage & SwitchesUsage, const UnicodeString & Name, const UnicodeString & Desc)
- {
- SwitchesUsage.push_back(std::make_pair(LowerCase(Name), Desc));
- }
- //---------------------------------------------------------------------------
- static void __fastcall RegisterSwitch(
- TSwitchesUsage & SwitchesUsage, const UnicodeString & Name, int DescID)
- {
- UnicodeString Desc = LoadStr(DescID);
- Desc = ReplaceText(Desc, L"%APP%", GetExeBaseName());
- RegisterSwitch(SwitchesUsage, Name, Desc);
- }
- //---------------------------------------------------------------------------
- void __fastcall Usage(TConsole * Console)
- {
- Console->PrintLine(FORMAT(L"WinSCP, %s", (Configuration->VersionStr)));
- UnicodeString Copyright =
- ReplaceText(LoadStr(WINSCP_COPYRIGHT), L"©", L"(c)");
- Console->PrintLine(Copyright);
- Console->PrintLine();
- Console->PrintLine(LoadStr(USAGE_SYNTAX_LABEL));
- bool CommandLineOnly = Console->HasFlag(cfCommandLineOnly);
- if (!CommandLineOnly)
- {
- PrintUsageSyntax(Console, L"site|workspace|folder");
- PrintUsageSyntax(Console, L"(sftp|scp|ftp[es]|dav[s]|s3)://[user[:password]@]host[:port][/path/[file]]");
- PrintUsageSyntax(Console, FORMAT(L"[mysession] /%s=<name>", (LowerCase(SESSIONNAME_SWICH))));
- PrintUsageSyntax(Console, L"[mysession] /newinstance");
- PrintUsageSyntax(Console, L"[mysession] /edit <path>");
- PrintUsageSyntax(Console, FORMAT(L"[mysession] /%s[=<file>]", (LowerCase(EXPLORE_SWITCH))));
- PrintUsageSyntax(Console, FORMAT(L"[mysession] /%s [local_dir] [remote_dir] [/%s]", (LowerCase(SYNCHRONIZE_SWITCH), LowerCase(DEFAULTS_SWITCH))));
- PrintUsageSyntax(Console, FORMAT(L"[mysession] /%s [local_dir] [remote_dir] [/%s]", (LowerCase(KEEP_UP_TO_DATE_SWITCH), LowerCase(DEFAULTS_SWITCH))));
- PrintUsageSyntax(Console, FORMAT(L"[mysession] /%s [path]", (LowerCase(REFRESH_SWITCH))));
- PrintUsageSyntax(Console, FORMAT(L"[mysession] [/%s=<file> [/%s=<passphrase>]]", (LowerCase(PRIVATEKEY_SWITCH), PassphraseOption)));
- PrintUsageSyntax(Console, L"[mysession] [/hostkey=<fingerprint>]");
- PrintUsageSyntax(Console, FORMAT(L"[mysession] [/%s=<user> [/%s=<password>]]", (LowerCase(USERNAME_SWITCH), LowerCase(PASSWORD_SWITCH))));
- PrintUsageSyntax(Console, FORMAT(L"[mysession] [/%s]", (LowerCase(PASSWORDSFROMFILES_SWITCH))));
- PrintUsageSyntax(Console, FORMAT(L"[mysession] [/clientcert=<file> [/%s=<passphrase>]]", (PassphraseOption)));
- PrintUsageSyntax(Console, L"[mysession] [/certificate=<fingerprint>]");
- PrintUsageSyntax(Console, L"[mysession] [/passive[=on|off]] [/implicit|explicit]");
- PrintUsageSyntax(Console, L"[mysession] [/timeout=<sec>]");
- PrintUsageSyntax(Console, L"[mysession] [/rawsettings setting1=value1 setting2=value2 ...]");
- }
- PrintUsageSyntax(Console,
- UnicodeString(!CommandLineOnly ? L"[/console] " : L"") +
- FORMAT(L"[/script=<file>] [/%s cmd1...] [/parameter // param1...]", (LowerCase(COMMAND_SWITCH))));
- if (CommandLineOnly)
- {
- PrintUsageSyntax(
- Console, FORMAT(L"[/%s[=%s|%s]] [/%s]",
- (LowerCase(STDOUT_SWITCH), STDINOUT_BINARY_VALUE, STDINOUT_CHUNKED_VALUE, LowerCase(STDIN_SWITCH))));
- }
- PrintUsageSyntax(Console,
- FORMAT(L"[/%s=<logfile> [/loglevel=<level>]] [/%s=[<count>%s]<size>]", (LowerCase(LOG_SWITCH), LowerCase(LOGSIZE_SWITCH), LOGSIZE_SEPARATOR)));
- if (!CommandLineOnly)
- {
- PrintUsageSyntax(Console, L"[/xmllog=<logfile> [/xmlgroups]]");
- }
- else
- {
- PrintUsageSyntax(Console, FORMAT(L"[/xmllog=<logfile> [/xmlgroups]] [/%s]", (LowerCase(NOINTERACTIVEINPUT_SWITCH))));
- }
- PrintUsageSyntax(Console,
- FORMAT(L"[/%s=<inifile>]", (LowerCase(INI_SWITCH))));
- PrintUsageSyntax(Console, FORMAT(L"[/%s config1=value1 config2=value2 ...]", (LowerCase(RAW_CONFIG_SWITCH))));
- PrintUsageSyntax(Console, FORMAT(L"[/%s setting1=value1 setting2=value2 ...]", (LowerCase(RAWTRANSFERSETTINGS_SWITCH))));
- PrintUsageSyntax(Console, L"/batchsettings <site_mask> setting1=value1 setting2=value2 ...");
- PrintUsageSyntax(Console, FORMAT(L"/%s keyfile [/%s=<file>] [/%s]", (
- LowerCase(KEYGEN_SWITCH), LowerCase(KEYGEN_OUTPUT_SWITCH), LowerCase(KEYGEN_CHANGE_PASSPHRASE_SWITCH))));
- PrintUsageSyntax(Console, FORMAT(L"/%s keyfile [/%s=<text>] [/%s=<file>]",
- (LowerCase(KEYGEN_SWITCH), LowerCase(KEYGEN_COMMENT_SWITCH), LowerCase(KEYGEN_CERTIFICATE_SWITCH))));
- PrintUsageSyntax(Console, FORMAT(L"/%s /%s=<file> mysession", (
- LowerCase(COPYID_SWITCH), LowerCase(IDENTITY_SWITCH))));
- if (!CommandLineOnly)
- {
- PrintUsageSyntax(Console, L"/update");
- }
- PrintUsageSyntax(Console, TProgramParams::FormatSwitch(LowerCase(INFO_SWITCH)));
- PrintUsageSyntax(Console, L"/help");
- Console->PrintLine();
- TSwitchesUsage SwitchesUsage;
- if (!CommandLineOnly)
- {
- RegisterSwitch(SwitchesUsage, L"session", USAGE_SESSION);
- RegisterSwitch(SwitchesUsage, TProgramParams::FormatSwitch(SESSIONNAME_SWICH) + L"=", USAGE_SESSIONNAME);
- RegisterSwitch(SwitchesUsage, L"/newinstance", USAGE_NEWINSTANCE);
- RegisterSwitch(SwitchesUsage, L"/edit", USAGE_EDIT);
- RegisterSwitch(SwitchesUsage, TProgramParams::FormatSwitch(EXPLORE_SWITCH), USAGE_EXPLORE);
- RegisterSwitch(SwitchesUsage, TProgramParams::FormatSwitch(SYNCHRONIZE_SWITCH), USAGE_SYNCHRONIZE);
- RegisterSwitch(SwitchesUsage, TProgramParams::FormatSwitch(KEEP_UP_TO_DATE_SWITCH), USAGE_KEEPUPTODATE);
- RegisterSwitch(SwitchesUsage, TProgramParams::FormatSwitch(REFRESH_SWITCH), USAGE_REFRESH);
- RegisterSwitch(SwitchesUsage, TProgramParams::FormatSwitch(DEFAULTS_SWITCH), USAGE_DEFAULTS);
- RegisterSwitch(SwitchesUsage, TProgramParams::FormatSwitch(PRIVATEKEY_SWITCH) + L"=", USAGE_PRIVATEKEY);
- RegisterSwitch(SwitchesUsage, L"/hostkey=", USAGE_HOSTKEY);
- RegisterSwitch(SwitchesUsage, TProgramParams::FormatSwitch(USERNAME_SWITCH), USAGE_USERNAME);
- RegisterSwitch(SwitchesUsage, TProgramParams::FormatSwitch(PASSWORD_SWITCH), USAGE_PASSWORD);
- RegisterSwitch(SwitchesUsage, TProgramParams::FormatSwitch(PASSWORDSFROMFILES_SWITCH), USAGE_PASSWORDSFROMFILES);
- RegisterSwitch(SwitchesUsage, L"/clientcert=", USAGE_CLIENTCERT);
- RegisterSwitch(SwitchesUsage, L"/certificate=", USAGE_CERTIFICATE);
- RegisterSwitch(SwitchesUsage, TProgramParams::FormatSwitch(PassphraseOption) + L"=", USAGE_PASSPHRASE);
- RegisterSwitch(SwitchesUsage, L"/passive=", USAGE_PASSIVE);
- RegisterSwitch(SwitchesUsage, L"/implicit", USAGE_IMPLICIT);
- RegisterSwitch(SwitchesUsage, L"/explicit", USAGE_EXPLICIT);
- RegisterSwitch(SwitchesUsage, L"/timeout=", USAGE_TIMEOUT);
- RegisterSwitch(SwitchesUsage, L"/rawsettings", USAGE_RAWSETTINGS);
- RegisterSwitch(SwitchesUsage, L"/console", USAGE_CONSOLE);
- }
- RegisterSwitch(SwitchesUsage, L"/script=", USAGE_SCRIPT);
- RegisterSwitch(SwitchesUsage, TProgramParams::FormatSwitch(COMMAND_SWITCH), USAGE_COMMAND);
- RegisterSwitch(SwitchesUsage, L"/parameter", USAGE_PARAMETER);
- if (CommandLineOnly)
- {
- RegisterSwitch(SwitchesUsage, TProgramParams::FormatSwitch(STDOUT_SWITCH), USAGE_STDOUT);
- RegisterSwitch(SwitchesUsage, TProgramParams::FormatSwitch(STDIN_SWITCH), USAGE_STDIN);
- }
- RegisterSwitch(SwitchesUsage, TProgramParams::FormatSwitch(LOG_SWITCH) + L"=", USAGE_LOG);
- RegisterSwitch(SwitchesUsage, L"/loglevel=", USAGE_LOGLEVEL);
- RegisterSwitch(SwitchesUsage, TProgramParams::FormatSwitch(LOGSIZE_SWITCH) + L"=", USAGE_LOGSIZE);
- RegisterSwitch(SwitchesUsage, L"/xmllog=", USAGE_XMLLOG);
- RegisterSwitch(SwitchesUsage, L"/xmlgroups", USAGE_XMLGROUPS);
- if (CommandLineOnly)
- {
- RegisterSwitch(SwitchesUsage, TProgramParams::FormatSwitch(NOINTERACTIVEINPUT_SWITCH), USAGE_INTERACTIVEINPUT);
- }
- RegisterSwitch(SwitchesUsage, TProgramParams::FormatSwitch(INI_SWITCH) + L"=", USAGE_INI);
- RegisterSwitch(SwitchesUsage, TProgramParams::FormatSwitch(RAW_CONFIG_SWITCH), USAGE_RAWCONFIG);
- RegisterSwitch(SwitchesUsage, TProgramParams::FormatSwitch(RAWTRANSFERSETTINGS_SWITCH), USAGE_RAWTRANSFERSETTINGS);
- RegisterSwitch(SwitchesUsage, L"/batchsettings", USAGE_BATCHSETTINGS);
- UnicodeString KeyGenDesc =
- FMTLOAD(USAGE_KEYGEN2, (
- TProgramParams::FormatSwitch(LowerCase(KEYGEN_OUTPUT_SWITCH)) + L"=",
- TProgramParams::FormatSwitch(LowerCase(KEYGEN_CHANGE_PASSPHRASE_SWITCH)),
- TProgramParams::FormatSwitch(LowerCase(KEYGEN_COMMENT_SWITCH)) + L"=",
- TProgramParams::FormatSwitch(LowerCase(KEYGEN_CERTIFICATE_SWITCH)) + L"="));
- RegisterSwitch(SwitchesUsage, TProgramParams::FormatSwitch(KEYGEN_SWITCH), KeyGenDesc);
- RegisterSwitch(SwitchesUsage, TProgramParams::FormatSwitch(COPYID_SWITCH), USAGE_COPYID);
- if (!CommandLineOnly)
- {
- RegisterSwitch(SwitchesUsage, L"/update", USAGE_UPDATE);
- }
- RegisterSwitch(SwitchesUsage, TProgramParams::FormatSwitch(INFO_SWITCH), USAGE_INFO);
- RegisterSwitch(SwitchesUsage, L"/help", USAGE_HELP);
- int MaxSwitchLen = 0;
- TSwitchesUsage::const_iterator Index = SwitchesUsage.begin();
- while (Index != SwitchesUsage.end())
- {
- MaxSwitchLen = std::max(Index->first.Length(), MaxSwitchLen);
- ++Index;
- }
- Index = SwitchesUsage.begin();
- while (Index != SwitchesUsage.end())
- {
- UnicodeString Label =
- UnicodeString(L" ") +
- Index->first +
- UnicodeString::StringOfChar(L' ', MaxSwitchLen - Index->first.Length()) +
- L" ";
- Console->Print(Label);
- const int ConsoleWidth = 80;
- int DescWidth = ConsoleWidth - Label.Length() - 1;
- bool FirstLine = true;
- UnicodeString Desc = Index->second;
- while (!Desc.IsEmpty())
- {
- UnicodeString DescLine = CutToChar(Desc, L'\n', true);
- DescLine = WrapText(DescLine, L"\n", TSysCharSet() << L' ', DescWidth);
- while (!DescLine.IsEmpty())
- {
- UnicodeString DescLineLine = CutToChar(DescLine, L'\n', true);
- if (!FirstLine)
- {
- DescLineLine =
- UnicodeString::StringOfChar(L' ', Label.Length()) +
- DescLineLine;
- }
- FirstLine = false;
- Console->PrintLine(DescLineLine);
- }
- }
- ++Index;
- }
- Console->WaitBeforeExit();
- }
- //---------------------------------------------------------------------------
- int __fastcall HandleException(TConsole * Console, Exception & E)
- {
- UnicodeString Message;
- if (ExceptionFullMessage(&E, Message))
- {
- Console->Print(Message);
- }
- return RESULT_ANY_ERROR;
- }
- //---------------------------------------------------------------------------
- int __fastcall BatchSettings(TConsole * Console, TProgramParams * Params)
- {
- int Result = RESULT_SUCCESS;
- try
- {
- std::unique_ptr<TStrings> Arguments(new TStringList());
- if (!DebugAlwaysTrue(Params->FindSwitch(L"batchsettings", Arguments.get())))
- {
- Abort();
- }
- else
- {
- if (Arguments->Count < 1)
- {
- throw Exception(LoadStr(BATCH_SET_NO_MASK));
- }
- else if (Arguments->Count < 2)
- {
- throw Exception(LoadStr(BATCH_SET_NO_SETTINGS));
- }
- else
- {
- TFileMasks Mask(Arguments->Strings[0]);
- Arguments->Delete(0);
- std::unique_ptr<TOptionsStorage> OptionsStorage(new TOptionsStorage(Arguments.get(), false));
- int Matches = 0;
- int Changes = 0;
- for (int Index = 0; Index < StoredSessions->Count; Index++)
- {
- TSessionData * Data = StoredSessions->Sessions[Index];
- if (!Data->IsWorkspace &&
- Mask.Matches(Data->Name, false, false))
- {
- Matches++;
- std::unique_ptr<TSessionData> OriginalData(new TSessionData(L""));
- OriginalData->CopyDataNoRecrypt(Data);
- Data->ApplyRawSettings(OptionsStorage.get(), false, true);
- bool Changed = !OriginalData->IsSame(Data, false);
- if (Changed)
- {
- Changes++;
- }
- UnicodeString StateStr = LoadStr(Changed ? BATCH_SET_CHANGED : BATCH_SET_NOT_CHANGED);
- Console->PrintLine(Data->Name + TitleSeparator + StateStr);
- }
- }
- StoredSessions->Save(false, true); // explicit
- Console->PrintLine(FMTLOAD(BATCH_SET_SUMMARY, (Matches, Changes)));
- }
- }
- }
- catch (Exception & E)
- {
- Result = HandleException(Console, E);
- }
- Console->WaitBeforeExit();
- return Result;
- }
- //---------------------------------------------------------------------------
- bool FindNixCompatibleSwitch(
- TProgramParams * Params, const UnicodeString & Name, const UnicodeString & NixName, UnicodeString & Value, bool & Set)
- {
- bool Result =
- !Name.IsEmpty() &&
- Params->FindSwitch(Name, Value, Set);
- if (!Result)
- {
- std::unique_ptr<TStrings> Args(new TStringList());
- Result = Params->FindSwitchCaseSensitive(NixName, Args.get(), 1);
- if (Result && (Args->Count >= 1))
- {
- Value = Args->Strings[0];
- Set = true;
- }
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- int __fastcall KeyGen(TConsole * Console, TProgramParams * Params)
- {
- int Result = RESULT_SUCCESS;
- UnicodeString Passphrase;
- UnicodeString NewPassphrase;
- try
- {
- UnicodeString InputFileName;
- std::unique_ptr<TStrings> Args(new TStringList());
- if (!Params->FindSwitch(KEYGEN_SWITCH, Args.get(), 1) ||
- (Args->Count < 1) ||
- Args->Strings[0].IsEmpty())
- {
- throw Exception(LoadStr(KEYGEN_NO_INPUT));
- }
- InputFileName = Args->Strings[0];
- bool ValueSet;
- UnicodeString OutputFileName;
- FindNixCompatibleSwitch(Params, KEYGEN_OUTPUT_SWITCH, L"o", OutputFileName, ValueSet);
- UnicodeString NewComment;
- FindNixCompatibleSwitch(Params, KEYGEN_COMMENT_SWITCH, L"C", NewComment, ValueSet);
- bool ChangePassphrase;
- bool NewPassphraseSet;
- if (Params->FindSwitchCaseSensitive(L"P"))
- {
- ChangePassphrase = true;
- FindNixCompatibleSwitch(Params, EmptyStr, L"-new-passphrase", NewPassphrase, NewPassphraseSet);
- }
- else
- {
- ChangePassphrase = Params->FindSwitch(KEYGEN_CHANGE_PASSPHRASE_SWITCH, NewPassphrase, NewPassphraseSet);
- }
- UnicodeString Certificate;
- // It's --certificate in puttygen
- FindNixCompatibleSwitch(Params, KEYGEN_CERTIFICATE_SWITCH, L"-certificate", Certificate, ValueSet);
- FindNixCompatibleSwitch(Params, PassphraseOption, L"-old-passphrase", Passphrase, ValueSet);
- if (Params->ParamCount > 0)
- {
- throw Exception(LoadStr(TOO_MANY_PARAMS_ERROR));
- }
- TKeyType Type = KeyType(InputFileName);
- int Error = errno;
- switch (Type)
- {
- case ktSSH1:
- throw Exception(LoadStr(KEYGEN_SSH1));
- case ktSSH2:
- if (NewComment.IsEmpty() && !ChangePassphrase && Certificate.IsEmpty() &&
- (Configuration->KeyVersion == 0)) // We should better check for version change
- {
- throw Exception(LoadStr(KEYGEN_NO_ACTION));
- }
- break;
- case ktOpenSSHPEM:
- case ktOpenSSHNew:
- case ktSSHCom:
- if (OutputFileName.IsEmpty())
- {
- throw Exception(LoadStr(KEYGEN_NO_OUTPUT));
- }
- break;
- case ktSSH1Public:
- case ktSSH2PublicRFC4716:
- case ktSSH2PublicOpenSSH:
- throw Exception(LoadStr(KEYGEN_PUBLIC));
- case ktUnopenable:
- throw EOSExtException(FMTLOAD(KEY_TYPE_UNOPENABLE, (InputFileName)), Error);
- case ktOpenSSHAuto:
- default:
- DebugFail();
- // fallthru
- case ktUnknown:
- throw Exception(FMTLOAD(KEY_TYPE_UNKNOWN2, (InputFileName)));
- }
- UnicodeString Comment;
- if (IsKeyEncrypted(Type, InputFileName, Comment) &&
- Passphrase.IsEmpty())
- {
- Console->Print(StripHotkey(FMTLOAD(PROMPT_KEY_PASSPHRASE, (Comment))) + L" ");
- if (!Console->Input(Passphrase, false, 0) ||
- Passphrase.IsEmpty())
- {
- Abort();
- }
- }
- TPrivateKey * PrivateKey = LoadKey(Type, InputFileName, Passphrase);
- try
- {
- if (!NewComment.IsEmpty())
- {
- ChangeKeyComment(PrivateKey, NewComment);
- }
- if (!Certificate.IsEmpty())
- {
- AddCertificateToKey(PrivateKey, Certificate);
- }
- if (ChangePassphrase)
- {
- if (!NewPassphraseSet)
- {
- Console->Print(LoadStr(KEYGEN_PASSPHRASE) + L" ");
- if (!Console->Input(NewPassphrase, false, 0))
- {
- Abort();
- }
- Console->Print(LoadStr(KEYGEN_PASSPHRASE2) + L" ");
- UnicodeString NewPassphrase2;
- if (!Console->Input(NewPassphrase2, false, 0))
- {
- Abort();
- }
- if (NewPassphrase != NewPassphrase2)
- {
- Shred(NewPassphrase2);
- throw Exception(LoadStr(KEYGEN_PASSPHRASES_MISMATCH));
- }
- }
- }
- else
- {
- NewPassphrase = Passphrase;
- }
- if (OutputFileName.IsEmpty())
- {
- OutputFileName = InputFileName;
- }
- SaveKey(ktSSH2, OutputFileName, NewPassphrase, PrivateKey);
- Console->PrintLine(FMTLOAD(KEYGEN_SAVED, (OutputFileName)));
- }
- __finally
- {
- FreeKey(PrivateKey);
- }
- }
- catch (Exception & E)
- {
- Result = HandleException(Console, E);
- }
- Shred(Passphrase);
- Shred(NewPassphrase);
- Console->WaitBeforeExit();
- return Result;
- }
- //---------------------------------------------------------------------------
- int __fastcall FingerprintScan(TConsole * Console, TProgramParams * Params)
- {
- int Result = RESULT_SUCCESS;
- try
- {
- CheckLogParam(Params);
- std::unique_ptr<TSessionData> SessionData;
- if (Params->ParamCount > 0)
- {
- UnicodeString SessionUrl = Params->Param[1];
- bool DefaultsOnly;
- SessionData.reset(StoredSessions->ParseUrl(SessionUrl, Params, DefaultsOnly));
- if (DefaultsOnly || !SessionData->CanLogin ||
- (!SessionData->UsesSsh && (SessionData->Ftps == ftpsNone)))
- {
- SessionData.reset(NULL);
- }
- }
- if (!SessionData)
- {
- throw Exception(LoadStr(FINGERPRINTSCAN_NEED_SECURE_SESSION));
- }
- std::unique_ptr<TTerminal> Terminal(new TTerminal(SessionData.get(), Configuration));
- UnicodeString SHA256;
- UnicodeString SHA1;
- UnicodeString MD5;
- Terminal->FingerprintScan(SHA256, SHA1, MD5);
- if (!SHA256.IsEmpty())
- {
- Console->PrintLine(FORMAT(L"SHA-256: %s", (SHA256)));
- }
- if (!SHA1.IsEmpty())
- {
- Console->PrintLine(FORMAT(L"SHA-1: %s", (SHA1)));
- }
- if (!MD5.IsEmpty())
- {
- Console->PrintLine(FORMAT(L"MD5: %s", (MD5)));
- }
- }
- catch (Exception & E)
- {
- Result = HandleException(Console, E);
- }
- Console->WaitBeforeExit();
- return Result;
- }
- //---------------------------------------------------------------------------
- int __fastcall DumpCallstack(TConsole * Console, TProgramParams * Params)
- {
- int Result = RESULT_SUCCESS;
- try
- {
- int ProcessId = StrToInt(Params->SwitchValue(DUMPCALLSTACK_SWITCH));
- UnicodeString EventName = DumpCallstackEventName(ProcessId);
- UnicodeString FileName = DumpCallstackFileName(ProcessId);
- if (FileExists(FileName))
- {
- DeleteFileChecked(FileName);
- }
- HANDLE Event = OpenEvent(EVENT_ALL_ACCESS, false, EventName.c_str());
- if (Event == NULL)
- {
- throw ExtException(FORMAT(L"Error communicating with process %d.", (ProcessId)), LastSysErrorMessage());
- }
- SetEvent(Event);
- CloseHandle(Event);
- Console->PrintLine(FORMAT(L"Requested callstack dump for process %d...", (ProcessId)));
- int Timeout = 30;
- while (!FileExists(FileName))
- {
- Sleep(1000);
- Timeout--;
- if (Timeout == 0)
- {
- throw Exception(L"Timeout");
- }
- }
- Console->PrintLine(FORMAT(L"Callstack dumped to file \"%s\".", (FileName)));
- }
- catch (Exception & E)
- {
- Result = HandleException(Console, E);
- }
- Console->WaitBeforeExit();
- return Result;
- }
- //---------------------------------------------------------------------------
- void static PrintList(TConsole * Console, const UnicodeString & Caption, TStrings * List)
- {
- Console->PrintLine(Caption);
- for (int Index = 0; Index < List->Count; Index++)
- {
- Console->PrintLine(List->Strings[Index]);
- }
- Console->PrintLine();
- }
- //---------------------------------------------------------------------------
- void static PrintListAndFree(TConsole * Console, const UnicodeString & Caption, TStrings * List)
- {
- std::unique_ptr<TStrings> Owner(List);
- PrintList(Console, Caption, List);
- }
- //---------------------------------------------------------------------------
- int Info(TConsole * Console)
- {
- int Result = RESULT_SUCCESS;
- try
- {
- Console->PrintLine(FORMAT(L"SSH implementation: PuTTY %s", (GetPuTTYVersion())));
- Console->PrintLine();
- PrintListAndFree(Console, L"SSH encryption ciphers:", SshCipherList());
- PrintListAndFree(Console, L"SSH key exchange algorithms:", SshKexList());
- PrintListAndFree(Console, L"SSH host key algorithms:", SshHostKeyList());
- PrintListAndFree(Console, L"SSH MAC algorithms:", SshMacList());
- Console->PrintLine(TSessionLog::GetSeparator());
- Console->PrintLine(FORMAT(L"TLS/SSL implementation: %s", (GetOpenSSLVersionText())));
- Console->PrintLine();
- PrintListAndFree(Console, L"TLS/SSL cipher suites:", TlsCipherList());
- }
- catch (Exception & E)
- {
- Result = HandleException(Console, E);
- }
- Console->WaitBeforeExit();
- return Result;
- }
- //---------------------------------------------------------------------------
- TStdInOutMode ParseStdInOutMode(TProgramParams * Params, const UnicodeString & Switch, bool AllowChunked)
- {
- TStdInOutMode Result;
- UnicodeString Value;
- if (!Params->FindSwitch(Switch, Value))
- {
- Result = TConsoleCommStruct::TInitEvent::OFF;
- }
- else
- {
- if (Value.IsEmpty() || SameText(Value, STDINOUT_BINARY_VALUE))
- {
- Result = TConsoleCommStruct::TInitEvent::BINARY;
- }
- else if (SameText(Value, STDINOUT_CHUNKED_VALUE) && AllowChunked)
- {
- Result = TConsoleCommStruct::TInitEvent::CHUNKED;
- }
- else
- {
- throw Exception(FORMAT(SCRIPT_VALUE_UNKNOWN, (Value, Switch))); // abuse of the string
- }
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- int __fastcall Console(TConsoleMode Mode)
- {
- DebugAssert(Mode != cmNone);
- TProgramParams * Params = TProgramParams::Instance();
- int Result = RESULT_SUCCESS;
- TConsole * Console = NULL;
- TConsoleRunner * Runner = NULL;
- TStrings * ScriptCommands = new TStringList();
- TStrings * ScriptParameters = new TStringList();
- try
- {
- UnicodeString ConsoleInstance;
- // First check for /consoleinstance as /console is always used by winscp.com
- if (Params->FindSwitch(L"consoleinstance", ConsoleInstance))
- {
- Configuration->Usage->Inc(L"ConsoleExternal");
- TStdInOutMode StdOut = ParseStdInOutMode(Params, STDOUT_SWITCH, true);
- TStdInOutMode StdIn = ParseStdInOutMode(Params, STDIN_SWITCH, false);
- bool NoInteractiveInput = Params->FindSwitch(NOINTERACTIVEINPUT_SWITCH) || (StdIn != TConsoleCommStruct::TInitEvent::OFF);
- Console = new TExternalConsole(ConsoleInstance, NoInteractiveInput, StdOut, StdIn);
- }
- else if (Params->FindSwitch(L"Console") || (Mode != cmScripting))
- {
- Configuration->Usage->Inc(L"ConsoleOwn");
- Console = TOwnConsole::Instance();
- }
- else
- {
- Configuration->Usage->Inc(L"ConsoleNull");
- Console = new TNullConsole();
- }
- SetNoGUI();
- if (Mode == cmHelp)
- {
- Configuration->Usage->Inc(L"UsageShown");
- Usage(Console);
- }
- else if (Mode == cmBatchSettings)
- {
- if (CheckSafe(Params))
- {
- Configuration->Usage->Inc(L"BatchSettings");
- Result = BatchSettings(Console, Params);
- }
- }
- else if (Mode == cmKeyGen)
- {
- if (CheckSafe(Params))
- {
- Configuration->Usage->Inc(L"KeyGen");
- Result = KeyGen(Console, Params);
- }
- }
- else if (Mode == cmFingerprintScan)
- {
- if (CheckSafe(Params))
- {
- Configuration->Usage->Inc(L"FingerprintScan");
- Result = FingerprintScan(Console, Params);
- }
- }
- else if (Mode == cmDumpCallstack)
- {
- Result = DumpCallstack(Console, Params);
- }
- else if (Mode == cmInfo)
- {
- Result = Info(Console);
- }
- else if (Mode == cmComRegistration)
- {
- if (CheckSafe(Params))
- {
- Result = ComRegistration(Console);
- }
- }
- else
- {
- Runner = new TConsoleRunner(Console);
- try
- {
- if (CheckSafe(Params))
- {
- UnicodeString Value;
- if (Mode == cmCopyId)
- {
- Configuration->Usage->Inc(L"CopyId");
- UnicodeString IdentityFileName;
- bool ValueSet;
- FindNixCompatibleSwitch(Params, IDENTITY_SWITCH, L"i", IdentityFileName, ValueSet);
- if (IdentityFileName.IsEmpty())
- {
- throw Exception(LoadStr(COPYID_IDETITY_MISSING));
- }
- ScriptCommands->Add(FORMAT(L"%s \"%s\"", (COPYID_COMMAND, IdentityFileName)));
- ScriptCommands->Add(EXIT_COMMAND);
- }
- else if (Params->FindSwitch(SCRIPT_SWITCH, Value) && !Value.IsEmpty())
- {
- Configuration->Usage->Inc(L"ScriptFile");
- LoadScriptFromFile(Value, ScriptCommands);
- }
- Params->FindSwitch(COMMAND_SWITCH, ScriptCommands);
- if (ScriptCommands->Count > 0)
- {
- Configuration->Usage->Inc(L"ScriptCommands");
- }
- Params->FindSwitch(L"parameter", ScriptParameters);
- if (ScriptParameters->Count > 0)
- {
- Configuration->Usage->Inc(L"ScriptParameters");
- }
- }
- UnicodeString Session;
- if (Params->ParamCount >= 1)
- {
- Session = Params->Param[1];
- if (Params->ParamCount > 1)
- {
- // Check if the pending parameters will be consumed by ParseUrl (/rawsettings) in TManagementScript::Connect.
- // This way we parse the options twice, but we do not want to refactor the code just for nicer test for this minor warning.
- TOptions OptionsCopy(*Params);
- bool DefaultsOnly = false;
- StoredSessions->ParseUrl(Session, &OptionsCopy, DefaultsOnly);
- if (OptionsCopy.ParamCount > 1)
- {
- Runner->PrintMessage(LoadStr(SCRIPT_CMDLINE_PARAMETERS));
- }
- }
- }
- CheckLogParam(Params);
- CheckXmlLogParam(Params);
- bool UsageWarnings = (Mode != cmCopyId) && ((ScriptCommands->Count > 0) || !Console->HasFlag(cfInteractive));
- Result = Runner->Run(Session, Params, ScriptCommands, ScriptParameters, UsageWarnings);
- }
- catch(Exception & E)
- {
- Runner->ShowException(&E);
- Result = RESULT_ANY_ERROR;
- }
- if (Mode == cmCopyId)
- {
- Console->WaitBeforeExit();
- }
- }
- }
- __finally
- {
- delete Runner;
- delete Console;
- delete ScriptCommands;
- delete ScriptParameters;
- }
- return Result;
- }
|