Script.cpp 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028
  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 (SameText(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_HELP3, &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. (ProgressData.Operation == foMove))
  1429. {
  1430. if (ProgressData.InProgress && ProgressData.FileInProgress &&
  1431. !ProgressData.FileName.IsEmpty())
  1432. {
  1433. bool DoPrint = false;
  1434. bool First = false;
  1435. AnsiString ProgressFileName = ProgressData.FileName;
  1436. if (ProgressData.Side == osLocal)
  1437. {
  1438. ProgressFileName = ExcludeTrailingBackslash(ProgressFileName);
  1439. }
  1440. else
  1441. {
  1442. ProgressFileName = UnixExcludeTrailingBackslash(ProgressFileName);
  1443. }
  1444. if (ProgressFileName != FLastProgressFile)
  1445. {
  1446. First = true;
  1447. DoPrint = true;
  1448. ShowPendingProgress();
  1449. }
  1450. if (!DoPrint && ((FLastProgressTime != time(NULL)) || ProgressData.IsTransferDone()))
  1451. {
  1452. DoPrint = true;
  1453. }
  1454. if (DoPrint)
  1455. {
  1456. static int WidthFileName = 25;
  1457. AnsiString FileName;
  1458. if (FLimitedOutput)
  1459. {
  1460. FileName = MinimizeName(ProgressFileName, WidthFileName,
  1461. ProgressData.Side == osRemote);
  1462. }
  1463. else
  1464. {
  1465. FileName = ProgressFileName;
  1466. }
  1467. AnsiString ProgressMessage = FORMAT("%-*s | %10d KiB | %6.1f KiB/s | %-6.6s | %3d%%",
  1468. (WidthFileName, FileName,
  1469. static_cast<int>(ProgressData.TransferedSize / 1024),
  1470. static_cast<float>(ProgressData.CPS()) / 1024,
  1471. ProgressData.AsciiTransfer ? "ascii" : "binary",
  1472. ProgressData.TransferProgress()));
  1473. if (FLastProgressMessage != ProgressMessage)
  1474. {
  1475. FLastProgressTime = time(NULL);
  1476. PrintProgress(First, ProgressMessage);
  1477. FLastProgressMessage = ProgressMessage;
  1478. FLastProgressFile = ProgressFileName;
  1479. }
  1480. }
  1481. }
  1482. else
  1483. {
  1484. FLastProgressFile = "";
  1485. }
  1486. }
  1487. if (QueryCancel())
  1488. {
  1489. Cancel = csCancel;
  1490. }
  1491. }
  1492. //---------------------------------------------------------------------------
  1493. void __fastcall TManagementScript::TerminalOperationFinished(
  1494. TFileOperation Operation, TOperationSide /*Side*/,
  1495. bool /*Temp*/, const AnsiString & FileName, Boolean Success,
  1496. TOnceDoneOperation & /*OnceDoneOperation*/)
  1497. {
  1498. assert(Operation != foCalculateSize);
  1499. if (Success && (Operation != foCalculateSize) && (Operation != foCopy) && (Operation != foMove))
  1500. {
  1501. ShowPendingProgress();
  1502. // For FKeepingUpToDate we should send events to synchronize controller eventually.
  1503. if (Synchronizing() && (Operation == foDelete))
  1504. {
  1505. // Note that this is duplicated with "keep up to date" log.
  1506. PrintLine(FMTLOAD(SCRIPT_SYNCHRONIZE_DELETED, (FileName)));
  1507. }
  1508. else
  1509. {
  1510. PrintLine(FileName);
  1511. }
  1512. }
  1513. }
  1514. //---------------------------------------------------------------------------
  1515. void __fastcall TManagementScript::TerminalSynchronizeDirectory(
  1516. const AnsiString LocalDirectory, const AnsiString RemoteDirectory,
  1517. bool & Continue, bool Collect)
  1518. {
  1519. int SynchronizeMode = FSynchronizeMode;
  1520. if (FKeepingUpToDate)
  1521. {
  1522. SynchronizeMode = TTerminal::smRemote;
  1523. }
  1524. AnsiString Arrow;
  1525. switch (SynchronizeMode)
  1526. {
  1527. case TTerminal::smRemote:
  1528. Arrow = "=>";
  1529. break;
  1530. case TTerminal::smLocal:
  1531. Arrow = "<=";
  1532. break;
  1533. case TTerminal::smBoth:
  1534. Arrow = "<=>";
  1535. break;
  1536. }
  1537. AnsiString Progress = FMTLOAD(SCRIPT_SYNCHRONIZE, (ExcludeTrailingBackslash(LocalDirectory),
  1538. Arrow, UnixExcludeTrailingBackslash(RemoteDirectory)));
  1539. if (Collect)
  1540. {
  1541. PrintProgress(false, Progress);
  1542. }
  1543. else
  1544. {
  1545. FSynchronizeIntro = Progress;
  1546. }
  1547. if (QueryCancel())
  1548. {
  1549. Continue = false;
  1550. }
  1551. }
  1552. //---------------------------------------------------------------------------
  1553. TTerminal * __fastcall TManagementScript::FindSession(const AnsiString Index)
  1554. {
  1555. int i = StrToIntDef(Index, -1);
  1556. if ((i <= 0) || (i > FTerminalList->Count))
  1557. {
  1558. throw Exception(FMTLOAD(SCRIPT_SESSION_INDEX_INVALID, (Index)));
  1559. }
  1560. else
  1561. {
  1562. return FTerminalList->Terminals[i - 1];
  1563. }
  1564. }
  1565. //---------------------------------------------------------------------------
  1566. void __fastcall TManagementScript::PrintActiveSession()
  1567. {
  1568. assert(FTerminal != NULL);
  1569. PrintLine(FMTLOAD(SCRIPT_ACTIVE_SESSION,
  1570. (FTerminalList->IndexOf(FTerminal) + 1, FTerminal->SessionData->SessionName)));
  1571. }
  1572. //---------------------------------------------------------------------------
  1573. bool __fastcall TManagementScript::HandleExtendedException(Exception * E,
  1574. TTerminal * Terminal)
  1575. {
  1576. bool Result = TScript::HandleExtendedException(E);
  1577. if (Terminal == NULL)
  1578. {
  1579. Terminal = FTerminal;
  1580. }
  1581. if ((Terminal != NULL) && (Terminal == FTerminal) && (dynamic_cast<EFatal*>(E) != NULL))
  1582. {
  1583. try
  1584. {
  1585. DoClose(Terminal);
  1586. }
  1587. catch(...)
  1588. {
  1589. // ignore disconnect errors
  1590. }
  1591. }
  1592. return Result;
  1593. }
  1594. //---------------------------------------------------------------------------
  1595. void __fastcall TManagementScript::Connect(const AnsiString Session,
  1596. TOptions * Options, bool CheckParams)
  1597. {
  1598. try
  1599. {
  1600. bool DefaultsOnly;
  1601. TSessionData * Data = FStoredSessions->ParseUrl(Session, Options, DefaultsOnly);
  1602. try
  1603. {
  1604. if (CheckParams)
  1605. {
  1606. TScriptCommands::CheckParams(Options, false);
  1607. }
  1608. assert(Data != NULL);
  1609. if (!Data->CanLogin || DefaultsOnly)
  1610. {
  1611. if (Data->HostName.IsEmpty())
  1612. {
  1613. AnsiString Value;
  1614. Input(LoadStr(SCRIPT_HOST_PROMPT), Value, false);
  1615. Data->HostName = Value;
  1616. }
  1617. assert(Data->CanLogin);
  1618. }
  1619. TTerminal * Terminal = FTerminalList->NewTerminal(Data);
  1620. try
  1621. {
  1622. Terminal->AutoReadDirectory = false;
  1623. Terminal->OnInformation = TerminalInformation;
  1624. Terminal->OnPromptUser = TerminalPromptUser;
  1625. Terminal->OnShowExtendedException = OnShowExtendedException;
  1626. Terminal->OnQueryUser = OnTerminalQueryUser;
  1627. Terminal->OnProgress = TerminalOperationProgress;
  1628. Terminal->OnFinished = TerminalOperationFinished;
  1629. ConnectTerminal(Terminal);
  1630. }
  1631. catch(Exception & E)
  1632. {
  1633. // make sure errors (mainly fatal ones) are associated
  1634. // with this terminal, not the last active one
  1635. bool Handled = HandleExtendedException(&E, Terminal);
  1636. FTerminalList->FreeTerminal(Terminal);
  1637. Terminal = NULL;
  1638. if (!Handled)
  1639. {
  1640. throw;
  1641. }
  1642. }
  1643. if (Terminal != NULL)
  1644. {
  1645. FTerminal = Terminal;
  1646. if (!Data->LocalDirectory.IsEmpty())
  1647. {
  1648. try
  1649. {
  1650. DoChangeLocalDirectory(ExpandFileName(Data->LocalDirectory));
  1651. }
  1652. catch(Exception & E)
  1653. {
  1654. if (!HandleExtendedException(&E))
  1655. {
  1656. throw;
  1657. }
  1658. }
  1659. }
  1660. PrintActiveSession();
  1661. }
  1662. }
  1663. __finally
  1664. {
  1665. delete Data;
  1666. }
  1667. }
  1668. catch(Exception & E)
  1669. {
  1670. if (!HandleExtendedException(&E))
  1671. {
  1672. throw;
  1673. }
  1674. }
  1675. }
  1676. //---------------------------------------------------------------------------
  1677. void __fastcall TManagementScript::DoClose(TTerminal * Terminal)
  1678. {
  1679. int Index = FTerminalList->IndexOf(Terminal);
  1680. assert(Index >= 0);
  1681. try
  1682. {
  1683. if (Terminal->Active)
  1684. {
  1685. Terminal->Close();
  1686. }
  1687. AnsiString SessionName = Terminal->SessionData->SessionName;
  1688. FreeTerminal(Terminal);
  1689. PrintLine(FMTLOAD(SCRIPT_SESSION_CLOSED, (SessionName)));
  1690. }
  1691. __finally
  1692. {
  1693. if (FTerminalList->Count > 0)
  1694. {
  1695. if (Index < FTerminalList->Count)
  1696. {
  1697. FTerminal = FTerminalList->Terminals[Index];
  1698. }
  1699. else
  1700. {
  1701. FTerminal = FTerminalList->Terminals[0];
  1702. }
  1703. PrintActiveSession();
  1704. }
  1705. else
  1706. {
  1707. FTerminal = NULL;
  1708. PrintLine(LoadStr(SCRIPT_NO_SESSION));
  1709. }
  1710. }
  1711. }
  1712. //---------------------------------------------------------------------------
  1713. void __fastcall TManagementScript::DoChangeLocalDirectory(AnsiString Directory)
  1714. {
  1715. if (!SetCurrentDir(Directory))
  1716. {
  1717. throw Exception(FMTLOAD(CHANGE_DIR_ERROR, (Directory)));
  1718. }
  1719. }
  1720. //---------------------------------------------------------------------------
  1721. void __fastcall TManagementScript::ExitProc(TScriptProcParams * /*Parameters*/)
  1722. {
  1723. FContinue = false;
  1724. }
  1725. //---------------------------------------------------------------------------
  1726. void __fastcall TManagementScript::OpenProc(TScriptProcParams * Parameters)
  1727. {
  1728. Connect(Parameters->ParamCount > 0 ? Parameters->Param[1] : AnsiString(),
  1729. Parameters, true);
  1730. }
  1731. //---------------------------------------------------------------------------
  1732. void __fastcall TManagementScript::CloseProc(TScriptProcParams * Parameters)
  1733. {
  1734. CheckSession();
  1735. TTerminal * Terminal;
  1736. if (Parameters->ParamCount == 0)
  1737. {
  1738. Terminal = FTerminal;
  1739. }
  1740. else
  1741. {
  1742. Terminal = FindSession(Parameters->Param[1]);
  1743. }
  1744. DoClose(Terminal);
  1745. }
  1746. //---------------------------------------------------------------------------
  1747. void __fastcall TManagementScript::SessionProc(TScriptProcParams * Parameters)
  1748. {
  1749. CheckSession();
  1750. if (Parameters->ParamCount == 0)
  1751. {
  1752. for (int i = 0; i < FTerminalList->Count; i++)
  1753. {
  1754. PrintLine(FORMAT("%3d %s",
  1755. (i + 1, FTerminalList->Terminals[i]->SessionData->SessionName)));
  1756. }
  1757. PrintActiveSession();
  1758. }
  1759. else
  1760. {
  1761. FTerminal = FindSession(Parameters->Param[1]);
  1762. PrintActiveSession();
  1763. }
  1764. }
  1765. //---------------------------------------------------------------------------
  1766. void __fastcall TManagementScript::LPwdProc(TScriptProcParams * /*Parameters*/)
  1767. {
  1768. PrintLine(GetCurrentDir());
  1769. }
  1770. //---------------------------------------------------------------------------
  1771. void __fastcall TManagementScript::LCdProc(TScriptProcParams * Parameters)
  1772. {
  1773. assert(Parameters->ParamCount == 1);
  1774. DoChangeLocalDirectory(Parameters->Param[1]);
  1775. PrintLine(GetCurrentDir());
  1776. }
  1777. //---------------------------------------------------------------------------
  1778. void __fastcall TManagementScript::LLsProc(TScriptProcParams * Parameters)
  1779. {
  1780. AnsiString Directory;
  1781. AnsiString Mask;
  1782. if (Parameters->ParamCount > 0)
  1783. {
  1784. Directory = Parameters->Param[1];
  1785. Mask = ExtractFileName(Directory);
  1786. if (TFileMasks::IsMask(Mask))
  1787. {
  1788. Directory = ExtractFilePath(Directory);
  1789. }
  1790. else
  1791. {
  1792. Mask = "";
  1793. }
  1794. }
  1795. if (Directory.IsEmpty())
  1796. {
  1797. Directory = GetCurrentDir();
  1798. }
  1799. if (Mask.IsEmpty())
  1800. {
  1801. Mask = "*.*";
  1802. }
  1803. TSearchRec SearchRec;
  1804. int FindAttrs = faReadOnly | faHidden | faSysFile | faDirectory | faArchive;
  1805. if (FindFirst(IncludeTrailingBackslash(Directory) + Mask, FindAttrs, SearchRec) != 0)
  1806. {
  1807. throw Exception(FMTLOAD(LIST_DIR_ERROR, (Directory)));
  1808. }
  1809. try
  1810. {
  1811. AnsiString TimeFormat = FixedLenDateTimeFormat(ShortTimeFormat);
  1812. AnsiString DateFormat = FixedLenDateTimeFormat(ShortDateFormat);
  1813. int DateLen = 0;
  1814. int TimeLen = 0;
  1815. bool First = true;
  1816. do
  1817. {
  1818. if (SearchRec.Name != ".")
  1819. {
  1820. TDateTime DateTime = FileDateToDateTime(SearchRec.Time);
  1821. AnsiString TimeStr = FormatDateTime(TimeFormat, DateTime);
  1822. AnsiString DateStr = FormatDateTime(DateFormat, DateTime);
  1823. if (First)
  1824. {
  1825. if (TimeLen < TimeStr.Length())
  1826. {
  1827. TimeLen = TimeStr.Length();
  1828. }
  1829. if (DateLen < DateStr.Length())
  1830. {
  1831. DateLen = DateStr.Length();
  1832. }
  1833. First = false;
  1834. }
  1835. AnsiString SizeStr;
  1836. if (FLAGSET(SearchRec.Attr, faDirectory))
  1837. {
  1838. SizeStr = "<DIR>";
  1839. }
  1840. else
  1841. {
  1842. SizeStr = FORMAT("%14.0n", (double(SearchRec.Size)));
  1843. }
  1844. PrintLine(FORMAT("%-*s %-*s %-14s %s", (
  1845. DateLen, DateStr, TimeLen, TimeStr, SizeStr, SearchRec.Name)));
  1846. }
  1847. }
  1848. while (FindNext(SearchRec) == 0);
  1849. }
  1850. __finally
  1851. {
  1852. FindClose(SearchRec);
  1853. }
  1854. }
  1855. //---------------------------------------------------------------------------