Script.cpp 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976
  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. // on error user may select "skip", then we get NULL
  729. if (FileList != NULL)
  730. {
  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. //---------------------------------------------------------------------------
  749. void __fastcall TScript::RmProc(TScriptProcParams * Parameters)
  750. {
  751. CheckSession();
  752. TStrings * FileList = CreateFileList(Parameters, 1, Parameters->ParamCount, fltMask);
  753. try
  754. {
  755. FTerminal->DeleteFiles(FileList);
  756. }
  757. __finally
  758. {
  759. FreeFileList(FileList);
  760. }
  761. }
  762. //---------------------------------------------------------------------------
  763. void __fastcall TScript::RmDirProc(TScriptProcParams * Parameters)
  764. {
  765. CheckSession();
  766. TStrings * FileList = CreateFileList(Parameters, 1, Parameters->ParamCount, fltDirectories);
  767. try
  768. {
  769. FTerminal->DeleteFiles(FileList);
  770. }
  771. __finally
  772. {
  773. FreeFileList(FileList);
  774. }
  775. }
  776. //---------------------------------------------------------------------------
  777. void __fastcall TScript::MvProc(TScriptProcParams * Parameters)
  778. {
  779. CheckSession();
  780. TStrings * FileList = CreateFileList(Parameters, 1, Parameters->ParamCount - 1,
  781. fltMask);
  782. try
  783. {
  784. assert(Parameters->ParamCount >= 1);
  785. AnsiString Target = Parameters->Param[Parameters->ParamCount];
  786. AnsiString TargetDirectory = UnixExtractFilePath(Target);
  787. AnsiString FileMask = UnixExtractFileName(Target);
  788. FTerminal->MoveFiles(FileList, TargetDirectory, FileMask);
  789. }
  790. __finally
  791. {
  792. FreeFileList(FileList);
  793. }
  794. }
  795. //---------------------------------------------------------------------------
  796. void __fastcall TScript::ChModProc(TScriptProcParams * Parameters)
  797. {
  798. CheckSession();
  799. TStrings * FileList = CreateFileList(Parameters, 2, Parameters->ParamCount,
  800. fltMask);
  801. try
  802. {
  803. TRemoteProperties Properties;
  804. Properties.Valid = TValidProperties() << vpRights;
  805. Properties.Rights.Octal = Parameters->Param[1];
  806. FTerminal->ChangeFilesProperties(FileList, &Properties);
  807. }
  808. __finally
  809. {
  810. FreeFileList(FileList);
  811. }
  812. }
  813. //---------------------------------------------------------------------------
  814. void __fastcall TScript::LnProc(TScriptProcParams * Parameters)
  815. {
  816. CheckSession();
  817. assert(Parameters->ParamCount == 2);
  818. FTerminal->CreateLink(Parameters->Param[2], Parameters->Param[1], true);
  819. }
  820. //---------------------------------------------------------------------------
  821. void __fastcall TScript::MkDirProc(TScriptProcParams * Parameters)
  822. {
  823. CheckSession();
  824. FTerminal->CreateDirectory(Parameters->Param[1]);
  825. }
  826. //---------------------------------------------------------------------------
  827. void __fastcall TScript::GetProc(TScriptProcParams * Parameters)
  828. {
  829. CheckSession();
  830. ResetTransfer();
  831. int LastFileParam = (Parameters->ParamCount == 1 ? 1 : Parameters->ParamCount - 1);
  832. TStrings * FileList = CreateFileList(Parameters, 1, LastFileParam,
  833. fltQueryServer | fltMask);
  834. try
  835. {
  836. TCopyParamType CopyParam = FCopyParam;
  837. AnsiString TargetDirectory;
  838. if (Parameters->ParamCount == 1)
  839. {
  840. TargetDirectory = GetCurrentDir();
  841. CopyParam.FileMask = "";
  842. }
  843. else
  844. {
  845. AnsiString Target = Parameters->Param[Parameters->ParamCount];
  846. TargetDirectory = ExtractFilePath(Target);
  847. if (TargetDirectory.IsEmpty())
  848. {
  849. TargetDirectory = GetCurrentDir();
  850. }
  851. CopyParam.FileMask = ExtractFileName(Target);
  852. }
  853. int Params = FLAGMASK(!FConfirm, cpNoConfirmation);
  854. if (Parameters->FindSwitch("delete"))
  855. {
  856. Params |= cpDelete;
  857. }
  858. CopyParamParams(CopyParam, Parameters);
  859. CheckParams(Parameters);
  860. FTerminal->CopyToLocal(FileList, TargetDirectory, &CopyParam, Params);
  861. }
  862. __finally
  863. {
  864. FreeFileList(FileList);
  865. }
  866. }
  867. //---------------------------------------------------------------------------
  868. void __fastcall TScript::PutProc(TScriptProcParams * Parameters)
  869. {
  870. CheckSession();
  871. ResetTransfer();
  872. int LastFileParam = (Parameters->ParamCount == 1 ? 1 : Parameters->ParamCount - 1);
  873. TStrings * FileList = CreateLocalFileList(Parameters, 1, LastFileParam, fltMask);
  874. try
  875. {
  876. TCopyParamType CopyParam = FCopyParam;
  877. AnsiString TargetDirectory;
  878. if (Parameters->ParamCount == 1)
  879. {
  880. TargetDirectory = FTerminal->CurrentDirectory;
  881. CopyParam.FileMask = "";
  882. }
  883. else
  884. {
  885. AnsiString Target = Parameters->Param[Parameters->ParamCount];
  886. TargetDirectory = UnixExtractFilePath(Target);
  887. if (TargetDirectory.IsEmpty())
  888. {
  889. TargetDirectory = FTerminal->CurrentDirectory;
  890. }
  891. CopyParam.FileMask = UnixExtractFileName(Target);
  892. }
  893. int Params = FLAGMASK(!FConfirm, cpNoConfirmation);
  894. if (Parameters->FindSwitch("delete"))
  895. {
  896. Params |= cpDelete;
  897. }
  898. CopyParamParams(CopyParam, Parameters);
  899. CheckParams(Parameters);
  900. FTerminal->CopyToRemote(FileList, TargetDirectory, &CopyParam, Params);
  901. }
  902. __finally
  903. {
  904. FreeFileList(FileList);
  905. }
  906. }
  907. //---------------------------------------------------------------------------
  908. void __fastcall TScript::OptionImpl(AnsiString OptionName, AnsiString ValueName)
  909. {
  910. enum { Echo, Batch, Confirm, Transfer, SynchDelete, Exclude, Include };
  911. static const char * Names[] = { "echo", "batch", "confirm", "transfer",
  912. "synchdelete", "exclude", "include" };
  913. enum { Off, On };
  914. static const char * ToggleNames[] = { "off", "on" };
  915. assert((BatchOff == 0) && (BatchOn == 1) && (BatchAbort == 2) && (BatchContinue == 3));
  916. static const char * BatchModeNames[] = { "off", "on", "abort", "continue" };
  917. assert((tmBinary == 0) && (tmAscii == 1) && (tmAutomatic == 2));
  918. static const char * TransferModeNames[] = { "binary", "ascii", "automatic" };
  919. int Option = -1;
  920. if (!OptionName.IsEmpty())
  921. {
  922. Option = TScriptCommands::FindCommand(Names, LENOF(Names), OptionName);
  923. if (Option < 0)
  924. {
  925. throw Exception(FMTLOAD(SCRIPT_OPTION_UNKNOWN, (OptionName)));
  926. }
  927. else
  928. {
  929. OptionName = Names[Option];
  930. }
  931. }
  932. #define OPT(OPT) ((Option < 0) || (Option == OPT))
  933. const char * ListFormat = "%-12s %-10s";
  934. bool SetValue = !ValueName.IsEmpty();
  935. if (OPT(Echo))
  936. {
  937. if (SetValue)
  938. {
  939. int Value = TScriptCommands::FindCommand(ToggleNames, LENOF(ToggleNames), ValueName);
  940. if (Value < 0)
  941. {
  942. throw Exception(FMTLOAD(SCRIPT_VALUE_UNKNOWN, (ValueName, OptionName)));
  943. }
  944. FEcho = (Value == On);
  945. }
  946. PrintLine(FORMAT(ListFormat, (Names[Echo], ToggleNames[FEcho ? On : Off])));
  947. }
  948. if (OPT(Batch))
  949. {
  950. if (SetValue)
  951. {
  952. int Value = TScriptCommands::FindCommand(BatchModeNames, LENOF(BatchModeNames), ValueName);
  953. if (Value < 0)
  954. {
  955. throw Exception(FMTLOAD(SCRIPT_VALUE_UNKNOWN, (ValueName, OptionName)));
  956. }
  957. FBatch = (TBatchMode)Value;
  958. }
  959. PrintLine(FORMAT(ListFormat, (Names[Batch], BatchModeNames[FBatch])));
  960. }
  961. if (OPT(Confirm))
  962. {
  963. if (SetValue)
  964. {
  965. int Value = TScriptCommands::FindCommand(ToggleNames, LENOF(ToggleNames), ValueName);
  966. if (Value < 0)
  967. {
  968. throw Exception(FMTLOAD(SCRIPT_VALUE_UNKNOWN, (ValueName, OptionName)));
  969. }
  970. FConfirm = (Value == On);
  971. }
  972. PrintLine(FORMAT(ListFormat, (Names[Confirm], ToggleNames[FConfirm ? On : Off])));
  973. }
  974. if (OPT(Transfer))
  975. {
  976. if (SetValue)
  977. {
  978. int Value = TScriptCommands::FindCommand(TransferModeNames,
  979. LENOF(TransferModeNames), ValueName);
  980. if (Value < 0)
  981. {
  982. throw Exception(FMTLOAD(SCRIPT_VALUE_UNKNOWN, (ValueName, OptionName)));
  983. }
  984. FCopyParam.TransferMode = (TTransferMode)Value;
  985. }
  986. assert(FCopyParam.TransferMode < LENOF(TransferModeNames));
  987. const char * Value = TransferModeNames[FCopyParam.TransferMode];
  988. PrintLine(FORMAT(ListFormat, (Names[Transfer], Value)));
  989. }
  990. // omit the option in listing
  991. if (Option == SynchDelete)
  992. {
  993. if (SetValue)
  994. {
  995. int Value = TScriptCommands::FindCommand(ToggleNames, LENOF(ToggleNames), ValueName);
  996. if (Value < 0)
  997. {
  998. throw Exception(FMTLOAD(SCRIPT_VALUE_UNKNOWN, (ValueName, OptionName)));
  999. }
  1000. FSynchronizeParams =
  1001. (FSynchronizeParams & ~TTerminal::spDelete) |
  1002. FLAGMASK(Value == On, TTerminal::spDelete);
  1003. }
  1004. PrintLine(FORMAT(ListFormat, (Names[SynchDelete],
  1005. ToggleNames[FLAGSET(FSynchronizeParams, TTerminal::spDelete) ? On : Off])));
  1006. }
  1007. static const char * Clear = "clear";
  1008. if (OPT(Include))
  1009. {
  1010. if (SetValue)
  1011. {
  1012. FCopyParam.NegativeExclude = true;
  1013. FCopyParam.ExcludeFileMask =
  1014. (ValueName == Clear ? AnsiString() : ValueName);
  1015. }
  1016. if (SetValue ||
  1017. (FCopyParam.NegativeExclude && !FCopyParam.ExcludeFileMask.Masks.IsEmpty()))
  1018. {
  1019. PrintLine(FORMAT(ListFormat, (Names[Include], FCopyParam.ExcludeFileMask.Masks)));
  1020. }
  1021. }
  1022. if (OPT(Exclude))
  1023. {
  1024. if (SetValue)
  1025. {
  1026. FCopyParam.NegativeExclude = false;
  1027. FCopyParam.ExcludeFileMask =
  1028. (ValueName == Clear ? AnsiString() : ValueName);
  1029. }
  1030. if (SetValue ||
  1031. (!FCopyParam.NegativeExclude && !FCopyParam.ExcludeFileMask.Masks.IsEmpty()))
  1032. {
  1033. PrintLine(FORMAT(ListFormat, (Names[Exclude], FCopyParam.ExcludeFileMask.Masks)));
  1034. }
  1035. }
  1036. #undef OPT
  1037. }
  1038. //---------------------------------------------------------------------------
  1039. void __fastcall TScript::OptionProc(TScriptProcParams * Parameters)
  1040. {
  1041. AnsiString OptionName;
  1042. AnsiString ValueName;
  1043. if (Parameters->ParamCount >= 1)
  1044. {
  1045. OptionName = Parameters->Param[1];
  1046. }
  1047. if (Parameters->ParamCount >= 2)
  1048. {
  1049. ValueName = Parameters->Param[2];
  1050. }
  1051. OptionImpl(OptionName, ValueName);
  1052. }
  1053. //---------------------------------------------------------------------------
  1054. void __fastcall TScript::AsciiProc(TScriptProcParams * /*Parameters*/)
  1055. {
  1056. OptionImpl("transfer", "ascii");
  1057. }
  1058. //---------------------------------------------------------------------------
  1059. void __fastcall TScript::BinaryProc(TScriptProcParams * /*Parameters*/)
  1060. {
  1061. OptionImpl("transfer", "binary");
  1062. }
  1063. //---------------------------------------------------------------------------
  1064. void __fastcall TScript::SynchronizeDirectories(TScriptProcParams * Parameters,
  1065. AnsiString & LocalDirectory, AnsiString & RemoteDirectory, int FirstParam)
  1066. {
  1067. if (Parameters->ParamCount >= FirstParam)
  1068. {
  1069. LocalDirectory = Parameters->Param[FirstParam];
  1070. }
  1071. else
  1072. {
  1073. LocalDirectory = GetCurrentDir();
  1074. }
  1075. if (Parameters->ParamCount >= FirstParam + 1)
  1076. {
  1077. RemoteDirectory = Parameters->Param[FirstParam + 1];
  1078. }
  1079. else
  1080. {
  1081. RemoteDirectory = FTerminal->CurrentDirectory;
  1082. }
  1083. }
  1084. //---------------------------------------------------------------------------
  1085. void __fastcall TScript::SynchronizeProc(TScriptProcParams * Parameters)
  1086. {
  1087. CheckSession();
  1088. ResetTransfer();
  1089. static const char * ModeNames[] = { "remote", "local", "both" };
  1090. AnsiString ModeName = Parameters->Param[1];
  1091. assert(FSynchronizeMode < 0);
  1092. FSynchronizeMode = TScriptCommands::FindCommand(ModeNames, LENOF(ModeNames), ModeName);
  1093. try
  1094. {
  1095. if (FSynchronizeMode < 0)
  1096. {
  1097. throw Exception(FMTLOAD(SCRIPT_OPTION_UNKNOWN, (ModeName)));
  1098. }
  1099. AnsiString LocalDirectory;
  1100. AnsiString RemoteDirectory;
  1101. SynchronizeDirectories(Parameters, LocalDirectory, RemoteDirectory, 2);
  1102. TCopyParamType CopyParam = FCopyParam;
  1103. CopyParamParams(CopyParam, Parameters);
  1104. int SynchronizeParams = FSynchronizeParams | TTerminal::spNoConfirmation;
  1105. AnsiString Value;
  1106. if (Parameters->FindSwitch("delete"))
  1107. {
  1108. SynchronizeParams |= TTerminal::spDelete;
  1109. }
  1110. if (Parameters->FindSwitch("mirror") &&
  1111. (FSynchronizeMode != TTerminal::smBoth))
  1112. {
  1113. SynchronizeParams |= TTerminal::spMirror;
  1114. }
  1115. if (Parameters->FindSwitch("criteria", Value))
  1116. {
  1117. enum { Time, Size, Both };
  1118. static const char * CriteriaNames[] = { "time", "size", "both" };
  1119. int Criteria = TScriptCommands::FindCommand(CriteriaNames, LENOF(CriteriaNames), Value);
  1120. switch (Criteria)
  1121. {
  1122. case Time:
  1123. SynchronizeParams &= ~(TTerminal::spNotByTime | TTerminal::spBySize);
  1124. break;
  1125. case Size:
  1126. SynchronizeParams |= TTerminal::spNotByTime | TTerminal::spBySize;
  1127. break;
  1128. case Both:
  1129. SynchronizeParams &= ~TTerminal::spNotByTime;
  1130. SynchronizeParams |= TTerminal::spBySize;
  1131. break;
  1132. }
  1133. }
  1134. // enforce rules
  1135. if (FSynchronizeMode == TTerminal::smBoth)
  1136. {
  1137. SynchronizeParams &= ~(TTerminal::spNotByTime | TTerminal::spBySize);
  1138. }
  1139. if (FLAGSET(SynchronizeParams, TTerminal::spMirror))
  1140. {
  1141. SynchronizeParams &= ~TTerminal::spNotByTime;
  1142. }
  1143. CheckParams(Parameters);
  1144. PrintLine(LoadStr(SCRIPT_SYNCHRONIZE_COLLECTING));
  1145. TSynchronizeChecklist * Checklist =
  1146. FTerminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
  1147. static_cast<TTerminal::TSynchronizeMode>(FSynchronizeMode),
  1148. &CopyParam, SynchronizeParams, OnTerminalSynchronizeDirectory, NULL);
  1149. try
  1150. {
  1151. if (Checklist->Count > 0)
  1152. {
  1153. PrintLine(LoadStr(SCRIPT_SYNCHRONIZE_SYNCHRONIZING));
  1154. FTerminal->SynchronizeApply(Checklist, LocalDirectory, RemoteDirectory,
  1155. &CopyParam, SynchronizeParams, OnTerminalSynchronizeDirectory);
  1156. }
  1157. else
  1158. {
  1159. PrintLine(LoadStr(SCRIPT_SYNCHRONIZE_NODIFFERENCE));
  1160. }
  1161. }
  1162. __finally
  1163. {
  1164. delete Checklist;
  1165. }
  1166. }
  1167. __finally
  1168. {
  1169. FSynchronizeMode = -1;
  1170. }
  1171. }
  1172. //---------------------------------------------------------------------------
  1173. void __fastcall TScript::Synchronize(const AnsiString LocalDirectory,
  1174. const AnsiString RemoteDirectory, const TCopyParamType & CopyParam,
  1175. int SynchronizeParams, TSynchronizeChecklist ** Checklist)
  1176. {
  1177. try
  1178. {
  1179. FKeepingUpToDate = true;
  1180. TSynchronizeChecklist * AChecklist =
  1181. FTerminal->SynchronizeCollect(LocalDirectory, RemoteDirectory, TTerminal::smRemote,
  1182. &CopyParam, SynchronizeParams, NULL, NULL);
  1183. try
  1184. {
  1185. if (AChecklist->Count > 0)
  1186. {
  1187. FTerminal->SynchronizeApply(AChecklist, LocalDirectory, RemoteDirectory,
  1188. &CopyParam, SynchronizeParams, OnTerminalSynchronizeDirectory);
  1189. }
  1190. }
  1191. __finally
  1192. {
  1193. if (Checklist == NULL)
  1194. {
  1195. delete AChecklist;
  1196. }
  1197. else
  1198. {
  1199. *Checklist = AChecklist;
  1200. }
  1201. }
  1202. // to break line after the last transfer (if any);
  1203. Print("");
  1204. FKeepingUpToDate = false;
  1205. }
  1206. catch(Exception & E)
  1207. {
  1208. FKeepingUpToDate = false;
  1209. HandleExtendedException(&E);
  1210. throw;
  1211. }
  1212. }
  1213. //---------------------------------------------------------------------------
  1214. void __fastcall TScript::KeepUpToDateProc(TScriptProcParams * Parameters)
  1215. {
  1216. if (OnSynchronizeStartStop == NULL)
  1217. {
  1218. Abort();
  1219. }
  1220. CheckSession();
  1221. ResetTransfer();
  1222. AnsiString LocalDirectory;
  1223. AnsiString RemoteDirectory;
  1224. SynchronizeDirectories(Parameters, LocalDirectory, RemoteDirectory, 1);
  1225. int SynchronizeParams = FSynchronizeParams | TTerminal::spNoConfirmation |
  1226. TTerminal::spNoRecurse | TTerminal::spUseCache | TTerminal::spDelayProgress |
  1227. TTerminal::spSubDirs;
  1228. if (Parameters->FindSwitch("delete"))
  1229. {
  1230. SynchronizeParams |= TTerminal::spDelete;
  1231. }
  1232. TCopyParamType CopyParam = FCopyParam;
  1233. CopyParamParams(CopyParam, Parameters);
  1234. CheckParams(Parameters);
  1235. PrintLine(LoadStr(SCRIPT_KEEPING_UP_TO_DATE));
  1236. OnSynchronizeStartStop(this, LocalDirectory, RemoteDirectory, CopyParam, SynchronizeParams);
  1237. }
  1238. //---------------------------------------------------------------------------
  1239. //---------------------------------------------------------------------------
  1240. __fastcall TManagementScript::TManagementScript(TStoredSessionList * StoredSessions,
  1241. bool LimitedOutput) :
  1242. TScript(LimitedOutput)
  1243. {
  1244. assert(StoredSessions != NULL);
  1245. FOnInput = NULL;
  1246. FOnTerminalPromptUser = NULL;
  1247. FOnShowExtendedException = NULL;
  1248. FOnTerminalQueryUser = NULL;
  1249. FStoredSessions = StoredSessions;
  1250. FTerminalList = new TTerminalList(Configuration);
  1251. FOnQueryCancel = NULL;
  1252. FContinue = true;
  1253. OnTerminalSynchronizeDirectory = TerminalSynchronizeDirectory;
  1254. FCommands->Register("exit", SCRIPT_EXIT_DESC, SCRIPT_EXIT_HELP, &ExitProc, 0, 0, false);
  1255. FCommands->Register("bye", 0, SCRIPT_EXIT_HELP, &ExitProc, 0, 0, false);
  1256. FCommands->Register("open", SCRIPT_OPEN_DESC, SCRIPT_OPEN_HELP2, &OpenProc, 0, 1, true);
  1257. FCommands->Register("close", SCRIPT_CLOSE_DESC, SCRIPT_CLOSE_HELP, &CloseProc, 0, 1, false);
  1258. FCommands->Register("session", SCRIPT_SESSION_DESC, SCRIPT_SESSION_HELP, &SessionProc, 0, 1, false);
  1259. FCommands->Register("lpwd", SCRIPT_LPWD_DESC, SCRIPT_LPWD_HELP, &LPwdProc, 0, 0, false);
  1260. FCommands->Register("lcd", SCRIPT_LCD_DESC, SCRIPT_LCD_HELP, &LCdProc, 1, 1, false);
  1261. FCommands->Register("lls", SCRIPT_LLS_DESC, SCRIPT_LLS_HELP, &LLsProc, 0, 1, false);
  1262. }
  1263. //---------------------------------------------------------------------------
  1264. __fastcall TManagementScript::~TManagementScript()
  1265. {
  1266. while (FTerminalList->Count > 0)
  1267. {
  1268. FreeTerminal(FTerminalList->Terminals[0]);
  1269. }
  1270. delete FTerminalList;
  1271. }
  1272. //---------------------------------------------------------------------------
  1273. void __fastcall TManagementScript::FreeTerminal(TTerminal * Terminal)
  1274. {
  1275. if (!Terminal->SessionData->Name.IsEmpty())
  1276. {
  1277. Terminal->SessionData->RemoteDirectory = Terminal->CurrentDirectory;
  1278. TSessionData * Data;
  1279. Data = (TSessionData *)StoredSessions->FindByName(Terminal->SessionData->Name);
  1280. if (Data != NULL)
  1281. {
  1282. bool Changed = false;
  1283. if (Terminal->SessionData->UpdateDirectories)
  1284. {
  1285. Data->RemoteDirectory = Terminal->SessionData->RemoteDirectory;
  1286. Changed = true;
  1287. }
  1288. if (Changed)
  1289. {
  1290. // only modified, implicit
  1291. StoredSessions->Save(false, false);
  1292. }
  1293. }
  1294. }
  1295. FTerminalList->FreeTerminal(Terminal);
  1296. }
  1297. //---------------------------------------------------------------------------
  1298. void __fastcall TManagementScript::Input(const AnsiString Prompt,
  1299. AnsiString & Str, bool AllowEmpty)
  1300. {
  1301. do
  1302. {
  1303. Str = "";
  1304. if (FOnInput != NULL)
  1305. {
  1306. FOnInput(this, Prompt, Str);
  1307. }
  1308. else
  1309. {
  1310. Abort();
  1311. }
  1312. }
  1313. while (Str.Trim().IsEmpty() && !AllowEmpty);
  1314. }
  1315. //---------------------------------------------------------------------------
  1316. void __fastcall TManagementScript::PrintProgress(bool First, const AnsiString Str)
  1317. {
  1318. if (FOnPrintProgress != NULL)
  1319. {
  1320. FOnPrintProgress(this, First, Str);
  1321. }
  1322. }
  1323. //---------------------------------------------------------------------------
  1324. void __fastcall TManagementScript::ResetTransfer()
  1325. {
  1326. TScript::ResetTransfer();
  1327. FLastProgressFile = "";
  1328. FLastProgressTime = 0;
  1329. FLastProgressMessage = "";
  1330. }
  1331. //---------------------------------------------------------------------------
  1332. bool __fastcall TManagementScript::QueryCancel()
  1333. {
  1334. bool Result = false;
  1335. if (OnQueryCancel != NULL)
  1336. {
  1337. OnQueryCancel(this, Result);
  1338. }
  1339. return Result;
  1340. }
  1341. //---------------------------------------------------------------------------
  1342. void __fastcall TManagementScript::TerminalInformation(TTerminal * Terminal,
  1343. const AnsiString & Str, bool /*Status*/, bool /*Active*/)
  1344. {
  1345. assert(Terminal != NULL);
  1346. if (Terminal->Status == ssOpening)
  1347. {
  1348. PrintLine(Str);
  1349. }
  1350. }
  1351. //---------------------------------------------------------------------------
  1352. void __fastcall TManagementScript::TerminalPromptUser(TTerminal * Terminal,
  1353. TPromptKind Kind, AnsiString Name, AnsiString Instructions, TStrings * Prompts,
  1354. TStrings * Results, bool & Result, void * Arg)
  1355. {
  1356. if ((!Terminal->StoredCredentialsTried || !IsAuthenticationPrompt(Kind)) &&
  1357. (OnTerminalPromptUser != NULL))
  1358. {
  1359. OnTerminalPromptUser(Terminal, Kind, Name, Instructions, Prompts, Results, Result, Arg);
  1360. }
  1361. }
  1362. //---------------------------------------------------------------------------
  1363. bool __fastcall TManagementScript::Synchronizing()
  1364. {
  1365. return (FKeepingUpToDate || (FSynchronizeMode >= 0));
  1366. }
  1367. //---------------------------------------------------------------------------
  1368. void __fastcall TManagementScript::ShowPendingProgress()
  1369. {
  1370. if (!FSynchronizeIntro.IsEmpty())
  1371. {
  1372. if (Synchronizing())
  1373. {
  1374. PrintLine(FSynchronizeIntro);
  1375. }
  1376. FSynchronizeIntro = "";
  1377. }
  1378. }
  1379. //---------------------------------------------------------------------------
  1380. void __fastcall TManagementScript::TerminalOperationProgress(
  1381. TFileOperationProgressType & ProgressData, TCancelStatus & Cancel)
  1382. {
  1383. if (ProgressData.Operation == foCopy)
  1384. {
  1385. if (ProgressData.InProgress && !ProgressData.FileName.IsEmpty())
  1386. {
  1387. bool DoPrint = false;
  1388. bool First = false;
  1389. AnsiString ProgressFileName = ProgressData.FileName;
  1390. if (ProgressData.Side == osLocal)
  1391. {
  1392. ProgressFileName = ExcludeTrailingBackslash(ProgressFileName);
  1393. }
  1394. else
  1395. {
  1396. ProgressFileName = UnixExcludeTrailingBackslash(ProgressFileName);
  1397. }
  1398. if (ProgressFileName != FLastProgressFile)
  1399. {
  1400. First = true;
  1401. DoPrint = true;
  1402. ShowPendingProgress();
  1403. }
  1404. if (!DoPrint && ((FLastProgressTime != time(NULL)) || ProgressData.IsTransferDone()))
  1405. {
  1406. DoPrint = true;
  1407. }
  1408. if (DoPrint)
  1409. {
  1410. static int WidthFileName = 25;
  1411. AnsiString FileName;
  1412. if (FLimitedOutput)
  1413. {
  1414. FileName = MinimizeName(ProgressFileName, WidthFileName,
  1415. ProgressData.Side == osRemote);
  1416. }
  1417. else
  1418. {
  1419. FileName = ProgressFileName;
  1420. }
  1421. AnsiString ProgressMessage = FORMAT("%-*s | %10d KiB | %6.1f KiB/s | %-6.6s | %3d%%",
  1422. (WidthFileName, FileName,
  1423. static_cast<int>(ProgressData.TransferedSize / 1024),
  1424. static_cast<float>(ProgressData.CPS()) / 1024,
  1425. ProgressData.AsciiTransfer ? "ascii" : "binary",
  1426. ProgressData.TransferProgress()));
  1427. if (FLastProgressMessage != ProgressMessage)
  1428. {
  1429. FLastProgressTime = time(NULL);
  1430. PrintProgress(First, ProgressMessage);
  1431. FLastProgressMessage = ProgressMessage;
  1432. FLastProgressFile = ProgressFileName;
  1433. }
  1434. }
  1435. }
  1436. else
  1437. {
  1438. FLastProgressFile = "";
  1439. }
  1440. }
  1441. if (QueryCancel())
  1442. {
  1443. Cancel = csCancel;
  1444. }
  1445. }
  1446. //---------------------------------------------------------------------------
  1447. void __fastcall TManagementScript::TerminalOperationFinished(
  1448. TFileOperation Operation, TOperationSide /*Side*/,
  1449. bool /*Temp*/, const AnsiString & FileName, Boolean Success,
  1450. bool & /*DisconnectWhenComplete*/)
  1451. {
  1452. assert(Operation != foCalculateSize);
  1453. if (Success && (Operation != foCalculateSize) && (Operation != foCopy))
  1454. {
  1455. ShowPendingProgress();
  1456. // For FKeepingUpToDate we should send events to synchronize controller eventuelly.
  1457. if (Synchronizing() && (Operation == foDelete))
  1458. {
  1459. // Note that this is duplicated with "keep up to date" log.
  1460. PrintLine(FMTLOAD(SCRIPT_SYNCHRONIZE_DELETED, (FileName)));
  1461. }
  1462. else
  1463. {
  1464. PrintLine(FileName);
  1465. }
  1466. }
  1467. }
  1468. //---------------------------------------------------------------------------
  1469. void __fastcall TManagementScript::TerminalSynchronizeDirectory(
  1470. const AnsiString LocalDirectory, const AnsiString RemoteDirectory,
  1471. bool & Continue, bool Collect)
  1472. {
  1473. int SynchronizeMode = FSynchronizeMode;
  1474. if (FKeepingUpToDate)
  1475. {
  1476. SynchronizeMode = TTerminal::smRemote;
  1477. }
  1478. AnsiString Arrow;
  1479. switch (SynchronizeMode)
  1480. {
  1481. case TTerminal::smRemote:
  1482. Arrow = "=>";
  1483. break;
  1484. case TTerminal::smLocal:
  1485. Arrow = "<=";
  1486. break;
  1487. case TTerminal::smBoth:
  1488. Arrow = "<=>";
  1489. break;
  1490. }
  1491. AnsiString Progress = FMTLOAD(SCRIPT_SYNCHRONIZE, (ExcludeTrailingBackslash(LocalDirectory),
  1492. Arrow, UnixExcludeTrailingBackslash(RemoteDirectory)));
  1493. if (Collect)
  1494. {
  1495. PrintProgress(false, Progress);
  1496. }
  1497. else
  1498. {
  1499. FSynchronizeIntro = Progress;
  1500. }
  1501. if (QueryCancel())
  1502. {
  1503. Continue = false;
  1504. }
  1505. }
  1506. //---------------------------------------------------------------------------
  1507. TTerminal * __fastcall TManagementScript::FindSession(const AnsiString Index)
  1508. {
  1509. int i = StrToIntDef(Index, -1);
  1510. if ((i <= 0) || (i > FTerminalList->Count))
  1511. {
  1512. throw Exception(FMTLOAD(SCRIPT_SESSION_INDEX_INVALID, (Index)));
  1513. }
  1514. else
  1515. {
  1516. return FTerminalList->Terminals[i - 1];
  1517. }
  1518. }
  1519. //---------------------------------------------------------------------------
  1520. void __fastcall TManagementScript::PrintActiveSession()
  1521. {
  1522. assert(FTerminal != NULL);
  1523. PrintLine(FMTLOAD(SCRIPT_ACTIVE_SESSION,
  1524. (FTerminalList->IndexOf(FTerminal) + 1, FTerminal->SessionData->SessionName)));
  1525. }
  1526. //---------------------------------------------------------------------------
  1527. bool __fastcall TManagementScript::HandleExtendedException(Exception * E,
  1528. TTerminal * Terminal)
  1529. {
  1530. bool Result = TScript::HandleExtendedException(E);
  1531. if (Terminal == NULL)
  1532. {
  1533. Terminal = FTerminal;
  1534. }
  1535. if ((Terminal != NULL) && (Terminal == FTerminal) && (dynamic_cast<EFatal*>(E) != NULL))
  1536. {
  1537. try
  1538. {
  1539. DoClose(Terminal);
  1540. }
  1541. catch(...)
  1542. {
  1543. // ignore disconnect errors
  1544. }
  1545. }
  1546. return Result;
  1547. }
  1548. //---------------------------------------------------------------------------
  1549. void __fastcall TManagementScript::Connect(const AnsiString Session,
  1550. TOptions * Options, bool CheckParams)
  1551. {
  1552. try
  1553. {
  1554. DoConnect(Session, Options, CheckParams);
  1555. }
  1556. catch(Exception & E)
  1557. {
  1558. if (!HandleExtendedException(&E))
  1559. {
  1560. throw;
  1561. }
  1562. }
  1563. }
  1564. //---------------------------------------------------------------------------
  1565. void __fastcall TManagementScript::DoConnect(const AnsiString & Session,
  1566. TOptions * Options, bool CheckParams)
  1567. {
  1568. bool DefaultsOnly;
  1569. TSessionData * Data = FStoredSessions->ParseUrl(Session, Options, DefaultsOnly);
  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. if (!Data->LocalDirectory.IsEmpty())
  1606. {
  1607. try
  1608. {
  1609. DoChangeLocalDirectory(Data->LocalDirectory);
  1610. }
  1611. catch(Exception & E)
  1612. {
  1613. if (!HandleExtendedException(&E))
  1614. {
  1615. throw;
  1616. }
  1617. }
  1618. }
  1619. }
  1620. __finally
  1621. {
  1622. delete Data;
  1623. }
  1624. PrintActiveSession();
  1625. }
  1626. //---------------------------------------------------------------------------
  1627. void __fastcall TManagementScript::DoClose(TTerminal * Terminal)
  1628. {
  1629. int Index = FTerminalList->IndexOf(Terminal);
  1630. assert(Index >= 0);
  1631. try
  1632. {
  1633. if (Terminal->Active)
  1634. {
  1635. Terminal->Close();
  1636. }
  1637. AnsiString SessionName = Terminal->SessionData->SessionName;
  1638. FreeTerminal(Terminal);
  1639. PrintLine(FMTLOAD(SCRIPT_SESSION_CLOSED, (SessionName)));
  1640. }
  1641. __finally
  1642. {
  1643. if (FTerminalList->Count > 0)
  1644. {
  1645. if (Index < FTerminalList->Count)
  1646. {
  1647. FTerminal = FTerminalList->Terminals[Index];
  1648. }
  1649. else
  1650. {
  1651. FTerminal = FTerminalList->Terminals[0];
  1652. }
  1653. PrintActiveSession();
  1654. }
  1655. else
  1656. {
  1657. FTerminal = NULL;
  1658. PrintLine(LoadStr(SCRIPT_NO_SESSION));
  1659. }
  1660. }
  1661. }
  1662. //---------------------------------------------------------------------------
  1663. void __fastcall TManagementScript::DoChangeLocalDirectory(AnsiString Directory)
  1664. {
  1665. if (!SetCurrentDir(Directory))
  1666. {
  1667. throw Exception(FMTLOAD(CHANGE_DIR_ERROR, (Directory)));
  1668. }
  1669. }
  1670. //---------------------------------------------------------------------------
  1671. void __fastcall TManagementScript::ExitProc(TScriptProcParams * /*Parameters*/)
  1672. {
  1673. FContinue = false;
  1674. }
  1675. //---------------------------------------------------------------------------
  1676. void __fastcall TManagementScript::OpenProc(TScriptProcParams * Parameters)
  1677. {
  1678. Connect(Parameters->ParamCount > 0 ? Parameters->Param[1] : AnsiString(),
  1679. Parameters, true);
  1680. }
  1681. //---------------------------------------------------------------------------
  1682. void __fastcall TManagementScript::CloseProc(TScriptProcParams * Parameters)
  1683. {
  1684. CheckSession();
  1685. TTerminal * Terminal;
  1686. if (Parameters->ParamCount == 0)
  1687. {
  1688. Terminal = FTerminal;
  1689. }
  1690. else
  1691. {
  1692. Terminal = FindSession(Parameters->Param[1]);
  1693. }
  1694. DoClose(Terminal);
  1695. }
  1696. //---------------------------------------------------------------------------
  1697. void __fastcall TManagementScript::SessionProc(TScriptProcParams * Parameters)
  1698. {
  1699. CheckSession();
  1700. if (Parameters->ParamCount == 0)
  1701. {
  1702. for (int i = 0; i < FTerminalList->Count; i++)
  1703. {
  1704. PrintLine(FORMAT("%3d %s",
  1705. (i + 1, FTerminalList->Terminals[i]->SessionData->SessionName)));
  1706. }
  1707. PrintActiveSession();
  1708. }
  1709. else
  1710. {
  1711. FTerminal = FindSession(Parameters->Param[1]);
  1712. PrintActiveSession();
  1713. }
  1714. }
  1715. //---------------------------------------------------------------------------
  1716. void __fastcall TManagementScript::LPwdProc(TScriptProcParams * /*Parameters*/)
  1717. {
  1718. PrintLine(GetCurrentDir());
  1719. }
  1720. //---------------------------------------------------------------------------
  1721. void __fastcall TManagementScript::LCdProc(TScriptProcParams * Parameters)
  1722. {
  1723. assert(Parameters->ParamCount == 1);
  1724. DoChangeLocalDirectory(Parameters->Param[1]);
  1725. PrintLine(GetCurrentDir());
  1726. }
  1727. //---------------------------------------------------------------------------
  1728. void __fastcall TManagementScript::LLsProc(TScriptProcParams * Parameters)
  1729. {
  1730. AnsiString Directory;
  1731. AnsiString Mask;
  1732. if (Parameters->ParamCount > 0)
  1733. {
  1734. Directory = Parameters->Param[1];
  1735. Mask = ExtractFileName(Directory);
  1736. if (TFileMasks::IsMask(Mask))
  1737. {
  1738. Directory = ExtractFilePath(Directory);
  1739. }
  1740. else
  1741. {
  1742. Mask = "";
  1743. }
  1744. }
  1745. if (Directory.IsEmpty())
  1746. {
  1747. Directory = GetCurrentDir();
  1748. }
  1749. if (Mask.IsEmpty())
  1750. {
  1751. Mask = "*.*";
  1752. }
  1753. TSearchRec SearchRec;
  1754. int FindAttrs = faReadOnly | faHidden | faSysFile | faDirectory | faArchive;
  1755. if (FindFirst(IncludeTrailingBackslash(Directory) + Mask, FindAttrs, SearchRec) != 0)
  1756. {
  1757. throw Exception(FMTLOAD(LIST_DIR_ERROR, (Directory)));
  1758. }
  1759. try
  1760. {
  1761. AnsiString TimeFormat = FixedLenDateTimeFormat(ShortTimeFormat);
  1762. AnsiString DateFormat = FixedLenDateTimeFormat(ShortDateFormat);
  1763. int DateLen = 0;
  1764. int TimeLen = 0;
  1765. bool First = true;
  1766. do
  1767. {
  1768. if (SearchRec.Name != ".")
  1769. {
  1770. TDateTime DateTime = FileDateToDateTime(SearchRec.Time);
  1771. AnsiString TimeStr = FormatDateTime(TimeFormat, DateTime);
  1772. AnsiString DateStr = FormatDateTime(DateFormat, DateTime);
  1773. if (First)
  1774. {
  1775. if (TimeLen < TimeStr.Length())
  1776. {
  1777. TimeLen = TimeStr.Length();
  1778. }
  1779. if (DateLen < DateStr.Length())
  1780. {
  1781. DateLen = DateStr.Length();
  1782. }
  1783. First = false;
  1784. }
  1785. AnsiString SizeStr;
  1786. if (FLAGSET(SearchRec.Attr, faDirectory))
  1787. {
  1788. SizeStr = "<DIR>";
  1789. }
  1790. else
  1791. {
  1792. SizeStr = FORMAT("%14.0n", (double(SearchRec.Size)));
  1793. }
  1794. PrintLine(FORMAT("%-*s %-*s %-14s %s", (
  1795. DateLen, DateStr, TimeLen, TimeStr, SizeStr, SearchRec.Name)));
  1796. }
  1797. }
  1798. while (FindNext(SearchRec) == 0);
  1799. }
  1800. __finally
  1801. {
  1802. FindClose(SearchRec);
  1803. }
  1804. }
  1805. //---------------------------------------------------------------------------