Run.hs: +2 functions, safeSpawn & unsafeSpawn

See their documentation. This is part of a re-organization of various 'run' commands; this two
make it easier to go through the shell or not, and will be re-used elsewhere.
This commit is contained in:
gwern0
2007-10-19 18:10:09 +00:00
parent 0917e5f56f
commit c7e40aa771

21
Run.hs
View File

@@ -20,6 +20,8 @@ module XMonadContrib.Run (
-- $usage
runProcessWithInput,
runProcessWithInputAndWait,
safeSpawn,
unsafeSpawn,
seconds
) where
@@ -81,3 +83,22 @@ runProcessWithInputAndWait cmd args input timeout = do
-}
seconds :: Rational -> Int
seconds = fromEnum . (* 1000000)
{- | safeSpawn bypasses XMonad's 'spawn' command, because spawn passes strings to /bin/sh to be interpreted as shell
commands. This is often what one wants, but in many cases the passed string will contain shell metacharacters
which one does not want interpreted as such (URLs particularly often have shell metacharacters like '&' in them).
In this case, it is more useful to specify a file or program to be run and a string to give it as an argument so
as to bypass the shell and be certain the program will receive the string as you typed it.
unsafeSpawn is an alias for XMonad's 'spawn', to remind one that use of it can be, well, unsafe.
Examples:
> , ((modMask, xK_Print ), unsafeSpawn "import -window root png:$HOME/xwd-$(date +%s)$$.png")
> , ((modMask, xK_d ), safeSpawn "firefox" "")
Note that the unsafeSpawn example must be unsafe and not safe because it makes use of shell interpretation by relying on
$HOME and interpolation, whereas the safeSpawn example can be safe because Firefox doesn't need any arguments if it is
just being started.
-}
safeSpawn :: FilePath -> String -> X ()
safeSpawn prog arg = io (try (forkProcess $ executeFile prog True [arg] Nothing) >> return ())
unsafeSpawn :: String -> X ()
unsafeSpawn = spawn