From ed0663aac942113a693e225dbacaa69784017976 Mon Sep 17 00:00:00 2001 From: Sibi Prabakaran Date: Tue, 19 May 2020 11:37:19 +0530 Subject: Add EOF check before getLine operation from stdin Fixes https://github.com/jaor/xmobar/issues/442 --- src/Xmobar/Plugins/StdinReader.hs | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/Xmobar/Plugins/StdinReader.hs b/src/Xmobar/Plugins/StdinReader.hs index bc4bb5f..8c0c0bf 100644 --- a/src/Xmobar/Plugins/StdinReader.hs +++ b/src/Xmobar/Plugins/StdinReader.hs @@ -22,21 +22,31 @@ import Prelude import System.Posix.Process import System.Exit import System.IO -import Control.Exception (SomeException(..), handle) import Xmobar.Run.Exec import Xmobar.X11.Actions (stripActions) +import Xmobar.System.Utils (onSomeException) data StdinReader = StdinReader | UnsafeStdinReader deriving (Read, Show) instance Exec StdinReader where start stdinReader cb = do - s <- handle (\(SomeException e) -> do hPrint stderr e; return "") getLine - cb $ escape stdinReader s + -- The EOF check is necessary for certain systems + -- More details here https://github.com/jaor/xmobar/issues/442 eof <- isEOF if eof - then exitImmediately ExitSuccess - else start stdinReader cb + then do + hPrint stderr $ "xmobar: eof at an early stage" + exitImmediately ExitSuccess + else return () + s <- + getLine `onSomeException` + (\e -> do + let errorMessage = "xmobar: Received exception " <> show e + hPrint stderr errorMessage + cb errorMessage) + cb $ escape stdinReader s + start stdinReader cb escape :: StdinReader -> String -> String escape StdinReader = stripActions -- cgit v1.2.3