From 7ce28a9d0c84179bd0beceab29264542b9d2adcc Mon Sep 17 00:00:00 2001 From: gwern0 Date: Fri, 19 Oct 2007 20:10:09 +0200 Subject: 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. darcs-hash:20071019181009-f7719-4f4073639389f335080d552c4788667fd109b6f3.gz --- Run.hs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/Run.hs b/Run.hs index 2eb16d4..5d61110 100644 --- a/Run.hs +++ b/Run.hs @@ -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 -- cgit v1.2.3