Script.cpp 57 KB

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