magic (empty) → 1.0.5
raw patch · 11 files changed
+478/−0 lines, 11 filesdep +basebuild-type:Customsetup-changed
Dependencies added: base
Files
- COPYING +26/−0
- COPYRIGHT +4/−0
- Magic.hs +35/−0
- Magic/Data.hsc +71/−0
- Magic/Init.hsc +58/−0
- Magic/Operations.hsc +102/−0
- Magic/Types.hsc +42/−0
- Magic/TypesLL.hs +26/−0
- Magic/Utils.hsc +77/−0
- Setup.lhs +7/−0
- magic.cabal +30/−0
+ COPYING view
@@ -0,0 +1,26 @@+Copyright (c) 2005 John Goerzen.+All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions+are met:+1. Redistributions of source code must retain the above copyright+ notice, this list of conditions and the following disclaimer.+2. 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.+3. Neither the name of John Goerzen nor the names of contributors+ may be used to endorse or promote products derived from this software+ without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
+ COPYRIGHT view
@@ -0,0 +1,4 @@+Haskell libmagic Interface+Copyright (C) 2005 John Goerzen <jgoerzen@complete.org>++This code is under a 3-clause BSD license; see COPYING for details.
+ Magic.hs view
@@ -0,0 +1,35 @@+{- -*- Mode: haskell; -*-+Haskell Magic Interface+Copyright (C) 2005 John Goerzen <jgoerzen@complete.org>++This code is under a 3-clause BSD license; see COPYING for details.+-}++{- |+ Module : Magic+ Copyright : Copyright (C) 2005 John Goerzen+ License : BSD++ Maintainer : John Goerzen,+ Maintainer : jgoerzen\@complete.org+ Stability : provisional+ Portability: portable++Top-level Magic module.++Written by John Goerzen, jgoerzen\@complete.org++Foo bar+-}++module Magic (-- * Basic Types+ module Magic.Types,+ -- * Initialization+ module Magic.Init,+ -- * Operation+ module Magic.Operations+ )+where+import Magic.Types+import Magic.Init+import Magic.Operations
+ Magic/Data.hsc view
@@ -0,0 +1,71 @@+-- AUTO-GENERATED FILE, DO NOT EDIT. GENERATED BY utils/genconsts.hs+{- |+ Module : Magic.Data+ Copyright : Copyright (C) 2005 John Goerzen+ License : BSD++ Maintainer : John Goerzen,+ Maintainer : jgoerzen\@complete.org+ Stability : provisional+ Portability: portable++Haskell types for libmagic constants++Written by John Goerzen, jgoerzen\@complete.org+-}++module Magic.Data (module Magic.Data) where++#include "magic.h"+++data MagicFlag =+ MagicNone+ | MagicDebug+ | MagicSymlink+ | MagicCompress+ | MagicDevices+ | MagicMime+ | MagicContinue+ | MagicCheck+ | MagicPreserveAtime+ | MagicRaw+ | MagicError+ | UnknownMagicFlag Int++ deriving (Show)++instance Enum MagicFlag where+ toEnum (#{const MAGIC_NONE}) = MagicNone+ toEnum (#{const MAGIC_DEBUG}) = MagicDebug+ toEnum (#{const MAGIC_SYMLINK}) = MagicSymlink+ toEnum (#{const MAGIC_COMPRESS}) = MagicCompress+ toEnum (#{const MAGIC_DEVICES}) = MagicDevices+ toEnum (#{const MAGIC_MIME}) = MagicMime+ toEnum (#{const MAGIC_CONTINUE}) = MagicContinue+ toEnum (#{const MAGIC_CHECK}) = MagicCheck+ toEnum (#{const MAGIC_PRESERVE_ATIME}) = MagicPreserveAtime+ toEnum (#{const MAGIC_RAW}) = MagicRaw+ toEnum (#{const MAGIC_ERROR}) = MagicError+ toEnum x = UnknownMagicFlag x++ fromEnum MagicNone = (#{const MAGIC_NONE})+ fromEnum MagicDebug = (#{const MAGIC_DEBUG})+ fromEnum MagicSymlink = (#{const MAGIC_SYMLINK})+ fromEnum MagicCompress = (#{const MAGIC_COMPRESS})+ fromEnum MagicDevices = (#{const MAGIC_DEVICES})+ fromEnum MagicMime = (#{const MAGIC_MIME})+ fromEnum MagicContinue = (#{const MAGIC_CONTINUE})+ fromEnum MagicCheck = (#{const MAGIC_CHECK})+ fromEnum MagicPreserveAtime = (#{const MAGIC_PRESERVE_ATIME})+ fromEnum MagicRaw = (#{const MAGIC_RAW})+ fromEnum MagicError = (#{const MAGIC_ERROR})+ fromEnum (UnknownMagicFlag x) = x++instance Ord MagicFlag where+ compare x y = compare (fromEnum x) (fromEnum y)++instance Eq MagicFlag where+ x == y = (fromEnum x) == (fromEnum y)++
+ Magic/Init.hsc view
@@ -0,0 +1,58 @@+{- -*- Mode: haskell; -*-+Haskell magic Interface+Copyright (C) 2005 John Goerzen <jgoerzen@complete.org>++This code is under a 3-clause BSD license; see COPYING for details.+-}++{- |+ Module : Magic.Init+ Copyright : Copyright (C) 2005 John Goerzen+ License : BSD++ Maintainer : John Goerzen,+ Maintainer : jgoerzen\@complete.org+ Stability : provisional+ Portability: portable++Initialization and shutdown for magic programs++Written by John Goerzen, jgoerzen\@complete.org+-}++module Magic.Init(magicOpen, magicLoad, magicLoadDefault)+where++import Foreign.Ptr+import Foreign.C.String+import Magic.Types+import Foreign.C.Types+import Magic.Utils+import Magic.TypesLL+import Foreign.Marshal.Utils++{- | Create a Magic object. You must call either 'magicLoadDefault'+or 'magicLoad' after this.+-}+magicOpen :: [MagicFlag] -> IO Magic+magicOpen mfl =+ fromMagicPtr "magicOpen" (magic_open flags)+ where flags = flaglist2int mfl++{- | Load the system's default magic database. -}+magicLoadDefault :: Magic -> IO ()+magicLoadDefault m = withMagicPtr m (\cmagic ->+ checkIntError "magicLoadDefault" m $ magic_load cmagic nullPtr)++{- | Load the specified magic database(s). The given string may contain+multiple colon-separated pathnames. -}+magicLoad :: Magic -> String -> IO ()+magicLoad m s = withMagicPtr m (\cmagic ->+ withCString s (\cs ->+ checkIntError "magicLoad" m $ magic_load cmagic cs))+ +foreign import ccall unsafe "magic.h magic_open"+ magic_open :: CInt -> IO (Ptr CMagic)++foreign import ccall unsafe "magic.h magic_load"+ magic_load :: Ptr CMagic -> CString -> IO CInt
+ Magic/Operations.hsc view
@@ -0,0 +1,102 @@+{- -*- Mode: haskell; -*-+Haskell magic Interface+Copyright (C) 2005 John Goerzen <jgoerzen@complete.org>++This code is under a 3-clause BSD license; see COPYING for details.+-}++{- |+ Module : Magic.Init+ Copyright : Copyright (C) 2005 John Goerzen+ License : BSD++ Maintainer : John Goerzen,+ Maintainer : jgoerzen\@complete.org+ Stability : provisional+ Portability: portable++Initialization and shutdown for magic programs++Written by John Goerzen, jgoerzen\@complete.org+-}++module Magic.Operations(-- * Guessing the type+ magicFile, magicStdin,+ magicString, magicCString,+ -- * Other operations+ magicSetFlags, magicCompile)+where++import Foreign.Ptr+import Foreign.C.String+import Magic.Types+import Foreign.C.Types+import Data.Word+import Foreign.C.String+import Foreign.C.Error+import Magic.Utils+import Magic.TypesLL+import Foreign.Marshal.Utils++{- | Calls the Magic system on the specified file. -}+magicFile :: Magic -> FilePath -> IO String+magicFile magic fp =+ withMagicPtr magic (\cmagic ->+ withCString fp (\cfp ->+ do res <- throwErrorIfNull "magicFile" magic (magic_file cmagic cfp)+ peekCString res+ )+ )++{- | Calls the Magic system on stdin. -}+magicStdin :: Magic -> IO String+magicStdin magic =+ withMagicPtr magic (\cmagic ->+ do res <- throwErrorIfNull "magicStdin" magic (magic_file cmagic nullPtr)+ peekCString res+ )++{- | Calls the Magic system to process the given String. Please note:+it is not evaluated lazily. -}+magicString :: Magic -> String -> IO String+magicString m s = withCStringLen s (magicCString m)++{- | Lower-level function used to call the Magic system to process a C +string. -}+magicCString :: Magic -> CStringLen -> IO String+magicCString magic (cstr, len) =+ withMagicPtr magic (\cmagic ->+ do res <- throwErrorIfNull "magicCString" magic (magic_buffer cmagic cstr (fromIntegral len))+ peekCString res+ )++{- | Change the flags on an already-created object. -}+magicSetFlags :: Magic -> [MagicFlag] -> IO ()+magicSetFlags m mfl = withMagicPtr m (\cmagic ->+ checkIntError "magicSetFlags" m $ magic_setflags cmagic flags)+ where flags = flaglist2int mfl++{- | Compile the colon-separated list of database file(s). The compiled files+created have .mgc added to the names of the argument.+-}+magicCompile :: Magic -- ^ Object to use+ -> Maybe String -- ^ Colon separated list of databases, or Nothing for default+ -> IO ()+magicCompile m mstr = withMagicPtr m (\cm ->+ case mstr of+ Nothing -> worker cm nullPtr+ Just x -> withCString x (worker cm)+ )+ where worker cm cs = checkIntError "magicCompile" m $ magic_compile cm cs++foreign import ccall unsafe "magic.h magic_file"+ magic_file :: Ptr CMagic -> CString -> IO CString++foreign import ccall unsafe "magic.h magic_buffer"+ magic_buffer :: Ptr CMagic -> CString -> #{type size_t} -> IO CString++foreign import ccall unsafe "magic.h magic_setflags"+ magic_setflags :: Ptr CMagic -> CInt -> IO CInt++foreign import ccall unsafe "magic.h magic_compile"+ magic_compile :: Ptr CMagic -> CString -> IO CInt
+ Magic/Types.hsc view
@@ -0,0 +1,42 @@+{- -*- Mode: haskell; -*-+Haskell magic Interface+Copyright (C) 2005 John Goerzen <jgoerzen@complete.org>++This code is under a 3-clause BSD license; see COPYING for details.+-}++{- |+ Module : Magic.Types+ Copyright : Copyright (C) 2005 John Goerzen+ License : BSD++ Maintainer : John Goerzen,+ Maintainer : jgoerzen\@complete.org+ Stability : provisional+ Portability: portable++Types for magic programs.++Written by John Goerzen, jgoerzen\@complete.org+-}++module Magic.Types(Magic,+ MagicFlag(..))+where+import Foreign.Ptr+import Data.Word+import Data.Int+import Foreign.C.Types+import Foreign.ForeignPtr+import Magic.Data+import Magic.TypesLL++#include <magic.h>++{- | Main Magic object type.++Magic objects are automatically closed (and memory freed) when they are+garbage-collected by Haskell. There is no need to explicitly close them.+-}+type Magic = ForeignPtr CMagic+
+ Magic/TypesLL.hs view
@@ -0,0 +1,26 @@+{- -*- Mode: haskell; -*-+Haskell magic Interface+Copyright (C) 2005 John Goerzen <jgoerzen@complete.org>++This code is under a 3-clause BSD license; see COPYING for details.+-}++{- |+ Module : Magic.TypesLL+ Copyright : Copyright (C) 2005 John Goerzen+ License : BSD++ Maintainer : John Goerzen,+ Maintainer : jgoerzen\@complete.org+ Stability : provisional+ Portability: portable++Low-Types for magic programs.++Written by John Goerzen, jgoerzen\@complete.org+-}++module Magic.TypesLL(CMagic)+where++data CMagic
+ Magic/Utils.hsc view
@@ -0,0 +1,77 @@+{- -*- Mode: haskell; -*-+Haskell magic Interface+Copyright (C) 2005 John Goerzen <jgoerzen@complete.org>++This code is under a 3-clause BSD license; see COPYING for details.+-}++{- |+ Module : Magic.Utils+ Copyright : Copyright (C) 2005 John Goerzen+ License : BSD++ Maintainer : John Goerzen,+ Maintainer : jgoerzen\@complete.org+ Stability : provisional+ Portability: portable++Utils++Written by John Goerzen, jgoerzen\@complete.org+-}++module Magic.Utils (flaglist2int, fromMagicPtr, withMagicPtr, checkIntError,+ throwErrorIfNull)+where++import Foreign+import Foreign.C.Error+import Foreign.C.String+import Foreign.ForeignPtr+import Magic.TypesLL+import Magic.Types+import Data.Bits+import Foreign.C.Types+import Magic.Data++flaglist2int :: [MagicFlag] -> CInt+flaglist2int mfl =+ foldl (\c f -> c .|. (fromIntegral . fromEnum $ f)) 0 mfl++fromMagicPtr :: String -> IO (Ptr CMagic) -> IO Magic+fromMagicPtr caller action =+ do ptr <- throwErrnoIfNull caller action+ newForeignPtr magic_close ptr++throwErrorIfNull :: String -> Magic -> IO (Ptr a) -> IO (Ptr a)+throwErrorIfNull caller m action =+ do res <- action+ if res == nullPtr+ then throwError caller m+ else return res++withMagicPtr :: Magic -> (Ptr CMagic -> IO a) -> IO a+withMagicPtr m = withForeignPtr m++throwError :: String -> Magic -> IO a+throwError caller m = withMagicPtr m (\cmagic ->+ do errormsg <- magic_error cmagic+ if errormsg /= nullPtr+ then do em <- peekCString errormsg+ fail $ caller ++ ": " ++ em+ else fail $ caller ++ ": got error code but no error message"+ )++checkIntError :: String -> Magic -> IO CInt -> IO ()+checkIntError caller m action = + do res <- action+ if res == 0+ then return ()+ else throwError caller m+++foreign import ccall unsafe "magic.h &magic_close"+ magic_close :: FunPtr (Ptr CMagic -> IO ())++foreign import ccall unsafe "magic.h magic_error"+ magic_error :: Ptr CMagic -> IO CString
+ Setup.lhs view
@@ -0,0 +1,7 @@+#!/usr/bin/env runhugs+arch-tag: Main setup script++> import Distribution.Simple++> main = defaultMain+
+ magic.cabal view
@@ -0,0 +1,30 @@+Extra-Libraries: magic+-- End detected settings section. Everything below here should not+-- need editing.+Name: magic+Version: 1.0.5+License: GPL+Maintainer: John Goerzen <jgoerzen@complete.org>+Author: John Goerzen+Stability: Alpha+Copyright: Copyright (c) 2005-2006 John Goerzen+license-file: COPYRIGHT+extra-source-files: COPYING+Category: Text+Synopsis: Interface to C file/magic library+Description: This package provides a Haskell interface to the C libmagic library.+ With it, you can determine the type of a file by examining its contents+ rather than its name. The Haskell interface provides a full-featured+ binding.+-- C-Sources: glue/glue.c+Exposed-Modules: Magic,+ Magic.Data,+ Magic.Types,+ Magic.Init,+ Magic.Operations+Other-Modules: Magic.Utils,+ Magic.TypesLL+Build-Depends: base+GHC-Options: -O2+-- CC-Options: -Iglue+Extensions: ForeignFunctionInterface, TypeSynonymInstances