Browse Source

Bug 1456: Expand patterns in default value of extension options

https://winscp.net/tracker/1456

Source commit: 4dab0653f422d054736959e799d9443d18f05754
Martin Prikryl 9 years ago
parent
commit
393eae5450

+ 7 - 1
source/core/FileMasks.cpp

@@ -980,7 +980,8 @@ bool __fastcall TCustomCommand::FindPattern(const UnicodeString & Command,
     wchar_t APatternCmd;
     GetToken(Command, Index, Len, APatternCmd);
     if (((PatternCmd != L'!') && (tolower(PatternCmd) == tolower(APatternCmd))) ||
-        ((PatternCmd == L'!') && (Len == 1) && (APatternCmd != TEXT_TOKEN)))
+        ((PatternCmd == L'!') && (Len == 1) && (APatternCmd != TEXT_TOKEN)) ||
+        ((PatternCmd == L'\0') && (APatternCmd != TEXT_TOKEN)))
     {
       Result = true;
     }
@@ -991,6 +992,11 @@ bool __fastcall TCustomCommand::FindPattern(const UnicodeString & Command,
   return Result;
 }
 //---------------------------------------------------------------------------
+bool __fastcall TCustomCommand::HasAnyPatterns(const UnicodeString & Command)
+{
+  return FindPattern(Command, L'\0');
+}
+//---------------------------------------------------------------------------
 void __fastcall TCustomCommand::ValidatePattern(const UnicodeString & /*Command*/,
   int /*Index*/, int /*Len*/, wchar_t /*PatternCmd*/, void * /*Arg*/)
 {

+ 1 - 0
source/core/FileMasks.h

@@ -138,6 +138,7 @@ public:
 
   UnicodeString __fastcall Complete(const UnicodeString & Command, bool LastPass);
   virtual void __fastcall Validate(const UnicodeString & Command);
+  bool __fastcall HasAnyPatterns(const UnicodeString & Command);
 
   static UnicodeString __fastcall Escape(const UnicodeString & S);
 

+ 18 - 7
source/forms/Custom.cpp

@@ -837,7 +837,8 @@ class TCustomCommandOptionsDialog : public TCustomDialog
 {
 public:
   __fastcall TCustomCommandOptionsDialog(
-    const TCustomCommandType * Command, TStrings * CustomCommandOptions, unsigned int Flags);
+    const TCustomCommandType * Command, TStrings * CustomCommandOptions, unsigned int Flags,
+    TCustomCommand * CustomCommandForOptions);
 
   bool __fastcall Execute();
 
@@ -866,7 +867,8 @@ private:
 };
 //---------------------------------------------------------------------------
 __fastcall TCustomCommandOptionsDialog::TCustomCommandOptionsDialog(
-    const TCustomCommandType * Command, TStrings * CustomCommandOptions, unsigned int Flags) :
+    const TCustomCommandType * Command, TStrings * CustomCommandOptions,
+    unsigned int Flags, TCustomCommand * CustomCommandForOptions) :
   TCustomDialog(HELP_EXTENSION_OPTIONS)
 {
   FCommand = Command;
@@ -884,13 +886,21 @@ __fastcall TCustomCommandOptionsDialog::TCustomCommandOptionsDialog(
     {
       UnicodeString OptionKey = FCommand->GetOptionKey(Option);
       UnicodeString Value;
-      if (FCustomCommandOptions->IndexOfName(OptionKey) >= 0)
+      if ((CustomCommandForOptions != NULL) &&
+          Option.HasPatterns(CustomCommandForOptions))
       {
-        Value = FCustomCommandOptions->Values[OptionKey];
+        Value = CustomCommandForOptions->Complete(Option.Default, true);
       }
       else
       {
-        Value = Option.Default;
+        if (FCustomCommandOptions->IndexOfName(OptionKey) >= 0)
+        {
+          Value = FCustomCommandOptions->Values[OptionKey];
+        }
+        else
+        {
+          Value = Option.Default;
+        }
       }
 
       int Tag = (OptionIndex << 16) + ControlIndex;
@@ -1280,9 +1290,10 @@ void __fastcall TCustomCommandOptionsDialog::DoShow()
 }
 //---------------------------------------------------------------------------
 bool __fastcall DoCustomCommandOptionsDialog(
-  const TCustomCommandType * Command, TStrings * CustomCommandOptions, unsigned int Flags)
+  const TCustomCommandType * Command, TStrings * CustomCommandOptions,
+  unsigned int Flags, TCustomCommand * CustomCommandForOptions)
 {
   std::unique_ptr<TCustomCommandOptionsDialog> Dialog(
-    new TCustomCommandOptionsDialog(Command, CustomCommandOptions, Flags));
+    new TCustomCommandOptionsDialog(Command, CustomCommandOptions, Flags, CustomCommandForOptions));
   return Dialog->Execute();
 }

