Slava,
A common requirement when interfacing to command-line programs is to
capture stdout, stderr, and the exit status. In particular, it's
sometimes necessary to have the data sent to stdout and stderr converted
to "lines", i.e. an array of strings.
I don't know of a (good) easy way to do this using what 'io.launcher'
provides. This is what prompted the '<process-stdout-stderr-reader>'
word I wrote about recently.
I think a good way to approach needing stdout, stderr, and status, is to
keep them in a tuple of some sort. Well, it just so happens that the
'process' tuple has slots for those. :-) I "abuse" this in 'git-tool'. A
"proper" approach might involve having a 'process-result' tuple.
The word (from git-tool) which implements the above functionality is
(hey look, no locals! :-) ) :
: run-process/result ( desc -- process )
<process-stdout-stderr-reader>
{
[ contents [ string-lines ] [ f ] if* ]
[ contents [ string-lines ] [ f ] if* ]
}
parallel-spread
[ >>stdout ] [ >>stderr ] bi*
dup wait-for-process >>status ;
To make this concrete, here's an example usage. I'm using 'ls' and
referencing a non-existent file, as well as an existing one:
{ "ls" "/etc/inittab" "/xyz" } run-process/result .
The resulting 'process' object looks like this:
T{ process
{ command { "ls" "/etc/inittab" "/xyz" } }
{ environment H{ } }
{ environment-mode +append-environment+ }
{ stdout { "/etc/inittab" "" } }
{ stderr
{
"ls: cannot access /xyz: No such file or directory"
""
}
}
{ status 2 }
}
I think this idiom should be directly supported by 'io.launcher'. I'm
sure others will want to do this eventually. We should nail it down now.
Ed
------------------------------------------------------------------------------
This SF.net email is sponsored by:
SourcForge Community
SourceForge wants to tell your story.
http://p.sf.net/sfu/sf-spreadtheword
_______________________________________________
Factor-talk mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/factor-talk