Script.cpp 55 KB

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