diff options
| author | Akshay Nair <phenax5@gmail.com> | 2025-11-07 10:37:00 +0530 |
|---|---|---|
| committer | Akshay Nair <phenax5@gmail.com> | 2025-11-07 10:37:00 +0530 |
| commit | 0e4a7e45357e6024586b6042c1f4c173926a2ace (patch) | |
| tree | a61c5bc6b3f274845487fc06af1352b4c94d628a /lib | |
| parent | 1481e69cee500a98aafca08e676c9e47fb1c9fb7 (diff) | |
| download | daffm-0e4a7e45357e6024586b6042c1f4c173926a2ace.tar.gz daffm-0e4a7e45357e6024586b6042c1f4c173926a2ace.zip | |
Fix shell escaping for substitutions
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/Daffm/Action/Core.hs | 15 | ||||
| -rw-r--r-- | lib/Daffm/Args.hs | 2 |
2 files changed, 10 insertions, 7 deletions
diff --git a/lib/Daffm/Action/Core.hs b/lib/Daffm/Action/Core.hs index dc68c5d..85f861a 100644 --- a/lib/Daffm/Action/Core.hs +++ b/lib/Daffm/Action/Core.hs @@ -10,11 +10,13 @@ import Control.Monad (void) import Control.Monad.State (MonadIO (liftIO), MonadState, get, gets, modify, put) import Daffm.State import Daffm.Types (AppEvent, AppState (..), FileInfo (..), FilePathText, FileType (..)) +import qualified Data.Map as Map import Data.Maybe (fromMaybe) import qualified Data.Set as Set import qualified Data.Text as Text import qualified Data.Vector as Vec import System.Directory (getHomeDirectory) +import System.Environment (getEnvironment) import qualified System.Exit as Proc import System.FilePath (takeDirectory) import qualified System.Process as Proc @@ -54,7 +56,7 @@ openSelectedFile = do cmdSubstitutions :: Text.Text -> AppEvent Text.Text cmdSubstitutions cmd = gets (`substitute` cmd) where - escape = (\s -> "'" <> s <> "'") . Text.replace "'" "\\'" + escape = (\s -> "'" <> s <> "'") . Text.replace "'" "'\\''" substitute (AppState {stateFiles, stateCwd, stateFileSelections}) = Text.replace "%" (escape cursorFile) . Text.replace "%d" (escape stateCwd) @@ -72,18 +74,19 @@ cmdSubstitutions cmd = gets (`substitute` cmd) -- When exit code is non-zero, it will print it and prompt for key press regardless of waitForKey suspendAndRunShellCommand :: Bool -> Text.Text -> AppEvent () suspendAndRunShellCommand waitForKey cmd = do - suspendAndResume' $ - shellCommand (Text.unpack cmd) >>= \case + suspendAndResume' $ do + shellCommand (Text.unpack cmd) Map.empty >>= \case Proc.ExitFailure code -> do putStrLn $ "Process exited with " <> show code putStrLn "Press any key to continue" >> void getChar _ | waitForKey -> putStrLn "Press any key to continue" >> void getChar _ -> pure () -shellCommand :: String -> IO Proc.ExitCode -shellCommand cmd = do +shellCommand :: String -> Map.Map String String -> IO Proc.ExitCode +shellCommand cmd env = do + currentEnv <- getEnvironment Proc.withCreateProcess - (Proc.shell cmd) {Proc.delegate_ctlc = True} + (Proc.shell cmd) {Proc.delegate_ctlc = True, Proc.env = Just $ currentEnv ++ Map.toList env} $ \_ _ _ p -> Proc.waitForProcess p currentFile :: AppEvent (Maybe FileInfo) diff --git a/lib/Daffm/Args.hs b/lib/Daffm/Args.hs index aeaead7..1b81827 100644 --- a/lib/Daffm/Args.hs +++ b/lib/Daffm/Args.hs @@ -9,7 +9,7 @@ parseArgs rawArgs = case parsedArgs of Left e -> throwIO $ userError e Right v -> pure v where - parsedArgs = parse rawArgs (Args {argsDirOrFile = Nothing, argsConfigFile = Nothing, argsHelp = False}) + parsedArgs = parse rawArgs $ Args {argsDirOrFile = Nothing, argsConfigFile = Nothing, argsHelp = False} parse :: [String] -> Args -> Either String Args parse [] args = Right args parse ("-h" : _) args = Right $ args {argsHelp = True} |
