Be consistent with core utf8-string usage.

Now that spawn assumes executeFile takes a String containing utf8 codepoints
(and takes an actual String as input) adjust Prompt.Shell to avoid double
encoding. U.Run functions are updated to be consistent with spawn.
This commit is contained in:
Adam Vogt
2011-11-18 18:47:45 +00:00
parent 067ccb950e
commit 001b38c7ab
2 changed files with 10 additions and 7 deletions

View File

@@ -62,7 +62,7 @@ instance XPrompt Shell where
shellPrompt :: XPConfig -> X () shellPrompt :: XPConfig -> X ()
shellPrompt c = do shellPrompt c = do
cmds <- io getCommands cmds <- io getCommands
mkXPrompt Shell c (getShellCompl cmds) (spawn . encodeString) mkXPrompt Shell c (getShellCompl cmds) spawn
{- | See safe and unsafeSpawn. prompt is an alias for safePrompt; {- | See safe and unsafeSpawn. prompt is an alias for safePrompt;
safePrompt and unsafePrompt work on the same principles, but will use safePrompt and unsafePrompt work on the same principles, but will use
@@ -81,9 +81,9 @@ shellPrompt c = do
prompt, unsafePrompt, safePrompt :: FilePath -> XPConfig -> X () prompt, unsafePrompt, safePrompt :: FilePath -> XPConfig -> X ()
prompt = unsafePrompt prompt = unsafePrompt
safePrompt c config = mkXPrompt Shell config (getShellCompl [c]) run safePrompt c config = mkXPrompt Shell config (getShellCompl [c]) run
where run = safeSpawn c . return . encodeString where run = safeSpawn c . return
unsafePrompt c config = mkXPrompt Shell config (getShellCompl [c]) run unsafePrompt c config = mkXPrompt Shell config (getShellCompl [c]) run
where run a = unsafeSpawn $ c ++ " " ++ encodeString a where run a = unsafeSpawn $ c ++ " " ++ a
getShellCompl :: [String] -> String -> IO [String] getShellCompl :: [String] -> String -> IO [String]
getShellCompl cmds s | s == "" || last s == ' ' = return [] getShellCompl cmds s | s == "" || last s == ' ' = return []

View File

@@ -31,6 +31,7 @@ module XMonad.Util.Run (
hPutStr, hPutStrLn -- re-export for convenience hPutStr, hPutStrLn -- re-export for convenience
) where ) where
import Codec.Binary.UTF8.String
import System.Posix.IO import System.Posix.IO
import System.Posix.Process (createSession, executeFile, forkProcess) import System.Posix.Process (createSession, executeFile, forkProcess)
import Control.Concurrent (threadDelay) import Control.Concurrent (threadDelay)
@@ -53,7 +54,8 @@ import Control.Monad
-- | Returns the output. -- | Returns the output.
runProcessWithInput :: MonadIO m => FilePath -> [String] -> String -> m String runProcessWithInput :: MonadIO m => FilePath -> [String] -> String -> m String
runProcessWithInput cmd args input = io $ do runProcessWithInput cmd args input = io $ do
(pin, pout, perr, _) <- runInteractiveProcess cmd args Nothing Nothing (pin, pout, perr, _) <- runInteractiveProcess (encodeString cmd)
(map encodeString args) Nothing Nothing
hPutStr pin input hPutStr pin input
hClose pin hClose pin
output <- hGetContents pout output <- hGetContents pout
@@ -67,7 +69,8 @@ runProcessWithInput cmd args input = io $ do
runProcessWithInputAndWait :: MonadIO m => FilePath -> [String] -> String -> Int -> m () runProcessWithInputAndWait :: MonadIO m => FilePath -> [String] -> String -> Int -> m ()
runProcessWithInputAndWait cmd args input timeout = io $ do runProcessWithInputAndWait cmd args input timeout = io $ do
_ <- xfork $ do _ <- xfork $ do
(pin, pout, perr, _) <- runInteractiveProcess cmd args Nothing Nothing (pin, pout, perr, _) <- runInteractiveProcess (encodeString cmd)
(map encodeString args) Nothing Nothing
hPutStr pin input hPutStr pin input
hFlush pin hFlush pin
threadDelay timeout threadDelay timeout
@@ -108,7 +111,7 @@ safeSpawn :: MonadIO m => FilePath -> [String] -> m ()
safeSpawn prog args = io $ void_ $ forkProcess $ do safeSpawn prog args = io $ void_ $ forkProcess $ do
uninstallSignalHandlers uninstallSignalHandlers
_ <- createSession _ <- createSession
executeFile prog True args Nothing executeFile (encodeString prog) True (map encodeString args) Nothing
where void_ = (>> return ()) -- TODO: replace with Control.Monad.void / void not in ghc6 apparently where void_ = (>> return ()) -- TODO: replace with Control.Monad.void / void not in ghc6 apparently
-- | Simplified 'safeSpawn'; only takes a program (and no arguments): -- | Simplified 'safeSpawn'; only takes a program (and no arguments):
@@ -141,6 +144,6 @@ spawnPipe x = io $ do
hSetBuffering h LineBuffering hSetBuffering h LineBuffering
_ <- xfork $ do _ <- xfork $ do
_ <- dupTo rd stdInput _ <- dupTo rd stdInput
executeFile "/bin/sh" False ["-c", x] Nothing executeFile "/bin/sh" False ["-c", encodeString x] Nothing
closeFd rd closeFd rd
return h return h