Script.cpp 89 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include <SysUtils.hpp>
  5. #include "Common.h"
  6. #include "Exceptions.h"
  7. #include "TextsCore.h"
  8. #include "Script.h"
  9. #include "Terminal.h"
  10. #include "SessionData.h"
  11. #include "CoreMain.h"
  12. //---------------------------------------------------------------------------
  13. #pragma package(smart_init)
  14. //---------------------------------------------------------------------------
  15. const wchar_t * ToggleNames[] = { L"off", L"on" };
  16. const UnicodeString InOutParam(TraceInitStr(L"-"));
  17. //---------------------------------------------------------------------------
  18. __fastcall TScriptProcParams::TScriptProcParams(const UnicodeString & FullCommand, const UnicodeString & ParamsStr)
  19. {
  20. int P = FSwitchMarks.Pos(L"/");
  21. DebugAssert(P > 0);
  22. if (P > 0)
  23. {
  24. FSwitchMarks.Delete(P, 1);
  25. }
  26. FFullCommand = FullCommand;
  27. FParamsStr = ParamsStr;
  28. Parse(ParamsStr);
  29. }
  30. //---------------------------------------------------------------------------
  31. //---------------------------------------------------------------------------
  32. class TScriptCommands : TStringList
  33. {
  34. public:
  35. typedef void __fastcall (__closure *TCommandProc)(TScriptProcParams * Parameters);
  36. __fastcall TScriptCommands(TScript * Script);
  37. virtual __fastcall ~TScriptCommands();
  38. void __fastcall Execute(const UnicodeString & Command, const UnicodeString & Params);
  39. UnicodeString __fastcall ResolveCommand(const UnicodeString & Command);
  40. void __fastcall Register(const wchar_t * Command,
  41. const UnicodeString Description, const UnicodeString Help, TCommandProc Proc,
  42. int MinParams, int MaxParams, bool Switches);
  43. void __fastcall Register(const wchar_t * Command,
  44. int Description, int Help, TCommandProc Proc,
  45. int MinParams, int MaxParams, bool Switches);
  46. bool __fastcall Info(const UnicodeString Command,
  47. UnicodeString * Description, UnicodeString * Help);
  48. bool __fastcall Enumerate(int Index,
  49. UnicodeString * Command, UnicodeString * Description, UnicodeString * Help);
  50. static int __fastcall FindCommand(TStrings * Commands, const UnicodeString Command,
  51. UnicodeString * Matches = NULL);
  52. static int __fastcall FindCommand(const wchar_t ** Commands, size_t Count,
  53. const UnicodeString Command, UnicodeString * Matches = NULL);
  54. static void __fastcall CheckParams(TOptions * Parameters, bool Switches);
  55. protected:
  56. struct TScriptCommand
  57. {
  58. UnicodeString Description;
  59. UnicodeString Help;
  60. TCommandProc Proc;
  61. int MinParams;
  62. int MaxParams;
  63. bool Switches;
  64. };
  65. TScript * FScript;
  66. };
  67. //---------------------------------------------------------------------------
  68. __fastcall TScriptCommands::TScriptCommands(TScript * Script)
  69. {
  70. FScript = Script;
  71. Sorted = true;
  72. CaseSensitive = false;
  73. }
  74. //---------------------------------------------------------------------------
  75. __fastcall TScriptCommands::~TScriptCommands()
  76. {
  77. for (int Index = 0; Index < Count; Index++)
  78. {
  79. delete reinterpret_cast<TScriptCommand *>(Objects[Index]);
  80. }
  81. }
  82. //---------------------------------------------------------------------------
  83. void __fastcall TScriptCommands::Register(const wchar_t * Command,
  84. const UnicodeString Description, const UnicodeString Help, TCommandProc Proc,
  85. int MinParams, int MaxParams, bool Switches)
  86. {
  87. TScriptCommand * ScriptCommand = new TScriptCommand;
  88. ScriptCommand->Description = Description;
  89. ScriptCommand->Help = Help;
  90. ScriptCommand->Proc = Proc;
  91. ScriptCommand->MinParams = MinParams;
  92. ScriptCommand->MaxParams = MaxParams;
  93. ScriptCommand->Switches = Switches;
  94. AddObject(Command, reinterpret_cast<TObject *>(ScriptCommand));
  95. }
  96. //---------------------------------------------------------------------------
  97. void __fastcall TScriptCommands::Register(const wchar_t * Command,
  98. int Description, int Help, TCommandProc Proc,
  99. int MinParams, int MaxParams, bool Switches)
  100. {
  101. UnicodeString ADescription;
  102. if (Description > 0)
  103. {
  104. ADescription = LoadStr(Description);
  105. }
  106. UnicodeString AHelp;
  107. if (Help > 0)
  108. {
  109. AHelp = LoadStr(Help, 10240);
  110. }
  111. Register(Command, ADescription, AHelp, Proc, MinParams, MaxParams, Switches);
  112. }
  113. //---------------------------------------------------------------------------
  114. bool __fastcall TScriptCommands::Info(const UnicodeString Command,
  115. UnicodeString * Description, UnicodeString * Help)
  116. {
  117. int Index = FindCommand(this, Command);
  118. bool Result = (Index >= 0);
  119. if (Result)
  120. {
  121. TScriptCommand * ScriptCommand = reinterpret_cast<TScriptCommand *>(Objects[Index]);
  122. if (Description != NULL)
  123. {
  124. *Description = ScriptCommand->Description;
  125. }
  126. if (Help != NULL)
  127. {
  128. *Help = ScriptCommand->Help;
  129. }
  130. }
  131. return Result;
  132. }
  133. //---------------------------------------------------------------------------
  134. bool __fastcall TScriptCommands::Enumerate(int Index,
  135. UnicodeString * Command, UnicodeString * Description, UnicodeString * Help)
  136. {
  137. bool Result = (Index < Count);
  138. if (Result)
  139. {
  140. TScriptCommand * ScriptCommand = reinterpret_cast<TScriptCommand *>(Objects[Index]);
  141. if (Command != NULL)
  142. {
  143. *Command = Strings[Index];
  144. }
  145. if (Description != NULL)
  146. {
  147. *Description = ScriptCommand->Description;
  148. }
  149. if (Help != NULL)
  150. {
  151. *Help = ScriptCommand->Help;
  152. }
  153. }
  154. return Result;
  155. }
  156. //---------------------------------------------------------------------------
  157. int __fastcall TScriptCommands::FindCommand(TStrings * Commands,
  158. const UnicodeString Command, UnicodeString * Matches)
  159. {
  160. int Result = Commands->IndexOf(Command);
  161. if (Result < 0)
  162. {
  163. int MatchesCount = 0;
  164. for (int i = 0; i < Commands->Count; i++)
  165. {
  166. if ((Command.Length() <= Commands->Strings[i].Length()) &&
  167. SameText(Command, Commands->Strings[i].SubString(1, Command.Length())))
  168. {
  169. if (Matches != NULL)
  170. {
  171. if (!Matches->IsEmpty())
  172. {
  173. *Matches += L", ";
  174. }
  175. *Matches += Commands->Strings[i];
  176. }
  177. MatchesCount++;
  178. Result = i;
  179. }
  180. }
  181. if (MatchesCount == 0)
  182. {
  183. Result = -1;
  184. }
  185. else if (MatchesCount > 1)
  186. {
  187. Result = -2;
  188. }
  189. }
  190. return Result;
  191. }
  192. //---------------------------------------------------------------------------
  193. int __fastcall TScriptCommands::FindCommand(const wchar_t ** Commands, size_t Count,
  194. const UnicodeString Command, UnicodeString * Matches)
  195. {
  196. int Result;
  197. TStringList * Strings = new TStringList;
  198. try
  199. {
  200. Strings->CaseSensitive = false;
  201. for (unsigned int i = 0; i < Count; i++)
  202. {
  203. Strings->Add(Commands[i]);
  204. }
  205. Result = FindCommand(Strings, Command, Matches);
  206. }
  207. __finally
  208. {
  209. delete Strings;
  210. }
  211. return Result;
  212. }
  213. //---------------------------------------------------------------------------
  214. void __fastcall TScriptCommands::CheckParams(TOptions * Parameters,
  215. bool Switches)
  216. {
  217. UnicodeString Switch;
  218. if (!Switches && Parameters->UnusedSwitch(Switch))
  219. {
  220. throw Exception(FMTLOAD(SCRIPT_UNKNOWN_SWITCH, (Switch)));
  221. }
  222. }
  223. //---------------------------------------------------------------------------
  224. UnicodeString __fastcall TScriptCommands::ResolveCommand(const UnicodeString & Command)
  225. {
  226. UnicodeString Matches;
  227. int Index = FindCommand(this, Command, &Matches);
  228. if (Index >= 0)
  229. {
  230. return Strings[Index];
  231. }
  232. else
  233. {
  234. return UnicodeString();
  235. }
  236. }
  237. //---------------------------------------------------------------------------
  238. // parameters are by purpose passed by (constant) reference.
  239. // because if passed by value (copy), UnicodeString reference is not for some reason
  240. // decreased on exit by exception, leading to memory leak
  241. void __fastcall TScriptCommands::Execute(const UnicodeString & Command, const UnicodeString & Params)
  242. {
  243. UnicodeString Matches;
  244. int Index = FindCommand(this, Command, &Matches);
  245. if (Index == -2)
  246. {
  247. throw Exception(FMTLOAD(SCRIPT_COMMAND_AMBIGUOUS, (Command, Matches)));
  248. }
  249. else if (Index < 0)
  250. {
  251. throw Exception(FMTLOAD(SCRIPT_COMMAND_UNKNOWN, (Command)));
  252. }
  253. TScriptCommand * ScriptCommand = reinterpret_cast<TScriptCommand *>(Objects[Index]);
  254. UnicodeString FullCommand = Strings[Index];
  255. std::unique_ptr<TScriptProcParams> Parameters(new TScriptProcParams(FullCommand, Params));
  256. if (Parameters->ParamCount < ScriptCommand->MinParams)
  257. {
  258. throw Exception(FMTLOAD(SCRIPT_MISSING_PARAMS, (FullCommand)));
  259. }
  260. else if ((ScriptCommand->MaxParams >= 0) && (Parameters->ParamCount > ScriptCommand->MaxParams))
  261. {
  262. throw Exception(FMTLOAD(SCRIPT_TOO_MANY_PARAMS, (FullCommand)));
  263. }
  264. else
  265. {
  266. CheckParams(Parameters.get(), ScriptCommand->Switches);
  267. ScriptCommand->Proc(Parameters.get());
  268. }
  269. }
  270. //---------------------------------------------------------------------------
  271. //---------------------------------------------------------------------------
  272. // keep in sync with Session constructor in .NET
  273. const int BatchSessionReopenTimeout = 2 * MSecsPerSec * SecsPerMin; // 2 mins
  274. //---------------------------------------------------------------------------
  275. __fastcall TScript::TScript(bool LimitedOutput)
  276. {
  277. FLimitedOutput = LimitedOutput;
  278. FTerminal = NULL;
  279. FGroups = false;
  280. FWantsProgress = false;
  281. FInteractive = false;
  282. FOnTransferOut = NULL;
  283. FOnTransferIn = NULL;
  284. FIncludeFileMaskOptionUsed = false;
  285. FPendingLogLines = new TStringList();
  286. Init();
  287. }
  288. //---------------------------------------------------------------------------
  289. __fastcall TScript::~TScript()
  290. {
  291. delete FCommands;
  292. delete FPendingLogLines;
  293. }
  294. //---------------------------------------------------------------------------
  295. void __fastcall TScript::Init()
  296. {
  297. FBatch = BatchAbort;
  298. FInteractiveBatch = BatchOff;
  299. FConfirm = false;
  300. FInteractiveConfirm = true;
  301. FSessionReopenTimeout = Configuration->SessionReopenTimeout;
  302. FInteractiveSessionReopenTimeout = FSessionReopenTimeout;
  303. if (FSessionReopenTimeout == 0)
  304. {
  305. FSessionReopenTimeout = BatchSessionReopenTimeout;
  306. }
  307. FEcho = false;
  308. FFailOnNoMatch = false;
  309. FSynchronizeParams = 0;
  310. FOnPrint = NULL;
  311. FOnTerminalSynchronizeDirectory = NULL;
  312. FOnSynchronizeStartStop = NULL;
  313. FSynchronizeMode = -1;
  314. FKeepingUpToDate = false;
  315. FWarnNonDefaultCopyParam = false;
  316. FWarnNonDefaultSynchronizeParams = false;
  317. FCommands = new TScriptCommands(this);
  318. FCommands->Register(L"help", SCRIPT_HELP_DESC, SCRIPT_HELP_HELP, &HelpProc, 0, -1, false);
  319. FCommands->Register(L"man", 0, SCRIPT_HELP_HELP, &HelpProc, 0, -1, false);
  320. // the call command does not have switches itself, but the commands may have
  321. FCommands->Register(L"call", SCRIPT_CALL_DESC2, SCRIPT_CALL_HELP2, &CallProc, 1, -1, true);
  322. FCommands->Register(L"!", 0, SCRIPT_CALL_HELP2, &CallProc, 1, -1, true);
  323. FCommands->Register(L"pwd", SCRIPT_PWD_DESC, SCRIPT_PWD_HELP, &PwdProc, 0, 0, false);
  324. FCommands->Register(L"cd", SCRIPT_CD_DESC, SCRIPT_CD_HELP, &CdProc, 0, 1, false);
  325. FCommands->Register(L"ls", SCRIPT_LS_DESC, SCRIPT_LS_HELP2, &LsProc, 0, 1, false);
  326. FCommands->Register(L"dir", 0, SCRIPT_LS_HELP2, &LsProc, 0, 1, false);
  327. FCommands->Register(L"rm", SCRIPT_RM_DESC, SCRIPT_RM_HELP2, &RmProc, 1, -1, true);
  328. FCommands->Register(L"rmdir", SCRIPT_RMDIR_DESC, SCRIPT_RMDIR_HELP, &RmDirProc, 1, -1, false);
  329. FCommands->Register(L"mv", SCRIPT_MV_DESC, SCRIPT_MV_HELP2, &MvProc, 2, -1, false);
  330. FCommands->Register(L"rename", 0, SCRIPT_MV_HELP2, &MvProc, 2, -1, false);
  331. FCommands->Register(L"cp", SCRIPT_CP_DESC, SCRIPT_CP_HELP, &CpProc, 2, -1, false);
  332. FCommands->Register(L"chmod", SCRIPT_CHMOD_DESC, SCRIPT_CHMOD_HELP2, &ChModProc, 2, -1, false);
  333. FCommands->Register(L"ln", SCRIPT_LN_DESC, SCRIPT_LN_HELP, &LnProc, 2, 2, false);
  334. FCommands->Register(L"symlink", 0, SCRIPT_LN_HELP, &LnProc, 2, 2, false);
  335. FCommands->Register(L"mkdir", SCRIPT_MKDIR_DESC, SCRIPT_MKDIR_HELP, &MkDirProc, 1, 1, false);
  336. FCommands->Register(L"get", SCRIPT_GET_DESC, SCRIPT_GET_HELP8, &GetProc, 0, -1, true);
  337. FCommands->Register(L"recv", 0, SCRIPT_GET_HELP8, &GetProc, 0, -1, true);
  338. FCommands->Register(L"mget", 0, SCRIPT_GET_HELP8, &GetProc, 0, -1, true);
  339. FCommands->Register(L"put", SCRIPT_PUT_DESC, SCRIPT_PUT_HELP8, &PutProc, 0, -1, true);
  340. FCommands->Register(L"send", 0, SCRIPT_PUT_HELP8, &PutProc, 0, -1, true);
  341. FCommands->Register(L"mput", 0, SCRIPT_PUT_HELP8, &PutProc, 0, -1, true);
  342. FCommands->Register(L"option", SCRIPT_OPTION_DESC, SCRIPT_OPTION_HELP7, &OptionProc, -1, 2, false);
  343. FCommands->Register(L"ascii", 0, SCRIPT_OPTION_HELP7, &AsciiProc, 0, 0, false);
  344. FCommands->Register(L"binary", 0, SCRIPT_OPTION_HELP7, &BinaryProc, 0, 0, false);
  345. FCommands->Register(L"synchronize", SCRIPT_SYNCHRONIZE_DESC, SCRIPT_SYNCHRONIZE_HELP7, &SynchronizeProc, 0, -1, true);
  346. FCommands->Register(L"keepuptodate", SCRIPT_KEEPUPTODATE_DESC, SCRIPT_KEEPUPTODATE_HELP5, &KeepUpToDateProc, 0, 2, true);
  347. // the echo command does not have switches actually, but it must handle dashes in its arguments
  348. FCommands->Register(L"echo", SCRIPT_ECHO_DESC, SCRIPT_ECHO_HELP, &EchoProc, -1, -1, true);
  349. FCommands->Register(L"stat", SCRIPT_STAT_DESC, SCRIPT_STAT_HELP, &StatProc, 1, 1, false);
  350. FCommands->Register(L"checksum", SCRIPT_CHECKSUM_DESC, SCRIPT_CHECKSUM_HELP, &ChecksumProc, 2, 2, false);
  351. }
  352. //---------------------------------------------------------------------------
  353. void __fastcall TScript::RequireParams(TScriptProcParams * Parameters, int MinParams)
  354. {
  355. if (Parameters->ParamCount < MinParams)
  356. {
  357. throw Exception(FMTLOAD(SCRIPT_MISSING_PARAMS, (Parameters->FullCommand)));
  358. }
  359. }
  360. //---------------------------------------------------------------------------
  361. void __fastcall TScript::CheckDefaultCopyParam()
  362. {
  363. if (FWarnNonDefaultCopyParam)
  364. {
  365. // started with non-factory settings and still have them, warn
  366. if (HasNonDefaultCopyParams())
  367. {
  368. PrintLine(LoadStr(SCRIPT_NON_DEFAULT_COPY_PARAM));
  369. }
  370. FWarnNonDefaultCopyParam = false;
  371. }
  372. }
  373. //---------------------------------------------------------------------------
  374. bool __fastcall TScript::HasNonDefaultCopyParams()
  375. {
  376. return !(FCopyParam == TCopyParamType());
  377. }
  378. //---------------------------------------------------------------------------
  379. void __fastcall TScript::SetCopyParam(const TCopyParamType & value)
  380. {
  381. FCopyParam.Assign(&value);
  382. FWarnNonDefaultCopyParam = HasNonDefaultCopyParams();
  383. }
  384. //---------------------------------------------------------------------------
  385. void __fastcall TScript::CheckDefaultSynchronizeParams()
  386. {
  387. if (FWarnNonDefaultSynchronizeParams)
  388. {
  389. // as opposite to CheckDefaultCopyParam(), not checking
  390. // current params as we cannot override any of those we accept anyway
  391. PrintLine(LoadStr(SCRIPT_NON_DEFAULT_SYNC_PARAM));
  392. FWarnNonDefaultSynchronizeParams = false;
  393. }
  394. }
  395. //---------------------------------------------------------------------------
  396. void __fastcall TScript::SetSynchronizeParams(int value)
  397. {
  398. const int AcceptedParams =
  399. TTerminal::spExistingOnly | TTerminal::spTimestamp |
  400. TTerminal::spNotByTime | TTerminal::spBySize;
  401. FSynchronizeParams = (value & AcceptedParams);
  402. FWarnNonDefaultSynchronizeParams =
  403. (FSynchronizeParams != (TTerminal::spDefault & AcceptedParams));
  404. }
  405. //---------------------------------------------------------------------------
  406. bool __fastcall TScript::IsTerminalLogging(TTerminal * ATerminal)
  407. {
  408. return (ATerminal != NULL) && ATerminal->Log->Logging;
  409. }
  410. //---------------------------------------------------------------------------
  411. const static UnicodeString ScriptLogFormat(L"Script: %s");
  412. void __fastcall TScript::Log(TLogLineType Type, UnicodeString Str)
  413. {
  414. Str = FORMAT(ScriptLogFormat, (Str));
  415. if (IsTerminalLogging(Terminal))
  416. {
  417. Terminal->Log->Add(Type, Str);
  418. }
  419. else if (Configuration->Logging)
  420. {
  421. FPendingLogLines->AddObject(Str, reinterpret_cast<TObject *>(Type));
  422. }
  423. }
  424. //---------------------------------------------------------------------------
  425. void __fastcall TScript::LogOption(const UnicodeString & LogStr)
  426. {
  427. Log(llInput, LogStr);
  428. }
  429. //---------------------------------------------------------------------------
  430. void __fastcall TScript::LogPendingLines(TTerminal * ATerminal)
  431. {
  432. if (IsTerminalLogging(ATerminal) && (FPendingLogLines->Count > 0))
  433. {
  434. // not using Log(), as we want to log to ATerminal, not Terminal,
  435. // what is different here, as we are called from TManagementScript::Connect()
  436. ATerminal->Log->Add(llMessage, FORMAT(ScriptLogFormat, (L"Retrospectively logging previous script records:")));
  437. for (int Index = 0; Index < FPendingLogLines->Count; Index++)
  438. {
  439. ATerminal->Log->Add(
  440. reinterpret_cast<TLogLineType>(FPendingLogLines->Objects[Index]),
  441. FPendingLogLines->Strings[Index]);
  442. }
  443. FPendingLogLines->Clear();
  444. ATerminal->Log->AddSeparator();
  445. }
  446. }
  447. //---------------------------------------------------------------------------
  448. UnicodeString __fastcall TScript::GetLogCmd(const UnicodeString & FullCommand,
  449. const UnicodeString & /*Command*/, const UnicodeString & /*Params*/)
  450. {
  451. return FullCommand;
  452. }
  453. //---------------------------------------------------------------------------
  454. void __fastcall TScript::StartInteractive()
  455. {
  456. FBatch = FInteractiveBatch;
  457. FConfirm = FInteractiveConfirm;
  458. FSessionReopenTimeout = FInteractiveSessionReopenTimeout;
  459. }
  460. //---------------------------------------------------------------------------
  461. void __fastcall TScript::Command(UnicodeString Cmd)
  462. {
  463. try
  464. {
  465. if (!Cmd.Trim().IsEmpty() && (Cmd[1] != L';') && (Cmd[1] != L'#'))
  466. {
  467. UnicodeString FullCmd = Cmd;
  468. UnicodeString Command;
  469. if (CutToken(Cmd, Command))
  470. {
  471. UnicodeString LogCmd = GetLogCmd(FullCmd, Command, Cmd);
  472. Log(llInput, LogCmd);
  473. if (Configuration->LogProtocol >= 1)
  474. {
  475. UnicodeString DummyLogCmd;
  476. if (DebugAlwaysTrue(CutToken(LogCmd, DummyLogCmd)))
  477. {
  478. std::unique_ptr<TScriptProcParams> Parameters(new TScriptProcParams(FCommands->ResolveCommand(Cmd), LogCmd));
  479. Parameters->LogOptions(LogOption);
  480. }
  481. }
  482. if (FEcho)
  483. {
  484. PrintLine(LogCmd);
  485. }
  486. TTerminal * BeforeGroupTerminal = FGroups ? Terminal : NULL;
  487. if (BeforeGroupTerminal != NULL)
  488. {
  489. BeforeGroupTerminal->ActionLog->BeginGroup(LogCmd);
  490. }
  491. int ASessionReopenTimeout = Configuration->SessionReopenTimeout;
  492. try
  493. {
  494. Configuration->SessionReopenTimeout = FSessionReopenTimeout;
  495. try
  496. {
  497. FCommands->Execute(Command, Cmd);
  498. }
  499. catch(Exception & E)
  500. {
  501. // seemingly duplicate (to the method-level one) catch clause,
  502. // ensures the <failure/> tag is enclosed in <group/> tag
  503. if (!HandleExtendedException(&E))
  504. {
  505. throw;
  506. }
  507. }
  508. }
  509. __finally
  510. {
  511. Configuration->SessionReopenTimeout = ASessionReopenTimeout;
  512. TTerminal * AfterGroupTerminal = FGroups ? Terminal : NULL;
  513. if (AfterGroupTerminal != NULL)
  514. {
  515. // this happens for "open" command
  516. if (AfterGroupTerminal != BeforeGroupTerminal)
  517. {
  518. AfterGroupTerminal->ActionLog->BeginGroup(LogCmd);
  519. }
  520. AfterGroupTerminal->ActionLog->EndGroup();
  521. }
  522. }
  523. }
  524. }
  525. }
  526. catch(Exception & E)
  527. {
  528. if (!HandleExtendedException(&E))
  529. {
  530. throw;
  531. }
  532. }
  533. }
  534. //---------------------------------------------------------------------------
  535. TStrings * __fastcall TScript::CreateFileList(TScriptProcParams * Parameters, int Start,
  536. int End, TFileListType ListType)
  537. {
  538. TStrings * Result = new TStringList();
  539. TStrings * FileLists = NULL;
  540. try
  541. {
  542. try
  543. {
  544. for (int i = Start; i <= End; i++)
  545. {
  546. UnicodeString FileName = Parameters->Param[i];
  547. if (SimpleUnixExcludeTrailingBackslash(FileName) != FileName)
  548. {
  549. PrintLine(LoadStr(SCRIPT_AMBIGUOUS_SLASH_IN_PATH));
  550. }
  551. if (FLAGSET(ListType, fltDirectories))
  552. {
  553. TRemoteFile * File = new TRemoteFile();
  554. File->FileName = FileName;
  555. File->Type = FILETYPE_DIRECTORY;
  556. Result->AddObject(FileName, File);
  557. }
  558. else if (FLAGSET(ListType, fltMask) && TFileMasks::IsMask(FileName))
  559. {
  560. UnicodeString FileDirectory = UnixExtractFilePath(FileName);
  561. UnicodeString Directory = FileDirectory;
  562. if (Directory.IsEmpty())
  563. {
  564. Directory = UnixIncludeTrailingBackslash(FTerminal->CurrentDirectory);
  565. }
  566. TRemoteFileList * FileList = NULL;
  567. if (FileLists != NULL)
  568. {
  569. int Index = FileLists->IndexOf(Directory);
  570. if (Index > 0)
  571. {
  572. FileList = dynamic_cast<TRemoteFileList *>(FileLists->Objects[Index]);
  573. }
  574. }
  575. if (FileList == NULL)
  576. {
  577. FileList = FTerminal->CustomReadDirectoryListing(Directory, false);
  578. if (FileLists == NULL)
  579. {
  580. FileLists = new TStringList();
  581. }
  582. FileLists->AddObject(Directory, FileList);
  583. }
  584. TFileMasks Mask;
  585. Mask.SetMask(UnixExtractFileName(FileName));
  586. bool AnyFound = false;
  587. // Can happen in "batch continue" mode
  588. if (FileList != NULL)
  589. {
  590. for (int i = 0; i < FileList->Count; i++)
  591. {
  592. TRemoteFile * File = FileList->Files[i];
  593. TFileMasks::TParams Params;
  594. Params.Size = File->Size;
  595. Params.Modification = File->Modification;
  596. if (IsRealFile(File->FileName) &&
  597. Mask.Matches(File->FileName, false, UnicodeString(), &Params))
  598. {
  599. Result->AddObject(FileDirectory + File->FileName,
  600. FLAGSET(ListType, fltQueryServer) ? File->Duplicate() : NULL);
  601. AnyFound = true;
  602. }
  603. }
  604. }
  605. if (!AnyFound)
  606. {
  607. NoMatch(Mask.Masks, UnicodeString());
  608. }
  609. }
  610. else
  611. {
  612. TRemoteFile * File = NULL;
  613. if (FLAGSET(ListType, fltQueryServer))
  614. {
  615. FTerminal->ExceptionOnFail = true;
  616. try
  617. {
  618. FTerminal->ReadFile(UnixExcludeTrailingBackslash(FileName), File);
  619. if (!File->HaveFullFileName)
  620. {
  621. File->FullFileName = FileName;
  622. }
  623. }
  624. __finally
  625. {
  626. FTerminal->ExceptionOnFail = false;
  627. }
  628. }
  629. Result->AddObject(FileName, File);
  630. }
  631. }
  632. }
  633. catch(...)
  634. {
  635. FreeFileList(Result);
  636. throw;
  637. }
  638. }
  639. __finally
  640. {
  641. if (FileLists != NULL)
  642. {
  643. for (int i = 0; i < FileLists->Count; i++)
  644. {
  645. delete FileLists->Objects[i];
  646. }
  647. delete FileLists;
  648. }
  649. }
  650. if (FLAGSET(ListType, fltLatest) && (Result->Count > 1))
  651. {
  652. // otherwise we do not have TRemoteFile's
  653. DebugAssert(FLAGSET(ListType, fltQueryServer));
  654. int LatestIndex = 0;
  655. for (int Index = 1; Index < Result->Count; Index++)
  656. {
  657. TRemoteFile * File = dynamic_cast<TRemoteFile *>(Result->Objects[Index]);
  658. if (dynamic_cast<TRemoteFile *>(Result->Objects[LatestIndex])->Modification < File->Modification)
  659. {
  660. LatestIndex = Index;
  661. }
  662. }
  663. TRemoteFile * File = dynamic_cast<TRemoteFile *>(Result->Objects[LatestIndex]);
  664. UnicodeString Path = Result->Strings[LatestIndex];
  665. Result->Delete(LatestIndex);
  666. FreeFiles(Result);
  667. Result->Clear();
  668. Result->AddObject(Path, File);
  669. }
  670. if (FLAGSET(ListType, fltOnlyFile))
  671. {
  672. for (int Index = 0; Index < Result->Count; Index++)
  673. {
  674. TRemoteFile * File = dynamic_cast<TRemoteFile *>(Result->Objects[Index]);
  675. if (File->IsDirectory)
  676. {
  677. throw Exception(FMTLOAD(NOT_FILE_ERROR, (File->FileName)));
  678. }
  679. }
  680. }
  681. return Result;
  682. }
  683. //---------------------------------------------------------------------------
  684. TStrings * __fastcall TScript::CreateLocalFileList(TScriptProcParams * Parameters,
  685. int Start, int End, TFileListType ListType)
  686. {
  687. TStringList * Result = new TStringList();
  688. try
  689. {
  690. Result->OwnsObjects = true;
  691. UnicodeString LatestFileName;
  692. TDateTime LatestModification; // initialized to 0
  693. for (int i = Start; i <= End; i++)
  694. {
  695. // FindFirstFile called (indirectly) below fails if path ends with slash.
  696. // (it actually won't make a difference functionally as we fall back to adding
  697. // the path as is in "else" branch, but the comment "let it fail later" won't stand)
  698. UnicodeString FileName = ExcludeTrailingBackslash(Parameters->Param[i]);
  699. if (FileName != Parameters->Param[i])
  700. {
  701. PrintLine(LoadStr(SCRIPT_AMBIGUOUS_SLASH_IN_PATH));
  702. }
  703. if (FLAGSET(ListType, fltMask))
  704. {
  705. TSearchRecOwned SearchRec;
  706. int FindAttrs = faReadOnly | faHidden | faSysFile | faDirectory | faArchive;
  707. UnicodeString Error;
  708. bool AnyFound = false;
  709. if (FindFirstUnchecked(FileName, FindAttrs, SearchRec) == 0)
  710. {
  711. UnicodeString Directory = ExtractFilePath(FileName);
  712. do
  713. {
  714. if (SearchRec.IsRealFile())
  715. {
  716. UnicodeString FileName = Directory + SearchRec.Name;
  717. TLocalFile * LocalFile = new TLocalFile;
  718. CopySearchRec(SearchRec, LocalFile->SearchRec);
  719. Result->AddObject(FileName, LocalFile);
  720. if (SearchRec.TimeStamp > LatestModification)
  721. {
  722. LatestFileName = FileName;
  723. LatestModification = SearchRec.TimeStamp;
  724. }
  725. AnyFound = true;
  726. }
  727. }
  728. while (FindNextChecked(SearchRec) == 0);
  729. }
  730. else
  731. {
  732. if (FileName.LastDelimiter(L"?*") == 0)
  733. {
  734. // No match, and it is not a mask, let it fail latter.
  735. // But with -latest, we have to fail straight away
  736. // (so maybe we should fail unconditionally now,
  737. // once we need to have the code in place anyway)
  738. if (FLAGSET(ListType, fltLatest))
  739. {
  740. throw Exception(FMTLOAD(FILE_NOT_EXISTS, (FileName)));
  741. }
  742. Result->Add(FileName);
  743. AnyFound = true;
  744. }
  745. else
  746. {
  747. Error = ListingSysErrorMessage();
  748. }
  749. }
  750. if (!AnyFound)
  751. {
  752. NoMatch(ExtractFileName(FileName), Error);
  753. }
  754. }
  755. else
  756. {
  757. DebugAssert(FLAGCLEAR(ListType, fltLatest));
  758. // this branch is currently never used
  759. Result->Add(FileName);
  760. }
  761. }
  762. if (FLAGSET(ListType, fltLatest))
  763. {
  764. Result->Clear();
  765. if (!LatestFileName.IsEmpty())
  766. {
  767. Result->Add(LatestFileName);
  768. }
  769. }
  770. }
  771. catch(...)
  772. {
  773. delete Result;
  774. throw;
  775. }
  776. return Result;
  777. }
  778. //---------------------------------------------------------------------------
  779. UnicodeString __fastcall TScript::ListingSysErrorMessage()
  780. {
  781. UnicodeString Result;
  782. int LastError = GetLastError();
  783. // System error text for ERROR_FILE_NOT_FOUND is more or less redundant to ours
  784. // SCRIPT_MATCH_NO_MATCH. Also the system error does not look nice/user friendly
  785. // so avoid using it for this frequent case.
  786. if (LastError != ERROR_FILE_NOT_FOUND)
  787. {
  788. Result = SysErrorMessageForError(LastError);
  789. }
  790. return Result;
  791. }
  792. //---------------------------------------------------------------------------
  793. void __fastcall TScript::NoMatch(const UnicodeString & Message)
  794. {
  795. if (FFailOnNoMatch)
  796. {
  797. throw Exception(Message);
  798. }
  799. else
  800. {
  801. PrintLine(Message);
  802. }
  803. }
  804. //---------------------------------------------------------------------------
  805. void __fastcall TScript::NoMatch(const UnicodeString & Mask, const UnicodeString & Error)
  806. {
  807. UnicodeString Message = FMTLOAD(SCRIPT_MATCH_NO_MATCH, (Mask));
  808. if (!Error.IsEmpty())
  809. {
  810. Message += FORMAT(L" (%s)", (Error));
  811. }
  812. NoMatch(Message);
  813. }
  814. //---------------------------------------------------------------------------
  815. void __fastcall TScript::FreeFiles(TStrings * FileList)
  816. {
  817. for (int i = 0; i < FileList->Count; i++)
  818. {
  819. if (FileList->Objects[i] != NULL)
  820. {
  821. TRemoteFile * File = dynamic_cast<TRemoteFile *>(FileList->Objects[i]);
  822. delete File;
  823. }
  824. }
  825. }
  826. //---------------------------------------------------------------------------
  827. void __fastcall TScript::FreeFileList(TStrings * FileList)
  828. {
  829. FreeFiles(FileList);
  830. delete FileList;
  831. }
  832. //---------------------------------------------------------------------------
  833. void __fastcall TScript::ConnectTerminal(TTerminal * ATerminal)
  834. {
  835. ATerminal->Open();
  836. }
  837. //---------------------------------------------------------------------------
  838. void __fastcall TScript::Print(const UnicodeString Str, bool Error)
  839. {
  840. if (FOnPrint != NULL)
  841. {
  842. FOnPrint(this, Str, Error);
  843. }
  844. }
  845. //---------------------------------------------------------------------------
  846. void __fastcall TScript::PrintLine(const UnicodeString Str, bool Error)
  847. {
  848. Log(llOutput, Str);
  849. Print(Str + L"\n", Error);
  850. }
  851. //---------------------------------------------------------------------------
  852. bool __fastcall TScript::HandleExtendedException(Exception * E, TTerminal * ATerminal)
  853. {
  854. bool Result = (OnShowExtendedException != NULL);
  855. if (Result)
  856. {
  857. if (ATerminal == NULL)
  858. {
  859. ATerminal = FTerminal;
  860. }
  861. OnShowExtendedException(ATerminal, E, NULL);
  862. }
  863. return Result;
  864. }
  865. //---------------------------------------------------------------------------
  866. void __fastcall TScript::CheckSession()
  867. {
  868. if (FTerminal == NULL)
  869. {
  870. throw Exception(LoadStr(SCRIPT_NO_SESSION));
  871. }
  872. }
  873. //---------------------------------------------------------------------------
  874. void __fastcall TScript::NotSupported()
  875. {
  876. throw Exception(LoadStr(NOTSUPPORTED));
  877. }
  878. //---------------------------------------------------------------------------
  879. void __fastcall TScript::CheckMultiFilesToOne(TStrings * FileList, const UnicodeString & Target, bool Unix)
  880. {
  881. UnicodeString Name;
  882. if (Unix)
  883. {
  884. Name = UnixExtractFileName(Target);
  885. }
  886. else
  887. {
  888. Name = ExtractFileName(Target);
  889. }
  890. if (!IsFileNameMask(Name) && (FileList->Count > 1))
  891. {
  892. UnicodeString Message =
  893. RemoveEmptyLines(UnformatMessage(FormatMultiFilesToOneConfirmation(Target, Unix)));
  894. PrintLine(Message);
  895. }
  896. }
  897. //---------------------------------------------------------------------------
  898. void __fastcall TScript::CheckParams(TScriptProcParams * Parameters)
  899. {
  900. TScriptCommands::CheckParams(Parameters, false);
  901. }
  902. //---------------------------------------------------------------------------
  903. void __fastcall TScript::TransferParamParams(int & Params, TScriptProcParams * Parameters)
  904. {
  905. Params |= FLAGMASK(!FConfirm, cpNoConfirmation);
  906. if (Parameters->FindSwitch(DELETE_SWITCH))
  907. {
  908. Params |= cpDelete;
  909. }
  910. if (Parameters->FindSwitch(L"resume"))
  911. {
  912. Params |= cpResume;
  913. }
  914. else if (Parameters->FindSwitch(L"append"))
  915. {
  916. Params |= cpAppend;
  917. }
  918. }
  919. //---------------------------------------------------------------------------
  920. void __fastcall TScript::CopyParamParams(TCopyParamType & CopyParam, TScriptProcParams * Parameters)
  921. {
  922. UnicodeString Value;
  923. if (!FWantsProgress)
  924. {
  925. // total size is not visualized, hence it makes no sense to calculate it
  926. CopyParam.CalculateSize = false;
  927. }
  928. if (Parameters->FindSwitch(NOPRESERVETIME_SWITCH))
  929. {
  930. CopyParam.PreserveTime = false;
  931. CopyParam.PreserveTimeDirs = false;
  932. }
  933. if (Parameters->FindSwitch(PRESERVETIME_SWITCH, Value))
  934. {
  935. CopyParam.PreserveTime = true;
  936. if (SameText(Value, PRESERVETIMEDIRS_SWITCH_VALUE))
  937. {
  938. CopyParam.PreserveTimeDirs = true;
  939. }
  940. }
  941. if (Parameters->FindSwitch(NOPERMISSIONS_SWITCH))
  942. {
  943. CopyParam.PreserveRights = false;
  944. }
  945. if (Parameters->FindSwitch(PERMISSIONS_SWITCH, Value))
  946. {
  947. CopyParam.PreserveRights = true;
  948. CopyParam.Rights.Octal = Value;
  949. }
  950. if (Parameters->FindSwitch(SPEED_SWITCH, Value))
  951. {
  952. int CPSLimit;
  953. if (Value.IsEmpty())
  954. {
  955. CPSLimit = 0;
  956. }
  957. else
  958. {
  959. CPSLimit = StrToInt(Value) * 1024;
  960. if (CPSLimit < 0)
  961. {
  962. CPSLimit = 0;
  963. }
  964. }
  965. CopyParam.CPSLimit = CPSLimit;
  966. }
  967. if (Parameters->FindSwitch(TRANSFER_SWITCH, Value))
  968. {
  969. CopyParam.TransferMode = ParseTransferModeName(Value);
  970. }
  971. if (Parameters->FindSwitch(FILEMASK_SWITCH, Value))
  972. {
  973. CopyParam.IncludeFileMask = Value;
  974. if (FIncludeFileMaskOptionUsed)
  975. {
  976. PrintLine(LoadStr(SCRIPT_FILEMASK_INCLUDE_EXCLUDE));
  977. }
  978. }
  979. if (Parameters->FindSwitch(RESUMESUPPORT_SWITCH, Value))
  980. {
  981. int ToggleValue = TScriptCommands::FindCommand(ToggleNames,
  982. LENOF(ToggleNames), Value);
  983. if (ToggleValue >= 0)
  984. {
  985. switch (ToggleValue)
  986. {
  987. case ToggleOff:
  988. CopyParam.ResumeSupport = rsOff;
  989. break;
  990. case ToggleOn:
  991. CopyParam.ResumeSupport = rsOn;
  992. break;
  993. default:
  994. DebugFail();
  995. break;
  996. }
  997. }
  998. else
  999. {
  1000. int ThresholdValue;
  1001. if (!TryStrToInt(Value, ThresholdValue))
  1002. {
  1003. throw Exception(FMTLOAD(SCRIPT_VALUE_UNKNOWN, (RESUMESUPPORT_SWITCH, Value)));
  1004. }
  1005. CopyParam.ResumeSupport = rsSmart;
  1006. CopyParam.ResumeThreshold = ThresholdValue * 1024;
  1007. }
  1008. }
  1009. if (Parameters->FindSwitch(NONEWERONLY_SWICH))
  1010. {
  1011. CopyParam.NewerOnly = false;
  1012. }
  1013. if (Parameters->FindSwitch(NEWERONLY_SWICH))
  1014. {
  1015. CopyParam.NewerOnly = true;
  1016. }
  1017. std::unique_ptr<TStrings> RawSettings(new TStringList());
  1018. if (Parameters->FindSwitch(RAWTRANSFERSETTINGS_SWITCH, RawSettings.get()))
  1019. {
  1020. std::unique_ptr<TOptionsStorage> OptionsStorage(new TOptionsStorage(RawSettings.get(), false));
  1021. CopyParam.Load(OptionsStorage.get());
  1022. }
  1023. }
  1024. //---------------------------------------------------------------------------
  1025. void __fastcall TScript::ResetTransfer()
  1026. {
  1027. }
  1028. //---------------------------------------------------------------------------
  1029. bool __fastcall TScript::EnsureCommandSessionFallback(
  1030. TFSCapability Capability, TSessionAction & Action)
  1031. {
  1032. bool Result = FTerminal->IsCapable[Capability] ||
  1033. FTerminal->CommandSessionOpened;
  1034. if (!Result)
  1035. {
  1036. try
  1037. {
  1038. ConnectTerminal(FTerminal->CommandSession);
  1039. Result = true;
  1040. }
  1041. catch(Exception & E)
  1042. {
  1043. Action.Rollback(&E);
  1044. HandleExtendedException(&E, FTerminal->CommandSession);
  1045. Result = false;
  1046. }
  1047. }
  1048. return Result;
  1049. }
  1050. //---------------------------------------------------------------------------
  1051. void __fastcall TScript::HelpProc(TScriptProcParams * Parameters)
  1052. {
  1053. UnicodeString Output;
  1054. if (Parameters->ParamCount == 0)
  1055. {
  1056. UnicodeString Command;
  1057. UnicodeString Description;
  1058. int Index = 0;
  1059. while (FCommands->Enumerate(Index, &Command, &Description, NULL))
  1060. {
  1061. if (!Description.IsEmpty())
  1062. {
  1063. Output += FORMAT(L"%-8s %s\n", (Command, Description));
  1064. }
  1065. Index++;
  1066. }
  1067. }
  1068. else
  1069. {
  1070. for (int i = 1; i <= Parameters->ParamCount; i++)
  1071. {
  1072. UnicodeString Help;
  1073. if (FCommands->Info(Parameters->Param[i], NULL, &Help))
  1074. {
  1075. Output += Help;
  1076. }
  1077. else
  1078. {
  1079. throw Exception(FMTLOAD(SCRIPT_COMMAND_UNKNOWN, (Parameters->Param[i])));
  1080. }
  1081. }
  1082. }
  1083. Print(Output);
  1084. }
  1085. //---------------------------------------------------------------------------
  1086. void __fastcall TScript::CallProc(TScriptProcParams * Parameters)
  1087. {
  1088. CheckSession();
  1089. if (!FTerminal->IsCapable[fcAnyCommand] &&
  1090. !FTerminal->IsCapable[fcSecondaryShell])
  1091. {
  1092. NotSupported();
  1093. }
  1094. // this is used only to log failures to open separate shell session,
  1095. // the actual call logging is done in TTerminal::AnyCommand
  1096. TCallSessionAction Action(
  1097. FTerminal->ActionLog, Parameters->ParamsStr, FTerminal->CurrentDirectory);
  1098. if (EnsureCommandSessionFallback(fcAnyCommand, Action))
  1099. {
  1100. Action.Cancel();
  1101. FTerminal->AnyCommand(Parameters->ParamsStr, TerminalCaptureLog);
  1102. }
  1103. }
  1104. //---------------------------------------------------------------------------
  1105. void __fastcall TScript::EchoProc(TScriptProcParams * Parameters)
  1106. {
  1107. PrintLine(Parameters->ParamsStr);
  1108. }
  1109. //---------------------------------------------------------------------------
  1110. void __fastcall TScript::StatProc(TScriptProcParams * Parameters)
  1111. {
  1112. CheckSession();
  1113. UnicodeString Path = UnixExcludeTrailingBackslash(Parameters->Param[1]);
  1114. FTerminal->ExceptionOnFail = true;
  1115. TRemoteFile * File = NULL;
  1116. try
  1117. {
  1118. File = FTerminal->ReadFileListing(Path);
  1119. PrintLine(File->ListingStr);
  1120. }
  1121. __finally
  1122. {
  1123. FTerminal->ExceptionOnFail = false;
  1124. delete File;
  1125. }
  1126. }
  1127. //---------------------------------------------------------------------------
  1128. void __fastcall TScript::ChecksumProc(TScriptProcParams * Parameters)
  1129. {
  1130. CheckSession();
  1131. if (!FTerminal->IsCapable[fcCalculatingChecksum])
  1132. {
  1133. NotSupported();
  1134. }
  1135. UnicodeString Alg = Parameters->Param[1];
  1136. std::unique_ptr<TStrings> Checksums(new TStringList());
  1137. TStrings * FileList = CreateFileList(Parameters, 2, 2, fltQueryServer);
  1138. FTerminal->ExceptionOnFail = true;
  1139. try
  1140. {
  1141. if ((FileList->Count != 1) ||
  1142. DebugNotNull(dynamic_cast<TRemoteFile *>(FileList->Objects[0]))->IsDirectory)
  1143. {
  1144. throw Exception(FMTLOAD(NOT_FILE_ERROR, (FileList->Strings[0])));
  1145. }
  1146. FTerminal->CalculateFilesChecksum(Alg, FileList, Checksums.get(), NULL);
  1147. if (DebugAlwaysTrue(Checksums->Count == 1))
  1148. {
  1149. PrintLine(FORMAT(L"%s %s", (Checksums->Strings[0], FileList->Strings[0])));
  1150. }
  1151. }
  1152. __finally
  1153. {
  1154. FTerminal->ExceptionOnFail = false;
  1155. FreeFileList(FileList);
  1156. }
  1157. }
  1158. //---------------------------------------------------------------------------
  1159. void __fastcall TScript::TerminalCaptureLog(const UnicodeString & AddedLine,
  1160. TCaptureOutputType OutputType)
  1161. {
  1162. if ((OutputType == cotOutput) || (OutputType == cotError))
  1163. {
  1164. PrintLine(AddedLine);
  1165. }
  1166. }
  1167. //---------------------------------------------------------------------------
  1168. void __fastcall TScript::PwdProc(TScriptProcParams * /*Parameters*/)
  1169. {
  1170. CheckSession();
  1171. PrintLine(FTerminal->CurrentDirectory);
  1172. TCwdSessionAction Action(FTerminal->ActionLog, FTerminal->CurrentDirectory);
  1173. }
  1174. //---------------------------------------------------------------------------
  1175. void __fastcall TScript::CdProc(TScriptProcParams * Parameters)
  1176. {
  1177. CheckSession();
  1178. if (Parameters->ParamCount == 0)
  1179. {
  1180. FTerminal->HomeDirectory();
  1181. }
  1182. else
  1183. {
  1184. FTerminal->ChangeDirectory(Parameters->Param[1]);
  1185. }
  1186. PrintLine(FTerminal->CurrentDirectory);
  1187. }
  1188. //---------------------------------------------------------------------------
  1189. void __fastcall TScript::LsProc(TScriptProcParams * Parameters)
  1190. {
  1191. CheckSession();
  1192. UnicodeString Directory;
  1193. TFileMasks Mask;
  1194. bool HaveMask = false;
  1195. if (Parameters->ParamCount > 0)
  1196. {
  1197. Directory = Parameters->Param[1];
  1198. UnicodeString MaskStr = UnixExtractFileName(Directory);
  1199. HaveMask = TFileMasks::IsMask(MaskStr);
  1200. if (HaveMask)
  1201. {
  1202. Mask.SetMask(MaskStr);
  1203. Directory = UnixExtractFilePath(Directory);
  1204. }
  1205. }
  1206. if (Directory.IsEmpty())
  1207. {
  1208. Directory = FTerminal->CurrentDirectory;
  1209. }
  1210. TRemoteFileList * FileList = FTerminal->ReadDirectoryListing(Directory, Mask);
  1211. // on error user may select "skip", then we get NULL
  1212. if (FileList != NULL)
  1213. {
  1214. try
  1215. {
  1216. if (FileList->Count > 0)
  1217. {
  1218. for (int i = 0; i < FileList->Count; i++)
  1219. {
  1220. PrintLine(FileList->Files[i]->ListingStr);
  1221. }
  1222. }
  1223. else
  1224. {
  1225. if (HaveMask)
  1226. {
  1227. NoMatch(Mask.Masks, UnicodeString());
  1228. }
  1229. }
  1230. }
  1231. __finally
  1232. {
  1233. delete FileList;
  1234. }
  1235. }
  1236. }
  1237. //---------------------------------------------------------------------------
  1238. void __fastcall TScript::RmProc(TScriptProcParams * Parameters)
  1239. {
  1240. CheckSession();
  1241. bool OnlyFile = Parameters->FindSwitch(L"onlyfile");
  1242. TStrings * FileList = CreateFileList(
  1243. Parameters, 1, Parameters->ParamCount,
  1244. (TFileListType)(fltQueryServer | fltMask| FLAGMASK(OnlyFile, fltOnlyFile)));
  1245. try
  1246. {
  1247. CheckParams(Parameters);
  1248. FTerminal->DeleteFiles(FileList);
  1249. }
  1250. __finally
  1251. {
  1252. FreeFileList(FileList);
  1253. }
  1254. }
  1255. //---------------------------------------------------------------------------
  1256. void __fastcall TScript::RmDirProc(TScriptProcParams * Parameters)
  1257. {
  1258. CheckSession();
  1259. TStrings * FileList = CreateFileList(Parameters, 1, Parameters->ParamCount, fltDirectories);
  1260. try
  1261. {
  1262. FTerminal->DeleteFiles(FileList);
  1263. }
  1264. __finally
  1265. {
  1266. FreeFileList(FileList);
  1267. }
  1268. }
  1269. //---------------------------------------------------------------------------
  1270. void __fastcall TScript::DoMvOrCp(TScriptProcParams * Parameters, TFSCapability Capability, bool Cp)
  1271. {
  1272. CheckSession();
  1273. if (!FTerminal->IsCapable[Capability])
  1274. {
  1275. NotSupported();
  1276. }
  1277. TStrings * FileList =
  1278. CreateFileList(Parameters, 1, Parameters->ParamCount - 1, TFileListType(fltMask | fltQueryServer));
  1279. try
  1280. {
  1281. DebugAssert(Parameters->ParamCount >= 1);
  1282. UnicodeString Target = Parameters->Param[Parameters->ParamCount];
  1283. UnicodeString TargetDirectory = UnixExtractFilePath(Target);
  1284. UnicodeString FileMask = UnixExtractFileName(Target);
  1285. Target = UnixIncludeTrailingBackslash(TargetDirectory) + FileMask;
  1286. CheckMultiFilesToOne(FileList, Target, true);
  1287. if (Cp)
  1288. {
  1289. FTerminal->CopyFiles(FileList, TargetDirectory, FileMask);
  1290. }
  1291. else
  1292. {
  1293. FTerminal->MoveFiles(FileList, TargetDirectory, FileMask);
  1294. }
  1295. }
  1296. __finally
  1297. {
  1298. FreeFileList(FileList);
  1299. }
  1300. }
  1301. //---------------------------------------------------------------------------
  1302. void __fastcall TScript::MvProc(TScriptProcParams * Parameters)
  1303. {
  1304. DoMvOrCp(Parameters, fcRemoteMove, false);
  1305. }
  1306. //---------------------------------------------------------------------------
  1307. void __fastcall TScript::CpProc(TScriptProcParams * Parameters)
  1308. {
  1309. DoMvOrCp(Parameters, fcRemoteCopy, true);
  1310. }
  1311. //---------------------------------------------------------------------------
  1312. void __fastcall TScript::ChModProc(TScriptProcParams * Parameters)
  1313. {
  1314. CheckSession();
  1315. if (!FTerminal->IsCapable[fcModeChanging])
  1316. {
  1317. NotSupported();
  1318. }
  1319. TStrings * FileList = CreateFileList(Parameters, 2, Parameters->ParamCount,
  1320. fltMask);
  1321. try
  1322. {
  1323. TRemoteProperties Properties;
  1324. Properties.Valid = TValidProperties() << vpRights;
  1325. Properties.Rights.Octal = Parameters->Param[1];
  1326. FTerminal->ChangeFilesProperties(FileList, &Properties);
  1327. }
  1328. __finally
  1329. {
  1330. FreeFileList(FileList);
  1331. }
  1332. }
  1333. //---------------------------------------------------------------------------
  1334. void __fastcall TScript::LnProc(TScriptProcParams * Parameters)
  1335. {
  1336. CheckSession();
  1337. if (!FTerminal->IsCapable[fcSymbolicLink])
  1338. {
  1339. NotSupported();
  1340. }
  1341. DebugAssert(Parameters->ParamCount == 2);
  1342. FTerminal->CreateLink(Parameters->Param[2], Parameters->Param[1], true);
  1343. }
  1344. //---------------------------------------------------------------------------
  1345. void __fastcall TScript::MkDirProc(TScriptProcParams * Parameters)
  1346. {
  1347. CheckSession();
  1348. TRemoteProperties Properties;
  1349. Properties.Valid = TValidProperties() << vpEncrypt;
  1350. Properties.Encrypt = FCopyParam.EncryptNewFiles;
  1351. FTerminal->CreateDirectory(Parameters->Param[1], &Properties);
  1352. }
  1353. //---------------------------------------------------------------------------
  1354. void __fastcall TScript::GetProc(TScriptProcParams * Parameters)
  1355. {
  1356. CheckSession();
  1357. ResetTransfer();
  1358. bool Latest = Parameters->FindSwitch(L"latest");
  1359. bool OnlyFile = Parameters->FindSwitch(L"onlyfile");
  1360. CheckDefaultCopyParam();
  1361. TCopyParamType CopyParam = FCopyParam;
  1362. CopyParamParams(CopyParam, Parameters);
  1363. int Params = 0;
  1364. TransferParamParams(Params, Parameters);
  1365. RequireParams(Parameters, 1);
  1366. int LastFileParam = (Parameters->ParamCount == 1 ? 1 : Parameters->ParamCount - 1);
  1367. DebugAssert(CopyParam.OnTransferOut == NULL);
  1368. if ((OnTransferOut != NULL) && (Parameters->ParamCount > 1) && SameText(Parameters->Param[Parameters->ParamCount], InOutParam))
  1369. {
  1370. CopyParam.OnTransferOut = OnTransferOut;
  1371. OnlyFile = true;
  1372. }
  1373. TStrings * FileList = CreateFileList(Parameters, 1, LastFileParam,
  1374. (TFileListType)(fltQueryServer | fltMask | FLAGMASK(Latest, fltLatest) | FLAGMASK(OnlyFile, fltOnlyFile)));
  1375. try
  1376. {
  1377. UnicodeString TargetDirectory;
  1378. if (CopyParam.OnTransferOut == NULL)
  1379. {
  1380. if (Parameters->ParamCount == 1)
  1381. {
  1382. TargetDirectory = GetCurrentDir();
  1383. CopyParam.FileMask = L"";
  1384. }
  1385. else
  1386. {
  1387. UnicodeString Target = Parameters->Param[Parameters->ParamCount];
  1388. TargetDirectory = ExtractFilePath(Target);
  1389. if (TargetDirectory.IsEmpty())
  1390. {
  1391. TargetDirectory = GetCurrentDir();
  1392. }
  1393. CopyParam.FileMask = ExtractFileName(Target);
  1394. Target = IncludeTrailingBackslash(TargetDirectory) + CopyParam.FileMask;
  1395. CheckMultiFilesToOne(FileList, Target, false);
  1396. }
  1397. }
  1398. CheckParams(Parameters);
  1399. FTerminal->CopyToLocal(FileList, TargetDirectory, &CopyParam, Params, NULL);
  1400. }
  1401. __finally
  1402. {
  1403. FreeFileList(FileList);
  1404. }
  1405. }
  1406. //---------------------------------------------------------------------------
  1407. void __fastcall TScript::PutProc(TScriptProcParams * Parameters)
  1408. {
  1409. CheckSession();
  1410. ResetTransfer();
  1411. bool Latest = Parameters->FindSwitch(L"latest");
  1412. CheckDefaultCopyParam();
  1413. TCopyParamType CopyParam = FCopyParam;
  1414. CopyParamParams(CopyParam, Parameters);
  1415. int Params = 0;
  1416. TransferParamParams(Params, Parameters);
  1417. RequireParams(Parameters, 1);
  1418. int LastFileParam = (Parameters->ParamCount == 1 ? 1 : Parameters->ParamCount - 1);
  1419. DebugAssert(CopyParam.OnTransferIn == NULL);
  1420. TStrings * FileList;
  1421. // We use stdin only if - is the very first parameter
  1422. if ((OnTransferIn != NULL) && SameText(Parameters->Param[1], InOutParam))
  1423. {
  1424. if (Parameters->ParamCount > 2)
  1425. {
  1426. throw Exception(LoadStr(STREAM_IN_SCRIPT_ERROR));
  1427. }
  1428. CopyParam.OnTransferIn = OnTransferIn;
  1429. FileList = new TStringList();
  1430. }
  1431. else
  1432. {
  1433. FileList = CreateLocalFileList(Parameters, 1, LastFileParam, (TFileListType)(fltMask | FLAGMASK(Latest, fltLatest)));
  1434. }
  1435. try
  1436. {
  1437. UnicodeString TargetDirectory;
  1438. if (Parameters->ParamCount == 1)
  1439. {
  1440. TargetDirectory = FTerminal->CurrentDirectory;
  1441. CopyParam.FileMask = L"";
  1442. }
  1443. else
  1444. {
  1445. UnicodeString Target = Parameters->Param[Parameters->ParamCount];
  1446. TargetDirectory = UnixExtractFilePath(Target);
  1447. if (TargetDirectory.IsEmpty())
  1448. {
  1449. TargetDirectory = FTerminal->CurrentDirectory;
  1450. }
  1451. CopyParam.FileMask = UnixExtractFileName(Target);
  1452. Target = UnixIncludeTrailingBackslash(TargetDirectory) + CopyParam.FileMask;
  1453. CheckMultiFilesToOne(FileList, Target, true);
  1454. }
  1455. if (CopyParam.OnTransferIn != NULL)
  1456. {
  1457. if (IsFileNameMask(CopyParam.FileMask))
  1458. {
  1459. throw Exception(LoadStr(STREAM_IN_SCRIPT_ERROR));
  1460. }
  1461. FileList->Add(CopyParam.FileMask);
  1462. }
  1463. CheckParams(Parameters);
  1464. FTerminal->CopyToRemote(FileList, TargetDirectory, &CopyParam, Params, NULL);
  1465. }
  1466. __finally
  1467. {
  1468. delete FileList;
  1469. }
  1470. }
  1471. //---------------------------------------------------------------------------
  1472. TTransferMode __fastcall TScript::ParseTransferModeName(UnicodeString Name)
  1473. {
  1474. DebugAssert((tmBinary == 0) && (tmAscii == 1) && (tmAutomatic == 2));
  1475. int Value = TScriptCommands::FindCommand(TransferModeNames,
  1476. TransferModeNamesCount, Name);
  1477. if (Value < 0)
  1478. {
  1479. throw Exception(FMTLOAD(SCRIPT_VALUE_UNKNOWN, (L"transfer", Name)));
  1480. }
  1481. return (TTransferMode)Value;
  1482. }
  1483. //---------------------------------------------------------------------------
  1484. void __fastcall TScript::OptionImpl(UnicodeString OptionName, UnicodeString ValueName)
  1485. {
  1486. enum { Echo, Batch, Confirm, Transfer, SynchDelete, Exclude, Include, ReconnectTime, FailOnNoMatch };
  1487. static const wchar_t * Names[] = { L"echo", L"batch", L"confirm", L"transfer",
  1488. L"synchdelete", L"exclude", L"include", L"reconnecttime", L"failonnomatch" };
  1489. DebugAssert((BatchOff == 0) && (BatchOn == 1) && (BatchAbort == 2) && (BatchContinue == 3));
  1490. static const wchar_t * BatchModeNames[] = { L"off", L"on", L"abort", L"continue" };
  1491. int Option = -1;
  1492. if (!OptionName.IsEmpty())
  1493. {
  1494. Option = TScriptCommands::FindCommand(Names, LENOF(Names), OptionName);
  1495. if (Option < 0)
  1496. {
  1497. throw Exception(FMTLOAD(SCRIPT_OPTION_UNKNOWN, (OptionName)));
  1498. }
  1499. else
  1500. {
  1501. OptionName = Names[Option];
  1502. }
  1503. }
  1504. #define OPT(OPT) ((Option < 0) || (Option == OPT))
  1505. const wchar_t * ListFormat = L"%-15s %-10s";
  1506. bool SetValue = !ValueName.IsEmpty();
  1507. bool PrintReconnectTime = false;
  1508. if (OPT(Echo))
  1509. {
  1510. if (SetValue)
  1511. {
  1512. int Value = TScriptCommands::FindCommand(ToggleNames, LENOF(ToggleNames), ValueName);
  1513. if (Value < 0)
  1514. {
  1515. throw Exception(FMTLOAD(SCRIPT_VALUE_UNKNOWN, (ValueName, OptionName)));
  1516. }
  1517. FEcho = (Value == ToggleOn);
  1518. }
  1519. PrintLine(FORMAT(ListFormat, (Names[Echo], ToggleNames[FEcho ? ToggleOn : ToggleOff])));
  1520. }
  1521. if (OPT(Batch))
  1522. {
  1523. if (SetValue)
  1524. {
  1525. int Value = TScriptCommands::FindCommand(BatchModeNames, LENOF(BatchModeNames), ValueName);
  1526. if (Value < 0)
  1527. {
  1528. throw Exception(FMTLOAD(SCRIPT_VALUE_UNKNOWN, (ValueName, OptionName)));
  1529. }
  1530. FBatch = (TBatchMode)Value;
  1531. FInteractiveBatch = FBatch;
  1532. if (SetValue && (FBatch != BatchOff) && (FSessionReopenTimeout == 0))
  1533. {
  1534. FSessionReopenTimeout = BatchSessionReopenTimeout;
  1535. FInteractiveSessionReopenTimeout = FSessionReopenTimeout;
  1536. PrintReconnectTime = true;
  1537. }
  1538. }
  1539. PrintLine(FORMAT(ListFormat, (Names[Batch], BatchModeNames[FBatch])));
  1540. }
  1541. if (OPT(Confirm))
  1542. {
  1543. if (SetValue)
  1544. {
  1545. int Value = TScriptCommands::FindCommand(ToggleNames, LENOF(ToggleNames), ValueName);
  1546. if (Value < 0)
  1547. {
  1548. throw Exception(FMTLOAD(SCRIPT_VALUE_UNKNOWN, (ValueName, OptionName)));
  1549. }
  1550. FConfirm = (Value == ToggleOn);
  1551. FInteractiveConfirm = FConfirm;
  1552. }
  1553. PrintLine(FORMAT(ListFormat, (Names[Confirm], ToggleNames[FConfirm ? ToggleOn : ToggleOff])));
  1554. }
  1555. // omit the option in listing
  1556. if (Option == Transfer)
  1557. {
  1558. if (SetValue)
  1559. {
  1560. FCopyParam.TransferMode = ParseTransferModeName(ValueName);
  1561. }
  1562. DebugAssert(FCopyParam.TransferMode < (TTransferMode)TransferModeNamesCount);
  1563. const wchar_t * Value = TransferModeNames[FCopyParam.TransferMode];
  1564. PrintLine(FORMAT(ListFormat, (Names[Transfer], Value)));
  1565. }
  1566. // omit the option in listing
  1567. if (Option == SynchDelete)
  1568. {
  1569. if (SetValue)
  1570. {
  1571. int Value = TScriptCommands::FindCommand(ToggleNames, LENOF(ToggleNames), ValueName);
  1572. if (Value < 0)
  1573. {
  1574. throw Exception(FMTLOAD(SCRIPT_VALUE_UNKNOWN, (ValueName, OptionName)));
  1575. }
  1576. FSynchronizeParams =
  1577. (FSynchronizeParams & ~TTerminal::spDelete) |
  1578. FLAGMASK(Value == ToggleOn, TTerminal::spDelete);
  1579. }
  1580. PrintLine(FORMAT(ListFormat, (Names[SynchDelete],
  1581. ToggleNames[FLAGSET(FSynchronizeParams, TTerminal::spDelete) ? ToggleOn : ToggleOff])));
  1582. }
  1583. static const wchar_t * Clear = L"clear";
  1584. // omit the option in listing
  1585. if (Option == Include)
  1586. {
  1587. if (SetValue)
  1588. {
  1589. FCopyParam.IncludeFileMask =
  1590. (ValueName == Clear ? UnicodeString() : ValueName);
  1591. FIncludeFileMaskOptionUsed = (ValueName != Clear);
  1592. }
  1593. PrintLine(FORMAT(ListFormat, (Names[Include], FCopyParam.IncludeFileMask.Masks)));
  1594. }
  1595. // omit the option in listing
  1596. if (Option == Exclude)
  1597. {
  1598. if (SetValue)
  1599. {
  1600. // will throw if ValueName already includes IncludeExcludeFileMasksDelimiter
  1601. FCopyParam.IncludeFileMask =
  1602. (ValueName == Clear ? UnicodeString() : UnicodeString(IncludeExcludeFileMasksDelimiter) + ValueName);
  1603. FIncludeFileMaskOptionUsed = (ValueName != Clear);
  1604. }
  1605. PrintLine(FORMAT(ListFormat, (Names[Include], FCopyParam.IncludeFileMask.Masks)));
  1606. }
  1607. if (OPT(ReconnectTime) || PrintReconnectTime)
  1608. {
  1609. if (SetValue && !PrintReconnectTime)
  1610. {
  1611. int Value;
  1612. if (SameText(ValueName, ToggleNames[ToggleOff]))
  1613. {
  1614. Value = 0;
  1615. }
  1616. else
  1617. {
  1618. if (!TryStrToInt(ValueName, Value))
  1619. {
  1620. throw Exception(FMTLOAD(SCRIPT_VALUE_UNKNOWN, (ValueName, OptionName)));
  1621. }
  1622. else
  1623. {
  1624. Value *= MSecsPerSec;
  1625. }
  1626. }
  1627. FSessionReopenTimeout = Value;
  1628. FInteractiveSessionReopenTimeout = FSessionReopenTimeout;
  1629. }
  1630. if (FSessionReopenTimeout == 0)
  1631. {
  1632. ValueName = ToggleNames[ToggleOff];
  1633. }
  1634. else
  1635. {
  1636. ValueName = IntToStr(FSessionReopenTimeout / MSecsPerSec);
  1637. }
  1638. PrintLine(FORMAT(ListFormat, (Names[ReconnectTime], ValueName)));
  1639. }
  1640. if (OPT(FailOnNoMatch))
  1641. {
  1642. if (SetValue)
  1643. {
  1644. int Value = TScriptCommands::FindCommand(ToggleNames, LENOF(ToggleNames), ValueName);
  1645. if (Value < 0)
  1646. {
  1647. throw Exception(FMTLOAD(SCRIPT_VALUE_UNKNOWN, (ValueName, OptionName)));
  1648. }
  1649. FFailOnNoMatch = (Value == ToggleOn);
  1650. }
  1651. PrintLine(FORMAT(ListFormat, (Names[FailOnNoMatch], ToggleNames[FFailOnNoMatch ? ToggleOn : ToggleOff])));
  1652. }
  1653. #undef OPT
  1654. }
  1655. //---------------------------------------------------------------------------
  1656. void __fastcall TScript::OptionProc(TScriptProcParams * Parameters)
  1657. {
  1658. UnicodeString OptionName;
  1659. UnicodeString ValueName;
  1660. if (Parameters->ParamCount >= 1)
  1661. {
  1662. OptionName = Parameters->Param[1];
  1663. }
  1664. if (Parameters->ParamCount >= 2)
  1665. {
  1666. ValueName = Parameters->Param[2];
  1667. }
  1668. OptionImpl(OptionName, ValueName);
  1669. }
  1670. //---------------------------------------------------------------------------
  1671. void __fastcall TScript::AsciiProc(TScriptProcParams * /*Parameters*/)
  1672. {
  1673. OptionImpl(L"transfer", L"ascii");
  1674. }
  1675. //---------------------------------------------------------------------------
  1676. void __fastcall TScript::BinaryProc(TScriptProcParams * /*Parameters*/)
  1677. {
  1678. OptionImpl(L"transfer", L"binary");
  1679. }
  1680. //---------------------------------------------------------------------------
  1681. void __fastcall TScript::SynchronizeDirectories(TScriptProcParams * Parameters,
  1682. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory, int FirstParam)
  1683. {
  1684. if (Parameters->ParamCount >= FirstParam)
  1685. {
  1686. LocalDirectory = Parameters->Param[FirstParam];
  1687. }
  1688. else
  1689. {
  1690. LocalDirectory = GetCurrentDir();
  1691. }
  1692. if (Parameters->ParamCount >= FirstParam + 1)
  1693. {
  1694. RemoteDirectory = Parameters->Param[FirstParam + 1];
  1695. }
  1696. else
  1697. {
  1698. RemoteDirectory = FTerminal->CurrentDirectory;
  1699. }
  1700. }
  1701. //---------------------------------------------------------------------------
  1702. UnicodeString __fastcall TScript::SynchronizeFileRecord(
  1703. const UnicodeString & RootDirectory, const TSynchronizeChecklist::TItem * Item, bool Local)
  1704. {
  1705. const TSynchronizeChecklist::TItem::TFileInfo & FileInfo =
  1706. Local ? Item->Local : Item->Remote;
  1707. UnicodeString Path;
  1708. if (Local)
  1709. {
  1710. Path = IncludeTrailingBackslash(FileInfo.Directory) + FileInfo.FileName;
  1711. }
  1712. else
  1713. {
  1714. Path = UnixIncludeTrailingBackslash(FileInfo.Directory) + FileInfo.FileName;
  1715. }
  1716. if (SameText(RootDirectory, Path.SubString(1, RootDirectory.Length())))
  1717. {
  1718. Path[1] = L'.';
  1719. Path.Delete(2, RootDirectory.Length() - 2);
  1720. }
  1721. UnicodeString Result;
  1722. if (Item->IsDirectory)
  1723. {
  1724. if (Local)
  1725. {
  1726. Result = IncludeTrailingBackslash(Path);
  1727. }
  1728. else
  1729. {
  1730. Result = UnixIncludeTrailingBackslash(Path);
  1731. }
  1732. }
  1733. else
  1734. {
  1735. UnicodeString SizeStr = IntToStr(FileInfo.Size);
  1736. UnicodeString ModificationStr =
  1737. ::ModificationStr(FileInfo.Modification, FileInfo.ModificationFmt);
  1738. Result = FORMAT("%s [%s, %s]", (Path, SizeStr, ModificationStr));
  1739. }
  1740. return Result;
  1741. }
  1742. //---------------------------------------------------------------------------
  1743. void __fastcall TScript::SynchronizePreview(
  1744. UnicodeString LocalDirectory, UnicodeString RemoteDirectory,
  1745. TSynchronizeChecklist * Checklist)
  1746. {
  1747. LocalDirectory = IncludeTrailingBackslash(LocalDirectory);
  1748. RemoteDirectory = UnixIncludeTrailingBackslash(RemoteDirectory);
  1749. for (int Index = 0; (Index < Checklist->Count); Index++)
  1750. {
  1751. const TSynchronizeChecklist::TItem * Item = Checklist->Item[Index];
  1752. if (Item->Checked)
  1753. {
  1754. TDifferenceSessionAction Action(FTerminal->ActionLog, Item);
  1755. UnicodeString Message;
  1756. UnicodeString LocalRecord = SynchronizeFileRecord(LocalDirectory, Item, true);
  1757. UnicodeString RemoteRecord = SynchronizeFileRecord(RemoteDirectory, Item, false);
  1758. switch (Item->Action)
  1759. {
  1760. case TSynchronizeChecklist::saUploadNew:
  1761. Message =
  1762. FMTLOAD(SCRIPT_SYNC_UPLOAD_NEW, (LocalRecord));
  1763. break;
  1764. case TSynchronizeChecklist::saDownloadNew:
  1765. Message =
  1766. FMTLOAD(SCRIPT_SYNC_DOWNLOAD_NEW, (RemoteRecord));
  1767. break;
  1768. case TSynchronizeChecklist::saUploadUpdate:
  1769. Message =
  1770. FMTLOAD(SCRIPT_SYNC_UPLOAD_UPDATE,
  1771. (LocalRecord, RemoteRecord));
  1772. break;
  1773. case TSynchronizeChecklist::saDownloadUpdate:
  1774. Message =
  1775. FMTLOAD(SCRIPT_SYNC_DOWNLOAD_UPDATE,
  1776. (RemoteRecord, LocalRecord));
  1777. break;
  1778. case TSynchronizeChecklist::saDeleteRemote:
  1779. Message =
  1780. FMTLOAD(SCRIPT_SYNC_DELETE_REMOTE, (RemoteRecord));
  1781. break;
  1782. case TSynchronizeChecklist::saDeleteLocal:
  1783. Message =
  1784. FMTLOAD(SCRIPT_SYNC_DELETE_LOCAL, (LocalRecord));
  1785. break;
  1786. default:
  1787. DebugFail();
  1788. }
  1789. PrintLine(Message);
  1790. }
  1791. }
  1792. }
  1793. //---------------------------------------------------------------------------
  1794. void __fastcall TScript::SynchronizeProc(TScriptProcParams * Parameters)
  1795. {
  1796. CheckSession();
  1797. ResetTransfer();
  1798. static const wchar_t * ModeNames[] = { L"remote", L"local", L"both" };
  1799. CheckDefaultCopyParam();
  1800. TCopyParamType CopyParam = FCopyParam;
  1801. CopyParamParams(CopyParam, Parameters);
  1802. RequireParams(Parameters, 1);
  1803. if (Parameters->ParamCount > 3)
  1804. {
  1805. throw Exception(FMTLOAD(SCRIPT_TOO_MANY_PARAMS, (L"synchronize")));
  1806. }
  1807. UnicodeString ModeName = Parameters->Param[1];
  1808. DebugAssert(FSynchronizeMode < 0);
  1809. FSynchronizeMode = TScriptCommands::FindCommand(ModeNames, LENOF(ModeNames), ModeName);
  1810. try
  1811. {
  1812. if (FSynchronizeMode < 0)
  1813. {
  1814. throw Exception(FMTLOAD(SCRIPT_OPTION_UNKNOWN, (ModeName)));
  1815. }
  1816. UnicodeString LocalDirectory;
  1817. UnicodeString RemoteDirectory;
  1818. SynchronizeDirectories(Parameters, LocalDirectory, RemoteDirectory, 2);
  1819. CheckDefaultSynchronizeParams();
  1820. int SynchronizeParams = FSynchronizeParams | TTerminal::spNoConfirmation;
  1821. UnicodeString Value;
  1822. if (Parameters->FindSwitch(L"delete"))
  1823. {
  1824. SynchronizeParams |= TTerminal::spDelete;
  1825. }
  1826. if (Parameters->FindSwitch(L"mirror") &&
  1827. (FSynchronizeMode != TTerminal::smBoth))
  1828. {
  1829. SynchronizeParams |= TTerminal::spMirror;
  1830. }
  1831. if (Parameters->FindSwitch(L"criteria", Value))
  1832. {
  1833. enum { None, Time, Size, Either, EitherBoth };
  1834. static const wchar_t * CriteriaNames[] = { L"none", L"time", L"size", L"either", L"both" };
  1835. int Criteria = TScriptCommands::FindCommand(CriteriaNames, LENOF(CriteriaNames), Value);
  1836. switch (Criteria)
  1837. {
  1838. case None:
  1839. SynchronizeParams |= TTerminal::spNotByTime;
  1840. SynchronizeParams &= ~TTerminal::spBySize;
  1841. break;
  1842. case Time:
  1843. SynchronizeParams &= ~(TTerminal::spNotByTime | TTerminal::spBySize);
  1844. break;
  1845. case Size:
  1846. SynchronizeParams |= TTerminal::spNotByTime | TTerminal::spBySize;
  1847. break;
  1848. case Either:
  1849. case EitherBoth:
  1850. SynchronizeParams &= ~TTerminal::spNotByTime;
  1851. SynchronizeParams |= TTerminal::spBySize;
  1852. break;
  1853. }
  1854. }
  1855. bool Preview = Parameters->FindSwitch(L"preview");
  1856. // enforce rules
  1857. if (FSynchronizeMode == TTerminal::smBoth)
  1858. {
  1859. SynchronizeParams &= ~(TTerminal::spNotByTime | TTerminal::spBySize);
  1860. }
  1861. CheckParams(Parameters);
  1862. PrintLine(LoadStr(SCRIPT_SYNCHRONIZE_COLLECTING));
  1863. TSynchronizeChecklist * Checklist =
  1864. FTerminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  1865. static_cast<TTerminal::TSynchronizeMode>(FSynchronizeMode),
  1866. &CopyParam, SynchronizeParams, OnTerminalSynchronizeDirectory, NULL);
  1867. try
  1868. {
  1869. bool AnyChecked = false;
  1870. for (int Index = 0; !AnyChecked && (Index < Checklist->Count); Index++)
  1871. {
  1872. AnyChecked = Checklist->Item[Index]->Checked;
  1873. }
  1874. if (AnyChecked)
  1875. {
  1876. if (Preview)
  1877. {
  1878. PrintLine(LoadStr(SCRIPT_SYNCHRONIZE_CHECKLIST));
  1879. SynchronizePreview(LocalDirectory, RemoteDirectory, Checklist);
  1880. }
  1881. else
  1882. {
  1883. PrintLine(LoadStr(SCRIPT_SYNCHRONIZE_SYNCHRONIZING));
  1884. FTerminal->SynchronizeApply(
  1885. Checklist, &CopyParam, SynchronizeParams, OnTerminalSynchronizeDirectory, NULL, NULL, NULL, NULL);
  1886. }
  1887. }
  1888. else
  1889. {
  1890. NoMatch(LoadStr(SCRIPT_SYNCHRONIZE_NODIFFERENCE));
  1891. }
  1892. }
  1893. __finally
  1894. {
  1895. delete Checklist;
  1896. }
  1897. }
  1898. __finally
  1899. {
  1900. FSynchronizeMode = -1;
  1901. }
  1902. }
  1903. //---------------------------------------------------------------------------
  1904. void __fastcall TScript::Synchronize(const UnicodeString LocalDirectory,
  1905. const UnicodeString RemoteDirectory, const TCopyParamType & CopyParam,
  1906. int SynchronizeParams, TSynchronizeChecklist ** Checklist)
  1907. {
  1908. try
  1909. {
  1910. FKeepingUpToDate = true;
  1911. TSynchronizeChecklist * AChecklist =
  1912. FTerminal->SynchronizeCollect(LocalDirectory, RemoteDirectory, TTerminal::smRemote,
  1913. &CopyParam, SynchronizeParams, NULL, NULL);
  1914. try
  1915. {
  1916. if (AChecklist->Count > 0)
  1917. {
  1918. FTerminal->SynchronizeApply(
  1919. AChecklist, &CopyParam, SynchronizeParams, OnTerminalSynchronizeDirectory, NULL, NULL, NULL, NULL);
  1920. }
  1921. }
  1922. __finally
  1923. {
  1924. if (Checklist == NULL)
  1925. {
  1926. delete AChecklist;
  1927. }
  1928. else
  1929. {
  1930. *Checklist = AChecklist;
  1931. }
  1932. }
  1933. // to break line after the last transfer (if any);
  1934. Print(L"");
  1935. FKeepingUpToDate = false;
  1936. }
  1937. catch(Exception & E)
  1938. {
  1939. FKeepingUpToDate = false;
  1940. HandleExtendedException(&E);
  1941. throw;
  1942. }
  1943. }
  1944. //---------------------------------------------------------------------------
  1945. void __fastcall TScript::KeepUpToDateProc(TScriptProcParams * Parameters)
  1946. {
  1947. if (OnSynchronizeStartStop == NULL)
  1948. {
  1949. Abort();
  1950. }
  1951. CheckSession();
  1952. ResetTransfer();
  1953. CheckDefaultCopyParam();
  1954. TCopyParamType CopyParam = FCopyParam;
  1955. CopyParamParams(CopyParam, Parameters);
  1956. UnicodeString LocalDirectory;
  1957. UnicodeString RemoteDirectory;
  1958. SynchronizeDirectories(Parameters, LocalDirectory, RemoteDirectory, 1);
  1959. CheckDefaultSynchronizeParams();
  1960. int SynchronizeParams = FSynchronizeParams | TTerminal::spNoConfirmation |
  1961. TTerminal::spNoRecurse | TTerminal::spUseCache | TTerminal::spDelayProgress |
  1962. TTerminal::spSubDirs;
  1963. if (Parameters->FindSwitch(L"delete"))
  1964. {
  1965. SynchronizeParams |= TTerminal::spDelete;
  1966. }
  1967. CheckParams(Parameters);
  1968. PrintLine(LoadStr(SCRIPT_KEEPING_UP_TO_DATE));
  1969. OnSynchronizeStartStop(this, LocalDirectory, RemoteDirectory, CopyParam, SynchronizeParams);
  1970. }
  1971. //---------------------------------------------------------------------------
  1972. //---------------------------------------------------------------------------
  1973. __fastcall TManagementScript::TManagementScript(TStoredSessionList * StoredSessions,
  1974. bool LimitedOutput) :
  1975. TScript(LimitedOutput)
  1976. {
  1977. DebugAssert(StoredSessions != NULL);
  1978. FOnInput = NULL;
  1979. FOnTerminalPromptUser = NULL;
  1980. FOnShowExtendedException = NULL;
  1981. FOnTerminalQueryUser = NULL;
  1982. FStoredSessions = StoredSessions;
  1983. FTerminalList = new TTerminalList(Configuration);
  1984. FOnQueryCancel = NULL;
  1985. FContinue = true;
  1986. OnTerminalSynchronizeDirectory = TerminalSynchronizeDirectory;
  1987. FCommands->Register(L"exit", SCRIPT_EXIT_DESC, SCRIPT_EXIT_HELP, &ExitProc, 0, 0, false);
  1988. FCommands->Register(L"bye", 0, SCRIPT_EXIT_HELP, &ExitProc, 0, 0, false);
  1989. FCommands->Register(L"open", SCRIPT_OPEN_DESC, SCRIPT_OPEN_HELP10, &OpenProc, 0, -1, true);
  1990. FCommands->Register(L"close", SCRIPT_CLOSE_DESC, SCRIPT_CLOSE_HELP, &CloseProc, 0, 1, false);
  1991. FCommands->Register(L"session", SCRIPT_SESSION_DESC, SCRIPT_SESSION_HELP, &SessionProc, 0, 1, false);
  1992. FCommands->Register(L"lpwd", SCRIPT_LPWD_DESC, SCRIPT_LPWD_HELP, &LPwdProc, 0, 0, false);
  1993. FCommands->Register(L"lcd", SCRIPT_LCD_DESC, SCRIPT_LCD_HELP, &LCdProc, 1, 1, false);
  1994. FCommands->Register(L"lls", SCRIPT_LLS_DESC, SCRIPT_LLS_HELP2, &LLsProc, 0, 1, false);
  1995. }
  1996. //---------------------------------------------------------------------------
  1997. __fastcall TManagementScript::~TManagementScript()
  1998. {
  1999. while (FTerminalList->Count > 0)
  2000. {
  2001. FreeTerminal(FTerminalList->Terminals[0]);
  2002. }
  2003. delete FTerminalList;
  2004. }
  2005. //---------------------------------------------------------------------------
  2006. void __fastcall TManagementScript::FreeTerminal(TTerminal * ATerminal)
  2007. {
  2008. TSessionData * Data = StoredSessions->FindSame(ATerminal->SessionData);
  2009. if (Data != NULL)
  2010. {
  2011. ATerminal->SessionData->RemoteDirectory = ATerminal->CurrentDirectory;
  2012. bool Changed = false;
  2013. if (ATerminal->SessionData->UpdateDirectories)
  2014. {
  2015. Data->RemoteDirectory = ATerminal->SessionData->RemoteDirectory;
  2016. Changed = true;
  2017. }
  2018. if (Changed)
  2019. {
  2020. // only modified, implicit
  2021. StoredSessions->Save(false, false);
  2022. }
  2023. }
  2024. FTerminalList->FreeTerminal(ATerminal);
  2025. }
  2026. //---------------------------------------------------------------------------
  2027. void __fastcall TManagementScript::Input(const UnicodeString Prompt,
  2028. UnicodeString & Str, bool AllowEmpty)
  2029. {
  2030. do
  2031. {
  2032. Str = L"";
  2033. if (FOnInput != NULL)
  2034. {
  2035. FOnInput(this, Prompt, Str);
  2036. }
  2037. else
  2038. {
  2039. Abort();
  2040. }
  2041. }
  2042. while (Str.Trim().IsEmpty() && !AllowEmpty);
  2043. }
  2044. //---------------------------------------------------------------------------
  2045. void __fastcall TManagementScript::PrintProgress(bool First, const UnicodeString Str)
  2046. {
  2047. if (FOnPrintProgress != NULL)
  2048. {
  2049. FOnPrintProgress(this, First, Str);
  2050. }
  2051. }
  2052. //---------------------------------------------------------------------------
  2053. void __fastcall TManagementScript::ResetTransfer()
  2054. {
  2055. TScript::ResetTransfer();
  2056. FLastProgressFile = L"";
  2057. FLastProgressTime = 0;
  2058. FLastProgressEventTime = 0;
  2059. FLastProgressMessage = L"";
  2060. }
  2061. //---------------------------------------------------------------------------
  2062. bool __fastcall TManagementScript::QueryCancel()
  2063. {
  2064. bool Result = false;
  2065. if (OnQueryCancel != NULL)
  2066. {
  2067. OnQueryCancel(this, Result);
  2068. }
  2069. return Result;
  2070. }
  2071. //---------------------------------------------------------------------------
  2072. void __fastcall TManagementScript::TerminalInformation(TTerminal * ATerminal,
  2073. const UnicodeString & Str, bool /*Status*/, int Phase)
  2074. {
  2075. DebugAssert(ATerminal != NULL);
  2076. if ((Phase < 0) && (ATerminal->Status == ssOpening))
  2077. {
  2078. PrintLine(Str);
  2079. }
  2080. }
  2081. //---------------------------------------------------------------------------
  2082. void __fastcall TManagementScript::TerminalPromptUser(TTerminal * ATerminal,
  2083. TPromptKind Kind, UnicodeString Name, UnicodeString Instructions, TStrings * Prompts,
  2084. TStrings * Results, bool & Result, void * Arg)
  2085. {
  2086. // When authentication using stored password fails,
  2087. // do not ask user for another password.
  2088. if ((!ATerminal->StoredCredentialsTried ||
  2089. !IsAuthenticationPrompt(Kind) ||
  2090. (Prompts->Count == 0)) && // allow instructions-only prompts
  2091. (OnTerminalPromptUser != NULL))
  2092. {
  2093. OnTerminalPromptUser(ATerminal, Kind, Name, Instructions, Prompts, Results, Result, Arg);
  2094. }
  2095. }
  2096. //---------------------------------------------------------------------------
  2097. bool __fastcall TManagementScript::Synchronizing()
  2098. {
  2099. return (FKeepingUpToDate || (FSynchronizeMode >= 0));
  2100. }
  2101. //---------------------------------------------------------------------------
  2102. void __fastcall TManagementScript::ShowPendingProgress()
  2103. {
  2104. if (!FSynchronizeIntro.IsEmpty())
  2105. {
  2106. if (Synchronizing())
  2107. {
  2108. PrintLine(FSynchronizeIntro);
  2109. }
  2110. FSynchronizeIntro = L"";
  2111. }
  2112. }
  2113. //---------------------------------------------------------------------------
  2114. void __fastcall TManagementScript::TerminalOperationProgress(
  2115. TFileOperationProgressType & ProgressData)
  2116. {
  2117. if ((ProgressData.Operation == foCopy) ||
  2118. (ProgressData.Operation == foMove))
  2119. {
  2120. if (ProgressData.InProgress && ProgressData.FileInProgress &&
  2121. !ProgressData.FileName.IsEmpty())
  2122. {
  2123. bool DoPrint = false;
  2124. bool First = false;
  2125. UnicodeString ProgressFileName = ProgressData.FileName;
  2126. if (ProgressData.Side == osLocal)
  2127. {
  2128. ProgressFileName = ExcludeTrailingBackslash(ProgressFileName);
  2129. }
  2130. else
  2131. {
  2132. ProgressFileName = UnixExcludeTrailingBackslash(ProgressFileName);
  2133. }
  2134. if (ProgressFileName != FLastProgressFile)
  2135. {
  2136. First = true;
  2137. DoPrint = true;
  2138. ShowPendingProgress();
  2139. }
  2140. time_t Time = time(NULL);
  2141. if ((OnProgress != NULL) && WantsProgress &&
  2142. (DoPrint || (FLastProgressEventTime != Time) || ProgressData.IsTransferDone()))
  2143. {
  2144. FLastProgressEventTime = Time;
  2145. TScriptProgress Progress;
  2146. Progress.Operation = ProgressData.Operation;
  2147. Progress.Side = ProgressData.Side;
  2148. Progress.FileName = ProgressData.FullFileName;
  2149. Progress.Directory = ProgressData.Directory;
  2150. Progress.OverallProgress = ProgressData.OverallProgress();
  2151. Progress.FileProgress = ProgressData.TransferProgress();
  2152. Progress.CPS = ProgressData.CPS();
  2153. Progress.Cancel = false;
  2154. OnProgress(this, Progress);
  2155. if (Progress.Cancel)
  2156. {
  2157. ProgressData.SetCancel(csCancel);
  2158. }
  2159. }
  2160. if (!DoPrint && ((FLastProgressTime != Time) || ProgressData.IsTransferDone()))
  2161. {
  2162. DoPrint = true;
  2163. }
  2164. if (DoPrint)
  2165. {
  2166. UnicodeString FileName;
  2167. if (FLimitedOutput)
  2168. {
  2169. bool Unix = (ProgressData.Side == osRemote);
  2170. FileName = MinimizeName(ProgressFileName, Configuration->ScriptProgressFileNameLimit, Unix);
  2171. }
  2172. else
  2173. {
  2174. FileName = ProgressFileName;
  2175. }
  2176. UnicodeString TransferredSizeStr;
  2177. if (ProgressData.TransferredSize < 1024)
  2178. {
  2179. TransferredSizeStr = FORMAT("%d B", (static_cast<int>(ProgressData.TransferredSize)));
  2180. }
  2181. else
  2182. {
  2183. TransferredSizeStr = FORMAT("%d KB", (static_cast<int>(ProgressData.TransferredSize / 1024)));
  2184. }
  2185. UnicodeString ProgressMessage = FORMAT(L"%-*s | %14s | %6.1f KB/s | %-6.6s | %3d%%",
  2186. (Configuration->ScriptProgressFileNameLimit, FileName,
  2187. TransferredSizeStr,
  2188. static_cast<float>(ProgressData.CPS()) / 1024,
  2189. ProgressData.AsciiTransfer ? L"ascii" : L"binary",
  2190. ProgressData.TransferProgress()));
  2191. if (FLastProgressMessage != ProgressMessage)
  2192. {
  2193. FLastProgressTime = Time;
  2194. PrintProgress(First, ProgressMessage);
  2195. FLastProgressMessage = ProgressMessage;
  2196. FLastProgressFile = ProgressFileName;
  2197. }
  2198. }
  2199. }
  2200. else
  2201. {
  2202. FLastProgressFile = L"";
  2203. }
  2204. }
  2205. if (QueryCancel())
  2206. {
  2207. ProgressData.SetCancel(csCancel);
  2208. }
  2209. }
  2210. //---------------------------------------------------------------------------
  2211. void __fastcall TManagementScript::TerminalOperationFinished(
  2212. TFileOperation Operation, TOperationSide /*Side*/,
  2213. bool /*Temp*/, const UnicodeString & FileName, Boolean Success,
  2214. TOnceDoneOperation & /*OnceDoneOperation*/)
  2215. {
  2216. if (Success &&
  2217. (Operation != foCalculateSize) && (Operation != foCalculateChecksum) &&
  2218. (Operation != foCopy) && (Operation != foMove))
  2219. {
  2220. ShowPendingProgress();
  2221. // For FKeepingUpToDate we should send events to synchronize controller eventually.
  2222. if (Synchronizing() && (Operation == foDelete))
  2223. {
  2224. // Note that this is duplicated with "keep up to date" log.
  2225. PrintLine(FMTLOAD(SCRIPT_SYNCHRONIZE_DELETED, (FileName)));
  2226. }
  2227. else
  2228. {
  2229. PrintLine(FileName);
  2230. }
  2231. }
  2232. }
  2233. //---------------------------------------------------------------------------
  2234. void __fastcall TManagementScript::TerminalSynchronizeDirectory(
  2235. const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  2236. bool & Continue, bool Collect, const TSynchronizeOptions *)
  2237. {
  2238. int SynchronizeMode = FSynchronizeMode;
  2239. if (FKeepingUpToDate)
  2240. {
  2241. SynchronizeMode = TTerminal::smRemote;
  2242. }
  2243. UnicodeString Arrow;
  2244. switch (SynchronizeMode)
  2245. {
  2246. case TTerminal::smRemote:
  2247. Arrow = L"=>";
  2248. break;
  2249. case TTerminal::smLocal:
  2250. Arrow = L"<=";
  2251. break;
  2252. case TTerminal::smBoth:
  2253. Arrow = L"<=>";
  2254. break;
  2255. }
  2256. UnicodeString Progress = FMTLOAD(SCRIPT_SYNCHRONIZE, (ExcludeTrailingBackslash(LocalDirectory),
  2257. Arrow, UnixExcludeTrailingBackslash(RemoteDirectory)));
  2258. if (Collect)
  2259. {
  2260. PrintProgress(false, Progress);
  2261. }
  2262. else
  2263. {
  2264. FSynchronizeIntro = Progress;
  2265. }
  2266. if (QueryCancel())
  2267. {
  2268. Continue = false;
  2269. }
  2270. }
  2271. //---------------------------------------------------------------------------
  2272. void __fastcall TManagementScript::TerminalInitializeLog(TObject * Sender)
  2273. {
  2274. TTerminal * ATerminal = dynamic_cast<TTerminal *>(Sender);
  2275. if (DebugAlwaysTrue(ATerminal != NULL))
  2276. {
  2277. LogPendingLines(ATerminal);
  2278. }
  2279. }
  2280. //---------------------------------------------------------------------------
  2281. TTerminal * __fastcall TManagementScript::FindSession(const UnicodeString Index)
  2282. {
  2283. int i = StrToIntDef(Index, -1);
  2284. if ((i <= 0) || (i > FTerminalList->Count))
  2285. {
  2286. throw Exception(FMTLOAD(SCRIPT_SESSION_INDEX_INVALID, (Index)));
  2287. }
  2288. else
  2289. {
  2290. return FTerminalList->Terminals[i - 1];
  2291. }
  2292. }
  2293. //---------------------------------------------------------------------------
  2294. void __fastcall TManagementScript::PrintActiveSession()
  2295. {
  2296. DebugAssert(FTerminal != NULL);
  2297. PrintLine(FMTLOAD(SCRIPT_ACTIVE_SESSION,
  2298. (FTerminalList->IndexOf(FTerminal) + 1, FTerminal->SessionData->SessionName)));
  2299. }
  2300. //---------------------------------------------------------------------------
  2301. bool __fastcall TManagementScript::HandleExtendedException(Exception * E,
  2302. TTerminal * ATerminal)
  2303. {
  2304. bool Result = TScript::HandleExtendedException(E, ATerminal);
  2305. if (ATerminal == NULL)
  2306. {
  2307. ATerminal = FTerminal;
  2308. }
  2309. if ((ATerminal != NULL) && (ATerminal == FTerminal) && (dynamic_cast<EFatal*>(E) != NULL))
  2310. {
  2311. try
  2312. {
  2313. DoClose(ATerminal);
  2314. }
  2315. catch(...)
  2316. {
  2317. // ignore disconnect errors
  2318. }
  2319. }
  2320. return Result;
  2321. }
  2322. //---------------------------------------------------------------------------
  2323. void __fastcall TManagementScript::MaskPasswordInCommandLine(UnicodeString & Command, bool Recurse)
  2324. {
  2325. UnicodeString Url;
  2326. UnicodeString MaskedParamsPre;
  2327. UnicodeString MaskedParamsPost;
  2328. UnicodeString Param;
  2329. UnicodeString RawParam;
  2330. UnicodeString Separator;
  2331. UnicodeString Separator2;
  2332. UnicodeString OptionWithParameters;
  2333. bool AnyMaskedParam = false;
  2334. TOptions Options;
  2335. UnicodeString ACommand = Command;
  2336. bool SubCommands = false;
  2337. while (CutToken(ACommand, Param, &RawParam, &Separator2))
  2338. {
  2339. Options.Add(Param);
  2340. // "Param 1" is the "winscp.exe" or "open"
  2341. if ((Options.ParamCount == 2) && Url.IsEmpty() && !SubCommands)
  2342. {
  2343. Url = Param;
  2344. }
  2345. else
  2346. {
  2347. UnicodeString & MaskedParams = Url.IsEmpty() ? MaskedParamsPre : MaskedParamsPost;
  2348. UnicodeString Switch;
  2349. wchar_t SwitchMark;
  2350. if (Options.WasSwitchAdded(Switch, SwitchMark))
  2351. {
  2352. OptionWithParameters = L"";
  2353. if (TSessionData::IsSensitiveOption(Switch))
  2354. {
  2355. // We should use something like TProgramParams::FormatSwitch here
  2356. RawParam = FORMAT(L"%s%s=%s", (SwitchMark, Switch, PasswordMask));
  2357. AnyMaskedParam = true;
  2358. }
  2359. else if (TSessionData::IsOptionWithParameters(Switch))
  2360. {
  2361. OptionWithParameters = Switch;
  2362. }
  2363. SubCommands = SameText(Switch, COMMAND_SWITCH);
  2364. }
  2365. else
  2366. {
  2367. if (!OptionWithParameters.IsEmpty())
  2368. {
  2369. if (TSessionData::MaskPasswordInOptionParameter(OptionWithParameters, Param))
  2370. {
  2371. RawParam = Param;
  2372. AnyMaskedParam = true;
  2373. }
  2374. }
  2375. if (Recurse && SubCommands)
  2376. {
  2377. UnicodeString Cmd2 = Param;
  2378. UnicodeString Command2;
  2379. if (CutToken(Cmd2, Command2))
  2380. {
  2381. UnicodeString MaskedParam = MaskPasswordInCommand(Param, Command2);
  2382. if (MaskedParam != Param)
  2383. {
  2384. RawParam = AddQuotes(EscapeParam(MaskedParam));
  2385. AnyMaskedParam = true;
  2386. }
  2387. }
  2388. }
  2389. }
  2390. // Separator is empty on the first loop, but so is the MaskedParams
  2391. AddToList(MaskedParams, RawParam, Separator);
  2392. }
  2393. Separator = Separator2;
  2394. }
  2395. if (!Url.IsEmpty() || AnyMaskedParam)
  2396. {
  2397. UnicodeString MaskedUrl;
  2398. if (!Url.IsEmpty())
  2399. {
  2400. bool DefaultsOnly;
  2401. std::unique_ptr<TSessionData> Data(
  2402. StoredSessions->ParseUrl(Url, &Options, DefaultsOnly, NULL, NULL, &MaskedUrl));
  2403. }
  2404. if ((Url != MaskedUrl) || AnyMaskedParam)
  2405. {
  2406. Command = MaskedParamsPre;
  2407. // AddToList is noop, when respective component is empty
  2408. AddToList(Command, AddQuotes(MaskedUrl), L" ");
  2409. AddToList(Command, MaskedParamsPost, L" ");
  2410. }
  2411. }
  2412. }
  2413. //---------------------------------------------------------------------------
  2414. UnicodeString __fastcall TManagementScript::MaskPasswordInCommand(const UnicodeString & FullCommand,
  2415. const UnicodeString & Command)
  2416. {
  2417. UnicodeString Result = FullCommand;
  2418. if (SameText(FCommands->ResolveCommand(Command), L"open") &&
  2419. !Configuration->LogSensitive)
  2420. {
  2421. MaskPasswordInCommandLine(Result, false);
  2422. }
  2423. return Result;
  2424. }
  2425. //---------------------------------------------------------------------------
  2426. UnicodeString __fastcall TManagementScript::GetLogCmd(const UnicodeString & FullCommand,
  2427. const UnicodeString & Command, const UnicodeString & Params)
  2428. {
  2429. UnicodeString Result = MaskPasswordInCommand(FullCommand, Command);
  2430. return TScript::GetLogCmd(Result, Command, Params);
  2431. }
  2432. //---------------------------------------------------------------------------
  2433. void __fastcall TManagementScript::Connect(const UnicodeString Session,
  2434. TOptions * Options, bool CheckParams)
  2435. {
  2436. try
  2437. {
  2438. bool DefaultsOnly;
  2439. TSessionData * Data;
  2440. if (Options->FindSwitch(L"filezilla"))
  2441. {
  2442. UnicodeString Error;
  2443. std::unique_ptr<TStoredSessionList> FilezillaSessionList(
  2444. Configuration->SelectFilezillaSessionsForImport(StoredSessions, Error));
  2445. if (!Error.IsEmpty())
  2446. {
  2447. throw Exception(Error);
  2448. }
  2449. Data = dynamic_cast<TSessionData *>(FilezillaSessionList->FindByName(Session));
  2450. if (Data == NULL)
  2451. {
  2452. throw Exception(FMTLOAD(FILEZILLA_SITE_NOT_EXIST, (Session)));
  2453. }
  2454. else
  2455. {
  2456. Data = Data->Clone();
  2457. }
  2458. }
  2459. else
  2460. {
  2461. if (FStoredSessions->IsFolder(Session) ||
  2462. FStoredSessions->IsWorkspace(Session))
  2463. {
  2464. throw Exception(LoadStr(CANNOT_OPEN_SESSION_FOLDER));
  2465. }
  2466. Data = FStoredSessions->ParseUrl(Session, Options, DefaultsOnly);
  2467. }
  2468. try
  2469. {
  2470. if (CheckParams)
  2471. {
  2472. if (Options->ParamCount > 1)
  2473. {
  2474. throw Exception(FMTLOAD(SCRIPT_TOO_MANY_PARAMS, (L"open")));
  2475. }
  2476. TScriptCommands::CheckParams(Options, false);
  2477. }
  2478. if (!Session.IsEmpty() && !Data->Name.IsEmpty() && (Batch != TScript::BatchOff) && !Interactive)
  2479. {
  2480. std::unique_ptr<TSessionData> DataWithFingerprint(Data->Clone());
  2481. DataWithFingerprint->LookupLastFingerprint();
  2482. DataWithFingerprint->MaskPasswords();
  2483. PrintLine(LoadStr(SCRIPT_SITE_WARNING));
  2484. PrintLine(L"open " + DataWithFingerprint->GenerateOpenCommandArgs(false));
  2485. }
  2486. DebugAssert(Data != NULL);
  2487. if (!Data->CanLogin || DefaultsOnly)
  2488. {
  2489. if (Data->HostName.IsEmpty())
  2490. {
  2491. UnicodeString Value;
  2492. Input(LoadStr(SCRIPT_HOST_PROMPT), Value, false);
  2493. Data->HostName = Value;
  2494. }
  2495. DebugAssert(Data->CanLogin);
  2496. }
  2497. bool WasLogActions = Configuration->LogActions;
  2498. TTerminal * ATerminal = FTerminalList->NewTerminal(Data);
  2499. try
  2500. {
  2501. ATerminal->AutoReadDirectory = false;
  2502. ATerminal->OnInformation = TerminalInformation;
  2503. ATerminal->OnPromptUser = TerminalPromptUser;
  2504. ATerminal->OnShowExtendedException = OnShowExtendedException;
  2505. ATerminal->OnQueryUser = OnTerminalQueryUser;
  2506. ATerminal->OnProgress = TerminalOperationProgress;
  2507. ATerminal->OnFinished = TerminalOperationFinished;
  2508. ATerminal->OnInitializeLog = TerminalInitializeLog;
  2509. ConnectTerminal(ATerminal);
  2510. }
  2511. catch(Exception & E)
  2512. {
  2513. // fatal error, most probably caused by XML logging failure (as it has been turned off),
  2514. // and XML log is required => abort
  2515. if ((dynamic_cast<EFatal *>(&E) != NULL) &&
  2516. WasLogActions && !Configuration->LogActions &&
  2517. Configuration->LogActionsRequired)
  2518. {
  2519. FContinue = false;
  2520. }
  2521. // make sure errors (mainly fatal ones) are associated
  2522. // with this terminal, not the last active one
  2523. bool Handled = HandleExtendedException(&E, ATerminal);
  2524. FTerminalList->FreeTerminal(ATerminal);
  2525. ATerminal = NULL;
  2526. if (!Handled)
  2527. {
  2528. throw;
  2529. }
  2530. }
  2531. if (ATerminal != NULL)
  2532. {
  2533. FTerminal = ATerminal;
  2534. if (!Data->LocalDirectory.IsEmpty())
  2535. {
  2536. try
  2537. {
  2538. DoChangeLocalDirectory(Data->LocalDirectoryExpanded);
  2539. }
  2540. catch(Exception & E)
  2541. {
  2542. if (!HandleExtendedException(&E))
  2543. {
  2544. throw;
  2545. }
  2546. }
  2547. }
  2548. PrintActiveSession();
  2549. }
  2550. }
  2551. __finally
  2552. {
  2553. delete Data;
  2554. }
  2555. }
  2556. catch(Exception & E)
  2557. {
  2558. if (!HandleExtendedException(&E))
  2559. {
  2560. throw;
  2561. }
  2562. }
  2563. }
  2564. //---------------------------------------------------------------------------
  2565. void __fastcall TManagementScript::DoClose(TTerminal * ATerminal)
  2566. {
  2567. int Index = FTerminalList->IndexOf(ATerminal);
  2568. DebugAssert(Index >= 0);
  2569. bool WasActiveTerminal = (FTerminal == ATerminal);
  2570. try
  2571. {
  2572. if (ATerminal->Active)
  2573. {
  2574. ATerminal->Close();
  2575. }
  2576. UnicodeString SessionName = ATerminal->SessionData->SessionName;
  2577. FreeTerminal(ATerminal);
  2578. if (WasActiveTerminal)
  2579. {
  2580. FTerminal = NULL;
  2581. }
  2582. PrintLine(FMTLOAD(SCRIPT_SESSION_CLOSED, (SessionName)));
  2583. }
  2584. __finally
  2585. {
  2586. if (WasActiveTerminal)
  2587. {
  2588. if (FTerminalList->Count > 0)
  2589. {
  2590. if (Index < FTerminalList->Count)
  2591. {
  2592. FTerminal = FTerminalList->Terminals[Index];
  2593. }
  2594. else
  2595. {
  2596. FTerminal = FTerminalList->Terminals[0];
  2597. }
  2598. PrintActiveSession();
  2599. }
  2600. else
  2601. {
  2602. PrintLine(LoadStr(SCRIPT_NO_SESSION));
  2603. }
  2604. }
  2605. }
  2606. }
  2607. //---------------------------------------------------------------------------
  2608. void __fastcall TManagementScript::DoChangeLocalDirectory(UnicodeString Directory)
  2609. {
  2610. if (!SetCurrentDir(ApiPath(Directory)))
  2611. {
  2612. throw EOSExtException(FMTLOAD(CHANGE_DIR_ERROR, (Directory)));
  2613. }
  2614. }
  2615. //---------------------------------------------------------------------------
  2616. void __fastcall TManagementScript::ExitProc(TScriptProcParams * /*Parameters*/)
  2617. {
  2618. FContinue = false;
  2619. }
  2620. //---------------------------------------------------------------------------
  2621. void __fastcall TManagementScript::OpenProc(TScriptProcParams * Parameters)
  2622. {
  2623. Connect(Parameters->ParamCount > 0 ? Parameters->Param[1] : UnicodeString(),
  2624. Parameters, true);
  2625. }
  2626. //---------------------------------------------------------------------------
  2627. void __fastcall TManagementScript::CloseProc(TScriptProcParams * Parameters)
  2628. {
  2629. CheckSession();
  2630. TTerminal * ATerminal;
  2631. if (Parameters->ParamCount == 0)
  2632. {
  2633. ATerminal = FTerminal;
  2634. }
  2635. else
  2636. {
  2637. ATerminal = FindSession(Parameters->Param[1]);
  2638. }
  2639. DoClose(ATerminal);
  2640. }
  2641. //---------------------------------------------------------------------------
  2642. void __fastcall TManagementScript::SessionProc(TScriptProcParams * Parameters)
  2643. {
  2644. CheckSession();
  2645. if (Parameters->ParamCount == 0)
  2646. {
  2647. for (int i = 0; i < FTerminalList->Count; i++)
  2648. {
  2649. PrintLine(FORMAT(L"%3d %s",
  2650. (i + 1, FTerminalList->Terminals[i]->SessionData->SessionName)));
  2651. }
  2652. PrintActiveSession();
  2653. }
  2654. else
  2655. {
  2656. FTerminal = FindSession(Parameters->Param[1]);
  2657. PrintActiveSession();
  2658. }
  2659. }
  2660. //---------------------------------------------------------------------------
  2661. void __fastcall TManagementScript::LPwdProc(TScriptProcParams * /*Parameters*/)
  2662. {
  2663. PrintLine(GetCurrentDir());
  2664. }
  2665. //---------------------------------------------------------------------------
  2666. void __fastcall TManagementScript::LCdProc(TScriptProcParams * Parameters)
  2667. {
  2668. DebugAssert(Parameters->ParamCount == 1);
  2669. DoChangeLocalDirectory(Parameters->Param[1]);
  2670. PrintLine(GetCurrentDir());
  2671. }
  2672. //---------------------------------------------------------------------------
  2673. void __fastcall TManagementScript::LLsProc(TScriptProcParams * Parameters)
  2674. {
  2675. UnicodeString Directory;
  2676. UnicodeString Mask;
  2677. if (Parameters->ParamCount > 0)
  2678. {
  2679. Directory = Parameters->Param[1];
  2680. Mask = ExtractFileName(Directory);
  2681. if (TFileMasks::IsMask(Mask))
  2682. {
  2683. Directory = ExtractFilePath(Directory);
  2684. }
  2685. else
  2686. {
  2687. Mask = L"";
  2688. }
  2689. }
  2690. if (Directory.IsEmpty())
  2691. {
  2692. Directory = GetCurrentDir();
  2693. }
  2694. if (Mask.IsEmpty())
  2695. {
  2696. Mask = L"*.*";
  2697. }
  2698. TSearchRecOwned SearchRec;
  2699. int FindAttrs = faReadOnly | faHidden | faSysFile | faDirectory | faArchive;
  2700. if (FindFirstUnchecked(IncludeTrailingBackslash(Directory) + Mask, FindAttrs, SearchRec) != 0)
  2701. {
  2702. NoMatch(Mask, ListingSysErrorMessage());
  2703. }
  2704. else
  2705. {
  2706. UnicodeString TimeFormat = FixedLenDateTimeFormat(FormatSettings.ShortTimeFormat);
  2707. UnicodeString DateFormat = FixedLenDateTimeFormat(FormatSettings.ShortDateFormat);
  2708. int DateLen = 0;
  2709. int TimeLen = 0;
  2710. bool First = true;
  2711. do
  2712. {
  2713. if (SearchRec.Name != L".")
  2714. {
  2715. TDateTime DateTime = SearchRec.GetLastWriteTime();
  2716. UnicodeString TimeStr = FormatDateTime(TimeFormat, DateTime);
  2717. UnicodeString DateStr = FormatDateTime(DateFormat, DateTime);
  2718. if (First)
  2719. {
  2720. if (TimeLen < TimeStr.Length())
  2721. {
  2722. TimeLen = TimeStr.Length();
  2723. }
  2724. if (DateLen < DateStr.Length())
  2725. {
  2726. DateLen = DateStr.Length();
  2727. }
  2728. First = false;
  2729. }
  2730. UnicodeString SizeStr;
  2731. if (SearchRec.IsDirectory())
  2732. {
  2733. SizeStr = L"<DIR>";
  2734. }
  2735. else
  2736. {
  2737. SizeStr = FORMAT(L"%14.0n", (double(SearchRec.Size)));
  2738. }
  2739. PrintLine(FORMAT(L"%-*s %-*s %-14s %s", (
  2740. DateLen, DateStr, TimeLen, TimeStr, SizeStr, SearchRec.Name)));
  2741. }
  2742. }
  2743. while (FindNextChecked(SearchRec) == 0);
  2744. }
  2745. }
  2746. //---------------------------------------------------------------------------
  2747. void __fastcall TManagementScript::ReflectSettings()
  2748. {
  2749. for (int i = 0; i < FTerminalList->Count; i++)
  2750. {
  2751. FTerminalList->Terminals[i]->ReflectSettings();
  2752. }
  2753. }
  2754. //---------------------------------------------------------------------------