% pipeout - start shell command and return stream for writing to its standard input
% pipeout :: 
%    text ~'bash shell command',
%    bool ~'quiet operation flag'
% -> java.io.OutputStream ~ 'connected to stdout of command',
%    (void -> action void) ~'call this to clean up afterwards'.
%
% If quiet flag is not supplied, it defaults to false.
function [str,cleanup]=pipeout(cmd,q)
	if nargin<2, q=false; end
	if ~q, fprintf('Starting sub-process: %s\n',cmd); end

	cs=feature('DefaultCharacterSet');
	process=java.lang.Runtime.getRuntime().exec('bash');
	writer=java.io.OutputStreamWriter(process.getOutputStream(),cs);
	writer.write(cmd); writer.close();

	str=process.getOutputStream();
	cleanup=@dispose;

	function dispose
		if ~q, fprintf('Killing subprocess...\n'); end
		process.destroy();
	end
end
