Script.cpp 58 KB

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