Script.cpp 76 KB

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