Improved io.launcher
parent
a81a3387bf
commit
e1ace82429
|
@ -1,13 +1,49 @@
|
||||||
! Copyright (C) 2007 Slava Pestov.
|
! Copyright (C) 2007 Slava Pestov.
|
||||||
! See http://factorcode.org/license.txt for BSD license.
|
! See http://factorcode.org/license.txt for BSD license.
|
||||||
USING: io.backend system ;
|
USING: io.backend system kernel namespaces strings hashtables
|
||||||
|
sequences assocs ;
|
||||||
IN: io.launcher
|
IN: io.launcher
|
||||||
|
|
||||||
HOOK: run-process io-backend ( string -- )
|
SYMBOL: +command+
|
||||||
|
SYMBOL: +arguments+
|
||||||
|
SYMBOL: +detached+
|
||||||
|
SYMBOL: +environment+
|
||||||
|
SYMBOL: +environment-mode+
|
||||||
|
|
||||||
HOOK: run-detached io-backend ( string -- )
|
SYMBOL: prepend-environment
|
||||||
|
SYMBOL: replace-environment
|
||||||
|
SYMBOL: append-environment
|
||||||
|
|
||||||
HOOK: <process-stream> io-backend ( string -- stream )
|
: default-descriptor
|
||||||
|
H{
|
||||||
|
{ +command+ f }
|
||||||
|
{ +arguments+ f }
|
||||||
|
{ +detached+ f }
|
||||||
|
{ +environment+ H{ } }
|
||||||
|
{ +environment-mode+ append-environment }
|
||||||
|
} ;
|
||||||
|
|
||||||
|
: with-descriptor ( desc quot -- )
|
||||||
|
default-descriptor [ >r clone r> bind ] bind ; inline
|
||||||
|
|
||||||
|
GENERIC: >descriptor ( obj -- desc )
|
||||||
|
|
||||||
|
M: string >descriptor +command+ associate ;
|
||||||
|
M: sequence >descriptor +arguments+ associate ;
|
||||||
|
M: assoc >descriptor ;
|
||||||
|
|
||||||
|
HOOK: run-process* io-backend ( desc -- )
|
||||||
|
|
||||||
|
: run-process ( obj -- )
|
||||||
|
>descriptor run-process* ;
|
||||||
|
|
||||||
|
: run-detached ( obj -- )
|
||||||
|
>descriptor H{ { +detached+ t } } union run-process* ;
|
||||||
|
|
||||||
|
HOOK: process-stream* io-backend ( desc -- stream )
|
||||||
|
|
||||||
|
: <process-stream> ( obj -- stream )
|
||||||
|
>descriptor process-stream* ;
|
||||||
|
|
||||||
USE-IF: unix? io.unix.launcher
|
USE-IF: unix? io.unix.launcher
|
||||||
USE-IF: windows? io.windows.launcher
|
USE-IF: windows? io.windows.launcher
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
|
! Copyright (C) 2007 Slava Pestov.
|
||||||
|
! See http://factorcode.org/license.txt for BSD license.
|
||||||
USING: io io.launcher io.unix.backend io.nonblocking
|
USING: io io.launcher io.unix.backend io.nonblocking
|
||||||
sequences kernel namespaces math system alien.c-types
|
sequences kernel namespaces math system alien.c-types
|
||||||
debugger continuations ;
|
debugger continuations arrays assocs combinators ;
|
||||||
|
|
||||||
IN: io.unix.launcher
|
IN: io.unix.launcher
|
||||||
|
|
||||||
! Search unix first
|
! Search unix first
|
||||||
|
@ -11,33 +11,66 @@ USE: unix
|
||||||
: with-fork ( child parent -- pid )
|
: with-fork ( child parent -- pid )
|
||||||
fork [ zero? -rot if ] keep ; inline
|
fork [ zero? -rot if ] keep ; inline
|
||||||
|
|
||||||
: prepare-execvp ( args -- cmd args )
|
: get-arguments ( -- seq )
|
||||||
|
+command+ get
|
||||||
|
[ "/bin/sh" "-c" rot 3array ] [ +arguments+ get ] if* ;
|
||||||
|
|
||||||
|
: execve? ( -- ? )
|
||||||
|
+environment+ get assoc-empty?
|
||||||
|
[ +environment-mode+ get replace-environment eq? ]
|
||||||
|
[ t ] if ;
|
||||||
|
|
||||||
|
: get-environment ( -- env )
|
||||||
|
+environment+ get
|
||||||
|
+environment-mode+ get {
|
||||||
|
{ prepend-environment [ os-envs union ] }
|
||||||
|
{ append-environment [ os-envs swap union ] }
|
||||||
|
{ replace-environment [ ] }
|
||||||
|
} case ;
|
||||||
|
|
||||||
|
: >null-term-array f add >c-void*-array ;
|
||||||
|
|
||||||
|
: prepare-execvp ( -- cmd args )
|
||||||
#! Doesn't free any memory, so we only call this word
|
#! Doesn't free any memory, so we only call this word
|
||||||
#! after forking.
|
#! after forking.
|
||||||
|
get-arguments
|
||||||
[ malloc-char-string ] map
|
[ malloc-char-string ] map
|
||||||
[ first ] keep
|
dup first swap >null-term-array ;
|
||||||
f add >c-void*-array ;
|
|
||||||
|
|
||||||
: (spawn-process) ( args -- )
|
: prepare-execve ( -- cmd args env )
|
||||||
[ prepare-execvp execvp ] catch 1 exit ;
|
#! Doesn't free any memory, so we only call this word
|
||||||
|
#! after forking.
|
||||||
|
prepare-execvp
|
||||||
|
get-environment
|
||||||
|
[ "=" swap 3append malloc-char-string ] { } assoc>map
|
||||||
|
>null-term-array ;
|
||||||
|
|
||||||
: spawn-process ( args -- pid )
|
: (spawn-process) ( -- )
|
||||||
[ (spawn-process) ] [ drop ] with-fork ;
|
[
|
||||||
|
execve? [
|
||||||
|
prepare-execve execve
|
||||||
|
] [
|
||||||
|
prepare-execvp execvp
|
||||||
|
] if io-error
|
||||||
|
] [ error. :c flush ] recover 1 exit ;
|
||||||
|
|
||||||
: wait-for-process ( pid -- )
|
: wait-for-process ( pid -- )
|
||||||
0 <int> 0 waitpid drop ;
|
0 <int> 0 waitpid drop ;
|
||||||
|
|
||||||
: shell-command ( string -- args )
|
: spawn-process ( -- pid )
|
||||||
{ "/bin/sh" "-c" } swap add ;
|
[ (spawn-process) ] [ ] with-fork ;
|
||||||
|
|
||||||
M: unix-io run-process ( string -- )
|
: spawn-detached ( -- )
|
||||||
shell-command spawn-process wait-for-process ;
|
[ spawn-process 0 exit ] [ ] with-fork wait-for-process ;
|
||||||
|
|
||||||
: detached-shell-command ( string -- args )
|
M: unix-io run-process* ( desc -- )
|
||||||
shell-command "&" add ;
|
[
|
||||||
|
+detached+ get [
|
||||||
M: unix-io run-detached ( string -- )
|
spawn-detached
|
||||||
detached-shell-command spawn-process wait-for-process ;
|
] [
|
||||||
|
spawn-process wait-for-process
|
||||||
|
] if
|
||||||
|
] with-descriptor ;
|
||||||
|
|
||||||
: open-pipe ( -- pair )
|
: open-pipe ( -- pair )
|
||||||
2 "int" <c-array> dup pipe zero?
|
2 "int" <c-array> dup pipe zero?
|
||||||
|
@ -47,13 +80,12 @@ M: unix-io run-detached ( string -- )
|
||||||
2dup first close second close
|
2dup first close second close
|
||||||
>r first 0 dup2 drop r> second 1 dup2 drop ;
|
>r first 0 dup2 drop r> second 1 dup2 drop ;
|
||||||
|
|
||||||
: spawn-process-stream ( args -- in out pid )
|
: spawn-process-stream ( -- in out pid )
|
||||||
open-pipe open-pipe [
|
open-pipe open-pipe [
|
||||||
setup-stdio-pipe
|
setup-stdio-pipe
|
||||||
(spawn-process)
|
(spawn-process)
|
||||||
] [
|
] [
|
||||||
2dup second close first close
|
2dup second close first close
|
||||||
rot drop
|
|
||||||
] with-fork >r first swap second r> ;
|
] with-fork >r first swap second r> ;
|
||||||
|
|
||||||
TUPLE: pipe-stream pid ;
|
TUPLE: pipe-stream pid ;
|
||||||
|
@ -66,5 +98,5 @@ M: pipe-stream stream-close
|
||||||
dup delegate stream-close
|
dup delegate stream-close
|
||||||
pipe-stream-pid wait-for-process ;
|
pipe-stream-pid wait-for-process ;
|
||||||
|
|
||||||
M: unix-io <process-stream>
|
M: unix-io process-stream*
|
||||||
shell-command spawn-process-stream <pipe-stream> ;
|
[ spawn-process-stream <pipe-stream> ] with-descriptor ;
|
||||||
|
|
Loading…
Reference in New Issue