1
0
forked from GitHub/gf-core

move gfLibraryPath and gfGrammarPath to UseIO. Now they are accessible and there is only one place to change if you want to use different envVar

This commit is contained in:
krasimir
2008-04-22 12:12:29 +00:00
parent e16215940e
commit 2a7c68ad65
5 changed files with 40 additions and 39 deletions

View File

@@ -60,9 +60,6 @@ import Control.Monad
import System.Directory import System.Directory
import System.FilePath import System.FilePath
-- | environment variable for grammar search path
gfGrammarPathVar = "GF_GRAMMAR_PATH"
-- | in batch mode: write code in a file -- | in batch mode: write code in a file
batchCompile f = liftM fst $ compileModule defOpts emptyShellState f batchCompile f = liftM fst $ compileModule defOpts emptyShellState f
where where
@@ -115,7 +112,7 @@ compileModule opts1 st0 file = do
let ps1 = if (useFileOpt && not useLineOpt) let ps1 = if (useFileOpt && not useLineOpt)
then (ps0 ++ map (combine fpath) ps0) then (ps0 ++ map (combine fpath) ps0)
else ps0 else ps0
ps <- ioeIO $ extendPathEnv gfLibraryPath gfGrammarPathVar ps1 ps <- ioeIO $ extendPathEnv ps1
let ioeIOIf = if oElem beVerbose opts then ioeIO else (const (return ())) let ioeIOIf = if oElem beVerbose opts then ioeIO else (const (return ()))
ioeIOIf $ putStrLn $ "module search path:" +++ show ps ---- ioeIOIf $ putStrLn $ "module search path:" +++ show ps ----
let st = st0 --- if useFileOpt then emptyShellState else st0 let st = st0 --- if useFileOpt then emptyShellState else st0
@@ -396,7 +393,7 @@ getGFEFiles opts1 file = useIOE [] $ do
let ps1 = if (useFileOpt && not useLineOpt) let ps1 = if (useFileOpt && not useLineOpt)
then (map (combine fpath) ps0) then (map (combine fpath) ps0)
else ps0 else ps0
ps <- ioeIO $ extendPathEnv gfLibraryPath gfGrammarPathVar ps1 ps <- ioeIO $ extendPathEnv ps1
let file' = if useFileOpt then takeFileName file else file -- to find file itself let file' = if useFileOpt then takeFileName file else file -- to find file itself
files <- getAllFiles opts ps [] file' files <- getAllFiles opts ps [] file'
efiles <- ioeIO $ filterM doesFileExist [replaceExtension f "gfe" | f <- files] efiles <- ioeIO $ filterM doesFileExist [replaceExtension f "gfe" | f <- files]

View File

@@ -48,9 +48,6 @@ prMod :: SourceModule -> String
prMod = compactPrint . prModule prMod = compactPrint . prModule
-- | environment variable for grammar search path
gfGrammarPathVar = "GF_GRAMMAR_PATH"
-- | the environment -- | the environment
type CompileEnv = (Int,SourceGrammar) type CompileEnv = (Int,SourceGrammar)
@@ -71,7 +68,7 @@ compileModule opts1 env file = do
let ps1 = if (useFileOpt && not useLineOpt) let ps1 = if (useFileOpt && not useLineOpt)
then (ps0 ++ map (combine fpath) ps0) then (ps0 ++ map (combine fpath) ps0)
else ps0 else ps0
ps <- ioeIO $ extendPathEnv gfLibraryPath gfGrammarPathVar ps1 ps <- ioeIO $ extendPathEnv ps1
let ioeIOIf = if oElem beVerbose opts then ioeIO else (const (return ())) let ioeIOIf = if oElem beVerbose opts then ioeIO else (const (return ()))
ioeIOIf $ putStrLn $ "module search path:" +++ show ps ---- ioeIOIf $ putStrLn $ "module search path:" +++ show ps ----
let sgr = snd env let sgr = snd env

View File

