Portability | portable |
---|---|
Stability | provisional |
Maintainer | John Goerzen <jgoerzen@complete.org> |
Copyright (c) 2006-2009 John Goerzen, jgoerzen@complete.org
- type Environment = Maybe [(String, String)]
- class Show a => ShellCommand a where
- fdInvoke :: a -> Environment -> Channel -> IO (Channel, [InvokeResult])
- data (ShellCommand a, ShellCommand b) => PipeCommand a b = PipeCommand a b
- (-|-) :: (ShellCommand a, ShellCommand b) => a -> b -> PipeCommand a b
- class RunResult a where
- run :: ShellCommand b => b -> a
- runIO :: ShellCommand a => a -> IO ()
- runSL :: ShellCommand a => a -> IO String
- type InvokeResult = (String, IO ExitCode)
- checkResults :: (String, ExitCode) -> IO ()
- tryEC :: IO a -> IO (Either ExitCode a)
- catchEC :: IO a -> (ExitCode -> IO a) -> IO a
- setenv :: ShellCommand cmd => [(String, String)] -> cmd -> EnvironCommand cmd
- unsetenv :: ShellCommand cmd => [String] -> cmd -> EnvironCommand cmd
Documentation
type Environment = Maybe [(String, String)]Source
Type for the environment.
class Show a => ShellCommand a whereSource
A shell command is something we can invoke, pipe to, pipe from, or pipe in both directions. All commands that can be run as shell commands must define these methods.
Minimum implementation is fdInvoke
.
Some pre-defined instances include:
- A simple bare string, which is passed to the shell for execution. The shell will then typically expand wildcards, parse parameters, etc.
- A
(String, [String])
tuple. The first item in the tuple gives the name of a program to run, and the second gives its arguments. The shell is never involved. This is ideal for passing filenames, since there is no security risk involving special shell characters. - A
Handle -> Handle -> IO ()
function, which reads from the first handle and write to the second. - Various functions. These functions will accept input representing its standard input and output will go to standard output.
Some pre-defined instance functions include:
-
(String -> String)
,(String -> IO String)
, plus the same definitions for ByteStrings. -
([String] -> [String])
,([String] -> IO [String])
, where eachString
in the list represents a single line -
(() -> String)
,(() -> IO String)
, for commands that explicitly read no input. Useful with closures. Useful when you want to avoid reading stdin because something else already is. These have the unit as part of the function because otherwise we would have conflicts with things such as bare Strings, which represent a command name.
:: a | The command |
-> Environment | The environment |
-> Channel | Where to read input from |
-> IO (Channel, [InvokeResult]) | Returns an action that, when evaluated, waits for the process to finish and returns an exit code. |
Invoke a command.
ShellCommand String | An instance of |
ShellCommand a => ShellCommand (EnvironCommand a) | |
ShellCommand ([String] -> [String]) | An instance of A [String] is generated from a Handle via the So, this function is intended to operate upon lines of input and produce lines of output. |
ShellCommand ([String] -> IO [String]) | The same for an IO function |
ShellCommand (String -> IO String) | |
ShellCommand (String -> String) | An instance of |
ShellCommand (() -> [String]) | |
ShellCommand (() -> IO [String]) | |
ShellCommand (() -> IO String) | A user function that takes no input, and generates output. We will deal with it using hPutStr to send the output on. |
ShellCommand (() -> IO ByteString) | |
ShellCommand (() -> IO ByteString) | |
ShellCommand (() -> String) | |
ShellCommand (() -> ByteString) | |
ShellCommand (() -> ByteString) | |
ShellCommand (ByteString -> IO ByteString) | |
ShellCommand (ByteString -> ByteString) | |
ShellCommand (ByteString -> IO ByteString) | |
ShellCommand (ByteString -> ByteString) | |
ShellCommand (Channel -> IO Channel) | |
ShellCommand (String, [String]) | An instance of |
(ShellCommand a, ShellCommand b) => ShellCommand (PipeCommand a b) | An instance of |
data (ShellCommand a, ShellCommand b) => PipeCommand a b Source
PipeCommand a b |
(ShellCommand a, ShellCommand b) => Show (PipeCommand a b) | |
(ShellCommand a, ShellCommand b) => ShellCommand (PipeCommand a b) | An instance of |
(-|-) :: (ShellCommand a, ShellCommand b) => a -> b -> PipeCommand a bSource
Pipe the output of the first command into the input of the second.
Different ways to get data from run
.
- IO () runs, throws an exception on error, and sends stdout to stdout
- IO String runs, throws an exception on error, reads stdout into a buffer, and returns it as a string. Note: This output is not lazy.
- IO [String] is same as IO String, but returns the results as lines. Note: this output is not lazy.
- IO ExitCode runs and returns an ExitCode with the exit information. stdout is sent to stdout. Exceptions are not thrown.
- IO (String, ExitCode) is like IO ExitCode, but also includes a description of the last command in the pipe to have an error (or the last command, if there was no error).
- IO ByteString and are similar to their String counterparts.
- IO (String, IO (String, ExitCode)) returns a String read lazily and an IO action that, when evaluated, finishes up the process and results in its exit status. This command returns immediately.
- IO (IO (String, ExitCode)) sends stdout to stdout but returns
immediately. It forks off the child but does not wait for it to finish.
You can use
checkResults
to wait for the finish. - IO Int returns the exit code from a program directly. If a signal caused the command to be reaped, returns 128 + SIGNUM.
- IO Bool returns True if the program exited normally (exit code 0, not stopped by a signal) and False otherwise.
To address insufficient laziness, you can process anything that needs to be processed lazily within the pipeline itself.
run :: ShellCommand b => b -> aSource
Runs a command (or pipe of commands), with results presented in any number of different ways.
RunResult (IO Bool) | |
RunResult (IO Int) | |
RunResult (IO [String]) | |
RunResult (IO (IO (String, ExitCode))) | |
RunResult (IO String) | |
RunResult (IO ()) | |
RunResult (IO (String, IO (String, ExitCode))) | |
RunResult (IO (String, ExitCode)) | |
RunResult (IO (ByteString, IO (String, ExitCode))) | |
RunResult (IO (ByteString, IO (String, ExitCode))) | |
RunResult (IO ExitCode) | |
RunResult (IO ByteString) | |
RunResult (IO ByteString) |
runIO :: ShellCommand a => a -> IO ()Source
runSL :: ShellCommand a => a -> IO StringSource
Another convenience function. This returns the first line of the output, with any trailing newlines or whitespace stripped off. No leading whitespace is stripped. This function will raise an exception if there is not at least one line of output. Mnemonic: runSL means "run single line".
This command exists separately from run
because there is already a
run
instance that returns a String, though that instance returns the
entirety of the output in that String.
type InvokeResult = (String, IO ExitCode)Source
Result type for shell commands. The String is the text description of the command, not its output.
checkResults :: (String, ExitCode) -> IO ()Source
Evaluates result codes and raises an error for any bad ones it finds.
tryEC :: IO a -> IO (Either ExitCode a)Source
Handle an exception derived from a program exiting abnormally
catchEC :: IO a -> (ExitCode -> IO a) -> IO aSource
Catch an exception derived from a program exiting abnormally
setenv :: ShellCommand cmd => [(String, String)] -> cmd -> EnvironCommand cmdSource
Sets an environment variable, replacing an existing one if it exists.
Here's a sample ghci session to illustrate. First, let's see the defaults for some variables:
Prelude HSH> runIO $ "echo $TERM, $LANG" xterm, en_US.UTF-8
Now, let's set one:
Prelude HSH> runIO $ setenv [("TERM", "foo")] $ "echo $TERM, $LANG" foo, en_US.UTF-8
Or two:
Prelude HSH> runIO $ setenv [("TERM", "foo")] $ setenv [("LANG", "de_DE.UTF-8")] $ "echo $TERM, $LANG" foo, de_DE.UTF-8
We could also do it easier, like this:
Prelude HSH> runIO $ setenv [("TERM", "foo"), ("LANG", "de_DE.UTF-8")] $ "echo $TERM, $LANG" foo, de_DE.UTF-8
It can be combined with unsetenv:
Prelude HSH> runIO $ setenv [("TERM", "foo")] $ unsetenv ["LANG"] $ "echo $TERM, $LANG" foo,
And used with pipes:
Prelude HSH> runIO $ setenv [("TERM", "foo")] $ "echo $TERM, $LANG" -|- "tr a-z A-Z" FOO, EN_US.UTF-8
See also unsetenv
.
unsetenv :: ShellCommand cmd => [String] -> cmd -> EnvironCommand cmdSource
Removes an environment variable if it exists; does nothing otherwise.
See also setenv
, which has a more extensive example.