{-# LANGUAGE RecordWildCards #-}
module System.Console.CmdArgs.Explicit.SplitJoin(splitArgs, joinArgs) where
import Data.Char
import Data.Maybe
-- | Given a sequence of arguments, join them together in a manner that could be used on-- the command line, giving preference to the Windows @cmd@ shell quoting conventions.---- For an alternative version, intended for actual running the result in a shell, see "System.Process.showCommandForUser"joinArgs :: [String] -> StringjoinArgs = unwords.mapf
where
fx = q++gx++q
where
hasSpace = anyisSpacexq = ['\"' | hasSpace||nullx]
g ('\\':'\"':xs) = '\\':'\\':'\\':'\"':gxsg"\\" | hasSpace = "\\\\"g ('\"':xs) = '\\':'\"':gxsg (x:xs) = x:gxsg [] = []
data State = Init-- either I just started, or just emitted something
| Norm-- I'm seeing characters
| Quot-- I've seen a quote-- | Given a string, split into the available arguments. The inverse of 'joinArgs'.splitArgs :: String -> [String]
splitArgs = join.fInit
where
-- Nothing is start a new string-- Just x is accumulate onto the existing stringjoin :: [MaybeChar] -> [String]
join [] = []
joinxs = mapfromJusta:join (drop1b)
where (a,b) = breakisNothingxsfInit (x:xs) | isSpacex = fInitxsfInit"\"\"" = [Nothing]
fInit"\"" = [Nothing]
fInitxs = fNormxsfm ('\"':'\"':'\"':xs) = Just'\"':fmxsfm ('\\':'\"':xs) = Just'\"':fmxsfm ('\\':'\\':'\"':xs) = Just'\\':fm ('\"':xs)
fNorm ('\"':xs) = fQuotxsfQuot ('\"':'\"':xs) = Just'\"':fNormxsfQuot ('\"':xs) = fNormxsfNorm (x:xs) | isSpacex = Nothing:fInitxsfm (x:xs) = Justx:fmxsfm [] = []