wordexp (empty) → 0.1.0.0
raw patch · 5 files changed
+194/−0 lines, 5 filesdep +arraydep +basesetup-changed
Dependencies added: array, base
Files
- LICENSE +30/−0
- Setup.hs +2/−0
- src/System/Wordexp.chs +119/−0
- src/System/Wordexp/Simple.hs +20/−0
- wordexp.cabal +23/−0
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c) 2013, Matvey Aksenov++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++ * Redistributions of source code must retain the above copyright+ notice, this list of conditions and the following disclaimer.++ * Redistributions in binary form must reproduce the above+ copyright notice, this list of conditions and the following+ disclaimer in the documentation and/or other materials provided+ with the distribution.++ * Neither the name of Matvey Aksenov nor the names of other+ contributors may be used to endorse or promote products derived+ from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ src/System/Wordexp.chs view
@@ -0,0 +1,119 @@+{-# LANGUAGE DeriveDataTypeable #-}+{-# LANGUAGE EmptyDataDecls #-}+{-# LANGUAGE ForeignFunctionInterface #-}+{-# LANGUAGE GeneralizedNewtypeDeriving #-}+-- | wordexp (and wordfree) Haskell wrapper+module System.Wordexp+ ( -- * Wrapper+ wordexp+ -- * Flags+ , Flags, nosubst, errors, noundef+ -- * Errors+ , WordexpError (..)+ ) where++import Control.Exception (Exception)+import Control.Monad+import Data.Data (Data)+import Data.Monoid (Monoid(..))+import Data.Typeable (Typeable)+import Foreign+import Foreign.C+import Foreign.C.Types++import Data.Array (Ix)++#include <wordexp.h>+++-- | Opaque wordexp_t struct image in Haskellland+data Wordexp+++-- | wordexp flags enum image in Haskellland+{# enum define FLAGS+ { WRDE_APPEND as WRDE_APPEND+ , WRDE_DOOFFS as WRDE_DOOFFS+ , WRDE_NOCMD as WRDE_NOCMD+ , WRDE_REUSE as WRDE_REUSE+ , WRDE_SHOWERR as WRDE_SHOWERR+ , WRDE_UNDEF as WRDE_UNDEF+ } deriving (Show, Read, Eq, Ord, Bounded, Ix, Data, Typeable) #}++-- | wordexp flags+--+-- Not every flag is supported since some of them do not make much sense in Haskell anyway+newtype Flags = Flags Int+#if defined(__GLASGOW_HASKELL__) && (__GLASGOW_HASKELL__ >= 706)+ deriving (Show, Read, Eq, Ord, Bounded, Bits, Ix, Data, Typeable)+#else+ deriving (Show, Read, Eq, Ord, Bounded, Num, Bits, Ix, Data, Typeable)+#endif++instance Monoid Flags where+ mempty = Flags 0+ a `mappend` b = a .|. b+ {-# INLINE mappend #-}++-- | Disable command substitution in patterns, treat them as errors+nosubst :: Flags+nosubst = Flags $ fromEnum WRDE_NOCMD+{-# INLINE nosubst #-}++-- | Do not hide shell error messages in /dev/null, print them right away+errors :: Flags+errors = Flags $ fromEnum WRDE_SHOWERR+{-# INLINE errors #-}++-- | Do not accept undefined shell variables, treat them as errors+noundef :: Flags+noundef = Flags $ fromEnum WRDE_UNDEF+{-# INLINE noundef #-}+++-- | Possible wordexp errors+{# enum define WordexpError+ { WRDE_NOSPACE as OutOfSpace+ , WRDE_BADCHAR as IllegalCharacterOccurence+ , WRDE_BADVAL as UndefinedShellVariable+ , WRDE_CMDSUB as CommandSubstitution+ , WRDE_SYNTAX as ShellSyntaxError+ } deriving (Show, Read, Eq, Ord, Bounded, Ix, Data, Typeable) #}++instance Exception WordexpError+++-- | wordexp wrapper+--+-- Allows to specify desired flags, return expanded strings or encountered error if any+wordexp :: Flags -> String -> IO (Either WordexpError [String])+wordexp (Flags f) s =+ withCString s $ \cs ->+ allocaBytes size $ \p -> do+ ret <- c_wordexp cs p (fromIntegral f)+ case ret of+ 0 -> do+ c <- fromIntegral `fmap` wordc p+ v <- wordv p+ xs <- forM [0 .. c-1] $ peekElemOff v >=> peekCString+ c_wordfree p+ return $ Right xs+ e -> return . Left . toEnum $ fromIntegral e+ where+ size = sizeOf (undefined :: CSize) + sizeOf (undefined :: Ptr CString) + sizeOf (undefined :: CSize)+ {-# INLINE size #-}++ wordc :: Ptr Wordexp -> IO CSize+ wordc p = peekByteOff p 0+ {-# INLINE wordc #-}++ wordv :: Ptr Wordexp -> IO (Ptr CString)+ wordv p = peekByteOff p (sizeOf (undefined :: CSize))+ {-# INLINE wordv #-}+++foreign import ccall unsafe "wordexp"+ c_wordexp :: CString -> Ptr Wordexp -> CInt -> IO CInt++foreign import ccall unsafe "wordfree"+ c_wordfree :: Ptr Wordexp -> IO ()
+ src/System/Wordexp/Simple.hs view
@@ -0,0 +1,20 @@+-- | Simple wordexp (and wordfree) Haskell wrapper+module System.Wordexp.Simple+ ( -- * Simple wrapper+ wordexp+ , -- * Exceptions+ W.WordexpError (..)+ ) where++import Control.Exception (throw)+import Data.Monoid (mempty)++import qualified System.Wordexp as W+++-- | Simple wordexp wrapper+--+-- Return expanded strings or throw an exception if any error is encountered+wordexp :: String -> IO [String]+wordexp s = either throw id `fmap` W.wordexp mempty s+{-# INLINE wordexp #-}
+ wordexp.cabal view
@@ -0,0 +1,23 @@+name: wordexp+version: 0.1.0.0+synopsis: wordexp(3) wrappers+description: man wordexp+license: BSD3+license-file: LICENSE+author: Matvey Aksenov+maintainer: matvey.aksenov@gmail.com+category: System+build-type: Simple+cabal-version: >= 1.8++library+ build-depends: base >= 4 && < 5,+ array+ hs-source-dirs: src+ exposed-modules: System.Wordexp+ System.Wordexp.Simple+ build-tools: c2hs++source-repository head+ type: git+ location: https://github.com/supki/wordexp