“Shell Scripting” with SML Kevin Jones Friday, May 13, 2005
Target Functionality Piping between process Branched pipes Echoing output from a a process to a file or standard out along with the next process Basic file handling Simplified environment and argument handling Allow all exceptions without to be caught outside of the structure
Changes in CML Added functionality to Unix structure signature UNIX = sig type proc val executeInEnv : string * string list * string list -> proc val execute : string * string list -> proc val pidOf : proc -> Posix.Process.pid val pipeOf : proc -> Posix.IO.file_desc * Posix.IO.file_desc val streamsOf : proc -> TextIO.instream * TextIO.outstream val mkPipe : string -> TextIO.instream * TextIO.outstream val reapEvt : proc -> Posix.Process.exit_status CML.event val reap : proc -> Posix.Process.exit_status val kill : proc * Posix.Signal.signal -> unit end
SCRIPTML Signature signature SCRIPTML = sig type environment type file datatype pipe = Pipe of (string * string list * environment * bool * (string * bool) option) list * output and output = Branch of pipe list | StdOut | FileAppend of string | FileOverwrite of string val start : string * string list * string list * (string * string list * environment -> unit) -> OS.Process.status val set : environment * (string * string) list -> environment val get : environment -> string -> string option val makeFile : string -> file option val openFile : string -> file option val touchFile : string -> bool val closeFile : file -> unit val readFile : string -> string list val write : file -> string -> unit val pipe : pipe -> unit end;
pipe datatype Complete path to the program Arguments for the program The environment to run it in If it should be echoed to standard out A file the output would be echoed to and if it should overwrite the file if it exists
Argument handling Arguments can be read and put in the environment testProgram -var1=value1 -var2 value2 All of the arguments get passed to the function even if they are added to the environment
Demonstrations
Possible Improvements Some programs seem to not work correctly, notably grep and maybe ls Special cases could be made to handle useful programs that do not work Add support for stream processing functions between processes Thread safety in output to files in pipes