 | HSH-1.2.6: Library to mix shell scripting with Haskell programs | Contents | Index |
|
HSH.Command | Portability | portable | Stability | provisional | Maintainer | John Goerzen <jgoerzen@complete.org> |
|
|
|
Description |
Copyright (c) 2006-2007 John Goerzen, jgoerzen@complete.org
|
|
Synopsis |
|
|
|
Documentation |
|
class Show a => ShellCommand a where |
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 each String
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.
| | Methods | fdInvoke | :: a | The command
| -> Fd | fd to pass to it as stdin
| -> Fd | fd to pass to it as stdout
| -> [Fd] | Fds to close in the child. Will not harm Fds this child needs for stdin and stdout.
| -> IO () | Action to run post-fork in child (or in main process if it doesn't fork)
| -> IO [InvokeResult] | Returns an action that, when evaluated, waits for the process to finish and returns an exit code.
| Invoke a command.
|
|
| | Instances | |
|
|
data PipeCommand a b |
Constructors | | Instances | |
|
|
(-|-) :: (ShellCommand a, ShellCommand b) => a -> b -> PipeCommand a b |
Pipe the output of the first command into the input of the second.
|
|
class RunResult a where |
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 ProcessStatus runs and returns a ProcessStatus with the exit
information. stdout is sent to stdout. Exceptions are not thrown.
- IO (String, ProcessStatus) is like IO ProcessStatus, 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, ProcessStatus)) 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, ProcessStatus)) 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.
| | Methods | run :: ShellCommand b => b -> a | Runs a command (or pipe of commands), with results presented
in any number of different ways.
|
| | Instances | |
|
|
run :: (RunResult a, ShellCommand b) => b -> a |
Runs a command (or pipe of commands), with results presented
in any number of different ways.
|
|
runIO :: ShellCommand a => a -> IO () |
A convenience function. Refers only to the version of run
that returns IO (). This prevents you from having to cast to it
all the time when you do not care about the result of run.
The implementation is simply:
runIO :: (ShellCommand a) => a -> IO ()
runIO = run
|
|
runSL :: ShellCommand a => a -> IO String |
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 ProcessStatus) |
Result type for shell commands. The String is the text description of
the command, not its output.
|
|
checkResults :: (String, ProcessStatus) -> IO () |
Evaluates result codes and raises an error for any bad ones it finds.
|
|
tryEC :: IO a -> IO (Either ProcessStatus a) |
Handle an exception derived from a program exiting abnormally
|
|
catchEC :: IO a -> (ProcessStatus -> IO a) -> IO a |
Catch an exception derived from a program exiting abnormally
|
|
Produced by Haddock version 0.8 |