This project allows to run OS sub processes from a Pharo image. It uses GLib IO library to spawn processes through FFI calls. SubProcess offers a high-level API, OS-agnostic API to run easily processes from your Pharo code. Windows, Linux and Mac Os are supported!
WARNING: For now, there is only support for synchroneous processes (i.e. the execution will wait the spawed process termination). Asynchroneous support will come a bit later. WARNING 2: fetching of the exit code may not be reliable (TO FIX!)
process := SPSProcessConfiguration new
command: '/bin/ls';
sync.
process run.
process := SPSProcessConfiguration new
command: 'C:\Windows\System32\systeminfo.exe';
sync.
process run.
process := SPSProcessConfiguration new
command: '/bin/ls';
sync.
process run.
out := process stdOut.
err := process stdErr.
process := SPSProcessConfiguration new
workingDirectory: '/etc';
command: '/bin/ls';
sync.
process run.
process := SPSProcessConfiguration new
command: '/bin/ls';
arguments: #('/etc');
sync.
process run.
process := SPSProcessConfiguration new
workingDirectory: 'C:\';
windowsShellCommand;
addArgument: 'dir';
sync.
process run.
process := SPSProcessConfiguration new
command: '/bin/ls';
sync.
process run.
self assert: process isComplete.
process := SPSProcessConfiguration new
command: '/bin/ls';
sync.
process run.
self assert: process isSuccess.
process := SPSProcessConfiguration new
command: '/bin/ls';
sync.
[ process run ]
on: SPSError
do: [ :error | error messageText inspect ]
When running a command that will give you back some output (standard output or standard error), you will get an encoded String (a byte array) that needs to be decoded. SubProcess cannot guess what will be the encoding as many encodings are used worldwide. One commonly used encoding is utf-8 on unix-like systems. On Windows, different encondings are used.
SubProcess configure a default encoding (utf-8
on unix-like systems and cp-850
on Windows) for convenience. Do not forget you could need a different encoding. If so, you can configure it before running the process:
process := SPSProcessConfiguration new
encoding: 'ISO-8859-2'
command: '/bin/ls';
sync.
process run.
out := process stdOut.