Skip to content

Commit

Permalink
Track command errors for generating command exit code
Browse files Browse the repository at this point in the history
  • Loading branch information
glguy committed Jun 28, 2024
1 parent 2911de1 commit 76bd701
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
21 changes: 11 additions & 10 deletions cryptol/REPL/Haskeline.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
{-# LANGUAGE CPP #-}
{-# LANGUAGE MultiWayIf #-}
{-# LANGUAGE PatternGuards #-}
{-# LANGUAGE BangPatterns #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}

module REPL.Haskeline where
Expand Down Expand Up @@ -55,42 +56,42 @@ data ReplMode
crySession :: ReplMode -> Bool -> REPL CommandResult
crySession replMode stopOnError =
do settings <- io (setHistoryFile (replSettings isBatch))
let act = runInputTBehavior behavior settings (withInterrupt (loop 1))
let act = runInputTBehavior behavior settings (withInterrupt (loop True 1))
if isBatch then asBatch act else act
where
(isBatch,behavior) = case replMode of
InteractiveRepl -> (False, defaultBehavior)
Batch path -> (True, useFile path)
InteractiveBatch path -> (False, useFile path)

loop :: Int -> InputT REPL CommandResult
loop lineNum =
loop :: Bool -> Int -> InputT REPL CommandResult
loop !success !lineNum =
do ln <- getInputLines =<< MTL.lift getPrompt
case ln of
NoMoreLines -> return emptyCommandResult
NoMoreLines -> return emptyCommandResult { crSuccess = success }
Interrupted
| isBatch && stopOnError -> return emptyCommandResult { crSuccess = False }
| otherwise -> loop lineNum
| otherwise -> loop success lineNum
NextLine ls
| all (all isSpace) ls -> loop (lineNum + length ls)
| otherwise -> doCommand lineNum ls
| all (all isSpace) ls -> loop success (lineNum + length ls)
| otherwise -> doCommand success lineNum ls

run lineNum cmd =
case replMode of
InteractiveRepl -> runCommand lineNum Nothing cmd
InteractiveBatch _ -> runCommand lineNum Nothing cmd
Batch path -> runCommand lineNum (Just path) cmd

doCommand lineNum txt =
doCommand success lineNum txt =
case parseCommand findCommandExact (unlines txt) of
Nothing | isBatch && stopOnError -> return emptyCommandResult { crSuccess = False }
| otherwise -> loop (lineNum + length txt) -- say somtething?
| otherwise -> loop False (lineNum + length txt) -- say somtething?
Just cmd -> join $ MTL.lift $
do status <- handleInterrupt (handleCtrlC emptyCommandResult { crSuccess = False }) (run lineNum cmd)
case crSuccess status of
False | isBatch && stopOnError -> return (return status)
_ -> do goOn <- shouldContinue
return (if goOn then loop (lineNum + length txt) else return status)
return (if goOn then loop (crSuccess status && success) (lineNum + length txt) else return status)


data NextLine = NextLine [String] | NoMoreLines | Interrupted
Expand Down
1 change: 1 addition & 0 deletions src/Cryptol/REPL/Command.hs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ data CommandResult = CommandResult
, crValue :: Maybe String -- ^ value output for relevant commands
, crSuccess :: Bool -- ^ indicator that command successfully performed its task
}
deriving (Show)

emptyCommandResult :: CommandResult
emptyCommandResult = CommandResult
Expand Down

0 comments on commit 76bd701

Please sign in to comment.