Script.cpp 55 KB

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