Script.cpp 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041
  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. __fastcall TScriptProcParams::TScriptProcParams(AnsiString ParamsStr)
  16. {
  17. int P = FSwitchMarks.Pos("/");
  18. assert(P > 0);
  19. if (P > 0)
  20. {
  21. FSwitchMarks.Delete(P, 1);
  22. }
  23. FParamsStr = ParamsStr;
  24. AnsiString Param;
  25. while (CutToken(ParamsStr, Param))
  26. {
  27. Add(Param);
  28. }
  29. }
  30. //---------------------------------------------------------------------------
  31. //---------------------------------------------------------------------------
  32. class TScriptCommands : TStringList
  33. {
  34. public:
  35. typedef void __fastcall (__closure *TCommandProc)(TScriptProcParams * Parameters);
  36. __fastcall TScriptCommands();
  37. virtual __fastcall ~TScriptCommands();
  38. void __fastcall Execute(const AnsiString & Command, const AnsiString & Params);
  39. void __fastcall Register(const char * Command,
  40. const AnsiString Description, const AnsiString Help, TCommandProc Proc,
  41. int MinParams, int MaxParams, bool Switches);
  42. void __fastcall Register(const char * Command,
  43. int Description, int Help, TCommandProc Proc,
  44. int MinParams, int MaxParams, bool Switches);
  45. void __fastcall Register(const char * Command, TCommandProc Proc,
  46. int MinParams, int MaxParams, bool Switches);
  47. bool __fastcall Info(const AnsiString Command,
  48. AnsiString * Description, AnsiString * Help);
  49. bool __fastcall Enumerate(int Index,
  50. AnsiString * Command, AnsiString * Description, AnsiString * Help);
  51. static int __fastcall FindCommand(TStrings * Commands, const AnsiString Command,
  52. AnsiString * Matches = NULL);
  53. static int __fastcall FindCommand(const char ** Commands, size_t Count,
  54. const AnsiString Command, AnsiString * Matches = NULL);
  55. static void __fastcall CheckParams(TOptions * Parameters, bool Switches);
  56. protected:
  57. struct TScriptCommand
  58. {
  59. AnsiString Description;
  60. AnsiString Help;
  61. TCommandProc Proc;
  62. int MinParams;
  63. int MaxParams;
  64. bool Switches;
  65. };
  66. };
  67. //---------------------------------------------------------------------------
  68. __fastcall TScriptCommands::TScriptCommands()
  69. {
  70. Sorted = true;
  71. CaseSensitive = false;
  72. }
  73. //---------------------------------------------------------------------------
  74. __fastcall TScriptCommands::~TScriptCommands()
  75. {
  76. for (int Index = 0; Index < Count; Index++)
  77. {
  78. delete reinterpret_cast<TScriptCommand *>(Objects[Index]);
  79. }
  80. }
  81. //---------------------------------------------------------------------------
  82. void __fastcall TScriptCommands::Register(const char * Command,
  83. const AnsiString Description, const AnsiString Help, TCommandProc Proc,
  84. int MinParams, int MaxParams, bool Switches)
  85. {
  86. TScriptCommand * ScriptCommand = new TScriptCommand;
  87. ScriptCommand->Description = Description;
  88. ScriptCommand->Help = Help;
  89. ScriptCommand->Proc = Proc;
  90. ScriptCommand->MinParams = MinParams;
  91. ScriptCommand->MaxParams = MaxParams;
  92. ScriptCommand->Switches = Switches;
  93. AddObject(Command, reinterpret_cast<TObject *>(ScriptCommand));
  94. }
  95. //---------------------------------------------------------------------------
  96. void __fastcall TScriptCommands::Register(const char * Command,
  97. int Description, int Help, TCommandProc Proc,
  98. int MinParams, int MaxParams, bool Switches)
  99. {
  100. AnsiString ADescription;
  101. if (Description > 0)
  102. {
  103. ADescription = LoadStr(Description);
  104. }
  105. AnsiString AHelp;
  106. if (Help > 0)
  107. {
  108. AHelp = LoadStr(Help, 10240);
  109. }
  110. Register(Command, ADescription, AHelp, Proc, MinParams, MaxParams, Switches);
  111. }
  112. //---------------------------------------------------------------------------
  113. void __fastcall TScriptCommands::Register(const char * Command, TCommandProc Proc,
  114. int MinParams, int MaxParams, bool Switches)
  115. {
  116. Register(Command, "", "", Proc, MinParams, MaxParams, Switches);
  117. }
  118. //---------------------------------------------------------------------------
  119. bool __fastcall TScriptCommands::Info(const AnsiString Command,
  120. AnsiString * Description, AnsiString * Help)
  121. {
  122. int Index = FindCommand(this, Command);
  123. bool Result = (Index >= 0);
  124. if (Result)
  125. {
  126. TScriptCommand * ScriptCommand = reinterpret_cast<TScriptCommand *>(Objects[Index]);
  127. if (Description != NULL)
  128. {
  129. *Description = ScriptCommand->Description;
  130. }
  131. if (Help != NULL)
  132. {
  133. *Help = ScriptCommand->Help;
  134. }
  135. }
  136. return Result;
  137. }
  138. //---------------------------------------------------------------------------
  139. bool __fastcall TScriptCommands::Enumerate(int Index,
  140. AnsiString * Command, AnsiString * Description, AnsiString * Help)
  141. {
  142. bool Result = (Index < Count);
  143. if (Result)
  144. {
  145. TScriptCommand * ScriptCommand = reinterpret_cast<TScriptCommand *>(Objects[Index]);
  146. if (Command != NULL)
  147. {
  148. *Command = Strings[Index];
  149. }
  150. if (Description != NULL)
  151. {
  152. *Description = ScriptCommand->Description;
  153. }
  154. if (Help != NULL)
  155. {
  156. *Help = ScriptCommand->Help;
  157. }
  158. }
  159. return Result;
  160. }
  161. //---------------------------------------------------------------------------
  162. int __fastcall TScriptCommands::FindCommand(TStrings * Commands,
  163. const AnsiString Command, AnsiString * Matches)
  164. {
  165. int Result = Commands->IndexOf(Command);
  166. if (Result < 0)
  167. {
  168. int MatchesCount = 0;
  169. for (int i = 0; i < Commands->Count; i++)
  170. {
  171. if ((Command.Length() <= Commands->Strings[i].Length()) &&
  172. SameText(Command, Commands->Strings[i].SubString(1, Command.Length())))
  173. {
  174. if (Matches != NULL)
  175. {
  176. if (!Matches->IsEmpty())
  177. {
  178. *Matches += ", ";
  179. }
  180. *Matches += Commands->Strings[i];
  181. }
  182. MatchesCount++;
  183. Result = i;
  184. }
  185. }
  186. if (MatchesCount == 0)
  187. {
  188. Result = -1;
  189. }
  190. else if (MatchesCount > 1)
  191. {
  192. Result = -2;
  193. }
  194. }
  195. return Result;
  196. }
  197. //---------------------------------------------------------------------------
  198. int __fastcall TScriptCommands::FindCommand(const char ** Commands, size_t Count,
  199. const AnsiString Command, AnsiString * Matches)
  200. {
  201. int Result;
  202. TStringList * Strings = new TStringList;
  203. try
  204. {
  205. Strings->CaseSensitive = false;
  206. for (unsigned int i = 0; i < Count; i++)
  207. {
  208. Strings->Add(Commands[i]);
  209. }
  210. Result = FindCommand(Strings, Command, Matches);
  211. }
  212. __finally
  213. {
  214. delete Strings;
  215. }
  216. return Result;
  217. }
  218. //---------------------------------------------------------------------------
  219. void __fastcall TScriptCommands::CheckParams(TOptions * Parameters,
  220. bool Switches)
  221. {
  222. AnsiString Switch;
  223. if (!Switches && Parameters->UnusedSwitch(Switch))
  224. {
  225. throw Exception(FMTLOAD(SCRIPT_UNKNOWN_SWITCH, (Switch)));
  226. }
  227. }
  228. //---------------------------------------------------------------------------
  229. // parameters are by purpose passed by (constant) reference.
  230. // because if passed by value (copy), AnsiString reference is not for some reason
  231. // decreased on exit by exception, leading to memory leak
  232. void __fastcall TScriptCommands::Execute(const AnsiString & Command, const AnsiString & Params)
  233. {
  234. TScriptProcParams * Parameters = new TScriptProcParams(Params);
  235. try
  236. {
  237. AnsiString Matches;
  238. int Index = FindCommand(this, Command, &Matches);
  239. if (Index == -2)
  240. {
  241. throw Exception(FMTLOAD(SCRIPT_COMMAND_AMBIGUOUS, (Command, Matches)));
  242. }
  243. else if (Index < 0)
  244. {
  245. throw Exception(FMTLOAD(SCRIPT_COMMAND_UNKNOWN, (Command)));
  246. }
  247. TScriptCommand * ScriptCommand = reinterpret_cast<TScriptCommand *>(Objects[Index]);
  248. AnsiString FullCommand = Strings[Index];
  249. if (Parameters->ParamCount < ScriptCommand->MinParams)
  250. {
  251. throw Exception(FMTLOAD(SCRIPT_MISSING_PARAMS, (FullCommand)));
  252. }
  253. else if ((ScriptCommand->MaxParams >= 0) && (Parameters->ParamCount > ScriptCommand->MaxParams))
  254. {
  255. throw Exception(FMTLOAD(SCRIPT_TOO_MANY_PARAMS, (FullCommand)));
  256. }
  257. else
  258. {
  259. CheckParams(Parameters, ScriptCommand->Switches);
  260. ScriptCommand->Proc(Parameters);
  261. }
  262. }
  263. __finally
  264. {
  265. delete Parameters;
  266. }
  267. }
  268. //---------------------------------------------------------------------------
  269. //---------------------------------------------------------------------------
  270. __fastcall TScript::TScript(bool LimitedOutput)
  271. {
  272. FLimitedOutput = LimitedOutput;
  273. FTerminal = NULL;
  274. FSessionReopenTimeout = Configuration->SessionReopenTimeout;
  275. Init();
  276. }
  277. //---------------------------------------------------------------------------
  278. __fastcall TScript::~TScript()
  279. {
  280. delete FCommands;
  281. }
  282. //---------------------------------------------------------------------------
  283. void __fastcall TScript::Init()
  284. {
  285. FBatch = BatchOff;
  286. FConfirm = true;
  287. FEcho = false;
  288. FSynchronizeParams = 0;
  289. FOnPrint = NULL;
  290. FOnTerminalSynchronizeDirectory = NULL;
  291. FOnSynchronizeStartStop = NULL;
  292. FSynchronizeMode = -1;
  293. FKeepingUpToDate = false;
  294. FCommands = new TScriptCommands;
  295. FCommands->Register("help", SCRIPT_HELP_DESC, SCRIPT_HELP_HELP, &HelpProc, 0, -1, false);
  296. FCommands->Register("man", 0, SCRIPT_HELP_HELP, &HelpProc, 0, -1, false);
  297. // the call command does not have switches itself, but the commands may have
  298. FCommands->Register("call", SCRIPT_CALL_DESC2, SCRIPT_CALL_HELP2, &CallProc, 1, -1, true);
  299. FCommands->Register("!", 0, SCRIPT_CALL_HELP2, &CallProc, 1, -1, false);
  300. FCommands->Register("pwd", SCRIPT_PWD_DESC, SCRIPT_PWD_HELP, &PwdProc, 0, 0, false);
  301. FCommands->Register("cd", SCRIPT_CD_DESC, SCRIPT_CD_HELP, &CdProc, 0, 1, false);
  302. FCommands->Register("ls", SCRIPT_LS_DESC, SCRIPT_LS_HELP, &LsProc, 0, 1, false);
  303. FCommands->Register("dir", 0, SCRIPT_LS_HELP, &LsProc, 0, 1, false);
  304. FCommands->Register("rm", SCRIPT_RM_DESC, SCRIPT_RM_HELP, &RmProc, 1, -1, false);
  305. FCommands->Register("rmdir", SCRIPT_RMDIR_DESC, SCRIPT_RMDIR_HELP, &RmDirProc, 1, -1, false);
  306. FCommands->Register("mv", SCRIPT_MV_DESC, SCRIPT_MV_HELP, &MvProc, 2, -1, false);
  307. FCommands->Register("rename", 0, SCRIPT_MV_HELP, &MvProc, 2, -1, false);
  308. FCommands->Register("chmod", SCRIPT_CHMOD_DESC, SCRIPT_CHMOD_HELP, &ChModProc, 2, -1, false);
  309. FCommands->Register("ln", SCRIPT_LN_DESC, SCRIPT_LN_HELP, &LnProc, 2, 2, false);
  310. FCommands->Register("symlink", 0, SCRIPT_LN_HELP, &LnProc, 2, 2, false);
  311. FCommands->Register("mkdir", SCRIPT_MKDIR_DESC, SCRIPT_MKDIR_HELP, &MkDirProc, 1, 1, false);
  312. FCommands->Register("get", SCRIPT_GET_DESC, SCRIPT_GET_HELP3, &GetProc, 1, -1, true);
  313. FCommands->Register("recv", 0, SCRIPT_GET_HELP3, &GetProc, 1, -1, false);
  314. FCommands->Register("put", SCRIPT_PUT_DESC, SCRIPT_PUT_HELP3, &PutProc, 1, -1, true);
  315. FCommands->Register("send", 0, SCRIPT_PUT_HELP3, &PutProc, 1, -1, false);
  316. FCommands->Register("option", SCRIPT_OPTION_DESC, SCRIPT_OPTION_HELP6, &OptionProc, -1, 2, false);
  317. FCommands->Register("ascii", 0, SCRIPT_OPTION_HELP6, &AsciiProc, 0, 0, false);
  318. FCommands->Register("binary", 0, SCRIPT_OPTION_HELP6, &BinaryProc, 0, 0, false);
  319. FCommands->Register("synchronize", SCRIPT_SYNCHRONIZE_DESC, SCRIPT_SYNCHRONIZE_HELP3, &SynchronizeProc, 1, 3, true);
  320. FCommands->Register("keepuptodate", SCRIPT_KEEPUPTODATE_DESC, SCRIPT_KEEPUPTODATE_HELP2, &KeepUpToDateProc, 0, 2, true);
  321. }
  322. //---------------------------------------------------------------------------
  323. void __fastcall TScript::SetCopyParam(const TCopyParamType & value)
  324. {
  325. FCopyParam.Assign(&value);
  326. }
  327. //---------------------------------------------------------------------------
  328. void __fastcall TScript::SetSynchronizeParams(int value)
  329. {
  330. FSynchronizeParams = (value &
  331. (TTerminal::spExistingOnly | TTerminal::spTimestamp |
  332. TTerminal::spNotByTime | TTerminal::spBySize));
  333. }
  334. //---------------------------------------------------------------------------
  335. void __fastcall TScript::Log(TLogLineType Type, AnsiString Str)
  336. {
  337. if ((Terminal != NULL) && Terminal->Log->Logging)
  338. {
  339. Terminal->Log->Add(Type, FORMAT("Script: %s", (Str)));
  340. }
  341. }
  342. //---------------------------------------------------------------------------
  343. void __fastcall TScript::Command(AnsiString Cmd)
  344. {
  345. try
  346. {
  347. if (!Cmd.Trim().IsEmpty() && (Cmd[1] != ';') && (Cmd[1] != '#'))
  348. {
  349. Log(llInput, Cmd);
  350. if (FEcho)
  351. {
  352. PrintLine(Cmd);
  353. }
  354. AnsiString Command;
  355. if (CutToken(Cmd, Command))
  356. {
  357. int ASessionReopenTimeout = Configuration->SessionReopenTimeout;
  358. try
  359. {
  360. Configuration->SessionReopenTimeout = FSessionReopenTimeout;
  361. FCommands->Execute(Command, Cmd);
  362. }
  363. __finally
  364. {
  365. Configuration->SessionReopenTimeout = ASessionReopenTimeout;
  366. }
  367. }
  368. }
  369. }
  370. catch(Exception & E)
  371. {
  372. if (!HandleExtendedException(&E))
  373. {
  374. throw;
  375. }
  376. }
  377. }
  378. //---------------------------------------------------------------------------
  379. TStrings * __fastcall TScript::CreateFileList(TScriptProcParams * Parameters, int Start,
  380. int End, TFileListType ListType)
  381. {
  382. TStrings * Result = new TStringList();
  383. TStrings * FileLists = NULL;
  384. try
  385. {
  386. try
  387. {
  388. for (int i = Start; i <= End; i++)
  389. {
  390. AnsiString FileName = Parameters->Param[i];
  391. if (FLAGSET(ListType, fltDirectories))
  392. {
  393. TRemoteFile * File = new TRemoteFile();
  394. File->FileName = FileName;
  395. File->Type = FILETYPE_DIRECTORY;
  396. Result->AddObject(FileName, File);
  397. }
  398. else if (FLAGSET(ListType, fltMask) && TFileMasks::IsMask(FileName))
  399. {
  400. AnsiString FileDirectory = UnixExtractFilePath(FileName);
  401. AnsiString Directory = FileDirectory;
  402. if (Directory.IsEmpty())
  403. {
  404. Directory = UnixIncludeTrailingBackslash(FTerminal->CurrentDirectory);
  405. }
  406. TRemoteFileList * FileList = NULL;
  407. if (FileLists != NULL)
  408. {
  409. int Index = FileLists->IndexOf(Directory);
  410. if (Index > 0)
  411. {
  412. FileList = dynamic_cast<TRemoteFileList *>(FileLists->Objects[Index]);
  413. }
  414. }
  415. if (FileList == NULL)
  416. {
  417. FileList = FTerminal->CustomReadDirectoryListing(Directory, false);
  418. if (FileLists == NULL)
  419. {
  420. FileLists = new TStringList();
  421. }
  422. FileLists->AddObject(Directory, FileList);
  423. }
  424. TFileMasks Mask;
  425. Mask.SetMask(UnixExtractFileName(FileName));
  426. for (int i = 0; i < FileList->Count; i++)
  427. {
  428. TRemoteFile * File = FileList->Files[i];
  429. TFileMasks::TParams Params;
  430. Params.Size = File->Size;
  431. if (!File->IsThisDirectory && !File->IsParentDirectory &&
  432. Mask.Matches(File->FileName, false, AnsiString(), &Params))
  433. {
  434. Result->AddObject(FileDirectory + File->FileName,
  435. FLAGSET(ListType, fltQueryServer) ? File->Duplicate() : NULL);
  436. }
  437. }
  438. }
  439. else
  440. {
  441. TRemoteFile * File = NULL;
  442. if (FLAGSET(ListType, fltQueryServer))
  443. {
  444. FTerminal->ExceptionOnFail = true;
  445. try
  446. {
  447. FTerminal->ReadFile(FileName, File);
  448. if (!File->HaveFullFileName)
  449. {
  450. File->FullFileName = FileName;
  451. }
  452. }
  453. __finally
  454. {
  455. FTerminal->ExceptionOnFail = false;
  456. }
  457. }
  458. Result->AddObject(FileName, File);
  459. }
  460. }
  461. }
  462. catch(...)
  463. {
  464. FreeFileList(Result);
  465. throw;
  466. }
  467. }
  468. __finally
  469. {
  470. if (FileLists != NULL)
  471. {
  472. for (int i = 0; i < FileLists->Count; i++)
  473. {
  474. delete FileLists->Objects[i];
  475. }
  476. delete FileLists;
  477. }
  478. }
  479. return Result;
  480. }
  481. //---------------------------------------------------------------------------
  482. TStrings * __fastcall TScript::CreateLocalFileList(TScriptProcParams * Parameters,
  483. int Start, int End, TFileListType ListType)
  484. {
  485. TStrings * Result = new TStringList();
  486. try
  487. {
  488. for (int i = Start; i <= End; i++)
  489. {
  490. AnsiString FileName = Parameters->Param[i];
  491. if (FLAGSET(ListType, fltMask))
  492. {
  493. TSearchRec SearchRec;
  494. int FindAttrs = faReadOnly | faHidden | faSysFile | faDirectory | faArchive;
  495. if (FindFirst(FileName, FindAttrs, SearchRec) == 0)
  496. {
  497. AnsiString Directory = ExtractFilePath(FileName);
  498. try
  499. {
  500. do
  501. {
  502. if ((SearchRec.Name != ".") && (SearchRec.Name != ".."))
  503. {
  504. Result->Add(Directory + SearchRec.Name);
  505. }
  506. }
  507. while (FindNext(SearchRec) == 0);
  508. }
  509. __finally
  510. {
  511. FindClose(SearchRec);
  512. }
  513. }
  514. else
  515. {
  516. // not match, let it fail latter however
  517. Result->Add(FileName);
  518. }
  519. }
  520. else
  521. {
  522. Result->Add(FileName);
  523. }
  524. }
  525. }
  526. catch(...)
  527. {
  528. delete Result;
  529. throw;
  530. }
  531. return Result;
  532. }
  533. //---------------------------------------------------------------------------
  534. void __fastcall TScript::FreeFileList(TStrings * FileList)
  535. {
  536. for (int i = 0; i < FileList->Count; i++)
  537. {
  538. if (FileList->Objects[i] != NULL)
  539. {
  540. TRemoteFile * File = dynamic_cast<TRemoteFile *>(FileList->Objects[i]);
  541. delete File;
  542. }
  543. }
  544. delete FileList;
  545. }
  546. //---------------------------------------------------------------------------
  547. void __fastcall TScript::ConnectTerminal(TTerminal * Terminal)
  548. {
  549. Terminal->Open();
  550. }
  551. //---------------------------------------------------------------------------
  552. void __fastcall TScript::Print(const AnsiString Str)
  553. {
  554. if (FOnPrint != NULL)
  555. {
  556. FOnPrint(this, Str);
  557. }
  558. }
  559. //---------------------------------------------------------------------------
  560. void __fastcall TScript::PrintLine(const AnsiString Str)
  561. {
  562. Log(llOutput, Str);
  563. Print(Str + "\n");
  564. }
  565. //---------------------------------------------------------------------------
  566. bool __fastcall TScript::HandleExtendedException(Exception * E, TTerminal * Terminal)
  567. {
  568. bool Result = (OnShowExtendedException != NULL);
  569. if (Result)
  570. {
  571. if (Terminal == NULL)
  572. {
  573. Terminal = FTerminal;
  574. }
  575. OnShowExtendedException(Terminal, E, NULL);
  576. }
  577. return Result;
  578. }
  579. //---------------------------------------------------------------------------
  580. void __fastcall TScript::CheckSession()
  581. {
  582. if (FTerminal == NULL)
  583. {
  584. throw Exception(LoadStr(SCRIPT_NO_SESSION));
  585. }
  586. }
  587. //---------------------------------------------------------------------------
  588. void __fastcall TScript::CheckParams(TScriptProcParams * Parameters)
  589. {
  590. TScriptCommands::CheckParams(Parameters, false);
  591. }
  592. //---------------------------------------------------------------------------
  593. void __fastcall TScript::TransferParamParams(int & Params, TScriptProcParams * Parameters)
  594. {
  595. Params |= FLAGMASK(!FConfirm, cpNoConfirmation);
  596. if (Parameters->FindSwitch("delete"))
  597. {
  598. Params |= cpDelete;
  599. }
  600. if (Parameters->FindSwitch("resume"))
  601. {
  602. Params |= cpResume;
  603. }
  604. else if (Parameters->FindSwitch("append"))
  605. {
  606. Params |= cpAppend;
  607. }
  608. }
  609. //---------------------------------------------------------------------------
  610. void __fastcall TScript::CopyParamParams(TCopyParamType & CopyParam, TScriptProcParams * Parameters)
  611. {
  612. AnsiString Value;
  613. // total size is not visualized, hence it makes no sense to calculate it
  614. CopyParam.CalculateSize = false;
  615. if (Parameters->FindSwitch("nopreservetime"))
  616. {
  617. CopyParam.PreserveTime = false;
  618. }
  619. if (Parameters->FindSwitch("preservetime"))
  620. {
  621. CopyParam.PreserveTime = true;
  622. }
  623. if (Parameters->FindSwitch("nopermissions"))
  624. {
  625. CopyParam.PreserveRights = false;
  626. }
  627. if (Parameters->FindSwitch("permissions", Value))
  628. {
  629. CopyParam.PreserveRights = true;
  630. CopyParam.Rights.Octal = Value;
  631. }
  632. if (Parameters->FindSwitch("speed", Value))
  633. {
  634. int CPSLimit;
  635. if (Value.IsEmpty())
  636. {
  637. CPSLimit = 0;
  638. }
  639. else
  640. {
  641. CPSLimit = StrToInt(Value) * 1024;
  642. if (CPSLimit < 0)
  643. {
  644. CPSLimit = 0;
  645. }
  646. }
  647. CopyParam.CPSLimit = CPSLimit;
  648. }
  649. }
  650. //---------------------------------------------------------------------------
  651. void __fastcall TScript::ResetTransfer()
  652. {
  653. }
  654. //---------------------------------------------------------------------------
  655. bool __fastcall TScript::EnsureCommandSessionFallback(TFSCapability Capability)
  656. {
  657. bool Result = FTerminal->IsCapable[Capability] ||
  658. FTerminal->CommandSessionOpened;
  659. if (!Result)
  660. {
  661. try
  662. {
  663. ConnectTerminal(FTerminal->CommandSession);
  664. Result = true;
  665. }
  666. catch(Exception & E)
  667. {
  668. HandleExtendedException(&E, FTerminal->CommandSession);
  669. Result = false;
  670. }
  671. }
  672. return Result;
  673. }
  674. //---------------------------------------------------------------------------
  675. void __fastcall TScript::HelpProc(TScriptProcParams * Parameters)
  676. {
  677. AnsiString Output;
  678. if (Parameters->ParamCount == 0)
  679. {
  680. AnsiString Command;
  681. AnsiString Description;
  682. int Index = 0;
  683. while (FCommands->Enumerate(Index, &Command, &Description, NULL))
  684. {
  685. if (!Description.IsEmpty())
  686. {
  687. Output += FORMAT("%-8s %s\n", (Command, Description));
  688. }
  689. Index++;
  690. }
  691. }
  692. else
  693. {
  694. for (int i = 1; i <= Parameters->ParamCount; i++)
  695. {
  696. AnsiString Help;
  697. if (FCommands->Info(Parameters->Param[i], NULL, &Help))
  698. {
  699. Output += Help;
  700. }
  701. else
  702. {
  703. throw Exception(FMTLOAD(SCRIPT_COMMAND_UNKNOWN, (Parameters->Param[i])));
  704. }
  705. }
  706. }
  707. Print(Output);
  708. }
  709. //---------------------------------------------------------------------------
  710. void __fastcall TScript::CallProc(TScriptProcParams * Parameters)
  711. {
  712. CheckSession();
  713. if (EnsureCommandSessionFallback(fcAnyCommand))
  714. {
  715. FTerminal->AnyCommand(Parameters->ParamsStr, TerminalCaptureLog);
  716. }
  717. }
  718. //---------------------------------------------------------------------------
  719. void __fastcall TScript::TerminalCaptureLog(const AnsiString & AddedLine,
  720. bool /*StdError*/)
  721. {
  722. PrintLine(AddedLine);
  723. }
  724. //---------------------------------------------------------------------------
  725. void __fastcall TScript::PwdProc(TScriptProcParams * /*Parameters*/)
  726. {
  727. CheckSession();
  728. PrintLine(FTerminal->CurrentDirectory);
  729. }
  730. //---------------------------------------------------------------------------
  731. void __fastcall TScript::CdProc(TScriptProcParams * Parameters)
  732. {
  733. CheckSession();
  734. if (Parameters->ParamCount == 0)
  735. {
  736. FTerminal->HomeDirectory();
  737. }
  738. else
  739. {
  740. FTerminal->ChangeDirectory(Parameters->Param[1]);
  741. }
  742. PrintLine(FTerminal->CurrentDirectory);
  743. }
  744. //---------------------------------------------------------------------------
  745. void __fastcall TScript::LsProc(TScriptProcParams * Parameters)
  746. {
  747. CheckSession();
  748. AnsiString Directory;
  749. TFileMasks Mask;
  750. if (Parameters->ParamCount > 0)
  751. {
  752. Directory = Parameters->Param[1];
  753. AnsiString MaskStr = UnixExtractFileName(Directory);
  754. if (TFileMasks::IsMask(MaskStr))
  755. {
  756. Mask.SetMask(MaskStr);
  757. Directory = UnixExtractFilePath(Directory);
  758. }
  759. }
  760. if (Directory.IsEmpty())
  761. {
  762. Directory = FTerminal->CurrentDirectory;
  763. }
  764. TRemoteFileList * FileList = FTerminal->ReadDirectoryListing(Directory, Mask);
  765. // on error user may select "skip", then we get NULL
  766. if (FileList != NULL)
  767. {
  768. try
  769. {
  770. for (int i = 0; i < FileList->Count; i++)
  771. {
  772. PrintLine(FileList->Files[i]->ListingStr);
  773. }
  774. }
  775. __finally
  776. {
  777. delete FileList;
  778. }
  779. }
  780. }
  781. //---------------------------------------------------------------------------
  782. void __fastcall TScript::RmProc(TScriptProcParams * Parameters)
  783. {
  784. CheckSession();
  785. TStrings * FileList = CreateFileList(Parameters, 1, Parameters->ParamCount, fltMask);
  786. try
  787. {
  788. FTerminal->DeleteFiles(FileList);
  789. }
  790. __finally
  791. {
  792. FreeFileList(FileList);
  793. }
  794. }
  795. //---------------------------------------------------------------------------
  796. void __fastcall TScript::RmDirProc(TScriptProcParams * Parameters)
  797. {
  798. CheckSession();
  799. TStrings * FileList = CreateFileList(Parameters, 1, Parameters->ParamCount, fltDirectories);
  800. try
  801. {
  802. FTerminal->DeleteFiles(FileList);
  803. }
  804. __finally
  805. {
  806. FreeFileList(FileList);
  807. }
  808. }
  809. //---------------------------------------------------------------------------
  810. void __fastcall TScript::MvProc(TScriptProcParams * Parameters)
  811. {
  812. CheckSession();
  813. TStrings * FileList = CreateFileList(Parameters, 1, Parameters->ParamCount - 1,
  814. fltMask);
  815. try
  816. {
  817. assert(Parameters->ParamCount >= 1);
  818. AnsiString Target = Parameters->Param[Parameters->ParamCount];
  819. AnsiString TargetDirectory = UnixExtractFilePath(Target);
  820. AnsiString FileMask = UnixExtractFileName(Target);
  821. FTerminal->MoveFiles(FileList, TargetDirectory, FileMask);
  822. }
  823. __finally
  824. {
  825. FreeFileList(FileList);
  826. }
  827. }
  828. //---------------------------------------------------------------------------
  829. void __fastcall TScript::ChModProc(TScriptProcParams * Parameters)
  830. {
  831. CheckSession();
  832. TStrings * FileList = CreateFileList(Parameters, 2, Parameters->ParamCount,
  833. fltMask);
  834. try
  835. {
  836. TRemoteProperties Properties;
  837. Properties.Valid = TValidProperties() << vpRights;
  838. Properties.Rights.Octal = Parameters->Param[1];
  839. FTerminal->ChangeFilesProperties(FileList, &Properties);
  840. }
  841. __finally
  842. {
  843. FreeFileList(FileList);
  844. }
  845. }
  846. //---------------------------------------------------------------------------
  847. void __fastcall TScript::LnProc(TScriptProcParams * Parameters)
  848. {
  849. CheckSession();
  850. assert(Parameters->ParamCount == 2);
  851. FTerminal->CreateLink(Parameters->Param[2], Parameters->Param[1], true);
  852. }
  853. //---------------------------------------------------------------------------
  854. void __fastcall TScript::MkDirProc(TScriptProcParams * Parameters)
  855. {
  856. CheckSession();
  857. FTerminal->CreateDirectory(Parameters->Param[1]);
  858. }
  859. //---------------------------------------------------------------------------
  860. void __fastcall TScript::GetProc(TScriptProcParams * Parameters)
  861. {
  862. CheckSession();
  863. ResetTransfer();
  864. int LastFileParam = (Parameters->ParamCount == 1 ? 1 : Parameters->ParamCount - 1);
  865. TStrings * FileList = CreateFileList(Parameters, 1, LastFileParam,
  866. fltQueryServer | fltMask);
  867. try
  868. {
  869. TCopyParamType CopyParam = FCopyParam;
  870. AnsiString TargetDirectory;
  871. if (Parameters->ParamCount == 1)
  872. {
  873. TargetDirectory = GetCurrentDir();
  874. CopyParam.FileMask = "";
  875. }
  876. else
  877. {
  878. AnsiString Target = Parameters->Param[Parameters->ParamCount];
  879. TargetDirectory = ExtractFilePath(Target);
  880. if (TargetDirectory.IsEmpty())
  881. {
  882. TargetDirectory = GetCurrentDir();
  883. }
  884. CopyParam.FileMask = ExtractFileName(Target);
  885. }
  886. int Params = 0;
  887. TransferParamParams(Params, Parameters);
  888. CopyParamParams(CopyParam, Parameters);
  889. CheckParams(Parameters);
  890. FTerminal->CopyToLocal(FileList, TargetDirectory, &CopyParam, Params);
  891. }
  892. __finally
  893. {
  894. FreeFileList(FileList);
  895. }
  896. }
  897. //---------------------------------------------------------------------------
  898. void __fastcall TScript::PutProc(TScriptProcParams * Parameters)
  899. {
  900. CheckSession();
  901. ResetTransfer();
  902. int LastFileParam = (Parameters->ParamCount == 1 ? 1 : Parameters->ParamCount - 1);
  903. TStrings * FileList = CreateLocalFileList(Parameters, 1, LastFileParam, fltMask);
  904. try
  905. {
  906. TCopyParamType CopyParam = FCopyParam;
  907. AnsiString TargetDirectory;
  908. if (Parameters->ParamCount == 1)
  909. {
  910. TargetDirectory = FTerminal->CurrentDirectory;
  911. CopyParam.FileMask = "";
  912. }
  913. else
  914. {
  915. AnsiString Target = Parameters->Param[Parameters->ParamCount];
  916. TargetDirectory = UnixExtractFilePath(Target);
  917. if (TargetDirectory.IsEmpty())
  918. {
  919. TargetDirectory = FTerminal->CurrentDirectory;
  920. }
  921. CopyParam.FileMask = UnixExtractFileName(Target);
  922. }
  923. int Params = 0;
  924. TransferParamParams(Params, Parameters);
  925. CopyParamParams(CopyParam, Parameters);
  926. CheckParams(Parameters);
  927. FTerminal->CopyToRemote(FileList, TargetDirectory, &CopyParam, Params);
  928. }
  929. __finally
  930. {
  931. FreeFileList(FileList);
  932. }
  933. }
  934. //---------------------------------------------------------------------------
  935. void __fastcall TScript::OptionImpl(AnsiString OptionName, AnsiString ValueName)
  936. {
  937. enum { Echo, Batch, Confirm, Transfer, SynchDelete, Exclude, Include, ReconnectTime };
  938. static const char * Names[] = { "echo", "batch", "confirm", "transfer",
  939. "synchdelete", "exclude", "include", "reconnecttime" };
  940. enum { Off, On };
  941. static const char * ToggleNames[] = { "off", "on" };
  942. assert((BatchOff == 0) && (BatchOn == 1) && (BatchAbort == 2) && (BatchContinue == 3));
  943. static const char * BatchModeNames[] = { "off", "on", "abort", "continue" };
  944. assert((tmBinary == 0) && (tmAscii == 1) && (tmAutomatic == 2));
  945. static const char * TransferModeNames[] = { "binary", "ascii", "automatic" };
  946. int Option = -1;
  947. if (!OptionName.IsEmpty())
  948. {
  949. Option = TScriptCommands::FindCommand(Names, LENOF(Names), OptionName);
  950. if (Option < 0)
  951. {
  952. throw Exception(FMTLOAD(SCRIPT_OPTION_UNKNOWN, (OptionName)));
  953. }
  954. else
  955. {
  956. OptionName = Names[Option];
  957. }
  958. }
  959. #define OPT(OPT) ((Option < 0) || (Option == OPT))
  960. const char * ListFormat = "%-15s %-10s";
  961. bool SetValue = !ValueName.IsEmpty();
  962. if (OPT(Echo))
  963. {
  964. if (SetValue)
  965. {
  966. int Value = TScriptCommands::FindCommand(ToggleNames, LENOF(ToggleNames), ValueName);
  967. if (Value < 0)
  968. {
  969. throw Exception(FMTLOAD(SCRIPT_VALUE_UNKNOWN, (ValueName, OptionName)));
  970. }
  971. FEcho = (Value == On);
  972. }
  973. PrintLine(FORMAT(ListFormat, (Names[Echo], ToggleNames[FEcho ? On : Off])));
  974. }
  975. if (OPT(Batch))
  976. {
  977. if (SetValue)
  978. {
  979. int Value = TScriptCommands::FindCommand(BatchModeNames, LENOF(BatchModeNames), ValueName);
  980. if (Value < 0)
  981. {
  982. throw Exception(FMTLOAD(SCRIPT_VALUE_UNKNOWN, (ValueName, OptionName)));
  983. }
  984. FBatch = (TBatchMode)Value;
  985. }
  986. PrintLine(FORMAT(ListFormat, (Names[Batch], BatchModeNames[FBatch])));
  987. }
  988. if (OPT(Confirm))
  989. {
  990. if (SetValue)
  991. {
  992. int Value = TScriptCommands::FindCommand(ToggleNames, LENOF(ToggleNames), ValueName);
  993. if (Value < 0)
  994. {
  995. throw Exception(FMTLOAD(SCRIPT_VALUE_UNKNOWN, (ValueName, OptionName)));
  996. }
  997. FConfirm = (Value == On);
  998. }
  999. PrintLine(FORMAT(ListFormat, (Names[Confirm], ToggleNames[FConfirm ? On : Off])));
  1000. }
  1001. if (OPT(Transfer))
  1002. {
  1003. if (SetValue)
  1004. {
  1005. int Value = TScriptCommands::FindCommand(TransferModeNames,
  1006. LENOF(TransferModeNames), ValueName);
  1007. if (Value < 0)
  1008. {
  1009. throw Exception(FMTLOAD(SCRIPT_VALUE_UNKNOWN, (ValueName, OptionName)));
  1010. }
  1011. FCopyParam.TransferMode = (TTransferMode)Value;
  1012. }
  1013. assert(FCopyParam.TransferMode < LENOF(TransferModeNames));
  1014. const char * Value = TransferModeNames[FCopyParam.TransferMode];
  1015. PrintLine(FORMAT(ListFormat, (Names[Transfer], Value)));
  1016. }
  1017. // omit the option in listing
  1018. if (Option == SynchDelete)
  1019. {
  1020. if (SetValue)
  1021. {
  1022. int Value = TScriptCommands::FindCommand(ToggleNames, LENOF(ToggleNames), ValueName);
  1023. if (Value < 0)
  1024. {
  1025. throw Exception(FMTLOAD(SCRIPT_VALUE_UNKNOWN, (ValueName, OptionName)));
  1026. }
  1027. FSynchronizeParams =
  1028. (FSynchronizeParams & ~TTerminal::spDelete) |
  1029. FLAGMASK(Value == On, TTerminal::spDelete);
  1030. }
  1031. PrintLine(FORMAT(ListFormat, (Names[SynchDelete],
  1032. ToggleNames[FLAGSET(FSynchronizeParams, TTerminal::spDelete) ? On : Off])));
  1033. }
  1034. static const char * Clear = "clear";
  1035. if (OPT(Include))
  1036. {
  1037. if (SetValue)
  1038. {
  1039. FCopyParam.NegativeExclude = true;
  1040. FCopyParam.ExcludeFileMask =
  1041. (ValueName == Clear ? AnsiString() : ValueName);
  1042. }
  1043. if (SetValue ||
  1044. (FCopyParam.NegativeExclude && !FCopyParam.ExcludeFileMask.Masks.IsEmpty()))
  1045. {
  1046. PrintLine(FORMAT(ListFormat, (Names[Include], FCopyParam.ExcludeFileMask.Masks)));
  1047. }
  1048. }
  1049. if (OPT(Exclude))
  1050. {
  1051. if (SetValue)
  1052. {
  1053. FCopyParam.NegativeExclude = false;
  1054. FCopyParam.ExcludeFileMask =
  1055. (ValueName == Clear ? AnsiString() : ValueName);
  1056. }
  1057. if (SetValue ||
  1058. (!FCopyParam.NegativeExclude && !FCopyParam.ExcludeFileMask.Masks.IsEmpty()))
  1059. {
  1060. PrintLine(FORMAT(ListFormat, (Names[Exclude], FCopyParam.ExcludeFileMask.Masks)));
  1061. }
  1062. }
  1063. if (OPT(ReconnectTime))
  1064. {
  1065. if (SetValue)
  1066. {
  1067. int Value;
  1068. if (SameText(ValueName, ToggleNames[Off]))
  1069. {
  1070. Value = 0;
  1071. }
  1072. else
  1073. {
  1074. Value = StrToInt(ValueName) * 1000;
  1075. }
  1076. if (Value < 0)
  1077. {
  1078. throw Exception(FMTLOAD(SCRIPT_VALUE_UNKNOWN, (ValueName, OptionName)));
  1079. }
  1080. FSessionReopenTimeout = Value;
  1081. }
  1082. if (FSessionReopenTimeout == 0)
  1083. {
  1084. ValueName = ToggleNames[Off];
  1085. }
  1086. else
  1087. {
  1088. ValueName = IntToStr(FSessionReopenTimeout / 1000);
  1089. }
  1090. PrintLine(FORMAT(ListFormat, (Names[ReconnectTime], ValueName)));
  1091. }
  1092. #undef OPT
  1093. }
  1094. //---------------------------------------------------------------------------
  1095. void __fastcall TScript::OptionProc(TScriptProcParams * Parameters)
  1096. {
  1097. AnsiString OptionName;
  1098. AnsiString ValueName;
  1099. if (Parameters->ParamCount >= 1)
  1100. {
  1101. OptionName = Parameters->Param[1];
  1102. }
  1103. if (Parameters->ParamCount >= 2)
  1104. {
  1105. ValueName = Parameters->Param[2];
  1106. }
  1107. OptionImpl(OptionName, ValueName);
  1108. }
  1109. //---------------------------------------------------------------------------
  1110. void __fastcall TScript::AsciiProc(TScriptProcParams * /*Parameters*/)
  1111. {
  1112. OptionImpl("transfer", "ascii");
  1113. }
  1114. //---------------------------------------------------------------------------
  1115. void __fastcall TScript::BinaryProc(TScriptProcParams * /*Parameters*/)
  1116. {
  1117. OptionImpl("transfer", "binary");
  1118. }
  1119. //---------------------------------------------------------------------------
  1120. void __fastcall TScript::SynchronizeDirectories(TScriptProcParams * Parameters,
  1121. AnsiString & LocalDirectory, AnsiString & RemoteDirectory, int FirstParam)
  1122. {
  1123. if (Parameters->ParamCount >= FirstParam)
  1124. {
  1125. LocalDirectory = Parameters->Param[FirstParam];
  1126. }
  1127. else
  1128. {
  1129. LocalDirectory = GetCurrentDir();
  1130. }
  1131. if (Parameters->ParamCount >= FirstParam + 1)
  1132. {
  1133. RemoteDirectory = Parameters->Param[FirstParam + 1];
  1134. }
  1135. else
  1136. {
  1137. RemoteDirectory = FTerminal->CurrentDirectory;
  1138. }
  1139. }
  1140. //---------------------------------------------------------------------------
  1141. void __fastcall TScript::SynchronizeProc(TScriptProcParams * Parameters)
  1142. {
  1143. CheckSession();
  1144. ResetTransfer();
  1145. static const char * ModeNames[] = { "remote", "local", "both" };
  1146. AnsiString ModeName = Parameters->Param[1];
  1147. assert(FSynchronizeMode < 0);
  1148. FSynchronizeMode = TScriptCommands::FindCommand(ModeNames, LENOF(ModeNames), ModeName);
  1149. try
  1150. {
  1151. if (FSynchronizeMode < 0)
  1152. {
  1153. throw Exception(FMTLOAD(SCRIPT_OPTION_UNKNOWN, (ModeName)));
  1154. }
  1155. AnsiString LocalDirectory;
  1156. AnsiString RemoteDirectory;
  1157. SynchronizeDirectories(Parameters, LocalDirectory, RemoteDirectory, 2);
  1158. TCopyParamType CopyParam = FCopyParam;
  1159. CopyParamParams(CopyParam, Parameters);
  1160. int SynchronizeParams = FSynchronizeParams | TTerminal::spNoConfirmation;
  1161. AnsiString Value;
  1162. if (Parameters->FindSwitch("delete"))
  1163. {
  1164. SynchronizeParams |= TTerminal::spDelete;
  1165. }
  1166. if (Parameters->FindSwitch("mirror") &&
  1167. (FSynchronizeMode != TTerminal::smBoth))
  1168. {
  1169. SynchronizeParams |= TTerminal::spMirror;
  1170. }
  1171. if (Parameters->FindSwitch("criteria", Value))
  1172. {
  1173. enum { None, Time, Size, Both };
  1174. static const char * CriteriaNames[] = { "none", "time", "size", "both" };
  1175. int Criteria = TScriptCommands::FindCommand(CriteriaNames, LENOF(CriteriaNames), Value);
  1176. switch (Criteria)
  1177. {
  1178. case None:
  1179. SynchronizeParams |= TTerminal::spNotByTime;
  1180. SynchronizeParams &= ~TTerminal::spBySize;
  1181. break;
  1182. case Time:
  1183. SynchronizeParams &= ~(TTerminal::spNotByTime | TTerminal::spBySize);
  1184. break;
  1185. case Size:
  1186. SynchronizeParams |= TTerminal::spNotByTime | TTerminal::spBySize;
  1187. break;
  1188. case Both:
  1189. SynchronizeParams &= ~TTerminal::spNotByTime;
  1190. SynchronizeParams |= TTerminal::spBySize;
  1191. break;
  1192. }
  1193. }
  1194. // enforce rules
  1195. if (FSynchronizeMode == TTerminal::smBoth)
  1196. {
  1197. SynchronizeParams &= ~(TTerminal::spNotByTime | TTerminal::spBySize);
  1198. }
  1199. CheckParams(Parameters);
  1200. PrintLine(LoadStr(SCRIPT_SYNCHRONIZE_COLLECTING));
  1201. TSynchronizeChecklist * Checklist =
  1202. FTerminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  1203. static_cast<TTerminal::TSynchronizeMode>(FSynchronizeMode),
  1204. &CopyParam, SynchronizeParams, OnTerminalSynchronizeDirectory, NULL);
  1205. try
  1206. {
  1207. if (Checklist->Count > 0)
  1208. {
  1209. PrintLine(LoadStr(SCRIPT_SYNCHRONIZE_SYNCHRONIZING));
  1210. FTerminal->SynchronizeApply(Checklist, LocalDirectory, RemoteDirectory,
  1211. &CopyParam, SynchronizeParams, OnTerminalSynchronizeDirectory);
  1212. }
  1213. else
  1214. {
  1215. PrintLine(LoadStr(SCRIPT_SYNCHRONIZE_NODIFFERENCE));
  1216. }
  1217. }
  1218. __finally
  1219. {
  1220. delete Checklist;
  1221. }
  1222. }
  1223. __finally
  1224. {
  1225. FSynchronizeMode = -1;
  1226. }
  1227. }
  1228. //---------------------------------------------------------------------------
  1229. void __fastcall TScript::Synchronize(const AnsiString LocalDirectory,
  1230. const AnsiString RemoteDirectory, const TCopyParamType & CopyParam,
  1231. int SynchronizeParams, TSynchronizeChecklist ** Checklist)
  1232. {
  1233. try
  1234. {
  1235. FKeepingUpToDate = true;
  1236. TSynchronizeChecklist * AChecklist =
  1237. FTerminal->SynchronizeCollect(LocalDirectory, RemoteDirectory, TTerminal::smRemote,
  1238. &CopyParam, SynchronizeParams, NULL, NULL);
  1239. try
  1240. {
  1241. if (AChecklist->Count > 0)
  1242. {
  1243. FTerminal->SynchronizeApply(AChecklist, LocalDirectory, RemoteDirectory,
  1244. &CopyParam, SynchronizeParams, OnTerminalSynchronizeDirectory);
  1245. }
  1246. }
  1247. __finally
  1248. {
  1249. if (Checklist == NULL)
  1250. {
  1251. delete AChecklist;
  1252. }
  1253. else
  1254. {
  1255. *Checklist = AChecklist;
  1256. }
  1257. }
  1258. // to break line after the last transfer (if any);
  1259. Print("");
  1260. FKeepingUpToDate = false;
  1261. }
  1262. catch(Exception & E)
  1263. {
  1264. FKeepingUpToDate = false;
  1265. HandleExtendedException(&E);
  1266. throw;
  1267. }
  1268. }
  1269. //---------------------------------------------------------------------------
  1270. void __fastcall TScript::KeepUpToDateProc(TScriptProcParams * Parameters)
  1271. {
  1272. if (OnSynchronizeStartStop == NULL)
  1273. {
  1274. Abort();
  1275. }
  1276. CheckSession();
  1277. ResetTransfer();
  1278. AnsiString LocalDirectory;
  1279. AnsiString RemoteDirectory;
  1280. SynchronizeDirectories(Parameters, LocalDirectory, RemoteDirectory, 1);
  1281. int SynchronizeParams = FSynchronizeParams | TTerminal::spNoConfirmation |
  1282. TTerminal::spNoRecurse | TTerminal::spUseCache | TTerminal::spDelayProgress |
  1283. TTerminal::spSubDirs;
  1284. if (Parameters->FindSwitch("delete"))
  1285. {
  1286. SynchronizeParams |= TTerminal::spDelete;
  1287. }
  1288. TCopyParamType CopyParam = FCopyParam;
  1289. CopyParamParams(CopyParam, Parameters);
  1290. CheckParams(Parameters);
  1291. PrintLine(LoadStr(SCRIPT_KEEPING_UP_TO_DATE));
  1292. OnSynchronizeStartStop(this, LocalDirectory, RemoteDirectory, CopyParam, SynchronizeParams);
  1293. }
  1294. //---------------------------------------------------------------------------
  1295. //---------------------------------------------------------------------------
  1296. __fastcall TManagementScript::TManagementScript(TStoredSessionList * StoredSessions,
  1297. bool LimitedOutput) :
  1298. TScript(LimitedOutput)
  1299. {
  1300. assert(StoredSessions != NULL);
  1301. FOnInput = NULL;
  1302. FOnTerminalPromptUser = NULL;
  1303. FOnShowExtendedException = NULL;
  1304. FOnTerminalQueryUser = NULL;
  1305. FStoredSessions = StoredSessions;
  1306. FTerminalList = new TTerminalList(Configuration);
  1307. FOnQueryCancel = NULL;
  1308. FContinue = true;
  1309. OnTerminalSynchronizeDirectory = TerminalSynchronizeDirectory;
  1310. FCommands->Register("exit", SCRIPT_EXIT_DESC, SCRIPT_EXIT_HELP, &ExitProc, 0, 0, false);
  1311. FCommands->Register("bye", 0, SCRIPT_EXIT_HELP, &ExitProc, 0, 0, false);
  1312. FCommands->Register("open", SCRIPT_OPEN_DESC, SCRIPT_OPEN_HELP3, &OpenProc, 0, 1, true);
  1313. FCommands->Register("close", SCRIPT_CLOSE_DESC, SCRIPT_CLOSE_HELP, &CloseProc, 0, 1, false);
  1314. FCommands->Register("session", SCRIPT_SESSION_DESC, SCRIPT_SESSION_HELP, &SessionProc, 0, 1, false);
  1315. FCommands->Register("lpwd", SCRIPT_LPWD_DESC, SCRIPT_LPWD_HELP, &LPwdProc, 0, 0, false);
  1316. FCommands->Register("lcd", SCRIPT_LCD_DESC, SCRIPT_LCD_HELP, &LCdProc, 1, 1, false);
  1317. FCommands->Register("lls", SCRIPT_LLS_DESC, SCRIPT_LLS_HELP, &LLsProc, 0, 1, false);
  1318. }
  1319. //---------------------------------------------------------------------------
  1320. __fastcall TManagementScript::~TManagementScript()
  1321. {
  1322. while (FTerminalList->Count > 0)
  1323. {
  1324. FreeTerminal(FTerminalList->Terminals[0]);
  1325. }
  1326. delete FTerminalList;
  1327. }
  1328. //---------------------------------------------------------------------------
  1329. void __fastcall TManagementScript::FreeTerminal(TTerminal * Terminal)
  1330. {
  1331. if (!Terminal->SessionData->Name.IsEmpty())
  1332. {
  1333. Terminal->SessionData->RemoteDirectory = Terminal->CurrentDirectory;
  1334. TSessionData * Data;
  1335. Data = (TSessionData *)StoredSessions->FindByName(Terminal->SessionData->Name);
  1336. if (Data != NULL)
  1337. {
  1338. bool Changed = false;
  1339. if (Terminal->SessionData->UpdateDirectories)
  1340. {
  1341. Data->RemoteDirectory = Terminal->SessionData->RemoteDirectory;
  1342. Changed = true;
  1343. }
  1344. if (Changed)
  1345. {
  1346. // only modified, implicit
  1347. StoredSessions->Save(false, false);
  1348. }
  1349. }
  1350. }
  1351. FTerminalList->FreeTerminal(Terminal);
  1352. }
  1353. //---------------------------------------------------------------------------
  1354. void __fastcall TManagementScript::Input(const AnsiString Prompt,
  1355. AnsiString & Str, bool AllowEmpty)
  1356. {
  1357. do
  1358. {
  1359. Str = "";
  1360. if (FOnInput != NULL)
  1361. {
  1362. FOnInput(this, Prompt, Str);
  1363. }
  1364. else
  1365. {
  1366. Abort();
  1367. }
  1368. }
  1369. while (Str.Trim().IsEmpty() && !AllowEmpty);
  1370. }
  1371. //---------------------------------------------------------------------------
  1372. void __fastcall TManagementScript::PrintProgress(bool First, const AnsiString Str)
  1373. {
  1374. if (FOnPrintProgress != NULL)
  1375. {
  1376. FOnPrintProgress(this, First, Str);
  1377. }
  1378. }
  1379. //---------------------------------------------------------------------------
  1380. void __fastcall TManagementScript::ResetTransfer()
  1381. {
  1382. TScript::ResetTransfer();
  1383. FLastProgressFile = "";
  1384. FLastProgressTime = 0;
  1385. FLastProgressMessage = "";
  1386. }
  1387. //---------------------------------------------------------------------------
  1388. bool __fastcall TManagementScript::QueryCancel()
  1389. {
  1390. bool Result = false;
  1391. if (OnQueryCancel != NULL)
  1392. {
  1393. OnQueryCancel(this, Result);
  1394. }
  1395. return Result;
  1396. }
  1397. //---------------------------------------------------------------------------
  1398. void __fastcall TManagementScript::TerminalInformation(TTerminal * Terminal,
  1399. const AnsiString & Str, bool /*Status*/, bool Active)
  1400. {
  1401. assert(Terminal != NULL);
  1402. if (Active && (Terminal->Status == ssOpening))
  1403. {
  1404. PrintLine(Str);
  1405. }
  1406. }
  1407. //---------------------------------------------------------------------------
  1408. void __fastcall TManagementScript::TerminalPromptUser(TTerminal * Terminal,
  1409. TPromptKind Kind, AnsiString Name, AnsiString Instructions, TStrings * Prompts,
  1410. TStrings * Results, bool & Result, void * Arg)
  1411. {
  1412. if ((!Terminal->StoredCredentialsTried || !IsAuthenticationPrompt(Kind)) &&
  1413. (OnTerminalPromptUser != NULL))
  1414. {
  1415. OnTerminalPromptUser(Terminal, Kind, Name, Instructions, Prompts, Results, Result, Arg);
  1416. }
  1417. }
  1418. //---------------------------------------------------------------------------
  1419. bool __fastcall TManagementScript::Synchronizing()
  1420. {
  1421. return (FKeepingUpToDate || (FSynchronizeMode >= 0));
  1422. }
  1423. //---------------------------------------------------------------------------
  1424. void __fastcall TManagementScript::ShowPendingProgress()
  1425. {
  1426. if (!FSynchronizeIntro.IsEmpty())
  1427. {
  1428. if (Synchronizing())
  1429. {
  1430. PrintLine(FSynchronizeIntro);
  1431. }
  1432. FSynchronizeIntro = "";
  1433. }
  1434. }
  1435. //---------------------------------------------------------------------------
  1436. void __fastcall TManagementScript::TerminalOperationProgress(
  1437. TFileOperationProgressType & ProgressData, TCancelStatus & Cancel)
  1438. {
  1439. if ((ProgressData.Operation == foCopy) ||
  1440. (ProgressData.Operation == foMove))
  1441. {
  1442. if (ProgressData.InProgress && ProgressData.FileInProgress &&
  1443. !ProgressData.FileName.IsEmpty())
  1444. {
  1445. bool DoPrint = false;
  1446. bool First = false;
  1447. AnsiString ProgressFileName = ProgressData.FileName;
  1448. if (ProgressData.Side == osLocal)
  1449. {
  1450. ProgressFileName = ExcludeTrailingBackslash(ProgressFileName);
  1451. }
  1452. else
  1453. {
  1454. ProgressFileName = UnixExcludeTrailingBackslash(ProgressFileName);
  1455. }
  1456. if (ProgressFileName != FLastProgressFile)
  1457. {
  1458. First = true;
  1459. DoPrint = true;
  1460. ShowPendingProgress();
  1461. }
  1462. if (!DoPrint && ((FLastProgressTime != time(NULL)) || ProgressData.IsTransferDone()))
  1463. {
  1464. DoPrint = true;
  1465. }
  1466. if (DoPrint)
  1467. {
  1468. static int WidthFileName = 25;
  1469. AnsiString FileName;
  1470. if (FLimitedOutput)
  1471. {
  1472. FileName = MinimizeName(ProgressFileName, WidthFileName,
  1473. ProgressData.Side == osRemote);
  1474. }
  1475. else
  1476. {
  1477. FileName = ProgressFileName;
  1478. }
  1479. AnsiString ProgressMessage = FORMAT("%-*s | %10d KiB | %6.1f KiB/s | %-6.6s | %3d%%",
  1480. (WidthFileName, FileName,
  1481. static_cast<int>(ProgressData.TransferedSize / 1024),
  1482. static_cast<float>(ProgressData.CPS()) / 1024,
  1483. ProgressData.AsciiTransfer ? "ascii" : "binary",
  1484. ProgressData.TransferProgress()));
  1485. if (FLastProgressMessage != ProgressMessage)
  1486. {
  1487. FLastProgressTime = time(NULL);
  1488. PrintProgress(First, ProgressMessage);
  1489. FLastProgressMessage = ProgressMessage;
  1490. FLastProgressFile = ProgressFileName;
  1491. }
  1492. }
  1493. }
  1494. else
  1495. {
  1496. FLastProgressFile = "";
  1497. }
  1498. }
  1499. if (QueryCancel())
  1500. {
  1501. Cancel = csCancel;
  1502. }
  1503. }
  1504. //---------------------------------------------------------------------------
  1505. void __fastcall TManagementScript::TerminalOperationFinished(
  1506. TFileOperation Operation, TOperationSide /*Side*/,
  1507. bool /*Temp*/, const AnsiString & FileName, Boolean Success,
  1508. TOnceDoneOperation & /*OnceDoneOperation*/)
  1509. {
  1510. assert(Operation != foCalculateSize);
  1511. if (Success && (Operation != foCalculateSize) && (Operation != foCopy) && (Operation != foMove))
  1512. {
  1513. ShowPendingProgress();
  1514. // For FKeepingUpToDate we should send events to synchronize controller eventually.
  1515. if (Synchronizing() && (Operation == foDelete))
  1516. {
  1517. // Note that this is duplicated with "keep up to date" log.
  1518. PrintLine(FMTLOAD(SCRIPT_SYNCHRONIZE_DELETED, (FileName)));
  1519. }
  1520. else
  1521. {
  1522. PrintLine(FileName);
  1523. }
  1524. }
  1525. }
  1526. //---------------------------------------------------------------------------
  1527. void __fastcall TManagementScript::TerminalSynchronizeDirectory(
  1528. const AnsiString LocalDirectory, const AnsiString RemoteDirectory,
  1529. bool & Continue, bool Collect)
  1530. {
  1531. int SynchronizeMode = FSynchronizeMode;
  1532. if (FKeepingUpToDate)
  1533. {
  1534. SynchronizeMode = TTerminal::smRemote;
  1535. }
  1536. AnsiString Arrow;
  1537. switch (SynchronizeMode)
  1538. {
  1539. case TTerminal::smRemote:
  1540. Arrow = "=>";
  1541. break;
  1542. case TTerminal::smLocal:
  1543. Arrow = "<=";
  1544. break;
  1545. case TTerminal::smBoth:
  1546. Arrow = "<=>";
  1547. break;
  1548. }
  1549. AnsiString Progress = FMTLOAD(SCRIPT_SYNCHRONIZE, (ExcludeTrailingBackslash(LocalDirectory),
  1550. Arrow, UnixExcludeTrailingBackslash(RemoteDirectory)));
  1551. if (Collect)
  1552. {
  1553. PrintProgress(false, Progress);
  1554. }
  1555. else
  1556. {
  1557. FSynchronizeIntro = Progress;
  1558. }
  1559. if (QueryCancel())
  1560. {
  1561. Continue = false;
  1562. }
  1563. }
  1564. //---------------------------------------------------------------------------
  1565. TTerminal * __fastcall TManagementScript::FindSession(const AnsiString Index)
  1566. {
  1567. int i = StrToIntDef(Index, -1);
  1568. if ((i <= 0) || (i > FTerminalList->Count))
  1569. {
  1570. throw Exception(FMTLOAD(SCRIPT_SESSION_INDEX_INVALID, (Index)));
  1571. }
  1572. else
  1573. {
  1574. return FTerminalList->Terminals[i - 1];
  1575. }
  1576. }
  1577. //---------------------------------------------------------------------------
  1578. void __fastcall TManagementScript::PrintActiveSession()
  1579. {
  1580. assert(FTerminal != NULL);
  1581. PrintLine(FMTLOAD(SCRIPT_ACTIVE_SESSION,
  1582. (FTerminalList->IndexOf(FTerminal) + 1, FTerminal->SessionData->SessionName)));
  1583. }
  1584. //---------------------------------------------------------------------------
  1585. bool __fastcall TManagementScript::HandleExtendedException(Exception * E,
  1586. TTerminal * Terminal)
  1587. {
  1588. bool Result = TScript::HandleExtendedException(E);
  1589. if (Terminal == NULL)
  1590. {
  1591. Terminal = FTerminal;
  1592. }
  1593. if ((Terminal != NULL) && (Terminal == FTerminal) && (dynamic_cast<EFatal*>(E) != NULL))
  1594. {
  1595. try
  1596. {
  1597. DoClose(Terminal);
  1598. }
  1599. catch(...)
  1600. {
  1601. // ignore disconnect errors
  1602. }
  1603. }
  1604. return Result;
  1605. }
  1606. //---------------------------------------------------------------------------
  1607. void __fastcall TManagementScript::Connect(const AnsiString Session,
  1608. TOptions * Options, bool CheckParams)
  1609. {
  1610. try
  1611. {
  1612. bool DefaultsOnly;
  1613. TSessionData * Data = FStoredSessions->ParseUrl(Session, Options, DefaultsOnly);
  1614. try
  1615. {
  1616. if (CheckParams)
  1617. {
  1618. TScriptCommands::CheckParams(Options, false);
  1619. }
  1620. assert(Data != NULL);
  1621. if (!Data->CanLogin || DefaultsOnly)
  1622. {
  1623. if (Data->HostName.IsEmpty())
  1624. {
  1625. AnsiString Value;
  1626. Input(LoadStr(SCRIPT_HOST_PROMPT), Value, false);
  1627. Data->HostName = Value;
  1628. }
  1629. assert(Data->CanLogin);
  1630. }
  1631. TTerminal * Terminal = FTerminalList->NewTerminal(Data);
  1632. try
  1633. {
  1634. Terminal->AutoReadDirectory = false;
  1635. Terminal->OnInformation = TerminalInformation;
  1636. Terminal->OnPromptUser = TerminalPromptUser;
  1637. Terminal->OnShowExtendedException = OnShowExtendedException;
  1638. Terminal->OnQueryUser = OnTerminalQueryUser;
  1639. Terminal->OnProgress = TerminalOperationProgress;
  1640. Terminal->OnFinished = TerminalOperationFinished;
  1641. ConnectTerminal(Terminal);
  1642. }
  1643. catch(Exception & E)
  1644. {
  1645. // make sure errors (mainly fatal ones) are associated
  1646. // with this terminal, not the last active one
  1647. bool Handled = HandleExtendedException(&E, Terminal);
  1648. FTerminalList->FreeTerminal(Terminal);
  1649. Terminal = NULL;
  1650. if (!Handled)
  1651. {
  1652. throw;
  1653. }
  1654. }
  1655. if (Terminal != NULL)
  1656. {
  1657. FTerminal = Terminal;
  1658. if (!Data->LocalDirectory.IsEmpty())
  1659. {
  1660. try
  1661. {
  1662. DoChangeLocalDirectory(ExpandFileName(Data->LocalDirectory));
  1663. }
  1664. catch(Exception & E)
  1665. {
  1666. if (!HandleExtendedException(&E))
  1667. {
  1668. throw;
  1669. }
  1670. }
  1671. }
  1672. PrintActiveSession();
  1673. }
  1674. }
  1675. __finally
  1676. {
  1677. delete Data;
  1678. }
  1679. }
  1680. catch(Exception & E)
  1681. {
  1682. if (!HandleExtendedException(&E))
  1683. {
  1684. throw;
  1685. }
  1686. }
  1687. }
  1688. //---------------------------------------------------------------------------
  1689. void __fastcall TManagementScript::DoClose(TTerminal * Terminal)
  1690. {
  1691. int Index = FTerminalList->IndexOf(Terminal);
  1692. assert(Index >= 0);
  1693. try
  1694. {
  1695. if (Terminal->Active)
  1696. {
  1697. Terminal->Close();
  1698. }
  1699. AnsiString SessionName = Terminal->SessionData->SessionName;
  1700. FreeTerminal(Terminal);
  1701. PrintLine(FMTLOAD(SCRIPT_SESSION_CLOSED, (SessionName)));
  1702. }
  1703. __finally
  1704. {
  1705. if (FTerminalList->Count > 0)
  1706. {
  1707. if (Index < FTerminalList->Count)
  1708. {
  1709. FTerminal = FTerminalList->Terminals[Index];
  1710. }
  1711. else
  1712. {
  1713. FTerminal = FTerminalList->Terminals[0];
  1714. }
  1715. PrintActiveSession();
  1716. }
  1717. else
  1718. {
  1719. FTerminal = NULL;
  1720. PrintLine(LoadStr(SCRIPT_NO_SESSION));
  1721. }
  1722. }
  1723. }
  1724. //---------------------------------------------------------------------------
  1725. void __fastcall TManagementScript::DoChangeLocalDirectory(AnsiString Directory)
  1726. {
  1727. if (!SetCurrentDir(Directory))
  1728. {
  1729. throw Exception(FMTLOAD(CHANGE_DIR_ERROR, (Directory)));
  1730. }
  1731. }
  1732. //---------------------------------------------------------------------------
  1733. void __fastcall TManagementScript::ExitProc(TScriptProcParams * /*Parameters*/)
  1734. {
  1735. FContinue = false;
  1736. }
  1737. //---------------------------------------------------------------------------
  1738. void __fastcall TManagementScript::OpenProc(TScriptProcParams * Parameters)
  1739. {
  1740. Connect(Parameters->ParamCount > 0 ? Parameters->Param[1] : AnsiString(),
  1741. Parameters, true);
  1742. }
  1743. //---------------------------------------------------------------------------
  1744. void __fastcall TManagementScript::CloseProc(TScriptProcParams * Parameters)
  1745. {
  1746. CheckSession();
  1747. TTerminal * Terminal;
  1748. if (Parameters->ParamCount == 0)
  1749. {
  1750. Terminal = FTerminal;
  1751. }
  1752. else
  1753. {
  1754. Terminal = FindSession(Parameters->Param[1]);
  1755. }
  1756. DoClose(Terminal);
  1757. }
  1758. //---------------------------------------------------------------------------
  1759. void __fastcall TManagementScript::SessionProc(TScriptProcParams * Parameters)
  1760. {
  1761. CheckSession();
  1762. if (Parameters->ParamCount == 0)
  1763. {
  1764. for (int i = 0; i < FTerminalList->Count; i++)
  1765. {
  1766. PrintLine(FORMAT("%3d %s",
  1767. (i + 1, FTerminalList->Terminals[i]->SessionData->SessionName)));
  1768. }
  1769. PrintActiveSession();
  1770. }
  1771. else
  1772. {
  1773. FTerminal = FindSession(Parameters->Param[1]);
  1774. PrintActiveSession();
  1775. }
  1776. }
  1777. //---------------------------------------------------------------------------
  1778. void __fastcall TManagementScript::LPwdProc(TScriptProcParams * /*Parameters*/)
  1779. {
  1780. PrintLine(GetCurrentDir());
  1781. }
  1782. //---------------------------------------------------------------------------
  1783. void __fastcall TManagementScript::LCdProc(TScriptProcParams * Parameters)
  1784. {
  1785. assert(Parameters->ParamCount == 1);
  1786. DoChangeLocalDirectory(Parameters->Param[1]);
  1787. PrintLine(GetCurrentDir());
  1788. }
  1789. //---------------------------------------------------------------------------
  1790. void __fastcall TManagementScript::LLsProc(TScriptProcParams * Parameters)
  1791. {
  1792. AnsiString Directory;
  1793. AnsiString Mask;
  1794. if (Parameters->ParamCount > 0)
  1795. {
  1796. Directory = Parameters->Param[1];
  1797. Mask = ExtractFileName(Directory);
  1798. if (TFileMasks::IsMask(Mask))
  1799. {
  1800. Directory = ExtractFilePath(Directory);
  1801. }
  1802. else
  1803. {
  1804. Mask = "";
  1805. }
  1806. }
  1807. if (Directory.IsEmpty())
  1808. {
  1809. Directory = GetCurrentDir();
  1810. }
  1811. if (Mask.IsEmpty())
  1812. {
  1813. Mask = "*.*";
  1814. }
  1815. TSearchRec SearchRec;
  1816. int FindAttrs = faReadOnly | faHidden | faSysFile | faDirectory | faArchive;
  1817. if (FindFirst(IncludeTrailingBackslash(Directory) + Mask, FindAttrs, SearchRec) != 0)
  1818. {
  1819. throw Exception(FMTLOAD(LIST_DIR_ERROR, (Directory)));
  1820. }
  1821. try
  1822. {
  1823. AnsiString TimeFormat = FixedLenDateTimeFormat(ShortTimeFormat);
  1824. AnsiString DateFormat = FixedLenDateTimeFormat(ShortDateFormat);
  1825. int DateLen = 0;
  1826. int TimeLen = 0;
  1827. bool First = true;
  1828. do
  1829. {
  1830. if (SearchRec.Name != ".")
  1831. {
  1832. TDateTime DateTime = FileDateToDateTime(SearchRec.Time);
  1833. AnsiString TimeStr = FormatDateTime(TimeFormat, DateTime);
  1834. AnsiString DateStr = FormatDateTime(DateFormat, DateTime);
  1835. if (First)
  1836. {
  1837. if (TimeLen < TimeStr.Length())
  1838. {
  1839. TimeLen = TimeStr.Length();
  1840. }
  1841. if (DateLen < DateStr.Length())
  1842. {
  1843. DateLen = DateStr.Length();
  1844. }
  1845. First = false;
  1846. }
  1847. AnsiString SizeStr;
  1848. if (FLAGSET(SearchRec.Attr, faDirectory))
  1849. {
  1850. SizeStr = "<DIR>";
  1851. }
  1852. else
  1853. {
  1854. SizeStr = FORMAT("%14.0n", (double(SearchRec.Size)));
  1855. }
  1856. PrintLine(FORMAT("%-*s %-*s %-14s %s", (
  1857. DateLen, DateStr, TimeLen, TimeStr, SizeStr, SearchRec.Name)));
  1858. }
  1859. }
  1860. while (FindNext(SearchRec) == 0);
  1861. }
  1862. __finally
  1863. {
  1864. FindClose(SearchRec);
  1865. }
  1866. }
  1867. //---------------------------------------------------------------------------