% MINERVA (c) IF Computer 1996,2002 % http://www.ifcomputer.com/MINERVA mailto:support@ifcomputer.com % exec.min execute an asynchronous process :- package(runtime). :- package(process). % ex exec(ls) % ex exec('Mail -s test me@work',ExitCode,[hello,you],OutputLines,ErrorLines) exec(Commandline) :- exec(Commandline,_ExitCode,_Lines,_ErrorLines). exec(Commandline,ExitCode) :- exec(Commandline,ExitCode,_InputLines,_OutputLines,_ErrorLines). exec(Commandline,ExitCode,InputLines) :- exec(Commandline,ExitCode,InputLines,_OutputLines,_ErrorLines). exec(Commandline,ExitCode,InputLines,OutputLines) :- exec(Commandline,ExitCode,InputLines,OutputLines,_ErrorLines). % exec(+Commandline,-ExitCode,+InputLines,-OutputLines,-ErrorLines) :- exec(Commandline,ExitCode,InputLines,OutputLines,ErrorLines) :- runtime_getRuntime(Runtime), runtime_exec(Runtime, Commandline,Process), process_getInputStream(Process, InputStream), process_getOutputStream(Process, OutputStream), process_getErrorStream(Process, ErrorInputStream), open(InputStream,read,FromCmd,[stream]), open(ErrorInputStream,read,ErrorsFromCmd,[stream]), open(OutputStream,write,ToCmd,[stream]), '_writeLines'(ToCmd,InputLines),flush_output(ToCmd), close(ToCmd), '_readLines'(FromCmd,OutputLines), '_readLines'(ErrorsFromCmd,ErrorLines), process_waitFor(Process, _WaitCode), process_exitValue(Process, ExitCode), close(FromCmd), close(ErrorsFromCmd), % writeq(info(InputLines,OutputLines,ErrorLines,ExitCode)),nl,flush_output, true. % ex exec_files('Mail -s test me@work',ExitCode,'exec.min','out.log','error.log') % exec_files(+Commandline,-ExitCode,+InputFile,+OutputFile,+ErrorFile) :- exec_files(Commandline,ExitCode,InputFile,OutputFile,ErrorFile) :- open(InputFile,read,Input), '_readLines'(Input,InputLines), exec(Commandline,ExitCode,InputLines,OutputLines,ErrorLines), open(OutputFile,write,Output), open(ErrorFile,write,Error), '_writeLines'(Output,OutputLines), '_writeLines'(Error,ErrorLines). % '_readLines'(+Stream,-Lines) <- '_readLines'(Stream,[Line|Lines]) :- get_line(Stream,Line), !, '_readLines'(Stream,Lines). '_readLines'(_, []). % '_writeLines'(+Stream,+Lines) <- '_writeLines'(_Stream,[]). '_writeLines'(Stream,[Line|Lines]) :- write(Stream,Line),nl(Stream), '_writeLines'(Stream,Lines).