2013-02-13 18:30:04 +00:00
|
|
|
{- /bin/sh handling
|
|
|
|
-
|
2015-01-21 16:50:09 +00:00
|
|
|
- Copyright 2013 Joey Hess <id@joeyh.name>
|
2013-02-13 18:30:04 +00:00
|
|
|
-
|
2014-05-10 14:01:27 +00:00
|
|
|
- License: BSD-2-clause
|
2013-02-13 18:30:04 +00:00
|
|
|
-}
|
|
|
|
|
|
|
|
{-# LANGUAGE CPP #-}
|
|
|
|
|
|
|
|
module Utility.Shell where
|
|
|
|
|
2016-09-05 16:09:23 +00:00
|
|
|
import Utility.SafeCommand
|
|
|
|
#ifdef mingw32_HOST_OS
|
|
|
|
import Utility.Path
|
|
|
|
import Utility.Exception
|
|
|
|
import Utility.PartialPrelude
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef mingw32_HOST_OS
|
|
|
|
import System.FilePath
|
|
|
|
#endif
|
|
|
|
|
2018-10-13 05:36:06 +00:00
|
|
|
shellPath :: FilePath
|
|
|
|
shellPath = "/bin/sh"
|
2013-05-06 19:58:13 +00:00
|
|
|
|
2018-10-13 05:36:06 +00:00
|
|
|
shebang :: String
|
|
|
|
shebang = "#!" ++ shellPath
|
2016-09-05 16:09:23 +00:00
|
|
|
|
|
|
|
-- | On Windows, shebang is not handled by the kernel, so to support
|
|
|
|
-- shell scripts etc, have to look at the program being run and
|
|
|
|
-- parse it for shebang.
|
|
|
|
--
|
|
|
|
-- This has no effect on Unix.
|
|
|
|
findShellCommand :: FilePath -> IO (FilePath, [CommandParam])
|
|
|
|
findShellCommand f = do
|
|
|
|
#ifndef mingw32_HOST_OS
|
|
|
|
defcmd
|
|
|
|
#else
|
2017-03-13 20:20:52 +00:00
|
|
|
l <- catchDefaultIO Nothing $ headMaybe . lines <$> readFile f
|
2016-09-05 16:09:23 +00:00
|
|
|
case l of
|
|
|
|
Just ('#':'!':rest) -> case words rest of
|
|
|
|
[] -> defcmd
|
|
|
|
(c:ps) -> do
|
|
|
|
let ps' = map Param ps ++ [File f]
|
|
|
|
-- If the command is not inPath,
|
|
|
|
-- take the base of it, and run eg "sh"
|
|
|
|
-- which in some cases on windows will work
|
|
|
|
-- despite it not being inPath.
|
|
|
|
ok <- inPath c
|
|
|
|
return (if ok then c else takeFileName c, ps')
|
|
|
|
_ -> defcmd
|
|
|
|
#endif
|
|
|
|
where
|
|
|
|
defcmd = return (f, [])
|