Movatterモバイル変換


[0]ホーム

URL:


{-# LANGUAGE ScopedTypeVariables #-}------------------------------------------------------------------------------- |-- Module      :  GHC.ResponseFile-- License     :  BSD-style (see the file LICENSE)---- Maintainer  :  libraries@haskell.org-- Stability   :  internal-- Portability :  portable---- GCC style response files.---- @since 4.12.0.0------------------------------------------------------------------------------ Migrated from Haddock.moduleGHC.ResponseFile(getArgsWithResponseFiles,unescapeArgs,escapeArgs,expandResponse)whereimportControl.ExceptionimportData.Char(isSpace)importData.Foldable(foldl')importSystem.Environment(getArgs)importSystem.Exit(exitFailure)importSystem.IO{-|Like 'getArgs', but can also read arguments supplied via response files.For example, consider a program @foo@:@main :: IO ()main = do  args <- getArgsWithResponseFiles  putStrLn (show args)@And a response file @args.txt@:@--one 1--'two' 2--"three" 3@Then the result of invoking @foo@ with @args.txt@ is:> > ./foo @args.txt> ["--one","1","--two","2","--three","3"]-}getArgsWithResponseFiles::IO[String]getArgsWithResponseFiles=getArgs>>=expandResponse-- | Given a string of concatenated strings, separate each by removing-- a layer of /quoting/ and\/or /escaping/ of certain characters.---- These characters are: any whitespace, single quote, double quote,-- and the backslash character.  The backslash character always-- escapes (i.e., passes through without further consideration) the-- character which follows.  Characters can also be escaped in blocks-- by quoting (i.e., surrounding the blocks with matching pairs of-- either single- or double-quotes which are not themselves escaped).---- Any whitespace which appears outside of either of the quoting and-- escaping mechanisms, is interpreted as having been added by this-- special concatenation process to designate where the boundaries-- are between the original, un-concatenated list of strings.  These-- added whitespace characters are removed from the output.---- > unescapeArgs "hello\\ \\\"world\\\"\n" == escapeArgs "hello \"world\""unescapeArgs::String->[String]unescapeArgs=filter(not.null).unescape-- | Given a list of strings, concatenate them into a single string-- with escaping of certain characters, and the addition of a newline-- between each string.  The escaping is done by adding a single-- backslash character before any whitespace, single quote, double-- quote, or backslash character, so this escaping character must be-- removed.  Unescaped whitespace (in this case, newline) is part-- of this "transport" format to indicate the end of the previous-- string and the start of a new string.---- While 'unescapeArgs' allows using quoting (i.e., convenient-- escaping of many characters) by having matching sets of single- or-- double-quotes,'escapeArgs' does not use the quoting mechasnism,-- and thus will always escape any whitespace, quotes, and-- backslashes.---- > unescapeArgs "hello\\ \\\"world\\\"\\n" == escapeArgs "hello \"world\""escapeArgs::[String]->StringescapeArgs=unlines.mapescapeArg-- | Arguments which look like '@foo' will be replaced with the-- contents of file @foo@. A gcc-like syntax for response files arguments-- is expected.  This must re-constitute the argument list by doing an-- inverse of the escaping mechanism done by the calling-program side.---- We quit if the file is not found or reading somehow fails.-- (A convenience routine for haddock or possibly other clients)expandResponse::[String]->IO[String]expandResponse=fmapconcat.mapMexpandwhereexpand::String->IO[String]expand('@':f)=readFileExcf>>=return.unescapeArgsexpandx=return[x]readFileExcf=readFilef`catch`\(e::IOException)->dohPutStrLnstderr$"Error while expanding response file: "++showeexitFailuredataQuoting=NoneQ|SngQ|DblQunescape::String->[String]unescapeargs=reverse.mapreverse$goargsNoneQFalse[][]where-- n.b., the order of these cases matters; these are cribbed from gcc-- case 1: end of inputgo[]_q_bsaas=a:as-- case 2: back-slash escape in progressgo(c:cs)qTrueaas=gocsqFalse(c:a)as-- case 3: no back-slash escape in progress, but got a back-slashgo(c:cs)qFalseaas|'\\'==c=gocsqTrueaas-- case 4: single-quote escaping in progressgo(c:cs)SngQFalseaas|'\''==c=gocsNoneQFalseaas|otherwise=gocsSngQFalse(c:a)as-- case 5: double-quote escaping in progressgo(c:cs)DblQFalseaas|'"'==c=gocsNoneQFalseaas|otherwise=gocsDblQFalse(c:a)as-- case 6: no escaping is in progressgo(c:cs)NoneQFalseaas|isSpacec=gocsNoneQFalse[](a:as)|'\''==c=gocsSngQFalseaas|'"'==c=gocsDblQFalseaas|otherwise=gocsNoneQFalse(c:a)asescapeArg::String->StringescapeArg=reverse.foldl'escape[]escape::String->Char->Stringescapecsc|isSpacec||'\\'==c||'\''==c||'"'==c=c:'\\':cs-- n.b., our caller must reverse the result|otherwise=c:cs

[8]ページ先頭

©2009-2025 Movatter.jp