GF shell: restore the eh command to working order and document it

Also, when the command line parser fails, append the problematic command line
to the error message "command not parsed".
This commit is contained in:
hallgren
2015-08-18 13:13:31 +00:00
parent ee5e103653
commit d1cf9d734e
4 changed files with 39 additions and 18 deletions

View File

@@ -71,6 +71,11 @@ commonCommands = fmap (mapCommandExec liftSIO) $ Map.fromList [
longname = "empty", longname = "empty",
synopsis = "empty the environment" synopsis = "empty the environment"
}), }),
("eh", emptyCommandInfo {
longname = "execute_history",
syntax = "eh FILE",
synopsis = "read commands from a file and execute them"
}),
("ph", emptyCommandInfo { ("ph", emptyCommandInfo {
longname = "print_history", longname = "print_history",
synopsis = "print command history", synopsis = "print command history",

View File

@@ -27,7 +27,7 @@ interpretCommandLine env line =
case readCommandLine line of case readCommandLine line of
Just [] -> return () Just [] -> return ()
Just pipes -> mapM_ (interpretPipe env) pipes Just pipes -> mapM_ (interpretPipe env) pipes
Nothing -> putStrLnE "command not parsed" Nothing -> putStrLnE $ "command not parsed: "++line
interpretPipe env cs = do interpretPipe env cs = do
Piped v@(_,s) <- intercs cs void Piped v@(_,s) <- intercs cs void

View File

@@ -13,7 +13,7 @@ import GF.Command.Help(helpCommand)
import GF.Command.Abstract import GF.Command.Abstract
import GF.Command.Parse(readCommandLine,pCommand) import GF.Command.Parse(readCommandLine,pCommand)
import GF.Data.Operations (Err(..),done) import GF.Data.Operations (Err(..),done)
import GF.Data.Utilities(repeatM) import GF.Data.Utilities(whenM,repeatM)
import GF.Grammar hiding (Ident,isPrefixOf) import GF.Grammar hiding (Ident,isPrefixOf)
import GF.Infra.UseIO(ioErrorText,putStrLnE) import GF.Infra.UseIO(ioErrorText,putStrLnE)
import GF.Infra.SIO import GF.Infra.SIO
@@ -113,17 +113,21 @@ type ShellM = StateT GFEnv SIO
-- | Execute a given command line, returning 'True' to continue execution, -- | Execute a given command line, returning 'True' to continue execution,
-- | 'False' when it is time to quit -- | 'False' when it is time to quit
execute1 :: String -> ShellM Bool execute1, execute1' :: String -> ShellM Bool
execute1 s0 = execute1 s0 =
do modify $ \ gfenv0 -> gfenv0 {history = s0 : history gfenv0} do modify $ \ gfenv0 -> gfenv0 {history = s0 : history gfenv0}
opts <- gets startOpts execute1' s0
-- | Execute a given command line, without adding it to the history
execute1' s0 =
do opts <- gets startOpts
interruptible $ optionallyShowCPUTime opts $ interruptible $ optionallyShowCPUTime opts $
case pwords s0 of case pwords s0 of
-- cc, sd, so, ss and dg are now in GF.Commands.SourceCommands -- cc, sd, so, ss and dg are now in GF.Commands.SourceCommands
-- special commands -- special commands
"q" :_ -> quit "q" :_ -> quit
"!" :ws -> system_command ws "!" :ws -> system_command ws
"eh":ws -> eh ws "eh":ws -> execute_history ws
"i" :ws -> do import_ ws; continue "i" :ws -> do import_ ws; continue
-- other special commands, working on GFEnv -- other special commands, working on GFEnv
"dc":ws -> define_command ws "dc":ws -> define_command ws
@@ -157,11 +161,15 @@ execute1 s0 =
cs <- readFile w >>= return . map words . lines cs <- readFile w >>= return . map words . lines
gfenv' <- foldM (flip (process False benv)) gfenv cs gfenv' <- foldM (flip (process False benv)) gfenv cs
loopNewCPU gfenv' -} loopNewCPU gfenv' -}
eh [w] = -- Ehhh? Reads commands from a file, but does not execute them execute_history [w] =
do env <- gets commandenv do execute . lines =<< lift (restricted (readFile w))
cs <- lift $ restricted (readFile w) >>= return . map (interpretCommandLine env) . lines
continue continue
eh _ = do putStrLnE "eh command not parsed" where
execute [] = done
execute (line:lines) = whenM (execute1' line) (execute lines)
execute_history _ =
do putStrLnE "eh command not parsed"
continue continue
define_command (f:ws) = define_command (f:ws) =

View File

@@ -11,7 +11,7 @@ import GF.Command.Help(helpCommand)
import GF.Command.Abstract import GF.Command.Abstract
import GF.Command.Parse(readCommandLine,pCommand) import GF.Command.Parse(readCommandLine,pCommand)
import GF.Data.Operations (Err(..),done) import GF.Data.Operations (Err(..),done)
import GF.Data.Utilities(repeatM) import GF.Data.Utilities(whenM,repeatM)
import GF.Infra.UseIO(ioErrorText,putStrLnE) import GF.Infra.UseIO(ioErrorText,putStrLnE)
import GF.Infra.SIO import GF.Infra.SIO
@@ -117,14 +117,18 @@ type ShellM = StateT GFEnv SIO
execute1 :: String -> ShellM Bool execute1 :: String -> ShellM Bool
execute1 s0 = execute1 s0 =
do modify $ \ gfenv0 -> gfenv0 {history = s0 : history gfenv0} do modify $ \ gfenv0 -> gfenv0 {history = s0 : history gfenv0}
opts <- gets startOpts execute1' s0
-- | Execute a given command line, without adding it to the history
execute1' s0 =
do opts <- gets startOpts
interruptible $ optionallyShowCPUTime opts $ interruptible $ optionallyShowCPUTime opts $
case pwords s0 of case pwords s0 of
-- cc, sd, so, ss and dg are now in GF.Commands.SourceCommands -- cc, sd, so, ss and dg are now in GF.Commands.SourceCommands
-- special commands -- special commands
"q" :_ -> quit "q" :_ -> quit
"!" :ws -> system_command ws "!" :ws -> system_command ws
"eh":ws -> eh ws "eh":ws -> execute_history ws
"i" :ws -> do import_ ws; continue "i" :ws -> do import_ ws; continue
-- other special commands, working on GFEnv -- other special commands, working on GFEnv
"dc":ws -> define_command ws "dc":ws -> define_command ws
@@ -158,11 +162,15 @@ execute1 s0 =
cs <- readFile w >>= return . map words . lines cs <- readFile w >>= return . map words . lines
gfenv' <- foldM (flip (process False benv)) gfenv cs gfenv' <- foldM (flip (process False benv)) gfenv cs
loopNewCPU gfenv' -} loopNewCPU gfenv' -}
eh [w] = -- Ehhh? Reads commands from a file, but does not execute them execute_history [w] =
do env <- gets commandenv do execute . lines =<< lift (restricted (readFile w))
cs <- lift $ restricted (readFile w) >>= return . map (interpretCommandLine env) . lines
continue continue
eh _ = do putStrLnE "eh command not parsed" where
execute [] = done
execute (line:lines) = whenM (execute1' line) (execute lines)
execute_history _ =
do putStrLnE "eh command not parsed"
continue continue
define_command (f:ws) = define_command (f:ws) =