@@ -44,9 +44,6 @@ intermOut opts opt s =
prMod :: SourceModule -> String prMod :: SourceModule -> String
prMod = prModule prMod = prModule
-- | environment variable for grammar search path
gfGrammarPathVar = "GF_GRAMMAR_PATH"
-- | the environment -- | the environment
type CompileEnv = (Int,GF) type CompileEnv = (Int,GF)
@@ -67,7 +64,7 @@ compileModule opts1 env file = do
let ps1 = if (useFileOpt && not useLineOpt) let ps1 = if (useFileOpt && not useLineOpt)
then (ps0 ++ map (combine fpath) ps0) then (ps0 ++ map (combine fpath) ps0)
else ps0 else ps0
ps <- ioeIO $ extendPathEnv gfLibraryPath gfGrammarPathVar ps1 ps <- ioeIO $ extendPathEnv ps1
let ioeIOIf = if oElem beVerbose opts then ioeIO else (const (return ())) let ioeIOIf = if oElem beVerbose opts then ioeIO else (const (return ()))
ioeIOIf $ putStrLn $ "module search path:" +++ show ps ---- ioeIOIf $ putStrLn $ "module search path:" +++ show ps ----
let sgr = snd env let sgr = snd env

View File

@@ -122,10 +122,13 @@ doesFileExistPath paths file = do
mpfile <- ioeIO $ getFilePathMsg "" paths file mpfile <- ioeIO $ getFilePathMsg "" paths file
return $ maybe False (const True) mpfile return $ maybe False (const True) mpfile
gfLibraryPath = "GF_LIB_PATH"
gfGrammarPathVar = "GF_GRAMMAR_PATH"
getLibraryPath :: IO FilePath getLibraryPath :: IO FilePath
getLibraryPath = getLibraryPath =
catch catch
(getEnv "GF_LIB_PATH") (getEnv gfLibraryPath)
#ifdef mingw32_HOST_OS #ifdef mingw32_HOST_OS
(\_ -> do exepath <- getModuleFileName nullPtr (\_ -> do exepath <- getModuleFileName nullPtr
let (path,_) = splitFileName exepath let (path,_) = splitFileName exepath
@@ -134,23 +137,26 @@ getLibraryPath =
(const (return libdir)) (const (return libdir))
#endif #endif
-- | first var is lib prefix, second is like class path -- | extends the search path with the
-- | path in environment variable has lower priority -- 'gfLibraryPath' and 'gfGrammarPathVar'
extendPathEnv :: String -> String -> [FilePath] -> IO [FilePath] -- environment variables. Returns only existing paths.
extendPathEnv lib var ps = do extendPathEnv :: [FilePath] -> IO [FilePath]
extendPathEnv ps = do
b <- getLibraryPath -- e.g. GF_LIB_PATH b <- getLibraryPath -- e.g. GF_LIB_PATH
s <- catch (getEnv var) (const (return "")) -- e.g. GF_GRAMMAR_PATH s <- catch (getEnv gfGrammarPathVar) (const (return "")) -- e.g. GF_GRAMMAR_PATH
let ss = ps ++ splitSearchPath s let ss = ps ++ splitSearchPath s
liftM concat $ mapM allSubdirs $ ss ++ [b </> s | s <- ss ++ ["prelude"]] liftM concat $ mapM allSubdirs $ ss ++ [b </> s | s <- ss ++ ["prelude"]]
where where
allSubdirs :: FilePath -> IO [FilePath] allSubdirs :: FilePath -> IO [FilePath]
allSubdirs [] = return [[]] allSubdirs [] = return [[]]
allSubdirs p = case last p of allSubdirs p = case last p of
'*' -> do '*' -> do let path = init p
let path = init p fs <- getSubdirs path
fs <- getSubdirs path return [path </> f | f <- fs]
return [path </> f | f <- fs] _ -> do exists <- doesDirectoryExist p
_ -> return [p] if exists
then return [p]
else return []
getSubdirs :: FilePath -> IO [FilePath] getSubdirs :: FilePath -> IO [FilePath]
getSubdirs dir = do getSubdirs dir = do
@@ -278,8 +284,6 @@ putPointEgen cond opts msg act = do
putPointEVerb :: Options -> String -> IOE a -> IOE a putPointEVerb :: Options -> String -> IOE a -> IOE a
putPointEVerb opts = putPointE (addOption beVerbose opts) putPointEVerb opts = putPointE (addOption beVerbose opts)
gfLibraryPath = "GF_LIB_PATH"
-- ((do {s <- readFile f; return (return s)}) ) -- ((do {s <- readFile f; return (return s)}) )
readFileIOE :: FilePath -> IOE BS.ByteString readFileIOE :: FilePath -> IOE BS.ByteString
readFileIOE f = ioe $ catch (BS.readFile f >>= return . return) readFileIOE f = ioe $ catch (BS.readFile f >>= return . return)

View File

@@ -126,10 +126,15 @@ doesFileExistPath paths file = do
mpfile <- ioeIO $ getFilePathMsg "" paths file mpfile <- ioeIO $ getFilePathMsg "" paths file
return $ maybe False (const True) mpfile return $ maybe False (const True) mpfile
gfLibraryPath = "GF_LIB_PATH"
-- | environment variable for grammar search path
gfGrammarPathVar = "GF_GRAMMAR_PATH"
getLibraryPath :: IO FilePath getLibraryPath :: IO FilePath
getLibraryPath = getLibraryPath =
catch catch
(getEnv "GF_LIB_PATH") (getEnv gfLibraryPath)
#ifdef mingw32_HOST_OS #ifdef mingw32_HOST_OS
(\_ -> do exepath <- getModuleFileName nullPtr (\_ -> do exepath <- getModuleFileName nullPtr
let (path,_) = splitFileName exepath let (path,_) = splitFileName exepath
@@ -138,23 +143,26 @@ getLibraryPath =
(const (return libdir)) (const (return libdir))
#endif #endif
-- | first var is lib prefix, second is like class path -- | extends the search path with the
-- | path in environment variable has lower priority -- 'gfLibraryPath' and 'gfGrammarPathVar'
extendPathEnv :: String -> String -> [FilePath] -> IO [FilePath] -- environment variables. Returns only existing paths.
extendPathEnv lib var ps = do extendPathEnv :: [FilePath] -> IO [FilePath]
extendPathEnv ps = do
b <- getLibraryPath -- e.g. GF_LIB_PATH b <- getLibraryPath -- e.g. GF_LIB_PATH
s <- catch (getEnv var) (const (return "")) -- e.g. GF_GRAMMAR_PATH s <- catch (getEnv gfGrammarPathVar) (const (return "")) -- e.g. GF_GRAMMAR_PATH
let ss = ps ++ splitSearchPath s let ss = ps ++ splitSearchPath s
liftM concat $ mapM allSubdirs $ ss ++ [b </> s | s <- ss ++ ["prelude"]] liftM concat $ mapM allSubdirs $ ss ++ [b </> s | s <- ss ++ ["prelude"]]
where where
allSubdirs :: FilePath -> IO [FilePath] allSubdirs :: FilePath -> IO [FilePath]
allSubdirs [] = return [[]] allSubdirs [] = return [[]]
allSubdirs p = case last p of allSubdirs p = case last p of
'*' -> do '*' -> do let path = init p
let path = init p fs <- getSubdirs path
fs <- getSubdirs path return [path </> f | f <- fs]
return [path </> f | f <- fs] _ -> do exists <- doesDirectoryExist p
_ -> return [p] if exists
then return [p]
else return []
getSubdirs :: FilePath -> IO [FilePath] getSubdirs :: FilePath -> IO [FilePath]
getSubdirs dir = do getSubdirs dir = do
@@ -291,8 +299,6 @@ putPointE opts msg act = do
putPointEVerb :: Options -> String -> IOE a -> IOE a putPointEVerb :: Options -> String -> IOE a -> IOE a
putPointEVerb opts = putPointE (addOption beVerbose opts) putPointEVerb opts = putPointE (addOption beVerbose opts)
gfLibraryPath = "GF_LIB_PATH"
-- ((do {s <- readFile f; return (return s)}) ) -- ((do {s <- readFile f; return (return s)}) )
readFileIOE :: FilePath -> IOE (String) readFileIOE :: FilePath -> IOE (String)
readFileIOE f = ioe $ catch (readFileStrict f >>= return . return) readFileIOE f = ioe $ catch (readFileStrict f >>= return . return)