summaryrefslogtreecommitdiff
path: root/Utility.hs
diff options
context:
space:
mode:
authorGravatar Joey Hess <joey@kitenet.net>2010-12-31 20:33:43 -0400
committerGravatar Joey Hess <joey@kitenet.net>2010-12-31 20:33:43 -0400
commit5c29bb3b7c280b3e2db26dbcb38f063430f731d6 (patch)
treef996fc2565d3198f7aaebb29264bf8658fb79196 /Utility.hs
parente153a116bb45eac409a7d4b0a07c5ba10634bd36 (diff)
git-annex-shell can now be used as a login shell
Diffstat (limited to 'Utility.hs')
-rw-r--r--Utility.hs29
1 files changed, 28 insertions, 1 deletions
diff --git a/Utility.hs b/Utility.hs
index 3a6c75751..4ab5f0930 100644
--- a/Utility.hs
+++ b/Utility.hs
@@ -14,9 +14,13 @@ module Utility (
relPathDirToDir,
boolSystem,
shellEscape,
+ shellUnEscape,
unsetFileMode,
readMaybe,
- safeWriteFile
+ safeWriteFile,
+
+ prop_idempotent_shellescape,
+ prop_idempotent_shellescape_multiword
) where
import System.IO
@@ -128,6 +132,29 @@ shellEscape f = "'" ++ escaped ++ "'"
-- replace ' with '"'"'
escaped = join "'\"'\"'" $ split "'" f
+{- Unescapes a set of shellEscaped words or filenames. -}
+shellUnEscape :: String -> [String]
+shellUnEscape [] = []
+shellUnEscape s = word:(shellUnEscape rest)
+ where
+ (word, rest) = findword "" s
+ findword w [] = (w, "")
+ findword w (c:cs)
+ | c == ' ' = (w, cs)
+ | c == '\'' = inquote c w cs
+ | c == '"' = inquote c w cs
+ | otherwise = findword (w++[c]) cs
+ inquote _ w [] = (w, "")
+ inquote q w (c:cs)
+ | c == q = findword w cs
+ | otherwise = inquote q (w++[c]) cs
+
+{- For quickcheck. -}
+prop_idempotent_shellescape :: String -> Bool
+prop_idempotent_shellescape s = [s] == (shellUnEscape $ shellEscape s)
+prop_idempotent_shellescape_multiword :: [String] -> Bool
+prop_idempotent_shellescape_multiword s = s == (shellUnEscape $ unwords $ map shellEscape s)
+
{- Removes a FileMode from a file.
- For example, call with otherWriteMode to chmod o-w -}
unsetFileMode :: FilePath -> FileMode -> IO ()