<commit_before>\chapter{EventLoop module}

\begin{code}
{-# OPTIONS_GHC -cpp #-}
module System.Console.ShSh.EventLoop ( eventLoop, sourceProfile, source )
    where

import System.Console.ShSh.Command ( process )
import System.Console.ShSh.Expansions ( shellExpansions )
import System.Console.ShSh.IO ( ePutStrLn, oPutStrLn, oPutStr )
import System.Console.ShSh.Parse ( parseLine, Command(..) )
import System.Console.ShSh.Shell ( Shell, getEnv, getFlag, withHandler )
import System.Console.ShSh.Prompt ( prompt )
import System.IO ( hFlush, hIsEOF, openFile, IOMode(..),
                   stdin, stdout, stderr, hGetLine, Handle )
import System.Directory ( doesFileExist )
import System.FilePath ( (</>) )
import System.Exit ( ExitCode(..) )
import Control.Monad ( when )
import Control.Monad.Trans ( liftIO )

#ifdef HAVE_HASKELINE
import System.Console.Haskeline ( runInputT, getInputLine,
                                  defaultSettings, historyFile )
#endif

source :: FilePath -> Shell ()
source f = do h <- liftIO $ openFile f ReadMode
              eventLoop $ Just h

sourceProfile :: Shell ExitCode
sourceProfile = withHandler $ do dir <- getEnv "HOME"
                                 let file = dir </> ".shshrc"
                                 exists <- liftIO $ doesFileExist file
                                 when exists $ ePutStrLn ("Sourcing "++file) >>
                                               source file

eventLoop :: Maybe Handle -> Shell ()
eventLoop h = do
  s' <- case h of
          Nothing -> getInput =<< prompt
          Just h' -> getNoninteractiveInput h'
  case s' of
    Nothing -> return ()
    Just s  -> do am_e <- getFlag 'e'
                  am_v <- getFlag 'v'
                  if am_v then ePutStrLn s
                          else return ()
                  s' <- shellExpansions s
                  code <- case parseLine s' of -- Later, add more to s' (PS2)
                            Left e -> do ePutStrLn e
                                         return $ ExitFailure 1 -- ????
                            Right cmd -> process cmd
                  if am_e && code /= ExitSuccess
                    then fail ""
                    else eventLoop h

-- What do we want to do with history?  Bash defines a $HISTFILE variable,
-- but that only deals with saving the history on exit - apparently readline
-- takes care of saving things automatically on a per-session basis.
-- We certainly don't want to clobber ~/.bash_history.  But it would be
-- good to have a variable such as $HISTFILE to deal with this...
getInput :: String -> Shell (Maybe String)
#ifdef HAVE_HASKELINE
getInput prompt =
    do mhome <- getEnv "HOME"
       liftIO $ runInputT (settings mhome) (getInputLine prompt)
    where settings (Just home) = defaultSettings
                                 { historyFile = Just $ home++"/.shsh_history" }
          settings Nothing     = defaultSettings
#else
getInput prompt = do oPutStr prompt<commit_msg>Make shsh compile again (without any expansion support for now)<commit_after>\chapter{EventLoop module}

\begin{code}
{-# OPTIONS_GHC -cpp #-}
module System.Console.ShSh.EventLoop ( eventLoop, sourceProfile, source )
    where

import System.Console.ShSh.Command ( process )
-- import System.Console.ShSh.Expansions ( shellExpansions )
import System.Console.ShSh.IO ( ePutStrLn, oPutStrLn, oPutStr )
import System.Console.ShSh.Parse ( parseLine, Command(..) )
import System.Console.ShSh.Shell ( Shell, getEnv, getFlag, withHandler )
import System.Console.ShSh.Prompt ( prompt )
import System.IO ( hFlush, hIsEOF, openFile, IOMode(..),
                   stdin, stdout, stderr, hGetLine, Handle )
import System.Directory ( doesFileExist )
import System.FilePath ( (</>) )
import System.Exit ( ExitCode(..) )
import Control.Monad ( when )
import Control.Monad.Trans ( liftIO )

#ifdef HAVE_HASKELINE
import System.Console.Haskeline ( runInputT, getInputLine,
                                  defaultSettings, historyFile )
#endif

source :: FilePath -> Shell ()
source f = do h <- liftIO $ openFile f ReadMode
              eventLoop $ Just h

sourceProfile :: Shell ExitCode
sourceProfile = withHandler $ do dir <- getEnv "HOME"
                                 let file = dir </> ".shshrc"
                                 exists <- liftIO $ doesFileExist file
                                 when exists $ ePutStrLn ("Sourcing "++file) >>
                                               source file

eventLoop :: Maybe Handle -> Shell ()
eventLoop h = do
  s' <- case h of
          Nothing -> getInput =<< prompt
          Just h' -> getNoninteractiveInput h'
  case s' of
    Nothing -> return ()
    Just s  -> do am_e <- getFlag 'e'
                  am_v <- getFlag 'v'
                  if am_v then ePutStrLn s
                          else return ()
--                  s' <- shellExpansions s
                  code <- case parseLine s of -- Later, add more to s' (PS2)
                            Left e -> do ePutStrLn e
                                         return $ ExitFailure 1 -- ????
                            Right cmd -> process cmd
                  if am_e && code /= ExitSuccess
                    then fail ""
                    else eventLoop h

-- What do we want to do with history?  Bash defines a $HISTFILE variable,
-- but that only deals with saving the history on exit - apparently readline
-- takes care of saving things automatically on a per-session basis.
-- We certainly don't want to clobber ~/.bash_history.  But it would be
-- good to have a variable such as $HISTFILE to deal with this...
getInput :: String -> Shell (Maybe String)
#ifdef HAVE_HASKELINE
getInput prompt =
    do mhome <- getEnv "HOME"
       liftIO $ runInputT (settings mhome) (getInputLine prompt)
    where settings (Just home) = defaultSettings
                                 { historyFile = Just $ home++"/.shsh_history" }
          settings Nothing     = defaultSettings
#else
getInput prompt = do oPutStr prompt
{'fraction_non_alphanumeric': 0.0915282392026578, 'fraction_numerical': 0.000664451827242525, 'mean_word_length': 1.9101014983083615, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4647908', 'n_tokens_mistral': 1664, 'n_tokens_neox': 1606, 'n_words': 558}