+ 12 - 10
source/forms/CustomScpExplorer.cpp

@@ -1671,10 +1671,21 @@ void __fastcall TCustomScpExplorerForm::CustomCommand(TStrings * FileList,
   const TCustomCommandType & ACommand, TStrings * ALocalFileList)
 {
 
+  TCustomCommandData Data(Terminal);
+  std::unique_ptr<TCustomCommand> CustomCommandForOptions;
+  if (FLAGCLEAR(ACommand.Params, ccLocal))
+  {
+    CustomCommandForOptions.reset(new TRemoteCustomCommand(Data, Terminal->CurrentDirectory));
+  }
+  else
+  {
+    CustomCommandForOptions.reset(new TLocalCustomCommand(Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory()));
+  }
+
   std::unique_ptr<TStrings> CustomCommandOptions(CloneStrings(WinConfiguration->CustomCommandOptions));
   if (ACommand.AnyOptionWithFlag(TCustomCommandType::ofRun))
   {
-    if (!DoCustomCommandOptionsDialog(&ACommand, CustomCommandOptions.get(), TCustomCommandType::ofRun))
+    if (!DoCustomCommandOptionsDialog(&ACommand, CustomCommandOptions.get(), TCustomCommandType::ofRun, CustomCommandForOptions.get()))
     {
       Abort();
     }
@@ -1686,7 +1697,6 @@ void __fastcall TCustomScpExplorerForm::CustomCommand(TStrings * FileList,
   {
     if (EnsureCommandSessionFallback(fcShellAnyCommand))
     {
-      TCustomCommandData Data(Terminal);
       TRemoteCustomCommand RemoteCustomCommand(Data, Terminal->CurrentDirectory);
       TWinInteractiveCustomCommand InteractiveCustomCommand(
         &RemoteCustomCommand, ACommand.Name, ACommand.HomePage);
@@ -1738,7 +1748,6 @@ void __fastcall TCustomScpExplorerForm::CustomCommand(TStrings * FileList,
   }
   else
   {
-    TCustomCommandData Data(Terminal);
     TLocalCustomCommand LocalCustomCommand(Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory());
     TWinInteractiveCustomCommand InteractiveCustomCommand(
       &LocalCustomCommand, ACommand.Name, ACommand.HomePage);
@@ -1851,7 +1860,6 @@ void __fastcall TCustomScpExplorerForm::CustomCommand(TStrings * FileList,
                 LocalFile = LocalFileList->Strings[0];
               }
 
-              TCustomCommandData Data(FTerminal);
               TLocalCustomCommand CustomCommand(Data,
                 Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), L"", LocalFile, FileList);
               UnicodeString ShellCommand = CustomCommand.Complete(Command, true);
@@ -1874,7 +1882,6 @@ void __fastcall TCustomScpExplorerForm::CustomCommand(TStrings * FileList,
                 for (int Index = 0; Index < RemoteFileList->Count; Index++)
                 {
                   UnicodeString FileName = RemoteFileList->Strings[Index];
-                  TCustomCommandData Data(FTerminal);
                   TLocalCustomCommand CustomCommand(Data,
                     Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(), FileName, LocalFile, L"");
                   ExecuteShellAndWait(CustomCommand.Complete(Command, true));
@@ -1886,7 +1893,6 @@ void __fastcall TCustomScpExplorerForm::CustomCommand(TStrings * FileList,
 
                 for (int Index = 0; Index < LocalFileList->Count; Index++)
                 {
-                  TCustomCommandData Data(FTerminal);
                   TLocalCustomCommand CustomCommand(
                     Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
                     FileName, LocalFileList->Strings[Index], L"");
@@ -1903,7 +1909,6 @@ void __fastcall TCustomScpExplorerForm::CustomCommand(TStrings * FileList,
                 for (int Index = 0; Index < LocalFileList->Count; Index++)
                 {
                   UnicodeString FileName = RemoteFileList->Strings[Index];
-                  TCustomCommandData Data(FTerminal);
                   TLocalCustomCommand CustomCommand(
                     Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
                     FileName, LocalFileList->Strings[Index], L"");
@@ -1915,7 +1920,6 @@ void __fastcall TCustomScpExplorerForm::CustomCommand(TStrings * FileList,
             {
               for (int Index = 0; Index < RemoteFileList->Count; Index++)
               {
-                TCustomCommandData Data(FTerminal);
                 TLocalCustomCommand CustomCommand(Data,
                   Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
                   RemoteFileList->Strings[Index], L"", L"");
@@ -2026,7 +2030,6 @@ void __fastcall TCustomScpExplorerForm::CustomCommand(TStrings * FileList,
       if (FileListCommand)
       {
         UnicodeString FileList = MakeFileList(LocalFileList.get());
-        TCustomCommandData Data(FTerminal);
         TLocalCustomCommand CustomCommand(
           Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
           L"", L"", FileList);
@@ -2045,7 +2048,6 @@ void __fastcall TCustomScpExplorerForm::CustomCommand(TStrings * FileList,
           for (int Index = 0; Index < LocalFileList->Count; Index++)
           {
             UnicodeString FileName = LocalFileList->Strings[Index];
-            TCustomCommandData Data(FTerminal);
             TLocalCustomCommand CustomCommand(
               Data, Terminal->CurrentDirectory, DefaultDownloadTargetDirectory(),
               FileName, L"", L"");

+ 1 - 1
source/forms/Preferences.cpp

@@ -2660,7 +2660,7 @@ void __fastcall TPreferencesDialog::ConfigureCommand()
   int Index = CustomCommandsView->ItemIndex;
   const TCustomCommandType * Command = GetCommandList(Index)->Commands[GetCommandIndex(Index)];
 
-  DoCustomCommandOptionsDialog(Command, FCustomCommandOptions.get(), TCustomCommandType::ofConfig);
+  DoCustomCommandOptionsDialog(Command, FCustomCommandOptions.get(), TCustomCommandType::ofConfig, NULL);
   UpdateCustomCommandsView();
 }
 //---------------------------------------------------------------------------

+ 23 - 0
source/windows/WinConfiguration.cpp

@@ -3184,6 +3184,29 @@ bool __fastcall TCustomCommandType::TOption::GetIsControl() const
   return (Id != L"-");
 }
 //---------------------------------------------------------------------------
+bool TCustomCommandType::TOption::HasPatterns(TCustomCommand * CustomCommandForOptions) const
+{
+  bool CanHavePatterns;
+  switch (Kind)
+  {
+    case okTextBox:
+    case okFile:
+      CanHavePatterns = true;
+      break;
+
+    default:
+      CanHavePatterns = false;
+      break;
+  }
+
+  bool Result =
+    CanHavePatterns &&
+    FLAGSET(Flags, TCustomCommandType::ofRun) &&
+    FLAGCLEAR(Flags, TCustomCommandType::ofConfig) &&
+    CustomCommandForOptions->HasAnyPatterns(Default);
+  return Result;
+}
+//---------------------------------------------------------------------------
 bool TCustomCommandType::TOption::operator==(const TCustomCommandType::TOption & Other) const
 {
   // needed by vector<> but probably never used

+ 1 - 0
source/windows/WinConfiguration.h

@@ -734,6 +734,7 @@ public:
 
     bool operator==(const TOption & Other) const;
     __property bool IsControl = { read = GetIsControl };
+    bool HasPatterns(TCustomCommand * CustomCommandForOptions) const;
 
   private:
     bool __fastcall GetIsControl() const;

+ 2 - 1
source/windows/WinInterface.h

@@ -132,7 +132,8 @@ class TShortCuts;
 bool __fastcall DoShortCutDialog(TShortCut & ShortCut,
   const TShortCuts & ShortCuts, UnicodeString HelpKeyword);
 bool __fastcall DoCustomCommandOptionsDialog(
-  const TCustomCommandType * Command, TStrings * CustomCommandOptions, unsigned int Flags);
+  const TCustomCommandType * Command, TStrings * CustomCommandOptions, unsigned int Flags,
+  TCustomCommand * CustomCommandForOptions);
 
 // windows\UserInterface.cpp
 bool __fastcall DoMasterPasswordDialog();