| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867 |
- //---------------------------------------------------------------------------
- #include <vcl.h>
- #pragma hdrstop
- #include <SysUtils.hpp>
- #include "Common.h"
- #include "TextsCore.h"
- #include "Script.h"
- #include "Terminal.h"
- #include "SessionData.h"
- #include "ScpMain.h"
- //---------------------------------------------------------------------------
- #pragma package(smart_init)
- //---------------------------------------------------------------------------
- __fastcall TScriptProcParams::TScriptProcParams(TStrings * Params,
- const AnsiString & ParamsStr)
- {
- FParams = Params;
- FParamsStr = ParamsStr;
- FSkipParams = 0;
- FArg = 0;
- }
- //---------------------------------------------------------------------------
- void __fastcall TScriptProcParams::SkipParam()
- {
- assert(FSkipParams < FParams->Count);
- FSkipParams++;
- }
- //---------------------------------------------------------------------------
- int __fastcall TScriptProcParams::GetParamCount()
- {
- return (FSkipParams > FParams->Count ? 0 : FParams->Count - FSkipParams);
- }
- //---------------------------------------------------------------------------
- AnsiString __fastcall TScriptProcParams::GetParam(int Index)
- {
- return FParams->Strings[FSkipParams + Index];
- }
- //---------------------------------------------------------------------------
- //---------------------------------------------------------------------------
- class TScriptCommands : TStringList
- {
- public:
- typedef void __fastcall (__closure *TCommandProc)(TScriptProcParams * Parameters);
- __fastcall TScriptCommands();
- void __fastcall Execute(TScriptProcParams * Parameters);
- void __fastcall Execute(TStrings * Tokens, AnsiString Params);
- void __fastcall Register(const char * Command,
- const AnsiString Description, const AnsiString Help, TCommandProc Proc,
- int MinParams, int MaxParams, void * Arg = NULL);
- void __fastcall Register(const char * Command,
- int Description, int Help, TCommandProc Proc,
- int MinParams, int MaxParams, void * Arg = NULL);
- void __fastcall Register(const char * Command, TCommandProc Proc,
- int MinParams, int MaxParams, void * Arg = NULL);
- bool __fastcall Info(const AnsiString Command,
- AnsiString * Description, AnsiString * Help);
- bool __fastcall Enumerate(int Index,
- AnsiString * Command, AnsiString * Description, AnsiString * Help);
- static int __fastcall FindCommand(TStrings * Commands, const AnsiString Command,
- AnsiString * Matches = NULL);
- static int __fastcall FindCommand(const char ** Commands, size_t Count,
- const AnsiString Command, AnsiString * Matches = NULL);
- __property TCommandProc DefaultProc = { read = FDefaultProc, write = FDefaultProc };
- protected:
- TCommandProc FDefaultProc;
- struct TScriptCommand
- {
- AnsiString Description;
- AnsiString Help;
- TCommandProc Proc;
- int MinParams;
- int MaxParams;
- void * Arg;
- };
- };
- //---------------------------------------------------------------------------
- __fastcall TScriptCommands::TScriptCommands()
- {
- Sorted = true;
- CaseSensitive = false;
- FDefaultProc = NULL;
- }
- //---------------------------------------------------------------------------
- void __fastcall TScriptCommands::Register(const char * Command,
- const AnsiString Description, const AnsiString Help, TCommandProc Proc,
- int MinParams, int MaxParams, void * Arg)
- {
- TScriptCommand * ScriptCommand = new TScriptCommand;
- ScriptCommand->Description = Description;
- ScriptCommand->Help = Help;
- ScriptCommand->Proc = Proc;
- ScriptCommand->MinParams = MinParams;
- ScriptCommand->MaxParams = MaxParams;
- ScriptCommand->Arg = Arg;
- AddObject(Command, reinterpret_cast<TObject *>(ScriptCommand));
- }
- //---------------------------------------------------------------------------
- void __fastcall TScriptCommands::Register(const char * Command,
- int Description, int Help, TCommandProc Proc,
- int MinParams, int MaxParams, void * Arg)
- {
- Register(Command,
- (Description > 0 ? LoadStr(Description) : AnsiString()),
- (Help > 0 ? LoadStr(Help, 10240) : AnsiString()),
- Proc, MinParams, MaxParams, Arg);
- }
- //---------------------------------------------------------------------------
- void __fastcall TScriptCommands::Register(const char * Command, TCommandProc Proc,
- int MinParams, int MaxParams, void * Arg)
- {
- Register(Command, "", "", Proc, MinParams, MaxParams, Arg);
- }
- //---------------------------------------------------------------------------
- bool __fastcall TScriptCommands::Info(const AnsiString Command,
- AnsiString * Description, AnsiString * Help)
- {
- int Index = FindCommand(this, Command);
- bool Result = (Index >= 0);
- if (Result)
- {
- TScriptCommand * ScriptCommand = reinterpret_cast<TScriptCommand *>(Objects[Index]);
- if (Description != NULL)
- {
- *Description = ScriptCommand->Description;
- }
- if (Help != NULL)
- {
- *Help = ScriptCommand->Help;
- }
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- bool __fastcall TScriptCommands::Enumerate(int Index,
- AnsiString * Command, AnsiString * Description, AnsiString * Help)
- {
- bool Result = (Index < Count);
- if (Result)
- {
- TScriptCommand * ScriptCommand = reinterpret_cast<TScriptCommand *>(Objects[Index]);
- if (Command != NULL)
- {
- *Command = Strings[Index];
- }
- if (Description != NULL)
- {
- *Description = ScriptCommand->Description;
- }
- if (Help != NULL)
- {
- *Help = ScriptCommand->Help;
- }
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- int __fastcall TScriptCommands::FindCommand(TStrings * Commands,
- const AnsiString Command, AnsiString * Matches)
- {
- int Result = Commands->IndexOf(Command);
- if (Result < 0)
- {
- int MatchesCount = 0;
- for (int i = 0; i < Commands->Count; i++)
- {
- if ((Command.Length() <= Commands->Strings[i].Length()) &&
- SameText(Command, Commands->Strings[i].SubString(1, Command.Length())))
- {
- if (Matches != NULL)
- {
- if (!Matches->IsEmpty())
- {
- *Matches += ", ";
- }
- *Matches += Commands->Strings[i];
- }
- MatchesCount++;
- Result = i;
- }
- }
- if (MatchesCount == 0)
- {
- Result = -1;
- }
- else if (MatchesCount > 1)
- {
- Result = -2;
- }
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- int __fastcall TScriptCommands::FindCommand(const char ** Commands, size_t Count,
- const AnsiString Command, AnsiString * Matches)
- {
- int Result;
- TStringList * Strings = new TStringList;
- try
- {
- Strings->CaseSensitive = false;
- for (unsigned int i = 0; i < Count; i++)
- {
- Strings->Add(Commands[i]);
- }
- Result = FindCommand(Strings, Command, Matches);
- }
- __finally
- {
- delete Strings;
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- void __fastcall TScriptCommands::Execute(TScriptProcParams * Parameters)
- {
- assert(Parameters->ParamCount > 0);
- AnsiString Command = Parameters->Param[0];
- AnsiString Matches;
- int Index = FindCommand(this, Command, &Matches);
- if (Index == -2)
- {
- throw Exception(FMTLOAD(SCRIPT_COMMAND_AMBIGUOUS, (Command, Matches)));
- }
- else if (Index < 0)
- {
- throw Exception(FMTLOAD(SCRIPT_COMMAND_UNKNOWN, (Command)));
- }
- TScriptCommand * ScriptCommand = reinterpret_cast<TScriptCommand *>(Objects[Index]);
- Command = Strings[Index];
- Parameters->SkipParam();
- Parameters->Arg = ScriptCommand->Arg;
- if (Parameters->ParamCount < ScriptCommand->MinParams)
- {
- throw Exception(FMTLOAD(SCRIPT_MISSING_PARAMS, (Command)));
- }
- else if ((ScriptCommand->MaxParams >= 0) && (Parameters->ParamCount > ScriptCommand->MaxParams))
- {
- throw Exception(FMTLOAD(SCRIPT_TOO_MANY_PARAMS, (Command)));
- }
- else
- {
- ScriptCommand->Proc(Parameters);
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TScriptCommands::Execute(TStrings * Tokens, AnsiString Params)
- {
- TScriptProcParams * Parameters = new TScriptProcParams(Tokens, Params);
- try
- {
- Execute(Parameters);
- }
- __finally
- {
- delete Parameters;
- }
- }
- //---------------------------------------------------------------------------
- //---------------------------------------------------------------------------
- __fastcall TScript::TScript()
- {
- FTerminal = NULL;
- Init();
- }
- //---------------------------------------------------------------------------
- __fastcall TScript::~TScript()
- {
- delete FCommands;
- }
- //---------------------------------------------------------------------------
- __fastcall TScript::TScript(TTerminal * Terminal)
- {
- FTerminal = Terminal;
- Init();
- }
- //---------------------------------------------------------------------------
- void __fastcall TScript::Init()
- {
- FBatch = BatchOff;
- FConfirm = true;
- FSynchronizeParams = 0;
- FOnPrint = NULL;
- FOnTerminalSynchronizeDirectory = NULL;
- FOnSynchronizeStartStop = NULL;
- FSynchronizeMode = -1;
- FKeepingUpToDate = false;
- FCommands = new TScriptCommands;
- FCommands->Register(";", 0, 0, &DummyProc, 0, -1);
- FCommands->Register("#", 0, 0, &DummyProc, 0, -1);
- FCommands->Register("help", SCRIPT_HELP_DESC, SCRIPT_HELP_HELP, &HelpProc, 0, -1);
- FCommands->Register("man", 0, SCRIPT_HELP_HELP, &HelpProc, 0, -1);
- FCommands->Register("call", SCRIPT_CALL_DESC, SCRIPT_CALL_HELP, &CallProc, 1, -1);
- FCommands->Register("!", 0, SCRIPT_CALL_HELP, &CallProc, 1, -1);
- FCommands->Register("pwd", SCRIPT_PWD_DESC, SCRIPT_PWD_HELP, &PwdProc, 0, 0);
- FCommands->Register("cd", SCRIPT_CD_DESC, SCRIPT_CD_HELP, &CdProc, 0, 1);
- FCommands->Register("ls", SCRIPT_LS_DESC, SCRIPT_LS_HELP, &LsProc, 0, 1);
- FCommands->Register("dir", 0, SCRIPT_LS_HELP, &LsProc, 0, 1);
- FCommands->Register("rm", SCRIPT_RM_DESC, SCRIPT_RM_HELP, &RmProc, 1, -1);
- FCommands->Register("rmdir", SCRIPT_RMDIR_DESC, SCRIPT_RMDIR_HELP, &RmDirProc, 1, -1);
- FCommands->Register("mv", SCRIPT_MV_DESC, SCRIPT_MV_HELP, &MvProc, 2, -1);
- FCommands->Register("rename", 0, SCRIPT_MV_HELP, &MvProc, 2, -1);
- FCommands->Register("chmod", SCRIPT_CHMOD_DESC, SCRIPT_CHMOD_HELP, &ChModProc, 2, -1);
- FCommands->Register("ln", SCRIPT_LN_DESC, SCRIPT_LN_HELP, &LnProc, 2, 2);
- FCommands->Register("symlink", 0, SCRIPT_LN_HELP, &LnProc, 2, 2);
- FCommands->Register("mkdir", SCRIPT_MKDIR_DESC, SCRIPT_MKDIR_HELP, &MkDirProc, 1, 1);
- FCommands->Register("get", SCRIPT_GET_DESC, SCRIPT_GET_HELP, &GetProc, 1, -1);
- FCommands->Register("recv", 0, SCRIPT_GET_HELP, &GetProc, 1, -1);
- FCommands->Register("put", SCRIPT_PUT_DESC, SCRIPT_PUT_HELP, &PutProc, 1, -1);
- FCommands->Register("send", 0, SCRIPT_PUT_HELP, &PutProc, 1, -1);
- FCommands->Register("option", SCRIPT_OPTION_DESC, SCRIPT_OPTION_HELP2, &OptionProc, -1, 2);
- FCommands->Register("ascii", 0, SCRIPT_OPTION_HELP2, &AsciiProc, 0, 0);
- FCommands->Register("binary", 0, SCRIPT_OPTION_HELP2, &BinaryProc, 0, 0);
- FCommands->Register("synchronize", SCRIPT_SYNCHRONIZE_DESC, SCRIPT_SYNCHRONIZE_HELP, &SynchronizeProc, 1, 3);
- FCommands->Register("keepuptodate", SCRIPT_KEEPUPTODATE_DESC, SCRIPT_KEEPUPTODATE_HELP, &KeepUpToDateProc, 0, 2);
- }
- //---------------------------------------------------------------------------
- void __fastcall TScript::SetCopyParam(const TCopyParamType & value)
- {
- FCopyParam.Assign(&value);
- }
- //---------------------------------------------------------------------------
- void __fastcall TScript::SetSynchronizeParams(int value)
- {
- FSynchronizeParams = (value &
- (TTerminal::spDelete | TTerminal::spExistingOnly | TTerminal::spTimestamp |
- TTerminal::spNotByTime | TTerminal::spBySize));
- }
- //---------------------------------------------------------------------------
- void __fastcall TScript::Command(const AnsiString Cmd)
- {
- try
- {
- TStrings * Tokens = new TStringList();
- try
- {
- AnsiString AllButFirst;
- Tokenize(Cmd, Tokens, AllButFirst);
- if (Tokens->Count > 0)
- {
- FCommands->Execute(Tokens, AllButFirst);
- }
- }
- __finally
- {
- delete Tokens;
- }
- }
- catch(Exception & E)
- {
- if (!HandleExtendedException(&E))
- {
- throw;
- }
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TScript::Tokenize(const AnsiString Str, TStrings * Tokens,
- AnsiString & AllButFirst)
- {
- assert(Tokens->Count == 0);
- // inspired by Putty's sftp_getcmd() from PSFTP.C
- int Index = 1;
- while (Index <= Str.Length())
- {
- while ((Index <= Str.Length()) &&
- ((Str[Index] == ' ') || (Str[Index] == '\t')))
- {
- Index++;
- }
- if (Tokens->Count == 1)
- {
- AllButFirst = Str.SubString(Index, Str.Length() - Index + 1);
- }
- if (Index <= Str.Length())
- {
- bool Quoting = false;
- AnsiString Token;
- while (Index <= Str.Length())
- {
- if (!Quoting && ((Str[Index] == ' ') || (Str[Index] == '\t')))
- {
- break;
- }
- else if ((Str[Index] == '"') && (Index + 1 <= Str.Length()) &&
- (Str[Index + 1] == '"'))
- {
- Index += 2;
- Token += '"';
- }
- else if (Str[Index] == '"')
- {
- Index++;
- Quoting = !Quoting;
- }
- else
- {
- Token += Str[Index];
- Index++;
- }
- }
- if (Index <= Str.Length())
- {
- Index++;
- }
- Tokens->Add(Token);
- }
- }
- }
- //---------------------------------------------------------------------------
- TStrings * __fastcall TScript::CreateFileList(TScriptProcParams * Parameters, int Start,
- int End, TFileListType ListType)
- {
- TStrings * Result = new TStringList();
- TStrings * FileLists = NULL;
- try
- {
- try
- {
- for (int i = Start; i <= End; i++)
- {
- AnsiString FileName = Parameters->Param[i];
- if (FLAGSET(ListType, fltDirectories))
- {
- TRemoteFile * File = new TRemoteFile();
- File->FileName = FileName;
- File->Type = FILETYPE_DIRECTORY;
- Result->AddObject(FileName, File);
- }
- else if (FLAGSET(ListType, fltMask) && TFileMasks::IsMask(FileName))
- {
- AnsiString FileDirectory = UnixExtractFilePath(FileName);
- AnsiString Directory = FileDirectory;
- if (Directory.IsEmpty())
- {
- Directory = UnixIncludeTrailingBackslash(FTerminal->CurrentDirectory);
- }
- TRemoteFileList * FileList = NULL;
- if (FileLists != NULL)
- {
- int Index = FileLists->IndexOf(Directory);
- if (Index > 0)
- {
- FileList = dynamic_cast<TRemoteFileList *>(FileLists->Objects[Index]);
- }
- }
- if (FileList == NULL)
- {
- FileList = FTerminal->ReadDirectoryListing(Directory, false);
- if (FileLists == NULL)
- {
- FileLists = new TStringList();
- }
- FileLists->AddObject(Directory, FileList);
- }
- AnsiString Mask = UnixExtractFileName(FileName);
- for (int i = 0; i < FileList->Count; i++)
- {
- TRemoteFile * File = FileList->Files[i];
- if (!File->IsThisDirectory && !File->IsParentDirectory &&
- TFileMasks::SingleMaskMatch(Mask, File->FileName))
- {
- Result->AddObject(FileDirectory + File->FileName,
- FLAGSET(ListType, fltQueryServer) ? File->Duplicate() : NULL);
- }
- }
- }
- else
- {
- TRemoteFile * File = NULL;
- if (FLAGSET(ListType, fltQueryServer))
- {
- FTerminal->ExceptionOnFail = true;
- try
- {
- FTerminal->ReadFile(FileName, File);
- if (!File->HaveFullFileName)
- {
- File->FullFileName = FileName;
- }
- }
- __finally
- {
- FTerminal->ExceptionOnFail = false;
- }
- }
- Result->AddObject(FileName, File);
- }
- }
- }
- catch(...)
- {
- FreeFileList(Result);
- throw;
- }
- }
- __finally
- {
- if (FileLists != NULL)
- {
- for (int i = 0; i < FileLists->Count; i++)
- {
- delete FileLists->Objects[i];
- }
- delete FileLists;
- }
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- TStrings * __fastcall TScript::CreateLocalFileList(TScriptProcParams * Parameters,
- int Start, int End, TFileListType ListType)
- {
- TStrings * Result = new TStringList();
- try
- {
- for (int i = Start; i <= End; i++)
- {
- AnsiString FileName = Parameters->Param[i];
- if (FLAGSET(ListType, fltMask))
- {
- TSearchRec SearchRec;
- int FindAttrs = faReadOnly | faHidden | faSysFile | faDirectory | faArchive;
- if (FindFirst(FileName, FindAttrs, SearchRec) == 0)
- {
- AnsiString Directory = ExtractFilePath(FileName);
- try
- {
- do
- {
- if ((SearchRec.Name != ".") && (SearchRec.Name != ".."))
- {
- Result->Add(Directory + SearchRec.Name);
- }
- }
- while (FindNext(SearchRec) == 0);
- }
- __finally
- {
- FindClose(SearchRec);
- }
- }
- else
- {
- // not match, let it fail latter however
- Result->Add(FileName);
- }
- }
- else
- {
- Result->Add(FileName);
- }
- }
- }
- catch(...)
- {
- delete Result;
- throw;
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- void __fastcall TScript::FreeFileList(TStrings * FileList)
- {
- for (int i = 0; i < FileList->Count; i++)
- {
- if (FileList->Objects[i] != NULL)
- {
- TRemoteFile * File = dynamic_cast<TRemoteFile *>(FileList->Objects[i]);
- delete File;
- }
- }
- delete FileList;
- }
- //---------------------------------------------------------------------------
- void __fastcall TScript::ConnectTerminal(TTerminal * Terminal)
- {
- Terminal->Open();
- }
- //---------------------------------------------------------------------------
- void __fastcall TScript::Print(const AnsiString Str)
- {
- if (FOnPrint != NULL)
- {
- FOnPrint(this, Str);
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TScript::PrintLine(const AnsiString Str)
- {
- Print(Str + "\n");
- }
- //---------------------------------------------------------------------------
- bool __fastcall TScript::HandleExtendedException(Exception * E, TTerminal * Terminal)
- {
- bool Result = (OnShowExtendedException != NULL);
- if (Result)
- {
- if (Terminal == NULL)
- {
- Terminal = FTerminal;
- }
- OnShowExtendedException(Terminal, E, NULL);
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- void __fastcall TScript::CheckSession()
- {
- if (FTerminal == NULL)
- {
- throw Exception(LoadStr(SCRIPT_NO_SESSION));
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TScript::ResetTransfer()
- {
- }
- //---------------------------------------------------------------------------
- bool __fastcall TScript::EnsureCommandSessionFallback(TFSCapability Capability)
- {
- bool Result = FTerminal->IsCapable[Capability] ||
- FTerminal->CommandSessionOpened;
- if (!Result)
- {
- try
- {
- ConnectTerminal(FTerminal->CommandSession);
- Result = true;
- }
- catch(Exception & E)
- {
- HandleExtendedException(&E, FTerminal->CommandSession);
- Result = false;
- }
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- void __fastcall TScript::SecondaryProc(TScriptProcParams * Parameters)
- {
- TScriptCommands * Commands = static_cast<TScriptCommands *>(Parameters->Arg);
- assert(Commands != NULL);
- if (Parameters->ParamCount == 0)
- {
- assert(Commands->DefaultProc != NULL);
- Commands->DefaultProc(Parameters);
- }
- else
- {
- Commands->Execute(Parameters);
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TScript::DummyProc(TScriptProcParams * /*Parameters*/)
- {
- // noop
- }
- //---------------------------------------------------------------------------
- void __fastcall TScript::HelpProc(TScriptProcParams * Parameters)
- {
- AnsiString Output;
- if (Parameters->ParamCount == 0)
- {
- AnsiString Command;
- AnsiString Description;
- int Index = 0;
- while (FCommands->Enumerate(Index, &Command, &Description, NULL))
- {
- if (!Description.IsEmpty())
- {
- Output += FORMAT("%-8s %s\n", (Command, Description));
- }
- Index++;
- }
- }
- else
- {
- for (int i = 0; i < Parameters->ParamCount; i++)
- {
- AnsiString Help;
- if (FCommands->Info(Parameters->Param[i], NULL, &Help))
- {
- Output += Help;
- }
- else
- {
- throw Exception(FMTLOAD(SCRIPT_COMMAND_UNKNOWN, (Parameters->Param[i])));
- }
- }
- }
- Print(Output);
- }
- //---------------------------------------------------------------------------
- void __fastcall TScript::CallProc(TScriptProcParams * Parameters)
- {
- CheckSession();
- if (EnsureCommandSessionFallback(fcAnyCommand))
- {
- FTerminal->AnyCommand(Parameters->ParamsStr, TerminalCaptureLog);
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TScript::TerminalCaptureLog(TObject* /*Sender*/,
- TLogLineType Type, const AnsiString AddedLine)
- {
- USEDPARAM(Type);
- assert((Type == llOutput) || (Type == llStdError));
- PrintLine(AddedLine);
- }
- //---------------------------------------------------------------------------
- void __fastcall TScript::PwdProc(TScriptProcParams * /*Parameters*/)
- {
- CheckSession();
- PrintLine(FTerminal->CurrentDirectory);
- }
- //---------------------------------------------------------------------------
- void __fastcall TScript::CdProc(TScriptProcParams * Parameters)
- {
- CheckSession();
- if (Parameters->ParamCount == 0)
- {
- FTerminal->HomeDirectory();
- }
- else
- {
- FTerminal->ChangeDirectory(Parameters->Param[0]);
- }
- PrintLine(FTerminal->CurrentDirectory);
- }
- //---------------------------------------------------------------------------
- void __fastcall TScript::LsProc(TScriptProcParams * Parameters)
- {
- CheckSession();
- AnsiString Directory;
- AnsiString Mask;
- if (Parameters->ParamCount > 0)
- {
- Directory = Parameters->Param[0];
- Mask = UnixExtractFileName(Directory);
- if (TFileMasks::IsMask(Mask))
- {
- Directory = UnixExtractFilePath(Directory);
- }
- else
- {
- Mask = "";
- }
- }
- if (Directory.IsEmpty())
- {
- Directory = FTerminal->CurrentDirectory;
- }
- TRemoteFileList * FileList = FTerminal->ReadDirectoryListing(Directory, false);
- try
- {
- for (int i = 0; i < FileList->Count; i++)
- {
- TRemoteFile * File = FileList->Files[i];
- if (Mask.IsEmpty() || TFileMasks::SingleMaskMatch(Mask, File->FileName))
- {
- PrintLine(FileList->Files[i]->ListingStr);
- }
- }
- }
- __finally
- {
- delete FileList;
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TScript::RmProc(TScriptProcParams * Parameters)
- {
- CheckSession();
- TStrings * FileList = CreateFileList(Parameters, 0, Parameters->ParamCount - 1, fltMask);
- try
- {
- FTerminal->DeleteFiles(FileList);
- }
- __finally
- {
- FreeFileList(FileList);
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TScript::RmDirProc(TScriptProcParams * Parameters)
- {
- CheckSession();
- TStrings * FileList = CreateFileList(Parameters, 0, Parameters->ParamCount - 1, fltDirectories);
- try
- {
- FTerminal->DeleteFiles(FileList);
- }
- __finally
- {
- FreeFileList(FileList);
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TScript::MvProc(TScriptProcParams * Parameters)
- {
- CheckSession();
- TStrings * FileList = CreateFileList(Parameters, 0, Parameters->ParamCount - 2,
- fltMask);
- try
- {
- assert(Parameters->ParamCount >= 1);
- AnsiString Target = Parameters->Param[Parameters->ParamCount - 1];
- AnsiString TargetDirectory = UnixExtractFilePath(Target);
- AnsiString FileMask = UnixExtractFileName(Target);
- FTerminal->MoveFiles(FileList, TargetDirectory, FileMask);
- }
- __finally
- {
- FreeFileList(FileList);
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TScript::ChModProc(TScriptProcParams * Parameters)
- {
- CheckSession();
- TStrings * FileList = CreateFileList(Parameters, 1, Parameters->ParamCount - 1,
- fltMask);
- try
- {
- TRemoteProperties Properties;
- Properties.Valid = TValidProperties() << vpRights;
- Properties.Rights.Octal = Parameters->Param[0];
- FTerminal->ChangeFilesProperties(FileList, &Properties);
- }
- __finally
- {
- FreeFileList(FileList);
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TScript::LnProc(TScriptProcParams * Parameters)
- {
- CheckSession();
- assert(Parameters->ParamCount == 2);
- FTerminal->CreateLink(Parameters->Param[1], Parameters->Param[0], true);
- }
- //---------------------------------------------------------------------------
- void __fastcall TScript::MkDirProc(TScriptProcParams * Parameters)
- {
- CheckSession();
- FTerminal->CreateDirectory(Parameters->Param[0]);
- }
- //---------------------------------------------------------------------------
- void __fastcall TScript::GetProc(TScriptProcParams * Parameters)
- {
- CheckSession();
- ResetTransfer();
- int LastFileParam = (Parameters->ParamCount == 1 ? 0 : Parameters->ParamCount - 2);
- TStrings * FileList = CreateFileList(Parameters, 0, LastFileParam,
- fltQueryServer | fltMask);
- try
- {
- TCopyParamType CopyParam = FCopyParam;
- CopyParam.CalculateSize = false;
- AnsiString TargetDirectory;
- if (Parameters->ParamCount == 1)
- {
- TargetDirectory = GetCurrentDir();
- CopyParam.FileMask = "";
- }
- else
- {
- AnsiString Target = Parameters->Param[Parameters->ParamCount - 1];
- TargetDirectory = ExtractFilePath(Target);
- if (TargetDirectory.IsEmpty())
- {
- TargetDirectory = GetCurrentDir();
- }
- CopyParam.FileMask = ExtractFileName(Target);
- }
- int Params = FLAGMASK(!FConfirm, cpNoConfirmation);
- FTerminal->CopyToLocal(FileList, TargetDirectory, &CopyParam, Params);
- }
- __finally
- {
- FreeFileList(FileList);
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TScript::PutProc(TScriptProcParams * Parameters)
- {
- CheckSession();
- ResetTransfer();
- int LastFileParam = (Parameters->ParamCount == 1 ? 0 : Parameters->ParamCount - 2);
- TStrings * FileList = CreateLocalFileList(Parameters, 0, LastFileParam, fltMask);
- try
- {
- TCopyParamType CopyParam = FCopyParam;
- CopyParam.CalculateSize = false;
- AnsiString TargetDirectory;
- if (Parameters->ParamCount == 1)
- {
- TargetDirectory = FTerminal->CurrentDirectory;
- CopyParam.FileMask = "";
- }
- else
- {
- AnsiString Target = Parameters->Param[Parameters->ParamCount - 1];
- TargetDirectory = UnixExtractFilePath(Target);
- if (TargetDirectory.IsEmpty())
- {
- TargetDirectory = FTerminal->CurrentDirectory;
- }
- CopyParam.FileMask = UnixExtractFileName(Target);
- }
- int Params = FLAGMASK(!FConfirm, cpNoConfirmation);
- FTerminal->CopyToRemote(FileList, TargetDirectory, &CopyParam, Params);
- }
- __finally
- {
- FreeFileList(FileList);
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TScript::OptionImpl(AnsiString OptionName, AnsiString ValueName)
- {
- enum { Batch, Confirm, Transfer, SynchDelete, Exclude, Include };
- static const char * Names[] = { "batch", "confirm", "transfer", "synchdelete",
- "exclude", "include" };
- enum { Off, On };
- static const char * ToggleNames[] = { "off", "on" };
- assert((BatchOff == 0) && (BatchOn == 1) && (BatchAbort == 2));
- static const char * BatchModeNames[] = { "off", "on", "abort" };
- assert((tmBinary == 0) && (tmAscii == 1) && (tmAutomatic == 2));
- static const char * TransferModeNames[] = { "binary", "ascii", "automatic" };
- int Option = -1;
- if (!OptionName.IsEmpty())
- {
- Option = TScriptCommands::FindCommand(Names, LENOF(Names), OptionName);
- if (Option < 0)
- {
- throw Exception(FMTLOAD(SCRIPT_OPTION_UNKNOWN, (OptionName)));
- }
- else
- {
- OptionName = Names[Option];
- }
- }
- #define OPT(OPT) ((Option < 0) || (Option == OPT))
- const char * ListFormat = "%-12s %-10s";
- bool SetValue = !ValueName.IsEmpty();
- if (OPT(Batch))
- {
- if (SetValue)
- {
- int Value = TScriptCommands::FindCommand(BatchModeNames, LENOF(BatchModeNames), ValueName);
- if (Value < 0)
- {
- throw Exception(FMTLOAD(SCRIPT_VALUE_UNKNOWN, (ValueName, OptionName)));
- }
- FBatch = (TBatchMode)Value;
- }
- PrintLine(FORMAT(ListFormat, (Names[Batch], BatchModeNames[FBatch])));
- }
- if (OPT(Confirm))
- {
- if (SetValue)
- {
- int Value = TScriptCommands::FindCommand(ToggleNames, LENOF(ToggleNames), ValueName);
- if (Value < 0)
- {
- throw Exception(FMTLOAD(SCRIPT_VALUE_UNKNOWN, (ValueName, OptionName)));
- }
- FConfirm = (Value == On);
- }
- PrintLine(FORMAT(ListFormat, (Names[Confirm], ToggleNames[FConfirm ? On : Off])));
- }
- if (OPT(Transfer))
- {
- if (SetValue)
- {
- int Value = TScriptCommands::FindCommand(TransferModeNames,
- LENOF(TransferModeNames), ValueName);
- if (Value < 0)
- {
- throw Exception(FMTLOAD(SCRIPT_VALUE_UNKNOWN, (ValueName, OptionName)));
- }
- FCopyParam.TransferMode = (TTransferMode)Value;
- }
- assert(FCopyParam.TransferMode < LENOF(TransferModeNames));
- const char * Value = TransferModeNames[FCopyParam.TransferMode];
- PrintLine(FORMAT(ListFormat, (Names[Transfer], Value)));
- }
- if (OPT(SynchDelete))
- {
- if (SetValue)
- {
- int Value = TScriptCommands::FindCommand(ToggleNames, LENOF(ToggleNames), ValueName);
- if (Value < 0)
- {
- throw Exception(FMTLOAD(SCRIPT_VALUE_UNKNOWN, (ValueName, OptionName)));
- }
- FSynchronizeParams =
- (FSynchronizeParams & ~TTerminal::spDelete) |
- FLAGMASK(Value == On, TTerminal::spDelete);
- }
- PrintLine(FORMAT(ListFormat, (Names[SynchDelete],
- ToggleNames[FLAGSET(FSynchronizeParams, TTerminal::spDelete) ? On : Off])));
- }
- static const char * Clear = "clear";
- if (OPT(Include))
- {
- if (SetValue)
- {
- FCopyParam.NegativeExclude = true;
- FCopyParam.ExcludeFileMask =
- (ValueName == Clear ? AnsiString() : ValueName);
- }
- if (SetValue ||
- (FCopyParam.NegativeExclude && !FCopyParam.ExcludeFileMask.Masks.IsEmpty()))
- {
- PrintLine(FORMAT(ListFormat, (Names[Include], FCopyParam.ExcludeFileMask.Masks)));
- }
- }
- if (OPT(Exclude))
- {
- if (SetValue)
- {
- FCopyParam.NegativeExclude = false;
- FCopyParam.ExcludeFileMask =
- (ValueName == Clear ? AnsiString() : ValueName);
- }
- if (SetValue ||
- (!FCopyParam.NegativeExclude && !FCopyParam.ExcludeFileMask.Masks.IsEmpty()))
- {
- PrintLine(FORMAT(ListFormat, (Names[Exclude], FCopyParam.ExcludeFileMask.Masks)));
- }
- }
- #undef OPT
- }
- //---------------------------------------------------------------------------
- void __fastcall TScript::OptionProc(TScriptProcParams * Parameters)
- {
- AnsiString OptionName;
- AnsiString ValueName;
- if (Parameters->ParamCount >= 1)
- {
- OptionName = Parameters->Param[0];
- }
- if (Parameters->ParamCount >= 2)
- {
- ValueName = Parameters->Param[1];
- }
- OptionImpl(OptionName, ValueName);
- }
- //---------------------------------------------------------------------------
- void __fastcall TScript::AsciiProc(TScriptProcParams * /*Parameters*/)
- {
- OptionImpl("transfer", "ascii");
- }
- //---------------------------------------------------------------------------
- void __fastcall TScript::BinaryProc(TScriptProcParams * /*Parameters*/)
- {
- OptionImpl("transfer", "binary");
- }
- //---------------------------------------------------------------------------
- void __fastcall TScript::SynchronizeDirectories(TScriptProcParams * Parameters,
- AnsiString & LocalDirectory, AnsiString & RemoteDirectory, int FirstParam)
- {
- if (Parameters->ParamCount > FirstParam)
- {
- LocalDirectory = Parameters->Param[FirstParam];
- }
- else
- {
- LocalDirectory = GetCurrentDir();
- }
- if (Parameters->ParamCount > FirstParam + 1)
- {
- RemoteDirectory = Parameters->Param[FirstParam + 1];
- }
- else
- {
- RemoteDirectory = FTerminal->CurrentDirectory;
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TScript::SynchronizeProc(TScriptProcParams * Parameters)
- {
- CheckSession();
- ResetTransfer();
- static const char * ModeNames[] = { "remote", "local", "both" };
- AnsiString ModeName = Parameters->Param[0];
- assert(FSynchronizeMode < 0);
- FSynchronizeMode = TScriptCommands::FindCommand(ModeNames, LENOF(ModeNames), ModeName);
- try
- {
- if (FSynchronizeMode < 0)
- {
- throw Exception(FMTLOAD(SCRIPT_OPTION_UNKNOWN, (ModeName)));
- }
- AnsiString LocalDirectory;
- AnsiString RemoteDirectory;
- SynchronizeDirectories(Parameters, LocalDirectory, RemoteDirectory, 1);
- TCopyParamType CopyParam = FCopyParam;
- CopyParam.CalculateSize = false;
- int SynchronizeParams = FSynchronizeParams | TTerminal::spNoConfirmation;
- PrintLine(FMTLOAD(SCRIPT_SYNCHRONIZE_COLLECT,
- (ExcludeTrailingBackslash(LocalDirectory),
- UnixExcludeTrailingBackslash(RemoteDirectory))));
- TSynchronizeChecklist * Checklist =
- FTerminal->SynchronizeCollect(LocalDirectory, RemoteDirectory,
- static_cast<TTerminal::TSynchronizeMode>(FSynchronizeMode),
- &CopyParam, SynchronizeParams, NULL, NULL);
- try
- {
- if (Checklist->Count > 0)
- {
- FTerminal->SynchronizeApply(Checklist, LocalDirectory, RemoteDirectory,
- &CopyParam, SynchronizeParams, OnTerminalSynchronizeDirectory);
- }
- }
- __finally
- {
- delete Checklist;
- }
- }
- __finally
- {
- FSynchronizeMode = -1;
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TScript::Synchronize(const AnsiString LocalDirectory,
- const AnsiString RemoteDirectory, const TCopyParamType & ACopyParam,
- TSynchronizeChecklist ** Checklist)
- {
- try
- {
- FKeepingUpToDate = true;
- TCopyParamType CopyParam = ACopyParam;
- CopyParam.CalculateSize = false;
- int SynchronizeParams = FSynchronizeParams | TTerminal::spNoConfirmation |
- TTerminal::spNoRecurse | TTerminal::spUseCache | TTerminal::spDelayProgress |
- TTerminal::spSubDirs;
- TSynchronizeChecklist * AChecklist =
- FTerminal->SynchronizeCollect(LocalDirectory, RemoteDirectory, TTerminal::smRemote,
- &CopyParam, SynchronizeParams, NULL, NULL);
- try
- {
- if (AChecklist->Count > 0)
- {
- FTerminal->SynchronizeApply(AChecklist, LocalDirectory, RemoteDirectory,
- &CopyParam, SynchronizeParams, OnTerminalSynchronizeDirectory);
- }
- }
- __finally
- {
- if (Checklist == NULL)
- {
- delete AChecklist;
- }
- else
- {
- *Checklist = AChecklist;
- }
- }
- // to break line after the last transfer (if any);
- Print("");
- FKeepingUpToDate = false;
- }
- catch(Exception & E)
- {
- FKeepingUpToDate = false;
- HandleExtendedException(&E);
- throw;
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TScript::KeepUpToDateProc(TScriptProcParams * Parameters)
- {
- if (OnSynchronizeStartStop == NULL)
- {
- Abort();
- }
- CheckSession();
- ResetTransfer();
- AnsiString LocalDirectory;
- AnsiString RemoteDirectory;
- SynchronizeDirectories(Parameters, LocalDirectory, RemoteDirectory, 0);
- PrintLine(LoadStr(SCRIPT_KEEPING_UP_TO_DATE));
- OnSynchronizeStartStop(this, LocalDirectory, RemoteDirectory);
- }
- //---------------------------------------------------------------------------
- //---------------------------------------------------------------------------
- __fastcall TManagementScript::TManagementScript(TStoredSessionList * StoredSessions) :
- TScript()
- {
- assert(StoredSessions != NULL);
- FOnInput = NULL;
- FOnTerminalUpdateStatus = NULL;
- FOnTerminalPromptUser = NULL;
- FOnShowExtendedException = NULL;
- FOnTerminalQueryUser = NULL;
- FStoredSessions = StoredSessions;
- FTerminalList = new TTerminalList(Configuration);
- FOnQueryCancel = NULL;
- FContinue = true;
- OnTerminalSynchronizeDirectory = TerminalSynchronizeDirectory;
- FCommands->Register("exit", SCRIPT_EXIT_DESC, SCRIPT_EXIT_HELP, &ExitProc, 0, 0);
- FCommands->Register("bye", 0, SCRIPT_EXIT_HELP, &ExitProc, 0, 0);
- FCommands->Register("open", SCRIPT_OPEN_DESC, SCRIPT_OPEN_HELP, &OpenProc, 0, 1);
- FCommands->Register("close", SCRIPT_CLOSE_DESC, SCRIPT_CLOSE_HELP, &CloseProc, 0, 1);
- FCommands->Register("session", SCRIPT_SESSION_DESC, SCRIPT_SESSION_HELP, &SessionProc, 0, 1);
- FCommands->Register("lpwd", SCRIPT_LPWD_DESC, SCRIPT_LPWD_HELP, &LPwdProc, 0, 0);
- FCommands->Register("lcd", SCRIPT_LCD_DESC, SCRIPT_LCD_HELP, &LCdProc, 1, 1);
- FCommands->Register("lls", SCRIPT_LLS_DESC, SCRIPT_LLS_HELP, &LLsProc, 0, 1);
- }
- //---------------------------------------------------------------------------
- __fastcall TManagementScript::~TManagementScript()
- {
- while (FTerminalList->Count > 0)
- {
- FreeTerminal(FTerminalList->Terminals[0]);
- }
- delete FTerminalList;
- }
- //---------------------------------------------------------------------------
- void __fastcall TManagementScript::FreeTerminal(TTerminal * Terminal)
- {
- if (!Terminal->SessionData->Name.IsEmpty())
- {
- Terminal->SessionData->RemoteDirectory = Terminal->CurrentDirectory;
- TSessionData * Data;
- Data = (TSessionData *)StoredSessions->FindByName(Terminal->SessionData->Name);
- if (Data != NULL)
- {
- bool Changed = false;
- if (Terminal->SessionData->UpdateDirectories)
- {
- Data->RemoteDirectory = Terminal->SessionData->RemoteDirectory;
- Changed = true;
- }
- if (Changed)
- {
- StoredSessions->Save();
- }
- }
- }
- FTerminalList->FreeTerminal(Terminal);
- }
- //---------------------------------------------------------------------------
- void __fastcall TManagementScript::Input(const AnsiString Prompt,
- AnsiString & Str, bool AllowEmpty)
- {
- do
- {
- Str = "";
- if (FOnInput != NULL)
- {
- FOnInput(this, Prompt, Str);
- }
- else
- {
- Abort();
- }
- }
- while (Str.Trim().IsEmpty() && !AllowEmpty);
- }
- //---------------------------------------------------------------------------
- void __fastcall TManagementScript::PrintProgress(bool First, const AnsiString Str)
- {
- if (FOnPrintProgress != NULL)
- {
- FOnPrintProgress(this, First, Str);
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TManagementScript::ResetTransfer()
- {
- TScript::ResetTransfer();
- FLastProgressFile = "";
- FLastProgressTime = 0;
- FLastProgressMessage = "";
- }
- //---------------------------------------------------------------------------
- bool __fastcall TManagementScript::QueryCancel()
- {
- bool Result = false;
- if (OnQueryCancel != NULL)
- {
- OnQueryCancel(this, Result);
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- void __fastcall TManagementScript::TerminalOnStdError(TObject * Sender,
- TLogLineType /*Type*/, const AnsiString AddedLine)
- {
- TTerminal * Terminal = dynamic_cast<TTerminal*>(Sender);
- assert(Terminal != NULL);
- if (Terminal->Status == sshAuthenticate)
- {
- PrintLine(AddedLine);
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TManagementScript::TerminalPromptUser(TSecureShell * SecureShell,
- AnsiString Prompt, TPromptKind Kind, AnsiString & Response, bool & Result,
- void * Arg)
- {
- if (!SecureShell->StoredPasswordTried && (OnTerminalPromptUser != NULL))
- {
- OnTerminalPromptUser(SecureShell, Prompt, Kind, Response, Result, Arg);
- }
- }
- //---------------------------------------------------------------------------
- bool __fastcall TManagementScript::Synchronizing()
- {
- return (FKeepingUpToDate || (FSynchronizeMode >= 0));
- }
- //---------------------------------------------------------------------------
- void __fastcall TManagementScript::ShowPendingProgress()
- {
- if (!FSynchronizeIntro.IsEmpty())
- {
- if (Synchronizing())
- {
- PrintLine(FSynchronizeIntro);
- }
- FSynchronizeIntro = "";
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TManagementScript::TerminalOperationProgress(
- TFileOperationProgressType & ProgressData, TCancelStatus & Cancel)
- {
- if (ProgressData.Operation == foCopy)
- {
- if (ProgressData.InProgress && !ProgressData.FileName.IsEmpty())
- {
- bool DoPrint = false;
- bool First = false;
- AnsiString ProgressFileName = ProgressData.FileName;
- if (ProgressData.Side == osLocal)
- {
- ProgressFileName = ExcludeTrailingBackslash(ProgressFileName);
- }
- else
- {
- ProgressFileName = UnixExcludeTrailingBackslash(ProgressFileName);
- }
- if (ProgressFileName != FLastProgressFile)
- {
- First = true;
- DoPrint = true;
- ShowPendingProgress();
- }
- if (!DoPrint && ((FLastProgressTime != time(NULL)) || ProgressData.IsTransferDone()))
- {
- DoPrint = true;
- }
- if (DoPrint)
- {
- static int MaxFileName = 25;
- AnsiString FileName = MinimizeName(ProgressFileName, MaxFileName,
- ProgressData.Side == osRemote);
- AnsiString ProgressMessage = FORMAT("%-*.*s | %10d kB | %6.1f kB/s | %-6.6s | %3d%%",
- (MaxFileName, MaxFileName, FileName,
- static_cast<int>(ProgressData.TransferedSize / 1024),
- static_cast<float>(ProgressData.CPS()) / 1024,
- ProgressData.AsciiTransfer ? "ascii" : "binary",
- ProgressData.TransferProgress()));
- if (FLastProgressMessage != ProgressMessage)
- {
- FLastProgressTime = time(NULL);
- PrintProgress(First, ProgressMessage);
- FLastProgressMessage = ProgressMessage;
- FLastProgressFile = ProgressFileName;
- }
- }
- }
- else
- {
- FLastProgressFile = "";
- }
- }
- if (QueryCancel())
- {
- Cancel = csCancel;
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TManagementScript::TerminalOperationFinished(
- TFileOperation Operation, TOperationSide /*Side*/,
- bool /*Temp*/, const AnsiString FileName, Boolean Success,
- bool & /*DisconnectWhenComplete*/)
- {
- if (Success && (Operation != foCalculateSize) && (Operation != foCopy))
- {
- ShowPendingProgress();
- // For FKeepingUpToDate we should send events to synchronize controller eventuelly.
- if (Synchronizing() && (Operation == foDelete))
- {
- // Note that this is duplicated with "keep up to date" log.
- PrintLine(FMTLOAD(SCRIPT_SYNCHRONIZE_DELETED, (FileName)));
- }
- else
- {
- PrintLine(FileName);
- }
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TManagementScript::TerminalSynchronizeDirectory(
- const AnsiString LocalDirectory, const AnsiString RemoteDirectory,
- bool & Continue)
- {
- int SynchronizeMode = FSynchronizeMode;
- if (FKeepingUpToDate)
- {
- SynchronizeMode = TTerminal::smRemote;
- }
- AnsiString Arrow;
- switch (SynchronizeMode)
- {
- case TTerminal::smRemote:
- Arrow = "=>";
- break;
- case TTerminal::smLocal:
- Arrow = "<=";
- break;
- case TTerminal::smBoth:
- Arrow = "<=>";
- break;
- }
- FSynchronizeIntro = FMTLOAD(SCRIPT_SYNCHRONIZE, (ExcludeTrailingBackslash(LocalDirectory),
- Arrow, UnixExcludeTrailingBackslash(RemoteDirectory)));
- if (QueryCancel())
- {
- Continue = false;
- }
- }
- //---------------------------------------------------------------------------
- TTerminal * __fastcall TManagementScript::FindSession(const AnsiString Index)
- {
- int i = StrToIntDef(Index, -1);
- if ((i <= 0) || (i > FTerminalList->Count))
- {
- throw Exception(FMTLOAD(SCRIPT_SESSION_INDEX_INVALID, (Index)));
- }
- else
- {
- return FTerminalList->Terminals[i - 1];
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TManagementScript::PrintActiveSession()
- {
- assert(FTerminal != NULL);
- PrintLine(FMTLOAD(SCRIPT_ACTIVE_SESSION,
- (FTerminalList->IndexOf(FTerminal) + 1, FTerminal->SessionData->SessionName)));
- }
- //---------------------------------------------------------------------------
- bool __fastcall TManagementScript::HandleExtendedException(Exception * E,
- TTerminal * Terminal)
- {
- bool Result = TScript::HandleExtendedException(E);
- if (Terminal == NULL)
- {
- Terminal = FTerminal;
- }
- if ((Terminal != NULL) && (Terminal == FTerminal) && (dynamic_cast<EFatal*>(E) != NULL))
- {
- try
- {
- DoClose(Terminal);
- }
- catch(...)
- {
- // ignore disconnect errors
- }
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- void __fastcall TManagementScript::Connect(const AnsiString Session)
- {
- try
- {
- DoConnect(Session);
- }
- catch(Exception & E)
- {
- if (!HandleExtendedException(&E))
- {
- throw;
- }
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TManagementScript::DoConnect(const AnsiString Session)
- {
- bool DefaultsOnly;
- TSessionData * Data = FStoredSessions->ParseUrl(Session, DefaultsOnly,
- puDecodeUrlChars, NULL);
- try
- {
- assert(Data != NULL);
- if (!Data->CanLogin || DefaultsOnly)
- {
- if (Data->HostName.IsEmpty())
- {
- AnsiString Value;
- Input(LoadStr(SCRIPT_HOST_PROMPT), Value, false);
- Data->HostName = Value;
- }
- if (Data->UserName.IsEmpty())
- {
- AnsiString Value;
- Input(LoadStr(SCRIPT_USERNAME_PROMPT), Value, false);
- Data->UserName = Value;
- }
- assert(Data->CanLogin);
- }
- TTerminal * Terminal = FTerminalList->NewTerminal(Data);
- try
- {
- Terminal->AutoReadDirectory = false;
- Terminal->OnStdError = TerminalOnStdError;
- Terminal->OnPromptUser = TerminalPromptUser;
- Terminal->OnShowExtendedException = OnShowExtendedException;
- Terminal->OnQueryUser = OnTerminalQueryUser;
- Terminal->OnProgress = TerminalOperationProgress;
- Terminal->OnFinished = TerminalOperationFinished;
- Terminal->OnUpdateStatus = OnTerminalUpdateStatus;
- ConnectTerminal(Terminal);
- }
- catch(...)
- {
- FTerminalList->FreeTerminal(Terminal);
- throw;
- }
- FTerminal = Terminal;
- }
- __finally
- {
- delete Data;
- }
- PrintActiveSession();
- }
- //---------------------------------------------------------------------------
- void __fastcall TManagementScript::DoClose(TTerminal * Terminal)
- {
- int Index = FTerminalList->IndexOf(Terminal);
- assert(Index >= 0);
- try
- {
- Terminal->Active = false;
- AnsiString SessionName = Terminal->SessionData->SessionName;
- FreeTerminal(Terminal);
- PrintLine(FMTLOAD(SCRIPT_SESSION_CLOSED, (SessionName)));
- }
- __finally
- {
- if (FTerminalList->Count > 0)
- {
- if (Index < FTerminalList->Count)
- {
- FTerminal = FTerminalList->Terminals[Index];
- }
- else
- {
- FTerminal = FTerminalList->Terminals[0];
- }
- PrintActiveSession();
- }
- else
- {
- FTerminal = NULL;
- PrintLine(LoadStr(SCRIPT_NO_SESSION));
- }
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TManagementScript::ExitProc(TScriptProcParams * /*Parameters*/)
- {
- FContinue = false;
- }
- //---------------------------------------------------------------------------
- void __fastcall TManagementScript::OpenProc(TScriptProcParams * Parameters)
- {
- Connect(Parameters->ParamCount > 0 ? Parameters->Param[0] : AnsiString());
- }
- //---------------------------------------------------------------------------
- void __fastcall TManagementScript::CloseProc(TScriptProcParams * Parameters)
- {
- CheckSession();
- TTerminal * Terminal;
- if (Parameters->ParamCount == 0)
- {
- Terminal = FTerminal;
- }
- else
- {
- Terminal = FindSession(Parameters->Param[0]);
- }
- DoClose(Terminal);
- }
- //---------------------------------------------------------------------------
- void __fastcall TManagementScript::SessionProc(TScriptProcParams * Parameters)
- {
- CheckSession();
- if (Parameters->ParamCount == 0)
- {
- for (int i = 0; i < FTerminalList->Count; i++)
- {
- PrintLine(FORMAT("%3d %s",
- (i + 1, FTerminalList->Terminals[i]->SessionData->SessionName)));
- }
- PrintActiveSession();
- }
- else
- {
- FTerminal = FindSession(Parameters->Param[0]);
- PrintActiveSession();
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TManagementScript::LPwdProc(TScriptProcParams * /*Parameters*/)
- {
- PrintLine(GetCurrentDir());
- }
- //---------------------------------------------------------------------------
- void __fastcall TManagementScript::LCdProc(TScriptProcParams * Parameters)
- {
- assert(Parameters->ParamCount == 1);
- AnsiString Directory = Parameters->Param[0];
- if (!SetCurrentDir(Directory))
- {
- throw Exception(FMTLOAD(CHANGE_DIR_ERROR, (Directory)));
- }
- PrintLine(GetCurrentDir());
- }
- //---------------------------------------------------------------------------
- void __fastcall TManagementScript::LLsProc(TScriptProcParams * Parameters)
- {
- AnsiString Directory;
- AnsiString Mask;
- if (Parameters->ParamCount > 0)
- {
- Directory = Parameters->Param[0];
- Mask = ExtractFileName(Directory);
- if (TFileMasks::IsMask(Mask))
- {
- Directory = ExtractFilePath(Directory);
- }
- else
- {
- Mask = "";
- }
- }
- if (Directory.IsEmpty())
- {
- Directory = GetCurrentDir();
- }
- if (Mask.IsEmpty())
- {
- Mask = "*.*";
- }
- TSearchRec SearchRec;
- int FindAttrs = faReadOnly | faHidden | faSysFile | faDirectory | faArchive;
- if (FindFirst(IncludeTrailingBackslash(Directory) + Mask, FindAttrs, SearchRec) != 0)
- {
- throw Exception(FMTLOAD(LIST_DIR_ERROR, (Directory)));
- }
- try
- {
- AnsiString TimeFormat = FixedLenDateTimeFormat(ShortTimeFormat);
- AnsiString DateFormat = FixedLenDateTimeFormat(ShortDateFormat);
- int DateLen = 0;
- int TimeLen = 0;
- bool First = true;
- do
- {
- if (SearchRec.Name != ".")
- {
- TDateTime DateTime = FileDateToDateTime(SearchRec.Time);
- AnsiString TimeStr = FormatDateTime(TimeFormat, DateTime);
- AnsiString DateStr = FormatDateTime(DateFormat, DateTime);
- if (First)
- {
- if (TimeLen < TimeStr.Length())
- {
- TimeLen = TimeStr.Length();
- }
- if (DateLen < DateStr.Length())
- {
- DateLen = DateStr.Length();
- }
- First = false;
- }
- PrintLine(FORMAT("%-*s %-*s %-14s %s", (
- DateLen, DateStr, TimeLen, TimeStr,
- (FLAGSET(SearchRec.Attr, faDirectory) ?
- AnsiString("<DIR>") : FORMAT("%14.0n", (double(SearchRec.Size)))),
- SearchRec.Name)));
- }
- }
- while (FindNext(SearchRec) == 0);
- }
- __finally
- {
- FindClose(SearchRec);
- }
- }
- //---------------------------------------------------------------------------
|