Factor Language Blog

Improvements to io.launcher

Friday, January 25, 2008

Factor’s integration with the host operating system just keeps on getting better. I already blogged about Factor’s awesome process launcher library and today it got even better.

First of all, run-process and run-detached return an instance of process. These objects can be passed to wait-for-process which waits for the process to exit and returns the exit code.

Unlike before, waiting for a process to exit does not block the entire Factor VM, instead process exit notifications are hooked into the I/O event queue provided by our Windows and Unix “native I/O” support.

Second, while we already have flexible input/output redirection in the form of <process-stream>, redirecting input or output to a file was a bit bothersome; you had to start a thread which would read from the file and write to the pipe, or vice versa. Now, there’s a new feature for redirecting input and output directly to files:

H{
    { +command+ "ls /etc" }
    { +stdout+ "listing.txt" }
} run-process

Similarly, you can provide +stderr+ or +stdin+ keys. The value +closed+ may also be given.

Of course, if you have a program which needs to munge the output of a process in some way, or send it commands generated on the fly, you can still use a pipe, but for the common use-case of redirecting to files, having direct support is not only easier on the programmer but more efficient.

Finally, you can mix the above redirection features with <process-stream>. For example,

H{
    { +command+ "sort" }
    { +stdin+ "unsorted-data.txt" }
} <process-stream> lines

This starts the Unix “sort” command; it will take standard input from unsorted-data.txt, and standard output will be sent to your Factor process via a pipe. The lines word reads lines of text from the pipe until EOF.

As usual, both new features work and have been tested on both Unix and Windows. To me, it is important that Factor is not a second-class citizen on Windows.

Cool stuff!